-
Notifications
You must be signed in to change notification settings - Fork 331
Enhance VSM/HGS enclave session key binding validation #4532
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
Open
benrr101
wants to merge
5
commits into
main
Choose a base branch
from
dev/russellben/msrc
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7a0e280
Enhance Always Encrypted VSM/HGS enclave attestation to validate encl…
Ananya2 2405f82
Address review feedback on VSM/HGS enclave key binding check
Ananya2 b4ea8e5
Arrange/Act/Assert
benrr101 dc9d04a
Check for empty public key in validation
benrr101 e75fbfa
Remove reflection from unit tests by making method internal and static
benrr101 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
9 changes: 9 additions & 0 deletions
9
src/Microsoft.Data.SqlClient/src/Resources/Strings.Designer.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
99 changes: 99 additions & 0 deletions
99
...qlClient/tests/UnitTests/Microsoft/Data/SqlClient/VirtualSecureModeEnclaveProviderTest.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,99 @@ | ||
| // 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.Security.Cryptography; | ||
| using System.Text; | ||
| using Xunit; | ||
|
|
||
| namespace Microsoft.Data.SqlClient.UnitTests; | ||
|
|
||
| /// <summary> | ||
| /// Tests the enclave public key binding check on the VSM/HGS attestation path. A genuine VBS enclave | ||
| /// commits to its public key by placing SHA-256(public key) in the first 32 bytes of the report's | ||
| /// signature-covered EnclaveData, so the binding must accept a matching key and reject any other. | ||
| /// </summary> | ||
| public class VirtualSecureModeEnclaveProviderTest | ||
| { | ||
| private const int EnclaveReportPackageHeaderSize = 6 * sizeof(uint); // 24 | ||
| private const int EnclaveReportSize = (sizeof(uint) * 2) + 64 + 152; // ReportSize + ReportVersion + EnclaveData + EnclaveIdentity = 224 | ||
|
|
||
| /// <summary> | ||
| /// A report whose EnclaveData commits to the key used for the session passes the binding check. | ||
| /// </summary> | ||
| [Fact] | ||
| public void VerifyEnclavePublicKeyBinding_GenuineKey_Succeeds() | ||
| { | ||
| // Arrange | ||
| byte[] enclaveKey = Encoding.UTF8.GetBytes("genuine-enclave-public-key-blob"); | ||
| EnclaveReportPackage testPackage = BuildReportPackage(Sha256(enclaveKey)); | ||
| EnclavePublicKey testKey = new EnclavePublicKey(enclaveKey); | ||
|
|
||
| // Act / Assert | ||
| // Report commits to enclaveKey and the session uses enclaveKey: the binding holds, so no exception. | ||
| VirtualizationBasedSecurityEnclaveProviderBase.VerifyEnclavePublicKeyBinding(testPackage, testKey); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// A report whose committed data does not match the session's enclave public key is rejected. | ||
| /// </summary> | ||
| [Fact] | ||
| public void VerifyEnclavePublicKeyBinding_SwappedKey_Throws() | ||
| { | ||
| // Arrange | ||
| byte[] committedKeyBytes = Encoding.UTF8.GetBytes("committed-enclave-public-key-blob"); | ||
|
|
||
| // The signed report commits to committedKey... | ||
| EnclaveReportPackage testPackage = BuildReportPackage(Sha256(committedKeyBytes)); | ||
|
|
||
| // ...but a different enclave public key is offered for the session. | ||
| byte[] substitutedKeyBytes = Encoding.UTF8.GetBytes("substituted-enclave-public-key"); | ||
| EnclavePublicKey substitutedKey = new EnclavePublicKey(substitutedKeyBytes); | ||
|
|
||
|
|
||
| // Act | ||
| Action action = () => VirtualizationBasedSecurityEnclaveProviderBase.VerifyEnclavePublicKeyBinding( | ||
| testPackage, | ||
| substitutedKey); | ||
|
|
||
| // Assert | ||
| ArgumentException ex = Assert.Throws<ArgumentException>(action); | ||
| Assert.Equal(Strings.VerifyEnclaveKeyBindingFailed, ex.Message); | ||
| } | ||
|
|
||
| // Builds a minimal EnclaveReportPackage whose report EnclaveData begins with the given 32-byte | ||
| // binding value. The signature is empty because this targets the binding, not the report signature. | ||
| private static EnclaveReportPackage BuildReportPackage(byte[] enclaveDataFirst32) | ||
| { | ||
| byte[] payload = new byte[EnclaveReportPackageHeaderSize + EnclaveReportSize]; | ||
| int offset = 0; | ||
|
|
||
| void WriteUInt(uint value) | ||
| { | ||
| BitConverter.GetBytes(value).CopyTo(payload, offset); | ||
| offset += sizeof(uint); | ||
| } | ||
|
|
||
| // EnclaveReportPackageHeader | ||
| WriteUInt((uint)payload.Length); // PackageSize | ||
| WriteUInt(1); // Version | ||
| WriteUInt(1); // SignatureScheme | ||
| WriteUInt(EnclaveReportSize); // SignedStatementSize | ||
| WriteUInt(0); // SignatureSize | ||
| WriteUInt(0); // Reserved | ||
|
|
||
| // EnclaveReport | ||
| WriteUInt(EnclaveReportSize); // ReportSize | ||
| WriteUInt(1); // ReportVersion | ||
| Array.Copy(enclaveDataFirst32, 0, payload, offset, 32); // EnclaveData: first 32 bytes = SHA-256(public key) | ||
|
|
||
| return new EnclaveReportPackage(payload); | ||
| } | ||
|
|
||
| private static byte[] Sha256(byte[] data) | ||
| { | ||
| using SHA256 sha256 = SHA256.Create(); | ||
| return sha256.ComputeHash(data); | ||
| } | ||
| } |
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.