2 * Copyright 2000-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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
12 #include <openssl/e_os2.h>
14 #ifdef OPENSSL_NO_ENGINE
15 int main(int argc, char *argv[])
17 printf("No ENGINE support\n");
21 # include <openssl/buffer.h>
22 # include <openssl/crypto.h>
23 # include <openssl/engine.h>
24 # include <openssl/err.h>
25 # include <openssl/rsa.h>
26 # include <openssl/bn.h>
28 static void display_engine_list(void)
33 h = ENGINE_get_first();
35 printf("listing available engine types\n");
37 printf("engine %i, id = \"%s\", name = \"%s\"\n",
38 loop++, ENGINE_get_id(h), ENGINE_get_name(h));
39 h = ENGINE_get_next(h);
41 printf("end of list\n");
43 * ENGINE_get_first() increases the struct_ref counter, so we must call
44 * ENGINE_free() to decrease it again
49 /* Test EVP_PKEY method */
50 static EVP_PKEY_METHOD *test_rsa = NULL;
52 static int called_encrypt = 0;
54 /* Test function to check operation has been redirected */
55 static int test_encrypt(EVP_PKEY_CTX *ctx, unsigned char *sig,
56 size_t *siglen, const unsigned char *tbs, size_t tbslen)
62 static int test_pkey_meths(ENGINE *e, EVP_PKEY_METHOD **pmeth,
63 const int **pnids, int nid)
65 static const int rnid = EVP_PKEY_RSA;
71 if (nid == EVP_PKEY_RSA) {
80 /* Return a test EVP_PKEY value */
82 static EVP_PKEY *get_test_pkey(void)
84 static unsigned char n[] =
85 "\x00\xAA\x36\xAB\xCE\x88\xAC\xFD\xFF\x55\x52\x3C\x7F\xC4\x52\x3F"
86 "\x90\xEF\xA0\x0D\xF3\x77\x4A\x25\x9F\x2E\x62\xB4\xC5\xD9\x9C\xB5"
87 "\xAD\xB3\x00\xA0\x28\x5E\x53\x01\x93\x0E\x0C\x70\xFB\x68\x76\x93"
88 "\x9C\xE6\x16\xCE\x62\x4A\x11\xE0\x08\x6D\x34\x1E\xBC\xAC\xA0\xA1"
90 static unsigned char e[] = "\x11";
93 EVP_PKEY *pk = EVP_PKEY_new();
95 if (rsa == NULL || pk == NULL || !EVP_PKEY_assign_RSA(pk, rsa)) {
101 if (!RSA_set0_key(rsa, BN_bin2bn(n, sizeof(n)-1, NULL),
102 BN_bin2bn(e, sizeof(e)-1, NULL), NULL)) {
110 static int test_redirect(void)
112 const unsigned char pt[] = "Hello World\n";
113 unsigned char *tmp = NULL;
115 EVP_PKEY_CTX *ctx = NULL;
117 EVP_PKEY *pkey = NULL;
121 printf("\nRedirection test\n");
123 if ((pkey = get_test_pkey()) == NULL) {
124 printf("Get test key failed\n");
128 len = EVP_PKEY_size(pkey);
129 if ((tmp = OPENSSL_malloc(len)) == NULL) {
130 printf("Buffer alloc failed\n");
134 if ((ctx = EVP_PKEY_CTX_new(pkey, NULL)) == NULL) {
135 printf("Key context allocation failure\n");
138 printf("EVP_PKEY_encrypt test: no redirection\n");
139 /* Encrypt some data: should succeed but not be redirected */
140 if (EVP_PKEY_encrypt_init(ctx) <= 0
141 || EVP_PKEY_encrypt(ctx, tmp, &len, pt, sizeof(pt)) <= 0
143 printf("Test encryption failure\n");
146 EVP_PKEY_CTX_free(ctx);
149 /* Create a test ENGINE */
150 if ((e = ENGINE_new()) == NULL
151 || !ENGINE_set_id(e, "Test redirect engine")
152 || !ENGINE_set_name(e, "Test redirect engine")) {
153 printf("Redirection engine setup failure\n");
158 * Try to create a context for this engine and test key.
159 * Try setting test key engine. Both should fail because the
160 * engine has no public key methods.
162 if (EVP_PKEY_CTX_new(pkey, e) != NULL
163 || EVP_PKEY_set1_engine(pkey, e) > 0) {
164 printf("Unexpected redirection success\n");
168 /* Setup an empty test EVP_PKEY_METHOD and set callback to return it */
169 if ((test_rsa = EVP_PKEY_meth_new(EVP_PKEY_RSA, 0)) == NULL) {
170 printf("Test RSA algorithm setup failure\n");
173 ENGINE_set_pkey_meths(e, test_pkey_meths);
175 /* Getting a context for test ENGINE should now succeed */
176 if ((ctx = EVP_PKEY_CTX_new(pkey, e)) == NULL) {
177 printf("Redirected context allocation failed\n");
180 /* Encrypt should fail because operation is not supported */
181 if (EVP_PKEY_encrypt_init(ctx) > 0) {
182 printf("Encryption redirect unexpected success\n");
185 EVP_PKEY_CTX_free(ctx);
188 /* Add test encrypt operation to method */
189 EVP_PKEY_meth_set_encrypt(test_rsa, 0, test_encrypt);
191 printf("EVP_PKEY_encrypt test: redirection via EVP_PKEY_CTX_new()\n");
192 if ((ctx = EVP_PKEY_CTX_new(pkey, e)) == NULL) {
193 printf("Redirected context allocation failed\n");
196 /* Encrypt some data: should succeed and be redirected */
197 if (EVP_PKEY_encrypt_init(ctx) <= 0
198 || EVP_PKEY_encrypt(ctx, tmp, &len, pt, sizeof(pt)) <= 0
199 || !called_encrypt) {
200 printf("Redirected key context encryption failed\n");
204 EVP_PKEY_CTX_free(ctx);
208 printf("EVP_PKEY_encrypt test: check default operation not redirected\n");
210 /* Create context with default engine: should not be redirected */
211 if ((ctx = EVP_PKEY_CTX_new(pkey, NULL)) == NULL
212 || EVP_PKEY_encrypt_init(ctx) <= 0
213 || EVP_PKEY_encrypt(ctx, tmp, &len, pt, sizeof(pt)) <= 0
215 printf("Unredirected key context encryption failed\n");
219 EVP_PKEY_CTX_free(ctx);
222 /* Set engine explicitly for test key */
223 if (!EVP_PKEY_set1_engine(pkey, e)) {
224 printf("Key engine set failed\n");
228 printf("EVP_PKEY_encrypt test: redirection via EVP_PKEY_set1_engine()\n");
230 /* Create context with default engine: should be redirected now */
231 if ((ctx = EVP_PKEY_CTX_new(pkey, NULL)) == NULL
232 || EVP_PKEY_encrypt_init(ctx) <= 0
233 || EVP_PKEY_encrypt(ctx, tmp, &len, pt, sizeof(pt)) <= 0
234 || !called_encrypt) {
235 printf("Key redirection failure\n");
242 EVP_PKEY_CTX_free(ctx);
249 int main(int argc, char *argv[])
253 const char *id, *name, *p;
257 ENGINE *new_h1 = NULL;
258 ENGINE *new_h2 = NULL;
259 ENGINE *new_h3 = NULL;
260 ENGINE *new_h4 = NULL;
262 p = getenv("OPENSSL_DEBUG_MEMORY");
263 if (p != NULL && strcmp(p, "on") == 0)
264 CRYPTO_set_mem_debug(1);
266 memset(block, 0, sizeof(block));
267 if (((new_h1 = ENGINE_new()) == NULL) ||
268 !ENGINE_set_id(new_h1, "test_id0") ||
269 !ENGINE_set_name(new_h1, "First test item") ||
270 ((new_h2 = ENGINE_new()) == NULL) ||
271 !ENGINE_set_id(new_h2, "test_id1") ||
272 !ENGINE_set_name(new_h2, "Second test item") ||
273 ((new_h3 = ENGINE_new()) == NULL) ||
274 !ENGINE_set_id(new_h3, "test_id2") ||
275 !ENGINE_set_name(new_h3, "Third test item") ||
276 ((new_h4 = ENGINE_new()) == NULL) ||
277 !ENGINE_set_id(new_h4, "test_id3") ||
278 !ENGINE_set_name(new_h4, "Fourth test item")) {
279 printf("Couldn't set up test ENGINE structures\n");
282 printf("\nenginetest beginning\n\n");
283 display_engine_list();
284 if (!ENGINE_add(new_h1)) {
285 printf("Add failed!\n");
288 display_engine_list();
289 ptr = ENGINE_get_first();
290 if (!ENGINE_remove(ptr)) {
291 printf("Remove failed!\n");
295 display_engine_list();
296 if (!ENGINE_add(new_h3) || !ENGINE_add(new_h2)) {
297 printf("Add failed!\n");
300 display_engine_list();
301 if (!ENGINE_remove(new_h2)) {
302 printf("Remove failed!\n");
305 display_engine_list();
306 if (!ENGINE_add(new_h4)) {
307 printf("Add failed!\n");
310 display_engine_list();
311 if (ENGINE_add(new_h3)) {
312 printf("Add *should* have failed but didn't!\n");
315 printf("Add that should fail did.\n");
317 if (ENGINE_remove(new_h2)) {
318 printf("Remove *should* have failed but didn't!\n");
321 printf("Remove that should fail did.\n");
323 if (!ENGINE_remove(new_h3)) {
324 printf("Remove failed!\n");
327 display_engine_list();
328 if (!ENGINE_remove(new_h4)) {
329 printf("Remove failed!\n");
332 display_engine_list();
334 * Depending on whether there's any hardware support compiled in, this
335 * remove may be destined to fail.
337 ptr = ENGINE_get_first();
339 if (!ENGINE_remove(ptr))
340 printf("Remove failed!i - probably no hardware "
341 "support present.\n");
343 display_engine_list();
344 if (!ENGINE_add(new_h1) || !ENGINE_remove(new_h1)) {
345 printf("Couldn't add and remove to an empty list!\n");
348 printf("Successfully added and removed to an empty list!\n");
349 printf("About to beef up the engine-type list\n");
350 for (loop = 0; loop < 512; loop++) {
351 sprintf(buf, "id%i", loop);
352 id = OPENSSL_strdup(buf);
353 sprintf(buf, "Fake engine type %i", loop);
354 name = OPENSSL_strdup(buf);
355 if (((block[loop] = ENGINE_new()) == NULL) ||
356 !ENGINE_set_id(block[loop], id) ||
357 !ENGINE_set_name(block[loop], name)) {
358 printf("Couldn't create block of ENGINE structures.\n"
359 "I'll probably also core-dump now, damn.\n");
363 for (loop = 0; loop < 512; loop++) {
364 if (!ENGINE_add(block[loop])) {
365 printf("\nAdding stopped at %i, (%s,%s)\n",
366 loop, ENGINE_get_id(block[loop]),
367 ENGINE_get_name(block[loop]));
374 printf("\nAbout to empty the engine-type list\n");
375 while ((ptr = ENGINE_get_first()) != NULL) {
376 if (!ENGINE_remove(ptr)) {
377 printf("\nRemove failed!\n");
384 for (loop = 0; loop < 512; loop++) {
385 OPENSSL_free((void *)ENGINE_get_id(block[loop]));
386 OPENSSL_free((void *)ENGINE_get_name(block[loop]));
388 if (!test_redirect())
390 printf("\nTests completed happily\n");
394 ERR_print_errors_fp(stderr);
399 for (loop = 0; loop < 512; loop++)
400 ENGINE_free(block[loop]);
402 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
403 if (CRYPTO_mem_leaks_fp(stderr) <= 0)