cpsecplugins/cpadvancedsecplugin/src/advsecsettingscertlistbuilder_symbian.cpp
changeset 63 989397f9511c
equal deleted inserted replaced
62:3255e7d5bd67 63:989397f9511c
       
     1 /*
       
     2 * Copyright (c) 2010 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:  Helper class to construct certificate list
       
    15 *
       
    16 */
       
    17 
       
    18 #include "advsecsettingscertlistbuilder_symbian.h"
       
    19 #include "advsecsettingslabeledcertinfo_symbian.h"
       
    20 #include "advsecsettingsstoreuids.h"    // KAdvSecSettingsDeviceCertStore
       
    21 #include <x509cert.h>                   // CX509Certificate
       
    22 #include <ccertattributefilter.h>       // CCertAttributeFilter
       
    23 #include <X509CertNameParser.h>         // X509CertNameParser::PrimaryAndSecondaryNameL
       
    24 #include <stl/_auto_ptr.h>              // std::auto_ptr
       
    25 
       
    26 _LIT(KNameSeparator, " ");
       
    27 
       
    28 // implemented in advsecsettingssecuritymodulemodel_symbian_p.cpp
       
    29 QString CopyStringL(const TDesC16 &aDes16);
       
    30 
       
    31 
       
    32 // ======== MEMBER FUNCTIONS ========
       
    33 
       
    34 // ---------------------------------------------------------------------------
       
    35 // CAdvSecSettingsCertListBuilder::NewL()
       
    36 // ---------------------------------------------------------------------------
       
    37 //
       
    38 CAdvSecSettingsCertListBuilder *CAdvSecSettingsCertListBuilder::NewL(
       
    39     RFs &aFs, CUnifiedCertStore &aCertStore)
       
    40 {
       
    41     CAdvSecSettingsCertListBuilder *self = new(ELeave) CAdvSecSettingsCertListBuilder(
       
    42         aFs, aCertStore);
       
    43     CleanupStack::PushL(self);
       
    44     self->ConstructL();
       
    45     CleanupStack::Pop(self);
       
    46     return self;
       
    47 }
       
    48 
       
    49 // ---------------------------------------------------------------------------
       
    50 // CAdvSecSettingsCertListBuilder::~CAdvSecSettingsCertListBuilder()
       
    51 // ---------------------------------------------------------------------------
       
    52 //
       
    53 CAdvSecSettingsCertListBuilder::~CAdvSecSettingsCertListBuilder()
       
    54 {
       
    55     Cancel();
       
    56     iLabeledCertInfos.ResetAndDestroy();
       
    57     delete iCertificate;
       
    58     delete iCertAttributeFilter;
       
    59     iCertInfoArray.Close();
       
    60     iCertList = NULL;
       
    61     iClientInfoArray = NULL;
       
    62     iClientStatus = NULL;
       
    63 }
       
    64 
       
    65 // ---------------------------------------------------------------------------
       
    66 // CAdvSecSettingsCertListBuilder::GetCertificateList()
       
    67 // ---------------------------------------------------------------------------
       
    68 //
       
    69 void CAdvSecSettingsCertListBuilder::GetCertificateList(
       
    70     QList<AdvSecSettingsCertificate*> &certList,
       
    71     RMPointerArray<CCTCertInfo> &aCertInfoArray,
       
    72     TRequestStatus &aStatus)
       
    73 {
       
    74     aStatus = KRequestPending;
       
    75     if (!IsActive() && (iState == EIdle)) {
       
    76         iClientStatus = &aStatus;
       
    77         iCertList = &certList;
       
    78         iClientInfoArray = &aCertInfoArray;
       
    79 
       
    80         iCertInfoArray.Close();
       
    81         ResetCertAttributeFilter();
       
    82         iCertStore.List(iCertInfoArray, *iCertAttributeFilter, iStatus);
       
    83         iState = EListingCerts;
       
    84         SetActive();
       
    85    } else {
       
    86         TRequestStatus *status = &aStatus;
       
    87         User::RequestComplete(status, KErrInUse);
       
    88     }
       
    89 }
       
    90 
       
    91 // ---------------------------------------------------------------------------
       
    92 // CAdvSecSettingsCertListBuilder::DoCancel()
       
    93 // ---------------------------------------------------------------------------
       
    94 //
       
    95 void CAdvSecSettingsCertListBuilder::DoCancel()
       
    96 {
       
    97     switch (iState) {
       
    98     case EListingCerts:
       
    99         iCertStore.CancelList();
       
   100         break;
       
   101     case ERetrievingCert:
       
   102         iCertStore.CancelRetrieve();
       
   103         break;
       
   104     case EProcessingCert:
       
   105         // nothing to do
       
   106         break;
       
   107     default:
       
   108         break;
       
   109     }
       
   110     User::RequestComplete(iClientStatus, KErrCancel);
       
   111     iClientStatus = NULL;
       
   112     iState = EIdle;
       
   113 }
       
   114 
       
   115 // ---------------------------------------------------------------------------
       
   116 // CAdvSecSettingsCertListBuilder::RunL()
       
   117 // ---------------------------------------------------------------------------
       
   118 //
       
   119 void CAdvSecSettingsCertListBuilder::RunL()
       
   120 {
       
   121     User::LeaveIfError(iStatus.Int());
       
   122     switch (iState) {
       
   123     case EListingCerts:
       
   124         ProcessFirstCertificateL();
       
   125         break;
       
   126     case ERetrievingCert:
       
   127         ProcessNextCertificateL();
       
   128         break;
       
   129     case EProcessingCert:
       
   130         ProcessNextCertificateL();
       
   131         break;
       
   132     default:
       
   133         ASSERT(EFalse);
       
   134         break;
       
   135     }
       
   136 }
       
   137 
       
   138 // ---------------------------------------------------------------------------
       
   139 // CAdvSecSettingsCertListBuilder::RunError()
       
   140 // ---------------------------------------------------------------------------
       
   141 //
       
   142 TInt CAdvSecSettingsCertListBuilder::RunError(TInt aError)
       
   143 {
       
   144     User::RequestComplete(iClientStatus, aError);
       
   145     iClientStatus = NULL;
       
   146     iState = EIdle;
       
   147     return KErrNone;
       
   148 }
       
   149 
       
   150 // ---------------------------------------------------------------------------
       
   151 // CAdvSecSettingsCertListBuilder::CAdvSecSettingsCertListBuilder()
       
   152 // ---------------------------------------------------------------------------
       
   153 //
       
   154 CAdvSecSettingsCertListBuilder::CAdvSecSettingsCertListBuilder(RFs &aFs,
       
   155     CUnifiedCertStore &aCertStore) : CActive(CActive::EPriorityLow),
       
   156     iFs(aFs), iCertStore(aCertStore), iCertPtr(0,0)
       
   157 {
       
   158     CActiveScheduler::Add(this);
       
   159 }
       
   160 
       
   161 // ---------------------------------------------------------------------------
       
   162 // CAdvSecSettingsCertListBuilder::ConstructL()
       
   163 // ---------------------------------------------------------------------------
       
   164 //
       
   165 void CAdvSecSettingsCertListBuilder::ConstructL()
       
   166 {
       
   167     iCertAttributeFilter = CCertAttributeFilter::NewL();
       
   168 }
       
   169 
       
   170 // ---------------------------------------------------------------------------
       
   171 // CAdvSecSettingsCertListBuilder::ResetCertAttributeFilter()
       
   172 // ---------------------------------------------------------------------------
       
   173 //
       
   174 void CAdvSecSettingsCertListBuilder::ResetCertAttributeFilter()
       
   175 {
       
   176     ASSERT(iCertAttributeFilter);
       
   177     iCertAttributeFilter->iLabelIsSet = EFalse;
       
   178     iCertAttributeFilter->iUidIsSet = EFalse;
       
   179     iCertAttributeFilter->iFormatIsSet = EFalse;
       
   180     iCertAttributeFilter->iKeyUsage = EX509UsageAll;
       
   181     iCertAttributeFilter->iOwnerTypeIsSet = EFalse;
       
   182     iCertAttributeFilter->iSubjectKeyIdIsSet = EFalse;
       
   183     iCertAttributeFilter->iIssuerKeyIdIsSet = EFalse;
       
   184 }
       
   185 
       
   186 // ---------------------------------------------------------------------------
       
   187 // CAdvSecSettingsCertListBuilder::ProcessFirstCertificateL()
       
   188 // ---------------------------------------------------------------------------
       
   189 //
       
   190 void CAdvSecSettingsCertListBuilder::ProcessFirstCertificateL()
       
   191 {
       
   192     iCertInfoIndex = 0;
       
   193     StartProcessingCertificateL();
       
   194 }
       
   195 
       
   196 // ---------------------------------------------------------------------------
       
   197 // CAdvSecSettingsCertListBuilder::ProcessNextCertificateL()
       
   198 // ---------------------------------------------------------------------------
       
   199 //
       
   200 void CAdvSecSettingsCertListBuilder::ProcessNextCertificateL()
       
   201 {
       
   202     CompleteProcessingCertificateL();
       
   203     ++iCertInfoIndex;
       
   204     StartProcessingCertificateL();
       
   205 }
       
   206 
       
   207 // ---------------------------------------------------------------------------
       
   208 // CAdvSecSettingsCertListBuilder::StartProcessingCertificateL()
       
   209 // ---------------------------------------------------------------------------
       
   210 //
       
   211 void CAdvSecSettingsCertListBuilder::StartProcessingCertificateL()
       
   212 {
       
   213     if (iCertInfoIndex < iCertInfoArray.Count()) {
       
   214         CCTCertInfo *certInfo = iCertInfoArray[iCertInfoIndex];
       
   215 
       
   216         if ((certInfo->CertificateOwnerType() == ECACertificate) &&
       
   217             (certInfo->CertificateFormat() == EX509Certificate)) {
       
   218             // Have to retrieve the cert to add parts of subject
       
   219             // to certificate label. For example, all Java certs
       
   220             // have the same label by default.
       
   221             if (iCertificate) {
       
   222                 delete iCertificate;
       
   223                 iCertificate = NULL;
       
   224             }
       
   225             iCertificate = HBufC8::NewL(certInfo->Size());
       
   226             iCertPtr.Set(iCertificate->Des());
       
   227             iCertStore.Retrieve(*certInfo, iCertPtr, iStatus);
       
   228             iState = ERetrievingCert;
       
   229             SetActive();
       
   230         } else {
       
   231             // Certificate label can be used as such.
       
   232             TRequestStatus *status = &iStatus;
       
   233             User::RequestComplete(status, KErrNone);
       
   234             iState = EProcessingCert;
       
   235             SetActive();
       
   236         }
       
   237 
       
   238     } else {
       
   239         ReturnCertificateListL();
       
   240 
       
   241         User::RequestComplete(iClientStatus, KErrNone);
       
   242         iClientStatus = NULL;
       
   243         iState = EIdle;
       
   244     }
       
   245 }
       
   246 
       
   247 // ---------------------------------------------------------------------------
       
   248 // CAdvSecSettingsCertListBuilder::CompleteProcessingCertificateL()
       
   249 // ---------------------------------------------------------------------------
       
   250 //
       
   251 void CAdvSecSettingsCertListBuilder::CompleteProcessingCertificateL()
       
   252 {
       
   253     ASSERT(iCertInfoIndex < iCertInfoArray.Count());
       
   254     CCTCertInfo *certInfo = iCertInfoArray[iCertInfoIndex];
       
   255 
       
   256     CAdvSecSettingsLabeledCertInfo *labeledCertInfo =
       
   257         new(ELeave) CAdvSecSettingsLabeledCertInfo(*certInfo);
       
   258     CleanupStack::PushL(labeledCertInfo);
       
   259     labeledCertInfo->SetLabelL(certInfo->Label());
       
   260 
       
   261     if (iCertificate) {
       
   262         CX509Certificate *cert = CX509Certificate::NewL(*iCertificate);
       
   263         CleanupStack::PushL(cert);
       
   264 
       
   265         HBufC *primaryName = NULL;
       
   266         HBufC *secondaryName = NULL;
       
   267         TInt err = X509CertNameParser::PrimaryAndSecondaryNameL(*cert,
       
   268             primaryName, secondaryName, certInfo->Label());
       
   269         if (!err) {
       
   270             if (primaryName && primaryName->Length()) {
       
   271                 labeledCertInfo->AppendLabelL(KNameSeparator, *primaryName);
       
   272             } else {
       
   273                 if (secondaryName && secondaryName->Length()) {
       
   274                     labeledCertInfo->AppendLabelL(KNameSeparator, *secondaryName);
       
   275                 }
       
   276             }
       
   277         }
       
   278 
       
   279         CleanupStack::PopAndDestroy(cert);
       
   280         delete iCertificate;
       
   281         iCertificate = NULL;
       
   282     }
       
   283 
       
   284     iLabeledCertInfos.AppendL(labeledCertInfo);
       
   285     CleanupStack::Pop(labeledCertInfo);
       
   286 }
       
   287 
       
   288 // ---------------------------------------------------------------------------
       
   289 // CAdvSecSettingsCertListBuilder::ReturnCertificateListL()
       
   290 // ---------------------------------------------------------------------------
       
   291 //
       
   292 void CAdvSecSettingsCertListBuilder::ReturnCertificateListL()
       
   293 {
       
   294     TLinearOrder<CAdvSecSettingsLabeledCertInfo> order( CAdvSecSettingsLabeledCertInfo::Compare );
       
   295     iLabeledCertInfos.Sort( order );
       
   296 
       
   297     iCertList->clear();
       
   298     iClientInfoArray->Reset();
       
   299     TInt count = iLabeledCertInfos.Count();
       
   300     for (TInt index = 0; index < count; index++) {
       
   301         const CCTCertInfo &certInfo = iLabeledCertInfos[index]->CertInfo();
       
   302         iClientInfoArray->AppendL(&certInfo);
       
   303 
       
   304         std::auto_ptr<AdvSecSettingsCertificate> cert(new(ELeave) AdvSecSettingsCertificate);
       
   305         cert->setModelIndex(index);
       
   306         cert->setCertType(CertType(certInfo));
       
   307         cert->setLabel(CopyStringL(iLabeledCertInfos[index]->Label()));
       
   308         iCertList->append(cert.release());
       
   309     }
       
   310     iLabeledCertInfos.ResetAndDestroy();
       
   311     iCertInfoArray.Reset();
       
   312 }
       
   313 
       
   314 // ---------------------------------------------------------------------------
       
   315 // CAdvSecSettingsCertListBuilder::CertType()
       
   316 // ---------------------------------------------------------------------------
       
   317 //
       
   318 AdvSecSettingsCertificate::CertificateType CAdvSecSettingsCertListBuilder::CertType(
       
   319     const CCTCertInfo &aCertInfo)
       
   320 {
       
   321     AdvSecSettingsCertificate::CertificateType type =
       
   322         AdvSecSettingsCertificate::NotDefined;
       
   323     TUid storeType;
       
   324     switch (aCertInfo.CertificateOwnerType()) {
       
   325     case ECACertificate:
       
   326         type = AdvSecSettingsCertificate::AuthorityCertificate;
       
   327         break;
       
   328     case EUserCertificate:
       
   329         storeType = aCertInfo.Handle().iTokenHandle.iTokenTypeUid;
       
   330         if (storeType.iUid == KAdvSecSettingsDeviceCertStore) {
       
   331             type = AdvSecSettingsCertificate::DeviceCertificate;
       
   332         } else {
       
   333             type = AdvSecSettingsCertificate::PersonalCertificate;
       
   334         }
       
   335         break;
       
   336     case EPeerCertificate:
       
   337         type = AdvSecSettingsCertificate::TrustedSiteCertificate;
       
   338         break;
       
   339     default:
       
   340         break;
       
   341     }
       
   342     return type;
       
   343 }
       
   344