Skip to content

Commit a5ea53d

Browse files
committed
Update some jdk1.4 classes for recent changes
1 parent cc6137b commit a5ea53d

File tree

3 files changed

+79
-40
lines changed

3 files changed

+79
-40
lines changed

tls/src/main/jdk1.4/org/bouncycastle/tls/crypto/impl/jcajce/JcaTlsCrypto.java

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
import org.bouncycastle.tls.crypto.impl.AEADNonceGenerator;
6262
import org.bouncycastle.tls.crypto.impl.AEADNonceGeneratorFactory;
6363
import org.bouncycastle.tls.crypto.impl.AbstractTlsCrypto;
64+
import org.bouncycastle.tls.crypto.impl.Tls13NullCipher;
6465
import org.bouncycastle.tls.crypto.impl.TlsAEADCipher;
6566
import org.bouncycastle.tls.crypto.impl.TlsAEADCipherImpl;
6667
import org.bouncycastle.tls.crypto.impl.TlsBlockCipher;
@@ -245,6 +246,12 @@ public TlsCipher createCipher(TlsCryptoParameters cryptoParams, int encryptionAl
245246
return createChaCha20Poly1305(cryptoParams);
246247
case EncryptionAlgorithm.NULL:
247248
return createNullCipher(cryptoParams, macAlgorithm);
249+
case EncryptionAlgorithm.NULL_HMAC_SHA256:
250+
// NOTE: Ignores macAlgorithm
251+
return create13NullCipher(cryptoParams, MACAlgorithm.hmac_sha256);
252+
case EncryptionAlgorithm.NULL_HMAC_SHA384:
253+
// NOTE: Ignores macAlgorithm
254+
return create13NullCipher(cryptoParams, MACAlgorithm.hmac_sha384);
248255
case EncryptionAlgorithm.SEED_CBC:
249256
return createCipher_CBC(cryptoParams, "SEED", 16, macAlgorithm);
250257
case EncryptionAlgorithm.SM4_CBC:
@@ -442,18 +449,13 @@ public AlgorithmParameters getNamedGroupAlgorithmParameters(int namedGroup)
442449
{
443450
if (NamedGroup.refersToAnXDHCurve(namedGroup))
444451
{
445-
switch (namedGroup)
446-
{
447452
/*
448-
* TODO Return AlgorithmParameters to check against disabled algorithms
453+
* TODO Return AlgorithmParameters to check against disabled algorithms?
449454
*
450455
* NOTE: The JDK doesn't even support AlgorithmParameters for XDH, so SunJSSE also winds
451456
* up using null AlgorithmParameters when checking algorithm constraints.
452457
*/
453-
case NamedGroup.x25519:
454-
case NamedGroup.x448:
455-
return null;
456-
}
458+
return null;
457459
}
458460
else if (NamedGroup.refersToAnECDSACurve(namedGroup))
459461
{
@@ -947,6 +949,12 @@ protected TlsHash createHash(String digestName)
947949
return new JcaTlsHash(helper.createDigest(digestName));
948950
}
949951

952+
protected Tls13NullCipher create13NullCipher(TlsCryptoParameters cryptoParams, int macAlgorithm)
953+
throws IOException
954+
{
955+
return new Tls13NullCipher(cryptoParams, createHMAC(macAlgorithm), createHMAC(macAlgorithm));
956+
}
957+
950958
/**
951959
* To disable the null cipher suite, override this method with one that throws an IOException.
952960
*
@@ -1005,7 +1013,7 @@ protected TlsStreamSigner createStreamSigner(JcaJceHelper helper, String algorit
10051013
{
10061014
if (null != parameter)
10071015
{
1008-
Signature dummySigner;
1016+
Signature dummySigner;
10091017
try
10101018
{
10111019
dummySigner = helper.createSignature(algorithmName);
@@ -1170,6 +1178,12 @@ protected Boolean isSupportedEncryptionAlgorithm(int encryptionAlgorithm)
11701178
case EncryptionAlgorithm.SM4_GCM:
11711179
return Boolean.valueOf(isUsableCipher("SM4/GCM/NoPadding", 128));
11721180

1181+
case EncryptionAlgorithm.NULL_HMAC_SHA256:
1182+
return hasMacAlgorithm(MACAlgorithm.hmac_sha256);
1183+
1184+
case EncryptionAlgorithm.NULL_HMAC_SHA384:
1185+
return hasMacAlgorithm(MACAlgorithm.hmac_sha384);
1186+
11731187
case EncryptionAlgorithm._28147_CNT_IMIT:
11741188
case EncryptionAlgorithm.DES_CBC:
11751189
case EncryptionAlgorithm.DES40_CBC:

tls/src/main/jdk1.4/org/bouncycastle/tls/crypto/impl/jcajce/JceAEADCipherImpl.java

Lines changed: 57 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.security.GeneralSecurityException;
77
import java.security.PrivilegedAction;
88
import java.security.SecureRandom;
9+
import java.security.spec.AlgorithmParameterSpec;
910

1011
import javax.crypto.Cipher;
1112
import javax.crypto.SecretKey;
@@ -68,6 +69,9 @@ private static String getAlgParamsName(JcaJceHelper helper, String cipherName)
6869

6970
private SecretKey key;
7071

72+
private byte[] noncePre7;
73+
private int macSizePre7;
74+
7175
public JceAEADCipherImpl(JcaTlsCrypto crypto, JcaJceHelper helper, String cipherName, String algorithm, int keySize,
7276
boolean isEncrypting)
7377
throws GeneralSecurityException
@@ -91,9 +95,6 @@ public void setKey(byte[] key, int keyOff, int keyLen)
9195
this.key = new SecretKeySpec(key, keyOff, keyLen, algorithm);
9296
}
9397

94-
private byte[] nonce;
95-
private int macSize;
96-
9798
public void init(byte[] nonce, int macSize)
9899
{
99100
// NOTE: Shouldn't need a SecureRandom, but this is cheaper if the provider would auto-create one
@@ -104,23 +105,44 @@ public void init(byte[] nonce, int macSize)
104105
// if (canDoAEAD && algorithmParamsName != null)
105106
// {
106107
// AlgorithmParameters algParams = helper.createAlgorithmParameters(algorithmParamsName);
107-
//
108-
// // fortunately CCM and GCM parameters have the same ASN.1 structure
109-
// algParams.init(new GCMParameters(nonce, macSize).getEncoded());
110-
//
111-
// cipher.init(cipherMode, key, algParams);
112-
//
113-
// if (additionalData != null && additionalData.length > 0)
108+
//
109+
// // believe it or not but there are things out there that do not support the ASN.1 encoding...
110+
// if (GCMUtil.isGCMParameterSpecAvailable())
111+
// {
112+
// algParams.init(GCMUtil.createGCMParameterSpec(macSize * 8, nonce));
113+
// }
114+
// else
114115
// {
115-
// cipher.updateAAD(additionalData);
116+
// // fortunately CCM and GCM parameters have the same ASN.1 structure
117+
// algParams.init(new GCMParameters(nonce, macSize).getEncoded());
116118
// }
119+
//
120+
// cipher.init(cipherMode, key, algParams, random);
117121
// }
118122
// else
119123
// {
120-
// Otherwise fall back to the BC-specific AEADParameterSpec
121-
this.nonce = Arrays.clone(nonce);
122-
this.macSize = macSize;
123-
// }
124+
/*
125+
* Otherwise fall back to the BC-specific AEADParameterSpec. Since updateAAD is not available, we
126+
* need to use init to pass the associated data (in doFinal), but in order to call getOutputSize we
127+
* technically need to init the cipher first. So we init with a dummy nonce to avoid duplicate nonce
128+
* error from the init in doFinal.
129+
*/
130+
131+
if (this.noncePre7 == null || this.noncePre7.length != nonce.length)
132+
{
133+
this.noncePre7 = new byte[nonce.length];
134+
}
135+
136+
System.arraycopy(nonce, 0, this.noncePre7, 0, nonce.length);
137+
this.macSizePre7 = macSize;
138+
139+
this.noncePre7[0] ^= 0x80;
140+
141+
AlgorithmParameterSpec params = new AEADParameterSpec(noncePre7, macSizePre7 * 8, null);
142+
cipher.init(cipherMode, key, params, random);
143+
144+
this.noncePre7[0] ^= 0x80;
145+
// }
124146
}
125147
catch (Exception e)
126148
{
@@ -136,20 +158,27 @@ public int getOutputSize(int inputLength)
136158
public int doFinal(byte[] additionalData, byte[] input, int inputOffset, int inputLength, byte[] output, int outputOffset)
137159
throws IOException
138160
{
139-
try
140-
{
141-
if (!Arrays.isNullOrEmpty(additionalData))
142-
{
143-
cipher.init(cipherMode, key, new AEADParameterSpec(nonce, macSize * 8, additionalData));
144-
}
145-
else
146-
{
147-
cipher.init(cipherMode, key, new AEADParameterSpec(nonce, macSize * 8, null));
148-
}
149-
}
150-
catch (Exception e)
161+
if (!Arrays.isNullOrEmpty(additionalData))
151162
{
152-
throw new IOException(e.toString());
163+
// if (canDoAEAD)
164+
// {
165+
// cipher.updateAAD(additionalData);
166+
// }
167+
// else
168+
// {
169+
try
170+
{
171+
// NOTE: Shouldn't need a SecureRandom, but this is cheaper if the provider would auto-create one
172+
SecureRandom random = crypto.getSecureRandom();
173+
174+
AlgorithmParameterSpec params = new AEADParameterSpec(noncePre7, macSizePre7 * 8, additionalData);
175+
cipher.init(cipherMode, key, params, random);
176+
}
177+
catch (Exception e)
178+
{
179+
throw new IOException(e);
180+
}
181+
// }
153182
}
154183

155184
/*

tls/src/main/jdk1.4/org/bouncycastle/tls/crypto/impl/jcajce/JceTlsECDomain.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,13 @@ public class JceTlsECDomain
3232
implements TlsECDomain
3333
{
3434
protected final JcaTlsCrypto crypto;
35-
protected final TlsECConfig ecConfig;
36-
37-
3835
protected ECNamedCurveGenParameterSpec ecGenSpec;
3936
protected ECParameterSpec ecParameterSpec;
4037
protected ECCurve ecCurve;
4138

4239
public JceTlsECDomain(JcaTlsCrypto crypto, TlsECConfig ecConfig)
4340
{
4441
this.crypto = crypto;
45-
this.ecConfig = ecConfig;
4642

4743
init(ecConfig.getNamedGroup());
4844
}

0 commit comments

Comments
 (0)