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",
62 Read private key file to buffer.
64 @param[in] FileContext The file context of private key file.
65 @param[out] PrivateKeyDataAddr The buffer address to restore private key file, should be
67 @param[out] PrivateKeyDataSize The size of read private key file.
69 @retval EFI_SUCCESS Successfully read the private key file.
70 @retval EFI_INVALID_PARAMETER One or more of the parameters is invalid.
74 WifiMgrReadFileToBuffer (
75 IN WIFI_MGR_FILE_CONTEXT
*FileContext
,
82 if ((FileContext
!= NULL
) && (FileContext
->FHandle
!= NULL
)) {
83 Status
= ReadFileContent (
90 if (FileContext
->FHandle
!= NULL
) {
91 FileContext
->FHandle
->Close (FileContext
->FHandle
);
94 FileContext
->FHandle
= NULL
;
98 return EFI_INVALID_PARAMETER
;
102 Get the Nic data by the NicIndex.
104 @param[in] Private The pointer to the global private data structure.
105 @param[in] NicIndex The index indicates the position of wireless NIC.
107 @return Pointer to the Nic data, or NULL if not found.
110 WIFI_MGR_DEVICE_DATA
*
111 WifiMgrGetNicByIndex (
112 IN WIFI_MGR_PRIVATE_DATA
*Private
,
117 WIFI_MGR_DEVICE_DATA
*Nic
;
119 if (Private
== NULL
) {
123 NET_LIST_FOR_EACH (Entry
, &Private
->NicList
) {
124 Nic
= NET_LIST_USER_STRUCT_S (
126 WIFI_MGR_DEVICE_DATA
,
128 WIFI_MGR_DEVICE_DATA_SIGNATURE
130 if (Nic
->NicIndex
== NicIndex
) {
139 Find a network profile through its' SSId and securit type, and the SSId is an unicode string.
141 @param[in] SSId The target network's SSId.
142 @param[in] SecurityType The target network's security type.
143 @param[in] ProfileList The profile list on a Nic.
145 @return Pointer to a network profile, or NULL if not found.
148 WIFI_MGR_NETWORK_PROFILE
*
149 WifiMgrGetProfileByUnicodeSSId (
151 IN UINT8 SecurityType
,
152 IN LIST_ENTRY
*ProfileList
156 WIFI_MGR_NETWORK_PROFILE
*Profile
;
158 if ((SSId
== NULL
) || (ProfileList
== NULL
)) {
162 NET_LIST_FOR_EACH (Entry
, ProfileList
) {
163 Profile
= NET_LIST_USER_STRUCT_S (
165 WIFI_MGR_NETWORK_PROFILE
,
167 WIFI_MGR_PROFILE_SIGNATURE
169 if ((StrCmp (SSId
, Profile
->SSId
) == 0) && (SecurityType
== Profile
->SecurityType
)) {
178 Find a network profile through its' SSId and securit type, and the SSId is an ascii string.
180 @param[in] SSId The target network's SSId.
181 @param[in] SecurityType The target network's security type.
182 @param[in] ProfileList The profile list on a Nic.
184 @return Pointer to a network profile, or NULL if not found.
187 WIFI_MGR_NETWORK_PROFILE
*
188 WifiMgrGetProfileByAsciiSSId (
190 IN UINT8 SecurityType
,
191 IN LIST_ENTRY
*ProfileList
194 CHAR16 SSIdUniCode
[SSID_STORAGE_SIZE
];
200 if (AsciiStrToUnicodeStrS (SSId
, SSIdUniCode
, SSID_STORAGE_SIZE
) != RETURN_SUCCESS
) {
204 return WifiMgrGetProfileByUnicodeSSId (SSIdUniCode
, SecurityType
, ProfileList
);
208 Find a network profile through its' profile index.
210 @param[in] ProfileIndex The target network's profile index.
211 @param[in] ProfileList The profile list on a Nic.
213 @return Pointer to a network profile, or NULL if not found.
216 WIFI_MGR_NETWORK_PROFILE
*
217 WifiMgrGetProfileByProfileIndex (
218 IN UINT32 ProfileIndex
,
219 IN LIST_ENTRY
*ProfileList
222 WIFI_MGR_NETWORK_PROFILE
*Profile
;
225 if (ProfileList
== NULL
) {
229 NET_LIST_FOR_EACH (Entry
, ProfileList
) {
230 Profile
= NET_LIST_USER_STRUCT_S (
232 WIFI_MGR_NETWORK_PROFILE
,
234 WIFI_MGR_PROFILE_SIGNATURE
236 if (Profile
->ProfileIndex
== ProfileIndex
) {
244 To test if the AKMSuite is in supported AKMSuite list.
246 @param[in] SupportedAKMSuiteCount The count of the supported AKMSuites.
247 @param[in] SupportedAKMSuiteList The supported AKMSuite list.
248 @param[in] AKMSuite The AKMSuite to be tested.
250 @return True if this AKMSuite is supported, or False if not.
254 WifiMgrSupportAKMSuite (
255 IN UINT16 SupportedAKMSuiteCount
,
256 IN UINT32
*SupportedAKMSuiteList
,
262 if ((AKMSuite
== NULL
) || (SupportedAKMSuiteList
== NULL
) ||
263 (SupportedAKMSuiteCount
== 0))
268 for (Index
= 0; Index
< SupportedAKMSuiteCount
; Index
++) {
269 if (SupportedAKMSuiteList
[Index
] == *AKMSuite
) {
278 To check if the CipherSuite is in supported CipherSuite list.
280 @param[in] SupportedCipherSuiteCount The count of the supported CipherSuites.
281 @param[in] SupportedCipherSuiteList The supported CipherSuite list.
282 @param[in] CipherSuite The CipherSuite to be tested.
284 @return True if this CipherSuite is supported, or False if not.
288 WifiMgrSupportCipherSuite (
289 IN UINT16 SupportedCipherSuiteCount
,
290 IN UINT32
*SupportedCipherSuiteList
,
291 IN UINT32
*CipherSuite
296 if ((CipherSuite
== NULL
) || (SupportedCipherSuiteCount
== 0) ||
297 (SupportedCipherSuiteList
== NULL
))
302 for (Index
= 0; Index
< SupportedCipherSuiteCount
; Index
++) {
303 if (SupportedCipherSuiteList
[Index
] == *CipherSuite
) {
312 Check an AKM suite list and a Cipher suite list to see if one or more AKM suites or Cipher suites
313 are supported and find the matchable security type.
315 @param[in] AKMList The target AKM suite list to be checked.
316 @param[in] CipherList The target Cipher suite list to be checked
317 @param[in] Nic The Nic to operate, contains the supported AKMSuite list
318 and supported CipherSuite list
319 @param[out] SecurityType To identify a security type from the AKM suite list and
321 @param[out] AKMSuiteSupported To identify if this security type is supported. If it is
322 NULL, overcome this field
323 @param[out] CipherSuiteSupported To identify if this security type is supported. If it is
324 NULL, overcome this field
326 @retval EFI_SUCCESS This operation has completed successfully.
327 @retval EFI_INVALID_PARAMETER No Nic found or the suite list is null.
332 IN EFI_80211_AKM_SUITE_SELECTOR
*AKMList
,
333 IN EFI_80211_CIPHER_SUITE_SELECTOR
*CipherList
,
334 IN WIFI_MGR_DEVICE_DATA
*Nic
,
335 OUT UINT8
*SecurityType
,
336 OUT BOOLEAN
*AKMSuiteSupported
,
337 OUT BOOLEAN
*CipherSuiteSupported
340 EFI_80211_AKM_SUITE_SELECTOR
*SupportedAKMSuites
;
341 EFI_80211_CIPHER_SUITE_SELECTOR
*SupportedSwCipherSuites
;
342 EFI_80211_CIPHER_SUITE_SELECTOR
*SupportedHwCipherSuites
;
343 EFI_80211_SUITE_SELECTOR
*AKMSuite
;
344 EFI_80211_SUITE_SELECTOR
*CipherSuite
;
348 if ((Nic
== NULL
) || (AKMList
== NULL
) || (CipherList
== NULL
) || (SecurityType
== NULL
)) {
349 return EFI_INVALID_PARAMETER
;
352 SupportedAKMSuites
= Nic
->SupportedSuites
.SupportedAKMSuites
;
353 SupportedSwCipherSuites
= Nic
->SupportedSuites
.SupportedSwCipherSuites
;
354 SupportedHwCipherSuites
= Nic
->SupportedSuites
.SupportedHwCipherSuites
;
356 *SecurityType
= SECURITY_TYPE_UNKNOWN
;
357 if ((AKMSuiteSupported
!= NULL
) && (CipherSuiteSupported
!= NULL
)) {
358 *AKMSuiteSupported
= FALSE
;
359 *CipherSuiteSupported
= FALSE
;
362 if (AKMList
->AKMSuiteCount
== 0) {
363 if (CipherList
->CipherSuiteCount
== 0) {
364 *SecurityType
= SECURITY_TYPE_NONE
;
365 if ((AKMSuiteSupported
!= NULL
) && (CipherSuiteSupported
!= NULL
)) {
366 *AKMSuiteSupported
= TRUE
;
367 *CipherSuiteSupported
= TRUE
;
374 for (AKMIndex
= 0; AKMIndex
< AKMList
->AKMSuiteCount
; AKMIndex
++) {
375 AKMSuite
= AKMList
->AKMSuiteList
+ AKMIndex
;
376 if (WifiMgrSupportAKMSuite (
377 SupportedAKMSuites
->AKMSuiteCount
,
378 (UINT32
*)SupportedAKMSuites
->AKMSuiteList
,
382 if ((AKMSuiteSupported
!= NULL
) && (CipherSuiteSupported
!= NULL
)) {
383 *AKMSuiteSupported
= TRUE
;
386 for (CipherIndex
= 0; CipherIndex
< CipherList
->CipherSuiteCount
; CipherIndex
++) {
387 CipherSuite
= CipherList
->CipherSuiteList
+ CipherIndex
;
389 if (SupportedSwCipherSuites
!= NULL
) {
390 if (WifiMgrSupportCipherSuite (
391 SupportedSwCipherSuites
->CipherSuiteCount
,
392 (UINT32
*)SupportedSwCipherSuites
->CipherSuiteList
,
393 (UINT32
*)CipherSuite
396 *SecurityType
= WifiMgrGetSecurityType ((UINT32
*)AKMSuite
, (UINT32
*)CipherSuite
);
398 if (*SecurityType
!= SECURITY_TYPE_UNKNOWN
) {
399 if ((AKMSuiteSupported
!= NULL
) && (CipherSuiteSupported
!= NULL
)) {
400 *CipherSuiteSupported
= TRUE
;
408 if (SupportedHwCipherSuites
!= NULL
) {
409 if (WifiMgrSupportCipherSuite (
410 SupportedHwCipherSuites
->CipherSuiteCount
,
411 (UINT32
*)SupportedHwCipherSuites
->CipherSuiteList
,
412 (UINT32
*)CipherSuite
415 *SecurityType
= WifiMgrGetSecurityType ((UINT32
*)AKMSuite
, (UINT32
*)CipherSuite
);
417 if (*SecurityType
!= SECURITY_TYPE_UNKNOWN
) {
418 if ((AKMSuiteSupported
!= NULL
) && (CipherSuiteSupported
!= NULL
)) {
419 *CipherSuiteSupported
= TRUE
;
430 *SecurityType
= WifiMgrGetSecurityType (
431 (UINT32
*)AKMList
->AKMSuiteList
,
432 (UINT32
*)CipherList
->CipherSuiteList
439 Get the security type for a certain AKMSuite and CipherSuite.
441 @param[in] AKMSuite An certain AKMSuite.
442 @param[in] CipherSuite An certain CipherSuite.
444 @return a security type if found, or SECURITY_TYPE_UNKNOWN.
448 WifiMgrGetSecurityType (
450 IN UINT32
*CipherSuite
453 if (CipherSuite
== NULL
) {
454 if (AKMSuite
== NULL
) {
455 return SECURITY_TYPE_NONE
;
457 return SECURITY_TYPE_UNKNOWN
;
459 } else if (*CipherSuite
== IEEE_80211_PAIRWISE_CIPHER_SUITE_USE_GROUP
) {
460 if (AKMSuite
== NULL
) {
461 return SECURITY_TYPE_NONE
;
463 return SECURITY_TYPE_UNKNOWN
;
465 } else if ((*CipherSuite
== IEEE_80211_PAIRWISE_CIPHER_SUITE_WEP40
) ||
466 (*CipherSuite
== IEEE_80211_PAIRWISE_CIPHER_SUITE_WEP104
))
468 return SECURITY_TYPE_WEP
;
469 } else if (*CipherSuite
== IEEE_80211_PAIRWISE_CIPHER_SUITE_CCMP
) {
470 if (AKMSuite
== NULL
) {
471 return SECURITY_TYPE_UNKNOWN
;
474 if ((*AKMSuite
== IEEE_80211_AKM_SUITE_8021X_OR_PMKSA
) ||
475 (*AKMSuite
== IEEE_80211_AKM_SUITE_8021X_OR_PMKSA_SHA256
))
477 return SECURITY_TYPE_WPA2_ENTERPRISE
;
478 } else if ((*AKMSuite
== IEEE_80211_AKM_SUITE_PSK
) ||
479 (*AKMSuite
== IEEE_80211_AKM_SUITE_PSK_SHA256
))
481 return SECURITY_TYPE_WPA2_PERSONAL
;
483 return SECURITY_TYPE_UNKNOWN
;
485 } else if (*CipherSuite
== IEEE_80211_PAIRWISE_CIPHER_SUITE_TKIP
) {
486 if (AKMSuite
== NULL
) {
487 return SECURITY_TYPE_UNKNOWN
;
490 if ((*AKMSuite
== IEEE_80211_AKM_SUITE_8021X_OR_PMKSA
) ||
491 (*AKMSuite
== IEEE_80211_AKM_SUITE_8021X_OR_PMKSA_SHA256
))
493 return SECURITY_TYPE_WPA_ENTERPRISE
;
494 } else if ((*AKMSuite
== IEEE_80211_AKM_SUITE_PSK
) ||
495 (*AKMSuite
== IEEE_80211_AKM_SUITE_PSK_SHA256
))
497 return SECURITY_TYPE_WPA_PERSONAL
;
499 return SECURITY_TYPE_UNKNOWN
;
502 return SECURITY_TYPE_UNKNOWN
;
507 Get supported AKMSuites and CipherSuites from supplicant for a Nic.
509 @param[in] Nic The Nic to operate.
511 @retval EFI_SUCCESS Get the supported suite list successfully.
512 @retval EFI_INVALID_PARAMETER No Nic found or supplicant is NULL.
516 WifiMgrGetSupportedSuites (
517 IN WIFI_MGR_DEVICE_DATA
*Nic
521 EFI_SUPPLICANT_PROTOCOL
*Supplicant
;
522 EFI_80211_AKM_SUITE_SELECTOR
*SupportedAKMSuites
;
523 EFI_80211_CIPHER_SUITE_SELECTOR
*SupportedSwCipherSuites
;
524 EFI_80211_CIPHER_SUITE_SELECTOR
*SupportedHwCipherSuites
;
527 SupportedAKMSuites
= NULL
;
528 SupportedSwCipherSuites
= NULL
;
529 SupportedHwCipherSuites
= NULL
;
531 if ((Nic
== NULL
) || (Nic
->Supplicant
== NULL
)) {
532 return EFI_INVALID_PARAMETER
;
535 Supplicant
= Nic
->Supplicant
;
538 Status
= Supplicant
->GetData (Supplicant
, EfiSupplicant80211SupportedAKMSuites
, NULL
, &DataSize
);
539 if ((Status
== EFI_BUFFER_TOO_SMALL
) && (DataSize
> 0)) {
540 SupportedAKMSuites
= AllocateZeroPool (DataSize
);
541 if (SupportedAKMSuites
== NULL
) {
542 return EFI_OUT_OF_RESOURCES
;
545 Status
= Supplicant
->GetData (
547 EfiSupplicant80211SupportedAKMSuites
,
548 (UINT8
*)SupportedAKMSuites
,
551 if (!EFI_ERROR (Status
)) {
552 Nic
->SupportedSuites
.SupportedAKMSuites
= SupportedAKMSuites
;
554 FreePool (SupportedAKMSuites
);
557 SupportedAKMSuites
= NULL
;
561 Status
= Supplicant
->GetData (Supplicant
, EfiSupplicant80211SupportedSoftwareCipherSuites
, NULL
, &DataSize
);
562 if ((Status
== EFI_BUFFER_TOO_SMALL
) && (DataSize
> 0)) {
563 SupportedSwCipherSuites
= AllocateZeroPool (DataSize
);
564 if (SupportedSwCipherSuites
== NULL
) {
565 return EFI_OUT_OF_RESOURCES
;
568 Status
= Supplicant
->GetData (
570 EfiSupplicant80211SupportedSoftwareCipherSuites
,
571 (UINT8
*)SupportedSwCipherSuites
,
574 if (!EFI_ERROR (Status
)) {
575 Nic
->SupportedSuites
.SupportedSwCipherSuites
= SupportedSwCipherSuites
;
577 FreePool (SupportedSwCipherSuites
);
580 SupportedSwCipherSuites
= NULL
;
584 Status
= Supplicant
->GetData (Supplicant
, EfiSupplicant80211SupportedHardwareCipherSuites
, NULL
, &DataSize
);
585 if ((Status
== EFI_BUFFER_TOO_SMALL
) && (DataSize
> 0)) {
586 SupportedHwCipherSuites
= AllocateZeroPool (DataSize
);
587 if (SupportedHwCipherSuites
== NULL
) {
588 return EFI_OUT_OF_RESOURCES
;
591 Status
= Supplicant
->GetData (
593 EfiSupplicant80211SupportedHardwareCipherSuites
,
594 (UINT8
*)SupportedHwCipherSuites
,
597 if (!EFI_ERROR (Status
)) {
598 Nic
->SupportedSuites
.SupportedHwCipherSuites
= SupportedHwCipherSuites
;
600 FreePool (SupportedHwCipherSuites
);
603 SupportedHwCipherSuites
= NULL
;
610 Clean secrets from a network profile.
612 @param[in] Profile The profile to be cleanned.
616 WifiMgrCleanProfileSecrets (
617 IN WIFI_MGR_NETWORK_PROFILE
*Profile
620 ZeroMem (Profile
->Password
, sizeof (CHAR16
) * PASSWORD_STORAGE_SIZE
);
621 ZeroMem (Profile
->EapPassword
, sizeof (CHAR16
) * PASSWORD_STORAGE_SIZE
);
622 ZeroMem (Profile
->PrivateKeyPassword
, sizeof (CHAR16
) * PASSWORD_STORAGE_SIZE
);
624 if (Profile
->CACertData
!= NULL
) {
625 ZeroMem (Profile
->CACertData
, Profile
->CACertSize
);
626 FreePool (Profile
->CACertData
);
629 Profile
->CACertData
= NULL
;
630 Profile
->CACertSize
= 0;
632 if (Profile
->ClientCertData
!= NULL
) {
633 ZeroMem (Profile
->ClientCertData
, Profile
->ClientCertSize
);
634 FreePool (Profile
->ClientCertData
);
637 Profile
->ClientCertData
= NULL
;
638 Profile
->ClientCertSize
= 0;
640 if (Profile
->PrivateKeyData
!= NULL
) {
641 ZeroMem (Profile
->PrivateKeyData
, Profile
->PrivateKeyDataSize
);
642 FreePool (Profile
->PrivateKeyData
);
645 Profile
->PrivateKeyData
= NULL
;
646 Profile
->PrivateKeyDataSize
= 0;
650 Free all network profiles in a profile list.
652 @param[in] ProfileList The profile list to be freed.
656 WifiMgrFreeProfileList (
657 IN LIST_ENTRY
*ProfileList
660 WIFI_MGR_NETWORK_PROFILE
*Profile
;
662 LIST_ENTRY
*NextEntry
;
664 if (ProfileList
== NULL
) {
668 NET_LIST_FOR_EACH_SAFE (Entry
, NextEntry
, ProfileList
) {
669 Profile
= NET_LIST_USER_STRUCT_S (
671 WIFI_MGR_NETWORK_PROFILE
,
673 WIFI_MGR_PROFILE_SIGNATURE
676 WifiMgrCleanProfileSecrets (Profile
);
678 if (Profile
->Network
.AKMSuite
!= NULL
) {
679 FreePool (Profile
->Network
.AKMSuite
);
682 if (Profile
->Network
.CipherSuite
!= NULL
) {
683 FreePool (Profile
->Network
.CipherSuite
);
691 Free user configured hidden network list.
693 @param[in] HiddenList The hidden network list to be freed.
697 WifiMgrFreeHiddenList (
698 IN LIST_ENTRY
*HiddenList
701 WIFI_HIDDEN_NETWORK_DATA
*HiddenNetwork
;
703 LIST_ENTRY
*NextEntry
;
705 if (HiddenList
== NULL
) {
709 NET_LIST_FOR_EACH_SAFE (Entry
, NextEntry
, HiddenList
) {
710 HiddenNetwork
= NET_LIST_USER_STRUCT_S (
712 WIFI_HIDDEN_NETWORK_DATA
,
714 WIFI_MGR_HIDDEN_NETWORK_SIGNATURE
716 FreePool (HiddenNetwork
);
721 Free the resources of a config token.
723 @param[in] ConfigToken The config token to be freed.
727 IN WIFI_MGR_MAC_CONFIG_TOKEN
*ConfigToken
730 EFI_80211_GET_NETWORKS_RESULT
*Result
;
732 if (ConfigToken
== NULL
) {
736 switch (ConfigToken
->Type
) {
737 case TokenTypeGetNetworksToken
:
739 if (ConfigToken
->Token
.GetNetworksToken
!= NULL
) {
740 gBS
->CloseEvent (ConfigToken
->Token
.GetNetworksToken
->Event
);
741 if (ConfigToken
->Token
.GetNetworksToken
->Data
!= NULL
) {
742 FreePool (ConfigToken
->Token
.GetNetworksToken
->Data
);
745 Result
= ConfigToken
->Token
.GetNetworksToken
->Result
;
746 if (Result
!= NULL
) {
750 FreePool (ConfigToken
->Token
.GetNetworksToken
);
753 FreePool (ConfigToken
);
756 case TokenTypeConnectNetworkToken
:
758 if (ConfigToken
->Token
.ConnectNetworkToken
!= NULL
) {
759 gBS
->CloseEvent (ConfigToken
->Token
.ConnectNetworkToken
->Event
);
760 if (ConfigToken
->Token
.ConnectNetworkToken
->Data
!= NULL
) {
761 FreePool (ConfigToken
->Token
.ConnectNetworkToken
->Data
);
764 FreePool (ConfigToken
->Token
.ConnectNetworkToken
);
767 FreePool (ConfigToken
);
770 case TokenTypeDisconnectNetworkToken
:
772 if (ConfigToken
->Token
.DisconnectNetworkToken
!= NULL
) {
773 FreePool (ConfigToken
->Token
.DisconnectNetworkToken
);
776 FreePool (ConfigToken
);