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