Merge pull request #63 from cconlon/infer

Fix potential deadlock / thread safety issues reported by Infer
pull/64/head
JacobBarthelmeh 2024-02-14 04:00:45 +07:00 committed by GitHub
commit e1e1a818a3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 67 additions and 14 deletions

View File

@ -233,7 +233,8 @@ public class WolfCryptKeyAgreement extends KeyAgreementSpi {
"shared secret");
}
tmp = this.dh.makeSharedSecret(this.dh);
/* public key has been stored inside this.dh already */
tmp = this.dh.makeSharedSecret();
if (tmp == null) {
throw new RuntimeException("Error when creating DH " +
"shared secret");

View File

@ -233,6 +233,29 @@ public class Dh extends NativeStruct {
}
}
/**
* Generate DH shared secret using private and public key stored in
* this object.
*
* @return shared secret as byte array
*
* @throws WolfCryptException if native operation fails
* @throws IllegalStateException if this object has no stored private
* and public keys
*/
public synchronized byte[] makeSharedSecret()
throws WolfCryptException, IllegalStateException {
byte[] publicKey = getPublicKey();
if (publicKey == null) {
throw new IllegalStateException(
"Dh object has no public key");
}
return makeSharedSecret(publicKey);
}
/**
* Generate DH shared secret
*
@ -246,15 +269,42 @@ public class Dh extends NativeStruct {
public synchronized byte[] makeSharedSecret(Dh pubKey)
throws WolfCryptException, IllegalStateException {
byte[] publicKey = pubKey.getPublicKey();
byte[] publicKey = null;
if (privateKey != null || publicKey != null) {
synchronized (pointerLock) {
return wc_DhAgree(privateKey, publicKey);
}
} else {
if (pubKey == null) {
throw new IllegalStateException(
"No available key to perform the operation");
"Provided public key is null");
}
return makeSharedSecret(pubKey.getPublicKey());
}
/**
* Generate DH shared secret using internal private key and
* externally-provided public key as byte array.
*
* @param pubKey public key to use for secret generation
*
* @return shared secret as byte array
*
* @throws WolfCryptException if native operation fails
* @throws IllegalStateException if object has no key
*/
public synchronized byte[] makeSharedSecret(byte[] pubKey)
throws WolfCryptException, IllegalStateException {
if (pubKey == null) {
throw new IllegalStateException(
"Provided public key is null");
}
if (this.privateKey == null) {
throw new IllegalStateException(
"Dh object has no private key");
}
synchronized (pointerLock) {
return wc_DhAgree(this.privateKey, pubKey);
}
}
}

View File

@ -52,7 +52,9 @@ public abstract class NativeStruct extends WolfObject {
* @return pointer to native structure
*/
public long getNativeStruct() {
return this.pointer;
synchronized (pointerLock) {
return this.pointer;
}
}
/**

View File

@ -201,12 +201,12 @@ public class Rsa extends NativeStruct {
*/
protected void willUseKey(boolean priv) throws IllegalStateException {
if (priv && !hasPrivateKey) {
throw new IllegalStateException(
"No available private key to perform the operation");
}
synchronized (stateLock) {
if (priv && !hasPrivateKey) {
throw new IllegalStateException(
"No available private key to perform the operation");
}
if (state != WolfCryptState.READY) {
throw new IllegalStateException(
"No available key to perform the operation");