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.
sl@0
     1
/*
sl@0
     2
* Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     3
* All rights reserved.
sl@0
     4
* This component and the accompanying materials are made available
sl@0
     5
* under the terms of the License "Eclipse Public License v1.0"
sl@0
     6
* which accompanies this distribution, and is available
sl@0
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     8
*
sl@0
     9
* Initial Contributors:
sl@0
    10
* Nokia Corporation - initial contribution.
sl@0
    11
*
sl@0
    12
* Contributors:
sl@0
    13
*
sl@0
    14
* Description: 
sl@0
    15
*
sl@0
    16
*/
sl@0
    17
sl@0
    18
sl@0
    19
#include <mctkeystore.h>
sl@0
    20
#include <f32file.h>
sl@0
    21
#include <s32mem.h>
sl@0
    22
sl@0
    23
#include <authserver/authtypes.h>
sl@0
    24
#include <centralrepository.h>
sl@0
    25
#include <authserver/authclient.h>
sl@0
    26
#include <authserver/authexpression.h>
sl@0
    27
#include <authserver/auth_srv_errs.h>
sl@0
    28
#include <authserver/aspubsubdefs.h>
sl@0
    29
#include <authserver/authtypes.h>
sl@0
    30
#include <pbedata.h>
sl@0
    31
#include <mctkeystore.h>
sl@0
    32
#include <keystore_errs.h>
sl@0
    33
#include <securityerr.h>
sl@0
    34
#include <keytool.rsg>
sl@0
    35
sl@0
    36
#include "keystorecenrepconfig.h"
sl@0
    37
#include "fsdatatypes.h"
sl@0
    38
#include "ckeydatamanager.h"
sl@0
    39
#include "keystorepassphrase.h"
sl@0
    40
#include "keystreamutils.h"
sl@0
    41
#include "asymmetrickeys.h"
sl@0
    42
sl@0
    43
#include "keytool_utils.h"
sl@0
    44
#include "keytool_commands.h"
sl@0
    45
#include "keytool_controller.h"
sl@0
    46
sl@0
    47
// the size allocated to retrieve the private key.
sl@0
    48
const TInt KSize = 2048;
sl@0
    49
sl@0
    50
template <class T> inline void CKeytoolMigrateStore::RetrieveAndStorePublicKeyL( CFileKeyData* aKeyData, T* aPublicKey)
sl@0
    51
	{	
sl@0
    52
	// open the publickeystream
sl@0
    53
	RStoreReadStream publicStream;
sl@0
    54
	publicStream.OpenLC(*iReadFileStore, aKeyData->PublicDataStreamId());
sl@0
    55
			
sl@0
    56
	CreateL(publicStream,aPublicKey);
sl@0
    57
	CleanupStack::PushL(aPublicKey);
sl@0
    58
	RStoreWriteStream writePublicStream;
sl@0
    59
	TStreamId publicStrId = writePublicStream.CreateLC(*iWriteFileStore);
sl@0
    60
	aKeyData->SetPublicDataStreamId(publicStrId);
sl@0
    61
	writePublicStream << *aPublicKey;
sl@0
    62
	writePublicStream.CommitL();
sl@0
    63
	CleanupStack::PopAndDestroy(3,&publicStream); // aPublicKey, writePublicStream
sl@0
    64
	}
sl@0
    65
sl@0
    66
template <class T> inline void CKeytoolMigrateStore::RetrieveAndStorePrivateKeyL( CFileKeyData* aKeyData, T* aPrivateKey)
sl@0
    67
	{	
sl@0
    68
	// open the privatekeystream based on the supplied passphrase
sl@0
    69
	RStoreReadStream privateStream;
sl@0
    70
	privateStream.OpenLC(iPassphrase->Store(), aKeyData->PrivateDataStreamId());
sl@0
    71
					
sl@0
    72
	CreateL(privateStream,aPrivateKey);
sl@0
    73
	CleanupStack::PushL(aPrivateKey);
sl@0
    74
	RStoreWriteStream writePrivateStream;
sl@0
    75
	TStreamId pvtStrId = writePrivateStream.CreateLC(*iWriteFileStore);
sl@0
    76
	aKeyData->SetPrivateDataStreamId(pvtStrId);
sl@0
    77
	EncryptAndStoreL(*aPrivateKey, writePrivateStream );
sl@0
    78
	writePrivateStream.CommitL();
sl@0
    79
	CleanupStack::PopAndDestroy(3,&privateStream); // aPrivateKey, writePrivateStream
sl@0
    80
	}
sl@0
    81
sl@0
    82
template <class T> inline void CKeytoolMigrateStore::EncryptAndStoreL(const T& aKey, RStoreWriteStream& aStream )
sl@0
    83
	{
sl@0
    84
	RMemWriteStream writeStream;
sl@0
    85
	CleanupClosePushL(writeStream);
sl@0
    86
	// create the buffer for storing the encrypted private key
sl@0
    87
	HBufC8* privateKey = HBufC8::NewLC(KSize);
sl@0
    88
	TPtr8 keyPtr(privateKey->Des());
sl@0
    89
	keyPtr.FillZ(KSize);
sl@0
    90
	
sl@0
    91
	// a pointer to the data is required to pass it to the stream
sl@0
    92
	TAny* ptr = const_cast<TAny*>(static_cast<const TAny*>(privateKey->Des().Ptr()));
sl@0
    93
	writeStream.Open( ptr,KSize);
sl@0
    94
	writeStream << aKey;
sl@0
    95
	writeStream.CommitL();
sl@0
    96
	TStreamPos pos = writeStream.Sink()->TellL(MStreamBuf::EWrite);
sl@0
    97
	keyPtr.SetLength(pos.Offset());
sl@0
    98
	StoreKeyL(keyPtr, aStream);
sl@0
    99
	CleanupStack::PopAndDestroy(2, &writeStream); // privateKey
sl@0
   100
sl@0
   101
	}
sl@0
   102