]>
Commit | Line | Data |
---|---|---|
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> |
5270e702 | 26 | |
41a15c4f | 27 | static void display_engine_list(void) |
0f113f3e MC |
28 | { |
29 | ENGINE *h; | |
30 | int loop; | |
5270e702 | 31 | |
0f113f3e | 32 | loop = 0; |
b66411f6 RS |
33 | for (h = ENGINE_get_first(); h != NULL; h = ENGINE_get_next(h)) { |
34 | TEST_info("#%d: id = \"%s\", name = \"%s\"", | |
0f113f3e | 35 | loop++, ENGINE_get_id(h), ENGINE_get_name(h)); |
0f113f3e | 36 | } |
b66411f6 | 37 | |
0f113f3e MC |
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 | } | |
5270e702 | 44 | |
b66411f6 RS |
45 | #define NUMTOADD 512 |
46 | ||
47 | static int test_engines(void) | |
0f113f3e | 48 | { |
b66411f6 | 49 | ENGINE *block[NUMTOADD]; |
1057c2c3 VD |
50 | char *eid[NUMTOADD]; |
51 | char *ename[NUMTOADD]; | |
0f113f3e | 52 | char buf[256]; |
0f113f3e MC |
53 | ENGINE *ptr; |
54 | int loop; | |
b66411f6 | 55 | int to_return = 0; |
0f113f3e MC |
56 | ENGINE *new_h1 = NULL; |
57 | ENGINE *new_h2 = NULL; | |
58 | ENGINE *new_h3 = NULL; | |
59 | ENGINE *new_h4 = NULL; | |
5270e702 | 60 | |
16f8d4eb | 61 | memset(block, 0, sizeof(block)); |
b66411f6 RS |
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"))) | |
0f113f3e | 74 | goto end; |
b66411f6 | 75 | TEST_info("Engines:"); |
0f113f3e | 76 | display_engine_list(); |
b66411f6 RS |
77 | |
78 | if (!TEST_true(ENGINE_add(new_h1))) | |
0f113f3e | 79 | goto end; |
b66411f6 | 80 | TEST_info("Engines:"); |
0f113f3e | 81 | display_engine_list(); |
b66411f6 | 82 | |
0f113f3e | 83 | ptr = ENGINE_get_first(); |
b66411f6 | 84 | if (!TEST_true(ENGINE_remove(ptr))) |
0f113f3e | 85 | goto end; |
efa7dd64 | 86 | ENGINE_free(ptr); |
b66411f6 | 87 | TEST_info("Engines:"); |
0f113f3e | 88 | display_engine_list(); |
b66411f6 RS |
89 | |
90 | if (!TEST_true(ENGINE_add(new_h3)) | |
91 | || !TEST_true(ENGINE_add(new_h2))) | |
0f113f3e | 92 | goto end; |
b66411f6 | 93 | TEST_info("Engines:"); |
0f113f3e | 94 | display_engine_list(); |
b66411f6 RS |
95 | |
96 | if (!TEST_true(ENGINE_remove(new_h2))) | |
0f113f3e | 97 | goto end; |
b66411f6 | 98 | TEST_info("Engines:"); |
0f113f3e | 99 | display_engine_list(); |
b66411f6 RS |
100 | |
101 | if (!TEST_true(ENGINE_add(new_h4))) | |
0f113f3e | 102 | goto end; |
b66411f6 | 103 | TEST_info("Engines:"); |
0f113f3e | 104 | display_engine_list(); |
b66411f6 RS |
105 | |
106 | /* Should fail. */ | |
107 | if (!TEST_false(ENGINE_add(new_h3))) | |
0f113f3e | 108 | goto end; |
0f113f3e | 109 | ERR_clear_error(); |
b66411f6 RS |
110 | |
111 | /* Should fail. */ | |
112 | if (!TEST_false(ENGINE_remove(new_h2))) | |
0f113f3e | 113 | goto end; |
0f113f3e | 114 | ERR_clear_error(); |
b66411f6 RS |
115 | |
116 | if (!TEST_true(ENGINE_remove(new_h3))) | |
0f113f3e | 117 | goto end; |
b66411f6 | 118 | TEST_info("Engines:"); |
0f113f3e | 119 | display_engine_list(); |
b66411f6 RS |
120 | |
121 | if (!TEST_true(ENGINE_remove(new_h4))) | |
0f113f3e | 122 | goto end; |
b66411f6 | 123 | TEST_info("Engines:"); |
0f113f3e | 124 | display_engine_list(); |
b66411f6 | 125 | |
0f113f3e | 126 | /* |
5800ba76 RL |
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) | |
0f113f3e | 133 | */ |
b66411f6 | 134 | if ((ptr = ENGINE_get_first()) != NULL) { |
0f113f3e | 135 | if (!ENGINE_remove(ptr)) |
b66411f6 RS |
136 | TEST_info("Remove failed - probably no hardware support present"); |
137 | } | |
efa7dd64 | 138 | ENGINE_free(ptr); |
b66411f6 | 139 | TEST_info("Engines:"); |
0f113f3e | 140 | display_engine_list(); |
b66411f6 RS |
141 | |
142 | if (!TEST_true(ENGINE_add(new_h1)) | |
143 | || !TEST_true(ENGINE_remove(new_h1))) | |
0f113f3e | 144 | goto end; |
b66411f6 RS |
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); | |
1057c2c3 | 149 | eid[loop] = OPENSSL_strdup(buf); |
b66411f6 | 150 | sprintf(buf, "Fake engine type %d", loop); |
1057c2c3 | 151 | ename[loop] = OPENSSL_strdup(buf); |
b66411f6 | 152 | if (!TEST_ptr(block[loop] = ENGINE_new()) |
1057c2c3 VD |
153 | || !TEST_true(ENGINE_set_id(block[loop], eid[loop])) |
154 | || !TEST_true(ENGINE_set_name(block[loop], ename[loop]))) | |
0f113f3e | 155 | goto end; |
0f113f3e | 156 | } |
b66411f6 RS |
157 | for (loop = 0; loop < NUMTOADD; loop++) { |
158 | if (!TEST_true(ENGINE_add(block[loop]))) { | |
8fe3127c P |
159 | test_note("Adding stopped at %d, (%s,%s)", |
160 | loop, ENGINE_get_id(block[loop]), | |
161 | ENGINE_get_name(block[loop])); | |
0f113f3e | 162 | goto cleanup_loop; |
b66411f6 | 163 | } |
0f113f3e MC |
164 | } |
165 | cleanup_loop: | |
b66411f6 | 166 | TEST_info("About to empty the engine-type list"); |
0f113f3e | 167 | while ((ptr = ENGINE_get_first()) != NULL) { |
b66411f6 | 168 | if (!TEST_true(ENGINE_remove(ptr))) |
0f113f3e | 169 | goto end; |
0f113f3e | 170 | ENGINE_free(ptr); |
0f113f3e | 171 | } |
b66411f6 | 172 | for (loop = 0; loop < NUMTOADD; loop++) { |
1057c2c3 VD |
173 | OPENSSL_free(eid[loop]); |
174 | OPENSSL_free(ename[loop]); | |
0f113f3e | 175 | } |
b66411f6 RS |
176 | to_return = 1; |
177 | ||
0f113f3e | 178 | end: |
efa7dd64 RS |
179 | ENGINE_free(new_h1); |
180 | ENGINE_free(new_h2); | |
181 | ENGINE_free(new_h3); | |
182 | ENGINE_free(new_h4); | |
b66411f6 | 183 | for (loop = 0; loop < NUMTOADD; loop++) |
efa7dd64 | 184 | ENGINE_free(block[loop]); |
0f113f3e MC |
185 | return to_return; |
186 | } | |
e3662075 DSH |
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 | ||
ed576acd | 263 | len = EVP_PKEY_get_size(pkey); |
e3662075 DSH |
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 | */ | |
ad14e8e5 | 289 | if (!TEST_ptr_null(ctx = EVP_PKEY_CTX_new(pkey, e)) |
e3662075 DSH |
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 | } | |
ad887416 | 355 | #endif |
b66411f6 | 356 | |
5800ba76 RL |
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 | ||
ad887416 | 366 | int setup_tests(void) |
b66411f6 | 367 | { |
ad887416 P |
368 | #ifdef OPENSSL_NO_ENGINE |
369 | TEST_note("No ENGINE support"); | |
370 | #else | |
b66411f6 | 371 | ADD_TEST(test_engines); |
e3662075 | 372 | ADD_TEST(test_redirect); |
0b13e9f0 | 373 | #endif |
ad887416 P |
374 | return 1; |
375 | } |