-
Notifications
You must be signed in to change notification settings - Fork 330
Expand file tree
/
Copy pathSqlSetupStrategyCspProvider.cs
More file actions
77 lines (61 loc) · 2.91 KB
/
Copy pathSqlSetupStrategyCspProvider.cs
File metadata and controls
77 lines (61 loc) · 2.91 KB
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
// 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;
using System.Collections.Generic;
using System.Security.Cryptography;
using Microsoft.Data.SqlClient.ManualTesting.Tests.AlwaysEncrypted.Setup;
namespace Microsoft.Data.SqlClient.ManualTesting.Tests.AlwaysEncrypted
{
public class SQLSetupStrategyCspProvider : SQLSetupStrategy
{
private const int KeySize = 2048;
private readonly List<CspParameters> _cspKeyParameters = new List<CspParameters>();
public SQLSetupStrategyCspProvider(CspParameters cspParameters)
: base(cspParameters.ProviderName + "/" + cspParameters.KeyContainerName)
{
// Create a new instance of RSACryptoServiceProvider to generate
// a new key pair. Pass the CspParameters class to persist the
// key in the container.
using RSACryptoServiceProvider rsaAlg = new RSACryptoServiceProvider(KeySize, cspParameters);
rsaAlg.PersistKeyInCsp = true;
_cspKeyParameters.Add(cspParameters);
CspProvider = new SqlColumnEncryptionCspProvider();
SetupDatabase();
}
public SqlColumnEncryptionCspProvider CspProvider { get; }
internal override void SetupDatabase()
{
ColumnMasterKey columnMasterKey = new CspProviderColumnMasterKey(GenerateUniqueName("CspExt"), SqlColumnEncryptionCspProvider.ProviderName, ColumnMasterKeyPath);
databaseObjects.Add(columnMasterKey);
List<ColumnEncryptionKey> columnEncryptionKeys = CreateColumnEncryptionKeys(columnMasterKey, 2, CspProvider);
databaseObjects.AddRange(columnEncryptionKeys);
List<Table> tables = CreateTables(columnEncryptionKeys);
databaseObjects.AddRange(tables);
base.SetupDatabase();
InsertSampleData(ApiTestTable.Name);
}
protected override void Dispose(bool disposing)
{
foreach (CspParameters cspParameters in _cspKeyParameters)
{
try
{
// Create a new instance of RSACryptoServiceProvider.
// Pass the CspParameters class to use the
// key in the container.
using RSACryptoServiceProvider rsaAlg = new RSACryptoServiceProvider(cspParameters);
// Delete the key entry in the container.
rsaAlg.PersistKeyInCsp = false;
// Call Clear to release resources and delete the key from the container.
rsaAlg.Clear();
}
catch (Exception)
{
continue;
}
}
base.Dispose(disposing);
}
}
}