[Continued] How to export Ceritificate and Private Key as a PFX fi

From: RelyKY (RelyKY_at_discussions.microsoft.com)
Date: 09/30/04


Date: Wed, 29 Sep 2004 19:53:02 -0700


 Previous Question (9/23):
> Dear all:
> I make a RSA key pair with Crypto API, and store them in PC. Then sign
> the corresponding Certificate with the CSR file generated by the RSA key
> pair.
> And, I use Crypto API, "CertAddCertificateContextToStore()" to enroll
> the certificate to the certificate store in PC. Before enrolling, I had
> used
> API,"CertSetCertificateContextProperty()", to set up the
> property,"CERT_KEY_PROV_INFO_PROP_ID", in order to relate the Certificate
> to
> the key pair.
>
> And then, viewing the certificate from PC (using IE browser), the
> certificate viewer shows that "The Certificate has a corresponding private
> key.". But when I want to save/export it as a PFX file (using the
> certificate
> viewer), it says "Can't find the related private key.".
>
> Do you know any step I miss?
> Thanks so much!
>

Dear ALL:
    Thank you first. I had read the replied message.
    Well, my private key can be export. I call the APIs
"CryptAcquireContext", "CryptGetUserKey", "CryptExportKey", to export
"PRIVATEKEYBLOB". that ensure the private key is exporable.
    I also call "CertGetCertificateContextProperty(
...CERT_KEY_PROV_INFO_PROP_ID... )" and use
data returned in CRYPT_KEY_PROV_INFO structure as parameters to
CryptAcquireContext, and it's work normally.

   However, to call "CryptAcquireCertificatePrivateKey()" is fail always,
when I want to acquire CSP provider context with an indicated certificate. I
think I make mistake to fill the paramter for calling
API,"CertSetCertificateContextProperty", the flowing is the code I filled
"CERT_KEY_PROV_INFO".
  
  //================================================================
  //## set provider information
  CRYPT_KEY_PROV_INFO keyProvInfo;
  keyProvInfo.pwszContainerName = (LPWSTR) "My Key Storage"; //<---[I fill a
multi-character string. Is it right?]
  keyProvInfo.pwszProvName = (LPWSTR) "Microsoft Enhanced Cryptographic
Provider v1.0";
  keyProvInfo.dwKeySpec = AT_SIGNATURE;
  keyProvInfo.dwProvType = PROV_RSA_FULL;
  keyProvInfo.cProvParam = 0;
  keyProvInfo.rgProvParam = NULL;
  keyProvInfo.dwFlags = 0;
  if( !::CertSetCertificateContextProperty(
         pCertCtx,
         CERT_KEY_PROV_INFO_PROP_ID,
         0,
         &keyProvInfo))
  {
        throw ::GetLastError();
  }

  //## register certificate to certificate store.
  if(NULL == (hCertStore = ::CertOpenSystemStore(NULL, sCertStoreName)))
        throw ::GetLastError();
  //
  if( !::CertAddCertificateContextToStore(
        hCertStore,
        pCertCtx,
        CERT_STORE_ADD_REPLACE_EXISTING,
        &pCertCtx2)) //<--- return a new certificate context
  {
        throw ::GetLastError();
  }

  //##...test................
  HCRYPTPROV hCryptProv = NULL;
  DWORD dwKeySpec = 0;
  BOOL fCallerFreeProv = FALSE;
  BOOL rv = ::CryptAcquireCertificatePrivateKey(
        pCertCtx2, //<---[use this certificate to acquire the corresponding
provider context.]
        CRYPT_ACQUIRE_USE_PROV_INFO_FLAG,
        NULL,
        &hCryptProv,
        &dwKeySpec,
        &fCallerFreeProv);
  dwErrCode = ::GetLastError();
  //#define CRYPT_E_NO_KEY_PROPERTY _HRESULT_TYPEDEF_(0x8009200BL)
<---[this is the error code.]

  //================================================================
  Do you know any step I miss? And what is the condition to let
"CryptAcquireCertificatePrivateKey()" work normally.
  Thanks so much!



Relevant Pages

  • RE: Difference between AT_KEYEXCHANGE and AT_SIGNATURE
    ... normally you need CERTIFICATE for them. ... So if you want to use only 1 key pair for both, ... Normally you shold NEVER reveal youd signing private key. ... simply generate a new signing key pair and use that from that point of time. ...
    (microsoft.public.platformsdk.security)
  • RE: AT_SIGNATURE and AT_KEYEXCHANGE
    ... The certificate EXTENSIONwill tell what the key is valid for. ... private key is valid ONLY for signature than it will be set as AT_SIGNATURE. ... key pair and one cert. ...
    (microsoft.public.platformsdk.security)
  • Re: Expired Recovery Agent EFS Cert
    ... I may be wrong but I believe cipher /u may be user specific. ... RA certificate to see exactly what certificate it is referring to. ... The link below is on EFS best practices though you are already doing the ... > What I did was generate a new key pair using cipher /r and then added it ...
    (microsoft.public.win2000.security)
  • Windows 2003 CA bug?
    ... I'm encountering a problem attempting to import a key pair to use as ... generate the key pair and certificate" ... Windows created PFX file. ...
    (microsoft.public.security)
  • Re: CDP in root certificate when renewed
    ... Renew the CA certificate again with the existing key pair. ... The bug is that the CA does not read the capolicy.inf file during renewals with a new key pair. ... THe renewal with the same key pair will read the capolicy.inf file and remove the CDP once again. ...
    (microsoft.public.windows.server.security)