GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: lib/libcrypto/evp/evp_pbe.c Lines: 46 93 49.5 %
Date: 2017-11-13 Branches: 22 50 44.0 %

Line Branch Exec Source
1
/* $OpenBSD: evp_pbe.c,v 1.25 2017/01/29 17:49:23 beck 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
100
	int cipher_nid, md_nid;
126
50
	EVP_PBE_KEYGEN *keygen;
127
128
50
	if (!EVP_PBE_find(EVP_PBE_TYPE_OUTER, OBJ_obj2nid(pbe_obj),
129
	    &cipher_nid, &md_nid, &keygen)) {
130
		char obj_tmp[80];
131
		EVPerror(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
50
	if (!pass)
141
		passlen = 0;
142
50
	else if (passlen == -1)
143
10
		passlen = strlen(pass);
144
145
50
	if (cipher_nid == -1)
146
46
		cipher = NULL;
147
	else {
148
4
		cipher = EVP_get_cipherbynid(cipher_nid);
149
4
		if (!cipher) {
150
			EVPerror(EVP_R_UNKNOWN_CIPHER);
151
			return 0;
152
		}
153
	}
154
155
50
	if (md_nid == -1)
156
46
		md = NULL;
157
	else {
158
4
		md = EVP_get_digestbynid(md_nid);
159
4
		if (!md) {
160
			EVPerror(EVP_R_UNKNOWN_DIGEST);
161
			return 0;
162
		}
163
	}
164
165
50
	if (!keygen(ctx, pass, passlen, param, cipher, md, en_de)) {
166
		EVPerror(EVP_R_KEYGEN_FAILURE);
167
		return 0;
168
	}
169
50
	return 1;
170
50
}
171
172
static int pbe2_cmp_BSEARCH_CMP_FN(const void *, const void *);
173
static int pbe2_cmp(EVP_PBE_CTL const *, EVP_PBE_CTL const *);
174
static EVP_PBE_CTL *OBJ_bsearch_pbe2(EVP_PBE_CTL *key, EVP_PBE_CTL const *base, int num);
175
176
static int
177
pbe2_cmp(const EVP_PBE_CTL *pbe1, const EVP_PBE_CTL *pbe2)
178
{
179
576
	int ret = pbe1->pbe_type - pbe2->pbe_type;
180
181
288
	if (ret)
182
96
		return ret;
183
	else
184
192
		return pbe1->pbe_nid - pbe2->pbe_nid;
185
288
}
186
187
188
static int
189
pbe2_cmp_BSEARCH_CMP_FN(const void *a_, const void *b_)
190
{
191
576
	EVP_PBE_CTL const *a = a_;
192
288
	EVP_PBE_CTL const *b = b_;
193
288
	return pbe2_cmp(a, b);
194
}
195
196
static EVP_PBE_CTL *
197
OBJ_bsearch_pbe2(EVP_PBE_CTL *key, EVP_PBE_CTL const *base, int num)
198
{
199
192
	return (EVP_PBE_CTL *)OBJ_bsearch_(key, base, num, sizeof(EVP_PBE_CTL),
200
	    pbe2_cmp_BSEARCH_CMP_FN);
201
}
202
203
static int
204
pbe_cmp(const EVP_PBE_CTL * const *a, const EVP_PBE_CTL * const *b)
205
{
206
	int ret = (*a)->pbe_type - (*b)->pbe_type;
207
208
	if (ret)
209
		return ret;
210
	else
211
		return (*a)->pbe_nid - (*b)->pbe_nid;
212
}
213
214
/* Add a PBE algorithm */
215
216
int
217
EVP_PBE_alg_add_type(int pbe_type, int pbe_nid, int cipher_nid, int md_nid,
218
    EVP_PBE_KEYGEN *keygen)
219
{
220
	EVP_PBE_CTL *pbe_tmp;
221
222
	if (pbe_algs == NULL) {
223
		pbe_algs = sk_EVP_PBE_CTL_new(pbe_cmp);
224
		if (pbe_algs == NULL) {
225
			EVPerror(ERR_R_MALLOC_FAILURE);
226
			return 0;
227
		}
228
	}
229
	pbe_tmp = malloc(sizeof(EVP_PBE_CTL));
230
	if (pbe_tmp == NULL) {
231
		EVPerror(ERR_R_MALLOC_FAILURE);
232
		return 0;
233
	}
234
	pbe_tmp->pbe_type = pbe_type;
235
	pbe_tmp->pbe_nid = pbe_nid;
236
	pbe_tmp->cipher_nid = cipher_nid;
237
	pbe_tmp->md_nid = md_nid;
238
	pbe_tmp->keygen = keygen;
239
240
	if (sk_EVP_PBE_CTL_push(pbe_algs, pbe_tmp) == 0) {
241
		free(pbe_tmp);
242
		EVPerror(ERR_R_MALLOC_FAILURE);
243
		return 0;
244
	}
245
	return 1;
246
}
247
248
int
249
EVP_PBE_alg_add(int nid, const EVP_CIPHER *cipher, const EVP_MD *md,
250
    EVP_PBE_KEYGEN *keygen)
251
{
252
	int cipher_nid, md_nid;
253
254
	if (cipher)
255
		cipher_nid = EVP_CIPHER_nid(cipher);
256
	else
257
		cipher_nid = -1;
258
	if (md)
259
		md_nid = EVP_MD_type(md);
260
	else
261
		md_nid = -1;
262
263
	return EVP_PBE_alg_add_type(EVP_PBE_TYPE_OUTER, nid,
264
	    cipher_nid, md_nid, keygen);
265
}
266
267
int
268
EVP_PBE_find(int type, int pbe_nid,
269
    int *pcnid, int *pmnid, EVP_PBE_KEYGEN **pkeygen)
270
{
271
192
	EVP_PBE_CTL *pbetmp = NULL, pbelu;
272
	int i;
273
96
	if (pbe_nid == NID_undef)
274
		return 0;
275
276
96
	pbelu.pbe_type = type;
277
96
	pbelu.pbe_nid = pbe_nid;
278
279
96
	if (pbe_algs) {
280
		i = sk_EVP_PBE_CTL_find(pbe_algs, &pbelu);
281
		if (i != -1)
282
			pbetmp = sk_EVP_PBE_CTL_value (pbe_algs, i);
283
	}
284
96
	if (pbetmp == NULL) {
285
96
		pbetmp = OBJ_bsearch_pbe2(&pbelu, builtin_pbe,
286
		    sizeof(builtin_pbe)/sizeof(EVP_PBE_CTL));
287
96
	}
288
96
	if (pbetmp == NULL)
289
		return 0;
290
96
	if (pcnid)
291
50
		*pcnid = pbetmp->cipher_nid;
292
96
	if (pmnid)
293
96
		*pmnid = pbetmp->md_nid;
294
96
	if (pkeygen)
295
50
		*pkeygen = pbetmp->keygen;
296
96
	return 1;
297
96
}
298
299
static void
300
free_evp_pbe_ctl(EVP_PBE_CTL *pbe)
301
{
302
	free(pbe);
303
}
304
305
void
306
EVP_PBE_cleanup(void)
307
{
308
3136
	sk_EVP_PBE_CTL_pop_free(pbe_algs, free_evp_pbe_ctl);
309
1568
	pbe_algs = NULL;
310
1568
}