Auto init/deinit libcrypto
[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/dso.h>
69 #include <openssl/pem.h>
70 #include <openssl/evp.h>
71 #include <openssl/rand.h>
72 #ifndef OPENSSL_NO_RSA
73 # include <openssl/rsa.h>
74 #endif
75 #ifndef OPENSSL_NO_DSA
76 # include <openssl/dsa.h>
77 #endif
78 #ifndef OPENSSL_NO_DH
79 # include <openssl/dh.h>
80 #endif
81
82 #include <openssl/hmac.h>
83 #include <openssl/x509v3.h>
84
85 /*
86  * This testing gunk is implemented (and explained) lower down. It also
87  * assumes the application explicitly calls "ENGINE_load_openssl()" because
88  * this is no longer automatic in ENGINE_load_builtin_engines().
89  */
90 #define TEST_ENG_OPENSSL_RC4
91 #ifndef OPENSSL_NO_STDIO
92 #define TEST_ENG_OPENSSL_PKEY
93 #endif
94 /* #define TEST_ENG_OPENSSL_HMAC */
95 /* #define TEST_ENG_OPENSSL_HMAC_INIT */
96 /* #define TEST_ENG_OPENSSL_RC4_OTHERS */
97 #define TEST_ENG_OPENSSL_RC4_P_INIT
98 /* #define TEST_ENG_OPENSSL_RC4_P_CIPHER */
99 #define TEST_ENG_OPENSSL_SHA
100 /* #define TEST_ENG_OPENSSL_SHA_OTHERS */
101 /* #define TEST_ENG_OPENSSL_SHA_P_INIT */
102 /* #define TEST_ENG_OPENSSL_SHA_P_UPDATE */
103 /* #define TEST_ENG_OPENSSL_SHA_P_FINAL */
104
105 /* Now check what of those algorithms are actually enabled */
106 #ifdef OPENSSL_NO_RC4
107 # undef TEST_ENG_OPENSSL_RC4
108 # undef TEST_ENG_OPENSSL_RC4_OTHERS
109 # undef TEST_ENG_OPENSSL_RC4_P_INIT
110 # undef TEST_ENG_OPENSSL_RC4_P_CIPHER
111 #endif
112
113 static int openssl_destroy(ENGINE *e);
114
115 #ifdef TEST_ENG_OPENSSL_RC4
116 static int openssl_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
117                            const int **nids, int nid);
118 #endif
119 #ifdef TEST_ENG_OPENSSL_SHA
120 static int openssl_digests(ENGINE *e, const EVP_MD **digest,
121                            const int **nids, int nid);
122 #endif
123
124 #ifdef TEST_ENG_OPENSSL_PKEY
125 static EVP_PKEY *openssl_load_privkey(ENGINE *eng, const char *key_id,
126                                       UI_METHOD *ui_method,
127                                       void *callback_data);
128 #endif
129
130 #ifdef TEST_ENG_OPENSSL_HMAC
131 static int ossl_register_hmac_meth(void);
132 static int ossl_pkey_meths(ENGINE *e, EVP_PKEY_METHOD **pmeth,
133                            const int **nids, int nid);
134 #endif
135
136 /* The constants used when creating the ENGINE */
137 static const char *engine_openssl_id = "openssl";
138 static const char *engine_openssl_name = "Software engine support";
139
140 /*
141  * This internal function is used by ENGINE_openssl() and possibly by the
142  * "dynamic" ENGINE support too
143  */
144 static int bind_helper(ENGINE *e)
145 {
146     if (!ENGINE_set_id(e, engine_openssl_id)
147         || !ENGINE_set_name(e, engine_openssl_name)
148         || !ENGINE_set_destroy_function(e, openssl_destroy)
149 #ifndef TEST_ENG_OPENSSL_NO_ALGORITHMS
150 # ifndef OPENSSL_NO_RSA
151         || !ENGINE_set_RSA(e, RSA_get_default_method())
152 # endif
153 # ifndef OPENSSL_NO_DSA
154         || !ENGINE_set_DSA(e, DSA_get_default_method())
155 # endif
156 # ifndef OPENSSL_NO_EC
157         || !ENGINE_set_EC(e, EC_KEY_OpenSSL())
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_internal(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 typedef struct {
245     unsigned char key[TEST_RC4_KEY_SIZE];
246     RC4_KEY ks;
247 } TEST_RC4_KEY;
248 # define test(ctx) ((TEST_RC4_KEY *)EVP_CIPHER_CTX_cipher_data(ctx))
249 static int test_rc4_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
250                              const unsigned char *iv, int enc)
251 {
252 # ifdef TEST_ENG_OPENSSL_RC4_P_INIT
253     fprintf(stderr, "(TEST_ENG_OPENSSL_RC4) test_init_key() called\n");
254 # endif
255     memcpy(&test(ctx)->key[0], key, EVP_CIPHER_CTX_key_length(ctx));
256     RC4_set_key(&test(ctx)->ks, EVP_CIPHER_CTX_key_length(ctx),
257                 test(ctx)->key);
258     return 1;
259 }
260
261 static int test_rc4_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
262                            const unsigned char *in, size_t inl)
263 {
264 # ifdef TEST_ENG_OPENSSL_RC4_P_CIPHER
265     fprintf(stderr, "(TEST_ENG_OPENSSL_RC4) test_cipher() called\n");
266 # endif
267     RC4(&test(ctx)->ks, inl, in, out);
268     return 1;
269 }
270
271 static EVP_CIPHER *r4_cipher = NULL;
272 static const EVP_CIPHER *test_r4_cipher(void)
273 {
274     if (r4_cipher == NULL) {
275         EVP_CIPHER *cipher;
276
277         if ((cipher = EVP_CIPHER_meth_new(NID_rc4, 1, TEST_RC4_KEY_SIZE)) == NULL
278             || !EVP_CIPHER_meth_set_iv_length(cipher, 0)
279             || !EVP_CIPHER_meth_set_flags(cipher, EVP_CIPH_VARIABLE_LENGTH)
280             || !EVP_CIPHER_meth_set_init(cipher, test_rc4_init_key)
281             || !EVP_CIPHER_meth_set_do_cipher(cipher, test_rc4_cipher)
282             || !EVP_CIPHER_meth_set_impl_ctx_size(cipher, sizeof(TEST_RC4_KEY))) {
283             EVP_CIPHER_meth_free(cipher);
284             cipher = NULL;
285         }
286         r4_cipher = cipher;
287     }
288     return r4_cipher;
289 }
290 static void test_r4_cipher_destroy(void)
291 {
292     EVP_CIPHER_meth_free(r4_cipher);
293     r4_cipher = NULL;
294 }
295
296 static EVP_CIPHER *r4_40_cipher = NULL;
297 static const EVP_CIPHER *test_r4_40_cipher(void)
298 {
299     if (r4_40_cipher == NULL) {
300         EVP_CIPHER *cipher;
301
302         if ((cipher = EVP_CIPHER_meth_new(NID_rc4, 1, 5 /* 40 bits */)) == NULL
303             || !EVP_CIPHER_meth_set_iv_length(cipher, 0)
304             || !EVP_CIPHER_meth_set_flags(cipher, EVP_CIPH_VARIABLE_LENGTH)
305             || !EVP_CIPHER_meth_set_init(cipher, test_rc4_init_key)
306             || !EVP_CIPHER_meth_set_do_cipher(cipher, test_rc4_cipher)
307             || !EVP_CIPHER_meth_set_impl_ctx_size(cipher, sizeof(TEST_RC4_KEY))) {
308             EVP_CIPHER_meth_free(cipher);
309             cipher = NULL;
310         }
311         r4_40_cipher = cipher;
312     }
313     return r4_40_cipher;
314 }
315 static void test_r4_40_cipher_destroy(void)
316 {
317     EVP_CIPHER_meth_free(r4_40_cipher);
318     r4_40_cipher = NULL;
319 }
320 static int test_cipher_nids(const int **nids)
321 {
322     static int cipher_nids[4] = { 0, 0, 0 };
323     static int pos = 0;
324     static int init = 0;
325
326     if (!init) {
327         const EVP_CIPHER *cipher;
328         if ((cipher = test_r4_cipher()) != NULL)
329             cipher_nids[pos++] = EVP_CIPHER_nid(cipher);
330         if ((cipher = test_r4_40_cipher()) != NULL)
331             cipher_nids[pos++] = EVP_CIPHER_nid(cipher);
332         cipher_nids[pos] = 0;
333         init = 1;
334     }
335     *nids = cipher_nids;
336     return pos;
337 }
338
339 static int openssl_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
340                            const int **nids, int nid)
341 {
342     if (!cipher) {
343         /* We are returning a list of supported nids */
344         return test_cipher_nids(nids);
345     }
346     /* We are being asked for a specific cipher */
347     if (nid == NID_rc4)
348         *cipher = test_r4_cipher();
349     else if (nid == NID_rc4_40)
350         *cipher = test_r4_40_cipher();
351     else {
352 # ifdef TEST_ENG_OPENSSL_RC4_OTHERS
353         fprintf(stderr, "(TEST_ENG_OPENSSL_RC4) returning NULL for "
354                 "nid %d\n", nid);
355 # endif
356         *cipher = NULL;
357         return 0;
358     }
359     return 1;
360 }
361 #endif
362
363 #ifdef TEST_ENG_OPENSSL_SHA
364 /* Much the same sort of comment as for TEST_ENG_OPENSSL_RC4 */
365 # include <openssl/sha.h>
366
367 static int test_sha1_init(EVP_MD_CTX *ctx)
368 {
369 # ifdef TEST_ENG_OPENSSL_SHA_P_INIT
370     fprintf(stderr, "(TEST_ENG_OPENSSL_SHA) test_sha1_init() called\n");
371 # endif
372     return SHA1_Init(EVP_MD_CTX_md_data(ctx));
373 }
374
375 static int test_sha1_update(EVP_MD_CTX *ctx, const void *data, size_t count)
376 {
377 # ifdef TEST_ENG_OPENSSL_SHA_P_UPDATE
378     fprintf(stderr, "(TEST_ENG_OPENSSL_SHA) test_sha1_update() called\n");
379 # endif
380     return SHA1_Update(EVP_MD_CTX_md_data(ctx), data, count);
381 }
382
383 static int test_sha1_final(EVP_MD_CTX *ctx, unsigned char *md)
384 {
385 # ifdef TEST_ENG_OPENSSL_SHA_P_FINAL
386     fprintf(stderr, "(TEST_ENG_OPENSSL_SHA) test_sha1_final() called\n");
387 # endif
388     return SHA1_Final(md, EVP_MD_CTX_md_data(ctx));
389 }
390
391 static EVP_MD *sha1_md = NULL;
392 static const EVP_MD *test_sha_md(void)
393 {
394     if (sha1_md == NULL) {
395         EVP_MD *md;
396
397         if ((md = EVP_MD_meth_new(NID_sha1, NID_sha1WithRSAEncryption)) == NULL
398             || !EVP_MD_meth_set_result_size(md, SHA_DIGEST_LENGTH)
399             || !EVP_MD_meth_set_input_blocksize(md, SHA_CBLOCK)
400             || !EVP_MD_meth_set_app_datasize(md,
401                                              sizeof(EVP_MD *) + sizeof(SHA_CTX))
402             || !EVP_MD_meth_set_flags(md, 0)
403             || !EVP_MD_meth_set_init(md, test_sha1_init)
404             || !EVP_MD_meth_set_update(md, test_sha1_update)
405             || !EVP_MD_meth_set_final(md, test_sha1_final)) {
406             EVP_MD_meth_free(md);
407             md = NULL;
408         }
409         sha1_md = md;
410     }
411     return sha1_md;
412 }
413 static void test_sha_md_destroy(void)
414 {
415     EVP_MD_meth_free(sha1_md);
416     sha1_md = NULL;
417 }
418 static int test_digest_nids(const int **nids)
419 {
420     static int digest_nids[2] = { 0, 0 };
421     static int pos = 0;
422     static int init = 0;
423
424     if (!init) {
425         const EVP_MD *md;
426         if ((md = test_sha_md()) != NULL)
427             digest_nids[pos++] = EVP_MD_type(md);
428         digest_nids[pos] = 0;
429         init = 1;
430     }
431     *nids = digest_nids;
432     return pos;
433 }
434
435 static int openssl_digests(ENGINE *e, const EVP_MD **digest,
436                            const int **nids, int nid)
437 {
438     if (!digest) {
439         /* We are returning a list of supported nids */
440         return test_digest_nids(nids);
441     }
442     /* We are being asked for a specific digest */
443     if (nid == NID_sha1)
444         *digest = test_sha_md();
445     else {
446 # ifdef TEST_ENG_OPENSSL_SHA_OTHERS
447         fprintf(stderr, "(TEST_ENG_OPENSSL_SHA) returning NULL for "
448                 "nid %d\n", nid);
449 # endif
450         *digest = NULL;
451         return 0;
452     }
453     return 1;
454 }
455 #endif
456
457 #ifdef TEST_ENG_OPENSSL_PKEY
458 static EVP_PKEY *openssl_load_privkey(ENGINE *eng, const char *key_id,
459                                       UI_METHOD *ui_method,
460                                       void *callback_data)
461 {
462     BIO *in;
463     EVP_PKEY *key;
464     fprintf(stderr, "(TEST_ENG_OPENSSL_PKEY)Loading Private key %s\n",
465             key_id);
466     in = BIO_new_file(key_id, "r");
467     if (!in)
468         return NULL;
469     key = PEM_read_bio_PrivateKey(in, NULL, 0, NULL);
470     BIO_free(in);
471     return key;
472 }
473 #endif
474
475 #ifdef TEST_ENG_OPENSSL_HMAC
476
477 /*
478  * Experimental HMAC redirection implementation: mainly copied from
479  * hm_pmeth.c
480  */
481
482 /* HMAC pkey context structure */
483
484 typedef struct {
485     const EVP_MD *md;           /* MD for HMAC use */
486     ASN1_OCTET_STRING ktmp;     /* Temp storage for key */
487     HMAC_CTX *ctx;
488 } OSSL_HMAC_PKEY_CTX;
489
490 static int ossl_hmac_init(EVP_PKEY_CTX *ctx)
491 {
492     OSSL_HMAC_PKEY_CTX *hctx;
493
494     hctx = OPENSSL_zalloc(sizeof(*hctx));
495     if (hctx == NULL)
496         return 0;
497     hctx->ktmp.type = V_ASN1_OCTET_STRING;
498     hctx->ctx = HMAC_CTX_new();
499     EVP_PKEY_CTX_set_data(ctx, hctx);
500     EVP_PKEY_CTX_set0_keygen_info(ctx, NULL, 0);
501 # ifdef TEST_ENG_OPENSSL_HMAC_INIT
502     fprintf(stderr, "(TEST_ENG_OPENSSL_HMAC) ossl_hmac_init() called\n");
503 # endif
504     return 1;
505 }
506
507 static int ossl_hmac_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src)
508 {
509     OSSL_HMAC_PKEY_CTX *sctx, *dctx;
510     if (!ossl_hmac_init(dst))
511         return 0;
512     sctx = EVP_PKEY_CTX_get_data(src);
513     dctx = EVP_PKEY_CTX_get_data(dst);
514     dctx->md = sctx->md;
515     if (!HMAC_CTX_copy(dctx->ctx, sctx->ctx))
516         return 0;
517     if (sctx->ktmp.data) {
518         if (!ASN1_OCTET_STRING_set(&dctx->ktmp,
519                                    sctx->ktmp.data, sctx->ktmp.length))
520             return 0;
521     }
522     return 1;
523 }
524
525 static void ossl_hmac_cleanup(EVP_PKEY_CTX *ctx)
526 {
527     OSSL_HMAC_PKEY_CTX *hctx = EVP_PKEY_CTX_get_data(ctx);
528
529     HMAC_CTX_free(hctx->ctx);
530     OPENSSL_clear_free(hctx->ktmp.data, hctx->ktmp.length);
531     OPENSSL_free(hctx);
532 }
533
534 static int ossl_hmac_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
535 {
536     ASN1_OCTET_STRING *hkey = NULL;
537     OSSL_HMAC_PKEY_CTX *hctx = EVP_PKEY_CTX_get_data(ctx);
538     if (!hctx->ktmp.data)
539         return 0;
540     hkey = ASN1_OCTET_STRING_dup(&hctx->ktmp);
541     if (!hkey)
542         return 0;
543     EVP_PKEY_assign(pkey, EVP_PKEY_HMAC, hkey);
544
545     return 1;
546 }
547
548 static int ossl_int_update(EVP_MD_CTX *ctx, const void *data, size_t count)
549 {
550     OSSL_HMAC_PKEY_CTX *hctx = EVP_PKEY_CTX_get_data(EVP_MD_CTX_pkey_ctx(ctx));
551     if (!HMAC_Update(hctx->ctx, data, count))
552         return 0;
553     return 1;
554 }
555
556 static int ossl_hmac_signctx_init(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx)
557 {
558     EVP_MD_CTX_set_flags(mctx, EVP_MD_CTX_FLAG_NO_INIT);
559     EVP_MD_CTX_set_update_fn(mctx, ossl_int_update);
560     return 1;
561 }
562
563 static int ossl_hmac_signctx(EVP_PKEY_CTX *ctx, unsigned char *sig,
564                              size_t *siglen, EVP_MD_CTX *mctx)
565 {
566     unsigned int hlen;
567     OSSL_HMAC_PKEY_CTX *hctx = EVP_PKEY_CTX_get_data(ctx);
568     int l = EVP_MD_CTX_size(mctx);
569
570     if (l < 0)
571         return 0;
572     *siglen = l;
573     if (!sig)
574         return 1;
575
576     if (!HMAC_Final(hctx->ctx, sig, &hlen))
577         return 0;
578     *siglen = (size_t)hlen;
579     return 1;
580 }
581
582 static int ossl_hmac_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
583 {
584     OSSL_HMAC_PKEY_CTX *hctx = EVP_PKEY_CTX_get_data(ctx);
585     EVP_PKEY *pk;
586     ASN1_OCTET_STRING *key;
587     switch (type) {
588
589     case EVP_PKEY_CTRL_SET_MAC_KEY:
590         if ((!p2 && p1 > 0) || (p1 < -1))
591             return 0;
592         if (!ASN1_OCTET_STRING_set(&hctx->ktmp, p2, p1))
593             return 0;
594         break;
595
596     case EVP_PKEY_CTRL_MD:
597         hctx->md = p2;
598         break;
599
600     case EVP_PKEY_CTRL_DIGESTINIT:
601         pk = EVP_PKEY_CTX_get0_pkey(ctx);
602         key = EVP_PKEY_get0(pk);
603         if (!HMAC_Init_ex(hctx->ctx, key->data, key->length, hctx->md, NULL))
604             return 0;
605         break;
606
607     default:
608         return -2;
609
610     }
611     return 1;
612 }
613
614 static int ossl_hmac_ctrl_str(EVP_PKEY_CTX *ctx,
615                               const char *type, const char *value)
616 {
617     if (!value) {
618         return 0;
619     }
620     if (strcmp(type, "key") == 0) {
621         void *p = (void *)value;
622         return ossl_hmac_ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY, -1, p);
623     }
624     if (strcmp(type, "hexkey") == 0) {
625         unsigned char *key;
626         int r;
627         long keylen;
628         key = string_to_hex(value, &keylen);
629         if (!key)
630             return 0;
631         r = ossl_hmac_ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY, keylen, key);
632         OPENSSL_free(key);
633         return r;
634     }
635     return -2;
636 }
637
638 static EVP_PKEY_METHOD *ossl_hmac_meth;
639
640 static int ossl_register_hmac_meth(void)
641 {
642     EVP_PKEY_METHOD *meth;
643     meth = EVP_PKEY_meth_new(EVP_PKEY_HMAC, 0);
644     if (meth == NULL)
645         return 0;
646     EVP_PKEY_meth_set_init(meth, ossl_hmac_init);
647     EVP_PKEY_meth_set_copy(meth, ossl_hmac_copy);
648     EVP_PKEY_meth_set_cleanup(meth, ossl_hmac_cleanup);
649
650     EVP_PKEY_meth_set_keygen(meth, 0, ossl_hmac_keygen);
651
652     EVP_PKEY_meth_set_signctx(meth, ossl_hmac_signctx_init,
653                               ossl_hmac_signctx);
654
655     EVP_PKEY_meth_set_ctrl(meth, ossl_hmac_ctrl, ossl_hmac_ctrl_str);
656     ossl_hmac_meth = meth;
657     return 1;
658 }
659
660 static int ossl_pkey_meths(ENGINE *e, EVP_PKEY_METHOD **pmeth,
661                            const int **nids, int nid)
662 {
663     static int ossl_pkey_nids[] = {
664         EVP_PKEY_HMAC,
665         0
666     };
667     if (!pmeth) {
668         *nids = ossl_pkey_nids;
669         return 1;
670     }
671
672     if (nid == EVP_PKEY_HMAC) {
673         *pmeth = ossl_hmac_meth;
674         return 1;
675     }
676
677     *pmeth = NULL;
678     return 0;
679 }
680
681 #endif
682
683 int openssl_destroy(ENGINE *e)
684 {
685     test_sha_md_destroy();
686     test_r4_cipher_destroy();
687     test_r4_40_cipher_destroy();
688     return 1;
689 }
690