1a88cfce91374498578c44013041416c0c5b09b1eKenny Root/* crypto/cms/cms_io.c */ 2a88cfce91374498578c44013041416c0c5b09b1eKenny Root/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL 3a88cfce91374498578c44013041416c0c5b09b1eKenny Root * project. 4a88cfce91374498578c44013041416c0c5b09b1eKenny Root */ 5a88cfce91374498578c44013041416c0c5b09b1eKenny Root/* ==================================================================== 6a88cfce91374498578c44013041416c0c5b09b1eKenny Root * Copyright (c) 2008 The OpenSSL Project. All rights reserved. 7a88cfce91374498578c44013041416c0c5b09b1eKenny Root * 8a88cfce91374498578c44013041416c0c5b09b1eKenny Root * Redistribution and use in source and binary forms, with or without 9a88cfce91374498578c44013041416c0c5b09b1eKenny Root * modification, are permitted provided that the following conditions 10a88cfce91374498578c44013041416c0c5b09b1eKenny Root * are met: 11a88cfce91374498578c44013041416c0c5b09b1eKenny Root * 12a88cfce91374498578c44013041416c0c5b09b1eKenny Root * 1. Redistributions of source code must retain the above copyright 13a88cfce91374498578c44013041416c0c5b09b1eKenny Root * notice, this list of conditions and the following disclaimer. 14a88cfce91374498578c44013041416c0c5b09b1eKenny Root * 15a88cfce91374498578c44013041416c0c5b09b1eKenny Root * 2. Redistributions in binary form must reproduce the above copyright 16a88cfce91374498578c44013041416c0c5b09b1eKenny Root * notice, this list of conditions and the following disclaimer in 17a88cfce91374498578c44013041416c0c5b09b1eKenny Root * the documentation and/or other materials provided with the 18a88cfce91374498578c44013041416c0c5b09b1eKenny Root * distribution. 19a88cfce91374498578c44013041416c0c5b09b1eKenny Root * 20a88cfce91374498578c44013041416c0c5b09b1eKenny Root * 3. All advertising materials mentioning features or use of this 21a88cfce91374498578c44013041416c0c5b09b1eKenny Root * software must display the following acknowledgment: 22a88cfce91374498578c44013041416c0c5b09b1eKenny Root * "This product includes software developed by the OpenSSL Project 23a88cfce91374498578c44013041416c0c5b09b1eKenny Root * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" 24a88cfce91374498578c44013041416c0c5b09b1eKenny Root * 25a88cfce91374498578c44013041416c0c5b09b1eKenny Root * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 26a88cfce91374498578c44013041416c0c5b09b1eKenny Root * endorse or promote products derived from this software without 27a88cfce91374498578c44013041416c0c5b09b1eKenny Root * prior written permission. For written permission, please contact 28a88cfce91374498578c44013041416c0c5b09b1eKenny Root * licensing@OpenSSL.org. 29a88cfce91374498578c44013041416c0c5b09b1eKenny Root * 30a88cfce91374498578c44013041416c0c5b09b1eKenny Root * 5. Products derived from this software may not be called "OpenSSL" 31a88cfce91374498578c44013041416c0c5b09b1eKenny Root * nor may "OpenSSL" appear in their names without prior written 32a88cfce91374498578c44013041416c0c5b09b1eKenny Root * permission of the OpenSSL Project. 33a88cfce91374498578c44013041416c0c5b09b1eKenny Root * 34a88cfce91374498578c44013041416c0c5b09b1eKenny Root * 6. Redistributions of any form whatsoever must retain the following 35a88cfce91374498578c44013041416c0c5b09b1eKenny Root * acknowledgment: 36a88cfce91374498578c44013041416c0c5b09b1eKenny Root * "This product includes software developed by the OpenSSL Project 37a88cfce91374498578c44013041416c0c5b09b1eKenny Root * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" 38a88cfce91374498578c44013041416c0c5b09b1eKenny Root * 39a88cfce91374498578c44013041416c0c5b09b1eKenny Root * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 40a88cfce91374498578c44013041416c0c5b09b1eKenny Root * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 41a88cfce91374498578c44013041416c0c5b09b1eKenny Root * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 42a88cfce91374498578c44013041416c0c5b09b1eKenny Root * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 43a88cfce91374498578c44013041416c0c5b09b1eKenny Root * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 44a88cfce91374498578c44013041416c0c5b09b1eKenny Root * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 45a88cfce91374498578c44013041416c0c5b09b1eKenny Root * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 46a88cfce91374498578c44013041416c0c5b09b1eKenny Root * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 47a88cfce91374498578c44013041416c0c5b09b1eKenny Root * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 48a88cfce91374498578c44013041416c0c5b09b1eKenny Root * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 49a88cfce91374498578c44013041416c0c5b09b1eKenny Root * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 50a88cfce91374498578c44013041416c0c5b09b1eKenny Root * OF THE POSSIBILITY OF SUCH DAMAGE. 51a88cfce91374498578c44013041416c0c5b09b1eKenny Root * ==================================================================== 52a88cfce91374498578c44013041416c0c5b09b1eKenny Root */ 53a88cfce91374498578c44013041416c0c5b09b1eKenny Root 54a88cfce91374498578c44013041416c0c5b09b1eKenny Root#include <openssl/asn1t.h> 55a88cfce91374498578c44013041416c0c5b09b1eKenny Root#include <openssl/x509.h> 56a88cfce91374498578c44013041416c0c5b09b1eKenny Root#include <openssl/err.h> 57a88cfce91374498578c44013041416c0c5b09b1eKenny Root#include <openssl/pem.h> 58a88cfce91374498578c44013041416c0c5b09b1eKenny Root#include "cms.h" 59a88cfce91374498578c44013041416c0c5b09b1eKenny Root#include "cms_lcl.h" 60a88cfce91374498578c44013041416c0c5b09b1eKenny Root 61a88cfce91374498578c44013041416c0c5b09b1eKenny Rootint CMS_stream(unsigned char ***boundary, CMS_ContentInfo *cms) 62a88cfce91374498578c44013041416c0c5b09b1eKenny Root { 63a88cfce91374498578c44013041416c0c5b09b1eKenny Root ASN1_OCTET_STRING **pos; 64a88cfce91374498578c44013041416c0c5b09b1eKenny Root pos = CMS_get0_content(cms); 65a88cfce91374498578c44013041416c0c5b09b1eKenny Root if (!pos) 66a88cfce91374498578c44013041416c0c5b09b1eKenny Root return 0; 67a88cfce91374498578c44013041416c0c5b09b1eKenny Root if (!*pos) 68a88cfce91374498578c44013041416c0c5b09b1eKenny Root *pos = ASN1_OCTET_STRING_new(); 69a88cfce91374498578c44013041416c0c5b09b1eKenny Root if (*pos) 70a88cfce91374498578c44013041416c0c5b09b1eKenny Root { 71a88cfce91374498578c44013041416c0c5b09b1eKenny Root (*pos)->flags |= ASN1_STRING_FLAG_NDEF; 72a88cfce91374498578c44013041416c0c5b09b1eKenny Root (*pos)->flags &= ~ASN1_STRING_FLAG_CONT; 73a88cfce91374498578c44013041416c0c5b09b1eKenny Root *boundary = &(*pos)->data; 74a88cfce91374498578c44013041416c0c5b09b1eKenny Root return 1; 75a88cfce91374498578c44013041416c0c5b09b1eKenny Root } 76a88cfce91374498578c44013041416c0c5b09b1eKenny Root CMSerr(CMS_F_CMS_STREAM, ERR_R_MALLOC_FAILURE); 77a88cfce91374498578c44013041416c0c5b09b1eKenny Root return 0; 78a88cfce91374498578c44013041416c0c5b09b1eKenny Root } 79a88cfce91374498578c44013041416c0c5b09b1eKenny Root 80a88cfce91374498578c44013041416c0c5b09b1eKenny RootCMS_ContentInfo *d2i_CMS_bio(BIO *bp, CMS_ContentInfo **cms) 81a88cfce91374498578c44013041416c0c5b09b1eKenny Root { 82a88cfce91374498578c44013041416c0c5b09b1eKenny Root return ASN1_item_d2i_bio(ASN1_ITEM_rptr(CMS_ContentInfo), bp, cms); 83a88cfce91374498578c44013041416c0c5b09b1eKenny Root } 84a88cfce91374498578c44013041416c0c5b09b1eKenny Root 85a88cfce91374498578c44013041416c0c5b09b1eKenny Rootint i2d_CMS_bio(BIO *bp, CMS_ContentInfo *cms) 86a88cfce91374498578c44013041416c0c5b09b1eKenny Root { 87a88cfce91374498578c44013041416c0c5b09b1eKenny Root return ASN1_item_i2d_bio(ASN1_ITEM_rptr(CMS_ContentInfo), bp, cms); 88a88cfce91374498578c44013041416c0c5b09b1eKenny Root } 89a88cfce91374498578c44013041416c0c5b09b1eKenny Root 90a88cfce91374498578c44013041416c0c5b09b1eKenny RootIMPLEMENT_PEM_rw_const(CMS, CMS_ContentInfo, PEM_STRING_CMS, CMS_ContentInfo) 91a88cfce91374498578c44013041416c0c5b09b1eKenny Root 92a88cfce91374498578c44013041416c0c5b09b1eKenny RootBIO *BIO_new_CMS(BIO *out, CMS_ContentInfo *cms) 93a88cfce91374498578c44013041416c0c5b09b1eKenny Root { 94a88cfce91374498578c44013041416c0c5b09b1eKenny Root return BIO_new_NDEF(out, (ASN1_VALUE *)cms, 95a88cfce91374498578c44013041416c0c5b09b1eKenny Root ASN1_ITEM_rptr(CMS_ContentInfo)); 96a88cfce91374498578c44013041416c0c5b09b1eKenny Root } 97a88cfce91374498578c44013041416c0c5b09b1eKenny Root 98a88cfce91374498578c44013041416c0c5b09b1eKenny Root/* CMS wrappers round generalised stream and MIME routines */ 99a88cfce91374498578c44013041416c0c5b09b1eKenny Root 100a88cfce91374498578c44013041416c0c5b09b1eKenny Rootint i2d_CMS_bio_stream(BIO *out, CMS_ContentInfo *cms, BIO *in, int flags) 101a88cfce91374498578c44013041416c0c5b09b1eKenny Root { 102a88cfce91374498578c44013041416c0c5b09b1eKenny Root return i2d_ASN1_bio_stream(out, (ASN1_VALUE *)cms, in, flags, 103a88cfce91374498578c44013041416c0c5b09b1eKenny Root ASN1_ITEM_rptr(CMS_ContentInfo)); 104a88cfce91374498578c44013041416c0c5b09b1eKenny Root } 105a88cfce91374498578c44013041416c0c5b09b1eKenny Root 106a88cfce91374498578c44013041416c0c5b09b1eKenny Rootint PEM_write_bio_CMS_stream(BIO *out, CMS_ContentInfo *cms, BIO *in, int flags) 107a88cfce91374498578c44013041416c0c5b09b1eKenny Root { 108a88cfce91374498578c44013041416c0c5b09b1eKenny Root return PEM_write_bio_ASN1_stream(out, (ASN1_VALUE *) cms, in, flags, 109a88cfce91374498578c44013041416c0c5b09b1eKenny Root "CMS", 110a88cfce91374498578c44013041416c0c5b09b1eKenny Root ASN1_ITEM_rptr(CMS_ContentInfo)); 111a88cfce91374498578c44013041416c0c5b09b1eKenny Root } 112a88cfce91374498578c44013041416c0c5b09b1eKenny Root 113a88cfce91374498578c44013041416c0c5b09b1eKenny Rootint SMIME_write_CMS(BIO *bio, CMS_ContentInfo *cms, BIO *data, int flags) 114a88cfce91374498578c44013041416c0c5b09b1eKenny Root { 115a88cfce91374498578c44013041416c0c5b09b1eKenny Root STACK_OF(X509_ALGOR) *mdalgs; 116a88cfce91374498578c44013041416c0c5b09b1eKenny Root int ctype_nid = OBJ_obj2nid(cms->contentType); 117a88cfce91374498578c44013041416c0c5b09b1eKenny Root int econt_nid = OBJ_obj2nid(CMS_get0_eContentType(cms)); 118a88cfce91374498578c44013041416c0c5b09b1eKenny Root if (ctype_nid == NID_pkcs7_signed) 119a88cfce91374498578c44013041416c0c5b09b1eKenny Root mdalgs = cms->d.signedData->digestAlgorithms; 120a88cfce91374498578c44013041416c0c5b09b1eKenny Root else 121a88cfce91374498578c44013041416c0c5b09b1eKenny Root mdalgs = NULL; 122a88cfce91374498578c44013041416c0c5b09b1eKenny Root 123a88cfce91374498578c44013041416c0c5b09b1eKenny Root return SMIME_write_ASN1(bio, (ASN1_VALUE *)cms, data, flags, 124a88cfce91374498578c44013041416c0c5b09b1eKenny Root ctype_nid, econt_nid, mdalgs, 125a88cfce91374498578c44013041416c0c5b09b1eKenny Root ASN1_ITEM_rptr(CMS_ContentInfo)); 126a88cfce91374498578c44013041416c0c5b09b1eKenny Root } 127a88cfce91374498578c44013041416c0c5b09b1eKenny Root 128a88cfce91374498578c44013041416c0c5b09b1eKenny RootCMS_ContentInfo *SMIME_read_CMS(BIO *bio, BIO **bcont) 129a88cfce91374498578c44013041416c0c5b09b1eKenny Root { 130a88cfce91374498578c44013041416c0c5b09b1eKenny Root return (CMS_ContentInfo *)SMIME_read_ASN1(bio, bcont, 131a88cfce91374498578c44013041416c0c5b09b1eKenny Root ASN1_ITEM_rptr(CMS_ContentInfo)); 132a88cfce91374498578c44013041416c0c5b09b1eKenny Root } 133a88cfce91374498578c44013041416c0c5b09b1eKenny Root 134