webservices/wsidentitymanager/src/senbaseidentity.cpp
changeset 0 62f9d29f7211
equal deleted inserted replaced
-1:000000000000 0:62f9d29f7211
       
     1 /*
       
     2 * Copyright (c) 2002-2005 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:        
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 
       
    22 
       
    23 
       
    24 
       
    25 // INCLUDE FILES
       
    26 #include <SenXmlElement.h>
       
    27 #include <SenBaseFragment.h>
       
    28 #include <SenXmlUtils.h>
       
    29 #include <SenServiceConnection.h> // WSF error codes
       
    30 
       
    31 #include "senbaseidentity.h"
       
    32 #include "senwiseidentityprovider.h"
       
    33 #include "sendebug.h" // internal Utils\inc - filelogging MACROs
       
    34 #ifdef __CRYPTO_HW__
       
    35 #include "sencryptoutils.h"
       
    36 #include <SecEnv.h>
       
    37 #include <SecEnvDevice.h>
       
    38 #include <SecEnvType.h>
       
    39 #include <SecEnvMgr.h>
       
    40 const TInt KCryptoPADataLen     = 16;
       
    41 const TInt KCryptoPACipherLen   = 48;
       
    42 const TInt KPaCryptAuthLen     = 32;
       
    43 const TInt KEncryptNoSalt = 3;
       
    44 const TInt KDecrypt       = 2;
       
    45 const TUint KPadding = 0;
       
    46 const TUint KEncrypedLen = 128;
       
    47 _LIT8(KCryptoPA, "PA_CRYPT");
       
    48 
       
    49 #endif // __CRYPTO_HW__
       
    50 
       
    51 EXPORT_C CSenBaseIdentity* CSenBaseIdentity::NewL(TPtrC8 aIMEI)
       
    52     {
       
    53     CSenBaseIdentity* pNew = NewLC(aIMEI);
       
    54     CleanupStack::Pop();
       
    55     return(pNew) ;
       
    56     }
       
    57 EXPORT_C CSenBaseIdentity* CSenBaseIdentity::NewLC(TPtrC8 aIMEI)
       
    58     {
       
    59     CSenBaseIdentity* pNew = new (ELeave) CSenBaseIdentity(aIMEI);
       
    60     CleanupStack::PushL(pNew);
       
    61     pNew->ConstructL();
       
    62     return pNew;
       
    63     }
       
    64 CSenBaseIdentity::CSenBaseIdentity(TPtrC8 aIMEI)
       
    65 :   iProvider(NULL),
       
    66     iIMEI(aIMEI)
       
    67     {
       
    68     }
       
    69 
       
    70 void CSenBaseIdentity::ConstructL()
       
    71     {
       
    72     BaseConstructL( KIdentityElementLocalName() );
       
    73     }
       
    74 
       
    75 
       
    76 CSenIdentityProvider* CSenBaseIdentity::IdentityProviderL(MSenServiceDescription& aService )
       
    77     {
       
    78 
       
    79     TInt idpCount(iProviderList.Count());
       
    80 
       
    81     CSenIdentityProvider* pIdp = NULL;
       
    82     CSenIdentityProvider* pCandidate = NULL;
       
    83 
       
    84     for (TInt i=0; i<idpCount; i++)
       
    85         {
       
    86         pCandidate = (CSenIdentityProvider*)iProviderList[i];
       
    87         if(pCandidate->IsTrustedByL(aService))
       
    88             {
       
    89             pIdp = pCandidate;
       
    90             pCandidate = NULL;
       
    91             }
       
    92         if(pIdp == NULL && pCandidate->IsDefault()) 
       
    93             {
       
    94             pIdp = pCandidate;
       
    95             }
       
    96 
       
    97         if(pIdp)
       
    98             {
       
    99             break;
       
   100             }
       
   101         }
       
   102     return pIdp;
       
   103     }
       
   104 
       
   105 CSenIdentityProvider* CSenBaseIdentity::IdentityProviderL(const TDesC8& aURI )
       
   106     {
       
   107     TInt idpCount(iProviderList.Count());
       
   108     CSenIdentityProvider* retVal = NULL;
       
   109     for (TInt i=0; i<idpCount; i++)
       
   110         {
       
   111             retVal = (CSenIdentityProvider*)iProviderList[i];
       
   112             if (retVal->Endpoint() == aURI)
       
   113                 {
       
   114                 break;
       
   115                 }
       
   116             else
       
   117                 {
       
   118                 retVal = NULL;
       
   119                 }
       
   120         }
       
   121     return retVal;
       
   122     }
       
   123 
       
   124 CSenIdentityProvider* CSenBaseIdentity::IdentityProviderL(const CDesC8Array& aIdpIdList,
       
   125                                                           TBool aStrict )
       
   126     {
       
   127     TInt idpCount(iProviderList.Count());
       
   128     TInt patternCount(aIdpIdList.Count());
       
   129 
       
   130     if(patternCount==0)
       
   131         {
       
   132         if(aStrict)
       
   133             {
       
   134             return NULL;
       
   135             }
       
   136         else
       
   137             {
       
   138             return IdentityProviderL(); // return the default provider
       
   139             }
       
   140         }
       
   141 
       
   142     // to optimize the search, store the default idp
       
   143     CSenIdentityProvider* pDefault = NULL;
       
   144 
       
   145     for (TInt i=0; i<idpCount; i++)
       
   146         {
       
   147         for(TInt j=0; j<patternCount; j++)
       
   148             {
       
   149             TPtrC8 providerID = ((CSenIdentityProvider*)iProviderList[i])->ProviderID();
       
   150             if(providerID.Length()>0 && providerID == aIdpIdList[j])
       
   151                 {
       
   152                 return (CSenIdentityProvider*)iProviderList[i];
       
   153                 }
       
   154             }
       
   155         if(!aStrict)
       
   156             {
       
   157             // to optimize the search, store the default idp, if found
       
   158             if(((CSenIdentityProvider*)iProviderList[i])->IsDefault())
       
   159                 {
       
   160                 pDefault = (CSenIdentityProvider*)iProviderList[i];
       
   161                 }
       
   162             }
       
   163         }
       
   164     if(aStrict)
       
   165         {
       
   166         // default idp does not meet the search requirements
       
   167         // (not in strict pattern list)
       
   168         return NULL;
       
   169         }
       
   170     else
       
   171         {
       
   172         // if current item did not match with anything in the
       
   173         // pattern list, check if it's ok in non-strict perspective
       
   174         return pDefault;
       
   175         }
       
   176     }
       
   177 
       
   178 CSenIdentityProvider* CSenBaseIdentity::IdentityProviderL(MSenServiceDescription& aService,
       
   179                                                           const CDesC8Array& aIdpIdList,
       
   180                                                           TBool aStrict)
       
   181     {
       
   182     TInt idpCount(iProviderList.Count());
       
   183     TInt patternCount(aIdpIdList.Count());
       
   184 
       
   185     if(patternCount==0)
       
   186         {
       
   187         if(aStrict)
       
   188             {
       
   189             return NULL;
       
   190             }
       
   191         else
       
   192             {
       
   193             return IdentityProviderL(); // return the default provider
       
   194             }
       
   195         }
       
   196 
       
   197     // to optimize the search, store the default idp
       
   198     CSenIdentityProvider* pDefault = NULL;
       
   199     CSenIdentityProvider* pTrusted = NULL;
       
   200 
       
   201     for (TInt i=0; i<idpCount; i++)
       
   202         {
       
   203         for(TInt j=0; j<patternCount; j++)
       
   204             {
       
   205             TPtrC8 providerID = 
       
   206                 ((CSenIdentityProvider*)iProviderList[i])->ProviderID();
       
   207             if(providerID.Length()>0 && providerID == aIdpIdList[j])
       
   208                 {
       
   209                 if (((CSenIdentityProvider*)iProviderList[i])->IsTrustedByL(aService))
       
   210                     {
       
   211                     return (CSenIdentityProvider*)iProviderList[i];
       
   212                     }
       
   213                 }
       
   214             }
       
   215 
       
   216         if(!aStrict)
       
   217             {
       
   218             if (!pTrusted)
       
   219                 {
       
   220                 if (((CSenIdentityProvider*)iProviderList[i])->IsTrustedByL(aService))
       
   221                     {
       
   222                     pTrusted = (CSenIdentityProvider*)iProviderList[i];
       
   223                     }
       
   224                 }
       
   225 
       
   226             // to optimize the search, store the default IDP, if found
       
   227             if(((CSenIdentityProvider*)iProviderList[i])->IsDefault())
       
   228                 {
       
   229                 pDefault = (CSenIdentityProvider*)iProviderList[i];
       
   230                 }
       
   231             }
       
   232         }
       
   233 
       
   234 
       
   235     if(aStrict)
       
   236         {
       
   237         // default idp does not meet the search requirements
       
   238         // (not in strict pattern list)
       
   239         return NULL;
       
   240         }
       
   241     else
       
   242         {
       
   243         // if current item did not match with anything in the
       
   244         // pattern list, check if it's ok in non-strict perspective
       
   245         if (pTrusted)
       
   246             {
       
   247             return pTrusted;
       
   248             }
       
   249         else
       
   250             {
       
   251             return pDefault;
       
   252             }
       
   253         }
       
   254     }
       
   255 
       
   256 CSenIdentityProvider* CSenBaseIdentity::IdentityProviderL()
       
   257     {
       
   258     TInt idpCount(iProviderList.Count());
       
   259 
       
   260     CSenIdentityProvider* pDefaultIdp = NULL;
       
   261 
       
   262     for (TInt i=0; i<idpCount && !pDefaultIdp; i++)
       
   263         {
       
   264         pDefaultIdp = (CSenIdentityProvider*)iProviderList[i];
       
   265         if (!pDefaultIdp->IsDefault()) pDefaultIdp = NULL;
       
   266         }
       
   267 
       
   268     if (!pDefaultIdp && idpCount > 0)
       
   269         {
       
   270         // use the first IdentityProvider
       
   271         pDefaultIdp = (CSenIdentityProvider*)iProviderList[0];
       
   272         }
       
   273 
       
   274     return pDefaultIdp;
       
   275     }
       
   276 const RIdentityProviderList& CSenBaseIdentity::IdentityProvidersL() const
       
   277     {
       
   278     return iProviderList;
       
   279     }
       
   280 
       
   281 // NOTE: takes ownership of the given apIdentityProvider
       
   282 TInt CSenBaseIdentity::RegisterIdentityProviderL(CSenIdentityProvider* apIdentityProvider )
       
   283     {
       
   284     //SENDEBUG_L("CSenBaseIdentity::RegisterIdentityProviderL(*)");
       
   285 
       
   286     if(!apIdentityProvider)
       
   287         {
       
   288         SENDEBUG_L("RegisterIdentityProviderL(*) - Illegal argument: NULL");
       
   289         return KErrArgument;
       
   290         }
       
   291         
       
   292     if (apIdentityProvider->Endpoint() == KNullDesC8)
       
   293         {
       
   294         SENDEBUG_L("RegisterIdentityProviderL(*) - Error: no Endpoint in IDP");
       
   295         //Delete the object since ownership is with us
       
   296         delete apIdentityProvider;		
       
   297         apIdentityProvider = NULL;        
       
   298         return KErrSenNoEndpoint;
       
   299         }
       
   300 
       
   301     TInt retVal(KErrNone);
       
   302 
       
   303     // Now check for duplicates:
       
   304     CSenIdentityProvider* pDuplicate = NULL;
       
   305 
       
   306     retVal = FindMatchingIdentityProviderL(*apIdentityProvider, pDuplicate);
       
   307     if(pDuplicate)
       
   308         {
       
   309         //SENDEBUG((_L("  Duplicate(s) found, updating the IdP!.")));
       
   310         RPointerArray<CSenElement> newElements =
       
   311                             apIdentityProvider->AsElement().ElementsL();
       
   312 
       
   313         TInt elemCount = newElements.Count();
       
   314         TInt i(0);
       
   315         // Checking the aIdp elements
       
   316         while ( i < elemCount )
       
   317             {
       
   318             CSenElement* elem = newElements[i];
       
   319             TPtrC8 localName = elem->LocalName();
       
   320 
       
   321             RPointerArray<CSenElement> pDuplicateAsElems =
       
   322                                     pDuplicate->AsElement().ElementsL();
       
   323             TInt duplicateCount = pDuplicateAsElems.Count();
       
   324             TBool found = EFalse;
       
   325             TBool foundServiceID = EFalse;
       
   326             TInt j(0);
       
   327 
       
   328             // Checking if the duplicate has the element found from aIdp
       
   329             // If found update it, if not found insert new element.
       
   330             while ( j < duplicateCount )
       
   331                 {
       
   332                 CSenElement* dupElem = pDuplicateAsElems[j];
       
   333                 if ( localName == dupElem->LocalName() )
       
   334                     {
       
   335                     if ( localName == KServiceIDElementLocalName )
       
   336                         {
       
   337                         if ( elem->Content() == dupElem->Content() )
       
   338                             {
       
   339                             foundServiceID = ETrue;
       
   340                             }
       
   341                         }
       
   342                     else
       
   343                         {
       
   344                         // note, this create the element if it
       
   345                         // is not found
       
   346                         if (elem->Content() != KNullDesC8)
       
   347                             {
       
   348                             dupElem->SetContentL( elem->Content() );
       
   349                             }
       
   350                         RPointerArray<CSenElement> nestedElems = elem->ElementsL();
       
   351                         for (TInt k = 0; k < nestedElems.Count(); k++)
       
   352                             {
       
   353                             CSenElement* dupNestEl = dupElem->Element(nestedElems[k]->LocalName());
       
   354                             if (dupNestEl)
       
   355                                 {
       
   356                                 dupNestEl->SetContentL(nestedElems[k]->Content());
       
   357                                 }
       
   358                             else
       
   359                                 {
       
   360                                 CSenElement* newEl = dupElem->CreateElementL(nestedElems[k]->NsPrefix(),
       
   361                                                         nestedElems[k]->LocalName());
       
   362                                 CleanupStack::PushL(newEl);
       
   363                                 dupElem->AddElementL(*newEl);
       
   364                                 CleanupStack::Pop(newEl);
       
   365                                 newEl->SetContentL(nestedElems[k]->Content());
       
   366                                 }
       
   367                             }
       
   368                         
       
   369                         //cleaning, if sth has been removed on backend
       
   370                         //nested duplicate - local
       
   371                         //nested - backend
       
   372                         RPointerArray<CSenElement> nestedDupElems = dupElem->ElementsL();
       
   373                         for (TInt k = 0; k < nestedDupElems.Count(); k++)
       
   374                             {
       
   375                             CSenElement* nestEl = elem->Element(nestedDupElems[k]->LocalName());
       
   376                             if (!nestEl)
       
   377                                 {
       
   378                                 nestedDupElems[k]->SetContentL(KNullDesC8);
       
   379                                 }
       
   380                             }
       
   381                                                 
       
   382                                                 
       
   383                         found = ETrue;
       
   384                         break;
       
   385                         }
       
   386                     }
       
   387                 j++;
       
   388                 }
       
   389             
       
   390             if ( !found && !foundServiceID && elem)
       
   391                 {
       
   392                 // this is new kind of element, and not in original
       
   393                 // idp, thus it needs to be copied because it is 
       
   394                 // owned by idp being registered (which will be
       
   395                 // deleted inside this method)
       
   396                 CSenElement* pCopy = 
       
   397                     CSenXmlElement::NewL( elem->LocalName() ); 
       
   398                 CleanupStack::PushL(pCopy);
       
   399                 // copy the contents of serviceid 
       
   400                 pCopy->CopyFromL(*elem);
       
   401                 // append the copy into original idp
       
   402                 pDuplicate->AsElement().AddElementL( *pCopy );
       
   403                 CleanupStack::Pop(); // pCopy
       
   404                 }
       
   405             i++;
       
   406             }
       
   407         // NOTE: ownership of the given argument apIdentityProvider
       
   408         // will NOT be TRANSFERRED to anywhere.
       
   409         // So: free the memory, contents copied and thus updated
       
   410         
       
   411         delete apIdentityProvider;
       
   412         apIdentityProvider = NULL;
       
   413         if(retVal > 0)
       
   414             retVal = KErrNone;
       
   415         }
       
   416     else if(retVal==KErrNotFound)
       
   417         {
       
   418         //SENDEBUG((_L("  No duplicate(s) found, will try to add..")));
       
   419         // Append into both provider list and as child element
       
   420         retVal = iProviderList.Append( apIdentityProvider );
       
   421         if(retVal==KErrNone)
       
   422             {
       
   423             // OWNERSHIP of apIdentityProvider is now here
       
   424             TInt leaveCode(KErrNone);
       
   425             TRAP( leaveCode, AsElement().AddElementL(apIdentityProvider->AsElement() ) );
       
   426             if(leaveCode!=KErrNone)
       
   427                 {
       
   428                 retVal = leaveCode;
       
   429                 // no need to free the apIdentityProvider, it will
       
   430                 // be destroyed in destructor as part of iProviderList
       
   431                 }                                    
       
   432             }
       
   433         else
       
   434             {
       
   435             // free the orphan apIdentityProvider
       
   436             delete apIdentityProvider;
       
   437             apIdentityProvider = NULL;
       
   438             }
       
   439         }
       
   440     else // some other error occurred: free the orphan argument
       
   441         {
       
   442         //SENDEBUG((_L("An error occured  and no duplicate(s) were found.")));
       
   443         delete apIdentityProvider;
       
   444         apIdentityProvider = NULL;
       
   445         }
       
   446     return retVal;
       
   447     }
       
   448     
       
   449 TInt CSenBaseIdentity::UpdateIdentityProviderL(CSenIdentityProvider& aIdentityProvider)
       
   450     {
       
   451     if (aIdentityProvider.Endpoint() == KNullDesC8)
       
   452         {
       
   453         SENDEBUG_L("RegisterIdentityProviderL(*) - Error: no Endpoint in IDP");
       
   454         return KErrSenNoEndpoint;
       
   455         }
       
   456 
       
   457     TInt retVal(KErrNone);
       
   458 
       
   459     // Now check for duplicates:
       
   460     CSenIdentityProvider* pDuplicate = NULL;
       
   461 
       
   462     retVal = FindMatchingIdentityProviderL(aIdentityProvider, pDuplicate);
       
   463     if(pDuplicate)
       
   464         {
       
   465         //SENDEBUG((_L("  Duplicate(s) found, updating the IdP!.")));
       
   466         RPointerArray<CSenElement> newElements =
       
   467                             aIdentityProvider.AsElement().ElementsL();
       
   468 
       
   469         TInt elemCount = newElements.Count();
       
   470         TInt i(0);
       
   471         // Checking the aIdp elements
       
   472         while ( i < elemCount )
       
   473             {
       
   474             CSenElement* elem = newElements[i];
       
   475             TPtrC8 localName = elem->LocalName();
       
   476 
       
   477             RPointerArray<CSenElement> pDuplicateAsElems =
       
   478                                     pDuplicate->AsElement().ElementsL();
       
   479             TInt duplicateCount = pDuplicateAsElems.Count();
       
   480             TBool found = EFalse;
       
   481             TBool foundServiceID = EFalse;
       
   482             TInt j(0);
       
   483 
       
   484             // Checking if the duplicate has the element found from aIdp
       
   485             // If found update it, if not found insert new element.
       
   486             while ( j < duplicateCount )
       
   487                 {
       
   488                 CSenElement* dupElem = pDuplicateAsElems[j];
       
   489                 if ( localName == dupElem->LocalName() )
       
   490                     {
       
   491                     if ( localName == KServiceIDElementLocalName )
       
   492                         {
       
   493                         if ( elem->Content() == dupElem->Content() )
       
   494                             {
       
   495                             foundServiceID = ETrue;
       
   496                             }
       
   497                         }
       
   498                     else
       
   499                         {
       
   500                         // note, this create the element if it
       
   501                         // is not found
       
   502                         dupElem->SetContentL( elem->Content() );
       
   503                         found = ETrue;
       
   504                         break;
       
   505                         }
       
   506                     }
       
   507                 j++;
       
   508                 }
       
   509             
       
   510             if ( !found && !foundServiceID && elem)
       
   511                 {
       
   512                 // this is new kind of element, and not in original
       
   513                 // idp, thus it needs to be copied because it is 
       
   514                 // owned by idp being registered (which will be
       
   515                 // deleted inside this method)
       
   516                 CSenElement* pCopy = 
       
   517                     CSenXmlElement::NewL( elem->LocalName() ); 
       
   518                 CleanupStack::PushL(pCopy);
       
   519                 // copy the contents of serviceid 
       
   520                 pCopy->CopyFromL(*elem);
       
   521                 // append the copy into original idp
       
   522                 pDuplicate->AsElement().AddElementL( *pCopy );
       
   523                 CleanupStack::Pop(); // pCopy
       
   524                 }
       
   525             i++;
       
   526             }
       
   527         if(retVal > 0)
       
   528             retVal = KErrNone;
       
   529         }
       
   530 
       
   531     return retVal;    
       
   532     }
       
   533 
       
   534 // Note: non-strict search is used when unregistering: it must be possible
       
   535 // to unregister a provider using only provider Id in cases where someone
       
   536 // is unable to update and IDP (because provider id was *already *in use).
       
   537 // This "someone", cannot figure out the endpoint -> so, only way to unregister
       
   538 // blocking IDP is to call unregister(ProviderID) and then register the IDP
       
   539 // using different ProviderID & endpoint pair.
       
   540 
       
   541 // more about this "non strict" search used by unregistering: to get a MATCH
       
   542 // usingof plain endpoint must be accepted ONLY if providerid was NOT in use
       
   543 // with basic web services.
       
   544 //
       
   545 
       
   546 // FOR ID-WSF framework, mainly
       
   547 TInt CSenBaseIdentity::FindMatchingIdentityProviderL(CSenIdentityProvider& aIdp,
       
   548                                                      CSenIdentityProvider*& apMatch)
       
   549     {
       
   550     //SENDEBUG((
       
   551     //  _L("CSenBaseIdentity::FindMatchingIdentityProviderL() - Started ")));
       
   552 
       
   553     TInt retVal(KErrNotFound);
       
   554 
       
   555     TInt idpCount(iProviderList.Count());
       
   556     apMatch = NULL;
       
   557 
       
   558     for (TInt i=0; i<idpCount; i++)
       
   559         {
       
   560         TPtrC8 databaseProvID = 
       
   561             ((CSenIdentityProvider*)iProviderList[i])->ProviderID();
       
   562 
       
   563         TPtrC8 databaseEndpoint = 
       
   564             ((CSenIdentityProvider*)iProviderList[i])->Endpoint();
       
   565 
       
   566         
       
   567 
       
   568         TPtrC8 addedProvID = aIdp.ProviderID();
       
   569         TPtrC8 addedEndpoint = aIdp.Endpoint();
       
   570 
       
   571         if(databaseProvID==KNullDesC8)
       
   572             {
       
   573             //  This is Basic Web Services IDP and only endpoint matters 
       
   574             if( databaseEndpoint == addedEndpoint )
       
   575                 {
       
   576                 retVal = i; // match was found: return the index of found element
       
   577                 apMatch = iProviderList[i];
       
   578                 }
       
   579             }
       
   580         else if(databaseProvID == addedProvID) 
       
   581             {
       
   582              // This is an ID-WSF IDP, where providerIDs match
       
   583 
       
   584             // We might have a match for non-strict ("provider id only" -type  search). 
       
   585             // Now we must check that there was
       
   586             if( databaseEndpoint == addedEndpoint )
       
   587                 {
       
   588                 apMatch = (CSenIdentityProvider*)iProviderList[i];
       
   589                 retVal = i; // match was found: return the index of found IDP
       
   590                 }
       
   591             else
       
   592                 {
       
   593                 // we need to return an error code indicating that provider id
       
   594                 // is already mapped to different endpoint(!)
       
   595                 retVal = KErrSenProviderIdInUseByAnotherEndpoint;
       
   596                 }
       
   597             }
       
   598         }
       
   599     return retVal;
       
   600     }
       
   601 
       
   602 
       
   603 TInt CSenBaseIdentity::UnregisterIdentityProviderL(CSenIdentityProvider& aIdp )
       
   604     {
       
   605     TInt retVal(KErrNotFound);
       
   606 
       
   607     CSenIdentityProvider* pMatch = NULL;
       
   608     TInt index = FindMatchingIdentityProviderL(aIdp, pMatch);
       
   609 
       
   610     if ( index==KErrNotFound ||
       
   611          (aIdp.Endpoint()==KNullDesC8 &&
       
   612             index==KErrSenProviderIdInUseByAnotherEndpoint)
       
   613        )
       
   614         {
       
   615         // we need to make it possible to unregister an IDP using
       
   616         // plain provider ID
       
   617         TPtrC8 providerId = aIdp.ProviderID();
       
   618         index = IdentityProviderNamed( providerId, pMatch );
       
   619         }
       
   620 
       
   621     //  index is either >= 0, OR: if nothing was removed,
       
   622     // return KErrNotFound OR: provider id used by
       
   623     // another endpoint (new error code)
       
   624     if(index>=0 && pMatch)
       
   625         {
       
   626         // remove the match from database list, which allows us to
       
   627         // delete the IdP from memory.
       
   628         iProviderList.Remove(index);
       
   629         TRAP( retVal, AsElement().RemoveElement(pMatch->AsElement()); ) 
       
   630         delete pMatch;
       
   631         retVal = KErrNone;
       
   632         }
       
   633 
       
   634     return retVal; // if nothing was removed, this will return KErrNotFound
       
   635     }
       
   636 
       
   637 TBool CSenBaseIdentity::AssociateServiceL(const TDesC8& aServiceID,
       
   638                                           const TDesC8& aProviderID)
       
   639     {
       
   640     TBool ret = EFalse;
       
   641     CSenIdentityProvider* prov = IdentityProviderNamed( aProviderID );
       
   642     if ( prov != NULL )
       
   643         {
       
   644         if ( !prov->IsTrustedByL( aServiceID ))
       
   645             {
       
   646             CSenElement& service =
       
   647                 prov->AsElement().AddElementL(KServiceIDElementLocalName);
       
   648 
       
   649             service.SetContentL( aServiceID );
       
   650             }
       
   651         ret = ETrue;
       
   652         }
       
   653     return ret;
       
   654     }
       
   655 
       
   656 TBool CSenBaseIdentity::DissociateServiceL(const TDesC8& aServiceID,
       
   657                                            const TDesC8& aProviderID)
       
   658     {
       
   659     TBool ret = EFalse;
       
   660     CSenIdentityProvider* prov = IdentityProviderNamed( aProviderID );
       
   661     if ( prov != NULL )
       
   662         {
       
   663         if ( prov->IsTrustedByL( aServiceID ))
       
   664             {
       
   665             RPointerArray<CSenElement> services =
       
   666                                     prov->AsElement().ElementsL();
       
   667             TInt serviceCount = services.Count();
       
   668             TInt i(0);
       
   669             while (i < serviceCount)
       
   670                 {
       
   671                 if (services[i]->LocalName() == KServiceIDElementLocalName)
       
   672                     {
       
   673                     CSenElement* elem = services[i];
       
   674                     if ( elem->Content() == aServiceID)
       
   675                         {
       
   676                         TInt retVal;
       
   677                         TRAP(retVal, prov->AsElement().RemoveElement(*elem));
       
   678                         if (retVal == KErrNone )
       
   679                             {
       
   680                             if ( elem )
       
   681                                 {
       
   682                                 delete elem;
       
   683                                 }
       
   684                             ret = ETrue;
       
   685                             }
       
   686                         else
       
   687                             {
       
   688                             ret = EFalse;
       
   689                             }
       
   690                         break;
       
   691                         }
       
   692                     }
       
   693                 i++;
       
   694                 }
       
   695             }
       
   696         }
       
   697     return ret;
       
   698     }
       
   699 
       
   700 void CSenBaseIdentity::StartElementL(const TDesC8& aNsUri,
       
   701                                      const TDesC8& aLocalName,
       
   702                                      const TDesC8& aQName,
       
   703                                      const RAttributeArray& aAttributes)
       
   704     {
       
   705     if (aLocalName == KSenIdpLocalname)
       
   706         {
       
   707         // create CSenWiseIdentityProvider
       
   708         iProvider =
       
   709             CSenWiseIdentityProvider::NewL(iIMEI, KNullDesC8, KNullDesC8);
       
   710 
       
   711         iProvider->SetOwner(*this);
       
   712         iProvider->SetAttributesL(aAttributes);
       
   713         iProvider->SetReader(*Reader());
       
   714         DelegateParsingL(*iProvider);
       
   715         }
       
   716     else
       
   717         {
       
   718         DelegateParsingL(aNsUri, aLocalName, aQName, aAttributes);
       
   719         }
       
   720     }
       
   721 
       
   722 
       
   723 void CSenBaseIdentity::EndElementL(const TDesC8& aNsUri,
       
   724                                    const TDesC8& aLocalName,
       
   725                                    const TDesC8& aQName)
       
   726     {
       
   727     if(aLocalName == KSenIdpLocalname)
       
   728         {
       
   729         if(iProvider)
       
   730             {
       
   731 #ifdef __CRYPTO_HW__
       
   732             TBuf8<KEncrypedLen> decPwd;
       
   733             TInt leaveCode(KErrNone); 
       
   734             TRAP(leaveCode, DecryptPasswordL(iProvider->Password(), decPwd));
       
   735                 if (leaveCode == KErrNone)
       
   736                     {
       
   737                     TLSLOG_FORMAT((KSenCoreServiceManagerLogChannelBase  , KMinLogLevel, _L8("Identity ::DecryptPasswordL success")));
       
   738                     }
       
   739                 else
       
   740                     {
       
   741                     TLSLOG_FORMAT((KSenCoreServiceManagerLogChannelBase  , KMinLogLevel, _L8("Identity ::DecryptPasswordL Failed")));
       
   742                     }
       
   743             iProvider->SetUserInfoL(iProvider->UserName(),iProvider->UserName(), decPwd);
       
   744 			
       
   745 #endif // __CRYPTO_HW__
       
   746             RegisterIdentityProviderL(iProvider);
       
   747             iProvider=NULL;
       
   748             }
       
   749         }
       
   750     else
       
   751         {
       
   752         CSenBaseFragment::EndElementL(aNsUri,aLocalName,aQName);
       
   753         }
       
   754     }
       
   755 
       
   756 const TDesC8& CSenBaseIdentity::Type()
       
   757     {
       
   758     return KNullDesC8;
       
   759     }
       
   760 
       
   761 const TDesC8& CSenBaseIdentity::Name()
       
   762     {
       
   763     return KNullDesC8;
       
   764     }
       
   765 
       
   766 const TDesC8& CSenBaseIdentity::UserName()
       
   767     {
       
   768     return KNullDesC8;
       
   769     }
       
   770 
       
   771 
       
   772 HBufC8* CSenBaseIdentity::AsXmlL()
       
   773     {
       
   774     return this->AsElement().AsXmlL();
       
   775     }
       
   776 
       
   777 HBufC* CSenBaseIdentity::AsXmlUnicodeL()
       
   778     {
       
   779     return this->AsElement().AsXmlUnicodeL();
       
   780     }
       
   781 
       
   782 void CSenBaseIdentity::WriteAsXMLToL(RWriteStream& aWriteStream)
       
   783     {
       
   784 #ifdef __CRYPTO_HW__	
       
   785     TInt idpCount(iProviderList.Count());
       
   786     
       
   787     RArray<RBuf8 *> password;
       
   788     for(TInt i=0; i<idpCount; i++)
       
   789         {
       
   790         CSenIdentityProvider* idp = (CSenIdentityProvider*)iProviderList[i];
       
   791         
       
   792         TBuf8<KEncrypedLen> encPwd;
       
   793         TInt leaveCode(KErrNone);            
       
   794         //EncryptPasswordL(idp->Password(), encPwd);
       
   795         TRAP(leaveCode, EncryptPasswordL(idp->Password(), encPwd));
       
   796     if (leaveCode == KErrNone)
       
   797         {
       
   798         
       
   799         TLSLOG_FORMAT((KSenCoreServiceManagerLogChannelBase  , KMinLogLevel, _L8("Identity ::EncryptPasswordL success")));
       
   800         }
       
   801     else
       
   802         {
       
   803         
       
   804         TLSLOG_FORMAT((KSenCoreServiceManagerLogChannelBase  , KMinLogLevel, _L8("Identity ::EncryptPasswordL Failed")));
       
   805         }
       
   806         RBuf8 *tempPass = new RBuf8;
       
   807         tempPass->Create(idp->Password());
       
   808         password.Append(tempPass); 
       
   809         idp->SetUserInfoL(idp->UserName(),idp->UserName(),encPwd);
       
   810         }
       
   811 #endif // __CRYPTO_HW__
       
   812     this->AsElement().WriteAsXMLToL(aWriteStream);
       
   813 #ifdef __CRYPTO_HW__
       
   814     for(TInt i=0; i<idpCount; i++)
       
   815         {
       
   816         CSenIdentityProvider* idp = (CSenIdentityProvider*)iProviderList[i];
       
   817         idp->SetUserInfoL(idp->UserName(),idp->UserName(),*password[i]);
       
   818         password[i]->Close();
       
   819         delete password[i];
       
   820         }
       
   821 #endif // __CRYPTO_HW__		
       
   822     }
       
   823 
       
   824 EXPORT_C CSenBaseIdentity::~CSenBaseIdentity()
       
   825     {
       
   826     if(iProvider)
       
   827         {
       
   828         CSenElement* removed =
       
   829             CSenBaseIdentity::AsElement().RemoveElement(iProvider->AsElement());
       
   830         // prevend impossible "duplicate delete"
       
   831         if(removed)
       
   832             {
       
   833             delete iProvider; // a leave occured between
       
   834                               // StartElementL and EndElementL...
       
   835             }
       
   836         iProvider = NULL; // to prevent accidental duplicate delete
       
   837         }
       
   838 
       
   839     TInt idpCount(iProviderList.Count());
       
   840     for(TInt i=idpCount-1; i>=0; i--)
       
   841         {
       
   842         CSenElement* removed =
       
   843             CSenBaseIdentity::AsElement().RemoveElement(
       
   844                 ((CSenIdentityProvider*)iProviderList[i])->AsElement());
       
   845         // prevend impossible "duplicate delete"
       
   846         if(removed)
       
   847             {
       
   848             delete iProviderList[i];
       
   849             iProviderList.Remove(i);    // 6.11.2004
       
   850             }
       
   851         }
       
   852         
       
   853     iProviderList.ResetAndDestroy(); 
       
   854     }
       
   855 
       
   856 CSenIdentityProvider* CSenBaseIdentity::IdentityProviderNamed(const TDesC8& aProviderId)
       
   857     {
       
   858     CSenIdentityProvider* pMatch = NULL;
       
   859     IdentityProviderNamed( aProviderId, pMatch );
       
   860     return pMatch;
       
   861     }
       
   862 
       
   863 TInt CSenBaseIdentity::IdentityProviderNamed( const TDesC8& aProviderID,
       
   864                                               CSenIdentityProvider*&  apMatch)
       
   865     {
       
   866     TInt idpCount(iProviderList.Count());
       
   867     TInt retVal(KErrNotFound); // index, or an error
       
   868     apMatch = NULL;
       
   869     for(TInt i=0; i<idpCount; i++)
       
   870         {
       
   871         TPtrC8 providerId = 
       
   872             ((CSenIdentityProvider*)iProviderList[i])->ProviderID();
       
   873         if(providerId.Length()>0 && providerId == aProviderID)
       
   874             {
       
   875             retVal = i; // store the index
       
   876             apMatch = (CSenIdentityProvider*)iProviderList[i]; // found a match
       
   877             break;
       
   878             }
       
   879         }
       
   880     return retVal;
       
   881     }
       
   882 
       
   883 TInt CSenBaseIdentity::ContainsIdentityProviderL(TBool& aContains,
       
   884                                                  CSenIdentityProvider& aIDP)
       
   885     {
       
   886     aContains = EFalse;
       
   887     TInt idpCount(iProviderList.Count());
       
   888     
       
   889     for(TInt i=0; i<idpCount; i++)
       
   890         {
       
   891             if (iProviderList[i]->ConsistsOfL(aIDP)
       
   892                 )
       
   893                 {
       
   894                 aContains = ETrue;
       
   895                 break;  // No need to loop the rest of the providers, we are
       
   896                         // happy with the first hit
       
   897                 }
       
   898         }
       
   899     return KErrNone;
       
   900     }
       
   901 #ifdef __CRYPTO_HW__    
       
   902 void CSenBaseIdentity::EncryptPasswordL(const TDesC8& aData, TDes8& aCipherText)
       
   903     {
       
   904 #ifndef __WINSCW__
       
   905 		  TUint cipherLen;
       
   906 		  TBuf8<KEncrypedLen> temp;
       
   907 		  
       
   908 		  // add padding if length is  less than 16
       
   909 		if (aData.Length() < KCryptoPADataLen) 
       
   910 			  {
       
   911 			  const TInt remainder = aData.Length() % KCryptoPADataLen;
       
   912 			  temp.Copy(aData);
       
   913 			  temp.AppendFill( KPadding, KCryptoPADataLen - remainder );
       
   914 			  cipherLen = temp.Length() + KPaCryptAuthLen ;
       
   915 			  aCipherText.SetLength(cipherLen);
       
   916 			  }
       
   917 		else
       
   918 			  {
       
   919 			  temp.Copy(aData);
       
   920 			  cipherLen = aData.Length() + KPaCryptAuthLen ;
       
   921 			  aCipherText.SetLength(cipherLen);
       
   922 			  }
       
   923 		    
       
   924 	    	TBuf8<20> paId;
       
   925 		    paId.Copy(KCryptoPA);
       
   926 		   
       
   927 		    CSecEnv* secEnv = CSecEnv::NewL();
       
   928 		    TInt err = secEnv->ProtectedApplicationExecute(paId, (TAny*)temp.Ptr(), temp.Length(), 
       
   929 		                                                     (TAny*)aCipherText.Ptr(), cipherLen, KEncryptNoSalt); 
       
   930 		    delete secEnv;
       
   931 	    
       
   932 	    // Encode data into Base64 format
       
   933 	    	HBufC8 *b64encPwd = SenCryptoUtils::EncodeBase64L(aCipherText);
       
   934     	if(b64encPwd)
       
   935 	      {
       
   936 	      aCipherText = (*b64encPwd);
       
   937 	      delete b64encPwd;
       
   938 	      }
       
   939 	    	
       
   940 	          
       
   941 #else
       
   942 
       
   943 aCipherText = aData;
       
   944 //return KErrNotSupported;
       
   945 #endif
       
   946     
       
   947     }
       
   948 
       
   949 void CSenBaseIdentity::DecryptPasswordL(const TDesC8& aCipherText, TDes8& aData)
       
   950     {
       
   951 #ifndef __WINSCW__    	
       
   952     TUint dataLen = aCipherText.Length() - KPaCryptAuthLen ;
       
   953     aData.SetLength(dataLen);
       
   954     TBuf8<20> paId;
       
   955     paId.Copy(KCryptoPA);
       
   956    // Decode data from Base64 format
       
   957     HBufC8 *b64decPwd = SenCryptoUtils::DecodeBase64L(aCipherText);
       
   958     	TDesC8 aTmpTxt= (*b64decPwd);
       
   959     // Decrypt using PA	
       
   960     CSecEnv* secEnv = CSecEnv::NewL();
       
   961     TInt err = secEnv->ProtectedApplicationExecute(paId, (TAny*)aTmpTxt.Ptr(), aTmpTxt.Length(), 
       
   962                                                      (TAny*)aData.Ptr(), dataLen, KDecrypt);
       
   963     // remove padding
       
   964     if (aData.Length() == KCryptoPADataLen)
       
   965         {
       
   966         RBuf8 decPwd;
       
   967         decPwd.Create(KCryptoPADataLen);
       
   968         decPwd.Copy(aData);
       
   969         TInt pos = decPwd.Locate(KPadding);
       
   970         if(pos != KErrNotFound)
       
   971           {
       
   972           decPwd.SetLength(pos);
       
   973           aData.SetLength(decPwd.Length());
       
   974           aData = decPwd;
       
   975           }
       
   976         decPwd.Close();
       
   977         }
       
   978     delete secEnv;                                                  
       
   979 		if(b64decPwd)
       
   980 				{
       
   981 				delete b64decPwd;
       
   982 				}
       
   983 #else
       
   984 
       
   985 aData = aCipherText ;
       
   986 //return KErrNotSupported;
       
   987 #endif    
       
   988 }    
       
   989 #endif // __CRYPTO_HW__