steffengy / schannel-rs

Schannel API-bindings for rust (provides an interface for native SSL/TLS using windows APIs)
MIT License
49 stars 51 forks source link

Question: why NCRYPT_KEY_HANDLE is private? #83

Closed TheBestTvarynka closed 1 year ago

TheBestTvarynka commented 2 years ago

I'm trying to export the certificate private key using the NCryptExportKey function but the needed key handle is private in the NcryptKey struct. Why? Does exist other ways how to obtain a private key handle?

MattesWhite commented 1 year ago

As there is the RawPointer trait a combination of RawPointer::as_ptr() and a simple cast should do the trick to get the handle for the windows crate:

let PrivateKey::NcryptKey(private_key) = my_cert.private_key().acquire().unwrap() else {panic!("not a ncrypt key")};
let pkey_handle = NCRYPT_KEY_HANDLE(unsafe { private_key.as_ptr() } as _);

However, the pkey_handle I get from this approach always results in a

thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Error { code: HRESULT(0x80090026), message: "The supplied handle is invalid." }', src\main.rs:31:10
TheBestTvarynka commented 1 year ago

hm, got it