1 /* crypto/cms/cms_lib.c */
2 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
5 /* ====================================================================
6 * Copyright (c) 2008 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
54 #include <openssl/asn1t.h>
55 #include <openssl/x509v3.h>
56 #include <openssl/err.h>
57 #include <openssl/pem.h>
58 #include <openssl/bio.h>
59 #include <openssl/asn1.h>
63 IMPLEMENT_ASN1_FUNCTIONS(CMS_ContentInfo)
64 IMPLEMENT_ASN1_PRINT_FUNCTION(CMS_ContentInfo)
66 DECLARE_ASN1_ITEM(CMS_CertificateChoices)
67 DECLARE_ASN1_ITEM(CMS_RevocationInfoChoice)
68 DECLARE_STACK_OF(CMS_CertificateChoices)
69 DECLARE_STACK_OF(CMS_RevocationInfoChoice)
71 const ASN1_OBJECT *CMS_get0_type(CMS_ContentInfo *cms)
73 return cms->contentType;
76 CMS_ContentInfo *cms_Data_create(void)
79 cms = CMS_ContentInfo_new();
82 cms->contentType = OBJ_nid2obj(NID_pkcs7_data);
84 CMS_set_detached(cms, 0);
89 BIO *cms_content_bio(CMS_ContentInfo *cms)
91 ASN1_OCTET_STRING **pos = CMS_get0_content(cms);
94 /* If content detached data goes nowhere: create NULL BIO */
96 return BIO_new(BIO_s_null());
97 /* If content not detached and created return memory BIO
99 if (!*pos || ((*pos)->flags == ASN1_STRING_FLAG_CONT))
100 return BIO_new(BIO_s_mem());
101 /* Else content was read in: return read only BIO for it */
102 return BIO_new_mem_buf((*pos)->data, (*pos)->length);
105 BIO *CMS_dataInit(CMS_ContentInfo *cms, BIO *icont)
111 cont = cms_content_bio(cms);
114 CMSerr(CMS_F_CMS_DATAINIT, CMS_R_NO_CONTENT);
117 switch (OBJ_obj2nid(cms->contentType))
123 case NID_pkcs7_signed:
124 cmsbio = cms_SignedData_init_bio(cms);
127 case NID_pkcs7_digest:
128 cmsbio = cms_DigestedData_init_bio(cms);
131 case NID_id_smime_ct_compressedData:
132 cmsbio = cms_CompressedData_init_bio(cms);
136 case NID_pkcs7_encrypted:
137 cmsbio = cms_EncryptedData_init_bio(cms);
140 case NID_pkcs7_enveloped:
141 cmsbio = cms_EnvelopedData_init_bio(cms);
145 CMSerr(CMS_F_CMS_DATAINIT, CMS_R_UNSUPPORTED_TYPE);
150 return BIO_push(cmsbio, cont);
158 int CMS_dataFinal(CMS_ContentInfo *cms, BIO *cmsbio)
160 ASN1_OCTET_STRING **pos = CMS_get0_content(cms);
163 /* If ebmedded content find memory BIO and set content */
164 if (*pos && ((*pos)->flags & ASN1_STRING_FLAG_CONT))
169 mbio = BIO_find_type(cmsbio, BIO_TYPE_MEM);
172 CMSerr(CMS_F_CMS_DATAFINAL, CMS_R_CONTENT_NOT_FOUND);
175 contlen = BIO_get_mem_data(mbio, &cont);
176 /* Set bio as read only so its content can't be clobbered */
177 BIO_set_flags(mbio, BIO_FLAGS_MEM_RDONLY);
178 BIO_set_mem_eof_return(mbio, 0);
179 ASN1_STRING_set0(*pos, cont, contlen);
180 (*pos)->flags &= ~ASN1_STRING_FLAG_CONT;
183 switch (OBJ_obj2nid(cms->contentType))
187 case NID_pkcs7_enveloped:
188 case NID_pkcs7_encrypted:
189 case NID_id_smime_ct_compressedData:
193 case NID_pkcs7_signed:
194 return cms_SignedData_final(cms, cmsbio);
196 case NID_pkcs7_digest:
197 return cms_DigestedData_do_final(cms, cmsbio, 0);
200 CMSerr(CMS_F_CMS_DATAFINAL, CMS_R_UNSUPPORTED_TYPE);
205 /* Return an OCTET STRING pointer to content. This allows it to
206 * be accessed or set later.
209 ASN1_OCTET_STRING **CMS_get0_content(CMS_ContentInfo *cms)
211 switch (OBJ_obj2nid(cms->contentType))
217 case NID_pkcs7_signed:
218 return &cms->d.signedData->encapContentInfo->eContent;
220 case NID_pkcs7_enveloped:
221 return &cms->d.envelopedData->encryptedContentInfo->encryptedContent;
223 case NID_pkcs7_digest:
224 return &cms->d.digestedData->encapContentInfo->eContent;
226 case NID_pkcs7_encrypted:
227 return &cms->d.encryptedData->encryptedContentInfo->encryptedContent;
229 case NID_id_smime_ct_authData:
230 return &cms->d.authenticatedData->encapContentInfo->eContent;
232 case NID_id_smime_ct_compressedData:
233 return &cms->d.compressedData->encapContentInfo->eContent;
236 if (cms->d.other->type == V_ASN1_OCTET_STRING)
237 return &cms->d.other->value.octet_string;
238 CMSerr(CMS_F_CMS_GET0_CONTENT, CMS_R_UNSUPPORTED_CONTENT_TYPE);
244 /* Return an ASN1_OBJECT pointer to content type. This allows it to
245 * be accessed or set later.
248 static ASN1_OBJECT **cms_get0_econtent_type(CMS_ContentInfo *cms)
250 switch (OBJ_obj2nid(cms->contentType))
253 case NID_pkcs7_signed:
254 return &cms->d.signedData->encapContentInfo->eContentType;
256 case NID_pkcs7_enveloped:
257 return &cms->d.envelopedData->encryptedContentInfo->contentType;
259 case NID_pkcs7_digest:
260 return &cms->d.digestedData->encapContentInfo->eContentType;
262 case NID_pkcs7_encrypted:
263 return &cms->d.encryptedData->encryptedContentInfo->contentType;
265 case NID_id_smime_ct_authData:
266 return &cms->d.authenticatedData->encapContentInfo->eContentType;
268 case NID_id_smime_ct_compressedData:
269 return &cms->d.compressedData->encapContentInfo->eContentType;
272 CMSerr(CMS_F_CMS_GET0_ECONTENT_TYPE,
273 CMS_R_UNSUPPORTED_CONTENT_TYPE);
279 const ASN1_OBJECT *CMS_get0_eContentType(CMS_ContentInfo *cms)
281 ASN1_OBJECT **petype;
282 petype = cms_get0_econtent_type(cms);
288 int CMS_set1_eContentType(CMS_ContentInfo *cms, const ASN1_OBJECT *oid)
290 ASN1_OBJECT **petype, *etype;
291 petype = cms_get0_econtent_type(cms);
296 etype = OBJ_dup(oid);
299 ASN1_OBJECT_free(*petype);
304 int CMS_is_detached(CMS_ContentInfo *cms)
306 ASN1_OCTET_STRING **pos;
307 pos = CMS_get0_content(cms);
315 int CMS_set_detached(CMS_ContentInfo *cms, int detached)
317 ASN1_OCTET_STRING **pos;
318 pos = CMS_get0_content(cms);
325 ASN1_OCTET_STRING_free(*pos);
331 *pos = ASN1_OCTET_STRING_new();
334 /* NB: special flag to show content is created and not
337 (*pos)->flags |= ASN1_STRING_FLAG_CONT;
340 CMSerr(CMS_F_CMS_SET_DETACHED, ERR_R_MALLOC_FAILURE);
344 /* Set up an X509_ALGOR DigestAlgorithmIdentifier from an EVP_MD */
346 void cms_DigestAlgorithm_set(X509_ALGOR *alg, const EVP_MD *md)
350 if (md->flags & EVP_MD_FLAG_DIGALGID_ABSENT)
351 param_type = V_ASN1_UNDEF;
353 param_type = V_ASN1_NULL;
355 X509_ALGOR_set0(alg, OBJ_nid2obj(EVP_MD_type(md)), param_type, NULL);
359 /* Create a digest BIO from an X509_ALGOR structure */
361 BIO *cms_DigestAlgorithm_init_bio(X509_ALGOR *digestAlgorithm)
364 ASN1_OBJECT *digestoid;
365 const EVP_MD *digest;
366 X509_ALGOR_get0(&digestoid, NULL, NULL, digestAlgorithm);
367 digest = EVP_get_digestbyobj(digestoid);
370 CMSerr(CMS_F_CMS_DIGESTALGORITHM_INIT_BIO,
371 CMS_R_UNKNOWN_DIGEST_ALGORIHM);
374 mdbio = BIO_new(BIO_f_md());
375 if (!mdbio || !BIO_set_md(mdbio, digest))
377 CMSerr(CMS_F_CMS_DIGESTALGORITHM_INIT_BIO,
378 CMS_R_MD_BIO_INIT_ERROR);
388 /* Locate a message digest content from a BIO chain based on SignerInfo */
390 int cms_DigestAlgorithm_find_ctx(EVP_MD_CTX *mctx, BIO *chain,
395 X509_ALGOR_get0(&mdoid, NULL, NULL, mdalg);
396 nid = OBJ_obj2nid(mdoid);
397 /* Look for digest type to match signature */
401 chain = BIO_find_type(chain, BIO_TYPE_MD);
404 CMSerr(CMS_F_CMS_DIGESTALGORITHM_FIND_CTX,
405 CMS_R_NO_MATCHING_DIGEST);
408 BIO_get_md_ctx(chain, &mtmp);
409 if (EVP_MD_CTX_type(mtmp) == nid
410 /* Workaround for broken implementations that use signature
411 * algorithm OID instead of digest.
413 || EVP_MD_pkey_type(EVP_MD_CTX_md(mtmp)) == nid)
414 return EVP_MD_CTX_copy_ex(mctx, mtmp);
415 chain = BIO_next(chain);
419 static STACK_OF(CMS_CertificateChoices) **cms_get0_certificate_choices(CMS_ContentInfo *cms)
421 switch (OBJ_obj2nid(cms->contentType))
424 case NID_pkcs7_signed:
425 return &cms->d.signedData->certificates;
427 case NID_pkcs7_enveloped:
428 return &cms->d.envelopedData->originatorInfo->certificates;
431 CMSerr(CMS_F_CMS_GET0_CERTIFICATE_CHOICES,
432 CMS_R_UNSUPPORTED_CONTENT_TYPE);
438 CMS_CertificateChoices *CMS_add0_CertificateChoices(CMS_ContentInfo *cms)
440 STACK_OF(CMS_CertificateChoices) **pcerts;
441 CMS_CertificateChoices *cch;
442 pcerts = cms_get0_certificate_choices(cms);
446 *pcerts = sk_CMS_CertificateChoices_new_null();
449 cch = M_ASN1_new_of(CMS_CertificateChoices);
452 if (!sk_CMS_CertificateChoices_push(*pcerts, cch))
454 M_ASN1_free_of(cch, CMS_CertificateChoices);
460 int CMS_add0_cert(CMS_ContentInfo *cms, X509 *cert)
462 CMS_CertificateChoices *cch;
463 STACK_OF(CMS_CertificateChoices) **pcerts;
465 pcerts = cms_get0_certificate_choices(cms);
468 for (i = 0; i < sk_CMS_CertificateChoices_num(*pcerts); i++)
470 cch = sk_CMS_CertificateChoices_value(*pcerts, i);
471 if (cch->type == CMS_CERTCHOICE_CERT)
473 if (!X509_cmp(cch->d.certificate, cert))
475 CMSerr(CMS_F_CMS_ADD0_CERT,
476 CMS_R_CERTIFICATE_ALREADY_PRESENT);
481 cch = CMS_add0_CertificateChoices(cms);
484 cch->type = CMS_CERTCHOICE_CERT;
485 cch->d.certificate = cert;
489 int CMS_add1_cert(CMS_ContentInfo *cms, X509 *cert)
492 r = CMS_add0_cert(cms, cert);
494 CRYPTO_add(&cert->references, 1, CRYPTO_LOCK_X509);
498 static STACK_OF(CMS_RevocationInfoChoice) **cms_get0_revocation_choices(CMS_ContentInfo *cms)
500 switch (OBJ_obj2nid(cms->contentType))
503 case NID_pkcs7_signed:
504 return &cms->d.signedData->crls;
506 case NID_pkcs7_enveloped:
507 return &cms->d.envelopedData->originatorInfo->crls;
510 CMSerr(CMS_F_CMS_GET0_REVOCATION_CHOICES,
511 CMS_R_UNSUPPORTED_CONTENT_TYPE);
517 CMS_RevocationInfoChoice *CMS_add0_RevocationInfoChoice(CMS_ContentInfo *cms)
519 STACK_OF(CMS_RevocationInfoChoice) **pcrls;
520 CMS_RevocationInfoChoice *rch;
521 pcrls = cms_get0_revocation_choices(cms);
525 *pcrls = sk_CMS_RevocationInfoChoice_new_null();
528 rch = M_ASN1_new_of(CMS_RevocationInfoChoice);
531 if (!sk_CMS_RevocationInfoChoice_push(*pcrls, rch))
533 M_ASN1_free_of(rch, CMS_RevocationInfoChoice);
539 int CMS_add0_crl(CMS_ContentInfo *cms, X509_CRL *crl)
541 CMS_RevocationInfoChoice *rch;
542 rch = CMS_add0_RevocationInfoChoice(cms);
545 rch->type = CMS_REVCHOICE_CRL;
550 int CMS_add1_crl(CMS_ContentInfo *cms, X509_CRL *crl)
553 r = CMS_add0_crl(cms, crl);
555 CRYPTO_add(&crl->references, 1, CRYPTO_LOCK_X509_CRL);
559 STACK_OF(X509) *CMS_get1_certs(CMS_ContentInfo *cms)
561 STACK_OF(X509) *certs = NULL;
562 CMS_CertificateChoices *cch;
563 STACK_OF(CMS_CertificateChoices) **pcerts;
565 pcerts = cms_get0_certificate_choices(cms);
568 for (i = 0; i < sk_CMS_CertificateChoices_num(*pcerts); i++)
570 cch = sk_CMS_CertificateChoices_value(*pcerts, i);
575 certs = sk_X509_new_null();
579 if (!sk_X509_push(certs, cch->d.certificate))
581 sk_X509_pop_free(certs, X509_free);
584 CRYPTO_add(&cch->d.certificate->references,
585 1, CRYPTO_LOCK_X509);
592 STACK_OF(X509_CRL) *CMS_get1_crls(CMS_ContentInfo *cms)
594 STACK_OF(X509_CRL) *crls = NULL;
595 STACK_OF(CMS_RevocationInfoChoice) **pcrls;
596 CMS_RevocationInfoChoice *rch;
598 pcrls = cms_get0_revocation_choices(cms);
601 for (i = 0; i < sk_CMS_RevocationInfoChoice_num(*pcrls); i++)
603 rch = sk_CMS_RevocationInfoChoice_value(*pcrls, i);
608 crls = sk_X509_CRL_new_null();
612 if (!sk_X509_CRL_push(crls, rch->d.crl))
614 sk_X509_CRL_pop_free(crls, X509_CRL_free);
617 CRYPTO_add(&rch->d.crl->references,
618 1, CRYPTO_LOCK_X509_CRL);
624 int cms_ias_cert_cmp(CMS_IssuerAndSerialNumber *ias, X509 *cert)
627 ret = X509_NAME_cmp(ias->issuer, X509_get_issuer_name(cert));
630 return ASN1_INTEGER_cmp(ias->serialNumber, X509_get_serialNumber(cert));
633 int cms_keyid_cert_cmp(ASN1_OCTET_STRING *keyid, X509 *cert)
635 X509_check_purpose(cert, -1, -1);
638 return ASN1_OCTET_STRING_cmp(keyid, cert->skid);
641 int cms_set1_ias(CMS_IssuerAndSerialNumber **pias, X509 *cert)
643 CMS_IssuerAndSerialNumber *ias;
644 ias = M_ASN1_new_of(CMS_IssuerAndSerialNumber);
647 if (!X509_NAME_set(&ias->issuer, X509_get_issuer_name(cert)))
649 if (!ASN1_STRING_copy(ias->serialNumber, X509_get_serialNumber(cert)))
652 M_ASN1_free_of(*pias, CMS_IssuerAndSerialNumber);
657 M_ASN1_free_of(ias, CMS_IssuerAndSerialNumber);
658 CMSerr(CMS_F_CMS_SET1_IAS, ERR_R_MALLOC_FAILURE);
662 int cms_set1_keyid(ASN1_OCTET_STRING **pkeyid, X509 *cert)
664 ASN1_OCTET_STRING *keyid = NULL;
665 X509_check_purpose(cert, -1, -1);
668 CMSerr(CMS_F_CMS_SET1_KEYID, CMS_R_CERTIFICATE_HAS_NO_KEYID);
671 keyid = ASN1_STRING_dup(cert->skid);
674 CMSerr(CMS_F_CMS_SET1_KEYID, ERR_R_MALLOC_FAILURE);
678 ASN1_OCTET_STRING_free(*pkeyid);