Skip to content

Commit faa828b

Browse files
committed
Misc. internal comments cleanup
Also simplified exception handling in SqlColumnEncryptionCspProvider
1 parent 6a8f00a commit faa828b

File tree

2 files changed

+37
-45
lines changed

2 files changed

+37
-45
lines changed

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

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public override byte[] DecryptColumnEncryptionKey(string? masterKeyPath, string?
5252
// Validate encryptionAlgorithm
5353
ValidateEncryptionAlgorithm(encryptionAlgorithm, isSystemOp: true);
5454

55-
// Create RSA Provider with the given CNG name and key name
55+
// Create RSA Provider with the given CNG provider name and key name
5656
RSA rsaProvider = CreateRSACngProvider(masterKeyPath, isSystemOp: true);
5757
using EncryptedColumnEncryptionKeyParameters cekDecryptionParameters = new(rsaProvider, masterKeyPath, MasterKeyType, KeyPathReference);
5858

@@ -78,7 +78,7 @@ public override byte[] EncryptColumnEncryptionKey(string? masterKeyPath, string?
7878
// Validate encryptionAlgorithm
7979
ValidateEncryptionAlgorithm(encryptionAlgorithm, isSystemOp: false);
8080

81-
// Create RSACNGProviderWithKey
81+
// Create RSA Provider with the given CNG provider name and key name
8282
RSA rsaProvider = CreateRSACngProvider(masterKeyPath, isSystemOp: false);
8383
using EncryptedColumnEncryptionKeyParameters cekEncryptionParameters = new(rsaProvider, masterKeyPath, MasterKeyType, KeyPathReference);
8484

@@ -99,10 +99,10 @@ public override bool VerifyColumnMasterKeyMetadata(string? masterKeyPath, bool a
9999

100100
/// <summary>
101101
/// This function validates that the encryption algorithm is RSA_OAEP and if it is not,
102-
/// then throws an exception
102+
/// then throws an exception.
103103
/// </summary>
104-
/// <param name="encryptionAlgorithm">Asymmetric key encryption algorithm</param>
105-
/// <param name="isSystemOp">Indicates if ADO.NET calls or the customer calls the API</param>
104+
/// <param name="encryptionAlgorithm">Asymmetric key encryption algorithm.</param>
105+
/// <param name="isSystemOp">Indicates if ADO.NET calls or the customer calls the API.</param>
106106
private static void ValidateEncryptionAlgorithm([NotNull] string? encryptionAlgorithm, bool isSystemOp)
107107
{
108108
// This validates that the encryption algorithm is RSA_OAEP
@@ -120,8 +120,8 @@ private static void ValidateEncryptionAlgorithm([NotNull] string? encryptionAlgo
120120
/// <summary>
121121
/// Checks if the CNG key path is Empty or Null (and raises exception if they are).
122122
/// </summary>
123-
/// <param name="masterKeyPath">keypath containing the CNG provider name and key name</param>
124-
/// <param name="isSystemOp">Indicates if ADO.NET calls or the customer calls the API</param>
123+
/// <param name="masterKeyPath">Key path containing the CNG provider name and key name.</param>
124+
/// <param name="isSystemOp">Indicates if ADO.NET calls or the customer calls the API.</param>
125125
private static void ValidateNonEmptyKeyPath([NotNull] string? masterKeyPath, bool isSystemOp)
126126
{
127127
if (masterKeyPath is null)
@@ -136,10 +136,10 @@ private static void ValidateNonEmptyKeyPath([NotNull] string? masterKeyPath, boo
136136
}
137137

138138
/// <summary>
139-
/// Creates a RSACng object from the given keyPath.
139+
/// Creates a RSACng from the given key path.
140140
/// </summary>
141-
/// <param name="keyPath"></param>
142-
/// <param name="isSystemOp">Indicates if ADO.NET calls or the customer calls the API</param>
141+
/// <param name="keyPath">Key path in the format of [CNG provider name]/[key name].</param>
142+
/// <param name="isSystemOp">Indicates if ADO.NET calls or the customer calls the API.</param>
143143
/// <returns></returns>
144144
private static RSACng CreateRSACngProvider(string keyPath, bool isSystemOp)
145145
{
@@ -165,12 +165,12 @@ private static RSACng CreateRSACngProvider(string keyPath, bool isSystemOp)
165165
}
166166

167167
/// <summary>
168-
/// Extracts the CNG provider and key name from the key path
168+
/// Extracts the CNG provider name and key name from the given key path.
169169
/// </summary>
170-
/// <param name="keyPath">keypath in the format [CNG Provider]/[KeyName]</param>
171-
/// <param name="isSystemOp">Indicates if ADO.NET calls or the customer calls the API</param>
172-
/// <param name="cngProvider">CNG Provider</param>
173-
/// <param name="keyIdentifier">Key identifier inside the CNG provider</param>
170+
/// <param name="keyPath">Key path in the format [CNG provider name]/[key name].</param>
171+
/// <param name="isSystemOp">Indicates if ADO.NET calls or the customer calls the API.</param>
172+
/// <param name="cngProvider">CNG provider name.</param>
173+
/// <param name="keyIdentifier">Key name inside the CNG provider.</param>
174174
private static void GetCngProviderAndKeyId(string keyPath, bool isSystemOp, out string cngProvider, out string keyIdentifier)
175175
{
176176
int indexOfSlash = keyPath.IndexOf('/');

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

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public class SqlColumnEncryptionCspProvider : SqlColumnEncryptionKeyStoreProvide
2626
internal const string MasterKeyType = @"asymmetric key";
2727

2828
/// <summary>
29-
/// This encryption keystore uses the master key path to reference a CSP.
29+
/// This encryption keystore uses the master key path to reference a CSP provider.
3030
/// </summary>
3131
internal const string KeyPathReference = @"Microsoft Cryptographic Service Provider (CSP)";
3232

@@ -102,10 +102,10 @@ public override bool VerifyColumnMasterKeyMetadata(string? masterKeyPath, bool a
102102

103103
/// <summary>
104104
/// This function validates that the encryption algorithm is RSA_OAEP and if it is not,
105-
/// then throws an exception
105+
/// then throws an exception.
106106
/// </summary>
107-
/// <param name="encryptionAlgorithm">Asymmetric key encryption algorithm</param>
108-
/// <param name="isSystemOp">Indicates if ADO.NET calls or the customer calls the API</param>
107+
/// <param name="encryptionAlgorithm">Asymmetric key encryption algorithm.</param>
108+
/// <param name="isSystemOp">Indicates if ADO.NET calls or the customer calls the API.</param>
109109
private static void ValidateEncryptionAlgorithm([NotNull] string? encryptionAlgorithm, bool isSystemOp)
110110
{
111111
// This validates that the encryption algorithm is RSA_OAEP
@@ -123,8 +123,8 @@ private static void ValidateEncryptionAlgorithm([NotNull] string? encryptionAlgo
123123
/// <summary>
124124
/// Checks if the CSP key path is Empty or Null (and raises exception if they are).
125125
/// </summary>
126-
/// <param name="masterKeyPath">CSP key path.</param>
127-
/// <param name="isSystemOp">Indicates if ADO.NET calls or the customer calls the API</param>
126+
/// <param name="masterKeyPath">Key path containing the CSP provider name and key name.</param>
127+
/// <param name="isSystemOp">Indicates if ADO.NET calls or the customer calls the API.</param>
128128
private static void ValidateNonEmptyCSPKeyPath([NotNull] string? masterKeyPath, bool isSystemOp)
129129
{
130130
if (masterKeyPath is null)
@@ -139,10 +139,10 @@ private static void ValidateNonEmptyCSPKeyPath([NotNull] string? masterKeyPath,
139139
}
140140

141141
/// <summary>
142-
/// Creates a RSACryptoServiceProvider from the given key path which contains both CSP name and key name
142+
/// Creates a RSACryptoServiceProvider from the given key path.
143143
/// </summary>
144-
/// <param name="keyPath">key path in the format of [CAPI provider name]/[key name]</param>
145-
/// <param name="isSystemOp">Indicates if ADO.NET calls or the customer calls the API</param>
144+
/// <param name="keyPath">Key path in the format of [CSP provider name]/[key name].</param>
145+
/// <param name="isSystemOp">Indicates if ADO.NET calls or the customer calls the API.</param>
146146
/// <returns></returns>
147147
private static RSACryptoServiceProvider CreateRSACryptoProvider(string keyPath, bool isSystemOp)
148148
{
@@ -154,35 +154,27 @@ private static RSACryptoServiceProvider CreateRSACryptoProvider(string keyPath,
154154

155155
// Create a new instance of CspParameters for an RSA container.
156156
CspParameters cspParams = new(providerType, cspProviderName, keyName) { Flags = CspProviderFlags.UseExistingKey };
157+
const int KEYSETDOESNOTEXIST = -2146893802;
157158

158159
try
159160
{
160161
// Create a new instance of RSACryptoServiceProvider
161162
return new RSACryptoServiceProvider(cspParams);
162163
}
163-
catch (CryptographicException e)
164+
catch (CryptographicException e) when (e.HResult == KEYSETDOESNOTEXIST)
164165
{
165-
const int KEYSETDOESNOTEXIST = -2146893802;
166-
if (e.HResult == KEYSETDOESNOTEXIST)
167-
{
168-
// Key does not exist
169-
throw SQL.InvalidCspKeyIdentifier(keyName, keyPath, isSystemOp);
170-
}
171-
else
172-
{
173-
// Bubble up the exception
174-
throw;
175-
}
166+
// Key does not exist
167+
throw SQL.InvalidCspKeyIdentifier(keyName, keyPath, isSystemOp);
176168
}
177169
}
178170

179171
/// <summary>
180-
/// Extracts the CSP provider name and key name from the given key path
172+
/// Extracts the CSP provider name and key name from the given key path.
181173
/// </summary>
182-
/// <param name="keyPath">key path in the format of [CSP provider name]/[key name]</param>
183-
/// <param name="isSystemOp">Indicates if ADO.NET calls or the customer calls the API</param>
184-
/// <param name="cspProviderName">output containing the CSP provider name</param>
185-
/// <param name="keyIdentifier">output containing the key name</param>
174+
/// <param name="keyPath">Key path in the format [CSP provider name]/[key name].</param>
175+
/// <param name="isSystemOp">Indicates if ADO.NET calls or the customer calls the API.</param>
176+
/// <param name="cspProviderName">CSP provider name.</param>
177+
/// <param name="keyIdentifier">Key name inside the CSP provider.</param>
186178
private static void GetCspProviderAndKeyName(string keyPath, bool isSystemOp, out string cspProviderName, out string keyIdentifier)
187179
{
188180
int indexOfSlash = keyPath.IndexOf('/');
@@ -205,11 +197,11 @@ private static void GetCspProviderAndKeyName(string keyPath, bool isSystemOp, ou
205197
}
206198

207199
/// <summary>
208-
/// Gets the provider type from a given CAPI provider name
200+
/// Gets the type from a given CSP provider name.
209201
/// </summary>
210-
/// <param name="providerName">CAPI provider name</param>
211-
/// <param name="keyPath">key path in the format of [CSP provider name]/[key name]</param>
212-
/// <param name="isSystemOp">Indicates if ADO.NET calls or the customer calls the API</param>
202+
/// <param name="providerName">CSP provider name.</param>
203+
/// <param name="keyPath">Key path in the format of [CSP provider name]/[key name].</param>
204+
/// <param name="isSystemOp">Indicates if ADO.NET calls or the customer calls the API.</param>
213205
/// <returns></returns>
214206
private static int GetProviderType(string providerName, string keyPath, bool isSystemOp)
215207
{

0 commit comments

Comments
 (0)