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