authenticationservices/authenticationserver/source/common/protectionkey.cpp
changeset 102 deec7e509f66
parent 94 0e6c5a9328b5
child 108 ca9a0fc2f082
equal deleted inserted replaced
94:0e6c5a9328b5 102:deec7e509f66
     1 /*
       
     2 * Copyright (c) 2005-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 * CProtectionKey implementation
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 /**
       
    21  @file 
       
    22 */
       
    23 
       
    24 #include "authcommon_impl.h"
       
    25 #include <hash.h>
       
    26 
       
    27 using namespace AuthServer;
       
    28 
       
    29 /**
       
    30  * Generate a new key upon construction
       
    31  * @param aKeySize the length of the key in bits.  
       
    32  **/
       
    33 EXPORT_C CProtectionKey* CProtectionKey::NewL(TInt aKeySize)
       
    34 	{
       
    35 	CProtectionKey* key = NewLC(aKeySize);
       
    36 	CleanupStack::Pop(key);
       
    37 	return key;
       
    38 	}
       
    39 /**
       
    40  * Generate a new key upon construction
       
    41  * @param aKeySize the length of the key in bits.  
       
    42  **/
       
    43 EXPORT_C CProtectionKey* CProtectionKey::NewLC(TInt aKeySize)
       
    44 	{
       
    45 	CProtectionKey* key = new (ELeave) CProtectionKey();
       
    46 	CleanupStack::PushL(key);
       
    47 	key->ConstructL(aKeySize);
       
    48 	return key;
       
    49 	}
       
    50 
       
    51 /**
       
    52  * Creates a new key, passing in the key data. Should
       
    53  * really only be called by a CTransientKey::EncryptL. The new object
       
    54  * assumes ownership of the data.
       
    55  *
       
    56  * @param aKeyData the data to use as the key
       
    57  **/
       
    58 EXPORT_C CProtectionKey* CProtectionKey::NewL(HBufC8* aKeyData)
       
    59 	{
       
    60 	CProtectionKey*  key =
       
    61 	  CProtectionKey::NewLC(aKeyData);
       
    62 	CleanupStack::Pop(key);
       
    63 	return key;
       
    64 	}
       
    65 
       
    66 /**
       
    67  * Creates a new key, passing in the key data. Should
       
    68  * really only be called by a CTransientKey::EncryptL. The new object
       
    69  * assumes ownership of the data.
       
    70  *
       
    71  * @param aKeyData the data to use as the key
       
    72  **/
       
    73 EXPORT_C CProtectionKey* CProtectionKey::NewLC(HBufC8* aKeyData)
       
    74 	{
       
    75 	CProtectionKey*  key = new (ELeave) CProtectionKey();
       
    76 	CleanupStack::PushL(key);
       
    77 	key->ConstructL(aKeyData);
       
    78 	return key;
       
    79 	}
       
    80 /**
       
    81  * Constructor
       
    82  */
       
    83 CProtectionKey::CProtectionKey()
       
    84 	{
       
    85 	}
       
    86 
       
    87 /**
       
    88  * Creates a new random key of aKeySize
       
    89  * @param aKeySize the size of the key to generate
       
    90  **/
       
    91 void CProtectionKey::ConstructL(TInt aKeySize)
       
    92 	{
       
    93 	iKeyData = HBufC8::NewMaxL(aKeySize);
       
    94 	TPtr8 data = iKeyData->Des();
       
    95 	TRandom::RandomL(data);
       
    96 	}
       
    97 
       
    98 /**
       
    99  * Deletes the keydata.
       
   100  **/
       
   101 CProtectionKey::~CProtectionKey()
       
   102 	{
       
   103 	delete iKeyData;
       
   104 	}
       
   105 
       
   106 
       
   107 /**
       
   108  * Creates a new protection key generated using a combination of this key
       
   109  * and the client UID. Ownership of the returned object is passed to the 
       
   110  * caller.
       
   111  *
       
   112  * @param aClientUid the client uid to use in the generation process.
       
   113  *
       
   114  * @return a new heap allocated protection key 
       
   115  **/
       
   116 EXPORT_C CProtectionKey* CProtectionKey::ClientKeyL(TInt aClientUid) const
       
   117 	{
       
   118 	CSHA1* sha1 = CSHA1::NewL();
       
   119 	CleanupStack::PushL(sha1);
       
   120 
       
   121 	TPckg<TIdentityId> idPckg(aClientUid);
       
   122 	
       
   123 	sha1->Update(idPckg);
       
   124 	sha1->Update(*iKeyData);
       
   125 
       
   126 	TInt keyLength = iKeyData->Length();
       
   127 	
       
   128 	HBufC8* newKey = HBufC8::NewLC(keyLength);
       
   129 
       
   130 	TInt remaining = keyLength;
       
   131 	do
       
   132 	  {
       
   133 		newKey->Des().Append(sha1->Hash(idPckg).Left(remaining));
       
   134 		remaining = keyLength - newKey->Length(); 
       
   135 	  } while ( remaining > 0);
       
   136 		  
       
   137 	CProtectionKey* key = NewL(newKey);
       
   138 	CleanupStack::Pop(newKey);
       
   139 	CleanupStack::PopAndDestroy(sha1);
       
   140 	return key;
       
   141 	}
       
   142 	
       
   143 /**
       
   144  * Returns a descriptor to the key data for use in encryption and 
       
   145  * decryption methods. 
       
   146  *
       
   147  * @return a descripter pointing to the key data. 
       
   148  **/
       
   149 EXPORT_C TPtrC8 CProtectionKey::KeyData() const
       
   150     {
       
   151 	return *iKeyData;
       
   152     }
       
   153 
       
   154 /**
       
   155  * Constructs the key using supplied data.
       
   156  **/
       
   157 void CProtectionKey::ConstructL(HBufC8* aKeyData)
       
   158 	{
       
   159 	__ASSERT_DEBUG(((iKeyData == 0) &&
       
   160 				   (aKeyData != 0)),
       
   161 					User::Leave(KErrArgument));
       
   162 	iKeyData = aKeyData;
       
   163     }
       
   164