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