1183234Ssimon/* crypto/cms/cms_io.c */ 2280304Sjkim/* 3280304Sjkim * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL 4183234Ssimon * project. 5183234Ssimon */ 6183234Ssimon/* ==================================================================== 7183234Ssimon * Copyright (c) 2008 The OpenSSL Project. All rights reserved. 8183234Ssimon * 9183234Ssimon * Redistribution and use in source and binary forms, with or without 10183234Ssimon * modification, are permitted provided that the following conditions 11183234Ssimon * are met: 12183234Ssimon * 13183234Ssimon * 1. Redistributions of source code must retain the above copyright 14280304Sjkim * notice, this list of conditions and the following disclaimer. 15183234Ssimon * 16183234Ssimon * 2. Redistributions in binary form must reproduce the above copyright 17183234Ssimon * notice, this list of conditions and the following disclaimer in 18183234Ssimon * the documentation and/or other materials provided with the 19183234Ssimon * distribution. 20183234Ssimon * 21183234Ssimon * 3. All advertising materials mentioning features or use of this 22183234Ssimon * software must display the following acknowledgment: 23183234Ssimon * "This product includes software developed by the OpenSSL Project 24183234Ssimon * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" 25183234Ssimon * 26183234Ssimon * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 27183234Ssimon * endorse or promote products derived from this software without 28183234Ssimon * prior written permission. For written permission, please contact 29183234Ssimon * licensing@OpenSSL.org. 30183234Ssimon * 31183234Ssimon * 5. Products derived from this software may not be called "OpenSSL" 32183234Ssimon * nor may "OpenSSL" appear in their names without prior written 33183234Ssimon * permission of the OpenSSL Project. 34183234Ssimon * 35183234Ssimon * 6. Redistributions of any form whatsoever must retain the following 36183234Ssimon * acknowledgment: 37183234Ssimon * "This product includes software developed by the OpenSSL Project 38183234Ssimon * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" 39183234Ssimon * 40183234Ssimon * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 41183234Ssimon * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 42183234Ssimon * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 43183234Ssimon * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 44183234Ssimon * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 45183234Ssimon * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 46183234Ssimon * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 47183234Ssimon * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 48183234Ssimon * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 49183234Ssimon * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 50183234Ssimon * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 51183234Ssimon * OF THE POSSIBILITY OF SUCH DAMAGE. 52183234Ssimon * ==================================================================== 53183234Ssimon */ 54183234Ssimon 55183234Ssimon#include <openssl/asn1t.h> 56183234Ssimon#include <openssl/x509.h> 57183234Ssimon#include <openssl/err.h> 58183234Ssimon#include <openssl/pem.h> 59183234Ssimon#include "cms.h" 60183234Ssimon#include "cms_lcl.h" 61183234Ssimon 62238405Sjkimint CMS_stream(unsigned char ***boundary, CMS_ContentInfo *cms) 63280304Sjkim{ 64280304Sjkim ASN1_OCTET_STRING **pos; 65280304Sjkim pos = CMS_get0_content(cms); 66280304Sjkim if (!pos) 67280304Sjkim return 0; 68280304Sjkim if (!*pos) 69280304Sjkim *pos = ASN1_OCTET_STRING_new(); 70280304Sjkim if (*pos) { 71280304Sjkim (*pos)->flags |= ASN1_STRING_FLAG_NDEF; 72280304Sjkim (*pos)->flags &= ~ASN1_STRING_FLAG_CONT; 73280304Sjkim *boundary = &(*pos)->data; 74280304Sjkim return 1; 75280304Sjkim } 76280304Sjkim CMSerr(CMS_F_CMS_STREAM, ERR_R_MALLOC_FAILURE); 77280304Sjkim return 0; 78280304Sjkim} 79238405Sjkim 80183234SsimonCMS_ContentInfo *d2i_CMS_bio(BIO *bp, CMS_ContentInfo **cms) 81280304Sjkim{ 82280304Sjkim return ASN1_item_d2i_bio(ASN1_ITEM_rptr(CMS_ContentInfo), bp, cms); 83280304Sjkim} 84183234Ssimon 85183234Ssimonint i2d_CMS_bio(BIO *bp, CMS_ContentInfo *cms) 86280304Sjkim{ 87280304Sjkim return ASN1_item_i2d_bio(ASN1_ITEM_rptr(CMS_ContentInfo), bp, cms); 88280304Sjkim} 89183234Ssimon 90183234SsimonIMPLEMENT_PEM_rw_const(CMS, CMS_ContentInfo, PEM_STRING_CMS, CMS_ContentInfo) 91183234Ssimon 92280304SjkimBIO *BIO_new_CMS(BIO *out, CMS_ContentInfo *cms) 93280304Sjkim{ 94280304Sjkim return BIO_new_NDEF(out, (ASN1_VALUE *)cms, 95280304Sjkim ASN1_ITEM_rptr(CMS_ContentInfo)); 96280304Sjkim} 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) 101280304Sjkim{ 102280304Sjkim return i2d_ASN1_bio_stream(out, (ASN1_VALUE *)cms, in, flags, 103280304Sjkim ASN1_ITEM_rptr(CMS_ContentInfo)); 104280304Sjkim} 105183234Ssimon 106280304Sjkimint PEM_write_bio_CMS_stream(BIO *out, CMS_ContentInfo *cms, BIO *in, 107280304Sjkim int flags) 108280304Sjkim{ 109280304Sjkim return PEM_write_bio_ASN1_stream(out, (ASN1_VALUE *)cms, in, flags, 110280304Sjkim "CMS", ASN1_ITEM_rptr(CMS_ContentInfo)); 111280304Sjkim} 112183234Ssimon 113183234Ssimonint SMIME_write_CMS(BIO *bio, CMS_ContentInfo *cms, BIO *data, int flags) 114280304Sjkim{ 115280304Sjkim STACK_OF(X509_ALGOR) *mdalgs; 116280304Sjkim int ctype_nid = OBJ_obj2nid(cms->contentType); 117280304Sjkim int econt_nid = OBJ_obj2nid(CMS_get0_eContentType(cms)); 118280304Sjkim if (ctype_nid == NID_pkcs7_signed) 119280304Sjkim mdalgs = cms->d.signedData->digestAlgorithms; 120280304Sjkim else 121280304Sjkim mdalgs = NULL; 122183234Ssimon 123280304Sjkim return SMIME_write_ASN1(bio, (ASN1_VALUE *)cms, data, flags, 124280304Sjkim ctype_nid, econt_nid, mdalgs, 125280304Sjkim ASN1_ITEM_rptr(CMS_ContentInfo)); 126280304Sjkim} 127183234Ssimon 128183234SsimonCMS_ContentInfo *SMIME_read_CMS(BIO *bio, BIO **bcont) 129280304Sjkim{ 130280304Sjkim return (CMS_ContentInfo *)SMIME_read_ASN1(bio, bcont, 131280304Sjkim ASN1_ITEM_rptr 132280304Sjkim (CMS_ContentInfo)); 133280304Sjkim} 134