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