1*7bfedc82Sjoshua /* $OpenBSD: cms_io.c,v 1.21 2024/03/30 01:53:05 joshua Exp $ */
2b8b016bfSjsing /*
3f29d8588Sjsing * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
4f29d8588Sjsing * project.
5f29d8588Sjsing */
6f29d8588Sjsing /* ====================================================================
7f29d8588Sjsing * Copyright (c) 2008 The OpenSSL Project. All rights reserved.
8b8b016bfSjsing *
9f29d8588Sjsing * Redistribution and use in source and binary forms, with or without
10f29d8588Sjsing * modification, are permitted provided that the following conditions
11f29d8588Sjsing * are met:
12f29d8588Sjsing *
13f29d8588Sjsing * 1. Redistributions of source code must retain the above copyright
14f29d8588Sjsing * notice, this list of conditions and the following disclaimer.
15f29d8588Sjsing *
16f29d8588Sjsing * 2. Redistributions in binary form must reproduce the above copyright
17f29d8588Sjsing * notice, this list of conditions and the following disclaimer in
18f29d8588Sjsing * the documentation and/or other materials provided with the
19f29d8588Sjsing * distribution.
20f29d8588Sjsing *
21f29d8588Sjsing * 3. All advertising materials mentioning features or use of this
22f29d8588Sjsing * software must display the following acknowledgment:
23f29d8588Sjsing * "This product includes software developed by the OpenSSL Project
24f29d8588Sjsing * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25f29d8588Sjsing *
26f29d8588Sjsing * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27f29d8588Sjsing * endorse or promote products derived from this software without
28f29d8588Sjsing * prior written permission. For written permission, please contact
29f29d8588Sjsing * licensing@OpenSSL.org.
30f29d8588Sjsing *
31f29d8588Sjsing * 5. Products derived from this software may not be called "OpenSSL"
32f29d8588Sjsing * nor may "OpenSSL" appear in their names without prior written
33f29d8588Sjsing * permission of the OpenSSL Project.
34f29d8588Sjsing *
35f29d8588Sjsing * 6. Redistributions of any form whatsoever must retain the following
36f29d8588Sjsing * acknowledgment:
37f29d8588Sjsing * "This product includes software developed by the OpenSSL Project
38f29d8588Sjsing * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39f29d8588Sjsing *
40f29d8588Sjsing * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41f29d8588Sjsing * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42f29d8588Sjsing * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43f29d8588Sjsing * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
44f29d8588Sjsing * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45f29d8588Sjsing * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46f29d8588Sjsing * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47f29d8588Sjsing * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48f29d8588Sjsing * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49f29d8588Sjsing * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50f29d8588Sjsing * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51f29d8588Sjsing * OF THE POSSIBILITY OF SUCH DAMAGE.
52f29d8588Sjsing * ====================================================================
53b8b016bfSjsing */
54b8b016bfSjsing
55b8b016bfSjsing #include <openssl/asn1t.h>
56596644f2Stb #include <openssl/cms.h>
57b8b016bfSjsing #include <openssl/err.h>
58b8b016bfSjsing #include <openssl/pem.h>
59596644f2Stb #include <openssl/x509.h>
60596644f2Stb
612b5e1227Stb #include "asn1_local.h"
62c9675a23Stb #include "cms_local.h"
63b8b016bfSjsing
6472419cc7Sjsing int
CMS_stream(unsigned char *** boundary,CMS_ContentInfo * cms)6572419cc7Sjsing CMS_stream(unsigned char ***boundary, CMS_ContentInfo *cms)
66b8b016bfSjsing {
67b8b016bfSjsing ASN1_OCTET_STRING **pos;
6872419cc7Sjsing
698b8ebb2fStb if ((pos = CMS_get0_content(cms)) == NULL)
70b8b016bfSjsing return 0;
718b8ebb2fStb
72b8b016bfSjsing if (*pos == NULL)
73b8b016bfSjsing *pos = ASN1_OCTET_STRING_new();
748b8ebb2fStb if (*pos == NULL) {
758b8ebb2fStb CMSerror(ERR_R_MALLOC_FAILURE);
768b8ebb2fStb return 0;
778b8ebb2fStb }
788b8ebb2fStb
79b8b016bfSjsing (*pos)->flags |= ASN1_STRING_FLAG_NDEF;
80b8b016bfSjsing (*pos)->flags &= ~ASN1_STRING_FLAG_CONT;
81b8b016bfSjsing *boundary = &(*pos)->data;
828b8ebb2fStb
83b8b016bfSjsing return 1;
84b8b016bfSjsing }
85ead8f799Sbeck LCRYPTO_ALIAS(CMS_stream);
86b8b016bfSjsing
8772419cc7Sjsing CMS_ContentInfo *
d2i_CMS_bio(BIO * bp,CMS_ContentInfo ** cms)8872419cc7Sjsing d2i_CMS_bio(BIO *bp, CMS_ContentInfo **cms)
89b8b016bfSjsing {
900a940e1fSjsing return ASN1_item_d2i_bio(&CMS_ContentInfo_it, bp, cms);
91b8b016bfSjsing }
92ead8f799Sbeck LCRYPTO_ALIAS(d2i_CMS_bio);
93b8b016bfSjsing
9472419cc7Sjsing int
i2d_CMS_bio(BIO * bp,CMS_ContentInfo * cms)9572419cc7Sjsing i2d_CMS_bio(BIO *bp, CMS_ContentInfo *cms)
96b8b016bfSjsing {
970a940e1fSjsing return ASN1_item_i2d_bio(&CMS_ContentInfo_it, bp, cms);
98b8b016bfSjsing }
99ead8f799Sbeck LCRYPTO_ALIAS(i2d_CMS_bio);
100b8b016bfSjsing
1010a940e1fSjsing
1020a940e1fSjsing CMS_ContentInfo *
PEM_read_bio_CMS(BIO * bp,CMS_ContentInfo ** x,pem_password_cb * cb,void * u)1030a940e1fSjsing PEM_read_bio_CMS(BIO *bp, CMS_ContentInfo **x, pem_password_cb *cb, void *u)
1040a940e1fSjsing {
105ef20b249Stb return PEM_ASN1_read_bio((d2i_of_void *)d2i_CMS_ContentInfo,
106ef20b249Stb PEM_STRING_CMS, bp, (void **)x, cb, u);
1070a940e1fSjsing }
108*7bfedc82Sjoshua LCRYPTO_ALIAS(PEM_read_bio_CMS);
1090a940e1fSjsing
1100a940e1fSjsing CMS_ContentInfo *
PEM_read_CMS(FILE * fp,CMS_ContentInfo ** x,pem_password_cb * cb,void * u)1110a940e1fSjsing PEM_read_CMS(FILE *fp, CMS_ContentInfo **x, pem_password_cb *cb, void *u)
1120a940e1fSjsing {
113ef20b249Stb return PEM_ASN1_read((d2i_of_void *)d2i_CMS_ContentInfo,
114ef20b249Stb PEM_STRING_CMS, fp, (void **)x, cb, u);
1150a940e1fSjsing }
116*7bfedc82Sjoshua LCRYPTO_ALIAS(PEM_read_CMS);
1170a940e1fSjsing
1180a940e1fSjsing int
PEM_write_bio_CMS(BIO * bp,const CMS_ContentInfo * x)1190a940e1fSjsing PEM_write_bio_CMS(BIO *bp, const CMS_ContentInfo *x)
1200a940e1fSjsing {
121ef20b249Stb return PEM_ASN1_write_bio((i2d_of_void *)i2d_CMS_ContentInfo,
122ef20b249Stb PEM_STRING_CMS, bp, (void *)x, NULL, NULL, 0, NULL, NULL);
1230a940e1fSjsing }
124*7bfedc82Sjoshua LCRYPTO_ALIAS(PEM_write_bio_CMS);
1250a940e1fSjsing
1260a940e1fSjsing int
PEM_write_CMS(FILE * fp,const CMS_ContentInfo * x)1270a940e1fSjsing PEM_write_CMS(FILE *fp, const CMS_ContentInfo *x)
1280a940e1fSjsing {
129ef20b249Stb return PEM_ASN1_write((i2d_of_void *)i2d_CMS_ContentInfo,
130ef20b249Stb PEM_STRING_CMS, fp, (void *)x, NULL, NULL, 0, NULL, NULL);
1310a940e1fSjsing }
132*7bfedc82Sjoshua LCRYPTO_ALIAS(PEM_write_CMS);
133b8b016bfSjsing
13472419cc7Sjsing BIO *
BIO_new_CMS(BIO * out,CMS_ContentInfo * cms)13572419cc7Sjsing BIO_new_CMS(BIO *out, CMS_ContentInfo *cms)
136b8b016bfSjsing {
137847c3150Stb return BIO_new_NDEF(out, (ASN1_VALUE *)cms, &CMS_ContentInfo_it);
138b8b016bfSjsing }
139ead8f799Sbeck LCRYPTO_ALIAS(BIO_new_CMS);
140b8b016bfSjsing
141b8b016bfSjsing /* CMS wrappers round generalised stream and MIME routines */
142b8b016bfSjsing
143e636c802Stb int
i2d_CMS_bio_stream(BIO * out,CMS_ContentInfo * cms,BIO * in,int flags)144e636c802Stb i2d_CMS_bio_stream(BIO *out, CMS_ContentInfo *cms, BIO *in, int flags)
145b8b016bfSjsing {
146b8b016bfSjsing return i2d_ASN1_bio_stream(out, (ASN1_VALUE *)cms, in, flags,
1470a940e1fSjsing &CMS_ContentInfo_it);
148b8b016bfSjsing }
149ead8f799Sbeck LCRYPTO_ALIAS(i2d_CMS_bio_stream);
150b8b016bfSjsing
15172419cc7Sjsing int
PEM_write_bio_CMS_stream(BIO * out,CMS_ContentInfo * cms,BIO * in,int flags)15272419cc7Sjsing PEM_write_bio_CMS_stream(BIO *out, CMS_ContentInfo *cms, BIO *in, int flags)
153b8b016bfSjsing {
154b8b016bfSjsing return PEM_write_bio_ASN1_stream(out, (ASN1_VALUE *)cms, in, flags,
1550a940e1fSjsing "CMS", &CMS_ContentInfo_it);
156b8b016bfSjsing }
157ead8f799Sbeck LCRYPTO_ALIAS(PEM_write_bio_CMS_stream);
158b8b016bfSjsing
15972419cc7Sjsing int
SMIME_write_CMS(BIO * bio,CMS_ContentInfo * cms,BIO * data,int flags)16072419cc7Sjsing SMIME_write_CMS(BIO *bio, CMS_ContentInfo *cms, BIO *data, int flags)
161b8b016bfSjsing {
16287b95781Stb STACK_OF(X509_ALGOR) *mdalgs = NULL;
163b8b016bfSjsing int ctype_nid = OBJ_obj2nid(cms->contentType);
164b8b016bfSjsing int econt_nid = OBJ_obj2nid(CMS_get0_eContentType(cms));
16572419cc7Sjsing
166b8b016bfSjsing if (ctype_nid == NID_pkcs7_signed)
167b8b016bfSjsing mdalgs = cms->d.signedData->digestAlgorithms;
168b8b016bfSjsing
16972419cc7Sjsing return SMIME_write_ASN1(bio, (ASN1_VALUE *)cms, data, flags, ctype_nid,
1700a940e1fSjsing econt_nid, mdalgs, &CMS_ContentInfo_it);
171b8b016bfSjsing }
172ead8f799Sbeck LCRYPTO_ALIAS(SMIME_write_CMS);
173b8b016bfSjsing
17472419cc7Sjsing CMS_ContentInfo *
SMIME_read_CMS(BIO * bio,BIO ** bcont)17572419cc7Sjsing SMIME_read_CMS(BIO *bio, BIO **bcont)
176b8b016bfSjsing {
177b8b016bfSjsing return (CMS_ContentInfo *)SMIME_read_ASN1(bio, bcont,
1780a940e1fSjsing &CMS_ContentInfo_it);
179b8b016bfSjsing }
180ead8f799Sbeck LCRYPTO_ALIAS(SMIME_read_CMS);
181