/* Agent Tcl
   Bob Gray
   17 April 1996 
   
   agentSecure.h
  
   This file defines the data structures that keep track of the current
   security parameters.

   Copyright (c) 1995-1996, Bob Gray, Dartmouth College

   See the file "agent.terms" for information on usage and redistribution
   of this file and for a DISCLAIMER OF ALL WARRANTIES.
*/

#ifndef _AGENT_SECURE_H
#define _AGENT_SECURE_H

#ifndef NO_PRAGMAS
#pragma interface
#endif

#ifndef _SUPP_DSTRING_H
#include "suppDString.h"		// DynamicString
#endif
#ifndef _TRUEFALSE_H
#include "truefalse.h"		// BOOLEAN
#endif

    /* struct SEC_IDENTITY keeps track of a keyname and the associated pass */
    /* phrase.  Note that this structure is somewhat PGP dependent since    */
    /* PGP uses a pass phrase to unlock the user's private key whereas      */
    /* other security systems do not.					    */

struct SEC_IDENTITY
{
    BOOLEAN       m_authenticated;
    BOOLEAN       m_claimed;
    DynamicString m_keyname;
    DynamicString m_phrase;

	/* constructors and destructors */

    SEC_IDENTITY (void): 
	m_authenticated (e_FALSE),
	m_claimed (e_FALSE),
	m_keyname (), 
	m_phrase ()
    {
	/* empty */
    } 

    SEC_IDENTITY (const SEC_IDENTITY& id):
	m_authenticated (id.m_authenticated),
	m_claimed (id.m_claimed),
	m_keyname (id.m_keyname),
	m_phrase (id.m_phrase)
    {
	/* empty */
    } 

    SEC_IDENTITY (const DynamicString& keyname, const DynamicString& phrase, BOOLEAN authenticated, BOOLEAN claimed):
	m_authenticated (authenticated),
	m_claimed (claimed),
	m_keyname (keyname),
	m_phrase (phrase)
    {
	/* empty */
    } 

    ~SEC_IDENTITY () {
	// empty
    } 

	/* assignment operator */

    SEC_IDENTITY& operator= (const SEC_IDENTITY& id) {
	m_keyname       = id.m_keyname;
	m_phrase        = id.m_phrase;
	m_authenticated = id.m_authenticated;
	m_claimed       = id.m_claimed;
	return (*this);
    }
};

    /* class BASE_SECURITY describes the security characteristics that */
    /* are common to agents, servers and incoming messages             */

struct BASE_SECURITY
{
protected:

	/* id of the owner and id of the machine */

    struct SEC_IDENTITY m_owner;
    struct SEC_IDENTITY m_machine;

	/* "trustedMachine" is e_TRUE if the machine as one the server's */
	/* TrustedMachine's and e_FALSE if the machine is not one of the */
	/* server's TrustedMachine's                                     */

    BOOLEAN m_trustedMachine;

	/* "agentAuthenticated" is TRUE if we were able to verify the */
	/* actual agent and FALSE otherwise.  If "agentAuthenticated" */
	/* is TRUE, "localId" contains the numeric id of the          */
	/* authenticated agent.      				      */

    BOOLEAN m_agentAuthenticated;
    UINT_32 m_localId;

	/* "ownerSigned" is e_TRUE if we should sign with the owner's */
	/* key and e_FALSE if we should sign with the machine's key   */

    BOOLEAN m_ownerSigned;

	/* "encryption" is true if outgoing messages should be encrypted */
	/* "signatures" is true if outgoing messages should be signed    */
 
    BOOLEAN m_encryption; 
    BOOLEAN m_signatures;

public:

	/* constructor */

    BASE_SECURITY (void):
	m_trustedMachine (e_FALSE),
	m_agentAuthenticated (e_FALSE),
	m_localId (0),
	m_ownerSigned (e_TRUE),
	m_encryption (e_FALSE),
	m_signatures (e_FALSE)
    {
	// empty
    } 

    virtual ~BASE_SECURITY () {
	// empty
    }

