2016-08-03 10:59:49 +00:00
//------------------------------------------------------------------------------
// <copyright file="SqlException.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
// <owner current="true" primary="true">balnee</owner>
// <owner current="true" primary="false">krishnib</owner>
//------------------------------------------------------------------------------
namespace System.Data.SqlClient
{
using System ;
using System.Data.SqlClient ;
using System.Security.Cryptography ;
/// <summary>
/// Base class containing raw key bytes for symmetric key algorithms. Some encryption algorithms can use the key directly while others derive sub keys from this.
/// If an algorithm needs to derive more keys, have a derived class from this and use it in the corresponding encryption algorithm.
/// </summary>
2016-11-10 13:04:39 +00:00
internal class SqlClientSymmetricKey {
2016-08-03 10:59:49 +00:00
/// <summary>
2016-11-10 13:04:39 +00:00
/// The underlying key material
2016-08-03 10:59:49 +00:00
/// </summary>
protected readonly byte [ ] _rootKey ;
/// <summary>
/// Constructor that initializes the root key.
/// </summary>
/// <param name="rootKey">root key</param>
2016-11-10 13:04:39 +00:00
internal SqlClientSymmetricKey ( byte [ ] rootKey ) {
2016-08-03 10:59:49 +00:00
// Key validation
if ( rootKey = = null | | rootKey . Length = = 0 ) {
throw SQL . NullColumnEncryptionKeySysErr ( ) ;
}
_rootKey = rootKey ;
}
2016-11-10 13:04:39 +00:00
/// <summary>
/// Destructor that cleans up the key material.
/// This is a best effort approach since there are no guarantees around GC.
/// </summary>
~ SqlClientSymmetricKey ( ) {
if ( _rootKey ! = null ) {
for ( int i = 0 ; i < _rootKey . Length ; i + + ) {
_rootKey [ i ] = 0 ;
}
}
}
2016-08-03 10:59:49 +00:00
/// <summary>
/// Returns a copy of the plain text key
/// This is needed for actual encryption/decryption.
/// </summary>
2016-11-10 13:04:39 +00:00
internal virtual byte [ ] RootKey {
get {
2016-08-03 10:59:49 +00:00
return _rootKey ;
}
}
/// <summary>
/// Computes SHA256 value of the plain text key bytes
/// </summary>
/// <returns>A string containing SHA256 hash of the root key</returns>
2016-11-10 13:04:39 +00:00
internal virtual string GetKeyHash ( ) {
2016-08-03 10:59:49 +00:00
return SqlSecurityUtility . GetSHA256Hash ( RootKey ) ;
}
/// <summary>
/// Gets the length of the root key
/// </summary>
/// <returns>
/// Returns the length of the root key
/// </returns>
2016-11-10 13:04:39 +00:00
internal virtual int Length ( ) {
2016-08-03 10:59:49 +00:00
return _rootKey . Length ;
}
}
}