os/security/cryptoservices/filebasedcertificateandkeystores/source/keystore/Server/keystreamutils.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
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 "keystreamutils.h"
sl@0
    20
#include "asymmetrickeys.h"
sl@0
    21
sl@0
    22
#ifdef SYMBIAN_KEYSTORE_USE_AUTH_SERVER
sl@0
    23
#include <s32mem.h>
sl@0
    24
#include <pbe.h>
sl@0
    25
#include <pbedata.h>
sl@0
    26
#endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER
sl@0
    27
sl@0
    28
#include <e32debug.h>
sl@0
    29
sl@0
    30
void ExternalizeL(const CRSAPublicKey& aKey, RWriteStream& aStream)
sl@0
    31
	{
sl@0
    32
	aStream << aKey.N() << aKey.E();
sl@0
    33
	}
sl@0
    34
sl@0
    35
void ExternalizeL(const CRSAPrivateKey& aData, RWriteStream& aStream)
sl@0
    36
	{
sl@0
    37
	aStream << aData.N();
sl@0
    38
	
sl@0
    39
	// Check the incoming RSA private key (standard or CRT)	
sl@0
    40
	TRSAPrivateKeyType keyType = aData.PrivateKeyType();
sl@0
    41
	aStream.WriteInt32L((TInt32)keyType);
sl@0
    42
sl@0
    43
	if (EStandard==keyType)
sl@0
    44
		{
sl@0
    45
		const CRSAPrivateKeyStandard& key = static_cast<const CRSAPrivateKeyStandard&>(aData);
sl@0
    46
		aStream << key.D();
sl@0
    47
		}
sl@0
    48
	else if (EStandardCRT==keyType)
sl@0
    49
		{
sl@0
    50
		const CRSAPrivateKeyCRT& key = static_cast<const CRSAPrivateKeyCRT&>(aData);
sl@0
    51
		aStream << key.P() << key.Q() << key.DP() << key.DQ() << key.QInv();
sl@0
    52
		}
sl@0
    53
	else
sl@0
    54
		{
sl@0
    55
		User::Leave(KErrNotSupported);
sl@0
    56
		}
sl@0
    57
	}
sl@0
    58
sl@0
    59
void ExternalizeL(const CDSAPublicKey& aKey, RWriteStream& aStream)
sl@0
    60
	{
sl@0
    61
	aStream << aKey.P() << aKey.Q() << aKey.G() << aKey.Y();
sl@0
    62
	}
sl@0
    63
sl@0
    64
void ExternalizeL(const CDSAPrivateKey& aKey, RWriteStream& aStream)
sl@0
    65
	{
sl@0
    66
	aStream << aKey.P() << aKey.Q() << aKey.G() << aKey.X();
sl@0
    67
	}
sl@0
    68
sl@0
    69
void CreateL(RReadStream& aStream, CRSAPublicKey*& aOut)
sl@0
    70
	{
sl@0
    71
	RInteger N, keyPublicExp;
sl@0
    72
	CreateLC(aStream, N);
sl@0
    73
	CreateLC(aStream, keyPublicExp);
sl@0
    74
sl@0
    75
	aOut = CRSAPublicKey::NewL(N, keyPublicExp);
sl@0
    76
sl@0
    77
	CleanupStack::Pop(2, &N); // keyPublicExp, N
sl@0
    78
	}
sl@0
    79
sl@0
    80
void CreateL(RReadStream& aStream, CRSAPrivateKey*& aOut)
sl@0
    81
	{
sl@0
    82
	RInteger privateN;
sl@0
    83
	CreateLC(aStream, privateN);
sl@0
    84
		
sl@0
    85
	TRSAPrivateKeyType keyType = EStandard;
sl@0
    86
	keyType = (TRSAPrivateKeyType)aStream.ReadInt32L();
sl@0
    87
	
sl@0
    88
	if (EStandard==keyType)
sl@0
    89
		{
sl@0
    90
		RInteger D;
sl@0
    91
		CreateLC(aStream, D);
sl@0
    92
	
sl@0
    93
		aOut = CRSAPrivateKeyStandard::NewL(privateN, D);
sl@0
    94
sl@0
    95
		CleanupStack::Pop(&D);
sl@0
    96
		}
sl@0
    97
	else if (EStandardCRT==keyType)
sl@0
    98
		{
sl@0
    99
		RInteger p, q, dP, dQ, qInv;
sl@0
   100
		CreateLC(aStream, p);
sl@0
   101
		CreateLC(aStream, q);
sl@0
   102
		CreateLC(aStream, dP);
sl@0
   103
		CreateLC(aStream, dQ);
sl@0
   104
		CreateLC(aStream, qInv);
sl@0
   105
				
sl@0
   106
		aOut = CRSAPrivateKeyCRT::NewL(privateN, p, q, dP, dQ, qInv);
sl@0
   107
		
sl@0
   108
		CleanupStack::Pop(5, &p);
sl@0
   109
		}
sl@0
   110
	else
sl@0
   111
		{
sl@0
   112
		User::Leave(KErrNotSupported);
sl@0
   113
		}
sl@0
   114
sl@0
   115
	CleanupStack::Pop(&privateN);
sl@0
   116
	}
