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