8000 Add GMSSL support by Trisia · Pull Request #908 · bcgit/bc-java · GitHub
[go: up one dir, main page]

Skip to content

Add GMSSL support #908

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 25 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
42d67ad
Added SM4 block encryption algorithm
Trisia Mar 5, 2021
7eab95d
Added SM3 hash hmac alg support
Trisia Mar 5, 2021
087209a
Add the algorithm identifier related to ECC_SM4_SM3
Trisia Mar 5, 2021
2423ca5
Added GMSSL related algorithm identifier analysis to Spi
Trisia Mar 5, 2021
c42b9cb
fix CipherSuiteInfo prefix check logic error
Trisia Mar 5, 2021
5529e58
debug client hello message
Trisia Mar 9, 2021
3a2c0d4
add sm2 key exchange process
Trisia Mar 10, 2021
f01c27c
add gm client key exchange impl
Trisia Mar 10, 2021
0c7a246
fix big BigInteger need set flag to process byte array signum
Trisia Mar 11, 2021
a26181e
TlsBlockCipher support gmssl struct encrypt and decrypt.
Trisia Mar 11, 2021
99b4d74
Merge pull request #1 from bcgit/master
Trisia Mar 11, 2021
e1ad060
change test site.
Trisia Mar 11, 2021
79e8066
implement server side gmssl SM2_SM4_SM3 suite develop.
Trisia Mar 12, 2021
92ca36c
add server version limit, if server dont have version of protocol the…
Trisia Mar 15, 2021
5dfe15e
change mock GMSSL CLient/Server to SimpleGMSSL public access.
Trisia Mar 16, 2021
45a747f
Add GM Simple Socket Factory.
Trisia Mar 16, 2021
1373192
Manually resolve merge conflicts
Trisia Mar 16, 2021
56d3167
Merge branch 'master' of https://github.com/bcgit/bc-java into bcgit-…
Trisia Mar 16, 2021
cf634a0
Merge branch 'bcgit-master'
Trisia Mar 16, 2021
b90aab1
Fix apache HttpClient get session null throw error.
Trisia Mar 17, 2021
a608f56
remove author tag
Trisia Mar 21, 2021
2c7bdad
修复了Alert 40 错误
Trisia Oct 20, 2021
621e688
格式调整
Trisia Oct 20, 2021
abfb039
Completed the GMSSL session.
Trisia Oct 23, 2021
fb72a85
remove debug info and change certificate parer method.
Trisia Oct 23, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
change mock GMSSL CLient/Server to SimpleGMSSL public access.
  • Loading branch information
Trisia committed Mar 16, 2021
commit 5dfe15eaa3201b5e4038e4643602ad46487d6bee
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.bouncycastle.tls.test;
package org.bouncycastle.jsse.provider.gm;

import org.bouncycastle.tls.*;
import org.bouncycastle.tls.crypto.TlsCrypto;
Expand All @@ -9,12 +9,15 @@
import java.util.Hashtable;

