DEFINITION MODULE TLSsessID;

        (********************************************************)
        (*                                                      *)
        (*                Transport Layer Security              *)
        (*                Cacheing of session IDs               *)
        (*                                                      *)
        (*  Programmer:         P. Moylan                       *)
        (*  Started:            25 January 2025                 *)
        (*  Last edited:        2 August 2025                   *)
        (*  Status:             Complete, being tested          *)
        (*                                                      *)
        (********************************************************)


FROM SYSTEM IMPORT CARD8;

FROM TLSCipherSuites IMPORT
    (* type *)  CipherSuite;

FROM Names IMPORT
    (* type *)  HostName;

FROM VarStrings IMPORT
    (* type *)  ByteStr;


CONST lastAN = 10;

TYPE
    sessIDtype = ARRAY [0..32] OF CARD8;

    (* We only generate session IDs with lastAN+1 characters, plus a trailing   *)
    (* Nul, but other servers and clients might use longer IDs.                 *)



PROCEDURE BlockChecksum (N: CARDINAL;  VAR (*IN*) A: ARRAY OF CARD8): CARD8;

    (* Calculates a checksum for an array of N bytes. *)

PROCEDURE SetINIname (name: ARRAY OF CHAR);

    (* Takes note of our INI name. *)

PROCEDURE LastSavedID (hostaddr: CARDINAL;
                        VAR (*OUT*) sessID: sessIDtype): CARDINAL;

    (* Finds the ID of the last saved session for this host, returns    *)
    (* its length (or 0 if none found).  This is a client function.     *)

PROCEDURE GenerateSessionID (VAR (*OUT*) sessID: sessIDtype;
                                VAR (*OUT*) IDlength: CARDINAL);

    (* Assigns a session ID to a new session. *)
    (* This is a server function. *)

PROCEDURE DeleteSessID (addr: CARDINAL;  ID: sessIDtype;  IDlength: CARDINAL);

    (* Destroys this ID, since we believe it to be obsolete. *)

PROCEDURE AppendSessID (VAR (*IN*) ID: sessIDtype;  IDlength: CARDINAL;
                                    VAR (*INOUT*) message: ARRAY OF CHAR);

    (* For logging: Appends ID, as an ASCII string, to message. *)

PROCEDURE SaveSessionParams (hostaddr: CARDINAL;  VAR (*IN*) sessID: sessIDtype;
                                IDlength: CARDINAL;
                                  ciphersuite: CipherSuite;
                                    VAR (*IN*) mastersecret: ARRAY OF CARD8);

    (* Saves a copy of the current data for ID.    *)
    (* Called by GenerateKeyBlock, therefore by both server and client. *)

PROCEDURE RestoreSession (hostaddr: CARDINAL;  VAR (*IN*) sessID: sessIDtype;
                           IDlength: CARDINAL;
                           VAR (*OUT*) ciphersuite: CipherSuite;
                           VAR (*OUT*) mastersecret: ARRAY OF CARD8): BOOLEAN;

    (* Returns FALSE if a matching seeionID cannot be found.  If it is  *)
    (* found, returns TRUE with the saved ciphersuit and mastersecret.  *)
    (* Called by server.  Also by  client, but is this right? *)

END TLSsessID.

