I'm currently implementing an TLS-connection for our communication between our mongo database (Version 4.2) and our software. This is my method I use to import all certificates from a file.
/// <summary> Reads an certificate file </summary>
/// <param name="path"> Path to the certificate </param>
/// <param name="password"> Password to access the file </param>
/// <returns></returns>
public static X509Certificate2Collection ReadCertificateFile(string path, string password) {
var certificateCollection = new X509Certificate2Collection();
certificateCollection.Import(path, password, X509KeyStorageFlags.Exportable);
return certificateCollection;
}
It works fine with every certificate format except .pem. As far as I can say the private key is in the PEM file but cannot be read.
I used openssl to convert the .pfx file to a .pem file with and without password. Neither of which worked because the private key wasn't imported. I installed .net 6 because they have implemented new methods to import pem files but the same happend there too.
I thank in advance everyone who is trying to help me.
I installed .net 6 because they have implemented new methods to import pem files
Those new methods are new, not modifying the existing ones. To load a cert and key from two PEM files is with X509Certificate2.ImportFromPemFile(pathToCert, pathToKey)
; if they're in the same file you can either specify the same value twice or leave off the second parameter (X509Certificate2.ImportFromPemFile(pathToJoinedPem)
)