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 <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 DEFINE_STACK_OF(CMS_RevocationInfoChoice)
26 DEFINE_STACK_OF(X509_CRL)
28 IMPLEMENT_ASN1_PRINT_FUNCTION(CMS_ContentInfo)
30 CMS_ContentInfo *d2i_CMS_ContentInfo(CMS_ContentInfo **a,
31 const unsigned char **in, long len)
35 ci = (CMS_ContentInfo *)ASN1_item_d2i((ASN1_VALUE **)a, in, len,
36 (CMS_ContentInfo_it()));
37 if (ci != NULL && a != NULL)
38 cms_resolve_libctx(ci);
42 int i2d_CMS_ContentInfo(const CMS_ContentInfo *a, unsigned char **out)
44 return ASN1_item_i2d((const ASN1_VALUE *)a, out, (CMS_ContentInfo_it()));
47 CMS_ContentInfo *CMS_ContentInfo_new_with_libctx(OPENSSL_CTX *libctx,
52 ci = (CMS_ContentInfo *)ASN1_item_new(ASN1_ITEM_rptr(CMS_ContentInfo));
54 ci->ctx.libctx = libctx;
57 ci->ctx.propq = OPENSSL_strdup(propq);
58 if (ci->ctx.propq == NULL) {
59 CMS_ContentInfo_free(ci);
61 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
68 CMS_ContentInfo *CMS_ContentInfo_new(void)
70 return CMS_ContentInfo_new_with_libctx(NULL, NULL);
73 void CMS_ContentInfo_free(CMS_ContentInfo *cms)
76 OPENSSL_free(cms->ctx.propq);
77 ASN1_item_free((ASN1_VALUE *)cms, ASN1_ITEM_rptr(CMS_ContentInfo));
81 const CMS_CTX *cms_get0_cmsctx(const CMS_ContentInfo *cms)
83 return cms != NULL ? &cms->ctx : NULL;
86 OPENSSL_CTX *cms_ctx_get0_libctx(const CMS_CTX *ctx)
91 const char *cms_ctx_get0_propq(const CMS_CTX *ctx)
96 void cms_resolve_libctx(CMS_ContentInfo *ci)
99 CMS_CertificateChoices *cch;
100 STACK_OF(CMS_CertificateChoices) **pcerts;
106 ctx = cms_get0_cmsctx(ci);
107 cms_SignerInfos_set_cmsctx(ci);
108 cms_RecipientInfos_set_cmsctx(ci);
110 pcerts = cms_get0_certificate_choices(ci);
111 if (pcerts != NULL) {
112 for (i = 0; i < sk_CMS_CertificateChoices_num(*pcerts); i++) {
113 cch = sk_CMS_CertificateChoices_value(*pcerts, i);
114 if (cch->type == CMS_CERTCHOICE_CERT)
115 x509_set0_libctx(cch->d.certificate, ctx->libctx, ctx->propq);
120 const ASN1_OBJECT *CMS_get0_type(const CMS_ContentInfo *cms)
122 return cms->contentType;
125 CMS_ContentInfo *cms_Data_create(OPENSSL_CTX *libctx, const char *propq)
127 CMS_ContentInfo *cms = CMS_ContentInfo_new_with_libctx(libctx, propq);
130 cms->contentType = OBJ_nid2obj(NID_pkcs7_data);
132 CMS_set_detached(cms, 0);
137 BIO *cms_content_bio(CMS_ContentInfo *cms)
139 ASN1_OCTET_STRING **pos = CMS_get0_content(cms);
143 /* If content detached data goes nowhere: create NULL BIO */
145 return BIO_new(BIO_s_null());
147 * If content not detached and created return memory BIO
149 if (*pos == NULL || ((*pos)->flags == ASN1_STRING_FLAG_CONT))
150 return BIO_new(BIO_s_mem());
151 /* Else content was read in: return read only BIO for it */
152 return BIO_new_mem_buf((*pos)->data, (*pos)->length);
155 BIO *CMS_dataInit(CMS_ContentInfo *cms, BIO *icont)
161 cont = cms_content_bio(cms);
163 CMSerr(CMS_F_CMS_DATAINIT, CMS_R_NO_CONTENT);
166 switch (OBJ_obj2nid(cms->contentType)) {
171 case NID_pkcs7_signed:
172 cmsbio = cms_SignedData_init_bio(cms);
175 case NID_pkcs7_digest:
176 cmsbio = cms_DigestedData_init_bio(cms);
179 case NID_id_smime_ct_compressedData:
180 cmsbio = cms_CompressedData_init_bio(cms);
184 case NID_pkcs7_encrypted:
185 cmsbio = cms_EncryptedData_init_bio(cms);
188 case NID_pkcs7_enveloped:
189 cmsbio = cms_EnvelopedData_init_bio(cms);
192 case NID_id_smime_ct_authEnvelopedData:
193 cmsbio = cms_AuthEnvelopedData_init_bio(cms);
197 CMSerr(CMS_F_CMS_DATAINIT, CMS_R_UNSUPPORTED_TYPE);
202 return BIO_push(cmsbio, cont);
210 /* unfortunately cannot constify SMIME_write_ASN1() due to this function */
211 int CMS_dataFinal(CMS_ContentInfo *cms, BIO *cmsbio)
213 ASN1_OCTET_STRING **pos = CMS_get0_content(cms);
217 /* If embedded content find memory BIO and set content */
218 if (*pos && ((*pos)->flags & ASN1_STRING_FLAG_CONT)) {
222 mbio = BIO_find_type(cmsbio, BIO_TYPE_MEM);
224 CMSerr(CMS_F_CMS_DATAFINAL, CMS_R_CONTENT_NOT_FOUND);
227 contlen = BIO_get_mem_data(mbio, &cont);
228 /* Set bio as read only so its content can't be clobbered */
229 BIO_set_flags(mbio, BIO_FLAGS_MEM_RDONLY);
230 BIO_set_mem_eof_return(mbio, 0);
231 ASN1_STRING_set0(*pos, cont, contlen);
232 (*pos)->flags &= ~ASN1_STRING_FLAG_CONT;
235 switch (OBJ_obj2nid(cms->contentType)) {
238 case NID_pkcs7_encrypted:
239 case NID_id_smime_ct_compressedData:
243 case NID_pkcs7_enveloped:
244 return cms_EnvelopedData_final(cms, cmsbio);
246 case NID_id_smime_ct_authEnvelopedData:
247 return cms_AuthEnvelopedData_final(cms, cmsbio);
249 case NID_pkcs7_signed:
250 return cms_SignedData_final(cms, cmsbio);
252 case NID_pkcs7_digest:
253 return cms_DigestedData_do_final(cms, cmsbio, 0);
256 CMSerr(CMS_F_CMS_DATAFINAL, CMS_R_UNSUPPORTED_TYPE);
262 * Return an OCTET STRING pointer to content. This allows it to be accessed
266 ASN1_OCTET_STRING **CMS_get0_content(CMS_ContentInfo *cms)
268 switch (OBJ_obj2nid(cms->contentType)) {
273 case NID_pkcs7_signed:
274 return &cms->d.signedData->encapContentInfo->eContent;
276 case NID_pkcs7_enveloped:
277 return &cms->d.envelopedData->encryptedContentInfo->encryptedContent;
279 case NID_pkcs7_digest:
280 return &cms->d.digestedData->encapContentInfo->eContent;
282 case NID_pkcs7_encrypted:
283 return &cms->d.encryptedData->encryptedContentInfo->encryptedContent;
285 case NID_id_smime_ct_authEnvelopedData:
286 return &cms->d.authEnvelopedData->authEncryptedContentInfo
289 case NID_id_smime_ct_authData:
290 return &cms->d.authenticatedData->encapContentInfo->eContent;
292 case NID_id_smime_ct_compressedData:
293 return &cms->d.compressedData->encapContentInfo->eContent;
296 if (cms->d.other->type == V_ASN1_OCTET_STRING)
297 return &cms->d.other->value.octet_string;
298 CMSerr(CMS_F_CMS_GET0_CONTENT, CMS_R_UNSUPPORTED_CONTENT_TYPE);
305 * Return an ASN1_OBJECT pointer to content type. This allows it to be
306 * accessed or set later.
309 static ASN1_OBJECT **cms_get0_econtent_type(CMS_ContentInfo *cms)
311 switch (OBJ_obj2nid(cms->contentType)) {
313 case NID_pkcs7_signed:
314 return &cms->d.signedData->encapContentInfo->eContentType;
316 case NID_pkcs7_enveloped:
317 return &cms->d.envelopedData->encryptedContentInfo->contentType;
319 case NID_pkcs7_digest:
320 return &cms->d.digestedData->encapContentInfo->eContentType;
322 case NID_pkcs7_encrypted:
323 return &cms->d.encryptedData->encryptedContentInfo->contentType;
325 case NID_id_smime_ct_authEnvelopedData:
326 return &cms->d.authEnvelopedData->authEncryptedContentInfo
328 case NID_id_smime_ct_authData:
329 return &cms->d.authenticatedData->encapContentInfo->eContentType;
331 case NID_id_smime_ct_compressedData:
332 return &cms->d.compressedData->encapContentInfo->eContentType;
335 CMSerr(CMS_F_CMS_GET0_ECONTENT_TYPE, CMS_R_UNSUPPORTED_CONTENT_TYPE);
341 const ASN1_OBJECT *CMS_get0_eContentType(CMS_ContentInfo *cms)
343 ASN1_OBJECT **petype;
344 petype = cms_get0_econtent_type(cms);
350 int CMS_set1_eContentType(CMS_ContentInfo *cms, const ASN1_OBJECT *oid)
352 ASN1_OBJECT **petype, *etype;
354 petype = cms_get0_econtent_type(cms);
359 etype = OBJ_dup(oid);
362 ASN1_OBJECT_free(*petype);
367 int CMS_is_detached(CMS_ContentInfo *cms)
369 ASN1_OCTET_STRING **pos;
371 pos = CMS_get0_content(cms);
379 int CMS_set_detached(CMS_ContentInfo *cms, int detached)
381 ASN1_OCTET_STRING **pos;
383 pos = CMS_get0_content(cms);
387 ASN1_OCTET_STRING_free(*pos);
392 *pos = ASN1_OCTET_STRING_new();
395 * NB: special flag to show content is created and not read in.
397 (*pos)->flags |= ASN1_STRING_FLAG_CONT;
400 CMSerr(CMS_F_CMS_SET_DETACHED, ERR_R_MALLOC_FAILURE);
404 /* Create a digest BIO from an X509_ALGOR structure */
406 BIO *cms_DigestAlgorithm_init_bio(X509_ALGOR *digestAlgorithm,
410 const ASN1_OBJECT *digestoid;
411 const EVP_MD *digest = NULL;
412 EVP_MD *fetched_digest = NULL;
415 X509_ALGOR_get0(&digestoid, NULL, NULL, digestAlgorithm);
416 alg = OBJ_nid2sn(OBJ_obj2nid(digestoid));
418 (void)ERR_set_mark();
419 fetched_digest = EVP_MD_fetch(ctx->libctx, alg, ctx->propq);
421 if (fetched_digest != NULL)
422 digest = fetched_digest;
424 digest = EVP_get_digestbyobj(digestoid);
425 if (digest == NULL) {
426 (void)ERR_clear_last_mark();
427 CMSerr(CMS_F_CMS_DIGESTALGORITHM_INIT_BIO,
428 CMS_R_UNKNOWN_DIGEST_ALGORITHM);
431 (void)ERR_pop_to_mark();
433 mdbio = BIO_new(BIO_f_md());
434 if (mdbio == NULL || !BIO_set_md(mdbio, digest)) {
435 CMSerr(CMS_F_CMS_DIGESTALGORITHM_INIT_BIO, CMS_R_MD_BIO_INIT_ERROR);
438 EVP_MD_free(fetched_digest);
441 EVP_MD_free(fetched_digest);
446 /* Locate a message digest content from a BIO chain based on SignerInfo */
448 int cms_DigestAlgorithm_find_ctx(EVP_MD_CTX *mctx, BIO *chain,
452 const ASN1_OBJECT *mdoid;
453 X509_ALGOR_get0(&mdoid, NULL, NULL, mdalg);
454 nid = OBJ_obj2nid(mdoid);
455 /* Look for digest type to match signature */
458 chain = BIO_find_type(chain, BIO_TYPE_MD);
460 CMSerr(CMS_F_CMS_DIGESTALGORITHM_FIND_CTX,
461 CMS_R_NO_MATCHING_DIGEST);
464 BIO_get_md_ctx(chain, &mtmp);
465 if (EVP_MD_CTX_type(mtmp) == nid
467 * Workaround for broken implementations that use signature
468 * algorithm OID instead of digest.
470 || EVP_MD_pkey_type(EVP_MD_CTX_md(mtmp)) == nid)
471 return EVP_MD_CTX_copy_ex(mctx, mtmp);
472 chain = BIO_next(chain);
476 static STACK_OF(CMS_CertificateChoices)
477 **cms_get0_certificate_choices(CMS_ContentInfo *cms)
479 switch (OBJ_obj2nid(cms->contentType)) {
481 case NID_pkcs7_signed:
482 return &cms->d.signedData->certificates;
484 case NID_pkcs7_enveloped:
485 if (cms->d.envelopedData->originatorInfo == NULL)
487 return &cms->d.envelopedData->originatorInfo->certificates;
489 case NID_id_smime_ct_authEnvelopedData:
490 if (cms->d.authEnvelopedData->originatorInfo == NULL)
492 return &cms->d.authEnvelopedData->originatorInfo->certificates;
495 CMSerr(CMS_F_CMS_GET0_CERTIFICATE_CHOICES,
496 CMS_R_UNSUPPORTED_CONTENT_TYPE);
502 CMS_CertificateChoices *CMS_add0_CertificateChoices(CMS_ContentInfo *cms)
504 STACK_OF(CMS_CertificateChoices) **pcerts;
505 CMS_CertificateChoices *cch;
507 pcerts = cms_get0_certificate_choices(cms);
511 *pcerts = sk_CMS_CertificateChoices_new_null();
514 cch = M_ASN1_new_of(CMS_CertificateChoices);
517 if (!sk_CMS_CertificateChoices_push(*pcerts, cch)) {
518 M_ASN1_free_of(cch, CMS_CertificateChoices);
524 int CMS_add0_cert(CMS_ContentInfo *cms, X509 *cert)
526 CMS_CertificateChoices *cch;
527 STACK_OF(CMS_CertificateChoices) **pcerts;
530 pcerts = cms_get0_certificate_choices(cms);
533 for (i = 0; i < sk_CMS_CertificateChoices_num(*pcerts); i++) {
534 cch = sk_CMS_CertificateChoices_value(*pcerts, i);
535 if (cch->type == CMS_CERTCHOICE_CERT) {
536 if (!X509_cmp(cch->d.certificate, cert)) {
537 CMSerr(CMS_F_CMS_ADD0_CERT,
538 CMS_R_CERTIFICATE_ALREADY_PRESENT);
543 cch = CMS_add0_CertificateChoices(cms);
546 cch->type = CMS_CERTCHOICE_CERT;
547 cch->d.certificate = cert;
551 int CMS_add1_cert(CMS_ContentInfo *cms, X509 *cert)
554 r = CMS_add0_cert(cms, cert);
560 static STACK_OF(CMS_RevocationInfoChoice)
561 **cms_get0_revocation_choices(CMS_ContentInfo *cms)
563 switch (OBJ_obj2nid(cms->contentType)) {
565 case NID_pkcs7_signed:
566 return &cms->d.signedData->crls;
568 case NID_pkcs7_enveloped:
569 if (cms->d.envelopedData->originatorInfo == NULL)
571 return &cms->d.envelopedData->originatorInfo->crls;
573 case NID_id_smime_ct_authEnvelopedData:
574 if (cms->d.authEnvelopedData->originatorInfo == NULL)
576 return &cms->d.authEnvelopedData->originatorInfo->crls;
579 CMSerr(CMS_F_CMS_GET0_REVOCATION_CHOICES,
580 CMS_R_UNSUPPORTED_CONTENT_TYPE);
586 CMS_RevocationInfoChoice *CMS_add0_RevocationInfoChoice(CMS_ContentInfo *cms)
588 STACK_OF(CMS_RevocationInfoChoice) **pcrls;
589 CMS_RevocationInfoChoice *rch;
591 pcrls = cms_get0_revocation_choices(cms);
595 *pcrls = sk_CMS_RevocationInfoChoice_new_null();
598 rch = M_ASN1_new_of(CMS_RevocationInfoChoice);
601 if (!sk_CMS_RevocationInfoChoice_push(*pcrls, rch)) {
602 M_ASN1_free_of(rch, CMS_RevocationInfoChoice);
608 int CMS_add0_crl(CMS_ContentInfo *cms, X509_CRL *crl)
610 CMS_RevocationInfoChoice *rch;
611 rch = CMS_add0_RevocationInfoChoice(cms);
614 rch->type = CMS_REVCHOICE_CRL;
619 int CMS_add1_crl(CMS_ContentInfo *cms, X509_CRL *crl)
622 r = CMS_add0_crl(cms, crl);
624 X509_CRL_up_ref(crl);
628 STACK_OF(X509) *CMS_get1_certs(CMS_ContentInfo *cms)
630 STACK_OF(X509) *certs = NULL;
631 CMS_CertificateChoices *cch;
632 STACK_OF(CMS_CertificateChoices) **pcerts;
635 pcerts = cms_get0_certificate_choices(cms);
638 for (i = 0; i < sk_CMS_CertificateChoices_num(*pcerts); i++) {
639 cch = sk_CMS_CertificateChoices_value(*pcerts, i);
640 if (cch->type == 0) {
641 if (!X509_add_cert_new(&certs, cch->d.certificate,
642 X509_ADD_FLAG_UP_REF)) {
643 sk_X509_pop_free(certs, X509_free);
652 STACK_OF(X509_CRL) *CMS_get1_crls(CMS_ContentInfo *cms)
654 STACK_OF(X509_CRL) *crls = NULL;
655 STACK_OF(CMS_RevocationInfoChoice) **pcrls;
656 CMS_RevocationInfoChoice *rch;
659 pcrls = cms_get0_revocation_choices(cms);
662 for (i = 0; i < sk_CMS_RevocationInfoChoice_num(*pcrls); i++) {
663 rch = sk_CMS_RevocationInfoChoice_value(*pcrls, i);
664 if (rch->type == 0) {
666 crls = sk_X509_CRL_new_null();
670 if (!sk_X509_CRL_push(crls, rch->d.crl)) {
671 sk_X509_CRL_pop_free(crls, X509_CRL_free);
674 X509_CRL_up_ref(rch->d.crl);
680 int cms_ias_cert_cmp(CMS_IssuerAndSerialNumber *ias, X509 *cert)
683 ret = X509_NAME_cmp(ias->issuer, X509_get_issuer_name(cert));
686 return ASN1_INTEGER_cmp(ias->serialNumber, X509_get0_serialNumber(cert));
689 int cms_keyid_cert_cmp(ASN1_OCTET_STRING *keyid, X509 *cert)
691 const ASN1_OCTET_STRING *cert_keyid = X509_get0_subject_key_id(cert);
693 if (cert_keyid == NULL)
695 return ASN1_OCTET_STRING_cmp(keyid, cert_keyid);
698 int cms_set1_ias(CMS_IssuerAndSerialNumber **pias, X509 *cert)
700 CMS_IssuerAndSerialNumber *ias;
701 ias = M_ASN1_new_of(CMS_IssuerAndSerialNumber);
704 if (!X509_NAME_set(&ias->issuer, X509_get_issuer_name(cert)))
706 if (!ASN1_STRING_copy(ias->serialNumber, X509_get0_serialNumber(cert)))
708 M_ASN1_free_of(*pias, CMS_IssuerAndSerialNumber);
712 M_ASN1_free_of(ias, CMS_IssuerAndSerialNumber);
713 CMSerr(CMS_F_CMS_SET1_IAS, ERR_R_MALLOC_FAILURE);
717 int cms_set1_keyid(ASN1_OCTET_STRING **pkeyid, X509 *cert)
719 ASN1_OCTET_STRING *keyid = NULL;
720 const ASN1_OCTET_STRING *cert_keyid;
721 cert_keyid = X509_get0_subject_key_id(cert);
722 if (cert_keyid == NULL) {
723 CMSerr(CMS_F_CMS_SET1_KEYID, CMS_R_CERTIFICATE_HAS_NO_KEYID);
726 keyid = ASN1_STRING_dup(cert_keyid);
728 CMSerr(CMS_F_CMS_SET1_KEYID, ERR_R_MALLOC_FAILURE);
731 ASN1_OCTET_STRING_free(*pkeyid);