javaextensions/satsa/pki/src.s60/cstscredentialmanager.cpp
branchRCL_3
changeset 19 04becd199f91
child 50 023eef975703
equal deleted inserted replaced
16:f5050f1da672 19:04becd199f91
       
     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:
       
    15  *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include "cstscredentialmanager.h"
       
    22 #include "tstsdistinguishednameconverter.h"
       
    23 #include "CCMSIssuerAndSerialNumber.h"
       
    24 #include "cstsseprompt.h"
       
    25 #include "stspkiconstants.h"
       
    26 #include "fs_methodcall.h"
       
    27 #include <asn1dec.h>
       
    28 #include <x509cert.h>
       
    29 #include <unifiedcertstore.h>
       
    30 #include <unifiedkeystore.h>
       
    31 #include <mctwritablecertstore.h>
       
    32 #include <charconv.h>
       
    33 #include <pkcs10.h>
       
    34 #include <secdlg.h>
       
    35 #include <PKIDlg.h>
       
    36 #include <securityerr.h>
       
    37 
       
    38 
       
    39 
       
    40 // CONSTANTS
       
    41 _LIT(KSTSSymbianKeyStoreLabel, "Software key store");
       
    42 _LIT(KSTSSymbianCertificateStoreLabel, "Software certificate store");
       
    43 
       
    44 // ============================ MEMBER FUNCTIONS ===============================
       
    45 
       
    46 // -----------------------------------------------------------------------------
       
    47 // CSTSCredentialManager::NewLC
       
    48 // Two-phased constructor.
       
    49 // -----------------------------------------------------------------------------
       
    50 //
       
    51 CSTSCredentialManager* CSTSCredentialManager::NewLC()
       
    52 {
       
    53     CSTSCredentialManager* self = new(ELeave) CSTSCredentialManager;
       
    54     CallMethod(self,&CSTSCredentialManager::AddToScheduler,self);
       
    55     CleanupStack::PushL(self);
       
    56     CallMethodL(self,&CSTSCredentialManager::ConstructL,self);
       
    57     return self;
       
    58 }
       
    59 
       
    60 void CSTSCredentialManager::AddToScheduler()
       
    61 {
       
    62     CActiveScheduler::Add(this);
       
    63 }
       
    64 
       
    65 // Destructor
       
    66 CSTSCredentialManager::~CSTSCredentialManager()
       
    67 {
       
    68     delete iEncodedCert;
       
    69     delete iIssuerAndSerialNumber;
       
    70     if (iPKIDialog)
       
    71     {
       
    72         iPKIDialog->Release();
       
    73     }
       
    74     delete iDistinguishedName;
       
    75     delete iDisplayName;
       
    76     delete iRequestEncoded;
       
    77     delete iRequest;
       
    78     if (iCertInfoArray)
       
    79     {
       
    80         iCertInfoArray->Close();
       
    81         delete iCertInfoArray;
       
    82     }
       
    83     if (iKeyInfoArray)
       
    84     {
       
    85         iKeyInfoArray->Close();
       
    86         delete iKeyInfoArray;
       
    87     }
       
    88     if (iKeyInfo)
       
    89     {
       
    90         iKeyInfo->Release();
       
    91     }
       
    92     delete iKeyStore;
       
    93     delete iStore;
       
    94     delete iWait;
       
    95     iFileServer.Close();
       
    96 }
       
    97 
       
    98 // -----------------------------------------------------------------------------
       
    99 // CSTSCredentialManager::AddCredentialL
       
   100 // Adds a certificate or certificate URI to certificate store
       
   101 // -----------------------------------------------------------------------------
       
   102 //
       
   103 TBool CSTSCredentialManager::AddCredentialL(
       
   104     TInt/*const TDesC&*/ aCertDisplayName,
       
   105     TInt/*const TDesC&*/ aPkiPath)
       
   106 {
       
   107 
       
   108     if (iState != EReady)
       
   109     {
       
   110         User::Leave(KErrNotReady);
       
   111     }
       
   112 
       
   113     iWritableCertStoreIndex = CheckWriteCertStoreSEIDL(NULL, NULL);
       
   114 
       
   115     const TDesC8* path = reinterpret_cast< const TDesC8* >(aPkiPath);
       
   116 
       
   117     TASN1DecGeneric genericDecoder(*path);
       
   118 
       
   119     genericDecoder.InitL();
       
   120 
       
   121     TPtrC8 certificates = genericDecoder.GetContentDER();
       
   122 
       
   123     TInt certificatesLength = certificates.Length();
       
   124     TInt pos = 0;
       
   125 
       
   126     iSubject.Set(certificates.Right(certificatesLength));
       
   127 
       
   128     TPtrC8 issuer(iSubject);
       
   129 
       
   130     // go through the certificate chain, leaving the last certificate as subject
       
   131     // and second last as issuer
       
   132     while (pos < certificatesLength)
       
   133     {
       
   134         issuer.Set(iSubject);
       
   135         TASN1DecGeneric
       
   136         certDecoder(certificates.Right(certificatesLength - pos));
       
   137         certDecoder.InitL();
       
   138         TInt certLength = certDecoder.LengthDER();
       
   139 
       
   140         TPtrC8 singleCert(certificates.Mid(pos, certLength));
       
   141         iSubject.Set(singleCert);
       
   142         pos+=certLength;
       
   143     }
       
   144 
       
   145     CX509Certificate* cert =
       
   146         CX509Certificate::NewLC(iSubject);
       
   147 
       
   148     CX509Certificate* issuerCert =
       
   149         CX509Certificate::NewLC(issuer);
       
   150 
       
   151     iSubjectKeyId = cert->KeyIdentifierL();
       
   152     iIssuerKeyId = issuerCert->KeyIdentifierL();
       
   153 
       
   154     iCertDisplayName = reinterpret_cast<TDesC*>(aCertDisplayName);
       
   155 
       
   156     // now we have to check that this certificate does not exist in the
       
   157     // database yet
       
   158     CCertAttributeFilter* filter = CCertAttributeFilter::NewLC();
       
   159     filter->SetFormat(EX509Certificate);
       
   160     filter->SetSubjectKeyId(iSubjectKeyId);
       
   161     filter->SetIssuerKeyId(iIssuerKeyId);
       
   162 
       
   163     RMPointerArray< CCTCertInfo >* certInfoArray =
       
   164         new(ELeave) RMPointerArray< CCTCertInfo >();
       
   165     if (iCertInfoArray)
       
   166     {
       
   167         iCertInfoArray->Close();
       
   168         delete iCertInfoArray;
       
   169     }
       
   170     iCertInfoArray = certInfoArray;
       
   171 
       
   172     iState = EAddListing;
       
   173     iStore->WritableCertStore(iWritableCertStoreIndex)
       
   174     .List(*iCertInfoArray, *filter,
       
   175           iStatus);
       
   176     WaitForCompletionL();
       
   177 
       
   178     CleanupStack::PopAndDestroy(3); // filter, cert, issuerCert
       
   179 
       
   180     iCertInfoArray->Close();
       
   181     delete iCertInfoArray;
       
   182     iCertInfoArray = NULL;
       
   183 
       
   184 
       
   185     return ETrue;
       
   186 }
       
   187 
       
   188 void CSTSCredentialManager::StaticAddCredentialL(
       
   189     TInt /*aManager*/,
       
   190     TInt /*aCertDisplayName*/,
       
   191     TInt /*aPkiPath*/,
       
   192     TInt /*aRetVal*/)
       
   193 {
       
   194 
       
   195 }
       
   196 
       
   197 // -----------------------------------------------------------------------------
       
   198 // CSTSCredentialManager::GenerateCSRL
       
   199 // Creates a DER encoded PKCS#10 certificate enrollment request.
       
   200 // -----------------------------------------------------------------------------
       
   201 //
       
   202 TDesC8& CSTSCredentialManager::GenerateCSRL(
       
   203     const TDesC& aNameInfo,
       
   204     const TDesC& aAlgorithm,
       
   205     TInt aKeyLen,
       
   206     TInt aKeyUsage,
       
   207     const TDesC* aSecurityElementID,
       
   208     const TDesC* aSecurityElementPrompt)
       
   209 {
       
   210     TInt storeIndex = CheckKeyStoreSEIDL(aSecurityElementID,
       
   211                                          aSecurityElementPrompt);
       
   212 
       
   213     CKeyInfoBase::EKeyAlgorithm algorithm(CKeyInfoBase::EInvalidAlgorithm);
       
   214     if (aAlgorithm.Compare(KSTSAlgorithmRsa()) == 0)
       
   215     {
       
   216         algorithm = CKeyInfoBase::ERSA;
       
   217     }
       
   218     else
       
   219     {
       
   220         CheckOIDL(aAlgorithm);
       
   221         User::Leave(KSTSErrSeNoKeys);
       
   222     }
       
   223 
       
   224     TSTSDistinguishedNameConverter dnConverter;
       
   225 
       
   226     CX500DistinguishedName* dName = dnConverter.CreateDNL(aNameInfo);
       
   227     delete iDistinguishedName;
       
   228     iDistinguishedName = dName;
       
   229 
       
   230     HBufC* dispName = dName->DisplayNameL();
       
   231     delete iDisplayName;
       
   232     iDisplayName = dispName;
       
   233 
       
   234     TKeyUsagePKCS15 keyUsage(EPKCS15UsageNone);
       
   235     switch (aKeyUsage)
       
   236     {
       
   237     case KSTSKeyUsageAuthentication:
       
   238     {
       
   239         keyUsage = EPKCS15UsageSign;
       
   240         break;
       
   241     }
       
   242     case KSTSKeyUsageNonRepudiation:
       
   243     {
       
   244         keyUsage = EPKCS15UsageNonRepudiation;
       
   245         break;
       
   246     }
       
   247     default:
       
   248     {
       
   249         User::Leave(KErrKeyUsage);
       
   250     }
       
   251     }
       
   252 
       
   253     // have to search for the key
       
   254     TCTKeyAttributeFilter filter;
       
   255     filter.iKeyAlgorithm = algorithm;
       
   256     filter.iUsage = keyUsage;
       
   257     RMPointerArray< CCTKeyInfo >* keyInfoArray =
       
   258         new(ELeave) RMPointerArray< CCTKeyInfo >();
       
   259     if (iKeyInfoArray)
       
   260     {
       
   261         iKeyInfoArray->Close();
       
   262         delete iKeyInfoArray;
       
   263     }
       
   264     iKeyInfoArray = keyInfoArray;
       
   265 
       
   266     iKeyLen = aKeyLen;
       
   267 
       
   268     iState = ESearchingKey;
       
   269     iKeyStore->KeyStore(storeIndex).List(
       
   270         *iKeyInfoArray, filter, iStatus);
       
   271 
       
   272     WaitForCompletionL();
       
   273 
       
   274     return *iRequestEncoded;
       
   275 }
       
   276 
       
   277 void CSTSCredentialManager::StaticGenerateCSRL(
       
   278     TInt aParams,
       
   279     TInt aRetVal)
       
   280 {
       
   281     TGenerateCSRParams* params = reinterpret_cast<TGenerateCSRParams*>(aParams);
       
   282     TDesC8** ret = reinterpret_cast<TDesC8**>(aRetVal);
       
   283 
       
   284     TDesC8& retVal = GenerateCSRL(
       
   285                          *(params->iNameInfo), *(params->iAlgorithm),
       
   286                          params->iKeyLen, params->iKeyUsage,
       
   287                          params->iSecurityElementID,
       
   288                          params->iSecurityElementPrompt);
       
   289     *ret = &retVal;
       
   290 }
       
   291 
       
   292 // -----------------------------------------------------------------------------
       
   293 // CSTSCredentialManager::RemoveCredentialL
       
   294 // Removes a certificate or certificate URI from a certificate store.
       
   295 // -----------------------------------------------------------------------------
       
   296 //
       
   297 TBool CSTSCredentialManager::RemoveCredentialL(
       
   298     const TDesC& aCertDisplayName,
       
   299     const TDesC8& aIssuerAndSerialNumber,
       
   300     const TDesC* aSecurityElementID,
       
   301     const TDesC* aSecurityElementPrompt)
       
   302 
       
   303 {
       
   304     TInt storeIndex = CheckWriteCertStoreSEIDL(aSecurityElementID,
       
   305                       aSecurityElementPrompt);
       
   306 
       
   307     // currently the certificates are stored in the symbian store, always
       
   308     storeIndex = CheckWriteCertStoreSEIDL(NULL, NULL);
       
   309 
       
   310     if (iState != EReady)
       
   311     {
       
   312         User::Leave(KErrNotReady);
       
   313     }
       
   314     // Create filter
       
   315     CCertAttributeFilter* filter = CCertAttributeFilter::NewLC();
       
   316     TCertLabel label(aCertDisplayName);
       
   317     filter->SetLabel(label);
       
   318 
       
   319     RMPointerArray< CCTCertInfo >* certInfoArray =
       
   320         new(ELeave) RMPointerArray< CCTCertInfo >();
       
   321     if (iCertInfoArray)
       
   322     {
       
   323         iCertInfoArray->Close();
       
   324         delete iCertInfoArray;
       
   325     }
       
   326     iCertInfoArray = certInfoArray;
       
   327 
       
   328     // Decode Issuer and SerialNumber
       
   329     CCMSIssuerAndSerialNumber* issuerAndSerialNumber =
       
   330         CCMSIssuerAndSerialNumber::NewL();
       
   331     delete iIssuerAndSerialNumber;
       
   332     iIssuerAndSerialNumber = issuerAndSerialNumber;
       
   333 
       
   334     iIssuerAndSerialNumber->DecodeL(aIssuerAndSerialNumber);
       
   335 
       
   336     iState = ERemoveListing;
       
   337     iStore->WritableCertStore(storeIndex).
       
   338     List(*iCertInfoArray, *filter, iStatus);
       
   339     WaitForCompletionL();
       
   340 
       
   341     CleanupStack::PopAndDestroy();  // filter
       
   342 
       
   343     iCertInfoArray->Close();
       
   344     delete iCertInfoArray;
       
   345     iCertInfoArray = NULL;
       
   346 
       
   347     delete iIssuerAndSerialNumber;
       
   348     iIssuerAndSerialNumber = NULL;
       
   349 
       
   350     delete iEncodedCert;
       
   351     iEncodedCert = NULL;
       
   352 
       
   353     return ETrue;
       
   354 }
       
   355 
       
   356 void CSTSCredentialManager::StaticRemoveCredentialL(
       
   357     TInt aCertDisplayName,
       
   358     TInt aIssuerAndSerialNumber,
       
   359     TInt aSecurityElementID,
       
   360     TInt aSecurityElementPrompt,
       
   361     TInt aRetVal)
       
   362 {
       
   363     *(reinterpret_cast<TBool*>(aRetVal)) = RemoveCredentialL(*(reinterpret_cast<TDesC*>(aCertDisplayName)),
       
   364                                            *(reinterpret_cast<TDesC8*>(aIssuerAndSerialNumber)),
       
   365                                            reinterpret_cast<TDesC*>(aSecurityElementID),
       
   366                                            reinterpret_cast<TDesC*>(aSecurityElementPrompt));
       
   367 
       
   368 }
       
   369 
       
   370 // -----------------------------------------------------------------------------
       
   371 // CSTSCredentialManager::RunL
       
   372 // CActive callback
       
   373 // -----------------------------------------------------------------------------
       
   374 //
       
   375 void CSTSCredentialManager::RunL()
       
   376 {
       
   377     if (iStatus != KErrNone)
       
   378     {
       
   379         // Error has occured; inform java
       
   380         Complete(iStatus.Int());
       
   381     }
       
   382     else
       
   383     {
       
   384         switch (iState)
       
   385         {
       
   386         case EInitialising:
       
   387         {
       
   388             iState = EKeyStoreInit;
       
   389             iKeyStore->Initialize(iStatus);
       
   390             SetActive();
       
   391             break;
       
   392         }
       
   393         case ERemoveListing:
       
   394         {
       
   395             RemoveRetrieveL();
       
   396             break;
       
   397         }
       
   398         case ERemoveRetrieve:
       
   399         {
       
   400             RemoveVerifyL();
       
   401             break;
       
   402         }
       
   403         case ERemoveDialog:
       
   404         {
       
   405             Remove();
       
   406             break;
       
   407         }
       
   408         case EAddListing:
       
   409         {
       
   410             AddRetrieveL();
       
   411             break;
       
   412         }
       
   413         case EAddRetrieve:
       
   414         {
       
   415             AddVerifyL();
       
   416             break;
       
   417         }
       
   418         case EAddDialog:
       
   419         {
       
   420             AddL();
       
   421             break;
       
   422         }
       
   423         case EAdding:
       
   424         case EDeleting:
       
   425         case ECreatingCSR:
       
   426         case EKeyStoreInit:
       
   427         {
       
   428             Complete(KErrNone);
       
   429             break;
       
   430         }
       
   431         case ECSRDialog:
       
   432         {
       
   433             CreateCSRL();
       
   434             break;
       
   435         }
       
   436         case ESearchingKey:
       
   437         {
       
   438             CheckSearchResultsL();
       
   439             break;
       
   440         }
       
   441         default:
       
   442         {
       
   443             Complete(KErrGeneral);
       
   444         }
       
   445         }
       
   446     }
       
   447 }
       
   448 
       
   449 // -----------------------------------------------------------------------------
       
   450 // CSTSCredentialManager::RunError
       
   451 // CActive error callback
       
   452 // -----------------------------------------------------------------------------
       
   453 //
       
   454 TInt CSTSCredentialManager::RunError(TInt aError)
       
   455 {
       
   456     Complete(aError);
       
   457     return KErrNone;
       
   458 }
       
   459 
       
   460 // -----------------------------------------------------------------------------
       
   461 // CSTSCredentialManager::DoCancel
       
   462 // CActive cancellation callback - cancel whatever doing
       
   463 // -----------------------------------------------------------------------------
       
   464 //
       
   465 void CSTSCredentialManager::DoCancel()
       
   466 {
       
   467     switch (iState)
       
   468     {
       
   469     case EInitialising:
       
   470     {
       
   471         iStore->CancelInitialize();
       
   472         break;
       
   473     }
       
   474     case EKeyStoreInit:
       
   475     {
       
   476         iKeyStore->CancelInitialize();
       
   477         break;
       
   478     }
       
   479     case EAdding:
       
   480     {
       
   481         MCTWritableCertStore& writableStore =
       
   482             iStore->WritableCertStore(iWritableCertStoreIndex);
       
   483         writableStore.CancelAdd();
       
   484         break;
       
   485     }
       
   486     case EAddListing:
       
   487     case ERemoveListing:
       
   488     {
       
   489         iStore->CancelList();
       
   490         break;
       
   491     }
       
   492     case EAddRetrieve:
       
   493     case ERemoveRetrieve:
       
   494     {
       
   495         iStore->CancelRetrieve();
       
   496         break;
       
   497     }
       
   498     case EDeleting:
       
   499     {
       
   500         iStore->CancelRemove();
       
   501         break;
       
   502     }
       
   503     case EAddDialog: // AddDialog has nothing to cancel
       
   504     case ERemoveDialog: // RemoveDialog has nothing to cancel
       
   505     default:
       
   506     {
       
   507         // nothing to cancel
       
   508     }
       
   509     }
       
   510     iState = EError;
       
   511 }
       
   512 
       
   513 
       
   514 
       
   515 // -----------------------------------------------------------------------------
       
   516 // CSTSCredentialManager::CSTSCredentialManager
       
   517 // C++ default constructor can NOT contain any code, that
       
   518 // might leave.
       
   519 // -----------------------------------------------------------------------------
       
   520 //
       
   521 CSTSCredentialManager::CSTSCredentialManager()
       
   522         : CActive(EPriorityStandard),java::util::FunctionServer("MyPKIServer"),
       
   523         iState(EInitialising),
       
   524         iEncodedCertDes(NULL, 0)
       
   525 {
       
   526     createServerToNewThread();
       
   527 }
       
   528 
       
   529 // -----------------------------------------------------------------------------
       
   530 // CSTSCredentialManager::ConstructL
       
   531 // Symbian 2nd phase constructor can leave.
       
   532 // -----------------------------------------------------------------------------
       
   533 //
       
   534 void CSTSCredentialManager::ConstructL()
       
   535 {
       
   536     iPKIDialog = PKIDialogFactory::CreateNoteL();
       
   537 
       
   538     User::LeaveIfError(iFileServer.Connect());
       
   539     iWait = new(ELeave) CActiveSchedulerWait;
       
   540     iStore = CUnifiedCertStore::NewL(iFileServer, ETrue);
       
   541     iKeyStore = CUnifiedKeyStore::NewL(iFileServer);
       
   542     iStore->Initialize(iStatus);
       
   543     WaitForCompletionL();
       
   544 
       
   545 }
       
   546 
       
   547 // -----------------------------------------------------------------------------
       
   548 // CSTSCredentialManager::Complete
       
   549 // Completes the operation
       
   550 // -----------------------------------------------------------------------------
       
   551 //
       
   552 void CSTSCredentialManager::Complete(TInt aError)
       
   553 {
       
   554     if (KErrNone == aError)
       
   555     {
       
   556         iState = EReady;
       
   557     }
       
   558     else
       
   559     {
       
   560         iState = EError;
       
   561         iError = aError;
       
   562     }
       
   563     iWait->AsyncStop();
       
   564 
       
   565 }
       
   566 
       
   567 // -----------------------------------------------------------------------------
       
   568 // CSTSCredentialManager::WaitForCompletionL()
       
   569 // Wait for completion, leave on error
       
   570 // -----------------------------------------------------------------------------
       
   571 //
       
   572 void CSTSCredentialManager::WaitForCompletionL()
       
   573 {
       
   574     SetActive();
       
   575     iWait->Start();
       
   576     if (iState != EReady)
       
   577     {
       
   578         // we need to be ready for the next request
       
   579         iState = EReady;
       
   580         User::Leave(iError);
       
   581     }
       
   582 }
       
   583 
       
   584 // -----------------------------------------------------------------------------
       
   585 // CSTSCredentialManager::RemoveRetrieveL
       
   586 // Retrieves the certificate to be removed
       
   587 // -----------------------------------------------------------------------------
       
   588 //
       
   589 void CSTSCredentialManager::RemoveRetrieveL()
       
   590 {
       
   591     if (iCertInfoArray->Count() == 0)
       
   592     {
       
   593         Complete(KErrNotFound);
       
   594         return;
       
   595     }
       
   596 
       
   597     CCTCertInfo* certInfo = (*iCertInfoArray)[ 0 ];
       
   598 
       
   599     HBufC8* encodedCert = HBufC8::NewMaxL(certInfo->Size());
       
   600     delete iEncodedCert;
       
   601     iEncodedCert = encodedCert;
       
   602 
       
   603     iEncodedCertDes.Set(iEncodedCert->Des());
       
   604 
       
   605     iState = ERemoveRetrieve;
       
   606     iStore->Retrieve(*certInfo, iEncodedCertDes, iStatus);
       
   607     SetActive();
       
   608 }
       
   609 
       
   610 // -----------------------------------------------------------------------------
       
   611 // CSTSCredentialManager::AddRetrieveL
       
   612 // Retrieves possible conflicting certificate
       
   613 // -----------------------------------------------------------------------------
       
   614 //
       
   615 void CSTSCredentialManager::AddRetrieveL()
       
   616 {
       
   617 
       
   618     if (iCertInfoArray->Count() == 0)
       
   619     {
       
   620         // not found, ok to add
       
   621         iState = EAddDialog;
       
   622         iPKIDialog->SaveCertificate(EX509Certificate, EUserCertificate,
       
   623                                     iSubject, iStatus);
       
   624         SetActive();
       
   625         return;
       
   626     }
       
   627 
       
   628 
       
   629     CCTCertInfo* certInfo = (*iCertInfoArray)[ 0 ];
       
   630 
       
   631     HBufC8* encodedCert = HBufC8::NewMaxL(certInfo->Size());
       
   632     delete iEncodedCert;
       
   633     iEncodedCert = encodedCert;
       
   634 
       
   635     iEncodedCertDes.Set(iEncodedCert->Des());
       
   636 
       
   637     iState = EAddRetrieve;
       
   638     iStore->Retrieve(*certInfo, iEncodedCertDes, iStatus);
       
   639     SetActive();
       
   640 
       
   641 }
       
   642 
       
   643 // -----------------------------------------------------------------------------
       
   644 // CSTSCredentialManager::RemoveVerify
       
   645 // Verifies that the certificate to be removed has correct issuer and serial
       
   646 // -----------------------------------------------------------------------------
       
   647 //
       
   648 void CSTSCredentialManager::RemoveVerifyL()
       
   649 {
       
   650 
       
   651     TPtr8 certPtr(iEncodedCert->Des());
       
   652     CX509Certificate* cert = CX509Certificate::NewLC(certPtr);
       
   653 
       
   654     // Check issuer
       
   655     const CX500DistinguishedName& certIssuer = cert->IssuerName();
       
   656     if (!certIssuer.ExactMatchL(iIssuerAndSerialNumber->IssuerName()))
       
   657     {
       
   658         // remove current certificate from array, fetch next
       
   659         (*iCertInfoArray)[ 0 ]->Release();
       
   660         iCertInfoArray->Remove(0);
       
   661         RemoveRetrieveL();
       
   662     }
       
   663     // check serialNumber
       
   664     else if (cert->SerialNumber() != iIssuerAndSerialNumber->SerialNumber())
       
   665     {
       
   666         // remove current certificate from array, fetch next
       
   667         (*iCertInfoArray)[ 0 ]->Release();
       
   668         iCertInfoArray->Remove(0);
       
   669         RemoveRetrieveL();
       
   670     }
       
   671     else
       
   672     {
       
   673         // We have a match, show dialog
       
   674         RemoveDialog();
       
   675     }
       
   676     CleanupStack::PopAndDestroy(cert);
       
   677 
       
   678 }
       
   679 
       
   680 // -----------------------------------------------------------------------------
       
   681 // CSTSCredentialManager::AddVerify
       
   682 // Verifies that the certificate does not have correct serial
       
   683 // -----------------------------------------------------------------------------
       
   684 //
       
   685 void CSTSCredentialManager::AddVerifyL()
       
   686 {
       
   687 
       
   688     TPtr8 certPtr(iEncodedCert->Des());
       
   689     CX509Certificate* cert = CX509Certificate::NewLC(certPtr);
       
   690 
       
   691     CX509Certificate* certToAdd = CX509Certificate::NewLC(iSubject);
       
   692 
       
   693     // check serialNumber
       
   694     if (cert->SerialNumber() != certToAdd->SerialNumber())
       
   695     {
       
   696         // remove current certificate from array, fetch next
       
   697         (*iCertInfoArray)[ 0 ]->Release();
       
   698         iCertInfoArray->Remove(0);
       
   699         AddRetrieveL();
       
   700     }
       
   701     else
       
   702     {
       
   703         // We have a match, bail out
       
   704         Complete(KSTSErrAlreadyRegistered);
       
   705     }
       
   706     CleanupStack::PopAndDestroy(2);   // certToAdd, cert
       
   707 
       
   708 }
       
   709 
       
   710 
       
   711 // -----------------------------------------------------------------------------
       
   712 // CSTSCredentialManager::RemoveDialog
       
   713 // Displays a dialog asing user permission to remove a certificate
       
   714 // -----------------------------------------------------------------------------
       
   715 //
       
   716 void CSTSCredentialManager::RemoveDialog()
       
   717 {
       
   718 
       
   719 
       
   720     CCTCertInfo* certInfo = (*iCertInfoArray)[ 0 ];
       
   721     iState = ERemoveDialog;
       
   722     iHandle = certInfo->Handle();
       
   723     iPKIDialog->DeleteCertificate(iHandle, iStatus);
       
   724     SetActive();
       
   725 }
       
   726 
       
   727 
       
   728 
       
   729 // -----------------------------------------------------------------------------
       
   730 // CSTSCredentialManager::Remove
       
   731 // Remove the found certificate
       
   732 // -----------------------------------------------------------------------------
       
   733 //
       
   734 void CSTSCredentialManager::Remove()
       
   735 {
       
   736     if (iCertInfoArray->Count() == 0)
       
   737     {
       
   738         Complete(KErrNotFound);
       
   739         return;
       
   740     }
       
   741 
       
   742     CCTCertInfo* certInfo = (*iCertInfoArray)[ 0 ];
       
   743     iState = EDeleting;
       
   744     iStore->Remove(*certInfo, iStatus);
       
   745     SetActive();
       
   746 }
       
   747 
       
   748 
       
   749 
       
   750 // -----------------------------------------------------------------------------
       
   751 // CSTSCredentialManager::AddL
       
   752 // Adds a certificate
       
   753 // -----------------------------------------------------------------------------
       
   754 //
       
   755 void CSTSCredentialManager::AddL()
       
   756 {
       
   757     MCTWritableCertStore& writableStore =
       
   758         iStore->WritableCertStore(iWritableCertStoreIndex);
       
   759 
       
   760     iState = EAdding;
       
   761     writableStore.Add(
       
   762         *iCertDisplayName,
       
   763         EX509Certificate,
       
   764         EUserCertificate,
       
   765         &iSubjectKeyId,
       
   766         &iIssuerKeyId,
       
   767         iSubject,
       
   768         iStatus);
       
   769     SetActive();
       
   770 }
       
   771 
       
   772 // -----------------------------------------------------------------------------
       
   773 // CSTSCredentialManager::CSRDialog
       
   774 // Displays a CSR creation dialog
       
   775 // -----------------------------------------------------------------------------
       
   776 void CSTSCredentialManager::CSRDialog()
       
   777 {
       
   778     if (!iDisplayName || !iKeyInfo)
       
   779     {
       
   780         Complete(KErrGeneral);
       
   781         return;
       
   782     }
       
   783     iState = ECSRDialog;
       
   784     iHandle = iKeyInfo->Handle();
       
   785 
       
   786     iPKIDialog->CreateCSR(*iDisplayName, iHandle, iStatus);
       
   787     SetActive();
       
   788 
       
   789 }
       
   790 
       
   791 // -----------------------------------------------------------------------------
       
   792 // CSTSCredentialManager::CreateCSRL
       
   793 // Creates a CSR
       
   794 // -----------------------------------------------------------------------------
       
   795 void CSTSCredentialManager::CreateCSRL()
       
   796 {
       
   797 
       
   798     if ((!iKeyInfo) || (!iDistinguishedName))
       
   799     {
       
   800         Complete(KErrGeneral);
       
   801         return;
       
   802     }
       
   803 
       
   804     iRequest = CPKCS10Request::NewL(*iDistinguishedName, *iKeyInfo);
       
   805 
       
   806     delete iRequestEncoded;
       
   807     iRequestEncoded = NULL;
       
   808 
       
   809     iState = ECreatingCSR;
       
   810     iRequest->CreateEncoding(iRequestEncoded, iStatus);
       
   811     SetActive();
       
   812 }
       
   813 
       
   814 
       
   815 // -----------------------------------------------------------------------------
       
   816 // CSTSCredentialManager::CheckSearchResultsL
       
   817 // Check key search results, generate CSR
       
   818 // -----------------------------------------------------------------------------
       
   819 void CSTSCredentialManager::CheckSearchResultsL()
       
   820 {
       
   821     // we take the first key with valid dates
       
   822     TInt keyCount = iKeyInfoArray->Count();
       
   823     CCTKeyInfo* keyInfo = NULL;
       
   824     TTime timeNow;
       
   825     timeNow.UniversalTime();
       
   826 
       
   827     TBool found = EFalse;
       
   828     TInt keyIndex = 0;
       
   829 
       
   830     for (TInt i = 0; (i < keyCount) && (!found); i++)
       
   831     {
       
   832         keyInfo = iKeyInfoArray->operator[](i);
       
   833         TTime startDate = keyInfo->StartDate();
       
   834         if ((startDate.Int64() == 0) || (timeNow >= startDate))
       
   835         {
       
   836             // startDate was not set or is before current time
       
   837             // exactly same time is valid as well
       
   838             TTime endDate = keyInfo->EndDate();
       
   839             if ((endDate.Int64() == 0) || (timeNow < endDate))
       
   840             {
       
   841                 // endDate was not set or is after current time
       
   842                 // exactly same time is not valid
       
   843                 // (the key expires immediatelly)
       
   844                 if (keyInfo->Size() == iKeyLen)
       
   845                 {
       
   846                     // key must have same size
       
   847                     found = ETrue;
       
   848                     keyIndex = i;
       
   849                 }
       
   850             }
       
   851         }
       
   852     }
       
   853     if (!found)
       
   854     {
       
   855         Complete(KSTSErrSeNoKeys);
       
   856         return;
       
   857     }
       
   858 
       
   859     if (iKeyInfo)
       
   860     {
       
   861         iKeyInfo->Release();
       
   862     }
       
   863     iKeyInfo = keyInfo;
       
   864     // iKeyInfo is no longer owned by iKeyInfoArray
       
   865     iKeyInfoArray->Remove(keyIndex);
       
   866 
       
   867     CSRDialog();
       
   868 }
       
   869 
       
   870 
       
   871 // -----------------------------------------------------------------------------
       
   872 // CSTSCredentialManager::CheckKeyStoreSEIDL
       
   873 // Check if given SecurityElementID is available, prompts user if needed
       
   874 // -----------------------------------------------------------------------------
       
   875 TInt CSTSCredentialManager::CheckKeyStoreSEIDL(
       
   876     const TDesC* aSecurityElementID,
       
   877     const TDesC* aSecurityElementPrompt)
       
   878 {
       
   879 
       
   880     // Symbian store can't be used
       
   881     if (aSecurityElementID &&
       
   882             (KSTSSymbianKeyStoreLabel() == *aSecurityElementID))
       
   883     {
       
   884         User::Leave(KSTSErrSeNotFound);
       
   885     }
       
   886 
       
   887 
       
   888     TInt retVal = 0;
       
   889     TInt keyStoreCount = iKeyStore->KeyStoreCount();
       
   890     TBool found = EFalse;
       
   891     TInt i = 0;
       
   892     for (i = 0; (i < keyStoreCount) && (!found); i++)
       
   893     {
       
   894         // if aSecurityElementID is NULL, use the first one, otherwise first
       
   895         // with the same label
       
   896         const TDesC& storeLabel = iKeyStore->KeyStore(i).Token().Label();
       
   897         if (!aSecurityElementID ||
       
   898                 (*aSecurityElementID == KNullDesC()) ||
       
   899                 storeLabel == *aSecurityElementID)
       
   900         {
       
   901             if (storeLabel != KSTSSymbianKeyStoreLabel())
       
   902             {
       
   903                 found = ETrue;
       
   904                 retVal = i;
       
   905             }
       
   906         }
       
   907     }
       
   908 
       
   909     // if not found, display prompt( if not NULL ) and search again
       
   910     if (!found && aSecurityElementPrompt)
       
   911     {
       
   912         CSTSSEPrompt* prompt = CSTSSEPrompt::NewLC();
       
   913         prompt->DisplayPromptL(*aSecurityElementPrompt);
       
   914         keyStoreCount = iKeyStore->KeyStoreCount();
       
   915         for (i = 0; (i < keyStoreCount) && (!found); i++)
       
   916         {
       
   917             if (iKeyStore->KeyStore(i).Token().Label() ==
       
   918                     *aSecurityElementID)
       
   919             {
       
   920                 found = ETrue;
       
   921                 retVal = i;
       
   922             }
       
   923         }
       
   924     }
       
   925 
       
   926     if (!found)
       
   927     {
       
   928         User::Leave(KSTSErrSeNotFound);
       
   929     }
       
   930 
       
   931     return retVal;
       
   932 }
       
   933 
       
   934 // -----------------------------------------------------------------------------
       
   935 // CSTSCredentialManager::CheckWriteCertStoreSEIDL
       
   936 // Check if given SecurityElementID is available, prompts user if needed
       
   937 // -----------------------------------------------------------------------------
       
   938 TInt CSTSCredentialManager::CheckWriteCertStoreSEIDL(
       
   939     const TDesC* aSecurityElementID,
       
   940     const TDesC* aSecurityElementPrompt)
       
   941 {
       
   942     // If aSecurityElementID is NULL, use the default Symbian cert store.
       
   943     TPtrC securityElementID(KSTSSymbianCertificateStoreLabel());
       
   944     if (aSecurityElementID &&
       
   945             *aSecurityElementID != KNullDesC())
       
   946     {
       
   947         securityElementID.Set(*aSecurityElementID);
       
   948     }
       
   949 
       
   950     TInt retVal = 0;
       
   951     TInt certStoreCount = iStore->WritableCertStoreCount();
       
   952     TBool found = EFalse;
       
   953     TInt i = 0;
       
   954     for (i = 0; (i < certStoreCount) && (!found); i++)
       
   955     {
       
   956         // Select the first store with the specified label.
       
   957         const TDesC& storeLabel =
       
   958             iStore->WritableCertStore(i).Token().Label();
       
   959 
       
   960         if (storeLabel == securityElementID)
       
   961         {
       
   962             found = ETrue;
       
   963             retVal = i;
       
   964         }
       
   965     }
       
   966 
       
   967     // if not found, display prompt( if not NULL ) and search again
       
   968     if (!found && aSecurityElementPrompt)
       
   969     {
       
   970         CSTSSEPrompt* prompt = CSTSSEPrompt::NewLC();
       
   971         prompt->DisplayPromptL(*aSecurityElementPrompt);
       
   972         CleanupStack::PopAndDestroy(prompt);
       
   973         certStoreCount = iStore->CertStoreCount();
       
   974         for (i = 0; (i < certStoreCount) && (!found); i++)
       
   975         {
       
   976             if (iStore->CertStore(i).Token().Label() ==
       
   977                     securityElementID)
       
   978             {
       
   979                 found = ETrue;
       
   980                 retVal = i;
       
   981             }
       
   982         }
       
   983     }
       
   984 
       
   985     if (!found)
       
   986     {
       
   987         User::Leave(KSTSErrSeNotFound);
       
   988     }
       
   989 
       
   990     return retVal;
       
   991 }
       
   992 
       
   993 // -----------------------------------------------------------------------------
       
   994 // CSTSCredentialManager::CheckOIDL
       
   995 // Checks if given OID is formatted correctly
       
   996 // -----------------------------------------------------------------------------
       
   997 void CSTSCredentialManager::CheckOIDL(const TDesC& aOID)
       
   998 {
       
   999     TInt oidLength = aOID.Length();
       
  1000     if (oidLength == 0)
       
  1001     {
       
  1002         // empty oid is illegal
       
  1003         User::Leave(KErrArgument);
       
  1004     }
       
  1005     for (TInt i = 0; i < oidLength; i++)
       
  1006     {
       
  1007         TChar oidChar = aOID[ i ];
       
  1008         if (!oidChar.IsAlphaDigit())
       
  1009         {
       
  1010             if ((oidChar != KSTSDot) && (oidChar != KSTSLine))
       
  1011             {
       
  1012                 User::Leave(KErrArgument);
       
  1013             }
       
  1014         }
       
  1015     }
       
  1016 }
       
  1017 
       
  1018 //  End of File
       
  1019 void CSTSCredentialManager::vmAttached()
       
  1020 {
       
  1021 
       
  1022 }
       
  1023 
       
  1024 void CSTSCredentialManager::doServerSideInit()
       
  1025 {
       
  1026 
       
  1027     FunctionServer::doServerSideInit();
       
  1028 
       
  1029 }
       
  1030 
       
  1031