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