vpnengine/ikev2lib/src/ikemsgrec.cpp
changeset 0 33413c0669b9
equal deleted inserted replaced
-1:000000000000 0:33413c0669b9
       
     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: Received IKE message handling rules.
       
    15 *
       
    16 */
       
    17 
       
    18 #include "ikev2SAdata.h"
       
    19 #include "ikemsgrec.h"
       
    20 #include "ikecrypto.h"
       
    21 //
       
    22 //  CIkev2Payloads
       
    23 //
       
    24 
       
    25 CIkev2Payloads::CIkev2Payloads(const TIkev2SAData& aIkeV2SaData)
       
    26 :iIkeV2SaData(aIkeV2SaData)
       
    27 {
       
    28 }
       
    29 
       
    30 CIkev2Payloads::~CIkev2Payloads()
       
    31 {
       
    32 	delete iProps;
       
    33 	delete iTrans;  	
       
    34 	delete iCerts;
       
    35 	delete iCertReqs;
       
    36 	delete iNotifs;
       
    37 	delete iDeletes; 	
       
    38 	delete iVids;
       
    39 	delete iGenPlds;
       
    40 }
       
    41 
       
    42 CIkev2Payloads* CIkev2Payloads::NewL(const ThdrISAKMP &aHdr, const TIkev2SAData& aIkeV2SaData)
       
    43 {
       
    44 	CIkev2Payloads* Payloads = new (ELeave) CIkev2Payloads(aIkeV2SaData);
       
    45 	CleanupStack::PushL(Payloads);
       
    46 
       
    47 	Payloads->ConstructL();
       
    48 
       
    49 	Payloads->iIkeMsg = (ThdrISAKMP*)&aHdr;  // Save pointer to IKE message data
       
    50 	TInt Lth = aHdr.GetLength() - ThdrISAKMP::Size();
       
    51 	Payloads->ParsePayloadsL(TPayloadIkev2::Cast(aHdr.Next()), Lth, aHdr.GetPayload(), IKEV2_PAYLOAD_NONE);
       
    52 	
       
    53 	CleanupStack::Pop(Payloads);
       
    54 	
       
    55 	return Payloads;
       
    56 }
       
    57 
       
    58 CIkev2Payloads* CIkev2Payloads::NewL(TPayloadIkev2* aPayload, TUint8 aPayloadType, TIkev2SAData& aIkeV2SaData)
       
    59 {
       
    60 	User::LeaveIfNull((TAny*)aPayload);
       
    61 	CIkev2Payloads* Payloads = new (ELeave) CIkev2Payloads(aIkeV2SaData);
       
    62 	CleanupStack::PushL(Payloads);
       
    63 
       
    64 	Payloads->ConstructL();
       
    65 
       
    66 	Payloads->ParsePayloadsL(aPayload, aPayload->GetLength(), aPayloadType, aPayloadType);	
       
    67 
       
    68 	CleanupStack::Pop(Payloads);
       
    69 
       
    70 	return Payloads;
       
    71 }
       
    72 
       
    73 TBool CIkev2Payloads::ParsePayloadL(TPayloadIkev2* aPayload, TUint16 aPlType)
       
    74 {
       
    75 	User::LeaveIfNull((TAny*)aPayload);
       
    76 
       
    77 	if ( aPlType == IKEV2_PAYLOAD_SA )
       
    78 	    iProps->Reset();
       
    79 	else if ( aPlType == IKEV2_PAYLOAD_PROP )
       
    80 		iTrans->Reset();		
       
    81 	if ( ParsePayloadsL(aPayload, aPayload->GetLength(), aPlType, aPlType) != 0 )
       
    82 		 return EFalse;
       
    83 	else return ETrue;
       
    84 }
       
    85 
       
    86 void CIkev2Payloads::ConstructL()
       
    87 {
       
    88 	iProps    = new (ELeave) CArrayFixFlat<TProposalIkev2 *>(4);
       
    89 	iTrans    = new (ELeave) CArrayFixFlat<TTransformIkev2 *>(4);		
       
    90 	iCerts    = new (ELeave) CArrayFixFlat<TCertPayloadIkev2 *>(4);
       
    91 	iCertReqs = new (ELeave) CArrayFixFlat<TCReqPayloadIkev2 *>(2);
       
    92 	iNotifs   = new (ELeave) CArrayFixFlat<TNotifPayloadIkev2 *>(2);	
       
    93 	iDeletes  = new (ELeave) CArrayFixFlat<TDeletePlIkev2 *>(2);		
       
    94 	iVids     = new (ELeave) CArrayFixFlat<TVendorPlIkev2 *>(2);
       
    95 	iGenPlds  = new (ELeave) CArrayFixFlat<TPayloadIkev2 *>(2);
       
    96 }
       
    97 
       
    98 
       
    99 TInt CIkev2Payloads::ParsePayloadsL(TPayloadIkev2* aPayload, TInt aLength, TUint16 aPlType, TUint16 aRefPlType )
       
   100 {
       
   101 	ASSERT(aPayload);
       
   102 	TBool  Critical;
       
   103 	TInt   PlLth;
       
   104 	TInt   RefLth;	
       
   105 	
       
   106 	while ( aPlType != IKEV2_PAYLOAD_NONE )  
       
   107 	{
       
   108 		PlLth  = aPayload->GetLength();
       
   109 		RefLth = TPayloadIkev2::Size();		
       
   110 		
       
   111 		if ( ( aLength < TPayloadIkev2::Size() ) || (aLength < PlLth) )
       
   112 		{
       
   113 			if ( aLength && ( aRefPlType != IKEV2_PAYLOAD_SA )) 			
       
   114 			   SetStatus(INVALID_SYNTAX); // Payload length mismatch !!
       
   115 			return aLength;		   
       
   116 		}
       
   117 		
       
   118 		if ( (aRefPlType != IKEV2_PAYLOAD_NONE) && (aRefPlType != aPlType) )
       
   119 		{	
       
   120 		   SetStatus(INVALID_SYNTAX); // Illegal payload type !!
       
   121 		   return aLength;		   
       
   122 	    }
       
   123 		
       
   124 		Critical = aPayload->GetCritical();
       
   125 
       
   126 		switch ( aPlType )
       
   127 		{
       
   128 			case IKEV2_PAYLOAD_PROP:
       
   129 				iProps->AppendL(TProposalIkev2::Cast(aPayload));
       
   130 			    //
       
   131 			    // Parse Transform payloads within a Proposal payload
       
   132 				// (recursively)
       
   133 				//
       
   134 				ParsePayloadsL(TPayloadIkev2::Cast(TProposalIkev2::Cast(aPayload)->TransformPl()),
       
   135 							  (PlLth - TProposalIkev2::Cast(aPayload)->PropHdrLth()),
       
   136 							   IKEV2_PAYLOAD_TRANS, IKEV2_PAYLOAD_TRANS);
       
   137 				if ( Status() )
       
   138 					return aLength;
       
   139 				break;
       
   140 
       
   141 			case IKEV2_PAYLOAD_TRANS:
       
   142 				RefLth = TTransformIkev2::Cast(aPayload)->Size();
       
   143 				iTrans->AppendL(TTransformIkev2::Cast(aPayload));
       
   144 				break;
       
   145 			
       
   146 			case IKEV2_PAYLOAD_SA: //also includes proposal and transform
       
   147 				if ( !iSa ) // Only one SA payload (The first)
       
   148 				{ 
       
   149 				   iSa = aPayload;
       
   150 				   //
       
   151 				   // Parse Proposal payloads within a SA payload
       
   152 				   // (recursively)
       
   153 				   //
       
   154 				   ParsePayloadsL(TPayloadIkev2::Cast(aPayload->PayloadData()), aPayload->PlDataLen(),
       
   155 								  IKEV2_PAYLOAD_PROP, IKEV2_PAYLOAD_PROP);
       
   156 				   if ( Status() )
       
   157 					   return aLength;
       
   158 				}   
       
   159 				break;
       
   160 
       
   161 			case IKEV2_PAYLOAD_KE:
       
   162 				RefLth = TKEPayloadIkev2::Size();				
       
   163 				if ( !iKe )	 // Only one KE payload (The first)
       
   164 				{
       
   165 				   iKe = TKEPayloadIkev2::Cast(aPayload);
       
   166 				}   
       
   167 				break;
       
   168 
       
   169 			case IKEV2_PAYLOAD_ID_I:
       
   170 				if ( !Encrypted() )
       
   171 				{
       
   172 				    SetStatus(INVALID_SYNTAX);  // ID payload MUST be encrypted
       
   173 				    return aLength;
       
   174 				}
       
   175 				RefLth = TIDPayloadIkev2::Size();
       
   176 				if ( !iIdI )	 // Only one Initiator ID payload (The first)
       
   177 				{
       
   178 				    iIdI = TIDPayloadIkev2::Cast(aPayload);
       
   179 				}   
       
   180 				break;
       
   181 
       
   182 			case IKEV2_PAYLOAD_ID_R:
       
   183 				if ( !Encrypted() )
       
   184 				{
       
   185 					SetStatus(INVALID_SYNTAX);  // ID payload MUST be encrypted
       
   186 					return aLength;
       
   187 				}
       
   188 				RefLth = TIDPayloadIkev2::Size();
       
   189 				if ( !iIdR )	 // Only one Responder ID payload (The first)
       
   190 				{
       
   191 				    iIdR = TIDPayloadIkev2::Cast(aPayload);
       
   192 				}   
       
   193 				break;
       
   194 
       
   195 			case IKEV2_PAYLOAD_CERT:
       
   196 				RefLth = TCertPayloadIkev2::Size();
       
   197 				iCerts->AppendL(TCertPayloadIkev2::Cast(aPayload));
       
   198 				break;
       
   199 				
       
   200 			case IKEV2_PAYLOAD_CR:
       
   201 				RefLth = TCReqPayloadIkev2::Size();	
       
   202 				iCertReqs->AppendL(TCReqPayloadIkev2::Cast(aPayload));
       
   203 				break;
       
   204 
       
   205 			case IKEV2_PAYLOAD_AUTH:
       
   206 				if ( !Encrypted() )
       
   207 				{
       
   208 				    SetStatus(INVALID_SYNTAX);  // Auth payload MUST be encrypted
       
   209 				    return aLength;
       
   210 				}
       
   211 				RefLth = TAuthPayloadIkev2::Size();
       
   212 				if ( !iAuth )	 // Only one Authentication payload (The first)	
       
   213 				{
       
   214 					iAuth = TAuthPayloadIkev2::Cast(aPayload);
       
   215 				}   
       
   216 				break;
       
   217 
       
   218 			case IKEV2_PAYLOAD_NONCE:
       
   219 				if ( !iNonce )
       
   220 				{ // Only one SA payload (The first)	
       
   221 					iNonce = aPayload;
       
   222 				}   
       
   223 				break;
       
   224 
       
   225 			case IKEV2_PAYLOAD_NOTIF:
       
   226 				RefLth = TNotifPayloadIkev2::Size();
       
   227 				iNotifs->AppendL(TNotifPayloadIkev2::Cast(aPayload));						
       
   228 				break;
       
   229 
       
   230 			case IKEV2_PAYLOAD_DELETE:
       
   231 				RefLth = TDeletePlIkev2::Size();				
       
   232 				iDeletes->AppendL(TDeletePlIkev2::Cast(aPayload));						
       
   233 				break;
       
   234 				
       
   235 			case IKEV2_PAYLOAD_VID:
       
   236 				iVids->AppendL(aPayload);
       
   237 				break;
       
   238 
       
   239 			case IKEV2_PAYLOAD_TS_I:
       
   240 				if ( !Encrypted() )
       
   241 				{
       
   242 					SetStatus(INVALID_SYNTAX);  // ID payload MUST be encrypted
       
   243 					return aLength;
       
   244 				}
       
   245 				RefLth = TTSPayloadIkev2::Size();
       
   246 				if ( !iTsI )	 // Only one Initiator ID payload (The first)
       
   247 				{
       
   248 					iTsI = TTSPayloadIkev2::Cast(aPayload);
       
   249 				}   
       
   250 				break;
       
   251 
       
   252 			case IKEV2_PAYLOAD_TS_R:
       
   253 				if ( !Encrypted() )
       
   254 				{
       
   255 					SetStatus(INVALID_SYNTAX);  // ID payload MUST be encrypted
       
   256 					return aLength;
       
   257 				}
       
   258 				RefLth = TTSPayloadIkev2::Size();
       
   259 				if ( !iTsR )	 // Only one Initiator ID payload (The first)
       
   260 				{
       
   261 					iTsR = TTSPayloadIkev2::Cast(aPayload);
       
   262 				}   
       
   263 				break;
       
   264 
       
   265 			case IKEV2_PAYLOAD_ENCR:
       
   266 				if ( !iEncr )	 // Only one Initiator ID payload (The first)
       
   267 				{
       
   268 				   DecryptEncrPayloadL(aPayload);
       
   269 				   iEncr = aPayload;
       
   270 				}
       
   271 				else SetStatus(INVALID_SYNTAX);  // Only ONE encrypted payload per message
       
   272 				if ( Status() )
       
   273 					return aLength;
       
   274 				break;
       
   275 
       
   276 			case IKEV2_PAYLOAD_CONFIG:
       
   277 				if ( !Encrypted() )
       
   278 				{
       
   279 					SetStatus(INVALID_SYNTAX);  // ID payload MUST be encrypted
       
   280 					return aLength;
       
   281 				}
       
   282 				RefLth = TCPPayloadIkev2::Size();				
       
   283 				if ( !iCp )	 // Only one Config payload (The first)
       
   284 				{
       
   285 					iCp = TCPPayloadIkev2::Cast(aPayload);
       
   286 				}
       
   287 				break;
       
   288 
       
   289 			case IKEV2_PAYLOAD_EAP:
       
   290 				if ( !Encrypted() )
       
   291 				{
       
   292 					SetStatus(INVALID_SYNTAX);  // ID payload MUST be encrypted
       
   293 					return aLength;
       
   294 				}	
       
   295 				if ( !iEap )	 // Only one Config payload (The first)
       
   296 				{
       
   297 					iEap = aPayload;
       
   298 				}
       
   299 				break;
       
   300 				
       
   301 			//
       
   302 			// Unknown payload detected. If Critical bit is not set
       
   303 			// 
       
   304 			//
       
   305 			default:
       
   306 				if ( Critical )
       
   307 				{	
       
   308 				   SetStatus(UNSUPPORTED_CRITICAL_PAYLOAD);
       
   309 				   return aLength;
       
   310 				}		 
       
   311 				else iGenPlds->AppendL(aPayload);
       
   312 				break;
       
   313 				
       
   314 		}
       
   315 
       
   316 		if ( PlLth < RefLth )
       
   317 		{	
       
   318 			SetStatus(INVALID_SYNTAX);  // Length mismatch
       
   319 			return aLength;
       
   320 		}
       
   321 		
       
   322 		aLength  -= PlLth;
       
   323 		aPlType   = aPayload->GetNextPayload();
       
   324 		aPayload  = aPayload->Next();
       
   325 	}
       
   326 
       
   327 	if ( aLength )
       
   328 		SetStatus(INVALID_SYNTAX);  // Length mismatch
       
   329 		
       
   330 	return aLength;
       
   331 }	
       
   332 
       
   333 void CIkev2Payloads::DecryptEncrPayloadL(TPayloadIkev2* aPayload)
       
   334 {
       
   335     ASSERT(aPayload);
       
   336 	//
       
   337 	//  Process Encrypted Payload
       
   338 	//                      1                   2                   3
       
   339     //  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
       
   340 	//  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
       
   341 	//  ! Next Payload  !C!  RESERVED   !         Payload Length        !
       
   342 	//  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
       
   343 	//  !                     Initialization Vector                     !
       
   344 	//	!         (length is block size for encryption algorithm)       !
       
   345 	//	+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
       
   346 	//	!                    Encrypted IKE Payloads                     !
       
   347 	//	+               +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
       
   348 	//	!               !             Padding (0-255 octets)            !
       
   349 	//	+-+-+-+-+-+-+-+-+                               +-+-+-+-+-+-+-+-+
       
   350 	//	!                                               !  Pad Length   !
       
   351 	//	+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
       
   352 	//	~                    Integrity Checksum Data                    ~
       
   353 	//	+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
       
   354 	//
       
   355 	//  Save integrity checksum from encrypted payload tail the
       
   356 	//  integrity checksum length is defined by used integrity algorithm
       
   357 	//
       
   358 	TInt CheksumLth  = iIkeV2SaData.iIntChkSumLth;
       
   359 	TInt CbcLth      = iIkeV2SaData.iCipherBlkLth;	
       
   360 	TInt EmbeddedLth = aPayload->GetLength();
       
   361 	if ( EmbeddedLth <  (CheksumLth + CbcLth) )
       
   362 	{
       
   363 		SetStatus(INVALID_SYNTAX); 
       
   364 		return;
       
   365 	}
       
   366 	TUint8* MsgChecksum = (TUint8*)aPayload + EmbeddedLth - CheksumLth; 
       
   367 	TBuf8<IKEV2_KEY_MATERIAL_SIZE> CheckSum;
       
   368 	//
       
   369 	//  Decrypt encryption payload content and payload tail the
       
   370 	//  integrity checksum length is defined by used integrity algorithm
       
   371 	//
       
   372 	
       
   373     TInt EntireLth           = iIkeMsg->GetLength() - iIkeV2SaData.iIntChkSumLth;   
       
   374     TInt EncryptedDataLength = aPayload->GetLength() - (iIkeV2SaData.iIntChkSumLth + iIkeV2SaData.iCipherBlkLth + TPayloadIkev2::Size());
       
   375     TUint8* IvPtr            = aPayload->PayloadData();  
       
   376     TUint8* EncryptedData    = IvPtr + iIkeV2SaData.iCipherBlkLth;
       
   377             
       
   378     TPtrC8 entireDataPtr((TUint8*)iIkeMsg, EntireLth);
       
   379     if ( iIkeV2SaData.iInitiator )
       
   380     {   
       
   381        IkeCrypto::IntegHMACL(entireDataPtr, CheckSum, iIkeV2SaData.iSK_ar, iIkeV2SaData.iIntegAlg);        
       
   382        IkeCrypto::DecryptL(EncryptedData, EncryptedData, EncryptedDataLength, IvPtr, iIkeV2SaData.iSK_er, iIkeV2SaData.iEncrAlg);
       
   383     }
       
   384     else
       
   385     {
       
   386        IkeCrypto::IntegHMACL(entireDataPtr, CheckSum, iIkeV2SaData.iSK_ai, iIkeV2SaData.iIntegAlg);        
       
   387        IkeCrypto::DecryptL(EncryptedData, EncryptedData, EncryptedDataLength, IvPtr, iIkeV2SaData.iSK_ei, iIkeV2SaData.iEncrAlg);
       
   388     }   
       
   389 	
       
   390 	
       
   391 	//
       
   392 	//  Check that integrity cheksum is correct
       
   393 	//
       
   394 	if ( Mem::Compare(MsgChecksum, CheksumLth ,CheckSum.Ptr(), CheksumLth) != 0 )
       
   395 	{
       
   396 	   SetStatus(INVALID_SYNTAX);  // Length mismatch
       
   397        return;
       
   398 	}
       
   399 	//
       
   400 	//  Process embedded payloads inside the encrypted payload
       
   401 	//  - Bypass IV in the begin of encrypted payload data
       
   402 	//  - Assure that padded "embedded" payloads have length which
       
   403 	//    equals with multiple of CBC block length
       
   404 	//  - Ignore padding bytes from that length 
       
   405 	//
       
   406 	EmbeddedLth -= (CheksumLth + CbcLth + TPayloadIkev2::Size());
       
   407 	if ( EmbeddedLth % CbcLth )
       
   408 	{
       
   409 		SetStatus(INVALID_SYNTAX); 
       
   410 		return;
       
   411 	}
       
   412 	MsgChecksum --;  // Move pointer to padding length
       
   413 	if ( EmbeddedLth < (TInt)*MsgChecksum ) 
       
   414 	{
       
   415 		SetStatus(INVALID_SYNTAX); 
       
   416 		return;
       
   417 	}
       
   418 	EmbeddedLth -= (TInt)*MsgChecksum;
       
   419 	EmbeddedLth --;  // Pad Length itself
       
   420 	TPayloadIkev2* EmbeddedPl = TPayloadIkev2::Cast(aPayload->PayloadData() + CbcLth);
       
   421 	
       
   422 	iEncrypted = ETrue;   // Set encrypted indicator
       
   423 	
       
   424 	ParsePayloadsL(EmbeddedPl, EmbeddedLth,
       
   425 				   aPayload->GetNextPayload(), IKEV2_PAYLOAD_NONE);
       
   426 
       
   427 }
       
   428 
       
   429 ThdrISAKMP* CIkev2Payloads::GetIkeMsg() 
       
   430 {
       
   431     return iIkeMsg;
       
   432 }
       
   433 
       
   434 
       
   435 TInt CIkev2Payloads::Status() 
       
   436 {
       
   437     return iStatus;
       
   438 }
       
   439 
       
   440 
       
   441 void CIkev2Payloads::SetStatus(TInt aStatus) 
       
   442 { 
       
   443     if ( iStatus == 0) iStatus = aStatus;
       
   444 }
       
   445 
       
   446 
       
   447 TBool CIkev2Payloads::Encrypted() 
       
   448 {
       
   449     return iEncrypted;
       
   450 }
       
   451 
       
   452