bluetooth/btstack/secman/secman.cpp
changeset 17 907b2fb7aa8e
parent 16 0089b2f7ebd8
child 21 5e5528a288fe
equal deleted inserted replaced
16:0089b2f7ebd8 17:907b2fb7aa8e
   553 		}
   553 		}
   554 
   554 
   555 	CPhysicalLink* link = iPhysicalLinksManager->FindPhysicalLink(aAddr);
   555 	CPhysicalLink* link = iPhysicalLinksManager->FindPhysicalLink(aAddr);
   556 	__ASSERT_ALWAYS(link, PANIC(KBTSecPanic, EBTSecPhysicalLinkMissing));
   556 	__ASSERT_ALWAYS(link, PANIC(KBTSecPanic, EBTSecPhysicalLinkMissing));
   557 	__ASSERT_DEBUG(!link->InstanceNumericComparator(), PANIC(KBTSecPanic, EBTSecConnectionNumericComparisonTwice));
   557 	__ASSERT_DEBUG(!link->InstanceNumericComparator(), PANIC(KBTSecPanic, EBTSecConnectionNumericComparisonTwice));
   558 	if(link->InstanceNumericComparator())
   558 	__ASSERT_DEBUG(!link->InstanceUserConfirmer(), PANIC(KBTSecPanic, EBTSecConnectionUserConfirmationTwice));
       
   559 	if(link->InstanceNumericComparator() || link->InstanceUserConfirmer())
   559 		{
   560 		{
   560 		return;
   561 		return;
   561 		}
   562 		}
   562 
   563 
   563 	if(link->AuthWithMITM())
   564 	if(link->AuthWithMITM())
   579 				TRAP_IGNORE(iCommandController->UserConfirmationRequestNegativeReplyL(aAddr));
   580 				TRAP_IGNORE(iCommandController->UserConfirmationRequestNegativeReplyL(aAddr));
   580 				return;// No passkey or comparison dialogs; disconnect instead
   581 				return;// No passkey or comparison dialogs; disconnect instead
   581 				}
   582 				}
   582 			}
   583 			}
   583 		}
   584 		}
       
   585 	else if (!link->IsPairingExpected()
       
   586 			|| ((link->AuthenticationRequirement() == EMitmNotReqDedicatedBonding 
       
   587 					|| link->AuthenticationRequirement() == EMitmReqDedicatedBonding)
       
   588 				&& !IsDedicatedBondingAttempted(aAddr)))
       
   589 		{
       
   590 		TRAPD(err,link->NewUserConfirmerL(aAddr, *this, ETrue));
       
   591 		if(err)
       
   592 			{
       
   593 			if(requester)
       
   594 				{
       
   595 				requester->CompleteRequest(EBTSecManAccessDenied);
       
   596 				}
       
   597 			else
       
   598 				{
       
   599 				TRAP_IGNORE(iCommandController->UserConfirmationRequestNegativeReplyL(aAddr));
       
   600 				return;// No passkey or comparison dialogs; disconnect instead
       
   601 				}
       
   602 			}
       
   603 		}
   584 	else
   604 	else
   585 		{
   605 		{
   586 		// Just Work...
   606 		// Just Work...
   587 		if(requester)
   607 		if(requester)
   588 			{
   608 			{
   594 		// note: -- check errors here
   614 		// note: -- check errors here
   595 		TRAP_IGNORE(iCommandController->UserConfirmationRequestReplyL(aAddr));
   615 		TRAP_IGNORE(iCommandController->UserConfirmationRequestReplyL(aAddr));
   596 		}
   616 		}
   597 	}
   617 	}
   598 
   618 
   599 void CBTSecMan::UserConfirmationComplete(const TBTDevAddr& aAddr, TBool aResult, TInt aError)
   619 void CBTSecMan::NumericComparisonComplete(const TBTDevAddr& aAddr, TBool aResult, TInt aError)
   600 	{
   620 	{
   601 	LOG_FUNC
   621 	LOG_FUNC
   602 	CBTAccessRequester* requester = FindActiveAccessRequester(aAddr);
   622 	CBTAccessRequester* requester = FindActiveAccessRequester(aAddr);
   603 	if (requester)
   623 	if (requester)
   604 		{
   624 		{
   638 			// note: -- check errors here
   658 			// note: -- check errors here
   639 			TRAP_IGNORE(iCommandController->UserConfirmationRequestNegativeReplyL(aAddr));
   659 			TRAP_IGNORE(iCommandController->UserConfirmationRequestNegativeReplyL(aAddr));
   640 			}
   660 			}
   641 		}
   661 		}
   642 	link->DeleteNumericComparator();
   662 	link->DeleteNumericComparator();
       
   663 	}
       
   664 
       
   665 void CBTSecMan::UserConfirmationComplete(const TBTDevAddr& aAddr, TBool aResult, TInt aError)
       
   666 	{
       
   667 	LOG_FUNC
       
   668 	CBTAccessRequester* requester = FindActiveAccessRequester(aAddr);
       
   669 	if (requester)
       
   670 		{
       
   671 		LOG(_L8("\tCBTAccessRequester FOUND!\n"));
       
   672 		if (aError == KErrNone)
       
   673 			{
       
   674 			TBTSecEventUserConfirmationComplete event(aResult);
       
   675 			requester->SendEvent(event);
       
   676 			}
       
   677 		else if (aError == KErrNotFound) // KErrNotFound -> Notifier isn't present, so allow anyway
       
   678 			{
       
   679 			TBTSecEventUserConfirmationComplete event(ETrue);
       
   680 			requester->SendEvent(event);
       
   681 			}
       
   682 		else
       
   683 			{
       
   684 			TBTSecEventUserConfirmationComplete event(EFalse);  // Failed, so send EFalse
       
   685 			requester->SendEvent(event);
       
   686 			}
       
   687 		}
       
   688 
       
   689 	CPhysicalLink* link = iPhysicalLinksManager->FindPhysicalLink(aAddr);
       
   690 	__ASSERT_ALWAYS(link, PANIC(KBTSecPanic, EBTSecPhysicalLinkMissing));
       
   691 
       
   692 	if (aError==KErrNotFound) // KErrNotFound -> Notifier isn't present, so allow anyway
       
   693 		{
       
   694 		link->PinRequestSent();
       
   695 		// note: -- check errors here
       
   696 		TRAP_IGNORE(iCommandController->UserConfirmationRequestReplyL(aAddr));
       
   697 		}
       
   698 	else if (aError!=KErrNone)
       
   699 		{
       
   700 		// there was an error somewhere a long the way so respond negatively
       
   701 		// note: -- check errors here
       
   702 		TRAP_IGNORE(iCommandController->UserConfirmationRequestNegativeReplyL(aAddr));
       
   703 		}
       
   704 	else
       
   705 		{
       
   706 		// got a result
       
   707 		if(aResult)
       
   708 			{
       
   709 			link->PinRequestSent();
       
   710 			// note: -- check errors here
       
   711 			TRAP_IGNORE(iCommandController->UserConfirmationRequestReplyL(aAddr));
       
   712 			}
       
   713 		else
       
   714 			{
       
   715 			// note: -- check errors here
       
   716 			TRAP_IGNORE(iCommandController->UserConfirmationRequestNegativeReplyL(aAddr));
       
   717 			}
       
   718 		}
       
   719 	link->DeleteUserConfirmer();
   643 	}
   720 	}
   644 
   721 
   645 void CBTSecMan::PasskeyNotification(const TBTDevAddr& aAddr, TUint32 aPasskey)
   722 void CBTSecMan::PasskeyNotification(const TBTDevAddr& aAddr, TUint32 aPasskey)
   646 	{
   723 	{
   647 	LOG_FUNC
   724 	LOG_FUNC
   804 		PasskeyNotificationComplete(aAddr, err);
   881 		PasskeyNotificationComplete(aAddr, err);
   805 		}
   882 		}
   806 	if (link->InstanceNumericComparator() && link->IsNumericComparatorActive())
   883 	if (link->InstanceNumericComparator() && link->IsNumericComparatorActive())
   807 		{
   884 		{
   808 		link->CancelNumericComparator();
   885 		link->CancelNumericComparator();
       
   886 		NumericComparisonComplete(aAddr, EFalse, err);
       
   887 		}
       
   888 	if (link->InstanceUserConfirmer() && link->IsUserConfirmerActive())
       
   889 		{
       
   890 		link->CancelUserConfirmer();
   809 		UserConfirmationComplete(aAddr, EFalse, err);
   891 		UserConfirmationComplete(aAddr, EFalse, err);
   810 		}
   892 		}
   811 
       
   812 	iPhysicalLinksManager->SimplePairingComplete(aAddr, aError);
   893 	iPhysicalLinksManager->SimplePairingComplete(aAddr, aError);
   813 
   894 
   814  	// Add result to list (always with HCI error code).
   895  	// Add result to list (always with HCI error code).
   815  	// Pairing results will be added only if pairing that involved numeric Comparison or passkey entry.
   896  	// Pairing results will be added only if pairing that involved numeric Comparison or passkey entry.
   816  	// It should not be added if the pairing completed using Just Works
   897  	// It should not be added if the pairing completed using Just Works
  1751 	RemoveMyselfFromQue();
  1832 	RemoveMyselfFromQue();
  1752 	iIsAddedToNotifierQue = EFalse;
  1833 	iIsAddedToNotifierQue = EFalse;
  1753 
  1834 
  1754 	__ASSERT_DEBUG(iNumericComparisonParamsPckg().DeviceAddress() == iDevAddr, PANIC(KBTSecPanic, EBTSecBadDeviceAddress));
  1835 	__ASSERT_DEBUG(iNumericComparisonParamsPckg().DeviceAddress() == iDevAddr, PANIC(KBTSecPanic, EBTSecBadDeviceAddress));
  1755 
  1836 
  1756 	iSecMan.UserConfirmationComplete(iDevAddr, iResultPckg(), iStatus.Int());
  1837 	iSecMan.NumericComparisonComplete(iDevAddr, iResultPckg(), iStatus.Int());
  1757 	}
  1838 	}
  1758 
  1839 
  1759 TInt CBTNumericComparator::RunError(TInt aError)
  1840 TInt CBTNumericComparator::RunError(TInt aError)
  1760 	{
  1841 	{
  1761 	LOG_FUNC
  1842 	LOG_FUNC
  1915 	//will never get called as our RunL doesn't leave.
  1996 	//will never get called as our RunL doesn't leave.
  1916 	LOG1(_L8("\tCBTPasskeyEntry::RunError(%d)\n"), aError);
  1997 	LOG1(_L8("\tCBTPasskeyEntry::RunError(%d)\n"), aError);
  1917 	return aError;
  1998 	return aError;
  1918 	}
  1999 	}
  1919 
  2000 
       
  2001 //------------------------------------------------------------------------//
       
  2002 //class CBTNumericComparator
       
  2003 //------------------------------------------------------------------------//
       
  2004 CBTUserConfirmer* CBTUserConfirmer::NewL(const TBTDevAddr aAddr,
       
  2005 												 CBTSecMan& aSecMan,
       
  2006 												 TBool aInternallyInitiated)
       
  2007 	{
       
  2008 	LOG_STATIC_FUNC
       
  2009 	CBTUserConfirmer* s = CBTUserConfirmer::NewLC(aAddr, aSecMan, aInternallyInitiated);
       
  2010 	CleanupStack::Pop(s);
       
  2011 	return s;
       
  2012 	}
       
  2013 
       
  2014 CBTUserConfirmer* CBTUserConfirmer::NewLC(const TBTDevAddr aAddr,
       
  2015 												  CBTSecMan& aSecMan,
       
  2016 												  TBool aInternallyInitiated)
       
  2017 	{
       
  2018 	LOG_STATIC_FUNC
       
  2019 	CBTUserConfirmer* s = new(ELeave) CBTUserConfirmer(aSecMan, aInternallyInitiated);
       
  2020 	CleanupStack::PushL(s);
       
  2021 	s->ConstructL(aAddr);
       
  2022 	return s;
       
  2023 	}
       
  2024 
       
  2025 CBTUserConfirmer::CBTUserConfirmer(CBTSecMan& aSecMan, TBool aInternallyInitiated)
       
  2026 	: CSecNotifierRequester(aSecMan)
       
  2027 	, iSecMan(aSecMan)
       
  2028 	, iInternallyInitiated(aInternallyInitiated)
       
  2029 	{
       
  2030 	LOG_FUNC
       
  2031 	CActiveScheduler::Add(this);
       
  2032 	}
       
  2033 
       
  2034 CBTUserConfirmer::~CBTUserConfirmer()
       
  2035 	{
       
  2036 	LOG_FUNC
       
  2037 	Cancel();
       
  2038 	delete iNameUpdater;
       
  2039 	}
       
  2040 
       
  2041 
       
  2042 void CBTUserConfirmer::DoUpdateNotifier()
       
  2043 	{
       
  2044 	LOG_FUNC
       
  2045 	if(IsActive())
       
  2046 		{
       
  2047 		if(!iNameUpdater)
       
  2048 			{
       
  2049 			//Create a new CSecNotifierUpdateAO object
       
  2050 			TRAP_IGNORE(iNameUpdater = CSecNotifierUpdateAO<TBTDeviceNameUpdateParamsPckg>::NewL(iNotifier, KBTUserConfirmationNotifierUid));
       
  2051 			}
       
  2052 		if(iNameUpdater)
       
  2053 			{
       
  2054 			TBTDeviceName deviceName(KNullDesC);
       
  2055 			TInt err = KErrNotFound;
       
  2056 			if(iDeviceName)
       
  2057 				{
       
  2058 				TRAP(err, deviceName = BTDeviceNameConverter::ToUnicodeL(*iDeviceName)); // Best effort attempt.
       
  2059 				}
       
  2060 
       
  2061 			TBTDeviceNameUpdateParamsPckg pckg = TBTDeviceNameUpdateParams(deviceName, err);
       
  2062 			iNameUpdater->DoUpdate(pckg);
       
  2063 			}
       
  2064 		}
       
  2065 	}
       
  2066 
       
  2067 void CBTUserConfirmer::DoRequest()
       
  2068 /**
       
  2069 Start the RNotifier plugin that deals with authorisation.
       
  2070 **/
       
  2071 	{
       
  2072 	LOG_FUNC
       
  2073 	TInt err(KErrNone);
       
  2074 
       
  2075 	TBTDeviceName deviceName;
       
  2076 
       
  2077 	if (iDeviceName)
       
  2078 		{
       
  2079 		TRAP(err, deviceName = BTDeviceNameConverter::ToUnicodeL(*iDeviceName));
       
  2080 		if (err!=KErrNone)
       
  2081 			{
       
  2082 			deviceName = KNullDesC;
       
  2083 			}
       
  2084 		}
       
  2085 	else
       
  2086 		{
       
  2087 		deviceName = KNullDesC;
       
  2088 		}
       
  2089 	iUserConfirmationParamsPckg = TBTUserConfirmationParams(iDevAddr, deviceName, iInternallyInitiated);
       
  2090 
       
  2091 	iNotifier.StartNotifierAndGetResponse(iStatus, KBTUserConfirmationNotifierUid, iUserConfirmationParamsPckg, iResultPckg);
       
  2092 	SetActive();
       
  2093 	}
       
  2094 
       
  2095 
       
  2096 void CBTUserConfirmer::DoCancel()
       
  2097 	{
       
  2098 	LOG_FUNC
       
  2099 	iNotifier.CancelNotifier(KBTUserConfirmationNotifierUid);
       
  2100 	if(iNameUpdater)
       
  2101 		{
       
  2102 		iNameUpdater->Cancel();
       
  2103 		}
       
  2104 	}
       
  2105 
       
  2106 void CBTUserConfirmer::RunL()
       
  2107 	{
       
  2108 	LOG_FUNC
       
  2109 	// got an answer so unload the notifier
       
  2110 	iNotifier.CancelNotifier(KBTUserConfirmationNotifierUid);
       
  2111 
       
  2112 	//remove ourself from the notifier que, allowing the next notifier to be activated
       
  2113 	RemoveMyselfFromQue();
       
  2114 	iIsAddedToNotifierQue = EFalse;
       
  2115 
       
  2116 	__ASSERT_DEBUG(iUserConfirmationParamsPckg().DeviceAddress() == iDevAddr, PANIC(KBTSecPanic, EBTSecBadDeviceAddress));
       
  2117 
       
  2118 	iSecMan.UserConfirmationComplete(iDevAddr, iResultPckg(), iStatus.Int());
       
  2119 	}
       
  2120 
       
  2121 TInt CBTUserConfirmer::RunError(TInt aError)
       
  2122 	{
       
  2123 	LOG_FUNC
       
  2124 	//will never get called as our RunL doesn't leave.
       
  2125 	LOG1(_L8("\tCBTUserConfirmation::RunError(%d)\n"), aError);
       
  2126 	return aError;
       
  2127 	}
       
  2128 
       
  2129 
       
  2130