2 The Miscellaneous Routines for WiFi Connection Manager.
4 Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php.
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 #include "WifiConnectionMgrDxe.h"
19 Empty function for event process function.
21 @param Event The Event need to be process
22 @param Context The context of the event.
27 WifiMgrInternalEmptyFunction (
36 Convert the mac address into a hexadecimal encoded ":" seperated string.
38 @param[in] Mac The mac address.
39 @param[in] StrSize The size, in bytes, of the output buffer specified by Str.
40 @param[out] Str The storage to return the mac string.
45 IN EFI_80211_MAC_ADDRESS
*Mac
,
50 if (Mac
== NULL
|| Str
== NULL
) {
57 L
"%02X:%02X:%02X:%02X:%02X:%02X",
58 Mac
->Addr
[0], Mac
->Addr
[1], Mac
->Addr
[2],
59 Mac
->Addr
[3], Mac
->Addr
[4], Mac
->Addr
[5]
64 Read private key file to buffer.
66 @param[in] FileContext The file context of private key file.
67 @param[out] PrivateKeyDataAddr The buffer address to restore private key file, should be
69 @param[out] PrivateKeyDataSize The size of read private key file.
71 @retval EFI_SUCCESS Successfully read the private key file.
72 @retval EFI_INVALID_PARAMETER One or more of the parameters is invalid.
76 WifiMgrReadFileToBuffer (
77 IN WIFI_MGR_FILE_CONTEXT
*FileContext
,
84 if (FileContext
!= NULL
&& FileContext
->FHandle
!= NULL
) {
86 Status
= ReadFileContent (
93 if (FileContext
->FHandle
!= NULL
) {
94 FileContext
->FHandle
->Close (FileContext
->FHandle
);
96 FileContext
->FHandle
= NULL
;
100 return EFI_INVALID_PARAMETER
;
104 Get the Nic data by the NicIndex.
106 @param[in] Private The pointer to the global private data structure.
107 @param[in] NicIndex The index indicates the position of wireless NIC.
109 @return Pointer to the Nic data, or NULL if not found.
112 WIFI_MGR_DEVICE_DATA
*
113 WifiMgrGetNicByIndex (
114 IN WIFI_MGR_PRIVATE_DATA
*Private
,
119 WIFI_MGR_DEVICE_DATA
*Nic
;
121 if (Private
== NULL
) {
125 NET_LIST_FOR_EACH (Entry
, &Private
->NicList
) {
126 Nic
= NET_LIST_USER_STRUCT_S (Entry
, WIFI_MGR_DEVICE_DATA
,
127 Link
, WIFI_MGR_DEVICE_DATA_SIGNATURE
);
128 if (Nic
->NicIndex
== NicIndex
) {
137 Find a network profile through its' SSId and securit type, and the SSId is an unicode string.
139 @param[in] SSId The target network's SSId.
140 @param[in] SecurityType The target network's security type.
141 @param[in] ProfileList The profile list on a Nic.
143 @return Pointer to a network profile, or NULL if not found.
146 WIFI_MGR_NETWORK_PROFILE
*
147 WifiMgrGetProfileByUnicodeSSId (
149 IN UINT8 SecurityType
,
150 IN LIST_ENTRY
*ProfileList
154 WIFI_MGR_NETWORK_PROFILE
*Profile
;
156 if (SSId
== NULL
|| ProfileList
== NULL
) {
160 NET_LIST_FOR_EACH (Entry
, ProfileList
) {
161 Profile
= NET_LIST_USER_STRUCT_S (Entry
, WIFI_MGR_NETWORK_PROFILE
,
162 Link
, WIFI_MGR_PROFILE_SIGNATURE
);
163 if (StrCmp (SSId
, Profile
->SSId
) == 0 && SecurityType
== Profile
->SecurityType
) {
172 Find a network profile through its' SSId and securit type, and the SSId is an ascii string.
174 @param[in] SSId The target network's SSId.
175 @param[in] SecurityType The target network's security type.
176 @param[in] ProfileList The profile list on a Nic.
178 @return Pointer to a network profile, or NULL if not found.
181 WIFI_MGR_NETWORK_PROFILE
*
182 WifiMgrGetProfileByAsciiSSId (
184 IN UINT8 SecurityType
,
185 IN LIST_ENTRY
*ProfileList
188 CHAR16 SSIdUniCode
[SSID_STORAGE_SIZE
];
193 if (AsciiStrToUnicodeStrS (SSId
, SSIdUniCode
, SSID_STORAGE_SIZE
) != RETURN_SUCCESS
) {
197 return WifiMgrGetProfileByUnicodeSSId (SSIdUniCode
, SecurityType
, ProfileList
);
201 Find a network profile through its' profile index.
203 @param[in] ProfileIndex The target network's profile index.
204 @param[in] ProfileList The profile list on a Nic.
206 @return Pointer to a network profile, or NULL if not found.
209 WIFI_MGR_NETWORK_PROFILE
*
210 WifiMgrGetProfileByProfileIndex (
211 IN UINT32 ProfileIndex
,
212 IN LIST_ENTRY
*ProfileList
215 WIFI_MGR_NETWORK_PROFILE
*Profile
;
218 if (ProfileList
== NULL
) {
221 NET_LIST_FOR_EACH (Entry
, ProfileList
) {
222 Profile
= NET_LIST_USER_STRUCT_S (Entry
, WIFI_MGR_NETWORK_PROFILE
,
223 Link
, WIFI_MGR_PROFILE_SIGNATURE
);
224 if (Profile
->ProfileIndex
== ProfileIndex
) {
232 To test if the AKMSuite is in supported AKMSuite list.
234 @param[in] SupportedAKMSuiteCount The count of the supported AKMSuites.
235 @param[in] SupportedAKMSuiteList The supported AKMSuite list.
236 @param[in] AKMSuite The AKMSuite to be tested.
238 @return True if this AKMSuite is supported, or False if not.
242 WifiMgrSupportAKMSuite (
243 IN UINT16 SupportedAKMSuiteCount
,
244 IN UINT32
*SupportedAKMSuiteList
,
250 if (AKMSuite
== NULL
|| SupportedAKMSuiteList
== NULL
||
251 SupportedAKMSuiteCount
== 0) {
255 for (Index
= 0; Index
< SupportedAKMSuiteCount
; Index
++) {
256 if (SupportedAKMSuiteList
[Index
] == *AKMSuite
) {
265 To check if the CipherSuite is in supported CipherSuite list.
267 @param[in] SupportedCipherSuiteCount The count of the supported CipherSuites.
268 @param[in] SupportedCipherSuiteList The supported CipherSuite list.
269 @param[in] CipherSuite The CipherSuite to be tested.
271 @return True if this CipherSuite is supported, or False if not.
275 WifiMgrSupportCipherSuite (
276 IN UINT16 SupportedCipherSuiteCount
,
277 IN UINT32
*SupportedCipherSuiteList
,
278 IN UINT32
*CipherSuite
283 if (CipherSuite
== NULL
|| SupportedCipherSuiteCount
== 0 ||
284 SupportedCipherSuiteList
== NULL
) {
288 for (Index
= 0; Index
< SupportedCipherSuiteCount
; Index
++) {
289 if (SupportedCipherSuiteList
[Index
] == *CipherSuite
) {
298 Check an AKM suite list and a Cipher suite list to see if one or more AKM suites or Cipher suites
299 are supported and find the matchable security type.
301 @param[in] AKMList The target AKM suite list to be checked.
302 @param[in] CipherList The target Cipher suite list to be checked
303 @param[in] Nic The Nic to operate, contains the supported AKMSuite list
304 and supported CipherSuite list
305 @param[out] SecurityType To identify a security type from the AKM suite list and
307 @param[out] AKMSuiteSupported To identify if this security type is supported. If it is
308 NULL, overcome this field
309 @param[out] CipherSuiteSupported To identify if this security type is supported. If it is
310 NULL, overcome this field
312 @retval EFI_SUCCESS This operation has completed successfully.
313 @retval EFI_INVALID_PARAMETER No Nic found or the suite list is null.
318 IN EFI_80211_AKM_SUITE_SELECTOR
*AKMList
,
319 IN EFI_80211_CIPHER_SUITE_SELECTOR
*CipherList
,
320 IN WIFI_MGR_DEVICE_DATA
*Nic
,
321 OUT UINT8
*SecurityType
,
322 OUT BOOLEAN
*AKMSuiteSupported
,
323 OUT BOOLEAN
*CipherSuiteSupported
326 EFI_80211_AKM_SUITE_SELECTOR
*SupportedAKMSuites
;
327 EFI_80211_CIPHER_SUITE_SELECTOR
*SupportedSwCipherSuites
;
328 EFI_80211_CIPHER_SUITE_SELECTOR
*SupportedHwCipherSuites
;
329 EFI_80211_SUITE_SELECTOR
*AKMSuite
;
330 EFI_80211_SUITE_SELECTOR
*CipherSuite
;
334 if (Nic
== NULL
|| AKMList
== NULL
|| CipherList
== NULL
|| SecurityType
== NULL
) {
335 return EFI_INVALID_PARAMETER
;
338 SupportedAKMSuites
= Nic
->SupportedSuites
.SupportedAKMSuites
;
339 SupportedSwCipherSuites
= Nic
->SupportedSuites
.SupportedSwCipherSuites
;
340 SupportedHwCipherSuites
= Nic
->SupportedSuites
.SupportedHwCipherSuites
;
342 *SecurityType
= SECURITY_TYPE_UNKNOWN
;
343 if (AKMSuiteSupported
!= NULL
&& CipherSuiteSupported
!= NULL
) {
344 *AKMSuiteSupported
= FALSE
;
345 *CipherSuiteSupported
= FALSE
;
348 if (AKMList
->AKMSuiteCount
== 0) {
349 if (CipherList
->CipherSuiteCount
== 0) {
350 *SecurityType
= SECURITY_TYPE_NONE
;
351 if (AKMSuiteSupported
!= NULL
&& CipherSuiteSupported
!= NULL
) {
352 *AKMSuiteSupported
= TRUE
;
353 *CipherSuiteSupported
= TRUE
;
360 for (AKMIndex
= 0; AKMIndex
< AKMList
->AKMSuiteCount
; AKMIndex
++) {
362 AKMSuite
= AKMList
->AKMSuiteList
+ AKMIndex
;
363 if (WifiMgrSupportAKMSuite(SupportedAKMSuites
->AKMSuiteCount
,
364 (UINT32
*) SupportedAKMSuites
->AKMSuiteList
, (UINT32
*) AKMSuite
)) {
366 if (AKMSuiteSupported
!= NULL
&& CipherSuiteSupported
!= NULL
) {
367 *AKMSuiteSupported
= TRUE
;
369 for (CipherIndex
= 0; CipherIndex
< CipherList
->CipherSuiteCount
; CipherIndex
++) {
371 CipherSuite
= CipherList
->CipherSuiteList
+ CipherIndex
;
373 if (SupportedSwCipherSuites
!= NULL
) {
375 if (WifiMgrSupportCipherSuite(SupportedSwCipherSuites
->CipherSuiteCount
,
376 (UINT32
*) SupportedSwCipherSuites
->CipherSuiteList
, (UINT32
*) CipherSuite
)) {
378 *SecurityType
= WifiMgrGetSecurityType ((UINT32
*) AKMSuite
, (UINT32
*) CipherSuite
);
380 if (*SecurityType
!= SECURITY_TYPE_UNKNOWN
) {
382 if (AKMSuiteSupported
!= NULL
&& CipherSuiteSupported
!= NULL
) {
383 *CipherSuiteSupported
= TRUE
;
390 if (SupportedHwCipherSuites
!= NULL
) {
392 if (WifiMgrSupportCipherSuite(SupportedHwCipherSuites
->CipherSuiteCount
,
393 (UINT32
*) SupportedHwCipherSuites
->CipherSuiteList
, (UINT32
*) CipherSuite
)) {
395 *SecurityType
= WifiMgrGetSecurityType ((UINT32
*) AKMSuite
, (UINT32
*) CipherSuite
);
397 if (*SecurityType
!= SECURITY_TYPE_UNKNOWN
) {
399 if (AKMSuiteSupported
!= NULL
&& CipherSuiteSupported
!= NULL
) {
400 *CipherSuiteSupported
= TRUE
;
410 *SecurityType
= WifiMgrGetSecurityType ((UINT32
*) AKMList
->AKMSuiteList
,
411 (UINT32
*) CipherList
->CipherSuiteList
);
417 Get the security type for a certain AKMSuite and CipherSuite.
419 @param[in] AKMSuite An certain AKMSuite.
420 @param[in] CipherSuite An certain CipherSuite.
422 @return a security type if found, or SECURITY_TYPE_UNKNOWN.
426 WifiMgrGetSecurityType (
428 IN UINT32
*CipherSuite
431 if (CipherSuite
== NULL
) {
433 if (AKMSuite
== NULL
) {
434 return SECURITY_TYPE_NONE
;
436 return SECURITY_TYPE_UNKNOWN
;
438 } else if (*CipherSuite
== IEEE_80211_PAIRWISE_CIPHER_SUITE_USE_GROUP
) {
440 if (AKMSuite
== NULL
) {
441 return SECURITY_TYPE_NONE
;
443 return SECURITY_TYPE_UNKNOWN
;
445 } else if (*CipherSuite
== IEEE_80211_PAIRWISE_CIPHER_SUITE_WEP40
||
446 *CipherSuite
== IEEE_80211_PAIRWISE_CIPHER_SUITE_WEP104
) {
448 return SECURITY_TYPE_WEP
;
449 } else if (*CipherSuite
== IEEE_80211_PAIRWISE_CIPHER_SUITE_CCMP
) {
451 if (AKMSuite
== NULL
) {
452 return SECURITY_TYPE_UNKNOWN
;
455 if (*AKMSuite
== IEEE_80211_AKM_SUITE_8021X_OR_PMKSA
||
456 *AKMSuite
== IEEE_80211_AKM_SUITE_8021X_OR_PMKSA_SHA256
) {
458 return SECURITY_TYPE_WPA2_ENTERPRISE
;
459 } else if (*AKMSuite
== IEEE_80211_AKM_SUITE_PSK
||
460 *AKMSuite
== IEEE_80211_AKM_SUITE_PSK_SHA256
){
462 return SECURITY_TYPE_WPA2_PERSONAL
;
464 return SECURITY_TYPE_UNKNOWN
;
466 } else if (*CipherSuite
== IEEE_80211_PAIRWISE_CIPHER_SUITE_TKIP
) {
468 if (AKMSuite
== NULL
) {
469 return SECURITY_TYPE_UNKNOWN
;
472 if (*AKMSuite
== IEEE_80211_AKM_SUITE_8021X_OR_PMKSA
||
473 *AKMSuite
== IEEE_80211_AKM_SUITE_8021X_OR_PMKSA_SHA256
) {
475 return SECURITY_TYPE_WPA_ENTERPRISE
;
476 } else if (*AKMSuite
== IEEE_80211_AKM_SUITE_PSK
||
477 *AKMSuite
== IEEE_80211_AKM_SUITE_PSK_SHA256
){
479 return SECURITY_TYPE_WPA_PERSONAL
;
481 return SECURITY_TYPE_UNKNOWN
;
484 return SECURITY_TYPE_UNKNOWN
;
489 Get supported AKMSuites and CipherSuites from supplicant for a Nic.
491 @param[in] Nic The Nic to operate.
493 @retval EFI_SUCCESS Get the supported suite list successfully.
494 @retval EFI_INVALID_PARAMETER No Nic found or supplicant is NULL.
498 WifiMgrGetSupportedSuites (
499 IN WIFI_MGR_DEVICE_DATA
*Nic
503 EFI_SUPPLICANT_PROTOCOL
*Supplicant
;
504 EFI_80211_AKM_SUITE_SELECTOR
*SupportedAKMSuites
;
505 EFI_80211_CIPHER_SUITE_SELECTOR
*SupportedSwCipherSuites
;
506 EFI_80211_CIPHER_SUITE_SELECTOR
*SupportedHwCipherSuites
;
509 SupportedAKMSuites
= NULL
;
510 SupportedSwCipherSuites
= NULL
;
511 SupportedHwCipherSuites
= NULL
;
513 if (Nic
== NULL
|| Nic
->Supplicant
== NULL
) {
514 return EFI_INVALID_PARAMETER
;
517 Supplicant
= Nic
->Supplicant
;
520 Status
= Supplicant
->GetData (Supplicant
, EfiSupplicant80211SupportedAKMSuites
, NULL
, &DataSize
);
521 if (Status
== EFI_BUFFER_TOO_SMALL
&& DataSize
> 0) {
523 SupportedAKMSuites
= AllocateZeroPool(DataSize
);
524 if (SupportedAKMSuites
== NULL
) {
525 return EFI_OUT_OF_RESOURCES
;
527 Status
= Supplicant
->GetData (Supplicant
, EfiSupplicant80211SupportedAKMSuites
,
528 (UINT8
*) SupportedAKMSuites
, &DataSize
);
529 if (!EFI_ERROR (Status
)) {
530 Nic
->SupportedSuites
.SupportedAKMSuites
= SupportedAKMSuites
;
532 FreePool (SupportedAKMSuites
);
535 SupportedAKMSuites
= NULL
;
539 Status
= Supplicant
->GetData (Supplicant
, EfiSupplicant80211SupportedSoftwareCipherSuites
, NULL
, &DataSize
);
540 if (Status
== EFI_BUFFER_TOO_SMALL
&& DataSize
> 0) {
543 SupportedSwCipherSuites
= AllocateZeroPool(DataSize
);
544 if (SupportedSwCipherSuites
== NULL
) {
545 return EFI_OUT_OF_RESOURCES
;
547 Status
= Supplicant
->GetData (Supplicant
, EfiSupplicant80211SupportedSoftwareCipherSuites
,
548 (UINT8
*) SupportedSwCipherSuites
, &DataSize
);
549 if (!EFI_ERROR (Status
)) {
550 Nic
->SupportedSuites
.SupportedSwCipherSuites
= SupportedSwCipherSuites
;
552 FreePool (SupportedSwCipherSuites
);
555 SupportedSwCipherSuites
= NULL
;
559 Status
= Supplicant
->GetData (Supplicant
, EfiSupplicant80211SupportedHardwareCipherSuites
, NULL
, &DataSize
);
560 if (Status
== EFI_BUFFER_TOO_SMALL
&& DataSize
> 0) {
562 SupportedHwCipherSuites
= AllocateZeroPool(DataSize
);
563 if (SupportedHwCipherSuites
== NULL
) {
564 return EFI_OUT_OF_RESOURCES
;
566 Status
= Supplicant
->GetData (Supplicant
, EfiSupplicant80211SupportedHardwareCipherSuites
,
567 (UINT8
*) SupportedHwCipherSuites
, &DataSize
);
568 if (!EFI_ERROR (Status
)) {
569 Nic
->SupportedSuites
.SupportedHwCipherSuites
= SupportedHwCipherSuites
;
571 FreePool (SupportedHwCipherSuites
);
574 SupportedHwCipherSuites
= NULL
;
581 Clean secrets from a network profile.
583 @param[in] Profile The profile to be cleanned.
587 WifiMgrCleanProfileSecrets (
588 IN WIFI_MGR_NETWORK_PROFILE
*Profile
591 ZeroMem (Profile
->Password
, sizeof (CHAR16
) * PASSWORD_STORAGE_SIZE
);
592 ZeroMem (Profile
->EapPassword
, sizeof (CHAR16
) * PASSWORD_STORAGE_SIZE
);
593 ZeroMem (Profile
->PrivateKeyPassword
, sizeof (CHAR16
) * PASSWORD_STORAGE_SIZE
);
595 if (Profile
->CACertData
!= NULL
) {
597 ZeroMem (Profile
->CACertData
, Profile
->CACertSize
);
598 FreePool (Profile
->CACertData
);
600 Profile
->CACertData
= NULL
;
601 Profile
->CACertSize
= 0;
603 if (Profile
->ClientCertData
!= NULL
) {
605 ZeroMem (Profile
->ClientCertData
, Profile
->ClientCertSize
);
606 FreePool (Profile
->ClientCertData
);
608 Profile
->ClientCertData
= NULL
;
609 Profile
->ClientCertSize
= 0;
611 if (Profile
->PrivateKeyData
!= NULL
) {
613 ZeroMem (Profile
->PrivateKeyData
, Profile
->PrivateKeyDataSize
);
614 FreePool (Profile
->PrivateKeyData
);
616 Profile
->PrivateKeyData
= NULL
;
617 Profile
->PrivateKeyDataSize
= 0;
621 Free all network profiles in a profile list.
623 @param[in] ProfileList The profile list to be freed.
627 WifiMgrFreeProfileList (
628 IN LIST_ENTRY
*ProfileList
631 WIFI_MGR_NETWORK_PROFILE
*Profile
;
633 LIST_ENTRY
*NextEntry
;
635 if (ProfileList
== NULL
) {
639 NET_LIST_FOR_EACH_SAFE (Entry
, NextEntry
, ProfileList
) {
641 Profile
= NET_LIST_USER_STRUCT_S (Entry
, WIFI_MGR_NETWORK_PROFILE
,
642 Link
, WIFI_MGR_PROFILE_SIGNATURE
);
644 WifiMgrCleanProfileSecrets (Profile
);
646 if (Profile
->Network
.AKMSuite
!= NULL
) {
647 FreePool(Profile
->Network
.AKMSuite
);
650 if (Profile
->Network
.CipherSuite
!= NULL
) {
651 FreePool(Profile
->Network
.CipherSuite
);
659 Free user configured hidden network list.
661 @param[in] HiddenList The hidden network list to be freed.
665 WifiMgrFreeHiddenList (
666 IN LIST_ENTRY
*HiddenList
669 WIFI_HIDDEN_NETWORK_DATA
*HiddenNetwork
;
671 LIST_ENTRY
*NextEntry
;
673 if (HiddenList
== NULL
) {
677 NET_LIST_FOR_EACH_SAFE (Entry
, NextEntry
, HiddenList
) {
679 HiddenNetwork
= NET_LIST_USER_STRUCT_S (Entry
, WIFI_HIDDEN_NETWORK_DATA
,
680 Link
, WIFI_MGR_HIDDEN_NETWORK_SIGNATURE
);
681 FreePool (HiddenNetwork
);
687 Free the resources of a config token.
689 @param[in] ConfigToken The config token to be freed.
693 IN WIFI_MGR_MAC_CONFIG_TOKEN
*ConfigToken
696 EFI_80211_GET_NETWORKS_RESULT
*Result
;
698 if (ConfigToken
== NULL
) {
702 switch (ConfigToken
->Type
) {
704 case TokenTypeGetNetworksToken
:
706 if (ConfigToken
->Token
.GetNetworksToken
!= NULL
) {
708 gBS
->CloseEvent (ConfigToken
->Token
.GetNetworksToken
->Event
);
709 if (ConfigToken
->Token
.GetNetworksToken
->Data
!= NULL
) {
710 FreePool(ConfigToken
->Token
.GetNetworksToken
->Data
);
713 Result
= ConfigToken
->Token
.GetNetworksToken
->Result
;
714 if (Result
!= NULL
) {
718 FreePool(ConfigToken
->Token
.GetNetworksToken
);
721 FreePool (ConfigToken
);
724 case TokenTypeConnectNetworkToken
:
726 if (ConfigToken
->Token
.ConnectNetworkToken
!= NULL
) {
728 gBS
->CloseEvent (ConfigToken
->Token
.ConnectNetworkToken
->Event
);
729 if (ConfigToken
->Token
.ConnectNetworkToken
->Data
!= NULL
) {
730 FreePool(ConfigToken
->Token
.ConnectNetworkToken
->Data
);
732 FreePool(ConfigToken
->Token
.ConnectNetworkToken
);
734 FreePool (ConfigToken
);
737 case TokenTypeDisconnectNetworkToken
:
739 if (ConfigToken
->Token
.DisconnectNetworkToken
!= NULL
) {
741 FreePool(ConfigToken
->Token
.DisconnectNetworkToken
);
744 FreePool (ConfigToken
);