21d86f986e46e377fcd859a06533f3534a08bf2a
[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 ok = 0;
79
80         int enc, keep_key = 0;
81
82         enc = ec->cipher ? 1 : 0;
83
84         b = BIO_new(BIO_f_cipher());
85         if (!b)
86                 {
87                 CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO,
88                                                         ERR_R_MALLOC_FAILURE);
89                 return NULL;
90                 }
91
92         BIO_get_cipher_ctx(b, &ctx);
93
94         if (enc)
95                 {
96                 ciph = ec->cipher;
97                 /* If not keeping key set cipher to NULL so subsequent calls
98                  * decrypt.
99                  */
100                 if (ec->key)
101                         ec->cipher = NULL;
102                 }
103         else
104                 {
105                 ciph = EVP_get_cipherbyobj(calg->algorithm);
106
107                 if (!ciph)
108                         {
109                         CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO,
110                                                         CMS_R_UNKNOWN_CIPHER);
111                         goto err;
112                         }
113                 }
114
115         if (EVP_CipherInit_ex(ctx, ciph, NULL, NULL, NULL, enc) <= 0)
116                 {
117                 CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO,
118                                 CMS_R_CIPHER_INITIALISATION_ERROR);
119                 goto err;
120                 }
121
122         if (enc)
123                 {
124                 int ivlen;
125                 calg->algorithm = OBJ_nid2obj(EVP_CIPHER_CTX_type(ctx));
126                 /* Generate a random IV if we need one */
127                 ivlen = EVP_CIPHER_CTX_iv_length(ctx);
128                 if (ivlen > 0)
129                         {
130                         if (RAND_pseudo_bytes(iv, ivlen) <= 0)
131                                 goto err;
132                         piv = iv;
133                         }
134                 }
135         else if (EVP_CIPHER_asn1_to_param(ctx, calg->parameter) <= 0)
136                 {
137                 CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO,
138                                 CMS_R_CIPHER_PARAMETER_INITIALISATION_ERROR);
139                 goto err;
140                 }
141
142
143         if (enc && !ec->key)
144                 {
145                 /* Generate random key */
146                 if (!ec->keylen)
147                         ec->keylen = EVP_CIPHER_CTX_key_length(ctx);
148                 ec->key = OPENSSL_malloc(ec->keylen);
149                 if (!ec->key)
150                         {
151                         CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO,
152                                                         ERR_R_MALLOC_FAILURE);
153                         goto err;
154                         }
155                 if (EVP_CIPHER_CTX_rand_key(ctx, ec->key) <= 0)
156                         goto err;
157                 keep_key = 1;
158                 }
159         else if (ec->keylen != (unsigned int)EVP_CIPHER_CTX_key_length(ctx))
160                 {
161                 /* If necessary set key length */
162                 if (EVP_CIPHER_CTX_set_key_length(ctx, ec->keylen) <= 0)
163                         {
164                         CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO,
165                                 CMS_R_INVALID_KEY_LENGTH);
166                         goto err;
167                         }
168                 }
169
170         if (EVP_CipherInit_ex(ctx, NULL, NULL, ec->key, piv, enc) <= 0)
171                 {
172                 CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO,
173                                 CMS_R_CIPHER_INITIALISATION_ERROR);
174                 goto err;
175                 }
176
177         if (piv)
178                 {
179                 calg->parameter = ASN1_TYPE_new();
180                 if (!calg->parameter)
181                         {
182                         CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO,
183                                                         ERR_R_MALLOC_FAILURE);
184                         goto err;
185                         }
186                 if (EVP_CIPHER_param_to_asn1(ctx, calg->parameter) <= 0)
187                         {
188                         CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO,
189                                 CMS_R_CIPHER_PARAMETER_INITIALISATION_ERROR);
190                         goto err;
191                         }
192                 }
193         ok = 1;
194
195         err:
196         if (ec->key && !keep_key)
197                 {
198                 OPENSSL_cleanse(ec->key, ec->keylen);
199                 OPENSSL_free(ec->key);
200                 ec->key = NULL;
201                 }
202         if (ok)
203                 return b;
204         BIO_free(b);
205         return NULL;
206         }
207
208 int cms_EncryptedContent_init(CMS_EncryptedContentInfo *ec, 
209                                 const EVP_CIPHER *cipher,
210                                 const unsigned char *key, size_t keylen)
211         {
212         ec->cipher = cipher;
213         if (key)
214                 {
215                 ec->key = OPENSSL_malloc(keylen);
216                 if (!ec->key)
217                         return 0;
218                 memcpy(ec->key, key, keylen);
219                 }
220         ec->keylen = keylen;
221         if (cipher)
222                 ec->contentType = OBJ_nid2obj(NID_pkcs7_data);
223         return 1;
224         }
225
226 int CMS_EncryptedData_set1_key(CMS_ContentInfo *cms, const EVP_CIPHER *ciph,
227                                 const unsigned char *key, size_t keylen)
228         {
229         CMS_EncryptedContentInfo *ec;
230         if (!key || !keylen)
231                 {
232                 CMSerr(CMS_F_CMS_ENCRYPTEDDATA_SET1_KEY, CMS_R_NO_KEY);
233                 return 0;
234                 }
235         if (ciph)
236                 {
237                 cms->d.encryptedData = M_ASN1_new_of(CMS_EncryptedData);
238                 if (!cms->d.encryptedData)
239                         {
240                         CMSerr(CMS_F_CMS_ENCRYPTEDDATA_SET1_KEY,
241                                 ERR_R_MALLOC_FAILURE);
242                         return 0;
243                         }
244                 cms->contentType = OBJ_nid2obj(NID_pkcs7_encrypted);
245                 cms->d.encryptedData->version = 0;
246                 }
247         else if (OBJ_obj2nid(cms->contentType) != NID_pkcs7_encrypted)
248                 {
249                 CMSerr(CMS_F_CMS_ENCRYPTEDDATA_SET1_KEY,
250                                                 CMS_R_NOT_ENCRYPTED_DATA);
251                 return 0;
252                 }
253         ec = cms->d.encryptedData->encryptedContentInfo;
254         return cms_EncryptedContent_init(ec, ciph, key, keylen);
255         }
256
257 BIO *cms_EncryptedData_init_bio(CMS_ContentInfo *cms)
258         {
259         CMS_EncryptedData *enc = cms->d.encryptedData;
260         if (enc->encryptedContentInfo->cipher && enc->unprotectedAttrs)
261                 enc->version = 2;
262         return cms_EncryptedContent_init_bio(enc->encryptedContentInfo);
263         }