1 /* $OpenBSD: eng_openssl.c,v 1.15 2022/01/09 23:55:31 tb Exp $ */ 2 /* Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL 3 * project 2000. 4 */ 5 /* ==================================================================== 6 * Copyright (c) 1999-2001 The OpenSSL Project. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in 17 * the documentation and/or other materials provided with the 18 * distribution. 19 * 20 * 3. All advertising materials mentioning features or use of this 21 * software must display the following acknowledgment: 22 * "This product includes software developed by the OpenSSL Project 23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" 24 * 25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 26 * endorse or promote products derived from this software without 27 * prior written permission. For written permission, please contact 28 * licensing@OpenSSL.org. 29 * 30 * 5. Products derived from this software may not be called "OpenSSL" 31 * nor may "OpenSSL" appear in their names without prior written 32 * permission of the OpenSSL Project. 33 * 34 * 6. Redistributions of any form whatsoever must retain the following 35 * acknowledgment: 36 * "This product includes software developed by the OpenSSL Project 37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" 38 * 39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 50 * OF THE POSSIBILITY OF SUCH DAMAGE. 51 * ==================================================================== 52 * 53 * This product includes cryptographic software written by Eric Young 54 * (eay@cryptsoft.com). This product includes software written by Tim 55 * Hudson (tjh@cryptsoft.com). 56 * 57 */ 58 /* ==================================================================== 59 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. 60 * ECDH support in OpenSSL originally developed by 61 * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project. 62 */ 63 64 #include <stdio.h> 65 #include <string.h> 66 67 #include <openssl/opensslconf.h> 68 69 #include <openssl/crypto.h> 70 #include <openssl/dso.h> 71 #include <openssl/engine.h> 72 #include <openssl/err.h> 73 #include <openssl/evp.h> 74 #include <openssl/pem.h> 75 #include <openssl/rand.h> 76 77 #ifndef OPENSSL_NO_DH 78 #include <openssl/dh.h> 79 #endif 80 #ifndef OPENSSL_NO_DSA 81 #include <openssl/dsa.h> 82 #endif 83 #ifndef OPENSSL_NO_RSA 84 #include <openssl/rsa.h> 85 #endif 86 87 #include "evp_locl.h" 88 89 /* This testing gunk is implemented (and explained) lower down. It also assumes 90 * the application explicitly calls "ENGINE_load_openssl()" because this is no 91 * longer automatic in ENGINE_load_builtin_engines(). */ 92 #define TEST_ENG_OPENSSL_RC4 93 #define TEST_ENG_OPENSSL_PKEY 94 /* #define TEST_ENG_OPENSSL_RC4_OTHERS */ 95 #define TEST_ENG_OPENSSL_RC4_P_INIT 96 /* #define TEST_ENG_OPENSSL_RC4_P_CIPHER */ 97 #define TEST_ENG_OPENSSL_SHA 98 /* #define TEST_ENG_OPENSSL_SHA_OTHERS */ 99 /* #define TEST_ENG_OPENSSL_SHA_P_INIT */ 100 /* #define TEST_ENG_OPENSSL_SHA_P_UPDATE */ 101 /* #define TEST_ENG_OPENSSL_SHA_P_FINAL */ 102 103 /* Now check what of those algorithms are actually enabled */ 104 #ifdef OPENSSL_NO_RC4 105 #undef TEST_ENG_OPENSSL_RC4 106 #undef TEST_ENG_OPENSSL_RC4_OTHERS 107 #undef TEST_ENG_OPENSSL_RC4_P_INIT 108 #undef TEST_ENG_OPENSSL_RC4_P_CIPHER 109 #endif 110 #if defined(OPENSSL_NO_SHA) || defined(OPENSSL_NO_SHA1) 111 #undef TEST_ENG_OPENSSL_SHA 112 #undef TEST_ENG_OPENSSL_SHA_OTHERS 113 #undef TEST_ENG_OPENSSL_SHA_P_INIT 114 #undef TEST_ENG_OPENSSL_SHA_P_UPDATE 115 #undef TEST_ENG_OPENSSL_SHA_P_FINAL 116 #endif 117 118 #ifdef TEST_ENG_OPENSSL_RC4 119 static int openssl_ciphers(ENGINE *e, const EVP_CIPHER **cipher, 120 const int **nids, int nid); 121 #endif 122 #ifdef TEST_ENG_OPENSSL_SHA 123 static int openssl_digests(ENGINE *e, const EVP_MD **digest, 124 const int **nids, int nid); 125 #endif 126 127 #ifdef TEST_ENG_OPENSSL_PKEY 128 static EVP_PKEY *openssl_load_privkey(ENGINE *eng, const char *key_id, 129 UI_METHOD *ui_method, void *callback_data); 130 #endif 131 132 /* The constants used when creating the ENGINE */ 133 static const char *engine_openssl_id = "openssl"; 134 static const char *engine_openssl_name = "Software engine support"; 135 136 /* This internal function is used by ENGINE_openssl() and possibly by the 137 * "dynamic" ENGINE support too */ 138 static int 139 bind_helper(ENGINE *e) 140 { 141 if (!ENGINE_set_id(e, engine_openssl_id) || 142 !ENGINE_set_name(e, engine_openssl_name) 143 #ifndef TEST_ENG_OPENSSL_NO_ALGORITHMS 144 #ifndef OPENSSL_NO_RSA 145 || !ENGINE_set_RSA(e, RSA_get_default_method()) 146 #endif 147 #ifndef OPENSSL_NO_DSA 148 || !ENGINE_set_DSA(e, DSA_get_default_method()) 149 #endif 150 #ifndef OPENSSL_NO_ECDH 151 || !ENGINE_set_ECDH(e, ECDH_OpenSSL()) 152 #endif 153 #ifndef OPENSSL_NO_ECDSA 154 || !ENGINE_set_ECDSA(e, ECDSA_OpenSSL()) 155 #endif 156 #ifndef OPENSSL_NO_DH 157 || !ENGINE_set_DH(e, DH_get_default_method()) 158 #endif 159 || !ENGINE_set_RAND(e, RAND_SSLeay()) 160 #ifdef TEST_ENG_OPENSSL_RC4 161 || !ENGINE_set_ciphers(e, openssl_ciphers) 162 #endif 163 #ifdef TEST_ENG_OPENSSL_SHA 164 || !ENGINE_set_digests(e, openssl_digests) 165 #endif 166 #endif 167 #ifdef TEST_ENG_OPENSSL_PKEY 168 || !ENGINE_set_load_privkey_function(e, openssl_load_privkey) 169 #endif 170 ) 171 return 0; 172 /* If we add errors to this ENGINE, ensure the error handling is setup here */ 173 /* openssl_load_error_strings(); */ 174 return 1; 175 } 176 177 static ENGINE * 178 engine_openssl(void) 179 { 180 ENGINE *ret = ENGINE_new(); 181 182 if (ret == NULL) 183 return NULL; 184 if (!bind_helper(ret)) { 185 ENGINE_free(ret); 186 return NULL; 187 } 188 return ret; 189 } 190 191 void 192 ENGINE_load_openssl(void) 193 { 194 ENGINE *toadd = engine_openssl(); 195 196 if (toadd == NULL) 197 return; 198 (void) ENGINE_add(toadd); 199 /* If the "add" worked, it gets a structural reference. So either way, 200 * we release our just-created reference. */ 201 ENGINE_free(toadd); 202 ERR_clear_error(); 203 } 204 205 /* This stuff is needed if this ENGINE is being compiled into a self-contained 206 * shared-library. */ 207 #ifdef ENGINE_DYNAMIC_SUPPORT 208 static int 209 bind_fn(ENGINE *e, const char *id) 210 { 211 if (id && (strcmp(id, engine_openssl_id) != 0)) 212 return 0; 213 if (!bind_helper(e)) 214 return 0; 215 return 1; 216 } 217 IMPLEMENT_DYNAMIC_CHECK_FN() 218 IMPLEMENT_DYNAMIC_BIND_FN(bind_fn) 219 #endif /* ENGINE_DYNAMIC_SUPPORT */ 220 221 #ifdef TEST_ENG_OPENSSL_RC4 222 /* This section of code compiles an "alternative implementation" of two modes of 223 * RC4 into this ENGINE. The result is that EVP_CIPHER operation for "rc4" 224 * should under normal circumstances go via this support rather than the default 225 * EVP support. There are other symbols to tweak the testing; 226 * TEST_ENC_OPENSSL_RC4_OTHERS - print a one line message to stderr each time 227 * we're asked for a cipher we don't support (should not happen). 228 * TEST_ENG_OPENSSL_RC4_P_INIT - print a one line message to stderr each time 229 * the "init_key" handler is called. 230 * TEST_ENG_OPENSSL_RC4_P_CIPHER - ditto for the "cipher" handler. 231 */ 232 #include <openssl/rc4.h> 233 #define TEST_RC4_KEY_SIZE 16 234 static int test_cipher_nids[] = {NID_rc4, NID_rc4_40}; 235 static int test_cipher_nids_number = 2; 236 237 typedef struct { 238 unsigned char key[TEST_RC4_KEY_SIZE]; 239 RC4_KEY ks; 240 } TEST_RC4_KEY; 241 242 #define test(ctx) ((TEST_RC4_KEY *)(ctx)->cipher_data) 243 static int 244 test_rc4_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, 245 const unsigned char *iv, int enc) 246 { 247 #ifdef TEST_ENG_OPENSSL_RC4_P_INIT 248 fprintf(stderr, "(TEST_ENG_OPENSSL_RC4) test_init_key() called\n"); 249 #endif 250 memcpy(&test(ctx)->key[0], key, EVP_CIPHER_CTX_key_length(ctx)); 251 RC4_set_key(&test(ctx)->ks, EVP_CIPHER_CTX_key_length(ctx), 252 test(ctx)->key); 253 return 1; 254 } 255 256 static int 257 test_rc4_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, 258 const unsigned char *in, size_t inl) 259 { 260 #ifdef TEST_ENG_OPENSSL_RC4_P_CIPHER 261 fprintf(stderr, "(TEST_ENG_OPENSSL_RC4) test_cipher() called\n"); 262 #endif 263 RC4(&test(ctx)->ks, inl, in, out); 264 return 1; 265 } 266 267 static const EVP_CIPHER test_r4_cipher = { 268 NID_rc4, 269 1, TEST_RC4_KEY_SIZE, 0, 270 EVP_CIPH_VARIABLE_LENGTH, 271 test_rc4_init_key, 272 test_rc4_cipher, 273 NULL, 274 sizeof(TEST_RC4_KEY), 275 NULL, 276 NULL, 277 NULL, 278 NULL 279 }; 280 281 static const EVP_CIPHER test_r4_40_cipher = { 282 NID_rc4_40, 283 1,5 /* 40 bit */,0, 284 EVP_CIPH_VARIABLE_LENGTH, 285 test_rc4_init_key, 286 test_rc4_cipher, 287 NULL, 288 sizeof(TEST_RC4_KEY), 289 NULL, 290 NULL, 291 NULL, 292 NULL 293 }; 294 295 static int 296 openssl_ciphers(ENGINE *e, const EVP_CIPHER **cipher, const int **nids, int nid) 297 { 298 if (!cipher) { 299 /* We are returning a list of supported nids */ 300 *nids = test_cipher_nids; 301 return test_cipher_nids_number; 302 } 303 /* We are being asked for a specific cipher */ 304 if (nid == NID_rc4) 305 *cipher = &test_r4_cipher; 306 else if (nid == NID_rc4_40) 307 *cipher = &test_r4_40_cipher; 308 else { 309 #ifdef TEST_ENG_OPENSSL_RC4_OTHERS 310 fprintf(stderr, "(TEST_ENG_OPENSSL_RC4) returning NULL for " 311 "nid %d\n", nid); 312 #endif 313 *cipher = NULL; 314 return 0; 315 } 316 return 1; 317 } 318 #endif 319 320 #ifdef TEST_ENG_OPENSSL_SHA 321 /* Much the same sort of comment as for TEST_ENG_OPENSSL_RC4 */ 322 #include <openssl/sha.h> 323 static int test_digest_nids[] = {NID_sha1}; 324 static int test_digest_nids_number = 1; 325 326 static int 327 test_sha1_init(EVP_MD_CTX *ctx) 328 { 329 #ifdef TEST_ENG_OPENSSL_SHA_P_INIT 330 fprintf(stderr, "(TEST_ENG_OPENSSL_SHA) test_sha1_init() called\n"); 331 #endif 332 return SHA1_Init(ctx->md_data); 333 } 334 335 static int 336 test_sha1_update(EVP_MD_CTX *ctx, const void *data, size_t count) 337 { 338 #ifdef TEST_ENG_OPENSSL_SHA_P_UPDATE 339 fprintf(stderr, "(TEST_ENG_OPENSSL_SHA) test_sha1_update() called\n"); 340 #endif 341 return SHA1_Update(ctx->md_data, data, count); 342 } 343 344 static int 345 test_sha1_final(EVP_MD_CTX *ctx, unsigned char *md) 346 { 347 #ifdef TEST_ENG_OPENSSL_SHA_P_FINAL 348 fprintf(stderr, "(TEST_ENG_OPENSSL_SHA) test_sha1_final() called\n"); 349 #endif 350 return SHA1_Final(md, ctx->md_data); 351 } 352 353 static const EVP_MD test_sha_md = { 354 .type = NID_sha1, 355 .pkey_type = NID_sha1WithRSAEncryption, 356 .md_size = SHA_DIGEST_LENGTH, 357 .flags = 0, 358 .init = test_sha1_init, 359 .update = test_sha1_update, 360 .final = test_sha1_final, 361 .copy = NULL, 362 .cleanup = NULL, 363 .block_size = SHA_CBLOCK, 364 .ctx_size = sizeof(EVP_MD *) + sizeof(SHA_CTX), 365 }; 366 367 static int 368 openssl_digests(ENGINE *e, const EVP_MD **digest, const int **nids, int nid) 369 { 370 if (!digest) { 371 /* We are returning a list of supported nids */ 372 *nids = test_digest_nids; 373 return test_digest_nids_number; 374 } 375 /* We are being asked for a specific digest */ 376 if (nid == NID_sha1) 377 *digest = &test_sha_md; 378 else { 379 #ifdef TEST_ENG_OPENSSL_SHA_OTHERS 380 fprintf(stderr, "(TEST_ENG_OPENSSL_SHA) returning NULL for " 381 "nid %d\n", nid); 382 #endif 383 *digest = NULL; 384 return 0; 385 } 386 return 1; 387 } 388 #endif 389 390 #ifdef TEST_ENG_OPENSSL_PKEY 391 static EVP_PKEY * 392 openssl_load_privkey(ENGINE *eng, const char *key_id, UI_METHOD *ui_method, 393 void *callback_data) 394 { 395 BIO *in; 396 EVP_PKEY *key; 397 398 fprintf(stderr, "(TEST_ENG_OPENSSL_PKEY)Loading Private key %s\n", 399 key_id); 400 in = BIO_new_file(key_id, "r"); 401 if (!in) 402 return NULL; 403 key = PEM_read_bio_PrivateKey(in, NULL, 0, NULL); 404 BIO_free(in); 405 return key; 406 } 407 #endif 408