Merge pull request #63 from cconlon/infer
Fix potential deadlock / thread safety issues reported by Inferpull/64/head
commit
e1e1a818a3
|
@ -233,7 +233,8 @@ public class WolfCryptKeyAgreement extends KeyAgreementSpi {
|
||||||
"shared secret");
|
"shared secret");
|
||||||
}
|
}
|
||||||
|
|
||||||
tmp = this.dh.makeSharedSecret(this.dh);
|
/* public key has been stored inside this.dh already */
|
||||||
|
tmp = this.dh.makeSharedSecret();
|
||||||
if (tmp == null) {
|
if (tmp == null) {
|
||||||
throw new RuntimeException("Error when creating DH " +
|
throw new RuntimeException("Error when creating DH " +
|
||||||
"shared secret");
|
"shared secret");
|
||||||
|
|
|
@ -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
|
* Generate DH shared secret
|
||||||
*
|
*
|
||||||
|
@ -246,15 +269,42 @@ public class Dh extends NativeStruct {
|
||||||
public synchronized byte[] makeSharedSecret(Dh pubKey)
|
public synchronized byte[] makeSharedSecret(Dh pubKey)
|
||||||
throws WolfCryptException, IllegalStateException {
|
throws WolfCryptException, IllegalStateException {
|
||||||
|
|
||||||
byte[] publicKey = pubKey.getPublicKey();
|
byte[] publicKey = null;
|
||||||
|
|
||||||
if (privateKey != null || publicKey != null) {
|
if (pubKey == null) {
|
||||||
synchronized (pointerLock) {
|
|
||||||
return wc_DhAgree(privateKey, publicKey);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
throw new IllegalStateException(
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -52,8 +52,10 @@ public abstract class NativeStruct extends WolfObject {
|
||||||
* @return pointer to native structure
|
* @return pointer to native structure
|
||||||
*/
|
*/
|
||||||
public long getNativeStruct() {
|
public long getNativeStruct() {
|
||||||
|
synchronized (pointerLock) {
|
||||||
return this.pointer;
|
return this.pointer;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set pointer to native structure
|
* Set pointer to native structure
|
||||||
|
|
|
@ -201,12 +201,12 @@ public class Rsa extends NativeStruct {
|
||||||
*/
|
*/
|
||||||
protected void willUseKey(boolean priv) throws IllegalStateException {
|
protected void willUseKey(boolean priv) throws IllegalStateException {
|
||||||
|
|
||||||
|
synchronized (stateLock) {
|
||||||
if (priv && !hasPrivateKey) {
|
if (priv && !hasPrivateKey) {
|
||||||
throw new IllegalStateException(
|
throw new IllegalStateException(
|
||||||
"No available private key to perform the operation");
|
"No available private key to perform the operation");
|
||||||
}
|
}
|
||||||
|
|
||||||
synchronized (stateLock) {
|
|
||||||
if (state != WolfCryptState.READY) {
|
if (state != WolfCryptState.READY) {
|
||||||
throw new IllegalStateException(
|
throw new IllegalStateException(
|
||||||
"No available key to perform the operation");
|
"No available key to perform the operation");
|
||||||
|
|
Loading…
Reference in New Issue