-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SSLNTV-29 Add the ability to use a custom OpenSSL engine #16
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,7 +48,7 @@ static int SSL_CTX_app_data1_idx = -1; /* context metadata */ | |
|
||
static int OPENSSL_PROTOCOLS[6] = { SSL3_VERSION, SSL3_VERSION, TLS1_VERSION, TLS1_1_VERSION, TLS1_2_VERSION, TLS1_3_VERSION}; | ||
|
||
WF_OPENSSL(jint, initialize) (JNIEnv *e, jobject o, jstring libCryptoPath, jstring libSSLPath); | ||
WF_OPENSSL(jint, initialize) (JNIEnv *e, jobject o, jstring libCryptoPath, jstring libSSLPath, jstring customEngine); | ||
WF_OPENSSL(jlong, makeSSLContext)(JNIEnv *e, jobject o, jint protocol, jint mode); | ||
WF_OPENSSL(jobjectArray, getCiphers)(JNIEnv *e, jobject o, jlong ssl); | ||
WF_OPENSSL(jboolean, setCipherSuites)(JNIEnv *e, jobject o, jlong ssl, jstring ciphers); | ||
|
@@ -443,6 +443,12 @@ int load_openssl_dynamic_methods(JNIEnv *e, const char * libCryptoPath, const ch | |
REQUIRE_CRYPTO_SYMBOL(EVP_PKEY_type); | ||
REQUIRE_CRYPTO_SYMBOL(EVP_sha1); | ||
REQUIRE_CRYPTO_SYMBOL(OPENSSL_load_builtin_modules); | ||
REQUIRE_CRYPTO_SYMBOL(ENGINE_register_all_complete); | ||
REQUIRE_CRYPTO_SYMBOL(ENGINE_by_id); | ||
REQUIRE_CRYPTO_SYMBOL(ENGINE_ctrl); | ||
REQUIRE_CRYPTO_SYMBOL(ENGINE_ctrl_cmd_string); | ||
REQUIRE_CRYPTO_SYMBOL(ENGINE_free); | ||
REQUIRE_CRYPTO_SYMBOL(ENGINE_set_default); | ||
REQUIRE_CRYPTO_SYMBOL(PEM_read_bio_PrivateKey); | ||
REQUIRE_CRYPTO_SYMBOL(X509_CRL_verify); | ||
REQUIRE_CRYPTO_SYMBOL(X509_LOOKUP_ctrl); | ||
|
@@ -487,30 +493,37 @@ int load_openssl_dynamic_methods(JNIEnv *e, const char * libCryptoPath, const ch | |
return 0; | ||
} | ||
|
||
WF_OPENSSL(jint, initialize) (JNIEnv *e, jobject o, jstring libCryptoPath, jstring libSSLPath) { | ||
WF_OPENSSL(jint, initialize) (JNIEnv *e, jobject o, jstring libCryptoPath, jstring libSSLPath, jstring customEngine) { | ||
#pragma comment(linker, "/EXPORT:"__FUNCTION__"="__FUNCDNAME__) | ||
jclass clazz; | ||
jclass sClazz; | ||
const char * cPath = NULL; | ||
const char * sPath = NULL; | ||
const char * engine = NULL; | ||
TCN_ALLOC_CSTRING(libCryptoPath); | ||
TCN_ALLOC_CSTRING(libSSLPath); | ||
TCN_ALLOC_CSTRING(customEngine); | ||
if(libCryptoPath != NULL) { | ||
cPath = J2S(libCryptoPath); | ||
} | ||
if(libSSLPath != NULL) { | ||
sPath = J2S(libSSLPath); | ||
} | ||
if(customEngine != NULL) { | ||
engine = J2S(customEngine); | ||
} | ||
if(load_openssl_dynamic_methods(e, cPath, sPath) != 0) { | ||
TCN_FREE_CSTRING(libCryptoPath); | ||
TCN_FREE_CSTRING(libSSLPath); | ||
TCN_FREE_CSTRING(customEngine); | ||
return 0; | ||
} | ||
TCN_FREE_CSTRING(libCryptoPath); | ||
TCN_FREE_CSTRING(libSSLPath); | ||
|
||
/* Check if already initialized */ | ||
if (ssl_initialized++) { | ||
TCN_FREE_CSTRING(customEngine); | ||
return 0; | ||
} | ||
/* We must register the library in full, to ensure our configuration | ||
|
@@ -534,7 +547,42 @@ WF_OPENSSL(jint, initialize) (JNIEnv *e, jobject o, jstring libCryptoPath, jstri | |
|
||
ssl_thread_setup(); | ||
|
||
/* TODO: engine support? */ | ||
if (customEngine != NULL) { | ||
if (strcmp(engine, "auto") == 0) { | ||
crypto_methods.ENGINE_register_all_complete(); | ||
} else { | ||
ENGINE *ssl_engine = crypto_methods.ENGINE_by_id(engine); | ||
if (ssl_engine == NULL) { | ||
ssl_engine = crypto_methods.ENGINE_by_id("dynamic"); | ||
if (ssl_engine) { | ||
if (!crypto_methods.ENGINE_ctrl_cmd_string(ssl_engine, "SO_PATH", engine, 0) || !crypto_methods.ENGINE_ctrl_cmd_string(ssl_engine, "LOAD", NULL, 0)) { | ||
char err[2048]; | ||
crypto_methods.ENGINE_free(ssl_engine); | ||
ssl_engine = NULL; | ||
generate_openssl_stack_error(e, err, sizeof(err)); | ||
tcn_Throw(e, "Could not load openssl custom engine (%s) .so library file: %s", engine, err); | ||
goto init_failed; | ||
} | ||
} | ||
} | ||
|
||
if (ssl_engine) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For this case, it looks like both the Netty and Tomcat OpenSSL implementations also do this before attempting to set the default:
See https://github.com/netty/netty-tcnative/blob/main/openssl-dynamic/src/main/c/ssl.c#L804-L810 and https://github.com/apache/tomcat-native/blob/main/native/src/ssl.c#L540-L547. Do you think we could do something similar here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't know the real reason to add this logic. So I need to check. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This logic is specifically for the "chil" engine. It's not strictly required to add this for your changes but since both the Netty and Tomcat OpenSSL implementations have this, I think we might as well add the same logic here as well. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The +/* Flags specific to the nCipher "chil" engine */
+# define ENGINE_CTRL_CHIL_SET_FORKCHECK 100 And always execute the Nit-picky request, please add the brackets to the if. 😄 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @heyuanliu-intel The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. okay. I will remove it. |
||
if (strcmp(engine, "chil") == 0) { | ||
crypto_methods.ENGINE_ctrl(ssl_engine, ENGINE_CTRL_CHIL_SET_FORKCHECK, 1, 0, 0); | ||
} | ||
if (!crypto_methods.ENGINE_set_default(ssl_engine, ENGINE_METHOD_ALL)) { | ||
char err[2048]; | ||
crypto_methods.ENGINE_free(ssl_engine); | ||
ssl_engine = NULL; | ||
generate_openssl_stack_error(e, err, sizeof(err)); | ||
tcn_Throw(e, "Could not set custom engine (%s) to default engine: %s", engine, err); | ||
goto init_failed; | ||
} | ||
} | ||
} | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You are right and I will fix this. |
||
TCN_FREE_CSTRING(customEngine); | ||
|
||
/* Cache the byte[].class for performance reasons */ | ||
clazz = (*e)->FindClass(e, "[B"); | ||
|
@@ -548,6 +596,11 @@ WF_OPENSSL(jint, initialize) (JNIEnv *e, jobject o, jstring libCryptoPath, jstri | |
session_init(e); | ||
|
||
return (jint)0; | ||
|
||
init_failed: | ||
ssl_initialized = 0; | ||
TCN_FREE_CSTRING(customEngine); | ||
return 0; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think there should be a call to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes. You are right. I will do the code change. |
||
} | ||
|
||
/* Initialize server context */ | ||
|
@@ -722,7 +775,7 @@ WF_OPENSSL(jobjectArray, getCiphers)(JNIEnv *e, jobject o, jlong ssl) | |
return NULL; | ||
} | ||
|
||
/* Create the byte[][] array that holds all the certs */ | ||
/* Create the byte[][] array that holds all the certs */ | ||
array = (*e)->NewObjectArray(e, len, stringClass, NULL); | ||
|
||
for (i = 0; i < len; i++) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like we are missing a call to
TCN_FREE_CSTRING(customEngine)
after the changes below.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. You are right. I can move
TCN_FREE_CSTRING(customEngine)
to init_failed.