1 /* crypto/cms/cms_lib.c */
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>
64 IMPLEMENT_ASN1_FUNCTIONS(CMS_ContentInfo)
65 IMPLEMENT_ASN1_PRINT_FUNCTION(CMS_ContentInfo)
67 const ASN1_OBJECT *CMS_get0_type(CMS_ContentInfo *cms)
69 return cms->contentType;
72 CMS_ContentInfo *cms_Data_create(void)
75 cms = CMS_ContentInfo_new();
77 cms->contentType = OBJ_nid2obj(NID_pkcs7_data);
79 CMS_set_detached(cms, 0);
84 BIO *cms_content_bio(CMS_ContentInfo *cms)
86 ASN1_OCTET_STRING **pos = CMS_get0_content(cms);
89 /* If content detached data goes nowhere: create NULL BIO */
91 return BIO_new(BIO_s_null());
93 * If content not detached and created return memory BIO
95 if (!*pos || ((*pos)->flags == ASN1_STRING_FLAG_CONT))
96 return BIO_new(BIO_s_mem());
97 /* Else content was read in: return read only BIO for it */
98 return BIO_new_mem_buf((*pos)->data, (*pos)->length);
101 BIO *CMS_dataInit(CMS_ContentInfo *cms, BIO *icont)
107 cont = cms_content_bio(cms);
109 CMSerr(CMS_F_CMS_DATAINIT, CMS_R_NO_CONTENT);
112 switch (OBJ_obj2nid(cms->contentType)) {
117 case NID_pkcs7_signed:
118 cmsbio = cms_SignedData_init_bio(cms);
121 case NID_pkcs7_digest:
122 cmsbio = cms_DigestedData_init_bio(cms);
125 case NID_id_smime_ct_compressedData:
126 cmsbio = cms_CompressedData_init_bio(cms);
130 case NID_pkcs7_encrypted:
131 cmsbio = cms_EncryptedData_init_bio(cms);
134 case NID_pkcs7_enveloped:
135 cmsbio = cms_EnvelopedData_init_bio(cms);
139 CMSerr(CMS_F_CMS_DATAINIT, CMS_R_UNSUPPORTED_TYPE);
144 return BIO_push(cmsbio, cont);
152 int CMS_dataFinal(CMS_ContentInfo *cms, BIO *cmsbio)
154 ASN1_OCTET_STRING **pos = CMS_get0_content(cms);
157 /* If ebmedded content find memory BIO and set content */
158 if (*pos && ((*pos)->flags & ASN1_STRING_FLAG_CONT)) {
162 mbio = BIO_find_type(cmsbio, BIO_TYPE_MEM);
164 CMSerr(CMS_F_CMS_DATAFINAL, CMS_R_CONTENT_NOT_FOUND);
167 contlen = BIO_get_mem_data(mbio, &cont);
168 /* Set bio as read only so its content can't be clobbered */
169 BIO_set_flags(mbio, BIO_FLAGS_MEM_RDONLY);
170 BIO_set_mem_eof_return(mbio, 0);
171 ASN1_STRING_set0(*pos, cont, contlen);
172 (*pos)->flags &= ~ASN1_STRING_FLAG_CONT;
175 switch (OBJ_obj2nid(cms->contentType)) {
178 case NID_pkcs7_enveloped:
179 case NID_pkcs7_encrypted:
180 case NID_id_smime_ct_compressedData:
184 case NID_pkcs7_signed:
185 return cms_SignedData_final(cms, cmsbio);
187 case NID_pkcs7_digest:
188 return cms_DigestedData_do_final(cms, cmsbio, 0);
191 CMSerr(CMS_F_CMS_DATAFINAL, CMS_R_UNSUPPORTED_TYPE);
197 * Return an OCTET STRING pointer to content. This allows it to be accessed
201 ASN1_OCTET_STRING **CMS_get0_content(CMS_ContentInfo *cms)
203 switch (OBJ_obj2nid(cms->contentType)) {
208 case NID_pkcs7_signed:
209 return &cms->d.signedData->encapContentInfo->eContent;
211 case NID_pkcs7_enveloped:
212 return &cms->d.envelopedData->encryptedContentInfo->encryptedContent;
214 case NID_pkcs7_digest:
215 return &cms->d.digestedData->encapContentInfo->eContent;
217 case NID_pkcs7_encrypted:
218 return &cms->d.encryptedData->encryptedContentInfo->encryptedContent;
220 case NID_id_smime_ct_authData:
221 return &cms->d.authenticatedData->encapContentInfo->eContent;
223 case NID_id_smime_ct_compressedData:
224 return &cms->d.compressedData->encapContentInfo->eContent;
227 if (cms->d.other->type == V_ASN1_OCTET_STRING)
228 return &cms->d.other->value.octet_string;
229 CMSerr(CMS_F_CMS_GET0_CONTENT, CMS_R_UNSUPPORTED_CONTENT_TYPE);
236 * Return an ASN1_OBJECT pointer to content type. This allows it to be
237 * accessed or set later.
240 static ASN1_OBJECT **cms_get0_econtent_type(CMS_ContentInfo *cms)
242 switch (OBJ_obj2nid(cms->contentType)) {
244 case NID_pkcs7_signed:
245 return &cms->d.signedData->encapContentInfo->eContentType;
247 case NID_pkcs7_enveloped:
248 return &cms->d.envelopedData->encryptedContentInfo->contentType;
250 case NID_pkcs7_digest:
251 return &cms->d.digestedData->encapContentInfo->eContentType;
253 case NID_pkcs7_encrypted:
254 return &cms->d.encryptedData->encryptedContentInfo->contentType;
256 case NID_id_smime_ct_authData:
257 return &cms->d.authenticatedData->encapContentInfo->eContentType;
259 case NID_id_smime_ct_compressedData:
260 return &cms->d.compressedData->encapContentInfo->eContentType;
263 CMSerr(CMS_F_CMS_GET0_ECONTENT_TYPE, CMS_R_UNSUPPORTED_CONTENT_TYPE);
269 const ASN1_OBJECT *CMS_get0_eContentType(CMS_ContentInfo *cms)
271 ASN1_OBJECT **petype;
272 petype = cms_get0_econtent_type(cms);
278 int CMS_set1_eContentType(CMS_ContentInfo *cms, const ASN1_OBJECT *oid)
280 ASN1_OBJECT **petype, *etype;
281 petype = cms_get0_econtent_type(cms);
286 etype = OBJ_dup(oid);
289 ASN1_OBJECT_free(*petype);
294 int CMS_is_detached(CMS_ContentInfo *cms)
296 ASN1_OCTET_STRING **pos;
297 pos = CMS_get0_content(cms);
305 int CMS_set_detached(CMS_ContentInfo *cms, int detached)
307 ASN1_OCTET_STRING **pos;
308 pos = CMS_get0_content(cms);
312 ASN1_OCTET_STRING_free(*pos);
317 *pos = ASN1_OCTET_STRING_new();
320 * NB: special flag to show content is created and not read in.
322 (*pos)->flags |= ASN1_STRING_FLAG_CONT;
325 CMSerr(CMS_F_CMS_SET_DETACHED, ERR_R_MALLOC_FAILURE);
329 /* Create a digest BIO from an X509_ALGOR structure */
331 BIO *cms_DigestAlgorithm_init_bio(X509_ALGOR *digestAlgorithm)
334 ASN1_OBJECT *digestoid;
335 const EVP_MD *digest;
336 X509_ALGOR_get0(&digestoid, NULL, NULL, digestAlgorithm);
337 digest = EVP_get_digestbyobj(digestoid);
339 CMSerr(CMS_F_CMS_DIGESTALGORITHM_INIT_BIO,
340 CMS_R_UNKNOWN_DIGEST_ALGORIHM);
343 mdbio = BIO_new(BIO_f_md());
344 if (mdbio == NULL || !BIO_set_md(mdbio, digest)) {
345 CMSerr(CMS_F_CMS_DIGESTALGORITHM_INIT_BIO, CMS_R_MD_BIO_INIT_ERROR);
354 /* Locate a message digest content from a BIO chain based on SignerInfo */
356 int cms_DigestAlgorithm_find_ctx(EVP_MD_CTX *mctx, BIO *chain,
361 X509_ALGOR_get0(&mdoid, NULL, NULL, mdalg);
362 nid = OBJ_obj2nid(mdoid);
363 /* Look for digest type to match signature */
366 chain = BIO_find_type(chain, BIO_TYPE_MD);
368 CMSerr(CMS_F_CMS_DIGESTALGORITHM_FIND_CTX,
369 CMS_R_NO_MATCHING_DIGEST);
372 BIO_get_md_ctx(chain, &mtmp);
373 if (EVP_MD_CTX_type(mtmp) == nid
375 * Workaround for broken implementations that use signature
376 * algorithm OID instead of digest.
378 || EVP_MD_pkey_type(EVP_MD_CTX_md(mtmp)) == nid)
379 return EVP_MD_CTX_copy_ex(mctx, mtmp);
380 chain = BIO_next(chain);
384 static STACK_OF(CMS_CertificateChoices)
385 **cms_get0_certificate_choices(CMS_ContentInfo *cms)
387 switch (OBJ_obj2nid(cms->contentType)) {
389 case NID_pkcs7_signed:
390 return &cms->d.signedData->certificates;
392 case NID_pkcs7_enveloped:
393 return &cms->d.envelopedData->originatorInfo->certificates;
396 CMSerr(CMS_F_CMS_GET0_CERTIFICATE_CHOICES,
397 CMS_R_UNSUPPORTED_CONTENT_TYPE);
403 CMS_CertificateChoices *CMS_add0_CertificateChoices(CMS_ContentInfo *cms)
405 STACK_OF(CMS_CertificateChoices) **pcerts;
406 CMS_CertificateChoices *cch;
407 pcerts = cms_get0_certificate_choices(cms);
411 *pcerts = sk_CMS_CertificateChoices_new_null();
414 cch = M_ASN1_new_of(CMS_CertificateChoices);
417 if (!sk_CMS_CertificateChoices_push(*pcerts, cch)) {
418 M_ASN1_free_of(cch, CMS_CertificateChoices);
424 int CMS_add0_cert(CMS_ContentInfo *cms, X509 *cert)
426 CMS_CertificateChoices *cch;
427 STACK_OF(CMS_CertificateChoices) **pcerts;
429 pcerts = cms_get0_certificate_choices(cms);
432 for (i = 0; i < sk_CMS_CertificateChoices_num(*pcerts); i++) {
433 cch = sk_CMS_CertificateChoices_value(*pcerts, i);
434 if (cch->type == CMS_CERTCHOICE_CERT) {
435 if (!X509_cmp(cch->d.certificate, cert)) {
436 CMSerr(CMS_F_CMS_ADD0_CERT,
437 CMS_R_CERTIFICATE_ALREADY_PRESENT);
442 cch = CMS_add0_CertificateChoices(cms);
445 cch->type = CMS_CERTCHOICE_CERT;
446 cch->d.certificate = cert;
450 int CMS_add1_cert(CMS_ContentInfo *cms, X509 *cert)
453 r = CMS_add0_cert(cms, cert);
459 static STACK_OF(CMS_RevocationInfoChoice)
460 **cms_get0_revocation_choices(CMS_ContentInfo *cms)
462 switch (OBJ_obj2nid(cms->contentType)) {
464 case NID_pkcs7_signed:
465 return &cms->d.signedData->crls;
467 case NID_pkcs7_enveloped:
468 return &cms->d.envelopedData->originatorInfo->crls;
471 CMSerr(CMS_F_CMS_GET0_REVOCATION_CHOICES,
472 CMS_R_UNSUPPORTED_CONTENT_TYPE);
478 CMS_RevocationInfoChoice *CMS_add0_RevocationInfoChoice(CMS_ContentInfo *cms)
480 STACK_OF(CMS_RevocationInfoChoice) **pcrls;
481 CMS_RevocationInfoChoice *rch;
482 pcrls = cms_get0_revocation_choices(cms);
486 *pcrls = sk_CMS_RevocationInfoChoice_new_null();
489 rch = M_ASN1_new_of(CMS_RevocationInfoChoice);
492 if (!sk_CMS_RevocationInfoChoice_push(*pcrls, rch)) {
493 M_ASN1_free_of(rch, CMS_RevocationInfoChoice);
499 int CMS_add0_crl(CMS_ContentInfo *cms, X509_CRL *crl)
501 CMS_RevocationInfoChoice *rch;
502 rch = CMS_add0_RevocationInfoChoice(cms);
505 rch->type = CMS_REVCHOICE_CRL;
510 int CMS_add1_crl(CMS_ContentInfo *cms, X509_CRL *crl)
513 r = CMS_add0_crl(cms, crl);
515 X509_CRL_up_ref(crl);
519 STACK_OF(X509) *CMS_get1_certs(CMS_ContentInfo *cms)
521 STACK_OF(X509) *certs = NULL;
522 CMS_CertificateChoices *cch;
523 STACK_OF(CMS_CertificateChoices) **pcerts;
525 pcerts = cms_get0_certificate_choices(cms);
528 for (i = 0; i < sk_CMS_CertificateChoices_num(*pcerts); i++) {
529 cch = sk_CMS_CertificateChoices_value(*pcerts, i);
530 if (cch->type == 0) {
532 certs = sk_X509_new_null();
536 if (!sk_X509_push(certs, cch->d.certificate)) {
537 sk_X509_pop_free(certs, X509_free);
540 X509_up_ref(cch->d.certificate);
547 STACK_OF(X509_CRL) *CMS_get1_crls(CMS_ContentInfo *cms)
549 STACK_OF(X509_CRL) *crls = NULL;
550 STACK_OF(CMS_RevocationInfoChoice) **pcrls;
551 CMS_RevocationInfoChoice *rch;
553 pcrls = cms_get0_revocation_choices(cms);
556 for (i = 0; i < sk_CMS_RevocationInfoChoice_num(*pcrls); i++) {
557 rch = sk_CMS_RevocationInfoChoice_value(*pcrls, i);
558 if (rch->type == 0) {
560 crls = sk_X509_CRL_new_null();
564 if (!sk_X509_CRL_push(crls, rch->d.crl)) {
565 sk_X509_CRL_pop_free(crls, X509_CRL_free);
568 X509_CRL_up_ref(rch->d.crl);
574 int cms_ias_cert_cmp(CMS_IssuerAndSerialNumber *ias, X509 *cert)
577 ret = X509_NAME_cmp(ias->issuer, X509_get_issuer_name(cert));
580 return ASN1_INTEGER_cmp(ias->serialNumber, X509_get_serialNumber(cert));
583 int cms_keyid_cert_cmp(ASN1_OCTET_STRING *keyid, X509 *cert)
585 const ASN1_OCTET_STRING *cert_keyid = X509_get0_subject_key_id(cert);
587 if (cert_keyid == NULL)
589 return ASN1_OCTET_STRING_cmp(keyid, cert_keyid);
592 int cms_set1_ias(CMS_IssuerAndSerialNumber **pias, X509 *cert)
594 CMS_IssuerAndSerialNumber *ias;
595 ias = M_ASN1_new_of(CMS_IssuerAndSerialNumber);
598 if (!X509_NAME_set(&ias->issuer, X509_get_issuer_name(cert)))
600 if (!ASN1_STRING_copy(ias->serialNumber, X509_get_serialNumber(cert)))
602 M_ASN1_free_of(*pias, CMS_IssuerAndSerialNumber);
606 M_ASN1_free_of(ias, CMS_IssuerAndSerialNumber);
607 CMSerr(CMS_F_CMS_SET1_IAS, ERR_R_MALLOC_FAILURE);
611 int cms_set1_keyid(ASN1_OCTET_STRING **pkeyid, X509 *cert)
613 ASN1_OCTET_STRING *keyid = NULL;
614 const ASN1_OCTET_STRING *cert_keyid;
615 cert_keyid = X509_get0_subject_key_id(cert);
616 if (cert_keyid == NULL) {
617 CMSerr(CMS_F_CMS_SET1_KEYID, CMS_R_CERTIFICATE_HAS_NO_KEYID);
620 keyid = ASN1_STRING_dup(cert_keyid);
622 CMSerr(CMS_F_CMS_SET1_KEYID, ERR_R_MALLOC_FAILURE);
625 ASN1_OCTET_STRING_free(*pkeyid);