Skip to content

Commit ce923a3

Browse files
authored
load sm context builder from class loader (#914)
* load sm context builder from class loader * fix load cert by relative path failed bug
1 parent b4b0754 commit ce923a3

File tree

4 files changed

+149
-88
lines changed

4 files changed

+149
-88
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ ext {
3535
// integrationTest.mustRunAfter test
3636
allprojects {
3737
group = 'org.fisco-bcos.java-sdk'
38-
version = '2.10.0'
38+
version = '2.10.1-SNAPSHOT'
3939
apply plugin: 'maven'
4040
apply plugin: 'maven-publish'
4141
apply plugin: 'idea'

sdk-core/src/main/java/org/fisco/bcos/sdk/config/model/ConfigProperty.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public static InputStream getConfigInputStream(String configFilePath) throws Con
105105
}
106106
InputStream inputStream = null;
107107
try {
108-
configFilePath = configFilePath.replace("..", "");
108+
// configFilePath = configFilePath.replace("..", "");
109109
inputStream = new FileInputStream(configFilePath);
110110
if (inputStream != null) {
111111
return inputStream;

sdk-core/src/main/java/org/fisco/bcos/sdk/network/ConnectionManager.java

Lines changed: 5 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,11 @@
2828
import io.netty.channel.socket.SocketChannel;
2929
import io.netty.channel.socket.nio.NioSocketChannel;
3030
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
31-
import io.netty.handler.ssl.SMSslClientContextFactory;
3231
import io.netty.handler.ssl.SslContext;
33-
import io.netty.handler.ssl.SslContextBuilder;
3432
import io.netty.handler.ssl.SslHandler;
35-
import io.netty.handler.ssl.SslProvider;
3633
import io.netty.handler.timeout.IdleStateHandler;
3734
import io.netty.util.concurrent.Future;
38-
import java.io.IOException;
3935
import java.nio.channels.ClosedChannelException;
40-
import java.security.Security;
4136
import java.util.ArrayList;
4237
import java.util.List;
4338
import java.util.Map;
@@ -70,6 +65,9 @@ public class ConnectionManager {
7065
private EventLoopGroup workerGroup;
7166
private Boolean running = false;
7267
private Bootstrap bootstrap = new Bootstrap();
68+
69+
private SslContextInitializer sslContextInitializer = new SslContextInitializer();
70+
7371
private List<ChannelFuture> connChannelFuture = new ArrayList<ChannelFuture>();
7472
private ScheduledExecutorService reconnSchedule = new ScheduledThreadPoolExecutor(1);
7573
private int cryptoType;
@@ -228,85 +226,6 @@ public ChannelHandlerContext getConnectionCtx(String peer) {
228226
return availableConnections.get(peer);
229227
}
230228

231-
private SslContext initSslContext(ConfigOption configOption) throws NetworkException {
232-
try {
233-
Security.setProperty("jdk.disabled.namedCurves", "");
234-
System.setProperty("jdk.sunec.disableNative", "false");
235-
236-
// Get file, file existence is already checked when check config file.
237-
// Init SslContext
238-
logger.info(" build ECDSA ssl context with configured certificates ");
239-
SslContext sslCtx =
240-
SslContextBuilder.forClient()
241-
.trustManager(configOption.getCryptoMaterialConfig().getCaInputStream())
242-
.keyManager(
243-
configOption.getCryptoMaterialConfig().getSdkCertInputStream(),
244-
configOption
245-
.getCryptoMaterialConfig()
246-
.getSdkPrivateKeyInputStream())
247-
.sslProvider(SslProvider.OPENSSL)
248-
// .sslProvider(SslProvider.JDK)
249-
.build();
250-
return sslCtx;
251-
} catch (IOException e) {
252-
logger.error(
253-
"initSslContext failed, caCert: {}, sslCert: {}, sslKey: {}, error: {}, e: {}",
254-
configOption.getCryptoMaterialConfig().getCaCertPath(),
255-
configOption.getCryptoMaterialConfig().getSdkCertPath(),
256-
configOption.getCryptoMaterialConfig().getSdkPrivateKeyPath(),
257-
e.getMessage(),
258-
e);
259-
throw new NetworkException(
260-
"SSL context init failed, please make sure your cert and key files are properly configured. error info: "
261-
+ e.getMessage(),
262-
NetworkException.INIT_CONTEXT_FAILED);
263-
} catch (IllegalArgumentException e) {
264-
logger.error("initSslContext failed, error: {}, e: {}", e.getMessage(), e);
265-
throw new NetworkException(
266-
"SSL context init failed, error info: " + e.getMessage(),
267-
NetworkException.INIT_CONTEXT_FAILED);
268-
}
269-
}
270-
271-
private SslContext initSMSslContext(ConfigOption configOption) throws NetworkException {
272-
try {
273-
// Get file, file existence is already checked when check config file.
274-
// Init SslContext
275-
return SMSslClientContextFactory.build(
276-
configOption.getCryptoMaterialConfig().getCaInputStream(),
277-
configOption.getCryptoMaterialConfig().getEnSSLCertInputStream(),
278-
configOption.getCryptoMaterialConfig().getEnSSLPrivateKeyInputStream(),
279-
configOption.getCryptoMaterialConfig().getSdkCertInputStream(),
280-
configOption.getCryptoMaterialConfig().getSdkPrivateKeyInputStream());
281-
} catch (Exception e) {
282-
if (configOption.getCryptoMaterialConfig().getCryptoProvider().equalsIgnoreCase(HSM)) {
283-
logger.error(
284-
"initSMSslContext failed, caCert:{}, sslCert: {}, sslKeyIndex: {}, enCert: {}, enSslKeyIndex: {}, error: {}, e: {}",
285-
configOption.getCryptoMaterialConfig().getCaCertPath(),
286-
configOption.getCryptoMaterialConfig().getSdkCertPath(),
287-
configOption.getCryptoMaterialConfig().getSslKeyIndex(),
288-
configOption.getCryptoMaterialConfig().getEnSSLCertPath(),
289-
configOption.getCryptoMaterialConfig().getEnSslKeyIndex(),
290-
e.getMessage(),
291-
e);
292-
} else {
293-
logger.error(
294-
"initSMSslContext failed, caCert:{}, sslCert: {}, sslKey: {}, enCert: {}, enSslKey: {}, error: {}, e: {}",
295-
configOption.getCryptoMaterialConfig().getCaCertPath(),
296-
configOption.getCryptoMaterialConfig().getSdkCertPath(),
297-
configOption.getCryptoMaterialConfig().getSdkPrivateKeyPath(),
298-
configOption.getCryptoMaterialConfig().getEnSSLCertPath(),
299-
configOption.getCryptoMaterialConfig().getEnSSLPrivateKeyPath(),
300-
e.getMessage(),
301-
e);
302-
}
303-
throw new NetworkException(
304-
"SSL context init failed, please make sure your cert and key files are properly configured. error info: "
305-
+ e.getMessage(),
306-
e);
307-
}
308-
}
309-
310229
private void initNetty(ConfigOption configOption) throws NetworkException {
311230
workerGroup = new NioEventLoopGroup();
312231
bootstrap.group(workerGroup);
@@ -324,8 +243,8 @@ private void initNetty(ConfigOption configOption) throws NetworkException {
324243
}
325244
sslContext =
326245
(sslCryptoType == CryptoType.ECDSA_TYPE
327-
? initSslContext(configOption)
328-
: initSMSslContext(configOption));
246+
? sslContextInitializer.initSslContext(configOption)
247+
: sslContextInitializer.initSMSslContext(configOption));
329248
SslContext finalSslContext = sslContext;
330249
ChannelInitializer<SocketChannel> initializer =
331250
new ChannelInitializer<SocketChannel>() {
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
package org.fisco.bcos.sdk.network;
2+
3+
import static org.fisco.bcos.sdk.model.CryptoProviderType.HSM;
4+
5+
import io.netty.handler.ssl.SslContext;
6+
import io.netty.handler.ssl.SslContextBuilder;
7+
import io.netty.handler.ssl.SslProvider;
8+
import java.io.IOException;
9+
import java.io.InputStream;
10+
import java.lang.reflect.Method;
11+
import java.security.Security;
12+
import org.fisco.bcos.sdk.config.ConfigOption;
13+
import org.slf4j.Logger;
14+
import org.slf4j.LoggerFactory;
15+
16+
public class SslContextInitializer {
17+
18+
private static Logger logger = LoggerFactory.getLogger(SslContextInitializer.class);
19+
20+
private static boolean enableNettyOpenSSLProvider = false;
21+
22+
static {
23+
String property = System.getProperty("fisco.netty.enable.openssl.provider");
24+
if (property != null) {
25+
enableNettyOpenSSLProvider = Boolean.valueOf(property);
26+
logger.info("load `fisco.netty.enable.openssl.provider` value: {}", property);
27+
}
28+
}
29+
30+
public SslContext initSslContext(ConfigOption configOption) throws NetworkException {
31+
try {
32+
Security.setProperty("jdk.disabled.namedCurves", "");
33+
System.setProperty("jdk.sunec.disableNative", "false");
34+
35+
// Get file, file existence is already checked when check config file.
36+
// Init SslContext
37+
logger.info(" build ECDSA ssl context with configured certificates ");
38+
39+
SslProvider sslProvider = SslProvider.JDK;
40+
if (enableNettyOpenSSLProvider) {
41+
sslProvider = SslProvider.OPENSSL;
42+
}
43+
44+
logger.info("sslProvider: {}", sslProvider);
45+
46+
SslContext sslCtx =
47+
SslContextBuilder.forClient()
48+
.trustManager(configOption.getCryptoMaterialConfig().getCaInputStream())
49+
.keyManager(
50+
configOption.getCryptoMaterialConfig().getSdkCertInputStream(),
51+
configOption
52+
.getCryptoMaterialConfig()
53+
.getSdkPrivateKeyInputStream())
54+
// .sslProvider(SslProvider.OPENSSL)
55+
.sslProvider(sslProvider)
56+
.build();
57+
return sslCtx;
58+
} catch (IOException e) {
59+
logger.error(
60+
"initSslContext failed, caCert: {}, sslCert: {}, sslKey: {}, error: {}, e: {}",
61+
configOption.getCryptoMaterialConfig().getCaCertPath(),
62+
configOption.getCryptoMaterialConfig().getSdkCertPath(),
63+
configOption.getCryptoMaterialConfig().getSdkPrivateKeyPath(),
64+
e.getMessage(),
65+
e);
66+
throw new NetworkException(
67+
"SSL context init failed, please make sure your cert and key files are properly configured. error info: "
68+
+ e.getMessage(),
69+
NetworkException.INIT_CONTEXT_FAILED);
70+
} catch (IllegalArgumentException e) {
71+
logger.error("initSslContext failed, error: {}, e: {}", e.getMessage(), e);
72+
throw new NetworkException(
73+
"SSL context init failed, error info: " + e.getMessage(),
74+
NetworkException.INIT_CONTEXT_FAILED);
75+
}
76+
}
77+
78+
public SslContext initSMSslContext(ConfigOption configOption) throws NetworkException {
79+
try {
80+
// Get file, file existence is already checked when check config file.
81+
InputStream caInputStream = configOption.getCryptoMaterialConfig().getCaInputStream();
82+
InputStream enSSLCertInputStream =
83+
configOption.getCryptoMaterialConfig().getEnSSLCertInputStream();
84+
InputStream enSSLPrivateKeyInputStream =
85+
configOption.getCryptoMaterialConfig().getEnSSLPrivateKeyInputStream();
86+
InputStream sdkCertInputStream =
87+
configOption.getCryptoMaterialConfig().getSdkCertInputStream();
88+
InputStream sdkPrivateKeyInputStream =
89+
configOption.getCryptoMaterialConfig().getSdkPrivateKeyInputStream();
90+
91+
String smContextFactoryClassName = "io.netty.handler.ssl.SMSslClientContextFactory";
92+
93+
Class<?> smContextFactoryClass = Class.forName(smContextFactoryClassName);
94+
logger.info("加载类`{}`成功", smContextFactoryClassName);
95+
Method buildMethod =
96+
smContextFactoryClass.getMethod(
97+
"build",
98+
InputStream.class,
99+
InputStream.class,
100+
InputStream.class,
101+
InputStream.class,
102+
InputStream.class);
103+
SslContext sslContext =
104+
(SslContext)
105+
buildMethod.invoke(
106+
null,
107+
caInputStream,
108+
enSSLCertInputStream,
109+
enSSLPrivateKeyInputStream,
110+
sdkCertInputStream,
111+
sdkPrivateKeyInputStream);
112+
113+
return sslContext;
114+
} catch (Exception e) {
115+
if (configOption.getCryptoMaterialConfig().getCryptoProvider().equalsIgnoreCase(HSM)) {
116+
logger.error(
117+
"initSMSslContext failed, caCert:{}, sslCert: {}, sslKeyIndex: {}, enCert: {}, enSslKeyIndex: {}, error: {}, e: {}",
118+
configOption.getCryptoMaterialConfig().getCaCertPath(),
119+
configOption.getCryptoMaterialConfig().getSdkCertPath(),
120+
configOption.getCryptoMaterialConfig().getSslKeyIndex(),
121+
configOption.getCryptoMaterialConfig().getEnSSLCertPath(),
122+
configOption.getCryptoMaterialConfig().getEnSslKeyIndex(),
123+
e.getMessage(),
124+
e);
125+
} else {
126+
logger.error(
127+
"initSMSslContext failed, caCert:{}, sslCert: {}, sslKey: {}, enCert: {}, enSslKey: {}, error: {}, e: {}",
128+
configOption.getCryptoMaterialConfig().getCaCertPath(),
129+
configOption.getCryptoMaterialConfig().getSdkCertPath(),
130+
configOption.getCryptoMaterialConfig().getSdkPrivateKeyPath(),
131+
configOption.getCryptoMaterialConfig().getEnSSLCertPath(),
132+
configOption.getCryptoMaterialConfig().getEnSSLPrivateKeyPath(),
133+
e.getMessage(),
134+
e);
135+
}
136+
throw new NetworkException(
137+
"SSL context init failed, please make sure your cert and key files are properly configured. error info: "
138+
+ e.getMessage(),
139+
e);
140+
}
141+
}
142+
}

0 commit comments

Comments
 (0)