os/security/crypto/weakcryptospi/source/spi/cryptoparams.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200 (2014-06-10)
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
/*
sl@0
     2
* Copyright (c) 2006-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
* Generic crypto parameter implementation
sl@0
    16
* Generic crypto parameter implementation
sl@0
    17
*
sl@0
    18
*/
sl@0
    19
sl@0
    20
sl@0
    21
/**
sl@0
    22
 @file
sl@0
    23
*/
sl@0
    24
sl@0
    25
#include <cryptospi/cryptoparams.h>
sl@0
    26
sl@0
    27
using namespace CryptoSpi;
sl@0
    28
sl@0
    29
//
sl@0
    30
//Implementation of the generic crypto parameter
sl@0
    31
//
sl@0
    32
CCryptoParam::CCryptoParam(TParamType aType, TUid aUid)
sl@0
    33
	: iType(aType),
sl@0
    34
	  iUid(aUid)
sl@0
    35
	{
sl@0
    36
	}
sl@0
    37
sl@0
    38
EXPORT_C CCryptoParam::~CCryptoParam()
sl@0
    39
	{
sl@0
    40
		
sl@0
    41
	}
sl@0
    42
sl@0
    43
EXPORT_C TInt CCryptoParam::Type() const
sl@0
    44
	{
sl@0
    45
	return iType;
sl@0
    46
	}
sl@0
    47
sl@0
    48
EXPORT_C TUid CCryptoParam::Uid() const
sl@0
    49
	{
sl@0
    50
	return iUid;
sl@0
    51
	}
sl@0
    52
sl@0
    53
//
sl@0
    54
// Implementation of Descriptor parameters
sl@0
    55
//
sl@0
    56
EXPORT_C CCryptoDesC8Param* CCryptoDesC8Param::NewL(const TDesC8& aValue, TUid aUid)
sl@0
    57
	{
sl@0
    58
	CCryptoDesC8Param* self=NewLC(aValue, aUid);
sl@0
    59
	CleanupStack::Pop();
sl@0
    60
	return self;
sl@0
    61
	}
sl@0
    62
sl@0
    63
EXPORT_C CCryptoDesC8Param* CCryptoDesC8Param::NewLC(const TDesC8& aValue, TUid aUid)
sl@0
    64
	{
sl@0
    65
	CCryptoDesC8Param* self=new(ELeave) CCryptoDesC8Param(aUid);
sl@0
    66
	CleanupStack::PushL(self);
sl@0
    67
	self->ConstructL(aValue);
sl@0
    68
	return self;
sl@0
    69
	}
sl@0
    70
sl@0
    71
EXPORT_C const TDesC8& CCryptoDesC8Param::Value() const
sl@0
    72
	{
sl@0
    73
	return *iValue;
sl@0
    74
	}
sl@0
    75
sl@0
    76
EXPORT_C CCryptoDesC8Param::~CCryptoDesC8Param()
sl@0
    77
	{
sl@0
    78
	if (iValue)
sl@0
    79
		{
sl@0
    80
		iValue->Des().FillZ();
sl@0
    81
		}
sl@0
    82
	delete iValue;
sl@0
    83
	}
sl@0
    84
sl@0
    85
CCryptoDesC8Param::CCryptoDesC8Param(TUid aUid)
sl@0
    86
	: CCryptoParam(EDesC8, aUid)
sl@0
    87
	{
sl@0
    88
	}
sl@0
    89
	
sl@0
    90
void CCryptoDesC8Param::ConstructL(const TDesC8& aValue)
sl@0
    91
	{
sl@0
    92
	iValue=aValue.AllocL();
sl@0
    93
	}
sl@0
    94
sl@0
    95
sl@0
    96
//
sl@0
    97
// Implementation of Descriptor parameters
sl@0
    98
//
sl@0
    99
EXPORT_C CCryptoDesC16Param* CCryptoDesC16Param::NewL(const TDesC16& aValue, TUid aUid)
sl@0
   100
	{
sl@0
   101
	CCryptoDesC16Param* self=NewLC(aValue, aUid);
sl@0
   102
	CleanupStack::Pop();
sl@0
   103
	return self;
sl@0
   104
	}
sl@0
   105
sl@0
   106
