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