/**
* Mock GMSSL client
* Simple GMSSL client
*
* - make handshake connection
* - no authentication
*
* @author Cliven
* @since 2021-03-09 14:01:50
*/
public class MockGMSSLClient extends AbstractTlsClient
public class SimpleGMSSLClient extends AbstractTlsClient
{
private static final int[] DEFAULT_CIPHER_SUITES = new int[]
{
Expand All @@ -24,12 +27,12 @@ public class MockGMSSLClient extends AbstractTlsClient
CipherSuite.GMSSL_ECC_SM4_SM3,
};

public MockGMSSLClient()
public SimpleGMSSLClient()
{
this(new BcTlsCrypto(new SecureRandom()));
}

public MockGMSSLClient(TlsCrypto crypto)
public SimpleGMSSLClient(TlsCrypto crypto)
{
super(crypto);
}
Expand All @@ -52,12 +55,12 @@ public TlsAuthentication getAuthentication() throws IOException

public void notifyServerCertificate(TlsServerCertificate serverCertificate) throws IOException
{
System.out.println(">> TlsAuthentication on notifyServerCertificate");
// System.out.println(">> TlsAuthentication on notifyServerCertificate");
}

public TlsCredentials getClientCredentials(CertificateRequest certificateRequest) throws IOException
{
System.out.println(">> TlsAuthentication on getClientCredentials");
// System.out.println(">> TlsAuthentication on getClientCredentials");
return null;
}
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package org.bouncycastle.jsse.provider.gm;

import org.bouncycastle.crypto.params.AsymmetricKeyParameter;
import org.bouncycastle.tls.*;
import org.bouncycastle.tls.crypto.TlsCrypto;
import org.bouncycastle.tls.crypto.impl.bc.BcGMSSLCredentials;
import org.bouncycastle.tls.crypto.impl.bc.BcTlsCrypto;
import org.bouncycastle.util.encoders.Hex;

import java.io.IOException;
import java.io.PrintStream;
import java.security.SecureRandom;

/**
* Simple GMSSL Server
*
* @author Cliven
* @since 2021-03-16 09:31:14
*/
public class SimpleGMSSLServer
extends DefaultTlsServer
{
/*
* contain two cert, first for sign, second for encrypt
*/
protected Certificate certList;
protected AsymmetricKeyParameter signKey;
protected AsymmetricKeyParameter encKey;

/**
* Create GMSSL Server Instance
*
* @param crypto crypto
* @param certList contain two cert, first for sign, second for encrypt
* @param signKey sign private key
* @param encKey encrypt private key
*/
public SimpleGMSSLServer(TlsCrypto crypto, Certificate certList, AsymmetricKeyParameter signKey, AsymmetricKeyParameter encKey)
{
super(crypto);
this.certList = certList;
this.signKey = signKey;
this.encKey = encKey;
}

public void notifyAlertRaised(short alertLevel, short alertDescription, String message, Throwable cause)
{
// PrintStream out = (alertLevel == AlertLevel.fatal) ? System.err : System.out;
// out.println("GMSSL server raised alert: " + AlertLevel.getText(alertLevel)
// + ", " + AlertDescription.getText(alertDescription));
// if (message != null)
// {
// out.println("> " + message);
// }
// if (cause != null)
// {
// cause.printStackTrace(out);
// }
}

public void notifyAlertReceived(short alertLevel, short alertDescription)
{
// PrintStream out = (alertLevel == AlertLevel.fatal) ? System.err : System.out;
// out.println("GMSSL server received alert: " + AlertLevel.getText(alertLevel)
// + ", " + AlertDescription.getText(alertDescription));
}

@Override
public void notifySecureRenegotiation(boolean secureRenegotiation) throws IOException
{

}

public ProtocolVersion getServerVersion() throws IOException
{
return ProtocolVersion.GMSSLv11;
}

public CertificateRequest getCertificateRequest() throws IOException
{
return null;
}

public void notifyClientCertificate(Certificate clientCertificate) throws IOException
{

}

public void notifyHandshakeComplete() throws IOException
{

}

@Override
public TlsCredentials getCredentials() throws IOException
{
return new BcGMSSLCredentials((BcTlsCrypto) getCrypto(), certList, signKey, encKey);
}

@Override
public boolean shouldUseGMTUnixTime()
{
return true;
}

protected int[] getSupportedCipherSuites()
{
return new int[]{CipherSuite.GMSSL_ECC_SM4_SM3};
}

protected ProtocolVersion[] getSupportedVersions()
{
return ProtocolVersion.GMSSLv11.only();
}

protected String hex(byte[] data)
{
return data == null ? "(null)" : Hex.toHexString(data);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.jsse.provider.BouncyCastleJsseProvider;
import org.bouncycastle.jsse.provider.gm.SimpleGMSSLClient;
import org.bouncycastle.tls.TlsClientProtocol;
import org.bouncycastle.util.io.Streams;

Expand Down Expand Up @@ -30,16 +31,15 @@ public static void main(String[] args)
Security.addProvider(new BouncyCastleJsseProvider());

String host = "localhost";
int port = 446;
int port = 5557;
// String host = "sm2test.ovssl.cn";
// int port = 443;
// jsse(host, port);
bc(host, port);
}

private static void bc(String host, int port) throws IOException
{
final MockGMSSLClient client = new MockGMSSLClient();
final SimpleGMSSLClient client = new SimpleGMSSLClient();
Socket s = new Socket(host, port);
TlsClientProtocol protocol = new TlsClientProtocol(s.getInputStream(), s.getOutputStream());
protocol.connect(client);
Expand Down
33 changes: 23 additions & 10 deletions tls/src/test/java/org/bouncycastle/tls/test/GMSSLServerTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package org.bouncycastle.tls.test;

import org.bouncycastle.crypto.params.AsymmetricKeyParameter;
import org.bouncycastle.jsse.provider.gm.SimpleGMSSLServer;
import org.bouncycastle.tls.*;
import org.bouncycastle.tls.crypto.TlsCertificate;
import org.bouncycastle.tls.crypto.impl.bc.BcTlsCrypto;
import org.bouncycastle.util.io.Streams;
import org.bouncycastle.util.io.TeeOutputStream;
Expand All @@ -11,18 +14,33 @@

/**
* A simple test designed to conduct a GMSSL handshake with an external GMSSL client.
* <p>
*
* </p>
* @author Cliven
* @since 2021-03-16 09:57:58
*/
public class GMSSLServerTest
{
static BcTlsCrypto crypto;
static AsymmetricKeyParameter signKey;
static AsymmetricKeyParameter encKey;
static Certificate certList;

public static void main(String[] args) throws Exception
{

int port = 5557;

ServerSocket ss = new ServerSocket(port, 16);

crypto = new BcTlsCrypto(new SecureRandom());

certList = new Certificate(new TlsCertificate[]{
TlsTestUtils.loadCertificateResource(crypto, "x509-server-sm2-sign.pem"),
TlsTestUtils.loadCertificateResource(crypto, "x509-server-sm2-enc.pem"),
});

signKey = TlsTestUtils.loadBcPrivateKeyResource("x509-server-key-sm2-sign.pem");
encKey = TlsTestUtils.loadBcPrivateKeyResource("x509-server-key-sm2-enc.pem");

try
{
while (true)
Expand Down Expand Up @@ -50,10 +68,9 @@ static class ServerThread extends Thread

public void run()
{
byte[] buff = new byte[2028];
try
{
MockGMSSLServer server = new MockGMSSLServer();
SimpleGMSSLServer server = new SimpleGMSSLServer(crypto, certList, signKey, encKey);
TlsServerProtocol serverProtocol = new TlsServerProtocol(s.getInputStream(), s.getOutputStream());
serverProtocol.accept(server);

Expand All @@ -73,11 +90,7 @@ public void run()
System.out.println();

OutputStream outputStream = serverProtocol.getOutputStream();
String resp = "HTTP/1.1 200 OK\r\n"
+ "Content-Length: 6\r\n"
+ "Content-Type: text/plain; charset=utf-8\r\n"
+ "\r\n"
+ "hello\n";
String resp = "HTTP/1.1 200 OK\r\n" + "Content-Length: 6\r\n" + "Content-Type: text/plain; charset=utf-8\r\n" + "\r\n" + "hello\n";
outputStream.write(resp.getBytes("UTF-8"));
outputStream.flush();
serverProtocol.close();
Expand Down
112 changes: 0 additions & 112 deletions tls/src/test/java/org/bouncycastle/tls/test/MockGMSSLServer.java

This file was deleted.

0