EXPORT_C CCryptoDesC16Param* CCryptoDesC16Param::NewLC(const TDesC16& aValue, TUid aUid)
sl@0
   107
	{
sl@0
   108
	CCryptoDesC16Param* self=new(ELeave) CCryptoDesC16Param(aUid);
sl@0
   109
	CleanupStack::PushL(self);
sl@0
   110
	self->ConstructL(aValue);
sl@0
   111
	return self;
sl@0
   112
	}
sl@0
   113
sl@0
   114
EXPORT_C const TDesC16& CCryptoDesC16Param::Value() const
sl@0
   115
	{
sl@0
   116
	return *iValue;
sl@0
   117
	}
sl@0
   118
sl@0
   119
EXPORT_C CCryptoDesC16Param::~CCryptoDesC16Param()
sl@0
   120
	{
sl@0
   121
	if (iValue)
sl@0
   122
		{
sl@0
   123
		iValue->Des().FillZ();
sl@0
   124
		}
sl@0
   125
	delete iValue;
sl@0
   126
	}
sl@0
   127
sl@0
   128
CCryptoDesC16Param::CCryptoDesC16Param(TUid aUid)
sl@0
   129
	: CCryptoParam(EDesC16, aUid)
sl@0
   130
	{
sl@0
   131
	}
sl@0
   132
	
sl@0
   133
void CCryptoDesC16Param::ConstructL(const TDesC16& aValue)
sl@0
   134
	{
sl@0
   135
	iValue=aValue.AllocL();
sl@0
   136
	}
sl@0
   137
sl@0
   138
//
sl@0
   139
// Implementation of int parameters
sl@0
   140
//
sl@0
   141
EXPORT_C CCryptoIntParam* CCryptoIntParam::NewL(TInt aValue, TUid aUid)
sl@0
   142
	{
sl@0
   143
	return new(ELeave) CCryptoIntParam(aValue, aUid);
sl@0
   144
	}
sl@0
   145
sl@0
   146
EXPORT_C CCryptoIntParam* CCryptoIntParam::NewLC(TInt aValue, TUid aUid)
sl@0
   147
	{
sl@0
   148
	CCryptoIntParam* self=new(ELeave) CCryptoIntParam(aValue, aUid);
sl@0
   149
	CleanupStack::PushL(self);
sl@0
   150
	return self;
sl@0
   151
	}
sl@0
   152
sl@0
   153
EXPORT_C TInt CCryptoIntParam::Value() const
sl@0
   154
	{
sl@0
   155
	return iValue;
sl@0
   156
	}
sl@0
   157
sl@0
   158
EXPORT_C CCryptoIntParam::~CCryptoIntParam()
sl@0
   159
	{
sl@0
   160
	iValue = 0;
sl@0
   161
	}
sl@0
   162
sl@0
   163
CCryptoIntParam::CCryptoIntParam(TInt aValue, TUid aUid)
sl@0
   164
	: CCryptoParam(EInt, aUid),
sl@0
   165
	  iValue(aValue)
sl@0
   166
	{
sl@0
   167
		
sl@0
   168
	}
sl@0
   169
sl@0
   170
//	
sl@0
   171
// Implementation of BigInt parameters
sl@0
   172
//
sl@0
   173
EXPORT_C CCryptoBigIntParam* CCryptoBigIntParam::NewL(const TInteger& aValue, TUid aUid)
sl@0
   174
	{
sl@0
   175
	CCryptoBigIntParam* self = NewLC(aValue, aUid);
sl@0
   176
	CleanupStack::Pop();
sl@0
   177
	return self;
sl@0
   178
	}
sl@0
   179
sl@0
   180
EXPORT_C CCryptoBigIntParam* CCryptoBigIntParam::NewLC(const TInteger& aValue, TUid aUid)
sl@0
   181
	{
sl@0
   182
	CCryptoBigIntParam* self = new(ELeave) CCryptoBigIntParam(aUid);
sl@0
   183
	CleanupStack::PushL(self);
sl@0
   184
	self->ConstructL(aValue);
sl@0
   185
	return self;
sl@0
   186
	}
sl@0
   187
sl@0
   188
