email/imap4mtm/imapsession/src/cimapauthhelpers.cpp
changeset 0 72b543305e3a
equal deleted inserted replaced
-1:000000000000 0:72b543305e3a
       
     1 // Copyright (c) 2008-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 // IMAPAUTHHELPERS.CPP
       
    15 // 
       
    16 //
       
    17 
       
    18 #include "cimapauthhelpers.h"
       
    19 #include "cimaputils.h"			// forward declarations for utility fns
       
    20 #include "cimaplogger.h"
       
    21 #include "moutputstream.h"
       
    22 #include  "cimapsessionconsts.h"
       
    23 #include <e32des8.h>
       
    24 
       
    25 
       
    26 // IMAP LOGIN command
       
    27 _LIT8(KCommandAuthenticateCrammd5, "%d AUTHENTICATE CRAM-MD5\r\n");
       
    28 _LIT8(KCommandAuthenticatePlain, "%d AUTHENTICATE PLAIN ");
       
    29 _LIT8(KCommandAuthenticateLogin, "%d AUTHENTICATE LOGIN\r\n");
       
    30 _LIT8(KImapAuthBase64StringUsername,"username*"); // To match a folded 'username:' or 'username'
       
    31 _LIT8(KImapAuthBase64StringPassword,"password*"); // To match a folded 'password:' or 'password'
       
    32 
       
    33 const TInt KMaxLengthOfPlainMessageComponent = 255;
       
    34 const TInt KMd5BlockLength = 64;
       
    35 
       
    36 #define BASE64LEN(x) ((x*4)/3) // Every 3 bytes will be turned into 4 bytes
       
    37 
       
    38 const TInt KPreambleLength = 18; //"AUTH LOGIN\r\nPLAIN "
       
    39 const TInt KMaxLengthOfPlainMessage=	KPreambleLength + 1/*NUL*/ + KMaxLengthOfPlainMessageComponent/*Username*/ + 1/*NUL*/ + KMaxLengthOfPlainMessageComponent/*Password*/ + 2/* /r/n */;
       
    40 const TInt KMaxLengthOfPlainMessageBase64= BASE64LEN(KMaxLengthOfPlainMessage);
       
    41 
       
    42 
       
    43  
       
    44  /**
       
    45 Class constructor
       
    46 
       
    47 @param aImapSettings Settings for IMAP service
       
    48 @param aSelectedFolderData Imap Folder Info
       
    49 @param aLogId logging info
       
    50 */
       
    51  CImapAuthMechanismHelper::CImapAuthMechanismHelper(const CImapSettings& aSettings,CImapFolderInfo* aSelectedFolderData, TInt aLogId) : CImapCommandEx(aSelectedFolderData, aLogId),iSettings(aSettings){}
       
    52 
       
    53 
       
    54 /**
       
    55 Static factory constructor. Part of two phased construction.
       
    56 
       
    57 @param aImapSettings Settings for the Imap service
       
    58 @param aSelectedFolderData Imap Folder Info
       
    59 @param aLogId logging info
       
    60 @return The constructed CImapAuthPlainMechanismHelper object
       
    61 */
       
    62 CImapAuthPlainMechanismHelper* CImapAuthPlainMechanismHelper::NewL(const CImapSettings& aSettings,CImapFolderInfo* aSelectedFolderData, TInt aLogId)  
       
    63 	{
       
    64 	CImapAuthPlainMechanismHelper* self= new (ELeave) CImapAuthPlainMechanismHelper(aSettings,aSelectedFolderData,aLogId);
       
    65 	CleanupStack::PushL(self);
       
    66 	self->ConstructL();
       
    67 	CleanupStack::Pop();	                                
       
    68 	return self;
       
    69 	}
       
    70 
       
    71 
       
    72 /**
       
    73 Class constructor
       
    74 
       
    75 @param aImapSettings Settings for IMAP service
       
    76 @param aSelectedFolderData Imap Folder Info
       
    77 @param aLogId logging info
       
    78 */
       
    79 CImapAuthPlainMechanismHelper::CImapAuthPlainMechanismHelper(const CImapSettings& aSettings,CImapFolderInfo* aSelectedFolderData, TInt aLogId) : CImapAuthMechanismHelper(aSettings,aSelectedFolderData, aLogId){}
       
    80 
       
    81 /**
       
    82 Non-trival constructor. Part of two phased construction.
       
    83 */
       
    84 void CImapAuthPlainMechanismHelper::ConstructL(){}
       
    85 
       
    86 
       
    87 /** 
       
    88 Formats and sends the IMAP AUTHENTICATE PLAIN command.
       
    89 @param aTagId Command sequence id which will be send along with the IMAP command.
       
    90 @param aStream A wrapper for the outbound stream of a connected socket, using which 
       
    91 the command will be send to the server
       
    92 */
       
    93 void CImapAuthPlainMechanismHelper::SendMessageL(TInt aTagId, MOutputStream& aStream)
       
    94 	{
       
    95 	iTagId = aTagId;
       
    96 	iStream = &aStream;
       
    97 	
       
    98 	iNextClientMessage.Zero();
       
    99 	
       
   100 	// send Authenticate PLAIN command
       
   101 	TInt bufLength = KCommandAuthenticatePlain().Length();
       
   102 	bufLength += TagLength(aTagId);
       
   103 	iNextClientMessage.Format(KCommandAuthenticatePlain, aTagId);
       
   104 	
       
   105 	// Build up the un-encoded authorisation string in parts
       
   106  	HBufC8* authString = HBufC8::NewMaxLC(KMaxLengthOfPlainMessage); 
       
   107  	TPtr8 authStringPtr = authString->Des();
       
   108 
       
   109 	TBuf8<1> nul;
       
   110 	nul.SetLength(1);
       
   111 	nul[0] = 0;
       
   112 	
       
   113 	//Add Null to begining of auth string
       
   114 	authStringPtr = nul; 
       
   115 	
       
   116 	//Get LoginName from central repository and append to auth string
       
   117 	TPtrC8 loginName = iSettings.LoginName();
       
   118 	TInt length= Min(loginName.Length(),KMaxLengthOfPlainMessageComponent);
       
   119 	authStringPtr.Append(loginName.Left(length));
       
   120 
       
   121 	authStringPtr.Append(nul); // Separator between login and password
       
   122 	
       
   123 	//Get Password from central repository and append to auth string
       
   124 	length= Min(iSettings.Password().Length(), KMaxLengthOfPlainMessageComponent);
       
   125 	authStringPtr.Append(iSettings.Password().Left(length));
       
   126  
       
   127 	// Auth string is now built, 
       
   128 	HBufC8* encodedBuf = HBufC8::NewMaxLC(KMaxLengthOfPlainMessageBase64); 
       
   129 	TPtr8 encodedBufPtr = encodedBuf->Des();
       
   130  
       
   131 	//Encode string 
       
   132 	iEncoder.Encode(authStringPtr,encodedBufPtr);	
       
   133 	
       
   134 	// Now build the message
       
   135 	iNextClientMessage.Append(encodedBufPtr);
       
   136  	iNextClientMessage.Append(KImapTxtCrlf);	
       
   137  	
       
   138  	//send the command to the server
       
   139  	aStream.SendDataReq(iNextClientMessage);
       
   140  	
       
   141  	CleanupStack::PopAndDestroy(2,authString);
       
   142 	}
       
   143 
       
   144 
       
   145 /** 
       
   146 Respond  upon reciept of a continuation response from server.
       
   147 */
       
   148 void CImapAuthPlainMechanismHelper::ParseContinuationResponseL(){}
       
   149 
       
   150 /**
       
   151 Static factory constructor. Part of two phased construction.
       
   152 
       
   153 @param aImapSettings Settings for the Imap service
       
   154 @param aSelectedFolderData Imap Folder Info
       
   155 @param aLogId logging info
       
   156 @return The constructed CImapAuthLoginMechanismHelper object
       
   157 */
       
   158 CImapAuthLoginMechanismHelper* CImapAuthLoginMechanismHelper::NewL(const CImapSettings& aSettings,CImapFolderInfo* aSelectedFolderData, TInt aLogId)  
       
   159 	{
       
   160 	CImapAuthLoginMechanismHelper* self= new (ELeave) CImapAuthLoginMechanismHelper(aSettings,aSelectedFolderData,aLogId);
       
   161 	CleanupStack::PushL(self);
       
   162 	self->ConstructL();
       
   163 	CleanupStack::Pop();	                                
       
   164 	return self;
       
   165 	}
       
   166 	
       
   167 /**
       
   168 Class constructor
       
   169 
       
   170 @param aImapSettings Settings for IMAP service
       
   171 @param aSelectedFolderData Imap Folder Info
       
   172 @param aLogId logging info
       
   173 */
       
   174 CImapAuthLoginMechanismHelper::CImapAuthLoginMechanismHelper(const CImapSettings& aSettings,CImapFolderInfo* aSelectedFolderData, TInt aLogId) : CImapAuthMechanismHelper(aSettings,aSelectedFolderData, aLogId){}
       
   175 
       
   176 /**
       
   177 Class destructor
       
   178 */
       
   179 CImapAuthLoginMechanismHelper::~CImapAuthLoginMechanismHelper()
       
   180 	{
       
   181 	delete iBase64LoginName;
       
   182 	delete iBase64Password;
       
   183 	}
       
   184 	
       
   185 /**
       
   186 Non-trival constructor. Part of two phased construction.
       
   187 Creates Base64 Username and Password
       
   188 */
       
   189 void CImapAuthLoginMechanismHelper::ConstructL()
       
   190 	{
       
   191 	//Get LoginName from central repository
       
   192 	TInt len=(((iSettings.LoginName().Length()/3)+1)*4)+2; // length of LoginName in base 64 + 2 for CRLF
       
   193 	iBase64LoginName=HBufC8::NewL(len);
       
   194 	TPtr8 authString(iBase64LoginName->Des());
       
   195 	
       
   196 	//Encode LoginName
       
   197 	iEncoder.Encode(iSettings.LoginName(),authString);
       
   198 	//Append CRLF to authstring
       
   199 	authString.Append(KImapTxtCrlf);
       
   200 	
       
   201 	//Get Password from central repository
       
   202 	len=(((iSettings.Password().Length()/3)+1)*4)+2; // length of Password in base 64 + 2 for CRLF
       
   203 	iBase64Password=HBufC8::NewL(len);
       
   204 	authString.Set(iBase64Password->Des());
       
   205 	
       
   206 	//Encode Password Name
       
   207 	iEncoder.Encode(iSettings.Password(),authString);
       
   208 	//Append CRLF to authstring
       
   209 	authString.Append(KImapTxtCrlf);
       
   210 	}
       
   211 	
       
   212 	
       
   213 /** 
       
   214 Formats and sends the IMAP AUTHENTICATE LOGIN command.
       
   215 @param aTagId Command sequence id which will be send along with the IMAP command.
       
   216 @param aStream A wrapper for the outbound stream of a connected socket, using which 
       
   217 the command will be send to the server
       
   218 */
       
   219 void CImapAuthLoginMechanismHelper::SendMessageL(TInt aTagId, MOutputStream& aStream)
       
   220 	{
       
   221 	
       
   222 	iTagId = aTagId;
       
   223 	iStream = &aStream;
       
   224 	
       
   225 	TInt bufLength = KCommandAuthenticateLogin().Length();
       
   226 	
       
   227 	//Add tagId to buffer
       
   228 	bufLength += TagLength(aTagId);
       
   229 	
       
   230 	// Create  Authenticate LOGIN command message
       
   231 	iOutputBuffer = HBufC8::NewL(bufLength);
       
   232 	iOutputBuffer->Des().Format(KCommandAuthenticateLogin, aTagId);
       
   233 	
       
   234 	//send the command to the server
       
   235 	aStream.SendDataReq(*iOutputBuffer);
       
   236 	}
       
   237 
       
   238 /** 
       
   239 Sends the Encoded Username  message using Login, upon reciept of a continuation response from server.
       
   240 The next line Encoded Password  message  will be sent upon reciept of a continuation response from server, indicating that the first line has been succesfully sent.
       
   241 */
       
   242 
       
   243 void CImapAuthLoginMechanismHelper::ParseContinuationResponseL()
       
   244 	{
       
   245 	
       
   246 	iNextClientMessage.Zero();
       
   247 	
       
   248 	//Get next part of the response from server
       
   249 	TPtrC8 part = GetNextPart();
       
   250 
       
   251 	HBufC8* decodedMessage = NULL;
       
   252 	
       
   253 	decodedMessage = HBufC8::NewLC(KImMailMaxBufferSize);
       
   254 	if (decodedMessage)
       
   255 		{
       
   256 		TPtr8 decodedMessagePtr = decodedMessage->Des();
       
   257 		
       
   258 		//Decode Server response 
       
   259 		iEncoder.Decode(part, decodedMessagePtr);
       
   260 		}    
       
   261         
       
   262 	switch (iState)
       
   263 		{
       
   264 		case ESendingLoginName:
       
   265             if (decodedMessage)
       
   266                 {
       
   267                 // Match against 'username*'
       
   268                 if (decodedMessage->Des().MatchF(KImapAuthBase64StringUsername) == KErrNotFound)
       
   269 					{
       
   270                     User::Leave(KErrNotSupported);
       
   271 					}
       
   272 				//Initialise encoder	
       
   273 				iEncoder.Initialise();
       
   274 				
       
   275 				//Copy Base64 LoginName to buffer
       
   276 				iNextClientMessage=*iBase64LoginName;
       
   277 				
       
   278 				//Change state to ESendingPassword 
       
   279                 iState = ESendingPassword;
       
   280                 }
       
   281             else
       
   282                 {
       
   283 				User::Leave(KErrNotSupported);
       
   284 				}
       
   285 			break;
       
   286 		case ESendingPassword:
       
   287 			if (decodedMessage)
       
   288                 {
       
   289                 // Match against 'password*'
       
   290                 if (decodedMessage->Des().MatchF(KImapAuthBase64StringPassword) == KErrNotFound)
       
   291 					{
       
   292                     User::Leave(KErrNotSupported);
       
   293 					}
       
   294 					
       
   295 				//Copy Base64 Password to buffer
       
   296 				iNextClientMessage=*iBase64Password;
       
   297                 }
       
   298 			else
       
   299                 {
       
   300 				User::Leave(KErrNotSupported);
       
   301 				}
       
   302 			break;
       
   303 		default:
       
   304 			User::Leave(KErrNotSupported);
       
   305 			break;
       
   306 		}
       
   307 	
       
   308 	if (decodedMessage)
       
   309 		{
       
   310 		CleanupStack::PopAndDestroy(decodedMessage);
       
   311 		}
       
   312 		
       
   313 	//send the command to the server
       
   314 	iStream->SendDataReq(iNextClientMessage);
       
   315 	}
       
   316 
       
   317 /**
       
   318 Static factory constructor. Part of two phased construction.
       
   319 
       
   320 @param aImapSettings Settings for the Imap service
       
   321 @param aSelectedFolderData Imap Folder Info
       
   322 @param aLogId logging info
       
   323 @return The constructed CImapAuthCramMd5MechanismHelper object
       
   324 */
       
   325 CImapAuthCramMd5MechanismHelper* CImapAuthCramMd5MechanismHelper::NewL(const CImapSettings& aSettings,CImapFolderInfo* aSelectedFolderData, TInt aLogId)  
       
   326 	{
       
   327 	CImapAuthCramMd5MechanismHelper* self= new (ELeave) CImapAuthCramMd5MechanismHelper(aSettings,aSelectedFolderData,aLogId);
       
   328 	CleanupStack::PushL(self);
       
   329 	self->ConstructL();
       
   330 	CleanupStack::Pop();	                                
       
   331 	return self;
       
   332 	}
       
   333 
       
   334 /**
       
   335 Class constructor
       
   336 
       
   337 @param aImapSettings Settings for IMAP service
       
   338 @param aSelectedFolderData Imap Folder Info
       
   339 @param aLogId logging info
       
   340 */
       
   341 CImapAuthCramMd5MechanismHelper::CImapAuthCramMd5MechanismHelper(const CImapSettings& aSettings,CImapFolderInfo* aSelectedFolderData, TInt aLogId) :CImapAuthMechanismHelper(aSettings,aSelectedFolderData, aLogId){}
       
   342 
       
   343 /**
       
   344 Non-trival constructor. Part of two phased construction.
       
   345 Constructs md5 
       
   346 */
       
   347 void CImapAuthCramMd5MechanismHelper::ConstructL()
       
   348 	{
       
   349 	//Create Md5
       
   350 	iMd5Hash = CMD5::NewL();
       
   351 	}
       
   352 
       
   353 CImapAuthCramMd5MechanismHelper::~CImapAuthCramMd5MechanismHelper()
       
   354 	{
       
   355 	//Delete Md5 object
       
   356 	delete iMd5Hash;
       
   357 	}
       
   358 
       
   359 /** 
       
   360 Formats and sends the IMAP AUTHENTICATE CRAM-MD5 command.
       
   361 @param aTagId Command sequence id which will be send along with the IMAP command.
       
   362 @param aStream A wrapper for the outbound stream of a connected socket, using which 
       
   363 the command will be send to the server
       
   364 */
       
   365 void CImapAuthCramMd5MechanismHelper::SendMessageL(TInt aTagId, MOutputStream& aStream)
       
   366 	{
       
   367 	
       
   368 	iTagId = aTagId;
       
   369 	iStream = &aStream;
       
   370 	
       
   371 	TInt bufLength = KCommandAuthenticateCrammd5().Length();
       
   372 	
       
   373 	//Add tagId to buffer
       
   374 	bufLength += TagLength(aTagId);
       
   375 	
       
   376 	// Create  Authenticate CRAM-MD5 command message
       
   377 	iOutputBuffer = HBufC8::NewL(bufLength);
       
   378 	iOutputBuffer->Des().Format(KCommandAuthenticateCrammd5, aTagId);
       
   379 	
       
   380 	//send the command to the server		
       
   381 	aStream.SendDataReq(*iOutputBuffer);
       
   382 	}
       
   383 
       
   384 
       
   385 /**Creates Shared secret Key between client and server 
       
   386 @param aPassword
       
   387 */
       
   388 TPtr8 CImapAuthCramMd5MechanismHelper::FormSharedSecret(TPtr8 aPassword)
       
   389 	{
       
   390 	TPtr8 secret = aPassword;
       
   391 	
       
   392 	if (aPassword.Length() <= KMd5BlockLength)
       
   393 		{
       
   394 		//pad up to 64 bytes with zeros
       
   395 		secret.SetLength(64);
       
   396 		TInt origLen = aPassword.Length();
       
   397 		TInt lengthToFill = KMd5BlockLength-aPassword.Length();
       
   398 		for (TInt i = 0; i<lengthToFill; i++)
       
   399 			{
       
   400 			secret[origLen+i]=0x00;
       
   401 			}
       
   402 		}
       
   403 	else
       
   404 		{
       
   405 		//make shared secret the Digest of the password
       
   406 		secret.Zero();
       
   407 		
       
   408 		//Reset Md5
       
   409 		iMd5Hash->Reset();
       
   410 		
       
   411 		//Hash Md5
       
   412 		secret = (iMd5Hash->Hash(aPassword));
       
   413 		}
       
   414 
       
   415 	return secret;
       
   416 	}
       
   417 
       
   418 
       
   419 /** 
       
   420 Sends the Encoded Username and password message using CRAM-MD5, upon reciept of a continuation response from server.
       
   421 */
       
   422 void CImapAuthCramMd5MechanismHelper::ParseContinuationResponseL()
       
   423 	{
       
   424 	
       
   425 	//Sets the length of the data to zero. 
       
   426 	iNextClientMessage.Zero();
       
   427 	
       
   428 	//Get next part of the response from server
       
   429 	TPtrC8 part = GetNextPart();
       
   430 	__LOG_FORMAT((iLogId, "CImapCommand::ParseBlockL(): [%S] %d octets", &part, part.Length()));
       
   431 
       
   432 	//Create Cram-md5 Base64 message
       
   433 	HBufC8* authbuffer = HBufC8::NewMaxLC(KImMailMaxBufferSize);
       
   434 	TPtr8 authbufferptr = authbuffer->Des();
       
   435 	HBufC8* authbuffer2 = HBufC8::NewMaxLC(KImMailMaxBufferSize);
       
   436 	TPtr8 authbufferptr2 = authbuffer2->Des();
       
   437 	HBufC8* authbuffer3 = HBufC8::NewMaxLC(KImMailMaxBufferSize);
       
   438 	TPtr8 authbufferptr3 = authbuffer3->Des();
       
   439 	
       
   440 	//copy server response to buffer
       
   441 	authbufferptr = part;
       
   442 	
       
   443 	//Decode Server response,timestamp info now in authbufferptr2
       
   444 	iEncoder.Decode(authbufferptr,authbufferptr2);
       
   445 	
       
   446 	//Sets the length of the data to zero. 
       
   447 	authbufferptr.Zero();
       
   448 	authbufferptr3.Zero();
       
   449 	
       
   450 	//Get Password from Central repository
       
   451 	authbufferptr = iSettings.Password();
       
   452 	
       
   453 	//Create Shared secrete key 
       
   454 	authbufferptr3 = FormSharedSecret(authbufferptr);
       
   455 	
       
   456 	//Sets the length of the data to zero.
       
   457 	authbufferptr.Zero();
       
   458 	
       
   459 	// authbufferptr and authbufferptr3 contain the shared secret null padded to 64 bytes.
       
   460 	authbufferptr = authbufferptr3;  
       
   461 	TInt i=0;
       
   462 	for (i=0; i<KMd5BlockLength; i++) 
       
   463 		{
       
   464 		authbufferptr[i] ^= 0x36; //ipad
       
   465 		authbufferptr3[i] ^= 0x5c; //opad
       
   466 		}
       
   467 	
       
   468 	//Append timestamp info to authebufferptr
       
   469 	authbufferptr.Append(authbufferptr2);
       
   470 
       
   471 	//Reset Md5
       
   472 	iMd5Hash->Reset();                                                                                                                                                                                                                                                                                                                ;
       
   473 	
       
   474 	//Hash Md5                                                                                                                                                                                                                                                                                                                ;
       
   475 	authbufferptr2 = iMd5Hash->Hash(authbufferptr);
       
   476 
       
   477 	authbufferptr3.Append(authbufferptr2);
       
   478 	authbufferptr.Zero();
       
   479 	
       
   480 	//Reset Md5
       
   481 	iMd5Hash->Reset();
       
   482 	
       
   483 	//Hash  Md5
       
   484 	authbufferptr = iMd5Hash->Hash(authbufferptr3);
       
   485 
       
   486 	// MD5 algorithm ALWAYS returns 16 bytes of data - which will be converted into
       
   487 	// 32 characters; each byte represented by a 2 character hex representation, 
       
   488 	// eg 255="ff"
       
   489 	
       
   490 	TBuf<32> hexHash; 
       
   491 	for (i=0;i<16;i++)
       
   492 		{
       
   493 		hexHash.AppendNumFixedWidth(authbufferptr[i],EHex,2);
       
   494 		}
       
   495 
       
   496 	authbufferptr3.Zero();
       
   497 	
       
   498 	//Get LoginName from Central repository
       
   499 	authbufferptr3.Append(iSettings.LoginName());
       
   500 	
       
   501 	//Add Space to authbufferptr3
       
   502 	authbufferptr3.Append(_L8(" "));
       
   503 	
       
   504 	//Append hexHash authbufferptr3
       
   505 	authbufferptr3.Append(hexHash);
       
   506 	
       
   507 	//Encode Cram-md5 message
       
   508 	iEncoder.Encode(authbufferptr3, iNextClientMessage);
       
   509 	
       
   510 	//Append CRLF to iNextClientMessage
       
   511 	iNextClientMessage.Append(KImapTxtCrlf);
       
   512 	
       
   513 	CleanupStack::PopAndDestroy(3);	// authbufferptr3 ,authbufferptr2, authbufferptr
       
   514 	
       
   515 	//send the command to the server
       
   516 	iStream->SendDataReq(iNextClientMessage);
       
   517 	}
       
   518