2 * Copyright 2000-2020 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 /* We need to use some deprecated APIs */
11 #define OPENSSL_SUPPRESS_DEPRECATED
16 #include <openssl/e_os2.h>
18 # include "testutil.h"
20 #ifndef OPENSSL_NO_ENGINE
21 # include <openssl/buffer.h>
22 # include <openssl/crypto.h>
23 # include <openssl/engine.h>
24 # include <openssl/rsa.h>
25 # include <openssl/err.h>
27 static void display_engine_list(void)
33 for (h = ENGINE_get_first(); h != NULL; h = ENGINE_get_next(h)) {
34 TEST_info("#%d: id = \"%s\", name = \"%s\"",
35 loop++, ENGINE_get_id(h), ENGINE_get_name(h));
39 * ENGINE_get_first() increases the struct_ref counter, so we must call
40 * ENGINE_free() to decrease it again
47 static int test_engines(void)
49 ENGINE *block[NUMTOADD];
51 char *ename[NUMTOADD];
56 ENGINE *new_h1 = NULL;
57 ENGINE *new_h2 = NULL;
58 ENGINE *new_h3 = NULL;
59 ENGINE *new_h4 = NULL;
61 memset(block, 0, sizeof(block));
62 if (!TEST_ptr(new_h1 = ENGINE_new())
63 || !TEST_true(ENGINE_set_id(new_h1, "test_id0"))
64 || !TEST_true(ENGINE_set_name(new_h1, "First test item"))
65 || !TEST_ptr(new_h2 = ENGINE_new())
66 || !TEST_true(ENGINE_set_id(new_h2, "test_id1"))
67 || !TEST_true(ENGINE_set_name(new_h2, "Second test item"))
68 || !TEST_ptr(new_h3 = ENGINE_new())
69 || !TEST_true(ENGINE_set_id(new_h3, "test_id2"))
70 || !TEST_true(ENGINE_set_name(new_h3, "Third test item"))
71 || !TEST_ptr(new_h4 = ENGINE_new())
72 || !TEST_true(ENGINE_set_id(new_h4, "test_id3"))
73 || !TEST_true(ENGINE_set_name(new_h4, "Fourth test item")))
75 TEST_info("Engines:");
76 display_engine_list();
78 if (!TEST_true(ENGINE_add(new_h1)))
80 TEST_info("Engines:");
81 display_engine_list();
83 ptr = ENGINE_get_first();
84 if (!TEST_true(ENGINE_remove(ptr)))
87 TEST_info("Engines:");
88 display_engine_list();
90 if (!TEST_true(ENGINE_add(new_h3))
91 || !TEST_true(ENGINE_add(new_h2)))
93 TEST_info("Engines:");
94 display_engine_list();
96 if (!TEST_true(ENGINE_remove(new_h2)))
98 TEST_info("Engines:");
99 display_engine_list();
101 if (!TEST_true(ENGINE_add(new_h4)))
103 TEST_info("Engines:");
104 display_engine_list();
107 if (!TEST_false(ENGINE_add(new_h3)))
112 if (!TEST_false(ENGINE_remove(new_h2)))
116 if (!TEST_true(ENGINE_remove(new_h3)))
118 TEST_info("Engines:");
119 display_engine_list();
121 if (!TEST_true(ENGINE_remove(new_h4)))
123 TEST_info("Engines:");
124 display_engine_list();
127 * At this point, we should have an empty list, unless some hardware
128 * support engine got added. However, since we don't allow the config
129 * file to be loaded and don't otherwise load any built in engines,
130 * that is unlikely. Still, we check, if for nothing else, then to
131 * notify that something is a little off (and might mean that |new_h1|
132 * wasn't unloaded when it should have)
134 if ((ptr = ENGINE_get_first()) != NULL) {
135 if (!ENGINE_remove(ptr))
136 TEST_info("Remove failed - probably no hardware support present");
139 TEST_info("Engines:");
140 display_engine_list();
142 if (!TEST_true(ENGINE_add(new_h1))
143 || !TEST_true(ENGINE_remove(new_h1)))
146 TEST_info("About to beef up the engine-type list");
147 for (loop = 0; loop < NUMTOADD; loop++) {
148 sprintf(buf, "id%d", loop);
149 eid[loop] = OPENSSL_strdup(buf);
150 sprintf(buf, "Fake engine type %d", loop);
151 ename[loop] = OPENSSL_strdup(buf);
152 if (!TEST_ptr(block[loop] = ENGINE_new())
153 || !TEST_true(ENGINE_set_id(block[loop], eid[loop]))
154 || !TEST_true(ENGINE_set_name(block[loop], ename[loop])))
157 for (loop = 0; loop < NUMTOADD; loop++) {
158 if (!TEST_true(ENGINE_add(block[loop]))) {
159 test_note("Adding stopped at %d, (%s,%s)",
160 loop, ENGINE_get_id(block[loop]),
161 ENGINE_get_name(block[loop]));
166 TEST_info("About to empty the engine-type list");
167 while ((ptr = ENGINE_get_first()) != NULL) {
168 if (!TEST_true(ENGINE_remove(ptr)))
172 for (loop = 0; loop < NUMTOADD; loop++) {
173 OPENSSL_free(eid[loop]);
174 OPENSSL_free(ename[loop]);
183 for (loop = 0; loop < NUMTOADD; loop++)
184 ENGINE_free(block[loop]);
188 /* Test EVP_PKEY method */
189 static EVP_PKEY_METHOD *test_rsa = NULL;
191 static int called_encrypt = 0;
193 /* Test function to check operation has been redirected */
194 static int test_encrypt(EVP_PKEY_CTX *ctx, unsigned char *sig,
195 size_t *siglen, const unsigned char *tbs, size_t tbslen)
201 static int test_pkey_meths(ENGINE *e, EVP_PKEY_METHOD **pmeth,
202 const int **pnids, int nid)
204 static const int rnid = EVP_PKEY_RSA;
210 if (nid == EVP_PKEY_RSA) {
219 /* Return a test EVP_PKEY value */
221 static EVP_PKEY *get_test_pkey(void)
223 static unsigned char n[] =
224 "\x00\xAA\x36\xAB\xCE\x88\xAC\xFD\xFF\x55\x52\x3C\x7F\xC4\x52\x3F"
225 "\x90\xEF\xA0\x0D\xF3\x77\x4A\x25\x9F\x2E\x62\xB4\xC5\xD9\x9C\xB5"
226 "\xAD\xB3\x00\xA0\x28\x5E\x53\x01\x93\x0E\x0C\x70\xFB\x68\x76\x93"
227 "\x9C\xE6\x16\xCE\x62\x4A\x11\xE0\x08\x6D\x34\x1E\xBC\xAC\xA0\xA1"
229 static unsigned char e[] = "\x11";
231 RSA *rsa = RSA_new();
232 EVP_PKEY *pk = EVP_PKEY_new();
234 if (rsa == NULL || pk == NULL || !EVP_PKEY_assign_RSA(pk, rsa)) {
240 if (!RSA_set0_key(rsa, BN_bin2bn(n, sizeof(n)-1, NULL),
241 BN_bin2bn(e, sizeof(e)-1, NULL), NULL)) {
249 static int test_redirect(void)
251 const unsigned char pt[] = "Hello World\n";
252 unsigned char *tmp = NULL;
254 EVP_PKEY_CTX *ctx = NULL;
256 EVP_PKEY *pkey = NULL;
260 if (!TEST_ptr(pkey = get_test_pkey()))
263 len = EVP_PKEY_get_size(pkey);
264 if (!TEST_ptr(tmp = OPENSSL_malloc(len)))
267 if (!TEST_ptr(ctx = EVP_PKEY_CTX_new(pkey, NULL)))
269 TEST_info("EVP_PKEY_encrypt test: no redirection");
270 /* Encrypt some data: should succeed but not be redirected */
271 if (!TEST_int_gt(EVP_PKEY_encrypt_init(ctx), 0)
272 || !TEST_int_gt(EVP_PKEY_encrypt(ctx, tmp, &len, pt, sizeof(pt)), 0)
273 || !TEST_false(called_encrypt))
275 EVP_PKEY_CTX_free(ctx);
278 /* Create a test ENGINE */
279 if (!TEST_ptr(e = ENGINE_new())
280 || !TEST_true(ENGINE_set_id(e, "Test redirect engine"))
281 || !TEST_true(ENGINE_set_name(e, "Test redirect engine")))
285 * Try to create a context for this engine and test key.
286 * Try setting test key engine. Both should fail because the
287 * engine has no public key methods.
289 if (!TEST_ptr_null(ctx = EVP_PKEY_CTX_new(pkey, e))
290 || !TEST_int_le(EVP_PKEY_set1_engine(pkey, e), 0))
293 /* Setup an empty test EVP_PKEY_METHOD and set callback to return it */
294 if (!TEST_ptr(test_rsa = EVP_PKEY_meth_new(EVP_PKEY_RSA, 0)))
296 ENGINE_set_pkey_meths(e, test_pkey_meths);
298 /* Getting a context for test ENGINE should now succeed */
299 if (!TEST_ptr(ctx = EVP_PKEY_CTX_new(pkey, e)))
301 /* Encrypt should fail because operation is not supported */
302 if (!TEST_int_le(EVP_PKEY_encrypt_init(ctx), 0))
304 EVP_PKEY_CTX_free(ctx);
307 /* Add test encrypt operation to method */
308 EVP_PKEY_meth_set_encrypt(test_rsa, 0, test_encrypt);
310 TEST_info("EVP_PKEY_encrypt test: redirection via EVP_PKEY_CTX_new()");
311 if (!TEST_ptr(ctx = EVP_PKEY_CTX_new(pkey, e)))
313 /* Encrypt some data: should succeed and be redirected */
314 if (!TEST_int_gt(EVP_PKEY_encrypt_init(ctx), 0)
315 || !TEST_int_gt(EVP_PKEY_encrypt(ctx, tmp, &len, pt, sizeof(pt)), 0)
316 || !TEST_true(called_encrypt))
319 EVP_PKEY_CTX_free(ctx);
323 /* Create context with default engine: should not be redirected */
324 if (!TEST_ptr(ctx = EVP_PKEY_CTX_new(pkey, NULL))
325 || !TEST_int_gt(EVP_PKEY_encrypt_init(ctx), 0)
326 || !TEST_int_gt(EVP_PKEY_encrypt(ctx, tmp, &len, pt, sizeof(pt)), 0)
327 || !TEST_false(called_encrypt))
330 EVP_PKEY_CTX_free(ctx);
333 /* Set engine explicitly for test key */
334 if (!TEST_true(EVP_PKEY_set1_engine(pkey, e)))
337 TEST_info("EVP_PKEY_encrypt test: redirection via EVP_PKEY_set1_engine()");
339 /* Create context with default engine: should be redirected now */
340 if (!TEST_ptr(ctx = EVP_PKEY_CTX_new(pkey, NULL))
341 || !TEST_int_gt(EVP_PKEY_encrypt_init(ctx), 0)
342 || !TEST_int_gt(EVP_PKEY_encrypt(ctx, tmp, &len, pt, sizeof(pt)), 0)
343 || !TEST_true(called_encrypt))
349 EVP_PKEY_CTX_free(ctx);
357 int global_init(void)
360 * If the config file gets loaded, the dynamic engine will be loaded,
361 * and that interferes with our test above.
363 return OPENSSL_init_crypto(OPENSSL_INIT_NO_LOAD_CONFIG, NULL);
366 int setup_tests(void)
368 #ifdef OPENSSL_NO_ENGINE
369 TEST_note("No ENGINE support");
371 ADD_TEST(test_engines);
372 ADD_TEST(test_redirect);