email/imap4mtm/imaptransporthandler/src/csecuresocketcontroller.cpp
changeset 0 72b543305e3a
equal deleted inserted replaced
-1:000000000000 0:72b543305e3a
       
     1 // Copyright (c) 2006-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 //
       
    15 
       
    16 #include "csecuresocketcontroller.h"
       
    17 #include "mcommsinfoprovider.h"
       
    18 #include "imappaniccodes.h"
       
    19 #include <ssl_internal.h>
       
    20 
       
    21 /**
       
    22 The factory constructor.
       
    23 
       
    24 @param	aSocket	Reference to the socket.
       
    25 @param	aCommsInfoProvider	Reference to the comms info provider.
       
    26 */
       
    27 CSecureSocketController* CSecureSocketController::NewL(RSocket& aSocket, MCommsInfoProvider& aCommsInfoProvider)
       
    28 	{
       
    29 	CSecureSocketController* self = new (ELeave) CSecureSocketController(aSocket, aCommsInfoProvider);
       
    30 	return self;
       
    31 	}
       
    32 
       
    33 /**
       
    34 Destructor.
       
    35 */
       
    36 CSecureSocketController::~CSecureSocketController()
       
    37 	{
       
    38 	if( iTlsSocket )
       
    39 		{
       
    40 		iTlsSocket->Close();
       
    41 		delete iTlsSocket;
       
    42 		}
       
    43 	else
       
    44 		{
       
    45 		iSocket.Close(); // Has ownership of socket if secure layer was not created
       
    46 		}
       
    47 	}
       
    48 
       
    49 /**
       
    50 Constructor.
       
    51 
       
    52 @param	aSocket	Reference to the socket.
       
    53 @param	aCommsInfoProvider	Reference to the comms info provider.	
       
    54 */
       
    55 CSecureSocketController::CSecureSocketController(RSocket& aSocket, MCommsInfoProvider& aCommsInfoProvider)
       
    56 : iSocket(aSocket), iCommsInfoProvider(aCommsInfoProvider)
       
    57 	{
       
    58 	}
       
    59 
       
    60 /**
       
    61 Used to start a tls handshake with the host.
       
    62 
       
    63 @param	aStatus	The request status.
       
    64 @param	aSSLDomainName SSL domain name 
       
    65 */
       
    66 void CSecureSocketController::StartSecureHandshakeL(TRequestStatus& aStatus, const TDesC8& aSSLDomainName)
       
    67 	{
       
    68 	// Create the secure layer
       
    69 	_LIT(KTxtTls, "tls1.0");
       
    70 	if( !iTlsSocket )
       
    71 		{
       
    72 		iTlsSocket = CSecureSocket::NewL(iSocket, KTxtTls());
       
    73 		}
       
    74 
       
    75 	// Get the security preferences, dialog prompt and security policy
       
    76 	TBool dialogPref;
       
    77 	iCommsInfoProvider.SecurityPreferences(dialogPref);
       
    78 
       
    79 	// Dialog preferences
       
    80 	if( !dialogPref )
       
    81 		{
       
    82 		User::LeaveIfError(iTlsSocket->SetDialogMode(EDialogModeUnattended));
       
    83 		}
       
    84 
       
    85 	// Set an option on the socket to check the server certificate domain
       
    86 	User::LeaveIfError(iTlsSocket->SetOpt(KSoSSLDomainName, KSolInetSSL, aSSLDomainName));
       
    87 	
       
    88 	iTlsSocket->StartClientHandshake(aStatus);
       
    89 	}
       
    90 
       
    91 /**
       
    92 Receives data from a remote host and completes when data is available.
       
    93 @param	aBuffer	A descriptor where data read will be placed.
       
    94 @param	aStatus	On completion, KErrNone if successful, otherwise one of 
       
    95 				the system wide error codes. Note that KErrEof indicates 
       
    96 				either that a remote connection is closed, and that no more 
       
    97 				data is available for reading, or the socket has been 
       
    98 				shutdown with option RSocket::EStopInput. 
       
    99 @param	aLength	For non-datagram sockets, on return, a length 
       
   100 				which indicates how much data was read. This 
       
   101 				is the same as length of the returned aDesc. 
       
   102 				For datagram sockets, this parameter is not used.
       
   103 */
       
   104 void CSecureSocketController::RecvOneOrMore(TDes8& aBuffer, TRequestStatus& aStatus, TSockXfrLength& aLength)
       
   105 	{
       
   106 	__ASSERT_DEBUG( iTlsSocket!=NULL, TImapServerPanic::ImapPanic(TImapServerPanic::ETlsSocketNotStarted) );
       
   107 
       
   108 	iTlsSocket->RecvOneOrMore(aBuffer, aStatus, aLength);
       
   109 	}
       
   110 
       
   111 /**
       
   112 Cancels an outstanding Recv() operation.
       
   113 */
       
   114 void CSecureSocketController::CancelRecv()
       
   115 	{
       
   116 	if( iTlsSocket )
       
   117 		{
       
   118 		iTlsSocket->CancelRecv();
       
   119 		}
       
   120 	}
       
   121 
       
   122 /**
       
   123 Sends data to a remote host on a connected socket with options set by protocol specific flags.
       
   124 The length of the descriptor indicates the amount of data to be sent. The TSockXfrLength argument 
       
   125 will return the amount of data actually sent.
       
   126 A socket may only have one send operation in progress at any one time.
       
   127 Send() should only be used with connected sockets.
       
   128 If a protocol's information flag is marked with KSIUrgentData, then KSockWriteUrgent may be provided 
       
   129 as a flag to Send(). All other flags are protocol specific.
       
   130 
       
   131 @param	aBuffer	Reference to the data to send.
       
   132 @param	aStatus	The request status.
       
   133 */
       
   134 void CSecureSocketController::Send(const TDesC8& aBuffer, TRequestStatus& aStatus)
       
   135 	{
       
   136 	__ASSERT_DEBUG( iTlsSocket!=NULL, TImapServerPanic::ImapPanic(TImapServerPanic::ETlsSocketNotStarted) );
       
   137 	
       
   138 	iTlsSocket->Send(aBuffer, aStatus);
       
   139 	}
       
   140 
       
   141 /**
       
   142 Cancels an outstanding Send() operation. Calling the function will cause any outstanding 
       
   143 send operation to complete prematurely. The state of a socket after a send is cancelled 
       
   144 is defined by the characteristics of the protocol.
       
   145 */
       
   146 void CSecureSocketController::CancelSend()
       
   147 	{
       
   148 	if( iTlsSocket )
       
   149 		{
       
   150 		iTlsSocket->CancelSend();
       
   151 		}
       
   152 	}
       
   153 
       
   154 /**
       
   155 Cancels the handshake.
       
   156 */
       
   157 void CSecureSocketController::CancelHandshake()
       
   158 	{
       
   159 	if( iTlsSocket )
       
   160 		{
       
   161 		iTlsSocket->CancelHandshake();
       
   162 		}
       
   163 	}