networksecurity/tls/protocol/tlshandshake.h
changeset 0 af10295192d8
equal deleted inserted replaced
-1:000000000000 0:af10295192d8
       
     1 /**
       
     2 * Copyright (c) 2003-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 * SSL3.0 and TLS1.0 Handshake negotiation header file.
       
    16 * This file describes the Handshake negotiation state machine.
       
    17 * 
       
    18 *
       
    19 */
       
    20 
       
    21 
       
    22 
       
    23 /**
       
    24  @file TlsHandshake.h
       
    25 */
       
    26 
       
    27 #ifndef _TLSHANDSHAKE_H_
       
    28 #define _TLSHANDSHAKE_H_
       
    29 
       
    30 #include <x509cert.h>
       
    31 #include <comms-infras/statemachine.h>
       
    32 #include "LOGFILE.H"
       
    33 #include <tlstypedef.h>
       
    34 
       
    35 // History of the Handshake negotiation (i.e. events that have occured).
       
    36 enum ETlsHandshakeHistory {
       
    37 	ETlsHandshakeStart = 0,					/** Start of handshake negotiation */
       
    38 	ETlsClientHelloSent = 0x0001,			/** Client Hello has been sent */
       
    39 	ETlsServerHelloRecv = 0x0002,			/** Server Hello has been received */
       
    40 	ETlsServerCertificateRecv = 0x0004,		/** Server Certificate has been received */
       
    41 	ETlsServerKeyExchRecv = 0x0008,			/** Server Key Exchange has been received */
       
    42 	ETlsCertificateReqRecv = 0x0010,			/** Certificate Request has been received */
       
    43 	ETlsServerHelloDoneRecv = 0x0020,		/** Server Hello Done has been received */
       
    44 	ETlsClientCertificateSent = 0x0040,		/** Client Certificate has been sent */
       
    45 	ETlsClientKeyExchSent = 0x0080,			/** Client Key Exchange has been sent */
       
    46 	ETlsCertificateVerifySent = 0x0100,		/** Certificate Verify has been sent */
       
    47 	ETlsFinishedSent = 0x0200,				/** Client Finished has been sent */
       
    48 	ETlsFinishedRecv = 0x0400,				/** Server Finished has been received */
       
    49 	ETlsChangeCipherSent = 0x0800,			/** Change CipherSpec has been sent */
       
    50 	ETlsChangeCipherRecv = 0x1000,			/** Change CipherSpec has been received */
       
    51 	ETlsFullHandshake = 0x2000,				/** Full Handshake negotiation */
       
    52 	ETlsAbbreviatedHandshake = 0x4000,		/** Abbreviated handshake negotiation */
       
    53 	ETlsUsingPskKeyExchange = 0x8000		/** Using PSK for the key exchange */
       
    54 };
       
    55 
       
    56 const TInt8 KNullSessionId = 0;				// Zero value session id. 
       
    57 
       
    58 class CTlsConnection;
       
    59 class CTlsEvent;
       
    60 class CSHA1;  
       
    61 class CMD5;
       
    62 class CSendAlert;
       
    63 class CTLSSession;
       
    64 class CHandshake : public CStateMachine
       
    65 /** 
       
    66  * @class This class describes a state machine which handles SSL3.0/TLS1.0
       
    67  * handshake negotiations.
       
    68  */
       
    69 {
       
    70 public:
       
    71 	static CHandshake* NewL( CTlsConnection& aTlsConnection ); 
       
    72    
       
    73 	~CHandshake();
       
    74 	void StartL( TRequestStatus* aClientStatus, MStateMachineNotify* aStateMachineNotify );
       
    75 
       
    76 	CTlsEvent* InitiateReceiveL();
       
    77 	CTlsEvent* InitiateTransmitL();
       
    78 	void UpdateVerify( TDesC8& aMessage );
       
    79 	CSHA1* SHA1Verify() const;
       
    80 	CMD5*  MD5Verify() const;
       
    81 	CSendAlert* SendAlert() const;
       
    82 	TPtr8& ComposeMsg();
       
    83 	TBool SessionReUse() const;
       
    84 	CX509Certificate*& ServerCert();
       
    85 	TAlgorithmId& SignatureAlg();
       
    86 	void SetCertificateVerifyReqd( TBool aCertVerify );
       
    87 	TBool CertificateVerifyReqd() const;
       
    88 	void SetNegotiatedVersion( const TTLSProtocolVersion* aTlsVersion );
       
    89 	TSglQue<CTlsEvent>& TxMessageList();
       
    90 	void DestroyTxList();
       
    91 	void AddToList( CTlsEvent& aMsgItem);
       
    92    void GetServerAddrInfo( TTLSServerAddr& serverInfo );
       
    93    void ResetCryptoAttributes();
       
    94    CTLSSession*& TlsSession();
       
    95 
       
    96    CTlsEvent* NextTxEvent();       //fetches the next event from the Tx queue and shifts
       
    97                                     //the queue iterator one element up
       
    98 	
       
    99 	void Cancel(TInt aLastError);
       
   100 
       
   101 
       
   102 protected:
       
   103 	CHandshake( CTlsConnection& aTlsConnection );
       
   104 	void ConstructL( CTlsConnection& aTlsConnection );
       
   105 
       
   106 	virtual void DoCancel();
       
   107 
       
   108 protected:
       
   109 	CTlsConnection& iTlsConnection;		// Reference to the TlsConnection object that's negotiating a handshake
       
   110 	CSHA1* iSHA1Verify;					// SHA1 hash object, used to hash the handshake messages
       
   111 	CMD5*  iMD5Verify;					// MD5 hash object, used to hash the handshake messages
       
   112 public:
       
   113 	TSglQue<CTlsEvent> iTransmitList;	// List of events (messages) to transmit
       
   114 protected:
       
   115 	TSglQueIter<CTlsEvent> iTxListIter;	// List iterator
       
   116 	CSendAlert* iSendAlert;				// Pointer to a 'Send Alert' object
       
   117 public:
       
   118 	TPtr8 iComposeMsg;					// Pointer to a state machine fragment passed to a record composer object
       
   119 	CX509Certificate* iServerCert;		// Server certificate. Used to process ServerKeyExchange message
       
   120 	TAlgorithmId iSigAlg;				// Signature algorithm. Used to process the CertificateVerify message.
       
   121 protected:
       
   122 	TBool iCertVerify;					// Boolean value indicating whether the CertificateVerify message should be sent.
       
   123 };
       
   124 
       
   125 
       
   126 // Inline methods
       
   127 
       
   128 inline CHandshake::CHandshake(CTlsConnection& aTlsConnection) :
       
   129    iTlsConnection(aTlsConnection),
       
   130    iTransmitList( CTlsEvent::TxOffset() ),
       
   131    iTxListIter( iTransmitList ),
       
   132    iComposeMsg(NULL, 0),
       
   133    iCertVerify(EFalse)
       
   134 /**
       
   135  * Constructor. 
       
   136  * This method does the following:
       
   137  * 1 - Initialise a reference to the secure connection object.
       
   138  * 2 - Initialise iComposeMsg, the pointer descriptor used to access the data 
       
   139  *	   in the state machine's heap descriptor object, to NULL
       
   140  * 3 - Set the history of the negotiation, iHistory to the start. This object 
       
   141  *     keeps track of the negotiation and which messages have been sent or 
       
   142 	   received thus far.
       
   143  */
       
   144 {
       
   145 	LOG(Log::Printf(_L("CHandshake::CHandshake()\n"));)
       
   146 	iHistory = ETlsHandshakeStart; 
       
   147 }
       
   148 
       
   149 inline CTlsEvent* CHandshake::NextTxEvent()
       
   150 {
       
   151    return iTxListIter++;
       
   152 }
       
   153 
       
   154 inline TPtr8& CHandshake::ComposeMsg()
       
   155 /** 
       
   156  * This method returns a pointer descriptor (to the state machine's data fragment)
       
   157  */
       
   158 {
       
   159 	LOG(Log::Printf(_L("CHandshake::ComposeMsg()\n"));)
       
   160     return iComposeMsg;
       
   161 }
       
   162 
       
   163 inline CX509Certificate*& CHandshake::ServerCert()
       
   164 /** 
       
   165  * This method returns a reference to the Server's certificate
       
   166  */
       
   167 {
       
   168 	LOG(Log::Printf(_L("CHandshake::ServerCert()\n"));)
       
   169     return iServerCert;
       
   170 }
       
   171 
       
   172 inline TAlgorithmId& CHandshake::SignatureAlg()
       
   173 /** 
       
   174  * This method returns a reference to the Key exchange algorithm.
       
   175  */
       
   176 {
       
   177 	LOG(Log::Printf(_L("CHandshake::SignatureAlg()\n"));)
       
   178     return iSigAlg;
       
   179 }
       
   180 
       
   181 inline void CHandshake::SetCertificateVerifyReqd( TBool aCertVerify )
       
   182 {
       
   183    iCertVerify = aCertVerify;
       
   184 }
       
   185 
       
   186 inline TBool CHandshake::CertificateVerifyReqd() const
       
   187 /** 
       
   188  * This method returns a reference to a boolean which indicates whether the Certificate
       
   189  * Verify message should be sent.
       
   190  */
       
   191 {
       
   192 	LOG(Log::Printf(_L("CHandshake::CertificateVerifyReqd()\n"));)
       
   193     return iCertVerify;
       
   194 }
       
   195 
       
   196 inline CSendAlert* CHandshake::SendAlert() const
       
   197 /** 
       
   198  * This method returns a pointer to a 'Send Alert' object. It is used when the 
       
   199  * handshake state machine has to send an alert.
       
   200  */
       
   201 {
       
   202 	LOG(Log::Printf(_L("CHandshake::SendAlert()\n"));)
       
   203 	return iSendAlert;
       
   204 }
       
   205 
       
   206 inline CSHA1* CHandshake::SHA1Verify() const
       
   207 /** 
       
   208  * This method returns a pointer to a SHA1 object. This is used to calculate a 
       
   209  * SHA hash of messages.
       
   210  */
       
   211 {
       
   212 	LOG(Log::Printf(_L("CHandshake::SHA1Verify()\n"));)
       
   213 	return iSHA1Verify;
       
   214 }
       
   215 
       
   216 inline CMD5* CHandshake::MD5Verify() const
       
   217 /** 
       
   218  * This method returns a pointer to a MD5 object. This is used to calculate a 
       
   219  * MD5 hash of messages.
       
   220  */
       
   221 {
       
   222 	LOG(Log::Printf(_L("CHandshake::MD5Verify()\n"));)
       
   223 	return iMD5Verify;
       
   224 }
       
   225 
       
   226 inline TSglQue<CTlsEvent>& CHandshake::TxMessageList()
       
   227 /**
       
   228  * This method returns the list header (of the messages to be transmitted).
       
   229  */
       
   230 {
       
   231 	LOG(Log::Printf(_L("CHandshake::TxMessageList()\n"));)
       
   232 	return iTransmitList;
       
   233 }
       
   234 
       
   235 #endif