1 /* $OpenBSD: cms_lib.c,v 1.14 2019/08/12 18:13:13 jsing Exp $ */
3 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
6 /* ====================================================================
7 * Copyright (c) 2008 The OpenSSL Project. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
21 * 3. All advertising materials mentioning features or use of this
22 * software must display the following acknowledgment:
23 * "This product includes software developed by the OpenSSL Project
24 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
26 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27 * endorse or promote products derived from this software without
28 * prior written permission. For written permission, please contact
29 * licensing@OpenSSL.org.
31 * 5. Products derived from this software may not be called "OpenSSL"
32 * nor may "OpenSSL" appear in their names without prior written
33 * permission of the OpenSSL Project.
35 * 6. Redistributions of any form whatsoever must retain the following
37 * "This product includes software developed by the OpenSSL Project
38 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
40 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
44 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51 * OF THE POSSIBILITY OF SUCH DAMAGE.
52 * ====================================================================
55 #include <openssl/asn1t.h>
56 #include <openssl/x509v3.h>
57 #include <openssl/err.h>
58 #include <openssl/pem.h>
59 #include <openssl/bio.h>
60 #include <openssl/asn1.h>
61 #include <openssl/cms.h>
66 d2i_CMS_ContentInfo(CMS_ContentInfo **a, const unsigned char **in, long len)
68 return (CMS_ContentInfo *)ASN1_item_d2i((ASN1_VALUE **)a, in, len,
73 i2d_CMS_ContentInfo(CMS_ContentInfo *a, unsigned char **out)
75 return ASN1_item_i2d((ASN1_VALUE *)a, out, &CMS_ContentInfo_it);
79 CMS_ContentInfo_new(void)
81 return (CMS_ContentInfo *)ASN1_item_new(&CMS_ContentInfo_it);
85 CMS_ContentInfo_free(CMS_ContentInfo *a)
87 ASN1_item_free((ASN1_VALUE *)a, &CMS_ContentInfo_it);
91 CMS_ContentInfo_print_ctx(BIO *out, CMS_ContentInfo *x, int indent, const ASN1_PCTX *pctx)
93 return ASN1_item_print(out, (ASN1_VALUE *)x, indent,
94 &CMS_ContentInfo_it, pctx);
98 CMS_get0_type(const CMS_ContentInfo *cms)
100 return cms->contentType;
104 cms_Data_create(void)
106 CMS_ContentInfo *cms;
108 cms = CMS_ContentInfo_new();
110 cms->contentType = OBJ_nid2obj(NID_pkcs7_data);
112 CMS_set_detached(cms, 0);
118 cms_content_bio(CMS_ContentInfo *cms)
120 ASN1_OCTET_STRING **pos = CMS_get0_content(cms);
124 /* If content detached data goes nowhere: create NULL BIO */
126 return BIO_new(BIO_s_null());
128 * If content not detached and created return memory BIO
130 if (!*pos || ((*pos)->flags == ASN1_STRING_FLAG_CONT))
131 return BIO_new(BIO_s_mem());
133 /* Else content was read in: return read only BIO for it */
134 return BIO_new_mem_buf((*pos)->data, (*pos)->length);
138 CMS_dataInit(CMS_ContentInfo *cms, BIO *icont)
145 cont = cms_content_bio(cms);
147 CMSerror(CMS_R_NO_CONTENT);
150 switch (OBJ_obj2nid(cms->contentType)) {
155 case NID_pkcs7_signed:
156 cmsbio = cms_SignedData_init_bio(cms);
159 case NID_pkcs7_digest:
160 cmsbio = cms_DigestedData_init_bio(cms);
163 case NID_id_smime_ct_compressedData:
164 cmsbio = cms_CompressedData_init_bio(cms);
168 case NID_pkcs7_encrypted:
169 cmsbio = cms_EncryptedData_init_bio(cms);
172 case NID_pkcs7_enveloped:
173 cmsbio = cms_EnvelopedData_init_bio(cms);
177 CMSerror(CMS_R_UNSUPPORTED_TYPE);
182 return BIO_push(cmsbio, cont);
191 CMS_dataFinal(CMS_ContentInfo *cms, BIO *cmsbio)
193 ASN1_OCTET_STRING **pos = CMS_get0_content(cms);
197 /* If embedded content find memory BIO and set content */
198 if (*pos && ((*pos)->flags & ASN1_STRING_FLAG_CONT)) {
202 mbio = BIO_find_type(cmsbio, BIO_TYPE_MEM);
204 CMSerror(CMS_R_CONTENT_NOT_FOUND);
207 contlen = BIO_get_mem_data(mbio, &cont);
208 /* Set bio as read only so its content can't be clobbered */
209 BIO_set_flags(mbio, BIO_FLAGS_MEM_RDONLY);
210 BIO_set_mem_eof_return(mbio, 0);
211 ASN1_STRING_set0(*pos, cont, contlen);
212 (*pos)->flags &= ~ASN1_STRING_FLAG_CONT;
215 switch (OBJ_obj2nid(cms->contentType)) {
218 case NID_pkcs7_enveloped:
219 case NID_pkcs7_encrypted:
220 case NID_id_smime_ct_compressedData:
224 case NID_pkcs7_signed:
225 return cms_SignedData_final(cms, cmsbio);
227 case NID_pkcs7_digest:
228 return cms_DigestedData_do_final(cms, cmsbio, 0);
231 CMSerror(CMS_R_UNSUPPORTED_TYPE);
237 * Return an OCTET STRING pointer to content. This allows it to be accessed
242 CMS_get0_content(CMS_ContentInfo *cms)
244 switch (OBJ_obj2nid(cms->contentType)) {
248 case NID_pkcs7_signed:
249 return &cms->d.signedData->encapContentInfo->eContent;
251 case NID_pkcs7_enveloped:
252 return &cms->d.envelopedData->encryptedContentInfo->encryptedContent;
254 case NID_pkcs7_digest:
255 return &cms->d.digestedData->encapContentInfo->eContent;
257 case NID_pkcs7_encrypted:
258 return &cms->d.encryptedData->encryptedContentInfo->encryptedContent;
260 case NID_id_smime_ct_authData:
261 return &cms->d.authenticatedData->encapContentInfo->eContent;
263 case NID_id_smime_ct_compressedData:
264 return &cms->d.compressedData->encapContentInfo->eContent;
267 if (cms->d.other->type == V_ASN1_OCTET_STRING)
268 return &cms->d.other->value.octet_string;
269 CMSerror(CMS_R_UNSUPPORTED_CONTENT_TYPE);
275 * Return an ASN1_OBJECT pointer to content type. This allows it to be
276 * accessed or set later.
279 static ASN1_OBJECT **
280 cms_get0_econtent_type(CMS_ContentInfo *cms)
282 switch (OBJ_obj2nid(cms->contentType)) {
283 case NID_pkcs7_signed:
284 return &cms->d.signedData->encapContentInfo->eContentType;
286 case NID_pkcs7_enveloped:
287 return &cms->d.envelopedData->encryptedContentInfo->contentType;
289 case NID_pkcs7_digest:
290 return &cms->d.digestedData->encapContentInfo->eContentType;
292 case NID_pkcs7_encrypted:
293 return &cms->d.encryptedData->encryptedContentInfo->contentType;
295 case NID_id_smime_ct_authData:
296 return &cms->d.authenticatedData->encapContentInfo->eContentType;
298 case NID_id_smime_ct_compressedData:
299 return &cms->d.compressedData->encapContentInfo->eContentType;
302 CMSerror(CMS_R_UNSUPPORTED_CONTENT_TYPE);
308 CMS_get0_eContentType(CMS_ContentInfo *cms)
310 ASN1_OBJECT **petype;
312 petype = cms_get0_econtent_type(cms);
320 CMS_set1_eContentType(CMS_ContentInfo *cms, const ASN1_OBJECT *oid)
322 ASN1_OBJECT **petype, *etype;
324 petype = cms_get0_econtent_type(cms);
329 etype = OBJ_dup(oid);
332 ASN1_OBJECT_free(*petype);
339 CMS_is_detached(CMS_ContentInfo *cms)
341 ASN1_OCTET_STRING **pos;
343 pos = CMS_get0_content(cms);
353 CMS_set_detached(CMS_ContentInfo *cms, int detached)
355 ASN1_OCTET_STRING **pos;
357 pos = CMS_get0_content(cms);
361 ASN1_OCTET_STRING_free(*pos);
366 *pos = ASN1_OCTET_STRING_new();
369 * NB: special flag to show content is created and not read in.
371 (*pos)->flags |= ASN1_STRING_FLAG_CONT;
374 CMSerror(ERR_R_MALLOC_FAILURE);
379 /* Create a digest BIO from an X509_ALGOR structure */
382 cms_DigestAlgorithm_init_bio(X509_ALGOR *digestAlgorithm)
385 const ASN1_OBJECT *digestoid;
386 const EVP_MD *digest;
388 X509_ALGOR_get0(&digestoid, NULL, NULL, digestAlgorithm);
389 digest = EVP_get_digestbyobj(digestoid);
391 CMSerror(CMS_R_UNKNOWN_DIGEST_ALGORITHM);
394 mdbio = BIO_new(BIO_f_md());
395 if (mdbio == NULL || !BIO_set_md(mdbio, digest)) {
396 CMSerror(CMS_R_MD_BIO_INIT_ERROR);
407 /* Locate a message digest content from a BIO chain based on SignerInfo */
410 cms_DigestAlgorithm_find_ctx(EVP_MD_CTX *mctx, BIO *chain, X509_ALGOR *mdalg)
413 const ASN1_OBJECT *mdoid;
415 X509_ALGOR_get0(&mdoid, NULL, NULL, mdalg);
416 nid = OBJ_obj2nid(mdoid);
417 /* Look for digest type to match signature */
420 chain = BIO_find_type(chain, BIO_TYPE_MD);
422 CMSerror(CMS_R_NO_MATCHING_DIGEST);
425 BIO_get_md_ctx(chain, &mtmp);
426 if (EVP_MD_CTX_type(mtmp) == nid
428 * Workaround for broken implementations that use signature
429 * algorithm OID instead of digest.
431 || EVP_MD_pkey_type(EVP_MD_CTX_md(mtmp)) == nid)
432 return EVP_MD_CTX_copy_ex(mctx, mtmp);
433 chain = BIO_next(chain);
437 static STACK_OF(CMS_CertificateChoices) **
438 cms_get0_certificate_choices(CMS_ContentInfo *cms)
440 switch (OBJ_obj2nid(cms->contentType)) {
441 case NID_pkcs7_signed:
442 return &cms->d.signedData->certificates;
444 case NID_pkcs7_enveloped:
445 if (cms->d.envelopedData->originatorInfo == NULL)
447 return &cms->d.envelopedData->originatorInfo->certificates;
450 CMSerror(CMS_R_UNSUPPORTED_CONTENT_TYPE);
455 CMS_CertificateChoices *
456 CMS_add0_CertificateChoices(CMS_ContentInfo *cms)
458 STACK_OF(CMS_CertificateChoices) **pcerts;
459 CMS_CertificateChoices *cch;
461 pcerts = cms_get0_certificate_choices(cms);
465 *pcerts = sk_CMS_CertificateChoices_new_null();
468 cch = (CMS_CertificateChoices *)ASN1_item_new(&CMS_CertificateChoices_it);
471 if (!sk_CMS_CertificateChoices_push(*pcerts, cch)) {
472 ASN1_item_free((ASN1_VALUE *)cch, &CMS_CertificateChoices_it);
480 CMS_add0_cert(CMS_ContentInfo *cms, X509 *cert)
482 CMS_CertificateChoices *cch;
483 STACK_OF(CMS_CertificateChoices) **pcerts;
486 pcerts = cms_get0_certificate_choices(cms);
489 for (i = 0; i < sk_CMS_CertificateChoices_num(*pcerts); i++) {
490 cch = sk_CMS_CertificateChoices_value(*pcerts, i);
491 if (cch->type == CMS_CERTCHOICE_CERT) {
492 if (!X509_cmp(cch->d.certificate, cert)) {
493 CMSerror(CMS_R_CERTIFICATE_ALREADY_PRESENT);
498 cch = CMS_add0_CertificateChoices(cms);
501 cch->type = CMS_CERTCHOICE_CERT;
502 cch->d.certificate = cert;
508 CMS_add1_cert(CMS_ContentInfo *cms, X509 *cert)
512 r = CMS_add0_cert(cms, cert);
519 static STACK_OF(CMS_RevocationInfoChoice) **
520 cms_get0_revocation_choices(CMS_ContentInfo *cms)
522 switch (OBJ_obj2nid(cms->contentType)) {
523 case NID_pkcs7_signed:
524 return &cms->d.signedData->crls;
526 case NID_pkcs7_enveloped:
527 if (cms->d.envelopedData->originatorInfo == NULL)
529 return &cms->d.envelopedData->originatorInfo->crls;
532 CMSerror(CMS_R_UNSUPPORTED_CONTENT_TYPE);
537 CMS_RevocationInfoChoice *
538 CMS_add0_RevocationInfoChoice(CMS_ContentInfo *cms)
540 STACK_OF(CMS_RevocationInfoChoice) **pcrls;
541 CMS_RevocationInfoChoice *rch;
543 pcrls = cms_get0_revocation_choices(cms);
547 *pcrls = sk_CMS_RevocationInfoChoice_new_null();
550 rch = (CMS_RevocationInfoChoice *)ASN1_item_new(&CMS_RevocationInfoChoice_it);
553 if (!sk_CMS_RevocationInfoChoice_push(*pcrls, rch)) {
554 ASN1_item_free((ASN1_VALUE *)rch, &CMS_RevocationInfoChoice_it);
562 CMS_add0_crl(CMS_ContentInfo *cms, X509_CRL *crl)
564 CMS_RevocationInfoChoice *rch;
566 rch = CMS_add0_RevocationInfoChoice(cms);
569 rch->type = CMS_REVCHOICE_CRL;
576 CMS_add1_crl(CMS_ContentInfo *cms, X509_CRL *crl)
580 r = CMS_add0_crl(cms, crl);
582 X509_CRL_up_ref(crl);
588 CMS_get1_certs(CMS_ContentInfo *cms)
590 STACK_OF(X509) *certs = NULL;
591 CMS_CertificateChoices *cch;
592 STACK_OF(CMS_CertificateChoices) **pcerts;
595 pcerts = cms_get0_certificate_choices(cms);
598 for (i = 0; i < sk_CMS_CertificateChoices_num(*pcerts); i++) {
599 cch = sk_CMS_CertificateChoices_value(*pcerts, i);
600 if (cch->type == 0) {
602 certs = sk_X509_new_null();
606 if (!sk_X509_push(certs, cch->d.certificate)) {
607 sk_X509_pop_free(certs, X509_free);
610 X509_up_ref(cch->d.certificate);
617 CMS_get1_crls(CMS_ContentInfo *cms)
619 STACK_OF(X509_CRL) *crls = NULL;
620 STACK_OF(CMS_RevocationInfoChoice) **pcrls;
621 CMS_RevocationInfoChoice *rch;
624 pcrls = cms_get0_revocation_choices(cms);
627 for (i = 0; i < sk_CMS_RevocationInfoChoice_num(*pcrls); i++) {
628 rch = sk_CMS_RevocationInfoChoice_value(*pcrls, i);
629 if (rch->type == 0) {
631 crls = sk_X509_CRL_new_null();
635 if (!sk_X509_CRL_push(crls, rch->d.crl)) {
636 sk_X509_CRL_pop_free(crls, X509_CRL_free);
639 X509_CRL_up_ref(rch->d.crl);
645 static const ASN1_OCTET_STRING *
646 cms_X509_get0_subject_key_id(X509 *x)
648 /* Call for side-effect of computing hash and caching extensions */
649 X509_check_purpose(x, -1, -1);
654 cms_ias_cert_cmp(CMS_IssuerAndSerialNumber *ias, X509 *cert)
658 ret = X509_NAME_cmp(ias->issuer, X509_get_issuer_name(cert));
662 return ASN1_INTEGER_cmp(ias->serialNumber, X509_get_serialNumber(cert));
666 cms_keyid_cert_cmp(ASN1_OCTET_STRING *keyid, X509 *cert)
668 const ASN1_OCTET_STRING *cert_keyid = cms_X509_get0_subject_key_id(cert);
670 if (cert_keyid == NULL)
673 return ASN1_OCTET_STRING_cmp(keyid, cert_keyid);
677 cms_set1_ias(CMS_IssuerAndSerialNumber **pias, X509 *cert)
679 CMS_IssuerAndSerialNumber *ias;
681 ias = (CMS_IssuerAndSerialNumber *)ASN1_item_new(&CMS_IssuerAndSerialNumber_it);
684 if (!X509_NAME_set(&ias->issuer, X509_get_issuer_name(cert)))
686 if (!ASN1_STRING_copy(ias->serialNumber, X509_get_serialNumber(cert)))
688 ASN1_item_free((ASN1_VALUE *)*pias, &CMS_IssuerAndSerialNumber_it);
694 ASN1_item_free((ASN1_VALUE *)ias, &CMS_IssuerAndSerialNumber_it);
695 CMSerror(ERR_R_MALLOC_FAILURE);
701 cms_set1_keyid(ASN1_OCTET_STRING **pkeyid, X509 *cert)
703 ASN1_OCTET_STRING *keyid = NULL;
704 const ASN1_OCTET_STRING *cert_keyid;
706 cert_keyid = cms_X509_get0_subject_key_id(cert);
707 if (cert_keyid == NULL) {
708 CMSerror(CMS_R_CERTIFICATE_HAS_NO_KEYID);
711 keyid = ASN1_STRING_dup(cert_keyid);
713 CMSerror(ERR_R_MALLOC_FAILURE);
716 ASN1_OCTET_STRING_free(*pkeyid);