?? certtools.cpp
字號:
return -3002;
}
bResult = CryptDecodeObjectEx(
X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
X509_CERT_REQUEST_TO_BE_SIGNED,//X509_CERT
(unsigned char *)szBLOB,
pcbBinary,
0,
NULL,
*pvCertReqInfo,
&pcbStructInfo
);
if (!bResult)
{
dwError = GetLastError();
return -3005;
}
return 0;
}
//從指定的hCryptProv中獲取公鑰信息
int GetPublicKey (HCRYPTPROV hCryptProv, HANDLE hHeap,CERT_PUBLIC_KEY_INFO** keyinfo)
{
int dwError;
DWORD dwSize;
BOOL bResult;
// Get Public Key Info size
bResult = CryptExportPublicKeyInfo(hCryptProv, AT_SIGNATURE, MYCODING_TYPE, NULL, &dwSize);
if (!bResult)
{
dwError = GetLastError();
return -3010;
}
// Allocate memory for Public Key Info
*keyinfo = (PCERT_PUBLIC_KEY_INFO)HeapAlloc(hHeap, 0, dwSize);
if (!(*keyinfo))
{
dwError = GetLastError();
return -3012;
}
// Get Public Key Info
bResult = CryptExportPublicKeyInfo(hCryptProv, AT_SIGNATURE, MYCODING_TYPE, *keyinfo, &dwSize);
if (!bResult)
{
dwError = GetLastError();
return -3013;
}
return dwSize;
}
//從指定的hCryptProv中獲取公鑰的HASH信息
int GetPubKeyHash(HCRYPTPROV hCryptProv,HANDLE hHeap,CERT_PUBLIC_KEY_INFO* PublicKeyInfo,DWORD *pdwSize,BYTE** pbKeyIdentifier)
{
int dwError;
BOOL bResult;
HCRYPTHASH hHash = 0;
// Create Hash
bResult = CryptCreateHash(hCryptProv, CALG_SHA1, 0, 0, &hHash);
if (!bResult)
{
dwError = GetLastError();
return -3022;
}
// Hash Public Key Info
bResult = CryptHashData(hHash, (LPBYTE)PublicKeyInfo, *pdwSize, 0);
if (!bResult)
{
dwError = GetLastError();
return -3025;
}
// Get Size of Hash
bResult = CryptGetHashParam(hHash, HP_HASHVAL, NULL, pdwSize, 0);
if (!bResult)
{
dwError = GetLastError();
return -3030;
}
// Allocate Memory for Key Identifier (hash of Public Key info)
*pbKeyIdentifier = (LPBYTE)HeapAlloc(hHeap, 0, *pdwSize);
if (!(*pbKeyIdentifier))
{
dwError = GetLastError();
return -3035;
}
// Get Hash of Public Key Info
bResult = CryptGetHashParam(hHash, HP_HASHVAL, *pbKeyIdentifier, pdwSize, 0);
if (!bResult)
{
dwError = GetLastError();
return -3040;
}
if (hHash) CryptDestroyHash(hHash);
return *pdwSize;
}
//將X509名字編碼成Name BLOB結構
BOOL XFCertStrToName(HANDLE hHeap,DWORD dwCertEncodingType,
LPCTSTR pszX500,
DWORD dwStrType,
void* pvReserved,
BYTE** pbEncoded,
DWORD* pcbEncoded,
LPCTSTR* ppszError)
{
BOOL bResult;
int dwError;
// Get X509 Name and convert it to a Name Blob
bResult = CertStrToName(dwCertEncodingType,
pszX500,
dwStrType,
pvReserved,
NULL,
pcbEncoded,
ppszError);
if (!bResult)
{
dwError = GetLastError();
return FALSE;
}
// Allocate memory for Name Blob
*pbEncoded = (LPBYTE)HeapAlloc(hHeap, 0, *pcbEncoded);
if (!pbEncoded)
{
dwError = GetLastError();
return FALSE;
}
// Convert X509 Name to Name Blob
bResult = CertStrToName(dwCertEncodingType,
pszX500,
dwStrType,
pvReserved,
*pbEncoded,
pcbEncoded,
ppszError);
if (!bResult)
{
dwError = GetLastError();
return FALSE;
}
return TRUE;
}
//ASN.1編碼
BOOL XFCryptEncodeObject(
HANDLE hHeap,
DWORD dwCertEncodingType,
LPCSTR lpszStructType,
const void* pvStructInfo,
BYTE** pbEncoded,
DWORD* pcbEncoded
)
{
BOOL bResult;
int dwError;
// Get Subject Key Identifier Extension size
bResult = CryptEncodeObject(dwCertEncodingType,
lpszStructType,
pvStructInfo,
NULL,
pcbEncoded);
if (!bResult)
{
dwError = GetLastError();
return FALSE;
}
// Allocate Memory for Subject Key Identifier Blob
*pbEncoded = (LPBYTE)HeapAlloc(hHeap, 0, *pcbEncoded);
if (!pbEncoded)
{
dwError = GetLastError();
return FALSE;
}
// Get Subject Key Identifier Extension
bResult = CryptEncodeObject(dwCertEncodingType,
lpszStructType,
pvStructInfo,
*pbEncoded,
pcbEncoded);
if (!bResult)
{
dwError = GetLastError();
return FALSE;
}
return TRUE;
}
//寫數據到文件
int WriteToFile(const void* pBuf,DWORD nLen,char *FileName)
{
FILE *fp;
DWORD nRtnLen=0;
fp = fopen(FileName,"wb");
if(fp == NULL)
return -3050;
nRtnLen = fwrite(pBuf,1,nLen,fp);
if(nRtnLen != nLen)
return nRtnLen;
fflush(fp);
fclose(fp);
return nRtnLen;
}
//從文件讀數據
int ReadFromFile(void* pBuf,DWORD nLen,char *FileName)
{
FILE *fp;
int nRtn;
fp = fopen(FileName,"rb");
if(fp == NULL)
return -3055;
nRtn=fread(pBuf,1,nLen,fp);
if(nRtn <= 0)
return -3060;
fclose(fp);
return nRtn;
}
//從證書中獲取公鑰
BOOL GetRSAKeyFromCert(PCCERT_CONTEXT pCertContext,
BOOL fSign,
HCRYPTPROV *hProv,
HCRYPTKEY *hPubKey,
DWORD *dwKeySpec,
BOOL *fFreeProv)
{
BOOL fResult;
BOOL fReturn = FALSE;
DWORD dwError;
while(1)
{
if (hProv == NULL || hPubKey == NULL ||
dwKeySpec == NULL || fFreeProv == NULL)
{
break;
}
*hProv = 0;
*hPubKey = 0;
*fFreeProv = FALSE;
if (fSign)
{
// Acquire the certificate's private key
fResult = CryptAcquireCertificatePrivateKey(pCertContext,
CRYPT_ACQUIRE_USE_PROV_INFO_FLAG|
CRYPT_ACQUIRE_COMPARE_KEY_FLAG,
NULL,
hProv,
dwKeySpec,
fFreeProv);
if (!fResult)
{
dwError = GetLastError();
break;
}
}
else
{
fResult = CryptAcquireContext(hProv,
NULL,
szProvider,
PROV_RSA_FULL,
CRYPT_VERIFYCONTEXT);
if (!fResult)
{
dwError = GetLastError();
break;
}
*fFreeProv = TRUE;
// Import the public key from the certificate so we can verify
fResult = CryptImportPublicKeyInfo(*hProv,
MYCODING_TYPE,
&(pCertContext->pCertInfo->SubjectPublicKeyInfo),
hPubKey);
if (!fResult)
{
dwError = GetLastError();
break;
}
}
fReturn = TRUE;
break;
} //end while
{
if (!fReturn)
{
if (*hPubKey != NULL)
{
CryptDestroyKey(*hPubKey);
*hPubKey = NULL;
}
if ((*fFreeProv == TRUE) && (*hProv != NULL))
{
CryptReleaseContext(*hProv, 0);
*hProv = NULL;
*fFreeProv = FALSE;
}
}
}
return fReturn;
}
//獲取本機的格林威治時間
bool ReadTime(unsigned char *pTimeBuf,int nBufLen)
{
long thistime=0;
struct tm *pTm=NULL;
if(nBufLen < 16)
return false;
time(&thistime);
pTm = gmtime(&thistime);
sprintf((char *)pTimeBuf,"%04d%02d%02d-%02d%02d%02d",pTm->tm_year+1900,pTm->tm_mon+1,pTm->tm_mday,pTm->tm_hour,pTm->tm_min,pTm->tm_sec);
return true;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -