Jump to content
isnagil

HTTPRIO load a certificate from pfx file

Recommended Posts

Hello!

 

I have to communicate with a webservice using a certificate.

I'm trying to load a certificate by directly reading a pfx file to avoid the certificate selection screen being displayed. This prompt:

 

image.png.aebc34ca809d05e5c35cf6d8d4f2c566.png

 

I found this on the web but it is from a delphi 2010 version and I use 10.4. The parameters of the event have changed and the compiler doesn´t reconize data variable.

 

HTTPRIO1HTTPWebNode1BeforePost(const HTTPReqResp: THTTPReqResp; Client: THTTPClient);
  procedure CheckError(Point: Pointer);
  begin
    if not Assigned(Point) then
      RaiseLastOSError;
  end;

const
  INTERNET_OPTION_CLIENT_CERT_CONTEXT = 84;
  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;

  Pass = 'XXXXXXXXXX';
var
  pStore: HCERTSTORE;
  pCert: PCERT_CONTEXT;
  DataBlob: CRYPT_BIT_BLOB;
  PFX: TBytes;
begin
  pStore := nil;
  pCert := nil;

  PFX := TFile.ReadAllBytes(ExtractFilePath(Application.ExeName) + 'NameOfCertificate.pfx');

  try
    DataBlob.cbData := Length(PFX);
    DataBlob.pbData := @PFX[0];
    
    pStore := PFXImportCertStore(DataBlob, PWideChar(Pass), PKCS12_INCLUDE_EXTENDED_PROPERTIES);
    CheckError(pStore);

    pCert := CertFindCertificateInStore(pStore,
                                        X509_ASN_ENCODING,
                                        0,
                                        CERT_FIND_HAS_PRIVATE_KEY, //CERT_FIND_ANY,
                                        nil,
                                        nil);
    CheckError(pCert);

    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;

Older version: 

HTTPRIO1HTTPWebNode1BeforePost(const HTTPReqResp: THTTPReqResp; Data: Pointer);

New version:

 

HTTPRIO1HTTPWebNode1BeforePost(const HTTPReqResp: THTTPReqResp; Client: THTTPClient);

 

Can someone help me?

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×