const
OPEN_EXISTING = 3;
FILE_SHARE_READ = $00000001;
function CertAddEncodedCertificateToSystemStore(szCertStoreName: String; pbCertEncoded: Longint; cbCertEncoded: DWORD): Boolean;
external 'CertAddEncodedCertificateToSystemStore{#A}@Crypt32.dll stdcall';
function ReadFile(hFile: THandle; lpBuffer: Longint; nNumberOfBytesToRead: DWORD; var lpNumberOfBytesRead: DWORD; lpOverlapped: Cardinal): Boolean;
external 'ReadFile@Kernel32.dll stdcall';
function AddCert(Param: String):Boolean;
var
Store: String;
CA : String;
Enum: Integer;
Buffer : Longint;
BufferRead : DWORD;
dwBufferLength : DWORD;
CFileHandle, hHeap : THandle;
count : integer;
Path : String;
CerFileName : String;
FindRec: TFindRec;
begin
Enum := 0;
Store := 'ROOT'
CA := 'la la la la lal la la CA';
try
Enum := FindCert(Store, CA);
if Enum >= 11 then exit; // При изменении колличества обязательных сертов УЦ, поменять цифру тут!
if (CreateDir(ExpandConstant('{tmp}\Cert'))) and DownloadFile(Param,ExpandConstant('{tmp}\Cert')) then
begin
Path := ExtractFileExt(szFileName);
if (Path = '.rar') or (Path = '.zip') or (Path = '.7z') then
begin
if (UnZip(szFileName) = false) then
begin
MessageBox(hWnd, 'Ошибка при распаковки архива!', 'Скачивание сертификат(а)-ов', MB_OK or MB_ICONINFORMATION or MB_TASKMODAL);
exit;
end;
end;
if FindFirst(ExpandConstant('{tmp}\Cert\*.cer'), FindRec) then begin
try
hHeap := GetProcessHeap();
Buffer := HeapAlloc(hHeap, HEAP_ZERO_MEMORY, 2048); // буффер памяти в куче для одного сертификата!
dwBufferLength := HeapSize(hHeap, 0, Buffer);
repeat
if FindRec.Attributes and FILE_ATTRIBUTE_DIRECTORY = 0 then
begin
CerFileName := ExpandConstant('{tmp}\Cert\') + FindRec.Name;
CFileHandle := CreateFile(CerFileName, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
if ReadFile(CFileHandle, Buffer, dwBufferLength, BufferRead, 0) then
begin
//MessageBox(hWnd, 'BufferRead ' + inttostr(BufferRead), '', MB_OK or MB_TASKMODAL);
if CertAddEncodedCertificateToSystemStore(Store, Buffer, dwBufferLength) then count := count + 1
else Log('Ошибка CertAddEncodedCertificateToSystemStore: ' + SysErrorMessage(DLLGetLastError));
end else Log('Ошибка ReadFile:' + SysErrorMessage(DLLGetLastError));
end;
until not FindNext(FindRec);
finally
FindClose(FindRec);
CloseHandle(CFileHandle);
HeapFree(hHeap, 0, Buffer);
end;
end else MessageBox(hWnd, 'Ошибка скачивания файла!', '', MB_OK or MB_TASKMODAL);
end;
finally
end;
end;