free null cleanup finale
[openssl.git] / crypto / cms / cms_ess.c
1 /* crypto/cms/cms_ess.c */
2 /*
3  * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
4  * project.
5  */
6 /* ====================================================================
7  * Copyright (c) 2008 The OpenSSL Project.  All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in
18  *    the documentation and/or other materials provided with the
19  *    distribution.
20  *
21  * 3. All advertising materials mentioning features or use of this
22  *    software must display the following acknowledgment:
23  *    "This product includes software developed by the OpenSSL Project
24  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25  *
26  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27  *    endorse or promote products derived from this software without
28  *    prior written permission. For written permission, please contact
29  *    licensing@OpenSSL.org.
30  *
31  * 5. Products derived from this software may not be called "OpenSSL"
32  *    nor may "OpenSSL" appear in their names without prior written
33  *    permission of the OpenSSL Project.
34  *
35  * 6. Redistributions of any form whatsoever must retain the following
36  *    acknowledgment:
37  *    "This product includes software developed by the OpenSSL Project
38  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39  *
40  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
44  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51  * OF THE POSSIBILITY OF SUCH DAMAGE.
52  * ====================================================================
53  */
54
55 #include "cryptlib.h"
56 #include <openssl/asn1t.h>
57 #include <openssl/pem.h>
58 #include <openssl/rand.h>
59 #include <openssl/x509v3.h>
60 #include <openssl/err.h>
61 #include <openssl/cms.h>
62 #include "cms_lcl.h"
63
64 DECLARE_ASN1_ITEM(CMS_ReceiptRequest)
65 DECLARE_ASN1_ITEM(CMS_Receipt)
66
67 IMPLEMENT_ASN1_FUNCTIONS(CMS_ReceiptRequest)
68
69 /* ESS services: for now just Signed Receipt related */
70
71 int CMS_get1_ReceiptRequest(CMS_SignerInfo *si, CMS_ReceiptRequest **prr)
72 {
73     ASN1_STRING *str;
74     CMS_ReceiptRequest *rr = NULL;
75     if (prr)
76         *prr = NULL;
77     str = CMS_signed_get0_data_by_OBJ(si,
78                                       OBJ_nid2obj
79                                       (NID_id_smime_aa_receiptRequest), -3,
80                                       V_ASN1_SEQUENCE);
81     if (!str)
82         return 0;
83
84     rr = ASN1_item_unpack(str, ASN1_ITEM_rptr(CMS_ReceiptRequest));
85     if (!rr)
86         return -1;
87     if (prr)
88         *prr = rr;
89     else
90         CMS_ReceiptRequest_free(rr);
91     return 1;
92 }
93
94 CMS_ReceiptRequest *CMS_ReceiptRequest_create0(unsigned char *id, int idlen,
95                                                int allorfirst,
96                                                STACK_OF(GENERAL_NAMES)
97                                                *receiptList, STACK_OF(GENERAL_NAMES)
98                                                *receiptsTo)
99 {
100     CMS_ReceiptRequest *rr = NULL;
101
102     rr = CMS_ReceiptRequest_new();
103     if (!rr)
104         goto merr;
105     if (id)
106         ASN1_STRING_set0(rr->signedContentIdentifier, id, idlen);
107     else {
108         if (!ASN1_STRING_set(rr->signedContentIdentifier, NULL, 32))
109             goto merr;
110         if (RAND_bytes(rr->signedContentIdentifier->data, 32) <= 0)
111             goto err;
112     }
113
114     sk_GENERAL_NAMES_pop_free(rr->receiptsTo, GENERAL_NAMES_free);
115     rr->receiptsTo = receiptsTo;
116
117     if (receiptList) {
118         rr->receiptsFrom->type = 1;
119         rr->receiptsFrom->d.receiptList = receiptList;
120     } else {
121         rr->receiptsFrom->type = 0;
122         rr->receiptsFrom->d.allOrFirstTier = allorfirst;
123     }
124
125     return rr;
126
127  merr:
128     CMSerr(CMS_F_CMS_RECEIPTREQUEST_CREATE0, ERR_R_MALLOC_FAILURE);
129
130  err:
131     if (rr)
132         CMS_ReceiptRequest_free(rr);
133
134     return NULL;
135
136 }
137
138 int CMS_add1_ReceiptRequest(CMS_SignerInfo *si, CMS_ReceiptRequest *rr)
139 {
140     unsigned char *rrder = NULL;
141     int rrderlen, r = 0;
142
143     rrderlen = i2d_CMS_ReceiptRequest(rr, &rrder);
144     if (rrderlen < 0)
145         goto merr;
146
147     if (!CMS_signed_add1_attr_by_NID(si, NID_id_smime_aa_receiptRequest,
148                                      V_ASN1_SEQUENCE, rrder, rrderlen))
149         goto merr;
150
151     r = 1;
152
153  merr:
154     if (!r)
155         CMSerr(CMS_F_CMS_ADD1_RECEIPTREQUEST, ERR_R_MALLOC_FAILURE);
156
157     OPENSSL_free(rrder);
158
159     return r;
160
161 }
162
163 void CMS_ReceiptRequest_get0_values(CMS_ReceiptRequest *rr,
164                                     ASN1_STRING **pcid,
165                                     int *pallorfirst,
166                                     STACK_OF(GENERAL_NAMES) **plist,
167                                     STACK_OF(GENERAL_NAMES) **prto)
168 {
169     if (pcid)
170         *pcid = rr->signedContentIdentifier;
171     if (rr->receiptsFrom->type == 0) {
172         if (pallorfirst)
173             *pallorfirst = (int)rr->receiptsFrom->d.allOrFirstTier;
174         if (plist)
175             *plist = NULL;
176     } else {
177         if (pallorfirst)
178             *pallorfirst = -1;
179         if (plist)
180             *plist = rr->receiptsFrom->d.receiptList;
181     }
182     if (prto)
183         *prto = rr->receiptsTo;
184 }
185
186 /* Digest a SignerInfo structure for msgSigDigest attribute processing */
187
188 static int cms_msgSigDigest(CMS_SignerInfo *si,
189                             unsigned char *dig, unsigned int *diglen)
190 {
191     const EVP_MD *md;
192     md = EVP_get_digestbyobj(si->digestAlgorithm->algorithm);
193     if (md == NULL)
194         return 0;
195     if (!ASN1_item_digest(ASN1_ITEM_rptr(CMS_Attributes_Verify), md,
196                           si->signedAttrs, dig, diglen))
197         return 0;
198     return 1;
199 }
200
201 /* Add a msgSigDigest attribute to a SignerInfo */
202
203 int cms_msgSigDigest_add1(CMS_SignerInfo *dest, CMS_SignerInfo *src)
204 {
205     unsigned char dig[EVP_MAX_MD_SIZE];
206     unsigned int diglen;
207     if (!cms_msgSigDigest(src, dig, &diglen)) {
208         CMSerr(CMS_F_CMS_MSGSIGDIGEST_ADD1, CMS_R_MSGSIGDIGEST_ERROR);
209         return 0;
210     }
211     if (!CMS_signed_add1_attr_by_NID(dest, NID_id_smime_aa_msgSigDigest,
212                                      V_ASN1_OCTET_STRING, dig, diglen)) {
213         CMSerr(CMS_F_CMS_MSGSIGDIGEST_ADD1, ERR_R_MALLOC_FAILURE);
214         return 0;
215     }
216     return 1;
217 }
218
219 /* Verify signed receipt after it has already passed normal CMS verify */
220
221 int cms_Receipt_verify(CMS_ContentInfo *cms, CMS_ContentInfo *req_cms)
222 {
223     int r = 0, i;
224     CMS_ReceiptRequest *rr = NULL;
225     CMS_Receipt *rct = NULL;
226     STACK_OF(CMS_SignerInfo) *sis, *osis;
227     CMS_SignerInfo *si, *osi = NULL;
228     ASN1_OCTET_STRING *msig, **pcont;
229     ASN1_OBJECT *octype;
230     unsigned char dig[EVP_MAX_MD_SIZE];
231     unsigned int diglen;
232
233     /* Get SignerInfos, also checks SignedData content type */
234     osis = CMS_get0_SignerInfos(req_cms);
235     sis = CMS_get0_SignerInfos(cms);
236     if (!osis || !sis)
237         goto err;
238
239     if (sk_CMS_SignerInfo_num(sis) != 1) {
240         CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_NEED_ONE_SIGNER);
241         goto err;
242     }
243
244     /* Check receipt content type */
245     if (OBJ_obj2nid(CMS_get0_eContentType(cms)) != NID_id_smime_ct_receipt) {
246         CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_NOT_A_SIGNED_RECEIPT);
247         goto err;
248     }
249
250     /* Extract and decode receipt content */
251     pcont = CMS_get0_content(cms);
252     if (!pcont || !*pcont) {
253         CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_NO_CONTENT);
254         goto err;
255     }
256
257     rct = ASN1_item_unpack(*pcont, ASN1_ITEM_rptr(CMS_Receipt));
258
259     if (!rct) {
260         CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_RECEIPT_DECODE_ERROR);
261         goto err;
262     }
263
264     /* Locate original request */
265
266     for (i = 0; i < sk_CMS_SignerInfo_num(osis); i++) {
267         osi = sk_CMS_SignerInfo_value(osis, i);
268         if (!ASN1_STRING_cmp(osi->signature, rct->originatorSignatureValue))
269             break;
270     }
271
272     if (i == sk_CMS_SignerInfo_num(osis)) {
273         CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_NO_MATCHING_SIGNATURE);
274         goto err;
275     }
276
277     si = sk_CMS_SignerInfo_value(sis, 0);
278
279     /* Get msgSigDigest value and compare */
280
281     msig = CMS_signed_get0_data_by_OBJ(si,
282                                        OBJ_nid2obj
283                                        (NID_id_smime_aa_msgSigDigest), -3,
284                                        V_ASN1_OCTET_STRING);
285
286     if (!msig) {
287         CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_NO_MSGSIGDIGEST);
288         goto err;
289     }
290
291     if (!cms_msgSigDigest(osi, dig, &diglen)) {
292         CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_MSGSIGDIGEST_ERROR);
293         goto err;
294     }
295
296     if (diglen != (unsigned int)msig->length) {
297         CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_MSGSIGDIGEST_WRONG_LENGTH);
298         goto err;
299     }
300
301     if (memcmp(dig, msig->data, diglen)) {
302         CMSerr(CMS_F_CMS_RECEIPT_VERIFY,
303                CMS_R_MSGSIGDIGEST_VERIFICATION_FAILURE);
304         goto err;
305     }
306
307     /* Compare content types */
308
309     octype = CMS_signed_get0_data_by_OBJ(osi,
310                                          OBJ_nid2obj(NID_pkcs9_contentType),
311                                          -3, V_ASN1_OBJECT);
312     if (!octype) {
313         CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_NO_CONTENT_TYPE);
314         goto err;
315     }
316
317     /* Compare details in receipt request */
318
319     if (OBJ_cmp(octype, rct->contentType)) {
320         CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_CONTENT_TYPE_MISMATCH);
321         goto err;
322     }
323
324     /* Get original receipt request details */
325
326     if (CMS_get1_ReceiptRequest(osi, &rr) <= 0) {
327         CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_NO_RECEIPT_REQUEST);
328         goto err;
329     }
330
331     if (ASN1_STRING_cmp(rr->signedContentIdentifier,
332                         rct->signedContentIdentifier)) {
333         CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_CONTENTIDENTIFIER_MISMATCH);
334         goto err;
335     }
336
337     r = 1;
338
339  err:
340     if (rr)
341         CMS_ReceiptRequest_free(rr);
342     M_ASN1_free_of(rct, CMS_Receipt);
343
344     return r;
345
346 }
347
348 /*
349  * Encode a Receipt into an OCTET STRING read for including into content of a
350  * SignedData ContentInfo.
351  */
352
353 ASN1_OCTET_STRING *cms_encode_Receipt(CMS_SignerInfo *si)
354 {
355     CMS_Receipt rct;
356     CMS_ReceiptRequest *rr = NULL;
357     ASN1_OBJECT *ctype;
358     ASN1_OCTET_STRING *os = NULL;
359
360     /* Get original receipt request */
361
362     /* Get original receipt request details */
363
364     if (CMS_get1_ReceiptRequest(si, &rr) <= 0) {
365         CMSerr(CMS_F_CMS_ENCODE_RECEIPT, CMS_R_NO_RECEIPT_REQUEST);
366         goto err;
367     }
368
369     /* Get original content type */
370
371     ctype = CMS_signed_get0_data_by_OBJ(si,
372                                         OBJ_nid2obj(NID_pkcs9_contentType),
373                                         -3, V_ASN1_OBJECT);
374     if (!ctype) {
375         CMSerr(CMS_F_CMS_ENCODE_RECEIPT, CMS_R_NO_CONTENT_TYPE);
376         goto err;
377     }
378
379     rct.version = 1;
380     rct.contentType = ctype;
381     rct.signedContentIdentifier = rr->signedContentIdentifier;
382     rct.originatorSignatureValue = si->signature;
383
384     os = ASN1_item_pack(&rct, ASN1_ITEM_rptr(CMS_Receipt), NULL);
385
386  err:
387     if (rr)
388         CMS_ReceiptRequest_free(rr);
389
390     return os;
391
392 }