networksecurity/tlsprovider/Test/tlstest2/newkeyderivationstep.cpp
changeset 0 af10295192d8
equal deleted inserted replaced
-1:000000000000 0:af10295192d8
       
     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 /**
       
    17  @file newkeyderivationstep.cpp
       
    18  @internalTechnology
       
    19 */
       
    20 #include "newtlsstepbase.h"
       
    21 #include "newkeyderivationstep.h"
       
    22 #include "psuedorandom.h"
       
    23 
       
    24 #include <tlsprovinterface.h>
       
    25 
       
    26 CNewKeyDerivationStep::CNewKeyDerivationStep()
       
    27 	{
       
    28 	SetTestStepName(KNewKeyDerivationStep);
       
    29 	}
       
    30 	
       
    31 
       
    32 // These methods are been used because due to private inheritance some objects
       
    33 // of TLS provider cannot be added directly into cleanup stack 	
       
    34 void CleanupCTlsProvider(TAny* aCTLSProvider)
       
    35 	{
       
    36 	CTLSProvider * objToDelete=(CTLSProvider*)aCTLSProvider;
       
    37 	delete objToDelete;
       
    38 	}
       
    39 
       
    40 void CleanupCTlsSession(TAny* aCTLSSession)
       
    41 	{
       
    42 	CTLSSession * objToDelete=(CTLSSession*)aCTLSSession;
       
    43 	delete objToDelete;
       
    44 	}	
       
    45 
       
    46 void CleanupSuiteArray(TAny* aArraySuites)
       
    47 	{
       
    48 	RArray<TTLSCipherSuite> * objToDelete=(RArray<TTLSCipherSuite>*)aArraySuites;
       
    49 	objToDelete->Close();
       
    50 	objToDelete = NULL;
       
    51 	}	
       
    52 
       
    53 void CleanupKeyMaterial(TAny* aTestStep)
       
    54 	{
       
    55 	CNewKeyDerivationStep *testStepPointer =(CNewKeyDerivationStep*) aTestStep;
       
    56 
       
    57 	// Cleans and delete item by item.
       
    58 	if( testStepPointer->iClientMacSecret)
       
    59 		{
       
    60 		delete testStepPointer->iClientMacSecret;
       
    61 		testStepPointer->iClientMacSecret = NULL;	
       
    62 		}
       
    63 
       
    64 	if( testStepPointer->iServerMacSecret)
       
    65 		{
       
    66 		delete testStepPointer->iServerMacSecret;
       
    67 		testStepPointer->iServerMacSecret = NULL;	
       
    68 		}
       
    69 
       
    70 	if( testStepPointer->iClientWriteSecret)
       
    71 		{
       
    72 		delete testStepPointer->iClientWriteSecret;
       
    73 		testStepPointer->iClientWriteSecret = NULL;	
       
    74 		}
       
    75 
       
    76 	if( testStepPointer->iServerWriteSecret)
       
    77 		{
       
    78 		delete testStepPointer->iServerWriteSecret;
       
    79 		testStepPointer->iServerWriteSecret = NULL;	
       
    80 		}
       
    81 
       
    82 	if( testStepPointer->iClientInitVector)
       
    83 		{
       
    84 		delete testStepPointer->iClientInitVector;
       
    85 		testStepPointer->iClientInitVector = NULL;	
       
    86 		}
       
    87 
       
    88 	if( testStepPointer->iServerInitVector)
       
    89 		{
       
    90 		delete testStepPointer->iServerInitVector;
       
    91 		testStepPointer->iServerInitVector = NULL;	
       
    92 		}
       
    93 	}		
       
    94 	
       
    95 /************************************************************************
       
    96 *    Note: This test steps currently only supports DH and PSK
       
    97 *          key exchanges, to test additional algorithms the test step
       
    98 *          need to be updated.
       
    99 **************************************************************************/	
       
   100 
       
   101 void CNewKeyDerivationStep::doTestL()
       
   102 	{
       
   103 	TInt err;
       
   104 	iProvider = CTLSProvider::ConnectL();
       
   105 	TCleanupItem cleanupInsProvider(CleanupCTlsProvider, iProvider);
       
   106 	CleanupStack::PushL(cleanupInsProvider);
       
   107 
       
   108 	// cleanup of key material.
       
   109 	TCleanupItem cleanupInsKeyMaterial(CleanupKeyMaterial, this);
       
   110 	CleanupStack::PushL(cleanupInsKeyMaterial);
       
   111 
       
   112 	CTlsCryptoAttributes* atts = iProvider->Attributes();
       
   113 
       
   114 	// sets the "server" random
       
   115 	atts->iMasterSecretInput.iServerRandom.Copy(*iServerRandom);
       
   116 
       
   117 	// Sets the client random
       
   118 	atts->iMasterSecretInput.iClientRandom.Copy(*iClientRandom);
       
   119 
       
   120 	// we only support null compression...
       
   121 	atts->iCompressionMethod = ENullCompression;
       
   122 
       
   123 	// Sets cipher suite for the test. 
       
   124 	atts->iCurrentCipherSuite = iCipherSuite;
       
   125 
       
   126 	// Sets protocol version. 
       
   127 	atts->iNegotiatedProtocol = iProtocolVersion;
       
   128 	atts->iProposedProtocol = iProtocolVersion;
       
   129 
       
   130 	// set the session ID and "server" name (localhost)
       
   131 	atts->iSessionNameAndID.iSessionId = iSessionId;
       
   132 	atts->iSessionNameAndID.iServerName.iAddress = KLocalHost; 
       
   133 	atts->iSessionNameAndID.iServerName.iPort = 443;  
       
   134 	atts->idomainName.Copy(iDomainName);  
       
   135 
       
   136 	// If cipher suite under test uses PSK (Pre Shared Key)
       
   137 	if(UsePsk())
       
   138 		{
       
   139 		// Populates values for PSK 
       
   140 		atts->iPskConfigured = true;
       
   141 		atts->iPublicKeyParams->iKeyType = EPsk;
       
   142 
       
   143 		atts->iPublicKeyParams->iValue4 = PskIdentity()->Alloc(); // = PskIdentity();
       
   144 		atts->iPublicKeyParams->iValue5 = PskKey()->Alloc();
       
   145 
       
   146 		}   
       
   147 	else 
       
   148 		{
       
   149 		if(iUseDHParams)
       
   150 			{
       
   151 
       
   152 			// If cipher suite under test is NOT PSK 
       
   153 			atts->iPublicKeyParams->iKeyType = EDHE;
       
   154 
       
   155 			// The params are:
       
   156 			// 1 - Prime
       
   157 			// 2 - Generator
       
   158 			// 3 - generator ^ random mod prime
       
   159 
       
   160 			atts->iPublicKeyParams->iValue1 = Prime().BufferLC();
       
   161 			CleanupStack::Pop(atts->iPublicKeyParams->iValue1);
       
   162 
       
   163 			atts->iPublicKeyParams->iValue2 = Generator().BufferLC();
       
   164 			CleanupStack::Pop(atts->iPublicKeyParams->iValue2);
       
   165 
       
   166 			atts->iPublicKeyParams->iValue3 = KeyPair()->PublicKey().X().BufferLC();
       
   167 			CleanupStack::Pop(atts->iPublicKeyParams->iValue3);
       
   168 			}
       
   169 		} 
       
   170 
       
   171 	// No client authentication or dialogs for this test, please
       
   172 	atts->iClientAuthenticate = EFalse;
       
   173 	atts->iDialogNonAttendedMode = ETrue;
       
   174 
       
   175 	if(UseNullCipher())
       
   176 		{
       
   177 		// Enables null cipher by setting appropiate parameter  
       
   178 		atts->iAllowNullCipherSuites = ETrue;
       
   179 		}  
       
   180 
       
   181 	err = LeanGetCipherSuitesL();
       
   182 
       
   183 	if (err != KErrNone)
       
   184 		{
       
   185 		User::Leave(iActive->iStatus.Int());
       
   186 		}
       
   187 
       
   188 
       
   189 	TCleanupItem cleanupInsSuitesArray(CleanupSuiteArray,&iSuites);
       
   190 	CleanupStack::PushL(cleanupInsSuitesArray);
       
   191 
       
   192 
       
   193 	// verifies certificate if is not a PSK cipher suite
       
   194 	if( !UsePsk() )
       
   195 		{
       
   196 
       
   197 		// we have to verify the server certificate, to supply the certificate
       
   198 		// and its parameters to the TLS provider.
       
   199 		CX509Certificate* cert = NULL;
       
   200 
       
   201 		err = LeanVerifyServerCertificate(cert, iServerCertificate);
       
   202 		delete cert;
       
   203 
       
   204 		// make sure it completed sucessfully.
       
   205 		if (err != KErrNone)
       
   206 			{
       
   207 			User::Leave(iActive->iStatus.Int());
       
   208 			}
       
   209 		}    	
       
   210 
       
   211 	// now, create a session with the parameters set
       
   212 	err = LeanCreateSession();
       
   213 
       
   214 	// ensure we succeeded
       
   215 	if (err != KErrNone)
       
   216 		{
       
   217 		User::Leave(iActive->iStatus.Int());
       
   218 		}
       
   219 
       
   220 	TCleanupItem cleanupInsTlsSession(CleanupCTlsSession, iSession);
       
   221 	CleanupStack::PushL(cleanupInsTlsSession);
       
   222 
       
   223 	HBufC8* keyExMessage = NULL;
       
   224 	err = LeanClientKeyExchange(keyExMessage);
       
   225 
       
   226 	if (err != KErrNone)
       
   227 		{
       
   228 		User::Leave(iActive->iStatus.Int());
       
   229 		}
       
   230 
       
   231 	CleanupStack::PushL(keyExMessage);   
       
   232 	HBufC8* premaster = LeanDerivePreMasterSecretL(*keyExMessage, iServerPrivateKey);
       
   233 	CleanupStack::PopAndDestroy(keyExMessage);  
       
   234 
       
   235 	// compute the master secret from the premaster.
       
   236 	CleanupStack::PushL(premaster);
       
   237 	HBufC8* master = LeanComputeMasterSecretL(*premaster);
       
   238 	CleanupStack::PopAndDestroy(premaster);
       
   239 	CleanupStack::PushL(master);
       
   240 
       
   241 	// now generate what we think the derived EAP key block should look like.
       
   242 	TBuf8<192> ourEAP;
       
   243 	TBuf8<64> random;
       
   244 	random.Append(atts->iMasterSecretInput.iClientRandom);
       
   245 	random.Append(atts->iMasterSecretInput.iServerRandom);
       
   246 
       
   247 	// make sure we're using TLS. This step makes no sense for SSL 3.0
       
   248 	if (atts->iNegotiatedProtocol.iMajor == 3 && atts->iNegotiatedProtocol.iMinor == 0)
       
   249 		{
       
   250 		INFO_PRINTF1(_L("Error! Cannot use this test step with SSLv3!"));
       
   251 		User::Leave(KErrNotSupported);
       
   252 		}
       
   253 
       
   254 	// compute the 128 byte block that uses the master secret as key.
       
   255 	_LIT8(KEAPEncryptionLabel, "client EAP encryption");
       
   256 	HBufC8* block1 = CTls10PsuedoRandom::PseudoRandomL(*master, KEAPEncryptionLabel, random, 128);
       
   257 	ourEAP.Append(*block1);
       
   258 	delete block1;
       
   259 
       
   260 	// compute the 64 byte IV block
       
   261 	HBufC8* block2 = CTls10PsuedoRandom::PseudoRandomL(KNullDesC8, KEAPEncryptionLabel, random, 64);
       
   262 	ourEAP.Append(*block2);
       
   263 	delete block2;
       
   264 
       
   265 	// get the TLS provider's idea of what the EAP keyblock should be, and check they match.
       
   266 	TBuf8<192> theirEAP;
       
   267 	User::LeaveIfError(iSession->KeyDerivation(KEAPEncryptionLabel, atts->iMasterSecretInput, theirEAP));
       
   268 
       
   269 	if (ourEAP == theirEAP)
       
   270 		{
       
   271 		INFO_PRINTF1(_L("Test passed."));
       
   272 		SetTestStepResult(EPass);
       
   273 		}
       
   274 	else
       
   275 		{
       
   276 		INFO_PRINTF1(_L("Failed! EAP-TLS is corrupt!"));	
       
   277 		SetTestStepResult(EFail);
       
   278 		}  
       
   279 	
       
   280 	CleanupStack::PopAndDestroy(5);
       
   281 
       
   282 	}
       
   283