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 "cms_local.h"
19 DEFINE_STACK_OF(CMS_RevocationInfoChoice)
21 DEFINE_STACK_OF(X509_CRL)
23 IMPLEMENT_ASN1_FUNCTIONS(CMS_ContentInfo)
24 IMPLEMENT_ASN1_PRINT_FUNCTION(CMS_ContentInfo)
26 const ASN1_OBJECT *CMS_get0_type(const CMS_ContentInfo *cms)
28 return cms->contentType;
31 CMS_ContentInfo *cms_Data_create(void)
34 cms = CMS_ContentInfo_new();
36 cms->contentType = OBJ_nid2obj(NID_pkcs7_data);
38 CMS_set_detached(cms, 0);
43 BIO *cms_content_bio(CMS_ContentInfo *cms)
45 ASN1_OCTET_STRING **pos = CMS_get0_content(cms);
49 /* If content detached data goes nowhere: create NULL BIO */
51 return BIO_new(BIO_s_null());
53 * If content not detached and created return memory BIO
55 if (*pos == NULL || ((*pos)->flags == ASN1_STRING_FLAG_CONT))
56 return BIO_new(BIO_s_mem());
57 /* Else content was read in: return read only BIO for it */
58 return BIO_new_mem_buf((*pos)->data, (*pos)->length);
61 BIO *CMS_dataInit(CMS_ContentInfo *cms, BIO *icont)
67 cont = cms_content_bio(cms);
69 CMSerr(CMS_F_CMS_DATAINIT, CMS_R_NO_CONTENT);
72 switch (OBJ_obj2nid(cms->contentType)) {
77 case NID_pkcs7_signed:
78 cmsbio = cms_SignedData_init_bio(cms);
81 case NID_pkcs7_digest:
82 cmsbio = cms_DigestedData_init_bio(cms);
85 case NID_id_smime_ct_compressedData:
86 cmsbio = cms_CompressedData_init_bio(cms);
90 case NID_pkcs7_encrypted:
91 cmsbio = cms_EncryptedData_init_bio(cms);
94 case NID_pkcs7_enveloped:
95 cmsbio = cms_EnvelopedData_init_bio(cms);
99 CMSerr(CMS_F_CMS_DATAINIT, CMS_R_UNSUPPORTED_TYPE);
104 return BIO_push(cmsbio, cont);
112 /* unfortunately cannot constify SMIME_write_ASN1() due to this function */
113 int CMS_dataFinal(CMS_ContentInfo *cms, BIO *cmsbio)
115 ASN1_OCTET_STRING **pos = CMS_get0_content(cms);
119 /* If embedded content find memory BIO and set content */
120 if (*pos && ((*pos)->flags & ASN1_STRING_FLAG_CONT)) {
124 mbio = BIO_find_type(cmsbio, BIO_TYPE_MEM);
126 CMSerr(CMS_F_CMS_DATAFINAL, CMS_R_CONTENT_NOT_FOUND);
129 contlen = BIO_get_mem_data(mbio, &cont);
130 /* Set bio as read only so its content can't be clobbered */
131 BIO_set_flags(mbio, BIO_FLAGS_MEM_RDONLY);
132 BIO_set_mem_eof_return(mbio, 0);
133 ASN1_STRING_set0(*pos, cont, contlen);
134 (*pos)->flags &= ~ASN1_STRING_FLAG_CONT;
137 switch (OBJ_obj2nid(cms->contentType)) {
140 case NID_pkcs7_encrypted:
141 case NID_id_smime_ct_compressedData:
145 case NID_pkcs7_enveloped:
146 return cms_EnvelopedData_final(cms, cmsbio);
148 case NID_pkcs7_signed:
149 return cms_SignedData_final(cms, cmsbio);
151 case NID_pkcs7_digest:
152 return cms_DigestedData_do_final(cms, cmsbio, 0);
155 CMSerr(CMS_F_CMS_DATAFINAL, CMS_R_UNSUPPORTED_TYPE);
161 * Return an OCTET STRING pointer to content. This allows it to be accessed
165 ASN1_OCTET_STRING **CMS_get0_content(CMS_ContentInfo *cms)
167 switch (OBJ_obj2nid(cms->contentType)) {
172 case NID_pkcs7_signed:
173 return &cms->d.signedData->encapContentInfo->eContent;
175 case NID_pkcs7_enveloped:
176 return &cms->d.envelopedData->encryptedContentInfo->encryptedContent;
178 case NID_pkcs7_digest:
179 return &cms->d.digestedData->encapContentInfo->eContent;
181 case NID_pkcs7_encrypted:
182 return &cms->d.encryptedData->encryptedContentInfo->encryptedContent;
184 case NID_id_smime_ct_authData:
185 return &cms->d.authenticatedData->encapContentInfo->eContent;
187 case NID_id_smime_ct_compressedData:
188 return &cms->d.compressedData->encapContentInfo->eContent;
191 if (cms->d.other->type == V_ASN1_OCTET_STRING)
192 return &cms->d.other->value.octet_string;
193 CMSerr(CMS_F_CMS_GET0_CONTENT, CMS_R_UNSUPPORTED_CONTENT_TYPE);
200 * Return an ASN1_OBJECT pointer to content type. This allows it to be
201 * accessed or set later.
204 static ASN1_OBJECT **cms_get0_econtent_type(CMS_ContentInfo *cms)
206 switch (OBJ_obj2nid(cms->contentType)) {
208 case NID_pkcs7_signed:
209 return &cms->d.signedData->encapContentInfo->eContentType;
211 case NID_pkcs7_enveloped:
212 return &cms->d.envelopedData->encryptedContentInfo->contentType;
214 case NID_pkcs7_digest:
215 return &cms->d.digestedData->encapContentInfo->eContentType;
217 case NID_pkcs7_encrypted:
218 return &cms->d.encryptedData->encryptedContentInfo->contentType;
220 case NID_id_smime_ct_authData:
221 return &cms->d.authenticatedData->encapContentInfo->eContentType;
223 case NID_id_smime_ct_compressedData:
224 return &cms->d.compressedData->encapContentInfo->eContentType;
227 CMSerr(CMS_F_CMS_GET0_ECONTENT_TYPE, CMS_R_UNSUPPORTED_CONTENT_TYPE);
233 const ASN1_OBJECT *CMS_get0_eContentType(CMS_ContentInfo *cms)
235 ASN1_OBJECT **petype;
236 petype = cms_get0_econtent_type(cms);
242 int CMS_set1_eContentType(CMS_ContentInfo *cms, const ASN1_OBJECT *oid)
244 ASN1_OBJECT **petype, *etype;
246 petype = cms_get0_econtent_type(cms);
251 etype = OBJ_dup(oid);
254 ASN1_OBJECT_free(*petype);
259 int CMS_is_detached(CMS_ContentInfo *cms)
261 ASN1_OCTET_STRING **pos;
263 pos = CMS_get0_content(cms);
271 int CMS_set_detached(CMS_ContentInfo *cms, int detached)
273 ASN1_OCTET_STRING **pos;
275 pos = CMS_get0_content(cms);
279 ASN1_OCTET_STRING_free(*pos);
284 *pos = ASN1_OCTET_STRING_new();
287 * NB: special flag to show content is created and not read in.
289 (*pos)->flags |= ASN1_STRING_FLAG_CONT;
292 CMSerr(CMS_F_CMS_SET_DETACHED, ERR_R_MALLOC_FAILURE);
296 /* Create a digest BIO from an X509_ALGOR structure */
298 BIO *cms_DigestAlgorithm_init_bio(X509_ALGOR *digestAlgorithm)
301 const ASN1_OBJECT *digestoid;
302 const EVP_MD *digest;
303 X509_ALGOR_get0(&digestoid, NULL, NULL, digestAlgorithm);
304 digest = EVP_get_digestbyobj(digestoid);
306 CMSerr(CMS_F_CMS_DIGESTALGORITHM_INIT_BIO,
307 CMS_R_UNKNOWN_DIGEST_ALGORITHM);
310 mdbio = BIO_new(BIO_f_md());
311 if (mdbio == NULL || !BIO_set_md(mdbio, digest)) {
312 CMSerr(CMS_F_CMS_DIGESTALGORITHM_INIT_BIO, CMS_R_MD_BIO_INIT_ERROR);
321 /* Locate a message digest content from a BIO chain based on SignerInfo */
323 int cms_DigestAlgorithm_find_ctx(EVP_MD_CTX *mctx, BIO *chain,
327 const ASN1_OBJECT *mdoid;
328 X509_ALGOR_get0(&mdoid, NULL, NULL, mdalg);
329 nid = OBJ_obj2nid(mdoid);
330 /* Look for digest type to match signature */
333 chain = BIO_find_type(chain, BIO_TYPE_MD);
335 CMSerr(CMS_F_CMS_DIGESTALGORITHM_FIND_CTX,
336 CMS_R_NO_MATCHING_DIGEST);
339 BIO_get_md_ctx(chain, &mtmp);
340 if (EVP_MD_CTX_type(mtmp) == nid
342 * Workaround for broken implementations that use signature
343 * algorithm OID instead of digest.
345 || EVP_MD_pkey_type(EVP_MD_CTX_md(mtmp)) == nid)
346 return EVP_MD_CTX_copy_ex(mctx, mtmp);
347 chain = BIO_next(chain);
351 static STACK_OF(CMS_CertificateChoices)
352 **cms_get0_certificate_choices(CMS_ContentInfo *cms)
354 switch (OBJ_obj2nid(cms->contentType)) {
356 case NID_pkcs7_signed:
357 return &cms->d.signedData->certificates;
359 case NID_pkcs7_enveloped:
360 if (cms->d.envelopedData->originatorInfo == NULL)
362 return &cms->d.envelopedData->originatorInfo->certificates;
365 CMSerr(CMS_F_CMS_GET0_CERTIFICATE_CHOICES,
366 CMS_R_UNSUPPORTED_CONTENT_TYPE);
372 CMS_CertificateChoices *CMS_add0_CertificateChoices(CMS_ContentInfo *cms)
374 STACK_OF(CMS_CertificateChoices) **pcerts;
375 CMS_CertificateChoices *cch;
377 pcerts = cms_get0_certificate_choices(cms);
381 *pcerts = sk_CMS_CertificateChoices_new_null();
384 cch = M_ASN1_new_of(CMS_CertificateChoices);
387 if (!sk_CMS_CertificateChoices_push(*pcerts, cch)) {
388 M_ASN1_free_of(cch, CMS_CertificateChoices);
394 int CMS_add0_cert(CMS_ContentInfo *cms, X509 *cert)
396 CMS_CertificateChoices *cch;
397 STACK_OF(CMS_CertificateChoices) **pcerts;
400 pcerts = cms_get0_certificate_choices(cms);
403 for (i = 0; i < sk_CMS_CertificateChoices_num(*pcerts); i++) {
404 cch = sk_CMS_CertificateChoices_value(*pcerts, i);
405 if (cch->type == CMS_CERTCHOICE_CERT) {
406 if (!X509_cmp(cch->d.certificate, cert)) {
407 CMSerr(CMS_F_CMS_ADD0_CERT,
408 CMS_R_CERTIFICATE_ALREADY_PRESENT);
413 cch = CMS_add0_CertificateChoices(cms);
416 cch->type = CMS_CERTCHOICE_CERT;
417 cch->d.certificate = cert;
421 int CMS_add1_cert(CMS_ContentInfo *cms, X509 *cert)
424 r = CMS_add0_cert(cms, cert);
430 static STACK_OF(CMS_RevocationInfoChoice)
431 **cms_get0_revocation_choices(CMS_ContentInfo *cms)
433 switch (OBJ_obj2nid(cms->contentType)) {
435 case NID_pkcs7_signed:
436 return &cms->d.signedData->crls;
438 case NID_pkcs7_enveloped:
439 if (cms->d.envelopedData->originatorInfo == NULL)
441 return &cms->d.envelopedData->originatorInfo->crls;
444 CMSerr(CMS_F_CMS_GET0_REVOCATION_CHOICES,
445 CMS_R_UNSUPPORTED_CONTENT_TYPE);
451 CMS_RevocationInfoChoice *CMS_add0_RevocationInfoChoice(CMS_ContentInfo *cms)
453 STACK_OF(CMS_RevocationInfoChoice) **pcrls;
454 CMS_RevocationInfoChoice *rch;
456 pcrls = cms_get0_revocation_choices(cms);
460 *pcrls = sk_CMS_RevocationInfoChoice_new_null();
463 rch = M_ASN1_new_of(CMS_RevocationInfoChoice);
466 if (!sk_CMS_RevocationInfoChoice_push(*pcrls, rch)) {
467 M_ASN1_free_of(rch, CMS_RevocationInfoChoice);
473 int CMS_add0_crl(CMS_ContentInfo *cms, X509_CRL *crl)
475 CMS_RevocationInfoChoice *rch;
476 rch = CMS_add0_RevocationInfoChoice(cms);
479 rch->type = CMS_REVCHOICE_CRL;
484 int CMS_add1_crl(CMS_ContentInfo *cms, X509_CRL *crl)
487 r = CMS_add0_crl(cms, crl);
489 X509_CRL_up_ref(crl);
493 STACK_OF(X509) *CMS_get1_certs(CMS_ContentInfo *cms)
495 STACK_OF(X509) *certs = NULL;
496 CMS_CertificateChoices *cch;
497 STACK_OF(CMS_CertificateChoices) **pcerts;
500 pcerts = cms_get0_certificate_choices(cms);
503 for (i = 0; i < sk_CMS_CertificateChoices_num(*pcerts); i++) {
504 cch = sk_CMS_CertificateChoices_value(*pcerts, i);
505 if (cch->type == 0) {
507 certs = sk_X509_new_null();
511 if (!sk_X509_push(certs, cch->d.certificate)) {
512 sk_X509_pop_free(certs, X509_free);
515 X509_up_ref(cch->d.certificate);
522 STACK_OF(X509_CRL) *CMS_get1_crls(CMS_ContentInfo *cms)
524 STACK_OF(X509_CRL) *crls = NULL;
525 STACK_OF(CMS_RevocationInfoChoice) **pcrls;
526 CMS_RevocationInfoChoice *rch;
529 pcrls = cms_get0_revocation_choices(cms);
532 for (i = 0; i < sk_CMS_RevocationInfoChoice_num(*pcrls); i++) {
533 rch = sk_CMS_RevocationInfoChoice_value(*pcrls, i);
534 if (rch->type == 0) {
536 crls = sk_X509_CRL_new_null();
540 if (!sk_X509_CRL_push(crls, rch->d.crl)) {
541 sk_X509_CRL_pop_free(crls, X509_CRL_free);
544 X509_CRL_up_ref(rch->d.crl);
550 int cms_ias_cert_cmp(CMS_IssuerAndSerialNumber *ias, X509 *cert)
553 ret = X509_NAME_cmp(ias->issuer, X509_get_issuer_name(cert));
556 return ASN1_INTEGER_cmp(ias->serialNumber, X509_get_serialNumber(cert));
559 int cms_keyid_cert_cmp(ASN1_OCTET_STRING *keyid, X509 *cert)
561 const ASN1_OCTET_STRING *cert_keyid = X509_get0_subject_key_id(cert);
563 if (cert_keyid == NULL)
565 return ASN1_OCTET_STRING_cmp(keyid, cert_keyid);
568 int cms_set1_ias(CMS_IssuerAndSerialNumber **pias, X509 *cert)
570 CMS_IssuerAndSerialNumber *ias;
571 ias = M_ASN1_new_of(CMS_IssuerAndSerialNumber);
574 if (!X509_NAME_set(&ias->issuer, X509_get_issuer_name(cert)))
576 if (!ASN1_STRING_copy(ias->serialNumber, X509_get_serialNumber(cert)))
578 M_ASN1_free_of(*pias, CMS_IssuerAndSerialNumber);
582 M_ASN1_free_of(ias, CMS_IssuerAndSerialNumber);
583 CMSerr(CMS_F_CMS_SET1_IAS, ERR_R_MALLOC_FAILURE);
587 int cms_set1_keyid(ASN1_OCTET_STRING **pkeyid, X509 *cert)
589 ASN1_OCTET_STRING *keyid = NULL;
590 const ASN1_OCTET_STRING *cert_keyid;
591 cert_keyid = X509_get0_subject_key_id(cert);
592 if (cert_keyid == NULL) {
593 CMSerr(CMS_F_CMS_SET1_KEYID, CMS_R_CERTIFICATE_HAS_NO_KEYID);
596 keyid = ASN1_STRING_dup(cert_keyid);
598 CMSerr(CMS_F_CMS_SET1_KEYID, ERR_R_MALLOC_FAILURE);
601 ASN1_OCTET_STRING_free(*pkeyid);