wim/WimServer/src/WimCertUtil.cpp
changeset 0 164170e6151a
child 57 ead42e28e519
equal deleted inserted replaced
-1:000000000000 0:164170e6151a
       
     1 /*
       
     2 * Copyright (c) 2002-2009 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:  Utility functions for certificate related functionality
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 #include    "WimCertUtil.h"
       
    21 #include    "WimTrace.h"
       
    22 #include    "WimCertInfo.h"
       
    23 #include    "WimUtilityFuncs.h"
       
    24 #include    "WimCleanup.h"
       
    25 #include    <cctcertinfo.h>
       
    26 #include    <asn1dec.h>         // ASN.1 decoding
       
    27 
       
    28 
       
    29 // -----------------------------------------------------------------------------
       
    30 // CWimCertUtil::CWimCertUtil()
       
    31 // Default constructor
       
    32 // -----------------------------------------------------------------------------
       
    33 CWimCertUtil::CWimCertUtil( MCTToken& aToken )
       
    34     : CActive( EPriorityStandard ),
       
    35       iToken( aToken )
       
    36     {
       
    37     }
       
    38 
       
    39 // -----------------------------------------------------------------------------
       
    40 // CWimCertUtil::NewL()
       
    41 // Symbian 2nd phase constructor can leave.
       
    42 // -----------------------------------------------------------------------------
       
    43 CWimCertUtil* CWimCertUtil::NewL( MCTToken& aToken )
       
    44     {
       
    45     _WIMTRACE( _L( "WimServer|CWimCertUtil::NewL" ) );
       
    46     CWimCertUtil* self = new( ELeave ) CWimCertUtil( aToken );
       
    47     CleanupStack::PushL( self );
       
    48     self->ConstructL();
       
    49     CleanupStack::Pop( self ); //self
       
    50     return self;
       
    51     }
       
    52 
       
    53 // -----------------------------------------------------------------------------
       
    54 // CWimCertUtil::ConstructL()
       
    55 // Second phase
       
    56 // -----------------------------------------------------------------------------
       
    57 void CWimCertUtil::ConstructL()
       
    58     {
       
    59     _WIMTRACE( _L( "WimServer|CWimCertUtil::ConstructL" ) );
       
    60     CActiveScheduler::Add( this );
       
    61     iWimUtilFuncs = CWimUtilityFuncs::NewL();
       
    62     _WIMTRACE ( _L( "CWimCertUtil::ConstructL() completed" ) );
       
    63     }
       
    64 
       
    65 // -----------------------------------------------------------------------------
       
    66 // CWimCertUtil::Restore()
       
    67 // Returns CCTCertInfo objects in array
       
    68 // -----------------------------------------------------------------------------
       
    69 void CWimCertUtil::Restore( RPointerArray<CWimCertInfo>& aArray, 
       
    70                             TRequestStatus& aStatus )
       
    71     {
       
    72     _WIMTRACE( _L( "WimServer|CWimCertUtil::Restore" ) );
       
    73     iClientStatus = &aStatus;
       
    74     aStatus = KRequestPending;
       
    75     iArray = &aArray;
       
    76     iPhase = EListCertsFromWim;
       
    77     SignalOwnStatusAndComplete();
       
    78     }
       
    79 
       
    80 // -----------------------------------------------------------------------------
       
    81 // CWimCertUtil::CancelRestore()
       
    82 // Cancels outgoing Restore operation. Sets an internal flag to true. 
       
    83 // After necessary cleanup, caller is signalled with KErrCancel 
       
    84 // -error code.
       
    85 // -----------------------------------------------------------------------------
       
    86 void CWimCertUtil::CancelRestore()
       
    87     {
       
    88     _WIMTRACE ( _L( "CWimCertUtil::CancelRestore" ) );
       
    89     Cancel(); 
       
    90     }
       
    91 
       
    92 // -----------------------------------------------------------------------------
       
    93 // CWimCertUtil::RetrieveCertByIndexL()
       
    94 // Retrieves the actual certificate.
       
    95 // -----------------------------------------------------------------------------
       
    96 
       
    97 void CWimCertUtil::RetrieveCertByIndexL( const TInt aIndex,
       
    98                                          TDes8& aEncodedCert, 
       
    99                                          TRequestStatus& aStatus )
       
   100     {
       
   101     _WIMTRACE ( _L( "WimServer|CWimCertUtil::RetrieveCertByIndexL" ) );
       
   102     //Check that index is valid
       
   103     __ASSERT_ALWAYS( aIndex <= iArraySize && iCertRefLst,
       
   104         User::Leave( KErrArgument )  );
       
   105     iClientStatus = &aStatus;
       
   106     iCertRetrieveIndex = aIndex;
       
   107     iEncodedCert = &aEncodedCert;
       
   108     aStatus = KRequestPending;
       
   109     iPhase = ERetrieveCertificate;
       
   110     SignalOwnStatusAndComplete();
       
   111     }
       
   112 
       
   113 // -----------------------------------------------------------------------------
       
   114 // CWimCertUtil::SignalOwnStatusAndComplete()       
       
   115 // Sets own iStatus to KRequestPending, and signals it 
       
   116 // with User::RequestComplete() -request. This gives chance 
       
   117 // active scheduler to run other active objects. After a quick
       
   118 // visit in active scheduler, signal returns to RunL() and starts next
       
   119 // phase of operation. 
       
   120 // -----------------------------------------------------------------------------
       
   121 void CWimCertUtil::SignalOwnStatusAndComplete()
       
   122     {
       
   123     _WIMTRACE( _L( "WimServer|CWimCertUtil::SignalOwnStatusAndComplete()" ) );
       
   124     iStatus = KRequestPending;
       
   125     SetActive();
       
   126     TRequestStatus* status = &iStatus;
       
   127     User::RequestComplete( status, KErrNone );
       
   128     }
       
   129     
       
   130 // -----------------------------------------------------------------------------
       
   131 // CWimCertUtil::~CWimCertUtil()
       
   132 // Allocated memory is released.
       
   133 // -----------------------------------------------------------------------------
       
   134 CWimCertUtil::~CWimCertUtil()
       
   135     {
       
   136     _WIMTRACE( _L( "WimServer|CWimCertUtil::~CWimCertUtil()" ) );
       
   137     Cancel();
       
   138     DeallocWimCertInfo();
       
   139     DeallocCertHBufs();
       
   140     DeallocReferences();
       
   141     delete iTrustedUsages;
       
   142     delete iTrustedUsagesPtr;
       
   143     delete iKeyIdBuf;
       
   144     delete iKeyIdPointer;
       
   145     delete iWimUtilFuncs;
       
   146     }
       
   147 
       
   148 // -----------------------------------------------------------------------------
       
   149 // CWimCertUtil::RunL()
       
   150 // This has eight different phases which are explained here:
       
   151 // 1. EListCertsFromWim:Allocate member variables for arrays usage. 
       
   152 //    Array is passed as an argument to server and it is filled with 
       
   153 //    certificate information found from WIM.
       
   154 // 2. ECreateNewCertObjects: New certificate objects are created based on 
       
   155 //    received information on step one. A little bit of conversion is 
       
   156 //    required. Objects are inserted to an array.
       
   157 // 3. ERetrieveCertificate:Allocate member variables for struct, 
       
   158 //    which is used to fetch certificate details
       
   159 // 4. ERetrievingCompleted: Check cancellation. If not cancelled,
       
   160 //    copy information to caller's buffer. 
       
   161 // 5. EAddCertificate: Adds certificate to WIM.
       
   162 // 6. EAddCertificateCompleted: Certificate is added successfully. Deallocate
       
   163 //                              variables and complete request.
       
   164 // 7. ERemove: Before removing, we will check for cancellation. After that
       
   165 //    cancellation is too late.
       
   166 // 8. ERemoveCompleted: Removal operation completed ok. Deallocte variables and
       
   167 //    complete request
       
   168 // 9. EGetCertExtras: Extra data is fetched and async. waiter needs to be 
       
   169 //    stopped
       
   170 // -----------------------------------------------------------------------------
       
   171 void CWimCertUtil::RunL()
       
   172     {
       
   173     _WIMTRACE3(_L("WimServer|CWimCertUtil::RunL(), iStatus=%d, iPhase=%d"), iStatus.Int(), iPhase);
       
   174     //Check for error
       
   175     if ( iStatus.Int() != KErrNone )
       
   176         {
       
   177         //failed to retrieve certificate
       
   178         if ( iPhase == ERetrievingCompleted )
       
   179             {
       
   180              DeallocCertHBufs();
       
   181              _WIMTRACE2 ( _L( "CWimCertUtil::RunL() failed to retrieve \
       
   182                  certificate, ERROR = %d" ),iStatus.Int() );
       
   183             }
       
   184        
       
   185         User::RequestComplete( iClientStatus, iStatus.Int() );
       
   186         }
       
   187     else
       
   188         {
       
   189         switch( iPhase )
       
   190             {
       
   191             case EListCertsFromWim: //get certificates from WIM.
       
   192                 {                  
       
   193                 if ( iArraySize ) //Delete previous preferences. 
       
   194                     {           //User might added or removed a certificate. 
       
   195                     DeallocWimCertInfo();
       
   196                     DeallocReferences();
       
   197                     }
       
   198             
       
   199                 //Ok ready to begin. First get Cert count
       
   200                 iCertCount = CertCount( EWimEntryTypeAll ); 
       
   201                 _WIMTRACE2 ( _L( "CWimCertUtil::RunL() \
       
   202                     certCount =%d" ),iCertCount );
       
   203                 if ( !iCertCount )  
       
   204                     {
       
   205                     User::RequestComplete( iClientStatus, KErrNotFound );
       
   206                     }
       
   207                 else
       
   208                     {
       
   209                     __ASSERT_DEBUG( iCertRefLst == NULL, User::Invariant() );
       
   210                     iCertRefLst = new( ELeave ) TCertificateAddress[iCertCount];
       
   211 
       
   212                     //Creates new array according to certificate count
       
   213                     __ASSERT_DEBUG( iCertInfoArr == NULL, User::Invariant() );
       
   214                     iCertInfoArr  = new( ELeave ) TWimCertInfo[iCertCount];
       
   215             
       
   216                     AllocWimCertInfoL( iCertInfoArr, iCertCount );
       
   217                     iStatus = KRequestPending;
       
   218 
       
   219                     CertRefLstL( iCertRefLst, 
       
   220                                  iCertInfoArr, 
       
   221                                  EWimEntryTypeAll );
       
   222 
       
   223                     iPhase = ECreateNewCertObjects;
       
   224                     iIndex = 0;
       
   225                     SetActive();
       
   226 
       
   227                     TRequestStatus* status = &iStatus;
       
   228                     User::RequestComplete( status, KErrNone );
       
   229 
       
   230                     _WIMTRACE( _L( "CWimCertUtil::RunL() \
       
   231                         GetCertRefLst" ) );
       
   232                     }
       
   233                 break;
       
   234                 }
       
   235             case ECreateNewCertObjects: //Certificates fetched, 
       
   236                 { 
       
   237                 CreateNewCertObjectsL();//create new cert-objects
       
   238                 break;
       
   239                 }
       
   240             case EGetTrustedUsages:
       
   241                 {
       
   242                 GetTrustedUsagesL();
       
   243                 break;
       
   244                 }
       
   245             case ETrustedUsagesDone:
       
   246                 {
       
   247                 TrustedUsagesDoneL();
       
   248                 break;
       
   249                 }
       
   250             case ECertObjectsDone: 
       
   251                 {
       
   252                 User::RequestComplete( iClientStatus, KErrNone );    
       
   253                 _WIMTRACE ( _L( "CWimCertUtil::RunL() \
       
   254                     ECertObjectsDone" ) );
       
   255                 break;
       
   256                 }
       
   257             case ERetrieveCertificate: //Get Cert details from WIM
       
   258                 {
       
   259                 AllocMemoryForCertDetailsL();
       
   260                 CertDetailsL( iCertRefLst[iCertRetrieveIndex], 
       
   261                               iWimCertDetails );
       
   262                 iPhase = ERetrievingCompleted;
       
   263                 SetActive();
       
   264 
       
   265                 TRequestStatus* status = &iStatus;
       
   266                     User::RequestComplete( status, KErrNone );
       
   267 
       
   268                 _WIMTRACE ( _L( "CWimCertUtil::RunL() \
       
   269                     ERetrieveCertificate" ) );
       
   270                 break;
       
   271                 }
       
   272             case ERetrievingCompleted: //Cert details fetched, check errors &                         
       
   273                 {                      //cancellations & complete request                   
       
   274                 //copy cert details to iEncodedCert -buffer,
       
   275                 //which points to received aEncoded -cert buffer.
       
   276                 CopyRetrievedCertData();
       
   277                 DeallocCertHBufs();
       
   278                 User::RequestComplete( iClientStatus, KErrNone );
       
   279                 _WIMTRACE ( _L( "CWimCertUtil::RunL() \
       
   280                     ERetrievingCompleted" ) );
       
   281                 break;
       
   282                 }
       
   283            
       
   284             default:
       
   285                 {
       
   286                 User::RequestComplete( iClientStatus, KErrNotSupported );
       
   287                 break;
       
   288                 }          
       
   289             }
       
   290         }
       
   291     }
       
   292 
       
   293 // -----------------------------------------------------------------------------
       
   294 // CWimCertUtil::CreateNewCertObjectsL()
       
   295 // Creates new certificate objects which can be returned to the caller.
       
   296 // -----------------------------------------------------------------------------
       
   297 void CWimCertUtil::CreateNewCertObjectsL()
       
   298     {
       
   299     _WIMTRACE( _L( "WimServer|CWimCertUtil::CreateNewCertObjectsL() | Begin" ) );
       
   300 
       
   301     HBufC16* label16 = NULL;
       
   302     HBufC8* hash8 = NULL;
       
   303 
       
   304     if ( iIndex < iCertCount )
       
   305         {
       
   306         TCertificateOwnerType certificateType;
       
   307         if ( iCertInfoArr[iIndex].iUsage == 0 )
       
   308             {
       
   309             certificateType = EUserCertificate; // 0 == User
       
   310             }
       
   311         else
       
   312             {
       
   313             certificateType = ECACertificate;   // 1 == CA
       
   314             }
       
   315                         
       
   316         label16 = HBufC16::NewLC( iCertInfoArr[iIndex].iLabel.Length() );
       
   317         label16->Des().Copy( iCertInfoArr[iIndex].iLabel );
       
   318             
       
   319         hash8 = HBufC8::NewLC( iCertInfoArr[iIndex].iIssuerHash.Length() );
       
   320         hash8->Des().Copy( iCertInfoArr[iIndex].iIssuerHash );
       
   321         // Convert type 
       
   322         TCertificateFormat format;
       
   323         format = GetCertFormatByIndex( iIndex );
       
   324         //Create key indentifiers
       
   325         TKeyIdentifier subjectKeyId;
       
   326         TKeyIdentifier issuerKeyId;
       
   327         //needs these for CCTCertInfo object -creation
       
   328         subjectKeyId = iCertInfoArr[iIndex].iKeyId;
       
   329         issuerKeyId  = iCertInfoArr[iIndex].iCAId;
       
   330         //Create CCTCertInfo object. 
       
   331         if ( iCertInfoArr[iIndex].iIssuerHash.Length() > 0 )
       
   332             {
       
   333             iCert = CCTCertInfo::NewL( label16->Des(), 
       
   334                                       format, 
       
   335                                       certificateType,
       
   336                                       iCertInfoArr[iIndex].iCertlen, 
       
   337                                       &subjectKeyId, 
       
   338                                       &issuerKeyId, 
       
   339                                       iToken, 
       
   340                                       iIndex,
       
   341                                       iCertInfoArr[iIndex].iModifiable,
       
   342                                       hash8 );
       
   343             }
       
   344         else
       
   345             {
       
   346             iCert = CCTCertInfo::NewL( label16->Des(), 
       
   347                                       format, 
       
   348                                       certificateType,
       
   349                                       iCertInfoArr[iIndex].iCertlen, 
       
   350                                       &subjectKeyId, 
       
   351                                       &issuerKeyId, 
       
   352                                       iToken, 
       
   353                                       iIndex,
       
   354                                       iCertInfoArr[iIndex].iModifiable );
       
   355             }
       
   356 
       
   357         CleanupStack::PopAndDestroy( hash8 ); 
       
   358         CleanupStack::PopAndDestroy( label16 );
       
   359 
       
   360         iOids = new( ELeave ) RArray<HBufC*>();
       
   361         //Check whether certificate has extra data. Certificate type must be
       
   362         // X509
       
   363         if ( iCertInfoArr[iIndex].iCDFRefs &&
       
   364              iCertInfoArr[iIndex].iType == WIMI_CT_X509 )
       
   365             {  
       
   366             iPhase = EGetTrustedUsages;
       
   367             }
       
   368         else
       
   369             {
       
   370             CreateNewWimCertObjectL();
       
   371             }
       
   372         }
       
   373     else
       
   374         {
       
   375         DeallocWimCertInfo(); 
       
   376         iPhase = ECertObjectsDone;
       
   377         }
       
   378     SignalOwnStatusAndComplete();
       
   379     }
       
   380 
       
   381 // -----------------------------------------------------------------------------
       
   382 // CWimCertUtil::CreateNewWimCertObjectL()
       
   383 // 
       
   384 // -----------------------------------------------------------------------------
       
   385 void CWimCertUtil::CreateNewWimCertObjectL()
       
   386     {
       
   387     _WIMTRACE( _L( "WimServer|CWimCertUtil::CreateNewWimCertObjectL()" ) );
       
   388 
       
   389     iCertInfo = CWimCertInfo::NewL( iCert, //CCTCertInfo object
       
   390                                     iCert->SubjectKeyId(), //key hash
       
   391                                     *iOids, 
       
   392                                     iCertInfoArr[iIndex].iCDFRefs );
       
   393     
       
   394     delete iOids;
       
   395     iOids = NULL;
       
   396 
       
   397     //Append WimCertInfo -object to array. Ownership to iArray here.
       
   398     User::LeaveIfError( iArray->Append( iCertInfo ) );
       
   399 
       
   400     iPhase = ECreateNewCertObjects;
       
   401     iIndex++;
       
   402     }
       
   403         
       
   404 // -----------------------------------------------------------------------------
       
   405 // CWimCertUtil::GetTrustedUsagesL
       
   406 // Get trusted usages (OIDs) of a current certificate, which is pointed out 
       
   407 // by a index
       
   408 // -----------------------------------------------------------------------------
       
   409 void CWimCertUtil::GetTrustedUsagesL()
       
   410     {
       
   411     _WIMTRACE( _L( "WimServer|CWimCertUtil::GetTrustedUsagesL() | Begin" ) );
       
   412 
       
   413     delete iTrustedUsages;
       
   414     iTrustedUsages = NULL;
       
   415     delete iTrustedUsagesPtr;
       
   416     iTrustedUsagesPtr = NULL;
       
   417     //Allocate variables for trusted usage fetching
       
   418     iTrustedUsages = HBufC::NewL( iCertInfoArr[iIndex].iTrustedUsageLength );
       
   419     iTrustedUsagesPtr = new( ELeave ) TPtr( iTrustedUsages->Des() );
       
   420 
       
   421     iCertExtrasInfo.iTrustedUsage = iTrustedUsagesPtr;
       
   422     iPhase = ETrustedUsagesDone;
       
   423 
       
   424     delete iKeyIdBuf;
       
   425     iKeyIdBuf = NULL;
       
   426     delete iKeyIdPointer;
       
   427     iKeyIdPointer = NULL;
       
   428     // Take a copy of key identifier
       
   429     iKeyIdBuf = iCertInfoArr[iIndex].iKeyId.AllocL();
       
   430     iKeyIdPointer = new( ELeave ) TPtr8( iKeyIdBuf->Des() );
       
   431 
       
   432     GetCertExtrasL( iKeyIdPointer,
       
   433                     iCertExtrasInfo,
       
   434                     iCertInfoArr[iIndex].iUsage,
       
   435                     iStatus );
       
   436     SetActive();
       
   437     _WIMTRACE ( _L( "CWimCertUtil::GetTrustedUsagesL() | End" ) );
       
   438     }
       
   439 
       
   440 // -----------------------------------------------------------------------------
       
   441 // CWimCertUtil::TrustedUsagesDoneL()
       
   442 // -----------------------------------------------------------------------------
       
   443 void CWimCertUtil::TrustedUsagesDoneL()
       
   444     {
       
   445     _WIMTRACE( _L( "WimServer|CWimCertUtil::TrustedUsagesDoneL()" ) );
       
   446     if ( iStatus.Int() == KErrNone )
       
   447         {
       
   448         //Parse oids and put them to an array
       
   449         TLex16 lex( *iTrustedUsages );
       
   450         TPtrC16 lexToken;
       
   451         for ( TBool extrasDone = EFalse; extrasDone == EFalse; )
       
   452             {
       
   453             lexToken.Set( lex.NextToken() );
       
   454             if ( lexToken.Length() )
       
   455                 {
       
   456                 HBufC* oid = lexToken.AllocLC();
       
   457                 User::LeaveIfError( iOids->Append( oid ) );
       
   458                 CleanupStack::Pop( oid ); //oid
       
   459                 }
       
   460             else
       
   461                 {
       
   462                 extrasDone = ETrue;
       
   463                 }
       
   464             }
       
   465         CreateNewWimCertObjectL();
       
   466         SignalOwnStatusAndComplete();
       
   467         }
       
   468     else
       
   469         {
       
   470         User::RequestComplete( iClientStatus, iStatus.Int() );
       
   471         }
       
   472     }
       
   473 
       
   474 // -----------------------------------------------------------------------------
       
   475 // CWimCertUtil::GetCertFormatByIndex()
       
   476 // Returns certificate format according to received index
       
   477 // -----------------------------------------------------------------------------
       
   478 TCertificateFormat CWimCertUtil::GetCertFormatByIndex( TInt aIndex )
       
   479     {
       
   480     _WIMTRACE ( _L( "WimServer|CWimCertUtil::GetCertFormatByIndex()" ) );
       
   481 
       
   482     // Convert type 
       
   483     TCertificateFormat format;
       
   484     switch ( iCertInfoArr[aIndex].iType )
       
   485         {
       
   486         case WIMI_CT_WTLS: 
       
   487             {
       
   488             format = EWTLSCertificate;
       
   489             break;
       
   490             }
       
   491         case WIMI_CT_X509: 
       
   492             {
       
   493             format = EX509Certificate;
       
   494             break;
       
   495             }
       
   496         case WIMI_CT_X968: 
       
   497             {
       
   498             format = EX968Certificate;
       
   499             break;
       
   500             }
       
   501         case WIMI_CT_URL: 
       
   502             {
       
   503             format = EX509CertificateUrl;
       
   504             break;
       
   505             }
       
   506         default:
       
   507             {
       
   508             format = EUnknownCertificate;
       
   509             break;
       
   510             }         
       
   511         }
       
   512     return format;
       
   513     }
       
   514 
       
   515 // -----------------------------------------------------------------------------
       
   516 // CWimCertUtil::DoCancel()
       
   517 // Deallocates member variables and completes client status with
       
   518 // KErrCancel error code.
       
   519 // -----------------------------------------------------------------------------
       
   520 void CWimCertUtil::DoCancel()
       
   521     {
       
   522     _WIMTRACE( _L( "WimServer|CWimCertUtil::DoCancel()" ) );
       
   523 
       
   524     if ( iPhase == EGetTrustedUsages || iPhase == ETrustedUsagesDone )
       
   525         {
       
   526         iCert->Release();
       
   527         }
       
   528 
       
   529     DeallocWimCertInfo();
       
   530     DeallocCertHBufs();
       
   531     DeallocReferences();
       
   532     User::RequestComplete( iClientStatus, KErrCancel );
       
   533     }
       
   534 
       
   535 // -----------------------------------------------------------------------------
       
   536 // CWimCertUtil::RunError() 
       
   537 //                                       
       
   538 // The active scheduler calls this function if this active object's RunL() 
       
   539 // function leaves. This gives this active object the opportunity to perform 
       
   540 // any necessary cleanup.
       
   541 // After array's cleanup, complete request with received error code.
       
   542 // -----------------------------------------------------------------------------
       
   543 TInt CWimCertUtil::RunError( TInt aError )
       
   544     {
       
   545     _WIMTRACE( _L( "WimServer|CWimCertUtil::RunError Error = %d" ) );
       
   546     DeallocWimCertInfo();
       
   547     DeallocCertHBufs();
       
   548     delete iTrustedUsages;
       
   549     iTrustedUsages = NULL;
       
   550     delete iTrustedUsagesPtr;
       
   551     iTrustedUsagesPtr = NULL;
       
   552     delete iKeyIdBuf;
       
   553     iKeyIdBuf = NULL;
       
   554     delete iKeyIdPointer;
       
   555     iKeyIdPointer = NULL;
       
   556 
       
   557     User::RequestComplete( iClientStatus, aError );
       
   558     return KErrNone;
       
   559     }
       
   560 
       
   561 // -----------------------------------------------------------------------------
       
   562 // CWimCertUtil::AllocWimCertInfoL()
       
   563 // Allocates memory for the array which is filled by server.
       
   564 // -----------------------------------------------------------------------------
       
   565 void CWimCertUtil::AllocWimCertInfoL( 
       
   566     TWimCertInfo* aWimCertInfoArr, 
       
   567     TInt aCount )
       
   568     {
       
   569     _WIMTRACE( _L( "WimServer|CWimCertUtil::AllocWimCertInfoL()" ) );
       
   570 
       
   571     TUint8 index;    
       
   572     //These are arrays
       
   573     iLabel = new( ELeave ) PHBufC8[aCount];
       
   574     iKeyId = new( ELeave ) PHBufC8[aCount];
       
   575     iCAId  = new( ELeave ) PHBufC8[aCount];
       
   576     iIssuerHash = new( ELeave ) PHBufC8[aCount];
       
   577 
       
   578     iLabelPtr = new( ELeave ) PTPtr8[aCount];
       
   579     iKeyIdPtr = new( ELeave ) PTPtr8[aCount];
       
   580     iCAIdPtr  = new( ELeave ) PTPtr8[aCount];
       
   581     iIssuerHashPtr = new( ELeave ) PTPtr8[aCount];
       
   582 
       
   583     for ( index = 0; index < aCount ; index++ )
       
   584         {
       
   585         iLabel[index] = HBufC8::NewL( KLabelLen );
       
   586         iKeyId[index] = HBufC8::NewL( KKeyIdLen );
       
   587         iCAId[index]  = HBufC8::NewL( KPkcs15IdLen );
       
   588         iIssuerHash[index] = HBufC8::NewL( KIssuerHash );
       
   589         
       
   590         iLabelPtr[index]  = new( ELeave ) TPtr8( iLabel[index]->Des() );
       
   591         iKeyIdPtr[index]  = new( ELeave ) TPtr8( iKeyId[index]->Des() );
       
   592         iCAIdPtr[index]   = new( ELeave ) TPtr8( iCAId[index]->Des() );
       
   593         iIssuerHashPtr[index] = new( ELeave ) 
       
   594                                 TPtr8( iIssuerHash[index]->Des() );
       
   595 
       
   596         aWimCertInfoArr[index].iLabel.Copy( iLabelPtr[index]->Ptr(),
       
   597                                             iLabelPtr[index]->Length() );
       
   598         aWimCertInfoArr[index].iKeyId.Copy( iKeyIdPtr[index]->Ptr(),
       
   599                                             iKeyIdPtr[index]->Length() );
       
   600         aWimCertInfoArr[index].iCAId.Copy( iCAIdPtr[index]->Ptr(),
       
   601                                            iCAIdPtr[index]->Length() );
       
   602         aWimCertInfoArr[index].iIssuerHash.Copy(
       
   603             iIssuerHashPtr[index]->Ptr(), iIssuerHashPtr[index]->Length() );
       
   604         }
       
   605     iArraySize = aCount;   
       
   606     }
       
   607 
       
   608 // -----------------------------------------------------------------------------
       
   609 // CWimCertUtil::AllocMemoryForCertDetailsL()
       
   610 // Allocates memory for a struct which is filled by server.
       
   611 // -----------------------------------------------------------------------------     
       
   612 void CWimCertUtil::AllocMemoryForCertDetailsL()
       
   613     {
       
   614     _WIMTRACE( _L( "WimServer|CWimCertUtil::AllocMemoryForCertDetailsL()" ) );
       
   615     iCertHBufOne = HBufC8::NewL( iEncodedCert->MaxLength() ); //whole cert
       
   616     iCertHBufOnePtr = new( ELeave ) TPtr8( iCertHBufOne->Des() ); 
       
   617     iWimCertDetails.iCert = iCertHBufOnePtr;
       
   618     }
       
   619 
       
   620 // -----------------------------------------------------------------------------
       
   621 // CWimCertUtil::CopyRetrievedCertData()
       
   622 // Writes data to caller's buffer by using pointer.
       
   623 // -----------------------------------------------------------------------------
       
   624 void CWimCertUtil::CopyRetrievedCertData()
       
   625     {
       
   626     _WIMTRACE( _L( "WimServer|CWimCertUtil::CopyRetrievedCertData()" ) );
       
   627     TPtr8 ptr = iCertHBufOne->Des();
       
   628     iEncodedCert->Copy( ptr );
       
   629     }
       
   630 
       
   631 // -----------------------------------------------------------------------------
       
   632 // CWimCertUtil::DeallocCertHBufs()
       
   633 // DeAllocates memory from member variables, which are used
       
   634 // when communicating with WIM.
       
   635 // -----------------------------------------------------------------------------
       
   636 void CWimCertUtil::DeallocCertHBufs()
       
   637     {
       
   638     _WIMTRACE ( _L( "WimServer|CWimCertUtil::DeallocCertHBufs()" ) ); 
       
   639     if ( iCertHBufOne )
       
   640         {
       
   641         delete iCertHBufOne;
       
   642         delete iCertHBufOnePtr;
       
   643         iCertHBufOne = NULL;
       
   644         iCertHBufOnePtr = NULL;
       
   645         }
       
   646     if ( iCertHBufTwo )
       
   647         {
       
   648         delete iCertHBufTwo;
       
   649         delete iCertHBufTwoPtr;
       
   650         iCertHBufTwo = NULL;
       
   651         iCertHBufTwoPtr = NULL;
       
   652         }
       
   653     if ( iCertHBufThree )
       
   654         {
       
   655         delete iCertHBufThree;
       
   656         delete iCertHBufThreePtr;
       
   657         iCertHBufThree = NULL;
       
   658         iCertHBufThreePtr = NULL;
       
   659         }
       
   660     if ( iCertHBufFour )
       
   661         {
       
   662         delete iCertHBufFour;
       
   663         delete iCertHBufFourPtr;
       
   664         iCertHBufFour = NULL;
       
   665         iCertHBufFourPtr = NULL;
       
   666         }
       
   667     }
       
   668 
       
   669 // -----------------------------------------------------------------------------
       
   670 // CWimCertUtil::DeallocReferences()
       
   671 // Deallocates memory. If user has cancelled initialization process, we need
       
   672 // to dealloc our references to loaded certs. 
       
   673 // -----------------------------------------------------------------------------
       
   674 void CWimCertUtil::DeallocReferences()
       
   675     {
       
   676     _WIMTRACE( _L( "WimServer|CWimCertUtil::DeallocReferences()" ) ); 
       
   677     if ( iCertRefLst )
       
   678         {
       
   679         for( TInt index = 0; index < iCertCount; ++index )
       
   680             {
       
   681             WIMI_Ref_t* ref = reinterpret_cast< WIMI_Ref_t* >( iCertRefLst[ index ] );
       
   682             _WIMTRACE2( _L( "WimServer|CWimCertUtil::DeallocReferences(), -ref 0x%08x" ), ref );
       
   683             free_WIMI_Ref_t( ref );
       
   684             iCertRefLst[ index ] = 0;
       
   685             }
       
   686         delete[] iCertRefLst;
       
   687         iCertRefLst = NULL;
       
   688         }
       
   689     if ( iCertInfoArr )
       
   690         {
       
   691         delete iCertInfoArr;
       
   692         iCertInfoArr = NULL;
       
   693         }  
       
   694     iArraySize = 0;
       
   695     iArray = NULL;
       
   696     iCertCount = 0;
       
   697     }
       
   698 
       
   699 // -----------------------------------------------------------------------------
       
   700 // CWimCertUtil::DeallocWimCertInfo()
       
   701 // Deallocates memory. If something has leaved during asynchronous request, we 
       
   702 // will deallocate all member data. 
       
   703 // -----------------------------------------------------------------------------
       
   704 void CWimCertUtil::DeallocWimCertInfo()
       
   705     {
       
   706     _WIMTRACE( _L( "WimServer|CWimCertUtil::DeallocWimCertInfo()" ) ); 
       
   707     TUint8 index;
       
   708     for ( index = 0; index < iArraySize; index ++ )
       
   709         {
       
   710         if ( iLabel )
       
   711             {
       
   712             delete iLabel[index];
       
   713             }
       
   714         if ( iKeyId )
       
   715             {
       
   716             delete iKeyId[index];
       
   717             }
       
   718         if ( iCAId )
       
   719             {
       
   720             delete iCAId[index];
       
   721             }
       
   722         if ( iIssuerHash )
       
   723             {
       
   724             delete iIssuerHash[index];
       
   725             }
       
   726         if ( iLabelPtr )
       
   727             {
       
   728             delete iLabelPtr[index];
       
   729             }
       
   730         if ( iKeyIdPtr )
       
   731             {
       
   732             delete iKeyIdPtr[index];
       
   733             }
       
   734         if ( iCAIdPtr )
       
   735             {
       
   736             delete iCAIdPtr[index];
       
   737             }
       
   738         if ( iIssuerHashPtr )
       
   739             {
       
   740             delete iIssuerHashPtr[index];
       
   741             }
       
   742         }
       
   743     
       
   744     delete iLabel;   
       
   745     delete iKeyId;                
       
   746     delete iCAId;
       
   747     delete iIssuerHash; 
       
   748     delete iLabelPtr;        
       
   749     delete iKeyIdPtr;        
       
   750     delete iCAIdPtr;        
       
   751     delete iIssuerHashPtr;  
       
   752     iLabel = NULL;
       
   753     iKeyId  = NULL;
       
   754     iCAId = NULL;
       
   755     iIssuerHash = NULL;
       
   756     iLabelPtr = NULL;
       
   757     iKeyIdPtr = NULL;
       
   758     iCAIdPtr = NULL;
       
   759     iIssuerHashPtr = NULL;
       
   760     }
       
   761 
       
   762 // -----------------------------------------------------------------------------
       
   763 // CWimCertUtil::CertCount
       
   764 // Get count of certificate in WIM
       
   765 // -----------------------------------------------------------------------------
       
   766 //
       
   767 TUint8 CWimCertUtil::CertCount( TWimEntryType aType )
       
   768     {
       
   769     _WIMTRACE( _L( "WimServer|CWimCertUtil::DeallocWimCertInfo()" ) ); 
       
   770     WIMI_STAT callStatus = WIMI_Ok;
       
   771     
       
   772     WIMI_Ref_t* wimRef = NULL;
       
   773     TUint8 certCount = 0;
       
   774 
       
   775     wimRef = WIMI_GetWIMRef( 0 );
       
   776 
       
   777     if ( wimRef )
       
   778         {
       
   779         if ( aType == EWimEntryTypeAll || aType == EWimEntryTypeCA )
       
   780             {
       
   781             callStatus = GetCertificateCountByWIM( wimRef, 
       
   782                                                    certCount, 
       
   783                                                    WIMI_CU_CA );            
       
   784             }
       
   785 
       
   786         if ( callStatus == WIMI_Ok && ( aType == EWimEntryTypeAll || 
       
   787                                         aType == EWimEntryTypePersonal ) )
       
   788             {
       
   789             callStatus = GetCertificateCountByWIM( wimRef, 
       
   790                                                    certCount, 
       
   791                                                    WIMI_CU_Client );
       
   792             }
       
   793 
       
   794         free_WIMI_Ref_t( wimRef );
       
   795         }
       
   796     else
       
   797         {
       
   798         certCount = 0;
       
   799         }
       
   800 
       
   801     return certCount;
       
   802     }
       
   803 
       
   804 // -----------------------------------------------------------------------------
       
   805 // CWimCertUtil::CertRefLstL
       
   806 // 
       
   807 // -----------------------------------------------------------------------------
       
   808 //
       
   809 void CWimCertUtil::CertRefLstL(
       
   810     TCertificateAddressList aCertAddrLst, 
       
   811     TWimCertInfo* aCertInfoArr, 
       
   812     TWimEntryType aCertEntryType )
       
   813     {
       
   814     
       
   815     WIMI_STAT callStatus = WIMI_Ok;
       
   816     TUint8 certNum = 0;
       
   817     WIMI_Ref_t* wimRef = NULL;
       
   818 
       
   819     wimRef = WIMI_GetWIMRef( 0 );
       
   820 
       
   821     if ( wimRef )
       
   822         {
       
   823         CleanupPushWimRefL( wimRef );
       
   824 
       
   825         if ( aCertEntryType == EWimEntryTypeAll || 
       
   826              aCertEntryType == EWimEntryTypeCA )
       
   827             {
       
   828             callStatus = GetCertificateFromWimRefL( wimRef, 
       
   829                                                     WIMI_CU_CA,
       
   830                                                     certNum,
       
   831                                                     aCertAddrLst,
       
   832                                                     aCertInfoArr );
       
   833             }
       
   834         if ( callStatus == WIMI_Ok && 
       
   835             ( aCertEntryType == EWimEntryTypeAll || 
       
   836               aCertEntryType == EWimEntryTypePersonal ) )
       
   837             {
       
   838             callStatus = GetCertificateFromWimRefL( wimRef,
       
   839                                                     WIMI_CU_Client,
       
   840                                                     certNum,
       
   841                                                     aCertAddrLst,
       
   842                                                     aCertInfoArr );
       
   843             }
       
   844 
       
   845         CleanupStack::PopAndDestroy( wimRef );
       
   846         }
       
   847     else
       
   848         {
       
   849         callStatus = WIMI_ERR_OutOfMemory;
       
   850         }
       
   851     }
       
   852 
       
   853 // -----------------------------------------------------------------------------
       
   854 // CWimCertUtil::GetCertExtrasL
       
   855 // 
       
   856 // -----------------------------------------------------------------------------
       
   857 //
       
   858 void CWimCertUtil::GetCertExtrasL(
       
   859     const TPtr8* aKeyId, 
       
   860     TCertExtrasInfo& aCertExtrasInfo,
       
   861     TUint aUsage,
       
   862     TRequestStatus& aStatus )
       
   863     {
       
   864     WIMI_STAT callStatus = WIMI_Ok;
       
   865     TInt8 certUsage = 0;
       
   866 
       
   867     TPtrC8 keyID( aKeyId->Ptr(), aKeyId->Length() );
       
   868 
       
   869     WIMI_Ref_t* wimTempRef = WIMI_GetWIMRef( 0 );
       
   870 
       
   871     if ( wimTempRef )
       
   872         {
       
   873         CleanupPushWimRefL( wimTempRef );
       
   874 
       
   875         switch ( aUsage )
       
   876             {
       
   877             case EWimEntryTypeCA:
       
   878                 {
       
   879                 certUsage = WIMI_CU_CA;
       
   880                 break;
       
   881                 }
       
   882             case EWimEntryTypeAll: //Flow through
       
   883             case EWimEntryTypePersonal:
       
   884                 {
       
   885                 certUsage = WIMI_CU_Client;
       
   886                 break;
       
   887                 }
       
   888             default:
       
   889                 {
       
   890                 callStatus = WIMI_ERR_BadParameters;
       
   891                 break;
       
   892                 }
       
   893             }
       
   894 
       
   895         if ( callStatus == WIMI_Ok )
       
   896             {
       
   897             callStatus = GetExtrasFromWimRefL( wimTempRef,
       
   898                                                certUsage,
       
   899                                                keyID,
       
   900                                                aCertExtrasInfo );
       
   901             }
       
   902 
       
   903         CleanupStack::PopAndDestroy( wimTempRef );
       
   904         }
       
   905     else
       
   906         {
       
   907         callStatus = WIMI_ERR_BadReference;
       
   908         }
       
   909 
       
   910     TRequestStatus* status = &aStatus;
       
   911     User::RequestComplete( status, iWimUtilFuncs->MapWIMError( callStatus ) );
       
   912     }
       
   913 
       
   914 // -----------------------------------------------------------------------------
       
   915 // CWimCertUtil::CertDetailsL
       
   916 // 
       
   917 // -----------------------------------------------------------------------------
       
   918 //
       
   919 void CWimCertUtil::CertDetailsL(
       
   920     const TCertificateAddress aCertAddr, 
       
   921     TWimCertDetails& aWimCertDetails )
       
   922     {
       
   923     WIMI_STAT callStatus = WIMI_Ok;
       
   924     WIMI_TransactId_t trId = ( void* )EInitializationCertListHashing;
       
   925 
       
   926     WIMI_BinData_t ptCertData;
       
   927     ptCertData.pb_buf = NULL;
       
   928     ptCertData.ui_buf_length = 0;
       
   929 
       
   930     callStatus = WIMI_CertificateReqT( trId, ( void* )aCertAddr, &ptCertData );
       
   931 
       
   932     if ( callStatus == WIMI_Ok )
       
   933         {
       
   934         // Now we have certificate data, copy it to allocated buffer
       
   935         if( aWimCertDetails.iCert->MaxLength() < ptCertData.ui_buf_length )
       
   936             {
       
   937             WSL_OS_Free( ptCertData.pb_buf );
       
   938             User::Leave( KErrOverflow );
       
   939         	}
       
   940         aWimCertDetails.iCert->Copy( ptCertData.pb_buf,
       
   941                                      ptCertData.ui_buf_length );
       
   942         WSL_OS_Free( ptCertData.pb_buf );
       
   943         }
       
   944     }
       
   945 
       
   946 // -----------------------------------------------------------------------------
       
   947 // CWimCertUtil::GetCertificateCountByWIM
       
   948 // Fetches count of certicates in certain WIM card.
       
   949 // -----------------------------------------------------------------------------
       
   950 //
       
   951 WIMI_STAT CWimCertUtil::GetCertificateCountByWIM(
       
   952     WIMI_Ref_t* aRef, 
       
   953     TUint8& aCertCount, 
       
   954     TUint8 aUsage ) const
       
   955     {
       
   956     _WIMTRACE(_L("WIM | WIMServer | CWimCertUtil::GetCertificateCountByWIM | Begin"));
       
   957     TUint8 certNum = 0;
       
   958     WIMI_RefList_t refList ;
       
   959     WIMI_STAT callStatus = WIMI_GetCertificateListByWIM( aRef, 
       
   960                                                          aUsage,
       
   961                                                          &certNum, 
       
   962                                                          &refList );
       
   963     if ( callStatus == WIMI_Ok )
       
   964         {
       
   965         aCertCount = ( TUint8 )( aCertCount + certNum );
       
   966         }
       
   967     free_WIMI_RefList_t( refList );
       
   968     return callStatus;
       
   969     }
       
   970 
       
   971 // -----------------------------------------------------------------------------
       
   972 // CWimCertHandler::GetExtrasFromWimRefL
       
   973 // Fetches extra information (e.g. certs trusted usage) from the WIM card.
       
   974 // -----------------------------------------------------------------------------
       
   975 //
       
   976 WIMI_STAT CWimCertUtil::GetExtrasFromWimRefL(
       
   977     WIMI_Ref_t* aTmpWimRef,
       
   978     TInt8 aUsage,
       
   979     TDesC8& aKeyHash,
       
   980     TCertExtrasInfo& aCertExtrasInfo ) const
       
   981     {
       
   982     _WIMTRACE(_L("WIM | WIMServer | CWimCertHandler::GetExtrasFromWimRefL | Begin"));
       
   983   
       
   984     TUint8 tempCertCount = 0;
       
   985     WIMI_RefList_t certRefList = NULL;
       
   986     WIMI_STAT callStatus = WIMI_Ok;
       
   987     TInt certIndex = 0;
       
   988     TPtrC8 keyHash;
       
   989 
       
   990     if ( aTmpWimRef )
       
   991         {
       
   992         // List all certificates (by WIM and usage)
       
   993         callStatus = WIMI_GetCertificateListByWIM( aTmpWimRef, 
       
   994                                                    aUsage, 
       
   995                                                    &tempCertCount, 
       
   996                                                    &certRefList );
       
   997         }
       
   998     else
       
   999         {
       
  1000         callStatus = WIMI_ERR_BadReference;
       
  1001         }
       
  1002     
       
  1003     if ( callStatus == WIMI_Ok )
       
  1004         {
       
  1005         CleanupPushWimRefListL( certRefList );
       
  1006 
       
  1007         WIMI_Ref_t* tempRef = NULL;
       
  1008         WIMI_BinData_t ptLabel;
       
  1009         WIMI_BinData_t ptKeyID;
       
  1010         WIMI_BinData_t ptCAID;
       
  1011         WIMI_BinData_t ptIssuerHash;
       
  1012         WIMI_BinData_t ptTrustedUsage;
       
  1013         TUint8 uiCDFRefs;
       
  1014         TUint8 usage;
       
  1015         TUint8 certType;
       
  1016         TUint16 certLen;
       
  1017         TUint8 modifiable = 0;
       
  1018 
       
  1019         for ( TInt i = 0; i < tempCertCount; i++ )
       
  1020             {
       
  1021             // Get info for each certificate until we find valid cert
       
  1022             callStatus = WIMI_GetCertificateInfo( certRefList[i],
       
  1023                                                   &tempRef,
       
  1024                                                   &ptLabel,
       
  1025                                                   &ptKeyID,
       
  1026                                                   &ptCAID,
       
  1027                                                   &ptIssuerHash,
       
  1028                                                   &ptTrustedUsage,
       
  1029                                                   &uiCDFRefs,
       
  1030                                                   &usage,
       
  1031                                                   &certType,   
       
  1032                                                   &certLen,
       
  1033                                                   &modifiable );
       
  1034             if ( callStatus == WIMI_Ok )
       
  1035                 {
       
  1036                 WSL_OS_Free( ptLabel.pb_buf );
       
  1037                 WSL_OS_Free( ptCAID.pb_buf );
       
  1038                 WSL_OS_Free( ptIssuerHash.pb_buf );
       
  1039                 WSL_OS_Free( ptTrustedUsage.pb_buf );
       
  1040                 free_WIMI_Ref_t( tempRef );
       
  1041 
       
  1042                 keyHash.Set( ptKeyID.pb_buf, ptKeyID.ui_buf_length );
       
  1043 
       
  1044                 // Compare given and fetched key hash
       
  1045                 if ( keyHash.Compare( aKeyHash ) == 0 &&
       
  1046                      certType == WIMI_CT_X509 ) //Match
       
  1047                     {
       
  1048                     certIndex = i; // Found one
       
  1049                     i = tempCertCount; // Stop looping
       
  1050                     callStatus = WIMI_Ok;
       
  1051                     }
       
  1052                 else // Cert not supported
       
  1053                     {
       
  1054                     callStatus = WIMI_ERR_UnsupportedCertificate;
       
  1055                     }
       
  1056 
       
  1057                 WSL_OS_Free( ptKeyID.pb_buf );
       
  1058                 }
       
  1059             }
       
  1060 
       
  1061         if ( callStatus == WIMI_Ok )
       
  1062             {
       
  1063             CopyCertExtrasInfoL( certRefList[certIndex], aCertExtrasInfo );
       
  1064             }
       
  1065         CleanupStack::PopAndDestroy( certRefList );
       
  1066         }
       
  1067 
       
  1068     return callStatus;
       
  1069     }
       
  1070 
       
  1071 // -----------------------------------------------------------------------------
       
  1072 // CWimCertHandler::CopyCertExtrasInfoL
       
  1073 // Copies certs extra information to client's allocated structure.
       
  1074 // -----------------------------------------------------------------------------
       
  1075 //
       
  1076 void CWimCertUtil::CopyCertExtrasInfoL(
       
  1077     WIMI_Ref_t* aCert,
       
  1078     TCertExtrasInfo& aCertExtrasInfo ) const
       
  1079     {
       
  1080     _WIMTRACE(_L("WIM | WIMServer | CWimCertHandler::CopyCertExtrasInfoL | Begin"));
       
  1081     WIMI_Ref_t* tempref = NULL;
       
  1082     WIMI_BinData_t ptLabel;
       
  1083     WIMI_BinData_t ptKeyID;
       
  1084     WIMI_BinData_t ptCAID;
       
  1085     WIMI_BinData_t ptIssuerHash;
       
  1086     WIMI_BinData_t ptTrustedUsage;
       
  1087     TUint8 uiCDFRefs;
       
  1088     TUint8 usage;
       
  1089     TUint8 type;
       
  1090     TUint16 certlen;
       
  1091     TUint8 modifiable = 0;
       
  1092     TBool found = ETrue;
       
  1093 
       
  1094     WIMI_STAT callStatus = WIMI_GetCertificateInfo( 
       
  1095                                 aCert,
       
  1096                                 &tempref,
       
  1097                                 &ptLabel,
       
  1098                                 &ptKeyID, /* Key Id (hash)*/
       
  1099                                 &ptCAID,
       
  1100                                 &ptIssuerHash,
       
  1101                                 &ptTrustedUsage,
       
  1102                                 &uiCDFRefs,
       
  1103                                 &usage,  /* 0 = client, 1 = CA */
       
  1104                                 &type,   
       
  1105                                 &certlen,   /* cert. content or URL length */
       
  1106                                 &modifiable); 
       
  1107     if ( callStatus == WIMI_Ok )
       
  1108         {
       
  1109         free_WIMI_Ref_t( tempref );
       
  1110         WSL_OS_Free( ptLabel.pb_buf );
       
  1111         WSL_OS_Free( ptKeyID.pb_buf );
       
  1112         WSL_OS_Free( ptCAID.pb_buf );
       
  1113         WSL_OS_Free( ptIssuerHash.pb_buf );
       
  1114 
       
  1115         CleanupPushWimBufL( ptTrustedUsage );
       
  1116 
       
  1117         TPtrC8 undecodedUsage;
       
  1118         undecodedUsage.Set( ptTrustedUsage.pb_buf ); 
       
  1119 
       
  1120         if ( ptTrustedUsage.ui_buf_length == 0 ) // No OIDs
       
  1121             {
       
  1122             found = EFalse;
       
  1123             }
       
  1124 
       
  1125         // DECODE OIDs
       
  1126         TASN1DecObjectIdentifier decoder;
       
  1127         RPointerArray<HBufC> decodedOIDArray;
       
  1128         HBufC* decodedOIDs = NULL;
       
  1129         TInt oidsLength = 0;    // length of total OID buffer
       
  1130         TInt err;
       
  1131 
       
  1132         for ( TInt position = 0; found; )   //Loop until no OIDs found anymore
       
  1133             {
       
  1134             if ( undecodedUsage.Length() > position ) //Don't go over buffer
       
  1135                 {
       
  1136                 TRAP( err, decodedOIDs = decoder.DecodeDERL( undecodedUsage, position ) );
       
  1137                 if ( err == KErrNone ) //Found OID
       
  1138                     {
       
  1139                     CleanupStack::PushL( decodedOIDs );
       
  1140                     if ( decodedOIDs->Length() )
       
  1141                         {
       
  1142                         found = ETrue;
       
  1143                         decodedOIDArray.AppendL( decodedOIDs );
       
  1144                         oidsLength += decodedOIDs->Length();
       
  1145                         CleanupStack::Pop( decodedOIDs );
       
  1146                         }
       
  1147                     else    // Not found OID from buffer
       
  1148                         {
       
  1149                         found = EFalse;
       
  1150                         CleanupStack::PopAndDestroy( decodedOIDs );
       
  1151                         }
       
  1152                     decodedOIDs = NULL;
       
  1153                     }
       
  1154                 else    // Error in OID parsing -> Not found OID
       
  1155                     {
       
  1156                     found = EFalse;
       
  1157                     }
       
  1158                 }
       
  1159             else    // undecoded OID buffer seeked through
       
  1160                 {
       
  1161                 found = EFalse;
       
  1162                 }
       
  1163             }
       
  1164 
       
  1165         _LIT( KDelimeter16, " " ); //Delimeter between OIDs
       
  1166 
       
  1167         if ( oidsLength > 0 ) // OID's found
       
  1168             {
       
  1169             // Add OID's to one buffer from separate buffers
       
  1170             for ( TInt i = 0; i < decodedOIDArray.Count(); i++ )
       
  1171                 {
       
  1172                 if ( i == 0 ) //First one
       
  1173                     {
       
  1174                     aCertExtrasInfo.iTrustedUsage->Copy( decodedOIDArray[i]->Des() );
       
  1175                     }
       
  1176                 else // Append other OIDs, with delimeter
       
  1177                     {
       
  1178                     aCertExtrasInfo.iTrustedUsage->Append( KDelimeter16 );
       
  1179                     aCertExtrasInfo.iTrustedUsage->Append( decodedOIDArray[i]->Des() );
       
  1180                     }
       
  1181                 }
       
  1182             }
       
  1183 
       
  1184         aCertExtrasInfo.iCDFRefs = iWimUtilFuncs->MapCertLocation( uiCDFRefs );
       
  1185         
       
  1186         // Free the memory
       
  1187         decodedOIDArray.ResetAndDestroy();
       
  1188         CleanupStack::PopAndDestroy( ptTrustedUsage.pb_buf );
       
  1189         }
       
  1190     }
       
  1191 
       
  1192 // -----------------------------------------------------------------------------
       
  1193 // CWimCertHandler::GetCertificateFromWimRefL
       
  1194 // Fetches certificate from the WIM card.
       
  1195 // -----------------------------------------------------------------------------
       
  1196 //
       
  1197 WIMI_STAT CWimCertUtil::GetCertificateFromWimRefL(
       
  1198     WIMI_Ref_t* aTmpWimRef,
       
  1199     TInt8 aUsage,
       
  1200     TUint8& aCertNum,
       
  1201     TUint32* aCertRefLst,
       
  1202     TWimCertInfo* aCertInfoLst )
       
  1203     {
       
  1204     _WIMTRACE(_L("WIM | WIMServer | CWimCertHandler::GetCertificateFromWimRefL | Begin"));
       
  1205     TUint8 tempCrtCount;
       
  1206     WIMI_RefList_t refList ;
       
  1207     WIMI_STAT callStatus = WIMI_Ok;
       
  1208 
       
  1209     if ( aTmpWimRef )
       
  1210         {
       
  1211         callStatus = WIMI_GetCertificateListByWIM( aTmpWimRef, 
       
  1212                                                    aUsage, 
       
  1213                                                    &tempCrtCount, 
       
  1214                                                    &refList );
       
  1215         if ( callStatus == WIMI_Ok )
       
  1216             {
       
  1217             for ( TUint8 certIndex = 0; certIndex < tempCrtCount; certIndex++ )
       
  1218                 {
       
  1219                 TInt current = aCertNum + certIndex;
       
  1220                 _WIMTRACE2( _L( "CWimCertHandler::GetCertificateFromWimRefL, +ref 0x%08x" ),
       
  1221                         refList[certIndex] );
       
  1222                 // transfers ownership of the refList item to aCertRefLst
       
  1223                 aCertRefLst[current] = reinterpret_cast< TUint32 >( refList[certIndex] );
       
  1224                 CopyCertificateInfo( aCertInfoLst[current], refList[certIndex] );
       
  1225                 }
       
  1226             aCertNum = static_cast< TUint8 >( aCertNum + tempCrtCount );
       
  1227 
       
  1228             // Because list items are moved to aCertRefLst, only refList array
       
  1229             // needs to be freed. Cannot use free_WIMI_RefList_t() as it would
       
  1230             // delete also items contained in refList.
       
  1231             WSL_OS_Free( refList );
       
  1232             }
       
  1233         }
       
  1234     return callStatus;
       
  1235     }
       
  1236 
       
  1237 // -----------------------------------------------------------------------------
       
  1238 // CWimCertHandler::CopyCertificateInfo
       
  1239 // Copies certificate information to client's allocated memory area.
       
  1240 // -----------------------------------------------------------------------------
       
  1241 //
       
  1242 void CWimCertUtil::CopyCertificateInfo(
       
  1243     TWimCertInfo& aCertInfo,
       
  1244     WIMI_Ref_t* aCert )
       
  1245     {
       
  1246     _WIMTRACE(_L("WIM | WIMServer | CWimCertHandler::CopyCertificateInfoL | Begin"));
       
  1247 
       
  1248     WIMI_Ref_t* tempRef = NULL;
       
  1249     WIMI_BinData_t ptLabel;
       
  1250     WIMI_BinData_t ptKeyID;
       
  1251     WIMI_BinData_t ptCAID;
       
  1252     WIMI_BinData_t ptIssuerHash;
       
  1253     WIMI_BinData_t ptTrustedUsage;
       
  1254     TUint8 uiCDFRefs;
       
  1255     TUint8 usage;
       
  1256     TUint8 type;
       
  1257     TUint16 certLen;
       
  1258     TUint8 modifiable = 0;
       
  1259     WIMI_STAT callStatus = WIMI_GetCertificateInfo( aCert,
       
  1260                                                     &tempRef,
       
  1261                                                     &ptLabel,
       
  1262                                                     &ptKeyID,
       
  1263                                                     &ptCAID,
       
  1264                                                     &ptIssuerHash,
       
  1265                                                     &ptTrustedUsage,
       
  1266                                                     &uiCDFRefs,
       
  1267                                                     &usage,
       
  1268                                                     &type,
       
  1269                                                     &certLen, 
       
  1270                                                     &modifiable );
       
  1271 
       
  1272     if ( callStatus == WIMI_Ok )
       
  1273         {
       
  1274         free_WIMI_Ref_t( tempRef );
       
  1275 
       
  1276         aCertInfo.iLabel.Copy( TPtr8( ptLabel.pb_buf,
       
  1277                 ptLabel.ui_buf_length,
       
  1278                 ptLabel.ui_buf_length ) );
       
  1279 
       
  1280         aCertInfo.iKeyId.Copy( TPtr8( ptKeyID.pb_buf,
       
  1281                 ptKeyID.ui_buf_length,
       
  1282                 ptKeyID.ui_buf_length ) );
       
  1283         
       
  1284         aCertInfo.iCAId.Copy( TPtr8( ptCAID.pb_buf,
       
  1285                 ptCAID.ui_buf_length,
       
  1286                 ptCAID.ui_buf_length ) );
       
  1287         
       
  1288         aCertInfo.iIssuerHash.Copy( TPtr8( ptIssuerHash.pb_buf,
       
  1289                 ptIssuerHash.ui_buf_length,
       
  1290                 ptIssuerHash.ui_buf_length ) );
       
  1291 
       
  1292         aCertInfo.iUsage = usage;
       
  1293         aCertInfo.iType = type;
       
  1294         aCertInfo.iCertlen = certLen;
       
  1295         aCertInfo.iModifiable = modifiable;
       
  1296         
       
  1297         // Certificate location
       
  1298         aCertInfo.iCDFRefs = iWimUtilFuncs->MapCertLocation( uiCDFRefs );
       
  1299 
       
  1300         //Allocate enough memory for OID
       
  1301         aCertInfo.iTrustedUsageLength = ptTrustedUsage.ui_buf_length * 6;
       
  1302 
       
  1303         WSL_OS_Free( ptLabel.pb_buf );
       
  1304         WSL_OS_Free( ptKeyID.pb_buf );
       
  1305         WSL_OS_Free( ptCAID.pb_buf );
       
  1306         WSL_OS_Free( ptIssuerHash.pb_buf );
       
  1307         WSL_OS_Free( ptTrustedUsage.pb_buf );
       
  1308         }
       
  1309     }
       
  1310 
       
  1311 // End of File