os/security/cryptoservices/filebasedcertificateandkeystores/test/keytool/keytool_migratestore.inl
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/security/cryptoservices/filebasedcertificateandkeystores/test/keytool/keytool_migratestore.inl Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,102 @@
1.4 +/*
1.5 +* Copyright (c) 2004-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 +*
1.19 +*/
1.20 +
1.21 +
1.22 +#include <mctkeystore.h>
1.23 +#include <f32file.h>
1.24 +#include <s32mem.h>
1.25 +
1.26 +#include <authserver/authtypes.h>
1.27 +#include <centralrepository.h>
1.28 +#include <authserver/authclient.h>
1.29 +#include <authserver/authexpression.h>
1.30 +#include <authserver/auth_srv_errs.h>
1.31 +#include <authserver/aspubsubdefs.h>
1.32 +#include <authserver/authtypes.h>
1.33 +#include <pbedata.h>
1.34 +#include <mctkeystore.h>
1.35 +#include <keystore_errs.h>
1.36 +#include <securityerr.h>
1.37 +#include <keytool.rsg>
1.38 +
1.39 +#include "keystorecenrepconfig.h"
1.40 +#include "fsdatatypes.h"
1.41 +#include "ckeydatamanager.h"
1.42 +#include "keystorepassphrase.h"
1.43 +#include "keystreamutils.h"
1.44 +#include "asymmetrickeys.h"
1.45 +
1.46 +#include "keytool_utils.h"
1.47 +#include "keytool_commands.h"
1.48 +#include "keytool_controller.h"
1.49 +
1.50 +// the size allocated to retrieve the private key.
1.51 +const TInt KSize = 2048;
1.52 +
1.53 +template <class T> inline void CKeytoolMigrateStore::RetrieveAndStorePublicKeyL( CFileKeyData* aKeyData, T* aPublicKey)
1.54 + {
1.55 + // open the publickeystream
1.56 + RStoreReadStream publicStream;
1.57 + publicStream.OpenLC(*iReadFileStore, aKeyData->PublicDataStreamId());
1.58 +
1.59 + CreateL(publicStream,aPublicKey);
1.60 + CleanupStack::PushL(aPublicKey);
1.61 + RStoreWriteStream writePublicStream;
1.62 + TStreamId publicStrId = writePublicStream.CreateLC(*iWriteFileStore);
1.63 + aKeyData->SetPublicDataStreamId(publicStrId);
1.64 + writePublicStream << *aPublicKey;
1.65 + writePublicStream.CommitL();
1.66 + CleanupStack::PopAndDestroy(3,&publicStream); // aPublicKey, writePublicStream
1.67 + }
1.68 +
1.69 +template <class T> inline void CKeytoolMigrateStore::RetrieveAndStorePrivateKeyL( CFileKeyData* aKeyData, T* aPrivateKey)
1.70 + {
1.71 + // open the privatekeystream based on the supplied passphrase
1.72 + RStoreReadStream privateStream;
1.73 + privateStream.OpenLC(iPassphrase->Store(), aKeyData->PrivateDataStreamId());
1.74 +
1.75 + CreateL(privateStream,aPrivateKey);
1.76 + CleanupStack::PushL(aPrivateKey);
1.77 + RStoreWriteStream writePrivateStream;
1.78 + TStreamId pvtStrId = writePrivateStream.CreateLC(*iWriteFileStore);
1.79 + aKeyData->SetPrivateDataStreamId(pvtStrId);
1.80 + EncryptAndStoreL(*aPrivateKey, writePrivateStream );
1.81 + writePrivateStream.CommitL();
1.82 + CleanupStack::PopAndDestroy(3,&privateStream); // aPrivateKey, writePrivateStream
1.83 + }
1.84 +
1.85 +template <class T> inline void CKeytoolMigrateStore::EncryptAndStoreL(const T& aKey, RStoreWriteStream& aStream )
1.86 + {
1.87 + RMemWriteStream writeStream;
1.88 + CleanupClosePushL(writeStream);
1.89 + // create the buffer for storing the encrypted private key
1.90 + HBufC8* privateKey = HBufC8::NewLC(KSize);
1.91 + TPtr8 keyPtr(privateKey->Des());
1.92 + keyPtr.FillZ(KSize);
1.93 +
1.94 + // a pointer to the data is required to pass it to the stream
1.95 + TAny* ptr = const_cast<TAny*>(static_cast<const TAny*>(privateKey->Des().Ptr()));
1.96 + writeStream.Open( ptr,KSize);
1.97 + writeStream << aKey;
1.98 + writeStream.CommitL();
1.99 + TStreamPos pos = writeStream.Sink()->TellL(MStreamBuf::EWrite);
1.100 + keyPtr.SetLength(pos.Offset());
1.101 + StoreKeyL(keyPtr, aStream);
1.102 + CleanupStack::PopAndDestroy(2, &writeStream); // privateKey
1.103 +
1.104 + }
1.105 +