-
Notifications
You must be signed in to change notification settings - Fork 382
MSI POP Attestation #5612
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
Robbie-Microsoft
wants to merge
8
commits into
main
Choose a base branch
from
rginsburg/msiv2_pop_attestation
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
MSI POP Attestation #5612
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
cf72c38
Implemented feature request
Robbie-Microsoft 56889d7
Merge branch 'main' into rginsburg/msiv2_pop_attestation
Robbie-Microsoft ba29c76
renamed attestation package
Robbie-Microsoft 595aaa8
Removed MtlsPop package and updated references
Robbie-Microsoft 7acfbed
Removed references to mtlspop package
Robbie-Microsoft f9324df
changed visiblity of attestation
Robbie-Microsoft fac7299
Fixed empty attestation bug
Robbie-Microsoft 9db5690
Merge branch 'main' into rginsburg/msiv2_pop_attestation
Robbie-Microsoft 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
85 changes: 6 additions & 79 deletions
85
src/client/Microsoft.Identity.Client.MtlsPop/ManagedIdentityPopExtensions.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 |
|---|---|---|
| @@ -1,82 +1,9 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System.Runtime.InteropServices; | ||
| using Microsoft.Identity.Client.MtlsPop.Attestation; | ||
| using Microsoft.Identity.Client.PlatformsCommon.Shared; | ||
|
|
||
| namespace Microsoft.Identity.Client.MtlsPop | ||
| { | ||
| /// <summary> | ||
| /// Registers the mTLS PoP attestation runtime (interop) by installing a provider | ||
| /// function into MSAL's internal config. | ||
| /// </summary> | ||
| public static class ManagedIdentityPopExtensions | ||
| { | ||
| /// <summary> | ||
| /// App-level registration: tells MSAL how to obtain a KeyGuard/CNG handle | ||
| /// and perform attestation to get the JWT needed for mTLS PoP. | ||
| /// </summary> | ||
| public static AcquireTokenForManagedIdentityParameterBuilder WithMtlsProofOfPossession( | ||
| this AcquireTokenForManagedIdentityParameterBuilder builder) | ||
| { | ||
| void MtlsNotSupportedForManagedIdentity(string message) | ||
| { | ||
| throw new MsalClientException( | ||
| MsalError.MtlsNotSupportedForManagedIdentity, | ||
| message); | ||
| } | ||
|
|
||
| if (!DesktopOsHelper.IsWindows()) | ||
| { | ||
| MtlsNotSupportedForManagedIdentity(MsalErrorMessage.MtlsNotSupportedForNonWindowsMessage); | ||
| } | ||
|
|
||
| #if NET462 | ||
| MtlsNotSupportedForManagedIdentity(MsalErrorMessage.MtlsNotSupportedForManagedIdentityMessage); | ||
| #endif | ||
|
|
||
| builder.CommonParameters.IsMtlsPopRequested = true; | ||
| AddRuntimeSupport(builder); | ||
| return builder; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Adds the runtime support by registering the attestation function. | ||
| /// </summary> | ||
| /// <param name="builder"></param> | ||
| /// <exception cref="MsalClientException"></exception> | ||
| private static void AddRuntimeSupport( | ||
| AcquireTokenForManagedIdentityParameterBuilder builder) | ||
| { | ||
| // Register the "runtime" function that PoP operation will invoke. | ||
| builder.CommonParameters.AttestationTokenProvider = | ||
| async (req, ct) => | ||
| { | ||
| // 1) Get the caller-provided KeyGuard/CNG handle | ||
| SafeHandle keyHandle = req.KeyHandle; | ||
|
|
||
| // 2) Call the native interop via PopKeyAttestor | ||
| AttestationResult attestationResult = await PopKeyAttestor.AttestKeyGuardAsync( | ||
| req.AttestationEndpoint.AbsoluteUri, // expects string | ||
| keyHandle, | ||
| req.ClientId ?? string.Empty, | ||
| ct).ConfigureAwait(false); | ||
|
|
||
| // 3) Map to MSAL's internal response | ||
| if (attestationResult != null && | ||
| attestationResult.Status == AttestationStatus.Success && | ||
| !string.IsNullOrWhiteSpace(attestationResult.Jwt)) | ||
| { | ||
| return new ManagedIdentity.AttestationTokenResponse { AttestationToken = attestationResult.Jwt }; | ||
| } | ||
|
|
||
| throw new MsalClientException( | ||
| "attestation_failure", | ||
| $"Key Attestation failed " + | ||
| $"(status={attestationResult?.Status}, " + | ||
| $"code={attestationResult?.NativeErrorCode}). {attestationResult?.ErrorMessage}"); | ||
| }; | ||
| } | ||
| } | ||
| } | ||
| // This file intentionally left empty. | ||
| // The WithMtlsProofOfPossession extension method has been moved to the main MSAL package: | ||
| // Microsoft.Identity.Client.ManagedIdentityPopExtensions.WithMtlsProofOfPossession() | ||
| // | ||
| // For attestation support, reference the Msal.KeyAttestation package and call: | ||
| // .WithAttestationSupport() | ||
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 |
|---|---|---|
| @@ -1,3 +0,0 @@ | ||
| Microsoft.Identity.Client.MtlsPop.ManagedIdentityPopExtensions | ||
| static Microsoft.Identity.Client.MtlsPop.ManagedIdentityPopExtensions.WithMtlsProofOfPossession(this Microsoft.Identity.Client.AcquireTokenForManagedIdentityParameterBuilder builder) -> Microsoft.Identity.Client.AcquireTokenForManagedIdentityParameterBuilder | ||
|
|
||
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 |
|---|---|---|
| @@ -1,3 +0,0 @@ | ||
| Microsoft.Identity.Client.MtlsPop.ManagedIdentityPopExtensions | ||
| static Microsoft.Identity.Client.MtlsPop.ManagedIdentityPopExtensions.WithMtlsProofOfPossession(this Microsoft.Identity.Client.AcquireTokenForManagedIdentityParameterBuilder builder) -> Microsoft.Identity.Client.AcquireTokenForManagedIdentityParameterBuilder | ||
|
|
||
40 changes: 40 additions & 0 deletions
40
src/client/Microsoft.Identity.Client/ManagedIdentity/ManagedIdentityPopExtensions.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,40 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using Microsoft.Identity.Client.PlatformsCommon.Shared; | ||
|
|
||
| namespace Microsoft.Identity.Client | ||
| { | ||
| /// <summary> | ||
| /// Extension methods for enabling mTLS Proof-of-Possession in managed identity flows. | ||
| /// </summary> | ||
| public static class ManagedIdentityPopExtensions | ||
| { | ||
| /// <summary> | ||
| /// Enables mTLS Proof-of-Possession for managed identity token acquisition. | ||
| /// When attestation is required (KeyGuard scenarios), use the Msal.KeyAttestation package | ||
| /// and call .WithAttestationSupport() after this method. | ||
| /// </summary> | ||
| /// <param name="builder">The AcquireTokenForManagedIdentityParameterBuilder instance.</param> | ||
| /// <returns>The builder to chain .With methods.</returns> | ||
| public static AcquireTokenForManagedIdentityParameterBuilder WithMtlsProofOfPossession( | ||
| this AcquireTokenForManagedIdentityParameterBuilder builder) | ||
| { | ||
| if (!DesktopOsHelper.IsWindows()) | ||
| { | ||
| throw new MsalClientException( | ||
| MsalError.MtlsNotSupportedForManagedIdentity, | ||
| MsalErrorMessage.MtlsNotSupportedForNonWindowsMessage); | ||
| } | ||
|
|
||
| #if NET462 | ||
| throw new MsalClientException( | ||
| MsalError.MtlsNotSupportedForManagedIdentity, | ||
| MsalErrorMessage.MtlsNotSupportedForManagedIdentityMessage); | ||
| #else | ||
| builder.CommonParameters.IsMtlsPopRequested = true; | ||
| return builder; | ||
| #endif | ||
| } | ||
| } | ||
| } |
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
2 changes: 2 additions & 0 deletions
2
src/client/Microsoft.Identity.Client/PublicApi/net462/PublicAPI.Unshipped.txt
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 |
|---|---|---|
| @@ -1,2 +1,4 @@ | ||
| const Microsoft.Identity.Client.MsalError.ManagedIdentityAllSourcesUnavailable = "managed_identity_all_sources_unavailable" -> string | ||
| Microsoft.Identity.Client.ManagedIdentityApplication.GetManagedIdentitySourceAsync(System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.Task<Microsoft.Identity.Client.ManagedIdentity.ManagedIdentitySource> | ||
| Microsoft.Identity.Client.ManagedIdentityPopExtensions | ||
| static Microsoft.Identity.Client.ManagedIdentityPopExtensions.WithMtlsProofOfPossession(this Microsoft.Identity.Client.AcquireTokenForManagedIdentityParameterBuilder builder) -> Microsoft.Identity.Client.AcquireTokenForManagedIdentityParameterBuilder |
2 changes: 2 additions & 0 deletions
2
src/client/Microsoft.Identity.Client/PublicApi/net472/PublicAPI.Unshipped.txt
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 |
|---|---|---|
| @@ -1,2 +1,4 @@ | ||
| const Microsoft.Identity.Client.MsalError.ManagedIdentityAllSourcesUnavailable = "managed_identity_all_sources_unavailable" -> string | ||
| Microsoft.Identity.Client.ManagedIdentityApplication.GetManagedIdentitySourceAsync(System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.Task<Microsoft.Identity.Client.ManagedIdentity.ManagedIdentitySource> | ||
| Microsoft.Identity.Client.ManagedIdentityPopExtensions | ||
| static Microsoft.Identity.Client.ManagedIdentityPopExtensions.WithMtlsProofOfPossession(this Microsoft.Identity.Client.AcquireTokenForManagedIdentityParameterBuilder builder) -> Microsoft.Identity.Client.AcquireTokenForManagedIdentityParameterBuilder |
2 changes: 2 additions & 0 deletions
2
src/client/Microsoft.Identity.Client/PublicApi/net8.0-android/PublicAPI.Unshipped.txt
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 |
|---|---|---|
| @@ -1,2 +1,4 @@ | ||
| const Microsoft.Identity.Client.MsalError.ManagedIdentityAllSourcesUnavailable = "managed_identity_all_sources_unavailable" -> string | ||
| Microsoft.Identity.Client.ManagedIdentityApplication.GetManagedIdentitySourceAsync(System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.Task<Microsoft.Identity.Client.ManagedIdentity.ManagedIdentitySource> | ||
| Microsoft.Identity.Client.ManagedIdentityPopExtensions | ||
| static Microsoft.Identity.Client.ManagedIdentityPopExtensions.WithMtlsProofOfPossession(this Microsoft.Identity.Client.AcquireTokenForManagedIdentityParameterBuilder! builder) -> Microsoft.Identity.Client.AcquireTokenForManagedIdentityParameterBuilder! |
2 changes: 2 additions & 0 deletions
2
src/client/Microsoft.Identity.Client/PublicApi/net8.0-ios/PublicAPI.Unshipped.txt
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 |
|---|---|---|
| @@ -1,2 +1,4 @@ | ||
| const Microsoft.Identity.Client.MsalError.ManagedIdentityAllSourcesUnavailable = "managed_identity_all_sources_unavailable" -> string | ||
| Microsoft.Identity.Client.ManagedIdentityApplication.GetManagedIdentitySourceAsync(System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.Task<Microsoft.Identity.Client.ManagedIdentity.ManagedIdentitySource> | ||
| Microsoft.Identity.Client.ManagedIdentityPopExtensions | ||
| static Microsoft.Identity.Client.ManagedIdentityPopExtensions.WithMtlsProofOfPossession(this Microsoft.Identity.Client.AcquireTokenForManagedIdentityParameterBuilder! builder) -> Microsoft.Identity.Client.AcquireTokenForManagedIdentityParameterBuilder! |
2 changes: 2 additions & 0 deletions
2
src/client/Microsoft.Identity.Client/PublicApi/net8.0/PublicAPI.Unshipped.txt
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 |
|---|---|---|
| @@ -1,2 +1,4 @@ | ||
| const Microsoft.Identity.Client.MsalError.ManagedIdentityAllSourcesUnavailable = "managed_identity_all_sources_unavailable" -> string | ||
| Microsoft.Identity.Client.ManagedIdentityApplication.GetManagedIdentitySourceAsync(System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.Task<Microsoft.Identity.Client.ManagedIdentity.ManagedIdentitySource> | ||
| Microsoft.Identity.Client.ManagedIdentityPopExtensions | ||
| static Microsoft.Identity.Client.ManagedIdentityPopExtensions.WithMtlsProofOfPossession(this Microsoft.Identity.Client.AcquireTokenForManagedIdentityParameterBuilder builder) -> Microsoft.Identity.Client.AcquireTokenForManagedIdentityParameterBuilder |
2 changes: 2 additions & 0 deletions
2
src/client/Microsoft.Identity.Client/PublicApi/netstandard2.0/PublicAPI.Unshipped.txt
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 |
|---|---|---|
| @@ -1,2 +1,4 @@ | ||
| const Microsoft.Identity.Client.MsalError.ManagedIdentityAllSourcesUnavailable = "managed_identity_all_sources_unavailable" -> string | ||
| Microsoft.Identity.Client.ManagedIdentityApplication.GetManagedIdentitySourceAsync(System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.Task<Microsoft.Identity.Client.ManagedIdentity.ManagedIdentitySource> | ||
| Microsoft.Identity.Client.ManagedIdentityPopExtensions | ||
| static Microsoft.Identity.Client.ManagedIdentityPopExtensions.WithMtlsProofOfPossession(this Microsoft.Identity.Client.AcquireTokenForManagedIdentityParameterBuilder builder) -> Microsoft.Identity.Client.AcquireTokenForManagedIdentityParameterBuilder |
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,111 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System; | ||
| using System.Runtime.InteropServices; | ||
| using Microsoft.Win32.SafeHandles; | ||
|
|
||
| namespace Msal.KeyAttestation | ||
| { | ||
| /// <summary> | ||
| /// Managed façade for <c>AttestationClientLib.dll</c>. Holds initialization state, | ||
| /// does ref-count hygiene on <see cref="SafeNCryptKeyHandle"/>, and returns a JWT. | ||
| /// </summary> | ||
| internal sealed class AttestationClient : IDisposable | ||
| { | ||
| private bool _initialized; | ||
|
|
||
| /// <summary> | ||
| /// AttestationClient constructor. Relies on the default OS loader to locate the native DLL. | ||
| /// </summary> | ||
| /// <exception cref="InvalidOperationException"></exception> | ||
| public AttestationClient() | ||
| { | ||
| string dllError = NativeDiagnostics.ProbeNativeDll(); | ||
| // intentionally not throwing on dllError | ||
|
|
||
| // Load & initialize (logger is required by native lib) | ||
| var info = new AttestationClientLib.AttestationLogInfo | ||
| { | ||
| Log = AttestationLogger.ConsoleLogger, | ||
| Ctx = IntPtr.Zero | ||
| }; | ||
|
|
||
| _initialized = AttestationClientLib.InitAttestationLib(ref info) == 0; | ||
| if (!_initialized) | ||
| throw new InvalidOperationException("Failed to initialize AttestationClientLib."); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Calls the native <c>AttestKeyGuardImportKey</c> and returns a structured result. | ||
| /// </summary> | ||
| public AttestationResult Attest(string endpoint, | ||
| SafeNCryptKeyHandle keyHandle, | ||
| string clientId) | ||
| { | ||
| if (!_initialized) | ||
| return new(AttestationStatus.NotInitialized, null, -1, | ||
| "Native library not initialized."); | ||
|
|
||
| IntPtr buf = IntPtr.Zero; | ||
| bool addRef = false; | ||
|
|
||
| try | ||
| { | ||
| keyHandle.DangerousAddRef(ref addRef); | ||
|
|
||
| int rc = AttestationClientLib.AttestKeyGuardImportKey( | ||
| endpoint, null, null, keyHandle, out buf, clientId); | ||
|
|
||
| if (rc != 0) | ||
| return new(AttestationStatus.NativeError, null, rc, null); | ||
|
|
||
| if (buf == IntPtr.Zero) | ||
| return new(AttestationStatus.TokenEmpty, null, 0, | ||
| "rc==0 but token buffer was null."); | ||
|
|
||
| string jwt = Marshal.PtrToStringAnsi(buf)!; | ||
| return new(AttestationStatus.Success, jwt, 0, null); | ||
| } | ||
| catch (DllNotFoundException ex) | ||
| { | ||
| return new(AttestationStatus.Exception, null, -1, | ||
| $"Native DLL not found: {ex.Message}"); | ||
| } | ||
| catch (BadImageFormatException ex) | ||
| { | ||
| return new(AttestationStatus.Exception, null, -1, | ||
| $"Architecture mismatch (x86/x64) or corrupted DLL: {ex.Message}"); | ||
| } | ||
| catch (SEHException ex) | ||
| { | ||
| return new(AttestationStatus.Exception, null, -1, | ||
| $"Native library raised SEHException: {ex.Message}"); | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| return new(AttestationStatus.Exception, null, -1, ex.Message); | ||
| } | ||
| finally | ||
| { | ||
| if (buf != IntPtr.Zero) | ||
| AttestationClientLib.FreeAttestationToken(buf); | ||
| if (addRef) | ||
| keyHandle.DangerousRelease(); | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Disposes the client, releasing any resources and un-initializing the native library. | ||
| /// </summary> | ||
| public void Dispose() | ||
| { | ||
| if (_initialized) | ||
| { | ||
| AttestationClientLib.UninitAttestationLib(); | ||
| _initialized = false; | ||
| } | ||
| GC.SuppressFinalize(this); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
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.