Frequently Asked Component Specific Questions

Options

Display all FAQ items

Search FAQ items:


Displaying items 1 to 1 of 1, page 1 of 1

<< previous next >>

TMS Sparkle
Using client-certificates with Sparkle in Windows

You can send client-certificates when performing HTTP requests using Sparkle, from Windows. The following code snippet shows how to do it. Two comments about the code:

a) You have to declare TInternalHTTPClient in the same unit you use the code below, preferable in the implementation section

b) This code snippet shows also how to retrieve the certificate from Windows store. It’s there as an example but you can and should replace with your own code to retrieve the certificate. That part of the code is not Sparkle-related and you should be familiar with the Windows API that handles certificates. The relevant Sparkle code is highlighted in bold.

TInternalHTTPClient = class(THttpClient)
end;
 
var
  httpClient: THttpClient;
  httpEngine: TWinHttpEngine;
  httpRequest: THttpRequest;
  httpResponse: THttpResponse;
  Store: HCERTSTORE;
  Cert: PCERT_CONTEXT;
begin
  httpClient := THttpClient.Create;
 
  // Open the ''Personal'' SSL certificate store for the local machine and locate the required client-side certificate
  Cert := nil;
  Store := CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, 0, CERT_SYSTEM_STORE_LOCAL_MACHINE, PChar(''MY''));
  if (Store <> nil) then
    Cert := CertFindCertificateInStore(Store, X509_ASN_ENCODING, 0, CERT_FIND_SUBJECT_STR, PChar(''mycertsubject''), nil);
 
  // If a valid certificate was found then OK to create and send the HTTP request
  if (Cert <> nil) then
  begin
    // Setup HTTP request properties
    httpRequest := httpClient.CreateRequest;
    ...

    // Use ''BeforeWinHttpSendRequest'' event to set any HTTP request properties such as client-side SSL certificate
    httpEngine := TWinHttpEngine(TInternalHTTPClient(httpClient).Engine);
    httpEngine.BeforeWinHttpSendRequest :=
      procedure (Req: HINTERNET)
      begin
        WinHttpCheck(WinHttpSetOption(Req, WINHTTP_OPTION_CLIENT_CERT_CONTEXT, Cert, SizeOf(CERT_CONTEXT)));
      end;

    // Execute HTTP request
    httpResponse := httpClient.Send(httpRequest);
  end;