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/x509.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)
72 IMPLEMENT_ASN1_ALLOC_FUNCTIONS(CMS_CertificateChoices)
73 IMPLEMENT_ASN1_ALLOC_FUNCTIONS(CMS_RevocationInfoChoice)
77 const ASN1_OBJECT *CMS_get0_type(CMS_ContentInfo *cms)
79 return cms->contentType;
82 CMS_ContentInfo *cms_Data_create(void)
85 cms = CMS_ContentInfo_new();
88 cms->contentType = OBJ_nid2obj(NID_pkcs7_data);
90 CMS_set_detached(cms, 0);
95 BIO *cms_content_bio(CMS_ContentInfo *cms)
97 ASN1_OCTET_STRING **pos = CMS_get0_content(cms);
100 /* If content detached data goes nowhere: create NULL BIO */
102 return BIO_new(BIO_s_null());
103 /* If content not detached and created return memory BIO
105 if (!*pos || ((*pos)->flags == ASN1_STRING_FLAG_CONT))
106 return BIO_new(BIO_s_mem());
107 /* Else content was read in: return read only BIO for it */
108 return BIO_new_mem_buf((*pos)->data, (*pos)->length);
111 BIO *CMS_dataInit(CMS_ContentInfo *cms, BIO *icont)
117 cont = cms_content_bio(cms);
120 CMSerr(CMS_F_CMS_DATAINIT, CMS_R_NO_CONTENT);
123 switch (OBJ_obj2nid(cms->contentType))
129 case NID_pkcs7_signed:
130 cmsbio = cms_SignedData_init_bio(cms);
133 case NID_pkcs7_digest:
134 cmsbio = cms_DigestedData_init_bio(cms);
137 case NID_id_smime_ct_compressedData:
138 cmsbio = cms_CompressedData_init_bio(cms);
143 CMSerr(CMS_F_CMS_DATAINIT, CMS_R_UNSUPPORTED_TYPE);
148 return BIO_push(cmsbio, cont);
156 int CMS_dataFinal(CMS_ContentInfo *cms, BIO *cmsbio)
158 ASN1_OCTET_STRING **pos = CMS_get0_content(cms);
161 /* If ebmedded content find memory BIO and set content */
162 if (*pos && ((*pos)->flags & ASN1_STRING_FLAG_CONT))
167 mbio = BIO_find_type(cmsbio, BIO_TYPE_MEM);
170 CMSerr(CMS_F_CMS_DATAFINAL, CMS_R_CONTENT_NOT_FOUND);
173 contlen = BIO_get_mem_data(mbio, &cont);
174 /* Set bio as read only so its content can't be clobbered */
175 BIO_set_flags(mbio, BIO_FLAGS_MEM_RDONLY);
176 BIO_set_mem_eof_return(mbio, 0);
177 ASN1_STRING_set0(*pos, cont, contlen);
178 (*pos)->flags &= ~ASN1_STRING_FLAG_CONT;
181 switch (OBJ_obj2nid(cms->contentType))
185 case NID_id_smime_ct_compressedData:
189 case NID_pkcs7_signed:
190 return cms_SignedData_final(cms, cmsbio);
192 case NID_pkcs7_digest:
193 return cms_DigestedData_do_final(cms, cmsbio, 0);
196 CMSerr(CMS_F_CMS_DATAFINAL, CMS_R_UNSUPPORTED_TYPE);
201 /* Return an OCTET STRING pointer to content. This allows it to
202 * be accessed or set later.
205 ASN1_OCTET_STRING **CMS_get0_content(CMS_ContentInfo *cms)
207 switch (OBJ_obj2nid(cms->contentType))
213 case NID_pkcs7_signed:
214 return &cms->d.signedData->encapContentInfo->eContent;
216 case NID_pkcs7_enveloped:
217 return &cms->d.envelopedData->encryptedContentInfo->encryptedContent;
219 case NID_pkcs7_digest:
220 return &cms->d.digestedData->encapContentInfo->eContent;
222 case NID_pkcs7_encrypted:
223 return &cms->d.encryptedData->encryptedContentInfo->encryptedContent;
225 case NID_id_smime_ct_authData:
226 return &cms->d.authenticatedData->encapContentInfo->eContent;
228 case NID_id_smime_ct_compressedData:
229 return &cms->d.compressedData->encapContentInfo->eContent;
232 if (cms->d.other->type == V_ASN1_OCTET_STRING)
233 return &cms->d.other->value.octet_string;
234 CMSerr(CMS_F_CMS_GET0_CONTENT, CMS_R_UNSUPPORTED_CONTENT_TYPE);
240 /* Return an ASN1_OBJECT pointer to content type. This allows it to
241 * be accessed or set later.
244 static ASN1_OBJECT **cms_get0_econtent_type(CMS_ContentInfo *cms)
246 switch (OBJ_obj2nid(cms->contentType))
249 case NID_pkcs7_signed:
250 return &cms->d.signedData->encapContentInfo->eContentType;
252 case NID_pkcs7_enveloped:
253 return &cms->d.envelopedData->encryptedContentInfo->contentType;
255 case NID_pkcs7_digest:
256 return &cms->d.digestedData->encapContentInfo->eContentType;
258 case NID_pkcs7_encrypted:
259 return &cms->d.encryptedData->encryptedContentInfo->contentType;
261 case NID_id_smime_ct_authData:
262 return &cms->d.authenticatedData->encapContentInfo->eContentType;
264 case NID_id_smime_ct_compressedData:
265 return &cms->d.compressedData->encapContentInfo->eContentType;
268 CMSerr(CMS_F_CMS_GET0_ECONTENT_TYPE,
269 CMS_R_UNSUPPORTED_CONTENT_TYPE);
275 const ASN1_OBJECT *CMS_get0_eContentType(CMS_ContentInfo *cms)
277 ASN1_OBJECT **petype;
278 petype = cms_get0_econtent_type(cms);
284 int CMS_set1_eContentType(CMS_ContentInfo *cms, const ASN1_OBJECT *oid)
286 ASN1_OBJECT **petype, *etype;
287 petype = cms_get0_econtent_type(cms);
292 etype = OBJ_dup(oid);
295 ASN1_OBJECT_free(*petype);
300 int CMS_is_detached(CMS_ContentInfo *cms)
302 ASN1_OCTET_STRING **pos;
303 pos = CMS_get0_content(cms);
311 int CMS_set_detached(CMS_ContentInfo *cms, int detached)
313 ASN1_OCTET_STRING **pos;
314 pos = CMS_get0_content(cms);
321 ASN1_OCTET_STRING_free(*pos);
327 *pos = ASN1_OCTET_STRING_new();
330 /* NB: special flag to show content is created and not
333 (*pos)->flags |= ASN1_STRING_FLAG_CONT;
336 CMSerr(CMS_F_CMS_SET_DETACHED, ERR_R_MALLOC_FAILURE);
340 /* Set up an X509_ALGOR DigestAlgorithmIdentifier from an EVP_MD */
342 void cms_DigestAlgorithm_set(X509_ALGOR *alg, const EVP_MD *md)
346 if (md->flags & EVP_MD_FLAG_DIGALGID_ABSENT)
347 param_type = V_ASN1_UNDEF;
349 param_type = V_ASN1_NULL;
351 X509_ALGOR_set0(alg, OBJ_nid2obj(EVP_MD_type(md)), param_type, NULL);
355 /* Create a digest BIO from an X509_ALGOR structure */
357 BIO *cms_DigestAlgorithm_init_bio(X509_ALGOR *digestAlgorithm)
360 ASN1_OBJECT *digestoid;
361 const EVP_MD *digest;
362 X509_ALGOR_get0(&digestoid, NULL, NULL, digestAlgorithm);
363 digest = EVP_get_digestbyobj(digestoid);
366 CMSerr(CMS_F_CMS_DIGESTALGORITHM_INIT_BIO,
367 CMS_R_UNKNOWN_DIGEST_ALGORIHM);
370 mdbio = BIO_new(BIO_f_md());
371 if (!mdbio || !BIO_set_md(mdbio, digest))
373 CMSerr(CMS_F_CMS_DIGESTALGORITHM_INIT_BIO,
374 CMS_R_MD_BIO_INIT_ERROR);
384 /* Locate a message digest content from a BIO chain based on SignerInfo */
386 int cms_DigestAlgorithm_find_ctx(EVP_MD_CTX *mctx, BIO *chain,
391 X509_ALGOR_get0(&mdoid, NULL, NULL, mdalg);
392 nid = OBJ_obj2nid(mdoid);
393 /* Look for digest type to match signature */
397 chain = BIO_find_type(chain, BIO_TYPE_MD);
400 CMSerr(CMS_F_CMS_DIGESTALGORITHM_FIND_CTX,
401 CMS_R_NO_MATCHING_DIGEST);
404 BIO_get_md_ctx(chain, &mtmp);
405 if (EVP_MD_CTX_type(mtmp) == nid)
407 EVP_MD_CTX_copy_ex(mctx, mtmp);
410 chain = BIO_next(chain);
414 STACK_OF(CMS_CertificateChoices) **cms_get0_certificate_choices(CMS_ContentInfo *cms)
416 switch (OBJ_obj2nid(cms->contentType))
419 case NID_pkcs7_signed:
420 return &cms->d.signedData->certificates;
422 case NID_pkcs7_enveloped:
423 return &cms->d.envelopedData->originatorInfo->certificates;
426 CMSerr(CMS_F_CMS_GET0_CERTIFICATE_CHOICES,
427 CMS_R_UNSUPPORTED_CONTENT_TYPE);
433 CMS_CertificateChoices *CMS_add0_CertificateChoices(CMS_ContentInfo *cms)
435 STACK_OF(CMS_CertificateChoices) **pcerts;
436 CMS_CertificateChoices *cch;
437 pcerts = cms_get0_certificate_choices(cms);
441 *pcerts = sk_CMS_CertificateChoices_new_null();
444 cch = M_ASN1_new_of(CMS_CertificateChoices);
447 if (!sk_CMS_CertificateChoices_push(*pcerts, cch))
449 M_ASN1_free_of(cch, CMS_CertificateChoices);
455 int CMS_add0_cert(CMS_ContentInfo *cms, X509 *cert)
457 CMS_CertificateChoices *cch;
458 STACK_OF(CMS_CertificateChoices) **pcerts;
460 pcerts = cms_get0_certificate_choices(cms);
465 for (i = 0; i < sk_CMS_CertificateChoices_num(*pcerts); i++)
467 cch = sk_CMS_CertificateChoices_value(*pcerts, i);
468 if (cch->type == CMS_CERTCHOICE_CERT)
470 if (!X509_cmp(cch->d.certificate, cert))
475 cch = CMS_add0_CertificateChoices(cms);
478 cch->type = CMS_CERTCHOICE_CERT;
479 cch->d.certificate = cert;
483 int CMS_add1_cert(CMS_ContentInfo *cms, X509 *cert)
486 r = CMS_add0_cert(cms, cert);
488 CRYPTO_add(&cert->references, 1, CRYPTO_LOCK_X509);
492 STACK_OF(CMS_RevocationInfoChoice) **cms_get0_revocation_choices(CMS_ContentInfo *cms)
494 switch (OBJ_obj2nid(cms->contentType))
497 case NID_pkcs7_signed:
498 return &cms->d.signedData->crls;
500 case NID_pkcs7_enveloped:
501 return &cms->d.envelopedData->originatorInfo->crls;
504 CMSerr(CMS_F_CMS_GET0_REVOCATION_CHOICES,
505 CMS_R_UNSUPPORTED_CONTENT_TYPE);
511 CMS_RevocationInfoChoice *CMS_add0_RevocationInfoChoice(CMS_ContentInfo *cms)
513 STACK_OF(CMS_RevocationInfoChoice) **pcrls;
514 CMS_RevocationInfoChoice *rch;
515 pcrls = cms_get0_revocation_choices(cms);
519 *pcrls = sk_CMS_RevocationInfoChoice_new_null();
522 rch = M_ASN1_new_of(CMS_RevocationInfoChoice);
525 if (!sk_CMS_RevocationInfoChoice_push(*pcrls, rch))
527 M_ASN1_free_of(rch, CMS_RevocationInfoChoice);
533 int CMS_add0_crl(CMS_ContentInfo *cms, X509_CRL *crl)
535 CMS_RevocationInfoChoice *rch;
536 rch = CMS_add0_RevocationInfoChoice(cms);
539 rch->type = CMS_REVCHOICE_CRL;
544 STACK_OF(X509) *CMS_get1_certs(CMS_ContentInfo *cms)
546 STACK_OF(X509) *certs = NULL;
547 CMS_CertificateChoices *cch;
548 STACK_OF(CMS_CertificateChoices) **pcerts;
550 pcerts = cms_get0_certificate_choices(cms);
553 for (i = 0; i < sk_CMS_CertificateChoices_num(*pcerts); i++)
555 cch = sk_CMS_CertificateChoices_value(*pcerts, i);
560 certs = sk_X509_new_null();
564 if (!sk_X509_push(certs, cch->d.certificate))
566 sk_X509_pop_free(certs, X509_free);
569 CRYPTO_add(&cch->d.certificate->references,
570 1, CRYPTO_LOCK_X509);
577 STACK_OF(X509_CRL) *CMS_get1_crls(CMS_ContentInfo *cms)
579 STACK_OF(X509_CRL) *crls = NULL;
580 STACK_OF(CMS_RevocationInfoChoice) **pcrls;
581 CMS_RevocationInfoChoice *rch;
583 pcrls = cms_get0_revocation_choices(cms);
586 for (i = 0; i < sk_CMS_RevocationInfoChoice_num(*pcrls); i++)
588 rch = sk_CMS_RevocationInfoChoice_value(*pcrls, i);
593 crls = sk_X509_CRL_new_null();
597 if (!sk_X509_CRL_push(crls, rch->d.crl))
599 sk_X509_CRL_pop_free(crls, X509_CRL_free);
602 CRYPTO_add(&rch->d.crl->references,
603 1, CRYPTO_LOCK_X509_CRL);