os/security/cryptoservices/filebasedcertificateandkeystores/source/keystore/Server/keystreamutils.cpp
First public contribution.
2 * Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
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".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
19 #include "keystreamutils.h"
20 #include "asymmetrickeys.h"
22 #ifdef SYMBIAN_KEYSTORE_USE_AUTH_SERVER
26 #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER
30 void ExternalizeL(const CRSAPublicKey& aKey, RWriteStream& aStream)
32 aStream << aKey.N() << aKey.E();
35 void ExternalizeL(const CRSAPrivateKey& aData, RWriteStream& aStream)
39 // Check the incoming RSA private key (standard or CRT)
40 TRSAPrivateKeyType keyType = aData.PrivateKeyType();
41 aStream.WriteInt32L((TInt32)keyType);
43 if (EStandard==keyType)
45 const CRSAPrivateKeyStandard& key = static_cast<const CRSAPrivateKeyStandard&>(aData);
48 else if (EStandardCRT==keyType)
50 const CRSAPrivateKeyCRT& key = static_cast<const CRSAPrivateKeyCRT&>(aData);
51 aStream << key.P() << key.Q() << key.DP() << key.DQ() << key.QInv();
55 User::Leave(KErrNotSupported);
59 void ExternalizeL(const CDSAPublicKey& aKey, RWriteStream& aStream)
61 aStream << aKey.P() << aKey.Q() << aKey.G() << aKey.Y();
64 void ExternalizeL(const CDSAPrivateKey& aKey, RWriteStream& aStream)
66 aStream << aKey.P() << aKey.Q() << aKey.G() << aKey.X();
69 void CreateL(RReadStream& aStream, CRSAPublicKey*& aOut)
71 RInteger N, keyPublicExp;
73 CreateLC(aStream, keyPublicExp);
75 aOut = CRSAPublicKey::NewL(N, keyPublicExp);
77 CleanupStack::Pop(2, &N); // keyPublicExp, N
80 void CreateL(RReadStream& aStream, CRSAPrivateKey*& aOut)
83 CreateLC(aStream, privateN);
85 TRSAPrivateKeyType keyType = EStandard;
86 keyType = (TRSAPrivateKeyType)aStream.ReadInt32L();
88 if (EStandard==keyType)
93 aOut = CRSAPrivateKeyStandard::NewL(privateN, D);
95 CleanupStack::Pop(&D);
97 else if (EStandardCRT==keyType)
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);
106 aOut = CRSAPrivateKeyCRT::NewL(privateN, p, q, dP, dQ, qInv);
108 CleanupStack::Pop(5, &p);
112 User::Leave(KErrNotSupported);
115 CleanupStack::Pop(&privateN);
118 void CreateL(RReadStream& aStream, CDSAPublicKey*& aOut)
121 CreateLC(aStream, P);
122 CreateLC(aStream, Q);
123 CreateLC(aStream, G);
124 CreateLC(aStream, Y);
126 aOut = CDSAPublicKey::NewL(P, Q, G, Y);
128 CleanupStack::Pop(4, &P);
131 void CreateL(RReadStream& aStream, CDSAPrivateKey*& aOut)
134 CreateLC(aStream, P);
135 CreateLC(aStream, Q);
136 CreateLC(aStream, G);
137 CreateLC(aStream, X);
139 aOut = CDSAPrivateKey::NewL(P, Q, G, X);
141 CleanupStack::Pop(4, &P);
144 #ifdef SYMBIAN_KEYSTORE_USE_AUTH_SERVER
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
153 HBufC8* DecryptFromStreamL( RReadStream& aInStream, TPtrC8& aKey )
156 CPBEncryptionData* data = CPBEncryptionData::NewL(aInStream);
157 CleanupStack::PushL(data);
159 TInt32 encKeyLength = aInStream.ReadInt32L();
160 HBufC8* encKey = HBufC8::NewMaxLC(encKeyLength);
161 TPtr8 encKeyPtr(encKey->Des());
163 aInStream.ReadL(encKeyPtr,encKeyLength);
165 CPBEncryptElement* encryption = CPBEncryptElement::NewLC(*data,aKey);
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);
173 CleanupStack::Pop(plaintext);
174 CleanupStack::PopAndDestroy(4,data); // encKey, encryption, decryptor
179 #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER