2 * Copyright 2008-2019 The OpenSSL Project Authors. All Rights Reserved.
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
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>
19 #include "crypto/ess.h"
20 #include "crypto/cms.h"
22 IMPLEMENT_ASN1_FUNCTIONS(CMS_ReceiptRequest
)
26 int CMS_get1_ReceiptRequest(CMS_SignerInfo
*si
, CMS_ReceiptRequest
**prr
)
29 CMS_ReceiptRequest
*rr
= NULL
;
32 str
= CMS_signed_get0_data_by_OBJ(si
,
34 (NID_id_smime_aa_receiptRequest
), -3,
39 rr
= ASN1_item_unpack(str
, ASN1_ITEM_rptr(CMS_ReceiptRequest
));
45 CMS_ReceiptRequest_free(rr
);
49 CMS_ReceiptRequest
*CMS_ReceiptRequest_create0(unsigned char *id
, int idlen
,
51 STACK_OF(GENERAL_NAMES
)
52 *receiptList
, STACK_OF(GENERAL_NAMES
)
55 CMS_ReceiptRequest
*rr
= NULL
;
57 rr
= CMS_ReceiptRequest_new();
61 ASN1_STRING_set0(rr
->signedContentIdentifier
, id
, idlen
);
63 if (!ASN1_STRING_set(rr
->signedContentIdentifier
, NULL
, 32))
65 if (RAND_bytes(rr
->signedContentIdentifier
->data
, 32) <= 0)
69 sk_GENERAL_NAMES_pop_free(rr
->receiptsTo
, GENERAL_NAMES_free
);
70 rr
->receiptsTo
= receiptsTo
;
73 rr
->receiptsFrom
->type
= 1;
74 rr
->receiptsFrom
->d
.receiptList
= receiptList
;
76 rr
->receiptsFrom
->type
= 0;
77 rr
->receiptsFrom
->d
.allOrFirstTier
= allorfirst
;
83 CMSerr(CMS_F_CMS_RECEIPTREQUEST_CREATE0
, ERR_R_MALLOC_FAILURE
);
86 CMS_ReceiptRequest_free(rr
);
91 int CMS_add1_ReceiptRequest(CMS_SignerInfo
*si
, CMS_ReceiptRequest
*rr
)
93 unsigned char *rrder
= NULL
;
96 rrderlen
= i2d_CMS_ReceiptRequest(rr
, &rrder
);
100 if (!CMS_signed_add1_attr_by_NID(si
, NID_id_smime_aa_receiptRequest
,
101 V_ASN1_SEQUENCE
, rrder
, rrderlen
))
108 CMSerr(CMS_F_CMS_ADD1_RECEIPTREQUEST
, ERR_R_MALLOC_FAILURE
);
116 void CMS_ReceiptRequest_get0_values(CMS_ReceiptRequest
*rr
,
119 STACK_OF(GENERAL_NAMES
) **plist
,
120 STACK_OF(GENERAL_NAMES
) **prto
)
123 *pcid
= rr
->signedContentIdentifier
;
124 if (rr
->receiptsFrom
->type
== 0) {
126 *pallorfirst
= (int)rr
->receiptsFrom
->d
.allOrFirstTier
;
133 *plist
= rr
->receiptsFrom
->d
.receiptList
;
136 *prto
= rr
->receiptsTo
;
139 /* Digest a SignerInfo structure for msgSigDigest attribute processing */
141 static int cms_msgSigDigest(CMS_SignerInfo
*si
,
142 unsigned char *dig
, unsigned int *diglen
)
145 md
= EVP_get_digestbyobj(si
->digestAlgorithm
->algorithm
);
148 if (!ASN1_item_digest(ASN1_ITEM_rptr(CMS_Attributes_Verify
), md
,
149 si
->signedAttrs
, dig
, diglen
))
154 /* Add a msgSigDigest attribute to a SignerInfo */
156 int cms_msgSigDigest_add1(CMS_SignerInfo
*dest
, CMS_SignerInfo
*src
)
158 unsigned char dig
[EVP_MAX_MD_SIZE
];
160 if (!cms_msgSigDigest(src
, dig
, &diglen
)) {
161 CMSerr(CMS_F_CMS_MSGSIGDIGEST_ADD1
, CMS_R_MSGSIGDIGEST_ERROR
);
164 if (!CMS_signed_add1_attr_by_NID(dest
, NID_id_smime_aa_msgSigDigest
,
165 V_ASN1_OCTET_STRING
, dig
, diglen
)) {
166 CMSerr(CMS_F_CMS_MSGSIGDIGEST_ADD1
, ERR_R_MALLOC_FAILURE
);
172 /* Verify signed receipt after it has already passed normal CMS verify */
174 int cms_Receipt_verify(CMS_ContentInfo
*cms
, CMS_ContentInfo
*req_cms
)
177 CMS_ReceiptRequest
*rr
= NULL
;
178 CMS_Receipt
*rct
= NULL
;
179 STACK_OF(CMS_SignerInfo
) *sis
, *osis
;
180 CMS_SignerInfo
*si
, *osi
= NULL
;
181 ASN1_OCTET_STRING
*msig
, **pcont
;
183 unsigned char dig
[EVP_MAX_MD_SIZE
];
186 /* Get SignerInfos, also checks SignedData content type */
187 osis
= CMS_get0_SignerInfos(req_cms
);
188 sis
= CMS_get0_SignerInfos(cms
);
192 if (sk_CMS_SignerInfo_num(sis
) != 1) {
193 CMSerr(CMS_F_CMS_RECEIPT_VERIFY
, CMS_R_NEED_ONE_SIGNER
);
197 /* Check receipt content type */
198 if (OBJ_obj2nid(CMS_get0_eContentType(cms
)) != NID_id_smime_ct_receipt
) {
199 CMSerr(CMS_F_CMS_RECEIPT_VERIFY
, CMS_R_NOT_A_SIGNED_RECEIPT
);
203 /* Extract and decode receipt content */
204 pcont
= CMS_get0_content(cms
);
205 if (!pcont
|| !*pcont
) {
206 CMSerr(CMS_F_CMS_RECEIPT_VERIFY
, CMS_R_NO_CONTENT
);
210 rct
= ASN1_item_unpack(*pcont
, ASN1_ITEM_rptr(CMS_Receipt
));
213 CMSerr(CMS_F_CMS_RECEIPT_VERIFY
, CMS_R_RECEIPT_DECODE_ERROR
);
217 /* Locate original request */
219 for (i
= 0; i
< sk_CMS_SignerInfo_num(osis
); i
++) {
220 osi
= sk_CMS_SignerInfo_value(osis
, i
);
221 if (!ASN1_STRING_cmp(osi
->signature
, rct
->originatorSignatureValue
))
225 if (i
== sk_CMS_SignerInfo_num(osis
)) {
226 CMSerr(CMS_F_CMS_RECEIPT_VERIFY
, CMS_R_NO_MATCHING_SIGNATURE
);
230 si
= sk_CMS_SignerInfo_value(sis
, 0);
232 /* Get msgSigDigest value and compare */
234 msig
= CMS_signed_get0_data_by_OBJ(si
,
236 (NID_id_smime_aa_msgSigDigest
), -3,
237 V_ASN1_OCTET_STRING
);
240 CMSerr(CMS_F_CMS_RECEIPT_VERIFY
, CMS_R_NO_MSGSIGDIGEST
);
244 if (!cms_msgSigDigest(osi
, dig
, &diglen
)) {
245 CMSerr(CMS_F_CMS_RECEIPT_VERIFY
, CMS_R_MSGSIGDIGEST_ERROR
);
249 if (diglen
!= (unsigned int)msig
->length
) {
250 CMSerr(CMS_F_CMS_RECEIPT_VERIFY
, CMS_R_MSGSIGDIGEST_WRONG_LENGTH
);
254 if (memcmp(dig
, msig
->data
, diglen
)) {
255 CMSerr(CMS_F_CMS_RECEIPT_VERIFY
,
256 CMS_R_MSGSIGDIGEST_VERIFICATION_FAILURE
);
260 /* Compare content types */
262 octype
= CMS_signed_get0_data_by_OBJ(osi
,
263 OBJ_nid2obj(NID_pkcs9_contentType
),
266 CMSerr(CMS_F_CMS_RECEIPT_VERIFY
, CMS_R_NO_CONTENT_TYPE
);
270 /* Compare details in receipt request */
272 if (OBJ_cmp(octype
, rct
->contentType
)) {
273 CMSerr(CMS_F_CMS_RECEIPT_VERIFY
, CMS_R_CONTENT_TYPE_MISMATCH
);
277 /* Get original receipt request details */
279 if (CMS_get1_ReceiptRequest(osi
, &rr
) <= 0) {
280 CMSerr(CMS_F_CMS_RECEIPT_VERIFY
, CMS_R_NO_RECEIPT_REQUEST
);
284 if (ASN1_STRING_cmp(rr
->signedContentIdentifier
,
285 rct
->signedContentIdentifier
)) {
286 CMSerr(CMS_F_CMS_RECEIPT_VERIFY
, CMS_R_CONTENTIDENTIFIER_MISMATCH
);
293 CMS_ReceiptRequest_free(rr
);
294 M_ASN1_free_of(rct
, CMS_Receipt
);
300 * Encode a Receipt into an OCTET STRING read for including into content of a
301 * SignedData ContentInfo.
304 ASN1_OCTET_STRING
*cms_encode_Receipt(CMS_SignerInfo
*si
)
307 CMS_ReceiptRequest
*rr
= NULL
;
309 ASN1_OCTET_STRING
*os
= NULL
;
311 /* Get original receipt request */
313 /* Get original receipt request details */
315 if (CMS_get1_ReceiptRequest(si
, &rr
) <= 0) {
316 CMSerr(CMS_F_CMS_ENCODE_RECEIPT
, CMS_R_NO_RECEIPT_REQUEST
);
320 /* Get original content type */
322 ctype
= CMS_signed_get0_data_by_OBJ(si
,
323 OBJ_nid2obj(NID_pkcs9_contentType
),
326 CMSerr(CMS_F_CMS_ENCODE_RECEIPT
, CMS_R_NO_CONTENT_TYPE
);
331 rct
.contentType
= ctype
;
332 rct
.signedContentIdentifier
= rr
->signedContentIdentifier
;
333 rct
.originatorSignatureValue
= si
->signature
;
335 os
= ASN1_item_pack(&rct
, ASN1_ITEM_rptr(CMS_Receipt
), NULL
);
338 CMS_ReceiptRequest_free(rr
);
343 * Add signer certificate's V2 digest |sc| to a SignerInfo structure |si|
346 int cms_add1_signing_cert_v2(CMS_SignerInfo
*si
, ESS_SIGNING_CERT_V2
*sc
)
348 ASN1_STRING
*seq
= NULL
;
349 unsigned char *p
, *pp
;
352 /* Add SigningCertificateV2 signed attribute to the signer info. */
353 len
= i2d_ESS_SIGNING_CERT_V2(sc
, NULL
);
354 if ((pp
= OPENSSL_malloc(len
)) == NULL
)
357 i2d_ESS_SIGNING_CERT_V2(sc
, &p
);
358 if (!(seq
= ASN1_STRING_new()) || !ASN1_STRING_set(seq
, pp
, len
))
362 if (!CMS_signed_add1_attr_by_NID(si
, NID_id_smime_aa_signingCertificateV2
,
363 V_ASN1_SEQUENCE
, seq
, -1))
365 ASN1_STRING_free(seq
);
368 CMSerr(CMS_F_CMS_ADD1_SIGNING_CERT_V2
, ERR_R_MALLOC_FAILURE
);
369 ASN1_STRING_free(seq
);
375 * Add signer certificate's digest |sc| to a SignerInfo structure |si|
378 int cms_add1_signing_cert(CMS_SignerInfo
*si
, ESS_SIGNING_CERT
*sc
)
380 ASN1_STRING
*seq
= NULL
;
381 unsigned char *p
, *pp
;
384 /* Add SigningCertificate signed attribute to the signer info. */
385 len
= i2d_ESS_SIGNING_CERT(sc
, NULL
);
386 if ((pp
= OPENSSL_malloc(len
)) == NULL
)
389 i2d_ESS_SIGNING_CERT(sc
, &p
);
390 if (!(seq
= ASN1_STRING_new()) || !ASN1_STRING_set(seq
, pp
, len
))
394 if (!CMS_signed_add1_attr_by_NID(si
, NID_id_smime_aa_signingCertificate
,
395 V_ASN1_SEQUENCE
, seq
, -1))
397 ASN1_STRING_free(seq
);
400 CMSerr(CMS_F_CMS_ADD1_SIGNING_CERT
, ERR_R_MALLOC_FAILURE
);
401 ASN1_STRING_free(seq
);