"System.FormatException: Only integer can be converted" when creating X509Certificate2 object from .pfx file

36 Views Asked by At

I am trying to add a pfx certificate from c# using the X509Certificate2 constructor. The certificate was creating using these two commands from OpenSSL:

req -x509 -sha256 -newkey rsa:2048 -keyout keyname.key -out certificatename.crt -days 365 and pkcs12 -export -in C:\path\to\certificatename.crt -inkey C:\path\to\keyname.key -out C:\path\to\newpkcs12.pfx

My C# code looks like this

X509Store store = new X509Store("TestStore", StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadWrite);
string certPath = /Certificate/" + "mycertificate.pfx";

X509Certificate2 certificate = new X509Certificate2(certPath, "12345678");

The line with the X509Certificate2 constructor throws the error S"System.FormatException: Only integer can be converted\r\n at Mono.Security.ASN1Convert.ToInt32 (Mono.Security.ASN1 asn1) [0x00017] in <ca59a709f9e24fa0adef388a18d30264>:0 \r\n at Mono.Security.X509.PKCS12.Decrypt (Mono.Security.PKCS7+EncryptedData ed) [0x00033] in <ca59a709f9e24fa0adef388a18d30264>:0 \r\n at Mono.Security.X509.PKCS12.Decode (System.Byte[] data) [0x00271] in <ca59a709f9e24fa0adef388a18d30264>:0 \r\n at Mono.Security.X509.PKCS12..ctor (System.Byte[] data, System.String password) [0x0000d] in <ca59a709f9e24fa0adef388a18d30264>:0 \r\n at System.Security.Cryptography.X509Certificates.X509Certificate2ImplMono.ImportPkcs12 (System.Byte[] rawData, System.String password) [0x00023] in <d054a9182977441aa432503a474315ba>:0 \r\n at System.Security.Cryptography.X509Certificates.X509Certificate2ImplMono.ImportPkcs12 (System.Byte[] rawData, Microsoft.Win32.SafeHandles.SafePasswordHandle password) [0x0001b] in <d054a9182977441aa432503a474315ba>:0 \r\n at System.Security.Cryptography.X509Certificates.X509Certificate2ImplMono..ctor (System.Byte[] rawData, Microsoft.Win32.SafeHandles.SafePasswordHandle password, System.Security.Cryptography.X509Certificates.X509KeyStorageFlags keyStorageFlags) [0x0002f] in <d054a9182977441aa432503a474315ba>:0 \r\n at Mono.X509PalImpl.ImportFallback (System.Byte[] data, Microsoft.Win32.SafeHandles.SafePasswordHandle password, System.Security.Cryptography.X509Certificates.X509KeyStorageFlags keyStorageFlags) [0x00000] in <d054a9182977441aa432503a474315ba>:0 \r\n at Mono.X509PalImplMono.Import (System.Byte[] data, Microsoft.Win32.SafeHandles.SafePasswordHandle password, System.Security.Cryptography.X509Certificates.X509KeyStorageFlags keyStorageFlags) [0x00000] in <d054a9182977441aa432503a474315ba>:0 \r\n at Mono.SystemCertificateProvider.Import (System.Byte[] data, Microsoft.Win32.SafeHandles.SafePasswordHandle password, System.Security.Cryptography.X509Certificates.X509KeyStorageFlags keyStorageFlags, Mono.CertificateImportFlags importFlags) [0x00017] in <d054a9182977441aa432503a474315ba>:0 \r\n at Mono.SystemCertificateProvider.Mono.ISystemCertificateProvider.Import (System.Byte[] data, Microsoft.Win32.SafeHandles.SafePasswordHandle password, System.Security.Cryptography.X509Certificates.X509KeyStorageFlags keyStorageFlags, Mono.CertificateImportFlags importFlags) [0x00000] in <d054a9182977441aa432503a474315ba>:0 \r\n at System.Security.Cryptography.X509Certificates.X509Helper.Import (System.Byte[] rawData, Microsoft.Win32.SafeHandles.SafePasswordHandle password, System.Security.Cryptography.X509Certificates.X509KeyStorageFlags keyStorageFlags) [0x00005] in <787acc3c9a4c471ba7d971300105af24>:0 \r\n at System.Security.Cryptography.X509Certificates.X509Certificate..ctor (System.String fileName, System.String password, System.Security.Cryptography.X509Certificates.X509KeyStorageFlags keyStorageFlags) [0x0003e] in <787acc3c9a4c471ba7d971300105af24>:0 \r\n at System.Security.Cryptography.X509Certificates.X509Certificate..ctor (System.String fileName, System.String password) [0x00000] in <787acc3c9a4c471ba7d971300105af24>:0 \r\n at System.Security.Cryptography.X509Certificates.X509Certificate2..ctor (System.String fileName, System.String password) [0x00000] in <d054a9182977441aa432503a474315ba>:0

I am able to verify that my password is correct by installing the certificate with the windows certificate wizard. I am also able to verify that the certificate file is at the supplied path.

I have tried a password with and without numbers.

At this point I am starting to think it is a bug with the windows certificate library as I cannot find a lot of information on this error online.

0

There are 0 best solutions below