question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

API to add constraints on SSL for key size

See original GitHub issue

Is your feature request related to a problem? Please describe.

I have a gRPC server which is using mutual TLS and I want to reject connections from clients who have a public key size less than 2048 bits.

Describe the solution you’d like

There should be an API in the SslContextBuilder class to set SSL key size related constraints

Describe alternatives you’ve considered

I was able to achieve the same using a ServerInterceptor by doing something like this

public class SSLInterceptor implements ServerInterceptor {
    @Override
    public <ReqT, RespT> Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call, Metadata headers, ServerCallHandler<ReqT, RespT> next) {
        try {
            SSLSession sslSession = call.getAttributes().get(Grpc.TRANSPORT_ATTR_SSL_SESSION);
            RSAPublicKeyImpl pk = (RSAPublicKeyImpl) sslSession.getPeerCertificates()[0].getPublicKey();
            if (pk.getModulus().bitLength() < 2048) {
                // reject call
            }
            // proceed with the call
        } catch (SSLPeerUnverifiedException e) {
            // do something
        }
        ...
    }
}

This is a bad way to do it because

  1. The validation is being done after the connection is already established.
  2. The validation is only triggered when a request/call is made.
  3. Every single call involves an extra overhead of validation.
  4. In case of a validation failure, only the call is rejected but not the connection to the client.

In an ideal scenario

  1. The validation is done during the connection establishment phase. (or some point during the creation of the channel between client and server)
  2. The validation failure would prevent the connection from being created and not set it up and disconnect later.
  3. A client is only validated once per session and all the calls made during that session do not incur any overhead.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
ejona86commented, Oct 2, 2019

@adityavikasd, I’d suggest making your own TrustManagerFactory. It seems you could extend TrustManagerFactory and override all the methods, delegating to another instance, except for getTrustManagers() in which you would write custom logic to return your trust manager instead of the default one.

The other potential option is to implement TrustManagerFactorySpi. You will still need to delegate to a real instance, so maybe create one with something like (TrustManagerFactorySpi) Security.getProvider("JSSE").getService("TrustManagerFactory", TrustManagerFactory.getDefaultAlgorithm()).newInstance(null). I’d probably start with providing the “real” Provider to the TrustManagerFactorySpi.

I’ve never done any of that before, but they seem like avenues try. I realize that the Java API is quite opaque, but it’s also all we have.

0reactions
adityavikasdcommented, Oct 4, 2019

Thanks for the help. I was able to implement this. By creating my own TrustManagerFactorySp, TrustManagerFactorySpi and TrustManager.

The Java security API seem so twisted though!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Certificates does not conform to algorithm constraints ...
Solution. Background. The CA API Gateway ensures the security of message traffic by requiring that protected services using SSL or TLS?are ...
Read more >
Java Algorithm constraints check failed on key RSA with size ...
When my java applications call it, they fail because their policy is set to only accept RSA < 2048 keySize. I contacted AWS...
Read more >
So you're making an RSA key for an HTTPS certificate. What ...
So you're about to make an RSA key for an SSL certificate. What key size should you use? OpenSSL now use a 2048...
Read more >
Getting certificates ready in AWS Certificate Manager
As such, the requirements and constraints of a custom domain name SSL/TLS certificate are dictated by CloudFront. For example, the maximum size of...
Read more >
AlgorithmConstraints (Java Platform SE 7 ) - Oracle Help Center
This interface specifies constraints for cryptographic algorithms, keys (key sizes), and other algorithm parameters. AlgorithmConstraints objects are ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found