/*******************************************************************

Copyright (c) 2004 Open Mobile Alliance, Ltd.  All Rights Reserved.

This software is made available under the usage and distribution terms 
covered by the Common Public License 1.0 and by using this software you
are bound by the terms and conditions of the same.

EXCEPT AS EXPRESSLY SET FORTH IN THE COMMON PUBLIC LICENSE,
THIS PROGRAM IS PROVIDED ON AN "AS IS" BASIS, 
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, 
ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT, 
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
   
Each recipient is solely responsible for determining the 
appropriateness of using and distributing this program and assumes 
all risks associated with its exercise of rights under the Common 
Public License, including but not limited to the risks and 
costs of program errors, compliance with applicable laws, 
damage to or loss of data, programs or equipment, and 
unavailability or interruption of operations.

********************************************************************/

/******************************************************************
Change history

Ref#0004	29/05/2002	mashok@in.ibm.com
******************************************************************/

#ifndef _SCTS_AUTH_H
#define _SCTS_AUTH_H

#include "SctsPlatformLib.h"
#include "SctsCommonElements.h"

/* Maximum userID and password size */
#define MAX_USERID_SIZE		30
#define MAX_PASSWORD_SIZE	30
#define MAX_NONCE_SIZE		30

/* Base 64 Encoding/Decoding functions */
char *sctsBase64Encode(unsigned char *source,unsigned int& sourceSize);
unsigned char *sctsBase64Decode(char *source,unsigned int& targetSize);


#ifdef __SCTS_USING_RTK__
#include "global.h"
#include "md5.h"
#else

/* GLOBAL.H - RSAREF types and constants
 */

/* PROTOTYPES should be set to one if and only if the compiler supports
  function argument prototyping.
  The following makes PROTOTYPES default to 0 if it has not already
  been defined with C compiler flags.
 */
#ifndef PROTOTYPES
#define PROTOTYPES 1
#endif

/* POINTER defines a generic pointer type */
typedef unsigned char *POINTER;

/* UINT2 defines a two byte word */
typedef unsigned short int UINT2;

/* UINT4 defines a four byte word */
typedef unsigned long int UINT4;

/* PROTO_LIST is defined depending on how PROTOTYPES is defined above.
If using PROTOTYPES, then PROTO_LIST returns the list, otherwise it
  returns an empty list.
 */
#if PROTOTYPES
#define PROTO_LIST(list) list
#else
#define PROTO_LIST(list) ()
#endif

typedef struct {
  UINT4 state[4];                                   /* state (ABCD) */
  UINT4 count[2];        /* number of bits, modulo 2^64 (lsb first) */
  unsigned char buffer[64];                         /* input buffer */
} MD5_CTX;

void MD5Init PROTO_LIST ((MD5_CTX *));
void MD5Update PROTO_LIST
  ((MD5_CTX *, char *, unsigned int));
void MD5Final PROTO_LIST ((char [16], MD5_CTX *));

#endif


typedef enum
{
	AUTH_NONE=0,
	AUTH_BASIC,
	AUTH_MD5,
	AUTH_HMAC,
} SctsAuthType;


class SctsAuth
{
/* Data */
protected:
	SctsAuthType type;

/* Implementation */
public:
	virtual char *BuildAuthString(char *userID,char *password,unsigned int& size)=0;
	virtual bool VerifyAuthString(char *userID,char *password,char *credential,unsigned int size)=0;
	virtual SctsCred *BuildCred(char *userID,char *password,SctsEncoding encoding)=0;
	virtual SctsChal *BuildChal(SctsEncoding encoding)=0;
	virtual int VerifyCred(SctsCred& cred,char *userID,char *password,SctsEncoding encoding);
	virtual void Save(FILE *);
	static SctsAuth *Load(FILE *);
	SctsAuth(SctsAuthType _type) {type=_type;}
	SctsAuthType GetType() {return type;}
	/* Ref#0004 : Used for AuthMD5 destructor */
	virtual ~SctsAuth() {}
};

class SctsAuthBasic : public SctsAuth
{
/* Implementation */
public:
	char *BuildAuthString(char *userID,char *password,unsigned int& size);
	bool VerifyAuthString(char *userID,char *password,char *credential,unsigned int size);
	SctsAuthBasic();
	SctsCred *BuildCred(char *userID,char *password,SctsEncoding encoding);
	SctsChal *BuildChal(SctsEncoding encoding);
};

class SctsAuthMD5 : public SctsAuth
{
public:
/* Data */
	unsigned char *nonce;
	unsigned int size;

/* Implementation */
	char *BuildAuthString(char *userID,char *password,unsigned int& size);
	bool VerifyAuthString(char *userID,char *password,char *credential,unsigned int size);
	SctsCred *BuildCred(char *userID,char *password,SctsEncoding encoding);
	SctsChal *BuildChal(SctsEncoding encoding);
	int VerifyCred(SctsCred& cred,char *userID,char *password,SctsEncoding encoding);
	SctsAuthMD5(char *_nonce);
	SctsAuthMD5(unsigned char *_nonce,unsigned int _size);
	void Save(FILE *);
	SctsAuthMD5(FILE *);
	/* Ref#0004 : AuthMD5 destructor required to free nonce */
	~SctsAuthMD5() {if (nonce) sctsFree(nonce);}
};

	
class SctsAuthHMAC : public SctsAuth
{
public:
/* Data */
	unsigned char *nonce;
	unsigned int size;
	int	status;
/* Implementation */
	char *BuildHMACDigest(char *userID,char *password,void *message,int messageLen);
	bool VerifyHMACDigest(char *userID,char *password,void *message,char *Digest,int len);
	SctsChal *BuildChal(SctsEncoding encoding);
	/* Constructor */
	SctsAuthHMAC();
	SctsAuthHMAC(char *_nonce);
	SctsAuthHMAC(unsigned char *_nonce,unsigned int _size);
	void Save(FILE *);
	SctsAuthHMAC(FILE *);
	~SctsAuthHMAC() {if (nonce) sctsFree(nonce);}
	/* Not used, but there for the sake of compatibility with other Auth types.*/
	char *BuildAuthString(char *userID,char *password,unsigned int& size){return 0;};
	bool VerifyAuthString(char *userID,char *password,char *credential,unsigned int size){return false;};
	SctsCred *BuildCred(char *userID,char *password,SctsEncoding encoding){return 0;};
	int VerifyCred(SctsCred& cred,char *userID,char *password,SctsEncoding encoding);
};



#endif
