Статус: Новичок
Группы: Участники
Зарегистрирован: 12.01.2009(UTC) Сообщений: 6 Откуда: Tver
|
Код:
int main(int argc, _TCHAR* argv[])
{
CRYPT_DATA_BLOB SignedMessage;
SignMessage(&SignedMessage);
_tprintf(TEXT("Press any key to exit."));
_getch();
}
bool SignMessage(CRYPT_DATA_BLOB *pSignedMessageBlob)
{
bool fReturn = false;
HCERTSTORE hCertStore = NULL;
PCCERT_CONTEXT pSignerCert;
DWORD cbSignedMessageBlob=0;
BYTE *pbSignedMessageBlob = NULL;
BYTE *pbSignature=NULL;
DWORD dwSigLen=0;
LPBYTE pbUserCert;
HCRYPTKEY hKey = 0;
HCRYPTPROV phProv;
DWORD size;
DWORD fParam = CRYPT_FIRST;
PCCERT_CONTEXT pUserCert=0;
DWORD dwUserCertLength=0;
DWORD name_size;
char *SIGNER_NAME;
CString CERT_STORE;
CryptAcquireContext( &phProv,
NULL,
"Crypto-Pro GOST R 34.10-2001 Cryptographic ServiceProvider", 75,
CRYPT_VERIFYCONTEXT
);
while( CryptGetProvParam(phProv, PP_ENUMCONTAINERS, NULL, &size, fParam) )
{
BYTE * ContNameD=(BYTE*)malloc(sizeof(BYTE*)*size);
int len = (int)size;
CryptGetProvParam(phProv, PP_ENUMCONTAINERS, ContNameD, &size, fParam);
ContNameD[len]=0;
CERT_STORE = (CString) ContNameD;
_tprintf(CERT_STORE);
fParam = 0;
}
if(!CryptAcquireContext(&phProv,CERT_STORE,
"Crypto-Pro GOST R 34.10-2001 Cryptographic Service Provider",75,CRYPT_MACHINE_KEYSET))
MyHandleError("CryptAcquireContext");
if(!CryptGetUserKey(phProv,AT_KEYEXCHANGE,&hKey))
MyHandleError("CryptGetUserKey");
if (!CryptGetKeyParam (hKey, KP_CERTIFICATE, NULL,&dwUserCertLength, 0))
{
MyHandleError ("Error during GetKeyParam.\n");
}
pbUserCert = (BYTE*)malloc (dwUserCertLength);
if (pbUserCert == NULL)
{
MyHandleError ("Error during malloc.\n");
}
if (!CryptGetKeyParam (hKey, KP_CERTIFICATE, pbUserCert,&dwUserCertLength, 0))
{
MyHandleError ("Error during GetKeyParam.\n");
}
pUserCert = CertCreateCertificateContext (
X509_ASN_ENCODING | PKCS_7_ASN_ENCODING, pbUserCert,
dwUserCertLength);
if (pUserCert == NULL)
{
MyHandleError ("Error during CertCreateCertificateContext.\n");
}
name_size = CertNameToStr(MY_ENCODING_TYPE,
&(pUserCert->pCertInfo->Subject),
CERT_SIMPLE_NAME_STR,
NULL,
0);
if(!(SIGNER_NAME = (char *)malloc(name_size * sizeof(TCHAR))))
{
MyHandleError(TEXT("Memory allocation failed."));
}
name_size = CertNameToStr(MY_ENCODING_TYPE,
&(pUserCert->pCertInfo->Subject),
CERT_SIMPLE_NAME_STR,
SIGNER_NAME,
name_size);
_tprintf(TEXT("Subject -> %s.\n"), SIGNER_NAME);
for(int l=0;l<=3;l++) *SIGNER_NAME=*SIGNER_NAME++;
_tprintf(TEXT("Subject -> %s.\n"), SIGNER_NAME);
const int BuffSize = 1024;
WCHAR SIGNER_NAME1[BuffSize];
MultiByteToWideChar(CP_ACP, 0, SIGNER_NAME, strlen(SIGNER_NAME)+1, SIGNER_NAME1,strlen(SIGNER_NAME)+1);
if(pbSignature)
{
free(pbSignature);
pbSignature = NULL;
}
BYTE* pbMessage = (BYTE*)TEXT("12345");//SIGNER_NAME1;
DWORD cbMessage = (DWORD)strlen((char*) pbMessage + 1) ;
_tprintf(TEXT("The message to be signed is \n%s\n"),
SIGNER_NAME1);
_tprintf(TEXT("The message to be signed is \n%s\n"),
pbMessage);
if ( !( hCertStore = CertOpenStore(
CERT_STORE_PROV_SYSTEM,
0,
NULL,
CERT_SYSTEM_STORE_CURRENT_USER,
CERT_STORE_NAME)))
{
MyHandleError(TEXT("The store could not be opened."));
goto exit_SignMessage;
}
if(pSignerCert = CertFindCertificateInStore(
hCertStore,
MY_ENCODING_TYPE,
0,
CERT_FIND_SUBJECT_STR,
SIGNER_NAME1,
NULL))
{
_tprintf(TEXT("The signer's certificate was found.\n"));
}
else
{
MyHandleError( TEXT("Signer certificate not found."));
goto exit_SignMessage;
}
HCRYPTHASH hHash;
if(CryptCreateHash(
phProv,
CALG_GR3411 ,
0,
0,
&hHash))
{
printf("Hash object created.\n");
}
else
{
MyHandleError("error CryptCreateHash.");
}
if(CryptHashData(
hHash,
pbMessage,
cbMessage,
0))
{
printf("Hash object vu4islen.\n");
}
else
{
MyHandleError("error CryptHashData.");
}
dwSigLen=0;pbSignature=NULL;
if(CryptSignHash(
hHash,
AT_KEYEXCHANGE,
NULL,
0,
NULL,
&dwSigLen))
{
printf("Sign length %d .\n",dwSigLen);
}
else
{
MyHandleError("error CryptSignHash.");
}
if(pbSignature = new BYTE[dwSigLen])
{
printf("member for sign vudel.\n");
}
else
{
MyHandleError("member error.");
}
if(CryptSignHash(
hHash,
AT_KEYEXCHANGE,
NULL,
0,
pbSignature,
&dwSigLen))
{
print_signature(dwSigLen, pbSignature);
}
else
{
MyHandleError("error CryptSignHash.");
}
if(pbSignature)
delete pbSignature;
if(hHash)
CryptDestroyHash(hHash);
if(phProv)
CryptReleaseContext(phProv, 0);
if(pSignerCert)
CertFreeCertificateContext(pSignerCert);
if(CertCloseStore(hCertStore, CERT_CLOSE_STORE_CHECK_FLAG))
{
printf("\nxran closed. \n");
}
else
{
printf("error!");
}
exit_SignMessage:
return TRUE;
}
|