From 23f97773ee76ec60d036f5a704c6fe22c66f2417 Mon Sep 17 00:00:00 2001 From: Dmitry Timoshkov Date: Wed, 6 Feb 2019 15:25:49 +0800 Subject: [PATCH] crypt32: CryptAcquireCertificatePrivateKey should look up certificate in the user's store if not linked to a provider. Under Windows passing to CryptAcquireCertificatePrivateKey() a certificate retrieved from the key container using CryptGetKeyParam(KP_CERTIFICATE) + CertCreateCertificateContext() works although such a certificate doesn't have assigned properties at all. In order for this to work Windows probably looks up an original certificate in the user's private store. Signed-off-by: Dmitry Timoshkov Signed-off-by: Alexandre Julliard --- dlls/crypt32/cert.c | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/dlls/crypt32/cert.c b/dlls/crypt32/cert.c index e1c7c543408..02693b96d02 100644 --- a/dlls/crypt32/cert.c +++ b/dlls/crypt32/cert.c @@ -886,6 +886,7 @@ BOOL WINAPI CryptAcquireCertificatePrivateKey(PCCERT_CONTEXT pCert, PCRYPT_KEY_PROV_INFO info = NULL; CERT_KEY_CONTEXT keyContext; DWORD size; + PCCERT_CONTEXT cert_in_store = NULL; TRACE("(%p, %08x, %p, %p, %p, %p)\n", pCert, dwFlags, pvReserved, phCryptProv, pdwKeySpec, pfCallerFreeProv); @@ -896,6 +897,34 @@ BOOL WINAPI CryptAcquireCertificatePrivateKey(PCCERT_CONTEXT pCert, ret = CertGetCertificateContextProperty(pCert, CERT_KEY_PROV_INFO_PROP_ID, 0, &size); + + if (!ret) + { + static const WCHAR myW[] = { 'M','y',0 }; + HCERTSTORE hstore; + + hstore = CertOpenStore(CERT_STORE_PROV_SYSTEM_W, 0, 0, + CERT_SYSTEM_STORE_CURRENT_USER, myW); + if (hstore) + { + cert_in_store = CertFindCertificateInStore(hstore, pCert->dwCertEncodingType, 0, + CERT_FIND_EXISTING, pCert, NULL); + if (cert_in_store) + { + ret = CertGetCertificateContextProperty(cert_in_store, CERT_KEY_PROV_INFO_PROP_ID, 0, &size); + if (ret) + pCert = cert_in_store; + else + { + CertFreeCertificateContext(cert_in_store); + cert_in_store = NULL; + } + } + + CertCloseStore(hstore, 0); + } + } + if (ret) { info = HeapAlloc(GetProcessHeap(), 0, size); @@ -949,6 +978,8 @@ BOOL WINAPI CryptAcquireCertificatePrivateKey(PCCERT_CONTEXT pCert, } } HeapFree(GetProcessHeap(), 0, info); + if (cert_in_store) + CertFreeCertificateContext(cert_in_store); return ret; } -- 2.11.4.GIT