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