-
Notifications
You must be signed in to change notification settings - Fork 413
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix tests for X509Certificate2 deprecation in net9.0
- Loading branch information
Keegan Caruso
committed
Aug 14, 2024
1 parent
3b23d7c
commit 941bc90
Showing
11 changed files
with
112 additions
and
67 deletions.
There are no files selected for viewing
This file contains 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 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 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
Large diffs are not rendered by default.
Oops, something went wrong.
27 changes: 13 additions & 14 deletions
27
test/Microsoft.IdentityModel.TestUtils/ReferenceMetadata.cs
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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,54 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
using System.Runtime.InteropServices; | ||
using System.Security; | ||
using System.Security.Cryptography.X509Certificates; | ||
|
||
namespace Microsoft.IdentityModel.TestUtils | ||
{ | ||
public static class X509Helper | ||
{ | ||
public static X509Certificate2 ConvertToX509Certificate(string base64Data) | ||
{ | ||
var base64Bytes = Convert.FromBase64String(base64Data); | ||
|
||
#if NET9_0_OR_GREATER | ||
return X509CertificateLoader.LoadCertificate(base64Bytes); | ||
#else | ||
return new X509Certificate2(base64Bytes); | ||
#endif | ||
} | ||
|
||
public static X509Certificate2 ConvertToX509Certificate(string base64Data, SecureString securePassword, X509KeyStorageFlags flags = X509KeyStorageFlags.DefaultKeySet) | ||
{ | ||
var password = ConvertFromSecureString(securePassword); | ||
return ConvertToX509Certificate(base64Data, password, flags); | ||
} | ||
|
||
public static X509Certificate2 ConvertToX509Certificate(string base64Data, string password, X509KeyStorageFlags flags = X509KeyStorageFlags.DefaultKeySet) | ||
{ | ||
var base64Bytes = Convert.FromBase64String(base64Data); | ||
#if NET9_0_OR_GREATER | ||
return X509CertificateLoader.LoadPkcs12(base64Bytes, password, flags); | ||
#else | ||
return new X509Certificate2(base64Bytes, password); | ||
#endif | ||
} | ||
|
||
private static string ConvertFromSecureString(SecureString secureString) | ||
{ | ||
IntPtr valuePtr = IntPtr.Zero; | ||
try | ||
{ | ||
valuePtr = Marshal.SecureStringToGlobalAllocUnicode(secureString); | ||
return Marshal.PtrToStringUni(valuePtr); | ||
} | ||
finally | ||
{ | ||
Marshal.ZeroFreeGlobalAllocUnicode(valuePtr); | ||
} | ||
} | ||
} | ||
} |
This file contains 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 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 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.