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