1919
2020package com .webank .weid .service .v2 ;
2121
22- import java .io .InputStream ;
2322import java .util .ArrayList ;
2423import java .util .List ;
2524import java .util .Objects ;
3029import org .fisco .bcos .channel .handler .ChannelConnections ;
3130import org .fisco .bcos .channel .handler .ChannelHandler ;
3231import org .fisco .bcos .channel .handler .ConnectionInfo ;
32+ import org .fisco .bcos .web3j .crypto .EncryptType ;
3333import org .fisco .bcos .web3j .tuples .generated .Tuple3 ;
3434import org .slf4j .Logger ;
3535import org .slf4j .LoggerFactory ;
3636import org .springframework .core .io .Resource ;
37+ import org .springframework .core .io .support .PathMatchingResourcePatternResolver ;
3738import org .springframework .scheduling .concurrent .ThreadPoolTaskExecutor ;
3839
3940import io .netty .bootstrap .Bootstrap ;
5657public class Channel2Connections extends ChannelConnections {
5758
5859 private static Logger logger = LoggerFactory .getLogger (ChannelConnections .class );
59-
60+ /** SSL connection default configuration */
61+ private static final String CA_CERT = "classpath:ca.crt" ;
62+
63+ private static final String SSL_CERT = "classpath:node.crt" ;
64+ private static final String SSL_KEY = "classpath:node.key" ;
6065 private long idleTimeout = (long ) 10000 ;
6166 private long connectTimeout = (long ) 10000 ;
6267 private long sslHandShakeTimeout = (long ) 10000 ;
@@ -67,7 +72,7 @@ public class Channel2Connections extends ChannelConnections {
6772
6873 private boolean running = false ;
6974
70- public void startConnect () throws SSLException {
75+ public void startConnect () throws Exception {
7176 if (running ) {
7277 logger .debug ("running" );
7378 return ;
@@ -86,7 +91,10 @@ public void startConnect() throws SSLException {
8691 final ChannelConnections selfService = this ;
8792 final ThreadPoolTaskExecutor selfThreadPool = super .getThreadPool ();
8893
89- SslContext sslCtx = initSslContextForConnect ();
94+ SslContext sslContext =
95+ (EncryptType .encryptType == EncryptType .ECDSA_TYPE )
96+ ? initSslContext ()
97+ : initSMSslContext ();
9098 logger .debug (" connect sslcontext init success" );
9199
92100 bootstrap .handler (
@@ -100,7 +108,7 @@ public void initChannel(SocketChannel ch) throws Exception {
100108 handler .setConnections (selfService );
101109 handler .setThreadPool (selfThreadPool );
102110
103- SslHandler sslHandler = sslCtx .newHandler (ch .alloc ());
111+ SslHandler sslHandler = sslContext .newHandler (ch .alloc ());
104112 /** set ssl handshake timeout */
105113 sslHandler .setHandshakeTimeoutMillis (sslHandShakeTimeout );
106114
@@ -184,27 +192,80 @@ public void initChannel(SocketChannel ch) throws Exception {
184192 logger .debug (" start connect end. " );
185193 }
186194
187- private SslContext initSslContextForConnect () throws SSLException {
195+ private SslContext initSslContext () throws SSLException {
188196 SslContext sslCtx ;
189197 try {
198+
199+ if (!isEnableOpenSSL ()) {
200+ System .setProperty ("jdk.tls.namedGroups" , "secp256k1" );
201+ logger .info ("set jdk.tls.namedGroups option" );
202+ }
203+
204+ PathMatchingResourcePatternResolver resolver =
205+ new PathMatchingResourcePatternResolver ();
206+
207+ // check ssl cert file
190208 Resource caResource = getCaCert ();
191- InputStream caInputStream = caResource .getInputStream ();
192209 Resource keystorecaResource = getSslCert ();
193210 Resource keystorekeyResource = getSslKey ();
194211
212+ // check if ca.crt exist
213+ if (Objects .isNull (caResource ) || !caResource .exists ()) {
214+ Resource resource = resolver .getResource (CA_CERT );
215+ if (Objects .nonNull (resource ) && resource .exists ()) {
216+ caResource = resource ;
217+ } else {
218+ throw new RuntimeException (
219+ (Objects .nonNull (caResource ) ? "ca.crt" : caResource .getFilename ())
220+ + " not exist " );
221+ }
222+ }
223+
224+ // check if sdk.crt exist, if not , check the default value node.crt
225+ if (Objects .isNull (keystorecaResource ) || !keystorecaResource .exists ()) {
226+ Resource resource = resolver .getResource (SSL_CERT );
227+ if (Objects .nonNull (resource ) && resource .exists ()) {
228+ keystorecaResource = resource ;
229+ } else {
230+ throw new RuntimeException (
231+ (Objects .nonNull (keystorecaResource )
232+ ? "sdk.crt"
233+ : keystorecaResource .getFilename ())
234+ + " not exist " );
235+ }
236+ }
237+
238+ // check if sdk.key exist, if not, check the default value sdk.key
239+ if (Objects .isNull (keystorekeyResource ) || !keystorekeyResource .exists ()) {
240+ Resource resource = resolver .getResource (SSL_KEY );
241+ if (Objects .nonNull (resource ) && resource .exists ()) {
242+ keystorekeyResource = resource ;
243+ } else {
244+ throw new RuntimeException (
245+ (Objects .nonNull (keystorekeyResource )
246+ ? "sdk.key"
247+ : keystorekeyResource .getFilename ())
248+ + " not exist " );
249+ }
250+ }
251+
252+ logger .info (
253+ " ca certificate: {}, sdk certificate: {}, sdk key: {}, enableOpenSsl: {}" ,
254+ caResource .getFilename (),
255+ keystorecaResource .getFilename (),
256+ keystorekeyResource .getFilename (),
257+ isEnableOpenSSL ());
258+
195259 sslCtx =
196260 SslContextBuilder .forClient ()
197- .trustManager (caInputStream )
261+ .trustManager (caResource . getInputStream () )
198262 .keyManager (
199263 keystorecaResource .getInputStream (),
200264 keystorekeyResource .getInputStream ())
201- .sslProvider (SslProvider .OPENSSL )
265+ .sslProvider (isEnableOpenSSL () ? SslProvider .OPENSSL : SslProvider . JDK )
202266 .build ();
203267 } catch (Exception e ) {
204- logger .error (
205- " Failed to initialize the SSLContext, error mesage: {}, error: {} " ,
206- e .getMessage (),
207- e .getCause ());
268+ logger .error (" Failed to initialize the SSLContext, e: {} " , e .getCause ());
208269 throw new SSLException (" Failed to initialize the SSLContext: " + e .getMessage ());
209270 }
210271 return sslCtx ;
0 commit comments