os/security/cryptomgmtlibs/cryptotokenfw/source/ctframework/MCTKeyStore.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     1 /*
     2 * Copyright (c) 2001-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 *
    16 */
    17 
    18 
    19 #include <e32base.h>
    20 #include <ct.h>
    21 #include "mctauthobject.h"
    22 #include "mctkeystore.h"
    23 #include "mctkeystoremanager.h"
    24 #include <securityerr.h>
    25 #include "ct/logger.h"
    26 
    27 #ifdef SYMBIAN_ENABLE_SPLIT_HEADERS
    28 
    29 /** The type ID of CCTKeyInfo objects */
    30 const TUid KKeyInfoUID = {0x101F5152};
    31 
    32 #endif
    33 ////////////////////////////////////////////////////////////////////////////////
    34 // CKeyInfoBase
    35 ////////////////////////////////////////////////////////////////////////////////
    36 
    37 EXPORT_C CKeyInfoBase::CKeyInfoBase(TKeyIdentifier aID,
    38 									TKeyUsagePKCS15 aUsage,
    39 									TUint aSize, 
    40 									HBufC* aLabel,
    41 									TInt aHandle,
    42 									const TSecurityPolicy& aUsePolicy,
    43 									const TSecurityPolicy& aManagementPolicy,
    44 									EKeyAlgorithm aAlgorithm,
    45 									TInt aAccessType,
    46 									TBool aNative,
    47 									TTime aStartDate,
    48 									TTime aEndDate,
    49 									HBufC8* aPKCS8AttributeSet) :
    50 	iID(aID),
    51 	iUsage(aUsage),
    52 	iSize(aSize), 
    53 	iLabel(aLabel),
    54 	iHandle(aHandle),
    55 	iUsePolicy(aUsePolicy),
    56 	iManagementPolicy(aManagementPolicy),
    57 	iAlgorithm(aAlgorithm),
    58 	iAccessType(aAccessType),
    59 	iNative(aNative),
    60 	iStartDate(aStartDate), 
    61 	iEndDate(aEndDate),
    62 	iPKCS8AttributeSet(aPKCS8AttributeSet)
    63 	{
    64 	}
    65 
    66 EXPORT_C CKeyInfoBase::CKeyInfoBase()
    67 	{
    68 	}
    69 
    70 EXPORT_C CKeyInfoBase::~CKeyInfoBase()
    71 	{
    72 	delete iLabel;
    73 	delete iPKCS8AttributeSet;
    74 	}
    75 
    76 EXPORT_C void CKeyInfoBase::ConstructL()
    77 	{
    78 	}
    79 
    80 EXPORT_C void CKeyInfoBase::ConstructL(RReadStream& aIn)
    81 	{
    82 	// Called from derived classes' NewL methods when interalizing a stream
    83 
    84 	InternalizeL(aIn);
    85 	}
    86 
    87 EXPORT_C void CKeyInfoBase::ExternalizeL(RWriteStream& aStream) const
    88 	{
    89 	aStream.WriteL(iID);
    90 	aStream.WriteInt32L(iUsage);
    91 	aStream.WriteUint32L(iSize);
    92   	aStream.WriteInt32L(iLabel->Length());
    93   	TPtr16 theLabel(iLabel->Des());
    94   	aStream.WriteL(theLabel);
    95 	aStream.WriteInt32L(iHandle);
    96 	aStream.WriteL(TPckgC<TSecurityPolicy>(iUsePolicy));
    97 	aStream.WriteL(TPckgC<TSecurityPolicy>(iManagementPolicy));		
    98 	aStream.WriteInt32L(iAlgorithm);
    99 	aStream.WriteInt32L(iAccessType);
   100 	aStream.WriteInt32L(iNative);
   101 	aStream.WriteL(TPckgC<TTime>(iStartDate));
   102 	aStream.WriteL(TPckgC<TTime>(iEndDate));
   103 
   104 	if (iPKCS8AttributeSet)
   105 		{
   106 		aStream.WriteInt32L(iPKCS8AttributeSet->Length());
   107 		TPtr8 theAttributes(iPKCS8AttributeSet->Des());
   108 		aStream.WriteL(theAttributes);
   109 		}
   110 	else
   111 		aStream.WriteInt32L(0);
   112 	}
   113 
   114 void CKeyInfoBase::InternalizeL(RReadStream& aStream)
   115 	{
   116 	aStream.ReadL(iID);
   117 	iUsage = static_cast<TKeyUsagePKCS15>(aStream.ReadInt32L());
   118 	iSize = aStream.ReadUint32L();
   119 	
   120 	TInt labelLen = aStream.ReadInt32L();
   121 	iLabel = HBufC::NewMaxL(labelLen);
   122 	TPtr pLabel(iLabel->Des());
   123 	pLabel.FillZ();
   124 	// This will have made the length of pLabel equal to the length of
   125 	// the alloc cell of iLabel, which may be longer than labelLen. So
   126 	// we need to pass the length we want to read into ReadL
   127 	aStream.ReadL(pLabel, labelLen);
   128 	
   129 	iHandle = aStream.ReadInt32L();
   130 
   131 	TPckg<TSecurityPolicy> usePolicy(iUsePolicy);
   132 	aStream.ReadL(usePolicy);
   133 	TPckg<TSecurityPolicy> managementPolicy(iManagementPolicy);
   134 	aStream.ReadL(managementPolicy);
   135 		
   136 	iAlgorithm = (EKeyAlgorithm)(aStream.ReadInt32L());
   137 	iAccessType = (EKeyAccess)(aStream.ReadInt32L());
   138 	iNative = (TBool)(aStream.ReadInt32L());
   139 	TPckg<TTime> startDate(iStartDate);
   140 	aStream.ReadL(startDate);
   141 	TPckg<TTime> endDate(iEndDate);
   142 	aStream.ReadL(endDate);
   143 
   144 	TInt attributeLen = aStream.ReadInt32L();
   145 	if (attributeLen > 0)
   146 		{
   147 		iPKCS8AttributeSet = HBufC8::NewMaxL(attributeLen);
   148 		TPtr8 pAttributes(iPKCS8AttributeSet->Des());
   149 		pAttributes.FillZ();
   150 		//	This will have made the length of pAttributes equal to the length of
   151 		//	the alloc cell of iPKCS8AttributeSet, which may be longer than attributeLen
   152 		//	So we need to pass the length we want to read into ReadL
   153 		aStream.ReadL(pAttributes, attributeLen);
   154 		}
   155 	}
   156 
   157 ////////////////////////////////////////////////////////////////////////////////
   158 // CCTKeyInfo
   159 ////////////////////////////////////////////////////////////////////////////////
   160 
   161 EXPORT_C CCTKeyInfo* CCTKeyInfo::NewL(TKeyIdentifier aID,
   162 									  TKeyUsagePKCS15 aUsage, 
   163 									  TUint aSize,
   164 									  MCTAuthenticationObject* aProtector,
   165 									  HBufC* aLabel,
   166 									  MCTToken& aToken,
   167 									  TInt aHandle,
   168 									  const TSecurityPolicy& aUsePolicy,
   169 									  const TSecurityPolicy& aManagementPolicy,
   170 									  EKeyAlgorithm aAlgorithm,
   171 									  TInt aAccessType,
   172 									  TBool aNative,
   173 									  TTime aStartDate,
   174 									  TTime aEndDate,
   175 									  HBufC8* aPKCS8AttributeSet /*= NULL*/)
   176 	{
   177 	CCTKeyInfo* me = NULL;
   178 	me = new (ELeave) CCTKeyInfo(aID,
   179 								 aUsage,
   180 								 aSize,
   181 								 aProtector,
   182 								 aLabel,
   183 								 aToken,
   184 								 aHandle,
   185 								 aUsePolicy,
   186 								 aManagementPolicy,
   187 								 aAlgorithm,
   188 								 aAccessType,
   189 								 aNative,
   190 								 aStartDate,
   191 								 aEndDate,
   192 								 aPKCS8AttributeSet);
   193 
   194 	CleanupReleasePushL(*me);
   195 	me->ConstructL();
   196 	CleanupStack::Pop();
   197 	
   198 	return me;
   199 	}
   200 
   201 CCTKeyInfo::CCTKeyInfo(TKeyIdentifier aID,
   202 					   TKeyUsagePKCS15 aUsage,
   203 					   TUint aSize, 
   204 					   MCTAuthenticationObject* aProtector,
   205 					   HBufC* aLabel,
   206 					   MCTToken& aToken,
   207 					   TInt aHandle,
   208 					   const TSecurityPolicy& aUsePolicy,
   209 					   const TSecurityPolicy& aManagementPolicy,
   210 					   EKeyAlgorithm aAlgorithm,
   211 					   TInt aAccessType,
   212 					   TBool aNative, 
   213 					   TTime aStartDate,
   214 					   TTime aEndDate,
   215 					   HBufC8* aPKCS8AttributeSet /*= NULL*/) :
   216 	CKeyInfoBase(aID,
   217 				 aUsage,
   218 				 aSize,
   219 				 aLabel,
   220 				 aHandle,
   221 				 aUsePolicy,
   222 				 aManagementPolicy,
   223 				 aAlgorithm,
   224 				 aAccessType,
   225 				 aNative,
   226 				 aStartDate,
   227 				 aEndDate,
   228 				 aPKCS8AttributeSet),
   229 	MCTTokenObject(aToken),
   230 	iToken(aToken),
   231 	iProtector(aProtector)
   232 	{
   233 	LOG1(_L("CCTKeyInfo::CCTKeyInfo() with iProtector %08x"), iProtector);
   234 	}
   235 
   236 EXPORT_C CCTKeyInfo* CCTKeyInfo::NewL(RReadStream& aStream, MCTToken& aToken)
   237 	{
   238 	CCTKeyInfo* me = new (ELeave) CCTKeyInfo(aToken);
   239 	CleanupReleasePushL(*me);
   240 	me->ConstructL(aStream);
   241 	CleanupStack::Pop();
   242 	return me;
   243 	}
   244 
   245 CCTKeyInfo::CCTKeyInfo(MCTToken& aToken) :
   246 	CKeyInfoBase(),
   247 	MCTTokenObject(aToken),
   248 	iToken(aToken)
   249 	{
   250 	}
   251 
   252 CCTKeyInfo::~CCTKeyInfo()
   253 	{
   254 	LOG(_L("CCTKeyInfo::~CCTKeyInfo"));
   255 	if (iProtector)
   256 		{
   257 		LOG_INC_INDENT();
   258 		iProtector->Release();
   259 		LOG_DEC_INDENT();
   260 		}
   261 	}
   262 
   263 const TDesC& CCTKeyInfo::Label() const
   264 	{
   265 	return CKeyInfoBase::Label();
   266 	}
   267 
   268 MCTToken& CCTKeyInfo::Token() const
   269 	{
   270 	return iToken;
   271 	}
   272 
   273 TUid CCTKeyInfo::Type() const
   274 	{
   275 	return KKeyInfoUID;
   276 	}
   277 
   278 TCTTokenObjectHandle CCTKeyInfo::Handle() const
   279 	{
   280 	return TCTTokenObjectHandle(Token().Handle(), HandleID());
   281 	}
   282 
   283 ////////////////////////////////////////////////////////////////////////////////
   284 // TCTKeyAttributeFilter
   285 ////////////////////////////////////////////////////////////////////////////////
   286 
   287 EXPORT_C TCTKeyAttributeFilter::TCTKeyAttributeFilter()
   288 	{
   289 	iKeyId = KNullDesC8;
   290 	iUsage = EPKCS15UsageAll;
   291 	iPolicyFilter = EUsableKeys;
   292 	iKeyAlgorithm = CCTKeyInfo::EInvalidAlgorithm;
   293 	}