pkiutilities/DeviceToken/Src/KeyStore/Server/DevCertKeyStoreConduit.cpp
changeset 0 164170e6151a
equal deleted inserted replaced
-1:000000000000 0:164170e6151a
       
     1 /*
       
     2 * Copyright (c) 2006 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 DevCertKeyStoreConduit
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 #ifdef SYMBIAN_ENABLE_SPLIT_HEADERS
       
    21 #include <mctkeystoreuids.h>
       
    22 #endif
       
    23 #include "DevCertKeyStoreConduit.h"
       
    24 #include "DevCertKeyStoreServer.h"
       
    25 #include "DevCertKeyStoreSession.h"
       
    26 #include "DevCertOpenedKeysSrv.h"
       
    27 #include "DevTokenDataTypes.h"
       
    28 #include "DevTokenMarshaller.h"
       
    29 #include "DevTokenUtil.h"
       
    30 #include <asymmetric.h>
       
    31 #include <mctkeystoremanager.h>
       
    32 
       
    33 
       
    34 // ======== MEMBER FUNCTIONS ========
       
    35 
       
    36 // CDevCertKeyStoreConduit
       
    37 
       
    38 // ---------------------------------------------------------------------------
       
    39 // CDevCertKeyStoreConduit::NewL()
       
    40 // ---------------------------------------------------------------------------
       
    41 // 
       
    42 CDevCertKeyStoreConduit* CDevCertKeyStoreConduit::NewL(CDevCertKeyStoreServer& aServer)
       
    43     {
       
    44     CDevCertKeyStoreConduit* self = new (ELeave) CDevCertKeyStoreConduit(aServer);
       
    45     CleanupStack::PushL(self);
       
    46     self->ConstructL();
       
    47     CleanupStack::Pop(self);
       
    48     return self;
       
    49     }
       
    50 
       
    51 
       
    52 // ---------------------------------------------------------------------------
       
    53 // CDevCertKeyStoreConduit::~CDevCertKeyStoreConduit()
       
    54 // ---------------------------------------------------------------------------
       
    55 // 
       
    56 CDevCertKeyStoreConduit::~CDevCertKeyStoreConduit()
       
    57     {
       
    58     Cancel();
       
    59     }
       
    60 
       
    61 
       
    62 // ---------------------------------------------------------------------------
       
    63 // CDevCertKeyStoreConduit::CDevCertKeyStoreConduit()
       
    64 // ---------------------------------------------------------------------------
       
    65 // 
       
    66 CDevCertKeyStoreConduit::CDevCertKeyStoreConduit(CDevCertKeyStoreServer& aServer)
       
    67   : CActive(EPriorityHigh), iServer(aServer), iCurrentRequest(iStatus)
       
    68     {
       
    69     }
       
    70 
       
    71 
       
    72 // ---------------------------------------------------------------------------
       
    73 // CDevCertKeyStoreConduit::ConstructL()
       
    74 // ---------------------------------------------------------------------------
       
    75 //
       
    76 void CDevCertKeyStoreConduit::ConstructL()
       
    77     {
       
    78     CActiveScheduler::Add(this);
       
    79     }
       
    80 
       
    81 
       
    82 // ---------------------------------------------------------------------------
       
    83 // CDevCertKeyStoreConduit::ServiceRequestL()
       
    84 // ---------------------------------------------------------------------------
       
    85 //
       
    86 void CDevCertKeyStoreConduit::ServiceRequestL(const RMessage2& aMessage, CDevCertKeyStoreSession& aSession)
       
    87     {
       
    88     TDevTokenMessages request = static_cast<TDevTokenMessages>(aMessage.Function());
       
    89 
       
    90     if (iCurrentRequest.OutstandingRequest()!=EIdle)
       
    91         {
       
    92         //  There is currently a request outstanding, only allow this one if it's a cancel
       
    93         if ( (request!=ECancelCreateKey) &&
       
    94         (request!=ECancelImportKey) &&
       
    95         (request!=ECancelImportEncryptedKey) &&
       
    96         (request!=ECancelExportKey) &&
       
    97         (request!=ECancelRSASign) &&
       
    98         (request!=ECancelDSASign) &&
       
    99         (request!=ECancelDecrypt) )
       
   100             {
       
   101             User::Leave(KErrServerBusy);
       
   102             }
       
   103         }
       
   104 
       
   105     switch (request)
       
   106         {
       
   107         case EListKeys:
       
   108             ListL(aMessage);
       
   109             break;
       
   110         case EGetKeyInfo:
       
   111             GetKeyInfoL(aMessage);
       
   112             break;
       
   113         case ECreateKey:
       
   114             CreateKeyL(aMessage);
       
   115             break;
       
   116         case ECancelCreateKey:
       
   117             CancelCreateKey(aMessage);
       
   118             break;
       
   119         case EImportKey:
       
   120         case EImportEncryptedKey:
       
   121             ImportKeyL(aMessage);
       
   122             break;
       
   123         case ECancelImportKey:
       
   124         case ECancelImportEncryptedKey:
       
   125             CancelImportKey(aMessage);
       
   126             break;
       
   127         case EExportKey:
       
   128             ExportKeyL(aMessage);
       
   129             break;
       
   130         case ECancelExportKey:
       
   131             CancelExportKey(aMessage);
       
   132             break;
       
   133         case EExportPublic:
       
   134             ExportPublicL(aMessage);
       
   135             break;
       
   136         case EGetKeyLength:
       
   137             GetKeyLengthL(aMessage);
       
   138             break;
       
   139         case EDeleteKey:
       
   140             DeleteKeyL(aMessage);
       
   141             break;
       
   142         case ESetUsePolicy:
       
   143             SetUsePolicyL(aMessage);
       
   144             break;
       
   145         case ESetManagementPolicy:
       
   146             SetManagementPolicyL(aMessage);
       
   147             break;
       
   148         case EOpenKeyRepudiableRSASign:
       
   149             OpenKeyL(aMessage, aSession, KRSARepudiableSignerUID);
       
   150             break;
       
   151         case EOpenKeyRepudiableDSASign:
       
   152             OpenKeyL(aMessage, aSession, KDSARepudiableSignerUID);
       
   153             break;
       
   154         case EOpenKeyDecrypt:
       
   155             OpenKeyL(aMessage, aSession, KPrivateDecryptorUID);
       
   156             break;
       
   157         case EOpenKeyAgree:
       
   158             OpenKeyL(aMessage, aSession, KKeyAgreementUID);
       
   159             break;
       
   160         case ECloseObject:
       
   161             CloseObjectL(aMessage, aSession);
       
   162             break;
       
   163         case ERepudiableDSASign:
       
   164             RepudiableDSASignL(aMessage, aSession);
       
   165             break;
       
   166         case ECancelDSASign:
       
   167             CancelDSASign(aMessage);
       
   168             break;         
       
   169         case ERepudiableRSASign:
       
   170             RepudiableRSASignL(aMessage, aSession);
       
   171             break;
       
   172         case ECancelRSASign:
       
   173             CancelRSASign(aMessage);
       
   174             break;         
       
   175         case EDecryptText:
       
   176             DecryptL(aMessage, aSession);
       
   177             break;
       
   178         case ECancelDecrypt:
       
   179             CancelDecrypt(aMessage);
       
   180             break;         
       
   181         case EDHPublicKey:
       
   182             DHPublicKeyL(aMessage, aSession);
       
   183             break;
       
   184         case EDHAgree:
       
   185             DHAgreeL(aMessage, aSession);
       
   186             break;
       
   187         case ECancelDH:
       
   188             CancelDH(aMessage);
       
   189             break;         
       
   190 
       
   191         default:  //  Should not reach here
       
   192             __ASSERT_DEBUG(EFalse, PanicServer(EPanicInvalidRequest));
       
   193             User::Leave(KErrNotSupported);
       
   194         }
       
   195     }
       
   196 
       
   197 
       
   198 // ---------------------------------------------------------------------------
       
   199 // CDevCertKeyStoreConduit::ListL()
       
   200 // ---------------------------------------------------------------------------
       
   201 //
       
   202 void CDevCertKeyStoreConduit::ListL(const RMessage2& aMessage)
       
   203     {
       
   204     //  p[0] has the filter to use
       
   205     RCPointerArray<CDevTokenKeyInfo> keyInfos;
       
   206     CleanupClosePushL(keyInfos);
       
   207 
       
   208     TPckgBuf<TCTKeyAttributeFilter> filter;
       
   209     aMessage.ReadL(0, filter);
       
   210 
       
   211     iServer.ListL(filter(), keyInfos);
       
   212 
       
   213     TInt bufSize = User::LeaveIfError(aMessage.GetDesMaxLength(2)); 
       
   214     TInt reqdSize = DevTokenDataMarshaller::Size(keyInfos);
       
   215     if (bufSize >= reqdSize)
       
   216         {
       
   217         HBufC8* buffer = HBufC8::NewMaxLC(reqdSize);
       
   218         TPtr8 thePtr(buffer->Des());
       
   219         DevTokenDataMarshaller::Write(keyInfos, thePtr);
       
   220         aMessage.WriteL(2, thePtr);
       
   221         CleanupStack::PopAndDestroy(buffer);
       
   222         }
       
   223     else
       
   224         {
       
   225         TPckg<TInt> sizePckg(reqdSize);
       
   226         aMessage.WriteL(2, sizePckg);
       
   227         User::Leave(KErrOverflow);
       
   228         }
       
   229 
       
   230     CleanupStack::PopAndDestroy(&keyInfos); // keyInfos
       
   231     aMessage.Complete(KErrNone);
       
   232     }
       
   233 
       
   234 
       
   235 // ---------------------------------------------------------------------------
       
   236 // CDevCertKeyStoreConduit::GetKeyInfoL()
       
   237 // ---------------------------------------------------------------------------
       
   238 //
       
   239 void CDevCertKeyStoreConduit::GetKeyInfoL(const RMessage2& aMessage)
       
   240     {
       
   241     //  Token object to delete is in p[0]
       
   242     TInt objectId = aMessage.Int0();
       
   243     CDevTokenKeyInfo* info = NULL;
       
   244     iServer.GetKeyInfoL(objectId, info);
       
   245     info->CleanupPushL();
       
   246 
       
   247     TInt bufferSize = User::LeaveIfError(aMessage.GetDesMaxLength(2));
       
   248     TInt requiredSize = DevTokenDataMarshaller::Size(*info);
       
   249 
       
   250     if (bufferSize >= requiredSize)
       
   251         {
       
   252         HBufC8* buffer = HBufC8::NewMaxLC(requiredSize);
       
   253         TPtr8 thePtr(buffer->Des());
       
   254         DevTokenDataMarshaller::Write(*info, thePtr);
       
   255         aMessage.WriteL(2, thePtr);
       
   256         CleanupStack::PopAndDestroy(buffer);
       
   257         }
       
   258     else
       
   259         {
       
   260         TPckg<TInt> theSize(requiredSize);
       
   261         aMessage.WriteL(2, theSize);
       
   262         User::Leave(KErrOverflow);
       
   263         }
       
   264 
       
   265     CleanupStack::PopAndDestroy(info);
       
   266     aMessage.Complete(KErrNone);
       
   267     }
       
   268 
       
   269 
       
   270 // ---------------------------------------------------------------------------
       
   271 // CDevCertKeyStoreConduit::GetClientUidL()
       
   272 // Work out the UID of the client process.
       
   273 // ---------------------------------------------------------------------------
       
   274 //
       
   275 TUid CDevCertKeyStoreConduit::GetClientUidL(const RMessage2& aMessage) const
       
   276     {
       
   277     RThread clientThread;
       
   278     User::LeaveIfError(aMessage.Client(clientThread));
       
   279     CleanupClosePushL(clientThread);  
       
   280     RProcess clientProcess;
       
   281     User::LeaveIfError(clientThread.Process(clientProcess));
       
   282     CleanupClosePushL(clientProcess);
       
   283     TUid uid = clientProcess.Type()[2];
       
   284     CleanupStack::PopAndDestroy(2); // clientProcess, clientThread
       
   285     return uid;
       
   286     }
       
   287 
       
   288 
       
   289 // ---------------------------------------------------------------------------
       
   290 // CDevCertKeyStoreConduit::CreateKeyL()
       
   291 // ---------------------------------------------------------------------------
       
   292 //
       
   293 void CDevCertKeyStoreConduit::CreateKeyL(const RMessage2& aMessage)
       
   294     {
       
   295     ASSERT(!iKeyInfo);
       
   296     //  p[0] has the length of the buffer. Check our buffer is big
       
   297     //  enough, to cope with requests for keys with very long labels.
       
   298 
       
   299     TInt bufLength = User::LeaveIfError(aMessage.GetDesLength(1));
       
   300     HBufC8* keyInfoBuf = HBufC8::NewMaxLC(bufLength);
       
   301 
       
   302     //  p[1] has the CDevTokenKeyInfo structure required to create a key
       
   303     //  Read it and convert from descriptor back to a CDevTokenKeyInfo
       
   304     TPtr8 thePtr(keyInfoBuf->Des());
       
   305     thePtr.FillZ();
       
   306 
       
   307     aMessage.ReadL(1, thePtr);
       
   308     DevTokenDataMarshaller::ReadL(*keyInfoBuf, iKeyInfo);
       
   309     CleanupStack::PopAndDestroy(keyInfoBuf);
       
   310 
       
   311     iCurrentRequest.Set(ECreateKey, aMessage);
       
   312     iStatus = KRequestPending;
       
   313     SetActive();
       
   314     iServer.CreateKey(*iKeyInfo, iStatus);
       
   315     }
       
   316 
       
   317 
       
   318 // ---------------------------------------------------------------------------
       
   319 // CDevCertKeyStoreConduit::CancelCreateKey()
       
   320 // ---------------------------------------------------------------------------
       
   321 //
       
   322 void CDevCertKeyStoreConduit::CancelCreateKey(const RMessage2& aMessage)
       
   323     {
       
   324     if (iCurrentRequest.OutstandingRequest() == ECreateKey)
       
   325         {
       
   326         Cancel();
       
   327         }
       
   328     aMessage.Complete(KErrNone);
       
   329     }
       
   330 
       
   331 
       
   332 // ---------------------------------------------------------------------------
       
   333 // CDevCertKeyStoreConduit::ImportKeyL()
       
   334 // ---------------------------------------------------------------------------
       
   335 //
       
   336 void CDevCertKeyStoreConduit::ImportKeyL(const RMessage2& aMessage)
       
   337     {
       
   338     ASSERT(!iImportKey);
       
   339     ASSERT(!iKeyInfo);
       
   340 
       
   341     // p[0] has the descriptor containing the PKCS8 object (not be encrypted)
       
   342     TInt keyLen = User::LeaveIfError(aMessage.GetDesLength(0));
       
   343 
       
   344     HBufC8* importBuf = HBufC8::NewMaxLC(keyLen);
       
   345     TPtr8 theKeyData(importBuf->Des());
       
   346     theKeyData.FillZ();
       
   347     aMessage.ReadL(0, theKeyData);
       
   348 
       
   349     TInt bufLen = User::LeaveIfError(aMessage.GetDesLength(1));
       
   350     HBufC8* keyInfoBuf = HBufC8::NewMaxLC(bufLen);
       
   351 
       
   352     // p[1] has the CDevTokenKeyInfo structure required to create a key
       
   353     // Read it and convert from descriptor back to a CDevTokenKeyInfo
       
   354     TPtr8 thePtr(keyInfoBuf->Des());
       
   355     thePtr.FillZ();
       
   356     aMessage.ReadL(1, thePtr);
       
   357 
       
   358     DevTokenDataMarshaller::ReadL(*keyInfoBuf, iKeyInfo);
       
   359     CleanupStack::PopAndDestroy(keyInfoBuf);
       
   360 
       
   361     iImportKey = importBuf;
       
   362     CleanupStack::Pop(importBuf);
       
   363 
       
   364     iCurrentRequest.Set(static_cast<TDevTokenMessages>(aMessage.Function()), aMessage);
       
   365     iStatus = KRequestPending;
       
   366     SetActive();
       
   367     
       
   368     TBool isEncrypted = (aMessage.Function() == EImportEncryptedKey);
       
   369 	iServer.ImportKey(*iImportKey, *iKeyInfo, isEncrypted, iStatus);
       
   370     }
       
   371 
       
   372 
       
   373 // ---------------------------------------------------------------------------
       
   374 // CDevCertKeyStoreConduit::CancelImportKey()
       
   375 // ---------------------------------------------------------------------------
       
   376 //
       
   377 void CDevCertKeyStoreConduit::CancelImportKey(const RMessage2& aMessage)
       
   378     {
       
   379     if (iCurrentRequest.OutstandingRequest() == EImportKey ||
       
   380 		iCurrentRequest.OutstandingRequest() == EImportEncryptedKey)
       
   381         {
       
   382         Cancel();
       
   383         }
       
   384     aMessage.Complete(KErrNone); 
       
   385     }
       
   386 
       
   387 
       
   388 // ---------------------------------------------------------------------------
       
   389 // CDevCertKeyStoreConduit::GetKeyLengthL()
       
   390 // ---------------------------------------------------------------------------
       
   391 //
       
   392 void CDevCertKeyStoreConduit::GetKeyLengthL(const RMessage2& aMessage)
       
   393     {
       
   394     TInt length = iServer.GetKeyLengthL(aMessage.Int0());
       
   395     ASSERT(length > 0);
       
   396     aMessage.Complete(length);    
       
   397     }
       
   398 
       
   399 
       
   400 // ---------------------------------------------------------------------------
       
   401 // CDevCertKeyStoreConduit::CancelExportKey()
       
   402 // ---------------------------------------------------------------------------
       
   403 //
       
   404 void CDevCertKeyStoreConduit::CancelExportKey(const RMessage2& aMessage)
       
   405     {
       
   406     if (iCurrentRequest.OutstandingRequest() == EExportKey ||
       
   407         iCurrentRequest.OutstandingRequest() == EImportEncryptedKey)
       
   408         {
       
   409         Cancel();
       
   410         }
       
   411     aMessage.Complete(KErrNone);
       
   412     }
       
   413 
       
   414 
       
   415 // ---------------------------------------------------------------------------
       
   416 // CDevCertKeyStoreConduit::ExportKeyL()
       
   417 // ---------------------------------------------------------------------------
       
   418 //
       
   419 void CDevCertKeyStoreConduit::ExportKeyL(const RMessage2& aMessage)
       
   420     {
       
   421     ASSERT(!iExportBuf);
       
   422 
       
   423     TInt objectId = aMessage.Int0();
       
   424     TInt bufLen = User::LeaveIfError(aMessage.GetDesMaxLength(1));  // #1 IPC argument is the request Ptr
       
   425 
       
   426     HBufC8* exportBuf =  HBufC8::NewMaxLC(bufLen);
       
   427     TPtr8 temp(exportBuf->Des());
       
   428     temp.FillZ();
       
   429 
       
   430     // No more leaves
       
   431     iExportBuf = exportBuf;
       
   432     CleanupStack::Pop(exportBuf);
       
   433 
       
   434     iCurrentRequest.Set(static_cast<TDevTokenMessages>(aMessage.Function()), aMessage); 
       
   435     iStatus = KRequestPending;
       
   436     SetActive();    
       
   437 
       
   438     if (aMessage.Function() == EExportKey) 
       
   439         {
       
   440         iServer.ExportKey(objectId, temp, iStatus);
       
   441         }    
       
   442     }
       
   443 
       
   444 
       
   445 // ---------------------------------------------------------------------------
       
   446 // CDevCertKeyStoreConduit::ExportPublicL()
       
   447 // ---------------------------------------------------------------------------
       
   448 //
       
   449 void CDevCertKeyStoreConduit::ExportPublicL(const RMessage2& aMessage)
       
   450     {
       
   451     TInt objectId = aMessage.Int0();
       
   452     TInt bufLen = User::LeaveIfError(aMessage.GetDesMaxLength(1));
       
   453 
       
   454     HBufC8* exportBuf = HBufC8::NewMaxLC(bufLen);
       
   455     TPtr8 ptr(exportBuf->Des());
       
   456     ptr.FillZ();
       
   457     iServer.ExportPublicL(objectId, ptr);
       
   458     aMessage.WriteL(1, ptr);
       
   459 
       
   460     CleanupStack::PopAndDestroy(exportBuf);
       
   461     aMessage.Complete(KErrNone);
       
   462     }
       
   463 
       
   464 
       
   465 // ---------------------------------------------------------------------------
       
   466 // CDevCertKeyStoreConduit::DeleteKeyL()
       
   467 // ---------------------------------------------------------------------------
       
   468 //
       
   469 void CDevCertKeyStoreConduit::DeleteKeyL(const RMessage2& aMessage)
       
   470     {
       
   471     TInt objectId = aMessage.Int0();
       
   472     iServer.DeleteKeyL(objectId);
       
   473     aMessage.Complete(KErrNone);
       
   474     }
       
   475 
       
   476 
       
   477 // ---------------------------------------------------------------------------
       
   478 // CDevCertKeyStoreConduit::SetUsePolicyL()
       
   479 // ---------------------------------------------------------------------------
       
   480 //
       
   481 void CDevCertKeyStoreConduit::SetUsePolicyL(const RMessage2& aMessage)
       
   482     {
       
   483     TInt objectId = aMessage.Int0();
       
   484     TSecurityPolicyBuf policyBuf;
       
   485 
       
   486     aMessage.ReadL(1, policyBuf);
       
   487 
       
   488     iServer.SetUsePolicyL(objectId, policyBuf());
       
   489     aMessage.Complete(KErrNone);
       
   490     }
       
   491 
       
   492 
       
   493 // ---------------------------------------------------------------------------
       
   494 // CDevCertKeyStoreConduit::SetManagementPolicyL()
       
   495 // ---------------------------------------------------------------------------
       
   496 //
       
   497 void CDevCertKeyStoreConduit::SetManagementPolicyL(const RMessage2& aMessage)
       
   498     {
       
   499     TInt objectId = aMessage.Int0();
       
   500     TSecurityPolicyBuf policyBuf;
       
   501 
       
   502     aMessage.ReadL(1, policyBuf);
       
   503 
       
   504     iServer.SetManagementPolicyL(objectId, policyBuf());
       
   505     aMessage.Complete(KErrNone);
       
   506     }
       
   507 
       
   508 
       
   509 // ---------------------------------------------------------------------------
       
   510 // CDevCertKeyStoreConduit::OpenKeyL()
       
   511 // ---------------------------------------------------------------------------
       
   512 //
       
   513 void CDevCertKeyStoreConduit::OpenKeyL(const RMessage2& aMessage, 
       
   514                                        CDevCertKeyStoreSession& aSession, 
       
   515                                        TUid aType)
       
   516     { 
       
   517     CDevCertOpenedKeySrv* openedKey = iServer.OpenKeyL(aMessage.Int0(), aType);
       
   518     CleanupStack::PushL(openedKey);
       
   519 
       
   520     const TDesC& label = openedKey->Label();
       
   521 
       
   522     TInt writeBufLen = User::LeaveIfError(aMessage.GetDesMaxLength(3));
       
   523     TInt reqdLength = label.Length();
       
   524     if (writeBufLen < reqdLength)
       
   525         {
       
   526         // We're writing into a TDes16 so we can't use TPckg<TInt>
       
   527         TPtrC sizePtr(reinterpret_cast<TUint16*>(&reqdLength), 2);
       
   528         aMessage.WriteL(3, sizePtr);
       
   529         User::Leave(KErrOverflow);
       
   530         }
       
   531 
       
   532     aMessage.WriteL(3, label);
       
   533 
       
   534     TInt handle = aSession.AddOpenedKeyL(*openedKey);
       
   535 
       
   536     TPckg<TInt> handlePckg(handle);
       
   537     TRAPD(err, aMessage.WriteL(1, handlePckg));
       
   538 
       
   539     if (err != KErrNone)
       
   540         {
       
   541         aSession.RemoveOpenedKeyL(handle);
       
   542         User::Leave(err);
       
   543         }
       
   544 
       
   545     CleanupStack::Pop(openedKey); // now owned by session
       
   546     aMessage.Complete(KErrNone);
       
   547     }
       
   548 
       
   549 
       
   550 // ---------------------------------------------------------------------------
       
   551 // CDevCertKeyStoreConduit::RepudiableRSASignL()
       
   552 // ---------------------------------------------------------------------------
       
   553 //
       
   554 void CDevCertKeyStoreConduit::RepudiableRSASignL(const RMessage2& aMessage, 
       
   555                                          CDevCertKeyStoreSession& aSession)
       
   556     {
       
   557     ASSERT( iOpenedKey == NULL );
       
   558     TPtr8 thePtr(0,0);
       
   559     iOpenedKey = ProcessL(aMessage, aSession, KRSARepudiableSignerUID, ERepudiableRSASign, thePtr);
       
   560     static_cast<CDevCertRSARepudiableSignerSrv*>(iOpenedKey)->Sign(thePtr, iRSASignature, iStatus);
       
   561     }
       
   562 
       
   563 
       
   564 // ---------------------------------------------------------------------------
       
   565 // CDevCertKeyStoreConduit::CancelRSASign()
       
   566 // ---------------------------------------------------------------------------
       
   567 //
       
   568 void CDevCertKeyStoreConduit::CancelRSASign(const RMessage2& aMessage)
       
   569     {
       
   570     if (iCurrentRequest.OutstandingRequest() == ERepudiableRSASign)
       
   571         {
       
   572         Cancel();
       
   573         }
       
   574     aMessage.Complete(KErrNone);
       
   575     }
       
   576 
       
   577 
       
   578 // ---------------------------------------------------------------------------
       
   579 // CDevCertKeyStoreConduit::RepudiableDSASignL()
       
   580 // ---------------------------------------------------------------------------
       
   581 //
       
   582 void CDevCertKeyStoreConduit::RepudiableDSASignL(const RMessage2& aMessage, 
       
   583                                           CDevCertKeyStoreSession& aSession)
       
   584     {
       
   585     ASSERT( iOpenedKey == NULL );
       
   586     TPtr8 thePtr(0,0);
       
   587     iOpenedKey = ProcessL(aMessage, aSession, KDSARepudiableSignerUID, ERepudiableDSASign, thePtr);
       
   588     static_cast<CDevCertDSARepudiableSignerSrv*>(iOpenedKey)->Sign(thePtr, iDSASignature, iStatus);
       
   589     }
       
   590 
       
   591 
       
   592 // ---------------------------------------------------------------------------
       
   593 // CDevCertKeyStoreConduit::CancelDSASign()
       
   594 // ---------------------------------------------------------------------------
       
   595 //
       
   596 void CDevCertKeyStoreConduit::CancelDSASign(const RMessage2& aMessage)
       
   597     {
       
   598     if (iCurrentRequest.OutstandingRequest() == ERepudiableDSASign)
       
   599         {
       
   600         Cancel();
       
   601         }
       
   602     aMessage.Complete(KErrNone);
       
   603     }
       
   604 
       
   605 
       
   606 // ---------------------------------------------------------------------------
       
   607 // CDevCertKeyStoreConduit::DecryptL()
       
   608 // ---------------------------------------------------------------------------
       
   609 //
       
   610 void CDevCertKeyStoreConduit::DecryptL(const RMessage2& aMessage, 
       
   611                                        CDevCertKeyStoreSession& aSession)
       
   612     {
       
   613     ASSERT( iOpenedKey == NULL );
       
   614     TPtr8 thePtr(0,0);
       
   615     iOpenedKey = ProcessL(aMessage, aSession, KPrivateDecryptorUID, EDecryptText, thePtr);
       
   616     static_cast<CDevCertRSADecryptorSrv*>(iOpenedKey)->Decrypt(thePtr, iPlaintext, iStatus);
       
   617     }
       
   618 
       
   619 
       
   620 // ---------------------------------------------------------------------------
       
   621 // CDevCertKeyStoreConduit::CancelDecrypt()
       
   622 // ---------------------------------------------------------------------------
       
   623 //
       
   624 void CDevCertKeyStoreConduit::CancelDecrypt(const RMessage2& aMessage)
       
   625     {
       
   626     if (iCurrentRequest.OutstandingRequest() == EDecryptText)
       
   627         {
       
   628         Cancel();
       
   629         }
       
   630     aMessage.Complete(KErrNone);
       
   631     }
       
   632 
       
   633 
       
   634 // ---------------------------------------------------------------------------
       
   635 // CDevCertKeyStoreConduit::ProcessL()
       
   636 // ---------------------------------------------------------------------------
       
   637 //
       
   638 CDevCertOpenedKeySrv* CDevCertKeyStoreConduit::ProcessL(
       
   639                      const RMessage2& aMessage,
       
   640                      CDevCertKeyStoreSession& aSession,
       
   641                      const TUid& aCKeyInfoID,
       
   642                      const TDevTokenMessages& aState,
       
   643                      TPtr8& aTextPtr)
       
   644     {
       
   645     CDevCertOpenedKeySrv* object = aSession.OpenedKey(aMessage.Int0());
       
   646     if (!object)
       
   647         {
       
   648         User::Leave(KErrNotFound);
       
   649         }
       
   650 
       
   651     if (aCKeyInfoID != object->Type())
       
   652         {
       
   653         User::Leave(KErrAccessDenied);
       
   654         }
       
   655 
       
   656     TInt length = User::LeaveIfError(aMessage.GetDesLength(1));
       
   657     iText = HBufC8::NewL(length);
       
   658     aTextPtr.Set(iText->Des());
       
   659     aMessage.ReadL(1, aTextPtr);
       
   660 
       
   661     iCurrentRequest.Set(aState, aMessage);
       
   662     iStatus=KRequestPending;
       
   663     SetActive();
       
   664 
       
   665     return object;
       
   666     }
       
   667 
       
   668 
       
   669 // ---------------------------------------------------------------------------
       
   670 // CDevCertKeyStoreConduit::DHPublicKeyL()
       
   671 // ---------------------------------------------------------------------------
       
   672 //
       
   673 void CDevCertKeyStoreConduit::DHPublicKeyL(const RMessage2& aMessage, 
       
   674                                       CDevCertKeyStoreSession& aSession)
       
   675     {
       
   676     // 0: Object id
       
   677     // 1: DH paramters
       
   678 
       
   679     ASSERT(iDHParams == NULL );
       
   680     ASSERT(iOpenedKey == NULL );
       
   681     TRAPD(err, DoDHPublicKeyL(aMessage, aSession));
       
   682     if (err != KErrNone)
       
   683         {
       
   684         iOpenedKey = NULL;
       
   685         delete iDHParams;
       
   686         iDHParams = NULL;
       
   687         User::Leave(err);
       
   688         }
       
   689     }
       
   690 
       
   691 
       
   692 // ---------------------------------------------------------------------------
       
   693 // CDevCertKeyStoreConduit::DoDHPublicKeyL()
       
   694 // ---------------------------------------------------------------------------
       
   695 //
       
   696 void CDevCertKeyStoreConduit::DoDHPublicKeyL(const RMessage2& aMessage, 
       
   697                                       CDevCertKeyStoreSession& aSession)
       
   698     {
       
   699     iOpenedKey = aSession.OpenedKey(aMessage.Int0());
       
   700     if (!iOpenedKey)
       
   701         {
       
   702         User::Leave(KErrNotFound);
       
   703         }
       
   704 
       
   705     //Check that this is a DH object
       
   706     if (KKeyAgreementUID != iOpenedKey->Type())
       
   707         {
       
   708         iOpenedKey = NULL;
       
   709         User::Leave(KErrAccessDenied);
       
   710         }
       
   711 
       
   712     HBufC8* clientBuf = HBufC8::NewMaxLC(User::LeaveIfError(aMessage.GetDesLength(1)));
       
   713     TPtr8 clientPtr = clientBuf->Des();
       
   714     aMessage.ReadL(1, clientPtr);
       
   715     DevTokenDataMarshaller::ReadL(*clientBuf, iDHParams);
       
   716     CleanupStack::PopAndDestroy(clientBuf);
       
   717 
       
   718     static_cast<CDevCertDHAgreementSrv*>(iOpenedKey)->PublicKey(*iDHParams, iDHPublicKeyOut, iStatus);
       
   719 
       
   720     iCurrentRequest.Set(EDHPublicKey, aMessage);
       
   721     iStatus=KRequestPending;
       
   722     SetActive();
       
   723     }
       
   724 
       
   725 
       
   726 // ---------------------------------------------------------------------------
       
   727 // CDevCertKeyStoreConduit::FinishDHPublicKeyL()
       
   728 // ---------------------------------------------------------------------------
       
   729 //
       
   730 void CDevCertKeyStoreConduit::FinishDHPublicKeyL()
       
   731     {
       
   732     // Client buffer must be big enough for result because it had the DH
       
   733     // parameters in it
       
   734     TInt reqdSize = DevTokenDataMarshaller::Size(iDHPublicKeyOut);
       
   735     ASSERT(reqdSize <= iCurrentRequest.Message().GetDesMaxLength(1));
       
   736 
       
   737     HBufC8* clientBuf = HBufC8::NewMaxLC(reqdSize);
       
   738     TPtr8 clientPtr = clientBuf->Des();
       
   739     DevTokenDataMarshaller::WriteL(iDHPublicKeyOut, clientPtr);
       
   740     iCurrentRequest.Message().WriteL(1, clientPtr);
       
   741     CleanupStack::PopAndDestroy(clientBuf);
       
   742     }
       
   743 
       
   744 
       
   745 // ---------------------------------------------------------------------------
       
   746 // CDevCertKeyStoreConduit::DHAgreeL()
       
   747 // ---------------------------------------------------------------------------
       
   748 //
       
   749 void CDevCertKeyStoreConduit::DHAgreeL(const RMessage2& aMessage, CDevCertKeyStoreSession& aSession)
       
   750     {
       
   751     // 0: Object id
       
   752     // 1: DH public key
       
   753 
       
   754     ASSERT( iOpenedKey == NULL );
       
   755     ASSERT( iDHPublicKey == NULL );
       
   756     TRAPD(err, DoDHAgreeL(aMessage, aSession));
       
   757     if (err != KErrNone)
       
   758         {
       
   759         iOpenedKey = NULL;
       
   760         delete iDHPublicKey;
       
   761         iDHPublicKey = NULL;
       
   762         User::Leave(err);
       
   763         }
       
   764     }
       
   765 
       
   766 
       
   767 // ---------------------------------------------------------------------------
       
   768 // CDevCertKeyStoreConduit::DoDHAgreeL()
       
   769 // ---------------------------------------------------------------------------
       
   770 //
       
   771 void CDevCertKeyStoreConduit::DoDHAgreeL(const RMessage2& aMessage, CDevCertKeyStoreSession& aSession)
       
   772     {
       
   773     CDevCertOpenedKeySrv* iOpenedKey = aSession.OpenedKey(aMessage.Int0());
       
   774     if (!iOpenedKey)
       
   775         {
       
   776         User::Leave(KErrNotFound);
       
   777         }
       
   778 
       
   779     //Check that this is a DH object
       
   780     if (KKeyAgreementUID != iOpenedKey->Type())
       
   781         {
       
   782         iOpenedKey = NULL;
       
   783         User::Leave(KErrAccessDenied);
       
   784         }
       
   785 
       
   786     HBufC8* clientBuf = HBufC8::NewMaxLC(User::LeaveIfError(aMessage.GetDesLength(1)));
       
   787     TPtr8 clientPtr = clientBuf->Des();
       
   788     aMessage.ReadL(1, clientPtr);
       
   789     DevTokenDataMarshaller::ReadL(*clientBuf, iDHPublicKey);
       
   790     CleanupStack::PopAndDestroy(clientBuf);
       
   791 
       
   792     static_cast<CDevCertDHAgreementSrv*>(iOpenedKey)->Agree(*iDHPublicKey, iDHAgreedKeyOut, iStatus);
       
   793 
       
   794     iCurrentRequest.Set(EDHAgree, aMessage);
       
   795     iStatus=KRequestPending;
       
   796     SetActive();
       
   797     }
       
   798 
       
   799 
       
   800 // ---------------------------------------------------------------------------
       
   801 // CDevCertKeyStoreConduit::FinishDHAgreeL()
       
   802 // ---------------------------------------------------------------------------
       
   803 //
       
   804 void CDevCertKeyStoreConduit::FinishDHAgreeL()
       
   805     {
       
   806     // Client buffer must be big enough for result because it had the DH
       
   807     // public key in it
       
   808     ASSERT(iDHAgreedKeyOut->Length() <= iCurrentRequest.Message().GetDesMaxLength(1));
       
   809 
       
   810     iCurrentRequest.Message().WriteL(1, *iDHAgreedKeyOut);
       
   811     }
       
   812 
       
   813 
       
   814 // ---------------------------------------------------------------------------
       
   815 // CDevCertKeyStoreConduit::CancelDH()
       
   816 // ---------------------------------------------------------------------------
       
   817 //
       
   818 void CDevCertKeyStoreConduit::CancelDH(const RMessage2& aMessage)
       
   819     {
       
   820     if (iCurrentRequest.OutstandingRequest() == EDHPublicKey ||
       
   821     iCurrentRequest.OutstandingRequest() == EDHAgree)
       
   822         {
       
   823         Cancel();
       
   824         }
       
   825     aMessage.Complete(KErrNone);
       
   826     }
       
   827 
       
   828 
       
   829 // ---------------------------------------------------------------------------
       
   830 // CDevCertKeyStoreConduit::CloseObjectL()
       
   831 // ---------------------------------------------------------------------------
       
   832 //
       
   833 void CDevCertKeyStoreConduit::CloseObjectL(const RMessage2& aMessage, CDevCertKeyStoreSession& aSession)
       
   834     {
       
   835     aSession.RemoveOpenedKeyL(aMessage.Int0());
       
   836     aMessage.Complete(KErrNone);
       
   837     }
       
   838 
       
   839 
       
   840 // ---------------------------------------------------------------------------
       
   841 // CDevCertKeyStoreConduit::DoCancel()
       
   842 // ---------------------------------------------------------------------------
       
   843 //
       
   844 void CDevCertKeyStoreConduit::DoCancel()
       
   845     {
       
   846     switch (iCurrentRequest.OutstandingRequest())
       
   847         {
       
   848         case ECreateKey:
       
   849             iServer.CancelCreateKey();
       
   850             break;
       
   851 
       
   852         case ERepudiableRSASign:
       
   853         case ERepudiableDSASign:
       
   854         case EDecryptText:
       
   855         case EDHAgree:
       
   856         case EDHPublicKey:
       
   857             if (iOpenedKey)
       
   858                 {       
       
   859                 iOpenedKey->Cancel();
       
   860                 }
       
   861             break;
       
   862 
       
   863         case EImportKey:
       
   864             iServer.CancelImportKey();
       
   865             break;
       
   866 
       
   867         case EExportKey:
       
   868             iServer.CancelExportKey();
       
   869             break;
       
   870 
       
   871         default:
       
   872             // Nothing to do
       
   873             break;
       
   874         }
       
   875 
       
   876     Cleanup();
       
   877     iCurrentRequest.Cancel();
       
   878     }
       
   879 
       
   880 
       
   881 // ---------------------------------------------------------------------------
       
   882 // CDevCertKeyStoreConduit::RunL()
       
   883 // The token interface has completed the request - munge any return parameters
       
   884 // and write back to client.
       
   885 // ---------------------------------------------------------------------------
       
   886 // 
       
   887 void CDevCertKeyStoreConduit::RunL()
       
   888     {
       
   889     // Handle errors from server
       
   890     User::LeaveIfError(iStatus.Int());
       
   891 
       
   892     switch (iCurrentRequest.OutstandingRequest())
       
   893         {
       
   894         case ECreateKey:
       
   895         case EImportKey:
       
   896         case EImportEncryptedKey:
       
   897             {
       
   898             //  Marshal TKeyUpdate to client - the client's buffer will be large
       
   899             //  enough as it passed us a CCTKeyInfo in the first place
       
   900             ASSERT(iKeyInfo);
       
   901             TDevTokenKeyUpdate update;
       
   902             update.iReference = iKeyInfo->HandleID();
       
   903             update.iId = iKeyInfo->ID();
       
   904             update.iSize = iKeyInfo->Size();
       
   905             update.iAlgorithm = iKeyInfo->Algorithm();
       
   906             TPckg<TDevTokenKeyUpdate> pckg(update);
       
   907             iCurrentRequest.Message().WriteL(1, pckg);
       
   908             break;
       
   909             }
       
   910         case EExportKey:
       
   911             {
       
   912             ASSERT(iExportBuf);
       
   913             TPtr8 clientPtr(iExportBuf->Des());
       
   914             iCurrentRequest.Message().WriteL(1, clientPtr);
       
   915             break;
       
   916             }
       
   917         case ERepudiableDSASign:
       
   918             {
       
   919             ASSERT(iDSASignature);
       
   920 
       
   921             TInt length = User::LeaveIfError(iCurrentRequest.Message().GetDesMaxLength(2));
       
   922             TInt reqdLength = DevTokenDataMarshaller::Size(*iDSASignature);
       
   923             if (length < reqdLength)
       
   924                 {
       
   925                 User::Leave(KErrOverflow);
       
   926                 }
       
   927 
       
   928             HBufC8* clientBuffer = HBufC8::NewLC(reqdLength);
       
   929             TPtr8 ptr(clientBuffer->Des());
       
   930             DevTokenDataMarshaller::WriteL(*iDSASignature, ptr);
       
   931             iCurrentRequest.Message().WriteL(2, ptr);
       
   932             CleanupStack::PopAndDestroy(clientBuffer);
       
   933             break;
       
   934             }
       
   935         case ERepudiableRSASign:
       
   936             {
       
   937             ASSERT(iRSASignature);
       
   938             TInt length = User::LeaveIfError(iCurrentRequest.Message().GetDesMaxLength(2));
       
   939             TInt reqdLength = DevTokenDataMarshaller::Size(*iRSASignature);
       
   940             if (length < reqdLength)
       
   941                 {
       
   942                 User::Leave(KErrOverflow);
       
   943                 }
       
   944 
       
   945             HBufC8* clientBuffer = HBufC8::NewLC(reqdLength);
       
   946             TPtr8 ptr(clientBuffer->Des());
       
   947             DevTokenDataMarshaller::WriteL(*iRSASignature, ptr);
       
   948             iCurrentRequest.Message().WriteL(2, ptr);
       
   949             CleanupStack::PopAndDestroy(clientBuffer);
       
   950             break;
       
   951             }
       
   952         case EDecryptText:
       
   953             {
       
   954             ASSERT(iPlaintext);
       
   955             TInt length = User::LeaveIfError(iCurrentRequest.Message().GetDesMaxLength(2));
       
   956             if (length < iPlaintext->Length())
       
   957                 {
       
   958                 User::Leave(KErrOverflow);
       
   959                 }
       
   960             iCurrentRequest.Message().WriteL(2, *iPlaintext);
       
   961             break;
       
   962             }
       
   963 
       
   964         case EDHPublicKey:
       
   965             FinishDHPublicKeyL();
       
   966             break;
       
   967         case EDHAgree:
       
   968             FinishDHAgreeL();
       
   969             break;
       
   970 
       
   971         default:
       
   972             __ASSERT_DEBUG(EFalse, PanicServer(EPanicInvalidRequest));
       
   973             User::Leave(KErrNotSupported);
       
   974         }
       
   975 
       
   976     Cleanup();
       
   977     iCurrentRequest.Complete(KErrNone);
       
   978     }
       
   979 
       
   980 
       
   981 // ---------------------------------------------------------------------------
       
   982 // CDevCertKeyStoreConduit::Cleanup()
       
   983 // Cleans up data members used in processing a client request.  Called whenenver a
       
   984 // request is completed, either from RunL, RunError or indirectly from DoCancel.
       
   985 // ---------------------------------------------------------------------------
       
   986 // 
       
   987 void CDevCertKeyStoreConduit::Cleanup()
       
   988     {
       
   989     delete iKeyInfo;     iKeyInfo = NULL;
       
   990     delete iImportKey;     iImportKey = NULL;
       
   991     delete iText;        iText = NULL;
       
   992     delete iDSASignature;  iDSASignature = NULL;
       
   993     delete iRSASignature;  iRSASignature = NULL;
       
   994     delete iExportBuf;     iExportBuf = NULL;
       
   995     delete iDHParams;    iDHParams = NULL;
       
   996     iDHPublicKeyOut.Close();
       
   997     delete iDHPublicKey;   iDHPublicKey = NULL;
       
   998     delete iDHAgreedKeyOut;  iDHAgreedKeyOut = NULL;
       
   999     delete iPlaintext;     iPlaintext = NULL;
       
  1000     iOpenedKey = NULL;
       
  1001     }
       
  1002 
       
  1003 
       
  1004 // ---------------------------------------------------------------------------
       
  1005 // CDevCertKeyStoreConduit::RunError()
       
  1006 // ---------------------------------------------------------------------------
       
  1007 // 
       
  1008 TInt CDevCertKeyStoreConduit::RunError(TInt aError)
       
  1009     {
       
  1010     Cleanup();
       
  1011     //  Handle error by completing client appropriately
       
  1012     iCurrentRequest.Complete(aError);
       
  1013     return (KErrNone);
       
  1014     }
       
  1015 
       
  1016 
       
  1017 // CDevCertKeyStoreConduit::TAsyncRequest 
       
  1018 
       
  1019 // ---------------------------------------------------------------------------
       
  1020 // CDevCertKeyStoreConduit::TAsyncRequest::TAsyncRequest()
       
  1021 // ---------------------------------------------------------------------------
       
  1022 // 
       
  1023 CDevCertKeyStoreConduit::TAsyncRequest::TAsyncRequest(TRequestStatus& aStatus) :
       
  1024   iOwnerStatus(aStatus)
       
  1025     {
       
  1026     }
       
  1027 
       
  1028 
       
  1029 // ---------------------------------------------------------------------------
       
  1030 // CDevCertKeyStoreConduit::TAsyncRequest::~TAsyncRequest()
       
  1031 // ---------------------------------------------------------------------------
       
  1032 // 
       
  1033 CDevCertKeyStoreConduit::TAsyncRequest::~TAsyncRequest()
       
  1034     {
       
  1035     __ASSERT_DEBUG(EIdle==iRequest, PanicServer(EPanicRequestOutstanding));
       
  1036     }
       
  1037 
       
  1038 
       
  1039 // ---------------------------------------------------------------------------
       
  1040 // CDevCertKeyStoreConduit::TAsyncRequest::Set()
       
  1041 // ---------------------------------------------------------------------------
       
  1042 // 
       
  1043 void CDevCertKeyStoreConduit::TAsyncRequest::Set(TDevTokenMessages aRequest, const RMessage2& aMessage)
       
  1044     {
       
  1045     __ASSERT_DEBUG(EIdle==iRequest, PanicServer(EPanicRequestOutstanding));
       
  1046 
       
  1047     iOwnerStatus = KRequestPending;
       
  1048     iRequest = aRequest;
       
  1049     iMessage = aMessage;
       
  1050     }
       
  1051 
       
  1052 
       
  1053 // ---------------------------------------------------------------------------
       
  1054 // CDevCertKeyStoreConduit::TAsyncRequest::Complete()
       
  1055 // ---------------------------------------------------------------------------
       
  1056 //
       
  1057 void CDevCertKeyStoreConduit::TAsyncRequest::Complete(TInt aResult)
       
  1058     {
       
  1059     iMessage.Complete(aResult);
       
  1060     iRequest = EIdle;
       
  1061     }
       
  1062 
       
  1063 
       
  1064 // ---------------------------------------------------------------------------
       
  1065 // CDevCertKeyStoreConduit::TAsyncRequest::Cancel()
       
  1066 // ---------------------------------------------------------------------------
       
  1067 //
       
  1068 void CDevCertKeyStoreConduit::TAsyncRequest::Cancel()
       
  1069     {// Complete outstanding request with KErrCancel
       
  1070     iMessage.Complete(KErrCancel);
       
  1071     iRequest = EIdle;
       
  1072     }
       
  1073     
       
  1074 //EOF
       
  1075