]> git.ipfire.org Git - thirdparty/openssl.git/blame - test/enginetest.c
Make sure we use the libctx when creating an EVP_PKEY_CTX in libssl
[thirdparty/openssl.git] / test / enginetest.c
CommitLineData
0f113f3e 1/*
8fe3127c 2 * Copyright 2000-2017 The OpenSSL Project Authors. All Rights Reserved.
5270e702 3 *
909f1a2e 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
440e5d80
RS
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
5270e702
RL
8 */
9
1ae6ddac 10#include <stdio.h>
1023cfe7 11#include <string.h>
c2500f65 12#include <stdlib.h>
70531c14 13#include <openssl/e_os2.h>
0b13e9f0 14
e89f5fb3
P
15# include "testutil.h"
16
ad887416 17#ifndef OPENSSL_NO_ENGINE
0f113f3e
MC
18# include <openssl/buffer.h>
19# include <openssl/crypto.h>
20# include <openssl/engine.h>
e3662075 21# include <openssl/rsa.h>
0f113f3e 22# include <openssl/err.h>
5270e702 23
41a15c4f 24static void display_engine_list(void)
0f113f3e
MC
25{
26 ENGINE *h;
27 int loop;
5270e702 28
0f113f3e 29 loop = 0;
b66411f6
RS
30 for (h = ENGINE_get_first(); h != NULL; h = ENGINE_get_next(h)) {
31 TEST_info("#%d: id = \"%s\", name = \"%s\"",
0f113f3e 32 loop++, ENGINE_get_id(h), ENGINE_get_name(h));
0f113f3e 33 }
b66411f6 34
0f113f3e
MC
35 /*
36 * ENGINE_get_first() increases the struct_ref counter, so we must call
37 * ENGINE_free() to decrease it again
38 */
39 ENGINE_free(h);
40}
5270e702 41
b66411f6
RS
42#define NUMTOADD 512
43
44static int test_engines(void)
0f113f3e 45{
b66411f6 46 ENGINE *block[NUMTOADD];
1057c2c3
VD
47 char *eid[NUMTOADD];
48 char *ename[NUMTOADD];
0f113f3e 49 char buf[256];
0f113f3e
MC
50 ENGINE *ptr;
51 int loop;
b66411f6 52 int to_return = 0;
0f113f3e
MC
53 ENGINE *new_h1 = NULL;
54 ENGINE *new_h2 = NULL;
55 ENGINE *new_h3 = NULL;
56 ENGINE *new_h4 = NULL;
5270e702 57
16f8d4eb 58 memset(block, 0, sizeof(block));
b66411f6
RS
59 if (!TEST_ptr(new_h1 = ENGINE_new())
60 || !TEST_true(ENGINE_set_id(new_h1, "test_id0"))
61 || !TEST_true(ENGINE_set_name(new_h1, "First test item"))
62 || !TEST_ptr(new_h2 = ENGINE_new())
63 || !TEST_true(ENGINE_set_id(new_h2, "test_id1"))
64 || !TEST_true(ENGINE_set_name(new_h2, "Second test item"))
65 || !TEST_ptr(new_h3 = ENGINE_new())
66 || !TEST_true(ENGINE_set_id(new_h3, "test_id2"))
67 || !TEST_true(ENGINE_set_name(new_h3, "Third test item"))
68 || !TEST_ptr(new_h4 = ENGINE_new())
69 || !TEST_true(ENGINE_set_id(new_h4, "test_id3"))
70 || !TEST_true(ENGINE_set_name(new_h4, "Fourth test item")))
0f113f3e 71 goto end;
b66411f6 72 TEST_info("Engines:");
0f113f3e 73 display_engine_list();
b66411f6
RS
74
75 if (!TEST_true(ENGINE_add(new_h1)))
0f113f3e 76 goto end;
b66411f6 77 TEST_info("Engines:");
0f113f3e 78 display_engine_list();
b66411f6 79
0f113f3e 80 ptr = ENGINE_get_first();
b66411f6 81 if (!TEST_true(ENGINE_remove(ptr)))
0f113f3e 82 goto end;
efa7dd64 83 ENGINE_free(ptr);
b66411f6 84 TEST_info("Engines:");
0f113f3e 85 display_engine_list();
b66411f6
RS
86
87 if (!TEST_true(ENGINE_add(new_h3))
88 || !TEST_true(ENGINE_add(new_h2)))
0f113f3e 89 goto end;
b66411f6 90 TEST_info("Engines:");
0f113f3e 91 display_engine_list();
b66411f6
RS
92
93 if (!TEST_true(ENGINE_remove(new_h2)))
0f113f3e 94 goto end;
b66411f6 95 TEST_info("Engines:");
0f113f3e 96 display_engine_list();
b66411f6
RS
97
98 if (!TEST_true(ENGINE_add(new_h4)))
0f113f3e 99 goto end;
b66411f6 100 TEST_info("Engines:");
0f113f3e 101 display_engine_list();
b66411f6
RS
102
103 /* Should fail. */
104 if (!TEST_false(ENGINE_add(new_h3)))
0f113f3e 105 goto end;
0f113f3e 106 ERR_clear_error();
b66411f6
RS
107
108 /* Should fail. */
109 if (!TEST_false(ENGINE_remove(new_h2)))
0f113f3e 110 goto end;
0f113f3e 111 ERR_clear_error();
b66411f6
RS
112
113 if (!TEST_true(ENGINE_remove(new_h3)))
0f113f3e 114 goto end;
b66411f6 115 TEST_info("Engines:");
0f113f3e 116 display_engine_list();
b66411f6
RS
117
118 if (!TEST_true(ENGINE_remove(new_h4)))
0f113f3e 119 goto end;
b66411f6 120 TEST_info("Engines:");
0f113f3e 121 display_engine_list();
b66411f6 122
0f113f3e 123 /*
5800ba76
RL
124 * At this point, we should have an empty list, unless some hardware
125 * support engine got added. However, since we don't allow the config
126 * file to be loaded and don't otherwise load any built in engines,
127 * that is unlikely. Still, we check, if for nothing else, then to
128 * notify that something is a little off (and might mean that |new_h1|
129 * wasn't unloaded when it should have)
0f113f3e 130 */
b66411f6 131 if ((ptr = ENGINE_get_first()) != NULL) {
0f113f3e 132 if (!ENGINE_remove(ptr))
b66411f6
RS
133 TEST_info("Remove failed - probably no hardware support present");
134 }
efa7dd64 135 ENGINE_free(ptr);
b66411f6 136 TEST_info("Engines:");
0f113f3e 137 display_engine_list();
b66411f6
RS
138
139 if (!TEST_true(ENGINE_add(new_h1))
140 || !TEST_true(ENGINE_remove(new_h1)))
0f113f3e 141 goto end;
b66411f6
RS
142
143 TEST_info("About to beef up the engine-type list");
144 for (loop = 0; loop < NUMTOADD; loop++) {
145 sprintf(buf, "id%d", loop);
1057c2c3 146 eid[loop] = OPENSSL_strdup(buf);
b66411f6 147 sprintf(buf, "Fake engine type %d", loop);
1057c2c3 148 ename[loop] = OPENSSL_strdup(buf);
b66411f6 149 if (!TEST_ptr(block[loop] = ENGINE_new())
1057c2c3
VD
150 || !TEST_true(ENGINE_set_id(block[loop], eid[loop]))
151 || !TEST_true(ENGINE_set_name(block[loop], ename[loop])))
0f113f3e 152 goto end;
0f113f3e 153 }
b66411f6
RS
154 for (loop = 0; loop < NUMTOADD; loop++) {
155 if (!TEST_true(ENGINE_add(block[loop]))) {
8fe3127c
P
156 test_note("Adding stopped at %d, (%s,%s)",
157 loop, ENGINE_get_id(block[loop]),
158 ENGINE_get_name(block[loop]));
0f113f3e 159 goto cleanup_loop;
b66411f6 160 }
0f113f3e
MC
161 }
162 cleanup_loop:
b66411f6 163 TEST_info("About to empty the engine-type list");
0f113f3e 164 while ((ptr = ENGINE_get_first()) != NULL) {
b66411f6 165 if (!TEST_true(ENGINE_remove(ptr)))
0f113f3e 166 goto end;
0f113f3e 167 ENGINE_free(ptr);
0f113f3e 168 }
b66411f6 169 for (loop = 0; loop < NUMTOADD; loop++) {
1057c2c3
VD
170 OPENSSL_free(eid[loop]);
171 OPENSSL_free(ename[loop]);
0f113f3e 172 }
b66411f6
RS
173 to_return = 1;
174
0f113f3e 175 end:
efa7dd64
RS
176 ENGINE_free(new_h1);
177 ENGINE_free(new_h2);
178 ENGINE_free(new_h3);
179 ENGINE_free(new_h4);
b66411f6 180 for (loop = 0; loop < NUMTOADD; loop++)
efa7dd64 181 ENGINE_free(block[loop]);
0f113f3e
MC
182 return to_return;
183}
e3662075
DSH
184
185/* Test EVP_PKEY method */
186static EVP_PKEY_METHOD *test_rsa = NULL;
187
188static int called_encrypt = 0;
189
190/* Test function to check operation has been redirected */
191static int test_encrypt(EVP_PKEY_CTX *ctx, unsigned char *sig,
192 size_t *siglen, const unsigned char *tbs, size_t tbslen)
193{
194 called_encrypt = 1;
195 return 1;
196}
197
198static int test_pkey_meths(ENGINE *e, EVP_PKEY_METHOD **pmeth,
199 const int **pnids, int nid)
200{
201 static const int rnid = EVP_PKEY_RSA;
202 if (pmeth == NULL) {
203 *pnids = &rnid;
204 return 1;
205 }
206
207 if (nid == EVP_PKEY_RSA) {
208 *pmeth = test_rsa;
209 return 1;
210 }
211
212 *pmeth = NULL;
213 return 0;
214}
215
216/* Return a test EVP_PKEY value */
217
218static EVP_PKEY *get_test_pkey(void)
219{
220 static unsigned char n[] =
221 "\x00\xAA\x36\xAB\xCE\x88\xAC\xFD\xFF\x55\x52\x3C\x7F\xC4\x52\x3F"
222 "\x90\xEF\xA0\x0D\xF3\x77\x4A\x25\x9F\x2E\x62\xB4\xC5\xD9\x9C\xB5"
223 "\xAD\xB3\x00\xA0\x28\x5E\x53\x01\x93\x0E\x0C\x70\xFB\x68\x76\x93"
224 "\x9C\xE6\x16\xCE\x62\x4A\x11\xE0\x08\x6D\x34\x1E\xBC\xAC\xA0\xA1"
225 "\xF5";
226 static unsigned char e[] = "\x11";
227
228 RSA *rsa = RSA_new();
229 EVP_PKEY *pk = EVP_PKEY_new();
230
231 if (rsa == NULL || pk == NULL || !EVP_PKEY_assign_RSA(pk, rsa)) {
232 RSA_free(rsa);
233 EVP_PKEY_free(pk);
234 return NULL;
235 }
236
237 if (!RSA_set0_key(rsa, BN_bin2bn(n, sizeof(n)-1, NULL),
238 BN_bin2bn(e, sizeof(e)-1, NULL), NULL)) {
239 EVP_PKEY_free(pk);
240 return NULL;
241 }
242
243 return pk;
244}
245
246static int test_redirect(void)
247{
248 const unsigned char pt[] = "Hello World\n";
249 unsigned char *tmp = NULL;
250 size_t len;
251 EVP_PKEY_CTX *ctx = NULL;
252 ENGINE *e = NULL;
253 EVP_PKEY *pkey = NULL;
254
255 int to_return = 0;
256
257 if (!TEST_ptr(pkey = get_test_pkey()))
258 goto err;
259
260 len = EVP_PKEY_size(pkey);
261 if (!TEST_ptr(tmp = OPENSSL_malloc(len)))
262 goto err;
263
264 if (!TEST_ptr(ctx = EVP_PKEY_CTX_new(pkey, NULL)))
265 goto err;
266 TEST_info("EVP_PKEY_encrypt test: no redirection");
267 /* Encrypt some data: should succeed but not be redirected */
268 if (!TEST_int_gt(EVP_PKEY_encrypt_init(ctx), 0)
269 || !TEST_int_gt(EVP_PKEY_encrypt(ctx, tmp, &len, pt, sizeof(pt)), 0)
270 || !TEST_false(called_encrypt))
271 goto err;
272 EVP_PKEY_CTX_free(ctx);
273 ctx = NULL;
274
275 /* Create a test ENGINE */
276 if (!TEST_ptr(e = ENGINE_new())
277 || !TEST_true(ENGINE_set_id(e, "Test redirect engine"))
278 || !TEST_true(ENGINE_set_name(e, "Test redirect engine")))
279 goto err;
280
281 /*
282 * Try to create a context for this engine and test key.
283 * Try setting test key engine. Both should fail because the
284 * engine has no public key methods.
285 */
ad14e8e5 286 if (!TEST_ptr_null(ctx = EVP_PKEY_CTX_new(pkey, e))
e3662075
DSH
287 || !TEST_int_le(EVP_PKEY_set1_engine(pkey, e), 0))
288 goto err;
289
290 /* Setup an empty test EVP_PKEY_METHOD and set callback to return it */
291 if (!TEST_ptr(test_rsa = EVP_PKEY_meth_new(EVP_PKEY_RSA, 0)))
292 goto err;
293 ENGINE_set_pkey_meths(e, test_pkey_meths);
294
295 /* Getting a context for test ENGINE should now succeed */
296 if (!TEST_ptr(ctx = EVP_PKEY_CTX_new(pkey, e)))
297 goto err;
298 /* Encrypt should fail because operation is not supported */
299 if (!TEST_int_le(EVP_PKEY_encrypt_init(ctx), 0))
300 goto err;
301 EVP_PKEY_CTX_free(ctx);
302 ctx = NULL;
303
304 /* Add test encrypt operation to method */
305 EVP_PKEY_meth_set_encrypt(test_rsa, 0, test_encrypt);
306
307 TEST_info("EVP_PKEY_encrypt test: redirection via EVP_PKEY_CTX_new()");
308 if (!TEST_ptr(ctx = EVP_PKEY_CTX_new(pkey, e)))
309 goto err;
310 /* Encrypt some data: should succeed and be redirected */
311 if (!TEST_int_gt(EVP_PKEY_encrypt_init(ctx), 0)
312 || !TEST_int_gt(EVP_PKEY_encrypt(ctx, tmp, &len, pt, sizeof(pt)), 0)
313 || !TEST_true(called_encrypt))
314 goto err;
315
316 EVP_PKEY_CTX_free(ctx);
317 ctx = NULL;
318 called_encrypt = 0;
319
320 /* Create context with default engine: should not be redirected */
321 if (!TEST_ptr(ctx = EVP_PKEY_CTX_new(pkey, NULL))
322 || !TEST_int_gt(EVP_PKEY_encrypt_init(ctx), 0)
323 || !TEST_int_gt(EVP_PKEY_encrypt(ctx, tmp, &len, pt, sizeof(pt)), 0)
324 || !TEST_false(called_encrypt))
325 goto err;
326
327 EVP_PKEY_CTX_free(ctx);
328 ctx = NULL;
329
330 /* Set engine explicitly for test key */
331 if (!TEST_true(EVP_PKEY_set1_engine(pkey, e)))
332 goto err;
333
334 TEST_info("EVP_PKEY_encrypt test: redirection via EVP_PKEY_set1_engine()");
335
336 /* Create context with default engine: should be redirected now */
337 if (!TEST_ptr(ctx = EVP_PKEY_CTX_new(pkey, NULL))
338 || !TEST_int_gt(EVP_PKEY_encrypt_init(ctx), 0)
339 || !TEST_int_gt(EVP_PKEY_encrypt(ctx, tmp, &len, pt, sizeof(pt)), 0)
340 || !TEST_true(called_encrypt))
341 goto err;
342
343 to_return = 1;
344
345 err:
346 EVP_PKEY_CTX_free(ctx);
347 EVP_PKEY_free(pkey);
348 ENGINE_free(e);
349 OPENSSL_free(tmp);
350 return to_return;
351}
ad887416 352#endif
b66411f6 353
5800ba76
RL
354int global_init(void)
355{
356 /*
357 * If the config file gets loaded, the dynamic engine will be loaded,
358 * and that interferes with our test above.
359 */
360 return OPENSSL_init_crypto(OPENSSL_INIT_NO_LOAD_CONFIG, NULL);
361}
362
ad887416 363int setup_tests(void)
b66411f6 364{
ad887416
P
365#ifdef OPENSSL_NO_ENGINE
366 TEST_note("No ENGINE support");
367#else
b66411f6 368 ADD_TEST(test_engines);
e3662075 369 ADD_TEST(test_redirect);
0b13e9f0 370#endif
ad887416
P
371 return 1;
372}