Skip to content
Merged
Show file tree
Hide file tree
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
8 changes: 5 additions & 3 deletions src/main/java/com/microsoft/sqlserver/jdbc/DDC.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import java.text.*;
import java.util.*;

import static java.nio.charset.StandardCharsets.US_ASCII;

/**
* Utility class for all Data Dependant Conversions (DDC).
*/
Expand Down Expand Up @@ -532,7 +534,7 @@ static final Object convertStringToObject(
case CHARACTER:
return new StringReader(stringVal);
case ASCII:
return new ByteArrayInputStream(stringVal.getBytes("US-ASCII"));
return new ByteArrayInputStream(stringVal.getBytes(US_ASCII));
case BINARY:
return new ByteArrayInputStream(stringVal.getBytes());

Expand Down Expand Up @@ -622,7 +624,7 @@ static final Object convertStreamToObject(
}
else
{
return new ByteArrayInputStream((new String(stream.getBytes(), typeInfo.getCharset())).getBytes("US-ASCII"));
return new ByteArrayInputStream((new String(stream.getBytes(), typeInfo.getCharset())).getBytes(US_ASCII));
}
}
else if (StreamType.CHARACTER == getterArgs.streamType ||
Expand Down Expand Up @@ -1357,7 +1359,7 @@ static AsciiFilteredUnicodeInputStream MakeAsciiFilteredUnicodeInputStream(BaseI
private AsciiFilteredUnicodeInputStream( Reader rd) throws SQLServerException
{
containedReader = rd;
asciiCharSet = Charset.forName("US-ASCII");
asciiCharSet = US_ASCII;
}

public void close() throws IOException
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@

package com.microsoft.sqlserver.jdbc;

import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.text.MessageFormat;

import static java.nio.charset.StandardCharsets.UTF_16LE;

/**
* Encryption key class which consist of following 4 keys :
* 1) root key - Main key which is used to derive following keys
Expand Down Expand Up @@ -81,7 +82,7 @@ class SQLServerAeadAes256CbcHmac256EncryptionKey extends SQLServerSymmetricKey
// By default Java is big endian, we are getting bytes in little endian(LE in UTF-16LE)
// to make it compatible with C# driver which is little endian
encKeyBuff = SQLServerSecurityUtility.getHMACWithSHA256(
encryptionKeySaltFormat.getBytes("UTF-16LE"),
encryptionKeySaltFormat.getBytes(UTF_16LE),
rootKey,
encKeyBuff.length);

Expand All @@ -90,7 +91,7 @@ class SQLServerAeadAes256CbcHmac256EncryptionKey extends SQLServerSymmetricKey
// Derive mac key from root key
byte[] macKeyBuff = new byte[keySizeInBytes];
macKeyBuff = SQLServerSecurityUtility.getHMACWithSHA256(
macKeySaltFormat.getBytes("UTF-16LE"),
macKeySaltFormat.getBytes(UTF_16LE),
rootKey,
macKeyBuff.length);

Expand All @@ -99,18 +100,11 @@ class SQLServerAeadAes256CbcHmac256EncryptionKey extends SQLServerSymmetricKey
// Derive the initialization vector from root key
byte[] ivKeyBuff = new byte[keySizeInBytes];
ivKeyBuff = SQLServerSecurityUtility.getHMACWithSHA256(
ivKeySaltFormat.getBytes("UTF-16LE"),
ivKeySaltFormat.getBytes(UTF_16LE),
rootKey,
ivKeyBuff.length);
ivKey = new SQLServerSymmetricKey(ivKeyBuff);
}
catch (UnsupportedEncodingException e)
{
MessageFormat form = new MessageFormat(
SQLServerException.getErrString("R_unsupportedEncoding"));
Object[] msgArgs = { "UTF-16LE" };
throw new SQLServerException(this, form.format(msgArgs), null, 0, false);
}
catch (InvalidKeyException | NoSuchAlgorithmException e)
{
MessageFormat form = new MessageFormat(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,13 @@

package com.microsoft.sqlserver.jdbc;

import java.io.UnsupportedEncodingException;
import java.sql.SQLException;
import java.text.MessageFormat;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import javax.xml.bind.DatatypeConverter;

import static java.nio.charset.StandardCharsets.UTF_8;

/**
* Factory for SQLServerAeadAes256CbcHmac256Algorithm
*/
Expand All @@ -49,40 +48,33 @@ SQLServerEncryptionAlgorithm create(
}
String factoryKey="";

try {
StringBuffer factoryKeyBuilder=new StringBuffer();
factoryKeyBuilder.append(
DatatypeConverter.printBase64Binary(
new String(
columnEncryptionKey.getRootKey(),
"UTF-8"
).getBytes()
)
);
StringBuffer factoryKeyBuilder=new StringBuffer();
factoryKeyBuilder.append(
DatatypeConverter.printBase64Binary(
new String(
columnEncryptionKey.getRootKey(),
UTF_8
).getBytes()
)
);

factoryKeyBuilder.append(":");
factoryKeyBuilder.append(encryptionType);
factoryKeyBuilder.append(":");
factoryKeyBuilder.append(algorithmVersion);
factoryKey =factoryKeyBuilder.toString();
SQLServerAeadAes256CbcHmac256Algorithm aesAlgorithm;
if(!encryptionAlgorithms.containsKey(factoryKey)){
SQLServerAeadAes256CbcHmac256EncryptionKey encryptedKey = new SQLServerAeadAes256CbcHmac256EncryptionKey(columnEncryptionKey.getRootKey(), SQLServerAeadAes256CbcHmac256Algorithm.algorithmName);
aesAlgorithm = new SQLServerAeadAes256CbcHmac256Algorithm(encryptedKey, encryptionType, algorithmVersion);
encryptionAlgorithms.putIfAbsent(factoryKey, aesAlgorithm);
}
factoryKeyBuilder.append(":");
factoryKeyBuilder.append(encryptionType);
factoryKeyBuilder.append(":");
factoryKeyBuilder.append(algorithmVersion);

factoryKey =factoryKeyBuilder.toString();

SQLServerAeadAes256CbcHmac256Algorithm aesAlgorithm;

if(!encryptionAlgorithms.containsKey(factoryKey)){
SQLServerAeadAes256CbcHmac256EncryptionKey encryptedKey = new SQLServerAeadAes256CbcHmac256EncryptionKey(columnEncryptionKey.getRootKey(), SQLServerAeadAes256CbcHmac256Algorithm.algorithmName);
aesAlgorithm = new SQLServerAeadAes256CbcHmac256Algorithm(encryptedKey, encryptionType, algorithmVersion);
encryptionAlgorithms.putIfAbsent(factoryKey, aesAlgorithm);
}



} catch (UnsupportedEncodingException e) {
MessageFormat form = new MessageFormat(
SQLServerException.getErrString("R_unsupportedEncoding"));
Object[] msgArgs = { "UTF-8" };
throw new SQLServerException(this, form.format(msgArgs), null, 0, false);
}
return encryptionAlgorithms.get(factoryKey);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import java.math.BigDecimal;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.charset.Charset;
import java.sql.Connection;
import java.sql.Date;
import java.sql.ResultSet;
Expand All @@ -54,6 +53,9 @@
import java.util.Vector;
import java.util.logging.Level;

import static java.nio.charset.StandardCharsets.UTF_16LE;
import static java.nio.charset.StandardCharsets.UTF_8;

import javax.sql.RowSet;

import microsoft.sql.DateTimeOffset;
Expand Down Expand Up @@ -3524,7 +3526,7 @@ private byte[] normalizedValue(JDBCType destJdbcType, Object value, JDBCType src
Object[] msgArgs = { srcJdbcType, destJdbcType };
throw new SQLServerException(this, form.format(msgArgs), null, 0, false);
}
return ((String)value).getBytes(Charset.forName("UTF-8"));
return ((String)value).getBytes(UTF_8);

case NCHAR:
case NVARCHAR:
Expand All @@ -3536,7 +3538,7 @@ private byte[] normalizedValue(JDBCType destJdbcType, Object value, JDBCType src
Object[] msgArgs = { srcJdbcType, destJdbcType };
throw new SQLServerException(this, form.format(msgArgs), null, 0, false);
}
return ((String)value).getBytes(Charset.forName("UTF-16LE"));
return ((String)value).getBytes(UTF_16LE);

case REAL:
case FLOAT:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;

import static java.nio.charset.StandardCharsets.UTF_16LE;

import org.apache.http.impl.client.HttpClientBuilder;
import com.microsoft.azure.keyvault.KeyVaultClient;
import com.microsoft.azure.keyvault.KeyVaultClientImpl;
Expand Down Expand Up @@ -284,15 +286,7 @@ public byte[] encryptColumnEncryptionKey(String masterKeyPath, String encryption
byte[] version = new byte[] { firstVersion[0] };

// Get the Unicode encoded bytes of cultureinvariant lower case masterKeyPath
byte[] masterKeyPathBytes = null;
try {
masterKeyPathBytes = masterKeyPath.toLowerCase().getBytes("UTF-16LE");
} catch (UnsupportedEncodingException e) {
MessageFormat form = new MessageFormat(
SQLServerException.getErrString("R_unsupportedEncoding"));
Object[] msgArgs = { "UTF-16LE" };
throw new SQLServerException(this, form.format(msgArgs), null, 0, false);
}
byte[] masterKeyPathBytes = masterKeyPath.toLowerCase().getBytes(UTF_16LE);

byte[] keyPathLength = new byte[2];
keyPathLength[0] = (byte)(((short)masterKeyPathBytes.length) & 0xff);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.security.InvalidKeyException;
Expand All @@ -46,6 +45,8 @@

import com.microsoft.sqlserver.jdbc.KeyStoreProviderCommon;

import static java.nio.charset.StandardCharsets.UTF_16LE;

public class SQLServerColumnEncryptionJavaKeyStoreProvider extends SQLServerColumnEncryptionKeyStoreProvider
{
String name = "MSSQL_JAVA_KEYSTORE";
Expand Down Expand Up @@ -254,15 +255,7 @@ else if (0==plainTextColumnEncryptionKey.length) {
CertificateDetails certificateDetails = getCertificateDetails(masterKeyPath);
byte [] cipherText=encryptRSAOAEP(plainTextColumnEncryptionKey, certificateDetails);
byte[] cipherTextLength=getLittleEndianBytesFromShort((short)cipherText.length);
byte[] masterKeyPathBytes;

try {
masterKeyPathBytes = masterKeyPath.toLowerCase().getBytes(
"UTF-16LE");
} catch (UnsupportedEncodingException e) {
MessageFormat form = new MessageFormat(SQLServerException.getErrString("R_unsupportedEncoding"));
throw new SQLServerException(form.format(new Object[] {"UTF-16LE"}), null, 0, null);
}
byte[] masterKeyPathBytes = masterKeyPath.toLowerCase().getBytes(UTF_16LE);

byte[] keyPathLength=getLittleEndianBytesFromShort((short)masterKeyPathBytes.length);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;

import static java.nio.charset.StandardCharsets.UTF_16LE;

/**
* SQLServerConnection implements a JDBC connection to SQL Server.
* SQLServerConnections support JDBC connection pooling and may be either physical JDBC connections
Expand Down Expand Up @@ -1234,14 +1236,7 @@ Connection connectInternal(Properties propsIn, SQLServerPooledConnection pooledC
sPropValue = activeConnectionProperties.getProperty(sPropKey);
if (null != sPropValue)
{
try {
accessTokenInByte = sPropValue.getBytes("UTF-16LE");
} catch (UnsupportedEncodingException e) {
MessageFormat form = new MessageFormat(
SQLServerException.getErrString("R_unsupportedEncoding"));
Object[] msgArgs = { "UTF-16LE" };
throw new SQLServerException(this, form.format(msgArgs), null, 0, false);
}
accessTokenInByte = sPropValue.getBytes(UTF_16LE);
}

if((null != accessTokenInByte) && 0 == accessTokenInByte.length)
Expand Down Expand Up @@ -3552,13 +3547,7 @@ final void processFedAuthInfo(TDSReader tdsReader, TDSTokenHandler tdsTokenHandl
try {
byte[] dataArray = new byte[dataLen];
System.arraycopy(tokenData, dataOffset, dataArray, 0, dataLen);
data = new String(dataArray, "UTF-16LE");
}
catch(UnsupportedEncodingException e){
MessageFormat form = new MessageFormat(
SQLServerException.getErrString("R_unsupportedEncoding"));
Object[] msgArgs = { "UTF-16LE" };
throw new SQLServerException(this, form.format(msgArgs), null, 0, false);
data = new String(dataArray, UTF_16LE);
}
catch(Exception e){
connectionlogger.severe(toString() + "Failed to read FedAuthInfoData.");
Expand Down Expand Up @@ -3724,16 +3713,7 @@ else if(authenticationString.trim().equalsIgnoreCase(SqlAuthentication.ActiveDir

byte[] accessTokenFromDLL = dllInfo.accessTokenBytes;

String accessToken = null;
try {
accessToken = new String(accessTokenFromDLL, "UTF-16LE");
}
catch (UnsupportedEncodingException e) {
MessageFormat form = new MessageFormat(
SQLServerException.getErrString("R_unsupportedEncoding"));
Object[] msgArgs = { "UTF-16LE" };
throw new SQLServerException(null, form.format(msgArgs), null, 0, false);
}
String accessToken = new String(accessTokenFromDLL, UTF_16LE);

SqlFedAuthToken fedAuthToken = new SqlFedAuthToken(accessToken, dllInfo.expiresIn);

Expand All @@ -3754,15 +3734,7 @@ private void sendFedAuthToken(FedAuthTokenCommand fedAuthCommand, SqlFedAuthToke

TDSWriter tdsWriter = fedAuthCommand.startRequest(TDS.PKT_FEDAUTH_TOKEN_MESSAGE);

byte[] accessToken = null;
try {
accessToken = fedAuthToken.accessToken.getBytes("UTF-16LE");
} catch (UnsupportedEncodingException e) {
MessageFormat form = new MessageFormat(
SQLServerException.getErrString("R_unsupportedEncoding"));
Object[] msgArgs = { "UTF-16LE" };
throw new SQLServerException(this, form.format(msgArgs), null, 0, false);
}
byte[] accessToken = fedAuthToken.accessToken.getBytes(UTF_16LE);

// Send total length (length of token plus 4 bytes for the token length field)
// If we were sending a nonce, this would include that length as well
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@

package com.microsoft.sqlserver.jdbc;

import java.io.UnsupportedEncodingException;
import java.text.MessageFormat;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadFactory;

import static java.nio.charset.StandardCharsets.UTF_8;

import static java.util.concurrent.TimeUnit.*;

import javax.xml.bind.DatatypeConverter;
Expand Down Expand Up @@ -123,16 +124,7 @@ SQLServerSymmetricKey getKey(EncryptionKeyInfo keyInfo, SQLServerConnection conn
String keyLookupValue=null;
keyLookupValuebuffer.append(":");

try
{
keyLookupValuebuffer.append(DatatypeConverter.printBase64Binary((new String(keyInfo.encryptedKey,"UTF-8")).getBytes()));
}
catch (UnsupportedEncodingException e)
{
MessageFormat form = new MessageFormat(SQLServerException.getErrString("R_unsupportedEncoding"));
Object[] msgArgs = { "UTF-8" };
throw new SQLServerException(this, form.format(msgArgs), null, 0, false);
}
keyLookupValuebuffer.append(DatatypeConverter.printBase64Binary((new String(keyInfo.encryptedKey,UTF_8)).getBytes()));

keyLookupValuebuffer.append(":");
keyLookupValuebuffer.append(keyInfo.keyStoreName);
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/com/microsoft/sqlserver/jdbc/dtv.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@
import java.math.*;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.charset.Charset;
import java.sql.*;
import java.text.MessageFormat;
import java.time.*;

import static java.nio.charset.StandardCharsets.UTF_16LE;

import com.microsoft.sqlserver.jdbc.JavaType.SetterConversionAE;


Expand Down Expand Up @@ -1911,7 +1912,7 @@ else if ((JDBCType.NVARCHAR == jdbcTypeSetByUser)
// Each character is represented using 2 bytes in NVARCHAR
else if((JDBCType.NVARCHAR == jdbcTypeSetByUser) || (JDBCType.NCHAR == jdbcTypeSetByUser) || (JDBCType.LONGNVARCHAR == jdbcTypeSetByUser))
{
byteValue = ((String)value).getBytes(Charset.forName("UTF-16LE"));
byteValue = ((String)value).getBytes(UTF_16LE);
}
// Each character is represented using 1 bytes in VARCHAR
else if((JDBCType.VARCHAR == jdbcTypeSetByUser) || (JDBCType.CHAR == jdbcTypeSetByUser) || (JDBCType.LONGVARCHAR == jdbcTypeSetByUser))
Expand Down