vpnengine/pkiservice/src/pkiservicedecryptor.cpp
changeset 0 33413c0669b9
equal deleted inserted replaced
-1:000000000000 0:33413c0669b9
       
     1 /*
       
     2 * Copyright (c) 2008 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: A class that provides a decrypt operation.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 #include "pkiservicedecryptor.h"
       
    21 #include "logonservices.h"
       
    22 #include "pkiserviceconstants.h"
       
    23 #include "pkiserviceassert.h"
       
    24 
       
    25 CPkiServiceDecryptor* CPkiServiceDecryptor::NewL(CLogonServices& aLogonServices)
       
    26     {
       
    27     CPkiServiceDecryptor* self = new (ELeave)CPkiServiceDecryptor(aLogonServices);
       
    28     return self;
       
    29     }
       
    30 
       
    31 
       
    32 CPkiServiceDecryptor::CPkiServiceDecryptor(CLogonServices& aLogonServices)
       
    33 :CActive(EPriorityStandard), iLogonServices(aLogonServices)
       
    34     {
       
    35     CActiveScheduler::Add(this);
       
    36     }
       
    37 
       
    38 
       
    39 CPkiServiceDecryptor::~CPkiServiceDecryptor()
       
    40     {
       
    41     Cancel();
       
    42     }
       
    43 
       
    44 
       
    45 void CPkiServiceDecryptor::Decrypt(const TPKIKeyIdentifier& aKeyId,
       
    46                                    const TDesC8& aEncryptedData,
       
    47                                    TPtr8& aPlainTextData,
       
    48                                    CUnifiedKeyStore& aUnifiedKeyStore,
       
    49                                    TInt aUsedKeyStore, 
       
    50                                    TRequestStatus& aClientStatus)
       
    51     {
       
    52     PKISERVICE_ASSERT(iKeyStore == NULL);
       
    53     PKISERVICE_ASSERT(iInput == NULL);    
       
    54     PKISERVICE_ASSERT(iDecryptor == NULL);
       
    55     PKISERVICE_ASSERT(iKeysList.Count() == 0);
       
    56     PKISERVICE_ASSERT(iState == EDecryptorIdle);
       
    57     
       
    58     iState = EDecryptorGettingKey;
       
    59     
       
    60     iInput = &aEncryptedData;
       
    61     iPlainText = &aPlainTextData;
       
    62     iKeyStore = &aUnifiedKeyStore;      
       
    63     iUsedKeyStore = aUsedKeyStore;
       
    64     aClientStatus = KRequestPending;
       
    65     iClientStatus = &aClientStatus;
       
    66     
       
    67     TCTKeyAttributeFilter filter;
       
    68     filter.iKeyId = aKeyId;
       
    69     
       
    70     iKeyStore->List(iKeysList, filter, iStatus);
       
    71     SetActive();        
       
    72     }
       
    73 
       
    74 
       
    75 void CPkiServiceDecryptor::RunL()
       
    76     {
       
    77     if (iStatus == KErrNone)
       
    78         {
       
    79         switch(iState)
       
    80             {
       
    81             case EDecryptorGettingKey:                
       
    82                 iState = EDecryptorIdle;
       
    83                 TInt keyIndex;
       
    84                 for (keyIndex = 0; keyIndex < iKeysList.Count(); keyIndex++)
       
    85                     {
       
    86                     if (iUsedKeyStore == STORETYPE_ANY_KEY_ID ||
       
    87                         iUsedKeyStore == iKeysList[keyIndex]->Token().TokenType().Type().iUid)
       
    88                         {
       
    89                         break;
       
    90                         }
       
    91                     }
       
    92                 
       
    93                 if (keyIndex < iKeysList.Count())
       
    94 
       
    95                     {
       
    96                     iKeyInfo = iKeysList[keyIndex];
       
    97                     TCTTokenObjectHandle tokeHandle = iKeyInfo->Handle();
       
    98                     
       
    99                     iState = EDecryptorOpeningDecryptor;
       
   100                     iKeyStore->Open(tokeHandle, iDecryptor, iStatus);
       
   101                     SetActive();
       
   102                     }
       
   103                 else
       
   104                     {
       
   105                     Cleanup();
       
   106                     User::RequestComplete(iClientStatus, KPKIErrNotFound);                
       
   107                     }
       
   108 
       
   109                 break;
       
   110             case EDecryptorOpeningDecryptor:
       
   111                 iState = EDecryptorDecrypting;                                
       
   112                 iDecryptor->Decrypt(*iInput, *iPlainText, iStatus);
       
   113                 SetActive();                
       
   114                 break;
       
   115             case EDecryptorDecrypting:                            
       
   116                 iState = EDecryptorIdle;
       
   117                 if (iKeyInfo->Protector() != NULL)
       
   118                     {
       
   119                     //authObject is NULL for device store
       
   120                     iLogonServices.SetAuthenticationObject(iKeyInfo->Protector());
       
   121                     }
       
   122                 
       
   123                 Cleanup();  
       
   124                 User::RequestComplete(iClientStatus, KErrNone);
       
   125                 break;
       
   126             default:
       
   127                 PKISERVICE_INVARIANT();
       
   128                 break;            
       
   129             }        
       
   130         }
       
   131     else
       
   132         {
       
   133         iState = EDecryptorIdle;        
       
   134         Cleanup();
       
   135         User::RequestComplete(iClientStatus, iStatus.Int());
       
   136         }
       
   137     }
       
   138 
       
   139     
       
   140 void CPkiServiceDecryptor::DoCancel()
       
   141     {
       
   142     switch(iState)
       
   143         {
       
   144         case EDecryptorGettingKey:
       
   145             iKeyStore->CancelList();
       
   146             break;
       
   147         case EDecryptorOpeningDecryptor:
       
   148             iKeyStore->CancelOpen();
       
   149             break;
       
   150         case EDecryptorDecrypting:
       
   151             iDecryptor->CancelDecrypt();
       
   152             break;
       
   153         default:
       
   154             PKISERVICE_INVARIANT();
       
   155             break;            
       
   156         }
       
   157     Cleanup();        
       
   158     iState = EDecryptorIdle;    
       
   159     User::RequestComplete(iClientStatus, KErrCancel);
       
   160     }
       
   161 
       
   162 
       
   163 void CPkiServiceDecryptor::Cleanup()
       
   164     {
       
   165     if (iDecryptor != NULL)
       
   166         {        
       
   167         iDecryptor->Release();
       
   168         iDecryptor = NULL;
       
   169         }
       
   170     
       
   171     iKeyInfo = NULL;
       
   172     iKeysList.Close();    
       
   173     iInput = NULL;
       
   174     iKeyStore = NULL;
       
   175     }
       
   176