Update contrib.
2 * Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
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".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
15 * Generic crypto parameter implementation
16 * Generic crypto parameter implementation
25 #include <cryptospi/cryptoparams.h>
27 using namespace CryptoSpi;
30 //Implementation of the generic crypto parameter
32 CCryptoParam::CCryptoParam(TParamType aType, TUid aUid)
38 EXPORT_C CCryptoParam::~CCryptoParam()
43 EXPORT_C TInt CCryptoParam::Type() const
48 EXPORT_C TUid CCryptoParam::Uid() const
54 // Implementation of Descriptor parameters
56 EXPORT_C CCryptoDesC8Param* CCryptoDesC8Param::NewL(const TDesC8& aValue, TUid aUid)
58 CCryptoDesC8Param* self=NewLC(aValue, aUid);
63 EXPORT_C CCryptoDesC8Param* CCryptoDesC8Param::NewLC(const TDesC8& aValue, TUid aUid)
65 CCryptoDesC8Param* self=new(ELeave) CCryptoDesC8Param(aUid);
66 CleanupStack::PushL(self);
67 self->ConstructL(aValue);
71 EXPORT_C const TDesC8& CCryptoDesC8Param::Value() const
76 EXPORT_C CCryptoDesC8Param::~CCryptoDesC8Param()
80 iValue->Des().FillZ();
85 CCryptoDesC8Param::CCryptoDesC8Param(TUid aUid)
86 : CCryptoParam(EDesC8, aUid)
90 void CCryptoDesC8Param::ConstructL(const TDesC8& aValue)
92 iValue=aValue.AllocL();
97 // Implementation of Descriptor parameters
99 EXPORT_C CCryptoDesC16Param* CCryptoDesC16Param::NewL(const TDesC16& aValue, TUid aUid)
101 CCryptoDesC16Param* self=NewLC(aValue, aUid);
106 EXPORT_C CCryptoDesC16Param* CCryptoDesC16Param::NewLC(const TDesC16& aValue, TUid aUid)
108 CCryptoDesC16Param* self=new(ELeave) CCryptoDesC16Param(aUid);
109 CleanupStack::PushL(self);
110 self->ConstructL(aValue);
114 EXPORT_C const TDesC16& CCryptoDesC16Param::Value() const
119 EXPORT_C CCryptoDesC16Param::~CCryptoDesC16Param()
123 iValue->Des().FillZ();
128 CCryptoDesC16Param::CCryptoDesC16Param(TUid aUid)
129 : CCryptoParam(EDesC16, aUid)
133 void CCryptoDesC16Param::ConstructL(const TDesC16& aValue)
135 iValue=aValue.AllocL();
139 // Implementation of int parameters
141 EXPORT_C CCryptoIntParam* CCryptoIntParam::NewL(TInt aValue, TUid aUid)
143 return new(ELeave) CCryptoIntParam(aValue, aUid);
146 EXPORT_C CCryptoIntParam* CCryptoIntParam::NewLC(TInt aValue, TUid aUid)
148 CCryptoIntParam* self=new(ELeave) CCryptoIntParam(aValue, aUid);
149 CleanupStack::PushL(self);
153 EXPORT_C TInt CCryptoIntParam::Value() const
158 EXPORT_C CCryptoIntParam::~CCryptoIntParam()
163 CCryptoIntParam::CCryptoIntParam(TInt aValue, TUid aUid)
164 : CCryptoParam(EInt, aUid),
171 // Implementation of BigInt parameters
173 EXPORT_C CCryptoBigIntParam* CCryptoBigIntParam::NewL(const TInteger& aValue, TUid aUid)
175 CCryptoBigIntParam* self = NewLC(aValue, aUid);
180 EXPORT_C CCryptoBigIntParam* CCryptoBigIntParam::NewLC(const TInteger& aValue, TUid aUid)
182 CCryptoBigIntParam* self = new(ELeave) CCryptoBigIntParam(aUid);
183 CleanupStack::PushL(self);
184 self->ConstructL(aValue);
188 EXPORT_C const TInteger& CCryptoBigIntParam::Value() const
193 EXPORT_C CCryptoBigIntParam::~CCryptoBigIntParam()
195 // Secure delete, BigInts used for RSA/DSA modulus and exponent
199 CCryptoBigIntParam::CCryptoBigIntParam(TUid aUid)
200 : CCryptoParam(EBigInt, aUid)
204 void CCryptoBigIntParam::ConstructL(const TInteger& aValue)
206 iValue = RInteger::NewL(aValue);
210 * CCryptoParams implementation
212 EXPORT_C CCryptoParams* CCryptoParams::NewL(void)
214 CCryptoParams* self = NewLC();
219 EXPORT_C CCryptoParams* CCryptoParams::NewLC(void)
221 CCryptoParams* self = new(ELeave) CCryptoParams();
222 CleanupStack::PushL(self);
227 CCryptoParams::CCryptoParams()
231 void CCryptoParams::ConstructL(void)
235 EXPORT_C CCryptoParams::~CCryptoParams()
237 // delete all contained parameters
238 iParams.ResetAndDestroy();
241 EXPORT_C void CCryptoParams::AddL(const TInteger& aParam, TUid aUid)
243 CCryptoBigIntParam* param = CCryptoBigIntParam::NewLC(aParam, aUid);
244 iParams.AppendL(param);
245 CleanupStack::Pop(param);
248 EXPORT_C void CCryptoParams::AddL(const TInt aParam, TUid aUid)
250 CCryptoIntParam* param = CCryptoIntParam::NewLC(aParam, aUid);
251 iParams.AppendL(param);
252 CleanupStack::Pop(param);
255 EXPORT_C void CCryptoParams::AddL(const TDesC8& aParam, TUid aUid)
257 CCryptoDesC8Param* param = CCryptoDesC8Param::NewLC(aParam, aUid);
258 iParams.AppendL(param);
259 CleanupStack::Pop(param);
262 EXPORT_C void CCryptoParams::AddL(const TDesC16& aParam, TUid aUid)
264 CCryptoDesC16Param* param = CCryptoDesC16Param::NewLC(aParam, aUid);
265 iParams.AppendL(param);
266 CleanupStack::Pop(param);
269 EXPORT_C const TInteger& CCryptoParams::GetBigIntL(TUid aUid) const
271 TInteger* paramValue = NULL;
272 CCryptoParam* param = GetCryptoParamL(aUid);
273 if (param->Type() == CCryptoParam::EBigInt)
275 const CCryptoBigIntParam* typedParam = static_cast<const CCryptoBigIntParam*>(param);
276 paramValue = const_cast<TInteger*>(&typedParam->Value());
280 User::Leave(KErrArgument);
285 EXPORT_C TInt CCryptoParams::GetTIntL(TUid aUid) const
288 CCryptoParam* param = GetCryptoParamL(aUid);
289 if (param->Type() == CCryptoParam::EInt)
291 const CCryptoIntParam* typedParam = static_cast<const CCryptoIntParam*>(param);
292 paramValue = typedParam->Value();
296 User::Leave(KErrArgument);
301 EXPORT_C const TDesC8& CCryptoParams::GetTDesC8L(TUid aUid) const
303 TDesC8* paramValue = NULL;
304 CCryptoParam* param = GetCryptoParamL(aUid);
305 if (param->Type() == CCryptoParam::EDesC8)
307 const CCryptoDesC8Param* typedParam = static_cast<const CCryptoDesC8Param*>(param);
308 paramValue = const_cast<TDesC8*>(&typedParam->Value());
312 User::Leave(KErrArgument);
317 EXPORT_C const TDesC16& CCryptoParams::GetTDesC16L(TUid aUid) const
319 TDesC16* paramValue = NULL;
320 CCryptoParam* param = GetCryptoParamL(aUid);
321 if (param->Type() == CCryptoParam::EDesC16)
323 const CCryptoDesC16Param* typedParam = static_cast<const CCryptoDesC16Param*>(param);
324 paramValue = const_cast<TDesC16*>(&typedParam->Value());
328 User::Leave(KErrArgument);
333 EXPORT_C TBool CCryptoParams::IsPresent(TUid aUid) const
336 if (GetCryptoParam(aUid))
343 CCryptoParam* CCryptoParams::GetCryptoParam(TUid aUid) const
345 CCryptoParam* paramPtr = NULL;
346 TInt count = iParams.Count();
347 for (TInt i = 0 ;i < count; i++)
349 if (iParams[i]->Uid() == aUid)
351 paramPtr = iParams[i];
358 CCryptoParam* CCryptoParams::GetCryptoParamL(TUid aUid) const
360 CCryptoParam* paramPtr = GetCryptoParam(aUid);
361 // leave if requested uid was not found
364 User::Leave(KErrArgument);
369 EXPORT_C CCryptoParams& CCryptoParams::CopyL(const CCryptoParams& aParams)
372 TUint count = aParams.iParams.Count();
373 for (TUint num = 0; num < count; num++)
375 CCryptoParam* item = aParams.iParams[num];
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;
383 // For each type of cryptoparam, duplicate it, and append to RPtrArray
384 switch (item->Type())
386 case CCryptoParam::EBigInt:
387 b = CCryptoBigIntParam::NewL(((CCryptoBigIntParam*) item)->Value(), ((CCryptoBigIntParam*) item)->Uid());
388 CleanupStack::PushL(b);
390 CleanupStack::Pop(b);
393 case CCryptoParam::EInt:
394 i = CCryptoIntParam::NewL(((CCryptoIntParam*) item)->Value(), ((CCryptoBigIntParam*) item)->Uid());
395 CleanupStack::PushL(i);
397 CleanupStack::Pop(i);
400 case CCryptoParam::EDesC8:
401 d = CCryptoDesC8Param::NewL(((CCryptoDesC8Param*) item)->Value(), ((CCryptoBigIntParam*) item)->Uid());
402 CleanupStack::PushL(d);
404 CleanupStack::Pop(d);
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);
421 EXPORT_C TInt CCryptoParams::Count(void) const
423 return iParams.Count();
426 EXPORT_C const RPointerArray<CCryptoParam>& CCryptoParams::GetParams() const