Is there a way to get the private key in pem fromat from schannel::cert_context::PrivateKey

170 Views Asked by At

I'm trying to get the private key of certificates of my Windows Cert Store. I'm doing it with the schannel crate.

let cert_store = schannel::cert_store::CertStore::open_current_user("MY")?;
for cert in cert_store.certs() {
    /*
    pub enum PrivateKey {
        CryptProv(CryptProv),
        NcryptKey(NcryptKey),
    }
    */
    // Private Key is just an enum
    let private_key: schannel::cert_context::PrivateKey = cert.private_key().acquire()?;

    // Convert private key in pem format and print it...
    // pseudocode: let private_key_pem: String = private_key.to_pem()?;
    println!("Found {} with privatekey as pem:\n{}", cert.friendly_name()?, private_key_pem)
}

Sadly PrivateKey is just an enum and I don't see a possibly way to get the private key in pem format.

I could export the whole certstore as a pkcs12 and with openssl I can read a private key of it but that's not the private key of my specific certificate I wanted, its anyone and I don't know what private key it is.

Is there a way to get the private key in pem or der format? Is it even possible with the schannel crate?

0

There are 0 best solutions below