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