Skip to content

Commit 617c7e6

Browse files
committed
TLS: Some work on GOST support (RFC 9189)
1 parent 992b1da commit 617c7e6

File tree

19 files changed

+162
-15
lines changed

19 files changed

+162
-15
lines changed

tls/src/main/java/org/bouncycastle/tls/CipherSuite.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@ public static boolean isSCSV(int cipherSuite)
452452
public static final int TLS_SM4_CCM_SM3 = 0x00C7;
453453

454454
/*
455-
* draft-smyshlyaev-tls12-gost-suites-10
455+
* RFC 9189
456456
*/
457457
public static final int TLS_GOSTR341112_256_WITH_KUZNYECHIK_CTR_OMAC = 0xC100;
458458
public static final int TLS_GOSTR341112_256_WITH_MAGMA_CTR_OMAC = 0xC101;

tls/src/main/java/org/bouncycastle/tls/ClientCertificateType.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public class ClientCertificateType
2121
public static final short ecdsa_fixed_ecdh = 66;
2222

2323
/*
24-
* draft-smyshlyaev-tls12-gost-suites-10
24+
* RFC 9189
2525
*/
2626
public static final short gost_sign256 = 67;
2727
public static final short gost_sign512 = 68;

tls/src/main/java/org/bouncycastle/tls/EncryptionAlgorithm.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,11 @@ public class EncryptionAlgorithm
7777
* GMT 0024-2014
7878
*/
7979
public static final int SM4_CBC = 28;
80+
81+
/*
82+
* RFC 9189
83+
*/
84+
public static final int KUZNYECHIK_CTR_OMAC = 29;
85+
public static final int MAGMA_CTR_OMAC = 30;
86+
public static final int _28147_CNT_IMIT = 31;
8087
}

tls/src/main/java/org/bouncycastle/tls/KeyExchangeAlgorithm.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ public class KeyExchangeAlgorithm
5959
*/
6060
public static final int SM2 = 25;
6161

