persistentstorage/store/UCRYPT/UE_STRM.CPP
changeset 0 08ec8eefde2f
equal deleted inserted replaced
-1:000000000000 0:08ec8eefde2f
       
     1 // Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 #include "UE_STD.H"
       
    17 
       
    18 #include <pbe.h>
       
    19 
       
    20 #define PBEDATA_NO_EXPORTED_CONSTRUCTORS
       
    21 #include <pbedata.h>
       
    22 
       
    23 
       
    24 
       
    25 
       
    26 
       
    27 EXPORT_C void RDecryptStream::OpenL(RReadStream& aHost,const CPBEncryptionBase& aKey)
       
    28 /**
       
    29 Open a decrypting read stream to aHost.
       
    30 Prepares the source stream owned by the specified read stream interface object for 
       
    31 reading through a decrypting filter.
       
    32 @publishedPartner
       
    33 @leave KErrNoMemory
       
    34 @param aHost The read stream interface object for the source stream. 
       
    35 This function does not take ownership of the source stream.
       
    36 @param aKey A Password Based Encryption object for encryption handling.
       
    37 */
       
    38 	{
       
    39 	CPBDecryptor* decryptor = aKey.NewDecryptLC();
       
    40 	iFilter.SetL(aHost.Source(),decryptor,iFilter.ERead);
       
    41 	CleanupStack::Pop(decryptor);
       
    42 	RReadStream::Attach(&iFilter);
       
    43 	}
       
    44 
       
    45 EXPORT_C void RDecryptStream::OpenLC(RReadStream& aHost,const CPBEncryptionBase& aKey)
       
    46 /**
       
    47 Open a decrypting read stream to aHost.
       
    48 Prepares the source stream owned by the specified read stream interface object for 
       
    49 reading through a decrypting filter, and puts a cleanup item onto the cleanup stack.
       
    50 @publishedPartner
       
    51 @leave KErrNoMemory
       
    52 @param aHost The read stream interface object for the source stream. 
       
    53 This function does not take ownership of the source stream.
       
    54 @param aKey A Password Based Encryption object for encryption handling.
       
    55 */	
       
    56 {
       
    57 	OpenL(aHost,aKey);
       
    58 	PushL();
       
    59 	}
       
    60 
       
    61 EXPORT_C void RDecryptStream::AttachL(RReadStream& aHost,const CPBEncryptionBase& aKey)
       
    62 /**
       
    63 Attach a decrypting read stream to aHost.
       
    64 Takes ownership of the source stream owned by the specified read stream interface object, 
       
    65 and prepares the stream for reading through a decrypting filter.
       
    66 @publishedPartner
       
    67 @leave KErrNoMemory
       
    68 @param aHost The read stream interface object for the source stream.
       
    69 @param aKey A Password Based Encryption object for encryption handling.
       
    70 */	{
       
    71 	AttachLC(aHost,aKey);
       
    72 	CleanupStack::Pop();
       
    73 	}
       
    74 
       
    75 EXPORT_C void RDecryptStream::AttachLC(RReadStream& aHost,const CPBEncryptionBase& aKey)
       
    76 /**
       
    77 Attach a decrypting read stream to aHost.
       
    78 Takes ownership of the source stream owned by the specified read stream interface object, 
       
    79 prepares the stream for reading through a decrypting filter, and puts a cleanup item onto the cleanup stack.
       
    80 @publishedPartner
       
    81 @leave KErrNoMemory
       
    82 @param aHost The read stream interface object for the source stream.
       
    83 @param aKey A Password Based Encryption object for encryption handling.
       
    84 */	{
       
    85 	MStreamBuf* host=aHost.Source();
       
    86 	aHost=RReadStream();
       
    87 	RReadStream::Attach(host);		// initial cleanup via this
       
    88 	PushL();
       
    89 
       
    90 	CPBDecryptor* decryptor = aKey.NewDecryptLC();
       
    91 	iFilter.SetL(host,decryptor,iFilter.ERead|iFilter.EAttached);
       
    92 	CleanupStack::Pop(decryptor);
       
    93 	
       
    94 	RReadStream::Attach(&iFilter);
       
    95 	}
       
    96 
       
    97 //////////////////////////////////////////////////////////////////////////////
       
    98 
       
    99 
       
   100 
       
   101 
       
   102 
       
   103 EXPORT_C void REncryptStream::OpenL(RWriteStream& aHost,const CPBEncryptionBase& aKey)
       
   104 /**
       
   105 Open an encrypting write stream over aHost.
       
   106 Prepares the target stream owned by the specified write stream interface object for 
       
   107 writing through an encrypting filter.
       
   108 @publishedPartner
       
   109 @leave KErrNoMemory
       
   110 @param aHost The write stream interface object for the target stream. The function does not take ownership of the target stream.
       
   111 @param aKey A Password Based Encryption object for encryption handling.
       
   112 */
       
   113 	{
       
   114 	CPBEncryptor* encryptor = aKey.NewEncryptLC();
       
   115 	iFilter.SetL(aHost.Sink(),encryptor,iFilter.EWrite);
       
   116 	CleanupStack::Pop(encryptor);
       
   117 	RWriteStream::Attach(&iFilter);
       
   118     }
       
   119 
       
   120 EXPORT_C void REncryptStream::OpenLC(RWriteStream& aHost,const CPBEncryptionBase& aKey)
       
   121 /**
       
   122 Open an encrypting write stream over aHost.
       
   123 Prepares the target stream owned by the specified write stream interface object for 
       
   124 writing through an encrypting filter and puts a cleanup item onto the cleanup stack.
       
   125 @publishedPartner
       
   126 @leave KErrNoMemory
       
   127 @param aHost The write stream interface object for the target stream. The function does not take ownership of the target stream.
       
   128 @param aKey A Password Based Encryption object for encryption handling.
       
   129 */
       
   130 	{
       
   131 	OpenL(aHost,aKey);
       
   132 	PushL();
       
   133 	}
       
   134 
       
   135 EXPORT_C void REncryptStream::AttachL(RWriteStream& aHost,const CPBEncryptionBase& aKey)
       
   136 /**
       
   137 Attach an encrypting write stream to aHost.
       
   138 Takes ownership of the target stream owned by the specified write stream interface object, 
       
   139 and prepares the stream for writing through an encrypting filter.
       
   140 @publishedPartner
       
   141 @leave KErrNoMemory
       
   142 @param aHost The write stream interface object for the target stream
       
   143 @param aKey A Password Based Encryption object for encryption handling.
       
   144 */
       
   145 	{
       
   146 	AttachLC(aHost,aKey);
       
   147 	CleanupStack::Pop();
       
   148 	}
       
   149 
       
   150 EXPORT_C void REncryptStream::AttachLC(RWriteStream& aHost,const CPBEncryptionBase& aKey)
       
   151 /**
       
   152 Attach an encrypting write stream to aHost.
       
   153 Takes ownership of the target stream owned by the specified write stream interface object, 
       
   154 prepares the stream for writing through an encrypting filter, and puts a cleanup item onto the cleanup stack.
       
   155 @publishedPartner
       
   156 @leave KErrNoMemory
       
   157 @param aHost The write stream interface object for the target stream
       
   158 @param aKey A Password Based Encryption object for encryption handling.
       
   159 */
       
   160 	{
       
   161 	MStreamBuf* host=aHost.Sink();
       
   162 	aHost=RWriteStream();
       
   163 	RWriteStream::Attach(host);		// initial cleanup via this
       
   164 	PushL();
       
   165 
       
   166 	CPBEncryptor* encryptor = aKey.NewEncryptLC();
       
   167 	iFilter.SetL(host,encryptor,iFilter.EWrite|iFilter.EAttached);
       
   168 	CleanupStack::Pop(encryptor);
       
   169 	
       
   170 	RWriteStream::Attach(&iFilter);
       
   171 	}
       
   172 
       
   173 /////////////////////////////////////////////////////////////////////////
       
   174 // PBE data methods that depend on store, prevents static dependency
       
   175 /////////////////////////////////////////////////////////////////////////
       
   176 
       
   177 // CPBEncryptionData
       
   178 
       
   179 // HPRE-5TDFK2: Remove Store/estor.dll dependency on Cryptography/pbe.dll
       
   180 // This method is DUPLICATED in common/generic/security/crypto/source/pbe/pbedata.cpp
       
   181 CPBEncryptionData::CPBEncryptionData(void)
       
   182 	{
       
   183 	}
       
   184 
       
   185 // HPRE-5TDFK2: Remove Store/estor.dll dependency on Cryptography/pbe.dll
       
   186 // This method is DUPLICATED in common/generic/security/crypto/source/pbe/pbedata.cpp
       
   187 CPBEncryptionData::~CPBEncryptionData(void)
       
   188 	{
       
   189 	delete iParms;
       
   190 	delete iAuth;
       
   191 	}
       
   192 
       
   193 EXPORT_C CPBEncryptionData* CPBEncryptionData::NewL(RReadStream& aStream)
       
   194 	{
       
   195 	CPBEncryptionData* self = NewLC(aStream);
       
   196 	CleanupStack::Pop(self);
       
   197 	return self;
       
   198 	}
       
   199 
       
   200 EXPORT_C CPBEncryptionData* CPBEncryptionData::NewLC(RReadStream& aStream)
       
   201 	{
       
   202 	CPBEncryptionData* self = new(ELeave)CPBEncryptionData();
       
   203 	CleanupStack::PushL(self);
       
   204 	self->ConstructL(aStream);
       
   205 	return self;
       
   206 	}
       
   207 
       
   208 void CPBEncryptionData::ConstructL(RReadStream& aStream)
       
   209 	{
       
   210 	iAuth = CPBAuthData::NewL(aStream);
       
   211 	iParms = CPBEncryptParms::NewL(aStream);
       
   212 	}
       
   213 
       
   214 EXPORT_C void CPBEncryptionData::ExternalizeL(RWriteStream& aStream) const
       
   215 	{
       
   216 	iAuth->ExternalizeL(aStream);
       
   217 	iParms->ExternalizeL(aStream);
       
   218 	}
       
   219 
       
   220 // CPBEncryptParms
       
   221 
       
   222 // HPRE-5TDFK2: Remove Store/estor.dll dependency on Cryptography/pbe.dll
       
   223 // This method is DUPLICATED in common/generic/security/crypto/source/pbe/pbedata.cpp
       
   224 CPBEncryptParms::CPBEncryptParms()
       
   225 	{
       
   226 	}
       
   227 
       
   228 // HPRE-5TDFK2: Remove Store/estor.dll dependency on Cryptography/pbe.dll
       
   229 // This method is DUPLICATED in common/generic/security/crypto/source/pbe/pbedata.cpp
       
   230 CPBEncryptParms::~CPBEncryptParms()
       
   231 	{
       
   232 #ifdef SYMBIAN_PKCS12
       
   233 	delete iData;
       
   234 #endif	// #ifdef SYMBIAN_PKCS12
       
   235 	delete iSalt;
       
   236 	delete iIV;
       
   237 	}
       
   238 
       
   239 EXPORT_C CPBEncryptParms* CPBEncryptParms::NewL(RReadStream& aStream)
       
   240 	{
       
   241 	CPBEncryptParms* self = NewLC(aStream);
       
   242 	CleanupStack::Pop(self);
       
   243 	return self;
       
   244 	}
       
   245 
       
   246 EXPORT_C CPBEncryptParms* CPBEncryptParms::NewLC(RReadStream& aStream)
       
   247 	{
       
   248 	CPBEncryptParms* self = new(ELeave)CPBEncryptParms();
       
   249 	CleanupStack::PushL(self);
       
   250 	self->ConstructL(aStream);
       
   251 	return self;
       
   252 	}
       
   253 
       
   254 #ifdef SYMBIAN_PKCS12
       
   255 
       
   256 void CPBEncryptParms::ConstructL(RReadStream& aStream) 
       
   257 	{
       
   258 	iData = new(ELeave) TParamsData;
       
   259 	
       
   260 	TCardinality cipher;
       
   261 	aStream >> cipher;
       
   262 	TInt32 cipherInt32 = (TInt32) cipher;
       
   263 	iData->iCipher = (TPBECipher)(cipherInt32 & ~KBit16);
       
   264 	
       
   265 	// if bit 16 of the stored cipher is set, then a KDF
       
   266 	// follows.  (This preserves data compatability.)
       
   267 	if ((cipherInt32 & KBit16) == 0)
       
   268 		iData->iKdf = EKdfPkcs5;
       
   269 	else
       
   270 		{
       
   271 		TInt32 kdf;
       
   272 		aStream >> kdf;
       
   273 		iData->iKdf = (TKdf) kdf;
       
   274 		}
       
   275 
       
   276 	iSalt = HBufC8::NewL(aStream, KMaxTInt);
       
   277 
       
   278 	TCardinality iterations;
       
   279 	aStream >> iterations;
       
   280 	iIterations = iterations;
       
   281 
       
   282 	iIV = HBufC8::NewL(aStream, KMaxTInt);
       
   283 	}
       
   284 
       
   285 #else
       
   286 
       
   287 void CPBEncryptParms::ConstructL(RReadStream& aStream) 
       
   288 	{
       
   289 	TCardinality cipher;
       
   290 	aStream >> cipher;
       
   291 	iCipher = (TPBECipher)((TUint)(cipher));
       
   292 
       
   293 	iSalt = HBufC8::NewL(aStream, KMaxTInt);
       
   294 
       
   295 	TCardinality iterations;
       
   296 	aStream >> iterations;
       
   297 	iIterations = iterations;
       
   298 
       
   299 	iIV = HBufC8::NewL(aStream, KMaxTInt);
       
   300 	}
       
   301 
       
   302 #endif	// #else #ifdef SYMBIAN_PKCS12
       
   303 
       
   304 #ifdef SYMBIAN_PKCS12
       
   305 
       
   306 EXPORT_C void CPBEncryptParms::ExternalizeL(RWriteStream& aStream) const
       
   307 	{
       
   308 	TUint32 cipherInt32 = iData->iCipher;
       
   309 	if (iData->iKdf != EKdfPkcs5)
       
   310 		cipherInt32 |= KBit16;
       
   311 	aStream << TCardinality(cipherInt32);
       
   312 	
       
   313 	if (iData->iKdf != EKdfPkcs5)
       
   314 		aStream << (TInt32) iData->iKdf;
       
   315 	
       
   316 	aStream << *iSalt;
       
   317 	aStream << TCardinality(iIterations);
       
   318 	aStream << *iIV;
       
   319 	}
       
   320 
       
   321 #else
       
   322 
       
   323 EXPORT_C void CPBEncryptParms::ExternalizeL(RWriteStream& aStream) const
       
   324 	{
       
   325 	aStream << TCardinality((TUint)iCipher);
       
   326 	aStream << *iSalt;
       
   327 	aStream << TCardinality(iIterations);
       
   328 	aStream << *iIV;
       
   329 	}
       
   330 
       
   331 #endif	// #else #ifdef SYMBIAN_PKCS12
       
   332 
       
   333 // CPBAuthData
       
   334 
       
   335 // HPRE-5TDFK2: Remove Store/estor.dll dependency on Cryptography/pbe.dll
       
   336 // This method is DUPLICATED in common/generic/security/crypto/source/pbe/pbedata.cpp
       
   337 CPBAuthData::CPBAuthData()
       
   338 	{
       
   339 	}
       
   340 
       
   341 // HPRE-5TDFK2: Remove Store/estor.dll dependency on Cryptography/pbe.dll
       
   342 // This method is DUPLICATED in common/generic/security/crypto/source/pbe/pbedata.cpp
       
   343 CPBAuthData::~CPBAuthData()
       
   344 	{
       
   345 	delete iAuthKey;
       
   346 	delete iSalt;
       
   347 	}
       
   348 
       
   349 EXPORT_C CPBAuthData* CPBAuthData::NewL(RReadStream& aStream)
       
   350 	{
       
   351 	CPBAuthData* self = NewLC(aStream);
       
   352 	CleanupStack::Pop(self);
       
   353 	return self;
       
   354 	}
       
   355 
       
   356 EXPORT_C CPBAuthData* CPBAuthData::NewLC(RReadStream& aStream)
       
   357 	{
       
   358 	CPBAuthData* self = new(ELeave)CPBAuthData();
       
   359 	CleanupStack::PushL(self);
       
   360 	self->ConstructL(aStream);
       
   361 	return self;
       
   362 	}
       
   363 
       
   364 void CPBAuthData::ConstructL(RReadStream& aStream)
       
   365 	{
       
   366 	iAuthKey = HBufC8::NewL(aStream, KMaxTInt);
       
   367 	iSalt = HBufC8::NewL(aStream, KMaxTInt);
       
   368 	TCardinality iterations;
       
   369 	aStream >> iterations;
       
   370 	iIterations = iterations;
       
   371 	}
       
   372 
       
   373 EXPORT_C void CPBAuthData::ExternalizeL(RWriteStream& aStream) const
       
   374 	{
       
   375 	aStream << *iAuthKey;
       
   376 	aStream << *iSalt;
       
   377 	aStream << TCardinality(iIterations);
       
   378 	}