sl@0: /* sl@0: * Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: * All rights reserved. sl@0: * This component and the accompanying materials are made available sl@0: * under the terms of the License "Eclipse Public License v1.0" sl@0: * which accompanies this distribution, and is available sl@0: * at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: * sl@0: * Initial Contributors: sl@0: * Nokia Corporation - initial contribution. sl@0: * sl@0: * Contributors: sl@0: * sl@0: * Description: sl@0: * sl@0: */ sl@0: sl@0: sl@0: #include "keystreamutils.h" sl@0: #include "asymmetrickeys.h" sl@0: sl@0: #ifdef SYMBIAN_KEYSTORE_USE_AUTH_SERVER sl@0: #include sl@0: #include sl@0: #include sl@0: #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER sl@0: sl@0: #include sl@0: sl@0: void ExternalizeL(const CRSAPublicKey& aKey, RWriteStream& aStream) sl@0: { sl@0: aStream << aKey.N() << aKey.E(); sl@0: } sl@0: sl@0: void ExternalizeL(const CRSAPrivateKey& aData, RWriteStream& aStream) sl@0: { sl@0: aStream << aData.N(); sl@0: sl@0: // Check the incoming RSA private key (standard or CRT) sl@0: TRSAPrivateKeyType keyType = aData.PrivateKeyType(); sl@0: aStream.WriteInt32L((TInt32)keyType); sl@0: sl@0: if (EStandard==keyType) sl@0: { sl@0: const CRSAPrivateKeyStandard& key = static_cast(aData); sl@0: aStream << key.D(); sl@0: } sl@0: else if (EStandardCRT==keyType) sl@0: { sl@0: const CRSAPrivateKeyCRT& key = static_cast(aData); sl@0: aStream << key.P() << key.Q() << key.DP() << key.DQ() << key.QInv(); sl@0: } sl@0: else sl@0: { sl@0: User::Leave(KErrNotSupported); sl@0: } sl@0: } sl@0: sl@0: void ExternalizeL(const CDSAPublicKey& aKey, RWriteStream& aStream) sl@0: { sl@0: aStream << aKey.P() << aKey.Q() << aKey.G() << aKey.Y(); sl@0: } sl@0: sl@0: void ExternalizeL(const CDSAPrivateKey& aKey, RWriteStream& aStream) sl@0: { sl@0: aStream << aKey.P() << aKey.Q() << aKey.G() << aKey.X(); sl@0: } sl@0: sl@0: void CreateL(RReadStream& aStream, CRSAPublicKey*& aOut) sl@0: { sl@0: RInteger N, keyPublicExp; sl@0: CreateLC(aStream, N); sl@0: CreateLC(aStream, keyPublicExp); sl@0: sl@0: aOut = CRSAPublicKey::NewL(N, keyPublicExp); sl@0: sl@0: CleanupStack::Pop(2, &N); // keyPublicExp, N sl@0: } sl@0: sl@0: void CreateL(RReadStream& aStream, CRSAPrivateKey*& aOut) sl@0: { sl@0: RInteger privateN; sl@0: CreateLC(aStream, privateN); sl@0: sl@0: TRSAPrivateKeyType keyType = EStandard; sl@0: keyType = (TRSAPrivateKeyType)aStream.ReadInt32L(); sl@0: sl@0: if (EStandard==keyType) sl@0: { sl@0: RInteger D; sl@0: CreateLC(aStream, D); sl@0: sl@0: aOut = CRSAPrivateKeyStandard::NewL(privateN, D); sl@0: sl@0: CleanupStack::Pop(&D); sl@0: } sl@0: else if (EStandardCRT==keyType) sl@0: { sl@0: RInteger p, q, dP, dQ, qInv; sl@0: CreateLC(aStream, p); sl@0: CreateLC(aStream, q); sl@0: CreateLC(aStream, dP); sl@0: CreateLC(aStream, dQ); sl@0: CreateLC(aStream, qInv); sl@0: sl@0: aOut = CRSAPrivateKeyCRT::NewL(privateN, p, q, dP, dQ, qInv); sl@0: sl@0: CleanupStack::Pop(5, &p); sl@0: } sl@0: else sl@0: { sl@0: User::Leave(KErrNotSupported); sl@0: } sl@0: sl@0: CleanupStack::Pop(&privateN); sl@0: } sl@0: sl@0: void CreateL(RReadStream& aStream, CDSAPublicKey*& aOut) sl@0: { sl@0: RInteger P, Q, G, Y; sl@0: CreateLC(aStream, P); sl@0: CreateLC(aStream, Q); sl@0: CreateLC(aStream, G); sl@0: CreateLC(aStream, Y); sl@0: sl@0: aOut = CDSAPublicKey::NewL(P, Q, G, Y); sl@0: sl@0: CleanupStack::Pop(4, &P); sl@0: } sl@0: sl@0: void CreateL(RReadStream& aStream, CDSAPrivateKey*& aOut) sl@0: { sl@0: RInteger P, Q, G, X; sl@0: CreateLC(aStream, P); sl@0: CreateLC(aStream, Q); sl@0: CreateLC(aStream, G); sl@0: CreateLC(aStream, X); sl@0: sl@0: aOut = CDSAPrivateKey::NewL(P, Q, G, X); sl@0: sl@0: CleanupStack::Pop(4, &P); sl@0: } sl@0: sl@0: #ifdef SYMBIAN_KEYSTORE_USE_AUTH_SERVER sl@0: sl@0: /** sl@0: * The input stream contains data in encrypted form. This method sl@0: * supports pbe. In this case the key is the password. It sl@0: * retrieves the plaintext data by decrypting the data using the sl@0: * supplied key. sl@0: */ sl@0: sl@0: HBufC8* DecryptFromStreamL( RReadStream& aInStream, TPtrC8& aKey ) sl@0: { sl@0: sl@0: CPBEncryptionData* data = CPBEncryptionData::NewL(aInStream); sl@0: CleanupStack::PushL(data); sl@0: sl@0: TInt32 encKeyLength = aInStream.ReadInt32L(); sl@0: HBufC8* encKey = HBufC8::NewMaxLC(encKeyLength); sl@0: TPtr8 encKeyPtr(encKey->Des()); sl@0: encKeyPtr.FillZ(); sl@0: aInStream.ReadL(encKeyPtr,encKeyLength); sl@0: sl@0: CPBEncryptElement* encryption = CPBEncryptElement::NewLC(*data,aKey); sl@0: sl@0: CPBDecryptor* decryptor = encryption->NewDecryptLC(); sl@0: HBufC8* plaintext = HBufC8::NewLC(decryptor->MaxOutputLength(encKeyPtr.Length())); sl@0: TPtr8 plaintextPtr = plaintext->Des(); sl@0: plaintextPtr.FillZ(); sl@0: decryptor->ProcessFinalL(encKeyPtr, plaintextPtr); sl@0: sl@0: CleanupStack::Pop(plaintext); sl@0: CleanupStack::PopAndDestroy(4,data); // encKey, encryption, decryptor sl@0: sl@0: return plaintext; sl@0: } sl@0: sl@0: #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER sl@0: