|
63 | 63 | #endif
|
64 | 64 |
|
65 | 65 | #if HAVE_OPENSSL_ENGINE
|
| 66 | +#include <openssl/ui.h> |
66 | 67 | #include <openssl/engine.h>
|
67 | 68 |
|
68 | 69 | static bool engine_initialized = false; /* GLOBAL */
|
@@ -1070,4 +1071,59 @@ memcmp_constant_time(const void *a, const void *b, size_t size)
|
1070 | 1071 | {
|
1071 | 1072 | return CRYPTO_memcmp(a, b, size);
|
1072 | 1073 | }
|
| 1074 | + |
| 1075 | +#if HAVE_OPENSSL_ENGINE |
| 1076 | +static int |
| 1077 | +ui_reader(UI *ui, UI_STRING *uis) |
| 1078 | +{ |
| 1079 | + SSL_CTX *ctx = UI_get0_user_data(ui); |
| 1080 | + |
| 1081 | + if (UI_get_string_type(uis) == UIT_PROMPT) { |
| 1082 | + pem_password_cb *cb = SSL_CTX_get_default_passwd_cb(ctx); |
| 1083 | + void *d = SSL_CTX_get_default_passwd_cb_userdata(ctx); |
| 1084 | + char password[64]; |
| 1085 | + |
| 1086 | + cb(password, sizeof(password), 0, d); |
| 1087 | + UI_set_result(ui, uis, password); |
| 1088 | + |
| 1089 | + return 1; |
| 1090 | + } |
| 1091 | + return 0; |
| 1092 | +} |
| 1093 | +#endif |
| 1094 | + |
| 1095 | +EVP_PKEY * |
| 1096 | +engine_load_key(const char *file, SSL_CTX *ctx) |
| 1097 | +{ |
| 1098 | +#if HAVE_OPENSSL_ENGINE |
| 1099 | + UI_METHOD *ui; |
| 1100 | + EVP_PKEY *pkey; |
| 1101 | + |
| 1102 | + if (!engine_persist) |
| 1103 | + return NULL; |
| 1104 | + |
| 1105 | + /* this will print out the error from BIO_read */ |
| 1106 | + crypto_msg(M_INFO, "PEM_read_bio failed, now trying engine method to load private key"); |
| 1107 | + |
| 1108 | + ui = UI_create_method("openvpn"); |
| 1109 | + if (!ui) { |
| 1110 | + crypto_msg(M_FATAL, "Engine UI creation failed"); |
| 1111 | + return NULL; |
| 1112 | + } |
| 1113 | + |
| 1114 | + UI_method_set_reader(ui, ui_reader); |
| 1115 | + |
| 1116 | + ENGINE_init(engine_persist); |
| 1117 | + pkey = ENGINE_load_private_key(engine_persist, file, ui, ctx); |
| 1118 | + ENGINE_finish(engine_persist); |
| 1119 | + if (!pkey) |
| 1120 | + crypto_msg(M_FATAL, "Engine could not load key file"); |
| 1121 | + |
| 1122 | + UI_destroy_method(ui); |
| 1123 | + return pkey; |
| 1124 | +#else |
| 1125 | + return NULL; |
| 1126 | +#endif |
| 1127 | +} |
| 1128 | + |
1073 | 1129 | #endif /* ENABLE_CRYPTO_OPENSSL */
|
0 commit comments