简体   繁体   English

在Linux上使用dotnet核心获取私钥

[英]get private key with dotnet core on linux

I have a program that find a certificate into a store, and test if the rsa private key is present. 我有一个程序可以在商店中找到证书,并测试rsa私钥是否存在。

var store = (StoreName.CertificateAuthority, StoreLocation.LocalMachine);
store.Open(OpenFlags.ReadOnly);
X509Certificate2Collection certs = store.Certificates.Find(X509FindType.FindByThumbprint, "a02274af4e74defc0bf2ffb45e2d90bdbb1282f9", false);

if (certs.Count > 0)
{
    Console.WriteLine("Cert found");
    X509Certificate2 cert = certs[0];
    var rsa = cert.GetRSAPrivateKey();
    if (rsa == null)
        Console.WriteLine("rsa failed");
    else
        Console.WriteLine("rsa ok");
}

On Windows everything is fine with the certificate in pfx installed into the store. 在Windows上,将pfx中的证书安装到商店中就可以了。

On linux, the certificate is found, but the private key is not. 在linux上,找到了证书,但没有找到私钥。 I used this SO answer to convert my pfx to crt file : https://stackoverflow.com/a/16724275/1083225 and I put the crt file into /usr/local/share/ca-certificates , and executed update-ca-certificates If I look into the crt file, the rsa is present. 我用这个SO答案将我的pfx转换为crt文件: https ://stackoverflow.com/a/16724275/1083225,然后将crt文件放入/usr/local/share/ca-certificates ,并执行update-ca-certificates如果我查看crt文件,则表明存在rsa。

It's a .NETCoreApp 1.1 这是一个.NETCoreApp 1.1

The cert loader for LM\\Root on Linux only loads the files as X.509 DER or X.509 PEM, so no private key material is loaded. 在Linux上,用于LM \\ Root证书加载程序仅将文件加载为X.509 DER或X.509 PEM,因此不会加载私钥材料。

If you want a self-issued certificate to be root trusted, adding it to wherever your distro considers to be the root trust for OpenSSL is correct. 如果您希望自发行证书是根受信任的,则将其添加到您的发行版认为是OpenSSL根信任的任何位置都是正确的。 If you also need to access the private key associated with that certificate, you'll need to have it also be in a CurrentUser store, or for you to manually load it as a PFX. 如果您还需要访问与该证书关联的私钥,则需要使其也位于CurrentUser存储中,或者手动将其作为PFX加载。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM