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