GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: lib/libcrypto/crypto/../../libssl/src/crypto/evp/evp_pbe.c Lines: 4 87 4.6 %
Date: 2016-12-06 Branches: 0 50 0.0 %

Line Branch Exec Source
1
/* $OpenBSD: evp_pbe.c,v 1.23 2015/02/08 22:20:18 miod Exp $ */
2
/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3
 * project 1999.
4
 */
5
/* ====================================================================
6
 * Copyright (c) 1999-2006 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/opensslconf.h>
63
64
#include <openssl/err.h>
65
#include <openssl/evp.h>
66
#include <openssl/pkcs12.h>
67
#include <openssl/x509.h>
68
69
#include "evp_locl.h"
70
71
/* Password based encryption (PBE) functions */
72
73
DECLARE_STACK_OF(EVP_PBE_CTL)
74
static STACK_OF(EVP_PBE_CTL) *pbe_algs;
75
76
/* Setup a cipher context from a PBE algorithm */
77
78
typedef struct {
79
	int pbe_type;
80
	int pbe_nid;
81
	int cipher_nid;
82
	int md_nid;
83
	EVP_PBE_KEYGEN *keygen;
84
} EVP_PBE_CTL;
85
86
static const EVP_PBE_CTL builtin_pbe[] = {
87
	{EVP_PBE_TYPE_OUTER, NID_pbeWithMD2AndDES_CBC, NID_des_cbc, NID_md2, PKCS5_PBE_keyivgen},
88
	{EVP_PBE_TYPE_OUTER, NID_pbeWithMD5AndDES_CBC, NID_des_cbc, NID_md5, PKCS5_PBE_keyivgen},
89
	{EVP_PBE_TYPE_OUTER, NID_pbeWithSHA1AndRC2_CBC, NID_rc2_64_cbc, NID_sha1, PKCS5_PBE_keyivgen},
90
91
#ifndef OPENSSL_NO_HMAC
92
	{EVP_PBE_TYPE_OUTER, NID_id_pbkdf2, -1, -1, PKCS5_v2_PBKDF2_keyivgen},
93
#endif
94
95
	{EVP_PBE_TYPE_OUTER, NID_pbe_WithSHA1And128BitRC4, NID_rc4, NID_sha1, PKCS12_PBE_keyivgen},
96
	{EVP_PBE_TYPE_OUTER, NID_pbe_WithSHA1And40BitRC4, NID_rc4_40, NID_sha1, PKCS12_PBE_keyivgen},
97
	{EVP_PBE_TYPE_OUTER, NID_pbe_WithSHA1And3_Key_TripleDES_CBC, NID_des_ede3_cbc, NID_sha1, PKCS12_PBE_keyivgen},
98
	{EVP_PBE_TYPE_OUTER, NID_pbe_WithSHA1And2_Key_TripleDES_CBC, NID_des_ede_cbc, NID_sha1, PKCS12_PBE_keyivgen},
99
	{EVP_PBE_TYPE_OUTER, NID_pbe_WithSHA1And128BitRC2_CBC, NID_rc2_cbc, NID_sha1, PKCS12_PBE_keyivgen},
100
	{EVP_PBE_TYPE_OUTER, NID_pbe_WithSHA1And40BitRC2_CBC, NID_rc2_40_cbc, NID_sha1, PKCS12_PBE_keyivgen},
101
102
#ifndef OPENSSL_NO_HMAC
103
	{EVP_PBE_TYPE_OUTER, NID_pbes2, -1, -1, PKCS5_v2_PBE_keyivgen},
104
#endif
105
	{EVP_PBE_TYPE_OUTER, NID_pbeWithMD2AndRC2_CBC, NID_rc2_64_cbc, NID_md2, PKCS5_PBE_keyivgen},
106
	{EVP_PBE_TYPE_OUTER, NID_pbeWithMD5AndRC2_CBC, NID_rc2_64_cbc, NID_md5, PKCS5_PBE_keyivgen},
107
	{EVP_PBE_TYPE_OUTER, NID_pbeWithSHA1AndDES_CBC, NID_des_cbc, NID_sha1, PKCS5_PBE_keyivgen},
108
109
110
	{EVP_PBE_TYPE_PRF, NID_hmacWithSHA1, -1, NID_sha1, 0},
111
	{EVP_PBE_TYPE_PRF, NID_hmacWithMD5, -1, NID_md5, 0},
112
	{EVP_PBE_TYPE_PRF, NID_hmacWithSHA224, -1, NID_sha224, 0},
113
	{EVP_PBE_TYPE_PRF, NID_hmacWithSHA256, -1, NID_sha256, 0},
114
	{EVP_PBE_TYPE_PRF, NID_hmacWithSHA384, -1, NID_sha384, 0},
115
	{EVP_PBE_TYPE_PRF, NID_hmacWithSHA512, -1, NID_sha512, 0},
116
	{EVP_PBE_TYPE_PRF, NID_id_HMACGostR3411_94, -1, NID_id_GostR3411_94, 0},
117
};
118
119
int
120
EVP_PBE_CipherInit(ASN1_OBJECT *pbe_obj, const char *pass, int passlen,
121
    ASN1_TYPE *param, EVP_CIPHER_CTX *ctx, int en_de)
