Статус: Участник
Группы: Участники
Зарегистрирован: 02.08.2013(UTC) Сообщений: 11
|
Hi, I'm using Delphi XE. I need to sign file with certificate from smart card, and type of file must be PKCS7, p7s. I tried to sign .pdf or .unl and l can create new file (.p7s), but it is empty - size is 0KB. Could you see what is wrong.. This is code: Код:
procedure TForm1.BitBtn3Click(Sender: TObject);
var
signPara: CRYPT_SIGN_MESSAGE_PARA;//CRYPT_SIGN_MESSAGE_PARA ;
hashAlgorithm: CRYPT_ALGORITHM_IDENTIFIER ;
hStore: HCERTSTORE ;
pSignerContext: PCCERT_CONTEXT ;
lpData :Pointer; //array of PBYTE;//;
dwDataSize : Pointer; //DWORD;//;
dwDS: DWORD;
bResult:boolean;
lpSignedBlob:PBYTE;
dwSignedBlobSize:DWORD;
hFile, hTempFile:THandle ;
dwWriteByte:DWORD;
cToBeSigned:DWORD;
size_new: DWORD;
pStreamIn, tpStreamIn : TMemoryStream ;
pStreamOut, tpStreamOut : TMemoryStream ;
file_name:string;
ret: Integer;
Data: array [0..MAXBYTE] of AnsiChar;
Cert: array [0..MAX_BUFFER_LEN] of AnsiChar;
Lng: UINT;
PIN: array[0..19] of AnsiChar;
Rez: AnsiString;
citac: TCitac;
pwszCertSubject :PWideChar;
const
CERT_STORE_NAME = WideString('MY');
begin
hStore := CertOpenSystemStore (0, CERT_STORE_NAME);
//pwszCertSubject
pSignerContext := CertFindCertificateInStore (hStore, (PKCS_7_ASN_ENCODING or X509_ASN_ENCODING), 0, CERT_FIND_SUBJECT_STR, nil, nil);
CryptAcquireContext (@hStore, PWideChar(citac.ReaderId), PWideChar(citac.CardId), PROV_RSA_FULL, CRYPT_VERIFYCONTEXT);//CRYPT_VERIFYCONTEXT) ;
ZeroMemory (@hashAlgorithm, sizeof (CRYPT_ALGORITHM_IDENTIFIER));
hashAlgorithm.pszObjId := szOID_RSA_MD5;
signPara.cbSize := sizeof (CRYPT_SIGN_MESSAGE_PARA);
signPara.dwMsgEncodingType := PKCS_7_ASN_ENCODING or X509_ASN_ENCODING;
signPara.pSigningCert := pSignerContext;
signPara.HashAlgorithm := hashAlgorithm;
signPara.pvHashAuxInfo := nil;
signPara.cMsgCert := 1;
signPara.rgpMsgCert := &pSignerContext;
signPara.cMsgCrl := 0;
signPara.rgpMsgCrl := nil;
signPara.cAuthAttr := 0;
signPara.rgAuthAttr := nil;
signPara.cUnauthAttr := 0;
signPara.rgUnauthAttr := nil;
signPara.dwFlags :=0;// CRYPT_MACHINE_KEYSET;
signPara.dwInnerContentType := 0;
cToBeSigned:=1;
pStreamIn:= TMemoryStream.Create;
pStreamOut:= TMemoryStream.Create;
file_name:=edit1.Text;
pStreamIn.LoadFromFile(file_name);
tpStreamIn:= TMemoryStream.Create;
tpStreamOut:= TMemoryStream.Create;
tpStreamIn.CopyFrom (pStreamIn, pStreamIn.Size - pStreamIn.Position);
tpStreamIn.Position:= 0;
lpData:= tpStreamIn.Memory;
dwDataSize := Pointer(tpStreamIn.Size);
//bResult := CryptSignMessage(@signPara, true, cToBeSigned, @lpData, @dwDataSize, nil, @dwSignedBlobSize);
CryptSignMessage(@signPara, true, cToBeSigned, @lpData, @dwDataSize, nil, @dwSignedBlobSize);
/////
Problem starts here- dwSignedBlobSize is null...
lpSignedBlob := HeapAlloc (GetProcessHeap (), 0, DWORD(dwSignedBlobSize));
//bResult := CryptSignMessage ( @signPara, FALSE, 1, @lpData, @dwDataSize, @lpSignedBlob, @dwSignedBlobSize);
CryptSignMessage ( @signPara, FALSE, 1, @lpData, @dwDataSize, @lpSignedBlob, @dwSignedBlobSize);
hFile := CreateFile (PWideChar(edit2.Text), GENERIC_WRITE, 0, nil, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, hTempFile);
WriteFile (hFile, lpSignedBlob, DWORD(dwSignedBlobSize), dwWriteByte, 0);
CloseHandle (hFile);
HeapFree (GetProcessHeap (), 0, lpSignedBlob);
CertFreeCertificateContext (pSignerContext);
CertCloseStore (hStore, CERT_CLOSE_STORE_CHECK_FLAG);
|