Skip to content

Commit 9ddeb46

Browse files
author
liuchen
committed
add no-gm cert ca with gm cert ssl connect
1 parent ff68a14 commit 9ddeb46

2 files changed

Lines changed: 24 additions & 5 deletions

File tree

chainsql/src/main/java/com/peersafe/base/client/transport/impl/JavaWebSocketTransportImpl.java

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,11 @@
1515
import java.security.cert.Certificate;
1616
import java.util.Arrays;
1717
import java.io.IOException;
18+
import java.io.InputStreamReader;
19+
1820
import org.bouncycastle.openssl.*;
1921
import org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter;
22+
import org.bouncycastle.util.io.pem.PemReader;
2023
import org.bouncycastle.asn1.pkcs.PrivateKeyInfo;
2124
import org.java_websocket.client.WebSocketClient;
2225
import org.java_websocket.framing.Framedata;
@@ -46,6 +49,7 @@
4649
import com.peersafe.base.client.transport.TransportEventHandler;
4750
import com.peersafe.base.client.transport.WebSocketTransport;
4851
import com.peersafe.base.crypto.X509CryptoSuite;
52+
import com.peersafe.chainsql.util.Util;
4953

5054
class WS extends WebSocketClient {
5155

@@ -214,11 +218,26 @@ public void connectSSL(URI uri, String[] trustCAsPath, String sslKeyPath, String
214218
throw new RuntimeException("must call setEventHandler() before connect(...)");
215219
}
216220

217-
String certSigAlg = ((X509Certificate)readCert(trustCAsPath[0])).getSigAlgName();
218-
if(certSigAlg.equals("SM3withSM2"))
219-
{
220-
isGM = true;
221+
if(trustCAsPath.length != 0) {
222+
String certSigAlg = ((X509Certificate)readCert(trustCAsPath[0])).getSigAlgName();
223+
String certPubKeyAlg = "0608";
224+
if(sslCertPath.length() != 0) {
225+
PemReader pemReader = new PemReader(new InputStreamReader(new FileInputStream(sslCertPath)));
226+
byte[] subPubkeyInfo = org.bouncycastle.asn1.x509.Certificate.getInstance(pemReader.readPemObject()
227+
.getContent()).getSubjectPublicKeyInfo().getEncoded();
228+
pemReader.close();
229+
String subPubkeyAlg = Util.bytesToHex(subPubkeyInfo);
230+
certPubKeyAlg = subPubkeyAlg.length() > 45 ? subPubkeyAlg.substring(26,46) : subPubkeyAlg;
231+
}
232+
if(certSigAlg.equals("SM3withSM2") || certPubKeyAlg.equals("06082A811CCF5501822D"))
233+
{
234+
isGM = true;
235+
}
236+
}
237+
else {
238+
throw new RuntimeException("Must specify at least a trustCA");
221239
}
240+
222241

223242
KeyStore tks;
224243
tks = getKeyStore(trustCAsPath, null);

chainsql/src/main/java/com/peersafe/base/client/transport/impl/WebSocketClientHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ public void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception
208208

209209
@Override
210210
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
211-
// cause.printStackTrace();
211+
cause.printStackTrace();
212212
if (!handshakeFuture.isDone()) {
213213
handshakeFuture.setFailure(cause);
214214
}

0 commit comments

Comments
 (0)