122
{
123
	const EVP_CIPHER *cipher;
124
	const EVP_MD *md;
125
	int cipher_nid, md_nid;
126
	EVP_PBE_KEYGEN *keygen;
127
128
	if (!EVP_PBE_find(EVP_PBE_TYPE_OUTER, OBJ_obj2nid(pbe_obj),
129
	    &cipher_nid, &md_nid, &keygen)) {
130
		char obj_tmp[80];
131
		EVPerr(EVP_F_EVP_PBE_CIPHERINIT, EVP_R_UNKNOWN_PBE_ALGORITHM);
132
		if (!pbe_obj)
133
			strlcpy(obj_tmp, "NULL", sizeof obj_tmp);
134
		else
135
			i2t_ASN1_OBJECT(obj_tmp, sizeof obj_tmp, pbe_obj);
136
		ERR_asprintf_error_data("TYPE=%s", obj_tmp);
137
		return 0;
138
	}
139
140
	if (!pass)
141
		passlen = 0;
142
	else if (passlen == -1)
143
		passlen = strlen(pass);
144
145
	if (cipher_nid == -1)
146
		cipher = NULL;
147
	else {
148
		cipher = EVP_get_cipherbynid(cipher_nid);
149
		if (!cipher) {
150
			EVPerr(EVP_F_EVP_PBE_CIPHERINIT, EVP_R_UNKNOWN_CIPHER);
151
			return 0;
152
		}
153
	}
154
155
	if (md_nid == -1)
156
		md = NULL;
157
	else {
158
		md = EVP_get_digestbynid(md_nid);
159
		if (!md) {
160
			EVPerr(EVP_F_EVP_PBE_CIPHERINIT, EVP_R_UNKNOWN_DIGEST);
161
			return 0;
162
		}
163
	}
164
165
	if (!keygen(ctx, pass, passlen, param, cipher, md, en_de)) {
166
		EVPerr(EVP_F_EVP_PBE_CIPHERINIT, EVP_R_KEYGEN_FAILURE);
167
		return 0;
168
	}
169
	return 1;
170
}
171
172
DECLARE_OBJ_BSEARCH_CMP_FN(EVP_PBE_CTL, EVP_PBE_CTL, pbe2);
173
174
static int
175
pbe2_cmp(const EVP_PBE_CTL *pbe1, const EVP_PBE_CTL *pbe2)
176
{
177
	int ret = pbe1->pbe_type - pbe2->pbe_type;
178
179
	if (ret)
180
		return ret;
181
	else
182
		return pbe1->pbe_nid - pbe2->pbe_nid;
183
}
184
185
IMPLEMENT_OBJ_BSEARCH_CMP_FN(EVP_PBE_CTL, EVP_PBE_CTL, pbe2);
186
187
static int
188
pbe_cmp(const EVP_PBE_CTL * const *a, const EVP_PBE_CTL * const *b)
189
{
190
	int ret = (*a)->pbe_type - (*b)->pbe_type;
191
192
	if (ret)
193
		return ret;
194
	else
195
		return (*a)->pbe_nid - (*b)->pbe_nid;
196
}
197
198
/* Add a PBE algorithm */
199
200
int
201
EVP_PBE_alg_add_type(int pbe_type, int pbe_nid, int cipher_nid, int md_nid,
202
    EVP_PBE_KEYGEN *keygen)
