2828import java .security .KeyStore ;
2929import java .security .KeyStoreException ;
3030import java .security .NoSuchAlgorithmException ;
31+ import javax .annotation .Nullable ;
3132import javax .net .ssl .*;
3233
3334public class SimpleSslContextBuilder {
@@ -44,9 +45,9 @@ public class SimpleSslContextBuilder {
4445 // gRPC requires http2 protocol.
4546 ApplicationProtocolNames .HTTP_2 );
4647
47- private final PKCS pkcs ;
48- private final InputStream keyCertChain ;
49- private final InputStream key ;
48+ private final @ Nullable PKCS pkcs ;
49+ private final @ Nullable InputStream keyCertChain ;
50+ private final @ Nullable InputStream key ;
5051 private TrustManager trustManager ;
5152 private boolean useInsecureTrustManager ;
5253 private String keyPassword ;
@@ -66,20 +67,32 @@ public static SimpleSslContextBuilder newBuilder(InputStream keyCertChain, Input
6667 return forPKCS8 (keyCertChain , key );
6768 }
6869
70+ /**
71+ * Explicitly creates a builder without a client private key or certificate chain.
72+ *
73+ * <p>{@link #forPKCS8} and {@link #forPKCS12} support null inputs too for easier configuration
74+ * API
75+ */
76+ public static SimpleSslContextBuilder noKeyOrCertChain () {
77+ return new SimpleSslContextBuilder (null , null , null );
78+ }
79+
6980 /**
7081 * @param keyCertChain - an input stream for an X.509 client certificate chain in PEM format.
7182 * @param key - an input stream for a PKCS#8 client private key in PEM format.
7283 */
73- public static SimpleSslContextBuilder forPKCS8 (InputStream keyCertChain , InputStream key ) {
84+ public static SimpleSslContextBuilder forPKCS8 (
85+ @ Nullable InputStream keyCertChain , @ Nullable InputStream key ) {
7486 return new SimpleSslContextBuilder (PKCS .PKCS_8 , keyCertChain , key );
7587 }
7688
7789 /** @param pfxKeyArchive - an input stream for .pfx or .p12 PKCS12 archive file */
78- public static SimpleSslContextBuilder forPKCS12 (InputStream pfxKeyArchive ) {
90+ public static SimpleSslContextBuilder forPKCS12 (@ Nullable InputStream pfxKeyArchive ) {
7991 return new SimpleSslContextBuilder (PKCS .PKCS_12 , null , pfxKeyArchive );
8092 }
8193
82- private SimpleSslContextBuilder (PKCS pkcs , InputStream keyCertChain , InputStream key ) {
94+ private SimpleSslContextBuilder (
95+ @ Nullable PKCS pkcs , @ Nullable InputStream keyCertChain , @ Nullable InputStream key ) {
8396 this .pkcs = pkcs ;
8497 this .keyCertChain = keyCertChain ;
8598 this .key = key ;
@@ -109,16 +122,18 @@ public SslContext build() throws SSLException {
109122 : getDefaultTrustManager ())
110123 .applicationProtocolConfig (DEFAULT_APPLICATION_PROTOCOL_CONFIG );
111124
112- switch (pkcs ) {
113- case PKCS_8 :
114- // netty by default supports PKCS8
115- sslContextBuilder .keyManager (keyCertChain , key , keyPassword );
116- break ;
117- case PKCS_12 :
118- sslContextBuilder .keyManager (createPKCS12KeyManager ());
119- break ;
120- default :
121- throw new IllegalArgumentException ("PKCS " + pkcs + " is not implemented" );
125+ if (pkcs != null && (key != null || keyCertChain != null )) {
126+ switch (pkcs ) {
127+ case PKCS_8 :
128+ // netty by default supports PKCS8
129+ sslContextBuilder .keyManager (keyCertChain , key , keyPassword );
130+ break ;
131+ case PKCS_12 :
132+ sslContextBuilder .keyManager (createPKCS12KeyManager ());
133+ break ;
134+ default :
135+ throw new IllegalArgumentException ("PKCS " + pkcs + " is not implemented" );
136+ }
122137 }
123138
124139 return sslContextBuilder .build ();
0 commit comments