I was recently playing with SSL certificates in dotnet, and found myself in a position of needing to import a custom certificate. I created my custom CA, imported the file and in Windows, this ran fine:
var caCrt = new X509Certificate2(path);
When I ran the same code under a Docker Linux container, I got the rather confusing error:
System.Security.Cryptography.CryptographicException: 'ASN1 corrupted data.' Inner Exception AsnContentException: The encoded length exceeds the maximum supported by this library (Int32.MaxValue).
There appears to be different handling of certificates in Windows and Linux, and the following code was a work around that could use regular certificates and worked on both platforms. It loads the certificate, trims the header and footer, and then converts it from a B64 string to a byte array so you can use the working X509Certificate2 constructor. Use at your own peril :-)
var certData = File.ReadAllLines(path); var trimmedCertData = string.Join("", certData.Skip(1).SkipLast(1)); var certBytes = Convert.FromBase64String(trimmedCertData); var caCrt = new X509Certificate2(certBytes);