]>
Commit | Line | Data |
---|---|---|
0f113f3e | 1 | /* |
28428130 | 2 | * Copyright 2001-2018 The OpenSSL Project Authors. All Rights Reserved. |
aa8f3d76 | 3 | * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved |
5270e702 | 4 | * |
3c120f91 | 5 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
b1322259 RS |
6 | * this file except in compliance with the License. You can obtain a copy |
7 | * in the file LICENSE in the source distribution or at | |
8 | * https://www.openssl.org/source/license.html | |
5270e702 | 9 | */ |
b1322259 | 10 | |
5270e702 RL |
11 | #include <stdio.h> |
12 | #include <openssl/crypto.h> | |
b39fc560 | 13 | #include "internal/cryptlib.h" |
25f2138b | 14 | #include "crypto/engine.h" |
0dc09233 | 15 | #include <openssl/pem.h> |
67753262 | 16 | #include <openssl/evp.h> |
3a87a9b9 | 17 | #include <openssl/rand.h> |
3c27208f RS |
18 | #include <openssl/rsa.h> |
19 | #include <openssl/dsa.h> | |
20 | #include <openssl/dh.h> | |
5270e702 | 21 | |
e77906b9 DSH |
22 | #include <openssl/hmac.h> |
23 | #include <openssl/x509v3.h> | |
24 | ||
0f113f3e MC |
25 | /* |
26 | * This testing gunk is implemented (and explained) lower down. It also | |
27 | * assumes the application explicitly calls "ENGINE_load_openssl()" because | |
28 | * this is no longer automatic in ENGINE_load_builtin_engines(). | |
29 | */ | |
3b04cdd7 | 30 | #define TEST_ENG_OPENSSL_RC4 |
984d6c60 | 31 | #ifndef OPENSSL_NO_STDIO |
9f643f54 | 32 | # define TEST_ENG_OPENSSL_PKEY |
984d6c60 | 33 | #endif |
e77906b9 | 34 | /* #define TEST_ENG_OPENSSL_HMAC */ |
fa71cc7b | 35 | /* #define TEST_ENG_OPENSSL_HMAC_INIT */ |
3b04cdd7 | 36 | /* #define TEST_ENG_OPENSSL_RC4_OTHERS */ |
9f643f54 RL |
37 | #ifndef OPENSSL_NO_STDIO |
38 | # define TEST_ENG_OPENSSL_RC4_P_INIT | |
39 | #endif | |
3b04cdd7 GT |
40 | /* #define TEST_ENG_OPENSSL_RC4_P_CIPHER */ |
41 | #define TEST_ENG_OPENSSL_SHA | |
3b04cdd7 GT |
42 | /* #define TEST_ENG_OPENSSL_SHA_OTHERS */ |
43 | /* #define TEST_ENG_OPENSSL_SHA_P_INIT */ | |
44 | /* #define TEST_ENG_OPENSSL_SHA_P_UPDATE */ | |
45 | /* #define TEST_ENG_OPENSSL_SHA_P_FINAL */ | |
46 | ||
439ae4d3 RL |
47 | /* Now check what of those algorithms are actually enabled */ |
48 | #ifdef OPENSSL_NO_RC4 | |
0f113f3e MC |
49 | # undef TEST_ENG_OPENSSL_RC4 |
50 | # undef TEST_ENG_OPENSSL_RC4_OTHERS | |
51 | # undef TEST_ENG_OPENSSL_RC4_P_INIT | |
52 | # undef TEST_ENG_OPENSSL_RC4_P_CIPHER | |
439ae4d3 | 53 | #endif |
439ae4d3 | 54 | |
cddcea8c RL |
55 | static int openssl_destroy(ENGINE *e); |
56 | ||
3b04cdd7 GT |
57 | #ifdef TEST_ENG_OPENSSL_RC4 |
58 | static int openssl_ciphers(ENGINE *e, const EVP_CIPHER **cipher, | |
0f113f3e | 59 | const int **nids, int nid); |
3b04cdd7 GT |
60 | #endif |
61 | #ifdef TEST_ENG_OPENSSL_SHA | |
62 | static int openssl_digests(ENGINE *e, const EVP_MD **digest, | |
0f113f3e | 63 | const int **nids, int nid); |
3b04cdd7 GT |
64 | #endif |
65 | ||
0dc09233 DSH |
66 | #ifdef TEST_ENG_OPENSSL_PKEY |
67 | static EVP_PKEY *openssl_load_privkey(ENGINE *eng, const char *key_id, | |
0f113f3e MC |
68 | UI_METHOD *ui_method, |
69 | void *callback_data); | |
0dc09233 DSH |
70 | #endif |
71 | ||
e77906b9 DSH |
72 | #ifdef TEST_ENG_OPENSSL_HMAC |
73 | static int ossl_register_hmac_meth(void); | |
74 | static int ossl_pkey_meths(ENGINE *e, EVP_PKEY_METHOD **pmeth, | |
0f113f3e | 75 | const int **nids, int nid); |
e77906b9 DSH |
76 | #endif |
77 | ||
59bc3126 GT |
78 | /* The constants used when creating the ENGINE */ |
79 | static const char *engine_openssl_id = "openssl"; | |
b6d1e52d | 80 | static const char *engine_openssl_name = "Software engine support"; |
5270e702 | 81 | |
0f113f3e MC |
82 | /* |
83 | * This internal function is used by ENGINE_openssl() and possibly by the | |
84 | * "dynamic" ENGINE support too | |
85 | */ | |
329636d6 | 86 | static int bind_helper(ENGINE *e) |
0f113f3e MC |
87 | { |
88 | if (!ENGINE_set_id(e, engine_openssl_id) | |
89 | || !ENGINE_set_name(e, engine_openssl_name) | |
cddcea8c | 90 | || !ENGINE_set_destroy_function(e, openssl_destroy) |
0dc09233 | 91 | #ifndef TEST_ENG_OPENSSL_NO_ALGORITHMS |
0f113f3e MC |
92 | # ifndef OPENSSL_NO_RSA |
93 | || !ENGINE_set_RSA(e, RSA_get_default_method()) | |
94 | # endif | |
95 | # ifndef OPENSSL_NO_DSA | |
96 | || !ENGINE_set_DSA(e, DSA_get_default_method()) | |
97 | # endif | |
10bf4fc2 | 98 | # ifndef OPENSSL_NO_EC |
77d0d10d | 99 | || !ENGINE_set_EC(e, EC_KEY_OpenSSL()) |
0f113f3e MC |
100 | # endif |
101 | # ifndef OPENSSL_NO_DH | |
102 | || !ENGINE_set_DH(e, DH_get_default_method()) | |
103 | # endif | |
b0700d2c | 104 | || !ENGINE_set_RAND(e, RAND_OpenSSL()) |
0f113f3e MC |
105 | # ifdef TEST_ENG_OPENSSL_RC4 |
106 | || !ENGINE_set_ciphers(e, openssl_ciphers) | |
107 | # endif | |
108 | # ifdef TEST_ENG_OPENSSL_SHA | |
109 | || !ENGINE_set_digests(e, openssl_digests) | |
110 | # endif | |
0dc09233 DSH |
111 | #endif |
112 | #ifdef TEST_ENG_OPENSSL_PKEY | |
0f113f3e | 113 | || !ENGINE_set_load_privkey_function(e, openssl_load_privkey) |
e77906b9 DSH |
114 | #endif |
115 | #ifdef TEST_ENG_OPENSSL_HMAC | |
0f113f3e MC |
116 | || !ossl_register_hmac_meth() |
117 | || !ENGINE_set_pkey_meths(e, ossl_pkey_meths) | |
9e78e6c3 | 118 | #endif |
0f113f3e MC |
119 | ) |
120 | return 0; | |
121 | /* | |
122 | * If we add errors to this ENGINE, ensure the error handling is setup | |
123 | * here | |
124 | */ | |
125 | /* openssl_load_error_strings(); */ | |
126 | return 1; | |
127 | } | |
329636d6 GT |
128 | |
129 | static ENGINE *engine_openssl(void) | |
0f113f3e MC |
130 | { |
131 | ENGINE *ret = ENGINE_new(); | |
90945fa3 | 132 | if (ret == NULL) |
0f113f3e MC |
133 | return NULL; |
134 | if (!bind_helper(ret)) { | |
135 | ENGINE_free(ret); | |
136 | return NULL; | |
137 | } | |
138 | return ret; | |
139 | } | |
3b04cdd7 | 140 | |
b3599dbb | 141 | void engine_load_openssl_int(void) |
0f113f3e MC |
142 | { |
143 | ENGINE *toadd = engine_openssl(); | |
144 | if (!toadd) | |
145 | return; | |
146 | ENGINE_add(toadd); | |
147 | /* | |
148 | * If the "add" worked, it gets a structural reference. So either way, we | |
149 | * release our just-created reference. | |
150 | */ | |
151 | ENGINE_free(toadd); | |
152 | ERR_clear_error(); | |
153 | } | |
154 | ||
155 | /* | |
156 | * This stuff is needed if this ENGINE is being compiled into a | |
157 | * self-contained shared-library. | |
158 | */ | |
329636d6 GT |
159 | #ifdef ENGINE_DYNAMIC_SUPPORT |
160 | static int bind_fn(ENGINE *e, const char *id) | |
0f113f3e MC |
161 | { |
162 | if (id && (strcmp(id, engine_openssl_id) != 0)) | |
163 | return 0; | |
164 | if (!bind_helper(e)) | |
165 | return 0; | |
166 | return 1; | |
167 | } | |
329636d6 | 168 | |
0f113f3e MC |
169 | IMPLEMENT_DYNAMIC_CHECK_FN() |
170 | IMPLEMENT_DYNAMIC_BIND_FN(bind_fn) | |
171 | #endif /* ENGINE_DYNAMIC_SUPPORT */ | |
3b04cdd7 | 172 | #ifdef TEST_ENG_OPENSSL_RC4 |
3a83462d MC |
173 | /*- |
174 | * This section of code compiles an "alternative implementation" of two modes of | |
3b04cdd7 GT |
175 | * RC4 into this ENGINE. The result is that EVP_CIPHER operation for "rc4" |
176 | * should under normal circumstances go via this support rather than the default | |
177 | * EVP support. There are other symbols to tweak the testing; | |
3b04cdd7 | 178 | * TEST_ENC_OPENSSL_RC4_OTHERS - print a one line message to stderr each time |
004aa803 | 179 | * we're asked for a cipher we don't support (should not happen). |
3b04cdd7 GT |
180 | * TEST_ENG_OPENSSL_RC4_P_INIT - print a one line message to stderr each time |
181 | * the "init_key" handler is called. | |
182 | * TEST_ENG_OPENSSL_RC4_P_CIPHER - ditto for the "cipher" handler. | |
183 | */ | |
0f113f3e MC |
184 | # include <openssl/rc4.h> |
185 | # define TEST_RC4_KEY_SIZE 16 | |
3b04cdd7 | 186 | typedef struct { |
0f113f3e MC |
187 | unsigned char key[TEST_RC4_KEY_SIZE]; |
188 | RC4_KEY ks; | |
189 | } TEST_RC4_KEY; | |
44ab2dfd | 190 | # define test(ctx) ((TEST_RC4_KEY *)EVP_CIPHER_CTX_get_cipher_data(ctx)) |
3b04cdd7 | 191 | static int test_rc4_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, |
0f113f3e MC |
192 | const unsigned char *iv, int enc) |
193 | { | |
21d98569 P |
194 | const int n = EVP_CIPHER_CTX_key_length(ctx); |
195 | ||
0f113f3e MC |
196 | # ifdef TEST_ENG_OPENSSL_RC4_P_INIT |
197 | fprintf(stderr, "(TEST_ENG_OPENSSL_RC4) test_init_key() called\n"); | |
198 | # endif | |
21d98569 P |
199 | if (n <= 0) |
200 | return n; | |
201 | memcpy(&test(ctx)->key[0], key, n); | |
202 | RC4_set_key(&test(ctx)->ks, n, test(ctx)->key); | |
0f113f3e MC |
203 | return 1; |
204 | } | |
205 | ||
3b04cdd7 | 206 | static int test_rc4_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
0f113f3e MC |
207 | const unsigned char *in, size_t inl) |
208 | { | |
209 | # ifdef TEST_ENG_OPENSSL_RC4_P_CIPHER | |
210 | fprintf(stderr, "(TEST_ENG_OPENSSL_RC4) test_cipher() called\n"); | |
211 | # endif | |
212 | RC4(&test(ctx)->ks, inl, in, out); | |
213 | return 1; | |
214 | } | |
215 | ||
39e8d0ce RL |
216 | static EVP_CIPHER *r4_cipher = NULL; |
217 | static const EVP_CIPHER *test_r4_cipher(void) | |
218 | { | |
219 | if (r4_cipher == NULL) { | |
220 | EVP_CIPHER *cipher; | |
221 | ||
222 | if ((cipher = EVP_CIPHER_meth_new(NID_rc4, 1, TEST_RC4_KEY_SIZE)) == NULL | |
223 | || !EVP_CIPHER_meth_set_iv_length(cipher, 0) | |
224 | || !EVP_CIPHER_meth_set_flags(cipher, EVP_CIPH_VARIABLE_LENGTH) | |
225 | || !EVP_CIPHER_meth_set_init(cipher, test_rc4_init_key) | |
226 | || !EVP_CIPHER_meth_set_do_cipher(cipher, test_rc4_cipher) | |
227 | || !EVP_CIPHER_meth_set_impl_ctx_size(cipher, sizeof(TEST_RC4_KEY))) { | |
228 | EVP_CIPHER_meth_free(cipher); | |
229 | cipher = NULL; | |
230 | } | |
231 | r4_cipher = cipher; | |
232 | } | |
233 | return r4_cipher; | |
234 | } | |
235 | static void test_r4_cipher_destroy(void) | |
236 | { | |
237 | EVP_CIPHER_meth_free(r4_cipher); | |
238 | r4_cipher = NULL; | |
239 | } | |
240 | ||
241 | static EVP_CIPHER *r4_40_cipher = NULL; | |
242 | static const EVP_CIPHER *test_r4_40_cipher(void) | |
243 | { | |
244 | if (r4_40_cipher == NULL) { | |
245 | EVP_CIPHER *cipher; | |
246 | ||
247 | if ((cipher = EVP_CIPHER_meth_new(NID_rc4, 1, 5 /* 40 bits */)) == NULL | |
248 | || !EVP_CIPHER_meth_set_iv_length(cipher, 0) | |
249 | || !EVP_CIPHER_meth_set_flags(cipher, EVP_CIPH_VARIABLE_LENGTH) | |
250 | || !EVP_CIPHER_meth_set_init(cipher, test_rc4_init_key) | |
251 | || !EVP_CIPHER_meth_set_do_cipher(cipher, test_rc4_cipher) | |
252 | || !EVP_CIPHER_meth_set_impl_ctx_size(cipher, sizeof(TEST_RC4_KEY))) { | |
253 | EVP_CIPHER_meth_free(cipher); | |
254 | cipher = NULL; | |
255 | } | |
256 | r4_40_cipher = cipher; | |
257 | } | |
258 | return r4_40_cipher; | |
259 | } | |
260 | static void test_r4_40_cipher_destroy(void) | |
261 | { | |
262 | EVP_CIPHER_meth_free(r4_40_cipher); | |
263 | r4_40_cipher = NULL; | |
264 | } | |
265 | static int test_cipher_nids(const int **nids) | |
266 | { | |
1e4c66f9 | 267 | static int cipher_nids[4] = { 0, 0, 0, 0 }; |
39e8d0ce RL |
268 | static int pos = 0; |
269 | static int init = 0; | |
270 | ||
271 | if (!init) { | |
272 | const EVP_CIPHER *cipher; | |
273 | if ((cipher = test_r4_cipher()) != NULL) | |
274 | cipher_nids[pos++] = EVP_CIPHER_nid(cipher); | |
275 | if ((cipher = test_r4_40_cipher()) != NULL) | |
276 | cipher_nids[pos++] = EVP_CIPHER_nid(cipher); | |
277 | cipher_nids[pos] = 0; | |
278 | init = 1; | |
279 | } | |
280 | *nids = cipher_nids; | |
281 | return pos; | |
282 | } | |
0f113f3e | 283 | |
3b04cdd7 | 284 | static int openssl_ciphers(ENGINE *e, const EVP_CIPHER **cipher, |
0f113f3e MC |
285 | const int **nids, int nid) |
286 | { | |
287 | if (!cipher) { | |
288 | /* We are returning a list of supported nids */ | |
39e8d0ce | 289 | return test_cipher_nids(nids); |
0f113f3e MC |
290 | } |
291 | /* We are being asked for a specific cipher */ | |
292 | if (nid == NID_rc4) | |
39e8d0ce | 293 | *cipher = test_r4_cipher(); |
0f113f3e | 294 | else if (nid == NID_rc4_40) |
39e8d0ce | 295 | *cipher = test_r4_40_cipher(); |
0f113f3e MC |
296 | else { |
297 | # ifdef TEST_ENG_OPENSSL_RC4_OTHERS | |
298 | fprintf(stderr, "(TEST_ENG_OPENSSL_RC4) returning NULL for " | |
299 | "nid %d\n", nid); | |
300 | # endif | |
301 | *cipher = NULL; | |
302 | return 0; | |
303 | } | |
304 | return 1; | |
305 | } | |
3b04cdd7 GT |
306 | #endif |
307 | ||
308 | #ifdef TEST_ENG_OPENSSL_SHA | |
309 | /* Much the same sort of comment as for TEST_ENG_OPENSSL_RC4 */ | |
0f113f3e | 310 | # include <openssl/sha.h> |
0f113f3e | 311 | |
3b04cdd7 | 312 | static int test_sha1_init(EVP_MD_CTX *ctx) |
0f113f3e MC |
313 | { |
314 | # ifdef TEST_ENG_OPENSSL_SHA_P_INIT | |
315 | fprintf(stderr, "(TEST_ENG_OPENSSL_SHA) test_sha1_init() called\n"); | |
316 | # endif | |
6e59a892 | 317 | return SHA1_Init(EVP_MD_CTX_md_data(ctx)); |
0f113f3e MC |
318 | } |
319 | ||
320 | static int test_sha1_update(EVP_MD_CTX *ctx, const void *data, size_t count) | |
321 | { | |
322 | # ifdef TEST_ENG_OPENSSL_SHA_P_UPDATE | |
323 | fprintf(stderr, "(TEST_ENG_OPENSSL_SHA) test_sha1_update() called\n"); | |
324 | # endif | |
6e59a892 | 325 | return SHA1_Update(EVP_MD_CTX_md_data(ctx), data, count); |
0f113f3e MC |
326 | } |
327 | ||
328 | static int test_sha1_final(EVP_MD_CTX *ctx, unsigned char *md) | |
329 | { | |
330 | # ifdef TEST_ENG_OPENSSL_SHA_P_FINAL | |
331 | fprintf(stderr, "(TEST_ENG_OPENSSL_SHA) test_sha1_final() called\n"); | |
332 | # endif | |
6e59a892 | 333 | return SHA1_Final(md, EVP_MD_CTX_md_data(ctx)); |
0f113f3e MC |
334 | } |
335 | ||
cddcea8c RL |
336 | static EVP_MD *sha1_md = NULL; |
337 | static const EVP_MD *test_sha_md(void) | |
338 | { | |
339 | if (sha1_md == NULL) { | |
340 | EVP_MD *md; | |
341 | ||
342 | if ((md = EVP_MD_meth_new(NID_sha1, NID_sha1WithRSAEncryption)) == NULL | |
343 | || !EVP_MD_meth_set_result_size(md, SHA_DIGEST_LENGTH) | |
344 | || !EVP_MD_meth_set_input_blocksize(md, SHA_CBLOCK) | |
345 | || !EVP_MD_meth_set_app_datasize(md, | |
346 | sizeof(EVP_MD *) + sizeof(SHA_CTX)) | |
347 | || !EVP_MD_meth_set_flags(md, 0) | |
348 | || !EVP_MD_meth_set_init(md, test_sha1_init) | |
349 | || !EVP_MD_meth_set_update(md, test_sha1_update) | |
350 | || !EVP_MD_meth_set_final(md, test_sha1_final)) { | |
351 | EVP_MD_meth_free(md); | |
352 | md = NULL; | |
353 | } | |
354 | sha1_md = md; | |
355 | } | |
356 | return sha1_md; | |
357 | } | |
358 | static void test_sha_md_destroy(void) | |
359 | { | |
360 | EVP_MD_meth_free(sha1_md); | |
361 | sha1_md = NULL; | |
362 | } | |
363 | static int test_digest_nids(const int **nids) | |
364 | { | |
365 | static int digest_nids[2] = { 0, 0 }; | |
366 | static int pos = 0; | |
367 | static int init = 0; | |
368 | ||
369 | if (!init) { | |
370 | const EVP_MD *md; | |
371 | if ((md = test_sha_md()) != NULL) | |
372 | digest_nids[pos++] = EVP_MD_type(md); | |
373 | digest_nids[pos] = 0; | |
374 | init = 1; | |
375 | } | |
376 | *nids = digest_nids; | |
377 | return pos; | |
378 | } | |
0f113f3e | 379 | |
3b04cdd7 | 380 | static int openssl_digests(ENGINE *e, const EVP_MD **digest, |
0f113f3e MC |
381 | const int **nids, int nid) |
382 | { | |
383 | if (!digest) { | |
384 | /* We are returning a list of supported nids */ | |
cddcea8c | 385 | return test_digest_nids(nids); |
0f113f3e MC |
386 | } |
387 | /* We are being asked for a specific digest */ | |
388 | if (nid == NID_sha1) | |
cddcea8c | 389 | *digest = test_sha_md(); |
0f113f3e MC |
390 | else { |
391 | # ifdef TEST_ENG_OPENSSL_SHA_OTHERS | |
392 | fprintf(stderr, "(TEST_ENG_OPENSSL_SHA) returning NULL for " | |
393 | "nid %d\n", nid); | |
394 | # endif | |
395 | *digest = NULL; | |
396 | return 0; | |
397 | } | |
398 | return 1; | |
399 | } | |
3b04cdd7 | 400 | #endif |
0dc09233 DSH |
401 | |
402 | #ifdef TEST_ENG_OPENSSL_PKEY | |
403 | static EVP_PKEY *openssl_load_privkey(ENGINE *eng, const char *key_id, | |
0f113f3e MC |
404 | UI_METHOD *ui_method, |
405 | void *callback_data) | |
406 | { | |
407 | BIO *in; | |
408 | EVP_PKEY *key; | |
409 | fprintf(stderr, "(TEST_ENG_OPENSSL_PKEY)Loading Private key %s\n", | |
410 | key_id); | |
411 | in = BIO_new_file(key_id, "r"); | |
412 | if (!in) | |
413 | return NULL; | |
414 | key = PEM_read_bio_PrivateKey(in, NULL, 0, NULL); | |
415 | BIO_free(in); | |
416 | return key; | |
417 | } | |
0dc09233 | 418 | #endif |
e77906b9 DSH |
419 | |
420 | #ifdef TEST_ENG_OPENSSL_HMAC | |
421 | ||
0f113f3e MC |
422 | /* |
423 | * Experimental HMAC redirection implementation: mainly copied from | |
e77906b9 DSH |
424 | * hm_pmeth.c |
425 | */ | |
426 | ||
427 | /* HMAC pkey context structure */ | |
428 | ||
0f113f3e MC |
429 | typedef struct { |
430 | const EVP_MD *md; /* MD for HMAC use */ | |
431 | ASN1_OCTET_STRING ktmp; /* Temp storage for key */ | |
bf7c6817 | 432 | HMAC_CTX *ctx; |
0f113f3e | 433 | } OSSL_HMAC_PKEY_CTX; |
e77906b9 DSH |
434 | |
435 | static int ossl_hmac_init(EVP_PKEY_CTX *ctx) | |
0f113f3e MC |
436 | { |
437 | OSSL_HMAC_PKEY_CTX *hctx; | |
64b25758 | 438 | |
cdb10bae RS |
439 | if ((hctx = OPENSSL_zalloc(sizeof(*hctx))) == NULL) { |
440 | ENGINEerr(ENGINE_F_OSSL_HMAC_INIT, ERR_R_MALLOC_FAILURE); | |
0f113f3e | 441 | return 0; |
cdb10bae | 442 | } |
0f113f3e | 443 | hctx->ktmp.type = V_ASN1_OCTET_STRING; |
bf7c6817 | 444 | hctx->ctx = HMAC_CTX_new(); |
a93e0e78 MRA |
445 | if (hctx->ctx == NULL) { |
446 | OPENSSL_free(hctx); | |
447 | return 0; | |
448 | } | |
0f113f3e MC |
449 | EVP_PKEY_CTX_set_data(ctx, hctx); |
450 | EVP_PKEY_CTX_set0_keygen_info(ctx, NULL, 0); | |
451 | # ifdef TEST_ENG_OPENSSL_HMAC_INIT | |
452 | fprintf(stderr, "(TEST_ENG_OPENSSL_HMAC) ossl_hmac_init() called\n"); | |
453 | # endif | |
454 | return 1; | |
455 | } | |
e77906b9 | 456 | |
a93e0e78 MRA |
457 | static void ossl_hmac_cleanup(EVP_PKEY_CTX *ctx); |
458 | ||
e77906b9 | 459 | static int ossl_hmac_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src) |
0f113f3e MC |
460 | { |
461 | OSSL_HMAC_PKEY_CTX *sctx, *dctx; | |
a93e0e78 MRA |
462 | |
463 | /* allocate memory for dst->data and a new HMAC_CTX in dst->data->ctx */ | |
0f113f3e MC |
464 | if (!ossl_hmac_init(dst)) |
465 | return 0; | |
466 | sctx = EVP_PKEY_CTX_get_data(src); | |
467 | dctx = EVP_PKEY_CTX_get_data(dst); | |
468 | dctx->md = sctx->md; | |
bf7c6817 | 469 | if (!HMAC_CTX_copy(dctx->ctx, sctx->ctx)) |
a93e0e78 | 470 | goto err; |
0f113f3e MC |
471 | if (sctx->ktmp.data) { |
472 | if (!ASN1_OCTET_STRING_set(&dctx->ktmp, | |
473 | sctx->ktmp.data, sctx->ktmp.length)) | |
a93e0e78 | 474 | goto err; |
0f113f3e MC |
475 | } |
476 | return 1; | |
a93e0e78 MRA |
477 | err: |
478 | /* release HMAC_CTX in dst->data->ctx and memory allocated for dst->data */ | |
479 | ossl_hmac_cleanup(dst); | |
480 | return 0; | |
0f113f3e | 481 | } |
e77906b9 DSH |
482 | |
483 | static void ossl_hmac_cleanup(EVP_PKEY_CTX *ctx) | |
0f113f3e | 484 | { |
4b45c6e5 RS |
485 | OSSL_HMAC_PKEY_CTX *hctx = EVP_PKEY_CTX_get_data(ctx); |
486 | ||
a93e0e78 MRA |
487 | if (hctx) { |
488 | HMAC_CTX_free(hctx->ctx); | |
489 | OPENSSL_clear_free(hctx->ktmp.data, hctx->ktmp.length); | |
490 | OPENSSL_free(hctx); | |
491 | EVP_PKEY_CTX_set_data(ctx, NULL); | |
492 | } | |
0f113f3e | 493 | } |
e77906b9 DSH |
494 | |
495 | static int ossl_hmac_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey) | |
0f113f3e MC |
496 | { |
497 | ASN1_OCTET_STRING *hkey = NULL; | |
498 | OSSL_HMAC_PKEY_CTX *hctx = EVP_PKEY_CTX_get_data(ctx); | |
499 | if (!hctx->ktmp.data) | |
500 | return 0; | |
501 | hkey = ASN1_OCTET_STRING_dup(&hctx->ktmp); | |
502 | if (!hkey) | |
503 | return 0; | |
504 | EVP_PKEY_assign(pkey, EVP_PKEY_HMAC, hkey); | |
505 | ||
506 | return 1; | |
507 | } | |
508 | ||
509 | static int ossl_int_update(EVP_MD_CTX *ctx, const void *data, size_t count) | |
510 | { | |
bf7c6817 RL |
511 | OSSL_HMAC_PKEY_CTX *hctx = EVP_PKEY_CTX_get_data(EVP_MD_CTX_pkey_ctx(ctx)); |
512 | if (!HMAC_Update(hctx->ctx, data, count)) | |
0f113f3e MC |
513 | return 0; |
514 | return 1; | |
515 | } | |
e77906b9 DSH |
516 | |
517 | static int ossl_hmac_signctx_init(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx) | |
0f113f3e MC |
518 | { |
519 | EVP_MD_CTX_set_flags(mctx, EVP_MD_CTX_FLAG_NO_INIT); | |
bf7c6817 | 520 | EVP_MD_CTX_set_update_fn(mctx, ossl_int_update); |
0f113f3e MC |
521 | return 1; |
522 | } | |
523 | ||
524 | static int ossl_hmac_signctx(EVP_PKEY_CTX *ctx, unsigned char *sig, | |
525 | size_t *siglen, EVP_MD_CTX *mctx) | |
526 | { | |
527 | unsigned int hlen; | |
528 | OSSL_HMAC_PKEY_CTX *hctx = EVP_PKEY_CTX_get_data(ctx); | |
529 | int l = EVP_MD_CTX_size(mctx); | |
530 | ||
531 | if (l < 0) | |
532 | return 0; | |
533 | *siglen = l; | |
534 | if (!sig) | |
535 | return 1; | |
536 | ||
bf7c6817 | 537 | if (!HMAC_Final(hctx->ctx, sig, &hlen)) |
0f113f3e MC |
538 | return 0; |
539 | *siglen = (size_t)hlen; | |
540 | return 1; | |
541 | } | |
e77906b9 DSH |
542 | |
543 | static int ossl_hmac_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2) | |
0f113f3e MC |
544 | { |
545 | OSSL_HMAC_PKEY_CTX *hctx = EVP_PKEY_CTX_get_data(ctx); | |
546 | EVP_PKEY *pk; | |
547 | ASN1_OCTET_STRING *key; | |
548 | switch (type) { | |
549 | ||
550 | case EVP_PKEY_CTRL_SET_MAC_KEY: | |
551 | if ((!p2 && p1 > 0) || (p1 < -1)) | |
552 | return 0; | |
553 | if (!ASN1_OCTET_STRING_set(&hctx->ktmp, p2, p1)) | |
554 | return 0; | |
555 | break; | |
556 | ||
557 | case EVP_PKEY_CTRL_MD: | |
558 | hctx->md = p2; | |
559 | break; | |
560 | ||
561 | case EVP_PKEY_CTRL_DIGESTINIT: | |
562 | pk = EVP_PKEY_CTX_get0_pkey(ctx); | |
563 | key = EVP_PKEY_get0(pk); | |
bf7c6817 | 564 | if (!HMAC_Init_ex(hctx->ctx, key->data, key->length, hctx->md, NULL)) |
0f113f3e MC |
565 | return 0; |
566 | break; | |
567 | ||
568 | default: | |
569 | return -2; | |
570 | ||
571 | } | |
572 | return 1; | |
573 | } | |
e77906b9 DSH |
574 | |
575 | static int ossl_hmac_ctrl_str(EVP_PKEY_CTX *ctx, | |
0f113f3e MC |
576 | const char *type, const char *value) |
577 | { | |
578 | if (!value) { | |
579 | return 0; | |
580 | } | |
86885c28 | 581 | if (strcmp(type, "key") == 0) { |
0f113f3e MC |
582 | void *p = (void *)value; |
583 | return ossl_hmac_ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY, -1, p); | |
584 | } | |
86885c28 | 585 | if (strcmp(type, "hexkey") == 0) { |
0f113f3e MC |
586 | unsigned char *key; |
587 | int r; | |
588 | long keylen; | |
14f051a0 | 589 | key = OPENSSL_hexstr2buf(value, &keylen); |
0f113f3e MC |
590 | if (!key) |
591 | return 0; | |
592 | r = ossl_hmac_ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY, keylen, key); | |
593 | OPENSSL_free(key); | |
594 | return r; | |
595 | } | |
596 | return -2; | |
597 | } | |
e77906b9 DSH |
598 | |
599 | static EVP_PKEY_METHOD *ossl_hmac_meth; | |
600 | ||
601 | static int ossl_register_hmac_meth(void) | |
0f113f3e MC |
602 | { |
603 | EVP_PKEY_METHOD *meth; | |
604 | meth = EVP_PKEY_meth_new(EVP_PKEY_HMAC, 0); | |
90945fa3 | 605 | if (meth == NULL) |
0f113f3e MC |
606 | return 0; |
607 | EVP_PKEY_meth_set_init(meth, ossl_hmac_init); | |
608 | EVP_PKEY_meth_set_copy(meth, ossl_hmac_copy); | |
609 | EVP_PKEY_meth_set_cleanup(meth, ossl_hmac_cleanup); | |
e77906b9 | 610 | |
0f113f3e | 611 | EVP_PKEY_meth_set_keygen(meth, 0, ossl_hmac_keygen); |
e77906b9 | 612 | |
0f113f3e MC |
613 | EVP_PKEY_meth_set_signctx(meth, ossl_hmac_signctx_init, |
614 | ossl_hmac_signctx); | |
e77906b9 | 615 | |
0f113f3e MC |
616 | EVP_PKEY_meth_set_ctrl(meth, ossl_hmac_ctrl, ossl_hmac_ctrl_str); |
617 | ossl_hmac_meth = meth; | |
618 | return 1; | |
619 | } | |
e77906b9 DSH |
620 | |
621 | static int ossl_pkey_meths(ENGINE *e, EVP_PKEY_METHOD **pmeth, | |
0f113f3e MC |
622 | const int **nids, int nid) |
623 | { | |
624 | static int ossl_pkey_nids[] = { | |
625 | EVP_PKEY_HMAC, | |
626 | 0 | |
627 | }; | |
628 | if (!pmeth) { | |
629 | *nids = ossl_pkey_nids; | |
630 | return 1; | |
631 | } | |
632 | ||
633 | if (nid == EVP_PKEY_HMAC) { | |
634 | *pmeth = ossl_hmac_meth; | |
635 | return 1; | |
636 | } | |
637 | ||
638 | *pmeth = NULL; | |
639 | return 0; | |
640 | } | |
e77906b9 DSH |
641 | |
642 | #endif | |
cddcea8c RL |
643 | |
644 | int openssl_destroy(ENGINE *e) | |
645 | { | |
646 | test_sha_md_destroy(); | |
5abb2fc9 | 647 | #ifdef TEST_ENG_OPENSSL_RC4 |
39e8d0ce RL |
648 | test_r4_cipher_destroy(); |
649 | test_r4_40_cipher_destroy(); | |
5abb2fc9 | 650 | #endif |
cddcea8c RL |
651 | return 1; |
652 | } | |
653 |