os/security/cryptomgmtlibs/cryptotokenfw/source/ctframework/MCTKeyStore.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/security/cryptomgmtlibs/cryptotokenfw/source/ctframework/MCTKeyStore.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,293 @@
     1.4 +/*
     1.5 +* Copyright (c) 2001-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.6 +* All rights reserved.
     1.7 +* This component and the accompanying materials are made available
     1.8 +* under the terms of the License "Eclipse Public License v1.0"
     1.9 +* which accompanies this distribution, and is available
    1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.11 +*
    1.12 +* Initial Contributors:
    1.13 +* Nokia Corporation - initial contribution.
    1.14 +*
    1.15 +* Contributors:
    1.16 +*
    1.17 +* Description: 
    1.18 +*
    1.19 +*/
    1.20 +
    1.21 +
    1.22 +#include <e32base.h>
    1.23 +#include <ct.h>
    1.24 +#include "mctauthobject.h"
    1.25 +#include "mctkeystore.h"
    1.26 +#include "mctkeystoremanager.h"
    1.27 +#include <securityerr.h>
    1.28 +#include "ct/logger.h"
    1.29 +
    1.30 +#ifdef SYMBIAN_ENABLE_SPLIT_HEADERS
    1.31 +
    1.32 +/** The type ID of CCTKeyInfo objects */
    1.33 +const TUid KKeyInfoUID = {0x101F5152};
    1.34 +
    1.35 +#endif
    1.36 +////////////////////////////////////////////////////////////////////////////////
    1.37 +// CKeyInfoBase
    1.38 +////////////////////////////////////////////////////////////////////////////////
    1.39 +
    1.40 +EXPORT_C CKeyInfoBase::CKeyInfoBase(TKeyIdentifier aID,
    1.41 +									TKeyUsagePKCS15 aUsage,
    1.42 +									TUint aSize, 
    1.43 +									HBufC* aLabel,
    1.44 +									TInt aHandle,
    1.45 +									const TSecurityPolicy& aUsePolicy,
    1.46 +									const TSecurityPolicy& aManagementPolicy,
    1.47 +									EKeyAlgorithm aAlgorithm,
    1.48 +									TInt aAccessType,
    1.49 +									TBool aNative,
    1.50 +									TTime aStartDate,
    1.51 +									TTime aEndDate,
    1.52 +									HBufC8* aPKCS8AttributeSet) :
    1.53 +	iID(aID),
    1.54 +	iUsage(aUsage),
    1.55 +	iSize(aSize), 
    1.56 +	iLabel(aLabel),
    1.57 +	iHandle(aHandle),
    1.58 +	iUsePolicy(aUsePolicy),
    1.59 +	iManagementPolicy(aManagementPolicy),
    1.60 +	iAlgorithm(aAlgorithm),
    1.61 +	iAccessType(aAccessType),
    1.62 +	iNative(aNative),
    1.63 +	iStartDate(aStartDate), 
    1.64 +	iEndDate(aEndDate),
    1.65 +	iPKCS8AttributeSet(aPKCS8AttributeSet)
    1.66 +	{
    1.67 +	}
    1.68 +
    1.69 +EXPORT_C CKeyInfoBase::CKeyInfoBase()
    1.70 +	{
    1.71 +	}
    1.72 +
    1.73 +EXPORT_C CKeyInfoBase::~CKeyInfoBase()
    1.74 +	{
    1.75 +	delete iLabel;
    1.76 +	delete iPKCS8AttributeSet;
    1.77 +	}
    1.78 +
    1.79 +EXPORT_C void CKeyInfoBase::ConstructL()
    1.80 +	{
    1.81 +	}
    1.82 +
    1.83 +EXPORT_C void CKeyInfoBase::ConstructL(RReadStream& aIn)
    1.84 +	{
    1.85 +	// Called from derived classes' NewL methods when interalizing a stream
    1.86 +
    1.87 +	InternalizeL(aIn);
    1.88 +	}
    1.89 +
    1.90 +EXPORT_C void CKeyInfoBase::ExternalizeL(RWriteStream& aStream) const
    1.91 +	{
    1.92 +	aStream.WriteL(iID);
    1.93 +	aStream.WriteInt32L(iUsage);
    1.94 +	aStream.WriteUint32L(iSize);
    1.95 +  	aStream.WriteInt32L(iLabel->Length());
    1.96 +  	TPtr16 theLabel(iLabel->Des());
    1.97 +  	aStream.WriteL(theLabel);
    1.98 +	aStream.WriteInt32L(iHandle);
    1.99 +	aStream.WriteL(TPckgC<TSecurityPolicy>(iUsePolicy));
   1.100 +	aStream.WriteL(TPckgC<TSecurityPolicy>(iManagementPolicy));		
   1.101 +	aStream.WriteInt32L(iAlgorithm);
   1.102 +	aStream.WriteInt32L(iAccessType);
   1.103 +	aStream.WriteInt32L(iNative);
   1.104 +	aStream.WriteL(TPckgC<TTime>(iStartDate));
   1.105 +	aStream.WriteL(TPckgC<TTime>(iEndDate));
   1.106 +
   1.107 +	if (iPKCS8AttributeSet)
   1.108 +		{
   1.109 +		aStream.WriteInt32L(iPKCS8AttributeSet->Length());
   1.110 +		TPtr8 theAttributes(iPKCS8AttributeSet->Des());
   1.111 +		aStream.WriteL(theAttributes);
   1.112 +		}
   1.113 +	else
   1.114 +		aStream.WriteInt32L(0);
   1.115 +	}
   1.116 +
   1.117 +void CKeyInfoBase::InternalizeL(RReadStream& aStream)
   1.118 +	{
   1.119 +	aStream.ReadL(iID);
   1.120 +	iUsage = static_cast<TKeyUsagePKCS15>(aStream.ReadInt32L());
   1.121 +	iSize = aStream.ReadUint32L();
   1.122 +	
   1.123 +	TInt labelLen = aStream.ReadInt32L();
   1.124 +	iLabel = HBufC::NewMaxL(labelLen);
   1.125 +	TPtr pLabel(iLabel->Des());
   1.126 +	pLabel.FillZ();
   1.127 +	// This will have made the length of pLabel equal to the length of
   1.128 +	// the alloc cell of iLabel, which may be longer than labelLen. So
   1.129 +	// we need to pass the length we want to read into ReadL
   1.130 +	aStream.ReadL(pLabel, labelLen);
   1.131 +	
   1.132 +	iHandle = aStream.ReadInt32L();
   1.133 +
   1.134 +	TPckg<TSecurityPolicy> usePolicy(iUsePolicy);
   1.135 +	aStream.ReadL(usePolicy);
   1.136 +	TPckg<TSecurityPolicy> managementPolicy(iManagementPolicy);
   1.137 +	aStream.ReadL(managementPolicy);
   1.138 +		
   1.139 +	iAlgorithm = (EKeyAlgorithm)(aStream.ReadInt32L());
   1.140 +	iAccessType = (EKeyAccess)(aStream.ReadInt32L());
   1.141 +	iNative = (TBool)(aStream.ReadInt32L());
   1.142 +	TPckg<TTime> startDate(iStartDate);
   1.143 +	aStream.ReadL(startDate);
   1.144 +	TPckg<TTime> endDate(iEndDate);
   1.145 +	aStream.ReadL(endDate);
   1.146 +
   1.147 +	TInt attributeLen = aStream.ReadInt32L();
   1.148 +	if (attributeLen > 0)
   1.149 +		{
   1.150 +		iPKCS8AttributeSet = HBufC8::NewMaxL(attributeLen);
   1.151 +		TPtr8 pAttributes(iPKCS8AttributeSet->Des());
   1.152 +		pAttributes.FillZ();
   1.153 +		//	This will have made the length of pAttributes equal to the length of
   1.154 +		//	the alloc cell of iPKCS8AttributeSet, which may be longer than attributeLen
   1.155 +		//	So we need to pass the length we want to read into ReadL
   1.156 +		aStream.ReadL(pAttributes, attributeLen);
   1.157 +		}
   1.158 +	}
   1.159 +
   1.160 +////////////////////////////////////////////////////////////////////////////////
   1.161 +// CCTKeyInfo
   1.162 +////////////////////////////////////////////////////////////////////////////////
   1.163 +
   1.164 +EXPORT_C CCTKeyInfo* CCTKeyInfo::NewL(TKeyIdentifier aID,
   1.165 +									  TKeyUsagePKCS15 aUsage, 
   1.166 +									  TUint aSize,
   1.167 +									  MCTAuthenticationObject* aProtector,
   1.168 +									  HBufC* aLabel,
   1.169 +									  MCTToken& aToken,
   1.170 +									  TInt aHandle,
   1.171 +									  const TSecurityPolicy& aUsePolicy,
   1.172 +									  const TSecurityPolicy& aManagementPolicy,
   1.173 +									  EKeyAlgorithm aAlgorithm,
   1.174 +									  TInt aAccessType,
   1.175 +									  TBool aNative,
   1.176 +									  TTime aStartDate,
   1.177 +									  TTime aEndDate,
   1.178 +									  HBufC8* aPKCS8AttributeSet /*= NULL*/)
   1.179 +	{
   1.180 +	CCTKeyInfo* me = NULL;
   1.181 +	me = new (ELeave) CCTKeyInfo(aID,
   1.182 +								 aUsage,
   1.183 +								 aSize,
   1.184 +								 aProtector,
   1.185 +								 aLabel,
   1.186 +								 aToken,
   1.187 +								 aHandle,
   1.188 +								 aUsePolicy,
   1.189 +								 aManagementPolicy,
   1.190 +								 aAlgorithm,
   1.191 +								 aAccessType,
   1.192 +								 aNative,
   1.193 +								 aStartDate,
   1.194 +								 aEndDate,
   1.195 +								 aPKCS8AttributeSet);
   1.196 +
   1.197 +	CleanupReleasePushL(*me);
   1.198 +	me->ConstructL();
   1.199 +	CleanupStack::Pop();
   1.200 +	
   1.201 +	return me;
   1.202 +	}
   1.203 +
   1.204 +CCTKeyInfo::CCTKeyInfo(TKeyIdentifier aID,
   1.205 +					   TKeyUsagePKCS15 aUsage,
   1.206 +					   TUint aSize, 
   1.207 +					   MCTAuthenticationObject* aProtector,
   1.208 +					   HBufC* aLabel,
   1.209 +					   MCTToken& aToken,
   1.210 +					   TInt aHandle,
   1.211 +					   const TSecurityPolicy& aUsePolicy,
   1.212 +					   const TSecurityPolicy& aManagementPolicy,
   1.213 +					   EKeyAlgorithm aAlgorithm,
   1.214 +					   TInt aAccessType,
   1.215 +					   TBool aNative, 
   1.216 +					   TTime aStartDate,
   1.217 +					   TTime aEndDate,
   1.218 +					   HBufC8* aPKCS8AttributeSet /*= NULL*/) :
   1.219 +	CKeyInfoBase(aID,
   1.220 +				 aUsage,
   1.221 +				 aSize,
   1.222 +				 aLabel,
   1.223 +				 aHandle,
   1.224 +				 aUsePolicy,
   1.225 +				 aManagementPolicy,
   1.226 +				 aAlgorithm,
   1.227 +				 aAccessType,
   1.228 +				 aNative,
   1.229 +				 aStartDate,
   1.230 +				 aEndDate,
   1.231 +				 aPKCS8AttributeSet),
   1.232 +	MCTTokenObject(aToken),
   1.233 +	iToken(aToken),
   1.234 +	iProtector(aProtector)
   1.235 +	{
   1.236 +	LOG1(_L("CCTKeyInfo::CCTKeyInfo() with iProtector %08x"), iProtector);
   1.237 +	}
   1.238 +
   1.239 +EXPORT_C CCTKeyInfo* CCTKeyInfo::NewL(RReadStream& aStream, MCTToken& aToken)
   1.240 +	{
   1.241 +	CCTKeyInfo* me = new (ELeave) CCTKeyInfo(aToken);
   1.242 +	CleanupReleasePushL(*me);
   1.243 +	me->ConstructL(aStream);
   1.244 +	CleanupStack::Pop();
   1.245 +	return me;
   1.246 +	}
   1.247 +
   1.248 +CCTKeyInfo::CCTKeyInfo(MCTToken& aToken) :
   1.249 +	CKeyInfoBase(),
   1.250 +	MCTTokenObject(aToken),
   1.251 +	iToken(aToken)
   1.252 +	{
   1.253 +	}
   1.254 +
   1.255 +CCTKeyInfo::~CCTKeyInfo()
   1.256 +	{
   1.257 +	LOG(_L("CCTKeyInfo::~CCTKeyInfo"));
   1.258 +	if (iProtector)
   1.259 +		{
   1.260 +		LOG_INC_INDENT();
   1.261 +		iProtector->Release();
   1.262 +		LOG_DEC_INDENT();
   1.263 +		}
   1.264 +	}
   1.265 +
   1.266 +const TDesC& CCTKeyInfo::Label() const
   1.267 +	{
   1.268 +	return CKeyInfoBase::Label();
   1.269 +	}
   1.270 +
   1.271 +MCTToken& CCTKeyInfo::Token() const
   1.272 +	{
   1.273 +	return iToken;
   1.274 +	}
   1.275 +
   1.276 +TUid CCTKeyInfo::Type() const
   1.277 +	{
   1.278 +	return KKeyInfoUID;
   1.279 +	}
   1.280 +
   1.281 +TCTTokenObjectHandle CCTKeyInfo::Handle() const
   1.282 +	{
   1.283 +	return TCTTokenObjectHandle(Token().Handle(), HandleID());
   1.284 +	}
   1.285 +
   1.286 +////////////////////////////////////////////////////////////////////////////////
   1.287 +// TCTKeyAttributeFilter
   1.288 +////////////////////////////////////////////////////////////////////////////////
   1.289 +
   1.290 +EXPORT_C TCTKeyAttributeFilter::TCTKeyAttributeFilter()
   1.291 +	{
   1.292 +	iKeyId = KNullDesC8;
   1.293 +	iUsage = EPKCS15UsageAll;
   1.294 +	iPolicyFilter = EUsableKeys;
   1.295 +	iKeyAlgorithm = CCTKeyInfo::EInvalidAlgorithm;
   1.296 +	}