0

We are forced to send (and receive back) JSON data to the government from our restaurant program

  • using a special RSA Key (PEM stream) they provide
  • using TLS 1.3 (OpenSSL 3.0+) < This works! :-)

The only way I found is to create a separate DLL for that under Lazarus, which can use a separate component to do that, (and load that special key, so it does not interfere with the main program's Indy component.)

Ararat Synapse's new Trunk (R260) can handle OpenSSL 3.0, so I've downloaded and installed as component, but the JSON I'm sending to a test server (webhook.com) are distorted krix-krax.

How do I load a special .PEM key/stream (not a file!) for the SSL communication before sending it?

Here is my code:

function HttpPostGetJSON(const URL: string; var JSON: UTF8String; const key, pem: string; const TimeOut: integer = 5000): Boolean;
var
    HTTP    : THTTPSend;
    Data    : TStringStream;
begin
    HTTP := THTTPSend.Create;
    Data := TStringStream.Create(JSON, TEncoding.UTF8);
    JSON := '';
    HTTP.Timeout := TimeOut;           

    try
        HTTP.Headers.Add('Content-Type: application/json; charset=UTF-8') ;
        HTTP.Headers.Add('Accept: application/json') ;

        // TODO: specify PEM key for the SSL !

        Data.Position := 0;
        HTTP.Document.CopyFrom( Data, 0); 
        Data.Size := 0;

        Result := HTTP.HTTPMethod('POST', URL);
        if Result then begin
            if HTTP.Document.Size > 0 then begin
                Data.LoadFromStream( HTTP.Document );
                JSON := Data.UnicodeDataString;  
            end;
        end;
    finally
        HTTP.Free;
        Data.Free;
    end;
end;
Thom A
  • 88,727
  • 11
  • 45
  • 75
SzakiLaci
  • 347
  • 1
  • 16
  • I think the solution will be: `HTTP.Sock.SSL.Certificate := '...' ; ` and `...SSL.PrivateKey:= '...';` but first I have to get that PEM file from the government to test it. – SzakiLaci Apr 20 '22 at 18:11
  • Well, it does not work this way. If I set both CER + KEY like this: `HTTP.Sock.SSL.Certificate := '-----BEGIN CERTIFICATE-----'#10+'MIIG...'` I get from `TSSLOpenSSL(HTTP.Sock.SSL).LastErrorDesc` this: `error:068000A8:asn1 encoding routines::wrong tag` . – SzakiLaci May 08 '22 at 16:06

0 Answers0