authenticationservices/authenticationserver/source/server/transientkey.cpp
changeset 19 ece3df019add
equal deleted inserted replaced
17:cd501b96611d 19:ece3df019add
       
     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 * CTransientKey implementation
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 /**
       
    21  @file 
       
    22 */
       
    23 
       
    24 #include "authserver_impl.h"
       
    25 
       
    26 
       
    27 using namespace AuthServer;
       
    28 
       
    29 CTransientKey* CTransientKey::NewL(CPBEncryptElement* aEncryptor)
       
    30 	{
       
    31 	CTransientKey*  key =
       
    32 		CTransientKey::NewLC(aEncryptor);
       
    33 	CleanupStack::Pop(key);
       
    34 	return key;
       
    35 	}
       
    36 
       
    37 CTransientKey* CTransientKey::NewLC(CPBEncryptElement* aEncryptor)
       
    38 	{
       
    39 	CTransientKey*  key = new (ELeave) CTransientKey();
       
    40 	CleanupStack::PushL(key);
       
    41 	key->ConstructL(aEncryptor);
       
    42 	return key;
       
    43 	}
       
    44 
       
    45 void CTransientKey::ConstructL(CPBEncryptElement* aEncryptor)
       
    46 	{
       
    47 	iPbeElement = aEncryptor;
       
    48     }
       
    49 
       
    50 CTransientKey::~CTransientKey()
       
    51 	{
       
    52 	delete iPbeElement;
       
    53 	}
       
    54 
       
    55 CEncryptedProtectionKey*
       
    56 CTransientKey::EncryptL(const CProtectionKey& aKey) const
       
    57 	{
       
    58 	TPtrC8 protData = aKey.KeyData();
       
    59 	
       
    60 	CPBEncryptor* encryptor = iPbeElement->NewEncryptLC();
       
    61 	
       
    62 	HBufC8* ciphertext =
       
    63 		HBufC8::NewLC(encryptor->MaxFinalOutputLength(protData.Length()));
       
    64 
       
    65 	TPtr8 ciphertextTemp = ciphertext->Des();
       
    66 	
       
    67 	encryptor->ProcessFinalL(protData, ciphertextTemp);
       
    68 
       
    69 	CEncryptedProtectionKey* key = CEncryptedProtectionKey::NewL(ciphertext);
       
    70 	
       
    71 	CleanupStack::Pop(ciphertext); // don't delete cipher text
       
    72 	CleanupStack::PopAndDestroy(encryptor);
       
    73 	
       
    74 	return key;
       
    75 	}
       
    76 
       
    77 CProtectionKey*
       
    78 CTransientKey::DecryptL(const CEncryptedProtectionKey& aKey) const
       
    79 	{
       
    80 	TPtrC8 encData = aKey.KeyData();
       
    81 	
       
    82 	CPBDecryptor* decryptor = iPbeElement->NewDecryptLC();
       
    83 
       
    84 	HBufC8* plaintext =
       
    85 		HBufC8::NewLC(decryptor->MaxFinalOutputLength(encData.Length()));
       
    86 	TPtr8 plaintextTemp = plaintext->Des();
       
    87 
       
    88 	// Decrypt the data
       
    89 	decryptor->ProcessFinalL(encData, plaintextTemp);
       
    90 		 
       
    91 	CProtectionKey* key = CProtectionKey::NewL(plaintext);
       
    92 	CleanupStack::Pop(plaintext);
       
    93 	CleanupStack::PopAndDestroy(decryptor);
       
    94 	return key;
       
    95 	}