os/security/cryptoservices/filebasedcertificateandkeystores/test/keytool/keytool_migratestore.inl
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) 2004-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 *
    16 */
    17 
    18 
    19 #include <mctkeystore.h>
    20 #include <f32file.h>
    21 #include <s32mem.h>
    22 
    23 #include <authserver/authtypes.h>
    24 #include <centralrepository.h>
    25 #include <authserver/authclient.h>
    26 #include <authserver/authexpression.h>
    27 #include <authserver/auth_srv_errs.h>
    28 #include <authserver/aspubsubdefs.h>
    29 #include <authserver/authtypes.h>
    30 #include <pbedata.h>
    31 #include <mctkeystore.h>
    32 #include <keystore_errs.h>
    33 #include <securityerr.h>
    34 #include <keytool.rsg>
    35 
    36 #include "keystorecenrepconfig.h"
    37 #include "fsdatatypes.h"
    38 #include "ckeydatamanager.h"
    39 #include "keystorepassphrase.h"
    40 #include "keystreamutils.h"
    41 #include "asymmetrickeys.h"
    42 
    43 #include "keytool_utils.h"
    44 #include "keytool_commands.h"
    45 #include "keytool_controller.h"
    46 
    47 // the size allocated to retrieve the private key.
    48 const TInt KSize = 2048;
    49 
    50 template <class T> inline void CKeytoolMigrateStore::RetrieveAndStorePublicKeyL( CFileKeyData* aKeyData, T* aPublicKey)
    51 	{	
    52 	// open the publickeystream
    53 	RStoreReadStream publicStream;
    54 	publicStream.OpenLC(*iReadFileStore, aKeyData->PublicDataStreamId());
    55 			
    56 	CreateL(publicStream,aPublicKey);
    57 	CleanupStack::PushL(aPublicKey);
    58 	RStoreWriteStream writePublicStream;
    59 	TStreamId publicStrId = writePublicStream.CreateLC(*iWriteFileStore);
    60 	aKeyData->SetPublicDataStreamId(publicStrId);
    61 	writePublicStream << *aPublicKey;
    62 	writePublicStream.CommitL();
    63 	CleanupStack::PopAndDestroy(3,&publicStream); // aPublicKey, writePublicStream
    64 	}
    65 
    66 template <class T> inline void CKeytoolMigrateStore::RetrieveAndStorePrivateKeyL( CFileKeyData* aKeyData, T* aPrivateKey)
    67 	{	
    68 	// open the privatekeystream based on the supplied passphrase
    69 	RStoreReadStream privateStream;
    70 	privateStream.OpenLC(iPassphrase->Store(), aKeyData->PrivateDataStreamId());
    71 					
    72 	CreateL(privateStream,aPrivateKey);
    73 	CleanupStack::PushL(aPrivateKey);
    74 	RStoreWriteStream writePrivateStream;
    75 	TStreamId pvtStrId = writePrivateStream.CreateLC(*iWriteFileStore);
    76 	aKeyData->SetPrivateDataStreamId(pvtStrId);
    77 	EncryptAndStoreL(*aPrivateKey, writePrivateStream );
    78 	writePrivateStream.CommitL();
    79 	CleanupStack::PopAndDestroy(3,&privateStream); // aPrivateKey, writePrivateStream
    80 	}
    81 
    82 template <class T> inline void CKeytoolMigrateStore::EncryptAndStoreL(const T& aKey, RStoreWriteStream& aStream )
    83 	{
    84 	RMemWriteStream writeStream;
    85 	CleanupClosePushL(writeStream);
    86 	// create the buffer for storing the encrypted private key
    87 	HBufC8* privateKey = HBufC8::NewLC(KSize);
    88 	TPtr8 keyPtr(privateKey->Des());
    89 	keyPtr.FillZ(KSize);
    90 	
    91 	// a pointer to the data is required to pass it to the stream
    92 	TAny* ptr = const_cast<TAny*>(static_cast<const TAny*>(privateKey->Des().Ptr()));
    93 	writeStream.Open( ptr,KSize);
    94 	writeStream << aKey;
    95 	writeStream.CommitL();
    96 	TStreamPos pos = writeStream.Sink()->TellL(MStreamBuf::EWrite);
    97 	keyPtr.SetLength(pos.Offset());
    98 	StoreKeyL(keyPtr, aStream);
    99 	CleanupStack::PopAndDestroy(2, &writeStream); // privateKey
   100 
   101 	}
   102