90a95541bae1e364ae1ad6ce5fd31142b6050726
[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 # endif
159 # ifndef OPENSSL_NO_DH
160         || !ENGINE_set_DH(e, DH_get_default_method())
161 # endif
162         || !ENGINE_set_RAND(e, RAND_OpenSSL())
163 # ifdef TEST_ENG_OPENSSL_RC4
164         || !ENGINE_set_ciphers(e, openssl_ciphers)
165 # endif
166 # ifdef TEST_ENG_OPENSSL_SHA
167         || !ENGINE_set_digests(e, openssl_digests)
168 # endif
169 #endif
170 #ifdef TEST_ENG_OPENSSL_PKEY
171         || !ENGINE_set_load_privkey_function(e, openssl_load_privkey)
172 #endif
173 #ifdef TEST_ENG_OPENSSL_HMAC
174         || !ossl_register_hmac_meth()
175         || !ENGINE_set_pkey_meths(e, ossl_pkey_meths)
176 #endif
177         )
178         return 0;
179     /*
180      * If we add errors to this ENGINE, ensure the error handling is setup
181      * here
182      */
183     /* openssl_load_error_strings(); */
184     return 1;
185 }
186
187 static ENGINE *engine_openssl(void)
188 {
189     ENGINE *ret = ENGINE_new();
190     if (ret == NULL)
191         return NULL;
192     if (!bind_helper(ret)) {
193         ENGINE_free(ret);
194         return NULL;
195     }
196     return ret;
197 }
198
199 void ENGINE_load_openssl(void)
200 {
201     ENGINE *toadd = engine_openssl();
202     if (!toadd)
203         return;
204     ENGINE_add(toadd);
205     /*
206      * If the "add" worked, it gets a structural reference. So either way, we
207      * release our just-created reference.
208      */
209     ENGINE_free(toadd);
210     ERR_clear_error();
211 }
212
213 /*
214  * This stuff is needed if this ENGINE is being compiled into a
215  * self-contained shared-library.
216  */
217 #ifdef ENGINE_DYNAMIC_SUPPORT
218 static int bind_fn(ENGINE *e, const char *id)
219 {
220     if (id && (strcmp(id, engine_openssl_id) != 0))
221         return 0;
222     if (!bind_helper(e))
223         return 0;
224     return 1;
225 }
226
227 IMPLEMENT_DYNAMIC_CHECK_FN()
228     IMPLEMENT_DYNAMIC_BIND_FN(bind_fn)
229 #endif                          /* ENGINE_DYNAMIC_SUPPORT */
230 #ifdef TEST_ENG_OPENSSL_RC4
231 /*-
232  * This section of code compiles an "alternative implementation" of two modes of
233  * RC4 into this ENGINE. The result is that EVP_CIPHER operation for "rc4"
234  * should under normal circumstances go via this support rather than the default
235  * EVP support. There are other symbols to tweak the testing;
236  *    TEST_ENC_OPENSSL_RC4_OTHERS - print a one line message to stderr each time
237  *        we're asked for a cipher we don't support (should not happen).
238  *    TEST_ENG_OPENSSL_RC4_P_INIT - print a one line message to stderr each time
239  *        the "init_key" handler is called.
240  *    TEST_ENG_OPENSSL_RC4_P_CIPHER - ditto for the "cipher" handler.
241  */
242 # include <openssl/rc4.h>
243 # define TEST_RC4_KEY_SIZE               16
244 static const int test_cipher_nids[] = { NID_rc4, NID_rc4_40 };
245
246 static const int test_cipher_nids_number = 2;
247 typedef struct {
248     unsigned char key[TEST_RC4_KEY_SIZE];
249     RC4_KEY ks;
250 } TEST_RC4_KEY;
251 # define test(ctx) ((TEST_RC4_KEY *)(ctx)->cipher_data)
252 static int test_rc4_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
253                              const unsigned char *iv, int enc)
254 {
255 # ifdef TEST_ENG_OPENSSL_RC4_P_INIT
256     fprintf(stderr, "(TEST_ENG_OPENSSL_RC4) test_init_key() called\n");
257 # endif
258     memcpy(&test(ctx)->key[0], key, EVP_CIPHER_CTX_key_length(ctx));
259     RC4_set_key(&test(ctx)->ks, EVP_CIPHER_CTX_key_length(ctx),
260                 test(ctx)->key);
261     return 1;
262 }
263
264 static int test_rc4_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
265                            const unsigned char *in, size_t inl)
266 {
267 # ifdef TEST_ENG_OPENSSL_RC4_P_CIPHER
268     fprintf(stderr, "(TEST_ENG_OPENSSL_RC4) test_cipher() called\n");
269 # endif
270     RC4(&test(ctx)->ks, inl, in, out);
271     return 1;
272 }
273
274 static const EVP_CIPHER test_r4_cipher = {
275     NID_rc4,
276     1, TEST_RC4_KEY_SIZE, 0,
277     EVP_CIPH_VARIABLE_LENGTH,
278     test_rc4_init_key,
279     test_rc4_cipher,
280     NULL,
281     sizeof(TEST_RC4_KEY),
282     NULL,
283     NULL,
284     NULL,
285     NULL
286 };
287
288 static const EVP_CIPHER test_r4_40_cipher = {
289     NID_rc4_40,
290     1, 5 /* 40 bit */ , 0,
291     EVP_CIPH_VARIABLE_LENGTH,
292     test_rc4_init_key,
293     test_rc4_cipher,
294     NULL,
295     sizeof(TEST_RC4_KEY),
296     NULL,
297     NULL,
298     NULL,
299     NULL
300 };
301
302 static int openssl_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
303                            const int **nids, int nid)
304 {
305     if (!cipher) {
306         /* We are returning a list of supported nids */
307         *nids = test_cipher_nids;
308         return test_cipher_nids_number;
309     }
310     /* We are being asked for a specific cipher */
311     if (nid == NID_rc4)
312         *cipher = &test_r4_cipher;
313     else if (nid == NID_rc4_40)
314         *cipher = &test_r4_40_cipher;
315     else {
316 # ifdef TEST_ENG_OPENSSL_RC4_OTHERS
317         fprintf(stderr, "(TEST_ENG_OPENSSL_RC4) returning NULL for "
318                 "nid %d\n", nid);
319 # endif
320         *cipher = NULL;
321         return 0;
322     }
323     return 1;
324 }
325 #endif
326
327 #ifdef TEST_ENG_OPENSSL_SHA
328 /* Much the same sort of comment as for TEST_ENG_OPENSSL_RC4 */
329 # include <openssl/sha.h>
330
331 static int test_sha1_init(EVP_MD_CTX *ctx)
332 {
333 # ifdef TEST_ENG_OPENSSL_SHA_P_INIT
334     fprintf(stderr, "(TEST_ENG_OPENSSL_SHA) test_sha1_init() called\n");
335 # endif
336     return SHA1_Init(EVP_MD_CTX_md_data(ctx));
337 }
338
339 static int test_sha1_update(EVP_MD_CTX *ctx, const void *data, size_t count)
340 {
341 # ifdef TEST_ENG_OPENSSL_SHA_P_UPDATE
342     fprintf(stderr, "(TEST_ENG_OPENSSL_SHA) test_sha1_update() called\n");
343 # endif
344     return SHA1_Update(EVP_MD_CTX_md_data(ctx), data, count);
345 }
346
347 static int test_sha1_final(EVP_MD_CTX *ctx, unsigned char *md)
348 {
349 # ifdef TEST_ENG_OPENSSL_SHA_P_FINAL
350     fprintf(stderr, "(TEST_ENG_OPENSSL_SHA) test_sha1_final() called\n");
351 # endif
352     return SHA1_Final(md, EVP_MD_CTX_md_data(ctx));
353 }
354
355 static EVP_MD *sha1_md = NULL;
356 static const EVP_MD *test_sha_md(void)
357 {
358     if (sha1_md == NULL) {
359         EVP_MD *md;
360
361         if ((md = EVP_MD_meth_new(NID_sha1, NID_sha1WithRSAEncryption)) == NULL
362             || !EVP_MD_meth_set_result_size(md, SHA_DIGEST_LENGTH)
363             || !EVP_MD_meth_set_input_blocksize(md, SHA_CBLOCK)
364             || !EVP_MD_meth_set_app_datasize(md,
365                                              sizeof(EVP_MD *) + sizeof(SHA_CTX))
366             || !EVP_MD_meth_set_flags(md, 0)
367             || !EVP_MD_meth_set_init(md, test_sha1_init)
368             || !EVP_MD_meth_set_update(md, test_sha1_update)
369             || !EVP_MD_meth_set_final(md, test_sha1_final)) {
370             EVP_MD_meth_free(md);
371             md = NULL;
372         }
373         sha1_md = md;
374     }
375     return sha1_md;
376 }
377 static void test_sha_md_destroy(void)
378 {
379     EVP_MD_meth_free(sha1_md);
380     sha1_md = NULL;
381 }
382 static int test_digest_nids(const int **nids)
383 {
384     static int digest_nids[2] = { 0, 0 };
385     static int pos = 0;
386     static int init = 0;
387
388     if (!init) {
389         const EVP_MD *md;
390         if ((md = test_sha_md()) != NULL)
391             digest_nids[pos++] = EVP_MD_type(md);
392         digest_nids[pos] = 0;
393         init = 1;
394     }
395     *nids = digest_nids;
396     return pos;
397 }
398
399 static int openssl_digests(ENGINE *e, const EVP_MD **digest,
400                            const int **nids, int nid)
401 {
402     if (!digest) {
403         /* We are returning a list of supported nids */
404         return test_digest_nids(nids);
405     }
406     /* We are being asked for a specific digest */
407     if (nid == NID_sha1)
408         *digest = test_sha_md();
409     else {
410 # ifdef TEST_ENG_OPENSSL_SHA_OTHERS
411         fprintf(stderr, "(TEST_ENG_OPENSSL_SHA) returning NULL for "
412                 "nid %d\n", nid);
413 # endif
414         *digest = NULL;
415         return 0;
416     }
417     return 1;
418 }
419 #endif
420
421 #ifdef TEST_ENG_OPENSSL_PKEY
422 static EVP_PKEY *openssl_load_privkey(ENGINE *eng, const char *key_id,
423                                       UI_METHOD *ui_method,
424                                       void *callback_data)
425 {
426     BIO *in;
427     EVP_PKEY *key;
428     fprintf(stderr, "(TEST_ENG_OPENSSL_PKEY)Loading Private key %s\n",
429             key_id);
430     in = BIO_new_file(key_id, "r");
431     if (!in)
432         return NULL;
433     key = PEM_read_bio_PrivateKey(in, NULL, 0, NULL);
434     BIO_free(in);
435     return key;
436 }
437 #endif
438
439 #ifdef TEST_ENG_OPENSSL_HMAC
440
441 /*
442  * Experimental HMAC redirection implementation: mainly copied from
443  * hm_pmeth.c
444  */
445
446 /* HMAC pkey context structure */
447
448 typedef struct {
449     const EVP_MD *md;           /* MD for HMAC use */
450     ASN1_OCTET_STRING ktmp;     /* Temp storage for key */
451     HMAC_CTX *ctx;
452 } OSSL_HMAC_PKEY_CTX;
453
454 static int ossl_hmac_init(EVP_PKEY_CTX *ctx)
455 {
456     OSSL_HMAC_PKEY_CTX *hctx;
457
458     hctx = OPENSSL_zalloc(sizeof(*hctx));
459     if (hctx == NULL)
460         return 0;
461     hctx->ktmp.type = V_ASN1_OCTET_STRING;
462     hctx->ctx = HMAC_CTX_new();
463     EVP_PKEY_CTX_set_data(ctx, hctx);
464     EVP_PKEY_CTX_set0_keygen_info(ctx, NULL, 0);
465 # ifdef TEST_ENG_OPENSSL_HMAC_INIT
466     fprintf(stderr, "(TEST_ENG_OPENSSL_HMAC) ossl_hmac_init() called\n");
467 # endif
468     return 1;
469 }
470
471 static int ossl_hmac_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src)
472 {
473     OSSL_HMAC_PKEY_CTX *sctx, *dctx;
474     if (!ossl_hmac_init(dst))
475         return 0;
476     sctx = EVP_PKEY_CTX_get_data(src);
477     dctx = EVP_PKEY_CTX_get_data(dst);
478     dctx->md = sctx->md;
479     if (!HMAC_CTX_copy(dctx->ctx, sctx->ctx))
480         return 0;
481     if (sctx->ktmp.data) {
482         if (!ASN1_OCTET_STRING_set(&dctx->ktmp,
483                                    sctx->ktmp.data, sctx->ktmp.length))
484             return 0;
485     }
486     return 1;
487 }
488
489 static void ossl_hmac_cleanup(EVP_PKEY_CTX *ctx)
490 {
491     OSSL_HMAC_PKEY_CTX *hctx = EVP_PKEY_CTX_get_data(ctx);
492
493     HMAC_CTX_free(hctx->ctx);
494     OPENSSL_clear_free(hctx->ktmp.data, hctx->ktmp.length);
495     OPENSSL_free(hctx);
496 }
497
498 static int ossl_hmac_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
499 {
500     ASN1_OCTET_STRING *hkey = NULL;
501     OSSL_HMAC_PKEY_CTX *hctx = EVP_PKEY_CTX_get_data(ctx);
502     if (!hctx->ktmp.data)
503         return 0;
504     hkey = ASN1_OCTET_STRING_dup(&hctx->ktmp);
505     if (!hkey)
506         return 0;
507     EVP_PKEY_assign(pkey, EVP_PKEY_HMAC, hkey);
508
509     return 1;
510 }
511
512 static int ossl_int_update(EVP_MD_CTX *ctx, const void *data, size_t count)
513 {
514     OSSL_HMAC_PKEY_CTX *hctx = EVP_PKEY_CTX_get_data(EVP_MD_CTX_pkey_ctx(ctx));
515     if (!HMAC_Update(hctx->ctx, data, count))
516         return 0;
517     return 1;
518 }
519
520 static int ossl_hmac_signctx_init(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx)
521 {
522     EVP_MD_CTX_set_flags(mctx, EVP_MD_CTX_FLAG_NO_INIT);
523     EVP_MD_CTX_set_update_fn(mctx, ossl_int_update);
524     return 1;
525 }
526
527 static int ossl_hmac_signctx(EVP_PKEY_CTX *ctx, unsigned char *sig,
528                              size_t *siglen, EVP_MD_CTX *mctx)
529 {
530     unsigned int hlen;
531     OSSL_HMAC_PKEY_CTX *hctx = EVP_PKEY_CTX_get_data(ctx);
532     int l = EVP_MD_CTX_size(mctx);
533
534     if (l < 0)
535         return 0;
536     *siglen = l;
537     if (!sig)
538         return 1;
539
540     if (!HMAC_Final(hctx->ctx, sig, &hlen))
541         return 0;
542     *siglen = (size_t)hlen;
543     return 1;
544 }
545
546 static int ossl_hmac_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
547 {
548     OSSL_HMAC_PKEY_CTX *hctx = EVP_PKEY_CTX_get_data(ctx);
549     EVP_PKEY *pk;
550     ASN1_OCTET_STRING *key;
551     switch (type) {
552
553     case EVP_PKEY_CTRL_SET_MAC_KEY:
554         if ((!p2 && p1 > 0) || (p1 < -1))
555             return 0;
556         if (!ASN1_OCTET_STRING_set(&hctx->ktmp, p2, p1))
557             return 0;
558         break;
559
560     case EVP_PKEY_CTRL_MD:
561         hctx->md = p2;
562         break;
563
564     case EVP_PKEY_CTRL_DIGESTINIT:
565         pk = EVP_PKEY_CTX_get0_pkey(ctx);
566         key = EVP_PKEY_get0(pk);
567         if (!HMAC_Init_ex(hctx->ctx, key->data, key->length, hctx->md, NULL))
568             return 0;
569         break;
570
571     default:
572         return -2;
573
574     }
575     return 1;
576 }
577
578 static int ossl_hmac_ctrl_str(EVP_PKEY_CTX *ctx,
579                               const char *type, const char *value)
580 {
581     if (!value) {
582         return 0;
583     }
584     if (strcmp(type, "key") == 0) {
585         void *p = (void *)value;
586         return ossl_hmac_ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY, -1, p);
587     }
588     if (strcmp(type, "hexkey") == 0) {
589         unsigned char *key;
590         int r;
591         long keylen;
592         key = string_to_hex(value, &keylen);
593         if (!key)
594             return 0;
595         r = ossl_hmac_ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY, keylen, key);
596         OPENSSL_free(key);
597         return r;
598     }
599     return -2;
600 }
601
602 static EVP_PKEY_METHOD *ossl_hmac_meth;
603
604 static int ossl_register_hmac_meth(void)
605 {
606     EVP_PKEY_METHOD *meth;
607     meth = EVP_PKEY_meth_new(EVP_PKEY_HMAC, 0);
608     if (meth == NULL)
609         return 0;
610     EVP_PKEY_meth_set_init(meth, ossl_hmac_init);
611     EVP_PKEY_meth_set_copy(meth, ossl_hmac_copy);
612     EVP_PKEY_meth_set_cleanup(meth, ossl_hmac_cleanup);
613
614     EVP_PKEY_meth_set_keygen(meth, 0, ossl_hmac_keygen);
615
616     EVP_PKEY_meth_set_signctx(meth, ossl_hmac_signctx_init,
617                               ossl_hmac_signctx);
618
619     EVP_PKEY_meth_set_ctrl(meth, ossl_hmac_ctrl, ossl_hmac_ctrl_str);
620     ossl_hmac_meth = meth;
621     return 1;
622 }
623
624 static int ossl_pkey_meths(ENGINE *e, EVP_PKEY_METHOD **pmeth,
625                            const int **nids, int nid)
626 {
627     static int ossl_pkey_nids[] = {
628         EVP_PKEY_HMAC,
629         0
630     };
631     if (!pmeth) {
632         *nids = ossl_pkey_nids;
633         return 1;
634     }
635
636     if (nid == EVP_PKEY_HMAC) {
637         *pmeth = ossl_hmac_meth;
638         return 1;
639     }
640
641     *pmeth = NULL;
642     return 0;
643 }
644
645 #endif
646
647 int openssl_destroy(ENGINE *e)
648 {
649     test_sha_md_destroy();
650     return 1;
651 }
652