Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added locks to encrypt/decrypt in truststore password obfuscation #1968

Merged
merged 2 commits into from
Nov 18, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 25 additions & 20 deletions src/main/java/com/microsoft/sqlserver/jdbc/SecureStringUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,11 @@ final class SecureStringUtil {
private Cipher decryptCipher;

/* singleton instance */
private static SecureStringUtil instance;
private static volatile SecureStringUtil instance;

private static final Lock LOCK = new ReentrantLock();
private static final Lock INSTANCE_LOCK = new ReentrantLock();
private static final Lock ENCRYPT_LOCK = new ReentrantLock();
private static final Lock DECRYPT_LOCK = new ReentrantLock();

/**
* Get reference to SecureStringUtil instance
Expand All @@ -57,13 +59,13 @@ final class SecureStringUtil {
*/
static SecureStringUtil getInstance() throws SQLServerException {
if (instance == null) {
LOCK.lock();
INSTANCE_LOCK.lock();
try {
if (instance == null) {
instance = new SecureStringUtil();
}
} finally {
LOCK.unlock();
INSTANCE_LOCK.unlock();
}
}
return instance;
Expand Down Expand Up @@ -104,15 +106,15 @@ private SecureStringUtil() throws SQLServerException {
* if error
*/
byte[] getEncryptedBytes(char[] chars) throws SQLServerException {
if (chars == null)
return null;

byte[] iv = new byte[IV_LENGTH];
SecureRandom random = new SecureRandom();
random.nextBytes(iv);
GCMParameterSpec ivParamSpec = new GCMParameterSpec(TAG_LENGTH * 8, iv);

ENCRYPT_LOCK.lock();
try {
if (chars == null)
return null;
tkyc marked this conversation as resolved.
Show resolved Hide resolved

byte[] iv = new byte[IV_LENGTH];
SecureRandom random = new SecureRandom();
random.nextBytes(iv);
GCMParameterSpec ivParamSpec = new GCMParameterSpec(TAG_LENGTH * 8, iv);
encryptCipher.init(Cipher.ENCRYPT_MODE, secretKey, ivParamSpec);

byte[] cipherText = encryptCipher.doFinal(Util.charsToBytes(chars));
Expand All @@ -124,6 +126,8 @@ byte[] getEncryptedBytes(char[] chars) throws SQLServerException {
MessageFormat form = new MessageFormat(SQLServerException.getErrString("R_EncryptionFailed"));
Object[] msgArgs = {e.getMessage()};
throw new SQLServerException(this, form.format(msgArgs), null, 0, false);
} finally {
ENCRYPT_LOCK.unlock();
}
}

Expand All @@ -137,16 +141,16 @@ byte[] getEncryptedBytes(char[] chars) throws SQLServerException {
* @throws SQLServerException
tkyc marked this conversation as resolved.
Show resolved Hide resolved
*/
char[] getDecryptedChars(byte[] bytes) throws SQLServerException {
if (bytes == null)
return null;

byte[] iv = new byte[IV_LENGTH];
System.arraycopy(bytes, 0, iv, 0, IV_LENGTH);

GCMParameterSpec ivParamSpec = new GCMParameterSpec(TAG_LENGTH * 8, iv);

DECRYPT_LOCK.lock();
byte[] plainText = null;
try {
if (bytes == null)
return null;
tkyc marked this conversation as resolved.
Show resolved Hide resolved

byte[] iv = new byte[IV_LENGTH];
System.arraycopy(bytes, 0, iv, 0, IV_LENGTH);

GCMParameterSpec ivParamSpec = new GCMParameterSpec(TAG_LENGTH * 8, iv);
decryptCipher.init(Cipher.DECRYPT_MODE, secretKey, ivParamSpec);

plainText = decryptCipher.doFinal(bytes, IV_LENGTH, bytes.length - IV_LENGTH);
Expand All @@ -159,6 +163,7 @@ char[] getDecryptedChars(byte[] bytes) throws SQLServerException {
if (plainText != null) {
Arrays.fill(plainText, (byte) 0);
}
DECRYPT_LOCK.unlock();
}
}
}