2 * Copyright 2008-2021 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>
18 #include "crypto/ess.h"
19 #include "crypto/cms.h"
20 #include "crypto/x509.h"
21 #include "cms_local.h"
23 IMPLEMENT_ASN1_FUNCTIONS(CMS_ReceiptRequest
)
27 int CMS_get1_ReceiptRequest(CMS_SignerInfo
*si
, CMS_ReceiptRequest
**prr
)
30 CMS_ReceiptRequest
*rr
;
31 ASN1_OBJECT
*obj
= OBJ_nid2obj(NID_id_smime_aa_receiptRequest
);
35 str
= CMS_signed_get0_data_by_OBJ(si
, obj
, -3, V_ASN1_SEQUENCE
);
39 rr
= ASN1_item_unpack(str
, ASN1_ITEM_rptr(CMS_ReceiptRequest
));
45 CMS_ReceiptRequest_free(rr
);
49 int ossl_cms_check_signing_certs(const CMS_SignerInfo
*si
,
50 const STACK_OF(X509
) *chain
)
52 ESS_SIGNING_CERT
*ss
= NULL
;
53 ESS_SIGNING_CERT_V2
*ssv2
= NULL
;
54 int ret
= ossl_cms_signerinfo_get_signing_cert(si
, &ss
) >= 0
55 && ossl_cms_signerinfo_get_signing_cert_v2(si
, &ssv2
) >= 0
56 && ossl_ess_check_signing_certs(ss
, ssv2
, chain
, 1);
58 ESS_SIGNING_CERT_free(ss
);
59 ESS_SIGNING_CERT_V2_free(ssv2
);
63 CMS_ReceiptRequest
*CMS_ReceiptRequest_create0_ex(
64 unsigned char *id
, int idlen
, int allorfirst
,
65 STACK_OF(GENERAL_NAMES
) *receiptList
, STACK_OF(GENERAL_NAMES
) *receiptsTo
,
66 OSSL_LIB_CTX
*libctx
, const char *propq
)
68 CMS_ReceiptRequest
*rr
;
70 rr
= CMS_ReceiptRequest_new();
74 ASN1_STRING_set0(rr
->signedContentIdentifier
, id
, idlen
);
76 if (!ASN1_STRING_set(rr
->signedContentIdentifier
, NULL
, 32))
78 if (RAND_bytes_ex(libctx
, rr
->signedContentIdentifier
->data
, 32) <= 0)
82 sk_GENERAL_NAMES_pop_free(rr
->receiptsTo
, GENERAL_NAMES_free
);
83 rr
->receiptsTo
= receiptsTo
;
85 if (receiptList
!= NULL
) {
86 rr
->receiptsFrom
->type
= 1;
87 rr
->receiptsFrom
->d
.receiptList
= receiptList
;
89 rr
->receiptsFrom
->type
= 0;
90 rr
->receiptsFrom
->d
.allOrFirstTier
= allorfirst
;
96 ERR_raise(ERR_LIB_CMS
, ERR_R_MALLOC_FAILURE
);
99 CMS_ReceiptRequest_free(rr
);
104 CMS_ReceiptRequest
*CMS_ReceiptRequest_create0(
105 unsigned char *id
, int idlen
, int allorfirst
,
106 STACK_OF(GENERAL_NAMES
) *receiptList
, STACK_OF(GENERAL_NAMES
) *receiptsTo
)
108 return CMS_ReceiptRequest_create0_ex(id
, idlen
, allorfirst
, receiptList
,
109 receiptsTo
, NULL
, NULL
);
112 int CMS_add1_ReceiptRequest(CMS_SignerInfo
*si
, CMS_ReceiptRequest
*rr
)
114 unsigned char *rrder
= NULL
;
117 rrderlen
= i2d_CMS_ReceiptRequest(rr
, &rrder
);
121 if (!CMS_signed_add1_attr_by_NID(si
, NID_id_smime_aa_receiptRequest
,
122 V_ASN1_SEQUENCE
, rrder
, rrderlen
))
129 ERR_raise(ERR_LIB_CMS
, ERR_R_MALLOC_FAILURE
);
137 void CMS_ReceiptRequest_get0_values(CMS_ReceiptRequest
*rr
,
140 STACK_OF(GENERAL_NAMES
) **plist
,
141 STACK_OF(GENERAL_NAMES
) **prto
)
144 *pcid
= rr
->signedContentIdentifier
;
145 if (rr
->receiptsFrom
->type
== 0) {
146 if (pallorfirst
!= NULL
)
147 *pallorfirst
= (int)rr
->receiptsFrom
->d
.allOrFirstTier
;
151 if (pallorfirst
!= NULL
)
154 *plist
= rr
->receiptsFrom
->d
.receiptList
;
157 *prto
= rr
->receiptsTo
;
160 /* Digest a SignerInfo structure for msgSigDigest attribute processing */
162 static int cms_msgSigDigest(CMS_SignerInfo
*si
,
163 unsigned char *dig
, unsigned int *diglen
)
165 const EVP_MD
*md
= EVP_get_digestbyobj(si
->digestAlgorithm
->algorithm
);
169 if (!ossl_asn1_item_digest_ex(ASN1_ITEM_rptr(CMS_Attributes_Verify
), md
,
170 si
->signedAttrs
, dig
, diglen
,
171 ossl_cms_ctx_get0_libctx(si
->cms_ctx
),
172 ossl_cms_ctx_get0_propq(si
->cms_ctx
)))
177 /* Add a msgSigDigest attribute to a SignerInfo */
179 int ossl_cms_msgSigDigest_add1(CMS_SignerInfo
*dest
, CMS_SignerInfo
*src
)
181 unsigned char dig
[EVP_MAX_MD_SIZE
];
184 if (!cms_msgSigDigest(src
, dig
, &diglen
)) {
185 ERR_raise(ERR_LIB_CMS
, CMS_R_MSGSIGDIGEST_ERROR
);
188 if (!CMS_signed_add1_attr_by_NID(dest
, NID_id_smime_aa_msgSigDigest
,
189 V_ASN1_OCTET_STRING
, dig
, diglen
)) {
190 ERR_raise(ERR_LIB_CMS
, ERR_R_MALLOC_FAILURE
);
196 /* Verify signed receipt after it has already passed normal CMS verify */
198 int ossl_cms_Receipt_verify(CMS_ContentInfo
*cms
, CMS_ContentInfo
*req_cms
)
201 CMS_ReceiptRequest
*rr
= NULL
;
202 CMS_Receipt
*rct
= NULL
;
203 STACK_OF(CMS_SignerInfo
) *sis
, *osis
;
204 CMS_SignerInfo
*si
, *osi
= NULL
;
205 ASN1_OCTET_STRING
*msig
, **pcont
;
207 unsigned char dig
[EVP_MAX_MD_SIZE
];
210 /* Get SignerInfos, also checks SignedData content type */
211 osis
= CMS_get0_SignerInfos(req_cms
);
212 sis
= CMS_get0_SignerInfos(cms
);
216 if (sk_CMS_SignerInfo_num(sis
) != 1) {
217 ERR_raise(ERR_LIB_CMS
, CMS_R_NEED_ONE_SIGNER
);
221 /* Check receipt content type */
222 if (OBJ_obj2nid(CMS_get0_eContentType(cms
)) != NID_id_smime_ct_receipt
) {
223 ERR_raise(ERR_LIB_CMS
, CMS_R_NOT_A_SIGNED_RECEIPT
);
227 /* Extract and decode receipt content */
228 pcont
= CMS_get0_content(cms
);
229 if (pcont
== NULL
|| *pcont
== NULL
) {
230 ERR_raise(ERR_LIB_CMS
, CMS_R_NO_CONTENT
);
234 rct
= ASN1_item_unpack(*pcont
, ASN1_ITEM_rptr(CMS_Receipt
));
237 ERR_raise(ERR_LIB_CMS
, CMS_R_RECEIPT_DECODE_ERROR
);
241 /* Locate original request */
243 for (i
= 0; i
< sk_CMS_SignerInfo_num(osis
); i
++) {
244 osi
= sk_CMS_SignerInfo_value(osis
, i
);
245 if (!ASN1_STRING_cmp(osi
->signature
, rct
->originatorSignatureValue
))
249 if (i
== sk_CMS_SignerInfo_num(osis
)) {
250 ERR_raise(ERR_LIB_CMS
, CMS_R_NO_MATCHING_SIGNATURE
);
254 si
= sk_CMS_SignerInfo_value(sis
, 0);
256 /* Get msgSigDigest value and compare */
258 msig
= CMS_signed_get0_data_by_OBJ(si
,
260 (NID_id_smime_aa_msgSigDigest
), -3,
261 V_ASN1_OCTET_STRING
);
264 ERR_raise(ERR_LIB_CMS
, CMS_R_NO_MSGSIGDIGEST
);
268 if (!cms_msgSigDigest(osi
, dig
, &diglen
)) {
269 ERR_raise(ERR_LIB_CMS
, CMS_R_MSGSIGDIGEST_ERROR
);
273 if (diglen
!= (unsigned int)msig
->length
) {
274 ERR_raise(ERR_LIB_CMS
, CMS_R_MSGSIGDIGEST_WRONG_LENGTH
);
278 if (memcmp(dig
, msig
->data
, diglen
)) {
279 ERR_raise(ERR_LIB_CMS
, CMS_R_MSGSIGDIGEST_VERIFICATION_FAILURE
);
283 /* Compare content types */
285 octype
= CMS_signed_get0_data_by_OBJ(osi
,
286 OBJ_nid2obj(NID_pkcs9_contentType
),
289 ERR_raise(ERR_LIB_CMS
, CMS_R_NO_CONTENT_TYPE
);
293 /* Compare details in receipt request */
295 if (OBJ_cmp(octype
, rct
->contentType
)) {
296 ERR_raise(ERR_LIB_CMS
, CMS_R_CONTENT_TYPE_MISMATCH
);
300 /* Get original receipt request details */
302 if (CMS_get1_ReceiptRequest(osi
, &rr
) <= 0) {
303 ERR_raise(ERR_LIB_CMS
, CMS_R_NO_RECEIPT_REQUEST
);
307 if (ASN1_STRING_cmp(rr
->signedContentIdentifier
,
308 rct
->signedContentIdentifier
)) {
309 ERR_raise(ERR_LIB_CMS
, CMS_R_CONTENTIDENTIFIER_MISMATCH
);
316 CMS_ReceiptRequest_free(rr
);
317 M_ASN1_free_of(rct
, CMS_Receipt
);
323 * Encode a Receipt into an OCTET STRING read for including into content of a
324 * SignedData ContentInfo.
327 ASN1_OCTET_STRING
*ossl_cms_encode_Receipt(CMS_SignerInfo
*si
)
330 CMS_ReceiptRequest
*rr
= NULL
;
332 ASN1_OCTET_STRING
*os
= NULL
;
334 /* Get original receipt request */
336 /* Get original receipt request details */
338 if (CMS_get1_ReceiptRequest(si
, &rr
) <= 0) {
339 ERR_raise(ERR_LIB_CMS
, CMS_R_NO_RECEIPT_REQUEST
);
343 /* Get original content type */
345 ctype
= CMS_signed_get0_data_by_OBJ(si
,
346 OBJ_nid2obj(NID_pkcs9_contentType
),
349 ERR_raise(ERR_LIB_CMS
, CMS_R_NO_CONTENT_TYPE
);
354 rct
.contentType
= ctype
;
355 rct
.signedContentIdentifier
= rr
->signedContentIdentifier
;
356 rct
.originatorSignatureValue
= si
->signature
;
358 os
= ASN1_item_pack(&rct
, ASN1_ITEM_rptr(CMS_Receipt
), NULL
);
361 CMS_ReceiptRequest_free(rr
);
366 * Add signer certificate's V2 digest |sc| to a SignerInfo structure |si|
369 int ossl_cms_add1_signing_cert_v2(CMS_SignerInfo
*si
, ESS_SIGNING_CERT_V2
*sc
)
371 ASN1_STRING
*seq
= NULL
;
372 unsigned char *p
, *pp
= NULL
;
375 /* Add SigningCertificateV2 signed attribute to the signer info. */
376 len
= i2d_ESS_SIGNING_CERT_V2(sc
, NULL
);
377 if (len
<= 0 || (pp
= OPENSSL_malloc(len
)) == NULL
)
380 i2d_ESS_SIGNING_CERT_V2(sc
, &p
);
381 if (!(seq
= ASN1_STRING_new()) || !ASN1_STRING_set(seq
, pp
, len
))
385 if (!CMS_signed_add1_attr_by_NID(si
, NID_id_smime_aa_signingCertificateV2
,
386 V_ASN1_SEQUENCE
, seq
, -1))
388 ASN1_STRING_free(seq
);
391 ERR_raise(ERR_LIB_CMS
, ERR_R_MALLOC_FAILURE
);
392 ASN1_STRING_free(seq
);
398 * Add signer certificate's digest |sc| to a SignerInfo structure |si|
401 int ossl_cms_add1_signing_cert(CMS_SignerInfo
*si
, ESS_SIGNING_CERT
*sc
)
403 ASN1_STRING
*seq
= NULL
;
404 unsigned char *p
, *pp
= NULL
;
407 /* Add SigningCertificate signed attribute to the signer info. */
408 len
= i2d_ESS_SIGNING_CERT(sc
, NULL
);
409 if (len
<= 0 || (pp
= OPENSSL_malloc(len
)) == NULL
)
412 i2d_ESS_SIGNING_CERT(sc
, &p
);
413 if (!(seq
= ASN1_STRING_new()) || !ASN1_STRING_set(seq
, pp
, len
))
417 if (!CMS_signed_add1_attr_by_NID(si
, NID_id_smime_aa_signingCertificate
,
418 V_ASN1_SEQUENCE
, seq
, -1))
420 ASN1_STRING_free(seq
);
423 ERR_raise(ERR_LIB_CMS
, ERR_R_MALLOC_FAILURE
);
424 ASN1_STRING_free(seq
);