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