/src/openssl31/crypto/cms/cms_io.c
Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | /*  | 
2  |  |  * Copyright 2008-2022 The OpenSSL Project Authors. All Rights Reserved.  | 
3  |  |  *  | 
4  |  |  * Licensed under the Apache License 2.0 (the "License").  You may not use  | 
5  |  |  * this file except in compliance with the License.  You can obtain a copy  | 
6  |  |  * in the file LICENSE in the source distribution or at  | 
7  |  |  * https://www.openssl.org/source/license.html  | 
8  |  |  */  | 
9  |  |  | 
10  |  | #include <openssl/asn1t.h>  | 
11  |  | #include <openssl/x509.h>  | 
12  |  | #include <openssl/err.h>  | 
13  |  | #include <openssl/pem.h>  | 
14  |  | #include <openssl/cms.h>  | 
15  |  | #include "cms_local.h"  | 
16  |  |  | 
17  |  | /* unfortunately cannot constify BIO_new_NDEF() due to this and PKCS7_stream() */  | 
18  |  | int CMS_stream(unsigned char ***boundary, CMS_ContentInfo *cms)  | 
19  | 0  | { | 
20  | 0  |     ASN1_OCTET_STRING **pos;  | 
21  | 0  |     pos = CMS_get0_content(cms);  | 
22  | 0  |     if (pos == NULL)  | 
23  | 0  |         return 0;  | 
24  | 0  |     if (*pos == NULL)  | 
25  | 0  |         *pos = ASN1_OCTET_STRING_new();  | 
26  | 0  |     if (*pos != NULL) { | 
27  | 0  |         (*pos)->flags |= ASN1_STRING_FLAG_NDEF;  | 
28  | 0  |         (*pos)->flags &= ~ASN1_STRING_FLAG_CONT;  | 
29  | 0  |         *boundary = &(*pos)->data;  | 
30  | 0  |         return 1;  | 
31  | 0  |     }  | 
32  | 0  |     ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);  | 
33  | 0  |     return 0;  | 
34  | 0  | }  | 
35  |  |  | 
36  |  | CMS_ContentInfo *d2i_CMS_bio(BIO *bp, CMS_ContentInfo **cms)  | 
37  | 37.2k  | { | 
38  | 37.2k  |     CMS_ContentInfo *ci;  | 
39  | 37.2k  |     const CMS_CTX *ctx = ossl_cms_get0_cmsctx(cms == NULL ? NULL : *cms);  | 
40  |  |  | 
41  | 37.2k  |     ci = ASN1_item_d2i_bio_ex(ASN1_ITEM_rptr(CMS_ContentInfo), bp, cms,  | 
42  | 37.2k  |                               ossl_cms_ctx_get0_libctx(ctx),  | 
43  | 37.2k  |                               ossl_cms_ctx_get0_propq(ctx));  | 
44  | 37.2k  |     if (ci != NULL) { | 
45  | 4.33k  |         ERR_set_mark();  | 
46  | 4.33k  |         ossl_cms_resolve_libctx(ci);  | 
47  | 4.33k  |         ERR_pop_to_mark();  | 
48  | 4.33k  |     }  | 
49  | 37.2k  |     return ci;  | 
50  | 37.2k  | }  | 
51  |  |  | 
52  |  | int i2d_CMS_bio(BIO *bp, CMS_ContentInfo *cms)  | 
53  | 4.33k  | { | 
54  | 4.33k  |     return ASN1_item_i2d_bio(ASN1_ITEM_rptr(CMS_ContentInfo), bp, cms);  | 
55  | 4.33k  | }  | 
56  |  |  | 
57  |  | IMPLEMENT_PEM_rw(CMS, CMS_ContentInfo, PEM_STRING_CMS, CMS_ContentInfo)  | 
58  |  |  | 
59  |  | BIO *BIO_new_CMS(BIO *out, CMS_ContentInfo *cms)  | 
60  | 0  | { | 
61  | 0  |     return BIO_new_NDEF(out, (ASN1_VALUE *)cms,  | 
62  | 0  |                         ASN1_ITEM_rptr(CMS_ContentInfo));  | 
63  | 0  | }  | 
64  |  |  | 
65  |  | /* CMS wrappers round generalised stream and MIME routines */  | 
66  |  |  | 
67  |  | int i2d_CMS_bio_stream(BIO *out, CMS_ContentInfo *cms, BIO *in, int flags)  | 
68  | 0  | { | 
69  | 0  |     return i2d_ASN1_bio_stream(out, (ASN1_VALUE *)cms, in, flags,  | 
70  | 0  |                                ASN1_ITEM_rptr(CMS_ContentInfo));  | 
71  | 0  | }  | 
72  |  |  | 
73  |  | int PEM_write_bio_CMS_stream(BIO *out, CMS_ContentInfo *cms, BIO *in,  | 
74  |  |                              int flags)  | 
75  | 0  | { | 
76  | 0  |     return PEM_write_bio_ASN1_stream(out, (ASN1_VALUE *)cms, in, flags,  | 
77  | 0  |                                      "CMS", ASN1_ITEM_rptr(CMS_ContentInfo));  | 
78  | 0  | }  | 
79  |  |  | 
80  |  | int SMIME_write_CMS(BIO *bio, CMS_ContentInfo *cms, BIO *data, int flags)  | 
81  | 0  | { | 
82  | 0  |     STACK_OF(X509_ALGOR) *mdalgs;  | 
83  | 0  |     int ctype_nid = OBJ_obj2nid(cms->contentType);  | 
84  | 0  |     int econt_nid = OBJ_obj2nid(CMS_get0_eContentType(cms));  | 
85  | 0  |     const CMS_CTX *ctx = ossl_cms_get0_cmsctx(cms);  | 
86  |  | 
  | 
87  | 0  |     if (ctype_nid == NID_pkcs7_signed)  | 
88  | 0  |         mdalgs = cms->d.signedData->digestAlgorithms;  | 
89  | 0  |     else  | 
90  | 0  |         mdalgs = NULL;  | 
91  |  | 
  | 
92  | 0  |     return SMIME_write_ASN1_ex(bio, (ASN1_VALUE *)cms, data, flags, ctype_nid,  | 
93  | 0  |                                econt_nid, mdalgs,  | 
94  | 0  |                                ASN1_ITEM_rptr(CMS_ContentInfo),  | 
95  | 0  |                                ossl_cms_ctx_get0_libctx(ctx),  | 
96  | 0  |                                ossl_cms_ctx_get0_propq(ctx));  | 
97  | 0  | }  | 
98  |  |  | 
99  |  | CMS_ContentInfo *SMIME_read_CMS_ex(BIO *bio, int flags, BIO **bcont,  | 
100  |  |                                    CMS_ContentInfo **cms)  | 
101  | 0  | { | 
102  | 0  |     CMS_ContentInfo *ci;  | 
103  | 0  |     const CMS_CTX *ctx = ossl_cms_get0_cmsctx(cms == NULL ? NULL : *cms);  | 
104  |  | 
  | 
105  | 0  |     ci = (CMS_ContentInfo *)SMIME_read_ASN1_ex(bio, flags, bcont,  | 
106  | 0  |                                                ASN1_ITEM_rptr(CMS_ContentInfo),  | 
107  | 0  |                                                (ASN1_VALUE **)cms,  | 
108  | 0  |                                                ossl_cms_ctx_get0_libctx(ctx),  | 
109  | 0  |                                                ossl_cms_ctx_get0_propq(ctx));  | 
110  | 0  |     if (ci != NULL) { | 
111  | 0  |         ERR_set_mark();  | 
112  | 0  |         ossl_cms_resolve_libctx(ci);  | 
113  | 0  |         ERR_pop_to_mark();  | 
114  | 0  |     }  | 
115  | 0  |     return ci;  | 
116  | 0  | }  | 
117  |  |  | 
118  |  | CMS_ContentInfo *SMIME_read_CMS(BIO *bio, BIO **bcont)  | 
119  | 0  | { | 
120  | 0  |     return SMIME_read_CMS_ex(bio, 0, bcont, NULL);  | 
121  | 0  | }  |