cryptography - Windows CryptoAPI: CryptSignHash with CALG_SHA_256 and private key from MY keystore -


i trying generate digital signatures on windows (from xp sp3, testing windows 7) cryptoapi compatible following openssl commands:

openssl dgst -sha256 -sign <parameters> (for signing) openssl dgst -sha256 -verify <parameters> (for validation) 

i want use private key windows "my" keystore signing.

i managed sign files using sha1 digest algorithm using following cryptoapi functions (omitting parameters brevity):

certopenstore certfindcertificateinstore cryptacquirecertificateprivatekey cryptcreatehash (with calg_sha1) crypthashdata cryptsignhash 

the generated signature compatible "openssl dgst -sha1 -verify" (once byte order reversed).

my problem is: when try use calg_sha_256 cryptcreatehash, fails error 80090008 (nte_bad_algid). googling around, found needed use specific provider (prov_rsa_aes) instead of default one. since have provider handle, need replace cryptacquirecertificateprivatekey cryptgetuserkey. modified program like:

cryptacquirecontext (with prov_rsa_aes) certopenstore certfindcertificateinstore cryptgetuserkey cryptcreatehash (with calg_sha256) crypthashdata cryptsignhash 

unfortunately, didn't work expected: cryptgetuserkey failed error 8009000d (nte_no_key). if remove cryptgetuserkey call, program runs until cryptsignhash, fails error 80090016 (nte_bad_keyset). know keyset exist , works fine, since able use sign sha1 digest.

i tried acquiring context again information certificate context got certfindcertificateinstore: best successful cryptgetuserkey call, cryptsignhash fail same error.

the private key trying use 2048 bits long, don't expect problem since works sha1 digest. @ loss, suggestion welcome!

the problem certificates on windows "know" in provider private keys stored. when import cert put key provider type (probably prov_rsa_full), when later try access key via certificate end in same provider type.

you need open associated context certificate (have @ certgetcertificatecontextproperty cert_key_prov_handle_prop_id option). handle yyou try exporting key original provider context , reimporting new prov_rsa_aes 1 (assuming key exportable).


Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

html - Instapaper-like algorithm -

c# - How to execute a particular part of code asynchronously in a class -