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