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

Revert "removing javax.xml.bind package dependency " #498

Merged
merged 1 commit into from
Sep 21, 2017
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
10 changes: 7 additions & 3 deletions src/main/java/com/microsoft/sqlserver/jdbc/IOBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import javax.net.ssl.X509TrustManager;
import javax.xml.bind.DatatypeConverter;

final class TDS {
// TDS protocol versions
Expand Down Expand Up @@ -4955,7 +4956,7 @@ else if (DataTypes.UNKNOWN_STREAM_LENGTH == dataLength)
isShortValue = columnPair.getValue().precision <= DataTypes.SHORT_VARTYPE_MAX_BYTES;
isNull = (null == currentObject);
if (currentObject instanceof String)
dataLength = isNull ? 0 : (ParameterUtils.HexToBin(currentObject.toString())).length;
dataLength = isNull ? 0 : (toByteArray(currentObject.toString())).length;
else
dataLength = isNull ? 0 : ((byte[]) currentObject).length;
if (!isShortValue) {
Expand All @@ -4974,7 +4975,7 @@ else if (DataTypes.UNKNOWN_STREAM_LENGTH == dataLength)
if (dataLength > 0) {
writeInt(dataLength);
if (currentObject instanceof String)
writeBytes(ParameterUtils.HexToBin(currentObject.toString()));
writeBytes(toByteArray(currentObject.toString()));
else
writeBytes((byte[]) currentObject);
}
Expand All @@ -4988,7 +4989,7 @@ else if (DataTypes.UNKNOWN_STREAM_LENGTH == dataLength)
else {
writeShort((short) dataLength);
if (currentObject instanceof String)
writeBytes(ParameterUtils.HexToBin(currentObject.toString()));
writeBytes(toByteArray(currentObject.toString()));
else
writeBytes((byte[]) currentObject);
}
Expand Down Expand Up @@ -5025,6 +5026,9 @@ private void writeTVPSqlVariantHeader(int length,
writeByte(probBytes);
}

private static byte[] toByteArray(String s) {
return DatatypeConverter.parseHexBinary(s);
}

void writeTVPColumnMetaData(TVP value) throws SQLServerException {
boolean isShortValue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
import static java.nio.charset.StandardCharsets.UTF_8;

import java.text.MessageFormat;
import java.util.Base64;
import java.util.concurrent.ConcurrentHashMap;

import javax.xml.bind.DatatypeConverter;

/**
* Factory for SQLServerAeadAes256CbcHmac256Algorithm
Expand All @@ -38,7 +38,7 @@ SQLServerEncryptionAlgorithm create(SQLServerSymmetricKey columnEncryptionKey,
}

StringBuilder factoryKeyBuilder = new StringBuilder();
factoryKeyBuilder.append(Base64.getEncoder().encodeToString(new String(columnEncryptionKey.getRootKey(), UTF_8).getBytes()));
factoryKeyBuilder.append(DatatypeConverter.printBase64Binary(new String(columnEncryptionKey.getRootKey(), UTF_8).getBytes()));

factoryKeyBuilder.append(":");
factoryKeyBuilder.append(encryptionType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.Enumeration;
import java.util.Locale;

import javax.xml.bind.DatatypeConverter;

/**
* The implementation of the key store provider for the Windows Certificate Store. This class enables using keys stored in the Windows Certificate
Expand Down Expand Up @@ -134,7 +135,7 @@ private String getThumbPrint(X509Certificate cert) throws NoSuchAlgorithmExcepti
byte[] der = cert.getEncoded();
md.update(der);
byte[] digest = md.digest();
return Util.bytesToHexString(digest, digest.length);
return DatatypeConverter.printHexBinary(digest);
}

private CertificateDetails getCertificateByThumbprint(String storeLocation,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
import static java.util.concurrent.TimeUnit.SECONDS;

import java.text.MessageFormat;
import java.util.Base64;
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 javax.xml.bind.DatatypeConverter;

class CacheClear implements Runnable {

Expand Down Expand Up @@ -98,7 +98,7 @@ SQLServerSymmetricKey getKey(EncryptionKeyInfo keyInfo,
String keyLookupValue;
keyLookupValuebuffer.append(":");

keyLookupValuebuffer.append(Base64.getEncoder().encodeToString((new String(keyInfo.encryptedKey, UTF_8)).getBytes()));
keyLookupValuebuffer.append(DatatypeConverter.printBase64Binary((new String(keyInfo.encryptedKey, UTF_8)).getBytes()));

keyLookupValuebuffer.append(":");
keyLookupValuebuffer.append(keyInfo.keyStoreName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.LinkedList;
import java.util.Properties;

import javax.xml.bind.DatatypeConverter;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
Expand Down Expand Up @@ -718,7 +719,7 @@ private static void createCEK(SQLServerColumnEncryptionKeyStoreProvider storePro
String cekSql = null;
byte[] key = storeProvider.encryptColumnEncryptionKey(javaKeyAliases, "RSA_OAEP", valuesDefault);
cekSql = "CREATE COLUMN ENCRYPTION KEY " + cekName + " WITH VALUES " + "(COLUMN_MASTER_KEY = " + cmkName
+ ", ALGORITHM = 'RSA_OAEP', ENCRYPTED_VALUE = 0x" + Util.bytesToHexString(key, key.length) + ")" + ";";
+ ", ALGORITHM = 'RSA_OAEP', ENCRYPTED_VALUE = 0x" + DatatypeConverter.printHexBinary(key) + ")" + ";";
stmt.execute(cekSql);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,13 @@
import java.util.Arrays;
import java.util.Random;

import javax.xml.bind.DatatypeConverter;

import org.junit.jupiter.api.Test;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.runner.RunWith;

import com.microsoft.sqlserver.jdbc.SQLServerException;
import com.microsoft.sqlserver.testframework.AbstractTest;
import com.microsoft.sqlserver.testframework.util.Util;

/**
* This test validates PR #342. In this PR, DatatypeConverter#parseHexBinary is replaced with type casting. This tests validates if the behavior
Expand All @@ -39,13 +38,12 @@ public class DriverVersionTest extends AbstractTest {

/**
* validates version byte array generated by the original method and type casting reminds the same.
* @throws SQLServerException
*/
@Test
public void testConnectionDriver() throws SQLServerException {
public void testConnectionDriver() {
// the original way to create version byte array
String interfaceLibVersion = generateInterfaceLibVersion();
byte originalVersionBytes[] = Util.hexStringToByte(interfaceLibVersion);
byte originalVersionBytes[] = DatatypeConverter.parseHexBinary(interfaceLibVersion);

String originalBytes = Arrays.toString(originalVersionBytes);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;

import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -42,18 +40,9 @@ public void testWarnings() throws SQLException {
}
conn.setClientInfo(info2);
warn = conn.getWarnings();
for (int i = 0; i < 5; i++) {
boolean found = false;
List<String> list = Arrays.asList(infoArray);
for (String word : list) {
if (warn.toString().contains(word)) {
found = true;
break;
}
}
assertTrue(found, "warning : '" + warn.toString() + "' not found!");
for (int i = 4; i >= 0; i--) {
assertTrue(warn.toString().contains(infoArray[i]), "Warnings not found!");
warn = warn.getNextWarning();
found = false;
}
conn.clearWarnings();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,8 @@ public void testSetup() throws TestAbortedException, Exception {
}

@AfterAll
public static void terminateVariation() throws TestAbortedException, Exception {
public static void terminateVariation() throws SQLException {

assumeTrue(13 <= new DBConnection(connectionString).getServerVersion(),
"Aborting test case as SQL Server version is not compatible with Always encrypted ");
SQLServerStatement stmt = (SQLServerStatement) connection.createStatement();
Utils.dropTableIfExists("esimple", stmt);

Expand Down
69 changes: 0 additions & 69 deletions src/test/java/com/microsoft/sqlserver/testframework/util/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

import com.microsoft.sqlserver.jdbc.SQLServerConnection;
import com.microsoft.sqlserver.jdbc.SQLServerDatabaseMetaData;
import com.microsoft.sqlserver.jdbc.SQLServerException;
import com.microsoft.sqlserver.jdbc.SQLServerStatementColumnEncryptionSetting;

/**
Expand Down Expand Up @@ -290,72 +289,4 @@ public static boolean supportJDBC42(Connection con) throws SQLException {
SQLServerDatabaseMetaData meta = (SQLServerDatabaseMetaData) con.getMetaData();
return (meta.getJDBCMajorVersion() >= 4 && meta.getJDBCMinorVersion() >= 2);
}

/**
*
* @param b
* byte value
* @param length
* length of the array
* @return
*/
final static char[] hexChars = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};

public static String bytesToHexString(byte[] b,
int length) {
StringBuilder sb = new StringBuilder(length * 2);
for (int i = 0; i < length; i++) {
int hexVal = b[i] & 0xFF;
sb.append(hexChars[(hexVal & 0xF0) >> 4]);
sb.append(hexChars[(hexVal & 0x0F)]);
}
return sb.toString();
}


/**
* conversion routine valid values 0-9 a-f A-F throws exception when failed to convert
*
* @param value
* charArray
* @return
* @throws SQLServerException
*/
static byte CharToHex(char value) throws SQLServerException {
byte ret = 0;
if (value >= 'A' && value <= 'F') {
ret = (byte) (value - 'A' + 10);
}
else if (value >= 'a' && value <= 'f') {
ret = (byte) (value - 'a' + 10);
}
else if (value >= '0' && value <= '9') {
ret = (byte) (value - '0');
}
else {
throw new IllegalArgumentException("The string is not in a valid hex format. ");
}
return ret;
}

/**
* Converts a string to an array of bytes
*
* @param hexV
* a hexized string representation of bytes
* @return
* @throws SQLServerException
*/
public static byte[] hexStringToByte(String hexV) throws SQLServerException {
int len = hexV.length();
char orig[] = hexV.toCharArray();
if ((len % 2) != 0) {
throw new IllegalArgumentException("The string is not in a valid hex format: " + hexV);
}
byte[] bin = new byte[len / 2];
for (int i = 0; i < len / 2; i++) {
bin[i] = (byte) ((CharToHex(orig[2 * i]) << 4) + CharToHex(orig[2 * i + 1]));
}
return bin;
}
}