RE: Certificate Install Problem
- From: lelteto <lelteto@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Wed, 8 Aug 2007 08:26:09 -0700
From your description it seems the problem is with usage property. Try to setthe usage with another
CertSetCertificateContextProperty(CERT_ENHKEY_USAGE_PROP_ID) call.
Laszlo Elteto
SafeNet, Inc.
"ersin" wrote:
Hello,.
I am using Windows 2003 CA server to issue Client Authentication
certificates on smartcards. When I install the certificate through the
CA server's web page (ie http:// server / certsrv ) I can use this
certificate and related smartcard without any problem. However if I
delete the certificate from the IE certificate store and re-install it
using CertSetCertificateContextProperty function (I use a custom
program for doing this) I cannot use this certificate with some
programs. (It can be used with Outlook but not with Cisco VPN client
program).
I would like to learn if the code I am using to install the
certificate to the certificate store is correct or not. I am attaching
a copy of the function.
Ersin.
-----------------------------------
int CFunctions::ImportCertificatetoIEStore(char* containerName,
unsigned char* pbCert, int cbCert)
{
int res=0;
PCCERT_CONTEXT pCertContext = NULL;
char* tempError = NULL;
char* errNum = NULL;
char* auxErr = NULL;
if((errNum = (char*)malloc(20*sizeof(char))) == NULL) return -1;
HCRYPTPROV hProv2 = NULL;
HCERTSTORE hCertStore = NULL;
// Open Certificate Store
hCertStore = CertOpenStore(CERT_STORE_PROV_SYSTEM, MY_ENCODING_TYPE,
0, CERT_SYSTEM_STORE_CURRENT_USER, L"MY");
if(!hCertStore){
writeMyLog("ImportCertificatetoIEStore","importCertificate -
CertOpenSystemStore - CERT_OPEN_STORE_ERROR, during importing cert,
Error No: 0x%x", GetLastError());
return CERT_OPEN_STORE_ERROR;
}
if(!CertAddEncodedCertificateToStore(hCertStore, X509_ASN_ENCODING,
pbCert, cbCert, CERT_STORE_ADD_REPLACE_EXISTING, &pCertContext)){
writeMyLog("ImportCertificatetoIEStore", "importCertificate -
CertAddCertificateContextToStore - CERT_ADD_CERT_TO_STORE_ERROR,
during importing cert, Error No: 0x%x", GetLastError());
MessageBox(NULL,"Sertifika depoya atılırken bir hata oluştu",
"HATA", MB_OK);
return CERT_ADD_CERT_TO_STORE_ERROR;
}
DWORD dwKeyLen = pCertContext->pCertInfo-
SubjectPublicKeyInfo.PublicKey.cbData;BYTE* pbPubKey = pCertContext->pCertInfo-
SubjectPublicKeyInfo.PublicKey.pbData;
DWORD keyUsageType=AT_KEYEXCHANGE;
CRYPT_KEY_PROV_INFO CryptKeyProvInfo;
CRYPT_KEY_PROV_INFO *pCryptKeyProvInfo;
DWORD dwPropId = CERT_KEY_PROV_INFO_PROP_ID;
DWORD dwFlags = CERT_STORE_NO_CRYPT_RELEASE_FLAG;
bool bKeyContainerFound = false;
// Name KeyContainer should be converted to WideChar
wchar_t *pwcContainer = (wchar_t *)malloc( 1024 );
memset(pwcContainer,0,1024);
mbstowcs( pwcContainer, containerName,
(size_t)strlen(containerName) );
wchar_t *pwcProvider = NULL;
pwcProvider = (wchar_t *)malloc( 1024 );
memset(pwcProvider,0,1024);
mbstowcs( pwcProvider, provider, (size_t)strlen(provider) );
// Initalize the CRYPT_KEY_PROV_INFO data structure.
CryptKeyProvInfo.pwszContainerName = pwcContainer;
CryptKeyProvInfo.pwszProvName = pwcProvider;
CryptKeyProvInfo.dwProvType = PROV_RSA_FULL;
CryptKeyProvInfo.dwFlags = CERT_SET_KEY_PROV_HANDLE_PROP_ID;
CryptKeyProvInfo.cProvParam = 0;
CryptKeyProvInfo.rgProvParam = NULL;
CryptKeyProvInfo.dwKeySpec = keyUsageType;
pCryptKeyProvInfo = &CryptKeyProvInfo;
// Set the property.
if(!(CertSetCertificateContextProperty(
pCertContext, // A pointer to the certificate where the
propertiy will be set.
dwPropId, // An identifier of the property to be set.
CERT_KEY_PROV_INFO_PROP_ID
dwFlags, // The flag used in this case is
CERT_STORE_NO_CRYPT_RELEASE_FLAG
pCryptKeyProvInfo
)))
{
writeMyLog("ImportCertificatetoIEStore","importCertificate -
CertSetCertificateContextProperty - CERT_SET_CONTEXT_PROP_ERROR,
during importing cert, Error No: 0x%x", GetLastError());
return CERT_SET_CONTEXT_PROP_ERROR;
}
int ret = CertFreeCertificateContext(pCertContext);
ret = CertCloseStore(hCertStore,CERT_CLOSE_STORE_FORCE_FLAG);
return 1;
}
- Follow-Ups:
- Re: Certificate Install Problem
- From: ersin
- Re: Certificate Install Problem
- References:
- Certificate Install Problem
- From: ersin
- Certificate Install Problem
- Prev by Date: Re: AD Authentication using smart card
- Next by Date: Re: Ctrl Alt Del on Vista
- Previous by thread: Certificate Install Problem
- Next by thread: Re: Certificate Install Problem
- Index(es):
Relevant Pages
|