os/security/cryptoservices/filebasedcertificateandkeystores/source/keystore/Server/keystreamutils.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/security/cryptoservices/filebasedcertificateandkeystores/source/keystore/Server/keystreamutils.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,180 @@
     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 "keystreamutils.h"
    1.23 +#include "asymmetrickeys.h"
    1.24 +
    1.25 +#ifdef SYMBIAN_KEYSTORE_USE_AUTH_SERVER
    1.26 +#include <s32mem.h>
    1.27 +#include <pbe.h>
    1.28 +#include <pbedata.h>
    1.29 +#endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER
    1.30 +
    1.31 +#include <e32debug.h>
    1.32 +
    1.33 +void ExternalizeL(const CRSAPublicKey& aKey, RWriteStream& aStream)
    1.34 +	{
    1.35 +	aStream << aKey.N() << aKey.E();
    1.36 +	}
    1.37 +
    1.38 +void ExternalizeL(const CRSAPrivateKey& aData, RWriteStream& aStream)
    1.39 +	{
    1.40 +	aStream << aData.N();
    1.41 +	
    1.42 +	// Check the incoming RSA private key (standard or CRT)	
    1.43 +	TRSAPrivateKeyType keyType = aData.PrivateKeyType();
    1.44 +	aStream.WriteInt32L((TInt32)keyType);
    1.45 +
    1.46 +	if (EStandard==keyType)
    1.47 +		{
    1.48 +		const CRSAPrivateKeyStandard& key = static_cast<const CRSAPrivateKeyStandard&>(aData);
    1.49 +		aStream << key.D();
    1.50 +		}
    1.51 +	else if (EStandardCRT==keyType)
    1.52 +		{
    1.53 +		const CRSAPrivateKeyCRT& key = static_cast<const CRSAPrivateKeyCRT&>(aData);
    1.54 +		aStream << key.P() << key.Q() << key.DP() << key.DQ() << key.QInv();
    1.55 +		}
    1.56 +	else
    1.57 +		{
    1.58 +		User::Leave(KErrNotSupported);
    1.59 +		}
    1.60 +	}
    1.61 +
    1.62 +void ExternalizeL(const CDSAPublicKey& aKey, RWriteStream& aStream)
    1.63 +	{
    1.64 +	aStream << aKey.P() << aKey.Q() << aKey.G() << aKey.Y();
    1.65 +	}
    1.66 +
    1.67 +void ExternalizeL(const CDSAPrivateKey& aKey, RWriteStream& aStream)
    1.68 +	{
    1.69 +	aStream << aKey.P() << aKey.Q() << aKey.G() << aKey.X();
    1.70 +	}
    1.71 +
    1.72 +void CreateL(RReadStream& aStream, CRSAPublicKey*& aOut)
    1.73 +	{
    1.74 +	RInteger N, keyPublicExp;
    1.75 +	CreateLC(aStream, N);
    1.76 +	CreateLC(aStream, keyPublicExp);
    1.77 +
    1.78 +	aOut = CRSAPublicKey::NewL(N, keyPublicExp);
    1.79 +
    1.80 +	CleanupStack::Pop(2, &N); // keyPublicExp, N
    1.81 +	}
    1.82 +
    1.83 +void CreateL(RReadStream& aStream, CRSAPrivateKey*& aOut)
    1.84 +	{
    1.85 +	RInteger privateN;
    1.86 +	CreateLC(aStream, privateN);
    1.87 +		
    1.88 +	TRSAPrivateKeyType keyType = EStandard;
    1.89 +	keyType = (TRSAPrivateKeyType)aStream.ReadInt32L();
    1.90 +	
    1.91 +	if (EStandard==keyType)
    1.92 +		{
    1.93 +		RInteger D;
    1.94 +		CreateLC(aStream, D);
    1.95 +	
    1.96 +		aOut = CRSAPrivateKeyStandard::NewL(privateN, D);
    1.97 +
    1.98 +		CleanupStack::Pop(&D);
    1.99 +		}
   1.100 +	else if (EStandardCRT==keyType)
   1.101 +		{
   1.102 +		RInteger p, q, dP, dQ, qInv;
   1.103 +		CreateLC(aStream, p);
   1.104 +		CreateLC(aStream, q);
   1.105 +		CreateLC(aStream, dP);
   1.106 +		CreateLC(aStream, dQ);
   1.107 +		CreateLC(aStream, qInv);
   1.108 +				
   1.109 +		aOut = CRSAPrivateKeyCRT::NewL(privateN, p, q, dP, dQ, qInv);
   1.110 +		
   1.111 +		CleanupStack::Pop(5, &p);
   1.112 +		}
   1.113 +	else
   1.114 +		{
   1.115 +		User::Leave(KErrNotSupported);
   1.116 +		}
   1.117 +
   1.118 +	CleanupStack::Pop(&privateN);
   1.119 +	}
   1.120 +
   1.121 +void CreateL(RReadStream& aStream, CDSAPublicKey*& aOut)
   1.122 +	{
   1.123 +	RInteger P, Q, G, Y;
   1.124 +	CreateLC(aStream, P);
   1.125 +	CreateLC(aStream, Q);
   1.126 +	CreateLC(aStream, G);
   1.127 +	CreateLC(aStream, Y);
   1.128 +
   1.129 +	aOut = CDSAPublicKey::NewL(P, Q, G, Y);
   1.130 +
   1.131 +	CleanupStack::Pop(4, &P);
   1.132 +	}
   1.133 +
   1.134 +void CreateL(RReadStream& aStream, CDSAPrivateKey*& aOut)
   1.135 +	{
   1.136 +	RInteger P, Q, G, X;
   1.137 +	CreateLC(aStream, P);
   1.138 +	CreateLC(aStream, Q);
   1.139 +	CreateLC(aStream, G);
   1.140 +	CreateLC(aStream, X);
   1.141 +
   1.142 +	aOut = CDSAPrivateKey::NewL(P, Q, G, X);
   1.143 +
   1.144 +	CleanupStack::Pop(4, &P);
   1.145 +	}
   1.146 +
   1.147 +#ifdef SYMBIAN_KEYSTORE_USE_AUTH_SERVER
   1.148 +
   1.149 +/**
   1.150 + * The input stream contains data in encrypted form. This method 
   1.151 + * supports pbe. In this case the key is the password. It 
   1.152 + * retrieves the plaintext data by decrypting the data using the 
   1.153 + * supplied key.
   1.154 + */
   1.155 +
   1.156 +HBufC8* DecryptFromStreamL( RReadStream& aInStream, TPtrC8& aKey )
   1.157 +	{
   1.158 +	
   1.159 +	CPBEncryptionData* data = CPBEncryptionData::NewL(aInStream);
   1.160 +	CleanupStack::PushL(data);
   1.161 +
   1.162 +	TInt32 encKeyLength = aInStream.ReadInt32L();
   1.163 +	HBufC8* encKey = HBufC8::NewMaxLC(encKeyLength);
   1.164 +	TPtr8 encKeyPtr(encKey->Des());
   1.165 +	encKeyPtr.FillZ();
   1.166 +	aInStream.ReadL(encKeyPtr,encKeyLength);
   1.167 +	
   1.168 +	CPBEncryptElement* encryption = CPBEncryptElement::NewLC(*data,aKey);
   1.169 +
   1.170 +	CPBDecryptor* decryptor = encryption->NewDecryptLC();
   1.171 +	HBufC8* plaintext = HBufC8::NewLC(decryptor->MaxOutputLength(encKeyPtr.Length())); 
   1.172 +	TPtr8 plaintextPtr = plaintext->Des();	
   1.173 +	plaintextPtr.FillZ();
   1.174 +	decryptor->ProcessFinalL(encKeyPtr, plaintextPtr);
   1.175 +
   1.176 +	CleanupStack::Pop(plaintext);
   1.177 +	CleanupStack::PopAndDestroy(4,data); // encKey, encryption, decryptor 
   1.178 +	
   1.179 +	return plaintext;
   1.180 +	}
   1.181 +
   1.182 +#endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER
   1.183 +