203
{
204
	EVP_PBE_CTL *pbe_tmp;
205
206
	if (pbe_algs == NULL) {
207
		pbe_algs = sk_EVP_PBE_CTL_new(pbe_cmp);
208
		if (pbe_algs == NULL) {
209
			EVPerr(EVP_F_EVP_PBE_ALG_ADD_TYPE,
210
			    ERR_R_MALLOC_FAILURE);
211
			return 0;
212
		}
213
	}
214
	pbe_tmp = malloc(sizeof(EVP_PBE_CTL));
215
	if (pbe_tmp == NULL) {
216
		EVPerr(EVP_F_EVP_PBE_ALG_ADD_TYPE, ERR_R_MALLOC_FAILURE);
217
		return 0;
218
	}
219
	pbe_tmp->pbe_type = pbe_type;
220
	pbe_tmp->pbe_nid = pbe_nid;
221
	pbe_tmp->cipher_nid = cipher_nid;
222
	pbe_tmp->md_nid = md_nid;
223
	pbe_tmp->keygen = keygen;
224
225
	if (sk_EVP_PBE_CTL_push(pbe_algs, pbe_tmp) == 0) {
226
		free(pbe_tmp);
227
		EVPerr(EVP_F_EVP_PBE_ALG_ADD_TYPE, ERR_R_MALLOC_FAILURE);
228
		return 0;
229
	}
230
	return 1;
231
}
232
233
int
234
EVP_PBE_alg_add(int nid, const EVP_CIPHER *cipher, const EVP_MD *md,
235
    EVP_PBE_KEYGEN *keygen)
236
{
237
	int cipher_nid, md_nid;
238
239
	if (cipher)
240
		cipher_nid = EVP_CIPHER_nid(cipher);
241
	else
242
		cipher_nid = -1;
243
	if (md)
244
		md_nid = EVP_MD_type(md);
245
	else
246
		md_nid = -1;
247
248
	return EVP_PBE_alg_add_type(EVP_PBE_TYPE_OUTER, nid,
249
	    cipher_nid, md_nid, keygen);
250
}
251
252
int
253
EVP_PBE_find(int type, int pbe_nid,
254
    int *pcnid, int *pmnid, EVP_PBE_KEYGEN **pkeygen)
255
{
256
	EVP_PBE_CTL *pbetmp = NULL, pbelu;
257
	int i;
258
	if (pbe_nid == NID_undef)
259
		return 0;
260
261
	pbelu.pbe_type = type;
262
	pbelu.pbe_nid = pbe_nid;
263
264
	if (pbe_algs) {
265
		i = sk_EVP_PBE_CTL_find(pbe_algs, &pbelu);
266
		if (i != -1)
267
			pbetmp = sk_EVP_PBE_CTL_value (pbe_algs, i);
268
	}
269
	if (pbetmp == NULL) {
270
		pbetmp = OBJ_bsearch_pbe2(&pbelu, builtin_pbe,
271
		    sizeof(builtin_pbe)/sizeof(EVP_PBE_CTL));
272
	}
273
	if (pbetmp == NULL)
274
		return 0;
275
	if (pcnid)
276
		*pcnid = pbetmp->cipher_nid;
277
	if (pmnid)
278
		*pmnid = pbetmp->md_nid;
279
	if (pkeygen)
280
		*pkeygen = pbetmp->keygen;
281
	return 1;
282
}
283
284
static void
285
free_evp_pbe_ctl(EVP_PBE_CTL *pbe)
286
{
287
	free(pbe);
288
}
289
290
void
291
EVP_PBE_cleanup(void)
292
10
{
293
10
	sk_EVP_PBE_CTL_pop_free(pbe_algs, free_evp_pbe_ctl);
294
10
	pbe_algs = NULL;
295
10
}