X509Certificate Algorithm 'RC2' is not supported on this platform
See original GitHub issueDescription
Hi, we are planning to migrate a Xamarin project to MAUI when it’s going to be released. While experimenting with the MQTTnet library framework, I stumbled on this bug:
While creating a new instance of a System.Security.Cryptography.X509Certificates.X509Certificate2
using a password protected .pfx certificate file The following error occurs:
[DOTNET] at Internal.Cryptography.Pal.UnixPkcs12Reader.DecryptAndProcessSafeContents(ReadOnlySpan`1 password, CertBagAsn[]& certBags, AttributeA01-27 10:06:22.806 I/DOTNET ( 3826): ---> System.PlatformNotSupportedException: Algorithm 'RC2' is not supported on this platform.
[DOTNET] at System.Security.Cryptography.PasswordBasedEncryption.CreateRC2()
I don’t know if it ever worked in previous MAUI releases, but it works in a Xamarin Forms project using the 5.0.0.2337 release. It also works in a net6.0 console application.
Steps to Reproduce
- Create a File > New .NET MAUI App
- Add a valid password protected .pfx Certificate to the Embedded resources of the project
- Add theses “using” to the MainPage.xml.cs
using System.Reflection;
using System.Security.Cryptography.X509Certificates;
- Add theses lines of code to the MainPage Constructor
public MainPage()
{
InitializeComponent();
// Read certificates bytes
var assembly = typeof(App).GetTypeInfo().Assembly;
var mqttUserStream = assembly.GetManifestResourceStream("MauiApp1.pfxCertificate.pfx");
byte[] mqttUserBuffer;
using (var memoryStream = new MemoryStream())
{
mqttUserStream.CopyTo(memoryStream);
mqttUserBuffer = memoryStream.ToArray();
}
try
{
var clientCert = new X509Certificate2(mqttUserBuffer, "PFXFilePAsswordHere");
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
- Start the app in a Android Device or Emulator
- Observe the Exception thrown.
Version with bug
Preview 12 (current)
Last version that worked well
Unknown/Other
Affected platforms
Android, I was not able test on other platforms
Affected platform versions
Android 9 and up.
Did you find any workaround?
No response
Relevant log output
[DOTNET] System.Security.Cryptography.CryptographicException: The certificate data cannot be read with the provided password, the password may be incorrect.
[DOTNET] at Internal.Cryptography.Pal.UnixPkcs12Reader.DecryptAndProcessSafeContents(ReadOnlySpan`1 password, CertBagAsn[]& certBags, AttributeA01-27 10:06:22.806 I/DOTNET ( 3826): ---> System.PlatformNotSupportedException: Algorithm 'RC2' is not supported on this platform.
[DOTNET] at System.Security.Cryptography.PasswordBasedEncryption.CreateRC2()
[DOTNET] at System.Security.Cryptography.PasswordBasedEncryption.Decrypt(AlgorithmIdentifierAsn& algorithmIdentifier, ReadOnlySpan`1 password, ReadOnlySpan`1 passwordBytes, ReadOnlySpan`1 encryptedData, Span`1 destination)
[DOTNET] at Internal.Cryptography.Pal.UnixPkcs12Reader.DecryptSafeContents(ReadOnlySpan`1 password, ContentInfoAsn& safeContentsAsn)
[DOTNET] at Internal.Cryptography.Pal.UnixPkcs12Reader.DecryptAndProcessSafeContents(ReadOnlySpan`1 password, CertBagAsn[]& certBags, AttributeAsn[][]& certBagAttrs, Int32& certBagIdx, SafeBagAsn[]& keyBags, Int32& keyBagIdx)
[DOTNET] at Internal.Cryptography.Pal.UnixPkcs12Reader.Decrypt(ReadOnlySpan`1 password, ReadOnlyMemory`1 authSafeContents)
[DOTNET] at Internal.Cryptography.Pal.UnixPkcs12Reader.VerifyAndDecrypt(ReadOnlySpan`1 password, ReadOnlyMemory`1 authSafeContents)
[DOTNET] at Internal.Cryptography.Pal.UnixPkcs12Reader.Decrypt(SafePasswordHandle password, Boolean ephemeralSpecified)
[DOTNET] --- End of inner exception stack trace ---
[DOTNET] at Internal.Cryptography.Pal.UnixPkcs12Reader.Decrypt(SafePasswordHandle password, Boolean ephemeralSpecified)
[DOTNET] at Interna01-27 10:06:22.807 I/DOTNET ( 3826): at Internal.Cryptography.Pal.AndroidCertificatePal.ReadPkcs12(ReadOnlySpan`1 rawData, SafePasswordHandle password, Boolean ephemeralSpecified)
[DOTNET] at Internal.Cryptography.Pal.AndroidCertificatePal.FromBlob(ReadOnlySpan`1 rawData, SafePasswordHandle password, X509KeyStorageFlags keyStorageFlags)
[DOTNET] at Internal.Cryptography.Pal.CertificatePal.FromBlob(ReadOnlySpan`1 rawData, SafePasswordHandle password, X509KeyStorageFlags keyStorageFlags)
[DOTNET] at System.Security.Cryptography.X509Certificates.X509Certificate..ctor(Byte[] rawData, String password, X509KeyStorageFlags keyStorageFlags)
[DOTNET] at System.Security.Cryptography.X509Certificates.X509Certificate..ctor(Byte[] rawData, String password)
[DOTNET] at System.Security.Cryptography.X509Certificates.X509Certificate2..ctor(Byte[] rawData, String password)
[DOTNET] at MauiApp1.MainPage..ctor() in C:\Users\kpantelakis\source\repos\MauiApp1\MauiApp1\MainPage.xaml.cs:line 23
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (1 by maintainers)
Top Results From Across the Web
Read X509 certificate in android .net 6.0 application
So the questions are: Does anyone know why is this code unsupported? Could I somehow use x509 certificate in my .net 6.0 android...
Read more >X509Certificate (6.0 API Documentation)
X.509 certificates are described platform-independently by using the Abstract Syntax ... getInstance(algorithm,provider) is not available method Signature.
Read more >System.Security.Cryptography.X509Certificates 4.3.2
Provides types for reading, exporting and verifying Authenticode X.509 v3 certificates. These certificates are signed with a private key ...
Read more >X509Certificate.GetKeyAlgorithm Method (System.Security. ...
Returns the key algorithm information for this X.509v3 certificate as a string.
Read more >Security | Apple Developer Forums
Hi all, I am trying to get a mutual authentication (client authentication) connection working (sockets, not http and all local network currently) which...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Hi @KevPantelakis , would you like to try this:
LoadMauiAsset
method to MainPage class:The app doesn’t throw any exception for me.
Sounds like there’s a workaround here for supported API’s on MAUI, thanks for posting. Please open a new issue if necessary in the future.