Skip to content

X509Certificate2: Unclear error message when file does not exist #18251

Description

@qmfrederik

This issue is similar to #17601

An exception with an obscure error message is thrown when creating a X509Certificate2 using a path to a file which does not exist.

Consider the following code:

using System;
using System.Security.Cryptography.X509Certificates;

namespace ConsoleApplication
{
    public class Program
    {
        public static void Main(string[] args)
        {
            new X509Certificate2(fileName: "invalid.cer");
        }
    }
}

on Windows, the following exception is thrown:

Unhandled Exception: Internal.Cryptography.CryptoThrowHelper+WindowsCryptographicException: The system cannot find the file specified
   at Internal.Cryptography.Pal.CertificatePal.FromBlobOrFile(Byte[] rawData, String fileName, String password, X509KeyStorageFlags keyStorageFlags)
   at System.Security.Cryptography.X509Certificates.X509Certificate..ctor(String fileName, String password, X509KeyStorageFlags keyStorageFlags)
   at System.Security.Cryptography.X509Certificates.X509Certificate2..ctor(String fileName)
   at ConsoleApplication.Program.Main(String[] args)

whereas the following exception is thrown on Linux and OS X:

nhandled Exception: Interop+Crypto+OpenSslCryptographicException: error:2006D080:BIO routines:BIO_new_file:no such file
   at Interop.Crypto.CheckValidOpenSslHandle(SafeHandle handle)
   at Internal.Cryptography.Pal.CertificatePal.FromFile(String fileName, String password, X509KeyStorageFlags keyStorageFlags)
   at System.Security.Cryptography.X509Certificates.X509Certificate..ctor(String fileName, String password, X509KeyStorageFlags keyStorageFlags)
   at System.Security.Cryptography.X509Certificates.X509Certificate2..ctor(String fileName)
   at ConsoleApplication.Program.Main(String[] args)

As such, this confirms to the MSDN specifications of the X509Certificate2 constructor because all it says is that a CryptographicException should be thrown - which is the case.

It seems that the error message contains too much information - I'd argue it should only read "no such file" or a user-friendly string, and the underlying OpenSSL error code (2006D080) could go on a different property of the OpenSslCryptographicException class.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions