Ver Mensaje Individual
  #4  
Antiguo 26-08-2021
Garada Garada is offline
Miembro
 
Registrado: jul 2004
Posts: 66
Reputación: 20
Garada Va por buen camino
Yo lo he hecho con Delphi 2010 con llamadas a la biblioteca WinCrypt.
Cargas el archivo PFX con el certificado (o lo puedes tener almacenado en la base de datos o en un recurso) a un almacén de certificados temporal, extraes el certificado y lo añades al WebService.

Código Delphi [-]
procedure HTTPRIOHTTPWebNode1BeforePost(const HTTPReqResp: THTTPReqResp; Data: Pointer);

  procedure CheckError(Puntero: Pointer);
  begin
    if not Assigned(Puntero) then
      RaiseLastOSError;
  end;

const
  INTERNET_OPTION_CLIENT_CERT_CONTEXT = 84;
  Pass = 'LaContraseña';
var
  pStore: HCERTSTORE;
  pCert: PCERT_CONTEXT;
  DataBlob: CRYPT_DATA_BLOB;
  PFX: AnsiString;
begin
  pStore := nil;
  pCert := nil;

  PFX := FuncionQueLeaElFicheroPFX;

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

    // Almacen temporal con el contenido del PFX
    pStore := PFXImportCertStore(DataBlob, PWideChar(Pass), {PKCS12_NO_PERSIST_KEY + }PKCS12_INCLUDE_EXTENDED_PROPERTIES);
    CheckError(pStore);

    // Buscar un certificado con clave privada
    // Solo debería haber uno
    pCert := CertFindCertificateInStore(pStore,
                                        X509_ASN_ENCODING,
                                        0,
                                        CERT_FIND_HAS_PRIVATE_KEY, //CERT_FIND_ANY,
                                        nil,
                                        nil);
    CheckError(pCert);

    // Pasarlo al servicio
    InternetSetOption(Data, INTERNET_OPTION_CLIENT_CERT_CONTEXT, pCert, SizeOf(CERT_CONTEXT));
  finally
    if Assigned(pCert) then
      CertFreeCertificateContext(pCert);

    if Assigned(pStore) then
      CertCloseStore(pStore, 0);
  end;
end;

Para las declaraciones de la biblioteca uso JwaWinCrypt y añado la declaración de PFXImportCertStore.
En Delphi 2010 sólo parte de las declaraciones están en CertHelper por eso prefiero las del JEDI, puede que en versiones más modernas esté más completa.

Código Delphi [-]
function PFXImportCertStore(var pPFX: CRYPT_DATA_BLOB;
                            szPassword: LPCWSTR;
                            dwFlags: DWORD): HCERTSTORE; stdcall; external 'Crypt32.dll';
Responder Con Cita