	/* return or set the owner */

    SEC_IDENTITY getOwner (void) const {
	return (m_owner);
    } 

    DynamicString getOwnerKeyname (void) const {
	return (m_owner.m_keyname);
    } 

    void setOwner (const SEC_IDENTITY &owner) {
	m_owner = owner;
    } 

    void transferOwner (const BASE_SECURITY &security) {
	m_owner = security.m_owner;
    }

	/* see if we have a particular owner or the same owner */

    BOOLEAN isOwnerKeyname (const DynamicString& ownerKeyname) {
	return ((ownerKeyname == m_owner.m_keyname) ? e_TRUE : e_FALSE);
    }

    BOOLEAN isOwnerKeynameSame (const BASE_SECURITY& security) {
	return ((security.m_owner.m_keyname == m_owner.m_keyname) ? e_TRUE : e_FALSE);
    }

	/* return or set the machine */

    SEC_IDENTITY getMachine (void) const {
	return (m_machine);
    } 

    DynamicString getMachineKeyname (void) const {
	return (m_machine.m_keyname);
    }

    void setMachine (const SEC_IDENTITY &machine) {
	m_machine = machine;
    }

    void transferMachine (const BASE_SECURITY &security) {
	m_machine = security.m_machine;
    } 

	/* see if the owner is authenticated */

    BOOLEAN isOwnerAuthenticated (void) const {
	return (m_owner.m_authenticated);
    }

    void setOwnerAuthenticated (BOOLEAN flag) {
	m_owner.m_authenticated = flag; 
    }

    BOOLEAN isOwnerClaimed (void) const {
	return (m_owner.m_claimed);
    }

    void setOwnerClaimed (BOOLEAN flag) {
	m_owner.m_claimed = flag;
    }

	/* see if the machine is authenticated */

    BOOLEAN isMachineAuthenticated (void) const {
	return (m_machine.m_authenticated);
    }

    void setMachineAuthenticated (BOOLEAN flag) {
	m_machine.m_authenticated = flag;
    }

    BOOLEAN isMachineClaimed (void) const {
	return (m_machine.m_claimed);
    }

    void setMachineClaimed (BOOLEAN flag) {
	m_machine.m_claimed = flag;
    }

	/* see if the machine is a TrustedMachine */

    BOOLEAN isMachineTrusted (void) const {
	return (m_trustedMachine);
    }

    void setMachineTrusted (BOOLEAN flag) {
	m_trustedMachine = flag;
    }

	/* see if the agent has been authenticated */ 

    BOOLEAN isAgentAuthenticated (void) const {
	return (m_agentAuthenticated);
    } 

    UINT_32 getAgentId (void) const {
	return (m_localId);
    } 

    void setAgentAuthenticated (BOOLEAN flag, UINT_32 id) {
	m_agentAuthenticated = flag;
	m_localId            = id;
    } 

	/* check or decide whether the owner is signing */

    BOOLEAN isOwnerSigned (void) const {
	return (m_ownerSigned);
    } 

    void setOwnerSigned (BOOLEAN flag) {
	m_ownerSigned = flag; 
    } 

	/* return the signer (either the owner or the machine) */

    SEC_IDENTITY getSigner (void) const {
	if (m_ownerSigned) {
	    return (m_owner);
	} else {
	    return (m_machine);
	}
    } 

	/* return whether or not we are encrypting or signing */

    BOOLEAN shouldEncrypt (void) const {
	return (m_encryption);
    }

    BOOLEAN shouldSign (void) const {
	return (m_signatures);
    }

	/* turn on or off encryption or signatures */

    virtual int setEncryption (BOOLEAN p_encryption);
    virtual int setSignatures (BOOLEAN p_signatures);

	/* linearization */

    int getIdLinearSize (void);
    int getCompleteLinearSize (void);
    char *idLinearize (char *bp);
    char *completeLinearize (char *bp);
    char *idDelinearize (char *bp, char *end_bp);
    char *completeDelinearize (char *bp, char *end_bp);
};

#endif /* _AGENT_SECURE */
