Skip to content
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
48 changes: 22 additions & 26 deletions src/main/java/com/microsoft/sqlserver/jdbc/AE.java
Original file line number Diff line number Diff line change
Expand Up @@ -226,27 +226,25 @@ short getOrdinal() {
encryptionKeyInfo = null;
}

boolean isAlgorithmInitialized() {
boolean IsAlgorithmInitialized() {
return null != cipherAlgorithm;
}
}


/**
* Fields in the first resultset of "sp_describe_parameter_encryption" We expect the server to return the fields in the
* resultset in the same order as mentioned below. If the server changes the below order, then transparent parameter
* encryption will break.
*/
// Fields in the first resultset of "sp_describe_parameter_encryption"
// We expect the server to return the fields in the resultset in the same order as mentioned below.
// If the server changes the below order, then transparent parameter encryption will break.
enum DescribeParameterEncryptionResultSet1 {
KEYORDINAL,
DBID,
KEYID,
KEYVERSION,
KEYMDVERSION,
ENCRYPTEDKEY,
PROVIDERNAME,
KEYPATH,
KEYENCRYPTIONALGORITHM;
KeyOrdinal,
DbId,
KeyId,
KeyVersion,
KeyMdVersion,
EncryptedKey,
ProviderName,
KeyPath,
KeyEncryptionAlgorithm;

int value() {
// Column indexing starts from 1;
Expand All @@ -255,18 +253,16 @@ int value() {
}


/**
* Fields in the second resultset of "sp_describe_parameter_encryption" We expect the server to return the fields in the
* resultset in the same order as mentioned below. If the server changes the below order, then transparent parameter
* encryption will break.
*/
// Fields in the second resultset of "sp_describe_parameter_encryption"
// We expect the server to return the fields in the resultset in the same order as mentioned below.
// If the server changes the below order, then transparent parameter encryption will break.
enum DescribeParameterEncryptionResultSet2 {
PARAMETERORDINAL,
PARAMETERNAME,
COLUMNENCRYPTIONALGORITHM,
COLUMNENCRYPTIONTYPE,
COLUMNENCXRYPTIONKEYORDINAL,
NORMALIZATIONRULEVERSION;
ParameterOrdinal,
ParameterName,
ColumnEncryptionAlgorithm,
ColumnEncrytionType,
ColumnEncryptionKeyOrdinal,
NormalizationRuleVersion;

int value() {
// Column indexing starts from 1;
Expand Down
101 changes: 61 additions & 40 deletions src/main/java/com/microsoft/sqlserver/jdbc/AuthenticationJNI.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/*
* Microsoft JDBC Driver for SQL Server Copyright(c) Microsoft Corporation All rights reserved. This program is made available under the terms of the MIT License. See the LICENSE file in the project root for more information.
* Microsoft JDBC Driver for SQL Server Copyright(c) Microsoft Corporation All rights reserved. This program is made
* available under the terms of the MIT License. See the LICENSE file in the project root for more information.
*/

package com.microsoft.sqlserver.jdbc;

import java.util.logging.Level;
import java.util.logging.Logger;


class FedAuthDllInfo {
Expand All @@ -23,20 +23,20 @@ class FedAuthDllInfo {
* Encapsulation of the JNI native calls for trusted authentication.
*/
final class AuthenticationJNI extends SSPIAuthentication {

private byte[] sniSec = new byte[MAXIMUM_POINTER_SIZE];
private int[] sniSecLen =
{0};
private SQLServerConnection con;
private final static int maximumpointersize = 128; // we keep the SNI_Sec pointer
private static boolean enabled = false;
private static Logger authLogger = Logger.getLogger("com.microsoft.sqlserver.jdbc.internals.AuthenticationJNI");
private static java.util.logging.Logger authLogger = java.util.logging.Logger
.getLogger("com.microsoft.sqlserver.jdbc.internals.AuthenticationJNI");
private static int sspiBlobMaxlen = 0;
private static final UnsatisfiedLinkError LINK_ERROR;
private static final int MAXIMUM_POINTER_SIZE = 128; // we keep the SNI_Sec pointer
private final String dnsName;
private byte[] sniSec = new byte[maximumpointersize];
private int sniSecLen[] = {0};
private final String DNSName;
private final int port;
private SQLServerConnection con;

private static final UnsatisfiedLinkError linkError;

static int getMaxSSPIBlobSize() {
static int GetMaxSSPIBlobSize() {
return sspiBlobMaxlen;
}

Expand All @@ -62,28 +62,25 @@ static boolean isDllLoaded() {
// This is not re-thrown on purpose - the constructor will terminate the properly with the appropriate error
// string
} finally {
LINK_ERROR = temp;
linkError = temp;
}

}

AuthenticationJNI(SQLServerConnection con, String address, int serverport) throws SQLServerException {
if (!enabled)
con.terminate(SQLServerException.DRIVER_ERROR_NONE, SQLServerException.getErrString("R_notConfiguredForIntegrated"), LINK_ERROR);

String[] dns = new String[1];
if (GetDNSName(address, dns, authLogger) != 0) {
// Simply initialize the DNS to address
dns[0] = address;
}
con.terminate(SQLServerException.DRIVER_ERROR_NONE,
SQLServerException.getErrString("R_notConfiguredForIntegrated"), linkError);

this.con = con;
dnsName = dns[0];
DNSName = GetDNSName(address);
port = serverport;
}

static FedAuthDllInfo getAccessTokenForWindowsIntegrated(String stsURL, String servicePrincipalName, String clientConnectionId, String clientId, long expirationFileTime) throws DLLException {
FedAuthDllInfo dllInfo = ADALGetAccessTokenForWindowsIntegrated(stsURL, servicePrincipalName, clientConnectionId, clientId, expirationFileTime, authLogger);
static FedAuthDllInfo getAccessTokenForWindowsIntegrated(String stsURL, String servicePrincipalName,
String clientConnectionId, String clientId, long expirationFileTime) throws DLLException {
FedAuthDllInfo dllInfo = ADALGetAccessTokenForWindowsIntegrated(stsURL, servicePrincipalName,
clientConnectionId, clientId, expirationFileTime, authLogger);
return dllInfo;
}

Expand All @@ -92,28 +89,29 @@ byte[] GenerateClientContext(byte[] pin, boolean[] done) throws SQLServerExcepti
byte[] pOut;
int[] outsize; // This is where the size of the filled data returned
outsize = new int[1];
outsize[0] = getMaxSSPIBlobSize();
outsize[0] = GetMaxSSPIBlobSize();
pOut = new byte[outsize[0]];

// assert dnsName cant be null
assert dnsName != null;
// assert DNSName cant be null
assert DNSName != null;

int failure = SNISecGenClientContext(sniSec, sniSecLen, pin, pin.length, pOut, outsize, done, dnsName, port, null, null, authLogger);
int failure = SNISecGenClientContext(sniSec, sniSecLen, pin, pin.length, pOut, outsize, done, DNSName, port,
null, null, authLogger);

if (failure != 0) {
if (authLogger.isLoggable(Level.WARNING)) {
authLogger.warning(toString() + " Authentication failed code : " + failure);
}
con.terminate(SQLServerException.DRIVER_ERROR_NONE, SQLServerException.getErrString("R_integratedAuthenticationFailed"), LINK_ERROR);
con.terminate(SQLServerException.DRIVER_ERROR_NONE,
SQLServerException.getErrString("R_integratedAuthenticationFailed"), linkError);
}
// allocate space based on the size returned
byte[] output = new byte[outsize[0]];

byte output[] = new byte[outsize[0]];
System.arraycopy(pOut, 0, output, 0, outsize[0]);
return output;
}

int releaseClientContext() {
/* L0 */ int ReleaseClientContext() {
int success = 0;
if (sniSecLen[0] > 0) {
success = SNISecReleaseClientContext(sniSec, sniSecLen[0], authLogger);
Expand All @@ -122,18 +120,41 @@ int releaseClientContext() {
return success;
}

/*
* we use arrays of size one in many places to retrieve output values Java Integer objects are immutable so we cant use them to get the output sizes. Same for String
*/
native static byte[] DecryptColumnEncryptionKey(String masterKeyPath, String encryptionAlgorithm, byte[] encryptedColumnEncryptionKey) throws DLLException;
// note we handle the failures of the GetDNSName in this function, this function will return an empty string if the
// underlying call fails.
private static String GetDNSName(String address) {
String DNS[] = new String[1];
if (GetDNSName(address, DNS, authLogger) != 0) {
// Simply initialize the DNS to address
DNS[0] = address;
}
return DNS[0];
}

// we use arrays of size one in many places to retrieve output values
// Java Integer objects are immutable so we cant use them to get the output sizes.
// Same for String
/* L0 */private native static int SNISecGenClientContext(byte[] psec, int[] secptrsize, byte[] pin, int insize,
byte[] pOut, int[] outsize, boolean[] done, String servername, int port, String username, String password,
java.util.logging.Logger log);

/* L0 */ private native static int SNISecReleaseClientContext(byte[] psec, int secptrsize,
java.util.logging.Logger log);

private native static int SNISecInitPackage(int[] pcbMaxToken, java.util.logging.Logger log);

private native static int SNISecTerminatePackage(java.util.logging.Logger log);

private native static int SNISecGenClientContext(byte[] psec, int[] secptrsize, byte[] pin, int insize, byte[] pOut, int[] outsize, boolean[] done, String servername, int port, String username, String password, Logger log);
private native static int SNIGetSID(byte[] SID, java.util.logging.Logger log);

private native static int SNISecReleaseClientContext(byte[] psec, int secptrsize, Logger log);
private native static boolean SNIIsEqualToCurrentSID(byte[] SID, java.util.logging.Logger log);

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

private native static int GetDNSName(String address, String[] DNSName, Logger log);
private native static FedAuthDllInfo ADALGetAccessTokenForWindowsIntegrated(String stsURL,
String servicePrincipalName, String clientConnectionId, String clientId, long expirationFileTime,
java.util.logging.Logger log);

private native static FedAuthDllInfo ADALGetAccessTokenForWindowsIntegrated(String stsURL, String servicePrincipalName, String clientConnectionId, String clientId, long expirationFileTime, Logger log);
native static byte[] DecryptColumnEncryptionKey(String masterKeyPath, String encryptionAlgorithm,
byte[] encryptedColumnEncryptionKey) throws DLLException;
}
Loading