EXPORT_C const TInteger& CCryptoBigIntParam::Value() const
sl@0
   189
	{
sl@0
   190
	return iValue;
sl@0
   191
	}
sl@0
   192
sl@0
   193
EXPORT_C CCryptoBigIntParam::~CCryptoBigIntParam()
sl@0
   194
	{
sl@0
   195
	// Secure delete, BigInts used for RSA/DSA modulus and exponent
sl@0
   196
	iValue.Close();
sl@0
   197
	}
sl@0
   198
sl@0
   199
CCryptoBigIntParam::CCryptoBigIntParam(TUid aUid)
sl@0
   200
	: CCryptoParam(EBigInt, aUid)
sl@0
   201
	{
sl@0
   202
	}
sl@0
   203
	
sl@0
   204
void CCryptoBigIntParam::ConstructL(const TInteger& aValue)
sl@0
   205
	{
sl@0
   206
	iValue = RInteger::NewL(aValue);
sl@0
   207
	}
sl@0
   208
sl@0
   209
/*
sl@0
   210
 * CCryptoParams implementation
sl@0
   211
 */
sl@0
   212
EXPORT_C CCryptoParams* CCryptoParams::NewL(void)
sl@0
   213
	{
sl@0
   214
	CCryptoParams* self = NewLC();
sl@0
   215
	CleanupStack::Pop();
sl@0
   216
	return self;
sl@0
   217
	}
sl@0
   218
sl@0
   219
EXPORT_C CCryptoParams* CCryptoParams::NewLC(void)
sl@0
   220
	{
sl@0
   221
	CCryptoParams* self = new(ELeave) CCryptoParams();
sl@0
   222
	CleanupStack::PushL(self);
sl@0
   223
	self->ConstructL();
sl@0
   224
	return self;
sl@0
   225
	}
sl@0
   226
sl@0
   227
CCryptoParams::CCryptoParams()
sl@0
   228
	{
sl@0
   229
	}
sl@0
   230
sl@0
   231
void CCryptoParams::ConstructL(void)
sl@0
   232
	{
sl@0
   233
	}
sl@0
   234
sl@0
   235
EXPORT_C CCryptoParams::~CCryptoParams()
sl@0
   236
	{
sl@0
   237
	// delete all contained parameters
sl@0
   238
	iParams.ResetAndDestroy();
sl@0
   239
	}
sl@0
   240
sl@0
   241
EXPORT_C void CCryptoParams::AddL(const TInteger& aParam, TUid aUid)
sl@0
   242
	{
sl@0
   243
	CCryptoBigIntParam* param = CCryptoBigIntParam::NewLC(aParam, aUid);
sl@0
   244
	iParams.AppendL(param);
sl@0
   245
	CleanupStack::Pop(param);
sl@0
   246
	}
sl@0
   247
sl@0
   248
EXPORT_C void CCryptoParams::AddL(const TInt aParam, TUid aUid)
sl@0
   249
	{
sl@0
   250
	CCryptoIntParam* param = CCryptoIntParam::NewLC(aParam, aUid);
sl@0
   251
	iParams.AppendL(param);
sl@0
   252
	CleanupStack::Pop(param);
sl@0
   253
	}
sl@0
   254
sl@0
   255
EXPORT_C void CCryptoParams::AddL(const TDesC8& aParam, TUid aUid)
sl@0
   256
	{
sl@0
   257
	CCryptoDesC8Param* param = CCryptoDesC8Param::NewLC(aParam, aUid);
sl@0
   258
	iParams.AppendL(param);
sl@0
   259
	CleanupStack::Pop(param);
sl@0
   260
	}
sl@0
   261
sl@0
   262
EXPORT_C void CCryptoParams::AddL(const TDesC16& aParam, TUid aUid)
sl@0
   263
	{
sl@0
   264
	CCryptoDesC16Param* param = CCryptoDesC16Param::NewLC(aParam, aUid);
sl@0
   265
	iParams.AppendL(param);
sl@0
   266
	CleanupStack::Pop(param);
sl@0
   267
	}
sl@0
   268
sl@0
   269
