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

Add ActiveDirectoryServicePrincipalCertificate authentication #2128

Merged
merged 26 commits into from
May 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
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
8 changes: 6 additions & 2 deletions src/main/java/com/microsoft/sqlserver/jdbc/IOBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,18 @@ final class TDS {
static final int TDS_FEDAUTH_LIBRARY_SECURITYTOKEN = 0x01;
static final int TDS_FEDAUTH_LIBRARY_ADAL = 0x02;
static final int TDS_FEDAUTH_LIBRARY_RESERVED = 0x7F;

// workflows
static final byte ADALWORKFLOW_ACTIVEDIRECTORYPASSWORD = 0x01;
static final byte ADALWORKFLOW_ACTIVEDIRECTORYINTEGRATED = 0x02;
static final byte ADALWORKFLOW_ACTIVEDIRECTORYMANAGEDIDENTITY = 0x03;
static final byte ADALWORKFLOW_ACTIVEDIRECTORYINTERACTIVE = 0x03;
static final byte ADALWORKFLOW_ACTIVEDIRECTORYDEFAULT = 0x03;
static final byte ADALWORKFLOW_ACCESSTOKENCALLBACK = 0x03;
static final byte ADALWORKFLOW_ACTIVEDIRECTORYSERVICEPRINCIPAL = 0x01; // Using the Password byte as that is the
// closest we have.
// closest we have
static final byte ADALWORKFLOW_ACTIVEDIRECTORYSERVICEPRINCIPALCERTIFICATE = 0x01;

static final byte FEDAUTH_INFO_ID_STSURL = 0x01; // FedAuthInfoData is token endpoint URL from which to acquire fed
// auth token
static final byte FEDAUTH_INFO_ID_SPN = 0x02; // FedAuthInfoData is the SPN to use for acquiring fed auth token
Expand Down Expand Up @@ -1799,7 +1803,7 @@ else if (con.getTrustManagerClass() != null) {
if (logger.isLoggable(Level.FINEST))
logger.finest(toString() + " Getting TLS or better SSL context");

KeyManager[] km = (null != clientCertificate && clientCertificate.length() > 0) ? SQLServerCertificateUtils
KeyManager[] km = (null != clientCertificate && !clientCertificate.isEmpty()) ? SQLServerCertificateUtils
.getKeyManagerFromFile(clientCertificate, clientKey, clientKeyPassword) : null;

sslContext = SSLContext.getInstance(sslProtocol);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ static void validateServerCerticate(X509Certificate cert, String certFile) throw
if (!CertificateFactory.getInstance("X509").generateCertificate(is).getPublicKey()
.equals(cert.getPublicKey())) {
MessageFormat form = new MessageFormat(SQLServerException.getErrString("R_publicKeyMismatch"));
Object[] msgArgs = {certFile.toString()};
Object[] msgArgs = {certFile};
throw new CertificateException(form.format(msgArgs));
}
} catch (Exception e) {
Expand Down Expand Up @@ -325,20 +325,18 @@ private static void logSuccessMessage(String nameInCert, String hostName) {
private static final String RC4_ALG = "RC4";
private static final String RSA_ALG = "RSA";

private static KeyManager[] readPKCS12Certificate(String certPath,
String keyPassword) throws NoSuchAlgorithmException, CertificateException, IOException, UnrecoverableKeyException, KeyStoreException, SQLServerException {
KeyStore keystore = KeyStore.getInstance(PKCS12_ALG);
static KeyStore loadPKCS12KeyStore(String certPath,
String keyPassword) throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException, SQLServerException {
KeyStore keyStore = KeyStore.getInstance(PKCS12_ALG);
try (FileInputStream certStream = new FileInputStream(certPath)) {
keystore.load(certStream, keyPassword.toCharArray());
keyStore.load(certStream, (keyPassword != null) ? keyPassword.toCharArray() : null);
} catch (FileNotFoundException e) {
throw new SQLServerException(SQLServerException.getErrString("R_clientCertError"), null, 0, null);
throw new SQLServerException(SQLServerException.getErrString("R_readCertError"), null, 0, null);
}
KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(SUN_X_509);
keyManagerFactory.init(keystore, keyPassword.toCharArray());
return keyManagerFactory.getKeyManagers();
return keyStore;
}

private static KeyManager[] readPKCS8Certificate(String certPath, String keyPath,
static KeyManager[] readPKCS8Certificate(String certPath, String keyPath,
String keyPassword) throws IOException, GeneralSecurityException, SQLServerException {
Certificate clientCertificate = loadCertificate(certPath);
((X509Certificate) clientCertificate).checkValidity();
Expand All @@ -354,6 +352,16 @@ private static KeyManager[] readPKCS8Certificate(String certPath, String keyPath
return kmf.getKeyManagers();
}

private static KeyManager[] readPKCS12Certificate(String certPath,
String keyPassword) throws NoSuchAlgorithmException, CertificateException, IOException,
UnrecoverableKeyException, KeyStoreException, SQLServerException {

KeyStore keyStore = loadPKCS12KeyStore(certPath, keyPassword);
KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(SUN_X_509);
keyManagerFactory.init(keyStore, (keyPassword != null) ? keyPassword.toCharArray() : null);
return keyManagerFactory.getKeyManagers();
}

private static PrivateKey loadPrivateKeyFromPKCS8(
String key) throws NoSuchAlgorithmException, InvalidKeySpecException {
StringBuilder sb = new StringBuilder(key);
Expand Down Expand Up @@ -445,15 +453,15 @@ private static PrivateKey loadPrivateKeyFromPVK(String keyPath,
}
}

private static Certificate loadCertificate(
static Certificate loadCertificate(
String certificatePem) throws IOException, GeneralSecurityException, SQLServerException {
CertificateFactory certificateFactory = CertificateFactory.getInstance("X509");
try (InputStream certStream = fileToStream(certificatePem)) {
return certificateFactory.generateCertificate(certStream);
}
}

private static PrivateKey loadPrivateKey(String privateKeyPemPath,
static PrivateKey loadPrivateKey(String privateKeyPemPath,
String privateKeyPassword) throws GeneralSecurityException, IOException, SQLServerException {
String privateKeyPem = getStringFromFile(privateKeyPemPath);

Expand Down Expand Up @@ -517,7 +525,7 @@ private static InputStream fileToStream(String fname) throws IOException, SQLSer
dis.readFully(bytes);
return new ByteArrayInputStream(bytes);
} catch (FileNotFoundException e) {
throw new SQLServerException(SQLServerException.getErrString("R_clientCertError"), null, 0, null);
throw new SQLServerException(SQLServerException.getErrString("R_readCertError"), null, 0, null);
}
}

Expand Down
Loading