X509Certificate2Collection.Import() fails with Internal.Cryptography.CryptoThrowHelper+WindowsCryptographicException: An internal error occurred
Description
When calling X509Certificate2Collection.Import() with a valid p12 file, we receive the error:
Internal.Cryptography.CryptoThrowHelper+WindowsCryptographicException: An internal error occurred
The issue is that the Import() appears unable to handle secret bag attributes in the p12 file, that use pbkdf2/AES. The rfc allows secret bag attributes in the p12, and Import() worked fine before when the p12 had secret bag attributes using older crypto.
Reproduction Steps
using System; using System.IO; using System.Security.Cryptography.X509Certificates;
class Program { static void Main() { var certPath = @“C:\temp\ewallet.p12”; var password = “[Redacted]”;
try
{
var allData = File.ReadAllBytes(certPath);
var x509Collection = new X509Certificate2Collection();
x509Collection.Import(allData, password, X509KeyStorageFlags.DefaultKeySet | X509KeyStorageFlags.PersistKeySet);
Console.WriteLine("Wallet imported Successfully");
}
catch (Exception ex)
{
Console.WriteLine($"Failed to import wallet data: {ex}");
}
} }
Exception:
Internal.Cryptography.CryptoThrowHelper+WindowsCryptographicException: An internal error occurred. at Internal.Cryptography.Pal.StorePal.FromBlobOrFile(Byte[] rawData, String fileName, SafePasswordHandle password, X509KeyStorageFlags keyStorageFlags) at System.Security.Cryptography.X509Certificates.X509Certificate2Collection.Import(Byte[] rawData, String password, X509KeyStorageFlags keyStorageFlags) at Program.Main() in C:\ADE\tihong_odp218\odpnet\odpm\test\ConsoleApp1\Program.cs:line 16
Expected behavior
Return the X509Certificate2Collection associated with the p12 file.
Actual behavior
Exception:
Internal.Cryptography.CryptoThrowHelper+WindowsCryptographicException: An internal error occurred. at Internal.Cryptography.Pal.StorePal.FromBlobOrFile(Byte[] rawData, String fileName, SafePasswordHandle password, X509KeyStorageFlags keyStorageFlags) at System.Security.Cryptography.X509Certificates.X509Certificate2Collection.Import(Byte[] rawData, String password, X509KeyStorageFlags keyStorageFlags) at Program.Main() in C:\ADE\tihong_odp218\odpnet\odpm\test\ConsoleApp1\Program.cs:line 16
Regression?
Works on Windows Server 2022, fails on 2012r2.
Known Workarounds
None.
Configuration
- Windows 2012 R2
- x64
- .NET Framework v4.8
- .NET 6
Wallet file that will recreate problem is attached (rename as p12 file) ewallet.p12.log
Other information
No response
Tagging subscribers to this area: @dotnet/area-system-security, @bartonjs, @vcsjones See info in area-owners.md if you want to be subscribed.
Issue Details
Description
When calling X509Certificate2Collection.Import() with a valid p12 file, we receive the error:
Internal.Cryptography.CryptoThrowHelper+WindowsCryptographicException: An internal error occurred
The issue is that the Import() appears unable to handle secret bag attributes in the p12 file, that use pbkdf2/AES. The rfc allows secret bag attributes in the p12, and Import() worked fine before when the p12 had secret bag attributes using older crypto.
Reproduction Steps
using System; using System.IO; using System.Security.Cryptography.X509Certificates;
class Program { static void Main() { var certPath = @“C:\temp\ewallet.p12”; var password = “[Redacted]”;
try
{
var allData = File.ReadAllBytes(certPath);
var x509Collection = new X509Certificate2Collection();
x509Collection.Import(allData, password, X509KeyStorageFlags.DefaultKeySet | X509KeyStorageFlags.PersistKeySet);
Console.WriteLine("Wallet imported Successfully");
}
catch (Exception ex)
{
Console.WriteLine($"Failed to import wallet data: {ex}");
}
} }
Exception:
Internal.Cryptography.CryptoThrowHelper+WindowsCryptographicException: An internal error occurred. at Internal.Cryptography.Pal.StorePal.FromBlobOrFile(Byte[] rawData, String fileName, SafePasswordHandle password, X509KeyStorageFlags keyStorageFlags) at System.Security.Cryptography.X509Certificates.X509Certificate2Collection.Import(Byte[] rawData, String password, X509KeyStorageFlags keyStorageFlags) at Program.Main() in C:\ADE\tihong_odp218\odpnet\odpm\test\ConsoleApp1\Program.cs:line 16
Expected behavior
Return the X509Certificate2Collection associated with the p12 file.
Actual behavior
Exception:
Internal.Cryptography.CryptoThrowHelper+WindowsCryptographicException: An internal error occurred. at Internal.Cryptography.Pal.StorePal.FromBlobOrFile(Byte[] rawData, String fileName, SafePasswordHandle password, X509KeyStorageFlags keyStorageFlags) at System.Security.Cryptography.X509Certificates.X509Certificate2Collection.Import(Byte[] rawData, String password, X509KeyStorageFlags keyStorageFlags) at Program.Main() in C:\ADE\tihong_odp218\odpnet\odpm\test\ConsoleApp1\Program.cs:line 16
Regression?
Works on Windows Server 2022, fails on 2012r2.
Known Workarounds
None.
Configuration
- Windows 2012 R2
- x64
- .NET Framework v4.8
- .NET 6
Wallet file that will recreate problem is attached (rename as p12 file) ewallet.p12.log
Other information
No response
| Author: | ScotMac |
|---|---|
| Assignees: | - |
| Labels: |
|
| Milestone: | - |
Works on Windows Server 2022, fails on 2012r2.
Importing PKCS12 on Windows is deferred to the PFXImportCertStore Win32 function.
If memory serves correctly, Windows 10 / Server 2016 is the first version that supports AES256-SHA256.
If you need to process these PKCS12 inputs, you can use the Pkcs12Info class. This class is a lower-level primitive that allows reading through the individual bag items, but it should work for all algorithms.
Yes, it appears that 2012r2/2016 don't have support for AES256/SHA256: https://learn.microsoft.com/en-us/answers/questions/1054881/windows-server-2016-2012r2-how-to-add-support-for
Yes, it appears that 2012r2/2016
I believe Win32 APIs support it in Windows 10 / Server 2016 version 1703 or later. It was just not exposed in the GUI for exporting until later.
I think we can close this issue as answered. To summarize:
X509Certificate2Collection.Importuses the Win32 APIPFXImportCertStoreto import the PKCS12 file.- Win32 does not support the cipher used until Windows 10.
Pkcs12Infocan be used to work around the OS limitation.
The error is unfortunately, not helpful, but that's the error we get from Windows.
Feel free to re-open the issue if you believe there are additional issues to address.
Yes, except pkcs12info can't be used to work around the issue for framework, since it isn't implemented for framework, only Core.