EXPORT_C const TInteger& CCryptoParams::GetBigIntL(TUid aUid) const
sl@0
   270
	{
sl@0
   271
	TInteger* paramValue = NULL;
sl@0
   272
	CCryptoParam* param = GetCryptoParamL(aUid);
sl@0
   273
	if (param->Type() == CCryptoParam::EBigInt)
sl@0
   274
		{
sl@0
   275
		const CCryptoBigIntParam* typedParam = static_cast<const CCryptoBigIntParam*>(param);
sl@0
   276
		paramValue = const_cast<TInteger*>(&typedParam->Value());
sl@0
   277
		}
sl@0
   278
	else
sl@0
   279
		{
sl@0
   280
		User::Leave(KErrArgument);
sl@0
   281
		}
sl@0
   282
		return *paramValue;
sl@0
   283
	}
sl@0
   284
sl@0
   285
EXPORT_C TInt CCryptoParams::GetTIntL(TUid aUid) const
sl@0
   286
	{
sl@0
   287
	TInt paramValue = 0;
sl@0
   288
	CCryptoParam* param = GetCryptoParamL(aUid);
sl@0
   289
	if (param->Type() == CCryptoParam::EInt)
sl@0
   290
		{
sl@0
   291
		const CCryptoIntParam* typedParam = static_cast<const CCryptoIntParam*>(param);
sl@0
   292
		paramValue = typedParam->Value();
sl@0
   293
		}
sl@0
   294
	else
sl@0
   295
		{
sl@0
   296
		User::Leave(KErrArgument);
sl@0
   297
		}
sl@0
   298
		return paramValue;
sl@0
   299
	}
sl@0
   300
sl@0
   301
EXPORT_C const TDesC8& CCryptoParams::GetTDesC8L(TUid aUid) const
sl@0
   302
	{
sl@0
   303
	TDesC8* paramValue = NULL;
sl@0
   304
	CCryptoParam* param = GetCryptoParamL(aUid);
sl@0
   305
	if (param->Type() == CCryptoParam::EDesC8)
sl@0
   306
		{
sl@0
   307
		const CCryptoDesC8Param* typedParam = static_cast<const CCryptoDesC8Param*>(param);
sl@0
   308
		paramValue = const_cast<TDesC8*>(&typedParam->Value());
sl@0
   309
		}
sl@0
   310
	else
sl@0
   311
		{
sl@0
   312
		User::Leave(KErrArgument);
sl@0
   313
		}
sl@0
   314
		return *paramValue;
sl@0
   315
	}
sl@0
   316
sl@0
   317
EXPORT_C const TDesC16& CCryptoParams::GetTDesC16L(TUid aUid) const
sl@0
   318
	{
sl@0
   319
	TDesC16* paramValue = NULL;
sl@0
   320
	CCryptoParam* param = GetCryptoParamL(aUid);
sl@0
   321
	if (param->Type() == CCryptoParam::EDesC16)
sl@0
   322
		{
sl@0
   323
		const CCryptoDesC16Param* typedParam = static_cast<const CCryptoDesC16Param*>(param);
sl@0
   324
		paramValue = const_cast<TDesC16*>(&typedParam->Value());
sl@0
   325
		}
sl@0
   326
	else
sl@0
   327
		{
sl@0
   328
		User::Leave(KErrArgument);
sl@0
   329
		}
sl@0
   330
		return *paramValue;
sl@0
   331
	}
sl@0
   332
sl@0
   333
EXPORT_C TBool CCryptoParams::IsPresent(TUid aUid) const
sl@0
   334
	{
sl@0
   335
	TBool ret = EFalse;
sl@0
   336
	if (GetCryptoParam(aUid))
sl@0
   337
		{
sl@0
   338
		ret = ETrue;
sl@0
   339
		}
sl@0
   340
	return ret;
sl@0
   341
	}
sl@0
   342
sl@0
   343
CCryptoParam* CCryptoParams::GetCryptoParam(TUid aUid) const
sl@0
   344
	{
sl@0
   345
	CCryptoParam* paramPtr = NULL;
sl@0
   346
	TInt count = iParams.Count();
sl@0
   347
	for (TInt i = 0 ;i < count; i++)
sl@0
   348
		{
sl@0
   349
		if (iParams[i]->Uid() == aUid)
sl@0
   350
			{
sl@0
   351
			paramPtr = iParams[i];
sl@0
   352
			break;
sl@0
   353
			}
sl@0
   354
		}
sl@0
   355
	return paramPtr;
sl@0
   356
	}
