webservices/idwsfplugin/src/senidwsfcrammd5saslmechanism.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 <SenXmlUtils.h>
       
    27 
       
    28 #include "SenIdWsfCrammd5SaslMechanism.h"
       
    29 #include "MSenSaslMessage.h"
       
    30 
       
    31 #include <flogger.h>
       
    32 
       
    33 namespace
       
    34     {
       
    35     _LIT8(KCrammd5, "CRAM-MD5");
       
    36     }
       
    37 
       
    38 // Create instance of concrete ECOM interface implementation
       
    39 CSenIdWsfCrammd5SaslMechanism* CSenIdWsfCrammd5SaslMechanism::NewL(
       
    40                                     MSenCoreServiceManager& aServiceManager)
       
    41     {
       
    42     CSenIdWsfCrammd5SaslMechanism* pNew =
       
    43                         CSenIdWsfCrammd5SaslMechanism::NewLC(aServiceManager);
       
    44     CleanupStack::Pop();
       
    45     return pNew;
       
    46     }
       
    47 
       
    48 CSenIdWsfCrammd5SaslMechanism*
       
    49             CSenIdWsfCrammd5SaslMechanism::NewLC(
       
    50                                     MSenCoreServiceManager& aServiceManager)
       
    51     {
       
    52     CSenIdWsfCrammd5SaslMechanism* pNew =
       
    53                 new (ELeave) CSenIdWsfCrammd5SaslMechanism(aServiceManager);
       
    54     CleanupStack::PushL(pNew);
       
    55     return pNew;
       
    56     }
       
    57 
       
    58 CSenIdWsfCrammd5SaslMechanism::CSenIdWsfCrammd5SaslMechanism(
       
    59                                     MSenCoreServiceManager& aServiceManager)
       
    60 :CSenIdWsfPlainSaslMechanism(aServiceManager)
       
    61     {
       
    62     }
       
    63 
       
    64 CSenIdWsfCrammd5SaslMechanism::~CSenIdWsfCrammd5SaslMechanism()
       
    65     {
       
    66     }
       
    67 
       
    68 const TDesC8& CSenIdWsfCrammd5SaslMechanism::Name()
       
    69     {
       
    70     return KCrammd5();
       
    71     }
       
    72 
       
    73 TInt CSenIdWsfCrammd5SaslMechanism::HandleResponseL(
       
    74                                                 MSenSaslMessage& aResponse,
       
    75                                                 MSenSaslMessage& aNewRequest
       
    76     )
       
    77     {
       
    78     const TInt KPasswordMinLength = 64;
       
    79 
       
    80     TInt retVal = KErrNone;
       
    81 
       
    82     TPtrC8 authzId = ipAccount->AuthzID();
       
    83     TPtrC8 authnId = ipAccount->AdvisoryAuthnID();
       
    84     if (authnId == KNullDesC8)
       
    85         {
       
    86         authnId.Set(authzId);
       
    87         }
       
    88 
       
    89     // Get challenge
       
    90     TPtrC8 theChallengeDataBase64 = aResponse.Data();
       
    91 
       
    92     HBufC8* pChallengeData = // push pChallengeData;
       
    93         iServiceManager.DecodeFromBase64LC(theChallengeDataBase64);
       
    94     if (pChallengeData->Length() == 0)
       
    95         {
       
    96         iServiceManager.Log()->Write(_L(
       
    97         "ERROR: CSenIdWsfCrammd5SaslMechanism::HandleResponseL \
       
    98         challenge date length = 0"));
       
    99         User::Leave(KErrArgument);
       
   100         }
       
   101 
       
   102     // Create MD5 hash generator
       
   103     CMD5* pMd5Gen = CMD5::NewL();
       
   104     CleanupStack::PushL(pMd5Gen);
       
   105 
       
   106     // The password is the shared secret
       
   107     // transform password
       
   108     TPtrC8 password(KNullDesC8);
       
   109     if(ipPassword)
       
   110         {
       
   111         password.Set(ipPassword->Des());
       
   112         }
       
   113 
       
   114     HBufC8* pPassword8 = TransformL(password);
       
   115     CleanupStack::PushL(pPassword8);
       
   116 
       
   117     if (pPassword8->Length() <= KPasswordMinLength)
       
   118         {
       
   119         // Pad with 0 bytes
       
   120         pPassword8 = pPassword8->ReAllocL(KPasswordMinLength);
       
   121         CleanupStack::Pop();    // Replace pwd on cleanup stack,
       
   122                                 // might have changed(!)
       
   123         CleanupStack::PushL(pPassword8);
       
   124         pPassword8->Des().AppendFill(TChar(0),
       
   125                                     KPasswordMinLength - pPassword8->Length());
       
   126         }
       
   127     else
       
   128         {
       
   129         // Password is longer than 64 bytes, replace pwd with MD5 hash
       
   130         HBufC8* pPasswordHash = pMd5Gen->Hash(*pPassword8).AllocL();
       
   131         CleanupStack::PopAndDestroy(); // pPassword8;
       
   132         pPassword8 = pPasswordHash;
       
   133         CleanupStack::PushL(pPassword8);
       
   134         }
       
   135 
       
   136     // Create HMAC generator
       
   137     CHMAC* pHmacGen = CHMAC::NewL(*pPassword8, pMd5Gen); // takes ownership of
       
   138                                                          // pMd5Gen AND makes
       
   139                                                          // use of pPassword8
       
   140     CleanupStack::PopAndDestroy(); // pPassword8
       
   141     CleanupStack::Pop(); // pMd5Gen
       
   142 
       
   143 
       
   144     CleanupStack::PushL(pHmacGen); // push
       
   145 
       
   146     // Calculate hash
       
   147     TPtrC8 ptrHash = pHmacGen->Hash(*pChallengeData);
       
   148     if (ptrHash.Length() > 0)
       
   149         {
       
   150 			_LIT8(KSpace," ");  //CodeScannerWarnings
       
   151         HBufC8 *pResponseData8 = HBufC8::NewLC(
       
   152             authnId.Length() + 1 + ptrHash.Length() * 2); // push pData8
       
   153         TPtr8 ptrResponseData8 = pResponseData8->Des();
       
   154 
       
   155         ptrResponseData8.Append(authnId);
       
   156         ptrResponseData8.Append(KSpace);
       
   157 
       
   158         // ptrHash needs to be converted to a lowercase hex value
       
   159         for (TInt i = 0; i < ptrHash.Length(); i++)
       
   160             {
       
   161             ptrResponseData8.AppendNumFixedWidth(ptrHash[i], EHex, 2);
       
   162             }
       
   163 
       
   164         retVal = aNewRequest.ConstructRequestFromL(KCrammd5, ptrResponseData8);
       
   165 
       
   166         CleanupStack::PopAndDestroy();  // pResponseData8;
       
   167         }
       
   168 
       
   169     CleanupStack::PopAndDestroy(); // pHmacGen
       
   170 
       
   171 
       
   172     CleanupStack::PopAndDestroy(); // pChallengeData;
       
   173     return retVal;
       
   174     }
       
   175 
       
   176 // End of File