fips module header inclusion fine-tunning
[openssl.git] / crypto / cms / cms_io.c
1 /*
2  * Copyright 2008-2021 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 {
20     ASN1_OCTET_STRING **pos;
21     pos = CMS_get0_content(cms);
22     if (pos == NULL)
23         return 0;
24     if (*pos == NULL)
25         *pos = ASN1_OCTET_STRING_new();
26     if (*pos != NULL) {
27         (*pos)->flags |= ASN1_STRING_FLAG_NDEF;
28         (*pos)->flags &= ~ASN1_STRING_FLAG_CONT;
29         *boundary = &(*pos)->data;
30         return 1;
31     }
32     ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
33     return 0;
34 }
35
36 CMS_ContentInfo *d2i_CMS_bio(BIO *bp, CMS_ContentInfo **cms)
37 {
38     CMS_ContentInfo *ci;
39     const CMS_CTX *ctx = ossl_cms_get0_cmsctx(cms == NULL ? NULL : *cms);
40
41     ci = ASN1_item_d2i_bio_ex(ASN1_ITEM_rptr(CMS_ContentInfo), bp, cms,
42                               ossl_cms_ctx_get0_libctx(ctx),
43                               ossl_cms_ctx_get0_propq(ctx));
44     if (ci != NULL)
45         ossl_cms_resolve_libctx(ci);
46     return ci;
47 }
48
49 int i2d_CMS_bio(BIO *bp, CMS_ContentInfo *cms)
50 {
51     return ASN1_item_i2d_bio(ASN1_ITEM_rptr(CMS_ContentInfo), bp, cms);
52 }
53
54 IMPLEMENT_PEM_rw(CMS, CMS_ContentInfo, PEM_STRING_CMS, CMS_ContentInfo)
55
56 BIO *BIO_new_CMS(BIO *out, CMS_ContentInfo *cms)
57 {
58     return BIO_new_NDEF(out, (ASN1_VALUE *)cms,
59                         ASN1_ITEM_rptr(CMS_ContentInfo));
60 }
61
62 /* CMS wrappers round generalised stream and MIME routines */
63
64 int i2d_CMS_bio_stream(BIO *out, CMS_ContentInfo *cms, BIO *in, int flags)
65 {
66     return i2d_ASN1_bio_stream(out, (ASN1_VALUE *)cms, in, flags,
67                                ASN1_ITEM_rptr(CMS_ContentInfo));
68 }
69
70 int PEM_write_bio_CMS_stream(BIO *out, CMS_ContentInfo *cms, BIO *in,
71                              int flags)
72 {
73     return PEM_write_bio_ASN1_stream(out, (ASN1_VALUE *)cms, in, flags,
74                                      "CMS", ASN1_ITEM_rptr(CMS_ContentInfo));
75 }
76
77 int SMIME_write_CMS(BIO *bio, CMS_ContentInfo *cms, BIO *data, int flags)
78 {
79     STACK_OF(X509_ALGOR) *mdalgs;
80     int ctype_nid = OBJ_obj2nid(cms->contentType);
81     int econt_nid = OBJ_obj2nid(CMS_get0_eContentType(cms));
82     const CMS_CTX *ctx = ossl_cms_get0_cmsctx(cms);
83
84     if (ctype_nid == NID_pkcs7_signed)
85         mdalgs = cms->d.signedData->digestAlgorithms;
86     else
87         mdalgs = NULL;
88
89     return SMIME_write_ASN1_ex(bio, (ASN1_VALUE *)cms, data, flags, ctype_nid,
90                                econt_nid, mdalgs,
91                                ASN1_ITEM_rptr(CMS_ContentInfo),
92                                ossl_cms_ctx_get0_libctx(ctx),
93                                ossl_cms_ctx_get0_propq(ctx));
94 }
95
96 CMS_ContentInfo *SMIME_read_CMS_ex(BIO *bio, int flags, BIO **bcont,
97                                    CMS_ContentInfo **cms)
98 {
99     CMS_ContentInfo *ci;
100     const CMS_CTX *ctx = ossl_cms_get0_cmsctx(cms == NULL ? NULL : *cms);
101
102     ci = (CMS_ContentInfo *)SMIME_read_ASN1_ex(bio, flags, bcont,
103                                                ASN1_ITEM_rptr(CMS_ContentInfo),
104                                                (ASN1_VALUE **)cms,
105                                                ossl_cms_ctx_get0_libctx(ctx),
106                                                ossl_cms_ctx_get0_propq(ctx));
107     if (ci != NULL)
108         ossl_cms_resolve_libctx(ci);
109     return ci;
110 }
111
112 CMS_ContentInfo *SMIME_read_CMS(BIO *bio, BIO **bcont)
113 {
114     return SMIME_read_CMS_ex(bio, 0, bcont, NULL);
115 }