Skip to content

Commit

Permalink
Updated calls to DLL's to be loom friendly and use Reentrant locks in…
Browse files Browse the repository at this point in the history
…stead of synchronized (#2225)
  • Loading branch information
lilgreenbird authored Oct 3, 2023
1 parent 3343f73 commit 298292b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
20 changes: 15 additions & 5 deletions src/main/java/com/microsoft/sqlserver/jdbc/AuthenticationJNI.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

import java.text.MessageFormat;
import java.util.logging.Level;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;


class FedAuthDllInfo {
Expand Down Expand Up @@ -37,6 +39,8 @@ final class AuthenticationJNI extends SSPIAuthentication {

private static final UnsatisfiedLinkError linkError;

private static final Lock lock = new ReentrantLock();

static int getMaxSSPIBlobSize() {
return sspiBlobMaxlen;
}
Expand Down Expand Up @@ -82,8 +86,14 @@ static boolean isDllLoaded() {

static FedAuthDllInfo getAccessTokenForWindowsIntegrated(String stsURL, String servicePrincipalName,
String clientConnectionId, String clientId, long expirationFileTime) throws DLLException {
return ADALGetAccessTokenForWindowsIntegrated(stsURL, servicePrincipalName, clientConnectionId, clientId,
expirationFileTime, authLogger);
try {
lock.lock();

return ADALGetAccessTokenForWindowsIntegrated(stsURL, servicePrincipalName, clientConnectionId, clientId,
expirationFileTime, authLogger);
} finally {
lock.unlock();
}
}

// InitDNSName should be called to initialize the DNSName before calling this function
Expand Down Expand Up @@ -154,13 +164,13 @@ private static native int SNISecGenClientContext(byte[] psec, int[] secptrsize,

private static native int GetDNSName(String address, String[] DNSName, java.util.logging.Logger log);

private static synchronized native FedAuthDllInfo ADALGetAccessTokenForWindowsIntegrated(String stsURL,
private static native FedAuthDllInfo ADALGetAccessTokenForWindowsIntegrated(String stsURL,
String servicePrincipalName, String clientConnectionId, String clientId, long expirationFileTime,
java.util.logging.Logger log);

static synchronized native byte[] DecryptColumnEncryptionKey(String masterKeyPath, String encryptionAlgorithm,
static native byte[] DecryptColumnEncryptionKey(String masterKeyPath, String encryptionAlgorithm,
byte[] encryptedColumnEncryptionKey) throws DLLException;

static synchronized native boolean VerifyColumnMasterKeyMetadata(String keyPath, boolean allowEnclaveComputations,
static native boolean VerifyColumnMasterKeyMetadata(String keyPath, boolean allowEnclaveComputations,
byte[] signature) throws DLLException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
package com.microsoft.sqlserver.jdbc;

import java.util.Locale;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;


/**
Expand All @@ -25,6 +27,8 @@ public final class SQLServerColumnEncryptionCertificateStoreProvider extends SQL
static final String CURRENT_USER_DIRECTORY = "CurrentUser";
static final String MY_CERTIFICATE_STORE = "My";

private static final Lock lock = new ReentrantLock();

static {
if (System.getProperty("os.name").toLowerCase(Locale.ENGLISH).startsWith("windows")) {
isWindows = true;
Expand Down Expand Up @@ -78,21 +82,29 @@ public byte[] decryptColumnEncryptionKey(String masterKeyPath, String encryption
public boolean verifyColumnMasterKeyMetadata(String masterKeyPath, boolean allowEnclaveComputations,
byte[] signature) throws SQLServerException {
try {
lock.lock();

return AuthenticationJNI.VerifyColumnMasterKeyMetadata(masterKeyPath, allowEnclaveComputations, signature);
} catch (DLLException e) {
DLLException.buildException(e.getErrCode(), e.getParam1(), e.getParam2(), e.getParam3());
return false;
} finally {
lock.unlock();
}
}

private byte[] decryptColumnEncryptionKeyWindows(String masterKeyPath, String encryptionAlgorithm,
byte[] encryptedColumnEncryptionKey) throws SQLServerException {
try {
lock.lock();

return AuthenticationJNI.DecryptColumnEncryptionKey(masterKeyPath, encryptionAlgorithm,
encryptedColumnEncryptionKey);
} catch (DLLException e) {
DLLException.buildException(e.getErrCode(), e.getParam1(), e.getParam2(), e.getParam3());
return null;
} finally {
lock.unlock();
}
}

Expand Down

0 comments on commit 298292b

Please sign in to comment.