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 <openssl/asn1t.h>
11 #include <openssl/x509v3.h>
12 #include <openssl/err.h>
13 #include <openssl/pem.h>
14 #include <openssl/bio.h>
15 #include <openssl/asn1.h>
16 #include <openssl/cms.h>
17 #include <openssl/cms.h>
18 #include "crypto/x509.h"
19 #include "cms_local.h"
21 static STACK_OF(CMS_CertificateChoices)
22 **cms_get0_certificate_choices(CMS_ContentInfo *cms);
24 IMPLEMENT_ASN1_PRINT_FUNCTION(CMS_ContentInfo)
26 CMS_ContentInfo *d2i_CMS_ContentInfo(CMS_ContentInfo **a,
27 const unsigned char **in, long len)
31 ci = (CMS_ContentInfo *)ASN1_item_d2i((ASN1_VALUE **)a, in, len,
32 (CMS_ContentInfo_it()));
34 cms_resolve_libctx(ci);
38 int i2d_CMS_ContentInfo(const CMS_ContentInfo *a, unsigned char **out)
40 return ASN1_item_i2d((const ASN1_VALUE *)a, out, (CMS_ContentInfo_it()));
43 CMS_ContentInfo *CMS_ContentInfo_new_ex(OSSL_LIB_CTX *libctx, const char *propq)
47 ci = (CMS_ContentInfo *)ASN1_item_new(ASN1_ITEM_rptr(CMS_ContentInfo));
49 ci->ctx.libctx = libctx;
52 ci->ctx.propq = OPENSSL_strdup(propq);
53 if (ci->ctx.propq == NULL) {
54 CMS_ContentInfo_free(ci);
56 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
63 CMS_ContentInfo *CMS_ContentInfo_new(void)
65 return CMS_ContentInfo_new_ex(NULL, NULL);
68 void CMS_ContentInfo_free(CMS_ContentInfo *cms)
71 OPENSSL_free(cms->ctx.propq);
72 ASN1_item_free((ASN1_VALUE *)cms, ASN1_ITEM_rptr(CMS_ContentInfo));
76 const CMS_CTX *cms_get0_cmsctx(const CMS_ContentInfo *cms)
78 return cms != NULL ? &cms->ctx : NULL;
81 OSSL_LIB_CTX *cms_ctx_get0_libctx(const CMS_CTX *ctx)
83 return ctx != NULL ? ctx->libctx : NULL;
86 const char *cms_ctx_get0_propq(const CMS_CTX *ctx)
88 return ctx != NULL ? ctx->propq : NULL;
91 void cms_resolve_libctx(CMS_ContentInfo *ci)
94 CMS_CertificateChoices *cch;
95 STACK_OF(CMS_CertificateChoices) **pcerts;
96 const CMS_CTX *ctx = cms_get0_cmsctx(ci);
97 OSSL_LIB_CTX *libctx = cms_ctx_get0_libctx(ctx);
98 const char *propq = cms_ctx_get0_propq(ctx);
100 cms_SignerInfos_set_cmsctx(ci);
101 cms_RecipientInfos_set_cmsctx(ci);
103 pcerts = cms_get0_certificate_choices(ci);
104 if (pcerts != NULL) {
105 for (i = 0; i < sk_CMS_CertificateChoices_num(*pcerts); i++) {
106 cch = sk_CMS_CertificateChoices_value(*pcerts, i);
107 if (cch->type == CMS_CERTCHOICE_CERT)
108 x509_set0_libctx(cch->d.certificate, libctx, propq);
113 const ASN1_OBJECT *CMS_get0_type(const CMS_ContentInfo *cms)
115 return cms->contentType;
118 CMS_ContentInfo *cms_Data_create(OSSL_LIB_CTX *libctx, const char *propq)
120 CMS_ContentInfo *cms = CMS_ContentInfo_new_ex(libctx, propq);
123 cms->contentType = OBJ_nid2obj(NID_pkcs7_data);
125 CMS_set_detached(cms, 0);
130 BIO *cms_content_bio(CMS_ContentInfo *cms)
132 ASN1_OCTET_STRING **pos = CMS_get0_content(cms);
136 /* If content detached data goes nowhere: create NULL BIO */
138 return BIO_new(BIO_s_null());
140 * If content not detached and created return memory BIO
142 if (*pos == NULL || ((*pos)->flags == ASN1_STRING_FLAG_CONT))
143 return BIO_new(BIO_s_mem());
144 /* Else content was read in: return read only BIO for it */
145 return BIO_new_mem_buf((*pos)->data, (*pos)->length);
148 BIO *CMS_dataInit(CMS_ContentInfo *cms, BIO *icont)
154 cont = cms_content_bio(cms);
156 ERR_raise(ERR_LIB_CMS, CMS_R_NO_CONTENT);
159 switch (OBJ_obj2nid(cms->contentType)) {
164 case NID_pkcs7_signed:
165 cmsbio = cms_SignedData_init_bio(cms);
168 case NID_pkcs7_digest:
169 cmsbio = cms_DigestedData_init_bio(cms);
172 case NID_id_smime_ct_compressedData:
173 cmsbio = cms_CompressedData_init_bio(cms);
177 case NID_pkcs7_encrypted:
178 cmsbio = cms_EncryptedData_init_bio(cms);
181 case NID_pkcs7_enveloped:
182 cmsbio = cms_EnvelopedData_init_bio(cms);
185 case NID_id_smime_ct_authEnvelopedData:
186 cmsbio = cms_AuthEnvelopedData_init_bio(cms);
190 ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_TYPE);
195 return BIO_push(cmsbio, cont);
203 /* unfortunately cannot constify SMIME_write_ASN1() due to this function */
204 int CMS_dataFinal(CMS_ContentInfo *cms, BIO *cmsbio)
206 ASN1_OCTET_STRING **pos = CMS_get0_content(cms);
210 /* If embedded content find memory BIO and set content */
211 if (*pos && ((*pos)->flags & ASN1_STRING_FLAG_CONT)) {
215 mbio = BIO_find_type(cmsbio, BIO_TYPE_MEM);
217 ERR_raise(ERR_LIB_CMS, CMS_R_CONTENT_NOT_FOUND);
220 contlen = BIO_get_mem_data(mbio, &cont);
221 /* Set bio as read only so its content can't be clobbered */
222 BIO_set_flags(mbio, BIO_FLAGS_MEM_RDONLY);
223 BIO_set_mem_eof_return(mbio, 0);
224 ASN1_STRING_set0(*pos, cont, contlen);
225 (*pos)->flags &= ~ASN1_STRING_FLAG_CONT;
228 switch (OBJ_obj2nid(cms->contentType)) {
231 case NID_pkcs7_encrypted:
232 case NID_id_smime_ct_compressedData:
236 case NID_pkcs7_enveloped:
237 return cms_EnvelopedData_final(cms, cmsbio);
239 case NID_id_smime_ct_authEnvelopedData:
240 return cms_AuthEnvelopedData_final(cms, cmsbio);
242 case NID_pkcs7_signed:
243 return cms_SignedData_final(cms, cmsbio);
245 case NID_pkcs7_digest:
246 return cms_DigestedData_do_final(cms, cmsbio, 0);
249 ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_TYPE);
255 * Return an OCTET STRING pointer to content. This allows it to be accessed
259 ASN1_OCTET_STRING **CMS_get0_content(CMS_ContentInfo *cms)
261 switch (OBJ_obj2nid(cms->contentType)) {
266 case NID_pkcs7_signed:
267 return &cms->d.signedData->encapContentInfo->eContent;
269 case NID_pkcs7_enveloped:
270 return &cms->d.envelopedData->encryptedContentInfo->encryptedContent;
272 case NID_pkcs7_digest:
273 return &cms->d.digestedData->encapContentInfo->eContent;
275 case NID_pkcs7_encrypted:
276 return &cms->d.encryptedData->encryptedContentInfo->encryptedContent;
278 case NID_id_smime_ct_authEnvelopedData:
279 return &cms->d.authEnvelopedData->authEncryptedContentInfo
282 case NID_id_smime_ct_authData:
283 return &cms->d.authenticatedData->encapContentInfo->eContent;
285 case NID_id_smime_ct_compressedData:
286 return &cms->d.compressedData->encapContentInfo->eContent;
289 if (cms->d.other->type == V_ASN1_OCTET_STRING)
290 return &cms->d.other->value.octet_string;
291 ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_CONTENT_TYPE);
298 * Return an ASN1_OBJECT pointer to content type. This allows it to be
299 * accessed or set later.
302 static ASN1_OBJECT **cms_get0_econtent_type(CMS_ContentInfo *cms)
304 switch (OBJ_obj2nid(cms->contentType)) {
306 case NID_pkcs7_signed:
307 return &cms->d.signedData->encapContentInfo->eContentType;
309 case NID_pkcs7_enveloped:
310 return &cms->d.envelopedData->encryptedContentInfo->contentType;
312 case NID_pkcs7_digest:
313 return &cms->d.digestedData->encapContentInfo->eContentType;
315 case NID_pkcs7_encrypted:
316 return &cms->d.encryptedData->encryptedContentInfo->contentType;
318 case NID_id_smime_ct_authEnvelopedData:
319 return &cms->d.authEnvelopedData->authEncryptedContentInfo
321 case NID_id_smime_ct_authData:
322 return &cms->d.authenticatedData->encapContentInfo->eContentType;
324 case NID_id_smime_ct_compressedData:
325 return &cms->d.compressedData->encapContentInfo->eContentType;
328 ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_CONTENT_TYPE);
334 const ASN1_OBJECT *CMS_get0_eContentType(CMS_ContentInfo *cms)
336 ASN1_OBJECT **petype;
337 petype = cms_get0_econtent_type(cms);
343 int CMS_set1_eContentType(CMS_ContentInfo *cms, const ASN1_OBJECT *oid)
345 ASN1_OBJECT **petype, *etype;
347 petype = cms_get0_econtent_type(cms);
352 etype = OBJ_dup(oid);
355 ASN1_OBJECT_free(*petype);
360 int CMS_is_detached(CMS_ContentInfo *cms)
362 ASN1_OCTET_STRING **pos;
364 pos = CMS_get0_content(cms);
372 int CMS_set_detached(CMS_ContentInfo *cms, int detached)
374 ASN1_OCTET_STRING **pos;
376 pos = CMS_get0_content(cms);
380 ASN1_OCTET_STRING_free(*pos);
385 *pos = ASN1_OCTET_STRING_new();
388 * NB: special flag to show content is created and not read in.
390 (*pos)->flags |= ASN1_STRING_FLAG_CONT;
393 ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
397 /* Create a digest BIO from an X509_ALGOR structure */
399 BIO *cms_DigestAlgorithm_init_bio(X509_ALGOR *digestAlgorithm,
403 const ASN1_OBJECT *digestoid;
404 const EVP_MD *digest = NULL;
405 EVP_MD *fetched_digest = NULL;
408 X509_ALGOR_get0(&digestoid, NULL, NULL, digestAlgorithm);
409 alg = OBJ_nid2sn(OBJ_obj2nid(digestoid));
411 (void)ERR_set_mark();
412 fetched_digest = EVP_MD_fetch(cms_ctx_get0_libctx(ctx), alg,
413 cms_ctx_get0_propq(ctx));
415 if (fetched_digest != NULL)
416 digest = fetched_digest;
418 digest = EVP_get_digestbyobj(digestoid);
419 if (digest == NULL) {
420 (void)ERR_clear_last_mark();
421 ERR_raise(ERR_LIB_CMS, CMS_R_UNKNOWN_DIGEST_ALGORITHM);
424 (void)ERR_pop_to_mark();
426 mdbio = BIO_new(BIO_f_md());
427 if (mdbio == NULL || !BIO_set_md(mdbio, digest)) {
428 ERR_raise(ERR_LIB_CMS, CMS_R_MD_BIO_INIT_ERROR);
431 EVP_MD_free(fetched_digest);
434 EVP_MD_free(fetched_digest);
439 /* Locate a message digest content from a BIO chain based on SignerInfo */
441 int cms_DigestAlgorithm_find_ctx(EVP_MD_CTX *mctx, BIO *chain,
445 const ASN1_OBJECT *mdoid;
446 X509_ALGOR_get0(&mdoid, NULL, NULL, mdalg);
447 nid = OBJ_obj2nid(mdoid);
448 /* Look for digest type to match signature */
451 chain = BIO_find_type(chain, BIO_TYPE_MD);
453 ERR_raise(ERR_LIB_CMS, CMS_R_NO_MATCHING_DIGEST);
456 BIO_get_md_ctx(chain, &mtmp);
457 if (EVP_MD_CTX_type(mtmp) == nid
459 * Workaround for broken implementations that use signature
460 * algorithm OID instead of digest.
462 || EVP_MD_pkey_type(EVP_MD_CTX_md(mtmp)) == nid)
463 return EVP_MD_CTX_copy_ex(mctx, mtmp);
464 chain = BIO_next(chain);
468 static STACK_OF(CMS_CertificateChoices)
469 **cms_get0_certificate_choices(CMS_ContentInfo *cms)
471 switch (OBJ_obj2nid(cms->contentType)) {
473 case NID_pkcs7_signed:
474 return &cms->d.signedData->certificates;
476 case NID_pkcs7_enveloped:
477 if (cms->d.envelopedData->originatorInfo == NULL)
479 return &cms->d.envelopedData->originatorInfo->certificates;
481 case NID_id_smime_ct_authEnvelopedData:
482 if (cms->d.authEnvelopedData->originatorInfo == NULL)
484 return &cms->d.authEnvelopedData->originatorInfo->certificates;
487 ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_CONTENT_TYPE);
493 CMS_CertificateChoices *CMS_add0_CertificateChoices(CMS_ContentInfo *cms)
495 STACK_OF(CMS_CertificateChoices) **pcerts;
496 CMS_CertificateChoices *cch;
498 pcerts = cms_get0_certificate_choices(cms);
502 *pcerts = sk_CMS_CertificateChoices_new_null();
505 cch = M_ASN1_new_of(CMS_CertificateChoices);
508 if (!sk_CMS_CertificateChoices_push(*pcerts, cch)) {
509 M_ASN1_free_of(cch, CMS_CertificateChoices);
515 int CMS_add0_cert(CMS_ContentInfo *cms, X509 *cert)
517 CMS_CertificateChoices *cch;
518 STACK_OF(CMS_CertificateChoices) **pcerts;
521 pcerts = cms_get0_certificate_choices(cms);
524 for (i = 0; i < sk_CMS_CertificateChoices_num(*pcerts); i++) {
525 cch = sk_CMS_CertificateChoices_value(*pcerts, i);
526 if (cch->type == CMS_CERTCHOICE_CERT) {
527 if (!X509_cmp(cch->d.certificate, cert)) {
528 ERR_raise(ERR_LIB_CMS, CMS_R_CERTIFICATE_ALREADY_PRESENT);
533 cch = CMS_add0_CertificateChoices(cms);
536 cch->type = CMS_CERTCHOICE_CERT;
537 cch->d.certificate = cert;
541 int CMS_add1_cert(CMS_ContentInfo *cms, X509 *cert)
544 r = CMS_add0_cert(cms, cert);
550 static STACK_OF(CMS_RevocationInfoChoice)
551 **cms_get0_revocation_choices(CMS_ContentInfo *cms)
553 switch (OBJ_obj2nid(cms->contentType)) {
555 case NID_pkcs7_signed:
556 return &cms->d.signedData->crls;
558 case NID_pkcs7_enveloped:
559 if (cms->d.envelopedData->originatorInfo == NULL)
561 return &cms->d.envelopedData->originatorInfo->crls;
563 case NID_id_smime_ct_authEnvelopedData:
564 if (cms->d.authEnvelopedData->originatorInfo == NULL)
566 return &cms->d.authEnvelopedData->originatorInfo->crls;
569 ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_CONTENT_TYPE);
575 CMS_RevocationInfoChoice *CMS_add0_RevocationInfoChoice(CMS_ContentInfo *cms)
577 STACK_OF(CMS_RevocationInfoChoice) **pcrls;
578 CMS_RevocationInfoChoice *rch;
580 pcrls = cms_get0_revocation_choices(cms);
584 *pcrls = sk_CMS_RevocationInfoChoice_new_null();
587 rch = M_ASN1_new_of(CMS_RevocationInfoChoice);
590 if (!sk_CMS_RevocationInfoChoice_push(*pcrls, rch)) {
591 M_ASN1_free_of(rch, CMS_RevocationInfoChoice);
597 int CMS_add0_crl(CMS_ContentInfo *cms, X509_CRL *crl)
599 CMS_RevocationInfoChoice *rch;
600 rch = CMS_add0_RevocationInfoChoice(cms);
603 rch->type = CMS_REVCHOICE_CRL;
608 int CMS_add1_crl(CMS_ContentInfo *cms, X509_CRL *crl)
611 r = CMS_add0_crl(cms, crl);
613 X509_CRL_up_ref(crl);
617 STACK_OF(X509) *CMS_get1_certs(CMS_ContentInfo *cms)
619 STACK_OF(X509) *certs = NULL;
620 CMS_CertificateChoices *cch;
621 STACK_OF(CMS_CertificateChoices) **pcerts;
624 pcerts = cms_get0_certificate_choices(cms);
627 for (i = 0; i < sk_CMS_CertificateChoices_num(*pcerts); i++) {
628 cch = sk_CMS_CertificateChoices_value(*pcerts, i);
629 if (cch->type == 0) {
630 if (!X509_add_cert_new(&certs, cch->d.certificate,
631 X509_ADD_FLAG_UP_REF)) {
632 sk_X509_pop_free(certs, X509_free);
641 STACK_OF(X509_CRL) *CMS_get1_crls(CMS_ContentInfo *cms)
643 STACK_OF(X509_CRL) *crls = NULL;
644 STACK_OF(CMS_RevocationInfoChoice) **pcrls;
645 CMS_RevocationInfoChoice *rch;
648 pcrls = cms_get0_revocation_choices(cms);
651 for (i = 0; i < sk_CMS_RevocationInfoChoice_num(*pcrls); i++) {
652 rch = sk_CMS_RevocationInfoChoice_value(*pcrls, i);
653 if (rch->type == 0) {
655 crls = sk_X509_CRL_new_null();
659 if (!sk_X509_CRL_push(crls, rch->d.crl)) {
660 sk_X509_CRL_pop_free(crls, X509_CRL_free);
663 X509_CRL_up_ref(rch->d.crl);
669 int cms_ias_cert_cmp(CMS_IssuerAndSerialNumber *ias, X509 *cert)
672 ret = X509_NAME_cmp(ias->issuer, X509_get_issuer_name(cert));
675 return ASN1_INTEGER_cmp(ias->serialNumber, X509_get0_serialNumber(cert));
678 int cms_keyid_cert_cmp(ASN1_OCTET_STRING *keyid, X509 *cert)
680 const ASN1_OCTET_STRING *cert_keyid = X509_get0_subject_key_id(cert);
682 if (cert_keyid == NULL)
684 return ASN1_OCTET_STRING_cmp(keyid, cert_keyid);
687 int cms_set1_ias(CMS_IssuerAndSerialNumber **pias, X509 *cert)
689 CMS_IssuerAndSerialNumber *ias;
690 ias = M_ASN1_new_of(CMS_IssuerAndSerialNumber);
693 if (!X509_NAME_set(&ias->issuer, X509_get_issuer_name(cert)))
695 if (!ASN1_STRING_copy(ias->serialNumber, X509_get0_serialNumber(cert)))
697 M_ASN1_free_of(*pias, CMS_IssuerAndSerialNumber);
701 M_ASN1_free_of(ias, CMS_IssuerAndSerialNumber);
702 ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
706 int cms_set1_keyid(ASN1_OCTET_STRING **pkeyid, X509 *cert)
708 ASN1_OCTET_STRING *keyid = NULL;
709 const ASN1_OCTET_STRING *cert_keyid;
710 cert_keyid = X509_get0_subject_key_id(cert);
711 if (cert_keyid == NULL) {
712 ERR_raise(ERR_LIB_CMS, CMS_R_CERTIFICATE_HAS_NO_KEYID);
715 keyid = ASN1_STRING_dup(cert_keyid);
717 ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
720 ASN1_OCTET_STRING_free(*pkeyid);