pkiutilities/untrustedcertificatedialog/tsrc/tlsconntest/tlsconnection.cpp
branchRCL_3
changeset 49 09b1ac925e3f
equal deleted inserted replaced
47:63339781d179 49:09b1ac925e3f
       
     1 /*
       
     2 * Copyright (c) 2010 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:  Secure connections test application
       
    15 *
       
    16 */
       
    17 
       
    18 #include "tlsconnection.h"
       
    19 #include <e32debug.h>
       
    20 #include <SecureSocket.h>
       
    21 #include <ssl_internal.h>       // KSolInetSSL, KSoSSLDomainName
       
    22 
       
    23 _LIT( KData, "GET index.html" );
       
    24 _LIT( KSecureProtocol, "TLS1.0" );
       
    25 _LIT( KCommDBIapId, "IAP\\Id" );
       
    26 _LIT8( KNewLine, "\n" );
       
    27 
       
    28 const TInt KRecvBufferLength = 0x4000;
       
    29 const TInt KOneSecondInMicroseconds = 1000000;
       
    30 
       
    31 
       
    32 // ======== MEMBER FUNCTIONS ========
       
    33 
       
    34 // ---------------------------------------------------------
       
    35 // CTlsConnection::NewL
       
    36 // ---------------------------------------------------------
       
    37 //
       
    38 CTlsConnection* CTlsConnection::NewL( MTlsConnectionObserver& aObs )
       
    39     {
       
    40     RDebug::Print( _L("CTlsConnection::NewL()") );
       
    41     CTlsConnection* self = new ( ELeave ) CTlsConnection( aObs );
       
    42     CleanupStack::PushL( self );
       
    43     self->ConstructL();
       
    44     CleanupStack::Pop( self );
       
    45     return self;
       
    46     }
       
    47 
       
    48 // -----------------------------------------------------------------------------
       
    49 // CTlsConnection::~CTlsConnection()
       
    50 // -----------------------------------------------------------------------------
       
    51 //
       
    52 CTlsConnection::~CTlsConnection()
       
    53     {
       
    54     RDebug::Print( _L("CTlsConnection::~CTlsConnection()") );
       
    55     Cancel();
       
    56     iDelayTimer.Close();
       
    57     CloseSession();
       
    58     delete iSendBuffer;
       
    59     delete iSendBufPtr;
       
    60     }
       
    61 
       
    62 // -----------------------------------------------------------------------------
       
    63 // CTlsConnection::ConnectL()
       
    64 // -----------------------------------------------------------------------------
       
    65 //
       
    66 void CTlsConnection::ConnectL( const TDesC& aHostName, TInt aPort, TInt aDelay )
       
    67     {
       
    68     RDebug::Print( _L("CTlsConnection::Connect() host '%S', port %d, delay %d"),
       
    69         &aHostName, aPort, aDelay );
       
    70     CloseConnection();
       
    71 
       
    72     // Parameters
       
    73     if( iHostName )
       
    74         {
       
    75         delete iHostName;
       
    76         iHostName = NULL;
       
    77         }
       
    78     iHostName = aHostName.AllocL();
       
    79     iPort = aPort;
       
    80     iDelay = aDelay;
       
    81 
       
    82     // Save host name in 8-bit form for server certificate checking
       
    83     if( iHostNameForCertCheck )
       
    84         {
       
    85         delete iHostNameForCertCheck;
       
    86         iHostNameForCertCheck = NULL;
       
    87         }
       
    88     iHostNameForCertCheck = HBufC8::NewL( aHostName.Length() );
       
    89     TPtr8 ptr( iHostNameForCertCheck->Des() );
       
    90     ptr.Copy( aHostName );
       
    91 
       
    92     // Convert send data to 8-bit and add newline at the end
       
    93     if( iSendBuffer || iSendBufPtr )
       
    94         {
       
    95         delete iSendBuffer;
       
    96         iSendBuffer = NULL;
       
    97         delete iSendBufPtr;
       
    98         iSendBufPtr = NULL;
       
    99         }
       
   100     iSendBuffer = HBufC8::NewL( KData().Length() + 1 );
       
   101     iSendBufPtr = new( ELeave ) TPtr8( iSendBuffer->Des() );
       
   102     iSendBufPtr->Copy( KData );
       
   103     iSendBufPtr->Append( KNewLine );
       
   104 
       
   105     // Connect
       
   106     RDebug::Printf( "CTlsConnection::ConnectL iConnection.Open" );
       
   107     User::LeaveIfError( iConnection.Open( iSockServer ) );
       
   108     RDebug::Printf( "CTlsConnection::ConnectL iConnPref.SetDirection" );
       
   109     iConnPref.SetDirection( ECommDbConnectionDirectionOutgoing );
       
   110     if( iCurrentIap )
       
   111         {
       
   112         // uses the same IAP as before
       
   113         RDebug::Printf( "CTlsConnection::ConnectL SetIapId(%d)", iCurrentIap );
       
   114         iConnPref.SetIapId( iCurrentIap );
       
   115         iConnPref.SetDialogPreference( ECommDbDialogPrefDoNotPrompt );
       
   116         }
       
   117     else
       
   118         {
       
   119         // not defined, prompts the IAP from user
       
   120         RDebug::Printf( "CTlsConnection::ConnectL ECommDbDialogPrefPrompt" );
       
   121         iConnPref.SetDialogPreference( ECommDbDialogPrefPrompt );
       
   122         }
       
   123     StateChange( EConnectingNetwork );
       
   124     RDebug::Printf( "CTlsConnection::ConnectL iConnection.Start" );
       
   125     iConnection.Start( iConnPref, iStatus );
       
   126     SetActive();
       
   127     RDebug::Printf( "CTlsConnection::ConnectL end" );
       
   128     }
       
   129 
       
   130 // -----------------------------------------------------------------------------
       
   131 // CTlsConnection::Disconnect()
       
   132 // -----------------------------------------------------------------------------
       
   133 //
       
   134 void CTlsConnection::Disconnect()
       
   135     {
       
   136     Cancel();
       
   137     CloseConnection();
       
   138     StateChange( EDisconnected );
       
   139     }
       
   140 
       
   141 // -----------------------------------------------------------------------------
       
   142 // CTlsConnection::RunL
       
   143 // -----------------------------------------------------------------------------
       
   144 //
       
   145 void CTlsConnection::RunL()
       
   146     {
       
   147     if( iState == EReading && ( iStatus.Int() == KErrEof || iStatus.Int() == KErrSSLAlertCloseNotify ) )
       
   148         {
       
   149         RDebug::Printf( "CTlsConnection::RunL: EReading, EOF: iStatus = %d", iStatus.Int() );
       
   150         StateChange( EAllDone );
       
   151         }
       
   152     else if( iStatus.Int() == KErrNone )
       
   153         {
       
   154         switch ( iState )
       
   155             {
       
   156             case EConnectingNetwork:
       
   157                 RDebug::Printf( "CTlsConnection::RunL: EConnectingNetwork" );
       
   158                 if( !iCurrentIap )
       
   159                     {
       
   160                     iConnection.GetIntSetting( KCommDBIapId, iCurrentIap );
       
   161                     }
       
   162                 if( iDelay )
       
   163                     {
       
   164                     TTimeIntervalMicroSeconds32 delay( iDelay * KOneSecondInMicroseconds );
       
   165                     iDelayTimer.After( iStatus, delay );
       
   166                     StateChange( EOpeningDelay );
       
   167                     }
       
   168                 else
       
   169                     {
       
   170                     iConnection.ProgressNotification( iProgress, iStatus );
       
   171                     StateChange( EResolvingHostName );
       
   172                     }
       
   173                 SetActive();
       
   174                 break;
       
   175 
       
   176             case EOpeningDelay:
       
   177                 iConnection.ProgressNotification( iProgress, iStatus );
       
   178                 StateChange( EResolvingHostName );
       
   179                 SetActive();
       
   180                 break;
       
   181 
       
   182             case EResolvingHostName:
       
   183                 {
       
   184                 RDebug::Printf( "CTlsConnection::RunL: EResolvingHostName" );
       
   185                 TInt err = iHostResolver.Open( iSockServer, KAfInet, KProtocolInetUdp, iConnection );
       
   186                 User::LeaveIfError( err );
       
   187                 iHostResolver.GetByName( *iHostName, iNameEntry, iStatus );
       
   188                 StateChange( EConnectingServer );
       
   189                 SetActive();
       
   190                 }
       
   191                 break;
       
   192 
       
   193             case EConnectingServer:
       
   194                 {
       
   195                 iRemoteAddr.SetFamily( KAfInet );
       
   196                 iRemoteAddr = TInetAddr( iNameEntry().iAddr );
       
   197                 iRemoteAddr.SetPort( iPort );
       
   198 
       
   199                 RDebug::Printf( "CTlsConnection::RunL: EConnectingServer" );
       
   200                 TInt err = iSocket.Open( iSockServer, KAfInet, KSockStream, KProtocolInetTcp, iConnection );
       
   201                 RDebug::Printf( "CTlsConnection::RunL: iSocket.Open ret %d", err );
       
   202                 User::LeaveIfError( err );
       
   203                 iSocket.Connect( iRemoteAddr, iStatus );
       
   204                 StateChange( EHandshaking );
       
   205                 SetActive();
       
   206                 }
       
   207                 break;
       
   208 
       
   209             case EHandshaking:
       
   210                 {
       
   211                 RDebug::Printf( "CTlsConnection::RunL: EHandshaking" );
       
   212                 if( iSecureSocket )
       
   213                     {
       
   214                     delete iSecureSocket;
       
   215                     iSecureSocket = NULL;
       
   216                     }
       
   217                 iSecureSocket = CSecureSocket::NewL( iSocket, KSecureProtocol );
       
   218                 iSecureSocket->SetDialogMode( EDialogModeAttended );
       
   219                 // Server name must be set, otherwise CCTSecurityDialogsAO fails with KErrArgument
       
   220                 iSecureSocket->SetOpt( KSoSSLDomainName, KSolInetSSL, *iHostNameForCertCheck );
       
   221                 iSecureSocket->StartClientHandshake( iStatus );
       
   222                 StateChange( EConnecting );
       
   223                 SetActive();
       
   224                 }
       
   225                 break;
       
   226 
       
   227             case EConnecting:
       
   228                 {
       
   229                 RDebug::Printf( "CTlsConnection::RunL: EConnecting" );
       
   230                 // Check if connection is closed
       
   231                 const TInt stage( iProgress().iStage );
       
   232                 if( stage == KConnectionClosed || stage == KLinkLayerClosed )
       
   233                     {
       
   234                     StateChange( EDisconnected );
       
   235                     }
       
   236                 else
       
   237                     {
       
   238                     iConnection.ProgressNotification( iProgress, iStatus );
       
   239                     StateChange( ESending );
       
   240                     SetActive();
       
   241                     }
       
   242                 }
       
   243                 break;
       
   244 
       
   245             case ESending:
       
   246                 RDebug::Printf( "CTlsConnection::RunL: ESending" );
       
   247                 iSecureSocket->Send( *iSendBufPtr, iStatus, iSendLength );
       
   248                 StateChange( EReading );
       
   249                 SetActive();
       
   250                 break;
       
   251 
       
   252             case EReading:
       
   253                 iTotalRecvCount += iRecvLength();
       
   254                 RDebug::Printf( "CTlsConnection::RunL: EReading, read %d bytes", iTotalRecvCount );
       
   255                 iObserver.HandleTransferData( iRecvBuffer->Des(), iTotalRecvCount );
       
   256 
       
   257                 iRecvBufPtr->Zero();
       
   258                 iSecureSocket->RecvOneOrMore( *iRecvBufPtr, iStatus, iRecvLength );
       
   259                 SetActive();
       
   260                 break;
       
   261 
       
   262             default:
       
   263                 break;
       
   264             }
       
   265         }
       
   266     else
       
   267         {
       
   268         RDebug::Printf( "CTlsConnection::RunL: ERROR iStatus %d", iStatus.Int() );
       
   269         CloseConnection();
       
   270         StateChange( EDisconnected, iStatus.Int() );
       
   271         }
       
   272     }
       
   273 
       
   274 // ---------------------------------------------------------------------------
       
   275 // CTlsConnection::DoCancel()
       
   276 // ---------------------------------------------------------------------------
       
   277 //
       
   278 void CTlsConnection::DoCancel()
       
   279     {
       
   280     RDebug::Printf( "CTlsConnection::DoCancel(), iState=%d", iState );
       
   281 
       
   282     switch( iState )
       
   283         {
       
   284         case EConnectingNetwork:
       
   285             iConnection.Stop();
       
   286             break;
       
   287 
       
   288         case EOpeningDelay:
       
   289             iDelayTimer.Cancel();
       
   290             iConnection.Stop();
       
   291             break;
       
   292 
       
   293         case EResolvingHostName:
       
   294             iConnection.CancelProgressNotification();
       
   295             iConnection.Stop();
       
   296             break;
       
   297 
       
   298         case EConnectingServer:
       
   299             iHostResolver.Cancel();
       
   300             iConnection.Stop();
       
   301             break;
       
   302 
       
   303         case EHandshaking:
       
   304             iSocket.CancelConnect();
       
   305             iConnection.Stop();
       
   306             break;
       
   307 
       
   308         case EConnecting:
       
   309             if( iSecureSocket )
       
   310                 {
       
   311                 iSecureSocket->CancelHandshake();
       
   312                 delete iSecureSocket;
       
   313                 iSecureSocket = NULL;
       
   314                 }
       
   315             iSocket.Close();
       
   316             iConnection.Stop();
       
   317             break;
       
   318 
       
   319         case ESending:
       
   320             if( iSecureSocket )
       
   321                 {
       
   322                 iSecureSocket->CancelSend();
       
   323                 delete iSecureSocket;
       
   324                 iSecureSocket = NULL;
       
   325                 }
       
   326             iSocket.Close();
       
   327             iConnection.Stop();
       
   328             break;
       
   329 
       
   330         case EReading:
       
   331             if( iSecureSocket )
       
   332                 {
       
   333                 iSecureSocket->CancelRecv();
       
   334                 delete iSecureSocket;
       
   335                 iSecureSocket = NULL;
       
   336                 }
       
   337             iSocket.Close();
       
   338             iConnection.Stop();
       
   339             break;
       
   340 
       
   341         case EAllDone:
       
   342         case EIdle:
       
   343         case EDisconnected:
       
   344         default:
       
   345             break;
       
   346         }
       
   347 
       
   348     StateChange( EDisconnected );
       
   349     }
       
   350 
       
   351 
       
   352 // -----------------------------------------------------------------------------
       
   353 // CTlsConnection::RunError()
       
   354 // -----------------------------------------------------------------------------
       
   355 //
       
   356 TInt CTlsConnection::RunError( TInt aError )
       
   357     {
       
   358     RDebug::Printf( "CTlsConnection::RunError, aError: %d", aError );
       
   359     CloseConnection();
       
   360     StateChange( EDisconnected, aError );
       
   361     return KErrNone;
       
   362     }
       
   363 
       
   364 // -----------------------------------------------------------------------------
       
   365 // CTlsConnection::CTlsConnection()
       
   366 // -----------------------------------------------------------------------------
       
   367 //
       
   368 CTlsConnection::CTlsConnection( MTlsConnectionObserver& aObs ) :
       
   369         CActive( CActive::EPriorityStandard ), iObserver( aObs ), iState( ENotInitialized )
       
   370     {
       
   371     CActiveScheduler::Add( this );
       
   372     }
       
   373 
       
   374 // -----------------------------------------------------------------------------
       
   375 // CTlsConnection::ConstructL()
       
   376 // -----------------------------------------------------------------------------
       
   377 //
       
   378 void CTlsConnection::ConstructL()
       
   379     {
       
   380     RDebug::Printf( "CTlsConnection::ConstructL begin" );
       
   381 
       
   382     User::LeaveIfError( iDelayTimer.CreateLocal() );
       
   383     RDebug::Printf( "CTlsConnection::ConstructL iSockServer" );
       
   384     User::LeaveIfError( iSockServer.Connect() );
       
   385     RDebug::Printf( "CTlsConnection::ConstructL iConnection" );
       
   386     User::LeaveIfError( iConnection.Open( iSockServer ) );
       
   387     RDebug::Printf( "CTlsConnection::ConstructL buffers" );
       
   388     iRecvBuffer = HBufC8::NewL( KRecvBufferLength );
       
   389     iRecvBufPtr = new( ELeave ) TPtr8( iRecvBuffer->Des() );
       
   390     StateChange( EIdle );
       
   391 
       
   392     RDebug::Printf( "CTlsConnection::ConstructL end" );
       
   393     }
       
   394 
       
   395 // ---------------------------------------------------------------------------
       
   396 // CTlsConnection::CloseConnection()
       
   397 // ---------------------------------------------------------------------------
       
   398 //
       
   399 void CTlsConnection::CloseConnection()
       
   400     {
       
   401     RDebug::Printf( "CTlsConnection::CloseConnection begin" );
       
   402     if( iSecureSocket )
       
   403         {
       
   404         delete iSecureSocket;
       
   405         iSecureSocket = NULL;
       
   406         }
       
   407     iSocket.Close();
       
   408     iHostResolver.Close();
       
   409     if( iConnection.SubSessionHandle() )
       
   410         {
       
   411         if( iState >= EConnectingNetwork )
       
   412             {
       
   413             TInt err = iConnection.Stop();
       
   414             if( err != KErrNone )
       
   415                 {
       
   416                 RDebug::Printf( "iConnection.Stop() failed: %d", err );
       
   417                 }
       
   418             }
       
   419         iConnection.Close();
       
   420         }
       
   421     RDebug::Printf( "CTlsConnection::CloseConnection end" );
       
   422     }
       
   423 
       
   424 // ---------------------------------------------------------------------------
       
   425 // CTlsConnection::CloseSession()
       
   426 // ---------------------------------------------------------------------------
       
   427 //
       
   428 void CTlsConnection::CloseSession()
       
   429     {
       
   430     RDebug::Printf( "CTlsConnection::CloseSession" );
       
   431     CloseConnection();
       
   432     iSockServer.Close();
       
   433     delete iHostName;
       
   434     iHostName = NULL;
       
   435     delete iHostNameForCertCheck;
       
   436     iHostNameForCertCheck = NULL;
       
   437     delete iRecvBuffer;
       
   438     iRecvBuffer = NULL;
       
   439     delete iRecvBufPtr;
       
   440     iRecvBufPtr = NULL;
       
   441     StateChange( EDisconnected );
       
   442     }
       
   443 
       
   444 // -----------------------------------------------------------------------------
       
   445 // CTlsConnection::StateChange()
       
   446 // -----------------------------------------------------------------------------
       
   447 //
       
   448 void CTlsConnection::StateChange( TTlsConnectionState aNewState, TInt aError )
       
   449     {
       
   450     RDebug::Printf( "CTlsConnection::StateChange, aNewState=%d, aError=%d", aNewState, aError );
       
   451     if( aNewState != iState && iState != EAllDone )
       
   452         {
       
   453         iState = aNewState;
       
   454         iObserver.HandleNetworkEvent( aNewState, aError );
       
   455         }
       
   456     }
       
   457 
       
   458 // ---------------------------------------------------------------------------
       
   459 // CTlsConnection::ReadL()
       
   460 // ---------------------------------------------------------------------------
       
   461 //
       
   462 void CTlsConnection::ReadL()
       
   463     {
       
   464     RDebug::Printf( "CTlsConnection::ReadL(), iState=%d", iState );
       
   465 
       
   466     TPtr8 recvBuffer = iRecvBuffer->Des();
       
   467     recvBuffer.Zero();
       
   468     iSecureSocket->RecvOneOrMore( recvBuffer, iStatus, iRecvLength );
       
   469     SetActive();
       
   470     }
       
   471