replaced snprintf with BIO version (for windows builds)
[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 #include <stdio.h>
11 #include <string.h>
12 #include <stdlib.h>
13 #include <openssl/e_os2.h>
14
15 # include "testutil.h"
16
17 #ifndef OPENSSL_NO_ENGINE
18 # include <openssl/buffer.h>
19 # include <openssl/crypto.h>
20 # include <openssl/engine.h>
21 # include <openssl/rsa.h>
22 # include <openssl/err.h>
23
24 static void display_engine_list(void)
25 {
26     ENGINE *h;
27     int loop;
28
29     loop = 0;
30     for (h = ENGINE_get_first(); h != NULL; h = ENGINE_get_next(h)) {
31         TEST_info("#%d: id = \"%s\", name = \"%s\"",
32                loop++, ENGINE_get_id(h), ENGINE_get_name(h));
33     }
34
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 }
41
42 #define NUMTOADD 512
43
44 static int test_engines(void)
45 {
46     ENGINE *block[NUMTOADD];
47     char *eid[NUMTOADD];
48     char *ename[NUMTOADD];
49     char buf[256];
50     ENGINE *ptr;
51     int loop;
52     int to_return = 0;
53     ENGINE *new_h1 = NULL;
54     ENGINE *new_h2 = NULL;
55     ENGINE *new_h3 = NULL;
56     ENGINE *new_h4 = NULL;
57
58     memset(block, 0, sizeof(block));
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")))
71         goto end;
72     TEST_info("Engines:");
73     display_engine_list();
74
75     if (!TEST_true(ENGINE_add(new_h1)))
76         goto end;
77     TEST_info("Engines:");
78     display_engine_list();
79
80     ptr = ENGINE_get_first();
81     if (!TEST_true(ENGINE_remove(ptr)))
82         goto end;
83     ENGINE_free(ptr);
84     TEST_info("Engines:");
85     display_engine_list();
86
87     if (!TEST_true(ENGINE_add(new_h3))
88             || !TEST_true(ENGINE_add(new_h2)))
89         goto end;
90     TEST_info("Engines:");
91     display_engine_list();
92
93     if (!TEST_true(ENGINE_remove(new_h2)))
94         goto end;
95     TEST_info("Engines:");
96     display_engine_list();
97
98     if (!TEST_true(ENGINE_add(new_h4)))
99         goto end;
100     TEST_info("Engines:");
101     display_engine_list();
102
103     /* Should fail. */
104     if (!TEST_false(ENGINE_add(new_h3)))
105         goto end;
106     ERR_clear_error();
107
108     /* Should fail. */
109     if (!TEST_false(ENGINE_remove(new_h2)))
110         goto end;
111     ERR_clear_error();
112
113     if (!TEST_true(ENGINE_remove(new_h3)))
114         goto end;
115     TEST_info("Engines:");
116     display_engine_list();
117
118     if (!TEST_true(ENGINE_remove(new_h4)))
119         goto end;
120     TEST_info("Engines:");
121     display_engine_list();
122
123     /*
124      * Depending on whether there's any hardware support compiled in, this
125      * remove may be destined to fail.
126      */
127     if ((ptr = ENGINE_get_first()) != NULL) {
128         if (!ENGINE_remove(ptr))
129             TEST_info("Remove failed - probably no hardware support present");
130     }
131     ENGINE_free(ptr);
132     TEST_info("Engines:");
133     display_engine_list();
134
135     if (!TEST_true(ENGINE_add(new_h1))
136             || !TEST_true(ENGINE_remove(new_h1)))
137         goto end;
138
139     TEST_info("About to beef up the engine-type list");
140     for (loop = 0; loop < NUMTOADD; loop++) {
141         sprintf(buf, "id%d", loop);
142         eid[loop] = OPENSSL_strdup(buf);
143         sprintf(buf, "Fake engine type %d", loop);
144         ename[loop] = OPENSSL_strdup(buf);
145         if (!TEST_ptr(block[loop] = ENGINE_new())
146                 || !TEST_true(ENGINE_set_id(block[loop], eid[loop]))
147                 || !TEST_true(ENGINE_set_name(block[loop], ename[loop])))
148             goto end;
149     }
150     for (loop = 0; loop < NUMTOADD; loop++) {
151         if (!TEST_true(ENGINE_add(block[loop]))) {
152             test_note("Adding stopped at %d, (%s,%s)",
153                       loop, ENGINE_get_id(block[loop]),
154                       ENGINE_get_name(block[loop]));
155             goto cleanup_loop;
156         }
157     }
158  cleanup_loop:
159     TEST_info("About to empty the engine-type list");
160     while ((ptr = ENGINE_get_first()) != NULL) {
161         if (!TEST_true(ENGINE_remove(ptr)))
162             goto end;
163         ENGINE_free(ptr);
164     }
165     for (loop = 0; loop < NUMTOADD; loop++) {
166         OPENSSL_free(eid[loop]);
167         OPENSSL_free(ename[loop]);
168     }
169     to_return = 1;
170
171  end:
172     ENGINE_free(new_h1);
173     ENGINE_free(new_h2);
174     ENGINE_free(new_h3);
175     ENGINE_free(new_h4);
176     for (loop = 0; loop < NUMTOADD; loop++)
177         ENGINE_free(block[loop]);
178     return to_return;
179 }
180
181 /* Test EVP_PKEY method */
182 static EVP_PKEY_METHOD *test_rsa = NULL;
183
184 static int called_encrypt = 0;
185
186 /* Test function to check operation has been redirected */
187 static int test_encrypt(EVP_PKEY_CTX *ctx, unsigned char *sig,
188                         size_t *siglen, const unsigned char *tbs, size_t tbslen)
189 {
190     called_encrypt = 1;
191     return 1;
192 }
193
194 static int test_pkey_meths(ENGINE *e, EVP_PKEY_METHOD **pmeth,
195                            const int **pnids, int nid)
196 {
197     static const int rnid = EVP_PKEY_RSA;
198     if (pmeth == NULL) {
199         *pnids = &rnid;
200         return 1;
201     }
202
203     if (nid == EVP_PKEY_RSA) {
204         *pmeth = test_rsa;
205         return 1;
206     }
207
208     *pmeth = NULL;
209     return 0;
210 }
211
212 /* Return a test EVP_PKEY value */
213
214 static EVP_PKEY *get_test_pkey(void)
215 {
216     static unsigned char n[] =
217         "\x00\xAA\x36\xAB\xCE\x88\xAC\xFD\xFF\x55\x52\x3C\x7F\xC4\x52\x3F"
218         "\x90\xEF\xA0\x0D\xF3\x77\x4A\x25\x9F\x2E\x62\xB4\xC5\xD9\x9C\xB5"
219         "\xAD\xB3\x00\xA0\x28\x5E\x53\x01\x93\x0E\x0C\x70\xFB\x68\x76\x93"
220         "\x9C\xE6\x16\xCE\x62\x4A\x11\xE0\x08\x6D\x34\x1E\xBC\xAC\xA0\xA1"
221         "\xF5";
222     static unsigned char e[] = "\x11";
223
224     RSA *rsa = RSA_new();
225     EVP_PKEY *pk = EVP_PKEY_new();
226
227     if (rsa == NULL || pk == NULL || !EVP_PKEY_assign_RSA(pk, rsa)) {
228         RSA_free(rsa);
229         EVP_PKEY_free(pk);
230         return NULL;
231     }
232
233     if (!RSA_set0_key(rsa, BN_bin2bn(n, sizeof(n)-1, NULL),
234                       BN_bin2bn(e, sizeof(e)-1, NULL), NULL)) {
235         EVP_PKEY_free(pk);
236         return NULL;
237     }
238
239     return pk;
240 }
241
242 static int test_redirect(void)
243 {
244     const unsigned char pt[] = "Hello World\n";
245     unsigned char *tmp = NULL;
246     size_t len;
247     EVP_PKEY_CTX *ctx = NULL;
248     ENGINE *e = NULL;
249     EVP_PKEY *pkey = NULL;
250
251     int to_return = 0;
252
253     if (!TEST_ptr(pkey = get_test_pkey()))
254         goto err;
255
256     len = EVP_PKEY_size(pkey);
257     if (!TEST_ptr(tmp = OPENSSL_malloc(len)))
258         goto err;
259
260     if (!TEST_ptr(ctx = EVP_PKEY_CTX_new(pkey, NULL)))
261         goto err;
262     TEST_info("EVP_PKEY_encrypt test: no redirection");
263     /* Encrypt some data: should succeed but not be redirected */
264     if (!TEST_int_gt(EVP_PKEY_encrypt_init(ctx), 0)
265             || !TEST_int_gt(EVP_PKEY_encrypt(ctx, tmp, &len, pt, sizeof(pt)), 0)
266             || !TEST_false(called_encrypt))
267         goto err;
268     EVP_PKEY_CTX_free(ctx);
269     ctx = NULL;
270
271     /* Create a test ENGINE */
272     if (!TEST_ptr(e = ENGINE_new())
273             || !TEST_true(ENGINE_set_id(e, "Test redirect engine"))
274             || !TEST_true(ENGINE_set_name(e, "Test redirect engine")))
275         goto err;
276
277     /*
278      * Try to create a context for this engine and test key.
279      * Try setting test key engine. Both should fail because the
280      * engine has no public key methods.
281      */
282     if (!TEST_ptr_null(EVP_PKEY_CTX_new(pkey, e))
283             || !TEST_int_le(EVP_PKEY_set1_engine(pkey, e), 0))
284         goto err;
285
286     /* Setup an empty test EVP_PKEY_METHOD and set callback to return it */
287     if (!TEST_ptr(test_rsa = EVP_PKEY_meth_new(EVP_PKEY_RSA, 0)))
288         goto err;
289     ENGINE_set_pkey_meths(e, test_pkey_meths);
290
291     /* Getting a context for test ENGINE should now succeed */
292     if (!TEST_ptr(ctx = EVP_PKEY_CTX_new(pkey, e)))
293         goto err;
294     /* Encrypt should fail because operation is not supported */
295     if (!TEST_int_le(EVP_PKEY_encrypt_init(ctx), 0))
296         goto err;
297     EVP_PKEY_CTX_free(ctx);
298     ctx = NULL;
299
300     /* Add test encrypt operation to method */
301     EVP_PKEY_meth_set_encrypt(test_rsa, 0, test_encrypt);
302
303     TEST_info("EVP_PKEY_encrypt test: redirection via EVP_PKEY_CTX_new()");
304     if (!TEST_ptr(ctx = EVP_PKEY_CTX_new(pkey, e)))
305         goto err;
306     /* Encrypt some data: should succeed and be redirected */
307     if (!TEST_int_gt(EVP_PKEY_encrypt_init(ctx), 0)
308             || !TEST_int_gt(EVP_PKEY_encrypt(ctx, tmp, &len, pt, sizeof(pt)), 0)
309             || !TEST_true(called_encrypt))
310         goto err;
311
312     EVP_PKEY_CTX_free(ctx);
313     ctx = NULL;
314     called_encrypt = 0;
315
316     /* Create context with default engine: should not be redirected */
317     if (!TEST_ptr(ctx = EVP_PKEY_CTX_new(pkey, NULL))
318             || !TEST_int_gt(EVP_PKEY_encrypt_init(ctx), 0)
319             || !TEST_int_gt(EVP_PKEY_encrypt(ctx, tmp, &len, pt, sizeof(pt)), 0)
320             || !TEST_false(called_encrypt))
321         goto err;
322
323     EVP_PKEY_CTX_free(ctx);
324     ctx = NULL;
325
326     /* Set engine explicitly for test key */
327     if (!TEST_true(EVP_PKEY_set1_engine(pkey, e)))
328         goto err;
329
330     TEST_info("EVP_PKEY_encrypt test: redirection via EVP_PKEY_set1_engine()");
331
332     /* Create context with default engine: should be redirected now */
333     if (!TEST_ptr(ctx = EVP_PKEY_CTX_new(pkey, NULL))
334             || !TEST_int_gt(EVP_PKEY_encrypt_init(ctx), 0)
335             || !TEST_int_gt(EVP_PKEY_encrypt(ctx, tmp, &len, pt, sizeof(pt)), 0)
336             || !TEST_true(called_encrypt))
337         goto err;
338
339     to_return = 1;
340
341  err:
342     EVP_PKEY_CTX_free(ctx);
343     EVP_PKEY_free(pkey);
344     ENGINE_free(e);
345     OPENSSL_free(tmp);
346     return to_return;
347 }
348 #endif
349
350 int setup_tests(void)
351 {
352 #ifdef OPENSSL_NO_ENGINE
353     TEST_note("No ENGINE support");
354 #else
355     ADD_TEST(test_engines);
356     ADD_TEST(test_redirect);
357 #endif
358     return 1;
359 }