15
15
16
16
package org .fisco .bcos .sdk .network ;
17
17
18
+ import static org .fisco .bcos .sdk .model .CryptoProviderType .HSM ;
19
+
18
20
import io .netty .bootstrap .Bootstrap ;
19
21
import io .netty .channel .Channel ;
20
22
import io .netty .channel .ChannelFuture ;
34
36
import io .netty .handler .timeout .IdleStateHandler ;
35
37
import io .netty .util .concurrent .Future ;
36
38
import java .io .IOException ;
37
- import java .security .NoSuchAlgorithmException ;
38
- import java .security .NoSuchProviderException ;
39
39
import java .security .Security ;
40
- import java .security .cert .CertificateException ;
41
- import java .security .spec .InvalidKeySpecException ;
42
40
import java .util .ArrayList ;
43
41
import java .util .List ;
44
42
import java .util .Map ;
@@ -96,8 +94,13 @@ public void startConnect(ConfigOption configOption) throws NetworkException {
96
94
}
97
95
logger .debug (" start connect. " );
98
96
/** init netty * */
99
- initNetty (configOption );
100
- running = true ;
97
+ try {
98
+ initNetty (configOption );
99
+ running = true ;
100
+ } catch (Exception e ) {
101
+ logger .debug ("init failed " + e .getMessage ());
102
+ throw new NetworkException ("init failed " + e .getMessage ());
103
+ }
101
104
102
105
/** try connection */
103
106
for (ConnectionInfo connect : connectionInfoList ) {
@@ -263,27 +266,34 @@ private SslContext initSMSslContext(ConfigOption configOption) throws NetworkExc
263
266
try {
264
267
// Get file, file existence is already checked when check config file.
265
268
// Init SslContext
266
- logger .info (" build SM ssl context with configured certificates " );
267
269
return SMSslClientContextFactory .build (
268
270
configOption .getCryptoMaterialConfig ().getCaInputStream (),
269
271
configOption .getCryptoMaterialConfig ().getEnSSLCertInputStream (),
270
272
configOption .getCryptoMaterialConfig ().getEnSSLPrivateKeyInputStream (),
271
273
configOption .getCryptoMaterialConfig ().getSdkCertInputStream (),
272
274
configOption .getCryptoMaterialConfig ().getSdkPrivateKeyInputStream ());
273
- } catch (IOException
274
- | CertificateException
275
- | NoSuchAlgorithmException
276
- | InvalidKeySpecException
277
- | NoSuchProviderException e ) {
278
- logger .error (
279
- "initSMSslContext failed, caCert:{}, sslCert: {}, sslKey: {}, enCert: {}, enKey: {}, error: {}, e: {}" ,
280
- configOption .getCryptoMaterialConfig ().getCaCertPath (),
281
- configOption .getCryptoMaterialConfig ().getSdkCertPath (),
282
- configOption .getCryptoMaterialConfig ().getSdkPrivateKeyPath (),
283
- configOption .getCryptoMaterialConfig ().getEnSSLCertPath (),
284
- configOption .getCryptoMaterialConfig ().getEnSSLPrivateKeyPath (),
285
- e .getMessage (),
286
- e );
275
+ } catch (Exception e ) {
276
+ if (configOption .getCryptoMaterialConfig ().getCryptoProvider ().equalsIgnoreCase (HSM )) {
277
+ logger .error (
278
+ "initSMSslContext failed, caCert:{}, sslCert: {}, sslKeyIndex: {}, enCert: {}, enSslKeyIndex: {}, error: {}, e: {}" ,
279
+ configOption .getCryptoMaterialConfig ().getCaCertPath (),
280
+ configOption .getCryptoMaterialConfig ().getSdkCertPath (),
281
+ configOption .getCryptoMaterialConfig ().getSslKeyIndex (),
282
+ configOption .getCryptoMaterialConfig ().getEnSSLCertPath (),
283
+ configOption .getCryptoMaterialConfig ().getEnSslKeyIndex (),
284
+ e .getMessage (),
285
+ e );
286
+ } else {
287
+ logger .error (
288
+ "initSMSslContext failed, caCert:{}, sslCert: {}, sslKey: {}, enCert: {}, enSslKey: {}, error: {}, e: {}" ,
289
+ configOption .getCryptoMaterialConfig ().getCaCertPath (),
290
+ configOption .getCryptoMaterialConfig ().getSdkCertPath (),
291
+ configOption .getCryptoMaterialConfig ().getSdkPrivateKeyPath (),
292
+ configOption .getCryptoMaterialConfig ().getEnSSLCertPath (),
293
+ configOption .getCryptoMaterialConfig ().getEnSSLPrivateKeyPath (),
294
+ e .getMessage (),
295
+ e );
296
+ }
287
297
throw new NetworkException (
288
298
"SSL context init failed, please make sure your cert and key files are properly configured. error info: "
289
299
+ e .getMessage (),
@@ -299,7 +309,14 @@ private void initNetty(ConfigOption configOption) throws NetworkException {
299
309
// set connection timeout
300
310
bootstrap .option (ChannelOption .CONNECT_TIMEOUT_MILLIS , (int ) TimeoutConfig .connectTimeout );
301
311
int sslCryptoType = configOption .getCryptoMaterialConfig ().getSslCryptoType ();
302
- SslContext sslContext =
312
+ SslContext sslContext ;
313
+ if (configOption .getCryptoMaterialConfig ().getCryptoProvider () != null
314
+ && configOption .getCryptoMaterialConfig ().getCryptoProvider ().equalsIgnoreCase (HSM )
315
+ && sslCryptoType == CryptoType .ECDSA_TYPE ) {
316
+ throw new NetworkException (
317
+ "NON-SM not support hardware secure module yet, please do not config cryptoMatirial.cryptoProvider = hsm." );
318
+ }
319
+ sslContext =
303
320
(sslCryptoType == CryptoType .ECDSA_TYPE
304
321
? initSslContext (configOption )
305
322
: initSMSslContext (configOption ));
0 commit comments