forked from dotnet/SqlClient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CspProviderExt.cs
226 lines (193 loc) · 11.1 KB
/
CspProviderExt.cs
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using Microsoft.Data.SqlClient.ManualTesting.Tests.AlwaysEncrypted.Setup;
using Xunit;
namespace Microsoft.Data.SqlClient.ManualTesting.Tests.AlwaysEncrypted
{
/// <summary>
/// Always Encrypted public CspProvider Manual tests.
/// TODO: These tests are marked as Windows only for now but should be run for all platforms once the Master Key is accessible to this app from Azure Key Vault.
/// </summary>
[PlatformSpecific(TestPlatforms.Windows)]
public class CspProviderExt
{
[ConditionalTheory(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup))]
[ClassData(typeof(AEConnectionStringProvider))]
public void TestKeysFromCertificatesCreatedWithMultipleCryptoProviders(string connectionString)
{
const string providersRegistryKeyPath = @"SOFTWARE\Microsoft\Cryptography\Defaults\Provider";
Microsoft.Win32.RegistryKey defaultCryptoProvidersRegistryKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(providersRegistryKeyPath);
foreach (string subKeyName in defaultCryptoProvidersRegistryKey.GetSubKeyNames())
{
// NOTE: RSACryptoServiceProvider.SignData() fails for other providers when testing locally
if (!subKeyName.Contains(@"RSA and AES"))
{
Console.WriteLine(@"INFO: Skipping Certificate creation for {0}.", subKeyName);
continue;
}
string providerName;
string providerType;
string certificateName;
using (Microsoft.Win32.RegistryKey providerKey = defaultCryptoProvidersRegistryKey.OpenSubKey(subKeyName))
{
// Get Provider Name and its type
providerName = providerKey.Name.Substring(providerKey.Name.LastIndexOf(@"\") + 1);
providerType = providerKey.GetValue(@"Type").ToString();
// Create a certificate from that provider
certificateName = string.Format(@"AETest - {0}", providerName);
}
CertificateUtilityWin.CreateCertificate(certificateName, StoreLocation.CurrentUser.ToString(), providerName, providerType);
SQLSetupStrategyCspExt sqlSetupStrategyCsp = null;
try
{
if (false == CertificateUtilityWin.CertificateExists(certificateName, StoreLocation.CurrentUser))
{
Console.WriteLine(@"INFO: Certificate creation for provider {0} failed so skipping it.", providerName);
continue;
}
X509Certificate2 cert = CertificateUtilityWin.GetCertificate(certificateName, StoreLocation.CurrentUser);
string cspPath = CertificateUtilityWin.GetCspPathFromCertificate(cert);
if (string.IsNullOrEmpty(cspPath))
{
Console.WriteLine(@"INFO: Certificate provider {0} is not a csp provider so skipping it.", providerName);
continue;
}
Console.WriteLine("CSP path is {0}", cspPath);
sqlSetupStrategyCsp = new SQLSetupStrategyCspExt(cspPath);
string tableName = sqlSetupStrategyCsp.CspProviderTable.Name;
using (SqlConnection sqlConn = new SqlConnection(connectionString))
{
sqlConn.Open();
Table.DeleteData(tableName, sqlConn);
// insert 1 row data
Customer customer = new Customer(45, "Microsoft", "Corporation");
DatabaseHelper.InsertCustomerData(sqlConn, tableName, customer);
// Test INPUT parameter on an encrypted parameter
using (SqlCommand sqlCommand = new SqlCommand(string.Format(@"SELECT CustomerId, FirstName, LastName FROM [{0}] WHERE FirstName = @firstName", tableName),
sqlConn, null, SqlCommandColumnEncryptionSetting.Enabled))
{
SqlParameter customerFirstParam = sqlCommand.Parameters.AddWithValue(@"firstName", @"Microsoft");
customerFirstParam.Direction = System.Data.ParameterDirection.Input;
using (SqlDataReader sqlDataReader = sqlCommand.ExecuteReader())
{
ValidateResultSet(sqlDataReader);
Console.WriteLine(@"INFO: Successfully validated using a certificate using provider:{0}", providerName);
}
}
}
}
catch (Exception e)
{
Console.WriteLine(@"INFO: Failed to validate using a certificate using provider:{0}", providerName);
Console.WriteLine(@"Exception: {0}", e.Message);
}
finally
{
CertificateUtilityWin.RemoveCertificate(certificateName, StoreLocation.CurrentUser);
// clean up database resources
sqlSetupStrategyCsp?.Dispose();
}
}
}
// [Fact(Skip="Run this in non-parallel mode")] or [ConditionalFact()]
[Fact(Skip = "Failing in TCE")]
public void TestRoundTripWithCSPAndCertStoreProvider()
{
const string providerName = "Microsoft Enhanced RSA and AES Cryptographic Provider";
string providerType = "24";
string certificateName = string.Format(@"AETest - {0}", providerName);
CertificateUtilityWin.CreateCertificate(certificateName, StoreLocation.CurrentUser.ToString(), providerName, providerType);
try
{
X509Certificate2 cert = CertificateUtilityWin.GetCertificate(certificateName, StoreLocation.CurrentUser);
string cspPath = CertificateUtilityWin.GetCspPathFromCertificate(cert);
string certificatePath = String.Concat(@"CurrentUser/my/", cert.Thumbprint);
SqlColumnEncryptionCertificateStoreProvider certProvider = new SqlColumnEncryptionCertificateStoreProvider();
SqlColumnEncryptionCspProvider cspProvider = new SqlColumnEncryptionCspProvider();
byte[] columnEncryptionKey = DatabaseHelper.GenerateRandomBytes(32);
byte[] encryptedColumnEncryptionKeyUsingCert = certProvider.EncryptColumnEncryptionKey(certificatePath, @"RSA_OAEP", columnEncryptionKey);
byte[] columnEncryptionKeyReturnedCert2CSP = cspProvider.DecryptColumnEncryptionKey(cspPath, @"RSA_OAEP", encryptedColumnEncryptionKeyUsingCert);
Assert.True(columnEncryptionKey.SequenceEqual(columnEncryptionKeyReturnedCert2CSP));
byte[] encryptedColumnEncryptionKeyUsingCSP = cspProvider.EncryptColumnEncryptionKey(cspPath, @"RSA_OAEP", columnEncryptionKey);
byte[] columnEncryptionKeyReturnedCSP2Cert = certProvider.DecryptColumnEncryptionKey(certificatePath, @"RSA_OAEP", encryptedColumnEncryptionKeyUsingCSP);
Assert.True(columnEncryptionKey.SequenceEqual(columnEncryptionKeyReturnedCSP2Cert));
}
finally
{
CertificateUtilityWin.RemoveCertificate(certificateName, StoreLocation.CurrentUser);
}
}
[ConditionalTheory(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringSetupForAE))]
[ClassData(typeof(AEConnectionStringProvider))]
public void TestEncryptDecryptWithCSP(string connectionString)
{
string providerName = @"Microsoft Enhanced RSA and AES Cryptographic Provider";
string keyIdentifier = DataTestUtility.GetUniqueNameForSqlServer("CSP");
try
{
CertificateUtilityWin.RSAPersistKeyInCsp(providerName, keyIdentifier);
string cspPath = String.Concat(providerName, @"/", keyIdentifier);
SQLSetupStrategyCspExt sqlSetupStrategyCsp = new SQLSetupStrategyCspExt(cspPath);
string tableName = sqlSetupStrategyCsp.CspProviderTable.Name;
try
{
using (SqlConnection sqlConn = new SqlConnection(connectionString))
{
sqlConn.Open();
Table.DeleteData(tableName, sqlConn);
// insert 1 row data
Customer customer = new Customer(45, "Microsoft", "Corporation");
DatabaseHelper.InsertCustomerData(sqlConn, tableName, customer);
// Test INPUT parameter on an encrypted parameter
using (SqlCommand sqlCommand = new SqlCommand(string.Format(@"SELECT CustomerId, FirstName, LastName FROM [{0}] WHERE FirstName = @firstName", tableName),
sqlConn, null, SqlCommandColumnEncryptionSetting.Enabled))
{
SqlParameter customerFirstParam = sqlCommand.Parameters.AddWithValue(@"firstName", @"Microsoft");
customerFirstParam.Direction = System.Data.ParameterDirection.Input;
using (SqlDataReader sqlDataReader = sqlCommand.ExecuteReader())
{
ValidateResultSet(sqlDataReader);
}
}
}
}
catch (Exception e)
{
Console.WriteLine(@"Exception: {0}", e.Message);
}
finally
{
// clean up database resources
sqlSetupStrategyCsp.Dispose();
}
}
finally
{
CertificateUtilityWin.RSADeleteKeyInCsp(providerName, keyIdentifier);
}
}
/// <summary>
/// Validates that the results are the ones expected.
/// </summary>
/// <param name="sqlDataReader"></param>
private void ValidateResultSet(SqlDataReader sqlDataReader)
{
int rowsFound = 0;
while (sqlDataReader.Read())
{
if (sqlDataReader.FieldCount == 3)
{
Assert.Equal(45, sqlDataReader.GetInt32(0));
Assert.Equal(@"Microsoft", sqlDataReader.GetString(1));
Assert.Equal(@"Corporation", sqlDataReader.GetString(2));
}
rowsFound++;
}
Assert.Equal(1, rowsFound);
}
}
}