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 IMPLEMENT_ASN1_PRINT_FUNCTION(CMS_ContentInfo)
28 CMS_ContentInfo *d2i_CMS_ContentInfo(CMS_ContentInfo **a,
29 const unsigned char **in, long len)
33 ci = (CMS_ContentInfo *)ASN1_item_d2i((ASN1_VALUE **)a, in, len,
34 (CMS_ContentInfo_it()));
35 if (ci != NULL && a != NULL)
36 cms_resolve_libctx(ci);
40 int i2d_CMS_ContentInfo(const CMS_ContentInfo *a, unsigned char **out)
42 return ASN1_item_i2d((const ASN1_VALUE *)a, out, (CMS_ContentInfo_it()));
45 CMS_ContentInfo *CMS_ContentInfo_new_with_libctx(OPENSSL_CTX *libctx,
50 ci = (CMS_ContentInfo *)ASN1_item_new(ASN1_ITEM_rptr(CMS_ContentInfo));
52 ci->ctx.libctx = libctx;
55 ci->ctx.propq = OPENSSL_strdup(propq);
56 if (ci->ctx.propq == NULL) {
57 CMS_ContentInfo_free(ci);
59 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
66 CMS_ContentInfo *CMS_ContentInfo_new(void)
68 return CMS_ContentInfo_new_with_libctx(NULL, NULL);
71 void CMS_ContentInfo_free(CMS_ContentInfo *cms)
74 OPENSSL_free(cms->ctx.propq);
75 ASN1_item_free((ASN1_VALUE *)cms, ASN1_ITEM_rptr(CMS_ContentInfo));
79 const CMS_CTX *cms_get0_cmsctx(const CMS_ContentInfo *cms)
81 return cms != NULL ? &cms->ctx : NULL;
84 OPENSSL_CTX *cms_ctx_get0_libctx(const CMS_CTX *ctx)
89 const char *cms_ctx_get0_propq(const CMS_CTX *ctx)
94 void cms_resolve_libctx(CMS_ContentInfo *ci)
97 CMS_CertificateChoices *cch;
98 STACK_OF(CMS_CertificateChoices) **pcerts;
104 ctx = cms_get0_cmsctx(ci);
105 cms_SignerInfos_set_cmsctx(ci);
106 cms_RecipientInfos_set_cmsctx(ci);
108 pcerts = cms_get0_certificate_choices(ci);
109 if (pcerts != NULL) {
110 for (i = 0; i < sk_CMS_CertificateChoices_num(*pcerts); i++) {
111 cch = sk_CMS_CertificateChoices_value(*pcerts, i);
112 if (cch->type == CMS_CERTCHOICE_CERT)
113 x509_set0_libctx(cch->d.certificate, ctx->libctx, ctx->propq);
118 const ASN1_OBJECT *CMS_get0_type(const CMS_ContentInfo *cms)
120 return cms->contentType;
123 CMS_ContentInfo *cms_Data_create(OPENSSL_CTX *libctx, const char *propq)
125 CMS_ContentInfo *cms = CMS_ContentInfo_new_with_libctx(libctx, propq);
128 cms->contentType = OBJ_nid2obj(NID_pkcs7_data);
130 CMS_set_detached(cms, 0);
135 BIO *cms_content_bio(CMS_ContentInfo *cms)
137 ASN1_OCTET_STRING **pos = CMS_get0_content(cms);
141 /* If content detached data goes nowhere: create NULL BIO */
143 return BIO_new(BIO_s_null());
145 * If content not detached and created return memory BIO
147 if (*pos == NULL || ((*pos)->flags == ASN1_STRING_FLAG_CONT))
148 return BIO_new(BIO_s_mem());
149 /* Else content was read in: return read only BIO for it */
150 return BIO_new_mem_buf((*pos)->data, (*pos)->length);
153 BIO *CMS_dataInit(CMS_ContentInfo *cms, BIO *icont)
159 cont = cms_content_bio(cms);
161 CMSerr(CMS_F_CMS_DATAINIT, CMS_R_NO_CONTENT);
164 switch (OBJ_obj2nid(cms->contentType)) {
169 case NID_pkcs7_signed:
170 cmsbio = cms_SignedData_init_bio(cms);
173 case NID_pkcs7_digest:
174 cmsbio = cms_DigestedData_init_bio(cms);
177 case NID_id_smime_ct_compressedData:
178 cmsbio = cms_CompressedData_init_bio(cms);
182 case NID_pkcs7_encrypted:
183 cmsbio = cms_EncryptedData_init_bio(cms);
186 case NID_pkcs7_enveloped:
187 cmsbio = cms_EnvelopedData_init_bio(cms);
190 case NID_id_smime_ct_authEnvelopedData:
191 cmsbio = cms_AuthEnvelopedData_init_bio(cms);
195 CMSerr(CMS_F_CMS_DATAINIT, CMS_R_UNSUPPORTED_TYPE);
200 return BIO_push(cmsbio, cont);
208 /* unfortunately cannot constify SMIME_write_ASN1() due to this function */
209 int CMS_dataFinal(CMS_ContentInfo *cms, BIO *cmsbio)
211 ASN1_OCTET_STRING **pos = CMS_get0_content(cms);
215 /* If embedded content find memory BIO and set content */
216 if (*pos && ((*pos)->flags & ASN1_STRING_FLAG_CONT)) {
220 mbio = BIO_find_type(cmsbio, BIO_TYPE_MEM);
222 CMSerr(CMS_F_CMS_DATAFINAL, CMS_R_CONTENT_NOT_FOUND);
225 contlen = BIO_get_mem_data(mbio, &cont);
226 /* Set bio as read only so its content can't be clobbered */
227 BIO_set_flags(mbio, BIO_FLAGS_MEM_RDONLY);
228 BIO_set_mem_eof_return(mbio, 0);
229 ASN1_STRING_set0(*pos, cont, contlen);
230 (*pos)->flags &= ~ASN1_STRING_FLAG_CONT;
233 switch (OBJ_obj2nid(cms->contentType)) {
236 case NID_pkcs7_encrypted:
237 case NID_id_smime_ct_compressedData:
241 case NID_pkcs7_enveloped:
242 return cms_EnvelopedData_final(cms, cmsbio);
244 case NID_id_smime_ct_authEnvelopedData:
245 return cms_AuthEnvelopedData_final(cms, cmsbio);
247 case NID_pkcs7_signed:
248 return cms_SignedData_final(cms, cmsbio);
250 case NID_pkcs7_digest:
251 return cms_DigestedData_do_final(cms, cmsbio, 0);
254 CMSerr(CMS_F_CMS_DATAFINAL, CMS_R_UNSUPPORTED_TYPE);
260 * Return an OCTET STRING pointer to content. This allows it to be accessed
264 ASN1_OCTET_STRING **CMS_get0_content(CMS_ContentInfo *cms)
266 switch (OBJ_obj2nid(cms->contentType)) {
271 case NID_pkcs7_signed:
272 return &cms->d.signedData->encapContentInfo->eContent;
274 case NID_pkcs7_enveloped:
275 return &cms->d.envelopedData->encryptedContentInfo->encryptedContent;
277 case NID_pkcs7_digest:
278 return &cms->d.digestedData->encapContentInfo->eContent;
280 case NID_pkcs7_encrypted:
281 return &cms->d.encryptedData->encryptedContentInfo->encryptedContent;
283 case NID_id_smime_ct_authEnvelopedData:
284 return &cms->d.authEnvelopedData->authEncryptedContentInfo
287 case NID_id_smime_ct_authData:
288 return &cms->d.authenticatedData->encapContentInfo->eContent;
290 case NID_id_smime_ct_compressedData:
291 return &cms->d.compressedData->encapContentInfo->eContent;
294 if (cms->d.other->type == V_ASN1_OCTET_STRING)
295 return &cms->d.other->value.octet_string;
296 CMSerr(CMS_F_CMS_GET0_CONTENT, CMS_R_UNSUPPORTED_CONTENT_TYPE);
303 * Return an ASN1_OBJECT pointer to content type. This allows it to be
304 * accessed or set later.
307 static ASN1_OBJECT **cms_get0_econtent_type(CMS_ContentInfo *cms)
309 switch (OBJ_obj2nid(cms->contentType)) {
311 case NID_pkcs7_signed:
312 return &cms->d.signedData->encapContentInfo->eContentType;
314 case NID_pkcs7_enveloped:
315 return &cms->d.envelopedData->encryptedContentInfo->contentType;
317 case NID_pkcs7_digest:
318 return &cms->d.digestedData->encapContentInfo->eContentType;
320 case NID_pkcs7_encrypted:
321 return &cms->d.encryptedData->encryptedContentInfo->contentType;
323 case NID_id_smime_ct_authEnvelopedData:
324 return &cms->d.authEnvelopedData->authEncryptedContentInfo
326 case NID_id_smime_ct_authData:
327 return &cms->d.authenticatedData->encapContentInfo->eContentType;
329 case NID_id_smime_ct_compressedData:
330 return &cms->d.compressedData->encapContentInfo->eContentType;
333 CMSerr(CMS_F_CMS_GET0_ECONTENT_TYPE, CMS_R_UNSUPPORTED_CONTENT_TYPE);
339 const ASN1_OBJECT *CMS_get0_eContentType(CMS_ContentInfo *cms)
341 ASN1_OBJECT **petype;
342 petype = cms_get0_econtent_type(cms);
348 int CMS_set1_eContentType(CMS_ContentInfo *cms, const ASN1_OBJECT *oid)
350 ASN1_OBJECT **petype, *etype;
352 petype = cms_get0_econtent_type(cms);
357 etype = OBJ_dup(oid);
360 ASN1_OBJECT_free(*petype);
365 int CMS_is_detached(CMS_ContentInfo *cms)
367 ASN1_OCTET_STRING **pos;
369 pos = CMS_get0_content(cms);
377 int CMS_set_detached(CMS_ContentInfo *cms, int detached)
379 ASN1_OCTET_STRING **pos;
381 pos = CMS_get0_content(cms);
385 ASN1_OCTET_STRING_free(*pos);
390 *pos = ASN1_OCTET_STRING_new();
393 * NB: special flag to show content is created and not read in.
395 (*pos)->flags |= ASN1_STRING_FLAG_CONT;
398 CMSerr(CMS_F_CMS_SET_DETACHED, ERR_R_MALLOC_FAILURE);
402 /* Create a digest BIO from an X509_ALGOR structure */
404 BIO *cms_DigestAlgorithm_init_bio(X509_ALGOR *digestAlgorithm,
408 const ASN1_OBJECT *digestoid;
409 const EVP_MD *digest = NULL;
410 EVP_MD *fetched_digest = NULL;
413 X509_ALGOR_get0(&digestoid, NULL, NULL, digestAlgorithm);
414 alg = OBJ_nid2sn(OBJ_obj2nid(digestoid));
416 (void)ERR_set_mark();
417 fetched_digest = EVP_MD_fetch(ctx->libctx, alg, ctx->propq);
419 if (fetched_digest != NULL)
420 digest = fetched_digest;
422 digest = EVP_get_digestbyobj(digestoid);
423 if (digest == NULL) {
424 (void)ERR_clear_last_mark();
425 CMSerr(CMS_F_CMS_DIGESTALGORITHM_INIT_BIO,
426 CMS_R_UNKNOWN_DIGEST_ALGORITHM);
429 (void)ERR_pop_to_mark();
431 mdbio = BIO_new(BIO_f_md());
432 if (mdbio == NULL || !BIO_set_md(mdbio, digest)) {
433 CMSerr(CMS_F_CMS_DIGESTALGORITHM_INIT_BIO, CMS_R_MD_BIO_INIT_ERROR);
436 EVP_MD_free(fetched_digest);
439 EVP_MD_free(fetched_digest);
444 /* Locate a message digest content from a BIO chain based on SignerInfo */
446 int cms_DigestAlgorithm_find_ctx(EVP_MD_CTX *mctx, BIO *chain,
450 const ASN1_OBJECT *mdoid;
451 X509_ALGOR_get0(&mdoid, NULL, NULL, mdalg);
452 nid = OBJ_obj2nid(mdoid);
453 /* Look for digest type to match signature */
456 chain = BIO_find_type(chain, BIO_TYPE_MD);
458 CMSerr(CMS_F_CMS_DIGESTALGORITHM_FIND_CTX,
459 CMS_R_NO_MATCHING_DIGEST);
462 BIO_get_md_ctx(chain, &mtmp);
463 if (EVP_MD_CTX_type(mtmp) == nid
465 * Workaround for broken implementations that use signature
466 * algorithm OID instead of digest.
468 || EVP_MD_pkey_type(EVP_MD_CTX_md(mtmp)) == nid)
469 return EVP_MD_CTX_copy_ex(mctx, mtmp);
470 chain = BIO_next(chain);
474 static STACK_OF(CMS_CertificateChoices)
475 **cms_get0_certificate_choices(CMS_ContentInfo *cms)
477 switch (OBJ_obj2nid(cms->contentType)) {
479 case NID_pkcs7_signed:
480 return &cms->d.signedData->certificates;
482 case NID_pkcs7_enveloped:
483 if (cms->d.envelopedData->originatorInfo == NULL)
485 return &cms->d.envelopedData->originatorInfo->certificates;
487 case NID_id_smime_ct_authEnvelopedData:
488 if (cms->d.authEnvelopedData->originatorInfo == NULL)
490 return &cms->d.authEnvelopedData->originatorInfo->certificates;
493 CMSerr(CMS_F_CMS_GET0_CERTIFICATE_CHOICES,
494 CMS_R_UNSUPPORTED_CONTENT_TYPE);
500 CMS_CertificateChoices *CMS_add0_CertificateChoices(CMS_ContentInfo *cms)
502 STACK_OF(CMS_CertificateChoices) **pcerts;
503 CMS_CertificateChoices *cch;
505 pcerts = cms_get0_certificate_choices(cms);
509 *pcerts = sk_CMS_CertificateChoices_new_null();
512 cch = M_ASN1_new_of(CMS_CertificateChoices);
515 if (!sk_CMS_CertificateChoices_push(*pcerts, cch)) {
516 M_ASN1_free_of(cch, CMS_CertificateChoices);
522 int CMS_add0_cert(CMS_ContentInfo *cms, X509 *cert)
524 CMS_CertificateChoices *cch;
525 STACK_OF(CMS_CertificateChoices) **pcerts;
528 pcerts = cms_get0_certificate_choices(cms);
531 for (i = 0; i < sk_CMS_CertificateChoices_num(*pcerts); i++) {
532 cch = sk_CMS_CertificateChoices_value(*pcerts, i);
533 if (cch->type == CMS_CERTCHOICE_CERT) {
534 if (!X509_cmp(cch->d.certificate, cert)) {
535 CMSerr(CMS_F_CMS_ADD0_CERT,
536 CMS_R_CERTIFICATE_ALREADY_PRESENT);
541 cch = CMS_add0_CertificateChoices(cms);
544 cch->type = CMS_CERTCHOICE_CERT;
545 cch->d.certificate = cert;
549 int CMS_add1_cert(CMS_ContentInfo *cms, X509 *cert)
552 r = CMS_add0_cert(cms, cert);
558 static STACK_OF(CMS_RevocationInfoChoice)
559 **cms_get0_revocation_choices(CMS_ContentInfo *cms)
561 switch (OBJ_obj2nid(cms->contentType)) {
563 case NID_pkcs7_signed:
564 return &cms->d.signedData->crls;
566 case NID_pkcs7_enveloped:
567 if (cms->d.envelopedData->originatorInfo == NULL)
569 return &cms->d.envelopedData->originatorInfo->crls;
571 case NID_id_smime_ct_authEnvelopedData:
572 if (cms->d.authEnvelopedData->originatorInfo == NULL)
574 return &cms->d.authEnvelopedData->originatorInfo->crls;
577 CMSerr(CMS_F_CMS_GET0_REVOCATION_CHOICES,
578 CMS_R_UNSUPPORTED_CONTENT_TYPE);
584 CMS_RevocationInfoChoice *CMS_add0_RevocationInfoChoice(CMS_ContentInfo *cms)
586 STACK_OF(CMS_RevocationInfoChoice) **pcrls;
587 CMS_RevocationInfoChoice *rch;
589 pcrls = cms_get0_revocation_choices(cms);
593 *pcrls = sk_CMS_RevocationInfoChoice_new_null();
596 rch = M_ASN1_new_of(CMS_RevocationInfoChoice);
599 if (!sk_CMS_RevocationInfoChoice_push(*pcrls, rch)) {
600 M_ASN1_free_of(rch, CMS_RevocationInfoChoice);
606 int CMS_add0_crl(CMS_ContentInfo *cms, X509_CRL *crl)
608 CMS_RevocationInfoChoice *rch;
609 rch = CMS_add0_RevocationInfoChoice(cms);
612 rch->type = CMS_REVCHOICE_CRL;
617 int CMS_add1_crl(CMS_ContentInfo *cms, X509_CRL *crl)
620 r = CMS_add0_crl(cms, crl);
622 X509_CRL_up_ref(crl);
626 STACK_OF(X509) *CMS_get1_certs(CMS_ContentInfo *cms)
628 STACK_OF(X509) *certs = NULL;
629 CMS_CertificateChoices *cch;
630 STACK_OF(CMS_CertificateChoices) **pcerts;
633 pcerts = cms_get0_certificate_choices(cms);
636 for (i = 0; i < sk_CMS_CertificateChoices_num(*pcerts); i++) {
637 cch = sk_CMS_CertificateChoices_value(*pcerts, i);
638 if (cch->type == 0) {
639 if (!X509_add_cert_new(&certs, cch->d.certificate,
640 X509_ADD_FLAG_UP_REF)) {
641 sk_X509_pop_free(certs, X509_free);
650 STACK_OF(X509_CRL) *CMS_get1_crls(CMS_ContentInfo *cms)
652 STACK_OF(X509_CRL) *crls = NULL;
653 STACK_OF(CMS_RevocationInfoChoice) **pcrls;
654 CMS_RevocationInfoChoice *rch;
657 pcrls = cms_get0_revocation_choices(cms);
660 for (i = 0; i < sk_CMS_RevocationInfoChoice_num(*pcrls); i++) {
661 rch = sk_CMS_RevocationInfoChoice_value(*pcrls, i);
662 if (rch->type == 0) {
664 crls = sk_X509_CRL_new_null();
668 if (!sk_X509_CRL_push(crls, rch->d.crl)) {
669 sk_X509_CRL_pop_free(crls, X509_CRL_free);
672 X509_CRL_up_ref(rch->d.crl);
678 int cms_ias_cert_cmp(CMS_IssuerAndSerialNumber *ias, X509 *cert)
681 ret = X509_NAME_cmp(ias->issuer, X509_get_issuer_name(cert));
684 return ASN1_INTEGER_cmp(ias->serialNumber, X509_get0_serialNumber(cert));
687 int cms_keyid_cert_cmp(ASN1_OCTET_STRING *keyid, X509 *cert)
689 const ASN1_OCTET_STRING *cert_keyid = X509_get0_subject_key_id(cert);
691 if (cert_keyid == NULL)
693 return ASN1_OCTET_STRING_cmp(keyid, cert_keyid);
696 int cms_set1_ias(CMS_IssuerAndSerialNumber **pias, X509 *cert)
698 CMS_IssuerAndSerialNumber *ias;
699 ias = M_ASN1_new_of(CMS_IssuerAndSerialNumber);
702 if (!X509_NAME_set(&ias->issuer, X509_get_issuer_name(cert)))
704 if (!ASN1_STRING_copy(ias->serialNumber, X509_get0_serialNumber(cert)))
706 M_ASN1_free_of(*pias, CMS_IssuerAndSerialNumber);
710 M_ASN1_free_of(ias, CMS_IssuerAndSerialNumber);
711 CMSerr(CMS_F_CMS_SET1_IAS, ERR_R_MALLOC_FAILURE);
715 int cms_set1_keyid(ASN1_OCTET_STRING **pkeyid, X509 *cert)
717 ASN1_OCTET_STRING *keyid = NULL;
718 const ASN1_OCTET_STRING *cert_keyid;
719 cert_keyid = X509_get0_subject_key_id(cert);
720 if (cert_keyid == NULL) {
721 CMSerr(CMS_F_CMS_SET1_KEYID, CMS_R_CERTIFICATE_HAS_NO_KEYID);
724 keyid = ASN1_STRING_dup(cert_keyid);
726 CMSerr(CMS_F_CMS_SET1_KEYID, ERR_R_MALLOC_FAILURE);
729 ASN1_OCTET_STRING_free(*pkeyid);