In OpenSSL builds, declare STACK for datatypes ...
[openssl.git] / crypto / cms / cms_ess.c
1 /*
2  * Copyright 2008-2019 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 "cms_local.h"
19 #include "crypto/ess.h"
20 #include "crypto/cms.h"
21
22 DEFINE_STACK_OF(GENERAL_NAMES)
23 DEFINE_STACK_OF(CMS_SignerInfo)
24
25 IMPLEMENT_ASN1_FUNCTIONS(CMS_ReceiptRequest)
26
27 /* ESS services */
28
29 int CMS_get1_ReceiptRequest(CMS_SignerInfo *si, CMS_ReceiptRequest **prr)
30 {
31     ASN1_STRING *str;
32     CMS_ReceiptRequest *rr = NULL;
33     if (prr)
34         *prr = NULL;
35     str = CMS_signed_get0_data_by_OBJ(si,
36                                       OBJ_nid2obj
37                                       (NID_id_smime_aa_receiptRequest), -3,
38                                       V_ASN1_SEQUENCE);
39     if (!str)
40         return 0;
41
42     rr = ASN1_item_unpack(str, ASN1_ITEM_rptr(CMS_ReceiptRequest));
43     if (!rr)
44         return -1;
45     if (prr)
46         *prr = rr;
47     else
48         CMS_ReceiptRequest_free(rr);
49     return 1;
50 }
51
52 CMS_ReceiptRequest *CMS_ReceiptRequest_create0(unsigned char *id, int idlen,
53                                                int allorfirst,
54                                                STACK_OF(GENERAL_NAMES)
55                                                *receiptList, STACK_OF(GENERAL_NAMES)
56                                                *receiptsTo)
57 {
58     CMS_ReceiptRequest *rr = NULL;
59
60     rr = CMS_ReceiptRequest_new();
61     if (rr == NULL)
62         goto merr;
63     if (id)
64         ASN1_STRING_set0(rr->signedContentIdentifier, id, idlen);
65     else {
66         if (!ASN1_STRING_set(rr->signedContentIdentifier, NULL, 32))
67             goto merr;
68         if (RAND_bytes(rr->signedContentIdentifier->data, 32) <= 0)
69             goto err;
70     }
71
72     sk_GENERAL_NAMES_pop_free(rr->receiptsTo, GENERAL_NAMES_free);
73     rr->receiptsTo = receiptsTo;
74
75     if (receiptList) {
76         rr->receiptsFrom->type = 1;
77         rr->receiptsFrom->d.receiptList = receiptList;
78     } else {
79         rr->receiptsFrom->type = 0;
80         rr->receiptsFrom->d.allOrFirstTier = allorfirst;
81     }
82
83     return rr;
84
85  merr:
86     CMSerr(CMS_F_CMS_RECEIPTREQUEST_CREATE0, ERR_R_MALLOC_FAILURE);
87
88  err:
89     CMS_ReceiptRequest_free(rr);
90     return NULL;
91
92 }
93
94 int CMS_add1_ReceiptRequest(CMS_SignerInfo *si, CMS_ReceiptRequest *rr)
95 {
96     unsigned char *rrder = NULL;
97     int rrderlen, r = 0;
98
99     rrderlen = i2d_CMS_ReceiptRequest(rr, &rrder);
100     if (rrderlen < 0)
101         goto merr;
102
103     if (!CMS_signed_add1_attr_by_NID(si, NID_id_smime_aa_receiptRequest,
104                                      V_ASN1_SEQUENCE, rrder, rrderlen))
105         goto merr;
106
107     r = 1;
108
109  merr:
110     if (!r)
111         CMSerr(CMS_F_CMS_ADD1_RECEIPTREQUEST, ERR_R_MALLOC_FAILURE);
112
113     OPENSSL_free(rrder);
114
115     return r;
116
117 }
118
119 void CMS_ReceiptRequest_get0_values(CMS_ReceiptRequest *rr,
120                                     ASN1_STRING **pcid,
121                                     int *pallorfirst,
122                                     STACK_OF(GENERAL_NAMES) **plist,
123                                     STACK_OF(GENERAL_NAMES) **prto)
124 {
125     if (pcid)
126         *pcid = rr->signedContentIdentifier;
127     if (rr->receiptsFrom->type == 0) {
128         if (pallorfirst)
129             *pallorfirst = (int)rr->receiptsFrom->d.allOrFirstTier;
130         if (plist)
131             *plist = NULL;
132     } else {
133         if (pallorfirst)
134             *pallorfirst = -1;
135         if (plist)
136             *plist = rr->receiptsFrom->d.receiptList;
137     }
138     if (prto)
139         *prto = rr->receiptsTo;
140 }
141
142 /* Digest a SignerInfo structure for msgSigDigest attribute processing */
143
144 static int cms_msgSigDigest(CMS_SignerInfo *si,
145                             unsigned char *dig, unsigned int *diglen)
146 {
147     const EVP_MD *md;
148     md = EVP_get_digestbyobj(si->digestAlgorithm->algorithm);
149     if (md == NULL)
150         return 0;
151     if (!ASN1_item_digest(ASN1_ITEM_rptr(CMS_Attributes_Verify), md,
152                           si->signedAttrs, dig, diglen))
153         return 0;
154     return 1;
155 }
156
157 /* Add a msgSigDigest attribute to a SignerInfo */
158
159 int cms_msgSigDigest_add1(CMS_SignerInfo *dest, CMS_SignerInfo *src)
160 {
161     unsigned char dig[EVP_MAX_MD_SIZE];
162     unsigned int diglen;
163     if (!cms_msgSigDigest(src, dig, &diglen)) {
164         CMSerr(CMS_F_CMS_MSGSIGDIGEST_ADD1, CMS_R_MSGSIGDIGEST_ERROR);
165         return 0;
166     }
167     if (!CMS_signed_add1_attr_by_NID(dest, NID_id_smime_aa_msgSigDigest,
168                                      V_ASN1_OCTET_STRING, dig, diglen)) {
169         CMSerr(CMS_F_CMS_MSGSIGDIGEST_ADD1, ERR_R_MALLOC_FAILURE);
170         return 0;
171     }
172     return 1;
173 }
174
175 /* Verify signed receipt after it has already passed normal CMS verify */
176
177 int cms_Receipt_verify(CMS_ContentInfo *cms, CMS_ContentInfo *req_cms)
178 {
179     int r = 0, i;
180     CMS_ReceiptRequest *rr = NULL;
181     CMS_Receipt *rct = NULL;
182     STACK_OF(CMS_SignerInfo) *sis, *osis;
183     CMS_SignerInfo *si, *osi = NULL;
184     ASN1_OCTET_STRING *msig, **pcont;
185     ASN1_OBJECT *octype;
186     unsigned char dig[EVP_MAX_MD_SIZE];
187     unsigned int diglen;
188
189     /* Get SignerInfos, also checks SignedData content type */
190     osis = CMS_get0_SignerInfos(req_cms);
191     sis = CMS_get0_SignerInfos(cms);
192     if (!osis || !sis)
193         goto err;
194
195     if (sk_CMS_SignerInfo_num(sis) != 1) {
196         CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_NEED_ONE_SIGNER);
197         goto err;
198     }
199
200     /* Check receipt content type */
201     if (OBJ_obj2nid(CMS_get0_eContentType(cms)) != NID_id_smime_ct_receipt) {
202         CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_NOT_A_SIGNED_RECEIPT);
203         goto err;
204     }
205
206     /* Extract and decode receipt content */
207     pcont = CMS_get0_content(cms);
208     if (pcont == NULL || *pcont == NULL) {
209         CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_NO_CONTENT);
210         goto err;
211     }
212
213     rct = ASN1_item_unpack(*pcont, ASN1_ITEM_rptr(CMS_Receipt));
214
215     if (!rct) {
216         CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_RECEIPT_DECODE_ERROR);
217         goto err;
218     }
219
220     /* Locate original request */
221
222     for (i = 0; i < sk_CMS_SignerInfo_num(osis); i++) {
223         osi = sk_CMS_SignerInfo_value(osis, i);
224         if (!ASN1_STRING_cmp(osi->signature, rct->originatorSignatureValue))
225             break;
226     }
227
228     if (i == sk_CMS_SignerInfo_num(osis)) {
229         CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_NO_MATCHING_SIGNATURE);
230         goto err;
231     }
232
233     si = sk_CMS_SignerInfo_value(sis, 0);
234
235     /* Get msgSigDigest value and compare */
236
237     msig = CMS_signed_get0_data_by_OBJ(si,
238                                        OBJ_nid2obj
239                                        (NID_id_smime_aa_msgSigDigest), -3,
240                                        V_ASN1_OCTET_STRING);
241
242     if (!msig) {
243         CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_NO_MSGSIGDIGEST);
244         goto err;
245     }
246
247     if (!cms_msgSigDigest(osi, dig, &diglen)) {
248         CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_MSGSIGDIGEST_ERROR);
249         goto err;
250     }
251
252     if (diglen != (unsigned int)msig->length) {
253         CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_MSGSIGDIGEST_WRONG_LENGTH);
254         goto err;
255     }
256
257     if (memcmp(dig, msig->data, diglen)) {
258         CMSerr(CMS_F_CMS_RECEIPT_VERIFY,
259                CMS_R_MSGSIGDIGEST_VERIFICATION_FAILURE);
260         goto err;
261     }
262
263     /* Compare content types */
264
265     octype = CMS_signed_get0_data_by_OBJ(osi,
266                                          OBJ_nid2obj(NID_pkcs9_contentType),
267                                          -3, V_ASN1_OBJECT);
268     if (!octype) {
269         CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_NO_CONTENT_TYPE);
270         goto err;
271     }
272
273     /* Compare details in receipt request */
274
275     if (OBJ_cmp(octype, rct->contentType)) {
276         CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_CONTENT_TYPE_MISMATCH);
277         goto err;
278     }
279
280     /* Get original receipt request details */
281
282     if (CMS_get1_ReceiptRequest(osi, &rr) <= 0) {
283         CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_NO_RECEIPT_REQUEST);
284         goto err;
285     }
286
287     if (ASN1_STRING_cmp(rr->signedContentIdentifier,
288                         rct->signedContentIdentifier)) {
289         CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_CONTENTIDENTIFIER_MISMATCH);
290         goto err;
291     }
292
293     r = 1;
294
295  err:
296     CMS_ReceiptRequest_free(rr);
297     M_ASN1_free_of(rct, CMS_Receipt);
298     return r;
299
300 }
301
302 /*
303  * Encode a Receipt into an OCTET STRING read for including into content of a
304  * SignedData ContentInfo.
305  */
306
307 ASN1_OCTET_STRING *cms_encode_Receipt(CMS_SignerInfo *si)
308 {
309     CMS_Receipt rct;
310     CMS_ReceiptRequest *rr = NULL;
311     ASN1_OBJECT *ctype;
312     ASN1_OCTET_STRING *os = NULL;
313
314     /* Get original receipt request */
315
316     /* Get original receipt request details */
317
318     if (CMS_get1_ReceiptRequest(si, &rr) <= 0) {
319         CMSerr(CMS_F_CMS_ENCODE_RECEIPT, CMS_R_NO_RECEIPT_REQUEST);
320         goto err;
321     }
322
323     /* Get original content type */
324
325     ctype = CMS_signed_get0_data_by_OBJ(si,
326                                         OBJ_nid2obj(NID_pkcs9_contentType),
327                                         -3, V_ASN1_OBJECT);
328     if (!ctype) {
329         CMSerr(CMS_F_CMS_ENCODE_RECEIPT, CMS_R_NO_CONTENT_TYPE);
330         goto err;
331     }
332
333     rct.version = 1;
334     rct.contentType = ctype;
335     rct.signedContentIdentifier = rr->signedContentIdentifier;
336     rct.originatorSignatureValue = si->signature;
337
338     os = ASN1_item_pack(&rct, ASN1_ITEM_rptr(CMS_Receipt), NULL);
339
340  err:
341     CMS_ReceiptRequest_free(rr);
342     return os;
343 }
344
345 /*
346  * Add signer certificate's V2 digest |sc| to a SignerInfo structure |si|
347  */
348
349 int cms_add1_signing_cert_v2(CMS_SignerInfo *si, ESS_SIGNING_CERT_V2 *sc)
350 {
351     ASN1_STRING *seq = NULL;
352     unsigned char *p, *pp;
353     int len;
354
355     /* Add SigningCertificateV2 signed attribute to the signer info. */
356     len = i2d_ESS_SIGNING_CERT_V2(sc, NULL);
357     if ((pp = OPENSSL_malloc(len)) == NULL)
358         goto err;
359     p = pp;
360     i2d_ESS_SIGNING_CERT_V2(sc, &p);
361     if (!(seq = ASN1_STRING_new()) || !ASN1_STRING_set(seq, pp, len))
362         goto err;
363     OPENSSL_free(pp);
364     pp = NULL;
365     if (!CMS_signed_add1_attr_by_NID(si, NID_id_smime_aa_signingCertificateV2,
366                                      V_ASN1_SEQUENCE, seq, -1))
367         goto err;
368     ASN1_STRING_free(seq);
369     return 1;
370  err:
371     CMSerr(CMS_F_CMS_ADD1_SIGNING_CERT_V2, ERR_R_MALLOC_FAILURE);
372     ASN1_STRING_free(seq);
373     OPENSSL_free(pp);
374     return 0;
375 }
376
377 /*
378  * Add signer certificate's digest |sc| to a SignerInfo structure |si|
379  */
380
381 int cms_add1_signing_cert(CMS_SignerInfo *si, ESS_SIGNING_CERT *sc)
382 {
383     ASN1_STRING *seq = NULL;
384     unsigned char *p, *pp;
385     int len;
386
387     /* Add SigningCertificate signed attribute to the signer info. */
388     len = i2d_ESS_SIGNING_CERT(sc, NULL);
389     if ((pp = OPENSSL_malloc(len)) == NULL)
390         goto err;
391     p = pp;
392     i2d_ESS_SIGNING_CERT(sc, &p);
393     if (!(seq = ASN1_STRING_new()) || !ASN1_STRING_set(seq, pp, len))
394         goto err;
395     OPENSSL_free(pp);
396     pp = NULL;
397     if (!CMS_signed_add1_attr_by_NID(si, NID_id_smime_aa_signingCertificate,
398                                      V_ASN1_SEQUENCE, seq, -1))
399         goto err;
400     ASN1_STRING_free(seq);
401     return 1;
402  err:
403     CMSerr(CMS_F_CMS_ADD1_SIGNING_CERT, ERR_R_MALLOC_FAILURE);
404     ASN1_STRING_free(seq);
405     OPENSSL_free(pp);
406     return 0;
407 }