obex/obexprotocol/obex/src/authentication.cpp
changeset 0 d0791faffa3f
equal deleted inserted replaced
-1:000000000000 0:d0791faffa3f
       
     1 // Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 /**
       
    17  @file
       
    18  @internalComponent
       
    19 */
       
    20 
       
    21 #include <hash.h>
       
    22 #include <e32math.h>
       
    23 #include <obex.h>
       
    24 #include "authentication.h"
       
    25 
       
    26 _LIT8(KColonCharacter, ":");
       
    27 
       
    28 CObexAuthenticator* CObexAuthenticator::NewL()
       
    29 	{
       
    30 	CObexAuthenticator* s = new(ELeave) CObexAuthenticator;
       
    31 	CleanupStack::PushL(s);
       
    32 	s->ConstructL();
       
    33 	CleanupStack::Pop();
       
    34 	return s;
       
    35 	}
       
    36 
       
    37 void CObexAuthenticator::ConstructL()
       
    38 	{
       
    39 	iMD5 = CMD5::NewL();
       
    40 	}
       
    41 
       
    42 CObexAuthenticator::CObexAuthenticator()
       
    43 	{
       
    44 	TTime time;
       
    45 	time.UniversalTime();
       
    46 	iSeed = time.Int64();
       
    47 	}
       
    48 
       
    49 CObexAuthenticator::~CObexAuthenticator()
       
    50 	{
       
    51 	delete iMD5;
       
    52 	}
       
    53 
       
    54 TInt CObexAuthenticator::GenerateNonce(TDes8& aNonce)
       
    55 	{
       
    56 	TTime time;
       
    57 	time.UniversalTime();
       
    58 
       
    59 	TInt64 randomNumber = Math::Rand(iSeed);
       
    60 	randomNumber <<= 32;
       
    61 	randomNumber += Math::Rand(iSeed);
       
    62 
       
    63 	TBuf8<33> key;
       
    64 	key.Zero();
       
    65 	key.AppendNum(time.Int64(), EHex);
       
    66 	key.Append(_L8(":"));
       
    67 	key.AppendNum(randomNumber, EHex);
       
    68 	
       
    69 	iMD5->Reset();
       
    70 	aNonce.Append(iMD5->Hash(key));
       
    71 	return KErrNone;
       
    72 	}
       
    73 
       
    74 void CObexAuthenticator::GenerateResponseL(const TDesC8& aPasswd, const TNonce& aNonce, TRequestDigest& aRequestDigest)
       
    75 	{
       
    76 	//work out the length of buffer we need
       
    77 	TInt buflen = aNonce.Length() + KColonCharacter().Length() + aPasswd.Length();
       
    78 	HBufC8* buf = HBufC8::NewLC(buflen);
       
    79 	TPtr8 ptr = buf->Des();
       
    80 	ptr.Zero();
       
    81 	ptr.Append(aNonce);
       
    82 	ptr.Append(KColonCharacter);
       
    83 	ptr.Append(aPasswd);
       
    84 	iMD5->Reset();
       
    85 	aRequestDigest.Append(iMD5->Hash(*buf));
       
    86 
       
    87 	CleanupStack::PopAndDestroy();//buf
       
    88 	}
       
    89 
       
    90 void CObexAuthenticator::ChallengeResponseL(const TDesC8& aPasswd, const TNonce& aNonce, const TRequestDigest& aRequestDigest)
       
    91 	{
       
    92 	TRequestDigest digest;
       
    93 	digest.Zero();
       
    94 	GenerateResponseL(aPasswd, aNonce, digest);
       
    95 	if (digest != aRequestDigest)
       
    96 		User::Leave(KErrAccessDenied);
       
    97 	}