1 /* crypto/engine/eng_openssl.c */
2 /* Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL
5 /* ====================================================================
6 * Copyright (c) 1999-2001 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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
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/)"
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.
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.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
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 * ====================================================================
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).
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.
66 #include <openssl/crypto.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>
76 #ifndef OPENSSL_NO_DSA
77 #include <openssl/dsa.h>
80 #include <openssl/dh.h>
83 #include <openssl/hmac.h>
84 #include <openssl/x509v3.h>
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_RC4_OTHERS */
93 #define TEST_ENG_OPENSSL_RC4_P_INIT
94 /* #define TEST_ENG_OPENSSL_RC4_P_CIPHER */
95 #define TEST_ENG_OPENSSL_SHA
96 /* #define TEST_ENG_OPENSSL_SHA_OTHERS */
97 /* #define TEST_ENG_OPENSSL_SHA_P_INIT */
98 /* #define TEST_ENG_OPENSSL_SHA_P_UPDATE */
99 /* #define TEST_ENG_OPENSSL_SHA_P_FINAL */
101 /* Now check what of those algorithms are actually enabled */
102 #ifdef OPENSSL_NO_RC4
103 #undef TEST_ENG_OPENSSL_RC4
104 #undef TEST_ENG_OPENSSL_RC4_OTHERS
105 #undef TEST_ENG_OPENSSL_RC4_P_INIT
106 #undef TEST_ENG_OPENSSL_RC4_P_CIPHER
108 #if defined(OPENSSL_NO_SHA) || defined(OPENSSL_NO_SHA0) || defined(OPENSSL_NO_SHA1)
109 #undef TEST_ENG_OPENSSL_SHA
110 #undef TEST_ENG_OPENSSL_SHA_OTHERS
111 #undef TEST_ENG_OPENSSL_SHA_P_INIT
112 #undef TEST_ENG_OPENSSL_SHA_P_UPDATE
113 #undef TEST_ENG_OPENSSL_SHA_P_FINAL
116 #ifdef TEST_ENG_OPENSSL_RC4
117 static int openssl_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
118 const int **nids, int nid);
120 #ifdef TEST_ENG_OPENSSL_SHA
121 static int openssl_digests(ENGINE *e, const EVP_MD **digest,
122 const int **nids, int nid);
125 #ifdef TEST_ENG_OPENSSL_PKEY
126 static EVP_PKEY *openssl_load_privkey(ENGINE *eng, const char *key_id,
127 UI_METHOD *ui_method, void *callback_data);
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);
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";
140 /* This internal function is used by ENGINE_openssl() and possibly by the
141 * "dynamic" ENGINE support too */
142 static int bind_helper(ENGINE *e)
144 if(!ENGINE_set_id(e, engine_openssl_id)
145 || !ENGINE_set_name(e, engine_openssl_name)
146 #ifndef TEST_ENG_OPENSSL_NO_ALGORITHMS
147 #ifndef OPENSSL_NO_RSA
148 || !ENGINE_set_RSA(e, RSA_get_default_method())
150 #ifndef OPENSSL_NO_DSA
151 || !ENGINE_set_DSA(e, DSA_get_default_method())
153 #ifndef OPENSSL_NO_ECDH
154 || !ENGINE_set_ECDH(e, ECDH_OpenSSL())
156 #ifndef OPENSSL_NO_ECDSA
157 || !ENGINE_set_ECDSA(e, ECDSA_OpenSSL())
159 #ifndef OPENSSL_NO_DH
160 || !ENGINE_set_DH(e, DH_get_default_method())
162 || !ENGINE_set_RAND(e, RAND_SSLeay())
163 #ifdef TEST_ENG_OPENSSL_RC4
164 || !ENGINE_set_ciphers(e, openssl_ciphers)
166 #ifdef TEST_ENG_OPENSSL_SHA
167 || !ENGINE_set_digests(e, openssl_digests)
170 #ifdef TEST_ENG_OPENSSL_PKEY
171 || !ENGINE_set_load_privkey_function(e, openssl_load_privkey)
173 #ifdef TEST_ENG_OPENSSL_HMAC
174 || !ossl_register_hmac_meth()
175 || !ENGINE_set_pkey_meths(e, ossl_pkey_meths)
179 /* If we add errors to this ENGINE, ensure the error handling is setup here */
180 /* openssl_load_error_strings(); */
184 static ENGINE *engine_openssl(void)
186 ENGINE *ret = ENGINE_new();
189 if(!bind_helper(ret))
197 void ENGINE_load_openssl(void)
199 ENGINE *toadd = engine_openssl();
202 /* If the "add" worked, it gets a structural reference. So either way,
203 * we release our just-created reference. */
208 /* This stuff is needed if this ENGINE is being compiled into a self-contained
210 #ifdef ENGINE_DYNAMIC_SUPPORT
211 static int bind_fn(ENGINE *e, const char *id)
213 if(id && (strcmp(id, engine_openssl_id) != 0))
219 IMPLEMENT_DYNAMIC_CHECK_FN()
220 IMPLEMENT_DYNAMIC_BIND_FN(bind_fn)
221 #endif /* ENGINE_DYNAMIC_SUPPORT */
223 #ifdef TEST_ENG_OPENSSL_RC4
224 /* This section of code compiles an "alternative implementation" of two modes of
225 * RC4 into this ENGINE. The result is that EVP_CIPHER operation for "rc4"
226 * should under normal circumstances go via this support rather than the default
227 * EVP support. There are other symbols to tweak the testing;
228 * TEST_ENC_OPENSSL_RC4_OTHERS - print a one line message to stderr each time
229 * we're asked for a cipher we don't support (should not happen).
230 * TEST_ENG_OPENSSL_RC4_P_INIT - print a one line message to stderr each time
231 * the "init_key" handler is called.
232 * TEST_ENG_OPENSSL_RC4_P_CIPHER - ditto for the "cipher" handler.
234 #include <openssl/rc4.h>
235 #define TEST_RC4_KEY_SIZE 16
236 static int test_cipher_nids[] = {NID_rc4,NID_rc4_40};
237 static int test_cipher_nids_number = 2;
239 unsigned char key[TEST_RC4_KEY_SIZE];
242 #define test(ctx) ((TEST_RC4_KEY *)(ctx)->cipher_data)
243 static int test_rc4_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
244 const unsigned char *iv, int enc)
246 #ifdef TEST_ENG_OPENSSL_RC4_P_INIT
247 fprintf(stderr, "(TEST_ENG_OPENSSL_RC4) test_init_key() called\n");
249 memcpy(&test(ctx)->key[0],key,EVP_CIPHER_CTX_key_length(ctx));
250 RC4_set_key(&test(ctx)->ks,EVP_CIPHER_CTX_key_length(ctx),
254 static int test_rc4_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
255 const unsigned char *in, size_t inl)
257 #ifdef TEST_ENG_OPENSSL_RC4_P_CIPHER
258 fprintf(stderr, "(TEST_ENG_OPENSSL_RC4) test_cipher() called\n");
260 RC4(&test(ctx)->ks,inl,in,out);
263 static const EVP_CIPHER test_r4_cipher=
266 1,TEST_RC4_KEY_SIZE,0,
267 EVP_CIPH_VARIABLE_LENGTH,
271 sizeof(TEST_RC4_KEY),
277 static const EVP_CIPHER test_r4_40_cipher=
281 EVP_CIPH_VARIABLE_LENGTH,
285 sizeof(TEST_RC4_KEY),
291 static int openssl_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
292 const int **nids, int nid)
296 /* We are returning a list of supported nids */
297 *nids = test_cipher_nids;
298 return test_cipher_nids_number;
300 /* We are being asked for a specific cipher */
302 *cipher = &test_r4_cipher;
303 else if(nid == NID_rc4_40)
304 *cipher = &test_r4_40_cipher;
307 #ifdef TEST_ENG_OPENSSL_RC4_OTHERS
308 fprintf(stderr, "(TEST_ENG_OPENSSL_RC4) returning NULL for "
318 #ifdef TEST_ENG_OPENSSL_SHA
319 /* Much the same sort of comment as for TEST_ENG_OPENSSL_RC4 */
320 #include <openssl/sha.h>
321 static int test_digest_nids[] = {NID_sha1};
322 static int test_digest_nids_number = 1;
323 static int test_sha1_init(EVP_MD_CTX *ctx)
325 #ifdef TEST_ENG_OPENSSL_SHA_P_INIT
326 fprintf(stderr, "(TEST_ENG_OPENSSL_SHA) test_sha1_init() called\n");
328 return SHA1_Init(ctx->md_data);
330 static int test_sha1_update(EVP_MD_CTX *ctx,const void *data,size_t count)
332 #ifdef TEST_ENG_OPENSSL_SHA_P_UPDATE
333 fprintf(stderr, "(TEST_ENG_OPENSSL_SHA) test_sha1_update() called\n");
335 return SHA1_Update(ctx->md_data,data,count);
337 static int test_sha1_final(EVP_MD_CTX *ctx,unsigned char *md)
339 #ifdef TEST_ENG_OPENSSL_SHA_P_FINAL
340 fprintf(stderr, "(TEST_ENG_OPENSSL_SHA) test_sha1_final() called\n");
342 return SHA1_Final(md,ctx->md_data);
344 static const EVP_MD test_sha_md=
347 NID_sha1WithRSAEncryption,
357 sizeof(EVP_MD *)+sizeof(SHA_CTX),
359 static int openssl_digests(ENGINE *e, const EVP_MD **digest,
360 const int **nids, int nid)
364 /* We are returning a list of supported nids */
365 *nids = test_digest_nids;
366 return test_digest_nids_number;
368 /* We are being asked for a specific digest */
370 *digest = &test_sha_md;
373 #ifdef TEST_ENG_OPENSSL_SHA_OTHERS
374 fprintf(stderr, "(TEST_ENG_OPENSSL_SHA) returning NULL for "
384 #ifdef TEST_ENG_OPENSSL_PKEY
385 static EVP_PKEY *openssl_load_privkey(ENGINE *eng, const char *key_id,
386 UI_METHOD *ui_method, void *callback_data)
390 fprintf(stderr, "(TEST_ENG_OPENSSL_PKEY)Loading Private key %s\n", key_id);
391 in = BIO_new_file(key_id, "r");
394 key = PEM_read_bio_PrivateKey(in, NULL, 0, NULL);
400 #ifdef TEST_ENG_OPENSSL_HMAC
402 /* Experimental HMAC redirection implementation: mainly copied from
406 /* HMAC pkey context structure */
410 const EVP_MD *md; /* MD for HMAC use */
411 ASN1_OCTET_STRING ktmp; /* Temp storage for key */
413 } OSSL_HMAC_PKEY_CTX;
415 static int ossl_hmac_init(EVP_PKEY_CTX *ctx)
417 OSSL_HMAC_PKEY_CTX *hctx;
418 hctx = OPENSSL_malloc(sizeof(OSSL_HMAC_PKEY_CTX));
422 hctx->ktmp.data = NULL;
423 hctx->ktmp.length = 0;
424 hctx->ktmp.flags = 0;
425 hctx->ktmp.type = V_ASN1_OCTET_STRING;
426 HMAC_CTX_init(&hctx->ctx);
427 EVP_PKEY_CTX_set_data(ctx, hctx);
428 EVP_PKEY_CTX_set0_keygen_info(ctx, NULL, 0);
433 static int ossl_hmac_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src)
435 OSSL_HMAC_PKEY_CTX *sctx, *dctx;
436 if (!ossl_hmac_init(dst))
438 sctx = EVP_PKEY_CTX_get_data(src);
439 dctx = EVP_PKEY_CTX_get_data(dst);
441 HMAC_CTX_init(&dctx->ctx);
442 if (!HMAC_CTX_copy(&dctx->ctx, &sctx->ctx))
446 if (!ASN1_OCTET_STRING_set(&dctx->ktmp,
447 sctx->ktmp.data, sctx->ktmp.length))
453 static void ossl_hmac_cleanup(EVP_PKEY_CTX *ctx)
455 OSSL_HMAC_PKEY_CTX *hctx;
456 hctx = EVP_PKEY_CTX_get_data(ctx);
457 HMAC_CTX_cleanup(&hctx->ctx);
460 if (hctx->ktmp.length)
461 OPENSSL_cleanse(hctx->ktmp.data, hctx->ktmp.length);
462 OPENSSL_free(hctx->ktmp.data);
463 hctx->ktmp.data = NULL;
468 static int ossl_hmac_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
470 ASN1_OCTET_STRING *hkey = NULL;
471 OSSL_HMAC_PKEY_CTX *hctx = EVP_PKEY_CTX_get_data(ctx);
472 if (!hctx->ktmp.data)
474 hkey = ASN1_OCTET_STRING_dup(&hctx->ktmp);
477 EVP_PKEY_assign(pkey, EVP_PKEY_HMAC, hkey);
482 static int ossl_int_update(EVP_MD_CTX *ctx,const void *data,size_t count)
484 OSSL_HMAC_PKEY_CTX *hctx = EVP_PKEY_CTX_get_data(ctx->pctx);
485 if (!HMAC_Update(&hctx->ctx, data, count))
490 static int ossl_hmac_signctx_init(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx)
492 EVP_MD_CTX_set_flags(mctx, EVP_MD_CTX_FLAG_NO_INIT);
493 mctx->update = ossl_int_update;
497 static int ossl_hmac_signctx(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
501 OSSL_HMAC_PKEY_CTX *hctx = EVP_PKEY_CTX_get_data(ctx);
502 int l = EVP_MD_CTX_size(mctx);
510 if (!HMAC_Final(&hctx->ctx, sig, &hlen))
512 *siglen = (size_t)hlen;
516 static int ossl_hmac_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
518 OSSL_HMAC_PKEY_CTX *hctx = EVP_PKEY_CTX_get_data(ctx);
520 ASN1_OCTET_STRING *key;
524 case EVP_PKEY_CTRL_SET_MAC_KEY:
525 if ((!p2 && p1 > 0) || (p1 < -1))
527 if (!ASN1_OCTET_STRING_set(&hctx->ktmp, p2, p1))
531 case EVP_PKEY_CTRL_MD:
535 case EVP_PKEY_CTRL_DIGESTINIT:
536 pk = EVP_PKEY_CTX_get0_pkey(ctx);
537 key = EVP_PKEY_get0(pk);
538 if (!HMAC_Init_ex(&hctx->ctx, key->data, key->length, hctx->md,
550 static int ossl_hmac_ctrl_str(EVP_PKEY_CTX *ctx,
551 const char *type, const char *value)
557 if (!strcmp(type, "key"))
559 void *p = (void *)value;
560 return ossl_hmac_ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY,
563 if (!strcmp(type, "hexkey"))
568 key = string_to_hex(value, &keylen);
571 r = ossl_hmac_ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY, keylen, key);
578 static EVP_PKEY_METHOD *ossl_hmac_meth;
580 static int ossl_register_hmac_meth(void)
582 EVP_PKEY_METHOD *meth;
583 meth = EVP_PKEY_meth_new(EVP_PKEY_HMAC, 0);
586 EVP_PKEY_meth_set_init(meth, ossl_hmac_init);
587 EVP_PKEY_meth_set_copy(meth, ossl_hmac_copy);
588 EVP_PKEY_meth_set_cleanup(meth, ossl_hmac_cleanup);
590 EVP_PKEY_meth_set_keygen(meth, 0, ossl_hmac_keygen);
592 EVP_PKEY_meth_set_signctx(meth, ossl_hmac_signctx_init,
595 EVP_PKEY_meth_set_ctrl(meth, ossl_hmac_ctrl, ossl_hmac_ctrl_str);
596 ossl_hmac_meth = meth;
600 static int ossl_pkey_meths(ENGINE *e, EVP_PKEY_METHOD **pmeth,
601 const int **nids, int nid)
604 static int ossl_pkey_nids[] =
611 *nids = ossl_pkey_nids;
615 if (nid == EVP_PKEY_HMAC)
617 *pmeth = ossl_hmac_meth;