b8850dcb0d5e71fc2b106f92e50d3e14ac4a5f79
[openssl.git] / crypto / engine / eng_openssl.c
1 /* crypto/engine/eng_openssl.c */
2 /*
3  * Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL project
4  * 2000.
5  */
6 /* ====================================================================
7  * Copyright (c) 1999-2001 The OpenSSL Project.  All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in
18  *    the documentation and/or other materials provided with the
19  *    distribution.
20  *
21  * 3. All advertising materials mentioning features or use of this
22  *    software must display the following acknowledgment:
23  *    "This product includes software developed by the OpenSSL Project
24  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25  *
26  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27  *    endorse or promote products derived from this software without
28  *    prior written permission. For written permission, please contact
29  *    licensing@OpenSSL.org.
30  *
31  * 5. Products derived from this software may not be called "OpenSSL"
32  *    nor may "OpenSSL" appear in their names without prior written
33  *    permission of the OpenSSL Project.
34  *
35  * 6. Redistributions of any form whatsoever must retain the following
36  *    acknowledgment:
37  *    "This product includes software developed by the OpenSSL Project
38  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39  *
40  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
44  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51  * OF THE POSSIBILITY OF SUCH DAMAGE.
52  * ====================================================================
53  *
54  * This product includes cryptographic software written by Eric Young
55  * (eay@cryptsoft.com).  This product includes software written by Tim
56  * Hudson (tjh@cryptsoft.com).
57  *
58  */
59 /* ====================================================================
60  * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
61  * ECDH support in OpenSSL originally developed by
62  * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
63  */
64
65 #include <stdio.h>
66 #include <openssl/crypto.h>
67 #include "internal/cryptlib.h"
68 #include <openssl/engine.h>
69 #include <openssl/dso.h>
70 #include <openssl/pem.h>
71 #include <openssl/evp.h>
72 #include <openssl/rand.h>
73 #ifndef OPENSSL_NO_RSA
74 # include <openssl/rsa.h>
75 #endif
76 #ifndef OPENSSL_NO_DSA
77 # include <openssl/dsa.h>
78 #endif
79 #ifndef OPENSSL_NO_DH
80 # include <openssl/dh.h>
81 #endif
82
83 #include <openssl/hmac.h>
84 #include <openssl/x509v3.h>
85
86 /*
87  * This testing gunk is implemented (and explained) lower down. It also
88  * assumes the application explicitly calls "ENGINE_load_openssl()" because
89  * this is no longer automatic in ENGINE_load_builtin_engines().
90  */
91 #define TEST_ENG_OPENSSL_RC4
92 #ifndef OPENSSL_NO_STDIO
93 #define TEST_ENG_OPENSSL_PKEY
94 #endif
95 /* #define TEST_ENG_OPENSSL_HMAC */
96 /* #define TEST_ENG_OPENSSL_HMAC_INIT */
97 /* #define TEST_ENG_OPENSSL_RC4_OTHERS */
98 #define TEST_ENG_OPENSSL_RC4_P_INIT
99 /* #define TEST_ENG_OPENSSL_RC4_P_CIPHER */
100 #define TEST_ENG_OPENSSL_SHA
101 /* #define TEST_ENG_OPENSSL_SHA_OTHERS */
102 /* #define TEST_ENG_OPENSSL_SHA_P_INIT */
103 /* #define TEST_ENG_OPENSSL_SHA_P_UPDATE */
104 /* #define TEST_ENG_OPENSSL_SHA_P_FINAL */
105
106 /* Now check what of those algorithms are actually enabled */
107 #ifdef OPENSSL_NO_RC4
108 # undef TEST_ENG_OPENSSL_RC4
109 # undef TEST_ENG_OPENSSL_RC4_OTHERS
110 # undef TEST_ENG_OPENSSL_RC4_P_INIT
111 # undef TEST_ENG_OPENSSL_RC4_P_CIPHER
112 #endif
113
114 static int openssl_destroy(ENGINE *e);
115
116 #ifdef TEST_ENG_OPENSSL_RC4
117 static int openssl_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
118                            const int **nids, int nid);
119 #endif
120 #ifdef TEST_ENG_OPENSSL_SHA
121 static int openssl_digests(ENGINE *e, const EVP_MD **digest,
122                            const int **nids, int nid);
123 #endif
124
125 #ifdef TEST_ENG_OPENSSL_PKEY
126 static EVP_PKEY *openssl_load_privkey(ENGINE *eng, const char *key_id,
127                                       UI_METHOD *ui_method,
128                                       void *callback_data);
129 #endif
130
131 #ifdef TEST_ENG_OPENSSL_HMAC
132 static int ossl_register_hmac_meth(void);
133 static int ossl_pkey_meths(ENGINE *e, EVP_PKEY_METHOD **pmeth,
134                            const int **nids, int nid);
135 #endif
136
137 /* The constants used when creating the ENGINE */
138 static const char *engine_openssl_id = "openssl";
139 static const char *engine_openssl_name = "Software engine support";
140
141 /*
142  * This internal function is used by ENGINE_openssl() and possibly by the
143  * "dynamic" ENGINE support too
144  */
145 static int bind_helper(ENGINE *e)
146 {
147     if (!ENGINE_set_id(e, engine_openssl_id)
148         || !ENGINE_set_name(e, engine_openssl_name)
149         || !ENGINE_set_destroy_function(e, openssl_destroy)
150 #ifndef TEST_ENG_OPENSSL_NO_ALGORITHMS
151 # ifndef OPENSSL_NO_RSA
152         || !ENGINE_set_RSA(e, RSA_get_default_method())
153 # endif
154 # ifndef OPENSSL_NO_DSA
155         || !ENGINE_set_DSA(e, DSA_get_default_method())
156 # endif
157 # ifndef OPENSSL_NO_EC
158         || !ENGINE_set_ECDH(e, ECDH_OpenSSL())
159         || !ENGINE_set_ECDSA(e, ECDSA_OpenSSL())
160 # endif
161 # ifndef OPENSSL_NO_DH
162         || !ENGINE_set_DH(e, DH_get_default_method())
163 # endif
164         || !ENGINE_set_RAND(e, RAND_OpenSSL())
165 # ifdef TEST_ENG_OPENSSL_RC4
166         || !ENGINE_set_ciphers(e, openssl_ciphers)
167 # endif
168 # ifdef TEST_ENG_OPENSSL_SHA
169         || !ENGINE_set_digests(e, openssl_digests)
170 # endif
171 #endif
172 #ifdef TEST_ENG_OPENSSL_PKEY
173         || !ENGINE_set_load_privkey_function(e, openssl_load_privkey)
174 #endif
175 #ifdef TEST_ENG_OPENSSL_HMAC
176         || !ossl_register_hmac_meth()
177         || !ENGINE_set_pkey_meths(e, ossl_pkey_meths)
178 #endif
179         )
180         return 0;
181     /*
182      * If we add errors to this ENGINE, ensure the error handling is setup
183      * here
184      */
185     /* openssl_load_error_strings(); */
186     return 1;
187 }
188
189 static ENGINE *engine_openssl(void)
190 {
191     ENGINE *ret = ENGINE_new();
192     if (ret == NULL)
193         return NULL;
194     if (!bind_helper(ret)) {
195         ENGINE_free(ret);
196         return NULL;
197     }
198     return ret;
199 }
200
201 void ENGINE_load_openssl(void)
202 {
203     ENGINE *toadd = engine_openssl();
204     if (!toadd)
205         return;
206     ENGINE_add(toadd);
207     /*
208      * If the "add" worked, it gets a structural reference. So either way, we
209      * release our just-created reference.
210      */
211     ENGINE_free(toadd);
212     ERR_clear_error();
213 }
214
215 /*
216  * This stuff is needed if this ENGINE is being compiled into a
217  * self-contained shared-library.
218  */
219 #ifdef ENGINE_DYNAMIC_SUPPORT
220 static int bind_fn(ENGINE *e, const char *id)
221 {
222     if (id && (strcmp(id, engine_openssl_id) != 0))
223         return 0;
224     if (!bind_helper(e))
225         return 0;
226     return 1;
227 }
228
229 IMPLEMENT_DYNAMIC_CHECK_FN()
230     IMPLEMENT_DYNAMIC_BIND_FN(bind_fn)
231 #endif                          /* ENGINE_DYNAMIC_SUPPORT */
232 #ifdef TEST_ENG_OPENSSL_RC4
233 /*-
234  * This section of code compiles an "alternative implementation" of two modes of
235  * RC4 into this ENGINE. The result is that EVP_CIPHER operation for "rc4"
236  * should under normal circumstances go via this support rather than the default
237  * EVP support. There are other symbols to tweak the testing;
238  *    TEST_ENC_OPENSSL_RC4_OTHERS - print a one line message to stderr each time
239  *        we're asked for a cipher we don't support (should not happen).
240  *    TEST_ENG_OPENSSL_RC4_P_INIT - print a one line message to stderr each time
241  *        the "init_key" handler is called.
242  *    TEST_ENG_OPENSSL_RC4_P_CIPHER - ditto for the "cipher" handler.
243  */
244 # include <openssl/rc4.h>
245 # define TEST_RC4_KEY_SIZE               16
246 static const int test_cipher_nids[] = { NID_rc4, NID_rc4_40 };
247
248 static const int test_cipher_nids_number = 2;
249 typedef struct {
250     unsigned char key[TEST_RC4_KEY_SIZE];
251     RC4_KEY ks;
252 } TEST_RC4_KEY;
253 # define test(ctx) ((TEST_RC4_KEY *)(ctx)->cipher_data)
254 static int test_rc4_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
255                              const unsigned char *iv, int enc)
256 {
257 # ifdef TEST_ENG_OPENSSL_RC4_P_INIT
258     fprintf(stderr, "(TEST_ENG_OPENSSL_RC4) test_init_key() called\n");
259 # endif
260     memcpy(&test(ctx)->key[0], key, EVP_CIPHER_CTX_key_length(ctx));
261     RC4_set_key(&test(ctx)->ks, EVP_CIPHER_CTX_key_length(ctx),
262                 test(ctx)->key);
263     return 1;
264 }
265
266 static int test_rc4_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
267                            const unsigned char *in, size_t inl)
268 {
269 # ifdef TEST_ENG_OPENSSL_RC4_P_CIPHER
270     fprintf(stderr, "(TEST_ENG_OPENSSL_RC4) test_cipher() called\n");
271 # endif
272     RC4(&test(ctx)->ks, inl, in, out);
273     return 1;
274 }
275
276 static const EVP_CIPHER test_r4_cipher = {
277     NID_rc4,
278     1, TEST_RC4_KEY_SIZE, 0,
279     EVP_CIPH_VARIABLE_LENGTH,
280     test_rc4_init_key,
281     test_rc4_cipher,
282     NULL,
283     sizeof(TEST_RC4_KEY),
284     NULL,
285     NULL,
286     NULL,
287     NULL
288 };
289
290 static const EVP_CIPHER test_r4_40_cipher = {
291     NID_rc4_40,
292     1, 5 /* 40 bit */ , 0,
293     EVP_CIPH_VARIABLE_LENGTH,
294     test_rc4_init_key,
295     test_rc4_cipher,
296     NULL,
297     sizeof(TEST_RC4_KEY),
298     NULL,
299     NULL,
300     NULL,
301     NULL
302 };
303
304 static int openssl_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
305                            const int **nids, int nid)
306 {
307     if (!cipher) {
308         /* We are returning a list of supported nids */
309         *nids = test_cipher_nids;
310         return test_cipher_nids_number;
311     }
312     /* We are being asked for a specific cipher */
313     if (nid == NID_rc4)
314         *cipher = &test_r4_cipher;
315     else if (nid == NID_rc4_40)
316         *cipher = &test_r4_40_cipher;
317     else {
318 # ifdef TEST_ENG_OPENSSL_RC4_OTHERS
319         fprintf(stderr, "(TEST_ENG_OPENSSL_RC4) returning NULL for "
320                 "nid %d\n", nid);
321 # endif
322         *cipher = NULL;
323         return 0;
324     }
325     return 1;
326 }
327 #endif
328
329 #ifdef TEST_ENG_OPENSSL_SHA
330 /* Much the same sort of comment as for TEST_ENG_OPENSSL_RC4 */
331 # include <openssl/sha.h>
332
333 static int test_sha1_init(EVP_MD_CTX *ctx)
334 {
335 # ifdef TEST_ENG_OPENSSL_SHA_P_INIT
336     fprintf(stderr, "(TEST_ENG_OPENSSL_SHA) test_sha1_init() called\n");
337 # endif
338     return SHA1_Init(EVP_MD_CTX_md_data(ctx));
339 }
340
341 static int test_sha1_update(EVP_MD_CTX *ctx, const void *data, size_t count)
342 {
343 # ifdef TEST_ENG_OPENSSL_SHA_P_UPDATE
344     fprintf(stderr, "(TEST_ENG_OPENSSL_SHA) test_sha1_update() called\n");
345 # endif
346     return SHA1_Update(EVP_MD_CTX_md_data(ctx), data, count);
347 }
348
349 static int test_sha1_final(EVP_MD_CTX *ctx, unsigned char *md)
350 {
351 # ifdef TEST_ENG_OPENSSL_SHA_P_FINAL
352     fprintf(stderr, "(TEST_ENG_OPENSSL_SHA) test_sha1_final() called\n");
353 # endif
354     return SHA1_Final(md, EVP_MD_CTX_md_data(ctx));
355 }
356
357 static EVP_MD *sha1_md = NULL;
358 static const EVP_MD *test_sha_md(void)
359 {
360     if (sha1_md == NULL) {
361         EVP_MD *md;
362
363         if ((md = EVP_MD_meth_new(NID_sha1, NID_sha1WithRSAEncryption)) == NULL
364             || !EVP_MD_meth_set_result_size(md, SHA_DIGEST_LENGTH)
365             || !EVP_MD_meth_set_input_blocksize(md, SHA_CBLOCK)
366             || !EVP_MD_meth_set_app_datasize(md,
367                                              sizeof(EVP_MD *) + sizeof(SHA_CTX))
368             || !EVP_MD_meth_set_flags(md, 0)
369             || !EVP_MD_meth_set_init(md, test_sha1_init)
370             || !EVP_MD_meth_set_update(md, test_sha1_update)
371             || !EVP_MD_meth_set_final(md, test_sha1_final)) {
372             EVP_MD_meth_free(md);
373             md = NULL;
374         }
375         sha1_md = md;
376     }
377     return sha1_md;
378 }
379 static void test_sha_md_destroy(void)
380 {
381     EVP_MD_meth_free(sha1_md);
382     sha1_md = NULL;
383 }
384 static int test_digest_nids(const int **nids)
385 {
386     static int digest_nids[2] = { 0, 0 };
387     static int pos = 0;
388     static int init = 0;
389
390     if (!init) {
391         const EVP_MD *md;
392         if ((md = test_sha_md()) != NULL)
393             digest_nids[pos++] = EVP_MD_type(md);
394         digest_nids[pos] = 0;
395         init = 1;
396     }
397     *nids = digest_nids;
398     return pos;
399 }
400
401 static int openssl_digests(ENGINE *e, const EVP_MD **digest,
402                            const int **nids, int nid)
403 {
404     if (!digest) {
405         /* We are returning a list of supported nids */
406         return test_digest_nids(nids);
407     }
408     /* We are being asked for a specific digest */
409     if (nid == NID_sha1)
410         *digest = test_sha_md();
411     else {
412 # ifdef TEST_ENG_OPENSSL_SHA_OTHERS
413         fprintf(stderr, "(TEST_ENG_OPENSSL_SHA) returning NULL for "
414                 "nid %d\n", nid);
415 # endif
416         *digest = NULL;
417         return 0;
418     }
419     return 1;
420 }
421 #endif
422
423 #ifdef TEST_ENG_OPENSSL_PKEY
424 static EVP_PKEY *openssl_load_privkey(ENGINE *eng, const char *key_id,
425                                       UI_METHOD *ui_method,
426                                       void *callback_data)
427 {
428     BIO *in;
429     EVP_PKEY *key;
430     fprintf(stderr, "(TEST_ENG_OPENSSL_PKEY)Loading Private key %s\n",
431             key_id);
432     in = BIO_new_file(key_id, "r");
433     if (!in)
434         return NULL;
435     key = PEM_read_bio_PrivateKey(in, NULL, 0, NULL);
436     BIO_free(in);
437     return key;
438 }
439 #endif
440
441 #ifdef TEST_ENG_OPENSSL_HMAC
442
443 /*
444  * Experimental HMAC redirection implementation: mainly copied from
445  * hm_pmeth.c
446  */
447
448 /* HMAC pkey context structure */
449
450 typedef struct {
451     const EVP_MD *md;           /* MD for HMAC use */
452     ASN1_OCTET_STRING ktmp;     /* Temp storage for key */
453     HMAC_CTX ctx;
454 } OSSL_HMAC_PKEY_CTX;
455
456 static int ossl_hmac_init(EVP_PKEY_CTX *ctx)
457 {
458     OSSL_HMAC_PKEY_CTX *hctx;
459
460     hctx = OPENSSL_zalloc(sizeof(*hctx));
461     if (hctx == NULL)
462         return 0;
463     hctx->ktmp.type = V_ASN1_OCTET_STRING;
464     HMAC_CTX_init(&hctx->ctx);
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 }
472
473 static int ossl_hmac_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src)
474 {
475     OSSL_HMAC_PKEY_CTX *sctx, *dctx;
476     if (!ossl_hmac_init(dst))
477         return 0;
478     sctx = EVP_PKEY_CTX_get_data(src);
479     dctx = EVP_PKEY_CTX_get_data(dst);
480     dctx->md = sctx->md;
481     /* Because HMAC_CTX_copy does HMAC_CTX_init */
482     HMAC_CTX_cleanup(&dctx->ctx);
483     if (!HMAC_CTX_copy(&dctx->ctx, &sctx->ctx))
484         return 0;
485     if (sctx->ktmp.data) {
486         if (!ASN1_OCTET_STRING_set(&dctx->ktmp,
487                                    sctx->ktmp.data, sctx->ktmp.length))
488             return 0;
489     }
490     return 1;
491 }
492
493 static void ossl_hmac_cleanup(EVP_PKEY_CTX *ctx)
494 {
495     OSSL_HMAC_PKEY_CTX *hctx = EVP_PKEY_CTX_get_data(ctx);
496
497     HMAC_CTX_cleanup(&hctx->ctx);
498     OPENSSL_clear_free(hctx->ktmp.data, hctx->ktmp.length);
499     OPENSSL_free(hctx);
500 }
501
502 static int ossl_hmac_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
503 {
504     ASN1_OCTET_STRING *hkey = NULL;
505     OSSL_HMAC_PKEY_CTX *hctx = EVP_PKEY_CTX_get_data(ctx);
506     if (!hctx->ktmp.data)
507         return 0;
508     hkey = ASN1_OCTET_STRING_dup(&hctx->ktmp);
509     if (!hkey)
510         return 0;
511     EVP_PKEY_assign(pkey, EVP_PKEY_HMAC, hkey);
512
513     return 1;
514 }
515
516 static int ossl_int_update(EVP_MD_CTX *ctx, const void *data, size_t count)
517 {
518     OSSL_HMAC_PKEY_CTX *hctx = EVP_PKEY_CTX_get_data(ctx->pctx);
519     if (!HMAC_Update(&hctx->ctx, data, count))
520         return 0;
521     return 1;
522 }
523
524 static int ossl_hmac_signctx_init(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx)
525 {
526     EVP_MD_CTX_set_flags(mctx, EVP_MD_CTX_FLAG_NO_INIT);
527     mctx->update = ossl_int_update;
528     return 1;
529 }
530
531 static int ossl_hmac_signctx(EVP_PKEY_CTX *ctx, unsigned char *sig,
532                              size_t *siglen, EVP_MD_CTX *mctx)
533 {
534     unsigned int hlen;
535     OSSL_HMAC_PKEY_CTX *hctx = EVP_PKEY_CTX_get_data(ctx);
536     int l = EVP_MD_CTX_size(mctx);
537
538     if (l < 0)
539         return 0;
540     *siglen = l;
541     if (!sig)
542         return 1;
543
544     if (!HMAC_Final(&hctx->ctx, sig, &hlen))
545         return 0;
546     *siglen = (size_t)hlen;
547     return 1;
548 }
549
550 static int ossl_hmac_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
551 {
552     OSSL_HMAC_PKEY_CTX *hctx = EVP_PKEY_CTX_get_data(ctx);
553     EVP_PKEY *pk;
554     ASN1_OCTET_STRING *key;
555     switch (type) {
556
557     case EVP_PKEY_CTRL_SET_MAC_KEY:
558         if ((!p2 && p1 > 0) || (p1 < -1))
559             return 0;
560         if (!ASN1_OCTET_STRING_set(&hctx->ktmp, p2, p1))
561             return 0;
562         break;
563
564     case EVP_PKEY_CTRL_MD:
565         hctx->md = p2;
566         break;
567
568     case EVP_PKEY_CTRL_DIGESTINIT:
569         pk = EVP_PKEY_CTX_get0_pkey(ctx);
570         key = EVP_PKEY_get0(pk);
571         if (!HMAC_Init_ex(&hctx->ctx, key->data, key->length, hctx->md, NULL))
572             return 0;
573         break;
574
575     default:
576         return -2;
577
578     }
579     return 1;
580 }
581
582 static int ossl_hmac_ctrl_str(EVP_PKEY_CTX *ctx,
583                               const char *type, const char *value)
584 {
585     if (!value) {
586         return 0;
587     }
588     if (strcmp(type, "key") == 0) {
589         void *p = (void *)value;
590         return ossl_hmac_ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY, -1, p);
591     }
592     if (strcmp(type, "hexkey") == 0) {
593         unsigned char *key;
594         int r;
595         long keylen;
596         key = string_to_hex(value, &keylen);
597         if (!key)
598             return 0;
599         r = ossl_hmac_ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY, keylen, key);
600         OPENSSL_free(key);
601         return r;
602     }
603     return -2;
604 }
605
606 static EVP_PKEY_METHOD *ossl_hmac_meth;
607
608 static int ossl_register_hmac_meth(void)
609 {
610     EVP_PKEY_METHOD *meth;
611     meth = EVP_PKEY_meth_new(EVP_PKEY_HMAC, 0);
612     if (meth == NULL)
613         return 0;
614     EVP_PKEY_meth_set_init(meth, ossl_hmac_init);
615     EVP_PKEY_meth_set_copy(meth, ossl_hmac_copy);
616     EVP_PKEY_meth_set_cleanup(meth, ossl_hmac_cleanup);
617
618     EVP_PKEY_meth_set_keygen(meth, 0, ossl_hmac_keygen);
619
620     EVP_PKEY_meth_set_signctx(meth, ossl_hmac_signctx_init,
621                               ossl_hmac_signctx);
622
623     EVP_PKEY_meth_set_ctrl(meth, ossl_hmac_ctrl, ossl_hmac_ctrl_str);
624     ossl_hmac_meth = meth;
625     return 1;
626 }
627
628 static int ossl_pkey_meths(ENGINE *e, EVP_PKEY_METHOD **pmeth,
629                            const int **nids, int nid)
630 {
631     static int ossl_pkey_nids[] = {
632         EVP_PKEY_HMAC,
633         0
634     };
635     if (!pmeth) {
636         *nids = ossl_pkey_nids;
637         return 1;
638     }
639
640     if (nid == EVP_PKEY_HMAC) {
641         *pmeth = ossl_hmac_meth;
642         return 1;
643     }
644
645     *pmeth = NULL;
646     return 0;
647 }
648
649 #endif
650
651 int openssl_destroy(ENGINE *e)
652 {
653     test_sha_md_destroy();
654     return 1;
655 }
656