0

So I'm trying to open a connection to Postgres using C# and npgsql Our system rules require sslmode to be VerifyFull. There's an example in another language that I'm trying to copy - basically it sets the Root Certificate to a given file.

so I take the file and try to do the same thing - but it fails - and when I dig into the code - npgsql calls X509Certificate2.CreateFromPemFile.

  • if I try to load up my certificate with

    X509Certificate2.CreateFromPem( File.ReadAllText( file )) it works fine.

  • if I try to load up my certificate with

    X509Certificate2.CreateFromPemFile( file ) it doesn't work

  • and that's because underneath, it calls CreateFromPem( text, text) where text is read from the file

  • and my certificate doesn't have a key

so to summarise

  • that certificate is working to that database with code in another language
  • the ssl root certificate shouldn't need a key
  • but it seems impossible for npgsqlconnection to load a certificate that doesn't have a key
  • and I don't have a key

the error message is:

"The key contents do not contain a PEM, the content is malformed, or the key does not match the certificate." Is there any way to fix this?

For more details - if I write the following test:

    var certificateFileName = @"C:\temp\blah.pem";
    //var decoded2 = X509Certificate2.CreateFromPemFile(certifcateFileName);
    var contents = File.ReadAllText(certificateFileName);
    var decoded = X509Certificate2.CreateFromPem( contents, null);
    Assert.False( decoded.HasPrivateKey);
    var bytes = decoded.Export(X509ContentType.Cert);
    File.WriteAllBytes(@"C:\temp\blah2.pem",  Encoding.UTF8.GetBytes( new string(PemEncoding.Write("CERTIFICATE", bytes))));
    var decoded3 = X509Certificate2.CreateFromPemFile(@"C:\temp\blah2.pem");

throws an exception on the last line with any certificate I try. Works fine if the certificate has a key

5
  • a link to the other language and your attempts including full error message would help Commented Oct 16, 2022 at 9:24
  • it's an internal ruby app - so it's hardly something I can link to - but the error message is: "The key contents do not contain a PEM, the content is malformed, or the key does not match the certificate." which is easily reproducible if I try to do a CreateFromPemFile for any .pem with only a certificate in it. Commented Oct 16, 2022 at 10:44
  • File.ReadAllText() is using ASCII Encoding while CreateFromPemFile() is not using ASCII. ASCII encoding is removing non printable characters. Commented Oct 16, 2022 at 13:53
  • no we can't reproduce your error as it basically works see stackoverflow.com/questions/39009311/… but it looks more like the certificate you generate isn't ok. see learn.microsoft.com/en-us/dotnet/api/… for more informaton Commented Oct 16, 2022 at 17:15
  • I have the same issue, could you find any solutions? Commented Aug 13, 2023 at 0:15

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.