Ver Mensaje Individual
  #25  
Antiguo 04-10-2023
Garada Garada is offline
Miembro
 
Registrado: jul 2004
Posts: 66
Reputación: 20
Garada Va por buen camino
Y esta es la modificación para leer varios PFX y pasarlos al THttpClient
Si hay alguna función que no tengas declarada avisa.

Código Delphi [-]
function CertAddCertificateContextToStore(hCertStore: HCERTSTORE;
                                          pCertContext: PCCERT_CONTEXT;
                                          dwAddDisposition: DWORD;
                                          ppStoreContext: PCCERT_CONTEXT): BOOL; stdcall; external 'Crypt32.dll';

class function TWinHttpLib.GetCertStore: HCERTSTORE;

  procedure AddPFX(f: string);
  const
    CERT_STORE_ADD_USE_EXISTING = 2;
    Pass = 'LaContraseña';
  var
    pTmpStore: HCERTSTORE;
    pCert: PCERT_CONTEXT;
    DataBlob: CRYPT_DATA_BLOB;
    PFX: TBytes;
  begin
    PFX := TFile.ReadAllBytes(f);

    DataBlob.cbData := Length(PFX);
    DataBlob.pbData := @PFX[0];

    // se lee el pfx en un almacen en memoria
    pTmpStore := PFXImportCertStore(@DataBlob, PWideChar(Pass), 0);

    // se copian los certificados al almacen que usa el HttpClient
    pCert := CertEnumCertificatesInStore(pTmpStore, nil);
    while pCert <> nil do
    begin
      if not CertAddCertificateContextToStore(FStore, pCert, CERT_STORE_ADD_USE_EXISTING, nil) then
        RaiseLastOSError;

      pCert := CertEnumCertificatesInStore(pTmpStore, pCert);
    end;

    CertCloseStore(pTmpStore, 0);
  end;

begin
  FLock.Enter;
  try
    if FStore = nil then
    begin
      // almacen temporal en memoria para el HttpClient
      FStore := CertOpenStore(sz_CERT_STORE_PROV_MEMORY, 0, 0, 0, nil);

      AddPFX('certificado1.pfx');
      AddPFX('certificado2.pfx');
    end;

    Result := FStore;
  finally
    FLock.Leave;
  end;
end;
Responder Con Cita