Reorganise encrypted content info code to avoid duplication and be more
[oweals/openssl.git] / crypto / cms / cms_enc.c
1 /* crypto/cms/cms_enc.c */
2 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3  * project.
4  */
5 /* ====================================================================
6  * Copyright (c) 2008 The OpenSSL Project.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer. 
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in
17  *    the documentation and/or other materials provided with the
18  *    distribution.
19  *
20  * 3. All advertising materials mentioning features or use of this
21  *    software must display the following acknowledgment:
22  *    "This product includes software developed by the OpenSSL Project
23  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24  *
25  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26  *    endorse or promote products derived from this software without
27  *    prior written permission. For written permission, please contact
28  *    licensing@OpenSSL.org.
29  *
30  * 5. Products derived from this software may not be called "OpenSSL"
31  *    nor may "OpenSSL" appear in their names without prior written
32  *    permission of the OpenSSL Project.
33  *
34  * 6. Redistributions of any form whatsoever must retain the following
35  *    acknowledgment:
36  *    "This product includes software developed by the OpenSSL Project
37  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38  *
39  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50  * OF THE POSSIBILITY OF SUCH DAMAGE.
51  * ====================================================================
52  */
53
54 #include "cryptlib.h"
55 #include <openssl/asn1t.h>
56 #include <openssl/pem.h>
57 #include <openssl/x509v3.h>
58 #include <openssl/err.h>
59 #include <openssl/cms.h>
60 #include <openssl/rand.h>
61 #include "cms_lcl.h"
62 #include "asn1_locl.h"
63
64 /* CMS EncryptedData Utilities */
65
66 DECLARE_ASN1_ITEM(CMS_EncryptedData)
67
68 /* Return BIO based on EncryptedContentInfo and key */
69
70 BIO *cms_EncryptedContent_init_bio(CMS_EncryptedContentInfo *ec)
71         {
72         BIO *b;
73         EVP_CIPHER_CTX *ctx;
74         const EVP_CIPHER *ciph;
75         X509_ALGOR *calg = ec->contentEncryptionAlgorithm;
76         unsigned char iv[EVP_MAX_IV_LENGTH], *piv = NULL;
77
78         int enc;
79
80         enc = ec->cipher ? 1 : 0;
81
82         b = BIO_new(BIO_f_cipher());
83         if (!b)
84                 {
85                 CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO,
86                                                         ERR_R_MALLOC_FAILURE);
87                 return NULL;
88                 }
89
90         BIO_get_cipher_ctx(b, &ctx);
91
92         if (enc)
93                 calg->algorithm = OBJ_nid2obj(EVP_CIPHER_CTX_type(ctx));
94         else
95                 {
96                 ciph = EVP_get_cipherbyobj(calg->algorithm);
97
98                 if (!ciph)
99                         {
100                         CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO,
101                                                         CMS_R_UNKNOWN_CIPHER);
102                         goto err;
103                         }
104                 }
105
106         if (EVP_CipherInit_ex(ctx, ciph, NULL, NULL, NULL, enc) <= 0)
107                 {
108                 CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO,
109                                 CMS_R_CIPHER_INITIALISATION_ERROR);
110                 goto err;
111                 }
112
113         /* If necessary set key length */
114
115         if (ec->keylen != EVP_CIPHER_CTX_key_length(ctx))
116                 {
117                 if (EVP_CIPHER_CTX_set_key_length(ctx, ec->keylen) <= 0)
118                         {
119                         CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO,
120                                 CMS_R_INVALID_KEY_LENGTH);
121                         goto err;
122                         }
123                 }
124
125         if (enc)
126                 {
127                 int ivlen;
128                 /* Generate a random IV if we need one */
129                 ivlen = EVP_CIPHER_CTX_iv_length(ctx);
130                 if (ivlen > 0)
131                         {
132                         if (RAND_pseudo_bytes(iv, ivlen) <= 0)
133                                 goto err;
134                         piv = iv;
135                         }
136                 }
137         else if (EVP_CIPHER_asn1_to_param(ctx, calg->parameter) <= 0)
138                         {
139                         CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO,
140                                 CMS_R_CIPHER_PARAMETER_INITIALISATION_ERROR);
141                         goto err;
142                         }
143
144         if (EVP_CipherInit_ex(ctx, NULL, NULL, ec->key, piv, enc) <= 0)
145                 {
146                 CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO,
147                                 CMS_R_CIPHER_INITIALISATION_ERROR);
148                 goto err;
149                 }
150
151         if (piv)
152                 {
153                 calg->parameter = ASN1_TYPE_new();
154                 if (!calg->parameter)
155                         {
156                         CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO,
157                                                         ERR_R_MALLOC_FAILURE);
158                         goto err;
159                         }
160                 if (EVP_CIPHER_param_to_asn1(ctx, calg->parameter) <= 0)
161                         {
162                         CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO,
163                                 CMS_R_CIPHER_PARAMETER_INITIALISATION_ERROR);
164                         goto err;
165                         }
166                 }
167         return b;
168
169         err:
170         BIO_free(b);
171         return NULL;
172         }
173
174 int cms_EncryptedContent_init(CMS_EncryptedContentInfo *ec, 
175                                 const EVP_CIPHER *cipher,
176                                 const unsigned char *key, size_t keylen)
177         {
178         ec->cipher = cipher;
179         ec->key = OPENSSL_malloc(keylen);
180         if (!ec->key)
181                 return 0;
182         if (cipher)
183                 ec->contentType = OBJ_nid2obj(NID_pkcs7_data);
184         memcpy(ec->key, key, keylen);
185         ec->keylen = keylen;
186         return 1;
187         }
188
189 int CMS_EncryptedData_set1_key(CMS_ContentInfo *cms, const EVP_CIPHER *ciph,
190                                 const unsigned char *key, size_t keylen)
191         {
192         CMS_EncryptedContentInfo *ec;
193         if (ciph)
194                 {
195                 cms->d.encryptedData = M_ASN1_new_of(CMS_EncryptedData);
196                 if (!cms->d.encryptedData)
197                         {
198                         CMSerr(CMS_F_CMS_ENCRYPTEDDATA_SET1_KEY,
199                                 ERR_R_MALLOC_FAILURE);
200                         return 0;
201                         }
202                 cms->contentType = OBJ_nid2obj(NID_pkcs7_encrypted);
203                 cms->d.encryptedData->version = 0;
204                 }
205         else if (OBJ_obj2nid(cms->contentType) != NID_pkcs7_encrypted)
206                 {
207                 CMSerr(CMS_F_CMS_ENCRYPTEDDATA_SET1_KEY,
208                                                 CMS_R_NOT_ENCRYPTED_DATA);
209                 return 0;
210                 }
211         ec = cms->d.encryptedData->encryptedContentInfo;
212         return cms_EncryptedContent_init(ec, ciph, key, keylen);
213         }
214
215 BIO *cms_EncryptedData_init_bio(CMS_ContentInfo *cms)
216         {
217         CMS_EncryptedContentInfo *ec;
218         if (OBJ_obj2nid(cms->contentType) != NID_pkcs7_encrypted)
219                 {
220                 CMSerr(CMS_F_CMS_ENCRYPTEDDATA_INIT_BIO,
221                                                 CMS_R_NOT_ENCRYPTED_DATA);
222                 return NULL;
223                 }
224         ec = cms->d.encryptedData->encryptedContentInfo;
225         return cms_EncryptedContent_init_bio(ec);
226         }