crypto/weakcrypto/test/tpbe/tactionset.cpp
changeset 71 dd83586b62d6
equal deleted inserted replaced
66:8873e6835f7b 71:dd83586b62d6
       
     1 /*
       
     2 * Copyright (c) 1998-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 the License "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: 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <pbedata.h>
       
    20 #include <stdlib.h>
       
    21 #include <s32mem.h>
       
    22 #include <s32std.h>
       
    23 #include "tpbe.h"
       
    24 #include "tactionset.h"
       
    25 #include "t_input.h"
       
    26 
       
    27 _LIT8(KSetStart, "<set>");
       
    28 _LIT8(KSetEnd, "</set>");
       
    29 
       
    30 CTestAction* CActionSet::NewL(RFs& aFs,
       
    31 									   CConsoleBase& aConsole,
       
    32 									   Output& aOut, 
       
    33 									   const TTestActionSpec& aTestActionSpec)
       
    34 	{
       
    35 	CTestAction* self = CActionSet::NewLC(aFs, aConsole,
       
    36 		aOut, aTestActionSpec);
       
    37 	CleanupStack::Pop();
       
    38 	return self;
       
    39 	}
       
    40 
       
    41 CTestAction* CActionSet::NewLC(RFs& aFs,
       
    42 										CConsoleBase& aConsole,
       
    43 										Output& aOut, 
       
    44 										const TTestActionSpec& aTestActionSpec)
       
    45 	{
       
    46 	CActionSet* self = new(ELeave) CActionSet(aFs, aConsole, aOut);
       
    47 	CleanupStack::PushL(self);
       
    48 	self->ConstructL(aTestActionSpec);
       
    49 	return self;
       
    50 	}
       
    51 
       
    52 CActionSet::~CActionSet()
       
    53 	{
       
    54 	delete iBody;
       
    55 	}
       
    56 
       
    57 CActionSet::CActionSet(RFs& aFs, 
       
    58 								 CConsoleBase& aConsole,
       
    59 								 Output& aOut)
       
    60 								 
       
    61 : CTestAction(aConsole, aOut), iFs(aFs)
       
    62 	{
       
    63 	}
       
    64 
       
    65 void CActionSet::ConstructL(const TTestActionSpec& aTestActionSpec)
       
    66 	{
       
    67 	CTestAction::ConstructL(aTestActionSpec);
       
    68 	iBody = HBufC8::NewL(aTestActionSpec.iActionBody.Length());
       
    69 	iBody->Des().Copy(aTestActionSpec.iActionBody);
       
    70 	
       
    71 	}
       
    72 
       
    73 void CActionSet::DoPerformPrerequisite(TRequestStatus& aStatus)
       
    74 	{
       
    75 	TRequestStatus* status = &aStatus;
       
    76 	TInt err = KErrNone;
       
    77 	TInt pos = 0;
       
    78 	TPtrC8 encryptElement = Input::ParseElement(*iBody, KSetStart,
       
    79 		KSetEnd, pos, err);
       
    80 	pos = 0;
       
    81 	TPtrC8 kdf = Input::ParseElement(*iBody, KKdfStart, KKdfEnd, pos, err);
       
    82 	if (err == KErrNone)
       
    83 		iKdf = kdf.AllocL();
       
    84 	
       
    85 	pos = 0;
       
    86 	TPtrC8 saltLenBytes = Input::ParseElement(*iBody, KSaltLenBytesStart, KSaltLenBytesEnd, pos, err);
       
    87 	if (err == KErrNone)
       
    88 		iSaltLenBytes = saltLenBytes.AllocL();
       
    89 	
       
    90 	pos = 0;
       
    91 	TPtrC8 iterCount = Input::ParseElement(*iBody, KIterCountStart, KIterCountEnd, pos, err);
       
    92 	if (err == KErrNone)
       
    93 		iIterCount = iterCount.AllocL();
       
    94 	
       
    95 	pos = 0;
       
    96 	TPtrC8 passwdTemp = Input::ParseElement(encryptElement, KPasswdStart, 
       
    97 		KPasswdEnd, pos, err);
       
    98 	iPasswd = HBufC::NewL(passwdTemp.Length());
       
    99 	TPtr16 passwdTemp3( iPasswd->Des());
       
   100 	passwdTemp3.Copy(passwdTemp);
       
   101 
       
   102 	pos = 0;
       
   103 	TPtrC8 inputTemp = Input::ParseElement(encryptElement, KInputStart, 
       
   104 		KInputEnd, pos, err);
       
   105 	iInput = HBufC8::NewL(inputTemp.Length());
       
   106 	*iInput = inputTemp;
       
   107 
       
   108 	TPtrC8 cipher = Input::ParseElement(*iBody, KCipherStart, KCipherEnd);
       
   109 	if (cipher.Compare(KECipherAES_CBC_128) == 0)
       
   110 	{
       
   111 	   iCipher = ECipherAES_CBC_128;
       
   112 	}
       
   113 	else if (cipher.Compare(KECipherAES_CBC_192) == 0)
       
   114 	{
       
   115 	   iCipher = ECipherAES_CBC_192;
       
   116 	}
       
   117 	else if (cipher.Compare(KECipherAES_CBC_256) == 0)
       
   118 	{
       
   119 	   iCipher = ECipherAES_CBC_256;
       
   120 	}
       
   121 	else if (cipher.Compare(KECipherDES_CBC) == 0)
       
   122 	{
       
   123 	   iCipher = ECipherDES_CBC;
       
   124 	}
       
   125 	else if (cipher.Compare(KECipher3DES_CBC) == 0)
       
   126 	{
       
   127 	   iCipher = ECipher3DES_CBC;
       
   128 	}
       
   129 	else if (cipher.Compare(KECipherRC2_CBC_40) == 0)
       
   130 	{
       
   131 	   iCipher = ECipherRC2_CBC_40;
       
   132 	}
       
   133 	else if (cipher.Compare(KECipherRC2_CBC_128) == 0)
       
   134 	{
       
   135 	   iCipher = ECipherRC2_CBC_128;
       
   136 	}
       
   137 	else if (cipher.Compare(KECipherRC2_CBC_40_16) == 0)
       
   138 	{
       
   139 	   iCipher = ECipherRC2_CBC_40_16;
       
   140 	}
       
   141 	else if (cipher.Compare(KECipherRC2_CBC_128_16) == 0)
       
   142 	{
       
   143 	   iCipher = ECipherRC2_CBC_128_16;
       
   144 	}
       
   145 	else if(cipher.Compare(KECipher2Key3DES_CBC) == 0)
       
   146 	{
       
   147 		iCipher = ECipher2Key3DES_CBC;
       
   148 	}
       
   149 	else if(cipher.Compare(KECipherRC2_CBC_40_5) == 0)
       
   150 	{
       
   151 		iCipher = ECipherRC2_CBC_40_5;
       
   152 	}	
       
   153 	else
       
   154 	{
       
   155 	   iCipher = ECipherAES_CBC_128; // Default value if the <cipher> tag is missing
       
   156 	}
       
   157 
       
   158 	User::RequestComplete(status, KErrNone);
       
   159 	iActionState = CTestAction::EAction;
       
   160 	}
       
   161 
       
   162 void CActionSet::DoPerformPostrequisite(TRequestStatus& aStatus)
       
   163 	{
       
   164 	TRequestStatus* status = &aStatus;
       
   165 	delete iPasswd;
       
   166 	delete iInput;
       
   167 	delete iKdf;
       
   168 	iKdf = 0;
       
   169 	delete iSaltLenBytes;
       
   170 	iSaltLenBytes = 0;
       
   171 	delete iIterCount;
       
   172 	iIterCount = 0;
       
   173 
       
   174 	iFinished = ETrue;
       
   175 	User::RequestComplete(status, KErrNone);
       
   176 	}
       
   177 
       
   178 void CActionSet::DoReportAction(void)
       
   179 	{
       
   180 	}
       
   181 
       
   182 void CActionSet::DoCheckResult(TInt)
       
   183 	{
       
   184 
       
   185 	}
       
   186 
       
   187 void CActionSet::PerformAction(TRequestStatus& aStatus)
       
   188 	{
       
   189 	__UHEAP_MARK;
       
   190 	TRequestStatus* status = &aStatus;
       
   191 	iResult = EFalse;
       
   192 	HBufC8* pkcs12Pwd = 0;
       
   193 	
       
   194 	// default value is NULL to avoid RVCT warning
       
   195 	// C2874W: set may be used before being set
       
   196 	CPBEncryptSet* set = 0;
       
   197 	if (iKdf == 0)
       
   198 		{
       
   199 		CleanupStack::PushL(pkcs12Pwd);
       
   200  		set = CPBEncryptSet::NewLC(*iPasswd, iCipher);
       
   201  		}
       
   202 	else
       
   203 		{
       
   204 		// if supply KDF, must also supply salt len and iteration count
       
   205 		ASSERT(iSaltLenBytes != 0 && iIterCount != 0);
       
   206 		
       
   207 		CPBEncryptParms* ep = CPBEncryptParms::NewLC();
       
   208 		
       
   209 		ep->SetCipher(iCipher);
       
   210 		
       
   211 		TInt saltLenBytes;
       
   212 		TInt r = TLex8(*iSaltLenBytes).Val(saltLenBytes);
       
   213 		ASSERT(r == KErrNone);
       
   214 		ep->ResizeSaltL(saltLenBytes);
       
   215 		
       
   216 		TInt iterCount;
       
   217 		r = TLex8(*iIterCount).Val(iterCount);
       
   218 		ASSERT(r == KErrNone);
       
   219 		ep->SetIterations(iterCount);
       
   220 		
       
   221 		CleanupStack::PushL((CBase*)0);
       
   222 		CleanupStack::Pop((CBase*)0);
       
   223 
       
   224 		if (*iKdf == _L8("PKCS#5"))
       
   225 			{
       
   226 			ep->SetKdf(CPBEncryptParms::EKdfPkcs5);
       
   227 			set = CPBEncryptSet::NewL(*iPasswd, *ep);
       
   228 			}
       
   229 		else if (*iKdf == _L8("PKCS#12"))
       
   230 			{
       
   231 			pkcs12Pwd = PKCS12KDF::GeneratePasswordLC(*iPasswd);
       
   232 			ep->SetKdf(CPBEncryptParms::EKdfPkcs12);
       
   233 			set = CPBEncryptSet::NewL(*pkcs12Pwd, *ep);
       
   234 			CleanupStack::Pop(pkcs12Pwd);
       
   235 			}
       
   236 		else
       
   237 			User::Panic(_L("Unrec KDF"), 0);
       
   238 		
       
   239 		CleanupStack::PopAndDestroy(ep);
       
   240 		// encryption could leak here, but for reservation above
       
   241 		CleanupStack::PushL(pkcs12Pwd);
       
   242 		CleanupStack::PushL(set);
       
   243 		}
       
   244 	CPBEncryptor* encryptor = set->NewEncryptLC();
       
   245 	HBufC8* ciphertextTemp = HBufC8::NewLC(encryptor->MaxFinalOutputLength(iInput->Length())); 
       
   246 
       
   247 	TPtr8 ciphertext = ciphertextTemp->Des();	
       
   248 	encryptor->ProcessFinalL(*iInput, ciphertext);
       
   249 	TBuf<128> newPwdTemp(*iPasswd);
       
   250 	newPwdTemp.Append('a');
       
   251 
       
   252 	TBuf8<128> newPwdTemp8;
       
   253 	
       
   254 	TPBPassword newPassword(KNullDesC);
       
   255 	if (pkcs12Pwd == 0)
       
   256 		new(&newPassword) TPBPassword(newPwdTemp);
       
   257 	else
       
   258 		{
       
   259 		HBufC8* newPwd = PKCS12KDF::GeneratePasswordLC(newPwdTemp);
       
   260 		newPwdTemp8.Copy(*newPwd);
       
   261 		new(&newPassword) TPBPassword(newPwdTemp8);
       
   262 		CleanupStack::PopAndDestroy(newPwd);
       
   263 		}
       
   264 
       
   265 	set->ChangePasswordL(newPassword);
       
   266 	
       
   267 	//create a mem  buffer store
       
   268 	CBufStore* store = CBufStore::NewLC(100);
       
   269 	RStoreWriteStream write;
       
   270 
       
   271 	//write the encrypted master key to a stream
       
   272 	TStreamId keyStreamId = write.CreateLC(*store);
       
   273 	write << set->EncryptedMasterKey();
       
   274 	write.CommitL();
       
   275 	CleanupStack::PopAndDestroy(); //CreateLC()
       
   276 
       
   277 	//write the encryption data to another stream
       
   278 	TStreamId dataStreamId = write.CreateLC(*store);
       
   279 	set->EncryptionData().ExternalizeL(write);
       
   280 	write.CommitL();
       
   281 	CleanupStack::PopAndDestroy(); //CreateLC()
       
   282 
       
   283 	//prepare to read the streams back in, creating a new TPBEncryptionData
       
   284 	RStoreReadStream read;
       
   285 	read.OpenLC(*store, dataStreamId);
       
   286 
       
   287 	//read in Encryption data
       
   288 	CPBEncryptionData* data = CPBEncryptionData::NewL(read);
       
   289 	CleanupStack::PopAndDestroy(); //OpenLC()
       
   290 	CleanupStack::PushL(data);
       
   291 
       
   292 	//read in encrypted master key
       
   293 	read.OpenLC(*store, keyStreamId);
       
   294 	HBufC8* encryptedMasterKey = HBufC8::NewLC(read, 10000); //some large number
       
   295 
       
   296 	//create a new set encryption class
       
   297 	CPBEncryptSet* set2 = CPBEncryptSet::NewLC(*data, *encryptedMasterKey, newPassword);
       
   298 
       
   299 	HBufC8* plaintextTemp = HBufC8::NewLC(ciphertext.Length());
       
   300 	TPtr8 plaintext = plaintextTemp->Des();	
       
   301 
       
   302 	CPBDecryptor* decryptor = set2->NewDecryptLC();
       
   303 	decryptor->Process(ciphertext, plaintext);
       
   304 
       
   305 	//this Mid call is due to get rid of the decrypted padding at the end
       
   306 	if(plaintext.Mid(0,iInput->Length()) == *iInput)
       
   307 		{
       
   308 		iResult = ETrue;			
       
   309 		}
       
   310 	
       
   311 	CleanupStack::PopAndDestroy(decryptor);
       
   312 	CleanupStack::PopAndDestroy(plaintextTemp);
       
   313 	CleanupStack::PopAndDestroy(set2);
       
   314 	CleanupStack::PopAndDestroy(encryptedMasterKey);
       
   315 	CleanupStack::PopAndDestroy(1); //OpenLC
       
   316 	CleanupStack::PopAndDestroy(data);
       
   317 	CleanupStack::PopAndDestroy(store);
       
   318 	CleanupStack::PopAndDestroy(ciphertextTemp);
       
   319 	CleanupStack::PopAndDestroy(encryptor);
       
   320 	CleanupStack::PopAndDestroy(set);
       
   321 	CleanupStack::PopAndDestroy(pkcs12Pwd);
       
   322 	
       
   323 	User::RequestComplete(status, KErrNone);
       
   324 	iActionState = CTestAction::EPostrequisite;
       
   325 	__UHEAP_MARKEND;
       
   326 	}
       
   327 
       
   328 void CActionSet::Hex(HBufC8& aString)
       
   329     {
       
   330     TPtr8 ptr=aString.Des();
       
   331     if (aString.Length()%2)
       
   332         {
       
   333         ptr.SetLength(0);
       
   334         return;
       
   335         }
       
   336     TInt i;
       
   337     for (i=0;i<aString.Length();i+=2)
       
   338         {
       
   339         TUint8 tmp;
       
   340         tmp=(TUint8)(aString[i]-(aString[i]>'9'?('A'-10):'0'));
       
   341         tmp*=16;
       
   342         tmp|=(TUint8)(aString[i+1]-(aString[i+1]>'9'?('A'-10):'0'));
       
   343         ptr[i/2]=tmp;
       
   344         }
       
   345     ptr.SetLength(aString.Length()/2);
       
   346     }