2 * Copyright 2008-2023 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 "internal/sizes.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_ALLOC_FUNCTIONS(CMS_ContentInfo)
25 IMPLEMENT_ASN1_PRINT_FUNCTION(CMS_ContentInfo)
27 CMS_ContentInfo *d2i_CMS_ContentInfo(CMS_ContentInfo **a,
28 const unsigned char **in, long len)
31 const CMS_CTX *ctx = ossl_cms_get0_cmsctx(a == NULL ? NULL : *a);
33 ci = (CMS_ContentInfo *)ASN1_item_d2i_ex((ASN1_VALUE **)a, in, len,
34 (CMS_ContentInfo_it()),
35 ossl_cms_ctx_get0_libctx(ctx),
36 ossl_cms_ctx_get0_propq(ctx));
39 ossl_cms_resolve_libctx(ci);
45 int i2d_CMS_ContentInfo(const CMS_ContentInfo *a, unsigned char **out)
47 return ASN1_item_i2d((const ASN1_VALUE *)a, out, (CMS_ContentInfo_it()));
50 CMS_ContentInfo *CMS_ContentInfo_new_ex(OSSL_LIB_CTX *libctx, const char *propq)
54 ci = (CMS_ContentInfo *)ASN1_item_new_ex(ASN1_ITEM_rptr(CMS_ContentInfo),
57 ci->ctx.libctx = libctx;
60 ci->ctx.propq = OPENSSL_strdup(propq);
61 if (ci->ctx.propq == NULL) {
62 CMS_ContentInfo_free(ci);
70 const CMS_CTX *ossl_cms_get0_cmsctx(const CMS_ContentInfo *cms)
72 return cms != NULL ? &cms->ctx : NULL;
75 OSSL_LIB_CTX *ossl_cms_ctx_get0_libctx(const CMS_CTX *ctx)
77 return ctx != NULL ? ctx->libctx : NULL;
80 const char *ossl_cms_ctx_get0_propq(const CMS_CTX *ctx)
82 return ctx != NULL ? ctx->propq : NULL;
85 void ossl_cms_resolve_libctx(CMS_ContentInfo *ci)
88 CMS_CertificateChoices *cch;
89 STACK_OF(CMS_CertificateChoices) **pcerts;
90 const CMS_CTX *ctx = ossl_cms_get0_cmsctx(ci);
91 OSSL_LIB_CTX *libctx = ossl_cms_ctx_get0_libctx(ctx);
92 const char *propq = ossl_cms_ctx_get0_propq(ctx);
94 ossl_cms_SignerInfos_set_cmsctx(ci);
95 ossl_cms_RecipientInfos_set_cmsctx(ci);
97 pcerts = cms_get0_certificate_choices(ci);
99 for (i = 0; i < sk_CMS_CertificateChoices_num(*pcerts); i++) {
100 cch = sk_CMS_CertificateChoices_value(*pcerts, i);
101 if (cch->type == CMS_CERTCHOICE_CERT)
102 ossl_x509_set0_libctx(cch->d.certificate, libctx, propq);
107 const ASN1_OBJECT *CMS_get0_type(const CMS_ContentInfo *cms)
109 return cms->contentType;
112 CMS_ContentInfo *ossl_cms_Data_create(OSSL_LIB_CTX *libctx, const char *propq)
114 CMS_ContentInfo *cms = CMS_ContentInfo_new_ex(libctx, propq);
117 cms->contentType = OBJ_nid2obj(NID_pkcs7_data);
119 CMS_set_detached(cms, 0);
124 BIO *ossl_cms_content_bio(CMS_ContentInfo *cms)
126 ASN1_OCTET_STRING **pos = CMS_get0_content(cms);
130 /* If content detached data goes nowhere: create NULL BIO */
132 return BIO_new(BIO_s_null());
134 * If content not detached and created return memory BIO
136 if (*pos == NULL || ((*pos)->flags == ASN1_STRING_FLAG_CONT))
137 return BIO_new(BIO_s_mem());
138 /* Else content was read in: return read only BIO for it */
139 return BIO_new_mem_buf((*pos)->data, (*pos)->length);
142 BIO *CMS_dataInit(CMS_ContentInfo *cms, BIO *icont)
148 cont = ossl_cms_content_bio(cms);
150 ERR_raise(ERR_LIB_CMS, CMS_R_NO_CONTENT);
153 switch (OBJ_obj2nid(cms->contentType)) {
158 case NID_pkcs7_signed:
159 cmsbio = ossl_cms_SignedData_init_bio(cms);
162 case NID_pkcs7_digest:
163 cmsbio = ossl_cms_DigestedData_init_bio(cms);
165 #ifndef OPENSSL_NO_ZLIB
166 case NID_id_smime_ct_compressedData:
167 cmsbio = ossl_cms_CompressedData_init_bio(cms);
171 case NID_pkcs7_encrypted:
172 cmsbio = ossl_cms_EncryptedData_init_bio(cms);
175 case NID_pkcs7_enveloped:
176 cmsbio = ossl_cms_EnvelopedData_init_bio(cms);
179 case NID_id_smime_ct_authEnvelopedData:
180 cmsbio = ossl_cms_AuthEnvelopedData_init_bio(cms);
184 ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_TYPE);
189 return BIO_push(cmsbio, cont);
197 /* unfortunately cannot constify SMIME_write_ASN1() due to this function */
198 int CMS_dataFinal(CMS_ContentInfo *cms, BIO *cmsbio)
200 return ossl_cms_DataFinal(cms, cmsbio, NULL, 0);
203 int ossl_cms_DataFinal(CMS_ContentInfo *cms, BIO *cmsbio,
204 const unsigned char *precomp_md,
205 unsigned int precomp_mdlen)
207 ASN1_OCTET_STRING **pos = CMS_get0_content(cms);
211 /* If embedded content find memory BIO and set content */
212 if (*pos && ((*pos)->flags & ASN1_STRING_FLAG_CONT)) {
216 mbio = BIO_find_type(cmsbio, BIO_TYPE_MEM);
218 ERR_raise(ERR_LIB_CMS, CMS_R_CONTENT_NOT_FOUND);
221 contlen = BIO_get_mem_data(mbio, &cont);
222 /* Set bio as read only so its content can't be clobbered */
223 BIO_set_flags(mbio, BIO_FLAGS_MEM_RDONLY);
224 BIO_set_mem_eof_return(mbio, 0);
225 ASN1_STRING_set0(*pos, cont, contlen);
226 (*pos)->flags &= ~ASN1_STRING_FLAG_CONT;
229 switch (OBJ_obj2nid(cms->contentType)) {
232 case NID_pkcs7_encrypted:
233 case NID_id_smime_ct_compressedData:
237 case NID_pkcs7_enveloped:
238 return ossl_cms_EnvelopedData_final(cms, cmsbio);
240 case NID_id_smime_ct_authEnvelopedData:
241 return ossl_cms_AuthEnvelopedData_final(cms, cmsbio);
243 case NID_pkcs7_signed:
244 return ossl_cms_SignedData_final(cms, cmsbio, precomp_md, precomp_mdlen);
246 case NID_pkcs7_digest:
247 return ossl_cms_DigestedData_do_final(cms, cmsbio, 0);
250 ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_TYPE);
256 * Return an OCTET STRING pointer to content. This allows it to be accessed
260 ASN1_OCTET_STRING **CMS_get0_content(CMS_ContentInfo *cms)
262 switch (OBJ_obj2nid(cms->contentType)) {
267 case NID_pkcs7_signed:
268 return &cms->d.signedData->encapContentInfo->eContent;
270 case NID_pkcs7_enveloped:
271 return &cms->d.envelopedData->encryptedContentInfo->encryptedContent;
273 case NID_pkcs7_digest:
274 return &cms->d.digestedData->encapContentInfo->eContent;
276 case NID_pkcs7_encrypted:
277 return &cms->d.encryptedData->encryptedContentInfo->encryptedContent;
279 case NID_id_smime_ct_authEnvelopedData:
280 return &cms->d.authEnvelopedData->authEncryptedContentInfo
283 case NID_id_smime_ct_authData:
284 return &cms->d.authenticatedData->encapContentInfo->eContent;
286 case NID_id_smime_ct_compressedData:
287 return &cms->d.compressedData->encapContentInfo->eContent;
290 if (cms->d.other->type == V_ASN1_OCTET_STRING)
291 return &cms->d.other->value.octet_string;
292 ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_CONTENT_TYPE);
299 * Return an ASN1_OBJECT pointer to content type. This allows it to be
300 * accessed or set later.
303 static ASN1_OBJECT **cms_get0_econtent_type(CMS_ContentInfo *cms)
305 switch (OBJ_obj2nid(cms->contentType)) {
307 case NID_pkcs7_signed:
308 return &cms->d.signedData->encapContentInfo->eContentType;
310 case NID_pkcs7_enveloped:
311 return &cms->d.envelopedData->encryptedContentInfo->contentType;
313 case NID_pkcs7_digest:
314 return &cms->d.digestedData->encapContentInfo->eContentType;
316 case NID_pkcs7_encrypted:
317 return &cms->d.encryptedData->encryptedContentInfo->contentType;
319 case NID_id_smime_ct_authEnvelopedData:
320 return &cms->d.authEnvelopedData->authEncryptedContentInfo
322 case NID_id_smime_ct_authData:
323 return &cms->d.authenticatedData->encapContentInfo->eContentType;
325 case NID_id_smime_ct_compressedData:
326 return &cms->d.compressedData->encapContentInfo->eContentType;
329 ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_CONTENT_TYPE);
335 const ASN1_OBJECT *CMS_get0_eContentType(CMS_ContentInfo *cms)
337 ASN1_OBJECT **petype;
338 petype = cms_get0_econtent_type(cms);
344 int CMS_set1_eContentType(CMS_ContentInfo *cms, const ASN1_OBJECT *oid)
346 ASN1_OBJECT **petype, *etype;
348 petype = cms_get0_econtent_type(cms);
353 etype = OBJ_dup(oid);
356 ASN1_OBJECT_free(*petype);
361 int CMS_is_detached(CMS_ContentInfo *cms)
363 ASN1_OCTET_STRING **pos;
365 pos = CMS_get0_content(cms);
373 int CMS_set_detached(CMS_ContentInfo *cms, int detached)
375 ASN1_OCTET_STRING **pos;
377 pos = CMS_get0_content(cms);
381 ASN1_OCTET_STRING_free(*pos);
386 *pos = ASN1_OCTET_STRING_new();
389 * NB: special flag to show content is created and not read in.
391 (*pos)->flags |= ASN1_STRING_FLAG_CONT;
394 ERR_raise(ERR_LIB_CMS, ERR_R_ASN1_LIB);
398 /* Create a digest BIO from an X509_ALGOR structure */
400 BIO *ossl_cms_DigestAlgorithm_init_bio(X509_ALGOR *digestAlgorithm,
404 const ASN1_OBJECT *digestoid;
405 const EVP_MD *digest = NULL;
406 EVP_MD *fetched_digest = NULL;
407 char alg[OSSL_MAX_NAME_SIZE];
409 X509_ALGOR_get0(&digestoid, NULL, NULL, digestAlgorithm);
410 OBJ_obj2txt(alg, sizeof(alg), digestoid, 0);
412 (void)ERR_set_mark();
413 fetched_digest = EVP_MD_fetch(ossl_cms_ctx_get0_libctx(ctx), alg,
414 ossl_cms_ctx_get0_propq(ctx));
416 if (fetched_digest != NULL)
417 digest = fetched_digest;
419 digest = EVP_get_digestbyobj(digestoid);
420 if (digest == NULL) {
421 (void)ERR_clear_last_mark();
422 ERR_raise(ERR_LIB_CMS, CMS_R_UNKNOWN_DIGEST_ALGORITHM);
425 (void)ERR_pop_to_mark();
427 mdbio = BIO_new(BIO_f_md());
428 if (mdbio == NULL || BIO_set_md(mdbio, digest) <= 0) {
429 ERR_raise(ERR_LIB_CMS, CMS_R_MD_BIO_INIT_ERROR);
432 EVP_MD_free(fetched_digest);
435 EVP_MD_free(fetched_digest);
440 /* Locate a message digest content from a BIO chain based on SignerInfo */
442 int ossl_cms_DigestAlgorithm_find_ctx(EVP_MD_CTX *mctx, BIO *chain,
446 const ASN1_OBJECT *mdoid;
447 X509_ALGOR_get0(&mdoid, NULL, NULL, mdalg);
448 nid = OBJ_obj2nid(mdoid);
449 /* Look for digest type to match signature */
452 chain = BIO_find_type(chain, BIO_TYPE_MD);
454 ERR_raise(ERR_LIB_CMS, CMS_R_NO_MATCHING_DIGEST);
457 BIO_get_md_ctx(chain, &mtmp);
458 if (EVP_MD_CTX_get_type(mtmp) == nid
460 * Workaround for broken implementations that use signature
461 * algorithm OID instead of digest.
463 || EVP_MD_get_pkey_type(EVP_MD_CTX_get0_md(mtmp)) == nid)
464 return EVP_MD_CTX_copy_ex(mctx, mtmp);
465 chain = BIO_next(chain);
469 static STACK_OF(CMS_CertificateChoices)
470 **cms_get0_certificate_choices(CMS_ContentInfo *cms)
472 switch (OBJ_obj2nid(cms->contentType)) {
474 case NID_pkcs7_signed:
475 return &cms->d.signedData->certificates;
477 case NID_pkcs7_enveloped:
478 if (cms->d.envelopedData->originatorInfo == NULL)
480 return &cms->d.envelopedData->originatorInfo->certificates;
482 case NID_id_smime_ct_authEnvelopedData:
483 if (cms->d.authEnvelopedData->originatorInfo == NULL)
485 return &cms->d.authEnvelopedData->originatorInfo->certificates;
488 ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_CONTENT_TYPE);
494 CMS_CertificateChoices *CMS_add0_CertificateChoices(CMS_ContentInfo *cms)
496 STACK_OF(CMS_CertificateChoices) **pcerts;
497 CMS_CertificateChoices *cch;
499 pcerts = cms_get0_certificate_choices(cms);
503 *pcerts = sk_CMS_CertificateChoices_new_null();
506 cch = M_ASN1_new_of(CMS_CertificateChoices);
509 if (!sk_CMS_CertificateChoices_push(*pcerts, cch)) {
510 M_ASN1_free_of(cch, CMS_CertificateChoices);
516 int CMS_add0_cert(CMS_ContentInfo *cms, X509 *cert)
518 CMS_CertificateChoices *cch;
519 STACK_OF(CMS_CertificateChoices) **pcerts;
522 pcerts = cms_get0_certificate_choices(cms);
525 for (i = 0; i < sk_CMS_CertificateChoices_num(*pcerts); i++) {
526 cch = sk_CMS_CertificateChoices_value(*pcerts, i);
527 if (cch->type == CMS_CERTCHOICE_CERT) {
528 if (X509_cmp(cch->d.certificate, cert) == 0) {
530 return 1; /* cert already present */
534 cch = CMS_add0_CertificateChoices(cms);
537 cch->type = CMS_CERTCHOICE_CERT;
538 cch->d.certificate = cert;
542 int CMS_add1_cert(CMS_ContentInfo *cms, X509 *cert)
544 if (!X509_up_ref(cert))
546 if (CMS_add0_cert(cms, cert))
552 static STACK_OF(CMS_RevocationInfoChoice)
553 **cms_get0_revocation_choices(CMS_ContentInfo *cms)
555 switch (OBJ_obj2nid(cms->contentType)) {
557 case NID_pkcs7_signed:
558 return &cms->d.signedData->crls;
560 case NID_pkcs7_enveloped:
561 if (cms->d.envelopedData->originatorInfo == NULL)
563 return &cms->d.envelopedData->originatorInfo->crls;
565 case NID_id_smime_ct_authEnvelopedData:
566 if (cms->d.authEnvelopedData->originatorInfo == NULL)
568 return &cms->d.authEnvelopedData->originatorInfo->crls;
571 ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_CONTENT_TYPE);
577 CMS_RevocationInfoChoice *CMS_add0_RevocationInfoChoice(CMS_ContentInfo *cms)
579 STACK_OF(CMS_RevocationInfoChoice) **pcrls;
580 CMS_RevocationInfoChoice *rch;
582 pcrls = cms_get0_revocation_choices(cms);
586 *pcrls = sk_CMS_RevocationInfoChoice_new_null();
589 rch = M_ASN1_new_of(CMS_RevocationInfoChoice);
592 if (!sk_CMS_RevocationInfoChoice_push(*pcrls, rch)) {
593 M_ASN1_free_of(rch, CMS_RevocationInfoChoice);
599 int CMS_add0_crl(CMS_ContentInfo *cms, X509_CRL *crl)
601 CMS_RevocationInfoChoice *rch = CMS_add0_RevocationInfoChoice(cms);
605 rch->type = CMS_REVCHOICE_CRL;
610 int CMS_add1_crl(CMS_ContentInfo *cms, X509_CRL *crl)
612 if (!X509_CRL_up_ref(crl))
614 if (CMS_add0_crl(cms, crl))
620 STACK_OF(X509) *CMS_get1_certs(CMS_ContentInfo *cms)
622 STACK_OF(X509) *certs = NULL;
623 CMS_CertificateChoices *cch;
624 STACK_OF(CMS_CertificateChoices) **pcerts;
627 pcerts = cms_get0_certificate_choices(cms);
630 for (i = 0; i < sk_CMS_CertificateChoices_num(*pcerts); i++) {
631 cch = sk_CMS_CertificateChoices_value(*pcerts, i);
632 if (cch->type == 0) {
633 if (!ossl_x509_add_cert_new(&certs, cch->d.certificate,
634 X509_ADD_FLAG_UP_REF)) {
635 OSSL_STACK_OF_X509_free(certs);
644 STACK_OF(X509_CRL) *CMS_get1_crls(CMS_ContentInfo *cms)
646 STACK_OF(X509_CRL) *crls = NULL;
647 STACK_OF(CMS_RevocationInfoChoice) **pcrls;
648 CMS_RevocationInfoChoice *rch;
651 pcrls = cms_get0_revocation_choices(cms);
654 for (i = 0; i < sk_CMS_RevocationInfoChoice_num(*pcrls); i++) {
655 rch = sk_CMS_RevocationInfoChoice_value(*pcrls, i);
656 if (rch->type == 0) {
658 if ((crls = sk_X509_CRL_new_null()) == NULL)
661 if (!sk_X509_CRL_push(crls, rch->d.crl)
662 || !X509_CRL_up_ref(rch->d.crl)) {
663 sk_X509_CRL_pop_free(crls, X509_CRL_free);
671 int ossl_cms_ias_cert_cmp(CMS_IssuerAndSerialNumber *ias, X509 *cert)
674 ret = X509_NAME_cmp(ias->issuer, X509_get_issuer_name(cert));
677 return ASN1_INTEGER_cmp(ias->serialNumber, X509_get0_serialNumber(cert));
680 int ossl_cms_keyid_cert_cmp(ASN1_OCTET_STRING *keyid, X509 *cert)
682 const ASN1_OCTET_STRING *cert_keyid = X509_get0_subject_key_id(cert);
684 if (cert_keyid == NULL)
686 return ASN1_OCTET_STRING_cmp(keyid, cert_keyid);
689 int ossl_cms_set1_ias(CMS_IssuerAndSerialNumber **pias, X509 *cert)
691 CMS_IssuerAndSerialNumber *ias;
692 ias = M_ASN1_new_of(CMS_IssuerAndSerialNumber);
694 ERR_raise(ERR_LIB_CMS, ERR_R_ASN1_LIB);
697 if (!X509_NAME_set(&ias->issuer, X509_get_issuer_name(cert))) {
698 ERR_raise(ERR_LIB_CMS, ERR_R_X509_LIB);
701 if (!ASN1_STRING_copy(ias->serialNumber, X509_get0_serialNumber(cert))) {
702 ERR_raise(ERR_LIB_CMS, ERR_R_ASN1_LIB);
705 M_ASN1_free_of(*pias, CMS_IssuerAndSerialNumber);
709 M_ASN1_free_of(ias, CMS_IssuerAndSerialNumber);
713 int ossl_cms_set1_keyid(ASN1_OCTET_STRING **pkeyid, X509 *cert)
715 ASN1_OCTET_STRING *keyid = NULL;
716 const ASN1_OCTET_STRING *cert_keyid;
717 cert_keyid = X509_get0_subject_key_id(cert);
718 if (cert_keyid == NULL) {
719 ERR_raise(ERR_LIB_CMS, CMS_R_CERTIFICATE_HAS_NO_KEYID);
722 keyid = ASN1_STRING_dup(cert_keyid);
724 ERR_raise(ERR_LIB_CMS, ERR_R_ASN1_LIB);
727 ASN1_OCTET_STRING_free(*pkeyid);