sl@0
   357
	
sl@0
   358
CCryptoParam* CCryptoParams::GetCryptoParamL(TUid aUid) const
sl@0
   359
	{
sl@0
   360
	CCryptoParam* paramPtr = GetCryptoParam(aUid);
sl@0
   361
	// leave if requested uid was not found
sl@0
   362
	if (!paramPtr)
sl@0
   363
		{
sl@0
   364
		User::Leave(KErrArgument);
sl@0
   365
		}
sl@0
   366
	return paramPtr;
sl@0
   367
	}
sl@0
   368
sl@0
   369
EXPORT_C CCryptoParams& CCryptoParams::CopyL(const CCryptoParams& aParams)
sl@0
   370
	{
sl@0
   371
	iParams.Close();
sl@0
   372
	TUint count = aParams.iParams.Count();
sl@0
   373
	for (TUint num = 0; num < count; num++)
sl@0
   374
		{
sl@0
   375
		CCryptoParam* item = aParams.iParams[num];
sl@0
   376
sl@0
   377
		// Stop armv5 compiler warning about init through switch statement
sl@0
   378
		CCryptoBigIntParam *b = 0;
sl@0
   379
		CCryptoDesC8Param *d = 0;
sl@0
   380
		CCryptoDesC16Param *d16 = 0;
sl@0
   381
		CCryptoIntParam *i = 0;
sl@0
   382
sl@0
   383
		// For each type of cryptoparam, duplicate it, and append to RPtrArray
sl@0
   384
		switch (item->Type())
sl@0
   385
			{
sl@0
   386
			case CCryptoParam::EBigInt:
sl@0
   387
				b = CCryptoBigIntParam::NewL(((CCryptoBigIntParam*) item)->Value(), ((CCryptoBigIntParam*) item)->Uid());
sl@0
   388
				CleanupStack::PushL(b);
sl@0
   389
				iParams.AppendL(b);
sl@0
   390
				CleanupStack::Pop(b);
sl@0
   391
				break;
sl@0
   392
				
sl@0
   393
			case CCryptoParam::EInt:
sl@0
   394
				i = CCryptoIntParam::NewL(((CCryptoIntParam*) item)->Value(), ((CCryptoBigIntParam*) item)->Uid());
sl@0
   395
				CleanupStack::PushL(i);
sl@0
   396
				iParams.AppendL(i);
sl@0
   397
				CleanupStack::Pop(i);
sl@0
   398
				break;
sl@0
   399
				
sl@0
   400
			case CCryptoParam::EDesC8:
sl@0
   401
				d = CCryptoDesC8Param::NewL(((CCryptoDesC8Param*) item)->Value(), ((CCryptoBigIntParam*) item)->Uid());
sl@0
   402
				CleanupStack::PushL(d);
sl@0
   403
				iParams.AppendL(d);
sl@0
   404
				CleanupStack::Pop(d);
sl@0
   405
				break;
sl@0
   406
				
sl@0
   407
			case CCryptoParam::EDesC16:
sl@0
   408
				d16 = CCryptoDesC16Param::NewL(((CCryptoDesC16Param*) item)->Value(), ((CCryptoBigIntParam*) item)->Uid());
sl@0
   409
				CleanupStack::PushL(d16);
sl@0
   410
				iParams.AppendL(d16);
sl@0
   411
				CleanupStack::Pop(d16);
sl@0
   412
				break;
sl@0
   413
				
sl@0
   414
			default:
sl@0
   415
				break;
sl@0
   416
			}
sl@0
   417
		}
sl@0
   418
		return *this;
sl@0
   419
	}
sl@0
   420
sl@0
   421
EXPORT_C TInt CCryptoParams::Count(void) const
sl@0
   422
	{
sl@0
   423
	return iParams.Count();
sl@0
   424
	}
sl@0
   425
sl@0
   426
EXPORT_C const RPointerArray<CCryptoParam>& CCryptoParams::GetParams() const
sl@0
   427
	{
sl@0
   428
	return iParams;
sl@0
   429
	}
sl@0
   430