|         |      1 /* | 
|         |      2 * Copyright (c) 2007 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 "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:  Implementation of CDevEncKeyUtils | 
|         |     15 * | 
|         |     16 */ | 
|         |     17  | 
|         |     18  | 
|         |     19 // INCLUDE FILES | 
|         |     20  | 
|         |     21 // Class includes | 
|         |     22 #include "DevEncDef.h" | 
|         |     23 #include "DevEncKeyUtils.h" | 
|         |     24 #include "DevEncLog.h" | 
|         |     25 #include "DevEncUids.hrh" | 
|         |     26  | 
|         |     27 #include <pbe.h> | 
|         |     28 #include <pbedata.h> | 
|         |     29 //#include <PathInfo.h> // for system path literals | 
|         |     30 #include <pkcs5kdf.h> | 
|         |     31 #include <s32file.h> | 
|         |     32 #include <s32mem.h> | 
|         |     33 #include <imcvcodc.h> | 
|         |     34  | 
|         |     35 #include <DevEncEngineConstants.h> | 
|         |     36 #include <DevEncEngineBase.h> | 
|         |     37  | 
|         |     38 // -------------------------------------------------------------------------- | 
|         |     39 // CDevEncKeyUtils::Connect() | 
|         |     40 // | 
|         |     41 // -------------------------------------------------------------------------- | 
|         |     42 EXPORT_C TInt CDevEncKeyUtils::Connect() | 
|         |     43     { | 
|         |     44     TRAPD( err, LoadDevEncEngineL() ); | 
|         |     45     return err; | 
|         |     46     } | 
|         |     47  | 
|         |     48 // -------------------------------------------------------------------------- | 
|         |     49 // CDevEncKeyUtils::Connect() | 
|         |     50 // | 
|         |     51 // -------------------------------------------------------------------------- | 
|         |     52 EXPORT_C void CDevEncKeyUtils::Close() | 
|         |     53     { | 
|         |     54     UnloadDevEncEngine(); | 
|         |     55     } | 
|         |     56  | 
|         |     57 // -------------------------------------------------------------------------- | 
|         |     58 // CDevEncKeyUtils::CDevEncKeyUtils() | 
|         |     59 // | 
|         |     60 // -------------------------------------------------------------------------- | 
|         |     61 EXPORT_C CDevEncKeyUtils::CDevEncKeyUtils(): iConnect( EFalse ) | 
|         |     62     { | 
|         |     63     // No implementation necessary | 
|         |     64     } | 
|         |     65  | 
|         |     66 // -------------------------------------------------------------------------- | 
|         |     67 // CDevEncKeyUtils::CDevEncKeyUtils() | 
|         |     68 // | 
|         |     69 // -------------------------------------------------------------------------- | 
|         |     70 CDevEncKeyUtils::~CDevEncKeyUtils() | 
|         |     71     { | 
|         |     72      | 
|         |     73     } | 
|         |     74  | 
|         |     75 // -------------------------------------------------------------------------- | 
|         |     76 // CDevEncKeyUtils::CreateSetKey() | 
|         |     77 // | 
|         |     78 // -------------------------------------------------------------------------- | 
|         |     79 EXPORT_C void CDevEncKeyUtils::CreateSetKey( TRequestStatus& aStatus, | 
|         |     80                                              HBufC8*& aResult, | 
|         |     81                                              const TDesC8& aPassword, | 
|         |     82                                              const TInt aLength ) const | 
|         |     83     { | 
|         |     84     TInt error( KErrNone ); | 
|         |     85     if ( ! ProcessHasCapability( ECapabilityDiskAdmin ) ) | 
|         |     86         { | 
|         |     87         DFLOG( "Process does not have DiskAdmin capability" ); | 
|         |     88         error = KErrAccessDenied; | 
|         |     89         } | 
|         |     90     else if ( aPassword.Length() > KMaxPasswordLength || | 
|         |     91               aPassword.Length() < KMinPasswordLength ) | 
|         |     92         { | 
|         |     93         DFLOG( "CDevEncKeyUtils::CreateSetKey Invalid password length" ); | 
|         |     94         error = KErrArgument; | 
|         |     95         } | 
|         |     96     else if ( aLength > KEncryptionKeyLength ) | 
|         |     97         { | 
|         |     98         DFLOG( "CDevEncKeyUtils::CreateSetKey Invalid key length" ); | 
|         |     99         error = KErrArgument; | 
|         |    100         } | 
|         |    101     else | 
|         |    102         { | 
|         |    103         TRAP( error, DoCreateSetKeyL( aResult, aPassword, aLength ) ); | 
|         |    104         DFLOG2( "CDevEncKeyUtils::CreateSetKey result %d", error ); | 
|         |    105         } | 
|         |    106     TRequestStatus* statusPtr = &aStatus; | 
|         |    107     User::RequestComplete( statusPtr, error ); | 
|         |    108     } | 
|         |    109  | 
|         |    110 // -------------------------------------------------------------------------- | 
|         |    111 // CDevEncKeyUtils::CreateSetKey() | 
|         |    112 // | 
|         |    113 // -------------------------------------------------------------------------- | 
|         |    114 EXPORT_C void CDevEncKeyUtils::CreateSetKey( TRequestStatus& aStatus, | 
|         |    115                                              const TInt aLength ) const | 
|         |    116     { | 
|         |    117     TInt error( KErrNone ); | 
|         |    118  | 
|         |    119     HBufC8* password( NULL ); | 
|         |    120  | 
|         |    121     if ( ! ProcessHasCapability( ECapabilityDiskAdmin ) ) | 
|         |    122         { | 
|         |    123         DFLOG( "Process does not have DiskAdmin capability" ); | 
|         |    124         error = KErrAccessDenied; | 
|         |    125         } | 
|         |    126     if ( aLength > KEncryptionKeyLength ) | 
|         |    127         { | 
|         |    128         DFLOG( "CDevEncKeyUtils::CreateSetKey Invalid key length" ); | 
|         |    129         error = KErrArgument; | 
|         |    130         } | 
|         |    131  | 
|         |    132     if ( !error ) | 
|         |    133         { | 
|         |    134         TRAP( error, password = HBufC8::NewL( KMaxPasswordLength ) ); | 
|         |    135         if ( error ) | 
|         |    136             { | 
|         |    137             DFLOG2( "Pwd buf alloc error %d", error ); | 
|         |    138             } | 
|         |    139         } | 
|         |    140  | 
|         |    141     if ( !error ) | 
|         |    142         { | 
|         |    143         // Get some random password | 
|         |    144         TPtr8 passwordPtr = password->Des(); | 
|         |    145         passwordPtr.SetLength( KMaxPasswordLength ); | 
|         |    146         if( iConnect ) | 
|         |    147             { | 
|         |    148             iDevEncEngine->RandomDataGet( passwordPtr, KMaxPasswordLength ); | 
|         |    149             } | 
|         |    150          | 
|         |    151         DFLOG( "Random password allocated" ); | 
|         |    152          | 
|         |    153         TRAP( error, DoCreateSetKeyL( *password, | 
|         |    154                                       aLength ) ); | 
|         |    155         DFLOG2( "CDevEncKeyUtils::CreateSetKey result %d", error ); | 
|         |    156         } | 
|         |    157  | 
|         |    158     // Cleanup on demand | 
|         |    159     if ( password ) | 
|         |    160         { | 
|         |    161         delete password; | 
|         |    162         } | 
|         |    163      | 
|         |    164     TRequestStatus* statusPtr = &aStatus; | 
|         |    165     User::RequestComplete( statusPtr, error ); | 
|         |    166     } | 
|         |    167  | 
|         |    168 // -------------------------------------------------------------------------- | 
|         |    169 // CDevEncKeyUtils::DoCreateSetKey() | 
|         |    170 // | 
|         |    171 // -------------------------------------------------------------------------- | 
|         |    172 void CDevEncKeyUtils::DoCreateSetKeyL( const TDesC8& aPassword, | 
|         |    173                                        TInt aLength ) const | 
|         |    174     { | 
|         |    175     DFLOG2( ">>CDevEncKeyUtils::DoCreateSetKeyL, length %d", aLength ); | 
|         |    176  | 
|         |    177     // Get some random key data | 
|         |    178     HBufC8* clearKey = HBufC8::NewLC( aLength ); | 
|         |    179     TPtr8 clearKeyPtr = clearKey->Des(); | 
|         |    180     clearKeyPtr.SetLength( aLength ); | 
|         |    181  | 
|         |    182     if( iConnect ) | 
|         |    183         { | 
|         |    184         iDevEncEngine->RandomDataGet( clearKeyPtr, KMaxPasswordLength ); | 
|         |    185         } | 
|         |    186      | 
|         |    187     // if supply KDF, must also supply salt len and iteration count | 
|         |    188     CPBEncryptElement* encryption = CPBEncryptElement::NewLC( aPassword, | 
|         |    189                                                               ECipherDES_CBC ); | 
|         |    190     CPBEncryptor* encryptor = encryption->NewEncryptLC(); | 
|         |    191  | 
|         |    192     HBufC8* ciphertextTemp = HBufC8::NewLC( encryptor->MaxFinalOutputLength( clearKey->Length() ) );  | 
|         |    193     TPtr8 ciphertext = ciphertextTemp->Des();    | 
|         |    194     encryptor->ProcessFinalL( *clearKey, ciphertext ); | 
|         |    195  | 
|         |    196     // ENCRYPTION DONE | 
|         |    197  | 
|         |    198     DFLOG( "DoCreateSetKeyL, Key in plaintext:" ); | 
|         |    199     RDebug::RawPrint( clearKeyPtr ); | 
|         |    200     DFLOG( "DoCreateSetKeyL, Key in ciphertext:" ); | 
|         |    201     RDebug::RawPrint( ciphertext ); | 
|         |    202     DFLOG( "DoCreateSetKeyL, Password:" ); | 
|         |    203     RDebug::RawPrint( aPassword ); | 
|         |    204      | 
|         |    205     if( iConnect ) | 
|         |    206         { | 
|         |    207         // Take the new key in use | 
|         |    208         iDevEncEngine->TakeKeyInUseL( *clearKey ); | 
|         |    209         } | 
|         |    210      | 
|         |    211     // Destroy the evidence | 
|         |    212     CleanupStack::PopAndDestroy( ciphertextTemp ); | 
|         |    213     CleanupStack::PopAndDestroy( encryptor ); | 
|         |    214     CleanupStack::PopAndDestroy( encryption ); | 
|         |    215     CleanupStack::PopAndDestroy( clearKey ); | 
|         |    216     }; | 
|         |    217  | 
|         |    218 // -------------------------------------------------------------------------- | 
|         |    219 // CDevEncKeyUtils::DoCreateSetKey() | 
|         |    220 // | 
|         |    221 // -------------------------------------------------------------------------- | 
|         |    222 void CDevEncKeyUtils::DoCreateSetKeyL( HBufC8*& aResult, | 
|         |    223                                        const TDesC8& aPassword, | 
|         |    224                                        TInt aLength ) const | 
|         |    225     { | 
|         |    226     DFLOG2( ">>CDevEncKeyUtils::DoCreateSetKeyL, length %d", aLength ); | 
|         |    227  | 
|         |    228     // Get some random key data | 
|         |    229     HBufC8* clearKey = HBufC8::NewLC( aLength ); | 
|         |    230     TPtr8 clearKeyPtr = clearKey->Des(); | 
|         |    231     clearKeyPtr.SetLength( aLength ); | 
|         |    232      | 
|         |    233     if( iConnect ) | 
|         |    234         { | 
|         |    235         iDevEncEngine->RandomDataGet( clearKeyPtr, KMaxPasswordLength ); | 
|         |    236         } | 
|         |    237      | 
|         |    238     // if supply KDF, must also supply salt len and iteration count | 
|         |    239     CPBEncryptElement* encryption = CPBEncryptElement::NewLC( aPassword, | 
|         |    240                                                               ECipherDES_CBC ); | 
|         |    241     CPBEncryptor* encryptor = encryption->NewEncryptLC(); | 
|         |    242  | 
|         |    243     HBufC8* ciphertextTemp = HBufC8::NewLC( encryptor->MaxFinalOutputLength( clearKey->Length() ) );  | 
|         |    244     TPtr8 ciphertext = ciphertextTemp->Des();    | 
|         |    245     encryptor->ProcessFinalL( *clearKey, ciphertext ); | 
|         |    246  | 
|         |    247     // ENCRYPTION DONE | 
|         |    248  | 
|         |    249     DFLOG( "DoCreateSetKeyL, Key in plaintext:" ); | 
|         |    250     RDebug::RawPrint( clearKeyPtr ); | 
|         |    251     DFLOG( "DoCreateSetKeyL, Key in ciphertext:" ); | 
|         |    252     RDebug::RawPrint( ciphertext ); | 
|         |    253     DFLOG( "DoCreateSetKeyL, Password:" ); | 
|         |    254     RDebug::RawPrint( aPassword ); | 
|         |    255      | 
|         |    256     if( iConnect ) | 
|         |    257         { | 
|         |    258         // Take the new key in use | 
|         |    259         iDevEncEngine->TakeKeyInUseL( *clearKey ); | 
|         |    260         } | 
|         |    261  | 
|         |    262     // If we got this far, the operation was successful. | 
|         |    263     // Give the caller a copy of the encrypted key | 
|         |    264     // Externalize the key data and ciphertext | 
|         |    265     HBufC8* result = HBufC8::NewLC( ciphertext.Length() + | 
|         |    266                                    KMaxPasswordLength + | 
|         |    267                                    50 // should be enough for encryption data | 
|         |    268                                    ); | 
|         |    269     TPtr8 resultPtr = result->Des(); | 
|         |    270     ExternalizeKeyL( encryption, ciphertext, resultPtr ); | 
|         |    271      | 
|         |    272     // Encode key to base64 before returning it | 
|         |    273     aResult = HBufC8::NewL( resultPtr.Length()*4/3+3 ); | 
|         |    274     TPtr8 returnPtr = aResult->Des(); | 
|         |    275     returnPtr.SetLength( 0 ); | 
|         |    276     TImCodecB64 b64codec; | 
|         |    277     b64codec.Initialise(); | 
|         |    278     b64codec.Encode( *result, returnPtr ); | 
|         |    279      | 
|         |    280     // Destroy the evidence | 
|         |    281     CleanupStack::PopAndDestroy( result ); | 
|         |    282     CleanupStack::PopAndDestroy( ciphertextTemp ); | 
|         |    283     CleanupStack::PopAndDestroy( encryptor ); | 
|         |    284     CleanupStack::PopAndDestroy( encryption ); | 
|         |    285     CleanupStack::PopAndDestroy( clearKey ); | 
|         |    286     } | 
|         |    287  | 
|         |    288 // -------------------------------------------------------------------------- | 
|         |    289 // CDevEncKeyUtils::SetKey() | 
|         |    290 // | 
|         |    291 // -------------------------------------------------------------------------- | 
|         |    292 EXPORT_C void CDevEncKeyUtils::SetKey( TRequestStatus& aStatus, | 
|         |    293                                        const TDesC8& aPkcs5Key, | 
|         |    294                                        const TDesC8& aPassword ) const | 
|         |    295     { | 
|         |    296     TInt error( KErrNone ); | 
|         |    297  | 
|         |    298     if ( ! ProcessHasCapability( ECapabilityDiskAdmin ) ) | 
|         |    299         { | 
|         |    300         DFLOG( "Process does not have DiskAdmin capability" ); | 
|         |    301         error = KErrAccessDenied; | 
|         |    302         } | 
|         |    303     else | 
|         |    304         { | 
|         |    305         TRAP( error, DoSetKeyL( aPkcs5Key, aPassword ) ); | 
|         |    306         DFLOG2( "CDevEncKeyUtils::SetKey result %d", error ); | 
|         |    307         } | 
|         |    308     TRequestStatus* statusPtr = &aStatus; | 
|         |    309     User::RequestComplete( statusPtr, error ); | 
|         |    310     } | 
|         |    311  | 
|         |    312 // -------------------------------------------------------------------------- | 
|         |    313 // CDevEncKeyUtils::DoSetKey() | 
|         |    314 // | 
|         |    315 // -------------------------------------------------------------------------- | 
|         |    316 void CDevEncKeyUtils::DoSetKeyL( const TDesC8& aPkcs5Key, | 
|         |    317                                  const TDesC8& aPassword ) const | 
|         |    318     { | 
|         |    319     // Decode the base64 encoded key | 
|         |    320     HBufC8* decodedKey = HBufC8::NewLC( aPkcs5Key.Length()*3/4 ); | 
|         |    321     TPtr8 keyPtr = decodedKey->Des(); | 
|         |    322     keyPtr.SetLength( 0 ); | 
|         |    323     TImCodecB64 b64codec; | 
|         |    324     b64codec.Initialise(); | 
|         |    325     b64codec.Decode( aPkcs5Key, keyPtr ); | 
|         |    326  | 
|         |    327     // Read the parameters and ciphertext from the input | 
|         |    328     CPBEncryptElement* encryption( NULL ); | 
|         |    329     HBufC8* ciphertext( NULL ); | 
|         |    330     InternalizeKeyL( encryption, aPassword, ciphertext, *decodedKey ); | 
|         |    331     CleanupStack::PopAndDestroy( decodedKey ); | 
|         |    332     CleanupStack::PushL( encryption ); | 
|         |    333     CleanupStack::PushL( ciphertext ); | 
|         |    334  | 
|         |    335     // Decrypt and take key in use | 
|         |    336     CPBDecryptor* decryptor = encryption->NewDecryptLC(); | 
|         |    337     HBufC8* plaintextTemp = | 
|         |    338         HBufC8::NewLC( decryptor->MaxOutputLength( (*ciphertext).Size() ) );  | 
|         |    339     TPtr8 plaintext = plaintextTemp->Des();  | 
|         |    340     decryptor->Process( *ciphertext, plaintext ); | 
|         |    341  | 
|         |    342     Pkcs5RemovePadding( plaintext ); | 
|         |    343      | 
|         |    344     if( iConnect ) | 
|         |    345         { | 
|         |    346         // Take the new key in use | 
|         |    347         iDevEncEngine->TakeKeyInUseL( plaintext ); | 
|         |    348         } | 
|         |    349  | 
|         |    350     DFLOG( "DoSetKeyL, Key in plaintext:" ); | 
|         |    351     RDebug::RawPrint( plaintext ); | 
|         |    352     DFLOG( "DoSetKeyL, Key in ciphertext:" ); | 
|         |    353     RDebug::RawPrint( *ciphertext ); | 
|         |    354     DFLOG( "DoSetKeyL, Password:" ); | 
|         |    355     RDebug::RawPrint( aPassword ); | 
|         |    356  | 
|         |    357      | 
|         |    358     CleanupStack::PopAndDestroy( plaintextTemp ); | 
|         |    359     CleanupStack::PopAndDestroy( decryptor ); | 
|         |    360     CleanupStack::PopAndDestroy( ciphertext ); | 
|         |    361     CleanupStack::PopAndDestroy( encryption ); | 
|         |    362     } | 
|         |    363  | 
|         |    364 // -------------------------------------------------------------------------- | 
|         |    365 // CDevEncKeyUtils::ResetKey() | 
|         |    366 // | 
|         |    367 // -------------------------------------------------------------------------- | 
|         |    368 EXPORT_C void CDevEncKeyUtils::ResetKey( TRequestStatus& aStatus ) const | 
|         |    369     { | 
|         |    370     TInt error( KErrNone ); | 
|         |    371  | 
|         |    372     if ( ! ProcessHasCapability( ECapabilityDiskAdmin ) ) | 
|         |    373         { | 
|         |    374         DFLOG( "Process does not have DiskAdmin capability" ); | 
|         |    375         error = KErrAccessDenied; | 
|         |    376         } | 
|         |    377     else | 
|         |    378         { | 
|         |    379         TBuf8<KEncryptionKeyLength> nullKey; | 
|         |    380         nullKey.FillZ( KEncryptionKeyLength ); | 
|         |    381         TRAP( error,  | 
|         |    382         	  if( iConnect ) | 
|         |    383                   { | 
|         |    384         		  // Take the new key in use | 
|         |    385         		  iDevEncEngine->TakeKeyInUseL( nullKey ); | 
|         |    386         		  }   | 
|         |    387         	); | 
|         |    388         DFLOG2( "CDevEncKeyUtils::TakeKeyInUseL result %d", error ); | 
|         |    389         } | 
|         |    390     TRequestStatus* statusPtr = &aStatus; | 
|         |    391     User::RequestComplete( statusPtr, error ); | 
|         |    392     } | 
|         |    393  | 
|         |    394 // -------------------------------------------------------------------------- | 
|         |    395 // CDevEncKeyUtils::GetNewFileStoreL() | 
|         |    396 // | 
|         |    397 // -------------------------------------------------------------------------- | 
|         |    398 void CDevEncKeyUtils::GetNewFileStoreL( RFs& aFs, | 
|         |    399                                         TDes& aFileName, | 
|         |    400                                         CFileStore*& aStore ) const | 
|         |    401     { | 
|         |    402     // Leaves with KErrAlreadyExists if file exists from before | 
|         |    403     aStore = CPermanentFileStore::CreateL( aFs, | 
|         |    404                                            aFileName, | 
|         |    405                                            EFileRead | EFileWrite ); | 
|         |    406     } | 
|         |    407  | 
|         |    408 // -------------------------------------------------------------------------- | 
|         |    409 // CDevEncKeyUtils::SaveKeyL() | 
|         |    410 // | 
|         |    411 // -------------------------------------------------------------------------- | 
|         |    412 void CDevEncKeyUtils::SaveKeyL( CFileStore* aStore, | 
|         |    413                                 const CPBEncryptElement* aElement, | 
|         |    414                                 const TDesC8& aCiphertext ) const | 
|         |    415     { | 
|         |    416     RStoreWriteStream write; | 
|         |    417      | 
|         |    418     aStore->SetTypeL( aStore->Layout() ); | 
|         |    419      | 
|         |    420     //write the encryption data to a new stream | 
|         |    421     write.CreateLC( *aStore ); | 
|         |    422     aElement->EncryptionData().ExternalizeL( write ); | 
|         |    423     write.CommitL(); | 
|         |    424     CleanupStack::PopAndDestroy(); //CreateLC() | 
|         |    425  | 
|         |    426     //write the cyphertext to a new stream | 
|         |    427     write.CreateLC( *aStore ); | 
|         |    428     write << aCiphertext; | 
|         |    429     write.CommitL(); | 
|         |    430     CleanupStack::PopAndDestroy(); //CreateLC() | 
|         |    431  | 
|         |    432     aStore->Commit(); | 
|         |    433     } | 
|         |    434  | 
|         |    435 // -------------------------------------------------------------------------- | 
|         |    436 // CDevEncKeyUtils::LoadKeyLC() | 
|         |    437 // | 
|         |    438 // -------------------------------------------------------------------------- | 
|         |    439 void CDevEncKeyUtils::LoadKeyLC( RFs& aFs, | 
|         |    440                                  const TFileName& aFileName, | 
|         |    441                                  CPBEncryptionData*& aData, | 
|         |    442                                  HBufC8*& aCiphertext ) const | 
|         |    443     { | 
|         |    444     //prepare to read the streams back in, creating a new TPBEncryptionData | 
|         |    445     RStoreReadStream read; | 
|         |    446     // open the next PFS | 
|         |    447     CFileStore *store = CPermanentFileStore::OpenLC( aFs, | 
|         |    448                                                  aFileName, | 
|         |    449                                                  EFileRead ); | 
|         |    450     TStreamId dataStreamId( 1 ); // we know it was the first stream written | 
|         |    451     read.OpenLC( *store, dataStreamId ); | 
|         |    452 //    CleanupStack::Pop(); | 
|         |    453     //read in Encryption data | 
|         |    454     aData = CPBEncryptionData::NewL( read ); | 
|         |    455     CleanupStack::Pop(); // read | 
|         |    456     read.Close(); | 
|         |    457     CleanupStack::PushL( aData ); | 
|         |    458  | 
|         |    459     //read in ciphertext key | 
|         |    460     TStreamId cipherId( 2 ); // we know it was the second stream written | 
|         |    461     read.OpenLC( *store, cipherId ); | 
|         |    462     CleanupStack::Pop(); | 
|         |    463     aCiphertext = HBufC8::NewL( read, 10000 ); //some large number | 
|         |    464     read.Close(); | 
|         |    465  | 
|         |    466     CleanupStack::Pop( aData ); | 
|         |    467     CleanupStack::PopAndDestroy( store ); | 
|         |    468     CleanupStack::PushL( aData ); | 
|         |    469     CleanupStack::PushL( aCiphertext ); | 
|         |    470     } | 
|         |    471  | 
|         |    472 // -------------------------------------------------------------------------- | 
|         |    473 // CDevEncKeyUtils::ExternalizeKeyL() | 
|         |    474 // | 
|         |    475 // -------------------------------------------------------------------------- | 
|         |    476 void CDevEncKeyUtils::ExternalizeKeyL( const CPBEncryptElement* aElement, | 
|         |    477                                        const TDesC8& aCiphertext, | 
|         |    478                                        //HBufC8*& aResult ) const | 
|         |    479                                        TDes8& aResult ) const | 
|         |    480     { | 
|         |    481     RDesWriteStream write; | 
|         |    482     write.Open( aResult ); | 
|         |    483     write.PushL(); | 
|         |    484     aElement->EncryptionData().ExternalizeL( write ); | 
|         |    485     write << aCiphertext; | 
|         |    486     write.CommitL(); | 
|         |    487     write.Pop(); | 
|         |    488     write.Close(); | 
|         |    489     DFLOG( "CDevEncKeyUtils::ExternalizeKeyL done" ); | 
|         |    490     } | 
|         |    491  | 
|         |    492 // -------------------------------------------------------------------------- | 
|         |    493 // CDevEncKeyUtils::InternalizeKeyL() | 
|         |    494 // | 
|         |    495 // -------------------------------------------------------------------------- | 
|         |    496 void CDevEncKeyUtils::InternalizeKeyL( CPBEncryptElement*& aElement, | 
|         |    497                                        const TDesC8& aPassword, | 
|         |    498                                        HBufC8*& aCiphertext, | 
|         |    499                                        const TDesC8& aSource ) const | 
|         |    500     { | 
|         |    501     RDesReadStream read; | 
|         |    502     read.Open( aSource ); | 
|         |    503     read.PushL(); | 
|         |    504     CPBEncryptionData* data = CPBEncryptionData::NewLC( read ); | 
|         |    505     aElement = CPBEncryptElement::NewLC( *data, aPassword ); | 
|         |    506     aCiphertext = HBufC8::NewL( aSource.Length() ); | 
|         |    507     TPtr8 cipherTextPtr = aCiphertext->Des(); | 
|         |    508     read >> cipherTextPtr; | 
|         |    509     CleanupStack::Pop( aElement ); | 
|         |    510     CleanupStack::Pop( data ); | 
|         |    511     read.Pop(); | 
|         |    512     read.Close(); | 
|         |    513     DFLOG( "CDevEncKeyUtils::InternalizeKeyL done" ); | 
|         |    514     } | 
|         |    515  | 
|         |    516 // -------------------------------------------------------------------------- | 
|         |    517 // CDevEncKeyUtils::Pkcs5RemovePadding() | 
|         |    518 // | 
|         |    519 // -------------------------------------------------------------------------- | 
|         |    520 void CDevEncKeyUtils::Pkcs5RemovePadding( TPtr8& aInput ) const | 
|         |    521     { | 
|         |    522     // From RFC 2898: | 
|         |    523     //    The padding string PS consists of 8-(||M|| mod 8) octets | 
|         |    524     //    each with value 8-(||M|| mod 8). The padding string PS will | 
|         |    525     //    satisfy one of the following statements: | 
|         |    526     // | 
|         |    527     //            PS = 01, if ||M|| mod 8 = 7 ; | 
|         |    528     //            PS = 02 02, if ||M|| mod 8 = 6 ; | 
|         |    529     //            ... | 
|         |    530     //            PS = 08 08 08 08 08 08 08 08, if ||M|| mod 8 = 0. | 
|         |    531     // So the last byte shows how much padding there is | 
|         |    532     DFLOG( "CDevEncKeyUtils::Pkcs5RemovePadding" ); | 
|         |    533     TInt paddingBytes = aInput[ aInput.Length() - 1 ]; | 
|         |    534      | 
|         |    535     if ( paddingBytes <= 0 || | 
|         |    536          paddingBytes > 8  || | 
|         |    537          paddingBytes > aInput.Length() ) | 
|         |    538         { | 
|         |    539         return; | 
|         |    540         } | 
|         |    541  | 
|         |    542     TInt delPos( aInput.Length() - paddingBytes - 1 ); | 
|         |    543     TInt delLen( aInput.Length() - delPos ); | 
|         |    544     aInput.Delete( delPos, delLen ); | 
|         |    545     } | 
|         |    546  | 
|         |    547 // -------------------------------------------------------------------------- | 
|         |    548 // CDevEncKeyUtils::ProcessHasCapability() | 
|         |    549 // | 
|         |    550 // -------------------------------------------------------------------------- | 
|         |    551 TBool CDevEncKeyUtils::ProcessHasCapability( TCapability aCapability ) const | 
|         |    552     { | 
|         |    553     RProcess process; | 
|         |    554     return process.HasCapability( aCapability ); | 
|         |    555     } | 
|         |    556  | 
|         |    557 // -------------------------------------------------------------------------- | 
|         |    558 // CDevEncKeyUtils::LoadDevEncEngineL() | 
|         |    559 // | 
|         |    560 // -------------------------------------------------------------------------- | 
|         |    561 void CDevEncKeyUtils::LoadDevEncEngineL() | 
|         |    562     { | 
|         |    563     FLOG(" CDevEncKeyUtils::LoadDevEncEngineL >> "); | 
|         |    564      | 
|         |    565     if (!iDevEncEngine) | 
|         |    566         { | 
|         |    567          iConnect = EFalse; | 
|         |    568          TInt err = iLibrary.Load(KEncryptionDll); | 
|         |    569          if (err != KErrNone) | 
|         |    570              { | 
|         |    571              FLOG2("Error in finding the library... %d", err); | 
|         |    572              if (err == KErrNotFound) | 
|         |    573                  err = KErrNotSupported; | 
|         |    574              User::Leave(err); | 
|         |    575              } | 
|         |    576          TLibraryFunction entry = iLibrary.Lookup(1); | 
|         |    577           | 
|         |    578          if (!entry) | 
|         |    579              { | 
|         |    580              FLOG("Error in loading the library..."); | 
|         |    581              User::Leave(KErrBadLibraryEntryPoint); | 
|         |    582              } | 
|         |    583          iDevEncEngine = (CDevEncEngineBase *) entry(); | 
|         |    584          iConnect = ETrue; | 
|         |    585         } | 
|         |    586     FLOG(" CDevEncKeyUtils::LoadDevEncEngineL << "); | 
|         |    587     } | 
|         |    588  | 
|         |    589 // -------------------------------------------------------------------------- | 
|         |    590 // CDevEncKeyUtils::UnloadDevEncEngine() | 
|         |    591 // | 
|         |    592 // -------------------------------------------------------------------------- | 
|         |    593 void CDevEncKeyUtils::UnloadDevEncEngine() | 
|         |    594     { | 
|         |    595     FLOG(" CDevEncKeyUtils::UnloadDevEncEngineL >> "); | 
|         |    596      | 
|         |    597     if (iDevEncEngine) | 
|         |    598         { | 
|         |    599         iDevEncEngine->Close(); | 
|         |    600         delete iDevEncEngine; | 
|         |    601         iDevEncEngine = NULL; | 
|         |    602         iLibrary.Close(); | 
|         |    603         } | 
|         |    604     iConnect = EFalse; | 
|         |    605      | 
|         |    606     FLOG(" CDevEncKeyUtils::UnloadDevEncEngineL << "); | 
|         |    607     } | 
|         |    608  | 
|         |    609 // End of file |