os/security/cryptomgmtlibs/cryptotokenfw/inc_interfaces/MCTKeyStore_v2.h
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) 2003-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 * MCTKeystore.h
    16 *
    17 */
    18 
    19 
    20 /**
    21  @file 
    22  @publishedPartner
    23  @released
    24 */
    25  
    26 #ifndef __MCTKEYSTORE_H__
    27 #define __MCTKEYSTORE_H__
    28 
    29 #include <securitydefs.h>
    30 #include <mkeystore.h>
    31 #include <s32file.h>
    32 
    33 #ifndef SYMBIAN_ENABLE_SPLIT_HEADERS
    34 #include <mctkeystoreuids.h>
    35 #endif
    36 
    37 /** The UID of the filekey store */
    38 const TInt KTokenTypeFileKeystore = 0x101F7333;
    39 
    40 #ifndef SYMBIAN_ENABLE_SPLIT_HEADERS
    41 
    42 /** The type ID of CCTKeyInfo objects */
    43 const TUid KKeyInfoUID = {0x101F5152};
    44 const TUid KNonRepudiationSignerUID = {0x101F7A40};
    45 const TUid KKeyStoreAuthObjectUID = {0x101FE681};
    46 
    47 #endif
    48 
    49 class MCTAuthenticationObject;
    50 
    51 /**
    52  * Defines the interface for a read-only key store token.
    53  *
    54  * All the details are defined in MKeyStore as they are shared by the unified
    55  * key store.
    56  * 
    57  * The class adds no extra member functions or data. 
    58  * 
    59  */
    60 class MCTKeyStore : public MCTTokenInterface, public MKeyStore
    61 	{
    62 	};
    63 
    64 /**
    65  * Base class for CCTKeyInfo.
    66  *
    67  */
    68 class CKeyInfoBase : protected CBase
    69 	{
    70 public:
    71 
    72 	/** Key algorithms. */
    73 	enum EKeyAlgorithm
    74 		{
    75 		EInvalidAlgorithm	= 0,
    76 		ERSA				= 1,
    77 		EDSA				= 2,
    78 		EDH					= 3,
    79 #ifdef SYMBIAN_ENABLE_SDP_WMDRM_SUPPORT
    80 		EECC                = 4,
    81 #endif
    82 		};
    83 
    84 	/** Flags for key access bitfield. */
    85 	enum EKeyAccess
    86 		{
    87 		EInvalidAccess		= 0x00,
    88 		ESensitive			= 0x01,
    89 		EExtractable		= 0x02,
    90 		EAlwaysSensitive	= 0x04,
    91 		ENeverExtractable	= 0x08,
    92 		ELocal				= 0x10
    93 		};
    94 
    95 public:
    96 	
    97 	inline TKeyIdentifier ID() const;						///< The ID (SHA-1 hash) of the key 
    98 	inline TKeyUsagePKCS15 Usage() const;					///< The key usage 
    99 	inline TUint Size() const;								///< The size of the key 
   100 	inline const TDesC& Label() const;						///< The key's label.
   101 	inline const TSecurityPolicy& UsePolicy() const; 		///< The security policy for key use 
   102 	inline const TSecurityPolicy& ManagementPolicy() const; ///< The security policy for key management 
   103 	inline EKeyAlgorithm Algorithm() const;					///< The key algorithm
   104 
   105 	/**
   106 	 * The key access type. The return code is bitfield made up of 0 or more
   107 	 * values from EKeyAccess ORed together.
   108 	 */
   109 	inline TInt AccessType() const;
   110 
   111 	/**
   112 	 * Returns whether the key is native.
   113 	 *
   114 	 * A native key is one where operations on the key are performed on the same
   115 	 * hardware as the the key is stored. For instance, if a key that is stored
   116 	 * on secure hardware but calculations are carried out on the main
   117 	 * processor, it isn't native.
   118 	 */
   119 	inline TBool Native() const;
   120 	
   121 	inline TTime StartDate() const;					///< The start time, or TTime(0) if not set
   122 	inline TTime EndDate() const;					///< The end time, or TTime(0) if not set
   123 	inline const TDesC8& PKCS8AttributeSet() const;	///< The PKCS#8 attribute set.
   124 
   125 	/**
   126 	 * Externalizes the key data to stream
   127 	 */
   128 	IMPORT_C void ExternalizeL(RWriteStream& aStream) const;
   129 
   130 public:
   131 
   132 	/**
   133 	 * Get the handle of the key.  This is a identifier that uniquely identifies
   134 	 * the key within one store, and is used to construct the token handle.
   135 	 */
   136 	inline TInt HandleID() const;
   137 
   138 	/**
   139 	 * Set the handle of the key.  Called by the token when a key is created.
   140 	 *
   141 	 * @param aHandle The new handle of the key
   142 	 */
   143 	inline void SetHandle(TInt aHandle);
   144 
   145 	/**
   146 	 * Called by the token when a key is created, to set the key identifier
   147 	 * (SHA-1 hash) of the new key
   148 	 *
   149 	 * @param aId The newly generated SHA-1 hash
   150 	 */
   151 	inline void SetIdentifier(TKeyIdentifier aId);
   152 
   153 	/**
   154 	 * Set the size of a key.
   155 	 */
   156 	inline void SetSize(TUint aSize);
   157 
   158 	/**
   159 	 * Set the key algorithm.
   160 	 */
   161 	inline void SetAlgorithm(EKeyAlgorithm aAlg);
   162 
   163 protected:
   164 
   165 	/**
   166 	 * Protected constructor, called by derived classes.
   167 	 */
   168 	IMPORT_C CKeyInfoBase(TKeyIdentifier aID,
   169 						  TKeyUsagePKCS15 aUsage,
   170 						  TUint aSize,
   171 						  HBufC* aLabel,
   172 						  TInt aHandle,
   173 						  const TSecurityPolicy& aUsePolicy,
   174 						  const TSecurityPolicy& aManagementPolicy,
   175 						  EKeyAlgorithm aAlgorithm,
   176 						  TInt aAccessType,
   177 						  TBool aNative,
   178 						  TTime aStartDate,
   179 						  TTime aEndDate,
   180 						  HBufC8* aPKCS8AttributeSet);
   181 
   182 	/**
   183 	 * Protected constructor, called by derived classes.
   184 	 */
   185 	IMPORT_C CKeyInfoBase();
   186 
   187 	/**
   188 	 * Second phase constructor.  Called by derived classes' NewL methods.
   189 	 */
   190 	IMPORT_C void ConstructL();
   191 
   192 	/**
   193 	 * Second phase constructor.  Called by derived classes' NewL methods.
   194 	 */
   195 	IMPORT_C void ConstructL(RReadStream& aIn);
   196 
   197 	/**
   198 	 * Destructor is protected so CCTKeyInfo can force users to call Release.
   199 	 */
   200 	IMPORT_C ~CKeyInfoBase();
   201 
   202 private:
   203 
   204 	/**
   205 	 * Internalizes key data from a stream.
   206 	 */
   207 	void InternalizeL(RReadStream& aStream);
   208 	
   209 protected:
   210 	TKeyIdentifier iID;					///< The ID of the key
   211 	TKeyUsagePKCS15 iUsage;				///< The usage of the key 
   212 	TUint iSize;						///< The size of the key 
   213 	HBufC* iLabel;						///< The identifying label of the key
   214 	TInt iHandle;						///< The handle of the key
   215 	TSecurityPolicy iUsePolicy;			///< The secutity policy for key use
   216 	TSecurityPolicy iManagementPolicy;	///< The secutity policy for key management
   217 	EKeyAlgorithm iAlgorithm;			///< The key type (RSA, DSA etc)
   218 	TInt iAccessType;					///< Key sensitivity
   219 	TBool iNative;						///< Key is native (cryptographic operations are carried out on the store)
   220 	TTime iStartDate;					///< Key Start Date 
   221 	TTime iEndDate;						///< Key end date 
   222 	HBufC8* iPKCS8AttributeSet;			///< Attributes as a DER-encoded set
   223 	};
   224 
   225 /**
   226  * Information about a key, as returned by MCTKeyStore::List.
   227  *
   228  */
   229 class CCTKeyInfo : public CKeyInfoBase, public MCTTokenObject
   230 	{
   231 public:
   232 
   233 	/**
   234 	 * 
   235 	 * Creates a CCTKeyInfo from constituents.  This is called by the unified
   236 	 * key store, and should not be called directly.
   237 	 *
   238 	 * @param aID The SHA1 hash of the key
   239 	 * @param aUsage The usage of the key
   240 	 * @param aSize The size of the key in bytes
   241 	 * @param aProtector A protector object if the key is protected by a PIN.
   242 	 * This may be NULL if the protector is not known.
   243 	 * @param aLabel The label of the key (takes ownership).
   244 	 * @param aToken The token the key is in
   245 	 * @param aHandle The object ID part of the object handle; an
   246 	 * integer that is unique amongst keys in this token.
   247 	 * @param aUsePolicy The security policy for key use
   248 	 * @param aManagementPolicy The security policy for key management
   249 	 * @param aAlgorithm The key algorithm (RSA, DSA or Diffie-Hellman)
   250 	 * @param aAccessType The access type of the key
   251 	 * @param aNative	Defines whether the key is native
   252 	 * @param aStartDate The key validity start date
   253 	 * @param aEndDate The key validity end date
   254 	 * @param aPKCS8AttributeSet (optional) The DER encoded PKCS8 attribute set
   255 	 * (takes ownership).
   256 	 *
   257 	 * @leave KErrKeyUsage If the key usage flags are not valid or not
   258 	 * consistent with the key algorithm.
   259 	 * @leave KErrKeyValidity If the validity start and end dates are specified
   260 	 * but do not form a valid time period.
   261 	 */
   262 	IMPORT_C static CCTKeyInfo* NewL(TKeyIdentifier aID,
   263 									 TKeyUsagePKCS15 aUsage,
   264 									 TUint aSize,
   265 									 MCTAuthenticationObject* aProtector,
   266 									 HBufC* aLabel,
   267 									 MCTToken& aToken,
   268 									 TInt aHandle,
   269 									 const TSecurityPolicy& aUsePolicy,
   270 									 const TSecurityPolicy& aManagementPolicy,
   271 									 EKeyAlgorithm aAlgorithm,
   272 									 TInt aAccessType,
   273 									 TBool aNative,
   274 									 TTime aStartDate,
   275 									 TTime aEndDate,
   276 									 HBufC8* aPKCS8AttributeSet = NULL);
   277 
   278 	/**
   279 	 * Creates a new KeyInfo from a stream.
   280 	 *
   281 	 * @param aStream The stream to read the key data from
   282 	 * @param aToken The token that the key is in
   283 	 *
   284 	 * @leave KErrKeyUsage If the key usage flags are not valid or not
   285 	 * consistent with the key algorithm.
   286 	 * @leave KErrKeyValidity If the validity start and end dates are specified
   287 	 * but do not form a valid time period.
   288 	 */
   289 	IMPORT_C static CCTKeyInfo* NewL(RReadStream& aStream, MCTToken& aToken);
   290 
   291 public:
   292 
   293 	/**
   294 	 * The PIN (or other authentication object) that protects the key, or NULL
   295 	 * if not set.  This object is owned by key store.
   296 	 */
   297 	inline MCTAuthenticationObject* Protector() const;
   298 
   299 	/**
   300 	 * Sets the authentication object for this key.  The object's Release method
   301 	 * will be called by the destructor, allowing for refence counting of auth
   302 	 * objects to be implemented if desired.
   303 	 */
   304 	inline void SetProtector(MCTAuthenticationObject& aProtector);
   305 	
   306 	/** The CT handle to this object. */
   307 	inline operator TCTTokenObjectHandle() const;
   308 
   309 public:
   310 
   311 	// from MCTTokenObject
   312 
   313 	/** The label of the key */
   314  	virtual const TDesC& Label() const;
   315 
   316 	/** The token the key is in */
   317 	virtual MCTToken& Token() const;
   318 
   319 	/** Returns KKeyInfoUID to indicate this is a key info object */
   320 	virtual TUid Type() const;
   321 
   322 	/**
   323 	 * A handle for the key. This can be used to identify a key to
   324 	 * another process.
   325 	 */
   326 	virtual TCTTokenObjectHandle Handle() const;
   327 
   328 private:
   329 
   330 	CCTKeyInfo(TKeyIdentifier aID,
   331 			   TKeyUsagePKCS15 aUsage,
   332 			   TUint aSize,
   333 			   MCTAuthenticationObject* aProtector,
   334 			   HBufC* aLabel,
   335 			   MCTToken& aToken,
   336 			   TInt aHandle,
   337 			   const TSecurityPolicy& aUsePolicy,
   338 			   const TSecurityPolicy& aManagementPolicy,
   339 			   EKeyAlgorithm aAlgorithm,
   340 			   TInt aAccessType,
   341 			   TBool aNative,
   342 			   TTime aStartDate,
   343 			   TTime aEndDate,
   344 			   HBufC8* aPKCS8AttributeSet);
   345 
   346 	CCTKeyInfo(MCTToken& aToken);
   347 
   348 	~CCTKeyInfo();
   349 
   350 private:
   351 
   352 	/** The token the key is in*/
   353 	MCTToken& iToken;
   354 
   355 	/** The protector object of the key. This pointer is not owned by the class */
   356 	MCTAuthenticationObject* iProtector;
   357 };
   358 
   359 /**
   360  * A filter to specify which keys should be returned from the store by
   361  * MCTKeyStore::List.
   362  *
   363  */
   364 struct TCTKeyAttributeFilter
   365 	{
   366 	enum TPolicyFilter
   367 		{
   368 		EAllKeys,
   369 		EUsableKeys,
   370 		EManageableKeys,
   371 		EUsableOrManageableKeys
   372 		};
   373 
   374 	/** Constructor */
   375 	IMPORT_C TCTKeyAttributeFilter();
   376 
   377 	/**
   378 	 * The hash of the key we're looking for. A zero-length descriptor means
   379 	 * 'don't care
   380 	 */
   381 	TKeyIdentifier iKeyId;
   382 
   383 	/**
   384 	 * The required usage of the key. A key must match any of the usages
   385 	 * specified. Use EAllUsages to return all usages.
   386 	 */
   387 	TKeyUsagePKCS15 iUsage;
   388 
   389 	/**
   390 	 * Filter returned keys by the operations the calling process is allowed to
   391 	 * perform on them (as determined by the security policies set on them).
   392 	 * 
   393 	 * The default is EUsableKeys.  Note that if this is to set to KAllKeys, all
   394 	 * keys will be returned, including those that are unusable by the calling
   395 	 * process.
   396 	 */
   397 	TPolicyFilter iPolicyFilter;
   398 
   399 	/** The algorithm. EInvalidAlgorithm indicates 'don't care */
   400 	CCTKeyInfo::EKeyAlgorithm iKeyAlgorithm;
   401 	};
   402 
   403 #include "mctkeystore.inl"
   404 
   405 #endif // __MCTKEYSTORE_H__