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 check for closed statement to registerColumnEncryptionKeyStoreProvidersOnStatement #1644

Merged
merged 1 commit into from
Aug 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class CMKMetadataSignatureInfo {
String masterKeyPath;
boolean allowEnclaveComputations;
String signatureHexString;

public CMKMetadataSignatureInfo(String masterKeyPath, boolean allowEnclaveComputations, byte[] signature) {
this.masterKeyPath = masterKeyPath;
this.allowEnclaveComputations = allowEnclaveComputations;
Expand All @@ -64,38 +64,37 @@ public boolean isAllowEnclaveComputations() {
public String getSignatureHexString() {
return signatureHexString;
}

@Override
public int hashCode() {
int hash = 7;
hash = 31 * hash + (null != masterKeyPath ? masterKeyPath.hashCode() : 0);
hash = 31 * hash + (allowEnclaveComputations ? 1 : 0);
hash = 31 * hash + (null != signatureHexString ? signatureHexString.hashCode() : 0);

return hash;
}

@Override
public boolean equals(Object object) {
if (this == object) {
return true;
}

if (null != object && CMKMetadataSignatureInfo.class == object.getClass()) {
CMKMetadataSignatureInfo other = (CMKMetadataSignatureInfo) object;

if (hashCode() == other.hashCode()) {
return ((null == masterKeyPath ? null == other.masterKeyPath
: masterKeyPath.equals(other.masterKeyPath))
&& allowEnclaveComputations == other.allowEnclaveComputations
&& (null == signatureHexString ? null == other.signatureHexString
: signatureHexString.equals(other.signatureHexString))
);
&& (null == signatureHexString ? null == other.signatureHexString
: signatureHexString.equals(other.signatureHexString)));
}

}
return false;

return false;
}
}

Expand Down Expand Up @@ -147,12 +146,13 @@ public class SQLServerColumnEncryptionAzureKeyVaultProvider extends SQLServerCol
* values. The default expiration is set to 2 hours.
*/
private final SimpleTtlCache<String, byte[]> columnEncryptionKeyCache = new SimpleTtlCache<>();

/**
* A cache for storing the results of signature verification of column master key metadata.
* The default expiration is set to 10 days.
* A cache for storing the results of signature verification of column master key metadata. The default expiration
* is set to 10 days.
*/
private final SimpleTtlCache<CMKMetadataSignatureInfo, Boolean> cmkMetadataSignatureVerificationCache = new SimpleTtlCache<>(Duration.ofDays(10));
private final SimpleTtlCache<CMKMetadataSignatureInfo, Boolean> cmkMetadataSignatureVerificationCache = new SimpleTtlCache<>(
Duration.ofDays(10));

public void setName(String name) {
this.name = name;
Expand Down Expand Up @@ -183,7 +183,6 @@ public void setColumnEncryptionCacheTtl(Duration duration) {
columnEncryptionKeyCache.setCacheTtl(duration);
}


/**
* Constructs a SQLServerColumnEncryptionAzureKeyVaultProvider to authenticate to AAD using the client id and client
* key. This is used by KeyVault client at runtime to authenticate to Azure Key Vault.
Expand Down Expand Up @@ -359,12 +358,12 @@ public byte[] decryptColumnEncryptionKey(String masterKeyPath, String encryption
String encryptedColumnEncryptionKeyHexString = Util.byteToHexDisplayString(encryptedColumnEncryptionKey);
if (columnEncryptionKeyCache.getCacheTtl().getSeconds() > 0) {
allowCache = true;

if (columnEncryptionKeyCache.contains(encryptedColumnEncryptionKeyHexString)) {
return columnEncryptionKeyCache.get(encryptedColumnEncryptionKeyHexString);
}
}

// Get key path length
int currentIndex = firstVersion.length;
short keyPathLength = convertTwoBytesToShort(encryptedColumnEncryptionKey, currentIndex);
Expand Down Expand Up @@ -436,7 +435,7 @@ public byte[] decryptColumnEncryptionKey(String masterKeyPath, String encryption
if (allowCache) {
columnEncryptionKeyCache.put(encryptedColumnEncryptionKeyHexString, decryptedCEK);
}

return decryptedCEK;
}

Expand Down Expand Up @@ -866,9 +865,9 @@ public boolean verifyColumnMasterKeyMetadata(String masterKeyPath, boolean allow
}

KeyStoreProviderCommon.validateNonEmptyMasterKeyPath(masterKeyPath);

CMKMetadataSignatureInfo key = new CMKMetadataSignatureInfo(masterKeyPath, allowEnclaveComputations, signature);

if (cmkMetadataSignatureVerificationCache.contains(key)) {
return cmkMetadataSignatureVerificationCache.get(key);
}
Expand All @@ -894,7 +893,7 @@ public boolean verifyColumnMasterKeyMetadata(String masterKeyPath, boolean allow
// Validate the signature
boolean isValid = AzureKeyVaultVerifySignature(dataToVerify, signature, masterKeyPath);
cmkMetadataSignatureVerificationCache.put(key, isValid);

return isValid;
} catch (NoSuchAlgorithmException e) {
throw new SQLServerException(SQLServerException.getErrString("R_NoSHA256Algorithm"), e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2410,6 +2410,8 @@ public synchronized void registerColumnEncryptionKeyStoreProvidersOnStatement(
loggerExternal.entering(loggingClassName, "registerColumnEncryptionKeyStoreProvidersOnStatement",
"Registering Column Encryption Key Store Providers on Statement");

checkClosed();

if (null == clientKeyStoreProviders) {
throw new SQLServerException(null, SQLServerException.getErrString("R_CustomKeyStoreProviderMapNull"), null,
0, false);
Expand Down
Loading