1183234Ssimon/* crypto/cms/cms_io.c */ 2183234Ssimon/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL 3183234Ssimon * project. 4183234Ssimon */ 5183234Ssimon/* ==================================================================== 6183234Ssimon * Copyright (c) 2008 The OpenSSL Project. All rights reserved. 7183234Ssimon * 8183234Ssimon * Redistribution and use in source and binary forms, with or without 9183234Ssimon * modification, are permitted provided that the following conditions 10183234Ssimon * are met: 11183234Ssimon * 12183234Ssimon * 1. Redistributions of source code must retain the above copyright 13183234Ssimon * notice, this list of conditions and the following disclaimer. 14183234Ssimon * 15183234Ssimon * 2. Redistributions in binary form must reproduce the above copyright 16183234Ssimon * notice, this list of conditions and the following disclaimer in 17183234Ssimon * the documentation and/or other materials provided with the 18183234Ssimon * distribution. 19183234Ssimon * 20183234Ssimon * 3. All advertising materials mentioning features or use of this 21183234Ssimon * software must display the following acknowledgment: 22183234Ssimon * "This product includes software developed by the OpenSSL Project 23183234Ssimon * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" 24183234Ssimon * 25183234Ssimon * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 26183234Ssimon * endorse or promote products derived from this software without 27183234Ssimon * prior written permission. For written permission, please contact 28183234Ssimon * licensing@OpenSSL.org. 29183234Ssimon * 30183234Ssimon * 5. Products derived from this software may not be called "OpenSSL" 31183234Ssimon * nor may "OpenSSL" appear in their names without prior written 32183234Ssimon * permission of the OpenSSL Project. 33183234Ssimon * 34183234Ssimon * 6. Redistributions of any form whatsoever must retain the following 35183234Ssimon * acknowledgment: 36183234Ssimon * "This product includes software developed by the OpenSSL Project 37183234Ssimon * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" 38183234Ssimon * 39183234Ssimon * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 40183234Ssimon * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 41183234Ssimon * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 42183234Ssimon * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 43183234Ssimon * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 44183234Ssimon * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 45183234Ssimon * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 46183234Ssimon * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 47183234Ssimon * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 48183234Ssimon * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 49183234Ssimon * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 50183234Ssimon * OF THE POSSIBILITY OF SUCH DAMAGE. 51183234Ssimon * ==================================================================== 52183234Ssimon */ 53183234Ssimon 54183234Ssimon#include <openssl/asn1t.h> 55183234Ssimon#include <openssl/x509.h> 56183234Ssimon#include <openssl/err.h> 57183234Ssimon#include <openssl/pem.h> 58183234Ssimon#include "cms.h" 59183234Ssimon#include "cms_lcl.h" 60183234Ssimon 61238405Sjkimint CMS_stream(unsigned char ***boundary, CMS_ContentInfo *cms) 62238405Sjkim { 63238405Sjkim ASN1_OCTET_STRING **pos; 64238405Sjkim pos = CMS_get0_content(cms); 65238405Sjkim if (!pos) 66238405Sjkim return 0; 67238405Sjkim if (!*pos) 68238405Sjkim *pos = ASN1_OCTET_STRING_new(); 69238405Sjkim if (*pos) 70238405Sjkim { 71238405Sjkim (*pos)->flags |= ASN1_STRING_FLAG_NDEF; 72238405Sjkim (*pos)->flags &= ~ASN1_STRING_FLAG_CONT; 73238405Sjkim *boundary = &(*pos)->data; 74238405Sjkim return 1; 75238405Sjkim } 76238405Sjkim CMSerr(CMS_F_CMS_STREAM, ERR_R_MALLOC_FAILURE); 77238405Sjkim return 0; 78238405Sjkim } 79238405Sjkim 80183234SsimonCMS_ContentInfo *d2i_CMS_bio(BIO *bp, CMS_ContentInfo **cms) 81183234Ssimon { 82183234Ssimon return ASN1_item_d2i_bio(ASN1_ITEM_rptr(CMS_ContentInfo), bp, cms); 83183234Ssimon } 84183234Ssimon 85183234Ssimonint i2d_CMS_bio(BIO *bp, CMS_ContentInfo *cms) 86183234Ssimon { 87183234Ssimon return ASN1_item_i2d_bio(ASN1_ITEM_rptr(CMS_ContentInfo), bp, cms); 88183234Ssimon } 89183234Ssimon 90183234SsimonIMPLEMENT_PEM_rw_const(CMS, CMS_ContentInfo, PEM_STRING_CMS, CMS_ContentInfo) 91183234Ssimon 92238405SjkimBIO *BIO_new_CMS(BIO *out, CMS_ContentInfo *cms) 93183234Ssimon { 94238405Sjkim return BIO_new_NDEF(out, (ASN1_VALUE *)cms, 95238405Sjkim ASN1_ITEM_rptr(CMS_ContentInfo)); 96238405Sjkim } 97183234Ssimon 98238405Sjkim/* CMS wrappers round generalised stream and MIME routines */ 99183234Ssimon 100238405Sjkimint i2d_CMS_bio_stream(BIO *out, CMS_ContentInfo *cms, BIO *in, int flags) 101238405Sjkim { 102238405Sjkim return i2d_ASN1_bio_stream(out, (ASN1_VALUE *)cms, in, flags, 103238405Sjkim ASN1_ITEM_rptr(CMS_ContentInfo)); 104238405Sjkim } 105183234Ssimon 106238405Sjkimint PEM_write_bio_CMS_stream(BIO *out, CMS_ContentInfo *cms, BIO *in, int flags) 107238405Sjkim { 108238405Sjkim return PEM_write_bio_ASN1_stream(out, (ASN1_VALUE *) cms, in, flags, 109238405Sjkim "CMS", 110238405Sjkim ASN1_ITEM_rptr(CMS_ContentInfo)); 111183234Ssimon } 112183234Ssimon 113183234Ssimonint SMIME_write_CMS(BIO *bio, CMS_ContentInfo *cms, BIO *data, int flags) 114183234Ssimon { 115183234Ssimon STACK_OF(X509_ALGOR) *mdalgs; 116183234Ssimon int ctype_nid = OBJ_obj2nid(cms->contentType); 117183234Ssimon int econt_nid = OBJ_obj2nid(CMS_get0_eContentType(cms)); 118183234Ssimon if (ctype_nid == NID_pkcs7_signed) 119183234Ssimon mdalgs = cms->d.signedData->digestAlgorithms; 120183234Ssimon else 121183234Ssimon mdalgs = NULL; 122183234Ssimon 123238405Sjkim return SMIME_write_ASN1(bio, (ASN1_VALUE *)cms, data, flags, 124183234Ssimon ctype_nid, econt_nid, mdalgs, 125183234Ssimon ASN1_ITEM_rptr(CMS_ContentInfo)); 126183234Ssimon } 127183234Ssimon 128183234SsimonCMS_ContentInfo *SMIME_read_CMS(BIO *bio, BIO **bcont) 129183234Ssimon { 130183234Ssimon return (CMS_ContentInfo *)SMIME_read_ASN1(bio, bcont, 131183234Ssimon ASN1_ITEM_rptr(CMS_ContentInfo)); 132183234Ssimon } 133238405Sjkim 134