2 * Copyright 2008-2020 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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 IMPLEMENT_ASN1_FUNCTIONS(CMS_ContentInfo)
20 IMPLEMENT_ASN1_PRINT_FUNCTION(CMS_ContentInfo)
22 const ASN1_OBJECT *CMS_get0_type(const CMS_ContentInfo *cms)
24 return cms->contentType;
27 CMS_ContentInfo *cms_Data_create(void)
30 cms = CMS_ContentInfo_new();
32 cms->contentType = OBJ_nid2obj(NID_pkcs7_data);
34 CMS_set_detached(cms, 0);
39 BIO *cms_content_bio(CMS_ContentInfo *cms)
41 ASN1_OCTET_STRING **pos = CMS_get0_content(cms);
44 /* If content detached data goes nowhere: create NULL BIO */
46 return BIO_new(BIO_s_null());
48 * If content not detached and created return memory BIO
50 if (!*pos || ((*pos)->flags == ASN1_STRING_FLAG_CONT))
51 return BIO_new(BIO_s_mem());
52 /* Else content was read in: return read only BIO for it */
53 return BIO_new_mem_buf((*pos)->data, (*pos)->length);
56 BIO *CMS_dataInit(CMS_ContentInfo *cms, BIO *icont)
62 cont = cms_content_bio(cms);
64 CMSerr(CMS_F_CMS_DATAINIT, CMS_R_NO_CONTENT);
67 switch (OBJ_obj2nid(cms->contentType)) {
72 case NID_pkcs7_signed:
73 cmsbio = cms_SignedData_init_bio(cms);
76 case NID_pkcs7_digest:
77 cmsbio = cms_DigestedData_init_bio(cms);
80 case NID_id_smime_ct_compressedData:
81 cmsbio = cms_CompressedData_init_bio(cms);
85 case NID_pkcs7_encrypted:
86 cmsbio = cms_EncryptedData_init_bio(cms);
89 case NID_pkcs7_enveloped:
90 cmsbio = cms_EnvelopedData_init_bio(cms);
94 CMSerr(CMS_F_CMS_DATAINIT, CMS_R_UNSUPPORTED_TYPE);
99 return BIO_push(cmsbio, cont);
108 int CMS_dataFinal(CMS_ContentInfo *cms, BIO *cmsbio)
110 ASN1_OCTET_STRING **pos = CMS_get0_content(cms);
113 /* If embedded content find memory BIO and set content */
114 if (*pos && ((*pos)->flags & ASN1_STRING_FLAG_CONT)) {
118 mbio = BIO_find_type(cmsbio, BIO_TYPE_MEM);
120 CMSerr(CMS_F_CMS_DATAFINAL, CMS_R_CONTENT_NOT_FOUND);
123 contlen = BIO_get_mem_data(mbio, &cont);
124 /* Set bio as read only so its content can't be clobbered */
125 BIO_set_flags(mbio, BIO_FLAGS_MEM_RDONLY);
126 BIO_set_mem_eof_return(mbio, 0);
127 ASN1_STRING_set0(*pos, cont, contlen);
128 (*pos)->flags &= ~ASN1_STRING_FLAG_CONT;
131 switch (OBJ_obj2nid(cms->contentType)) {
134 case NID_pkcs7_enveloped:
135 case NID_pkcs7_encrypted:
136 case NID_id_smime_ct_compressedData:
140 case NID_pkcs7_signed:
141 return cms_SignedData_final(cms, cmsbio);
143 case NID_pkcs7_digest:
144 return cms_DigestedData_do_final(cms, cmsbio, 0);
147 CMSerr(CMS_F_CMS_DATAFINAL, CMS_R_UNSUPPORTED_TYPE);
153 * Return an OCTET STRING pointer to content. This allows it to be accessed
157 ASN1_OCTET_STRING **CMS_get0_content(CMS_ContentInfo *cms)
159 switch (OBJ_obj2nid(cms->contentType)) {
164 case NID_pkcs7_signed:
165 return &cms->d.signedData->encapContentInfo->eContent;
167 case NID_pkcs7_enveloped:
168 return &cms->d.envelopedData->encryptedContentInfo->encryptedContent;
170 case NID_pkcs7_digest:
171 return &cms->d.digestedData->encapContentInfo->eContent;
173 case NID_pkcs7_encrypted:
174 return &cms->d.encryptedData->encryptedContentInfo->encryptedContent;
176 case NID_id_smime_ct_authData:
177 return &cms->d.authenticatedData->encapContentInfo->eContent;
179 case NID_id_smime_ct_compressedData:
180 return &cms->d.compressedData->encapContentInfo->eContent;
183 if (cms->d.other->type == V_ASN1_OCTET_STRING)
184 return &cms->d.other->value.octet_string;
185 CMSerr(CMS_F_CMS_GET0_CONTENT, CMS_R_UNSUPPORTED_CONTENT_TYPE);
192 * Return an ASN1_OBJECT pointer to content type. This allows it to be
193 * accessed or set later.
196 static ASN1_OBJECT **cms_get0_econtent_type(CMS_ContentInfo *cms)
198 switch (OBJ_obj2nid(cms->contentType)) {
200 case NID_pkcs7_signed:
201 return &cms->d.signedData->encapContentInfo->eContentType;
203 case NID_pkcs7_enveloped:
204 return &cms->d.envelopedData->encryptedContentInfo->contentType;
206 case NID_pkcs7_digest:
207 return &cms->d.digestedData->encapContentInfo->eContentType;
209 case NID_pkcs7_encrypted:
210 return &cms->d.encryptedData->encryptedContentInfo->contentType;
212 case NID_id_smime_ct_authData:
213 return &cms->d.authenticatedData->encapContentInfo->eContentType;
215 case NID_id_smime_ct_compressedData:
216 return &cms->d.compressedData->encapContentInfo->eContentType;
219 CMSerr(CMS_F_CMS_GET0_ECONTENT_TYPE, CMS_R_UNSUPPORTED_CONTENT_TYPE);
225 const ASN1_OBJECT *CMS_get0_eContentType(CMS_ContentInfo *cms)
227 ASN1_OBJECT **petype;
228 petype = cms_get0_econtent_type(cms);
234 int CMS_set1_eContentType(CMS_ContentInfo *cms, const ASN1_OBJECT *oid)
236 ASN1_OBJECT **petype, *etype;
237 petype = cms_get0_econtent_type(cms);
242 etype = OBJ_dup(oid);
245 ASN1_OBJECT_free(*petype);
250 int CMS_is_detached(CMS_ContentInfo *cms)
252 ASN1_OCTET_STRING **pos;
253 pos = CMS_get0_content(cms);
261 int CMS_set_detached(CMS_ContentInfo *cms, int detached)
263 ASN1_OCTET_STRING **pos;
264 pos = CMS_get0_content(cms);
268 ASN1_OCTET_STRING_free(*pos);
273 *pos = ASN1_OCTET_STRING_new();
276 * NB: special flag to show content is created and not read in.
278 (*pos)->flags |= ASN1_STRING_FLAG_CONT;
281 CMSerr(CMS_F_CMS_SET_DETACHED, ERR_R_MALLOC_FAILURE);
285 /* Create a digest BIO from an X509_ALGOR structure */
287 BIO *cms_DigestAlgorithm_init_bio(X509_ALGOR *digestAlgorithm)
290 const ASN1_OBJECT *digestoid;
291 const EVP_MD *digest;
292 X509_ALGOR_get0(&digestoid, NULL, NULL, digestAlgorithm);
293 digest = EVP_get_digestbyobj(digestoid);
295 CMSerr(CMS_F_CMS_DIGESTALGORITHM_INIT_BIO,
296 CMS_R_UNKNOWN_DIGEST_ALGORITHM);
299 mdbio = BIO_new(BIO_f_md());
300 if (mdbio == NULL || !BIO_set_md(mdbio, digest)) {
301 CMSerr(CMS_F_CMS_DIGESTALGORITHM_INIT_BIO, CMS_R_MD_BIO_INIT_ERROR);
310 /* Locate a message digest content from a BIO chain based on SignerInfo */
312 int cms_DigestAlgorithm_find_ctx(EVP_MD_CTX *mctx, BIO *chain,
316 const ASN1_OBJECT *mdoid;
317 X509_ALGOR_get0(&mdoid, NULL, NULL, mdalg);
318 nid = OBJ_obj2nid(mdoid);
319 /* Look for digest type to match signature */
322 chain = BIO_find_type(chain, BIO_TYPE_MD);
324 CMSerr(CMS_F_CMS_DIGESTALGORITHM_FIND_CTX,
325 CMS_R_NO_MATCHING_DIGEST);
328 BIO_get_md_ctx(chain, &mtmp);
329 if (EVP_MD_CTX_type(mtmp) == nid
331 * Workaround for broken implementations that use signature
332 * algorithm OID instead of digest.
334 || EVP_MD_pkey_type(EVP_MD_CTX_md(mtmp)) == nid)
335 return EVP_MD_CTX_copy_ex(mctx, mtmp);
336 chain = BIO_next(chain);
340 static STACK_OF(CMS_CertificateChoices)
341 **cms_get0_certificate_choices(CMS_ContentInfo *cms)
343 switch (OBJ_obj2nid(cms->contentType)) {
345 case NID_pkcs7_signed:
346 return &cms->d.signedData->certificates;
348 case NID_pkcs7_enveloped:
349 if (cms->d.envelopedData->originatorInfo == NULL)
351 return &cms->d.envelopedData->originatorInfo->certificates;
354 CMSerr(CMS_F_CMS_GET0_CERTIFICATE_CHOICES,
355 CMS_R_UNSUPPORTED_CONTENT_TYPE);
361 CMS_CertificateChoices *CMS_add0_CertificateChoices(CMS_ContentInfo *cms)
363 STACK_OF(CMS_CertificateChoices) **pcerts;
364 CMS_CertificateChoices *cch;
365 pcerts = cms_get0_certificate_choices(cms);
369 *pcerts = sk_CMS_CertificateChoices_new_null();
372 cch = M_ASN1_new_of(CMS_CertificateChoices);
375 if (!sk_CMS_CertificateChoices_push(*pcerts, cch)) {
376 M_ASN1_free_of(cch, CMS_CertificateChoices);
382 int CMS_add0_cert(CMS_ContentInfo *cms, X509 *cert)
384 CMS_CertificateChoices *cch;
385 STACK_OF(CMS_CertificateChoices) **pcerts;
387 pcerts = cms_get0_certificate_choices(cms);
390 for (i = 0; i < sk_CMS_CertificateChoices_num(*pcerts); i++) {
391 cch = sk_CMS_CertificateChoices_value(*pcerts, i);
392 if (cch->type == CMS_CERTCHOICE_CERT) {
393 if (!X509_cmp(cch->d.certificate, cert)) {
394 CMSerr(CMS_F_CMS_ADD0_CERT,
395 CMS_R_CERTIFICATE_ALREADY_PRESENT);
400 cch = CMS_add0_CertificateChoices(cms);
403 cch->type = CMS_CERTCHOICE_CERT;
404 cch->d.certificate = cert;
408 int CMS_add1_cert(CMS_ContentInfo *cms, X509 *cert)
411 r = CMS_add0_cert(cms, cert);
417 static STACK_OF(CMS_RevocationInfoChoice)
418 **cms_get0_revocation_choices(CMS_ContentInfo *cms)
420 switch (OBJ_obj2nid(cms->contentType)) {
422 case NID_pkcs7_signed:
423 return &cms->d.signedData->crls;
425 case NID_pkcs7_enveloped:
426 if (cms->d.envelopedData->originatorInfo == NULL)
428 return &cms->d.envelopedData->originatorInfo->crls;
431 CMSerr(CMS_F_CMS_GET0_REVOCATION_CHOICES,
432 CMS_R_UNSUPPORTED_CONTENT_TYPE);
438 CMS_RevocationInfoChoice *CMS_add0_RevocationInfoChoice(CMS_ContentInfo *cms)
440 STACK_OF(CMS_RevocationInfoChoice) **pcrls;
441 CMS_RevocationInfoChoice *rch;
442 pcrls = cms_get0_revocation_choices(cms);
446 *pcrls = sk_CMS_RevocationInfoChoice_new_null();
449 rch = M_ASN1_new_of(CMS_RevocationInfoChoice);
452 if (!sk_CMS_RevocationInfoChoice_push(*pcrls, rch)) {
453 M_ASN1_free_of(rch, CMS_RevocationInfoChoice);
459 int CMS_add0_crl(CMS_ContentInfo *cms, X509_CRL *crl)
461 CMS_RevocationInfoChoice *rch;
462 rch = CMS_add0_RevocationInfoChoice(cms);
465 rch->type = CMS_REVCHOICE_CRL;
470 int CMS_add1_crl(CMS_ContentInfo *cms, X509_CRL *crl)
473 r = CMS_add0_crl(cms, crl);
475 X509_CRL_up_ref(crl);
479 STACK_OF(X509) *CMS_get1_certs(CMS_ContentInfo *cms)
481 STACK_OF(X509) *certs = NULL;
482 CMS_CertificateChoices *cch;
483 STACK_OF(CMS_CertificateChoices) **pcerts;
485 pcerts = cms_get0_certificate_choices(cms);
488 for (i = 0; i < sk_CMS_CertificateChoices_num(*pcerts); i++) {
489 cch = sk_CMS_CertificateChoices_value(*pcerts, i);
490 if (cch->type == 0) {
492 certs = sk_X509_new_null();
496 if (!sk_X509_push(certs, cch->d.certificate)) {
497 sk_X509_pop_free(certs, X509_free);
500 X509_up_ref(cch->d.certificate);
507 STACK_OF(X509_CRL) *CMS_get1_crls(CMS_ContentInfo *cms)
509 STACK_OF(X509_CRL) *crls = NULL;
510 STACK_OF(CMS_RevocationInfoChoice) **pcrls;
511 CMS_RevocationInfoChoice *rch;
513 pcrls = cms_get0_revocation_choices(cms);
516 for (i = 0; i < sk_CMS_RevocationInfoChoice_num(*pcrls); i++) {
517 rch = sk_CMS_RevocationInfoChoice_value(*pcrls, i);
518 if (rch->type == 0) {
520 crls = sk_X509_CRL_new_null();
524 if (!sk_X509_CRL_push(crls, rch->d.crl)) {
525 sk_X509_CRL_pop_free(crls, X509_CRL_free);
528 X509_CRL_up_ref(rch->d.crl);
534 int cms_ias_cert_cmp(CMS_IssuerAndSerialNumber *ias, X509 *cert)
537 ret = X509_NAME_cmp(ias->issuer, X509_get_issuer_name(cert));
540 return ASN1_INTEGER_cmp(ias->serialNumber, X509_get_serialNumber(cert));
543 int cms_keyid_cert_cmp(ASN1_OCTET_STRING *keyid, X509 *cert)
545 const ASN1_OCTET_STRING *cert_keyid = X509_get0_subject_key_id(cert);
547 if (cert_keyid == NULL)
549 return ASN1_OCTET_STRING_cmp(keyid, cert_keyid);
552 int cms_set1_ias(CMS_IssuerAndSerialNumber **pias, X509 *cert)
554 CMS_IssuerAndSerialNumber *ias;
555 ias = M_ASN1_new_of(CMS_IssuerAndSerialNumber);
558 if (!X509_NAME_set(&ias->issuer, X509_get_issuer_name(cert)))
560 if (!ASN1_STRING_copy(ias->serialNumber, X509_get_serialNumber(cert)))
562 M_ASN1_free_of(*pias, CMS_IssuerAndSerialNumber);
566 M_ASN1_free_of(ias, CMS_IssuerAndSerialNumber);
567 CMSerr(CMS_F_CMS_SET1_IAS, ERR_R_MALLOC_FAILURE);
571 int cms_set1_keyid(ASN1_OCTET_STRING **pkeyid, X509 *cert)
573 ASN1_OCTET_STRING *keyid = NULL;
574 const ASN1_OCTET_STRING *cert_keyid;
575 cert_keyid = X509_get0_subject_key_id(cert);
576 if (cert_keyid == NULL) {
577 CMSerr(CMS_F_CMS_SET1_KEYID, CMS_R_CERTIFICATE_HAS_NO_KEYID);
580 keyid = ASN1_STRING_dup(cert_keyid);
582 CMSerr(CMS_F_CMS_SET1_KEYID, ERR_R_MALLOC_FAILURE);
585 ASN1_OCTET_STRING_free(*pkeyid);