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