2 * Copyright 2008-2020 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 CMSerr(CMS_F_ESS_CHECK_SIGNING_CERTS,
103 CMS_R_ESS_NO_SIGNING_CERTID_ATTRIBUTE);
109 CMSerr(CMS_F_ESS_CHECK_SIGNING_CERTS,
110 CMS_R_ESS_SIGNING_CERTID_MISMATCH_ERROR);
112 ESS_SIGNING_CERT_free(ss);
113 ESS_SIGNING_CERT_V2_free(ssv2);
117 CMS_ReceiptRequest *CMS_ReceiptRequest_create0_with_libctx(
118 unsigned char *id, int idlen, int allorfirst,
119 STACK_OF(GENERAL_NAMES) *receiptList, STACK_OF(GENERAL_NAMES) *receiptsTo,
120 OPENSSL_CTX *libctx, const char *propq)
122 CMS_ReceiptRequest *rr;
124 rr = CMS_ReceiptRequest_new();
128 ASN1_STRING_set0(rr->signedContentIdentifier, id, idlen);
130 if (!ASN1_STRING_set(rr->signedContentIdentifier, NULL, 32))
132 if (RAND_bytes_ex(libctx, rr->signedContentIdentifier->data, 32) <= 0)
136 sk_GENERAL_NAMES_pop_free(rr->receiptsTo, GENERAL_NAMES_free);
137 rr->receiptsTo = receiptsTo;
139 if (receiptList != NULL) {
140 rr->receiptsFrom->type = 1;
141 rr->receiptsFrom->d.receiptList = receiptList;
143 rr->receiptsFrom->type = 0;
144 rr->receiptsFrom->d.allOrFirstTier = allorfirst;
150 CMSerr(0, ERR_R_MALLOC_FAILURE);
153 CMS_ReceiptRequest_free(rr);
158 CMS_ReceiptRequest *CMS_ReceiptRequest_create0(
159 unsigned char *id, int idlen, int allorfirst,
160 STACK_OF(GENERAL_NAMES) *receiptList, STACK_OF(GENERAL_NAMES) *receiptsTo)
162 return CMS_ReceiptRequest_create0_with_libctx(id, idlen, allorfirst,
163 receiptList, receiptsTo,
167 int CMS_add1_ReceiptRequest(CMS_SignerInfo *si, CMS_ReceiptRequest *rr)
169 unsigned char *rrder = NULL;
172 rrderlen = i2d_CMS_ReceiptRequest(rr, &rrder);
176 if (!CMS_signed_add1_attr_by_NID(si, NID_id_smime_aa_receiptRequest,
177 V_ASN1_SEQUENCE, rrder, rrderlen))
184 CMSerr(CMS_F_CMS_ADD1_RECEIPTREQUEST, ERR_R_MALLOC_FAILURE);
192 void CMS_ReceiptRequest_get0_values(CMS_ReceiptRequest *rr,
195 STACK_OF(GENERAL_NAMES) **plist,
196 STACK_OF(GENERAL_NAMES) **prto)
199 *pcid = rr->signedContentIdentifier;
200 if (rr->receiptsFrom->type == 0) {
201 if (pallorfirst != NULL)
202 *pallorfirst = (int)rr->receiptsFrom->d.allOrFirstTier;
206 if (pallorfirst != NULL)
209 *plist = rr->receiptsFrom->d.receiptList;
212 *prto = rr->receiptsTo;
215 /* Digest a SignerInfo structure for msgSigDigest attribute processing */
217 static int cms_msgSigDigest(CMS_SignerInfo *si,
218 unsigned char *dig, unsigned int *diglen)
220 const EVP_MD *md = EVP_get_digestbyobj(si->digestAlgorithm->algorithm);
224 if (!asn1_item_digest_with_libctx(ASN1_ITEM_rptr(CMS_Attributes_Verify), md,
225 si->signedAttrs, dig, diglen,
226 si->cms_ctx->libctx, si->cms_ctx->propq))
231 /* Add a msgSigDigest attribute to a SignerInfo */
233 int cms_msgSigDigest_add1(CMS_SignerInfo *dest, CMS_SignerInfo *src)
235 unsigned char dig[EVP_MAX_MD_SIZE];
238 if (!cms_msgSigDigest(src, dig, &diglen)) {
239 CMSerr(CMS_F_CMS_MSGSIGDIGEST_ADD1, CMS_R_MSGSIGDIGEST_ERROR);
242 if (!CMS_signed_add1_attr_by_NID(dest, NID_id_smime_aa_msgSigDigest,
243 V_ASN1_OCTET_STRING, dig, diglen)) {
244 CMSerr(CMS_F_CMS_MSGSIGDIGEST_ADD1, ERR_R_MALLOC_FAILURE);
250 /* Verify signed receipt after it has already passed normal CMS verify */
252 int cms_Receipt_verify(CMS_ContentInfo *cms, CMS_ContentInfo *req_cms)
255 CMS_ReceiptRequest *rr = NULL;
256 CMS_Receipt *rct = NULL;
257 STACK_OF(CMS_SignerInfo) *sis, *osis;
258 CMS_SignerInfo *si, *osi = NULL;
259 ASN1_OCTET_STRING *msig, **pcont;
261 unsigned char dig[EVP_MAX_MD_SIZE];
264 /* Get SignerInfos, also checks SignedData content type */
265 osis = CMS_get0_SignerInfos(req_cms);
266 sis = CMS_get0_SignerInfos(cms);
270 if (sk_CMS_SignerInfo_num(sis) != 1) {
271 CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_NEED_ONE_SIGNER);
275 /* Check receipt content type */
276 if (OBJ_obj2nid(CMS_get0_eContentType(cms)) != NID_id_smime_ct_receipt) {
277 CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_NOT_A_SIGNED_RECEIPT);
281 /* Extract and decode receipt content */
282 pcont = CMS_get0_content(cms);
283 if (pcont == NULL || *pcont == NULL) {
284 CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_NO_CONTENT);
288 rct = ASN1_item_unpack(*pcont, ASN1_ITEM_rptr(CMS_Receipt));
291 CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_RECEIPT_DECODE_ERROR);
295 /* Locate original request */
297 for (i = 0; i < sk_CMS_SignerInfo_num(osis); i++) {
298 osi = sk_CMS_SignerInfo_value(osis, i);
299 if (!ASN1_STRING_cmp(osi->signature, rct->originatorSignatureValue))
303 if (i == sk_CMS_SignerInfo_num(osis)) {
304 CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_NO_MATCHING_SIGNATURE);
308 si = sk_CMS_SignerInfo_value(sis, 0);
310 /* Get msgSigDigest value and compare */
312 msig = CMS_signed_get0_data_by_OBJ(si,
314 (NID_id_smime_aa_msgSigDigest), -3,
315 V_ASN1_OCTET_STRING);
318 CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_NO_MSGSIGDIGEST);
322 if (!cms_msgSigDigest(osi, dig, &diglen)) {
323 CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_MSGSIGDIGEST_ERROR);
327 if (diglen != (unsigned int)msig->length) {
328 CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_MSGSIGDIGEST_WRONG_LENGTH);
332 if (memcmp(dig, msig->data, diglen)) {
333 CMSerr(CMS_F_CMS_RECEIPT_VERIFY,
334 CMS_R_MSGSIGDIGEST_VERIFICATION_FAILURE);
338 /* Compare content types */
340 octype = CMS_signed_get0_data_by_OBJ(osi,
341 OBJ_nid2obj(NID_pkcs9_contentType),
344 CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_NO_CONTENT_TYPE);
348 /* Compare details in receipt request */
350 if (OBJ_cmp(octype, rct->contentType)) {
351 CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_CONTENT_TYPE_MISMATCH);
355 /* Get original receipt request details */
357 if (CMS_get1_ReceiptRequest(osi, &rr) <= 0) {
358 CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_NO_RECEIPT_REQUEST);
362 if (ASN1_STRING_cmp(rr->signedContentIdentifier,
363 rct->signedContentIdentifier)) {
364 CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_CONTENTIDENTIFIER_MISMATCH);
371 CMS_ReceiptRequest_free(rr);
372 M_ASN1_free_of(rct, CMS_Receipt);
378 * Encode a Receipt into an OCTET STRING read for including into content of a
379 * SignedData ContentInfo.
382 ASN1_OCTET_STRING *cms_encode_Receipt(CMS_SignerInfo *si)
385 CMS_ReceiptRequest *rr = NULL;
387 ASN1_OCTET_STRING *os = NULL;
389 /* Get original receipt request */
391 /* Get original receipt request details */
393 if (CMS_get1_ReceiptRequest(si, &rr) <= 0) {
394 CMSerr(CMS_F_CMS_ENCODE_RECEIPT, CMS_R_NO_RECEIPT_REQUEST);
398 /* Get original content type */
400 ctype = CMS_signed_get0_data_by_OBJ(si,
401 OBJ_nid2obj(NID_pkcs9_contentType),
404 CMSerr(CMS_F_CMS_ENCODE_RECEIPT, CMS_R_NO_CONTENT_TYPE);
409 rct.contentType = ctype;
410 rct.signedContentIdentifier = rr->signedContentIdentifier;
411 rct.originatorSignatureValue = si->signature;
413 os = ASN1_item_pack(&rct, ASN1_ITEM_rptr(CMS_Receipt), NULL);
416 CMS_ReceiptRequest_free(rr);
421 * Add signer certificate's V2 digest |sc| to a SignerInfo structure |si|
424 int cms_add1_signing_cert_v2(CMS_SignerInfo *si, ESS_SIGNING_CERT_V2 *sc)
426 ASN1_STRING *seq = NULL;
427 unsigned char *p, *pp = NULL;
430 /* Add SigningCertificateV2 signed attribute to the signer info. */
431 len = i2d_ESS_SIGNING_CERT_V2(sc, NULL);
432 if (len <= 0 || (pp = OPENSSL_malloc(len)) == NULL)
435 i2d_ESS_SIGNING_CERT_V2(sc, &p);
436 if (!(seq = ASN1_STRING_new()) || !ASN1_STRING_set(seq, pp, len))
440 if (!CMS_signed_add1_attr_by_NID(si, NID_id_smime_aa_signingCertificateV2,
441 V_ASN1_SEQUENCE, seq, -1))
443 ASN1_STRING_free(seq);
446 CMSerr(CMS_F_CMS_ADD1_SIGNING_CERT_V2, ERR_R_MALLOC_FAILURE);
447 ASN1_STRING_free(seq);
453 * Add signer certificate's digest |sc| to a SignerInfo structure |si|
456 int cms_add1_signing_cert(CMS_SignerInfo *si, ESS_SIGNING_CERT *sc)
458 ASN1_STRING *seq = NULL;
459 unsigned char *p, *pp = NULL;
462 /* Add SigningCertificate signed attribute to the signer info. */
463 len = i2d_ESS_SIGNING_CERT(sc, NULL);
464 if (len <= 0 || (pp = OPENSSL_malloc(len)) == NULL)
467 i2d_ESS_SIGNING_CERT(sc, &p);
468 if (!(seq = ASN1_STRING_new()) || !ASN1_STRING_set(seq, pp, len))
472 if (!CMS_signed_add1_attr_by_NID(si, NID_id_smime_aa_signingCertificate,
473 V_ASN1_SEQUENCE, seq, -1))
475 ASN1_STRING_free(seq);
478 CMSerr(CMS_F_CMS_ADD1_SIGNING_CERT, ERR_R_MALLOC_FAILURE);
479 ASN1_STRING_free(seq);