sl@0: /* sl@0: * Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: * All rights reserved. sl@0: * This component and the accompanying materials are made available sl@0: * under the terms of the License "Eclipse Public License v1.0" sl@0: * which accompanies this distribution, and is available sl@0: * at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: * sl@0: * Initial Contributors: sl@0: * Nokia Corporation - initial contribution. sl@0: * sl@0: * Contributors: sl@0: * sl@0: * Description: sl@0: * Generic crypto parameter implementation sl@0: * Generic crypto parameter implementation sl@0: * sl@0: */ sl@0: sl@0: sl@0: /** sl@0: @file sl@0: */ sl@0: sl@0: #include sl@0: sl@0: using namespace CryptoSpi; sl@0: sl@0: // sl@0: //Implementation of the generic crypto parameter sl@0: // sl@0: CCryptoParam::CCryptoParam(TParamType aType, TUid aUid) sl@0: : iType(aType), sl@0: iUid(aUid) sl@0: { sl@0: } sl@0: sl@0: EXPORT_C CCryptoParam::~CCryptoParam() sl@0: { sl@0: sl@0: } sl@0: sl@0: EXPORT_C TInt CCryptoParam::Type() const sl@0: { sl@0: return iType; sl@0: } sl@0: sl@0: EXPORT_C TUid CCryptoParam::Uid() const sl@0: { sl@0: return iUid; sl@0: } sl@0: sl@0: // sl@0: // Implementation of Descriptor parameters sl@0: // sl@0: EXPORT_C CCryptoDesC8Param* CCryptoDesC8Param::NewL(const TDesC8& aValue, TUid aUid) sl@0: { sl@0: CCryptoDesC8Param* self=NewLC(aValue, aUid); sl@0: CleanupStack::Pop(); sl@0: return self; sl@0: } sl@0: sl@0: EXPORT_C CCryptoDesC8Param* CCryptoDesC8Param::NewLC(const TDesC8& aValue, TUid aUid) sl@0: { sl@0: CCryptoDesC8Param* self=new(ELeave) CCryptoDesC8Param(aUid); sl@0: CleanupStack::PushL(self); sl@0: self->ConstructL(aValue); sl@0: return self; sl@0: } sl@0: sl@0: EXPORT_C const TDesC8& CCryptoDesC8Param::Value() const sl@0: { sl@0: return *iValue; sl@0: } sl@0: sl@0: EXPORT_C CCryptoDesC8Param::~CCryptoDesC8Param() sl@0: { sl@0: if (iValue) sl@0: { sl@0: iValue->Des().FillZ(); sl@0: } sl@0: delete iValue; sl@0: } sl@0: sl@0: CCryptoDesC8Param::CCryptoDesC8Param(TUid aUid) sl@0: : CCryptoParam(EDesC8, aUid) sl@0: { sl@0: } sl@0: sl@0: void CCryptoDesC8Param::ConstructL(const TDesC8& aValue) sl@0: { sl@0: iValue=aValue.AllocL(); sl@0: } sl@0: sl@0: sl@0: // sl@0: // Implementation of Descriptor parameters sl@0: // sl@0: EXPORT_C CCryptoDesC16Param* CCryptoDesC16Param::NewL(const TDesC16& aValue, TUid aUid) sl@0: { sl@0: CCryptoDesC16Param* self=NewLC(aValue, aUid); sl@0: CleanupStack::Pop(); sl@0: return self; sl@0: } sl@0: sl@0: EXPORT_C CCryptoDesC16Param* CCryptoDesC16Param::NewLC(const TDesC16& aValue, TUid aUid) sl@0: { sl@0: CCryptoDesC16Param* self=new(ELeave) CCryptoDesC16Param(aUid); sl@0: CleanupStack::PushL(self); sl@0: self->ConstructL(aValue); sl@0: return self; sl@0: } sl@0: sl@0: EXPORT_C const TDesC16& CCryptoDesC16Param::Value() const sl@0: { sl@0: return *iValue; sl@0: } sl@0: sl@0: EXPORT_C CCryptoDesC16Param::~CCryptoDesC16Param() sl@0: { sl@0: if (iValue) sl@0: { sl@0: iValue->Des().FillZ(); sl@0: } sl@0: delete iValue; sl@0: } sl@0: sl@0: CCryptoDesC16Param::CCryptoDesC16Param(TUid aUid) sl@0: : CCryptoParam(EDesC16, aUid) sl@0: { sl@0: } sl@0: sl@0: void CCryptoDesC16Param::ConstructL(const TDesC16& aValue) sl@0: { sl@0: iValue=aValue.AllocL(); sl@0: } sl@0: sl@0: // sl@0: // Implementation of int parameters sl@0: // sl@0: EXPORT_C CCryptoIntParam* CCryptoIntParam::NewL(TInt aValue, TUid aUid) sl@0: { sl@0: return new(ELeave) CCryptoIntParam(aValue, aUid); sl@0: } sl@0: sl@0: EXPORT_C CCryptoIntParam* CCryptoIntParam::NewLC(TInt aValue, TUid aUid) sl@0: { sl@0: CCryptoIntParam* self=new(ELeave) CCryptoIntParam(aValue, aUid); sl@0: CleanupStack::PushL(self); sl@0: return self; sl@0: } sl@0: sl@0: EXPORT_C TInt CCryptoIntParam::Value() const sl@0: { sl@0: return iValue; sl@0: } sl@0: sl@0: EXPORT_C CCryptoIntParam::~CCryptoIntParam() sl@0: { sl@0: iValue = 0; sl@0: } sl@0: sl@0: CCryptoIntParam::CCryptoIntParam(TInt aValue, TUid aUid) sl@0: : CCryptoParam(EInt, aUid), sl@0: iValue(aValue) sl@0: { sl@0: sl@0: } sl@0: sl@0: // sl@0: // Implementation of BigInt parameters sl@0: // sl@0: EXPORT_C CCryptoBigIntParam* CCryptoBigIntParam::NewL(const TInteger& aValue, TUid aUid) sl@0: { sl@0: CCryptoBigIntParam* self = NewLC(aValue, aUid); sl@0: CleanupStack::Pop(); sl@0: return self; sl@0: } sl@0: sl@0: EXPORT_C CCryptoBigIntParam* CCryptoBigIntParam::NewLC(const TInteger& aValue, TUid aUid) sl@0: { sl@0: CCryptoBigIntParam* self = new(ELeave) CCryptoBigIntParam(aUid); sl@0: CleanupStack::PushL(self); sl@0: self->ConstructL(aValue); sl@0: return self; sl@0: } sl@0: sl@0: EXPORT_C const TInteger& CCryptoBigIntParam::Value() const sl@0: { sl@0: return iValue; sl@0: } sl@0: sl@0: EXPORT_C CCryptoBigIntParam::~CCryptoBigIntParam() sl@0: { sl@0: // Secure delete, BigInts used for RSA/DSA modulus and exponent sl@0: iValue.Close(); sl@0: } sl@0: sl@0: CCryptoBigIntParam::CCryptoBigIntParam(TUid aUid) sl@0: : CCryptoParam(EBigInt, aUid) sl@0: { sl@0: } sl@0: sl@0: void CCryptoBigIntParam::ConstructL(const TInteger& aValue) sl@0: { sl@0: iValue = RInteger::NewL(aValue); sl@0: } sl@0: sl@0: /* sl@0: * CCryptoParams implementation sl@0: */ sl@0: EXPORT_C CCryptoParams* CCryptoParams::NewL(void) sl@0: { sl@0: CCryptoParams* self = NewLC(); sl@0: CleanupStack::Pop(); sl@0: return self; sl@0: } sl@0: sl@0: EXPORT_C CCryptoParams* CCryptoParams::NewLC(void) sl@0: { sl@0: CCryptoParams* self = new(ELeave) CCryptoParams(); sl@0: CleanupStack::PushL(self); sl@0: self->ConstructL(); sl@0: return self; sl@0: } sl@0: sl@0: CCryptoParams::CCryptoParams() sl@0: { sl@0: } sl@0: sl@0: void CCryptoParams::ConstructL(void) sl@0: { sl@0: } sl@0: sl@0: EXPORT_C CCryptoParams::~CCryptoParams() sl@0: { sl@0: // delete all contained parameters sl@0: iParams.ResetAndDestroy(); sl@0: } sl@0: sl@0: EXPORT_C void CCryptoParams::AddL(const TInteger& aParam, TUid aUid) sl@0: { sl@0: CCryptoBigIntParam* param = CCryptoBigIntParam::NewLC(aParam, aUid); sl@0: iParams.AppendL(param); sl@0: CleanupStack::Pop(param); sl@0: } sl@0: sl@0: EXPORT_C void CCryptoParams::AddL(const TInt aParam, TUid aUid) sl@0: { sl@0: CCryptoIntParam* param = CCryptoIntParam::NewLC(aParam, aUid); sl@0: iParams.AppendL(param); sl@0: CleanupStack::Pop(param); sl@0: } sl@0: sl@0: EXPORT_C void CCryptoParams::AddL(const TDesC8& aParam, TUid aUid) sl@0: { sl@0: CCryptoDesC8Param* param = CCryptoDesC8Param::NewLC(aParam, aUid); sl@0: iParams.AppendL(param); sl@0: CleanupStack::Pop(param); sl@0: } sl@0: sl@0: EXPORT_C void CCryptoParams::AddL(const TDesC16& aParam, TUid aUid) sl@0: { sl@0: CCryptoDesC16Param* param = CCryptoDesC16Param::NewLC(aParam, aUid); sl@0: iParams.AppendL(param); sl@0: CleanupStack::Pop(param); sl@0: } sl@0: sl@0: EXPORT_C const TInteger& CCryptoParams::GetBigIntL(TUid aUid) const sl@0: { sl@0: TInteger* paramValue = NULL; sl@0: CCryptoParam* param = GetCryptoParamL(aUid); sl@0: if (param->Type() == CCryptoParam::EBigInt) sl@0: { sl@0: const CCryptoBigIntParam* typedParam = static_cast(param); sl@0: paramValue = const_cast(&typedParam->Value()); sl@0: } sl@0: else sl@0: { sl@0: User::Leave(KErrArgument); sl@0: } sl@0: return *paramValue; sl@0: } sl@0: sl@0: EXPORT_C TInt CCryptoParams::GetTIntL(TUid aUid) const sl@0: { sl@0: TInt paramValue = 0; sl@0: CCryptoParam* param = GetCryptoParamL(aUid); sl@0: if (param->Type() == CCryptoParam::EInt) sl@0: { sl@0: const CCryptoIntParam* typedParam = static_cast(param); sl@0: paramValue = typedParam->Value(); sl@0: } sl@0: else sl@0: { sl@0: User::Leave(KErrArgument); sl@0: } sl@0: return paramValue; sl@0: } sl@0: sl@0: EXPORT_C const TDesC8& CCryptoParams::GetTDesC8L(TUid aUid) const sl@0: { sl@0: TDesC8* paramValue = NULL; sl@0: CCryptoParam* param = GetCryptoParamL(aUid); sl@0: if (param->Type() == CCryptoParam::EDesC8) sl@0: { sl@0: const CCryptoDesC8Param* typedParam = static_cast(param); sl@0: paramValue = const_cast(&typedParam->Value()); sl@0: } sl@0: else sl@0: { sl@0: User::Leave(KErrArgument); sl@0: } sl@0: return *paramValue; sl@0: } sl@0: sl@0: EXPORT_C const TDesC16& CCryptoParams::GetTDesC16L(TUid aUid) const sl@0: { sl@0: TDesC16* paramValue = NULL; sl@0: CCryptoParam* param = GetCryptoParamL(aUid); sl@0: if (param->Type() == CCryptoParam::EDesC16) sl@0: { sl@0: const CCryptoDesC16Param* typedParam = static_cast(param); sl@0: paramValue = const_cast(&typedParam->Value()); sl@0: } sl@0: else sl@0: { sl@0: User::Leave(KErrArgument); sl@0: } sl@0: return *paramValue; sl@0: } sl@0: sl@0: EXPORT_C TBool CCryptoParams::IsPresent(TUid aUid) const sl@0: { sl@0: TBool ret = EFalse; sl@0: if (GetCryptoParam(aUid)) sl@0: { sl@0: ret = ETrue; sl@0: } sl@0: return ret; sl@0: } sl@0: sl@0: CCryptoParam* CCryptoParams::GetCryptoParam(TUid aUid) const sl@0: { sl@0: CCryptoParam* paramPtr = NULL; sl@0: TInt count = iParams.Count(); sl@0: for (TInt i = 0 ;i < count; i++) sl@0: { sl@0: if (iParams[i]->Uid() == aUid) sl@0: { sl@0: paramPtr = iParams[i]; sl@0: break; sl@0: } sl@0: } sl@0: return paramPtr; sl@0: } sl@0: sl@0: CCryptoParam* CCryptoParams::GetCryptoParamL(TUid aUid) const sl@0: { sl@0: CCryptoParam* paramPtr = GetCryptoParam(aUid); sl@0: // leave if requested uid was not found sl@0: if (!paramPtr) sl@0: { sl@0: User::Leave(KErrArgument); sl@0: } sl@0: return paramPtr; sl@0: } sl@0: sl@0: EXPORT_C CCryptoParams& CCryptoParams::CopyL(const CCryptoParams& aParams) sl@0: { sl@0: iParams.Close(); sl@0: TUint count = aParams.iParams.Count(); sl@0: for (TUint num = 0; num < count; num++) sl@0: { sl@0: CCryptoParam* item = aParams.iParams[num]; sl@0: sl@0: // Stop armv5 compiler warning about init through switch statement sl@0: CCryptoBigIntParam *b = 0; sl@0: CCryptoDesC8Param *d = 0; sl@0: CCryptoDesC16Param *d16 = 0; sl@0: CCryptoIntParam *i = 0; sl@0: sl@0: // For each type of cryptoparam, duplicate it, and append to RPtrArray sl@0: switch (item->Type()) sl@0: { sl@0: case CCryptoParam::EBigInt: sl@0: b = CCryptoBigIntParam::NewL(((CCryptoBigIntParam*) item)->Value(), ((CCryptoBigIntParam*) item)->Uid()); sl@0: CleanupStack::PushL(b); sl@0: iParams.AppendL(b); sl@0: CleanupStack::Pop(b); sl@0: break; sl@0: sl@0: case CCryptoParam::EInt: sl@0: i = CCryptoIntParam::NewL(((CCryptoIntParam*) item)->Value(), ((CCryptoBigIntParam*) item)->Uid()); sl@0: CleanupStack::PushL(i); sl@0: iParams.AppendL(i); sl@0: CleanupStack::Pop(i); sl@0: break; sl@0: sl@0: case CCryptoParam::EDesC8: sl@0: d = CCryptoDesC8Param::NewL(((CCryptoDesC8Param*) item)->Value(), ((CCryptoBigIntParam*) item)->Uid()); sl@0: CleanupStack::PushL(d); sl@0: iParams.AppendL(d); sl@0: CleanupStack::Pop(d); sl@0: break; sl@0: sl@0: case CCryptoParam::EDesC16: sl@0: d16 = CCryptoDesC16Param::NewL(((CCryptoDesC16Param*) item)->Value(), ((CCryptoBigIntParam*) item)->Uid()); sl@0: CleanupStack::PushL(d16); sl@0: iParams.AppendL(d16); sl@0: CleanupStack::Pop(d16); sl@0: break; sl@0: sl@0: default: sl@0: break; sl@0: } sl@0: } sl@0: return *this; sl@0: } sl@0: sl@0: EXPORT_C TInt CCryptoParams::Count(void) const sl@0: { sl@0: return iParams.Count(); sl@0: } sl@0: sl@0: EXPORT_C const RPointerArray& CCryptoParams::GetParams() const sl@0: { sl@0: return iParams; sl@0: } sl@0: