Skip to content

Commit

Permalink
OpenSSL might crash here when passing null on some platforms
Browse files Browse the repository at this point in the history
Also simplify code, it is best to set MemorySegment.NULL rather than
null and then check again to pass MemorySegment.NULL later.
Port from tomcat-native
  • Loading branch information
rmaucher committed May 17, 2024
1 parent 8ab881a commit c8b460d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -560,24 +560,24 @@ public void init(KeyManager[] kms, TrustManager[] tms, SecureRandom sr) {
} else {
// Client certificate verification based on trusted CA files and dirs
MemorySegment caCertificateFileNative = sslHostConfig.getCaCertificateFile() != null
? localArena.allocateFrom(SSLHostConfig.adjustRelativePath(sslHostConfig.getCaCertificateFile())) : null;
? localArena.allocateFrom(SSLHostConfig.adjustRelativePath(sslHostConfig.getCaCertificateFile())) : MemorySegment.NULL;
MemorySegment caCertificatePathNative = sslHostConfig.getCaCertificatePath() != null
? localArena.allocateFrom(SSLHostConfig.adjustRelativePath(sslHostConfig.getCaCertificatePath())) : null;
? localArena.allocateFrom(SSLHostConfig.adjustRelativePath(sslHostConfig.getCaCertificatePath())) : MemorySegment.NULL;
if ((sslHostConfig.getCaCertificateFile() != null || sslHostConfig.getCaCertificatePath() != null)
&& SSL_CTX_load_verify_locations(state.sslCtx,
caCertificateFileNative == null ? MemorySegment.NULL : caCertificateFileNative,
caCertificatePathNative == null ? MemorySegment.NULL : caCertificatePathNative) <= 0) {
caCertificateFileNative, caCertificatePathNative) <= 0) {
logLastError("openssl.errorConfiguringLocations");
} else {
var caCerts = SSL_CTX_get_client_CA_list(state.sslCtx);
if (MemorySegment.NULL.equals(caCerts)) {
caCerts = SSL_load_client_CA_file(caCertificateFileNative == null ? MemorySegment.NULL : caCertificateFileNative);
caCerts = SSL_load_client_CA_file(caCertificateFileNative);
if (!MemorySegment.NULL.equals(caCerts)) {
SSL_CTX_set_client_CA_list(state.sslCtx, caCerts);
}
} else {
if (SSL_add_file_cert_subjects_to_stack(caCerts,
caCertificateFileNative == null ? MemorySegment.NULL : caCertificateFileNative) <= 0) {
// OpenSSL might crash here when passing null on some platforms
if (MemorySegment.NULL.equals(caCertificateFileNative)
|| (SSL_add_file_cert_subjects_to_stack(caCerts, caCertificateFileNative) <= 0)) {
caCerts = MemorySegment.NULL;
}
}
Expand Down
4 changes: 4 additions & 0 deletions webapps/docs/changelog.xml
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@
Fix OpenSSL FFM use of ERR_error_string with a 128 byte buffer,
and use ERR_error_string_n instead. (remm)
</fix>
<fix>
Fix a crash on Windows setting CA certificate on null path.
(remm)
</fix>
</changelog>
</subsection>
<subsection name="Other">
Expand Down

0 comments on commit c8b460d

Please sign in to comment.