1/*
2 * Copyright 2008-2016 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() */
18int 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 CMSerr(CMS_F_CMS_STREAM, ERR_R_MALLOC_FAILURE);
33 return 0;
34}
35
36CMS_ContentInfo *d2i_CMS_bio(BIO *bp, CMS_ContentInfo **cms)
37{
38 return ASN1_item_d2i_bio(ASN1_ITEM_rptr(CMS_ContentInfo), bp, cms);
39}
40
41int i2d_CMS_bio(BIO *bp, CMS_ContentInfo *cms)
42{
43 return ASN1_item_i2d_bio(ASN1_ITEM_rptr(CMS_ContentInfo), bp, cms);
44}
45
46IMPLEMENT_PEM_rw(CMS, CMS_ContentInfo, PEM_STRING_CMS, CMS_ContentInfo)
47
48BIO *BIO_new_CMS(BIO *out, CMS_ContentInfo *cms)
49{
50 return BIO_new_NDEF(out, (ASN1_VALUE *)cms,
51 ASN1_ITEM_rptr(CMS_ContentInfo));
52}
53
54/* CMS wrappers round generalised stream and MIME routines */
55
56int i2d_CMS_bio_stream(BIO *out, CMS_ContentInfo *cms, BIO *in, int flags)
57{
58 return i2d_ASN1_bio_stream(out, (ASN1_VALUE *)cms, in, flags,
59 ASN1_ITEM_rptr(CMS_ContentInfo));
60}
61
62int PEM_write_bio_CMS_stream(BIO *out, CMS_ContentInfo *cms, BIO *in,
63 int flags)
64{
65 return PEM_write_bio_ASN1_stream(out, (ASN1_VALUE *)cms, in, flags,
66 "CMS", ASN1_ITEM_rptr(CMS_ContentInfo));
67}
68
69int SMIME_write_CMS(BIO *bio, CMS_ContentInfo *cms, BIO *data, int flags)
70{
71 STACK_OF(X509_ALGOR) *mdalgs;
72 int ctype_nid = OBJ_obj2nid(cms->contentType);
73 int econt_nid = OBJ_obj2nid(CMS_get0_eContentType(cms));
74 if (ctype_nid == NID_pkcs7_signed)
75 mdalgs = cms->d.signedData->digestAlgorithms;
76 else
77 mdalgs = NULL;
78
79 return SMIME_write_ASN1(bio, (ASN1_VALUE *)cms, data, flags,
80 ctype_nid, econt_nid, mdalgs,
81 ASN1_ITEM_rptr(CMS_ContentInfo));
82}
83
84CMS_ContentInfo *SMIME_read_CMS(BIO *bio, BIO **bcont)
85{
86 return (CMS_ContentInfo *)SMIME_read_ASN1(bio, bcont,
87 ASN1_ITEM_rptr
88 (CMS_ContentInfo));
89}
90