networksecurity/tls/protocol/applicationdata.cpp
changeset 0 af10295192d8
child 67 bb2423252ea3
equal deleted inserted replaced
-1:000000000000 0:af10295192d8
       
     1 // Copyright (c) 2003-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // SSL3.0 and TLS1.0 Application Data source file.
       
    15 // Describes the implementation of the Application data (transmission
       
    16 // and reception) state machines.
       
    17 // 
       
    18 //
       
    19 
       
    20 /**
       
    21  @file
       
    22 */
       
    23 
       
    24 #include "applicationdata.h"
       
    25 #include "tlsconnection.h"
       
    26 #include "recordprotocolevents.h"
       
    27 #include "AlertProtocolEvents.h"
       
    28 #include "hellorequest.h"
       
    29 
       
    30 
       
    31 CSendAppData* CSendAppData::NewL( CRecordComposer& aRecordComposer )
       
    32 {
       
    33 	LOG(Log::Printf(_L("CSendAppData::NewL()"));)
       
    34 	CSendAppData* self = new(ELeave) CSendAppData( aRecordComposer );
       
    35   	LOG(Log::Printf(_L("self %x - %x"), self, (TUint)self + sizeof( CSendAppData ));)
       
    36 	CleanupStack::PushL( self );
       
    37 	self->ConstructL( aRecordComposer );
       
    38 	CleanupStack::Pop();
       
    39 	return self;
       
    40 }
       
    41 
       
    42 inline CSendAppData::CSendAppData( CRecordComposer& aRecordComposer ) :
       
    43    iRecordComposer( aRecordComposer  )
       
    44 {
       
    45 	LOG(Log::Printf(_L("CSendAppData::CSendAppData()\n"));)
       
    46 	iHistory = KTlsApplicationData; // This shows explicitly where we are
       
    47 }
       
    48 
       
    49 CSendAppData::~CSendAppData()
       
    50 {
       
    51 	LOG(Log::Printf(_L("CSendAppData::~CSendAppData()\n"));)
       
    52    SetSockXfrLength( NULL );
       
    53    Cancel( KErrNone );
       
    54 	delete iSendAlert;
       
    55 }
       
    56 
       
    57 inline void CSendAppData::ConstructL( CRecordComposer& aRecordComposer )
       
    58 {
       
    59 	iSendAlert = new(ELeave)CSendAlert( *this, aRecordComposer );
       
    60 	LOG(Log::Printf(_L("CSendAppData::ConstructL() %x\n"), SendAlert());)
       
    61 	ResumeL();
       
    62 }
       
    63 
       
    64 void CSendAppData::Suspend()
       
    65 {
       
    66 	LOG(Log::Printf(_L("CSendAppData::Suspend()\n"));)
       
    67    
       
    68 	iCurrentPos = iRecordComposer.CurrentPos();
       
    69 	iAppData = iRecordComposer.UserData();
       
    70 	iRecordComposer.SetUserData( NULL );
       
    71 }
       
    72 
       
    73 void CSendAppData::ResumeL()
       
    74 {
       
    75 	LOG(Log::Printf(_L("CSendAppData::ResumeL()\n"));)
       
    76    
       
    77 	iRecordComposer.SetUserData( iAppData );
       
    78 	iRecordComposer.ReConstructL( this, iCurrentPos ); //it sets record type to ETlsAppDataContentType
       
    79 	iCurrentPos = 0;
       
    80 	
       
    81 	if ( !iActiveEvent )
       
    82 	{	//active event is the record composer
       
    83 		iActiveEvent = &iRecordComposer;
       
    84 	}
       
    85 }
       
    86 
       
    87 void CSendAppData::SetSockXfrLength( TInt* aLen )
       
    88 {
       
    89 	LOG(Log::Printf(_L("CSendAppData::SetSockXfrLength()\n"));)
       
    90 
       
    91 	iSockXfrLength = aLen;
       
    92 	if ( iSockXfrLength )
       
    93 	{
       
    94 		*iSockXfrLength = 0;
       
    95 	}
       
    96 }
       
    97 
       
    98 void CSendAppData::OnCompletion()
       
    99 {
       
   100 	LOG(Log::Printf(_L("CSendAppData::OnCompletion()\n"));)
       
   101    
       
   102 	TDesC8* pAppData = iRecordComposer.UserData();
       
   103 	if ( pAppData ) //could have finished via an error
       
   104 	{
       
   105 		if ( iSockXfrLength )
       
   106 		{
       
   107 			*iSockXfrLength = iRecordComposer.CurrentPos();
       
   108 		}
       
   109 		if ( iLastError == KErrNone && iStatus.Int() == KErrNone )
       
   110 		{
       
   111 			//hello request received?
       
   112 			if ( iStateMachineNotify == iHelloReq && iHelloReq )
       
   113 			{//no user request completion, signal to hello request
       
   114 				iHelloReq->OnCompletion( this );
       
   115 				return;
       
   116 			}
       
   117 			// Anything more to send?
       
   118 			else if ( pAppData->Length() > iRecordComposer.CurrentPos() )
       
   119 			{	// No error (cancel) && still some app data in the app buffer =>
       
   120 				// => write another fragment
       
   121 				iActiveEvent = &iRecordComposer;
       
   122 				Start( iClientStatus, iStateMachineNotify );
       
   123 				return;
       
   124 			}
       
   125 		}
       
   126 	}
       
   127    
       
   128 	iRecordComposer.SetUserData( NULL ); //we'r finished
       
   129 	iRecordComposer.ResetCurrentPos();
       
   130 
       
   131 	/* Fix for the TLS Client hang issue DEF130128.
       
   132 	 * In the case of where one of the state machines receives the 
       
   133 	 * asynchronous event alert due to TLS server reset, the state 
       
   134 	 * machine triggers the cleanup activities. The TlsConnection 
       
   135 	 * fails to clean up  the resources allocated for handling data 
       
   136 	 * transfer and TLS handshake in such cases if one of the state 
       
   137 	 * machines has issued the asynchronous server request and is 
       
   138 	 * waiting for the request to complete. The change made below 
       
   139 	 * will allow the code complete the pending status in such cases 
       
   140 	 * and allow the cleanup to proceed.
       
   141 	 */
       
   142 	if ( iStatus.Int() == KRequestPending )
       
   143 		{
       
   144 			TRequestStatus* p=&iStatus;
       
   145 			User::RequestComplete( p, iLastError );
       
   146 		}
       
   147 	/* End of fix for the TLS Client hang issue DEF130128. */
       
   148 	
       
   149 	CStateMachine::OnCompletion();
       
   150 }
       
   151 
       
   152 void CSendAppData::DoCancel()
       
   153 {
       
   154 	LOG(Log::Printf(_L("CSendAppData::DoCancel()\n"));)
       
   155 
       
   156 	iLastError = KErrCancel;
       
   157 	iRecordComposer.CancelAll();
       
   158 	CStateMachine::DoCancel();
       
   159 }
       
   160 
       
   161 //
       
   162 //
       
   163 CRecvAppData* CRecvAppData::NewL( CTlsConnection& aTlsConnection )
       
   164 {
       
   165 	LOG(Log::Printf(_L("CRecvAppData::NewL()"));)
       
   166 	CRecvAppData* self = new(ELeave) CRecvAppData( aTlsConnection );
       
   167   	LOG(Log::Printf(_L("self %x - %x"), self, (TUint)self + sizeof( CRecvAppData ));)
       
   168 	CleanupStack::PushL( self );
       
   169 	self->ConstructL( aTlsConnection );
       
   170 	CleanupStack::Pop();
       
   171 	return self;
       
   172 }
       
   173 
       
   174 inline CRecvAppData::CRecvAppData( CTlsConnection& aTlsConnection ) :
       
   175    iHeldData( 0, 0 ),
       
   176    iRecordParser( aTlsConnection.RecordParser() )
       
   177 {
       
   178 	LOG(Log::Printf(_L("CRecvAppData::CRecvAppData()\n"));)
       
   179 	iHistory = KTlsApplicationData; // This shows explicitly where we are
       
   180 }
       
   181 
       
   182 CRecvAppData::~CRecvAppData()
       
   183 {
       
   184 	LOG(Log::Printf(_L("CRecvAppData::~CRecvAppData()\n"));)
       
   185    SetSockXfrLength( NULL );
       
   186    Cancel( KErrNone );
       
   187 	delete iSendAlert;
       
   188 }
       
   189 
       
   190 inline void CRecvAppData::ConstructL( CTlsConnection& aTlsConnection )
       
   191 {
       
   192 	iSendAlert = new(ELeave)CSendAlert( *this, aTlsConnection.RecordComposer() );
       
   193 	LOG(Log::Printf(_L("CRecvAppData::ConstructL() %x\n"), SendAlert());)
       
   194 	ResumeL( aTlsConnection );
       
   195 }
       
   196 
       
   197 void CRecvAppData::Suspend()
       
   198 {
       
   199 	LOG(Log::Printf(_L("CRecvAppData::Suspend()\n"));)
       
   200    
       
   201 	iHeldData = iRecordParser.HeldData();
       
   202 	iAppData = iRecordParser.UserData();
       
   203 	iRecordParser.SetUserData( NULL );
       
   204 	iRecordParser.SetUserMaxLength( 0 );
       
   205 }
       
   206 
       
   207 void CRecvAppData::ResumeL( CTlsConnection& aTlsConnection )
       
   208 {
       
   209 	LOG(Log::Printf(_L("CRecvAppData::ResumeL()"));)
       
   210 	
       
   211 	__ASSERT_DEBUG( (iAppData && iClientStatus) || (!iAppData && !iClientStatus), TlsPanic(ETlsPanicAppDataResumeButNotStarted));
       
   212 	__ASSERT_DEBUG( iSendAlert, TlsPanic(ETlsPanicAlertReceived));
       
   213 	iRecordParser.SetUserData( iAppData );
       
   214 	iRecordParser.SetUserMaxLength( iAppData ? iAppData->MaxLength() : 0 );
       
   215 	iRecordParser.ReConstructL( this, iHeldData, *iSendAlert );
       
   216 
       
   217 	// Destroy the Rx list & add the only handshake event accepted during application data mode.
       
   218 	CHandshakeParser* pParser = iRecordParser.HandshakeParser();
       
   219 	pParser->DestroyRxList();
       
   220 	CHelloRequest* helloReq = new(ELeave)CHelloRequest( aTlsConnection, *this );
       
   221 	//coverity[leave_without_push]
       
   222 	LOG(Log::Printf(_L("helloReq %x - %x"), helloReq, (TUint)helloReq + sizeof( CHelloRequest ));)
       
   223 	pParser->AddToList( *helloReq );
       
   224 
       
   225 	aTlsConnection.SendAppData()->SetHelloRequest( helloReq ); //no ownership passed
       
   226 	if ( !iActiveEvent )
       
   227 	{	//active event is the record parser
       
   228 		iActiveEvent = &iRecordParser;
       
   229 	}
       
   230 }
       
   231 
       
   232 void CRecvAppData::OnCompletion()
       
   233 {
       
   234 	LOG(Log::Printf(_L("CRecvAppData::OnCompletion()\n"));)
       
   235 
       
   236    if ( iLastError == KErrNone && iStatus.Int() == KErrNone )
       
   237    {
       
   238       if ( iRecordParser.TlsRecordType() == ETlsHandshakeContentType )
       
   239       {//it could be just hello request received => suspend() could have already happened
       
   240          return;
       
   241       }
       
   242 	   TDes8* pAppData = iRecordParser.UserData();
       
   243 	   if ( pAppData ) //could have finished via an error
       
   244 	   {
       
   245 		   if ( iSockXfrLength && pAppData->Length() ) //are we to report RecvOneOrMore?
       
   246 		   {	//yes and finish
       
   247 			   *iSockXfrLength = pAppData->Length();
       
   248 		   }
       
   249 		   else if ( pAppData->Length() < pAppData->MaxLength() )
       
   250 		   {	//no error (cancel) && still some free room in the app data buffer =>
       
   251 			   //=> read another fragment
       
   252 			   iActiveEvent = &iRecordParser;
       
   253 			   Start( iClientStatus, iStateMachineNotify );
       
   254 			   return;
       
   255 		   }
       
   256 	   }
       
   257    }
       
   258    
       
   259 	iRecordParser.SetUserData( NULL ); //we'r finished
       
   260 	iRecordParser.SetUserMaxLength( 0 );
       
   261 	
       
   262 	/* Fix for the TLS Client hang issue DEF130128.
       
   263 	 * In the case of where one of the state machines receives the 
       
   264 	 * asynchronous event alert due to TLS server reset, the state 
       
   265 	 * machine triggers the cleanup activities. The TlsConnection 
       
   266 	 * fails to clean up  the resources allocated for handling data 
       
   267 	 * transfer and TLS handshake in such cases if one of the state 
       
   268 	 * machines has issued the asynchronous server request and is 
       
   269 	 * waiting for the request to complete. The change made below 
       
   270 	 * will allow the code complete the pending status in such cases 
       
   271 	 * and allow the cleanup to proceed.
       
   272 	 */
       
   273 	if ( iStatus.Int() == KRequestPending )
       
   274 		{
       
   275 			TRequestStatus* p=&iStatus;
       
   276 			User::RequestComplete( p, iLastError );
       
   277 		}
       
   278 	/* End of fix for the TLS Client hang issue DEF130128. */
       
   279 	
       
   280 	CStateMachine::OnCompletion();
       
   281 }
       
   282 
       
   283 void CRecvAppData::DoCancel()
       
   284 {
       
   285 	LOG(Log::Printf(_L("CRecvAppData::DoCancel()\n"));)
       
   286    
       
   287     iLastError = KErrCancel; 
       
   288 	iRecordParser.CancelAll();
       
   289 	CStateMachine::DoCancel();
       
   290 }