(**************************************************************************)
(*                                                                        *)
(*  Web server input stream handler                                       *)
(*  Copyright (C) 2025   Peter Moylan                                     *)
(*                                                                        *)
(*  This program is free software: you can redistribute it and/or modify  *)
(*  it under the terms of the GNU General Public License as published by  *)
(*  the Free Software Foundation, either version 3 of the License, or     *)
(*  (at your option) any later version.                                   *)
(*                                                                        *)
(*  This program is distributed in the hope that it will be useful,       *)
(*  but WITHOUT ANY WARRANTY; without even the implied warranty of        *)
(*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *)
(*  GNU General Public License for more details.                          *)
(*                                                                        *)
(*  You should have received a copy of the GNU General Public License     *)
(*  along with this program.  If not, see <http://www.gnu.org/licenses/>. *)
(*                                                                        *)
(*  To contact author:   http://www.pmoylan.org   peter@pmoylan.org       *)
(*                                                                        *)
(**************************************************************************)

DEFINITION MODULE TLS;

        (********************************************************)
        (*                                                      *)
        (*                Transport Layer Security              *)
        (*                                                      *)
        (*  Programmer:         P. Moylan                       *)
        (*  Started:            2 April 2018                    *)
        (*  Last edited:        29 July 2025                    *)
        (*  Status:             Mostly working                  *)
        (*                                                      *)
        (********************************************************)


FROM SYSTEM IMPORT CARD16, CARD8, LOC;

FROM Sockets IMPORT
    (* type *)  Socket;

FROM TransLog IMPORT
    (* type *)  TransactionLogID;


TYPE TLSsession;        (* is private *)


PROCEDURE SetSingleCertificateMode;

    (* Sets a mode where the domain name is ignored when fetching a     *)
    (* server certificate or private key, because the one certificate   *)
    (* is used for all domains.                                         *)

PROCEDURE OpenTLSsession (S: Socket;  as_server: BOOLEAN;
                            serveraddr: CARDINAL;
                            serverdomain: ARRAY OF CHAR;
                              logID: TransactionLogID;
                                VAR (*OUT*) session: TLSsession): BOOLEAN;

    (* Opens a new connection.  Returns FALSE if the operation failed.  *)

    (* If as_server is FALSE, the serverdomain parameter specifies the  *)
    (* domain the client is trying to contact. Otherwise, it specifies  *)
    (* an initial value for the domain the server is hosting, but in    *)
    (* that case it will probably be overridden by the domain           *)
    (* specified in a ClientHello message.                              *)

PROCEDURE CloseTLSsession (VAR (*INOUT*) sess: TLSsession);

    (* Deletes the session data. *)

PROCEDURE TLSsend (sess: TLSsession; message: ARRAY OF LOC;
                                               length: CARDINAL): BOOLEAN;

    (* Sends a message of "length" bytes.  *)

PROCEDURE TLSrecv (sess: TLSsession;  VAR (*OUT*) message: ARRAY OF LOC;
                    limit: CARDINAL;  VAR (*OUT*) actual: CARDINAL;
                    VAR (*OUT*) ConnectionLost: BOOLEAN);

    (* Receives a message of up to "length" bytes.  The "actual"        *)
    (* parameter returns the actual number of bytes received, or        *)
    (* MAX(CARDINAL) if there was an error.                             *)

PROCEDURE TLSrecvLine (sess: TLSsession;  VAR (*OUT*) message: ARRAY OF LOC;
                          limit: CARDINAL;
                                VAR (*OUT*) actual: CARDINAL): BOOLEAN;

    (* Like TLSrecv, but returns data up to the next CRLF line          *)
    (* terminator.  The CRLF is discarded.                              *)

END TLS.

