enginetest.c revision 1.2
1/* 2 * Copyright 2000-2016 The OpenSSL Project Authors. All Rights Reserved. 3 * 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 8 */ 9 10#include <stdio.h> 11#include <string.h> 12#include <openssl/e_os2.h> 13 14#ifdef OPENSSL_NO_ENGINE 15int main(int argc, char *argv[]) 16{ 17 printf("No ENGINE support\n"); 18 return (0); 19} 20#else 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> 27 28static void display_engine_list(void) 29{ 30 ENGINE *h; 31 int loop; 32 33 h = ENGINE_get_first(); 34 loop = 0; 35 printf("listing available engine types\n"); 36 while (h) { 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); 40 } 41 printf("end of list\n"); 42 /* 43 * ENGINE_get_first() increases the struct_ref counter, so we must call 44 * ENGINE_free() to decrease it again 45 */ 46 ENGINE_free(h); 47} 48 49/* Test EVP_PKEY method */ 50static EVP_PKEY_METHOD *test_rsa = NULL; 51 52static int called_encrypt = 0; 53 54/* Test function to check operation has been redirected */ 55static int test_encrypt(EVP_PKEY_CTX *ctx, unsigned char *sig, 56 size_t *siglen, const unsigned char *tbs, size_t tbslen) 57{ 58 called_encrypt = 1; 59 return 1; 60} 61 62static int test_pkey_meths(ENGINE *e, EVP_PKEY_METHOD **pmeth, 63 const int **pnids, int nid) 64{ 65 static const int rnid = EVP_PKEY_RSA; 66 if (pmeth == NULL) { 67 *pnids = &rnid; 68 return 1; 69 } 70 71 if (nid == EVP_PKEY_RSA) { 72 *pmeth = test_rsa; 73 return 1; 74 } 75 76 *pmeth = NULL; 77 return 0; 78} 79 80/* Return a test EVP_PKEY value */ 81 82static EVP_PKEY *get_test_pkey(void) 83{ 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" 89 "\xF5"; 90 static unsigned char e[] = "\x11"; 91 92 RSA *rsa = RSA_new(); 93 EVP_PKEY *pk = EVP_PKEY_new(); 94 95 if (rsa == NULL || pk == NULL || !EVP_PKEY_assign_RSA(pk, rsa)) { 96 RSA_free(rsa); 97 EVP_PKEY_free(pk); 98 return NULL; 99 } 100 101 if (!RSA_set0_key(rsa, BN_bin2bn(n, sizeof(n)-1, NULL), 102 BN_bin2bn(e, sizeof(e)-1, NULL), NULL)) { 103 EVP_PKEY_free(pk); 104 return NULL; 105 } 106 107 return pk; 108} 109 110static int test_redirect(void) 111{ 112 const unsigned char pt[] = "Hello World\n"; 113 unsigned char *tmp = NULL; 114 size_t len; 115 EVP_PKEY_CTX *ctx = NULL; 116 ENGINE *e = NULL; 117 EVP_PKEY *pkey = NULL; 118 119 int to_return = 0; 120 121 printf("\nRedirection test\n"); 122 123 if ((pkey = get_test_pkey()) == NULL) { 124 printf("Get test key failed\n"); 125 goto err; 126 } 127 128 len = EVP_PKEY_size(pkey); 129 if ((tmp = OPENSSL_malloc(len)) == NULL) { 130 printf("Buffer alloc failed\n"); 131 goto err; 132 } 133 134 if ((ctx = EVP_PKEY_CTX_new(pkey, NULL)) == NULL) { 135 printf("Key context allocation failure\n"); 136 goto err; 137 } 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 142 || called_encrypt) { 143 printf("Test encryption failure\n"); 144 goto err; 145 } 146 EVP_PKEY_CTX_free(ctx); 147 ctx = NULL; 148 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"); 154 goto err; 155 } 156 157 /* 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. 161 */ 162 if (EVP_PKEY_CTX_new(pkey, e) != NULL 163 || EVP_PKEY_set1_engine(pkey, e) > 0) { 164 printf("Unexpected redirection success\n"); 165 goto err; 166 } 167 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"); 171 goto err; 172 } 173 ENGINE_set_pkey_meths(e, test_pkey_meths); 174 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"); 178 goto err; 179 } 180 /* Encrypt should fail because operation is not supported */ 181 if (EVP_PKEY_encrypt_init(ctx) > 0) { 182 printf("Encryption redirect unexpected success\n"); 183 goto err; 184 } 185 EVP_PKEY_CTX_free(ctx); 186 ctx = NULL; 187 188 /* Add test encrypt operation to method */ 189 EVP_PKEY_meth_set_encrypt(test_rsa, 0, test_encrypt); 190 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"); 194 goto err; 195 } 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"); 201 goto err; 202 } 203 204 EVP_PKEY_CTX_free(ctx); 205 ctx = NULL; 206 called_encrypt = 0; 207 208 printf("EVP_PKEY_encrypt test: check default operation not redirected\n"); 209 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 214 || called_encrypt) { 215 printf("Unredirected key context encryption failed\n"); 216 goto err; 217 } 218 219 EVP_PKEY_CTX_free(ctx); 220 ctx = NULL; 221 222 /* Set engine explicitly for test key */ 223 if (!EVP_PKEY_set1_engine(pkey, e)) { 224 printf("Key engine set failed\n"); 225 goto err; 226 } 227 228 printf("EVP_PKEY_encrypt test: redirection via EVP_PKEY_set1_engine()\n"); 229 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"); 236 goto err; 237 } 238 239 to_return = 1; 240 241 err: 242 EVP_PKEY_CTX_free(ctx); 243 EVP_PKEY_free(pkey); 244 ENGINE_free(e); 245 OPENSSL_free(tmp); 246 return to_return; 247} 248 249int main(int argc, char *argv[]) 250{ 251 ENGINE *block[512]; 252 char buf[256]; 253 const char *id, *name, *p; 254 ENGINE *ptr; 255 int loop; 256 int to_return = 1; 257 ENGINE *new_h1 = NULL; 258 ENGINE *new_h2 = NULL; 259 ENGINE *new_h3 = NULL; 260 ENGINE *new_h4 = NULL; 261 262 p = getenv("OPENSSL_DEBUG_MEMORY"); 263 if (p != NULL && strcmp(p, "on") == 0) 264 CRYPTO_set_mem_debug(1); 265 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"); 280 goto end; 281 } 282 printf("\nenginetest beginning\n\n"); 283 display_engine_list(); 284 if (!ENGINE_add(new_h1)) { 285 printf("Add failed!\n"); 286 goto end; 287 } 288 display_engine_list(); 289 ptr = ENGINE_get_first(); 290 if (!ENGINE_remove(ptr)) { 291 printf("Remove failed!\n"); 292 goto end; 293 } 294 ENGINE_free(ptr); 295 display_engine_list(); 296 if (!ENGINE_add(new_h3) || !ENGINE_add(new_h2)) { 297 printf("Add failed!\n"); 298 goto end; 299 } 300 display_engine_list(); 301 if (!ENGINE_remove(new_h2)) { 302 printf("Remove failed!\n"); 303 goto end; 304 } 305 display_engine_list(); 306 if (!ENGINE_add(new_h4)) { 307 printf("Add failed!\n"); 308 goto end; 309 } 310 display_engine_list(); 311 if (ENGINE_add(new_h3)) { 312 printf("Add *should* have failed but didn't!\n"); 313 goto end; 314 } else 315 printf("Add that should fail did.\n"); 316 ERR_clear_error(); 317 if (ENGINE_remove(new_h2)) { 318 printf("Remove *should* have failed but didn't!\n"); 319 goto end; 320 } else 321 printf("Remove that should fail did.\n"); 322 ERR_clear_error(); 323 if (!ENGINE_remove(new_h3)) { 324 printf("Remove failed!\n"); 325 goto end; 326 } 327 display_engine_list(); 328 if (!ENGINE_remove(new_h4)) { 329 printf("Remove failed!\n"); 330 goto end; 331 } 332 display_engine_list(); 333 /* 334 * Depending on whether there's any hardware support compiled in, this 335 * remove may be destined to fail. 336 */ 337 ptr = ENGINE_get_first(); 338 if (ptr) 339 if (!ENGINE_remove(ptr)) 340 printf("Remove failed!i - probably no hardware " 341 "support present.\n"); 342 ENGINE_free(ptr); 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"); 346 goto end; 347 } else 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"); 360 goto end; 361 } 362 } 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])); 368 goto cleanup_loop; 369 } else 370 printf("."); 371 fflush(stdout); 372 } 373 cleanup_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"); 378 goto end; 379 } 380 ENGINE_free(ptr); 381 printf("."); 382 fflush(stdout); 383 } 384 for (loop = 0; loop < 512; loop++) { 385 OPENSSL_free(__UNCONST(ENGINE_get_id(block[loop]))); 386 OPENSSL_free(__UNCONST(ENGINE_get_name(block[loop]))); 387 } 388 if (!test_redirect()) 389 goto end; 390 printf("\nTests completed happily\n"); 391 to_return = 0; 392 end: 393 if (to_return) 394 ERR_print_errors_fp(stderr); 395 ENGINE_free(new_h1); 396 ENGINE_free(new_h2); 397 ENGINE_free(new_h3); 398 ENGINE_free(new_h4); 399 for (loop = 0; loop < 512; loop++) 400 ENGINE_free(block[loop]); 401 402#ifndef OPENSSL_NO_CRYPTO_MDEBUG 403 if (CRYPTO_mem_leaks_fp(stderr) <= 0) 404 to_return = 1; 405#endif 406 return to_return; 407} 408#endif 409