5
5
using Microsoft . Data . SqlClient . AlwaysEncrypted ;
6
6
using Microsoft . Win32 ;
7
7
using System ;
8
+ using System . Diagnostics . CodeAnalysis ;
8
9
using System . Security . Cryptography ;
9
10
10
11
#nullable enable
@@ -36,7 +37,7 @@ public class SqlColumnEncryptionCspProvider : SqlColumnEncryptionKeyStoreProvide
36
37
private const string RSAEncryptionAlgorithmWithOAEP = @"RSA_OAEP" ;
37
38
38
39
/// <include file='../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlColumnEncryptionCspProvider.xml' path='docs/members[@name="SqlColumnEncryptionCspProvider"]/DecryptColumnEncryptionKey/*' />
39
- public override byte [ ] DecryptColumnEncryptionKey ( string masterKeyPath , string encryptionAlgorithm , byte [ ] encryptedColumnEncryptionKey )
40
+ public override byte [ ] DecryptColumnEncryptionKey ( string ? masterKeyPath , string ? encryptionAlgorithm , byte [ ] ? encryptedColumnEncryptionKey )
40
41
{
41
42
// Validate the input parameters
42
43
ValidateNonEmptyCSPKeyPath ( masterKeyPath , isSystemOp : true ) ;
@@ -55,14 +56,14 @@ public override byte[] DecryptColumnEncryptionKey(string masterKeyPath, string e
55
56
ValidateEncryptionAlgorithm ( encryptionAlgorithm , isSystemOp : true ) ;
56
57
57
58
// Create RSA Provider with the given CSP name and key name
58
- RSACryptoServiceProvider rsaProvider = CreateRSACryptoProvider ( masterKeyPath , isSystemOp : true ) ;
59
+ RSA rsaProvider = CreateRSACryptoProvider ( masterKeyPath , isSystemOp : true ) ;
59
60
using EncryptedColumnEncryptionKeyParameters cekDecryptionParameters = new ( rsaProvider , masterKeyPath , MasterKeyType , KeyPathReference ) ;
60
61
61
62
return cekDecryptionParameters . Decrypt ( encryptedColumnEncryptionKey ) ;
62
63
}
63
64
64
65
/// <include file='../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlColumnEncryptionCspProvider.xml' path='docs/members[@name="SqlColumnEncryptionCspProvider"]/EncryptColumnEncryptionKey/*' />
65
- public override byte [ ] EncryptColumnEncryptionKey ( string masterKeyPath , string encryptionAlgorithm , byte [ ] columnEncryptionKey )
66
+ public override byte [ ] EncryptColumnEncryptionKey ( string ? masterKeyPath , string ? encryptionAlgorithm , byte [ ] ? columnEncryptionKey )
66
67
{
67
68
// Validate the input parameters
68
69
ValidateNonEmptyCSPKeyPath ( masterKeyPath , isSystemOp : false ) ;
@@ -81,20 +82,20 @@ public override byte[] EncryptColumnEncryptionKey(string masterKeyPath, string e
81
82
ValidateEncryptionAlgorithm ( encryptionAlgorithm , isSystemOp : false ) ;
82
83
83
84
// Create RSA Provider with the given CSP name and key name
84
- RSACryptoServiceProvider rsaProvider = CreateRSACryptoProvider ( masterKeyPath , isSystemOp : false ) ;
85
+ RSA rsaProvider = CreateRSACryptoProvider ( masterKeyPath , isSystemOp : false ) ;
85
86
using EncryptedColumnEncryptionKeyParameters cekEncryptionParameters = new ( rsaProvider , masterKeyPath , MasterKeyType , KeyPathReference ) ;
86
87
87
88
return cekEncryptionParameters . Encrypt ( columnEncryptionKey ) ;
88
89
}
89
90
90
91
/// <include file='../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlColumnEncryptionCspProvider.xml' path='docs/members[@name="SqlColumnEncryptionCspProvider"]/SignColumnMasterKeyMetadata/*' />
91
- public override byte [ ] SignColumnMasterKeyMetadata ( string masterKeyPath , bool allowEnclaveComputations )
92
+ public override byte [ ] SignColumnMasterKeyMetadata ( string ? masterKeyPath , bool allowEnclaveComputations )
92
93
{
93
94
throw new NotSupportedException ( ) ;
94
95
}
95
96
96
97
/// <include file='../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlColumnEncryptionCspProvider.xml' path='docs/members[@name="SqlColumnEncryptionCspProvider"]/VerifyColumnMasterKeyMetadata/*' />
97
- public override bool VerifyColumnMasterKeyMetadata ( string masterKeyPath , bool allowEnclaveComputations , byte [ ] signature )
98
+ public override bool VerifyColumnMasterKeyMetadata ( string ? masterKeyPath , bool allowEnclaveComputations , byte [ ] ? signature )
98
99
{
99
100
throw new NotSupportedException ( ) ;
100
101
}
@@ -105,7 +106,7 @@ public override bool VerifyColumnMasterKeyMetadata(string masterKeyPath, bool al
105
106
/// </summary>
106
107
/// <param name="encryptionAlgorithm">Asymmetric key encryption algorithm</param>
107
108
/// <param name="isSystemOp">Indicates if ADO.NET calls or the customer calls the API</param>
108
- private static void ValidateEncryptionAlgorithm ( string encryptionAlgorithm , bool isSystemOp )
109
+ private static void ValidateEncryptionAlgorithm ( [ NotNull ] string ? encryptionAlgorithm , bool isSystemOp )
109
110
{
110
111
// This validates that the encryption algorithm is RSA_OAEP
111
112
if ( encryptionAlgorithm is null )
@@ -124,7 +125,7 @@ private static void ValidateEncryptionAlgorithm(string encryptionAlgorithm, bool
124
125
/// </summary>
125
126
/// <param name="masterKeyPath">CSP key path.</param>
126
127
/// <param name="isSystemOp">Indicates if ADO.NET calls or the customer calls the API</param>
127
- private static void ValidateNonEmptyCSPKeyPath ( string masterKeyPath , bool isSystemOp )
128
+ private static void ValidateNonEmptyCSPKeyPath ( [ NotNull ] string ? masterKeyPath , bool isSystemOp )
128
129
{
129
130
if ( masterKeyPath is null )
130
131
{
@@ -153,12 +154,11 @@ private static RSACryptoServiceProvider CreateRSACryptoProvider(string keyPath,
153
154
154
155
// Create a new instance of CspParameters for an RSA container.
155
156
CspParameters cspParams = new ( providerType , cspProviderName , keyName ) { Flags = CspProviderFlags . UseExistingKey } ;
156
- RSACryptoServiceProvider rscp ;
157
157
158
158
try
159
159
{
160
160
// Create a new instance of RSACryptoServiceProvider
161
- rscp = new RSACryptoServiceProvider ( cspParams ) ;
161
+ return new RSACryptoServiceProvider ( cspParams ) ;
162
162
}
163
163
catch ( CryptographicException e )
164
164
{
@@ -174,8 +174,6 @@ private static RSACryptoServiceProvider CreateRSACryptoProvider(string keyPath,
174
174
throw ;
175
175
}
176
176
}
177
-
178
- return rscp ;
179
177
}
180
178
181
179
/// <summary>
0 commit comments