xref: /openbsd-src/lib/libcrypto/asn1/p5_pbev2.c (revision 50b7afb2c2c0993b0894d4e34bf857cb13ed9c80)
1 /* $OpenBSD: p5_pbev2.c,v 1.17 2014/07/11 08:44:47 jsing Exp $ */
2 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3  * project 1999-2004.
4  */
5 /* ====================================================================
6  * Copyright (c) 1999 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  * This product includes cryptographic software written by Eric Young
54  * (eay@cryptsoft.com).  This product includes software written by Tim
55  * Hudson (tjh@cryptsoft.com).
56  *
57  */
58 
59 #include <stdio.h>
60 #include <string.h>
61 
62 #include <openssl/asn1t.h>
63 #include <openssl/err.h>
64 #include <openssl/rand.h>
65 #include <openssl/x509.h>
66 
67 /* PKCS#5 v2.0 password based encryption structures */
68 
69 ASN1_SEQUENCE(PBE2PARAM) = {
70 	ASN1_SIMPLE(PBE2PARAM, keyfunc, X509_ALGOR),
71 	ASN1_SIMPLE(PBE2PARAM, encryption, X509_ALGOR)
72 } ASN1_SEQUENCE_END(PBE2PARAM)
73 
74 IMPLEMENT_ASN1_FUNCTIONS(PBE2PARAM)
75 
76 ASN1_SEQUENCE(PBKDF2PARAM) = {
77 	ASN1_SIMPLE(PBKDF2PARAM, salt, ASN1_ANY),
78 	ASN1_SIMPLE(PBKDF2PARAM, iter, ASN1_INTEGER),
79 	ASN1_OPT(PBKDF2PARAM, keylength, ASN1_INTEGER),
80 	ASN1_OPT(PBKDF2PARAM, prf, X509_ALGOR)
81 } ASN1_SEQUENCE_END(PBKDF2PARAM)
82 
83 IMPLEMENT_ASN1_FUNCTIONS(PBKDF2PARAM)
84 
85 /* Return an algorithm identifier for a PKCS#5 v2.0 PBE algorithm:
86  * yes I know this is horrible!
87  *
88  * Extended version to allow application supplied PRF NID and IV.
89  */
90 
91 X509_ALGOR *
92 PKCS5_pbe2_set_iv(const EVP_CIPHER *cipher, int iter, unsigned char *salt,
93     int saltlen, unsigned char *aiv, int prf_nid)
94 {
95 	X509_ALGOR *scheme = NULL, *kalg = NULL, *ret = NULL;
96 	int alg_nid, keylen;
97 	EVP_CIPHER_CTX ctx;
98 	unsigned char iv[EVP_MAX_IV_LENGTH];
99 	PBE2PARAM *pbe2 = NULL;
100 	ASN1_OBJECT *obj;
101 
102 	alg_nid = EVP_CIPHER_type(cipher);
103 	if (alg_nid == NID_undef) {
104 		ASN1err(ASN1_F_PKCS5_PBE2_SET_IV,
105 		ASN1_R_CIPHER_HAS_NO_OBJECT_IDENTIFIER);
106 		goto err;
107 	}
108 	obj = OBJ_nid2obj(alg_nid);
109 
110 	if (!(pbe2 = PBE2PARAM_new()))
111 		goto merr;
112 
113 	/* Setup the AlgorithmIdentifier for the encryption scheme */
114 	scheme = pbe2->encryption;
115 
116 	scheme->algorithm = obj;
117 	if (!(scheme->parameter = ASN1_TYPE_new()))
118 		goto merr;
119 
120 	/* Create random IV */
121 	if (EVP_CIPHER_iv_length(cipher)) {
122 		if (aiv)
123 			memcpy(iv, aiv, EVP_CIPHER_iv_length(cipher));
124 		else if (RAND_pseudo_bytes(iv,
125 		    EVP_CIPHER_iv_length(cipher)) < 0)
126 			goto err;
127 	}
128 
129 	EVP_CIPHER_CTX_init(&ctx);
130 
131 	/* Dummy cipherinit to just setup the IV, and PRF */
132 	if (!EVP_CipherInit_ex(&ctx, cipher, NULL, NULL, iv, 0))
133 		goto err;
134 	if (EVP_CIPHER_param_to_asn1(&ctx, scheme->parameter) < 0) {
135 		ASN1err(ASN1_F_PKCS5_PBE2_SET_IV,
136 		ASN1_R_ERROR_SETTING_CIPHER_PARAMS);
137 		EVP_CIPHER_CTX_cleanup(&ctx);
138 		goto err;
139 	}
140 	/* If prf NID unspecified see if cipher has a preference.
141 	 * An error is OK here: just means use default PRF.
142 	 */
143 	if ((prf_nid == -1) &&
144 	    EVP_CIPHER_CTX_ctrl(&ctx, EVP_CTRL_PBE_PRF_NID, 0, &prf_nid) <= 0) {
145 		ERR_clear_error();
146 		prf_nid = NID_hmacWithSHA1;
147 	}
148 	EVP_CIPHER_CTX_cleanup(&ctx);
149 
150 	/* If its RC2 then we'd better setup the key length */
151 
152 	if (alg_nid == NID_rc2_cbc)
153 		keylen = EVP_CIPHER_key_length(cipher);
154 	else
155 		keylen = -1;
156 
157 	/* Setup keyfunc */
158 
159 	X509_ALGOR_free(pbe2->keyfunc);
160 
161 	pbe2->keyfunc = PKCS5_pbkdf2_set(iter, salt, saltlen, prf_nid, keylen);
162 
163 	if (!pbe2->keyfunc)
164 		goto merr;
165 
166 	/* Now set up top level AlgorithmIdentifier */
167 
168 	if (!(ret = X509_ALGOR_new()))
169 		goto merr;
170 	if (!(ret->parameter = ASN1_TYPE_new()))
171 		goto merr;
172 
173 	ret->algorithm = OBJ_nid2obj(NID_pbes2);
174 
175 	/* Encode PBE2PARAM into parameter */
176 
177 	if (!ASN1_item_pack(pbe2, ASN1_ITEM_rptr(PBE2PARAM),
178 		&ret->parameter->value.sequence)) goto merr;
179 	ret->parameter->type = V_ASN1_SEQUENCE;
180 
181 	PBE2PARAM_free(pbe2);
182 	pbe2 = NULL;
183 
184 	return ret;
185 
186 merr:
187 	ASN1err(ASN1_F_PKCS5_PBE2_SET_IV, ERR_R_MALLOC_FAILURE);
188 
189 err:
190 	PBE2PARAM_free(pbe2);
191 	/* Note 'scheme' is freed as part of pbe2 */
192 	X509_ALGOR_free(kalg);
193 	X509_ALGOR_free(ret);
194 
195 	return NULL;
196 }
197 
198 X509_ALGOR *
199 PKCS5_pbe2_set(const EVP_CIPHER *cipher, int iter, unsigned char *salt,
200     int saltlen)
201 {
202 	return PKCS5_pbe2_set_iv(cipher, iter, salt, saltlen, NULL, -1);
203 }
204 
205 X509_ALGOR *
206 PKCS5_pbkdf2_set(int iter, unsigned char *salt, int saltlen, int prf_nid,
207     int keylen)
208 {
209 	X509_ALGOR *keyfunc = NULL;
210 	PBKDF2PARAM *kdf = NULL;
211 	ASN1_OCTET_STRING *osalt = NULL;
212 
213 	if (!(kdf = PBKDF2PARAM_new()))
214 		goto merr;
215 	if (!(osalt = M_ASN1_OCTET_STRING_new()))
216 		goto merr;
217 
218 	kdf->salt->value.octet_string = osalt;
219 	kdf->salt->type = V_ASN1_OCTET_STRING;
220 
221 	if (!saltlen)
222 		saltlen = PKCS5_SALT_LEN;
223 	if (!(osalt->data = malloc (saltlen)))
224 		goto merr;
225 
226 	osalt->length = saltlen;
227 
228 	if (salt)
229 		memcpy (osalt->data, salt, saltlen);
230 	else if (RAND_pseudo_bytes (osalt->data, saltlen) < 0)
231 		goto merr;
232 
233 	if (iter <= 0)
234 		iter = PKCS5_DEFAULT_ITER;
235 
236 	if (!ASN1_INTEGER_set(kdf->iter, iter))
237 		goto merr;
238 
239 	/* If have a key len set it up */
240 
241 	if (keylen > 0) {
242 		if (!(kdf->keylength = M_ASN1_INTEGER_new()))
243 			goto merr;
244 		if (!ASN1_INTEGER_set (kdf->keylength, keylen))
245 			goto merr;
246 	}
247 
248 	/* prf can stay NULL if we are using hmacWithSHA1 */
249 	if (prf_nid > 0 && prf_nid != NID_hmacWithSHA1) {
250 		kdf->prf = X509_ALGOR_new();
251 		if (!kdf->prf)
252 			goto merr;
253 		X509_ALGOR_set0(kdf->prf, OBJ_nid2obj(prf_nid),
254 		V_ASN1_NULL, NULL);
255 	}
256 
257 	/* Finally setup the keyfunc structure */
258 
259 	keyfunc = X509_ALGOR_new();
260 	if (!keyfunc)
261 		goto merr;
262 
263 	keyfunc->algorithm = OBJ_nid2obj(NID_id_pbkdf2);
264 
265 	/* Encode PBKDF2PARAM into parameter of pbe2 */
266 
267 	if (!(keyfunc->parameter = ASN1_TYPE_new()))
268 		goto merr;
269 
270 	if (!ASN1_item_pack(kdf, ASN1_ITEM_rptr(PBKDF2PARAM),
271 		&keyfunc->parameter->value.sequence))
272 		goto merr;
273 	keyfunc->parameter->type = V_ASN1_SEQUENCE;
274 
275 	PBKDF2PARAM_free(kdf);
276 	return keyfunc;
277 
278 merr:
279 	ASN1err(ASN1_F_PKCS5_PBKDF2_SET, ERR_R_MALLOC_FAILURE);
280 	PBKDF2PARAM_free(kdf);
281 	X509_ALGOR_free(keyfunc);
282 	return NULL;
283 }
284