Статус: Активный участник
Группы: Участники
Зарегистрирован: 30.07.2012(UTC) Сообщений: 34
|
Полный код ниже. А разве есть разница какой тип данный записывать в файл? Цитата:int SignMessage(CRYPT_DATA_BLOB *pSignedMessageBlob,char* infile,char* outfile,char* signer_name) { int fReturn = 0; BYTE* pbMessage; DWORD cbMessage; HCERTSTORE hCertStore = NULL; PCCERT_CONTEXT pSignerCert; CRYPT_SIGN_MESSAGE_PARA SigParams; DWORD cbSignedMessageBlob; BYTE *pbSignedMessageBlob = NULL; const BYTE* MessageArray[] = {"\0"}; DWORD_PTR MessageSizeArray[1]; FILE *infile_f; char infile_str[512] = {'\0'}; FILE *outfile_f; wchar_t *convCp_wbuf = NULL; unsigned short convCp_wbuf_len; LPCSTR lpszCertSubject;
convCp_wbuf_len = MultiByteToWideChar( CP_ACP , 0, signer_name, -1, NULL, 0); convCp_wbuf = calloc( sizeof( wchar_t ) , convCp_wbuf_len );
if(MultiByteToWideChar( CP_ACP, 0, signer_name, -1, convCp_wbuf, convCp_wbuf_len)){ lpszCertSubject = (LPCSTR)convCp_wbuf; }
infile_f = fopen(infile, "rb"); if(!infile_f) { return(0); }
while(!feof(infile_f)) { fgets(infile_str, 512, infile_f); pbMessage = (BYTE*)infile_str; } MessageArray[0] = pbMessage; fclose(infile_f); // Initialize the output pointer. pSignedMessageBlob->cbData = 0; pSignedMessageBlob->pbData = NULL;
// Calculate the size of message. To include the // terminating null character, the length is one more byte // than the length returned by the strlen function. cbMessage = (lstrlen((TCHAR*) pbMessage) + 1) * sizeof(TCHAR);
MessageSizeArray[0] = cbMessage;
// Begin processing. _tprintf(TEXT("The message to be signed is \"%s\".\n"), pbMessage);
// Open the certificate store. if ( !( hCertStore = CertOpenStore( CERT_STORE_PROV_SYSTEM, 0, NULL, CERT_SYSTEM_STORE_CURRENT_USER, CERT_STORE_NAME))) { MyHandleError(TEXT("The MY store could not be opened.")); goto exit_SignMessage; }
//MultiByteToWideChar(CP_ACP, 0, signer_name, strlen(signer_name)+1, signer_name,strlen(signer_name)+1);
// Get a pointer to the signer's certificate. // This certificate must have access to the signer's private key. if(pSignerCert = CertFindCertificateInStore( hCertStore, MY_ENCODING_TYPE, 0, CERT_FIND_SUBJECT_STR, lpszCertSubject, NULL)) { _tprintf(TEXT("The signer's certificate was found.\n")); } else { MyHandleError( TEXT("Signer certificate not found.")); goto exit_SignMessage; }
// Initialize the signature structure. SigParams.cbSize = sizeof(CRYPT_SIGN_MESSAGE_PARA); SigParams.dwMsgEncodingType = MY_ENCODING_TYPE; SigParams.pSigningCert = pSignerCert; SigParams.HashAlgorithm.pszObjId = szOID_OIWSEC_sha1; SigParams.HashAlgorithm.Parameters.cbData = NULL; SigParams.cMsgCert = 1; SigParams.rgpMsgCert = &pSignerCert; SigParams.cAuthAttr = 0; SigParams.dwInnerContentType = 0; SigParams.cMsgCrl = 0; SigParams.cUnauthAttr = 0; SigParams.dwFlags = 0; SigParams.pvHashAuxInfo = NULL; SigParams.rgAuthAttr = NULL;
// First, get the size of the signed BLOB. if(CryptSignMessage( &SigParams, TRUE, 1, MessageArray, MessageSizeArray, NULL, &cbSignedMessageBlob)) { _tprintf(TEXT("%d bytes needed for the encoded BLOB.\n"), cbSignedMessageBlob); } else { MyHandleError(TEXT("Getting signed BLOB size failed")); goto exit_SignMessage; }
// Allocate memory for the signed BLOB. if(!(pbSignedMessageBlob = (BYTE*)malloc(cbSignedMessageBlob))) { MyHandleError( TEXT("Memory allocation error while signing.")); goto exit_SignMessage; }
// Get the signed message BLOB. if(CryptSignMessage( &SigParams, TRUE, 1, MessageArray, MessageSizeArray, pbSignedMessageBlob, &cbSignedMessageBlob)) { _tprintf(TEXT("The message was signed successfully. \n"));
// pbSignedMessageBlob now contains the signed BLOB. fReturn = 1; } else { MyHandleError(TEXT("Error getting signed BLOB")); goto exit_SignMessage; }
exit_SignMessage:
// Clean up and free memory as needed. if(pSignerCert) { CertFreeCertificateContext(pSignerCert); } if(hCertStore) { CertCloseStore(hCertStore, CERT_CLOSE_STORE_CHECK_FLAG); hCertStore = NULL; }
// Only free the signed message if a failure occurred. if(!fReturn) { if(pbSignedMessageBlob) { free(pbSignedMessageBlob); pbSignedMessageBlob = NULL; } }
if(pbSignedMessageBlob) { char buf[256] = {NULL}; outfile_f = fopen(outfile, "wb"); if(!outfile_f) { return(0); } strcpy(buf,(char*)pbSignedMessageBlob); strcat(buf,"\0"); fprintf(outfile_f, buf); fclose(outfile_f); } return fReturn; }
|