]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/cms/cms_io.c
Run the withlibctx.pl script
[thirdparty/openssl.git] / crypto / cms / cms_io.c
CommitLineData
0f113f3e 1/*
b1322259 2 * Copyright 2008-2016 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 }
32 CMSerr(CMS_F_CMS_STREAM, ERR_R_MALLOC_FAILURE);
33 return 0;
34}
8931b30d
DSH
35
36CMS_ContentInfo *d2i_CMS_bio(BIO *bp, CMS_ContentInfo **cms)
0f113f3e 37{
c1669f41
SL
38 CMS_ContentInfo *ci;
39
40 ci = ASN1_item_d2i_bio(ASN1_ITEM_rptr(CMS_ContentInfo), bp, cms);
41 if (ci != NULL && cms != NULL)
42 cms_resolve_libctx(ci);
43 return ci;
0f113f3e 44}
8931b30d
DSH
45
46int i2d_CMS_bio(BIO *bp, CMS_ContentInfo *cms)
0f113f3e
MC
47{
48 return ASN1_item_i2d_bio(ASN1_ITEM_rptr(CMS_ContentInfo), bp, cms);
49}
8931b30d 50
de0799b0 51IMPLEMENT_PEM_rw(CMS, CMS_ContentInfo, PEM_STRING_CMS, CMS_ContentInfo)
8931b30d 52
0f113f3e
MC
53BIO *BIO_new_CMS(BIO *out, CMS_ContentInfo *cms)
54{
55 return BIO_new_NDEF(out, (ASN1_VALUE *)cms,
56 ASN1_ITEM_rptr(CMS_ContentInfo));
57}
8931b30d
DSH
58
59/* CMS wrappers round generalised stream and MIME routines */
60
61int i2d_CMS_bio_stream(BIO *out, CMS_ContentInfo *cms, BIO *in, int flags)
0f113f3e
MC
62{
63 return i2d_ASN1_bio_stream(out, (ASN1_VALUE *)cms, in, flags,
64 ASN1_ITEM_rptr(CMS_ContentInfo));
65}
8931b30d 66
0f113f3e
MC
67int PEM_write_bio_CMS_stream(BIO *out, CMS_ContentInfo *cms, BIO *in,
68 int flags)
69{
70 return PEM_write_bio_ASN1_stream(out, (ASN1_VALUE *)cms, in, flags,
71 "CMS", ASN1_ITEM_rptr(CMS_ContentInfo));
72}
8931b30d
DSH
73
74int SMIME_write_CMS(BIO *bio, CMS_ContentInfo *cms, BIO *data, int flags)
0f113f3e
MC
75{
76 STACK_OF(X509_ALGOR) *mdalgs;
77 int ctype_nid = OBJ_obj2nid(cms->contentType);
78 int econt_nid = OBJ_obj2nid(CMS_get0_eContentType(cms));
c1669f41
SL
79 const CMS_CTX *ctx = cms_get0_cmsctx(cms);
80
0f113f3e
MC
81 if (ctype_nid == NID_pkcs7_signed)
82 mdalgs = cms->d.signedData->digestAlgorithms;
83 else
84 mdalgs = NULL;
8931b30d 85
d8652be0
MC
86 return SMIME_write_ASN1_ex(bio, (ASN1_VALUE *)cms, data, flags, ctype_nid,
87 econt_nid, mdalgs,
88 ASN1_ITEM_rptr(CMS_ContentInfo),
89 cms_ctx_get0_libctx(ctx),
90 cms_ctx_get0_propq(ctx));
c1669f41
SL
91}
92
93CMS_ContentInfo *SMIME_read_CMS_ex(BIO *bio, BIO **bcont, CMS_ContentInfo **cms)
94{
95 CMS_ContentInfo *ci;
96
97 ci = (CMS_ContentInfo *)SMIME_read_ASN1_ex(bio, bcont,
98 ASN1_ITEM_rptr(CMS_ContentInfo),
99 (ASN1_VALUE **)cms);
100 if (ci != NULL && cms != NULL)
101 cms_resolve_libctx(ci);
102 return ci;
0f113f3e 103}
8931b30d
DSH
104
105CMS_ContentInfo *SMIME_read_CMS(BIO *bio, BIO **bcont)
0f113f3e 106{
c1669f41 107 return SMIME_read_CMS_ex(bio, bcont, NULL);
0f113f3e 108}