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