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