Skip to content

Commit 6a8f00a

Browse files
committed
Implement NRTs on both providers
1 parent 4d8147a commit 6a8f00a

File tree

4 files changed

+31
-24
lines changed

4 files changed

+31
-24
lines changed

src/Microsoft.Data.SqlClient/netfx/src/Microsoft.Data.SqlClient.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -978,6 +978,9 @@
978978
<Compile Include="$(CommonSourceRoot)System\Buffers\ArrayBufferWriter.netfx.cs">
979979
<Link>System\Buffers\ArrayBufferWriter.netfx.cs</Link>
980980
</Compile>
981+
<Compile Include="$(CommonSourceRoot)System\Diagnostics\CodeAnalysis.cs">
982+
<Link>System\Diagnostics\CodeAnalysis.cs</Link>
983+
</Compile>
981984
<Compile Include="$(CommonSourceRoot)System\IO\StreamExtensions.netfx.cs">
982985
<Link>System\IO\StreamExtensions.netfx.cs</Link>
983986
</Compile>

src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlColumnEncryptionCngProvider.Windows.cs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
using Microsoft.Data.SqlClient.AlwaysEncrypted;
66
using System;
7+
using System.Diagnostics.CodeAnalysis;
78
using System.Security.Cryptography;
89

910
#nullable enable
@@ -33,7 +34,7 @@ public class SqlColumnEncryptionCngProvider : SqlColumnEncryptionKeyStoreProvide
3334
private const string RSAEncryptionAlgorithmWithOAEP = @"RSA_OAEP";
3435

