Skip to content
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

Add GMSSL support #908

Open
wants to merge 25 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
180 changes: 180 additions & 0 deletions core/src/main/java/org/bouncycastle/asn1/gm/SM2Cipher.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
package org.bouncycastle.asn1.gm;

import org.bouncycastle.asn1.*;
import org.bouncycastle.util.BigIntegers;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.math.BigInteger;
import java.util.Enumeration;

/**
* GMT 0009-2012
* <p>
* sm2 encrypted data specific struct
*
*
* @since 2021-03-10 13:28:12
*/
public class SM2Cipher extends ASN1Object
{
/*
* SM2Cipher ::== SEQUENCE{
* XCoordinate INTEGER, --X Portion
* YCoordinate INTEGER, --Y Portion
* HASH OCTET STRING SIZE(32), --Plaintext sm3 hash
* CipherText OCTET STRING --CipherText
* }
*/

private ASN1Integer xCoordinate;
private ASN1Integer yCoordinate;
private ASN1OctetString hash;
private ASN1OctetString cipherText;

public SM2Cipher()
{
super();
}

public SM2Cipher(ASN1Sequence seq)
{
Enumeration<?> e = seq.getObjects();
xCoordinate = ASN1Integer.getInstance(e.nextElement());
yCoordinate = ASN1Integer.getInstance(e.nextElement());
hash = ASN1OctetString.getInstance(e.nextElement());
cipherText = ASN1OctetString.getInstance(e.nextElement());
}

public static SM2Cipher getInstance(Object o)
{
if(o instanceof SM2Cipher)
{
return (SM2Cipher) o;
}
else if(o != null)
{
return new SM2Cipher(ASN1Sequence.getInstance(o));
}
return null;
}

public ASN1Integer getxCoordinate()
{
return xCoordinate;
}

public void setxCoordinate(ASN1Integer xCoordinate)
{
this.xCoordinate = xCoordinate;
}

public ASN1Integer getyCoordinate()
{
return yCoordinate;
}

public void setyCoordinate(ASN1Integer yCoordinate)
{
this.yCoordinate = yCoordinate;
}

public ASN1OctetString getHash()
{
return hash;
}

public void setHash(ASN1OctetString hash)
{
this.hash = hash;
}

public ASN1OctetString getCipherText()
{
return cipherText;
}

public void setCipherText(ASN1OctetString cipherText)
{
this.cipherText = cipherText;
}

public ASN1Primitive toASN1Primitive()
{
ASN1EncodableVector v = new ASN1EncodableVector(4);
v.add(xCoordinate);
v.add(yCoordinate);
v.add(hash);
v.add(cipherText);
return new DERSequence(v);
}

/**
* Convert ASN.1 Struct to C1C3C2 format
*
* @return C1C3C2
* @throws IOException
*/
public byte[] convertC1C3C2() throws IOException
{
/*
* construct GMT0009-2012 encrypted data struct
*/
ByteArrayOutputStream stream = new ByteArrayOutputStream();


final byte[] x = new byte[32];
final byte[] y = new byte[32];

byte[] tmp = BigIntegers.asUnsignedByteArray(getxCoordinate().getValue());
System.arraycopy(tmp, 0, x, 32 - tmp.length, tmp.length);
tmp = BigIntegers.asUnsignedByteArray(getyCoordinate().getValue());
System.arraycopy(tmp, 0, y, 32 - tmp.length, tmp.length);

// C1
// read 1 byte for uncompressed point prefix 0x04
stream.write(0x04);
stream.write(x);
stream.write(y);
// C3
stream.write(getHash().getOctets());
// C2
stream.write(getCipherText().getOctets());
stream.flush();
return stream.toByteArray();
}

/**
* Convert SM2 encrypted result format of c1c3c2 to ASN.1 SM2Cipher
*
* @param c1c3c2 encrypted result
* @return SM2Cipher
* @throws IOException
*/
static public SM2Cipher fromC1C3C2(byte[] c1c3c2) throws IOException
{
/*
* construct GMT0009-2012 encrypted data struct
*/
ByteArrayInputStream stream = new ByteArrayInputStream(c1c3c2);
// read 1 byte for uncompressed point prefix 0x04
stream.read();
final byte[] x = new byte[32];
final byte[] y = new byte[32];
final byte[] hash = new byte[32];
int length = c1c3c2.length - 1 - 32 - 32 - 32;
final byte[] cipherText = new byte[length];
stream.read(x);
stream.read(y);
stream.read(hash);
stream.read(cipherText);

final SM2Cipher sm2Cipher = new SM2Cipher();
sm2Cipher.setxCoordinate(new ASN1Integer(new BigInteger(1, x)));
sm2Cipher.setyCoordinate(new ASN1Integer(new BigInteger(1, y)));
sm2Cipher.setHash(new DEROctetString(hash));
sm2Cipher.setCipherText(new DEROctetString(cipherText));
return sm2Cipher;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class CipherSuiteInfo
{
static CipherSuiteInfo forCipherSuite(int cipherSuite, String name)
{
if (!name.startsWith("TLS_"))
if (!name.startsWith("TLS_") && !name.startsWith("GMSSL_"))
{
throw new IllegalArgumentException();
}
Expand Down Expand Up @@ -81,6 +81,14 @@ boolean isTLSv13()
return isTLSv13;
}

/**
* GMSSL 1.1 crypto suites Start with 0xe0
* @return true - GMSSL suite; false - not
*/
boolean isGMSSLv11(){
return ((cipherSuite >> 8) & 0xFF) == 0xe0;
}

private static void addAll(Set<String> decomposition, String... entries)
{
for (String entry : entries)
Expand Down Expand Up @@ -150,6 +158,9 @@ private static void decomposeEncryptionAlgorithm(Set<String> decomposition, int
case EncryptionAlgorithm.CHACHA20_POLY1305:
// NOTE: Following SunJSSE, nothing beyond the transformation added above (i.e "ChaCha20-Poly1305")
break;
case EncryptionAlgorithm.SM4_CBC:
decomposition.add("SM4_CBC");
break;
case EncryptionAlgorithm.NULL:
decomposition.add("C_NULL");
break;
Expand All @@ -170,6 +181,9 @@ private static void decomposeHashAlgorithm(Set<String> decomposition, short hash
case HashAlgorithm.sha384:
addAll(decomposition, "SHA384", "SHA-384", "HmacSHA384");
break;
case HashAlgorithm.sm3:
addAll(decomposition, "SM3");
break;
// case HashAlgorithm.sha512:
// addAll(decomposition, "SHA512", "SHA-512", "HmacSHA512");
// break;
Expand Down Expand Up @@ -200,6 +214,9 @@ private static void decomposeKeyExchangeAlgorithm(Set<String> decomposition, int
case KeyExchangeAlgorithm.RSA:
addAll(decomposition, "RSA");
break;
case KeyExchangeAlgorithm.SM2:
addAll(decomposition, "SM2");
break;
default:
throw new IllegalArgumentException();
}
Expand Down Expand Up @@ -227,6 +244,9 @@ private static void decomposeMACAlgorithm(Set<String> decomposition, int cipherT
case MACAlgorithm.hmac_sha384:
addAll(decomposition, "SHA384", "SHA-384", "HmacSHA384");
break;
case MACAlgorithm.hmac_sm3:
addAll(decomposition, "SM3", "HmacSM3");
break;
// case MACAlgorithm.hmac_sha512:
// addAll(decomposition, "SHA512", "SHA-512", "HmacSHA512");
// break;
Expand Down Expand Up @@ -354,6 +374,9 @@ private static short getHashAlgorithm(int cipherSuite)
case CipherSuite.TLS_RSA_WITH_CAMELLIA_256_GCM_SHA384:
return HashAlgorithm.sha384;

case CipherSuite.GMSSL_ECC_SM4_SM3:
return HashAlgorithm.sm3;

default:
throw new IllegalArgumentException();
}
Expand Down Expand Up @@ -392,6 +415,8 @@ private static String getTransformation(int encryptionAlgorithm)
return "ChaCha20-Poly1305";
case EncryptionAlgorithm.NULL:
return "NULL";
case EncryptionAlgorithm.SM4_CBC:
return "SM4/CBC/NoPadding";
default:
throw new IllegalArgumentException();
}
Expand Down
Loading