Skip to content

Commit e84ba03

Browse files
committed
JCE: fix deserialization bugs in WolfCryptPBEKey and WolfCryptRandom, remove unused field, make inner class static
1 parent 499b3fe commit e84ba03

3 files changed

Lines changed: 41 additions & 6 deletions

File tree

src/main/java/com/wolfssl/provider/jce/WolfCryptDhParameterGenerator.java

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,21 +51,40 @@ public class WolfCryptDhParameterGenerator
5151
/* Exponent size in bits, 0 means not specified */
5252
private int exponentSize = 0;
5353

54-
/* SecureRandom for parameter generation */
55-
private SecureRandom random = null;
5654

5755
/**
5856
* Create new WolfCryptDhParameterGenerator object
5957
*/
6058
public WolfCryptDhParameterGenerator() {
6159
}
6260

61+
/**
62+
* Initialize with desired prime size.
63+
*
64+
* The SecureRandom parameter is intentionally ignored;
65+
* wolfCrypt uses its own internal RNG for DH parameter
66+
* generation to ensure FIPS-compliant randomness.
67+
*
68+
* @param size desired prime size in bits
69+
* @param random caller-supplied randomness (ignored)
70+
*/
6371
@Override
6472
protected void engineInit(int size, SecureRandom random) {
6573
this.size = size;
66-
this.random = random;
6774
}
6875

76+
/**
77+
* Initialize from a DHGenParameterSpec.
78+
*
79+
* The SecureRandom parameter is intentionally ignored;
80+
* wolfCrypt uses its own internal RNG for DH parameter
81+
* generation to ensure FIPS-compliant randomness.
82+
*
83+
* @param genParamSpec DH generation parameters
84+
* @param random caller-supplied randomness (ignored)
85+
* @throws InvalidAlgorithmParameterException if
86+
* genParamSpec is null or not a DHGenParameterSpec
87+
*/
6988
@Override
7089
protected void engineInit(AlgorithmParameterSpec genParamSpec,
7190
SecureRandom random) throws InvalidAlgorithmParameterException {
@@ -84,7 +103,6 @@ protected void engineInit(AlgorithmParameterSpec genParamSpec,
84103
DHGenParameterSpec dhGenSpec = (DHGenParameterSpec)genParamSpec;
85104
this.size = dhGenSpec.getPrimeSize();
86105
this.exponentSize = dhGenSpec.getExponentSize();
87-
this.random = random;
88106
}
89107

90108
@Override

src/main/java/com/wolfssl/provider/jce/WolfCryptPBEKey.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
package com.wolfssl.provider.jce;
2323

24+
import java.io.IOException;
25+
import java.io.ObjectInputStream;
2426
import java.util.Arrays;
2527
import java.security.spec.InvalidKeySpecException;
2628
import javax.crypto.interfaces.PBEKey;
@@ -51,7 +53,7 @@ public class WolfCryptPBEKey implements PBEKey {
5153
private boolean destroyed = false;
5254

5355
/** Lock around use of destroyed boolean */
54-
private transient final Object destroyedLock = new Object();
56+
private transient Object destroyedLock = new Object();
5557

5658
/**
5759
* Create new WolfCryptPBEKey object.
@@ -284,5 +286,20 @@ public synchronized boolean equals(Object obj) {
284286
return true;
285287
}
286288
}
289+
290+
/**
291+
* Restore transient fields on deserialization.
292+
*
293+
* @param in ObjectInputStream to read from
294+
*
295+
* @throws IOException on error reading stream
296+
* @throws ClassNotFoundException if class not found
297+
*/
298+
private void readObject(ObjectInputStream in)
299+
throws IOException, ClassNotFoundException {
300+
301+
in.defaultReadObject();
302+
this.destroyedLock = new Object();
303+
}
287304
}
288305

src/main/java/com/wolfssl/provider/jce/WolfCryptRandom.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ private synchronized void writeObject(ObjectOutputStream out)
158158
* @throws IOException on error reading from ObjectInputStream
159159
* @throws ClassNotFoundException if object class not found
160160
*/
161-
private synchronized void readObject(ObjectInputStream in)
161+
private void readObject(ObjectInputStream in)
162162
throws IOException, ClassNotFoundException {
163163

164164
in.defaultReadObject();

0 commit comments

Comments
 (0)