bluetoothengine/btsap/src/BTSapSecurityHandler.cpp
changeset 0 f63038272f30
equal deleted inserted replaced
-1:000000000000 0:f63038272f30
       
     1 /*
       
     2 * Copyright (c) 2004-2008 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 *	  This class is for security checking of key length of both encryption and pass keys
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 
       
    21 // INCLUDE FILES
       
    22 #include <btdevice.h>
       
    23 #include <btextnotifiers.h> // Needed to check the NOTIFIERS_SUPPORT_PASSKEY_MIN_LENGTH flag
       
    24 #include <hciproxy.h>
       
    25 #include <btengdomainpskeys.h>
       
    26 
       
    27 #include "BTSapSecurityHandler.h"
       
    28 #include "BTSapHciExtensionMan.h"
       
    29 #include "debug.h"
       
    30 
       
    31 const TInt KRequiredEncryptionKeyLen = 128;
       
    32 
       
    33 CBTSapSecurityHandler::CBTSapSecurityHandler()
       
    34 	: CActive(CActive::EPriorityStandard)
       
    35 	{
       
    36 	CActiveScheduler::Add(this);
       
    37 	}
       
    38 
       
    39 CBTSapSecurityHandler::~CBTSapSecurityHandler()
       
    40 	{
       
    41 	BTSAP_TRACE_OPT(KBTSAP_TRACE_FUNCTIONS, BTSapPrintTrace(_L("[BTSap]  ~CBTSapSecurityHandler")));
       
    42 	Cancel();
       
    43 	delete iBtHci;
       
    44 	if(iBtDeviceArray)
       
    45 	    {
       
    46         iBtDeviceArray->ResetAndDestroy();
       
    47         delete iBtDeviceArray;
       
    48 	    }
       
    49 	delete iBtDevMan;
       
    50 	}
       
    51 
       
    52 // ---------------------------------------------------------
       
    53 // CBTSapSecurityHandler::NewL()
       
    54 // ---------------------------------------------------------
       
    55 //
       
    56 CBTSapSecurityHandler* CBTSapSecurityHandler::NewL()
       
    57 	{
       
    58 	CBTSapSecurityHandler* self = new (ELeave) CBTSapSecurityHandler();
       
    59 	CleanupStack::PushL(self);
       
    60 	self->ConstructL();
       
    61 	CleanupStack::Pop();
       
    62 	return self;
       
    63 	}
       
    64 
       
    65 // ---------------------------------------------------------
       
    66 // CBTSapSecurityHandler::ConstructL
       
    67 // ---------------------------------------------------------
       
    68 //
       
    69 void CBTSapSecurityHandler::ConstructL()
       
    70 	{
       
    71 	BTSAP_TRACE_OPT(KBTSAP_TRACE_FUNCTIONS, BTSapPrintTrace(_L("[BTSap]  CBTSapSecurityHandler: ConstructL")));
       
    72 	iBtDevMan = CBTEngDevMan::NewL(this);
       
    73 	iBtDeviceArray = new (ELeave) CBTDeviceArray(1);
       
    74 	iBtHci = CBTHciExtensionMan::NewL();
       
    75 	}
       
    76 
       
    77 // ---------------------------------------------------------
       
    78 // CBTSapSecurityHandler::DoCancel
       
    79 // ---------------------------------------------------------
       
    80 //
       
    81 void CBTSapSecurityHandler::DoCancel()
       
    82 	{
       
    83 	BTSAP_TRACE_OPT(KBTSAP_TRACE_FUNCTIONS, BTSapPrintTrace(_L("[BTSap]  CBTSapSecurityHandler: DoCancel")));
       
    84 	if (iSecurityStatus && iSecurityStatus->Int() == KRequestPending)
       
    85 	    {
       
    86 	    User::RequestComplete(iSecurityStatus, KErrCancel);
       
    87 	    }
       
    88 	}
       
    89 	
       
    90 // ---------------------------------------------------------
       
    91 // CBTSapSecurityHandler::RunL
       
    92 // ---------------------------------------------------------
       
    93 //
       
    94 TInt CBTSapSecurityHandler::RunError(TInt aError)
       
    95     {
       
    96 	BTSAP_TRACE_OPT(KBTSAP_TRACE_FUNCTIONS, BTSapPrintTrace(_L("[BTSap]  CBTSapSecurityHandler: RunError: %d"), aError));    
       
    97    
       
    98     switch(iState)
       
    99         {
       
   100         case EEncryptionKeyLength:
       
   101             {
       
   102             BTSAP_TRACE_OPT(KBTSAP_TRACE_ERROR, BTSapPrintTrace(_L("[BTSap]  CBTSapSecurityHandler: RunError: Couldn't get EncryptionKeyLength: %d"), aError));
       
   103             User::RequestComplete(iSecurityStatus, static_cast <TInt> (EGetEncryptionKeyFail));
       
   104             }
       
   105             break;
       
   106         case EPassKeyLength:
       
   107             {
       
   108             User::RequestComplete(iSecurityStatus, static_cast <TInt> (EPassKeyTooShort));
       
   109             }
       
   110             break;
       
   111         default:
       
   112             {
       
   113             User::RequestComplete(iSecurityStatus, aError);
       
   114             }
       
   115         }
       
   116     return KErrNone;
       
   117     }
       
   118 
       
   119 
       
   120 // ---------------------------------------------------------
       
   121 // CBTSapSecurityHandler::RunL
       
   122 // ---------------------------------------------------------
       
   123 //
       
   124 void CBTSapSecurityHandler::RunL()
       
   125 	{
       
   126 	BTSAP_TRACE_OPT(KBTSAP_TRACE_FUNCTIONS, BTSapPrintTrace(_L("[BTSap]  CBTSapSecurityHandler: RunL: %d"), iStatus.Int()));
       
   127     TBTSapSecurityCheckResult result = ESecurityOK;
       
   128     TBool complete = EFalse;
       
   129     
       
   130     User::LeaveIfError(iStatus.Int()); // handle errors in RunError
       
   131     
       
   132     switch(iState)
       
   133         {
       
   134         case EEncryptionKeyLength:
       
   135             {
       
   136             iBtHci->GetResultL(iEncryptionKeyLength);
       
   137             BTSAP_TRACE_OPT(KBTSAP_TRACE_INFO, BTSapPrintTrace(_L("[BTSap]  CBTSapSecurityHandler: RunL: EncryptionKeyLength: %d"), iEncryptionKeyLength));
       
   138             
       
   139             if (iEncryptionKeyLength < KRequiredEncryptionKeyLen)
       
   140                 {
       
   141                 result = EEncryptionKeyTooShort;
       
   142                 complete = ETrue;
       
   143                 }
       
   144             else
       
   145                 {
       
   146                 ASSERT(iBtDeviceArray);
       
   147                 TBTSockAddr sockAddr;
       
   148                 iSocket->RemoteName(sockAddr);
       
   149                 TBTRegistrySearch criteria;
       
   150                 criteria.FindAddress(sockAddr.BTAddr());
       
   151                 iBtDeviceArray->ResetAndDestroy();
       
   152                 iBtDevMan->GetDevices(criteria, iBtDeviceArray);
       
   153                 iState = EPassKeyLength;
       
   154                 iStatus = KRequestPending;
       
   155                 SetActive();
       
   156                 }
       
   157             }
       
   158             break;
       
   159         case EPassKeyLength:
       
   160             {
       
   161             if (!iBtDeviceArray || !iBtDeviceArray->Count())
       
   162                 {
       
   163                 User::Leave(KErrNotFound);
       
   164                 }
       
   165 
       
   166             CBTDevice* device = iBtDeviceArray->At( 0 );
       
   167             // When SSP is used, the link key needs to be authenticated,
       
   168             // otherwise, the passkey needs to be 16 digits.
       
   169             if( device->LinkKeyType() != ELinkKeyAuthenticated )
       
   170                 {
       
   171                 BTSAP_TRACE_OPT(KBTSAP_TRACE_FUNCTIONS, BTSapPrintTrace(_L("[BTSap]  CBTSapSecurityHandler: unauthenticated link key")));
       
   172                 if( !( device->LinkKeyType() == ELinkKeyCombination &&
       
   173                        device->PassKeyLength() >= KRequiredPassKeyLen ) )
       
   174                     {
       
   175                     BTSAP_TRACE_OPT(KBTSAP_TRACE_FUNCTIONS, BTSapPrintTrace(_L("[BTSap]  CBTSapSecurityHandler: unacceptable link key")));
       
   176                     result = EPassKeyTooShort;
       
   177                     }
       
   178                 else if( device->LinkKeyType() == ELinkKeyDebug )
       
   179                     {
       
   180                     // For SAP, we do an extra check for debug mode, to be really sure.
       
   181                     BTSAP_TRACE_OPT(KBTSAP_TRACE_FUNCTIONS, BTSapPrintTrace(_L("[BTSap]  CBTSapSecurityHandler: Debug link key, checking debug mode")));
       
   182                     TBTSspDebugModeValue debugMode = EBTSspDebugModeOff;
       
   183                     TInt err = RProperty::Get( KPSUidBluetoothTestingMode, KBTSspDebugmode, (TInt&) debugMode );
       
   184                     if( err || debugMode == EBTSspDebugModeOff )
       
   185                         {
       
   186                         BTSAP_TRACE_OPT(KBTSAP_TRACE_FUNCTIONS, BTSapPrintTrace(_L("[BTSap]  CBTSapSecurityHandler: debug key not allowed")));
       
   187                         result = EPassKeyTooShort;
       
   188                         }
       
   189                     }
       
   190                 }
       
   191             complete = ETrue;
       
   192             }
       
   193             break;
       
   194         default:
       
   195             {
       
   196             User::Leave(KErrNotSupported);
       
   197             }
       
   198         }
       
   199     if (complete)
       
   200         {
       
   201         User::RequestComplete(iSecurityStatus, static_cast <TInt> (result));
       
   202         }
       
   203 	}
       
   204 
       
   205 // ---------------------------------------------------------
       
   206 // CBTSapSecurityHandler::CheckSapSecurity
       
   207 // ---------------------------------------------------------
       
   208 void CBTSapSecurityHandler::CheckSapSecurity(RSocket& aSocket, TRequestStatus& aStatus)
       
   209 	{
       
   210 	BTSAP_TRACE_OPT(KBTSAP_TRACE_FUNCTIONS, BTSapPrintTrace(_L("[BTSap]  CBTSapSecurityHandler::CheckSapSecurity")));
       
   211 
       
   212 	iSocket = &aSocket;
       
   213 	iSecurityStatus = &aStatus;
       
   214 	aStatus = KRequestPending;
       
   215 
       
   216     TBTSockAddr sockAddr;
       
   217     aSocket.RemoteName(sockAddr);
       
   218 
       
   219     TBTDevAddr btAddr = sockAddr.BTAddr();
       
   220     TRAPD(err, iBtHci->GetEncryptionKeyLengthL(btAddr, iStatus));
       
   221     if (err)
       
   222         {
       
   223         User::RequestComplete(iSecurityStatus, err);
       
   224         }
       
   225     else
       
   226         {
       
   227         iState = EEncryptionKeyLength;
       
   228         SetActive();
       
   229         }
       
   230 	}
       
   231 	
       
   232 
       
   233 // ---------------------------------------------------------
       
   234 // CBTSapSecurityHandler::HandleGetDevicesComplete, from MBTEngDevManObserver
       
   235 // ---------------------------------------------------------	
       
   236 void CBTSapSecurityHandler::HandleGetDevicesComplete( TInt aErr, CBTDeviceArray* /*aDeviceArray*/ )
       
   237     {
       
   238     BTSAP_TRACE_OPT(KBTSAP_TRACE_FUNCTIONS, BTSapPrintTrace(_L("[BTSap]  CBTSapSecurityHandler::HandleGetDevicesComplete")));
       
   239     // Complete our own request -> RunL
       
   240     TRequestStatus* ownStatus = &iStatus;
       
   241     User::RequestComplete(ownStatus, aErr);
       
   242     }
       
   243 
       
   244 //	End of File