62+
/*
63+
* RFC 9189
64+
*/
65+
public static final int GOSTR341112_256 = 26;
66+
6267
public static boolean isAnonymous(int keyExchangeAlgorithm)
6368
{
6469
switch (keyExchangeAlgorithm)

tls/src/main/java/org/bouncycastle/tls/NamedGroup.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public class NamedGroup
6161
public static final int brainpoolP512r1tls13 = 33;
6262

6363
/*
64-
* draft-smyshlyaev-tls12-gost-suites-10
64+
* RFC 9189
6565
*/
6666
public static final int GC256A = 34;
6767
public static final int GC256B = 35;

tls/src/main/java/org/bouncycastle/tls/PRFAlgorithm.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public class PRFAlgorithm
1616
public static final int tls13_hkdf_sha384 = 5;
1717
// public static final int tls13_hkdf_sha512 = 6;
1818
public static final int tls13_hkdf_sm3 = 7;
19+
public static final int tls_prf_gostr3411_2012_256 = 8;
1920

2021
public static String getName(int prfAlgorithm)
2122
{
@@ -35,6 +36,8 @@ public static String getName(int prfAlgorithm)
3536
return "tls13_hkdf_sha384";
3637
case tls13_hkdf_sm3:
3738
return "tls13_hkdf_sm3";
39+
case tls_prf_gostr3411_2012_256:
40+
return "tls_prf_gostr3411_2012_256";
3841
default:
3942
return "UNKNOWN";
4043
}

tls/src/main/java/org/bouncycastle/tls/SignatureAlgorithm.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public class SignatureAlgorithm
3535
public static final short ecdsa_brainpoolP512r1tls13_sha512 = 28;
3636

3737
/*
38-
* draft-smyshlyaev-tls12-gost-suites-10
38+
* RFC 9189
3939
*/
4040
public static final short gostr34102012_256 = 64;
4141
public static final short gostr34102012_512 = 65;

tls/src/main/java/org/bouncycastle/tls/TlsECCUtils.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public static boolean isECCCipherSuite(int cipherSuite)
4040
case KeyExchangeAlgorithm.ECDHE_ECDSA:
4141
case KeyExchangeAlgorithm.ECDHE_PSK:
4242
case KeyExchangeAlgorithm.ECDHE_RSA:
43+
case KeyExchangeAlgorithm.GOSTR341112_256:
4344
return true;
4445

4546
default:

tls/src/main/java/org/bouncycastle/tls/TlsUtils.java

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2219,6 +2219,17 @@ static int getPRFAlgorithm(SecurityParameters securityParameters, int cipherSuit
22192219
throw new TlsFatalAlert(AlertDescription.illegal_parameter);
22202220
}
22212221

2222+
case CipherSuite.TLS_GOSTR341112_256_WITH_28147_CNT_IMIT:
2223+
case CipherSuite.TLS_GOSTR341112_256_WITH_KUZNYECHIK_CTR_OMAC:
2224+
case CipherSuite.TLS_GOSTR341112_256_WITH_MAGMA_CTR_OMAC:
2225+
{
2226+
if (isTLSv12Exactly)
2227+
{
2228+
return PRFAlgorithm.tls_prf_gostr3411_2012_256;
2229+
}
2230+
throw new TlsFatalAlert(AlertDescription.illegal_parameter);
2231+
}
2232+
22222233
case CipherSuite.TLS_DHE_PSK_WITH_AES_256_CBC_SHA384:
22232234
case CipherSuite.TLS_DHE_PSK_WITH_CAMELLIA_256_CBC_SHA384:
22242235
case CipherSuite.TLS_DHE_PSK_WITH_NULL_SHA384:
@@ -2707,6 +2718,9 @@ public static int getEncryptionAlgorithm(int cipherSuite)
27072718
{
27082719
switch (cipherSuite)
27092720
{
2721+
case CipherSuite.TLS_GOSTR341112_256_WITH_28147_CNT_IMIT:
2722+
return EncryptionAlgorithm._28147_CNT_IMIT;
2723+
27102724
case CipherSuite.TLS_DH_anon_WITH_3DES_EDE_CBC_SHA:
27112725
case CipherSuite.TLS_DH_DSS_WITH_3DES_EDE_CBC_SHA:
27122726
case CipherSuite.TLS_DH_RSA_WITH_3DES_EDE_CBC_SHA:
@@ -3007,6 +3021,12 @@ public static int getEncryptionAlgorithm(int cipherSuite)
30073021
case CipherSuite.TLS_RSA_PSK_WITH_CHACHA20_POLY1305_SHA256:
30083022
return EncryptionAlgorithm.CHACHA20_POLY1305;
30093023

3024+
case CipherSuite.TLS_GOSTR341112_256_WITH_KUZNYECHIK_CTR_OMAC:
3025+
return EncryptionAlgorithm.KUZNYECHIK_CTR_OMAC;
3026+
3027+
case CipherSuite.TLS_GOSTR341112_256_WITH_MAGMA_CTR_OMAC:
3028+
return EncryptionAlgorithm.MAGMA_CTR_OMAC;
3029+
30103030
case CipherSuite.TLS_DHE_PSK_WITH_NULL_SHA:
30113031
case CipherSuite.TLS_ECDH_anon_WITH_NULL_SHA:
30123032
case CipherSuite.TLS_ECDH_ECDSA_WITH_NULL_SHA:
@@ -3085,6 +3105,9 @@ public static int getEncryptionAlgorithmType(int encryptionAlgorithm)
30853105
case EncryptionAlgorithm.SM4_CBC:
30863106
return CipherType.block;
30873107

3108+
case EncryptionAlgorithm._28147_CNT_IMIT:
3109+
case EncryptionAlgorithm.KUZNYECHIK_CTR_OMAC:
3110+
case EncryptionAlgorithm.MAGMA_CTR_OMAC:
30883111
case EncryptionAlgorithm.NULL:
30893112
case EncryptionAlgorithm.RC4_40:
30903113
case EncryptionAlgorithm.RC4_128:
@@ -3332,6 +3355,11 @@ public static int getKeyExchangeAlgorithm(int cipherSuite)
33323355
case CipherSuite.TLS_ECDHE_RSA_WITH_NULL_SHA:
33333356
return KeyExchangeAlgorithm.ECDHE_RSA;
33343357

3358+
case CipherSuite.TLS_GOSTR341112_256_WITH_28147_CNT_IMIT:
3359+
case CipherSuite.TLS_GOSTR341112_256_WITH_KUZNYECHIK_CTR_OMAC:
3360+
case CipherSuite.TLS_GOSTR341112_256_WITH_MAGMA_CTR_OMAC:
3361+
return KeyExchangeAlgorithm.GOSTR341112_256;
3362+
33353363
case CipherSuite.TLS_AES_128_CCM_8_SHA256:
33363364
case CipherSuite.TLS_AES_128_CCM_SHA256:
33373365
case CipherSuite.TLS_AES_128_GCM_SHA256:
@@ -3905,6 +3933,9 @@ public static ProtocolVersion getMinimumVersion(int cipherSuite)
39053933
case CipherSuite.TLS_ECDHE_RSA_WITH_CAMELLIA_256_CBC_SHA384:
39063934
case CipherSuite.TLS_ECDHE_RSA_WITH_CAMELLIA_256_GCM_SHA384:
39073935
case CipherSuite.TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256:
3936+
case CipherSuite.TLS_GOSTR341112_256_WITH_28147_CNT_IMIT:
3937+
case CipherSuite.TLS_GOSTR341112_256_WITH_KUZNYECHIK_CTR_OMAC:
3938+
case CipherSuite.TLS_GOSTR341112_256_WITH_MAGMA_CTR_OMAC:
39083939
case CipherSuite.TLS_PSK_DHE_WITH_AES_128_CCM_8:
39093940
case CipherSuite.TLS_PSK_DHE_WITH_AES_256_CCM_8:
39103941
case CipherSuite.TLS_PSK_WITH_AES_128_CCM:
@@ -4090,8 +4121,6 @@ static boolean isValidKeyShareSelection(ProtocolVersion negotiatedVersion, int[]
40904121

40914122
static boolean isValidSignatureAlgorithmForServerKeyExchange(short signatureAlgorithm, int keyExchangeAlgorithm)
40924123
{
4093-
// TODO[tls13]
4094-
40954124
switch (keyExchangeAlgorithm)
40964125
{
40974126
case KeyExchangeAlgorithm.DHE_RSA:
@@ -4129,6 +4158,7 @@ static boolean isValidSignatureAlgorithmForServerKeyExchange(short signatureAlgo
41294158
case KeyExchangeAlgorithm.NULL:
41304159
return SignatureAlgorithm.anonymous != signatureAlgorithm;
41314160

4161+
case KeyExchangeAlgorithm.GOSTR341112_256:
41324162
default:
41334163
return false;
41344164
}
@@ -4411,6 +4441,9 @@ public static boolean isSupportedKeyExchange(TlsCrypto crypto, int keyExchangeAl
44114441
return crypto.hasSRPAuthentication()
44124442
&& hasAnyRSASigAlgs(crypto);
44134443

4444+
// TODO[RFC 9189]
4445+
case KeyExchangeAlgorithm.GOSTR341112_256:
4446+
44144447
default:
44154448
return false;
44164449
}
@@ -5620,9 +5653,32 @@ static void negotiatedCipherSuite(SecurityParameters securityParameters, int cip
56205653
{
56215654
securityParameters.verifyDataLength = securityParameters.getPRFHashLength();
56225655
}
5656+
else if (negotiatedVersion.isSSL())
5657+
{
5658+
securityParameters.verifyDataLength = 36;
5659+
}
56235660
else
56245661
{
5625-
securityParameters.verifyDataLength = negotiatedVersion.isSSL() ? 36 : 12;
5662+
/*
5663+
* RFC 9189 4.2.6. The verify_data_length value is equal to 32 for the CTR_OMAC cipher
5664+
* suites and is equal to 12 for the CNT_IMIT cipher suite.
5665+
*/
5666+
switch (cipherSuite)
5667+
{
5668+
case CipherSuite.TLS_GOSTR341112_256_WITH_KUZNYECHIK_CTR_OMAC:
5669+
case CipherSuite.TLS_GOSTR341112_256_WITH_MAGMA_CTR_OMAC:
5670+
{
5671+
securityParameters.verifyDataLength = 32;
5672+
break;
5673+
}
5674+
5675+
case CipherSuite.TLS_GOSTR341112_256_WITH_28147_CNT_IMIT:
5676+
default:
5677+
{
5678+
securityParameters.verifyDataLength = 12;
5679+
break;
5680+
}
5681+
}
56265682
}
56275683
}
56285684

tls/src/main/java/org/bouncycastle/tls/crypto/CryptoHashAlgorithm.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ public abstract class CryptoHashAlgorithm
99
public static final int sha384 = 5;
1010
public static final int sha512 = 6;
1111
public static final int sm3 = 7;
12+
public static final int gostr3411_2012_256 = 8;
1213
}

0 commit comments

Comments
 (0)