-
Notifications
You must be signed in to change notification settings - Fork 331
Add Enclave Simulator for Internal Testing #427
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
karinazhou
merged 2 commits into
dotnet:master
from
karinazhou:optional_enclave_simulator
Feb 14, 2020
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
...ata.SqlClient/netcore/src/Microsoft/Data/SqlClient/SimulatorEnclaveProvider.NetCoreApp.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| // 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.Collections.Generic; | ||
| using System.Data.SqlClient; | ||
| using System.Diagnostics; | ||
| using System.Linq; | ||
| using System.Runtime.Caching; | ||
| using System.Security.Cryptography; | ||
| using System.Text; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using System.Collections.Concurrent; | ||
|
|
||
| namespace Microsoft.Data.SqlClient | ||
| { | ||
| internal class SimulatorEnclaveProvider : EnclaveProviderBase | ||
| { | ||
| private static readonly int EnclaveSessionHandleSize = 8; | ||
|
|
||
| // When overridden in a derived class, looks up an existing enclave session information in the enclave session cache. | ||
| // If the enclave provider doesn't implement enclave session caching, this method is expected to return null in the sqlEnclaveSession parameter. | ||
| public override void GetEnclaveSession(string servername, string attestationUrl, bool generateCustomData, out SqlEnclaveSession sqlEnclaveSession, out long counter, out byte[] customData, out int customDataLength) | ||
| { | ||
| GetEnclaveSessionHelper(servername, attestationUrl, false, out sqlEnclaveSession, out counter, out customData, out customDataLength); | ||
| } | ||
|
|
||
| // Gets the information that SqlClient subsequently uses to initiate the process of attesting the enclave and to establish a secure session with the enclave. | ||
| // <returns>The information SqlClient subsequently uses to initiate the process of attesting the enclave and to establish a secure session with the enclave.</returns> | ||
| public override SqlEnclaveAttestationParameters GetAttestationParameters(string attestationUrl, byte[] customData, int customDataLength) | ||
| { | ||
| ECDiffieHellmanCng clientDHKey = new ECDiffieHellmanCng(384); | ||
| clientDHKey.KeyDerivationFunction = ECDiffieHellmanKeyDerivationFunction.Hash; | ||
| clientDHKey.HashAlgorithm = CngAlgorithm.Sha256; | ||
|
|
||
| return new SqlEnclaveAttestationParameters(2, new byte[] { }, clientDHKey); | ||
| } | ||
|
|
||
| // When overridden in a derived class, performs enclave attestation, generates a symmetric key for the session, creates a an enclave session and stores the session information in the cache. | ||
| public override void CreateEnclaveSession(byte[] attestationInfo, ECDiffieHellmanCng clientDHKey, string attestationUrl, string servername, byte[] customData, int customDataLength, out SqlEnclaveSession sqlEnclaveSession, out long counter) | ||
| { | ||
| ////for simulator: enclave does not send public key, and sends an empty attestation info | ||
| //// The only non-trivial content it sends is the session setup info (DH pubkey of enclave) | ||
|
|
||
| sqlEnclaveSession = null; | ||
| counter = 0; | ||
| try | ||
| { | ||
| ThreadRetryCache.Remove(Thread.CurrentThread.ManagedThreadId.ToString()); | ||
| sqlEnclaveSession = GetEnclaveSessionFromCache(servername, attestationUrl, out counter); | ||
|
|
||
| if (sqlEnclaveSession == null) | ||
| { | ||
| if (!string.IsNullOrEmpty(attestationUrl)) | ||
| { | ||
| ////Read AttestationInfo | ||
| int attestationInfoOffset = 0; | ||
| uint sizeOfTrustedModuleAttestationInfoBuffer = BitConverter.ToUInt32(attestationInfo, attestationInfoOffset); | ||
| attestationInfoOffset += sizeof(UInt32); | ||
| int sizeOfTrustedModuleAttestationInfoBufferInt = checked((int)sizeOfTrustedModuleAttestationInfoBuffer); | ||
| Debug.Assert(sizeOfTrustedModuleAttestationInfoBuffer == 0); | ||
|
|
||
| ////read secure session info | ||
| uint sizeOfSecureSessionInfoResponse = BitConverter.ToUInt32(attestationInfo, attestationInfoOffset); | ||
| attestationInfoOffset += sizeof(UInt32); | ||
|
|
||
| byte[] enclaveSessionHandle = new byte[EnclaveSessionHandleSize]; | ||
| Buffer.BlockCopy(attestationInfo, attestationInfoOffset, enclaveSessionHandle, 0, EnclaveSessionHandleSize); | ||
| attestationInfoOffset += EnclaveSessionHandleSize; | ||
|
|
||
| uint sizeOfTrustedModuleDHPublicKeyBuffer = BitConverter.ToUInt32(attestationInfo, attestationInfoOffset); | ||
| attestationInfoOffset += sizeof(UInt32); | ||
| uint sizeOfTrustedModuleDHPublicKeySignatureBuffer = BitConverter.ToUInt32(attestationInfo, attestationInfoOffset); | ||
| attestationInfoOffset += sizeof(UInt32); | ||
| int sizeOfTrustedModuleDHPublicKeyBufferInt = checked((int)sizeOfTrustedModuleDHPublicKeyBuffer); | ||
|
|
||
| byte[] trustedModuleDHPublicKey = new byte[sizeOfTrustedModuleDHPublicKeyBuffer]; | ||
| Buffer.BlockCopy(attestationInfo, attestationInfoOffset, trustedModuleDHPublicKey, 0, | ||
| sizeOfTrustedModuleDHPublicKeyBufferInt); | ||
| attestationInfoOffset += sizeOfTrustedModuleDHPublicKeyBufferInt; | ||
|
|
||
| byte[] trustedModuleDHPublicKeySignature = new byte[sizeOfTrustedModuleDHPublicKeySignatureBuffer]; | ||
| Buffer.BlockCopy(attestationInfo, attestationInfoOffset, trustedModuleDHPublicKeySignature, 0, | ||
| checked((int)sizeOfTrustedModuleDHPublicKeySignatureBuffer)); | ||
|
|
||
| CngKey k = CngKey.Import(trustedModuleDHPublicKey, CngKeyBlobFormat.EccPublicBlob); | ||
| byte[] sharedSecret = clientDHKey.DeriveKeyMaterial(k); | ||
| long sessionId = BitConverter.ToInt64(enclaveSessionHandle, 0); | ||
| sqlEnclaveSession = AddEnclaveSessionToCache(attestationUrl, servername, sharedSecret, sessionId, out counter); | ||
| } | ||
| else | ||
| { | ||
| throw new AlwaysEncryptedAttestationException(SR.FailToCreateEnclaveSession); | ||
| } | ||
| } | ||
| } | ||
| finally | ||
| { | ||
| UpdateEnclaveSessionLockStatus(sqlEnclaveSession); | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// When overridden in a derived class, looks up and evicts an enclave session from the enclave session cache, if the provider implements session caching. | ||
| /// </summary> | ||
| /// <param name="serverName">The name of the SQL Server instance containing the enclave.</param> | ||
| /// <param name="enclaveAttestationUrl">The endpoint of an attestation service, SqlClient contacts to attest the enclave.</param> | ||
| /// <param name="enclaveSessionToInvalidate">The session to be invalidated.</param> | ||
| public override void InvalidateEnclaveSession(string serverName, string enclaveAttestationUrl, SqlEnclaveSession enclaveSessionToInvalidate) | ||
| { | ||
| InvalidateEnclaveSessionHelper(serverName, enclaveAttestationUrl, enclaveSessionToInvalidate); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.