Further comment amendments to preserve formatting prior to source reformat
[openssl.git] / crypto / engine / eng_openssl.c
1 /* crypto/engine/eng_openssl.c */
2 /* Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL
3  * project 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
65 #include <stdio.h>
66 #include <openssl/crypto.h>
67 #include "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 /* This testing gunk is implemented (and explained) lower down. It also assumes
87  * the application explicitly calls "ENGINE_load_openssl()" because this is no
88  * longer automatic in ENGINE_load_builtin_engines(). */
89 #define TEST_ENG_OPENSSL_RC4
90 #define TEST_ENG_OPENSSL_PKEY
91 /* #define TEST_ENG_OPENSSL_HMAC */
92 /* #define TEST_ENG_OPENSSL_HMAC_INIT */
93 /* #define TEST_ENG_OPENSSL_RC4_OTHERS */
94 #define TEST_ENG_OPENSSL_RC4_P_INIT
95 /* #define TEST_ENG_OPENSSL_RC4_P_CIPHER */
96 #define TEST_ENG_OPENSSL_SHA
97 /* #define TEST_ENG_OPENSSL_SHA_OTHERS */
98 /* #define TEST_ENG_OPENSSL_SHA_P_INIT */
99 /* #define TEST_ENG_OPENSSL_SHA_P_UPDATE */
100 /* #define TEST_ENG_OPENSSL_SHA_P_FINAL */
101
102 /* Now check what of those algorithms are actually enabled */
103 #ifdef OPENSSL_NO_RC4
104 #undef TEST_ENG_OPENSSL_RC4
105 #undef TEST_ENG_OPENSSL_RC4_OTHERS
106 #undef TEST_ENG_OPENSSL_RC4_P_INIT
107 #undef TEST_ENG_OPENSSL_RC4_P_CIPHER
108 #endif
109 #if defined(OPENSSL_NO_SHA) || defined(OPENSSL_NO_SHA0) || defined(OPENSSL_NO_SHA1)
110 #undef TEST_ENG_OPENSSL_SHA
111 #undef TEST_ENG_OPENSSL_SHA_OTHERS
112 #undef TEST_ENG_OPENSSL_SHA_P_INIT
113 #undef TEST_ENG_OPENSSL_SHA_P_UPDATE
114 #undef TEST_ENG_OPENSSL_SHA_P_FINAL 
115 #endif
116
117 #ifdef TEST_ENG_OPENSSL_RC4
118 static int openssl_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
119                                 const int **nids, int nid);
120 #endif
121 #ifdef TEST_ENG_OPENSSL_SHA
122 static int openssl_digests(ENGINE *e, const EVP_MD **digest,
123                                 const int **nids, int nid);
124 #endif
125
126 #ifdef TEST_ENG_OPENSSL_PKEY
127 static EVP_PKEY *openssl_load_privkey(ENGINE *eng, const char *key_id,
128         UI_METHOD *ui_method, 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 /* This internal function is used by ENGINE_openssl() and possibly by the
142  * "dynamic" ENGINE support too */
143 static int bind_helper(ENGINE *e)
144         {
145         if(!ENGINE_set_id(e, engine_openssl_id)
146                         || !ENGINE_set_name(e, engine_openssl_name)
147 #ifndef TEST_ENG_OPENSSL_NO_ALGORITHMS
148 #ifndef OPENSSL_NO_RSA
149                         || !ENGINE_set_RSA(e, RSA_get_default_method())
150 #endif
151 #ifndef OPENSSL_NO_DSA
152                         || !ENGINE_set_DSA(e, DSA_get_default_method())
153 #endif
154 #ifndef OPENSSL_NO_ECDH
155                         || !ENGINE_set_ECDH(e, ECDH_OpenSSL())
156 #endif
157 #ifndef OPENSSL_NO_ECDSA
158                         || !ENGINE_set_ECDSA(e, ECDSA_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_SSLeay())
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         /* If we add errors to this ENGINE, ensure the error handling is setup here */
181         /* openssl_load_error_strings(); */
182         return 1;
183         }
184
185 static ENGINE *engine_openssl(void)
186         {
187         ENGINE *ret = ENGINE_new();
188         if(!ret)
189                 return NULL;
190         if(!bind_helper(ret))
191                 {
192                 ENGINE_free(ret);
193                 return NULL;
194                 }
195         return ret;
196         }
197
198 void ENGINE_load_openssl(void)
199         {
200         ENGINE *toadd = engine_openssl();
201         if(!toadd) return;
202         ENGINE_add(toadd);
203         /* If the "add" worked, it gets a structural reference. So either way,
204          * we release our just-created reference. */
205         ENGINE_free(toadd);
206         ERR_clear_error();
207         }
208
209 /* This stuff is needed if this ENGINE is being compiled into a self-contained
210  * shared-library. */
211 #ifdef ENGINE_DYNAMIC_SUPPORT
212 static int bind_fn(ENGINE *e, const char *id)
213         {
214         if(id && (strcmp(id, engine_openssl_id) != 0))
215                 return 0;
216         if(!bind_helper(e))
217                 return 0;
218         return 1;
219         }
220 IMPLEMENT_DYNAMIC_CHECK_FN()
221 IMPLEMENT_DYNAMIC_BIND_FN(bind_fn)
222 #endif /* ENGINE_DYNAMIC_SUPPORT */
223
224 #ifdef TEST_ENG_OPENSSL_RC4
225 /*-
226  * This section of code compiles an "alternative implementation" of two modes of
227  * RC4 into this ENGINE. The result is that EVP_CIPHER operation for "rc4"
228  * should under normal circumstances go via this support rather than the default
229  * EVP support. There are other symbols to tweak the testing;
230  *    TEST_ENC_OPENSSL_RC4_OTHERS - print a one line message to stderr each time
231  *        we're asked for a cipher we don't support (should not happen).
232  *    TEST_ENG_OPENSSL_RC4_P_INIT - print a one line message to stderr each time
233  *        the "init_key" handler is called.
234  *    TEST_ENG_OPENSSL_RC4_P_CIPHER - ditto for the "cipher" handler.
235  */
236 #include <openssl/rc4.h>
237 #define TEST_RC4_KEY_SIZE               16
238 static const int test_cipher_nids[] = {NID_rc4,NID_rc4_40};
239 static const int test_cipher_nids_number = 2;
240 typedef struct {
241         unsigned char key[TEST_RC4_KEY_SIZE];
242         RC4_KEY ks;
243         } TEST_RC4_KEY;
244 #define test(ctx) ((TEST_RC4_KEY *)(ctx)->cipher_data)
245 static int test_rc4_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
246                         const unsigned char *iv, int enc)
247         {
248 #ifdef TEST_ENG_OPENSSL_RC4_P_INIT
249         fprintf(stderr, "(TEST_ENG_OPENSSL_RC4) test_init_key() called\n");
250 #endif
251         memcpy(&test(ctx)->key[0],key,EVP_CIPHER_CTX_key_length(ctx));
252         RC4_set_key(&test(ctx)->ks,EVP_CIPHER_CTX_key_length(ctx),
253                 test(ctx)->key);
254         return 1;
255         }
256 static int test_rc4_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
257                       const unsigned char *in, size_t inl)
258         {
259 #ifdef TEST_ENG_OPENSSL_RC4_P_CIPHER
260         fprintf(stderr, "(TEST_ENG_OPENSSL_RC4) test_cipher() called\n");
261 #endif
262         RC4(&test(ctx)->ks,inl,in,out);
263         return 1;
264         }
265 static const EVP_CIPHER test_r4_cipher=
266         {
267         NID_rc4,
268         1,TEST_RC4_KEY_SIZE,0,
269         EVP_CIPH_VARIABLE_LENGTH,
270         test_rc4_init_key,
271         test_rc4_cipher,
272         NULL,
273         sizeof(TEST_RC4_KEY),
274         NULL,
275         NULL,
276         NULL,
277         NULL
278         };
279 static const EVP_CIPHER test_r4_40_cipher=
280         {
281         NID_rc4_40,
282         1,5 /* 40 bit */,0,
283         EVP_CIPH_VARIABLE_LENGTH,
284         test_rc4_init_key,
285         test_rc4_cipher,
286         NULL,
287         sizeof(TEST_RC4_KEY),
288         NULL, 
289         NULL,
290         NULL,
291         NULL
292         };
293 static int openssl_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
294                         const int **nids, int nid)
295         {
296         if(!cipher)
297                 {
298                 /* We are returning a list of supported nids */
299                 *nids = test_cipher_nids;
300                 return test_cipher_nids_number;
301                 }
302         /* We are being asked for a specific cipher */
303         if(nid == NID_rc4)
304                 *cipher = &test_r4_cipher;
305         else if(nid == NID_rc4_40)
306                 *cipher = &test_r4_40_cipher;
307         else
308                 {
309 #ifdef TEST_ENG_OPENSSL_RC4_OTHERS
310                 fprintf(stderr, "(TEST_ENG_OPENSSL_RC4) returning NULL for "
311                                 "nid %d\n", nid);
312 #endif
313                 *cipher = NULL;
314                 return 0;
315                 }
316         return 1;
317         }
318 #endif
319
320 #ifdef TEST_ENG_OPENSSL_SHA
321 /* Much the same sort of comment as for TEST_ENG_OPENSSL_RC4 */
322 #include <openssl/sha.h>
323 static const int test_digest_nids[] = {NID_sha1};
324 static const int test_digest_nids_number = 1;
325 static int test_sha1_init(EVP_MD_CTX *ctx)
326         {
327 #ifdef TEST_ENG_OPENSSL_SHA_P_INIT
328         fprintf(stderr, "(TEST_ENG_OPENSSL_SHA) test_sha1_init() called\n");
329 #endif
330         return SHA1_Init(ctx->md_data);
331         }
332 static int test_sha1_update(EVP_MD_CTX *ctx,const void *data,size_t count)
333         {
334 #ifdef TEST_ENG_OPENSSL_SHA_P_UPDATE
335         fprintf(stderr, "(TEST_ENG_OPENSSL_SHA) test_sha1_update() called\n");
336 #endif
337         return SHA1_Update(ctx->md_data,data,count);
338         }
339 static int test_sha1_final(EVP_MD_CTX *ctx,unsigned char *md)
340         {
341 #ifdef TEST_ENG_OPENSSL_SHA_P_FINAL
342         fprintf(stderr, "(TEST_ENG_OPENSSL_SHA) test_sha1_final() called\n");
343 #endif
344         return SHA1_Final(md,ctx->md_data);
345         }
346 static const EVP_MD test_sha_md=
347         {
348         NID_sha1,
349         NID_sha1WithRSAEncryption,
350         SHA_DIGEST_LENGTH,
351         0,
352         test_sha1_init,
353         test_sha1_update,
354         test_sha1_final,
355         NULL,
356         NULL,
357         EVP_PKEY_RSA_method,
358         SHA_CBLOCK,
359         sizeof(EVP_MD *)+sizeof(SHA_CTX),
360         };
361 static int openssl_digests(ENGINE *e, const EVP_MD **digest,
362                         const int **nids, int nid)
363         {
364         if(!digest)
365                 {
366                 /* We are returning a list of supported nids */
367                 *nids = test_digest_nids;
368                 return test_digest_nids_number;
369                 }
370         /* We are being asked for a specific digest */
371         if(nid == NID_sha1)
372                 *digest = &test_sha_md;
373         else
374                 {
375 #ifdef TEST_ENG_OPENSSL_SHA_OTHERS
376                 fprintf(stderr, "(TEST_ENG_OPENSSL_SHA) returning NULL for "
377                                 "nid %d\n", nid);
378 #endif
379                 *digest = NULL;
380                 return 0;
381                 }
382         return 1;
383         }
384 #endif
385
386 #ifdef TEST_ENG_OPENSSL_PKEY
387 static EVP_PKEY *openssl_load_privkey(ENGINE *eng, const char *key_id,
388         UI_METHOD *ui_method, void *callback_data)
389         {
390         BIO *in;
391         EVP_PKEY *key;
392         fprintf(stderr, "(TEST_ENG_OPENSSL_PKEY)Loading Private key %s\n", key_id);
393         in = BIO_new_file(key_id, "r");
394         if (!in)
395                 return NULL;
396         key = PEM_read_bio_PrivateKey(in, NULL, 0, NULL);
397         BIO_free(in);
398         return key;
399         }
400 #endif
401
402 #ifdef TEST_ENG_OPENSSL_HMAC
403
404 /* Experimental HMAC redirection implementation: mainly copied from
405  * hm_pmeth.c
406  */
407
408 /* HMAC pkey context structure */
409
410 typedef struct
411         {
412         const EVP_MD *md;       /* MD for HMAC use */
413         ASN1_OCTET_STRING ktmp; /* Temp storage for key */
414         HMAC_CTX ctx;
415         } OSSL_HMAC_PKEY_CTX;
416
417 static int ossl_hmac_init(EVP_PKEY_CTX *ctx)
418         {
419         OSSL_HMAC_PKEY_CTX *hctx;
420         hctx = OPENSSL_malloc(sizeof(OSSL_HMAC_PKEY_CTX));
421         if (!hctx)
422                 return 0;
423         hctx->md = NULL;
424         hctx->ktmp.data = NULL;
425         hctx->ktmp.length = 0;
426         hctx->ktmp.flags = 0;
427         hctx->ktmp.type = V_ASN1_OCTET_STRING;
428         HMAC_CTX_init(&hctx->ctx);
429         EVP_PKEY_CTX_set_data(ctx, hctx);
430         EVP_PKEY_CTX_set0_keygen_info(ctx, NULL, 0);
431 #ifdef TEST_ENG_OPENSSL_HMAC_INIT
432         fprintf(stderr, "(TEST_ENG_OPENSSL_HMAC) ossl_hmac_init() called\n");
433 #endif
434         return 1;
435         }
436
437 static int ossl_hmac_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src)
438         {
439         OSSL_HMAC_PKEY_CTX *sctx, *dctx;
440         if (!ossl_hmac_init(dst))
441                 return 0;
442         sctx = EVP_PKEY_CTX_get_data(src);
443         dctx = EVP_PKEY_CTX_get_data(dst);
444         dctx->md = sctx->md;
445         HMAC_CTX_init(&dctx->ctx);
446         if (!HMAC_CTX_copy(&dctx->ctx, &sctx->ctx))
447                 return 0;
448         if (sctx->ktmp.data)
449                 {
450                 if (!ASN1_OCTET_STRING_set(&dctx->ktmp,
451                                         sctx->ktmp.data, sctx->ktmp.length))
452                         return 0;
453                 }
454         return 1;
455         }
456
457 static void ossl_hmac_cleanup(EVP_PKEY_CTX *ctx)
458         {
459         OSSL_HMAC_PKEY_CTX *hctx;
460         hctx = EVP_PKEY_CTX_get_data(ctx);
461         HMAC_CTX_cleanup(&hctx->ctx);
462         if (hctx->ktmp.data)
463                 {
464                 if (hctx->ktmp.length)
465                         OPENSSL_cleanse(hctx->ktmp.data, hctx->ktmp.length);
466                 OPENSSL_free(hctx->ktmp.data);
467                 hctx->ktmp.data = NULL;
468                 }
469         OPENSSL_free(hctx);
470         }
471
472 static int ossl_hmac_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
473         {
474         ASN1_OCTET_STRING *hkey = NULL;
475         OSSL_HMAC_PKEY_CTX *hctx = EVP_PKEY_CTX_get_data(ctx);
476         if (!hctx->ktmp.data)
477                 return 0;
478         hkey = ASN1_OCTET_STRING_dup(&hctx->ktmp);
479         if (!hkey)
480                 return 0;
481         EVP_PKEY_assign(pkey, EVP_PKEY_HMAC, hkey);
482         
483         return 1;
484         }
485
486 static int ossl_int_update(EVP_MD_CTX *ctx,const void *data,size_t count)
487         {
488         OSSL_HMAC_PKEY_CTX *hctx = EVP_PKEY_CTX_get_data(ctx->pctx);
489         if (!HMAC_Update(&hctx->ctx, data, count))
490                 return 0;
491         return 1;
492         }
493
494 static int ossl_hmac_signctx_init(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx)
495         {
496         EVP_MD_CTX_set_flags(mctx, EVP_MD_CTX_FLAG_NO_INIT);
497         mctx->update = ossl_int_update;
498         return 1;
499         }
500
501 static int ossl_hmac_signctx(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
502                                         EVP_MD_CTX *mctx)
503         {
504         unsigned int hlen;
505         OSSL_HMAC_PKEY_CTX *hctx = EVP_PKEY_CTX_get_data(ctx);
506         int l = EVP_MD_CTX_size(mctx);
507
508         if (l < 0)
509                 return 0;
510         *siglen = l;
511         if (!sig)
512                 return 1;
513
514         if (!HMAC_Final(&hctx->ctx, sig, &hlen))
515                 return 0;
516         *siglen = (size_t)hlen;
517         return 1;
518         }
519
520 static int ossl_hmac_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
521         {
522         OSSL_HMAC_PKEY_CTX *hctx = EVP_PKEY_CTX_get_data(ctx);
523         EVP_PKEY *pk;
524         ASN1_OCTET_STRING *key;
525         switch (type)
526                 {
527
528                 case EVP_PKEY_CTRL_SET_MAC_KEY:
529                 if ((!p2 && p1 > 0) || (p1 < -1))
530                         return 0;
531                 if (!ASN1_OCTET_STRING_set(&hctx->ktmp, p2, p1))
532                         return 0;
533                 break;
534
535                 case EVP_PKEY_CTRL_MD:
536                 hctx->md = p2;
537                 break;
538
539                 case EVP_PKEY_CTRL_DIGESTINIT:
540                 pk = EVP_PKEY_CTX_get0_pkey(ctx);
541                 key = EVP_PKEY_get0(pk);
542                 if (!HMAC_Init_ex(&hctx->ctx, key->data, key->length, hctx->md,
543                                 NULL))
544                         return 0;
545                 break;
546
547                 default:
548                 return -2;
549
550                 }
551         return 1;
552         }
553
554 static int ossl_hmac_ctrl_str(EVP_PKEY_CTX *ctx,
555                         const char *type, const char *value)
556         {
557         if (!value)
558                 {
559                 return 0;
560                 }
561         if (!strcmp(type, "key"))
562                 {
563                 void *p = (void *)value;
564                 return ossl_hmac_ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY,
565                                 -1, p);
566                 }
567         if (!strcmp(type, "hexkey"))
568                 {
569                 unsigned char *key;
570                 int r;
571                 long keylen;
572                 key = string_to_hex(value, &keylen);
573                 if (!key)
574                         return 0;
575                 r = ossl_hmac_ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY, keylen, key);
576                 OPENSSL_free(key);
577                 return r;
578                 }
579         return -2;
580         }
581
582 static EVP_PKEY_METHOD *ossl_hmac_meth;
583
584 static int ossl_register_hmac_meth(void)
585         {
586         EVP_PKEY_METHOD *meth;
587         meth = EVP_PKEY_meth_new(EVP_PKEY_HMAC, 0);
588         if (!meth)
589                 return 0;
590         EVP_PKEY_meth_set_init(meth, ossl_hmac_init);
591         EVP_PKEY_meth_set_copy(meth, ossl_hmac_copy);
592         EVP_PKEY_meth_set_cleanup(meth, ossl_hmac_cleanup);
593
594         EVP_PKEY_meth_set_keygen(meth, 0, ossl_hmac_keygen);
595
596         EVP_PKEY_meth_set_signctx(meth, ossl_hmac_signctx_init,
597                                                 ossl_hmac_signctx);
598
599         EVP_PKEY_meth_set_ctrl(meth, ossl_hmac_ctrl, ossl_hmac_ctrl_str);
600         ossl_hmac_meth = meth;
601         return 1;
602         }
603
604 static int ossl_pkey_meths(ENGINE *e, EVP_PKEY_METHOD **pmeth,
605         const int **nids, int nid)
606
607         {
608         static int ossl_pkey_nids[] = 
609                 {
610                 EVP_PKEY_HMAC,
611                 0
612                 };
613         if (!pmeth)
614                 {
615                 *nids = ossl_pkey_nids;
616                 return 1;
617                 }
618
619         if (nid == EVP_PKEY_HMAC)
620                 {
621                 *pmeth = ossl_hmac_meth;
622                 return 1;
623                 }
624
625         *pmeth = NULL;
626         return 0;
627         }
628
629
630 #endif