Update copyright year
[openssl.git] / crypto / cms / cms_ess.c
1 /*
2  * Copyright 2008-2021 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include "internal/cryptlib.h"
11 #include <openssl/asn1t.h>
12 #include <openssl/pem.h>
13 #include <openssl/rand.h>
14 #include <openssl/x509v3.h>
15 #include <openssl/err.h>
16 #include <openssl/cms.h>
17 #include <openssl/ess.h>
18 #include "crypto/ess.h"
19 #include "crypto/cms.h"
20 #include "crypto/x509.h"
21 #include "cms_local.h"
22
23 IMPLEMENT_ASN1_FUNCTIONS(CMS_ReceiptRequest)
24
25 /* ESS services */
26
27 int CMS_get1_ReceiptRequest(CMS_SignerInfo *si, CMS_ReceiptRequest **prr)
28 {
29     ASN1_STRING *str;
30     CMS_ReceiptRequest *rr;
31     ASN1_OBJECT *obj = OBJ_nid2obj(NID_id_smime_aa_receiptRequest);
32
33     if (prr != NULL)
34         *prr = NULL;
35     str = CMS_signed_get0_data_by_OBJ(si, obj, -3, V_ASN1_SEQUENCE);
36     if (str == NULL)
37         return 0;
38
39     rr = ASN1_item_unpack(str, ASN1_ITEM_rptr(CMS_ReceiptRequest));
40     if (rr == NULL)
41         return -1;
42     if (prr != NULL)
43         *prr = rr;
44     else
45         CMS_ReceiptRequest_free(rr);
46     return 1;
47 }
48
49 /*
50     First, get the ESS_SIGNING_CERT(V2) signed attribute from |si|.
51     Then check matching of each cert of trust |chain| with one of 
52     the |cert_ids|(Hash+IssuerID) list from this ESS_SIGNING_CERT.
53     Derived from ts_check_signing_certs()
54 */
55 int ess_check_signing_certs(CMS_SignerInfo *si, STACK_OF(X509) *chain)
56 {
57     ESS_SIGNING_CERT *ss = NULL;
58     ESS_SIGNING_CERT_V2 *ssv2 = NULL;
59     X509 *cert;
60     int i = 0, ret = 0;
61
62     if (cms_signerinfo_get_signing_cert(si, &ss) > 0 && ss->cert_ids != NULL) {
63         STACK_OF(ESS_CERT_ID) *cert_ids = ss->cert_ids;
64
65         cert = sk_X509_value(chain, 0);
66         if (ess_find_cert(cert_ids, cert) != 0)
67             goto err;
68
69         /*
70          * Check the other certificates of the chain.
71          * Fail if no signing certificate ids found for each certificate.
72          */
73         if (sk_ESS_CERT_ID_num(cert_ids) > 1) {
74             /* for each chain cert, try to find its cert id */
75             for (i = 1; i < sk_X509_num(chain); ++i) {
76                 cert = sk_X509_value(chain, i);
77                 if (ess_find_cert(cert_ids, cert) < 0)
78                     goto err;
79             }
80         }
81     } else if (cms_signerinfo_get_signing_cert_v2(si, &ssv2) > 0
82                    && ssv2->cert_ids!= NULL) {
83         STACK_OF(ESS_CERT_ID_V2) *cert_ids_v2 = ssv2->cert_ids;
84
85         cert = sk_X509_value(chain, 0);
86         if (ess_find_cert_v2(cert_ids_v2, cert) != 0)
87             goto err;
88
89         /*
90          * Check the other certificates of the chain.
91          * Fail if no signing certificate ids found for each certificate.
92          */
93         if (sk_ESS_CERT_ID_V2_num(cert_ids_v2) > 1) {
94             /* for each chain cert, try to find its cert id */
95             for (i = 1; i < sk_X509_num(chain); ++i) {
96                 cert = sk_X509_value(chain, i);
97                 if (ess_find_cert_v2(cert_ids_v2, cert) < 0)
98                     goto err;
99             }
100         }
101     } else {
102         ERR_raise(ERR_LIB_CMS, CMS_R_ESS_NO_SIGNING_CERTID_ATTRIBUTE);
103         return 0;
104     }
105     ret = 1;
106  err:
107     if (!ret)
108         ERR_raise(ERR_LIB_CMS, CMS_R_ESS_SIGNING_CERTID_MISMATCH_ERROR);
109
110     ESS_SIGNING_CERT_free(ss);
111     ESS_SIGNING_CERT_V2_free(ssv2);
112     return ret;
113 }
114
115 CMS_ReceiptRequest *CMS_ReceiptRequest_create0_ex(
116     unsigned char *id, int idlen, int allorfirst,
117     STACK_OF(GENERAL_NAMES) *receiptList, STACK_OF(GENERAL_NAMES) *receiptsTo,
118     OSSL_LIB_CTX *libctx, const char *propq)
119 {
120     CMS_ReceiptRequest *rr;
121
122     rr = CMS_ReceiptRequest_new();
123     if (rr == NULL)
124         goto merr;
125     if (id)
126         ASN1_STRING_set0(rr->signedContentIdentifier, id, idlen);
127     else {
128         if (!ASN1_STRING_set(rr->signedContentIdentifier, NULL, 32))
129             goto merr;
130         if (RAND_bytes_ex(libctx, rr->signedContentIdentifier->data, 32) <= 0)
131             goto err;
132     }
133
134     sk_GENERAL_NAMES_pop_free(rr->receiptsTo, GENERAL_NAMES_free);
135     rr->receiptsTo = receiptsTo;
136
137     if (receiptList != NULL) {
138         rr->receiptsFrom->type = 1;
139         rr->receiptsFrom->d.receiptList = receiptList;
140     } else {
141         rr->receiptsFrom->type = 0;
142         rr->receiptsFrom->d.allOrFirstTier = allorfirst;
143     }
144
145     return rr;
146
147  merr:
148     ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
149
150  err:
151     CMS_ReceiptRequest_free(rr);
152     return NULL;
153
154 }
155
156 CMS_ReceiptRequest *CMS_ReceiptRequest_create0(
157     unsigned char *id, int idlen, int allorfirst,
158     STACK_OF(GENERAL_NAMES) *receiptList, STACK_OF(GENERAL_NAMES) *receiptsTo)
159 {
160     return CMS_ReceiptRequest_create0_ex(id, idlen, allorfirst, receiptList,
161                                          receiptsTo, NULL, NULL);
162 }
163
164 int CMS_add1_ReceiptRequest(CMS_SignerInfo *si, CMS_ReceiptRequest *rr)
165 {
166     unsigned char *rrder = NULL;
167     int rrderlen, r = 0;
168
169     rrderlen = i2d_CMS_ReceiptRequest(rr, &rrder);
170     if (rrderlen < 0)
171         goto merr;
172
173     if (!CMS_signed_add1_attr_by_NID(si, NID_id_smime_aa_receiptRequest,
174                                      V_ASN1_SEQUENCE, rrder, rrderlen))
175         goto merr;
176
177     r = 1;
178
179  merr:
180     if (!r)
181         ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
182
183     OPENSSL_free(rrder);
184
185     return r;
186
187 }
188
189 void CMS_ReceiptRequest_get0_values(CMS_ReceiptRequest *rr,
190                                     ASN1_STRING **pcid,
191                                     int *pallorfirst,
192                                     STACK_OF(GENERAL_NAMES) **plist,
193                                     STACK_OF(GENERAL_NAMES) **prto)
194 {
195     if (pcid != NULL)
196         *pcid = rr->signedContentIdentifier;
197     if (rr->receiptsFrom->type == 0) {
198         if (pallorfirst != NULL)
199             *pallorfirst = (int)rr->receiptsFrom->d.allOrFirstTier;
200         if (plist != NULL)
201             *plist = NULL;
202     } else {
203         if (pallorfirst != NULL)
204             *pallorfirst = -1;
205         if (plist != NULL)
206             *plist = rr->receiptsFrom->d.receiptList;
207     }
208     if (prto != NULL)
209         *prto = rr->receiptsTo;
210 }
211
212 /* Digest a SignerInfo structure for msgSigDigest attribute processing */
213
214 static int cms_msgSigDigest(CMS_SignerInfo *si,
215                             unsigned char *dig, unsigned int *diglen)
216 {
217     const EVP_MD *md = EVP_get_digestbyobj(si->digestAlgorithm->algorithm);
218
219     if (md == NULL)
220         return 0;
221     if (!asn1_item_digest_ex(ASN1_ITEM_rptr(CMS_Attributes_Verify), md,
222                              si->signedAttrs, dig, diglen,
223                              cms_ctx_get0_libctx(si->cms_ctx),
224                              cms_ctx_get0_propq(si->cms_ctx)))
225         return 0;
226     return 1;
227 }
228
229 /* Add a msgSigDigest attribute to a SignerInfo */
230
231 int cms_msgSigDigest_add1(CMS_SignerInfo *dest, CMS_SignerInfo *src)
232 {
233     unsigned char dig[EVP_MAX_MD_SIZE];
234     unsigned int diglen;
235
236     if (!cms_msgSigDigest(src, dig, &diglen)) {
237         ERR_raise(ERR_LIB_CMS, CMS_R_MSGSIGDIGEST_ERROR);
238         return 0;
239     }
240     if (!CMS_signed_add1_attr_by_NID(dest, NID_id_smime_aa_msgSigDigest,
241                                      V_ASN1_OCTET_STRING, dig, diglen)) {
242         ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
243         return 0;
244     }
245     return 1;
246 }
247
248 /* Verify signed receipt after it has already passed normal CMS verify */
249
250 int cms_Receipt_verify(CMS_ContentInfo *cms, CMS_ContentInfo *req_cms)
251 {
252     int r = 0, i;
253     CMS_ReceiptRequest *rr = NULL;
254     CMS_Receipt *rct = NULL;
255     STACK_OF(CMS_SignerInfo) *sis, *osis;
256     CMS_SignerInfo *si, *osi = NULL;
257     ASN1_OCTET_STRING *msig, **pcont;
258     ASN1_OBJECT *octype;
259     unsigned char dig[EVP_MAX_MD_SIZE];
260     unsigned int diglen;
261
262     /* Get SignerInfos, also checks SignedData content type */
263     osis = CMS_get0_SignerInfos(req_cms);
264     sis = CMS_get0_SignerInfos(cms);
265     if (!osis || !sis)
266         goto err;
267
268     if (sk_CMS_SignerInfo_num(sis) != 1) {
269         ERR_raise(ERR_LIB_CMS, CMS_R_NEED_ONE_SIGNER);
270         goto err;
271     }
272
273     /* Check receipt content type */
274     if (OBJ_obj2nid(CMS_get0_eContentType(cms)) != NID_id_smime_ct_receipt) {
275         ERR_raise(ERR_LIB_CMS, CMS_R_NOT_A_SIGNED_RECEIPT);
276         goto err;
277     }
278
279     /* Extract and decode receipt content */
280     pcont = CMS_get0_content(cms);
281     if (pcont == NULL || *pcont == NULL) {
282         ERR_raise(ERR_LIB_CMS, CMS_R_NO_CONTENT);
283         goto err;
284     }
285
286     rct = ASN1_item_unpack(*pcont, ASN1_ITEM_rptr(CMS_Receipt));
287
288     if (!rct) {
289         ERR_raise(ERR_LIB_CMS, CMS_R_RECEIPT_DECODE_ERROR);
290         goto err;
291     }
292
293     /* Locate original request */
294
295     for (i = 0; i < sk_CMS_SignerInfo_num(osis); i++) {
296         osi = sk_CMS_SignerInfo_value(osis, i);
297         if (!ASN1_STRING_cmp(osi->signature, rct->originatorSignatureValue))
298             break;
299     }
300
301     if (i == sk_CMS_SignerInfo_num(osis)) {
302         ERR_raise(ERR_LIB_CMS, CMS_R_NO_MATCHING_SIGNATURE);
303         goto err;
304     }
305
306     si = sk_CMS_SignerInfo_value(sis, 0);
307
308     /* Get msgSigDigest value and compare */
309
310     msig = CMS_signed_get0_data_by_OBJ(si,
311                                        OBJ_nid2obj
312                                        (NID_id_smime_aa_msgSigDigest), -3,
313                                        V_ASN1_OCTET_STRING);
314
315     if (!msig) {
316         ERR_raise(ERR_LIB_CMS, CMS_R_NO_MSGSIGDIGEST);
317         goto err;
318     }
319
320     if (!cms_msgSigDigest(osi, dig, &diglen)) {
321         ERR_raise(ERR_LIB_CMS, CMS_R_MSGSIGDIGEST_ERROR);
322         goto err;
323     }
324
325     if (diglen != (unsigned int)msig->length) {
326         ERR_raise(ERR_LIB_CMS, CMS_R_MSGSIGDIGEST_WRONG_LENGTH);
327         goto err;
328     }
329
330     if (memcmp(dig, msig->data, diglen)) {
331         ERR_raise(ERR_LIB_CMS, CMS_R_MSGSIGDIGEST_VERIFICATION_FAILURE);
332         goto err;
333     }
334
335     /* Compare content types */
336
337     octype = CMS_signed_get0_data_by_OBJ(osi,
338                                          OBJ_nid2obj(NID_pkcs9_contentType),
339                                          -3, V_ASN1_OBJECT);
340     if (!octype) {
341         ERR_raise(ERR_LIB_CMS, CMS_R_NO_CONTENT_TYPE);
342         goto err;
343     }
344
345     /* Compare details in receipt request */
346
347     if (OBJ_cmp(octype, rct->contentType)) {
348         ERR_raise(ERR_LIB_CMS, CMS_R_CONTENT_TYPE_MISMATCH);
349         goto err;
350     }
351
352     /* Get original receipt request details */
353
354     if (CMS_get1_ReceiptRequest(osi, &rr) <= 0) {
355         ERR_raise(ERR_LIB_CMS, CMS_R_NO_RECEIPT_REQUEST);
356         goto err;
357     }
358
359     if (ASN1_STRING_cmp(rr->signedContentIdentifier,
360                         rct->signedContentIdentifier)) {
361         ERR_raise(ERR_LIB_CMS, CMS_R_CONTENTIDENTIFIER_MISMATCH);
362         goto err;
363     }
364
365     r = 1;
366
367  err:
368     CMS_ReceiptRequest_free(rr);
369     M_ASN1_free_of(rct, CMS_Receipt);
370     return r;
371
372 }
373
374 /*
375  * Encode a Receipt into an OCTET STRING read for including into content of a
376  * SignedData ContentInfo.
377  */
378
379 ASN1_OCTET_STRING *cms_encode_Receipt(CMS_SignerInfo *si)
380 {
381     CMS_Receipt rct;
382     CMS_ReceiptRequest *rr = NULL;
383     ASN1_OBJECT *ctype;
384     ASN1_OCTET_STRING *os = NULL;
385
386     /* Get original receipt request */
387
388     /* Get original receipt request details */
389
390     if (CMS_get1_ReceiptRequest(si, &rr) <= 0) {
391         ERR_raise(ERR_LIB_CMS, CMS_R_NO_RECEIPT_REQUEST);
392         goto err;
393     }
394
395     /* Get original content type */
396
397     ctype = CMS_signed_get0_data_by_OBJ(si,
398                                         OBJ_nid2obj(NID_pkcs9_contentType),
399                                         -3, V_ASN1_OBJECT);
400     if (!ctype) {
401         ERR_raise(ERR_LIB_CMS, CMS_R_NO_CONTENT_TYPE);
402         goto err;
403     }
404
405     rct.version = 1;
406     rct.contentType = ctype;
407     rct.signedContentIdentifier = rr->signedContentIdentifier;
408     rct.originatorSignatureValue = si->signature;
409
410     os = ASN1_item_pack(&rct, ASN1_ITEM_rptr(CMS_Receipt), NULL);
411
412  err:
413     CMS_ReceiptRequest_free(rr);
414     return os;
415 }
416
417 /*
418  * Add signer certificate's V2 digest |sc| to a SignerInfo structure |si|
419  */
420
421 int cms_add1_signing_cert_v2(CMS_SignerInfo *si, ESS_SIGNING_CERT_V2 *sc)
422 {
423     ASN1_STRING *seq = NULL;
424     unsigned char *p, *pp = NULL;
425     int len;
426
427     /* Add SigningCertificateV2 signed attribute to the signer info. */
428     len = i2d_ESS_SIGNING_CERT_V2(sc, NULL);
429     if (len <= 0 || (pp = OPENSSL_malloc(len)) == NULL)
430         goto err;
431     p = pp;
432     i2d_ESS_SIGNING_CERT_V2(sc, &p);
433     if (!(seq = ASN1_STRING_new()) || !ASN1_STRING_set(seq, pp, len))
434         goto err;
435     OPENSSL_free(pp);
436     pp = NULL;
437     if (!CMS_signed_add1_attr_by_NID(si, NID_id_smime_aa_signingCertificateV2,
438                                      V_ASN1_SEQUENCE, seq, -1))
439         goto err;
440     ASN1_STRING_free(seq);
441     return 1;
442  err:
443     ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
444     ASN1_STRING_free(seq);
445     OPENSSL_free(pp);
446     return 0;
447 }
448
449 /*
450  * Add signer certificate's digest |sc| to a SignerInfo structure |si|
451  */
452
453 int cms_add1_signing_cert(CMS_SignerInfo *si, ESS_SIGNING_CERT *sc)
454 {
455     ASN1_STRING *seq = NULL;
456     unsigned char *p, *pp = NULL;
457     int len;
458
459     /* Add SigningCertificate signed attribute to the signer info. */
460     len = i2d_ESS_SIGNING_CERT(sc, NULL);
461     if (len <= 0 || (pp = OPENSSL_malloc(len)) == NULL)
462         goto err;
463     p = pp;
464     i2d_ESS_SIGNING_CERT(sc, &p);
465     if (!(seq = ASN1_STRING_new()) || !ASN1_STRING_set(seq, pp, len))
466         goto err;
467     OPENSSL_free(pp);
468     pp = NULL;
469     if (!CMS_signed_add1_attr_by_NID(si, NID_id_smime_aa_signingCertificate,
470                                      V_ASN1_SEQUENCE, seq, -1))
471         goto err;
472     ASN1_STRING_free(seq);
473     return 1;
474  err:
475     ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
476     ASN1_STRING_free(seq);
477     OPENSSL_free(pp);
478     return 0;
479 }