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);
50 First, get the ESS_SIGNING_CERT(V2) signed attribute from |si|.
51 Then check matching of each cert of trust |chain| with one of
52 the |cert_ids|(Hash+IssuerID) list from this ESS_SIGNING_CERT.
53 Derived from ts_check_signing_certs()
55 int ess_check_signing_certs(CMS_SignerInfo *si, STACK_OF(X509) *chain)
57 ESS_SIGNING_CERT *ss = NULL;
58 ESS_SIGNING_CERT_V2 *ssv2 = NULL;
62 if (cms_signerinfo_get_signing_cert(si, &ss) > 0 && ss->cert_ids != NULL) {
63 STACK_OF(ESS_CERT_ID) *cert_ids = ss->cert_ids;
65 cert = sk_X509_value(chain, 0);
66 if (ess_find_cert(cert_ids, cert) != 0)
70 * Check the other certificates of the chain.
71 * Fail if no signing certificate ids found for each certificate.
73 if (sk_ESS_CERT_ID_num(cert_ids) > 1) {
74 /* for each chain cert, try to find its cert id */
75 for (i = 1; i < sk_X509_num(chain); ++i) {
76 cert = sk_X509_value(chain, i);
77 if (ess_find_cert(cert_ids, cert) < 0)
81 } else if (cms_signerinfo_get_signing_cert_v2(si, &ssv2) > 0
82 && ssv2->cert_ids!= NULL) {
83 STACK_OF(ESS_CERT_ID_V2) *cert_ids_v2 = ssv2->cert_ids;
85 cert = sk_X509_value(chain, 0);
86 if (ess_find_cert_v2(cert_ids_v2, cert) != 0)
90 * Check the other certificates of the chain.
91 * Fail if no signing certificate ids found for each certificate.
93 if (sk_ESS_CERT_ID_V2_num(cert_ids_v2) > 1) {
94 /* for each chain cert, try to find its cert id */
95 for (i = 1; i < sk_X509_num(chain); ++i) {
96 cert = sk_X509_value(chain, i);
97 if (ess_find_cert_v2(cert_ids_v2, cert) < 0)
102 ERR_raise(ERR_LIB_CMS, CMS_R_ESS_NO_SIGNING_CERTID_ATTRIBUTE);
108 ERR_raise(ERR_LIB_CMS, CMS_R_ESS_SIGNING_CERTID_MISMATCH_ERROR);
110 ESS_SIGNING_CERT_free(ss);
111 ESS_SIGNING_CERT_V2_free(ssv2);
115 CMS_ReceiptRequest *CMS_ReceiptRequest_create0_ex(
116 unsigned char *id, int idlen, int allorfirst,
117 STACK_OF(GENERAL_NAMES) *receiptList, STACK_OF(GENERAL_NAMES) *receiptsTo,
118 OSSL_LIB_CTX *libctx, const char *propq)
120 CMS_ReceiptRequest *rr;
122 rr = CMS_ReceiptRequest_new();
126 ASN1_STRING_set0(rr->signedContentIdentifier, id, idlen);
128 if (!ASN1_STRING_set(rr->signedContentIdentifier, NULL, 32))
130 if (RAND_bytes_ex(libctx, rr->signedContentIdentifier->data, 32) <= 0)
134 sk_GENERAL_NAMES_pop_free(rr->receiptsTo, GENERAL_NAMES_free);
135 rr->receiptsTo = receiptsTo;
137 if (receiptList != NULL) {
138 rr->receiptsFrom->type = 1;
139 rr->receiptsFrom->d.receiptList = receiptList;
141 rr->receiptsFrom->type = 0;
142 rr->receiptsFrom->d.allOrFirstTier = allorfirst;
148 ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
151 CMS_ReceiptRequest_free(rr);
156 CMS_ReceiptRequest *CMS_ReceiptRequest_create0(
157 unsigned char *id, int idlen, int allorfirst,
158 STACK_OF(GENERAL_NAMES) *receiptList, STACK_OF(GENERAL_NAMES) *receiptsTo)
160 return CMS_ReceiptRequest_create0_ex(id, idlen, allorfirst, receiptList,
161 receiptsTo, NULL, NULL);
164 int CMS_add1_ReceiptRequest(CMS_SignerInfo *si, CMS_ReceiptRequest *rr)
166 unsigned char *rrder = NULL;
169 rrderlen = i2d_CMS_ReceiptRequest(rr, &rrder);
173 if (!CMS_signed_add1_attr_by_NID(si, NID_id_smime_aa_receiptRequest,
174 V_ASN1_SEQUENCE, rrder, rrderlen))
181 ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
189 void CMS_ReceiptRequest_get0_values(CMS_ReceiptRequest *rr,
192 STACK_OF(GENERAL_NAMES) **plist,
193 STACK_OF(GENERAL_NAMES) **prto)
196 *pcid = rr->signedContentIdentifier;
197 if (rr->receiptsFrom->type == 0) {
198 if (pallorfirst != NULL)
199 *pallorfirst = (int)rr->receiptsFrom->d.allOrFirstTier;
203 if (pallorfirst != NULL)
206 *plist = rr->receiptsFrom->d.receiptList;
209 *prto = rr->receiptsTo;
212 /* Digest a SignerInfo structure for msgSigDigest attribute processing */
214 static int cms_msgSigDigest(CMS_SignerInfo *si,
215 unsigned char *dig, unsigned int *diglen)
217 const EVP_MD *md = EVP_get_digestbyobj(si->digestAlgorithm->algorithm);
221 if (!asn1_item_digest_ex(ASN1_ITEM_rptr(CMS_Attributes_Verify), md,
222 si->signedAttrs, dig, diglen,
223 cms_ctx_get0_libctx(si->cms_ctx),
224 cms_ctx_get0_propq(si->cms_ctx)))
229 /* Add a msgSigDigest attribute to a SignerInfo */
231 int cms_msgSigDigest_add1(CMS_SignerInfo *dest, CMS_SignerInfo *src)
233 unsigned char dig[EVP_MAX_MD_SIZE];
236 if (!cms_msgSigDigest(src, dig, &diglen)) {
237 ERR_raise(ERR_LIB_CMS, CMS_R_MSGSIGDIGEST_ERROR);
240 if (!CMS_signed_add1_attr_by_NID(dest, NID_id_smime_aa_msgSigDigest,
241 V_ASN1_OCTET_STRING, dig, diglen)) {
242 ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
248 /* Verify signed receipt after it has already passed normal CMS verify */
250 int cms_Receipt_verify(CMS_ContentInfo *cms, CMS_ContentInfo *req_cms)
253 CMS_ReceiptRequest *rr = NULL;
254 CMS_Receipt *rct = NULL;
255 STACK_OF(CMS_SignerInfo) *sis, *osis;
256 CMS_SignerInfo *si, *osi = NULL;
257 ASN1_OCTET_STRING *msig, **pcont;
259 unsigned char dig[EVP_MAX_MD_SIZE];
262 /* Get SignerInfos, also checks SignedData content type */
263 osis = CMS_get0_SignerInfos(req_cms);
264 sis = CMS_get0_SignerInfos(cms);
268 if (sk_CMS_SignerInfo_num(sis) != 1) {
269 ERR_raise(ERR_LIB_CMS, CMS_R_NEED_ONE_SIGNER);
273 /* Check receipt content type */
274 if (OBJ_obj2nid(CMS_get0_eContentType(cms)) != NID_id_smime_ct_receipt) {
275 ERR_raise(ERR_LIB_CMS, CMS_R_NOT_A_SIGNED_RECEIPT);
279 /* Extract and decode receipt content */
280 pcont = CMS_get0_content(cms);
281 if (pcont == NULL || *pcont == NULL) {
282 ERR_raise(ERR_LIB_CMS, CMS_R_NO_CONTENT);
286 rct = ASN1_item_unpack(*pcont, ASN1_ITEM_rptr(CMS_Receipt));
289 ERR_raise(ERR_LIB_CMS, CMS_R_RECEIPT_DECODE_ERROR);
293 /* Locate original request */
295 for (i = 0; i < sk_CMS_SignerInfo_num(osis); i++) {
296 osi = sk_CMS_SignerInfo_value(osis, i);
297 if (!ASN1_STRING_cmp(osi->signature, rct->originatorSignatureValue))
301 if (i == sk_CMS_SignerInfo_num(osis)) {
302 ERR_raise(ERR_LIB_CMS, CMS_R_NO_MATCHING_SIGNATURE);
306 si = sk_CMS_SignerInfo_value(sis, 0);
308 /* Get msgSigDigest value and compare */
310 msig = CMS_signed_get0_data_by_OBJ(si,
312 (NID_id_smime_aa_msgSigDigest), -3,
313 V_ASN1_OCTET_STRING);
316 ERR_raise(ERR_LIB_CMS, CMS_R_NO_MSGSIGDIGEST);
320 if (!cms_msgSigDigest(osi, dig, &diglen)) {
321 ERR_raise(ERR_LIB_CMS, CMS_R_MSGSIGDIGEST_ERROR);
325 if (diglen != (unsigned int)msig->length) {
326 ERR_raise(ERR_LIB_CMS, CMS_R_MSGSIGDIGEST_WRONG_LENGTH);
330 if (memcmp(dig, msig->data, diglen)) {
331 ERR_raise(ERR_LIB_CMS, CMS_R_MSGSIGDIGEST_VERIFICATION_FAILURE);
335 /* Compare content types */
337 octype = CMS_signed_get0_data_by_OBJ(osi,
338 OBJ_nid2obj(NID_pkcs9_contentType),
341 ERR_raise(ERR_LIB_CMS, CMS_R_NO_CONTENT_TYPE);
345 /* Compare details in receipt request */
347 if (OBJ_cmp(octype, rct->contentType)) {
348 ERR_raise(ERR_LIB_CMS, CMS_R_CONTENT_TYPE_MISMATCH);
352 /* Get original receipt request details */
354 if (CMS_get1_ReceiptRequest(osi, &rr) <= 0) {
355 ERR_raise(ERR_LIB_CMS, CMS_R_NO_RECEIPT_REQUEST);
359 if (ASN1_STRING_cmp(rr->signedContentIdentifier,
360 rct->signedContentIdentifier)) {
361 ERR_raise(ERR_LIB_CMS, CMS_R_CONTENTIDENTIFIER_MISMATCH);
368 CMS_ReceiptRequest_free(rr);
369 M_ASN1_free_of(rct, CMS_Receipt);
375 * Encode a Receipt into an OCTET STRING read for including into content of a
376 * SignedData ContentInfo.
379 ASN1_OCTET_STRING *cms_encode_Receipt(CMS_SignerInfo *si)
382 CMS_ReceiptRequest *rr = NULL;
384 ASN1_OCTET_STRING *os = NULL;
386 /* Get original receipt request */
388 /* Get original receipt request details */
390 if (CMS_get1_ReceiptRequest(si, &rr) <= 0) {
391 ERR_raise(ERR_LIB_CMS, CMS_R_NO_RECEIPT_REQUEST);
395 /* Get original content type */
397 ctype = CMS_signed_get0_data_by_OBJ(si,
398 OBJ_nid2obj(NID_pkcs9_contentType),
401 ERR_raise(ERR_LIB_CMS, CMS_R_NO_CONTENT_TYPE);
406 rct.contentType = ctype;
407 rct.signedContentIdentifier = rr->signedContentIdentifier;
408 rct.originatorSignatureValue = si->signature;
410 os = ASN1_item_pack(&rct, ASN1_ITEM_rptr(CMS_Receipt), NULL);
413 CMS_ReceiptRequest_free(rr);
418 * Add signer certificate's V2 digest |sc| to a SignerInfo structure |si|
421 int cms_add1_signing_cert_v2(CMS_SignerInfo *si, ESS_SIGNING_CERT_V2 *sc)
423 ASN1_STRING *seq = NULL;
424 unsigned char *p, *pp = NULL;
427 /* Add SigningCertificateV2 signed attribute to the signer info. */
428 len = i2d_ESS_SIGNING_CERT_V2(sc, NULL);
429 if (len <= 0 || (pp = OPENSSL_malloc(len)) == NULL)
432 i2d_ESS_SIGNING_CERT_V2(sc, &p);
433 if (!(seq = ASN1_STRING_new()) || !ASN1_STRING_set(seq, pp, len))
437 if (!CMS_signed_add1_attr_by_NID(si, NID_id_smime_aa_signingCertificateV2,
438 V_ASN1_SEQUENCE, seq, -1))
440 ASN1_STRING_free(seq);
443 ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
444 ASN1_STRING_free(seq);
450 * Add signer certificate's digest |sc| to a SignerInfo structure |si|
453 int cms_add1_signing_cert(CMS_SignerInfo *si, ESS_SIGNING_CERT *sc)
455 ASN1_STRING *seq = NULL;
456 unsigned char *p, *pp = NULL;
459 /* Add SigningCertificate signed attribute to the signer info. */
460 len = i2d_ESS_SIGNING_CERT(sc, NULL);
461 if (len <= 0 || (pp = OPENSSL_malloc(len)) == NULL)
464 i2d_ESS_SIGNING_CERT(sc, &p);
465 if (!(seq = ASN1_STRING_new()) || !ASN1_STRING_set(seq, pp, len))
469 if (!CMS_signed_add1_attr_by_NID(si, NID_id_smime_aa_signingCertificate,
470 V_ASN1_SEQUENCE, seq, -1))
472 ASN1_STRING_free(seq);
475 ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
476 ASN1_STRING_free(seq);