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