2 The Miscellaneous Routines for WiFi Connection Manager.
4 Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
10 #include "WifiConnectionMgrDxe.h"
13 Empty function for event process function.
15 @param Event The Event need to be process
16 @param Context The context of the event.
21 WifiMgrInternalEmptyFunction (
30 Convert the mac address into a hexadecimal encoded ":" seperated string.
32 @param[in] Mac The mac address.
33 @param[in] StrSize The size, in bytes, of the output buffer specified by Str.
34 @param[out] Str The storage to return the mac string.
39 IN EFI_80211_MAC_ADDRESS
*Mac
,
44 if (Mac
== NULL
|| Str
== NULL
) {
51 L
"%02X:%02X:%02X:%02X:%02X:%02X",
52 Mac
->Addr
[0], Mac
->Addr
[1], Mac
->Addr
[2],
53 Mac
->Addr
[3], Mac
->Addr
[4], Mac
->Addr
[5]
58 Read private key file to buffer.
60 @param[in] FileContext The file context of private key file.
61 @param[out] PrivateKeyDataAddr The buffer address to restore private key file, should be
63 @param[out] PrivateKeyDataSize The size of read private key file.
65 @retval EFI_SUCCESS Successfully read the private key file.
66 @retval EFI_INVALID_PARAMETER One or more of the parameters is invalid.
70 WifiMgrReadFileToBuffer (
71 IN WIFI_MGR_FILE_CONTEXT
*FileContext
,
78 if (FileContext
!= NULL
&& FileContext
->FHandle
!= NULL
) {
80 Status
= ReadFileContent (
87 if (FileContext
->FHandle
!= NULL
) {
88 FileContext
->FHandle
->Close (FileContext
->FHandle
);
90 FileContext
->FHandle
= NULL
;
94 return EFI_INVALID_PARAMETER
;
98 Get the Nic data by the NicIndex.
100 @param[in] Private The pointer to the global private data structure.
101 @param[in] NicIndex The index indicates the position of wireless NIC.
103 @return Pointer to the Nic data, or NULL if not found.
106 WIFI_MGR_DEVICE_DATA
*
107 WifiMgrGetNicByIndex (
108 IN WIFI_MGR_PRIVATE_DATA
*Private
,
113 WIFI_MGR_DEVICE_DATA
*Nic
;
115 if (Private
== NULL
) {
119 NET_LIST_FOR_EACH (Entry
, &Private
->NicList
) {
120 Nic
= NET_LIST_USER_STRUCT_S (Entry
, WIFI_MGR_DEVICE_DATA
,
121 Link
, WIFI_MGR_DEVICE_DATA_SIGNATURE
);
122 if (Nic
->NicIndex
== NicIndex
) {
131 Find a network profile through its' SSId and securit type, and the SSId is an unicode string.
133 @param[in] SSId The target network's SSId.
134 @param[in] SecurityType The target network's security type.
135 @param[in] ProfileList The profile list on a Nic.
137 @return Pointer to a network profile, or NULL if not found.
140 WIFI_MGR_NETWORK_PROFILE
*
141 WifiMgrGetProfileByUnicodeSSId (
143 IN UINT8 SecurityType
,
144 IN LIST_ENTRY
*ProfileList
148 WIFI_MGR_NETWORK_PROFILE
*Profile
;
150 if (SSId
== NULL
|| ProfileList
== NULL
) {
154 NET_LIST_FOR_EACH (Entry
, ProfileList
) {
155 Profile
= NET_LIST_USER_STRUCT_S (Entry
, WIFI_MGR_NETWORK_PROFILE
,
156 Link
, WIFI_MGR_PROFILE_SIGNATURE
);
157 if (StrCmp (SSId
, Profile
->SSId
) == 0 && SecurityType
== Profile
->SecurityType
) {
166 Find a network profile through its' SSId and securit type, and the SSId is an ascii string.
168 @param[in] SSId The target network's SSId.
169 @param[in] SecurityType The target network's security type.
170 @param[in] ProfileList The profile list on a Nic.
172 @return Pointer to a network profile, or NULL if not found.
175 WIFI_MGR_NETWORK_PROFILE
*
176 WifiMgrGetProfileByAsciiSSId (
178 IN UINT8 SecurityType
,
179 IN LIST_ENTRY
*ProfileList
182 CHAR16 SSIdUniCode
[SSID_STORAGE_SIZE
];
187 if (AsciiStrToUnicodeStrS (SSId
, SSIdUniCode
, SSID_STORAGE_SIZE
) != RETURN_SUCCESS
) {
191 return WifiMgrGetProfileByUnicodeSSId (SSIdUniCode
, SecurityType
, ProfileList
);
195 Find a network profile through its' profile index.
197 @param[in] ProfileIndex The target network's profile index.
198 @param[in] ProfileList The profile list on a Nic.
200 @return Pointer to a network profile, or NULL if not found.
203 WIFI_MGR_NETWORK_PROFILE
*
204 WifiMgrGetProfileByProfileIndex (
205 IN UINT32 ProfileIndex
,
206 IN LIST_ENTRY
*ProfileList
209 WIFI_MGR_NETWORK_PROFILE
*Profile
;
212 if (ProfileList
== NULL
) {
215 NET_LIST_FOR_EACH (Entry
, ProfileList
) {
216 Profile
= NET_LIST_USER_STRUCT_S (Entry
, WIFI_MGR_NETWORK_PROFILE
,
217 Link
, WIFI_MGR_PROFILE_SIGNATURE
);
218 if (Profile
->ProfileIndex
== ProfileIndex
) {
226 To test if the AKMSuite is in supported AKMSuite list.
228 @param[in] SupportedAKMSuiteCount The count of the supported AKMSuites.
229 @param[in] SupportedAKMSuiteList The supported AKMSuite list.
230 @param[in] AKMSuite The AKMSuite to be tested.
232 @return True if this AKMSuite is supported, or False if not.
236 WifiMgrSupportAKMSuite (
237 IN UINT16 SupportedAKMSuiteCount
,
238 IN UINT32
*SupportedAKMSuiteList
,
244 if (AKMSuite
== NULL
|| SupportedAKMSuiteList
== NULL
||
245 SupportedAKMSuiteCount
== 0) {
249 for (Index
= 0; Index
< SupportedAKMSuiteCount
; Index
++) {
250 if (SupportedAKMSuiteList
[Index
] == *AKMSuite
) {
259 To check if the CipherSuite is in supported CipherSuite list.
261 @param[in] SupportedCipherSuiteCount The count of the supported CipherSuites.
262 @param[in] SupportedCipherSuiteList The supported CipherSuite list.
263 @param[in] CipherSuite The CipherSuite to be tested.
265 @return True if this CipherSuite is supported, or False if not.
269 WifiMgrSupportCipherSuite (
270 IN UINT16 SupportedCipherSuiteCount
,
271 IN UINT32
*SupportedCipherSuiteList
,
272 IN UINT32
*CipherSuite
277 if (CipherSuite
== NULL
|| SupportedCipherSuiteCount
== 0 ||
278 SupportedCipherSuiteList
== NULL
) {
282 for (Index
= 0; Index
< SupportedCipherSuiteCount
; Index
++) {
283 if (SupportedCipherSuiteList
[Index
] == *CipherSuite
) {
292 Check an AKM suite list and a Cipher suite list to see if one or more AKM suites or Cipher suites
293 are supported and find the matchable security type.
295 @param[in] AKMList The target AKM suite list to be checked.
296 @param[in] CipherList The target Cipher suite list to be checked
297 @param[in] Nic The Nic to operate, contains the supported AKMSuite list
298 and supported CipherSuite list
299 @param[out] SecurityType To identify a security type from the AKM suite list and
301 @param[out] AKMSuiteSupported To identify if this security type is supported. If it is
302 NULL, overcome this field
303 @param[out] CipherSuiteSupported To identify if this security type is supported. If it is
304 NULL, overcome this field
306 @retval EFI_SUCCESS This operation has completed successfully.
307 @retval EFI_INVALID_PARAMETER No Nic found or the suite list is null.
312 IN EFI_80211_AKM_SUITE_SELECTOR
*AKMList
,
313 IN EFI_80211_CIPHER_SUITE_SELECTOR
*CipherList
,
314 IN WIFI_MGR_DEVICE_DATA
*Nic
,
315 OUT UINT8
*SecurityType
,
316 OUT BOOLEAN
*AKMSuiteSupported
,
317 OUT BOOLEAN
*CipherSuiteSupported
320 EFI_80211_AKM_SUITE_SELECTOR
*SupportedAKMSuites
;
321 EFI_80211_CIPHER_SUITE_SELECTOR
*SupportedSwCipherSuites
;
322 EFI_80211_CIPHER_SUITE_SELECTOR
*SupportedHwCipherSuites
;
323 EFI_80211_SUITE_SELECTOR
*AKMSuite
;
324 EFI_80211_SUITE_SELECTOR
*CipherSuite
;
328 if (Nic
== NULL
|| AKMList
== NULL
|| CipherList
== NULL
|| SecurityType
== NULL
) {
329 return EFI_INVALID_PARAMETER
;
332 SupportedAKMSuites
= Nic
->SupportedSuites
.SupportedAKMSuites
;
333 SupportedSwCipherSuites
= Nic
->SupportedSuites
.SupportedSwCipherSuites
;
334 SupportedHwCipherSuites
= Nic
->SupportedSuites
.SupportedHwCipherSuites
;
336 *SecurityType
= SECURITY_TYPE_UNKNOWN
;
337 if (AKMSuiteSupported
!= NULL
&& CipherSuiteSupported
!= NULL
) {
338 *AKMSuiteSupported
= FALSE
;
339 *CipherSuiteSupported
= FALSE
;
342 if (AKMList
->AKMSuiteCount
== 0) {
343 if (CipherList
->CipherSuiteCount
== 0) {
344 *SecurityType
= SECURITY_TYPE_NONE
;
345 if (AKMSuiteSupported
!= NULL
&& CipherSuiteSupported
!= NULL
) {
346 *AKMSuiteSupported
= TRUE
;
347 *CipherSuiteSupported
= TRUE
;
354 for (AKMIndex
= 0; AKMIndex
< AKMList
->AKMSuiteCount
; AKMIndex
++) {
356 AKMSuite
= AKMList
->AKMSuiteList
+ AKMIndex
;
357 if (WifiMgrSupportAKMSuite(SupportedAKMSuites
->AKMSuiteCount
,
358 (UINT32
*) SupportedAKMSuites
->AKMSuiteList
, (UINT32
*) AKMSuite
)) {
360 if (AKMSuiteSupported
!= NULL
&& CipherSuiteSupported
!= NULL
) {
361 *AKMSuiteSupported
= TRUE
;
363 for (CipherIndex
= 0; CipherIndex
< CipherList
->CipherSuiteCount
; CipherIndex
++) {
365 CipherSuite
= CipherList
->CipherSuiteList
+ CipherIndex
;
367 if (SupportedSwCipherSuites
!= NULL
) {
369 if (WifiMgrSupportCipherSuite(SupportedSwCipherSuites
->CipherSuiteCount
,
370 (UINT32
*) SupportedSwCipherSuites
->CipherSuiteList
, (UINT32
*) CipherSuite
)) {
372 *SecurityType
= WifiMgrGetSecurityType ((UINT32
*) AKMSuite
, (UINT32
*) CipherSuite
);
374 if (*SecurityType
!= SECURITY_TYPE_UNKNOWN
) {
376 if (AKMSuiteSupported
!= NULL
&& CipherSuiteSupported
!= NULL
) {
377 *CipherSuiteSupported
= TRUE
;
384 if (SupportedHwCipherSuites
!= NULL
) {
386 if (WifiMgrSupportCipherSuite(SupportedHwCipherSuites
->CipherSuiteCount
,
387 (UINT32
*) SupportedHwCipherSuites
->CipherSuiteList
, (UINT32
*) CipherSuite
)) {
389 *SecurityType
= WifiMgrGetSecurityType ((UINT32
*) AKMSuite
, (UINT32
*) CipherSuite
);
391 if (*SecurityType
!= SECURITY_TYPE_UNKNOWN
) {
393 if (AKMSuiteSupported
!= NULL
&& CipherSuiteSupported
!= NULL
) {
394 *CipherSuiteSupported
= TRUE
;
404 *SecurityType
= WifiMgrGetSecurityType ((UINT32
*) AKMList
->AKMSuiteList
,
405 (UINT32
*) CipherList
->CipherSuiteList
);
411 Get the security type for a certain AKMSuite and CipherSuite.
413 @param[in] AKMSuite An certain AKMSuite.
414 @param[in] CipherSuite An certain CipherSuite.
416 @return a security type if found, or SECURITY_TYPE_UNKNOWN.
420 WifiMgrGetSecurityType (
422 IN UINT32
*CipherSuite
425 if (CipherSuite
== NULL
) {
427 if (AKMSuite
== NULL
) {
428 return SECURITY_TYPE_NONE
;
430 return SECURITY_TYPE_UNKNOWN
;
432 } else if (*CipherSuite
== IEEE_80211_PAIRWISE_CIPHER_SUITE_USE_GROUP
) {
434 if (AKMSuite
== NULL
) {
435 return SECURITY_TYPE_NONE
;
437 return SECURITY_TYPE_UNKNOWN
;
439 } else if (*CipherSuite
== IEEE_80211_PAIRWISE_CIPHER_SUITE_WEP40
||
440 *CipherSuite
== IEEE_80211_PAIRWISE_CIPHER_SUITE_WEP104
) {
442 return SECURITY_TYPE_WEP
;
443 } else if (*CipherSuite
== IEEE_80211_PAIRWISE_CIPHER_SUITE_CCMP
) {
445 if (AKMSuite
== NULL
) {
446 return SECURITY_TYPE_UNKNOWN
;
449 if (*AKMSuite
== IEEE_80211_AKM_SUITE_8021X_OR_PMKSA
||
450 *AKMSuite
== IEEE_80211_AKM_SUITE_8021X_OR_PMKSA_SHA256
) {
452 return SECURITY_TYPE_WPA2_ENTERPRISE
;
453 } else if (*AKMSuite
== IEEE_80211_AKM_SUITE_PSK
||
454 *AKMSuite
== IEEE_80211_AKM_SUITE_PSK_SHA256
){
456 return SECURITY_TYPE_WPA2_PERSONAL
;
458 return SECURITY_TYPE_UNKNOWN
;
460 } else if (*CipherSuite
== IEEE_80211_PAIRWISE_CIPHER_SUITE_TKIP
) {
462 if (AKMSuite
== NULL
) {
463 return SECURITY_TYPE_UNKNOWN
;
466 if (*AKMSuite
== IEEE_80211_AKM_SUITE_8021X_OR_PMKSA
||
467 *AKMSuite
== IEEE_80211_AKM_SUITE_8021X_OR_PMKSA_SHA256
) {
469 return SECURITY_TYPE_WPA_ENTERPRISE
;
470 } else if (*AKMSuite
== IEEE_80211_AKM_SUITE_PSK
||
471 *AKMSuite
== IEEE_80211_AKM_SUITE_PSK_SHA256
){
473 return SECURITY_TYPE_WPA_PERSONAL
;
475 return SECURITY_TYPE_UNKNOWN
;
478 return SECURITY_TYPE_UNKNOWN
;
483 Get supported AKMSuites and CipherSuites from supplicant for a Nic.
485 @param[in] Nic The Nic to operate.
487 @retval EFI_SUCCESS Get the supported suite list successfully.
488 @retval EFI_INVALID_PARAMETER No Nic found or supplicant is NULL.
492 WifiMgrGetSupportedSuites (
493 IN WIFI_MGR_DEVICE_DATA
*Nic
497 EFI_SUPPLICANT_PROTOCOL
*Supplicant
;
498 EFI_80211_AKM_SUITE_SELECTOR
*SupportedAKMSuites
;
499 EFI_80211_CIPHER_SUITE_SELECTOR
*SupportedSwCipherSuites
;
500 EFI_80211_CIPHER_SUITE_SELECTOR
*SupportedHwCipherSuites
;
503 SupportedAKMSuites
= NULL
;
504 SupportedSwCipherSuites
= NULL
;
505 SupportedHwCipherSuites
= NULL
;
507 if (Nic
== NULL
|| Nic
->Supplicant
== NULL
) {
508 return EFI_INVALID_PARAMETER
;
511 Supplicant
= Nic
->Supplicant
;
514 Status
= Supplicant
->GetData (Supplicant
, EfiSupplicant80211SupportedAKMSuites
, NULL
, &DataSize
);
515 if (Status
== EFI_BUFFER_TOO_SMALL
&& DataSize
> 0) {
517 SupportedAKMSuites
= AllocateZeroPool(DataSize
);
518 if (SupportedAKMSuites
== NULL
) {
519 return EFI_OUT_OF_RESOURCES
;
521 Status
= Supplicant
->GetData (Supplicant
, EfiSupplicant80211SupportedAKMSuites
,
522 (UINT8
*) SupportedAKMSuites
, &DataSize
);
523 if (!EFI_ERROR (Status
)) {
524 Nic
->SupportedSuites
.SupportedAKMSuites
= SupportedAKMSuites
;
526 FreePool (SupportedAKMSuites
);
529 SupportedAKMSuites
= NULL
;
533 Status
= Supplicant
->GetData (Supplicant
, EfiSupplicant80211SupportedSoftwareCipherSuites
, NULL
, &DataSize
);
534 if (Status
== EFI_BUFFER_TOO_SMALL
&& DataSize
> 0) {
537 SupportedSwCipherSuites
= AllocateZeroPool(DataSize
);
538 if (SupportedSwCipherSuites
== NULL
) {
539 return EFI_OUT_OF_RESOURCES
;
541 Status
= Supplicant
->GetData (Supplicant
, EfiSupplicant80211SupportedSoftwareCipherSuites
,
542 (UINT8
*) SupportedSwCipherSuites
, &DataSize
);
543 if (!EFI_ERROR (Status
)) {
544 Nic
->SupportedSuites
.SupportedSwCipherSuites
= SupportedSwCipherSuites
;
546 FreePool (SupportedSwCipherSuites
);
549 SupportedSwCipherSuites
= NULL
;
553 Status
= Supplicant
->GetData (Supplicant
, EfiSupplicant80211SupportedHardwareCipherSuites
, NULL
, &DataSize
);
554 if (Status
== EFI_BUFFER_TOO_SMALL
&& DataSize
> 0) {
556 SupportedHwCipherSuites
= AllocateZeroPool(DataSize
);
557 if (SupportedHwCipherSuites
== NULL
) {
558 return EFI_OUT_OF_RESOURCES
;
560 Status
= Supplicant
->GetData (Supplicant
, EfiSupplicant80211SupportedHardwareCipherSuites
,
561 (UINT8
*) SupportedHwCipherSuites
, &DataSize
);
562 if (!EFI_ERROR (Status
)) {
563 Nic
->SupportedSuites
.SupportedHwCipherSuites
= SupportedHwCipherSuites
;
565 FreePool (SupportedHwCipherSuites
);
568 SupportedHwCipherSuites
= NULL
;
575 Clean secrets from a network profile.
577 @param[in] Profile The profile to be cleanned.
581 WifiMgrCleanProfileSecrets (
582 IN WIFI_MGR_NETWORK_PROFILE
*Profile
585 ZeroMem (Profile
->Password
, sizeof (CHAR16
) * PASSWORD_STORAGE_SIZE
);
586 ZeroMem (Profile
->EapPassword
, sizeof (CHAR16
) * PASSWORD_STORAGE_SIZE
);
587 ZeroMem (Profile
->PrivateKeyPassword
, sizeof (CHAR16
) * PASSWORD_STORAGE_SIZE
);
589 if (Profile
->CACertData
!= NULL
) {
591 ZeroMem (Profile
->CACertData
, Profile
->CACertSize
);
592 FreePool (Profile
->CACertData
);
594 Profile
->CACertData
= NULL
;
595 Profile
->CACertSize
= 0;
597 if (Profile
->ClientCertData
!= NULL
) {
599 ZeroMem (Profile
->ClientCertData
, Profile
->ClientCertSize
);
600 FreePool (Profile
->ClientCertData
);
602 Profile
->ClientCertData
= NULL
;
603 Profile
->ClientCertSize
= 0;
605 if (Profile
->PrivateKeyData
!= NULL
) {
607 ZeroMem (Profile
->PrivateKeyData
, Profile
->PrivateKeyDataSize
);
608 FreePool (Profile
->PrivateKeyData
);
610 Profile
->PrivateKeyData
= NULL
;
611 Profile
->PrivateKeyDataSize
= 0;
615 Free all network profiles in a profile list.
617 @param[in] ProfileList The profile list to be freed.
621 WifiMgrFreeProfileList (
622 IN LIST_ENTRY
*ProfileList
625 WIFI_MGR_NETWORK_PROFILE
*Profile
;
627 LIST_ENTRY
*NextEntry
;
629 if (ProfileList
== NULL
) {
633 NET_LIST_FOR_EACH_SAFE (Entry
, NextEntry
, ProfileList
) {
635 Profile
= NET_LIST_USER_STRUCT_S (Entry
, WIFI_MGR_NETWORK_PROFILE
,
636 Link
, WIFI_MGR_PROFILE_SIGNATURE
);
638 WifiMgrCleanProfileSecrets (Profile
);
640 if (Profile
->Network
.AKMSuite
!= NULL
) {
641 FreePool(Profile
->Network
.AKMSuite
);
644 if (Profile
->Network
.CipherSuite
!= NULL
) {
645 FreePool(Profile
->Network
.CipherSuite
);
653 Free user configured hidden network list.
655 @param[in] HiddenList The hidden network list to be freed.
659 WifiMgrFreeHiddenList (
660 IN LIST_ENTRY
*HiddenList
663 WIFI_HIDDEN_NETWORK_DATA
*HiddenNetwork
;
665 LIST_ENTRY
*NextEntry
;
667 if (HiddenList
== NULL
) {
671 NET_LIST_FOR_EACH_SAFE (Entry
, NextEntry
, HiddenList
) {
673 HiddenNetwork
= NET_LIST_USER_STRUCT_S (Entry
, WIFI_HIDDEN_NETWORK_DATA
,
674 Link
, WIFI_MGR_HIDDEN_NETWORK_SIGNATURE
);
675 FreePool (HiddenNetwork
);
681 Free the resources of a config token.
683 @param[in] ConfigToken The config token to be freed.
687 IN WIFI_MGR_MAC_CONFIG_TOKEN
*ConfigToken
690 EFI_80211_GET_NETWORKS_RESULT
*Result
;
692 if (ConfigToken
== NULL
) {
696 switch (ConfigToken
->Type
) {
698 case TokenTypeGetNetworksToken
:
700 if (ConfigToken
->Token
.GetNetworksToken
!= NULL
) {
702 gBS
->CloseEvent (ConfigToken
->Token
.GetNetworksToken
->Event
);
703 if (ConfigToken
->Token
.GetNetworksToken
->Data
!= NULL
) {
704 FreePool(ConfigToken
->Token
.GetNetworksToken
->Data
);
707 Result
= ConfigToken
->Token
.GetNetworksToken
->Result
;
708 if (Result
!= NULL
) {
712 FreePool(ConfigToken
->Token
.GetNetworksToken
);
715 FreePool (ConfigToken
);
718 case TokenTypeConnectNetworkToken
:
720 if (ConfigToken
->Token
.ConnectNetworkToken
!= NULL
) {
722 gBS
->CloseEvent (ConfigToken
->Token
.ConnectNetworkToken
->Event
);
723 if (ConfigToken
->Token
.ConnectNetworkToken
->Data
!= NULL
) {
724 FreePool(ConfigToken
->Token
.ConnectNetworkToken
->Data
);
726 FreePool(ConfigToken
->Token
.ConnectNetworkToken
);
728 FreePool (ConfigToken
);
731 case TokenTypeDisconnectNetworkToken
:
733 if (ConfigToken
->Token
.DisconnectNetworkToken
!= NULL
) {
735 FreePool(ConfigToken
->Token
.DisconnectNetworkToken
);
738 FreePool (ConfigToken
);