Recent

Author Topic: Client certificate mandatory and verification  (Read 1431 times)

nummer8

  • Full Member
  • ***
  • Posts: 124
Client certificate mandatory and verification
« on: March 23, 2024, 12:48:38 pm »
Hi,

Out of the info on the wiki I created a simple Webserver with a server-certificate.
To get this code working you need to create the necessary certificate.
For this I used xca from https://hohnstaedt.de but you can use OpenSSL to do the same.
 

Code: Pascal  [Select][+][-]
  1. program webserver;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   {$ifdef UNIX}
  7.   cthreads, cmem,
  8.   {$endif}
  9.   fphttpapp,
  10.   httpdefs,
  11.   httproute,
  12.   opensslsockets;
  13.  
  14. var
  15.   fUseSSL: boolean;
  16. const
  17.   fCertificatePassword: string = 'hello';
  18.   fCertificateHostName: string = 'localhost';
  19.   fCertificateFileName: string = 'Server.crt';
  20.   fCertificatePrivateKey: string = 'Server.key';
  21.  
  22.   procedure route1(aReq: TRequest; aResp: TResponse);
  23.   begin
  24.     aResp.Content := '<html><body><h1>Route 1 The Default</h1></body></html>';
  25.   end;
  26.  
  27.   procedure route2(aReq: TRequest; aResp: TResponse);
  28.   begin
  29.     aResp.Content := '<html><body><h1>Route 2</h1></body></html>';
  30.   end;
  31.  
  32. begin
  33.   HTTPRouter.RegisterRoute('/', @route1);
  34.   HTTPRouter.RegisterRoute('/2', @route2);
  35.   Application.Port := 1999;
  36.   fUseSSL :=true;
  37.   Application.UseSSL := fUseSSL;
  38.   if fUseSSL then
  39.   begin
  40.     Application.CertificateData.KeyPassword := fCertificatePassword;
  41.     Application.CertificateData.HostName := fCertificateHostName;
  42.     Application.CertificateData.Certificate.FileName := fCertificateFileName;
  43.     Application.CertificateData.PrivateKey.FileName := fCertificatePrivateKey;
  44.   end;
  45.   Application.Threaded := True;
  46.   Application.Initialize;
  47.   Application.Run;
  48. end.
  49.  

My questions are:
- How can I modify this example to enforce the use of a client certificate?
- How can I verify a client certificate in the server?


In the TLS handshake a client certificate is optional but the server can ensure that it is mandatory.

Any help, pointers, sample code is appreciated.

Thaddy

  • Hero Member
  • *****
  • Posts: 19269
  • Glad to be alive.
Re: Client certificate mandatory and verification
« Reply #1 on: March 23, 2024, 12:56:20 pm »
Since a few days there is an example to do just that, but there is also a small bug in that.
Thr bug is easy to spot and easy to fix for programmers that involve themselves in certificates and really understand them.
objects are fine constructs. You can even initialize them with constructors.

nummer8

  • Full Member
  • ***
  • Posts: 124
Re: Client certificate mandatory and verification
« Reply #2 on: March 23, 2024, 03:11:24 pm »
Just to be complete and if some one reads this post in the future.
I assume to refer to the fcm example in the fcl-web package?

https://gitlab.com/freepascal.org/fpc/source/-/tree/main/packages/fcl-web/examples/fcm?ref_type=heads

Thaddy

  • Hero Member
  • *****
  • Posts: 19269
  • Glad to be alive.
Re: Client certificate mandatory and verification
« Reply #3 on: March 23, 2024, 05:11:25 pm »
No the fcl-hash package.
« Last Edit: March 24, 2024, 11:48:16 am by Thaddy »
objects are fine constructs. You can even initialize them with constructors.

 

TinyPortal © 2005-2018