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 "cms_local.h"
19 #include "crypto/ess.h"
20 #include "crypto/cms.h"
22 DEFINE_STACK_OF(GENERAL_NAMES)
23 DEFINE_STACK_OF(CMS_SignerInfo)
24 DEFINE_STACK_OF(ESS_CERT_ID)
25 DEFINE_STACK_OF(ESS_CERT_ID_V2)
28 IMPLEMENT_ASN1_FUNCTIONS(CMS_ReceiptRequest)
32 int CMS_get1_ReceiptRequest(CMS_SignerInfo *si, CMS_ReceiptRequest **prr)
35 CMS_ReceiptRequest *rr;
36 ASN1_OBJECT *obj = OBJ_nid2obj(NID_id_smime_aa_receiptRequest);
40 str = CMS_signed_get0_data_by_OBJ(si, obj, -3, V_ASN1_SEQUENCE);
44 rr = ASN1_item_unpack(str, ASN1_ITEM_rptr(CMS_ReceiptRequest));
50 CMS_ReceiptRequest_free(rr);
55 First, get the ESS_SIGNING_CERT(V2) signed attribute from |si|.
56 Then check matching of each cert of trust |chain| with one of
57 the |cert_ids|(Hash+IssuerID) list from this ESS_SIGNING_CERT.
58 Derived from ts_check_signing_certs()
60 int ess_check_signing_certs(CMS_SignerInfo *si, STACK_OF(X509) *chain)
62 ESS_SIGNING_CERT *ss = NULL;
63 ESS_SIGNING_CERT_V2 *ssv2 = NULL;
67 if (cms_signerinfo_get_signing_cert(si, &ss) > 0 && ss->cert_ids != NULL) {
68 STACK_OF(ESS_CERT_ID) *cert_ids = ss->cert_ids;
70 cert = sk_X509_value(chain, 0);
71 if (ess_find_cert(cert_ids, cert) != 0)
75 * Check the other certificates of the chain.
76 * Fail if no signing certificate ids found for each certificate.
78 if (sk_ESS_CERT_ID_num(cert_ids) > 1) {
79 /* for each chain cert, try to find its cert id */
80 for (i = 1; i < sk_X509_num(chain); ++i) {
81 cert = sk_X509_value(chain, i);
82 if (ess_find_cert(cert_ids, cert) < 0)
86 } else if (cms_signerinfo_get_signing_cert_v2(si, &ssv2) > 0
87 && ssv2->cert_ids!= NULL) {
88 STACK_OF(ESS_CERT_ID_V2) *cert_ids_v2 = ssv2->cert_ids;
90 cert = sk_X509_value(chain, 0);
91 if (ess_find_cert_v2(cert_ids_v2, cert) != 0)
95 * Check the other certificates of the chain.
96 * Fail if no signing certificate ids found for each certificate.
98 if (sk_ESS_CERT_ID_V2_num(cert_ids_v2) > 1) {
99 /* for each chain cert, try to find its cert id */
100 for (i = 1; i < sk_X509_num(chain); ++i) {
101 cert = sk_X509_value(chain, i);
102 if (ess_find_cert_v2(cert_ids_v2, cert) < 0)
107 CMSerr(CMS_F_ESS_CHECK_SIGNING_CERTS,
108 CMS_R_ESS_NO_SIGNING_CERTID_ATTRIBUTE);
114 CMSerr(CMS_F_ESS_CHECK_SIGNING_CERTS,
115 CMS_R_ESS_SIGNING_CERTID_MISMATCH_ERROR);
117 ESS_SIGNING_CERT_free(ss);
118 ESS_SIGNING_CERT_V2_free(ssv2);
122 CMS_ReceiptRequest *CMS_ReceiptRequest_create0(unsigned char *id, int idlen,
124 STACK_OF(GENERAL_NAMES)
125 *receiptList, STACK_OF(GENERAL_NAMES)
128 CMS_ReceiptRequest *rr;
130 rr = CMS_ReceiptRequest_new();
134 ASN1_STRING_set0(rr->signedContentIdentifier, id, idlen);
136 if (!ASN1_STRING_set(rr->signedContentIdentifier, NULL, 32))
138 if (RAND_bytes(rr->signedContentIdentifier->data, 32) <= 0)
142 sk_GENERAL_NAMES_pop_free(rr->receiptsTo, GENERAL_NAMES_free);
143 rr->receiptsTo = receiptsTo;
146 rr->receiptsFrom->type = 1;
147 rr->receiptsFrom->d.receiptList = receiptList;
149 rr->receiptsFrom->type = 0;
150 rr->receiptsFrom->d.allOrFirstTier = allorfirst;
156 CMSerr(CMS_F_CMS_RECEIPTREQUEST_CREATE0, ERR_R_MALLOC_FAILURE);
159 CMS_ReceiptRequest_free(rr);
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 CMSerr(CMS_F_CMS_ADD1_RECEIPTREQUEST, 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) {
199 *pallorfirst = (int)rr->receiptsFrom->d.allOrFirstTier;
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)
219 md = EVP_get_digestbyobj(si->digestAlgorithm->algorithm);
222 if (!ASN1_item_digest(ASN1_ITEM_rptr(CMS_Attributes_Verify), md,
223 si->signedAttrs, dig, diglen))
228 /* Add a msgSigDigest attribute to a SignerInfo */
230 int cms_msgSigDigest_add1(CMS_SignerInfo *dest, CMS_SignerInfo *src)
232 unsigned char dig[EVP_MAX_MD_SIZE];
235 if (!cms_msgSigDigest(src, dig, &diglen)) {
236 CMSerr(CMS_F_CMS_MSGSIGDIGEST_ADD1, CMS_R_MSGSIGDIGEST_ERROR);
239 if (!CMS_signed_add1_attr_by_NID(dest, NID_id_smime_aa_msgSigDigest,
240 V_ASN1_OCTET_STRING, dig, diglen)) {
241 CMSerr(CMS_F_CMS_MSGSIGDIGEST_ADD1, ERR_R_MALLOC_FAILURE);
247 /* Verify signed receipt after it has already passed normal CMS verify */
249 int cms_Receipt_verify(CMS_ContentInfo *cms, CMS_ContentInfo *req_cms)
252 CMS_ReceiptRequest *rr = NULL;
253 CMS_Receipt *rct = NULL;
254 STACK_OF(CMS_SignerInfo) *sis, *osis;
255 CMS_SignerInfo *si, *osi = NULL;
256 ASN1_OCTET_STRING *msig, **pcont;
258 unsigned char dig[EVP_MAX_MD_SIZE];
261 /* Get SignerInfos, also checks SignedData content type */
262 osis = CMS_get0_SignerInfos(req_cms);
263 sis = CMS_get0_SignerInfos(cms);
267 if (sk_CMS_SignerInfo_num(sis) != 1) {
268 CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_NEED_ONE_SIGNER);
272 /* Check receipt content type */
273 if (OBJ_obj2nid(CMS_get0_eContentType(cms)) != NID_id_smime_ct_receipt) {
274 CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_NOT_A_SIGNED_RECEIPT);
278 /* Extract and decode receipt content */
279 pcont = CMS_get0_content(cms);
280 if (pcont == NULL || *pcont == NULL) {
281 CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_NO_CONTENT);
285 rct = ASN1_item_unpack(*pcont, ASN1_ITEM_rptr(CMS_Receipt));
288 CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_RECEIPT_DECODE_ERROR);
292 /* Locate original request */
294 for (i = 0; i < sk_CMS_SignerInfo_num(osis); i++) {
295 osi = sk_CMS_SignerInfo_value(osis, i);
296 if (!ASN1_STRING_cmp(osi->signature, rct->originatorSignatureValue))
300 if (i == sk_CMS_SignerInfo_num(osis)) {
301 CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_NO_MATCHING_SIGNATURE);
305 si = sk_CMS_SignerInfo_value(sis, 0);
307 /* Get msgSigDigest value and compare */
309 msig = CMS_signed_get0_data_by_OBJ(si,
311 (NID_id_smime_aa_msgSigDigest), -3,
312 V_ASN1_OCTET_STRING);
315 CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_NO_MSGSIGDIGEST);
319 if (!cms_msgSigDigest(osi, dig, &diglen)) {
320 CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_MSGSIGDIGEST_ERROR);
324 if (diglen != (unsigned int)msig->length) {
325 CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_MSGSIGDIGEST_WRONG_LENGTH);
329 if (memcmp(dig, msig->data, diglen)) {
330 CMSerr(CMS_F_CMS_RECEIPT_VERIFY,
331 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 CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_NO_CONTENT_TYPE);
345 /* Compare details in receipt request */
347 if (OBJ_cmp(octype, rct->contentType)) {
348 CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_CONTENT_TYPE_MISMATCH);
352 /* Get original receipt request details */
354 if (CMS_get1_ReceiptRequest(osi, &rr) <= 0) {
355 CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_NO_RECEIPT_REQUEST);
359 if (ASN1_STRING_cmp(rr->signedContentIdentifier,
360 rct->signedContentIdentifier)) {
361 CMSerr(CMS_F_CMS_RECEIPT_VERIFY, 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 CMSerr(CMS_F_CMS_ENCODE_RECEIPT, 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 CMSerr(CMS_F_CMS_ENCODE_RECEIPT, 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;
427 /* Add SigningCertificateV2 signed attribute to the signer info. */
428 len = i2d_ESS_SIGNING_CERT_V2(sc, NULL);
429 if ((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 CMSerr(CMS_F_CMS_ADD1_SIGNING_CERT_V2, 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;
459 /* Add SigningCertificate signed attribute to the signer info. */
460 len = i2d_ESS_SIGNING_CERT(sc, NULL);
461 if ((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 CMSerr(CMS_F_CMS_ADD1_SIGNING_CERT, ERR_R_MALLOC_FAILURE);
476 ASN1_STRING_free(seq);