1 /* 2 * Copyright 2015-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 /* 11 * This is the OSSLTEST engine. It provides deliberately crippled digest 12 * implementations for test purposes. It is highly insecure and must NOT be 13 * used for any purpose except testing 14 */ 15 16 #include <stdio.h> 17 #include <string.h> 18 19 #include <openssl/engine.h> 20 #include <openssl/sha.h> 21 #include <openssl/md5.h> 22 #include <openssl/rsa.h> 23 #include <openssl/evp.h> 24 #include <openssl/modes.h> 25 #include <openssl/aes.h> 26 #include <openssl/crypto.h> 27 28 #define OSSLTEST_LIB_NAME "OSSLTEST" 29 #include "e_ossltest_err.c" 30 31 /* Engine Id and Name */ 32 static const char *engine_ossltest_id = "ossltest"; 33 static const char *engine_ossltest_name = "OpenSSL Test engine support"; 34 35 36 /* Engine Lifetime functions */ 37 static int ossltest_destroy(ENGINE *e); 38 static int ossltest_init(ENGINE *e); 39 static int ossltest_finish(ENGINE *e); 40 void ENGINE_load_ossltest(void); 41 42 43 /* Set up digests */ 44 static int ossltest_digests(ENGINE *e, const EVP_MD **digest, 45 const int **nids, int nid); 46 47 /* MD5 */ 48 static int digest_md5_init(EVP_MD_CTX *ctx); 49 static int digest_md5_update(EVP_MD_CTX *ctx, const void *data, 50 size_t count); 51 static int digest_md5_final(EVP_MD_CTX *ctx, unsigned char *md); 52 53 static EVP_MD *_hidden_md5_md = NULL; 54 static const EVP_MD *digest_md5(void) 55 { 56 if (_hidden_md5_md == NULL) { 57 EVP_MD *md; 58 59 if ((md = EVP_MD_meth_new(NID_md5, NID_md5WithRSAEncryption)) == NULL 60 || !EVP_MD_meth_set_result_size(md, MD5_DIGEST_LENGTH) 61 || !EVP_MD_meth_set_input_blocksize(md, MD5_CBLOCK) 62 || !EVP_MD_meth_set_app_datasize(md, 63 sizeof(EVP_MD *) + sizeof(MD5_CTX)) 64 || !EVP_MD_meth_set_flags(md, 0) 65 || !EVP_MD_meth_set_init(md, digest_md5_init) 66 || !EVP_MD_meth_set_update(md, digest_md5_update) 67 || !EVP_MD_meth_set_final(md, digest_md5_final)) { 68 EVP_MD_meth_free(md); 69 md = NULL; 70 } 71 _hidden_md5_md = md; 72 } 73 return _hidden_md5_md; 74 } 75 76 /* SHA1 */ 77 static int digest_sha1_init(EVP_MD_CTX *ctx); 78 static int digest_sha1_update(EVP_MD_CTX *ctx, const void *data, 79 size_t count); 80 static int digest_sha1_final(EVP_MD_CTX *ctx, unsigned char *md); 81 82 static EVP_MD *_hidden_sha1_md = NULL; 83 static const EVP_MD *digest_sha1(void) 84 { 85 if (_hidden_sha1_md == NULL) { 86 EVP_MD *md; 87 88 if ((md = EVP_MD_meth_new(NID_sha1, NID_sha1WithRSAEncryption)) == NULL 89 || !EVP_MD_meth_set_result_size(md, SHA_DIGEST_LENGTH) 90 || !EVP_MD_meth_set_input_blocksize(md, SHA_CBLOCK) 91 || !EVP_MD_meth_set_app_datasize(md, 92 sizeof(EVP_MD *) + sizeof(SHA_CTX)) 93 || !EVP_MD_meth_set_flags(md, EVP_MD_FLAG_DIGALGID_ABSENT) 94 || !EVP_MD_meth_set_init(md, digest_sha1_init) 95 || !EVP_MD_meth_set_update(md, digest_sha1_update) 96 || !EVP_MD_meth_set_final(md, digest_sha1_final)) { 97 EVP_MD_meth_free(md); 98 md = NULL; 99 } 100 _hidden_sha1_md = md; 101 } 102 return _hidden_sha1_md; 103 } 104 105 /* SHA256 */ 106 static int digest_sha256_init(EVP_MD_CTX *ctx); 107 static int digest_sha256_update(EVP_MD_CTX *ctx, const void *data, 108 size_t count); 109 static int digest_sha256_final(EVP_MD_CTX *ctx, unsigned char *md); 110 111 static EVP_MD *_hidden_sha256_md = NULL; 112 static const EVP_MD *digest_sha256(void) 113 { 114 if (_hidden_sha256_md == NULL) { 115 EVP_MD *md; 116 117 if ((md = EVP_MD_meth_new(NID_sha256, NID_sha256WithRSAEncryption)) == NULL 118 || !EVP_MD_meth_set_result_size(md, SHA256_DIGEST_LENGTH) 119 || !EVP_MD_meth_set_input_blocksize(md, SHA256_CBLOCK) 120 || !EVP_MD_meth_set_app_datasize(md, 121 sizeof(EVP_MD *) + sizeof(SHA256_CTX)) 122 || !EVP_MD_meth_set_flags(md, EVP_MD_FLAG_DIGALGID_ABSENT) 123 || !EVP_MD_meth_set_init(md, digest_sha256_init) 124 || !EVP_MD_meth_set_update(md, digest_sha256_update) 125 || !EVP_MD_meth_set_final(md, digest_sha256_final)) { 126 EVP_MD_meth_free(md); 127 md = NULL; 128 } 129 _hidden_sha256_md = md; 130 } 131 return _hidden_sha256_md; 132 } 133 134 /* SHA384/SHA512 */ 135 static int digest_sha384_init(EVP_MD_CTX *ctx); 136 static int digest_sha512_init(EVP_MD_CTX *ctx); 137 static int digest_sha512_update(EVP_MD_CTX *ctx, const void *data, 138 size_t count); 139 static int digest_sha384_final(EVP_MD_CTX *ctx, unsigned char *md); 140 static int digest_sha512_final(EVP_MD_CTX *ctx, unsigned char *md); 141 142 static EVP_MD *_hidden_sha384_md = NULL; 143 static const EVP_MD *digest_sha384(void) 144 { 145 if (_hidden_sha384_md == NULL) { 146 EVP_MD *md; 147 148 if ((md = EVP_MD_meth_new(NID_sha384, NID_sha384WithRSAEncryption)) == NULL 149 || !EVP_MD_meth_set_result_size(md, SHA384_DIGEST_LENGTH) 150 || !EVP_MD_meth_set_input_blocksize(md, SHA512_CBLOCK) 151 || !EVP_MD_meth_set_app_datasize(md, 152 sizeof(EVP_MD *) + sizeof(SHA512_CTX)) 153 || !EVP_MD_meth_set_flags(md, EVP_MD_FLAG_DIGALGID_ABSENT) 154 || !EVP_MD_meth_set_init(md, digest_sha384_init) 155 || !EVP_MD_meth_set_update(md, digest_sha512_update) 156 || !EVP_MD_meth_set_final(md, digest_sha384_final)) { 157 EVP_MD_meth_free(md); 158 md = NULL; 159 } 160 _hidden_sha384_md = md; 161 } 162 return _hidden_sha384_md; 163 } 164 static EVP_MD *_hidden_sha512_md = NULL; 165 static const EVP_MD *digest_sha512(void) 166 { 167 if (_hidden_sha512_md == NULL) { 168 EVP_MD *md; 169 170 if ((md = EVP_MD_meth_new(NID_sha512, NID_sha512WithRSAEncryption)) == NULL 171 || !EVP_MD_meth_set_result_size(md, SHA512_DIGEST_LENGTH) 172 || !EVP_MD_meth_set_input_blocksize(md, SHA512_CBLOCK) 173 || !EVP_MD_meth_set_app_datasize(md, 174 sizeof(EVP_MD *) + sizeof(SHA512_CTX)) 175 || !EVP_MD_meth_set_flags(md, EVP_MD_FLAG_DIGALGID_ABSENT) 176 || !EVP_MD_meth_set_init(md, digest_sha512_init) 177 || !EVP_MD_meth_set_update(md, digest_sha512_update) 178 || !EVP_MD_meth_set_final(md, digest_sha512_final)) { 179 EVP_MD_meth_free(md); 180 md = NULL; 181 } 182 _hidden_sha512_md = md; 183 } 184 return _hidden_sha512_md; 185 } 186 static void destroy_digests(void) 187 { 188 EVP_MD_meth_free(_hidden_md5_md); 189 _hidden_md5_md = NULL; 190 EVP_MD_meth_free(_hidden_sha1_md); 191 _hidden_sha1_md = NULL; 192 EVP_MD_meth_free(_hidden_sha256_md); 193 _hidden_sha256_md = NULL; 194 EVP_MD_meth_free(_hidden_sha384_md); 195 _hidden_sha384_md = NULL; 196 EVP_MD_meth_free(_hidden_sha512_md); 197 _hidden_sha512_md = NULL; 198 } 199 static int ossltest_digest_nids(const int **nids) 200 { 201 static int digest_nids[6] = { 0, 0, 0, 0, 0, 0 }; 202 static int pos = 0; 203 static int init = 0; 204 205 if (!init) { 206 const EVP_MD *md; 207 if ((md = digest_md5()) != NULL) 208 digest_nids[pos++] = EVP_MD_type(md); 209 if ((md = digest_sha1()) != NULL) 210 digest_nids[pos++] = EVP_MD_type(md); 211 if ((md = digest_sha256()) != NULL) 212 digest_nids[pos++] = EVP_MD_type(md); 213 if ((md = digest_sha384()) != NULL) 214 digest_nids[pos++] = EVP_MD_type(md); 215 if ((md = digest_sha512()) != NULL) 216 digest_nids[pos++] = EVP_MD_type(md); 217 digest_nids[pos] = 0; 218 init = 1; 219 } 220 *nids = digest_nids; 221 return pos; 222 } 223 224 /* Setup ciphers */ 225 static int ossltest_ciphers(ENGINE *, const EVP_CIPHER **, 226 const int **, int); 227 228 static int ossltest_cipher_nids[] = { 229 NID_aes_128_cbc, 0 230 }; 231 232 /* AES128 */ 233 234 int ossltest_aes128_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, 235 const unsigned char *iv, int enc); 236 int ossltest_aes128_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, 237 const unsigned char *in, size_t inl); 238 239 static EVP_CIPHER *_hidden_aes_128_cbc = NULL; 240 static const EVP_CIPHER *ossltest_aes_128_cbc(void) 241 { 242 if (_hidden_aes_128_cbc == NULL 243 && ((_hidden_aes_128_cbc = EVP_CIPHER_meth_new(NID_aes_128_cbc, 244 16 /* block size */, 245 16 /* key len */)) == NULL 246 || !EVP_CIPHER_meth_set_iv_length(_hidden_aes_128_cbc,16) 247 || !EVP_CIPHER_meth_set_flags(_hidden_aes_128_cbc, 248 EVP_CIPH_FLAG_DEFAULT_ASN1 249 | EVP_CIPH_CBC_MODE) 250 || !EVP_CIPHER_meth_set_init(_hidden_aes_128_cbc, 251 ossltest_aes128_init_key) 252 || !EVP_CIPHER_meth_set_do_cipher(_hidden_aes_128_cbc, 253 ossltest_aes128_cbc_cipher) 254 || !EVP_CIPHER_meth_set_impl_ctx_size(_hidden_aes_128_cbc, 255 EVP_CIPHER_impl_ctx_size(EVP_aes_128_cbc())))) { 256 EVP_CIPHER_meth_free(_hidden_aes_128_cbc); 257 _hidden_aes_128_cbc = NULL; 258 } 259 return _hidden_aes_128_cbc; 260 } 261 static void destroy_ciphers(void) 262 { 263 EVP_CIPHER_meth_free(_hidden_aes_128_cbc); 264 _hidden_aes_128_cbc = NULL; 265 } 266 267 static int bind_ossltest(ENGINE *e) 268 { 269 /* Ensure the ossltest error handling is set up */ 270 ERR_load_OSSLTEST_strings(); 271 272 if (!ENGINE_set_id(e, engine_ossltest_id) 273 || !ENGINE_set_name(e, engine_ossltest_name) 274 || !ENGINE_set_digests(e, ossltest_digests) 275 || !ENGINE_set_ciphers(e, ossltest_ciphers) 276 || !ENGINE_set_destroy_function(e, ossltest_destroy) 277 || !ENGINE_set_init_function(e, ossltest_init) 278 || !ENGINE_set_finish_function(e, ossltest_finish)) { 279 OSSLTESTerr(OSSLTEST_F_BIND_OSSLTEST, OSSLTEST_R_INIT_FAILED); 280 return 0; 281 } 282 283 return 1; 284 } 285 286 #ifndef OPENSSL_NO_DYNAMIC_ENGINE 287 static int bind_helper(ENGINE *e, const char *id) 288 { 289 if (id && (strcmp(id, engine_ossltest_id) != 0)) 290 return 0; 291 if (!bind_ossltest(e)) 292 return 0; 293 return 1; 294 } 295 296 IMPLEMENT_DYNAMIC_CHECK_FN() 297 IMPLEMENT_DYNAMIC_BIND_FN(bind_helper) 298 #endif 299 300 static ENGINE *engine_ossltest(void) 301 { 302 ENGINE *ret = ENGINE_new(); 303 if (ret == NULL) 304 return NULL; 305 if (!bind_ossltest(ret)) { 306 ENGINE_free(ret); 307 return NULL; 308 } 309 return ret; 310 } 311 312 void ENGINE_load_ossltest(void) 313 { 314 /* Copied from eng_[openssl|dyn].c */ 315 ENGINE *toadd = engine_ossltest(); 316 if (!toadd) 317 return; 318 ENGINE_add(toadd); 319 ENGINE_free(toadd); 320 ERR_clear_error(); 321 } 322 323 324 static int ossltest_init(ENGINE *e) 325 { 326 return 1; 327 } 328 329 330 static int ossltest_finish(ENGINE *e) 331 { 332 return 1; 333 } 334 335 336 static int ossltest_destroy(ENGINE *e) 337 { 338 destroy_digests(); 339 destroy_ciphers(); 340 ERR_unload_OSSLTEST_strings(); 341 return 1; 342 } 343 344 static int ossltest_digests(ENGINE *e, const EVP_MD **digest, 345 const int **nids, int nid) 346 { 347 int ok = 1; 348 if (!digest) { 349 /* We are returning a list of supported nids */ 350 return ossltest_digest_nids(nids); 351 } 352 /* We are being asked for a specific digest */ 353 switch (nid) { 354 case NID_md5: 355 *digest = digest_md5(); 356 break; 357 case NID_sha1: 358 *digest = digest_sha1(); 359 break; 360 case NID_sha256: 361 *digest = digest_sha256(); 362 break; 363 case NID_sha384: 364 *digest = digest_sha384(); 365 break; 366 case NID_sha512: 367 *digest = digest_sha512(); 368 break; 369 default: 370 ok = 0; 371 *digest = NULL; 372 break; 373 } 374 return ok; 375 } 376 377 static int ossltest_ciphers(ENGINE *e, const EVP_CIPHER **cipher, 378 const int **nids, int nid) 379 { 380 int ok = 1; 381 if (!cipher) { 382 /* We are returning a list of supported nids */ 383 *nids = ossltest_cipher_nids; 384 return (sizeof(ossltest_cipher_nids) - 1) 385 / sizeof(ossltest_cipher_nids[0]); 386 } 387 /* We are being asked for a specific cipher */ 388 switch (nid) { 389 case NID_aes_128_cbc: 390 *cipher = ossltest_aes_128_cbc(); 391 break; 392 default: 393 ok = 0; 394 *cipher = NULL; 395 break; 396 } 397 return ok; 398 } 399 400 static void fill_known_data(unsigned char *md, unsigned int len) 401 { 402 unsigned int i; 403 404 for (i=0; i<len; i++) { 405 md[i] = (unsigned char)(i & 0xff); 406 } 407 } 408 409 /* 410 * MD5 implementation. We go through the motions of doing MD5 by deferring to 411 * the standard implementation. Then we overwrite the result with a will defined 412 * value, so that all "MD5" digests using the test engine always end up with 413 * the same value. 414 */ 415 #undef data 416 #define data(ctx) ((MD5_CTX *)EVP_MD_CTX_md_data(ctx)) 417 static int digest_md5_init(EVP_MD_CTX *ctx) 418 { 419 return MD5_Init(data(ctx)); 420 } 421 422 static int digest_md5_update(EVP_MD_CTX *ctx, const void *data, 423 size_t count) 424 { 425 return MD5_Update(data(ctx), data, (size_t)count); 426 } 427 428 static int digest_md5_final(EVP_MD_CTX *ctx, unsigned char *md) 429 { 430 int ret; 431 ret = MD5_Final(md, data(ctx)); 432 433 if (ret > 0) { 434 fill_known_data(md, MD5_DIGEST_LENGTH); 435 } 436 return ret; 437 } 438 439 /* 440 * SHA1 implementation. 441 */ 442 #undef data 443 #define data(ctx) ((SHA_CTX *)EVP_MD_CTX_md_data(ctx)) 444 static int digest_sha1_init(EVP_MD_CTX *ctx) 445 { 446 return SHA1_Init(data(ctx)); 447 } 448 449 static int digest_sha1_update(EVP_MD_CTX *ctx, const void *data, 450 size_t count) 451 { 452 return SHA1_Update(data(ctx), data, (size_t)count); 453 } 454 455 static int digest_sha1_final(EVP_MD_CTX *ctx, unsigned char *md) 456 { 457 int ret; 458 ret = SHA1_Final(md, data(ctx)); 459 460 if (ret > 0) { 461 fill_known_data(md, SHA_DIGEST_LENGTH); 462 } 463 return ret; 464 } 465 466 /* 467 * SHA256 implementation. 468 */ 469 #undef data 470 #define data(ctx) ((SHA256_CTX *)EVP_MD_CTX_md_data(ctx)) 471 static int digest_sha256_init(EVP_MD_CTX *ctx) 472 { 473 return SHA256_Init(data(ctx)); 474 } 475 476 static int digest_sha256_update(EVP_MD_CTX *ctx, const void *data, 477 size_t count) 478 { 479 return SHA256_Update(data(ctx), data, (size_t)count); 480 } 481 482 static int digest_sha256_final(EVP_MD_CTX *ctx, unsigned char *md) 483 { 484 int ret; 485 ret = SHA256_Final(md, data(ctx)); 486 487 if (ret > 0) { 488 fill_known_data(md, SHA256_DIGEST_LENGTH); 489 } 490 return ret; 491 } 492 493 /* 494 * SHA384/512 implementation. 495 */ 496 #undef data 497 #define data(ctx) ((SHA512_CTX *)EVP_MD_CTX_md_data(ctx)) 498 static int digest_sha384_init(EVP_MD_CTX *ctx) 499 { 500 return SHA384_Init(data(ctx)); 501 } 502 503 static int digest_sha512_init(EVP_MD_CTX *ctx) 504 { 505 return SHA512_Init(data(ctx)); 506 } 507 508 static int digest_sha512_update(EVP_MD_CTX *ctx, const void *data, 509 size_t count) 510 { 511 return SHA512_Update(data(ctx), data, (size_t)count); 512 } 513 514 static int digest_sha384_final(EVP_MD_CTX *ctx, unsigned char *md) 515 { 516 int ret; 517 /* Actually uses SHA512_Final! */ 518 ret = SHA512_Final(md, data(ctx)); 519 520 if (ret > 0) { 521 fill_known_data(md, SHA384_DIGEST_LENGTH); 522 } 523 return ret; 524 } 525 526 static int digest_sha512_final(EVP_MD_CTX *ctx, unsigned char *md) 527 { 528 int ret; 529 ret = SHA512_Final(md, data(ctx)); 530 531 if (ret > 0) { 532 fill_known_data(md, SHA512_DIGEST_LENGTH); 533 } 534 return ret; 535 } 536 537 /* 538 * AES128 Implementation 539 */ 540 541 int ossltest_aes128_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, 542 const unsigned char *iv, int enc) 543 { 544 return EVP_CIPHER_meth_get_init(EVP_aes_128_cbc()) (ctx, key, iv, enc); 545 } 546 547 int ossltest_aes128_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, 548 const unsigned char *in, size_t inl) 549 { 550 unsigned char *tmpbuf; 551 int ret; 552 553 tmpbuf = OPENSSL_malloc(inl); 554 if (tmpbuf == NULL) 555 return -1; 556 557 /* Remember what we were asked to encrypt */ 558 memcpy(tmpbuf, in, inl); 559 560 /* Go through the motions of encrypting it */ 561 ret = EVP_CIPHER_meth_get_do_cipher(EVP_aes_128_cbc())(ctx, out, in, inl); 562 563 /* Throw it all away and just use the plaintext as the output */ 564 memcpy(out, tmpbuf, inl); 565 OPENSSL_free(tmpbuf); 566 567 return ret; 568 } 569