os/security/cryptoservices/filebasedcertificateandkeystores/test/thwkeystore/common/tkeydetails.cpp
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/security/cryptoservices/filebasedcertificateandkeystores/test/thwkeystore/common/tkeydetails.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,154 @@
1.4 +/*
1.5 +* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
1.6 +* All rights reserved.
1.7 +* This component and the accompanying materials are made available
1.8 +* under the terms of the License "Eclipse Public License v1.0"
1.9 +* which accompanies this distribution, and is available
1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.11 +*
1.12 +* Initial Contributors:
1.13 +* Nokia Corporation - initial contribution.
1.14 +*
1.15 +* Contributors:
1.16 +*
1.17 +* Description:
1.18 +* Defines the class which represents the structure of the key on
1.19 +* which Crypto Token HAI internally operates. It contains the key
1.20 +* information relevant to Crypto Token HAI.
1.21 +*
1.22 +*/
1.23 +
1.24 +
1.25 +#include "tkeydetails.h"
1.26 +#include <mctkeystore.h>
1.27 +
1.28 +CKeyDetails::CKeyDetails()
1.29 + {}
1.30 +
1.31 +CKeyDetails::CKeyDetails(
1.32 + TKeyIdentifier aID,
1.33 + TKeyUsagePKCS15 aUsage,
1.34 + TUint aSize,
1.35 + HBufC* aLabel,
1.36 + TInt aHandle,
1.37 + const TSecurityPolicy& aUsePolicy,
1.38 + const TSecurityPolicy& aManagementPolicy,
1.39 + EKeyAlgorithm aAlgorithm,
1.40 + TInt aAccessType,
1.41 + TBool aNative,
1.42 + TTime aStartDate,
1.43 + TTime aEndDate,
1.44 + HBufC8* aPKCS8AttributeSet)
1.45 + : CKeyInfoBase( aID,aUsage,aSize,aLabel,aHandle,
1.46 + aUsePolicy,aManagementPolicy,aAlgorithm,
1.47 + aAccessType,aNative,aStartDate,aEndDate,aPKCS8AttributeSet)
1.48 + {}
1.49 +
1.50 +CKeyDetails::~CKeyDetails()
1.51 + {
1.52 + delete iPrivateKey;
1.53 + delete iPublicKey;
1.54 + }
1.55 +
1.56 +TInt CKeyDetails::Handle() const
1.57 + {
1.58 + return iHandle;
1.59 + }
1.60 +
1.61 +HBufC8* CKeyDetails::PrivateKey() const
1.62 + {
1.63 + return iPrivateKey;
1.64 + }
1.65 +
1.66 +HBufC8* CKeyDetails::PublicKey() const
1.67 + {
1.68 + return iPublicKey;
1.69 + }
1.70 +
1.71 +CKeyDetails* CKeyDetails::NewL( TInt aHandle,
1.72 + const TDesC& aLabel,
1.73 + const TDesC8& aPrivateKey,
1.74 + const TDesC8& aPublicKey )
1.75 + {
1.76 + TKeyIdentifier keyID;
1.77 + keyID.FillZ(keyID.MaxSize());
1.78 + TKeyUsagePKCS15 usage = EPKCS15UsageNone;
1.79 + TUint size = 0;
1.80 + TInt handle = aHandle;
1.81 + const TSecurityPolicy& usePolicy = TSecurityPolicy::EAlwaysPass;
1.82 + const TSecurityPolicy& managementPolicy = TSecurityPolicy::EAlwaysPass;
1.83 + EKeyAlgorithm algorithm = EECC;
1.84 + TInt accessType = CKeyInfoBase::ENeverExtractable;
1.85 + accessType |= CKeyInfoBase::ELocal;
1.86 + TBool native = ETrue;
1.87 + TTime startDate = 0;
1.88 + TTime endDate = 0;
1.89 + HBufC8* pkcs8AttributeSet = NULL;
1.90 +
1.91 + HBufC* label = HBufC::NewLC(aLabel.Length());
1.92 + label->Des().Copy(aLabel);
1.93 +
1.94 + CKeyDetails* keyDetails = new (ELeave) CKeyDetails(keyID,usage,size,label,handle,usePolicy,managementPolicy,algorithm,accessType,native,startDate,endDate,pkcs8AttributeSet);
1.95 +
1.96 + CleanupStack::Pop(label);
1.97 + CleanupStack::PushL(keyDetails);
1.98 + keyDetails->ConstructL(aPrivateKey, aPublicKey);
1.99 + CleanupStack::Pop(keyDetails);
1.100 + return keyDetails;
1.101 + }
1.102 +
1.103 +
1.104 +CKeyDetails* CKeyDetails::NewL(RStoreReadStream& aReadStream)
1.105 + {
1.106 + CKeyDetails* self = new (ELeave) CKeyDetails();
1.107 + CleanupStack::PushL(self);
1.108 + self->ConstructL(aReadStream);
1.109 + CleanupStack::Pop(self);
1.110 + return (self);
1.111 + }
1.112 +
1.113 +void CKeyDetails::ConstructL( const TDesC8& aPrivateKey, const TDesC8& aPublicKey )
1.114 + {
1.115 + iPrivateKey = aPrivateKey.AllocL();
1.116 + iPublicKey = aPublicKey.AllocL();
1.117 + }
1.118 +
1.119 +void CKeyDetails::ConstructL(RStoreReadStream& aReadStream)
1.120 + {
1.121 + CKeyInfoBase::ConstructL(aReadStream);
1.122 + InternalizeL(aReadStream);
1.123 + }
1.124 +
1.125 +void CKeyDetails::ExternalizeL(RWriteStream& aWriteStream) const
1.126 + {
1.127 + CKeyInfoBase::ExternalizeL(aWriteStream);
1.128 +
1.129 + TInt stringLen = iPrivateKey->Length();
1.130 + aWriteStream.WriteInt32L(stringLen);
1.131 + TPtr8 keyPtr = iPrivateKey->Des();
1.132 + keyPtr.SetLength(stringLen);
1.133 + aWriteStream.WriteL(keyPtr);
1.134 +
1.135 + stringLen = iPublicKey->Length();
1.136 + aWriteStream.WriteInt32L(stringLen);
1.137 + keyPtr = iPublicKey->Des();
1.138 + keyPtr.SetLength(stringLen);
1.139 + aWriteStream.WriteL(keyPtr);
1.140 +
1.141 + }
1.142 +
1.143 +void CKeyDetails::InternalizeL(RReadStream& aReadStream)
1.144 + {
1.145 + TInt stringLen = aReadStream.ReadInt32L();
1.146 + iPrivateKey = HBufC8::NewMaxL(stringLen);
1.147 + TPtr8 privateKeyPtr((TUint8*)iPrivateKey->Ptr(), stringLen, stringLen);
1.148 + privateKeyPtr.FillZ(stringLen);
1.149 + aReadStream.ReadL(privateKeyPtr);
1.150 +
1.151 + stringLen = aReadStream.ReadInt32L();
1.152 + iPublicKey = HBufC8::NewMaxL(stringLen);
1.153 + TPtr8 publicKeyPtr((TUint8*)iPublicKey->Ptr(), stringLen, stringLen);
1.154 + publicKeyPtr.FillZ(stringLen);
1.155 + aReadStream.ReadL(publicKeyPtr);
1.156 +
1.157 + }