]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/cms/cms_io.c
Update copyright year
[thirdparty/openssl.git] / crypto / cms / cms_io.c
CommitLineData
0f113f3e 1/*
fecb3aae 2 * Copyright 2008-2022 The OpenSSL Project Authors. All Rights Reserved.
8931b30d 3 *
08ddd302 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
b1322259
RS
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
8931b30d
DSH
8 */
9
10#include <openssl/asn1t.h>
11#include <openssl/x509.h>
12#include <openssl/err.h>
13#include <openssl/pem.h>
e52a3c3d 14#include <openssl/cms.h>
706457b7 15#include "cms_local.h"
8931b30d 16
9fdcc21f 17/* unfortunately cannot constify BIO_new_NDEF() due to this and PKCS7_stream() */
8931b30d 18int CMS_stream(unsigned char ***boundary, CMS_ContentInfo *cms)
0f113f3e
MC
19{
20 ASN1_OCTET_STRING **pos;
21 pos = CMS_get0_content(cms);
90945fa3 22 if (pos == NULL)
0f113f3e 23 return 0;
90945fa3 24 if (*pos == NULL)
0f113f3e 25 *pos = ASN1_OCTET_STRING_new();
90945fa3 26 if (*pos != NULL) {
0f113f3e
MC
27 (*pos)->flags |= ASN1_STRING_FLAG_NDEF;
28 (*pos)->flags &= ~ASN1_STRING_FLAG_CONT;
29 *boundary = &(*pos)->data;
30 return 1;
31 }
9311d0c4 32 ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
33 return 0;
34}
8931b30d
DSH
35
36CMS_ContentInfo *d2i_CMS_bio(BIO *bp, CMS_ContentInfo **cms)
0f113f3e 37{
c1669f41 38 CMS_ContentInfo *ci;
5dca2afc 39 const CMS_CTX *ctx = ossl_cms_get0_cmsctx(cms == NULL ? NULL : *cms);
c1669f41 40
5dca2afc
MC
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));
45a3c592
DF
44 if (ci != NULL) {
45 ERR_set_mark();
53155f1c 46 ossl_cms_resolve_libctx(ci);
45a3c592
DF
47 ERR_pop_to_mark();
48 }
c1669f41 49 return ci;
0f113f3e 50}
8931b30d
DSH
51
52int i2d_CMS_bio(BIO *bp, CMS_ContentInfo *cms)
0f113f3e
MC
53{
54 return ASN1_item_i2d_bio(ASN1_ITEM_rptr(CMS_ContentInfo), bp, cms);
55}
8931b30d 56
de0799b0 57IMPLEMENT_PEM_rw(CMS, CMS_ContentInfo, PEM_STRING_CMS, CMS_ContentInfo)
8931b30d 58
0f113f3e
MC
59BIO *BIO_new_CMS(BIO *out, CMS_ContentInfo *cms)
60{
61 return BIO_new_NDEF(out, (ASN1_VALUE *)cms,
62 ASN1_ITEM_rptr(CMS_ContentInfo));
63}
8931b30d
DSH
64
65/* CMS wrappers round generalised stream and MIME routines */
66
67int i2d_CMS_bio_stream(BIO *out, CMS_ContentInfo *cms, BIO *in, int flags)
0f113f3e
MC
68{
69 return i2d_ASN1_bio_stream(out, (ASN1_VALUE *)cms, in, flags,
70 ASN1_ITEM_rptr(CMS_ContentInfo));
71}
8931b30d 72
0f113f3e
MC
73int PEM_write_bio_CMS_stream(BIO *out, CMS_ContentInfo *cms, BIO *in,
74 int flags)
75{
76 return PEM_write_bio_ASN1_stream(out, (ASN1_VALUE *)cms, in, flags,
77 "CMS", ASN1_ITEM_rptr(CMS_ContentInfo));
78}
8931b30d
DSH
79
80int SMIME_write_CMS(BIO *bio, CMS_ContentInfo *cms, BIO *data, int flags)
0f113f3e
MC
81{
82 STACK_OF(X509_ALGOR) *mdalgs;
83 int ctype_nid = OBJ_obj2nid(cms->contentType);
84 int econt_nid = OBJ_obj2nid(CMS_get0_eContentType(cms));
53155f1c 85 const CMS_CTX *ctx = ossl_cms_get0_cmsctx(cms);
c1669f41 86
0f113f3e
MC
87 if (ctype_nid == NID_pkcs7_signed)
88 mdalgs = cms->d.signedData->digestAlgorithms;
89 else
90 mdalgs = NULL;
8931b30d 91
d8652be0
MC
92 return SMIME_write_ASN1_ex(bio, (ASN1_VALUE *)cms, data, flags, ctype_nid,
93 econt_nid, mdalgs,
94 ASN1_ITEM_rptr(CMS_ContentInfo),
53155f1c
SL
95 ossl_cms_ctx_get0_libctx(ctx),
96 ossl_cms_ctx_get0_propq(ctx));
c1669f41
SL
97}
98
5dca2afc
MC
99CMS_ContentInfo *SMIME_read_CMS_ex(BIO *bio, int flags, BIO **bcont,
100 CMS_ContentInfo **cms)
c1669f41
SL
101{
102 CMS_ContentInfo *ci;
5dca2afc 103 const CMS_CTX *ctx = ossl_cms_get0_cmsctx(cms == NULL ? NULL : *cms);
c1669f41 104
7c701c59 105 ci = (CMS_ContentInfo *)SMIME_read_ASN1_ex(bio, flags, bcont,
c1669f41 106 ASN1_ITEM_rptr(CMS_ContentInfo),
5dca2afc
MC
107 (ASN1_VALUE **)cms,
108 ossl_cms_ctx_get0_libctx(ctx),
109 ossl_cms_ctx_get0_propq(ctx));
45a3c592
DF
110 if (ci != NULL) {
111 ERR_set_mark();
53155f1c 112 ossl_cms_resolve_libctx(ci);
45a3c592
DF
113 ERR_pop_to_mark();
114 }
c1669f41 115 return ci;
0f113f3e 116}
8931b30d
DSH
117
118CMS_ContentInfo *SMIME_read_CMS(BIO *bio, BIO **bcont)
0f113f3e 119{
7c701c59 120 return SMIME_read_CMS_ex(bio, 0, bcont, NULL);
0f113f3e 121}