deprecate engine tests
[openssl.git] / test / enginetest.c
1 /*
2  * Copyright 2000-2017 The OpenSSL Project Authors. All Rights Reserved.
3  *
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
8  */
9
10 /* We need to use some engine deprecated APIs */
11 #define OPENSSL_SUPPRESS_DEPRECATED
12
13 #include <stdio.h>
14 #include <string.h>
15 #include <stdlib.h>
16 #include <openssl/e_os2.h>
17
18 # include "testutil.h"
19
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>
26
27 static void display_engine_list(void)
28 {
29     ENGINE *h;
30     int loop;
31
32     loop = 0;
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));
36     }
37
38     /*
39      * ENGINE_get_first() increases the struct_ref counter, so we must call
40      * ENGINE_free() to decrease it again
41      */
42     ENGINE_free(h);
43 }
44
45 #define NUMTOADD 512
46
47 static int test_engines(void)
48 {
49     ENGINE *block[NUMTOADD];
50     char *eid[NUMTOADD];
51     char *ename[NUMTOADD];
52     char buf[256];
53     ENGINE *ptr;
54     int loop;
55     int to_return = 0;
56     ENGINE *new_h1 = NULL;
57     ENGINE *new_h2 = NULL;
58     ENGINE *new_h3 = NULL;
59     ENGINE *new_h4 = NULL;
60
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")))
74         goto end;
75     TEST_info("Engines:");
76     display_engine_list();
77
78     if (!TEST_true(ENGINE_add(new_h1)))
79         goto end;
80     TEST_info("Engines:");
81     display_engine_list();
82
83     ptr = ENGINE_get_first();
84     if (!TEST_true(ENGINE_remove(ptr)))
85         goto end;
86     ENGINE_free(ptr);
87     TEST_info("Engines:");
88     display_engine_list();
89
90     if (!TEST_true(ENGINE_add(new_h3))
91             || !TEST_true(ENGINE_add(new_h2)))
92         goto end;
93     TEST_info("Engines:");
94     display_engine_list();
95
96     if (!TEST_true(ENGINE_remove(new_h2)))
97         goto end;
98     TEST_info("Engines:");
99     display_engine_list();
100
101     if (!TEST_true(ENGINE_add(new_h4)))
102         goto end;
103     TEST_info("Engines:");
104     display_engine_list();
105
106     /* Should fail. */
107     if (!TEST_false(ENGINE_add(new_h3)))
108         goto end;
109     ERR_clear_error();
110
111     /* Should fail. */
112     if (!TEST_false(ENGINE_remove(new_h2)))
113         goto end;
114     ERR_clear_error();
115
116     if (!TEST_true(ENGINE_remove(new_h3)))
117         goto end;
118     TEST_info("Engines:");
119     display_engine_list();
120
121     if (!TEST_true(ENGINE_remove(new_h4)))
122         goto end;
123     TEST_info("Engines:");
124     display_engine_list();
125
126     /*
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)
133      */
134     if ((ptr = ENGINE_get_first()) != NULL) {
135         if (!ENGINE_remove(ptr))
136             TEST_info("Remove failed - probably no hardware support present");
137     }
138     ENGINE_free(ptr);
139     TEST_info("Engines:");
140     display_engine_list();
141
142     if (!TEST_true(ENGINE_add(new_h1))
143             || !TEST_true(ENGINE_remove(new_h1)))
144         goto end;
145
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])))
155             goto end;
156     }
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]));
162             goto cleanup_loop;
163         }
164     }
165  cleanup_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)))
169             goto end;
170         ENGINE_free(ptr);
171     }
172     for (loop = 0; loop < NUMTOADD; loop++) {
173         OPENSSL_free(eid[loop]);
174         OPENSSL_free(ename[loop]);
175     }
176     to_return = 1;
177
178  end:
179     ENGINE_free(new_h1);
180     ENGINE_free(new_h2);
181     ENGINE_free(new_h3);
182     ENGINE_free(new_h4);
183     for (loop = 0; loop < NUMTOADD; loop++)
184         ENGINE_free(block[loop]);
185     return to_return;
186 }
187
188 /* Test EVP_PKEY method */
189 static EVP_PKEY_METHOD *test_rsa = NULL;
190
191 static int called_encrypt = 0;
192
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)
196 {
197     called_encrypt = 1;
198     return 1;
199 }
200
201 static int test_pkey_meths(ENGINE *e, EVP_PKEY_METHOD **pmeth,
202                            const int **pnids, int nid)
203 {
204     static const int rnid = EVP_PKEY_RSA;
205     if (pmeth == NULL) {
206         *pnids = &rnid;
207         return 1;
208     }
209
210     if (nid == EVP_PKEY_RSA) {
211         *pmeth = test_rsa;
212         return 1;
213     }
214
215     *pmeth = NULL;
216     return 0;
217 }
218
219 /* Return a test EVP_PKEY value */
220
221 static EVP_PKEY *get_test_pkey(void)
222 {
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"
228         "\xF5";
229     static unsigned char e[] = "\x11";
230
231     RSA *rsa = RSA_new();
232     EVP_PKEY *pk = EVP_PKEY_new();
233
234     if (rsa == NULL || pk == NULL || !EVP_PKEY_assign_RSA(pk, rsa)) {
235         RSA_free(rsa);
236         EVP_PKEY_free(pk);
237         return NULL;
238     }
239
240     if (!RSA_set0_key(rsa, BN_bin2bn(n, sizeof(n)-1, NULL),
241                       BN_bin2bn(e, sizeof(e)-1, NULL), NULL)) {
242         EVP_PKEY_free(pk);
243         return NULL;
244     }
245
246     return pk;
247 }
248
249 static int test_redirect(void)
250 {
251     const unsigned char pt[] = "Hello World\n";
252     unsigned char *tmp = NULL;
253     size_t len;
254     EVP_PKEY_CTX *ctx = NULL;
255     ENGINE *e = NULL;
256     EVP_PKEY *pkey = NULL;
257
258     int to_return = 0;
259
260     if (!TEST_ptr(pkey = get_test_pkey()))
261         goto err;
262
263     len = EVP_PKEY_size(pkey);
264     if (!TEST_ptr(tmp = OPENSSL_malloc(len)))
265         goto err;
266
267     if (!TEST_ptr(ctx = EVP_PKEY_CTX_new(pkey, NULL)))
268         goto err;
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))
274         goto err;
275     EVP_PKEY_CTX_free(ctx);
276     ctx = NULL;
277
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")))
282         goto err;
283
284     /*
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.
288      */
289     if (!TEST_ptr_null(ctx = EVP_PKEY_CTX_new(pkey, e))
290             || !TEST_int_le(EVP_PKEY_set1_engine(pkey, e), 0))
291         goto err;
292
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)))
295         goto err;
296     ENGINE_set_pkey_meths(e, test_pkey_meths);
297
298     /* Getting a context for test ENGINE should now succeed */
299     if (!TEST_ptr(ctx = EVP_PKEY_CTX_new(pkey, e)))
300         goto err;
301     /* Encrypt should fail because operation is not supported */
302     if (!TEST_int_le(EVP_PKEY_encrypt_init(ctx), 0))
303         goto err;
304     EVP_PKEY_CTX_free(ctx);
305     ctx = NULL;
306
307     /* Add test encrypt operation to method */
308     EVP_PKEY_meth_set_encrypt(test_rsa, 0, test_encrypt);
309
310     TEST_info("EVP_PKEY_encrypt test: redirection via EVP_PKEY_CTX_new()");
311     if (!TEST_ptr(ctx = EVP_PKEY_CTX_new(pkey, e)))
312         goto err;
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))
317         goto err;
318
319     EVP_PKEY_CTX_free(ctx);
320     ctx = NULL;
321     called_encrypt = 0;
322
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))
328         goto err;
329
330     EVP_PKEY_CTX_free(ctx);
331     ctx = NULL;
332
333     /* Set engine explicitly for test key */
334     if (!TEST_true(EVP_PKEY_set1_engine(pkey, e)))
335         goto err;
336
337     TEST_info("EVP_PKEY_encrypt test: redirection via EVP_PKEY_set1_engine()");
338
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))
344         goto err;
345
346     to_return = 1;
347
348  err:
349     EVP_PKEY_CTX_free(ctx);
350     EVP_PKEY_free(pkey);
351     ENGINE_free(e);
352     OPENSSL_free(tmp);
353     return to_return;
354 }
355 #endif
356
357 int global_init(void)
358 {
359     /*
360      * If the config file gets loaded, the dynamic engine will be loaded,
361      * and that interferes with our test above.
362      */
363     return OPENSSL_init_crypto(OPENSSL_INIT_NO_LOAD_CONFIG, NULL);
364 }
365
366 int setup_tests(void)
367 {
368 #ifdef OPENSSL_NO_ENGINE
369     TEST_note("No ENGINE support");
370 #else
371     ADD_TEST(test_engines);
372     ADD_TEST(test_redirect);
373 #endif
374     return 1;
375 }