long running TLS 1.2 JDBC connections are throwing NPE from recently added TLS 1.3 support
Long running JDBC connections using Microsoft's SQL Server JDBC driver and using BCTLS-FIPS 1.0.11 are throwing NullPointerExceptions. Here is the stack trace
java.lang.NullPointerException
at org.bouncycastle.tls.crypto.TlsCryptoUtils.hkdfExpandLabel(Unknown Source)
at org.bouncycastle.tls.TlsUtils.update13TrafficSecret(Unknown Source)
at org.bouncycastle.tls.TlsUtils.update13TrafficSecret(Unknown Source)
at org.bouncycastle.tls.TlsUtils.update13TrafficSecretLocal(Unknown Source)
at org.bouncycastle.tls.TlsProtocol.send13KeyUpdate(Unknown Source)
at org.bouncycastle.tls.TlsProtocol.writeApplicationData(Unknown Source)
at org.bouncycastle.jsse.provider.ProvSSLSocketWrap$AppDataOutput.write(Unknown Source)
at com.microsoft.sqlserver.jdbc.TDSChannel.write(IOBuffer.java:2075)
at com.microsoft.sqlserver.jdbc.TDSWriter.flush(IOBuffer.java:4323)
at com.microsoft.sqlserver.jdbc.TDSWriter.writePacket(IOBuffer.java:4225)
at com.microsoft.sqlserver.jdbc.TDSWriter.endMessage(IOBuffer.java:3269)
at com.microsoft.sqlserver.jdbc.TDSCommand.startResponse(IOBuffer.java:7780)
at com.microsoft.sqlserver.jdbc.TDSCommand.startResponse(IOBuffer.java:7768)
at com.microsoft.sqlserver.jdbc.SQLServerConnection$1ConnectionCommand.doExecute(SQLServerConnection.java:3316)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:7418)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:3274)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectionCommand(SQLServerConnection.java:3320)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.setAutoCommit(SQLServerConnection.java:3453)
This NPE is coming from the following line of code in org/bouncycastle/tls/crypto/TlsCryptoUtils.java
public static TlsSecret hkdfExpandLabel(TlsSecret secret, short hashAlgorithm, String label, byte[] context, int length)
throws IOException
...
return secret.hkdfExpand(hashAlgorithm, hkdfLabel, length);
as the function parameter secret is null. Parameter secret appears to be only initialized for TLS 1.3 protocol here in org/bouncycastle/tls/crypto/impl/TlsAEADCipher.java (if I am reading the code correctly)
if (isTLSv13)
{
rekeyCipher(securityParameters, decryptCipher, decryptNonce, !isServer);
rekeyCipher(securityParameters, encryptCipher, encryptNonce, isServer);
return;
}
Debugging a test program, we see that the method TlsProtocol.send13KeyUpdate is wrapped with the following logic:
else if (recordStream.needsKeyUpdate()) // code in needsKeyUpdate is return writeSeqNo.currentValue() >= (1L << 20);
{
send13KeyUpdate(true);
}
this is recently added code in this commit: TLS 1.3: Basic KeyUpdate policy
https://github.com/bcgit/bc-java/commit/98d8383fa7357cf3bbb84c0e5d28d30f0887a31d
During our Debugging sessions, inspecting the object recordStream in the debugger shows member variable:
private ProtocolVersion writeVersion = null;
is set to TLSv12.
Given that send13KeyUpdate was introduced for TLS 1.3 support, it appears additional code change is needed for 1.2 backward compatibility.
Perhaps a code change like the following code change is needed in RecordStream.needsKeyUpdate method.
boolean needsKeyUpdate()
{
return this.writeVersion==ProtocolVersion.TLSv13 && writeSeqNo.currentValue() >= (1L << 20);
}
Thanks for reporting this. I've fixed it in our git repo and a new beta should be up in a few days. I note you are using the bctls-fips jar. It will probably be a few weeks yet before a new version of that is available, but if you hold a support contract with Crypto Workshop and need an expedited fix, please contact us (CW).
Note that the proposed fix couldn't work because that version field is "legacy_record_version" from RFC 8446 and will contain "TLSv12" even when TLS 1.3 is negotiated.
The latest on https://www.bouncycastle.org/betas now includes this fix, it is also present in bctls-fips-1.0.11.4 which is now up on Maven Central.