sl@0
   117
sl@0
   118
void CreateL(RReadStream& aStream, CDSAPublicKey*& aOut)
sl@0
   119
	{
sl@0
   120
	RInteger P, Q, G, Y;
sl@0
   121
	CreateLC(aStream, P);
sl@0
   122
	CreateLC(aStream, Q);
sl@0
   123
	CreateLC(aStream, G);
sl@0
   124
	CreateLC(aStream, Y);
sl@0
   125
sl@0
   126
	aOut = CDSAPublicKey::NewL(P, Q, G, Y);
sl@0
   127
sl@0
   128
	CleanupStack::Pop(4, &P);
sl@0
   129
	}
sl@0
   130
sl@0
   131
void CreateL(RReadStream& aStream, CDSAPrivateKey*& aOut)
sl@0
   132
	{
sl@0
   133
	RInteger P, Q, G, X;
sl@0
   134
	CreateLC(aStream, P);
sl@0
   135
	CreateLC(aStream, Q);
sl@0
   136
	CreateLC(aStream, G);
sl@0
   137
	CreateLC(aStream, X);
sl@0
   138
sl@0
   139
	aOut = CDSAPrivateKey::NewL(P, Q, G, X);
sl@0
   140
sl@0
   141
	CleanupStack::Pop(4, &P);
sl@0
   142
	}
sl@0
   143
sl@0
   144
#ifdef SYMBIAN_KEYSTORE_USE_AUTH_SERVER
sl@0
   145
sl@0
   146
/**
sl@0
   147
 * The input stream contains data in encrypted form. This method 
sl@0
   148
 * supports pbe. In this case the key is the password. It 
sl@0
   149
 * retrieves the plaintext data by decrypting the data using the 
sl@0
   150
 * supplied key.
sl@0
   151
 */
sl@0
   152
sl@0
   153
HBufC8* DecryptFromStreamL( RReadStream& aInStream, TPtrC8& aKey )
sl@0
   154
	{
sl@0
   155
	
sl@0
   156
	CPBEncryptionData* data = CPBEncryptionData::NewL(aInStream);
sl@0
   157
	CleanupStack::PushL(data);
sl@0
   158
sl@0
   159
	TInt32 encKeyLength = aInStream.ReadInt32L();
sl@0
   160
	HBufC8* encKey = HBufC8::NewMaxLC(encKeyLength);
sl@0
   161
	TPtr8 encKeyPtr(encKey->Des());
sl@0
   162
	encKeyPtr.FillZ();
sl@0
   163
	aInStream.ReadL(encKeyPtr,encKeyLength);
sl@0
   164
	
sl@0
   165
	CPBEncryptElement* encryption = CPBEncryptElement::NewLC(*data,aKey);
sl@0
   166
sl@0
   167
	CPBDecryptor* decryptor = encryption->NewDecryptLC();
sl@0
   168
	HBufC8* plaintext = HBufC8::NewLC(decryptor->MaxOutputLength(encKeyPtr.Length())); 
sl@0
   169
	TPtr8 plaintextPtr = plaintext->Des();	
sl@0
   170
	plaintextPtr.FillZ();
sl@0
   171
	decryptor->ProcessFinalL(encKeyPtr, plaintextPtr);
sl@0
   172
sl@0
   173
	CleanupStack::Pop(plaintext);
sl@0
   174
	CleanupStack::PopAndDestroy(4,data); // encKey, encryption, decryptor 
sl@0
   175
	
sl@0
   176
	return plaintext;
sl@0
   177
	}
sl@0
   178
sl@0
   179
#endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER
sl@0
   180