I know that a particular TLS version can be enforced with a global property, like so:
- with code:
System.setProperty("jdk.tls.client.protocols", "TLSv1.2");
- or from the command line:
java -Djdk.tls.client.protocols=TLSv1.2 ...
Is there a way to do that in a more refined, on SSL per-context/engine bases? For example, what if I want to prevent usage of TLSv1.3 in a particular context, and only allow protocols up to TLSv1.2?
I thought this was the way:
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLEngine;
...
var sslContext = SSLContext.getInstance("TLSv1.2");
var sslEngine = sslContext.createSSLEngine();
...
but my tests still show TLSv1.3 creeping in the SSL handshake phase. Indeed, the Java 16+ java doc explains that the "protocol" parameter guarantees provider with support for the requested TLS version, but does not necessarily restrict the provider from going higher:
SSLContext.getProtocol(String protocol);
javadoc:
protocol – the standard name of the requested protocol. See the SSLContext section in the Java Security Standard Algorithm Names Specification for information about standard protocol names.
P.S: I found something closer to what I need answered here: Will SSLContext.getInstance("TLS") supports TLS v1.1 and TLS v1.2 also?
To use TLSv1.2 try to use below code:
SSLContext sslContext = SSLContext.getInstance("TLSv1.2");
sslContext.init(null, null, null);
Assuming the answer is correct(I think it is not), however I do want to initialize my context from specific trust store and key store managers, passing NULLs is not acceptable to me.