9
9
import java .security .KeyStore ;
10
10
import java .security .KeyStoreException ;
11
11
import java .security .NoSuchAlgorithmException ;
12
+ import java .security .PrivateKey ;
12
13
import java .security .SecureRandom ;
13
14
import java .security .UnrecoverableKeyException ;
14
15
import java .security .cert .Certificate ;
32
33
import com .clickhouse .data .ClickHouseUtils ;
33
34
34
35
public class ClickHouseDefaultSslContextProvider implements ClickHouseSslContextProvider {
35
- static final String PEM_BEGIN_PART1 = "---BEGIN " ;
36
- static final String PEM_BEGIN_PART2 = " PRIVATE KEY---" ;
36
+ static final String PEM_HEADER_PREFIX = "---BEGIN " ;
37
+ static final String PEM_HEADER_SUFFIX = " PRIVATE KEY---" ;
38
+ static final String PEM_FOOTER_PREFIX = "---END " ;
37
39
38
40
/**
39
41
* An insecure {@link javax.net.ssl.TrustManager}, that don't validate the
@@ -58,10 +60,41 @@ public void checkServerTrusted(X509Certificate[] certs, String authType) {
58
60
}
59
61
}
60
62
61
- protected KeyStore getKeyStore (String cert , String key )
62
- throws NoSuchAlgorithmException , InvalidKeySpecException , IOException , CertificateException ,
63
- KeyStoreException {
64
- KeyStore ks ;
63
+ static String getAlgorithm (String header , String defaultAlg ) {
64
+ int startIndex = header .indexOf (PEM_HEADER_PREFIX );
65
+ int endIndex = startIndex < 0 ? startIndex
66
+ : header .indexOf (PEM_HEADER_SUFFIX , (startIndex += PEM_HEADER_PREFIX .length ()));
67
+ return startIndex < endIndex ? header .substring (startIndex , endIndex ) : defaultAlg ;
68
+ }
69
+
70
+ static PrivateKey getPrivateKey (String keyFile )
71
+ throws NoSuchAlgorithmException , InvalidKeySpecException , IOException {
72
+ String algorithm = (String ) ClickHouseDefaults .SSL_KEY_ALGORITHM .getEffectiveDefaultValue ();
73
+ StringBuilder builder = new StringBuilder ();
74
+ try (BufferedReader reader = new BufferedReader (
75
+ new InputStreamReader (ClickHouseUtils .getFileInputStream (keyFile )))) {
76
+ String line = reader .readLine ();
77
+ if (line != null ) {
78
+ algorithm = getAlgorithm (line , algorithm );
79
+
80
+ while ((line = reader .readLine ()) != null ) {
81
+ if (line .indexOf (PEM_FOOTER_PREFIX ) >= 0 ) {
82
+ break ;
83
+ }
84
+
85
+ builder .append (line );
86
+ }
87
+ }
88
+ }
89
+ byte [] encoded = Base64 .getDecoder ().decode (builder .toString ());
90
+ KeyFactory kf = KeyFactory .getInstance (algorithm );
91
+ PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec (encoded );
92
+ return kf .generatePrivate (keySpec );
93
+ }
94
+
95
+ protected KeyStore getKeyStore (String cert , String key ) throws NoSuchAlgorithmException , InvalidKeySpecException ,
96
+ IOException , CertificateException , KeyStoreException {
97
+ final KeyStore ks ;
65
98
try {
66
99
ks = KeyStore .getInstance (KeyStore .getDefaultType ());
67
100
ks .load (null , null ); // needed to initialize the key store
@@ -79,33 +112,8 @@ protected KeyStore getKeyStore(String cert, String key)
79
112
ks .setCertificateEntry ("cert" + (index ++), c );
80
113
}
81
114
} else {
82
- String algorithm = (String ) ClickHouseDefaults .SSL_KEY_ALGORITHM .getEffectiveDefaultValue ();
83
- StringBuilder builder = new StringBuilder ();
84
- try (BufferedReader reader = new BufferedReader (
85
- new InputStreamReader (ClickHouseUtils .getFileInputStream (key )))) {
86
- String str ;
87
- boolean started = false ;
88
- while ((str = reader .readLine ()) != null ) {
89
- if (!started ) {
90
- int startIndex = str .indexOf (PEM_BEGIN_PART1 );
91
- int endIndex = startIndex < 0 ? -1
92
- : str .indexOf (PEM_BEGIN_PART2 , (startIndex += PEM_BEGIN_PART1 .length () - 1 ));
93
- if (startIndex < endIndex ) {
94
- algorithm = str .substring (startIndex , endIndex );
95
- }
96
- started = true ;
97
- } else if (str .indexOf ("---END " ) < 0 ) {
98
- builder .append (str );
99
- } else {
100
- break ;
101
- }
102
- }
103
- }
104
- byte [] encoded = Base64 .getDecoder ().decode (builder .toString ());
105
- KeyFactory kf = KeyFactory .getInstance (algorithm );
106
- PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec (encoded );
107
115
Certificate [] certChain = factory .generateCertificates (in ).toArray (new Certificate [0 ]);
108
- ks .setKeyEntry ("key" , kf . generatePrivate ( keySpec ), null , certChain );
116
+ ks .setKeyEntry ("key" , getPrivateKey ( key ), null , certChain );
109
117
}
110
118
}
111
119
return ks ;
0 commit comments