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/x509.h"
20 #include "cms_local.h"
22 IMPLEMENT_ASN1_FUNCTIONS(CMS_ReceiptRequest
)
26 int CMS_get1_ReceiptRequest(CMS_SignerInfo
*si
, CMS_ReceiptRequest
**prr
)
29 CMS_ReceiptRequest
*rr
;
30 ASN1_OBJECT
*obj
= OBJ_nid2obj(NID_id_smime_aa_receiptRequest
);
34 str
= CMS_signed_get0_data_by_OBJ(si
, obj
, -3, V_ASN1_SEQUENCE
);
38 rr
= ASN1_item_unpack(str
, ASN1_ITEM_rptr(CMS_ReceiptRequest
));
44 CMS_ReceiptRequest_free(rr
);
49 * Returns 0 if attribute is not found, 1 if found,
50 * or -1 on attribute parsing failure.
52 static int ossl_cms_signerinfo_get_signing_cert(const CMS_SignerInfo
*si
,
53 ESS_SIGNING_CERT
**psc
)
57 ASN1_OBJECT
*obj
= OBJ_nid2obj(NID_id_smime_aa_signingCertificate
);
61 str
= CMS_signed_get0_data_by_OBJ(si
, obj
, -3, V_ASN1_SEQUENCE
);
65 sc
= ASN1_item_unpack(str
, ASN1_ITEM_rptr(ESS_SIGNING_CERT
));
71 ESS_SIGNING_CERT_free(sc
);
76 * Returns 0 if attribute is not found, 1 if found,
77 * or -1 on attribute parsing failure.
79 static int ossl_cms_signerinfo_get_signing_cert_v2(const CMS_SignerInfo
*si
,
80 ESS_SIGNING_CERT_V2
**psc
)
83 ESS_SIGNING_CERT_V2
*sc
;
84 ASN1_OBJECT
*obj
= OBJ_nid2obj(NID_id_smime_aa_signingCertificateV2
);
88 str
= CMS_signed_get0_data_by_OBJ(si
, obj
, -3, V_ASN1_SEQUENCE
);
92 sc
= ASN1_item_unpack(str
, ASN1_ITEM_rptr(ESS_SIGNING_CERT_V2
));
98 ESS_SIGNING_CERT_V2_free(sc
);
102 int ossl_cms_check_signing_certs(const CMS_SignerInfo
*si
,
103 const STACK_OF(X509
) *chain
)
105 ESS_SIGNING_CERT
*ss
= NULL
;
106 ESS_SIGNING_CERT_V2
*ssv2
= NULL
;
107 int ret
= ossl_cms_signerinfo_get_signing_cert(si
, &ss
) >= 0
108 && ossl_cms_signerinfo_get_signing_cert_v2(si
, &ssv2
) >= 0
109 && OSSL_ESS_check_signing_certs(ss
, ssv2
, chain
, 1) > 0;
111 ESS_SIGNING_CERT_free(ss
);
112 ESS_SIGNING_CERT_V2_free(ssv2
);
116 CMS_ReceiptRequest
*CMS_ReceiptRequest_create0_ex(
117 unsigned char *id
, int idlen
, int allorfirst
,
118 STACK_OF(GENERAL_NAMES
) *receiptList
, STACK_OF(GENERAL_NAMES
) *receiptsTo
,
119 OSSL_LIB_CTX
*libctx
)
121 CMS_ReceiptRequest
*rr
;
123 rr
= CMS_ReceiptRequest_new();
125 ERR_raise(ERR_LIB_CMS
, ERR_R_CMS_LIB
);
129 ASN1_STRING_set0(rr
->signedContentIdentifier
, id
, idlen
);
131 if (!ASN1_STRING_set(rr
->signedContentIdentifier
, NULL
, 32)) {
132 ERR_raise(ERR_LIB_CMS
, ERR_R_ASN1_LIB
);
135 if (RAND_bytes_ex(libctx
, rr
->signedContentIdentifier
->data
, 32,
140 sk_GENERAL_NAMES_pop_free(rr
->receiptsTo
, GENERAL_NAMES_free
);
141 rr
->receiptsTo
= receiptsTo
;
143 if (receiptList
!= NULL
) {
144 rr
->receiptsFrom
->type
= 1;
145 rr
->receiptsFrom
->d
.receiptList
= receiptList
;
147 rr
->receiptsFrom
->type
= 0;
148 rr
->receiptsFrom
->d
.allOrFirstTier
= allorfirst
;
154 CMS_ReceiptRequest_free(rr
);
159 CMS_ReceiptRequest
*CMS_ReceiptRequest_create0(
160 unsigned char *id
, int idlen
, int allorfirst
,
161 STACK_OF(GENERAL_NAMES
) *receiptList
, STACK_OF(GENERAL_NAMES
) *receiptsTo
)
163 return CMS_ReceiptRequest_create0_ex(id
, idlen
, allorfirst
, receiptList
,
167 int CMS_add1_ReceiptRequest(CMS_SignerInfo
*si
, CMS_ReceiptRequest
*rr
)
169 unsigned char *rrder
= NULL
;
172 rrderlen
= i2d_CMS_ReceiptRequest(rr
, &rrder
);
174 ERR_raise(ERR_LIB_CMS
, ERR_R_CMS_LIB
);
178 if (!CMS_signed_add1_attr_by_NID(si
, NID_id_smime_aa_receiptRequest
,
179 V_ASN1_SEQUENCE
, rrder
, rrderlen
)) {
180 ERR_raise(ERR_LIB_CMS
, ERR_R_CMS_LIB
);
193 void CMS_ReceiptRequest_get0_values(CMS_ReceiptRequest
*rr
,
196 STACK_OF(GENERAL_NAMES
) **plist
,
197 STACK_OF(GENERAL_NAMES
) **prto
)
200 *pcid
= rr
->signedContentIdentifier
;
201 if (rr
->receiptsFrom
->type
== 0) {
202 if (pallorfirst
!= NULL
)
203 *pallorfirst
= (int)rr
->receiptsFrom
->d
.allOrFirstTier
;
207 if (pallorfirst
!= NULL
)
210 *plist
= rr
->receiptsFrom
->d
.receiptList
;
213 *prto
= rr
->receiptsTo
;
216 /* Digest a SignerInfo structure for msgSigDigest attribute processing */
218 static int cms_msgSigDigest(CMS_SignerInfo
*si
,
219 unsigned char *dig
, unsigned int *diglen
)
221 const EVP_MD
*md
= EVP_get_digestbyobj(si
->digestAlgorithm
->algorithm
);
225 if (!ossl_asn1_item_digest_ex(ASN1_ITEM_rptr(CMS_Attributes_Verify
), md
,
226 si
->signedAttrs
, dig
, diglen
,
227 ossl_cms_ctx_get0_libctx(si
->cms_ctx
),
228 ossl_cms_ctx_get0_propq(si
->cms_ctx
)))
233 /* Add a msgSigDigest attribute to a SignerInfo */
235 int ossl_cms_msgSigDigest_add1(CMS_SignerInfo
*dest
, CMS_SignerInfo
*src
)
237 unsigned char dig
[EVP_MAX_MD_SIZE
];
240 if (!cms_msgSigDigest(src
, dig
, &diglen
)) {
241 ERR_raise(ERR_LIB_CMS
, CMS_R_MSGSIGDIGEST_ERROR
);
244 if (!CMS_signed_add1_attr_by_NID(dest
, NID_id_smime_aa_msgSigDigest
,
245 V_ASN1_OCTET_STRING
, dig
, diglen
)) {
246 ERR_raise(ERR_LIB_CMS
, ERR_R_CMS_LIB
);
252 /* Verify signed receipt after it has already passed normal CMS verify */
254 int ossl_cms_Receipt_verify(CMS_ContentInfo
*cms
, CMS_ContentInfo
*req_cms
)
257 CMS_ReceiptRequest
*rr
= NULL
;
258 CMS_Receipt
*rct
= NULL
;
259 STACK_OF(CMS_SignerInfo
) *sis
, *osis
;
260 CMS_SignerInfo
*si
, *osi
= NULL
;
261 ASN1_OCTET_STRING
*msig
, **pcont
;
263 unsigned char dig
[EVP_MAX_MD_SIZE
];
266 /* Get SignerInfos, also checks SignedData content type */
267 osis
= CMS_get0_SignerInfos(req_cms
);
268 sis
= CMS_get0_SignerInfos(cms
);
272 if (sk_CMS_SignerInfo_num(sis
) != 1) {
273 ERR_raise(ERR_LIB_CMS
, CMS_R_NEED_ONE_SIGNER
);
277 /* Check receipt content type */
278 if (OBJ_obj2nid(CMS_get0_eContentType(cms
)) != NID_id_smime_ct_receipt
) {
279 ERR_raise(ERR_LIB_CMS
, CMS_R_NOT_A_SIGNED_RECEIPT
);
283 /* Extract and decode receipt content */
284 pcont
= CMS_get0_content(cms
);
285 if (pcont
== NULL
|| *pcont
== NULL
) {
286 ERR_raise(ERR_LIB_CMS
, CMS_R_NO_CONTENT
);
290 rct
= ASN1_item_unpack(*pcont
, ASN1_ITEM_rptr(CMS_Receipt
));
293 ERR_raise(ERR_LIB_CMS
, CMS_R_RECEIPT_DECODE_ERROR
);
297 /* Locate original request */
299 for (i
= 0; i
< sk_CMS_SignerInfo_num(osis
); i
++) {
300 osi
= sk_CMS_SignerInfo_value(osis
, i
);
301 if (!ASN1_STRING_cmp(osi
->signature
, rct
->originatorSignatureValue
))
305 if (i
== sk_CMS_SignerInfo_num(osis
)) {
306 ERR_raise(ERR_LIB_CMS
, CMS_R_NO_MATCHING_SIGNATURE
);
310 si
= sk_CMS_SignerInfo_value(sis
, 0);
312 /* Get msgSigDigest value and compare */
314 msig
= CMS_signed_get0_data_by_OBJ(si
,
316 (NID_id_smime_aa_msgSigDigest
), -3,
317 V_ASN1_OCTET_STRING
);
320 ERR_raise(ERR_LIB_CMS
, CMS_R_NO_MSGSIGDIGEST
);
324 if (!cms_msgSigDigest(osi
, dig
, &diglen
)) {
325 ERR_raise(ERR_LIB_CMS
, CMS_R_MSGSIGDIGEST_ERROR
);
329 if (diglen
!= (unsigned int)msig
->length
) {
330 ERR_raise(ERR_LIB_CMS
, CMS_R_MSGSIGDIGEST_WRONG_LENGTH
);
334 if (memcmp(dig
, msig
->data
, diglen
)) {
335 ERR_raise(ERR_LIB_CMS
, CMS_R_MSGSIGDIGEST_VERIFICATION_FAILURE
);
339 /* Compare content types */
341 octype
= CMS_signed_get0_data_by_OBJ(osi
,
342 OBJ_nid2obj(NID_pkcs9_contentType
),
345 ERR_raise(ERR_LIB_CMS
, CMS_R_NO_CONTENT_TYPE
);
349 /* Compare details in receipt request */
351 if (OBJ_cmp(octype
, rct
->contentType
)) {
352 ERR_raise(ERR_LIB_CMS
, CMS_R_CONTENT_TYPE_MISMATCH
);
356 /* Get original receipt request details */
358 if (CMS_get1_ReceiptRequest(osi
, &rr
) <= 0) {
359 ERR_raise(ERR_LIB_CMS
, CMS_R_NO_RECEIPT_REQUEST
);
363 if (ASN1_STRING_cmp(rr
->signedContentIdentifier
,
364 rct
->signedContentIdentifier
)) {
365 ERR_raise(ERR_LIB_CMS
, CMS_R_CONTENTIDENTIFIER_MISMATCH
);
372 CMS_ReceiptRequest_free(rr
);
373 M_ASN1_free_of(rct
, CMS_Receipt
);
379 * Encode a Receipt into an OCTET STRING read for including into content of a
380 * SignedData ContentInfo.
383 ASN1_OCTET_STRING
*ossl_cms_encode_Receipt(CMS_SignerInfo
*si
)
386 CMS_ReceiptRequest
*rr
= NULL
;
388 ASN1_OCTET_STRING
*os
= NULL
;
390 /* Get original receipt request */
392 /* Get original receipt request details */
394 if (CMS_get1_ReceiptRequest(si
, &rr
) <= 0) {
395 ERR_raise(ERR_LIB_CMS
, CMS_R_NO_RECEIPT_REQUEST
);
399 /* Get original content type */
401 ctype
= CMS_signed_get0_data_by_OBJ(si
,
402 OBJ_nid2obj(NID_pkcs9_contentType
),
405 ERR_raise(ERR_LIB_CMS
, CMS_R_NO_CONTENT_TYPE
);
410 rct
.contentType
= ctype
;
411 rct
.signedContentIdentifier
= rr
->signedContentIdentifier
;
412 rct
.originatorSignatureValue
= si
->signature
;
414 os
= ASN1_item_pack(&rct
, ASN1_ITEM_rptr(CMS_Receipt
), NULL
);
417 CMS_ReceiptRequest_free(rr
);