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