Skip to content

Commit 06672a3

Browse files
[release/8.0-staging] Re-try loading ENGINE keys with a non-NULL UI_METHOD
Co-authored-by: Kevin Jones <[email protected]>
1 parent 177d80c commit 06672a3

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

src/native/libs/System.Security.Cryptography.Native/opensslshim.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include <openssl/sha.h>
3232
#include <openssl/ssl.h>
3333
#include <openssl/tls1.h>
34+
#include <openssl/ui.h>
3435
#include <openssl/x509.h>
3536
#include <openssl/x509v3.h>
3637

@@ -607,6 +608,8 @@ EVP_PKEY *ENGINE_load_public_key(ENGINE *e, const char *key_id,
607608
LIGHTUP_FUNCTION(SSL_verify_client_post_handshake) \
608609
LIGHTUP_FUNCTION(SSL_set_post_handshake_auth) \
609610
REQUIRED_FUNCTION(SSL_version) \
611+
REQUIRED_FUNCTION(UI_create_method) \
612+
REQUIRED_FUNCTION(UI_destroy_method) \
610613
FALLBACK_FUNCTION(X509_check_host) \
611614
REQUIRED_FUNCTION(X509_check_purpose) \
612615
REQUIRED_FUNCTION(X509_cmp_current_time) \
@@ -1124,6 +1127,8 @@ FOR_ALL_OPENSSL_FUNCTIONS
11241127
#define SSL_set_post_handshake_auth SSL_set_post_handshake_auth_ptr
11251128
#define SSL_version SSL_version_ptr
11261129
#define TLS_method TLS_method_ptr
1130+
#define UI_create_method UI_create_method_ptr
1131+
#define UI_destroy_method UI_destroy_method_ptr
11271132
#define X509_check_host X509_check_host_ptr
11281133
#define X509_check_purpose X509_check_purpose_ptr
11291134
#define X509_cmp_current_time X509_cmp_current_time_ptr

src/native/libs/System.Security.Cryptography.Native/pal_evp_pkey.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,7 @@ static EVP_PKEY* LoadKeyFromEngine(
526526
*haveEngine = 1;
527527
EVP_PKEY* ret = NULL;
528528
ENGINE* engine = NULL;
529+
UI_METHOD* ui = NULL;
529530

530531
// Per https://github.com/openssl/openssl/discussions/21427
531532
// using EVP_PKEY after freeing ENGINE is correct.
@@ -537,12 +538,30 @@ static EVP_PKEY* LoadKeyFromEngine(
537538
{
538539
ret = load_func(engine, keyName, NULL, NULL);
539540

541+
if (ret == NULL)
542+
{
543+
// Some engines do not tolerate having NULL passed to the ui_method parameter.
544+
// We re-try with a non-NULL UI_METHOD.
545+
ERR_clear_error();
546+
ui = UI_create_method(".NET NULL UI");
547+
548+
if (ui)
549+
{
550+
ret = load_func(engine, keyName, ui, NULL);
551+
}
552+
}
553+
540554
ENGINE_finish(engine);
541555
}
542556

543557
ENGINE_free(engine);
544558
}
545559

560+
if (ui)
561+
{
562+
UI_destroy_method(ui);
563+
}
564+
546565
return ret;
547566
}
548567

0 commit comments

Comments
 (0)