os/security/cryptoservices/filebasedcertificateandkeystores/test/thwkeystore/common/tkeydetails.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) 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 * Defines the class which represents the structure of the key on 
    16 * which Crypto Token HAI internally operates. It contains the key 
    17 * information relevant to Crypto Token HAI.
    18 *
    19 */
    20 
    21 
    22 #include "tkeydetails.h"
    23 #include <mctkeystore.h>
    24 
    25 CKeyDetails::CKeyDetails()
    26 	{}
    27 
    28 CKeyDetails::CKeyDetails(	
    29 		TKeyIdentifier aID,
    30 		TKeyUsagePKCS15 aUsage,
    31 		TUint aSize, 
    32 		HBufC* aLabel,
    33 		TInt aHandle,
    34 		const TSecurityPolicy& aUsePolicy,
    35 		const TSecurityPolicy& aManagementPolicy,
    36 		EKeyAlgorithm aAlgorithm,
    37 		TInt aAccessType,
    38 		TBool aNative,
    39 		TTime aStartDate,
    40 		TTime aEndDate,
    41 		HBufC8* aPKCS8AttributeSet)
    42 		: CKeyInfoBase(	aID,aUsage,aSize,aLabel,aHandle,
    43 						aUsePolicy,aManagementPolicy,aAlgorithm,
    44 						aAccessType,aNative,aStartDate,aEndDate,aPKCS8AttributeSet)
    45 		{}
    46 
    47 CKeyDetails::~CKeyDetails()
    48 	{
    49 	delete iPrivateKey;
    50 	delete iPublicKey;
    51 	}
    52 
    53 TInt CKeyDetails::Handle() const
    54 	{
    55 	return iHandle;
    56 	}
    57 
    58 HBufC8* CKeyDetails::PrivateKey() const
    59 	{
    60 	return iPrivateKey;
    61 	}
    62 
    63 HBufC8* CKeyDetails::PublicKey() const
    64     {
    65     return iPublicKey;
    66     }
    67 
    68 CKeyDetails* CKeyDetails::NewL(	TInt aHandle, 
    69 								const TDesC& aLabel, 
    70 								const TDesC8& aPrivateKey, 
    71 								const TDesC8& aPublicKey )
    72 	{
    73 	TKeyIdentifier keyID;
    74 	keyID.FillZ(keyID.MaxSize());
    75 	TKeyUsagePKCS15 usage = EPKCS15UsageNone;
    76 	TUint size = 0;
    77 	TInt handle = aHandle;
    78 	const TSecurityPolicy& usePolicy = TSecurityPolicy::EAlwaysPass;
    79 	const TSecurityPolicy& managementPolicy = TSecurityPolicy::EAlwaysPass;
    80 	EKeyAlgorithm algorithm = EECC;
    81 	TInt accessType = CKeyInfoBase::ENeverExtractable;
    82 	accessType |= CKeyInfoBase::ELocal;
    83 	TBool native = ETrue;
    84 	TTime startDate = 0;
    85 	TTime endDate = 0;
    86 	HBufC8* pkcs8AttributeSet = NULL;
    87 	
    88 	HBufC* label = HBufC::NewLC(aLabel.Length());
    89 	label->Des().Copy(aLabel);
    90 
    91 	CKeyDetails* keyDetails = new (ELeave) CKeyDetails(keyID,usage,size,label,handle,usePolicy,managementPolicy,algorithm,accessType,native,startDate,endDate,pkcs8AttributeSet);
    92 	
    93 	CleanupStack::Pop(label);
    94 	CleanupStack::PushL(keyDetails);
    95 	keyDetails->ConstructL(aPrivateKey, aPublicKey);
    96 	CleanupStack::Pop(keyDetails);
    97 	return keyDetails;
    98 	}
    99 
   100 
   101 CKeyDetails* CKeyDetails::NewL(RStoreReadStream& aReadStream)
   102     {
   103     CKeyDetails* self = new (ELeave) CKeyDetails();
   104     CleanupStack::PushL(self);
   105     self->ConstructL(aReadStream);
   106     CleanupStack::Pop(self);
   107     return (self);
   108     }
   109 
   110 void CKeyDetails::ConstructL( const TDesC8& aPrivateKey, const TDesC8& aPublicKey )
   111 	{
   112 	iPrivateKey = aPrivateKey.AllocL();
   113 	iPublicKey = aPublicKey.AllocL();   
   114 	}
   115 
   116 void CKeyDetails::ConstructL(RStoreReadStream& aReadStream)
   117 	{
   118 	CKeyInfoBase::ConstructL(aReadStream);
   119 	InternalizeL(aReadStream);
   120 	}
   121 
   122 void CKeyDetails::ExternalizeL(RWriteStream& aWriteStream) const
   123     {		
   124     CKeyInfoBase::ExternalizeL(aWriteStream);
   125     
   126     TInt stringLen = iPrivateKey->Length();
   127     aWriteStream.WriteInt32L(stringLen);
   128     TPtr8 keyPtr = iPrivateKey->Des();
   129     keyPtr.SetLength(stringLen);
   130     aWriteStream.WriteL(keyPtr);
   131         
   132     stringLen = iPublicKey->Length();
   133     aWriteStream.WriteInt32L(stringLen);
   134     keyPtr = iPublicKey->Des();
   135     keyPtr.SetLength(stringLen);
   136     aWriteStream.WriteL(keyPtr);
   137     
   138     }
   139 
   140 void CKeyDetails::InternalizeL(RReadStream& aReadStream)
   141     {
   142     TInt stringLen = aReadStream.ReadInt32L();
   143     iPrivateKey = HBufC8::NewMaxL(stringLen);
   144     TPtr8 privateKeyPtr((TUint8*)iPrivateKey->Ptr(), stringLen, stringLen);
   145     privateKeyPtr.FillZ(stringLen);
   146     aReadStream.ReadL(privateKeyPtr);
   147         
   148     stringLen = aReadStream.ReadInt32L();
   149     iPublicKey = HBufC8::NewMaxL(stringLen);
   150     TPtr8 publicKeyPtr((TUint8*)iPublicKey->Ptr(), stringLen, stringLen);
   151     publicKeyPtr.FillZ(stringLen);
   152     aReadStream.ReadL(publicKeyPtr);
   153     
   154     }