3536
/// <include file='../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlColumnEncryptionCngProvider.xml' path='docs/members[@name="SqlColumnEncryptionCngProvider"]/DecryptColumnEncryptionKey/*' />
36-
public override byte[] DecryptColumnEncryptionKey(string masterKeyPath, string encryptionAlgorithm, byte[] encryptedColumnEncryptionKey)
37+
public override byte[] DecryptColumnEncryptionKey(string? masterKeyPath, string? encryptionAlgorithm, byte[]? encryptedColumnEncryptionKey)
3738
{
3839
// Validate the input parameters
3940
ValidateNonEmptyKeyPath(masterKeyPath, isSystemOp: true);
@@ -52,14 +53,14 @@ public override byte[] DecryptColumnEncryptionKey(string masterKeyPath, string e
5253
ValidateEncryptionAlgorithm(encryptionAlgorithm, isSystemOp: true);
5354

5455
// Create RSA Provider with the given CNG name and key name
55-
RSA rsaCngProvider = CreateRSACngProvider(masterKeyPath, isSystemOp: true);
56-
using EncryptedColumnEncryptionKeyParameters cekDecryptionParameters = new(rsaCngProvider, masterKeyPath, MasterKeyType, KeyPathReference);
56+
RSA rsaProvider = CreateRSACngProvider(masterKeyPath, isSystemOp: true);
57+
using EncryptedColumnEncryptionKeyParameters cekDecryptionParameters = new(rsaProvider, masterKeyPath, MasterKeyType, KeyPathReference);
5758

5859
return cekDecryptionParameters.Decrypt(encryptedColumnEncryptionKey);
5960
}
6061

6162
/// <include file='../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlColumnEncryptionCngProvider.xml' path='docs/members[@name="SqlColumnEncryptionCngProvider"]/EncryptColumnEncryptionKey/*' />
62-
public override byte[] EncryptColumnEncryptionKey(string masterKeyPath, string encryptionAlgorithm, byte[] columnEncryptionKey)
63+
public override byte[] EncryptColumnEncryptionKey(string? masterKeyPath, string? encryptionAlgorithm, byte[]? columnEncryptionKey)
6364
{
6465
// Validate the input parameters
6566
ValidateNonEmptyKeyPath(masterKeyPath, isSystemOp: false);
@@ -77,21 +78,21 @@ public override byte[] EncryptColumnEncryptionKey(string masterKeyPath, string e
7778
// Validate encryptionAlgorithm
7879
ValidateEncryptionAlgorithm(encryptionAlgorithm, isSystemOp: false);
7980

80-
// CreateCNGProviderWithKey
81-
RSA rsaCngProvider = CreateRSACngProvider(masterKeyPath, isSystemOp: false);
82-
using EncryptedColumnEncryptionKeyParameters cekEncryptionParameters = new(rsaCngProvider, masterKeyPath, MasterKeyType, KeyPathReference);
81+
// Create RSACNGProviderWithKey
82+
RSA rsaProvider = CreateRSACngProvider(masterKeyPath, isSystemOp: false);
83+
using EncryptedColumnEncryptionKeyParameters cekEncryptionParameters = new(rsaProvider, masterKeyPath, MasterKeyType, KeyPathReference);
8384

8485
return cekEncryptionParameters.Encrypt(columnEncryptionKey);
8586
}
8687

8788
/// <include file='../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlColumnEncryptionCngProvider.xml' path='docs/members[@name="SqlColumnEncryptionCngProvider"]/SignColumnMasterKeyMetadata/*' />
88-
public override byte[] SignColumnMasterKeyMetadata(string masterKeyPath, bool allowEnclaveComputations)
89+
public override byte[] SignColumnMasterKeyMetadata(string? masterKeyPath, bool allowEnclaveComputations)
8990
{
9091
throw new NotSupportedException();
9192
}
9293

9394
/// <include file='../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlColumnEncryptionCngProvider.xml' path='docs/members[@name="SqlColumnEncryptionCngProvider"]/VerifyColumnMasterKeyMetadata/*' />
94-
public override bool VerifyColumnMasterKeyMetadata(string masterKeyPath, bool allowEnclaveComputations, byte[] signature)
95+
public override bool VerifyColumnMasterKeyMetadata(string? masterKeyPath, bool allowEnclaveComputations, byte[]? signature)
9596
{
9697
throw new NotSupportedException();
9798
}
@@ -100,9 +101,9 @@ public override bool VerifyColumnMasterKeyMetadata(string masterKeyPath, bool al
100101
/// This function validates that the encryption algorithm is RSA_OAEP and if it is not,
101102
/// then throws an exception
102103
/// </summary>
103-
/// <param name="encryptionAlgorithm">Asymmetric key encryptio algorithm</param>
104+
/// <param name="encryptionAlgorithm">Asymmetric key encryption algorithm</param>
104105
/// <param name="isSystemOp">Indicates if ADO.NET calls or the customer calls the API</param>
105-
private static void ValidateEncryptionAlgorithm(string encryptionAlgorithm, bool isSystemOp)
106+
private static void ValidateEncryptionAlgorithm([NotNull] string? encryptionAlgorithm, bool isSystemOp)
106107
{
107108
// This validates that the encryption algorithm is RSA_OAEP
108109
if (encryptionAlgorithm is null)
@@ -121,7 +122,7 @@ private static void ValidateEncryptionAlgorithm(string encryptionAlgorithm, bool
121122
/// </summary>
122123
/// <param name="masterKeyPath">keypath containing the CNG provider name and key name</param>
123124
/// <param name="isSystemOp">Indicates if ADO.NET calls or the customer calls the API</param>
124-
private static void ValidateNonEmptyKeyPath(string masterKeyPath, bool isSystemOp)
125+
private static void ValidateNonEmptyKeyPath([NotNull] string? masterKeyPath, bool isSystemOp)
125126
{
126127
if (masterKeyPath is null)
127128
{

src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlColumnEncryptionCspProvider.Windows.cs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using Microsoft.Data.SqlClient.AlwaysEncrypted;
66
using Microsoft.Win32;
77
using System;
8+
using System.Diagnostics.CodeAnalysis;
89
using System.Security.Cryptography;
910

1011
#nullable enable
@@ -36,7 +37,7 @@ public class SqlColumnEncryptionCspProvider : SqlColumnEncryptionKeyStoreProvide
3637
private const string RSAEncryptionAlgorithmWithOAEP = @"RSA_OAEP";
3738

3839
/// <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)
4041
{
4142
// Validate the input parameters
4243
ValidateNonEmptyCSPKeyPath(masterKeyPath, isSystemOp: true);
@@ -55,14 +56,14 @@ public override byte[] DecryptColumnEncryptionKey(string masterKeyPath, string e
5556
ValidateEncryptionAlgorithm(encryptionAlgorithm, isSystemOp: true);
5657

5758
// 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);
5960
using EncryptedColumnEncryptionKeyParameters cekDecryptionParameters = new(rsaProvider, masterKeyPath, MasterKeyType, KeyPathReference);
6061

6162
return cekDecryptionParameters.Decrypt(encryptedColumnEncryptionKey);
6263
}
6364

6465
/// <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)
6667
{
6768
// Validate the input parameters
6869
ValidateNonEmptyCSPKeyPath(masterKeyPath, isSystemOp: false);
@@ -81,20 +82,20 @@ public override byte[] EncryptColumnEncryptionKey(string masterKeyPath, string e
8182
ValidateEncryptionAlgorithm(encryptionAlgorithm, isSystemOp: false);
8283

8384
// 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);
8586
using EncryptedColumnEncryptionKeyParameters cekEncryptionParameters = new(rsaProvider, masterKeyPath, MasterKeyType, KeyPathReference);
8687

8788
return cekEncryptionParameters.Encrypt(columnEncryptionKey);
8889
}
8990

9091
/// <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)
9293
{
9394
throw new NotSupportedException();
9495
}
9596

9697
/// <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)
9899
{
99100
throw new NotSupportedException();
100101
}
@@ -105,7 +106,7 @@ public override bool VerifyColumnMasterKeyMetadata(string masterKeyPath, bool al
105106
/// </summary>
106107
/// <param name="encryptionAlgorithm">Asymmetric key encryption algorithm</param>
107108
/// <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)
109110
{
110111
// This validates that the encryption algorithm is RSA_OAEP
111112
if (encryptionAlgorithm is null)
@@ -124,7 +125,7 @@ private static void ValidateEncryptionAlgorithm(string encryptionAlgorithm, bool
124125
/// </summary>
125126
/// <param name="masterKeyPath">CSP key path.</param>
126127
/// <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)
128129
{
129130
if (masterKeyPath is null)
130131
{
@@ -153,12 +154,11 @@ private static RSACryptoServiceProvider CreateRSACryptoProvider(string keyPath,
153154

154155
// Create a new instance of CspParameters for an RSA container.
155156
CspParameters cspParams = new(providerType, cspProviderName, keyName) { Flags = CspProviderFlags.UseExistingKey };
156-
RSACryptoServiceProvider rscp;
157157

158158
try
159159
{
160160
// Create a new instance of RSACryptoServiceProvider
161-
rscp = new RSACryptoServiceProvider(cspParams);
161+
return new RSACryptoServiceProvider(cspParams);
162162
}
163163
catch (CryptographicException e)
164164
{
@@ -174,8 +174,6 @@ private static RSACryptoServiceProvider CreateRSACryptoProvider(string keyPath,
174174
throw;
175175
}
176176
}
177-
178-
return rscp;
179177
}
180178

181179
/// <summary>

src/Microsoft.Data.SqlClient/src/System/Diagnostics/CodeAnalysis.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,10 @@ public MemberNotNullWhenAttribute(bool returnValue, params string[] members)
3838

3939
public string[] Members { get; }
4040
}
41+
42+
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property | AttributeTargets.ReturnValue, Inherited = false)]
43+
internal sealed class NotNullAttribute : Attribute
44+
{
45+
}
4146
#endif
4247
}

0 commit comments

Comments
 (0)