1 /* crypto/cms/cms_ess.c */
3 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
6 /* ====================================================================
7 * Copyright (c) 2008 The OpenSSL Project. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
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
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/)"
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.
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.
35 * 6. Redistributions of any form whatsoever must retain the following
37 * "This product includes software developed by the OpenSSL Project
38 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
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 * ====================================================================
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>
64 DECLARE_ASN1_ITEM(CMS_ReceiptRequest)
65 DECLARE_ASN1_ITEM(CMS_Receipt)
67 IMPLEMENT_ASN1_FUNCTIONS(CMS_ReceiptRequest)
69 /* ESS services: for now just Signed Receipt related */
71 int CMS_get1_ReceiptRequest(CMS_SignerInfo *si, CMS_ReceiptRequest **prr)
74 CMS_ReceiptRequest *rr = NULL;
77 str = CMS_signed_get0_data_by_OBJ(si,
79 (NID_id_smime_aa_receiptRequest), -3,
84 rr = ASN1_item_unpack(str, ASN1_ITEM_rptr(CMS_ReceiptRequest));
90 CMS_ReceiptRequest_free(rr);
94 CMS_ReceiptRequest *CMS_ReceiptRequest_create0(unsigned char *id, int idlen,
96 STACK_OF(GENERAL_NAMES)
97 *receiptList, STACK_OF(GENERAL_NAMES)
100 CMS_ReceiptRequest *rr = NULL;
102 rr = CMS_ReceiptRequest_new();
106 ASN1_STRING_set0(rr->signedContentIdentifier, id, idlen);
108 if (!ASN1_STRING_set(rr->signedContentIdentifier, NULL, 32))
110 if (RAND_bytes(rr->signedContentIdentifier->data, 32) <= 0)
114 sk_GENERAL_NAMES_pop_free(rr->receiptsTo, GENERAL_NAMES_free);
115 rr->receiptsTo = receiptsTo;
118 rr->receiptsFrom->type = 1;
119 rr->receiptsFrom->d.receiptList = receiptList;
121 rr->receiptsFrom->type = 0;
122 rr->receiptsFrom->d.allOrFirstTier = allorfirst;
128 CMSerr(CMS_F_CMS_RECEIPTREQUEST_CREATE0, ERR_R_MALLOC_FAILURE);
131 CMS_ReceiptRequest_free(rr);
136 int CMS_add1_ReceiptRequest(CMS_SignerInfo *si, CMS_ReceiptRequest *rr)
138 unsigned char *rrder = NULL;
141 rrderlen = i2d_CMS_ReceiptRequest(rr, &rrder);
145 if (!CMS_signed_add1_attr_by_NID(si, NID_id_smime_aa_receiptRequest,
146 V_ASN1_SEQUENCE, rrder, rrderlen))
153 CMSerr(CMS_F_CMS_ADD1_RECEIPTREQUEST, ERR_R_MALLOC_FAILURE);
161 void CMS_ReceiptRequest_get0_values(CMS_ReceiptRequest *rr,
164 STACK_OF(GENERAL_NAMES) **plist,
165 STACK_OF(GENERAL_NAMES) **prto)
168 *pcid = rr->signedContentIdentifier;
169 if (rr->receiptsFrom->type == 0) {
171 *pallorfirst = (int)rr->receiptsFrom->d.allOrFirstTier;
178 *plist = rr->receiptsFrom->d.receiptList;
181 *prto = rr->receiptsTo;
184 /* Digest a SignerInfo structure for msgSigDigest attribute processing */
186 static int cms_msgSigDigest(CMS_SignerInfo *si,
187 unsigned char *dig, unsigned int *diglen)
190 md = EVP_get_digestbyobj(si->digestAlgorithm->algorithm);
193 if (!ASN1_item_digest(ASN1_ITEM_rptr(CMS_Attributes_Verify), md,
194 si->signedAttrs, dig, diglen))
199 /* Add a msgSigDigest attribute to a SignerInfo */
201 int cms_msgSigDigest_add1(CMS_SignerInfo *dest, CMS_SignerInfo *src)
203 unsigned char dig[EVP_MAX_MD_SIZE];
205 if (!cms_msgSigDigest(src, dig, &diglen)) {
206 CMSerr(CMS_F_CMS_MSGSIGDIGEST_ADD1, CMS_R_MSGSIGDIGEST_ERROR);
209 if (!CMS_signed_add1_attr_by_NID(dest, NID_id_smime_aa_msgSigDigest,
210 V_ASN1_OCTET_STRING, dig, diglen)) {
211 CMSerr(CMS_F_CMS_MSGSIGDIGEST_ADD1, ERR_R_MALLOC_FAILURE);
217 /* Verify signed receipt after it has already passed normal CMS verify */
219 int cms_Receipt_verify(CMS_ContentInfo *cms, CMS_ContentInfo *req_cms)
222 CMS_ReceiptRequest *rr = NULL;
223 CMS_Receipt *rct = NULL;
224 STACK_OF(CMS_SignerInfo) *sis, *osis;
225 CMS_SignerInfo *si, *osi = NULL;
226 ASN1_OCTET_STRING *msig, **pcont;
228 unsigned char dig[EVP_MAX_MD_SIZE];
231 /* Get SignerInfos, also checks SignedData content type */
232 osis = CMS_get0_SignerInfos(req_cms);
233 sis = CMS_get0_SignerInfos(cms);
237 if (sk_CMS_SignerInfo_num(sis) != 1) {
238 CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_NEED_ONE_SIGNER);
242 /* Check receipt content type */
243 if (OBJ_obj2nid(CMS_get0_eContentType(cms)) != NID_id_smime_ct_receipt) {
244 CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_NOT_A_SIGNED_RECEIPT);
248 /* Extract and decode receipt content */
249 pcont = CMS_get0_content(cms);
250 if (!pcont || !*pcont) {
251 CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_NO_CONTENT);
255 rct = ASN1_item_unpack(*pcont, ASN1_ITEM_rptr(CMS_Receipt));
258 CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_RECEIPT_DECODE_ERROR);
262 /* Locate original request */
264 for (i = 0; i < sk_CMS_SignerInfo_num(osis); i++) {
265 osi = sk_CMS_SignerInfo_value(osis, i);
266 if (!ASN1_STRING_cmp(osi->signature, rct->originatorSignatureValue))
270 if (i == sk_CMS_SignerInfo_num(osis)) {
271 CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_NO_MATCHING_SIGNATURE);
275 si = sk_CMS_SignerInfo_value(sis, 0);
277 /* Get msgSigDigest value and compare */
279 msig = CMS_signed_get0_data_by_OBJ(si,
281 (NID_id_smime_aa_msgSigDigest), -3,
282 V_ASN1_OCTET_STRING);
285 CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_NO_MSGSIGDIGEST);
289 if (!cms_msgSigDigest(osi, dig, &diglen)) {
290 CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_MSGSIGDIGEST_ERROR);
294 if (diglen != (unsigned int)msig->length) {
295 CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_MSGSIGDIGEST_WRONG_LENGTH);
299 if (memcmp(dig, msig->data, diglen)) {
300 CMSerr(CMS_F_CMS_RECEIPT_VERIFY,
301 CMS_R_MSGSIGDIGEST_VERIFICATION_FAILURE);
305 /* Compare content types */
307 octype = CMS_signed_get0_data_by_OBJ(osi,
308 OBJ_nid2obj(NID_pkcs9_contentType),
311 CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_NO_CONTENT_TYPE);
315 /* Compare details in receipt request */
317 if (OBJ_cmp(octype, rct->contentType)) {
318 CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_CONTENT_TYPE_MISMATCH);
322 /* Get original receipt request details */
324 if (CMS_get1_ReceiptRequest(osi, &rr) <= 0) {
325 CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_NO_RECEIPT_REQUEST);
329 if (ASN1_STRING_cmp(rr->signedContentIdentifier,
330 rct->signedContentIdentifier)) {
331 CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_CONTENTIDENTIFIER_MISMATCH);
338 CMS_ReceiptRequest_free(rr);
339 M_ASN1_free_of(rct, CMS_Receipt);
345 * Encode a Receipt into an OCTET STRING read for including into content of a
346 * SignedData ContentInfo.
349 ASN1_OCTET_STRING *cms_encode_Receipt(CMS_SignerInfo *si)
352 CMS_ReceiptRequest *rr = NULL;
354 ASN1_OCTET_STRING *os = NULL;
356 /* Get original receipt request */
358 /* Get original receipt request details */
360 if (CMS_get1_ReceiptRequest(si, &rr) <= 0) {
361 CMSerr(CMS_F_CMS_ENCODE_RECEIPT, CMS_R_NO_RECEIPT_REQUEST);
365 /* Get original content type */
367 ctype = CMS_signed_get0_data_by_OBJ(si,
368 OBJ_nid2obj(NID_pkcs9_contentType),
371 CMSerr(CMS_F_CMS_ENCODE_RECEIPT, CMS_R_NO_CONTENT_TYPE);
376 rct.contentType = ctype;
377 rct.signedContentIdentifier = rr->signedContentIdentifier;
378 rct.originatorSignatureValue = si->signature;
380 os = ASN1_item_pack(&rct, ASN1_ITEM_rptr(CMS_Receipt), NULL);
383 CMS_ReceiptRequest_free(rr);