forked from microsoft/mssql-jdbc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSQLServerColumnEncryptionJavaKeyStoreProvider.java
328 lines (265 loc) · 13.9 KB
/
SQLServerColumnEncryptionJavaKeyStoreProvider.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/*
* 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 static java.nio.charset.StandardCharsets.UTF_16LE;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.Signature;
import java.security.SignatureException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.text.MessageFormat;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
/**
*
* The implementation of the key store provider for Java Key Store. This class enables using certificates stored in the Java keystore as column master
* keys.
*
*/
public class SQLServerColumnEncryptionJavaKeyStoreProvider extends SQLServerColumnEncryptionKeyStoreProvider {
String name = "MSSQL_JAVA_KEYSTORE";
String keyStorePath = null;
char[] keyStorePwd = null;
static final private java.util.logging.Logger javaKeyStoreLogger = java.util.logging.Logger
.getLogger("com.microsoft.sqlserver.jdbc.SQLServerColumnEncryptionJavaKeyStoreProvider");
public void setName(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
/**
* Key store provider for the Java Key Store.
*
* @param keyStoreLocation
* specifies the location of the keystore
* @param keyStoreSecret
* specifies the secret used for keystore
* @throws SQLServerException
* when an error occurs
*/
public SQLServerColumnEncryptionJavaKeyStoreProvider(String keyStoreLocation,
char[] keyStoreSecret) throws SQLServerException {
javaKeyStoreLogger.entering(SQLServerColumnEncryptionJavaKeyStoreProvider.class.getName(), "SQLServerColumnEncryptionJavaKeyStoreProvider");
if ((null == keyStoreLocation) || (0 == keyStoreLocation.length())) {
MessageFormat form = new MessageFormat(SQLServerException.getErrString("R_InvalidConnectionSetting"));
Object[] msgArgs = {"keyStoreLocation", keyStoreLocation};
throw new SQLServerException(form.format(msgArgs), null);
}
this.keyStorePath = keyStoreLocation;
if (javaKeyStoreLogger.isLoggable(java.util.logging.Level.FINE)) {
javaKeyStoreLogger.fine("Path of key store provider is set.");
}
// Password can be null or empty, PKCS12 type allows that.
if (null == keyStoreSecret) {
keyStoreSecret = "".toCharArray();
}
this.keyStorePwd = new char[keyStoreSecret.length];
System.arraycopy(keyStoreSecret, 0, this.keyStorePwd, 0, keyStoreSecret.length);
if (javaKeyStoreLogger.isLoggable(java.util.logging.Level.FINE)) {
javaKeyStoreLogger.fine("Password for key store provider is set.");
}
javaKeyStoreLogger.exiting(SQLServerColumnEncryptionJavaKeyStoreProvider.class.getName(), "SQLServerColumnEncryptionJavaKeyStoreProvider");
}
@Override
public byte[] decryptColumnEncryptionKey(String masterKeyPath,
String encryptionAlgorithm,
byte[] encryptedColumnEncryptionKey) throws SQLServerException {
javaKeyStoreLogger.entering(SQLServerColumnEncryptionJavaKeyStoreProvider.class.getName(), "decryptColumnEncryptionKey",
"Decrypting Column Encryption Key.");
KeyStoreProviderCommon.validateNonEmptyMasterKeyPath(masterKeyPath);
CertificateDetails certificateDetails = getCertificateDetails(masterKeyPath);
byte[] plainCEK = KeyStoreProviderCommon.decryptColumnEncryptionKey(masterKeyPath, encryptionAlgorithm, encryptedColumnEncryptionKey,
certificateDetails);
javaKeyStoreLogger.exiting(SQLServerColumnEncryptionJavaKeyStoreProvider.class.getName(), "decryptColumnEncryptionKey",
"Finished decrypting Column Encryption Key.");
return plainCEK;
}
private CertificateDetails getCertificateDetails(String masterKeyPath) throws SQLServerException {
FileInputStream fis = null;
KeyStore keyStore = null;
CertificateDetails certificateDetails = null;
try {
if (null == masterKeyPath || 0 == masterKeyPath.length()) {
throw new SQLServerException(null, SQLServerException.getErrString("R_InvalidMasterKeyDetails"), null, 0, false);
}
try {
// Try to load JKS first, if fails try PKCS12
keyStore = KeyStore.getInstance("JKS");
fis = new FileInputStream(keyStorePath);
keyStore.load(fis, keyStorePwd);
}
catch (IOException e) {
if (null != fis)
fis.close();
// Loading as JKS failed, try to load as PKCS12
keyStore = KeyStore.getInstance("PKCS12");
fis = new FileInputStream(keyStorePath);
keyStore.load(fis, keyStorePwd);
}
certificateDetails = getCertificateDetailsByAlias(keyStore, masterKeyPath);
}
catch (FileNotFoundException fileNotFound) {
throw new SQLServerException(this, SQLServerException.getErrString("R_KeyStoreNotFound"), null, 0, false);
}
catch (IOException | CertificateException | NoSuchAlgorithmException | KeyStoreException e) {
MessageFormat form = new MessageFormat(SQLServerException.getErrString("R_invalidKeyStoreFile"));
Object[] msgArgs = {keyStorePath};
throw new SQLServerException(form.format(msgArgs), e);
}
finally {
try {
if (null != fis)
fis.close();
}
// Ignore the exception as we are cleaning up.
catch (IOException e) {
}
}
return certificateDetails;
}
private CertificateDetails getCertificateDetailsByAlias(KeyStore keyStore,
String alias) throws SQLServerException {
try {
X509Certificate publicCertificate = (X509Certificate) keyStore.getCertificate(alias);
Key keyPrivate = keyStore.getKey(alias, keyStorePwd);
if (null == publicCertificate) {
// Certificate not found. Throw an exception.
MessageFormat form = new MessageFormat(SQLServerException.getErrString("R_CertificateNotFoundForAlias"));
Object[] msgArgs = {alias, "MSSQL_JAVA_KEYSTORE"};
throw new SQLServerException(this, form.format(msgArgs), null, 0, false);
}
// found certificate but corresponding private key not found, throw exception
if (null == keyPrivate) {
throw new UnrecoverableKeyException();
}
return new CertificateDetails(publicCertificate, keyPrivate);
}
catch (UnrecoverableKeyException unrecoverableKeyException) {
MessageFormat form = new MessageFormat(SQLServerException.getErrString("R_UnrecoverableKeyAE"));
Object[] msgArgs = {alias};
throw new SQLServerException(this, form.format(msgArgs), null, 0, false);
}
catch (NoSuchAlgorithmException | KeyStoreException e) {
MessageFormat form = new MessageFormat(SQLServerException.getErrString("R_CertificateError"));
Object[] msgArgs = {alias, name};
throw new SQLServerException(form.format(msgArgs), e);
}
}
@Override
public byte[] encryptColumnEncryptionKey(String masterKeyPath,
String encryptionAlgorithm,
byte[] plainTextColumnEncryptionKey) throws SQLServerException {
javaKeyStoreLogger.entering(SQLServerColumnEncryptionJavaKeyStoreProvider.class.getName(),
Thread.currentThread().getStackTrace()[1].getMethodName(), "Encrypting Column Encryption Key.");
byte[] version = KeyStoreProviderCommon.version;
KeyStoreProviderCommon.validateNonEmptyMasterKeyPath(masterKeyPath);
if (null == plainTextColumnEncryptionKey) {
throw new SQLServerException(null, SQLServerException.getErrString("R_NullColumnEncryptionKey"), null, 0, false);
}
else if (0 == plainTextColumnEncryptionKey.length) {
throw new SQLServerException(null, SQLServerException.getErrString("R_EmptyColumnEncryptionKey"), null, 0, false);
}
KeyStoreProviderCommon.validateEncryptionAlgorithm(encryptionAlgorithm, true);
CertificateDetails certificateDetails = getCertificateDetails(masterKeyPath);
byte[] cipherText = encryptRSAOAEP(plainTextColumnEncryptionKey, certificateDetails);
byte[] cipherTextLength = getLittleEndianBytesFromShort((short) cipherText.length);
byte[] masterKeyPathBytes = masterKeyPath.toLowerCase().getBytes(UTF_16LE);
byte[] keyPathLength = getLittleEndianBytesFromShort((short) masterKeyPathBytes.length);
byte[] dataToSign = new byte[version.length + keyPathLength.length + cipherTextLength.length + masterKeyPathBytes.length + cipherText.length];
int destinationPosition = version.length;
System.arraycopy(version, 0, dataToSign, 0, version.length);
System.arraycopy(keyPathLength, 0, dataToSign, destinationPosition, keyPathLength.length);
destinationPosition += keyPathLength.length;
System.arraycopy(cipherTextLength, 0, dataToSign, destinationPosition, cipherTextLength.length);
destinationPosition += cipherTextLength.length;
System.arraycopy(masterKeyPathBytes, 0, dataToSign, destinationPosition, masterKeyPathBytes.length);
destinationPosition += masterKeyPathBytes.length;
System.arraycopy(cipherText, 0, dataToSign, destinationPosition, cipherText.length);
byte[] signedHash = rsaSignHashedData(dataToSign, certificateDetails);
int encryptedColumnEncryptionKeyLength = version.length + cipherTextLength.length + keyPathLength.length + cipherText.length
+ masterKeyPathBytes.length + signedHash.length;
byte[] encryptedColumnEncryptionKey = new byte[encryptedColumnEncryptionKeyLength];
int currentIndex = 0;
System.arraycopy(version, 0, encryptedColumnEncryptionKey, currentIndex, version.length);
currentIndex += version.length;
System.arraycopy(keyPathLength, 0, encryptedColumnEncryptionKey, currentIndex, keyPathLength.length);
currentIndex += keyPathLength.length;
System.arraycopy(cipherTextLength, 0, encryptedColumnEncryptionKey, currentIndex, cipherTextLength.length);
currentIndex += cipherTextLength.length;
System.arraycopy(masterKeyPathBytes, 0, encryptedColumnEncryptionKey, currentIndex, masterKeyPathBytes.length);
currentIndex += masterKeyPathBytes.length;
System.arraycopy(cipherText, 0, encryptedColumnEncryptionKey, currentIndex, cipherText.length);
currentIndex += cipherText.length;
System.arraycopy(signedHash, 0, encryptedColumnEncryptionKey, currentIndex, signedHash.length);
javaKeyStoreLogger.exiting(SQLServerColumnEncryptionJavaKeyStoreProvider.class.getName(),
Thread.currentThread().getStackTrace()[1].getMethodName(), "Finished encrypting Column Encryption Key.");
return encryptedColumnEncryptionKey;
}
/**
* Encrypt plainText with the certificate provided
*
* @param plainText
* plain CEK to be encrypted
* @param certificateDetails
* @return encrypted CEK
* @throws SQLServerException
*/
private byte[] encryptRSAOAEP(byte[] plainText,
CertificateDetails certificateDetails) throws SQLServerException {
byte[] cipherText = null;
try {
Cipher rsa = Cipher.getInstance("RSA/ECB/OAEPWithSHA-1AndMGF1Padding");
rsa.init(Cipher.ENCRYPT_MODE, certificateDetails.certificate.getPublicKey());
rsa.update(plainText);
cipherText = rsa.doFinal();
}
catch (InvalidKeyException | NoSuchAlgorithmException | IllegalBlockSizeException | NoSuchPaddingException | BadPaddingException e) {
MessageFormat form = new MessageFormat(SQLServerException.getErrString("R_EncryptionFailed"));
Object[] msgArgs = {e.getMessage()};
throw new SQLServerException(this, form.format(msgArgs), null, 0, false);
}
return cipherText;
}
private byte[] rsaSignHashedData(byte[] dataToSign,
CertificateDetails certificateDetails) throws SQLServerException {
Signature signature;
byte[] signedHash = null;
try {
signature = Signature.getInstance("SHA256withRSA");
signature.initSign((PrivateKey) certificateDetails.privateKey);
signature.update(dataToSign);
signedHash = signature.sign();
}
catch (InvalidKeyException | NoSuchAlgorithmException | SignatureException e) {
MessageFormat form = new MessageFormat(SQLServerException.getErrString("R_EncryptionFailed"));
Object[] msgArgs = {e.getMessage()};
throw new SQLServerException(this, form.format(msgArgs), null, 0, false);
}
return signedHash;
}
private byte[] getLittleEndianBytesFromShort(short value) {
ByteBuffer byteBuffer = ByteBuffer.allocate(2);
byteBuffer.order(ByteOrder.LITTLE_ENDIAN);
byte[] byteValue = byteBuffer.putShort(value).array();
return byteValue;
}
}