Buenas a todos.
Hasta ahora, para cargar un certificado desde archivo antes de realizar una petición con un objeto HTTPRIO utilizaba la librería capicom.dll desde el método OnBeforePost. Pero como ya sabemos, el método había cambiado desde la versión 10.3 de Delphi, ya que el Data: Pointer lo cambiaron a Client: THTTPClient.
Pues bien, después de mucho batallar, he conseguido hacerlo funcionar simplemente usando métodos nativos de la librería crypt32.dll de Windows.
Primeramente, antes de llamar al bloque "implementation" de nuestro código, poner la siguiente línea:
Código Delphi
[-]function PFXImportCertStore(var pPFX: CRYPT_DATA_BLOB; szPassword: LPCWSTR; dwFlags: DWORD): HCERTSTORE; stdcall; external 'Crypt32.dll';
Luego en el método
Código Delphi
[-]HTTPRIOHTTPWebNodeBeforePost(const HTTPReqResp: THTTPReqResp; Client: THTTPClient)
se pone lo siguiente:
Código Delphi
[-]
const
PKCS12_INCLUDE_EXTENDED_PROPERTIES = $0010;
CERT_COMPARE_HAS_PRIVATE_KEY = 21;
CERT_FIND_HAS_PRIVATE_KEY = CERT_COMPARE_HAS_PRIVATE_KEY shl CERT_COMPARE_SHIFT;
var
Almacen: HCERTSTORE;
Certificado: PCERT_CONTEXT;
DataBlob: CRYPT_DATA_BLOB;
PFX: TBytes;
begin
Almacen := nil;
Certificado := nil;
PFX := TFile.ReadAllBytes(Cert.Text);
Try
DataBlob.cbData := Length(PFX);
DataBlob.pbData := @PFX[0];
Almacen := PFXImportCertStore(DataBlob, PWideChar(Pwd.Text), PKCS12_INCLUDE_EXTENDED_PROPERTIES);
If not Assigned(Almacen) Then
Salida.Lines.Add('[ERROR] No se pudo importar el certificado seleccionado.') Else
Begin
Certificado := CertFindCertificateInStore(Almacen, X509_ASN_ENCODING, 0, CERT_FIND_HAS_PRIVATE_KEY, nil, nil);
If not Assigned(Certificado) Then
Salida.Lines.Add('[ERROR] No se pudo encontrar el certificado digital en el contexto actual, o bien no tiene clave privada.'); End;
Finally
If Assigned(Certificado) Then CertFreeCertificateContext(Certificado);
If Assigned(Almacen) Then CertCloseStore(Almacen, 0);
End;
end;
, sabiendo que la ruta completa al certificado .pfx o .p12 está en un campo TEdit llamado 'Cert', y su contraseña en otro TEdit llamado 'Pwd'.
Creo que el código también es compatible con Delphi 12.1 Athens.
No olviden asignar este método al OnBeforePost del objeto HTTPRIO que realiza la petición al webservice, de lo contrario recibirán el error
Received content of invalid Content-Type setting: text/html - SOAP expects "text/xml".
De esta manera, conseguimos 3 mejoras:
1. Poder cambiar el certificado digital y su contraseña a petición del usuario justo antes de hacer la llamada al webservice.
2. Nos libramos de tener que modificar los fuentes de las librerías de Delphi.
3. Prescindimos de la antigua librería capicom.dll.
Tenéis mi mención a esto mismo en el foro del SII:
https://www.clubdelphi.com/foros/sho...387#post556387
Espero haber ayudado. Saludos.