javamanager/javacaptain/extensionplugins/javacertstore/src.s60/smartcardcryptotokenreader.cpp
branchRCL_3
changeset 19 04becd199f91
child 50 023eef975703
equal deleted inserted replaced
16:f5050f1da672 19:04becd199f91
       
     1 /*
       
     2 * Copyright (c) 2007-2007 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "smartcardcryptotokenreader.h"
       
    20 
       
    21 namespace java
       
    22 {
       
    23 namespace security
       
    24 {
       
    25 
       
    26 SmartCardCryptoTokenReader* SmartCardCryptoTokenReader::NewL()
       
    27 {
       
    28     SmartCardCryptoTokenReader* self = new(ELeave) SmartCardCryptoTokenReader();
       
    29     CleanupStack::PushL(self);
       
    30     self->ConstructL();
       
    31     CleanupStack::Pop(self);
       
    32     return self;
       
    33 }
       
    34 
       
    35 void SmartCardCryptoTokenReader::RetrieveCerts(RArray<HBufC8*>& aSmartCardCerts)
       
    36 {
       
    37     // keep a pointer to the provided array (this will be
       
    38     // filled with the smart card certificates)
       
    39     iSmartCardCerts = &aSmartCardCerts;
       
    40 
       
    41     // issue& complete the first request, so that we enter the state machine
       
    42     //(implemented inside RunL method)
       
    43     SetActive();
       
    44     CompleteRequest(KErrNone);
       
    45 
       
    46     // start the active scheduler
       
    47     CActiveScheduler::Start();
       
    48 }
       
    49 
       
    50 void SmartCardCryptoTokenReader::RunL()
       
    51 {
       
    52     if (iStatus.Int() != KErrNone)
       
    53     {
       
    54         iState = EReleaseToken;
       
    55     }
       
    56     switch (iState)
       
    57     {
       
    58     case EStart:
       
    59         NextState(Initialize());
       
    60         break;
       
    61     case EListTokenTypes:
       
    62         NextState(ListTokenTypes());
       
    63         break;
       
    64     case EOpenTokenType:
       
    65         NextState(OpenTokenType());
       
    66         break;
       
    67     case EOpenToken:
       
    68         NextState(OpenToken());
       
    69         break;
       
    70     case EGetTokenInterface:
       
    71         NextState(GetTokenInterface());
       
    72         break;
       
    73     case EListCertificates:
       
    74         NextState(ListCertificates());
       
    75         break;
       
    76     case EFilterCertificates:
       
    77         NextState(FilterCertificates());
       
    78         break;
       
    79     case ERetrieveCertificates:
       
    80         NextState(RetrieveCertificates());
       
    81         break;
       
    82     case EReleaseToken:
       
    83         NextState(ReleaseToken());
       
    84         break;
       
    85     case EFinish:
       
    86         // this is the final state
       
    87         CActiveScheduler::Stop();
       
    88         return;
       
    89     }
       
    90     // re-issue a new request
       
    91     SetActive();
       
    92 }
       
    93 
       
    94 void SmartCardCryptoTokenReader::DoCancel()
       
    95 {
       
    96     switch (iState)
       
    97     {
       
    98     case EStart:
       
    99     case EListTokenTypes:
       
   100     case EListCertificates:
       
   101     case ERetrieveCertificates:
       
   102     case EReleaseToken:
       
   103         // these were all self-completed operations
       
   104         // -> nothing to cancel
       
   105         break;
       
   106     case EOpenTokenType:
       
   107         CancelOpenTokenType();
       
   108         break;
       
   109     case EOpenToken:
       
   110         CancelOpenToken();
       
   111         break;
       
   112     case EGetTokenInterface:
       
   113         CancelGetTokenInterface();
       
   114         break;
       
   115     }
       
   116     DoReleaseToken();
       
   117 }
       
   118 
       
   119 TInt SmartCardCryptoTokenReader::RunError(TInt /*aError*/)
       
   120 {
       
   121     // cleanup
       
   122     DoReleaseToken();
       
   123     CActiveScheduler::Stop();
       
   124     return KErrNone;
       
   125 }
       
   126 
       
   127 
       
   128 bool SmartCardCryptoTokenReader::Initialize()
       
   129 {
       
   130     CompleteRequest(KErrNone);
       
   131     // the operation did succeed
       
   132     return true;
       
   133 }
       
   134 
       
   135 bool SmartCardCryptoTokenReader::ListTokenTypes()
       
   136 {
       
   137     TCTTokenTypeAttribute att = { KCTRemovable , 1 };
       
   138     iSmartCardTokensAttributes.Append(att);
       
   139     iSmartCardTokensInterfaces.Append(TUid::Uid(KInterfaceCertStore));
       
   140     TCTFindTokenTypesByInterfaceAndAttribute findByIAndA(
       
   141         iSmartCardTokensInterfaces.Array(), iSmartCardTokensAttributes.Array());
       
   142     CCTTokenTypeInfo::ListL(iSmartCardTokenTypes, findByIAndA);
       
   143     if (iSmartCardTokenTypes.Count() > 0)
       
   144     {
       
   145         // the operation did succeed
       
   146         CompleteRequest(KErrNone);
       
   147         return true;
       
   148     }
       
   149     else
       
   150     {
       
   151         // the operation did not succeed
       
   152         return false;
       
   153     }
       
   154 }
       
   155 
       
   156 bool SmartCardCryptoTokenReader::OpenTokenType()
       
   157 {
       
   158     iSmartCardTokenType = CCTTokenType::NewL(*iSmartCardTokenTypes[iCurrentTokenType], iFs);
       
   159     iSmartCardTokenType->List(iSmartCardTokenInfo, iStatus);
       
   160     iCurrentTokenType++;
       
   161     // the operation did succeed
       
   162     return true;
       
   163 }
       
   164 
       
   165 bool SmartCardCryptoTokenReader::OpenToken()
       
   166 {
       
   167     if (iSmartCardTokenInfo.Count() > 0)
       
   168     {
       
   169         iSmartCardTokenType->OpenToken(*iSmartCardTokenInfo[0], iSmartCardToken, iStatus);
       
   170         // the operation did succeed
       
   171         return true;
       
   172     }
       
   173     else
       
   174     {
       
   175         // the operation did not succeed
       
   176         return false;
       
   177     }
       
   178 }
       
   179 
       
   180 bool SmartCardCryptoTokenReader::GetTokenInterface()
       
   181 {
       
   182     iSmartCardToken->GetInterface(TUid::Uid(KInterfaceCertStore), iSmartCardTokenInterface, iStatus);
       
   183     // the operation did succeed
       
   184     return true;
       
   185 }
       
   186 
       
   187 bool SmartCardCryptoTokenReader::ListCertificates()
       
   188 {
       
   189     if (iSmartCardTokenInterface)
       
   190     {
       
   191         iSmartCardStore = static_cast<MCTCertStore*>(iSmartCardTokenInterface);
       
   192         iSmartCardCertsFilter = CCertAttributeFilter::NewL();
       
   193         iSmartCardCertsFilter->SetOwnerType(ECACertificate);
       
   194         iSmartCardCertsFilter->SetFormat(EX509Certificate);
       
   195         iSmartCardStore->List(iSmartCardCertInfos, *iSmartCardCertsFilter, iStatus);
       
   196         // the operation did succeed
       
   197         return true;
       
   198     }
       
   199     else
       
   200     {
       
   201         // the operation did not succeed
       
   202         return false;
       
   203     }
       
   204 }
       
   205 
       
   206 bool SmartCardCryptoTokenReader::FilterCertificates()
       
   207 {
       
   208     if (iCurrentRetrievedSmartCardCert < iSmartCardCertInfos.Count())
       
   209     {
       
   210         iSmartCardStore->Trusted(*iSmartCardCertInfos[iCurrentRetrievedSmartCardCert], iCurrentSmartCardCertIsTrusted, iStatus);
       
   211         // the operation did succeed
       
   212         return true;
       
   213     }
       
   214     else
       
   215     {
       
   216         // the operation did not succeed
       
   217         return false;
       
   218     }
       
   219 }
       
   220 
       
   221 bool SmartCardCryptoTokenReader::RetrieveCertificates()
       
   222 {
       
   223     if (iCurrentRetrievedSmartCardCert < iSmartCardCertInfos.Count())
       
   224     {
       
   225         // retrieve only the trusted certs
       
   226         if (iCurrentSmartCardCertIsTrusted)
       
   227         {
       
   228             iTmpBuf = HBufC8::NewMaxL(iSmartCardCertInfos[iCurrentRetrievedSmartCardCert]->Size());
       
   229             iTmpBufPtr.Set(iTmpBuf->Des());
       
   230             iSmartCardStore->Retrieve(*iSmartCardCertInfos[iCurrentRetrievedSmartCardCert], iTmpBufPtr, iStatus);
       
   231             iSmartCardCerts->Append(iTmpBuf);
       
   232         }
       
   233         else
       
   234         {
       
   235             // complete the outstanding request
       
   236             CompleteRequest(KErrNone);
       
   237         }
       
   238         // move on to the next certificate
       
   239         iCurrentRetrievedSmartCardCert++;
       
   240         // the operation did succeed
       
   241         return true;
       
   242     }
       
   243     else
       
   244     {
       
   245         // the operation did not succeed
       
   246         return false;
       
   247     }
       
   248 }
       
   249 
       
   250 bool SmartCardCryptoTokenReader::ReleaseToken()
       
   251 {
       
   252     DoReleaseToken();
       
   253     // complete the outstanding request
       
   254     CompleteRequest(KErrNone);
       
   255     // the operation did succeed
       
   256     return true;
       
   257 }
       
   258 
       
   259 void SmartCardCryptoTokenReader::CancelOpenTokenType()
       
   260 {
       
   261     iSmartCardTokenType->CancelList();
       
   262 }
       
   263 
       
   264 void SmartCardCryptoTokenReader::CancelOpenToken()
       
   265 {
       
   266     iSmartCardTokenType->CancelOpenToken();
       
   267 }
       
   268 
       
   269 void SmartCardCryptoTokenReader::CancelGetTokenInterface()
       
   270 {
       
   271     iSmartCardToken->CancelGetInterface();
       
   272 }
       
   273 
       
   274 void SmartCardCryptoTokenReader::ConstructL()
       
   275 {
       
   276     User::LeaveIfError(iFs.Connect());
       
   277     CActiveScheduler* CurrentActiveScheduler = CActiveScheduler::Current();
       
   278     if (CurrentActiveScheduler == NULL)
       
   279     {
       
   280         iActiveScheduler = new(ELeave) CActiveScheduler;
       
   281         CActiveScheduler::Install(iActiveScheduler);
       
   282     }
       
   283     CActiveScheduler::Add(this);
       
   284 }
       
   285 
       
   286 SmartCardCryptoTokenReader::SmartCardCryptoTokenReader()
       
   287         : CActive(EPriorityNormal), iState(EStart), iCurrentTokenType(0), iSmartCardToken(NULL), iSmartCardTokenType(NULL), iSmartCardStore(NULL), iSmartCardCertsFilter(NULL), iCurrentRetrievedSmartCardCert(0), iSmartCardCerts(NULL), iCurrentSmartCardCertIsTrusted(false), iTmpBuf(NULL), iTmpBufPtr(TPtr8(0,0)), iActiveScheduler(NULL)
       
   288 {
       
   289 }
       
   290 
       
   291 SmartCardCryptoTokenReader::~SmartCardCryptoTokenReader()
       
   292 {
       
   293     iSmartCardTokensInterfaces.Close();
       
   294     iSmartCardTokensAttributes.Close();
       
   295     iSmartCardTokenInfo.Close();
       
   296     iSmartCardCertInfos.Close();
       
   297     iFs.Close();
       
   298     if (iSmartCardCertsFilter)
       
   299     {
       
   300         delete iSmartCardCertsFilter;
       
   301         iSmartCardCertsFilter = NULL;
       
   302     }
       
   303     if (iActiveScheduler)
       
   304     {
       
   305         delete iActiveScheduler;
       
   306         iActiveScheduler = NULL;
       
   307     }
       
   308 }
       
   309 
       
   310 void SmartCardCryptoTokenReader::CompleteRequest(TInt aCompletionCode)
       
   311 {
       
   312     TRequestStatus* status = &iStatus;
       
   313     User::RequestComplete(status,aCompletionCode);
       
   314 }
       
   315 
       
   316 void SmartCardCryptoTokenReader::NextState(bool aCurrentOperationSucceeded)
       
   317 {
       
   318     if (!aCurrentOperationSucceeded)
       
   319     {
       
   320         // cancel the outstanding request
       
   321         CompleteRequest(KErrCancel);
       
   322         // move on to the last state
       
   323         iState = EReleaseToken;
       
   324         return;
       
   325     }
       
   326     switch (iState)
       
   327     {
       
   328     case EStart:
       
   329         iState = EListTokenTypes;
       
   330         break;
       
   331     case EListTokenTypes:
       
   332         iState = EOpenTokenType;
       
   333         break;
       
   334     case EOpenTokenType:
       
   335         iState = EOpenToken;
       
   336         break;
       
   337     case EOpenToken:
       
   338         iState = EGetTokenInterface;
       
   339         break;
       
   340     case EGetTokenInterface:
       
   341         iState = EListCertificates;
       
   342         break;
       
   343     case EListCertificates:
       
   344         iState = EFilterCertificates;
       
   345         break;
       
   346     case EFilterCertificates:
       
   347         iState = EFilterCertificates;
       
   348         if (iCurrentRetrievedSmartCardCert >= iSmartCardCertInfos.Count())
       
   349         {
       
   350             // change the state only if we are done with reading all
       
   351             // the certs
       
   352             iState = EReleaseToken;
       
   353         }
       
   354         else
       
   355         {
       
   356 
       
   357             iState = ERetrieveCertificates;
       
   358         }
       
   359         break;
       
   360     case ERetrieveCertificates:
       
   361         if (iCurrentRetrievedSmartCardCert >= iSmartCardCertInfos.Count())
       
   362         {
       
   363             // change the state only if we are done with reading all
       
   364             // the certs
       
   365             iState = EReleaseToken;
       
   366         }
       
   367         else
       
   368         {
       
   369             iState = EFilterCertificates;
       
   370         }
       
   371         break;
       
   372     case EReleaseToken:
       
   373         if (iCurrentTokenType >= iSmartCardTokenTypes.Count())
       
   374         {
       
   375             // if we are done going through all the token types,
       
   376             // move to finish state
       
   377             iState = EFinish;
       
   378         }
       
   379         else
       
   380         {
       
   381             // if there are still token types to be processed, go back
       
   382             // to EOpenTokenType
       
   383             iState = EOpenTokenType;
       
   384         }
       
   385         break;
       
   386     }
       
   387 }
       
   388 
       
   389 void SmartCardCryptoTokenReader::DoReleaseToken()
       
   390 {
       
   391     if (iSmartCardTokenType)
       
   392     {
       
   393         iSmartCardTokenType->Release();
       
   394         iSmartCardTokenType = NULL;
       
   395     }
       
   396     if (iSmartCardToken)
       
   397     {
       
   398         iSmartCardToken->Release();
       
   399         iSmartCardToken = NULL;
       
   400     }
       
   401     if (iSmartCardTokenInterface)
       
   402     {
       
   403         iSmartCardTokenInterface->Release();
       
   404         iSmartCardTokenInterface = NULL;
       
   405     }
       
   406     iSmartCardTokenInfo.Close();
       
   407 
       
   408 }
       
   409 
       
   410 } //end namespace security
       
   411 } //end namespace java