Skip to content

Commit f54fa7d

Browse files
maxd-nordickrish2718
authored andcommitted
[nrf fromtree] net: lib: add wifi_credentials library
Upstream NCS's library for storing Wi-Fi credentials. This library allows storage of Wi-Fi credentials using different backends. Either the Zephyr settings subsystem or the PSA secure backend can be used. For testing purposes, credentials can be defined statically. Signed-off-by: Ravi Dondaputi <[email protected]> Signed-off-by: Kapil Bhatt <[email protected]> Signed-off-by: Gregers Gram Rygg <[email protected]> Signed-off-by: Kaja Koren <[email protected]> Signed-off-by: Simen S. Røstad <[email protected]> Signed-off-by: Maximilian Deubel <[email protected]> (cherry picked from commit f6d305a)
1 parent d52893a commit f54fa7d

File tree

12 files changed

+1457
-1
lines changed

12 files changed

+1457
-1
lines changed
Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
1+
/*
2+
* Copyright (c) 2024 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#ifndef WIFI_CREDENTIALS_H__
8+
#define WIFI_CREDENTIALS_H__
9+
10+
#include <zephyr/types.h>
11+
#include <zephyr/net/wifi.h>
12+
#include <zephyr/kernel.h>
13+
14+
/**
15+
* @defgroup wifi_credentials Wi-Fi credentials library
16+
* @ingroup networking
17+
* @since 4.0
18+
* @version 0.1.0
19+
* @{
20+
* @brief Library that provides a way to store and load Wi-Fi credentials.
21+
*/
22+
23+
#ifdef __cplusplus
24+
extern "C" {
25+
#endif
26+
27+
/* this entry contains a BSSID */
28+
#define WIFI_CREDENTIALS_FLAG_BSSID BIT(0)
29+
/* this entry is to be preferred over others */
30+
#define WIFI_CREDENTIALS_FLAG_FAVORITE BIT(1)
31+
/* this entry can use the 2.4 GHz band */
32+
#define WIFI_CREDENTIALS_FLAG_2_4GHz BIT(2)
33+
/* this entry can use the 5 GHz band */
34+
#define WIFI_CREDENTIALS_FLAG_5GHz BIT(3)
35+
/* this entry requires management frame protection */
36+
#define WIFI_CREDENTIALS_FLAG_MFP_REQUIRED BIT(4)
37+
/* this entry disables management frame protection */
38+
#define WIFI_CREDENTIALS_FLAG_MFP_DISABLED BIT(5)
39+
40+
#define WIFI_CREDENTIALS_MAX_PASSWORD_LEN \
41+
MAX(WIFI_PSK_MAX_LEN, CONFIG_WIFI_CREDENTIALS_SAE_PASSWORD_LENGTH)
42+
43+
/**
44+
* @brief Wi-Fi credentials entry header
45+
* @note Every settings entry starts with this header.
46+
* Depending on the `type` field, the header can be casted to a larger type.
47+
* In addition to SSID (usually a string) and BSSID (a MAC address),
48+
* a `flags` field can be used to control some detail settings.
49+
*
50+
*/
51+
struct wifi_credentials_header {
52+
enum wifi_security_type type; /**< Wi-Fi security type */
53+
char ssid[WIFI_SSID_MAX_LEN]; /**< SSID (Service Set Identifier) */
54+
size_t ssid_len; /**< Length of the SSID */
55+
uint32_t flags; /**< Flags for controlling detail settings */
56+
uint32_t timeout; /**< Timeout for connecting to the network */
57+
uint8_t bssid[WIFI_MAC_ADDR_LEN]; /**< BSSID (Basic Service Set Identifier) */
58+
uint8_t channel; /**< Channel on which the network operates */
59+
};
60+
61+
/**
62+
* @brief Wi-Fi Personal credentials entry
63+
* @note Contains only the header and a password.
64+
* For PSK security, passwords can be up to `WIFI_PSK_MAX_LEN` bytes long
65+
* including NULL termination. For SAE security it can range up to
66+
* `CONFIG_WIFI_CREDENTIALS_SAE_PASSWORD_LENGTH`.
67+
*
68+
*/
69+
struct wifi_credentials_personal {
70+
struct wifi_credentials_header header; /**< Header */
71+
char password[WIFI_CREDENTIALS_MAX_PASSWORD_LEN]; /**< Password/PSK */
72+
size_t password_len; /**< Length of the password */
73+
};
74+
75+
/**
76+
* @brief Wi-Fi Enterprise credentials entry
77+
* @note This functionality is not yet implemented.
78+
*/
79+
struct wifi_credentials_enterprise {
80+
struct wifi_credentials_header header; /**< Header */
81+
size_t identity_len; /**< Length of the identity */
82+
size_t anonymous_identity_len; /**< Length of the anonymous identity */
83+
size_t password_len; /**< Length of the password */
84+
size_t ca_cert_len; /**< Length of the CA certificate */
85+
size_t client_cert_len; /**< Length of the client certificate */
86+
size_t private_key_len; /**< Length of the private key */
87+
size_t private_key_pw_len; /**< Length of the private key password */
88+
};
89+
90+
/**
91+
* @brief Get credentials for given SSID.
92+
*
93+
* @param[in] ssid SSID to look for
94+
* @param[in] ssid_len length of SSID
95+
* @param[out] type Wi-Fi security type
96+
* @param[out] bssid_buf buffer to store BSSID if it was fixed
97+
* @param[in] bssid_buf_len length of bssid_buf
98+
* @param[out] password_buf buffer to store password
99+
* @param[in] password_buf_len length of password_buf
100+
* @param[out] password_len length of password
101+
* @param[out] flags flags
102+
* @param[out] channel channel
103+
* @param[out] timeout timeout
104+
*
105+
* @return 0 Success.
106+
* @return -ENOENT No network with this SSID was found.
107+
* @return -EINVAL A required buffer was NULL or invalid SSID length.
108+
* @return -EPROTO The network with this SSID is not a personal network.
109+
*/
110+
int wifi_credentials_get_by_ssid_personal(const char *ssid, size_t ssid_len,
111+
enum wifi_security_type *type, uint8_t *bssid_buf,
112+
size_t bssid_buf_len, char *password_buf,
113+
size_t password_buf_len, size_t *password_len,
114+
uint32_t *flags, uint8_t *channel, uint32_t *timeout);
115+
116+
/**
117+
* @brief Set credentials for given SSID.
118+
*
119+
* @param[in] ssid SSID to look for
120+
* @param[in] ssid_len length of SSID
121+
* @param[in] type Wi-Fi security type
122+
* @param[in] bssid BSSID (may be NULL)
123+
* @param[in] bssid_len length of BSSID buffer (either 0 or WIFI_MAC_ADDR_LEN)
124+
* @param[in] password password
125+
* @param[in] password_len length of password
126+
* @param[in] flags flags
127+
* @param[in] channel Channel
128+
* @param[in] timeout Timeout
129+
*
130+
* @return 0 Success. Credentials are stored in persistent storage.
131+
* @return -EINVAL A required buffer was NULL or security type is not supported.
132+
* @return -ENOTSUP Security type is not supported.
133+
* @return -ENOBUFS All slots are already taken.
134+
*/
135+
int wifi_credentials_set_personal(const char *ssid, size_t ssid_len, enum wifi_security_type type,
136+
const uint8_t *bssid, size_t bssid_len, const char *password,
137+
size_t password_len, uint32_t flags, uint8_t channel,
138+
uint32_t timeout);
139+
140+
/**
141+
* @brief Get credentials for given SSID by struct.
142+
*
143+
* @param[in] ssid SSID to look for
144+
* @param[in] ssid_len length of SSID
145+
* @param[out] buf credentials Pointer to struct where credentials are stored
146+
*
147+
* @return 0 Success.
148+
* @return -ENOENT No network with this SSID was found.
149+
* @return -EINVAL A required buffer was NULL or too small.
150+
* @return -EPROTO The network with this SSID is not a personal network.
151+
*/
152+
int wifi_credentials_get_by_ssid_personal_struct(const char *ssid, size_t ssid_len,
153+
struct wifi_credentials_personal *buf);
154+
155+
/**
156+
* @brief Set credentials for given SSID by struct.
157+
*
158+
* @param[in] creds credentials Pointer to struct from which credentials are loaded
159+
*
160+
* @return 0 Success.
161+
* @return -ENOENT No network with this SSID was found.
162+
* @return -EINVAL A required buffer was NULL or incorrect size.
163+
* @return -ENOBUFS All slots are already taken.
164+
*/
165+
int wifi_credentials_set_personal_struct(const struct wifi_credentials_personal *creds);
166+
167+
/**
168+
* @brief Delete credentials for given SSID.
169+
*
170+
* @param[in] ssid SSID to look for
171+
* @param[in] ssid_len length of SSID
172+
*
173+
* @return -ENOENT if No network with this SSID was found.
174+
* @return 0 on success, otherwise a negative error code
175+
*/
176+
int wifi_credentials_delete_by_ssid(const char *ssid, size_t ssid_len);
177+
178+
/**
179+
* @brief Check if credentials storage is empty.
180+
*
181+
* @return true if credential storage is empty, otherwise false
182+
*/
183+
bool wifi_credentials_is_empty(void);
184+
185+
/**
186+
* @brief Deletes all stored Wi-Fi credentials.
187+
*
188+
* This function deletes all Wi-Fi credentials that have been stored in the system.
189+
* It is typically used when you want to clear all saved networks.
190+
*
191+
* @return 0 on successful, otherwise a negative error code
192+
*/
193+
int wifi_credentials_delete_all(void);
194+
195+
/**
196+
* @brief Callback type for wifi_credentials_for_each_ssid.
197+
* @param[in] cb_arg arguments for the callback function. Appropriate cb_arg is
198+
* transferred by wifi_credentials_for_each_ssid.
199+
* @param[in] ssid SSID
200+
* @param[in] ssid_len length of SSID
201+
*/
202+
typedef void (*wifi_credentials_ssid_cb)(void *cb_arg, const char *ssid, size_t ssid_len);
203+
204+
/**
205+
* @brief Call callback for each registered SSID.
206+
*
207+
* @param cb callback
208+
* @param cb_arg argument for callback function
209+
*/
210+
void wifi_credentials_for_each_ssid(wifi_credentials_ssid_cb cb, void *cb_arg);
211+
212+
#ifdef __cplusplus
213+
}
214+
#endif
215+
216+
/** @} */
217+
218+
#endif /* WIFI_CREDENTIALS_H__ */

samples/net/wifi/sample.yaml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,13 @@ tests:
4242
integration_platforms:
4343
- frdm_k64f
4444
sample.net.wifi.nrf70dk:
45-
extra_args: CONFIG_NRF_WIFI_BUILD_ONLY_MODE=y
45+
extra_args:
46+
- CONFIG_NRF_WIFI_BUILD_ONLY_MODE=y
47+
- CONFIG_WIFI_CREDENTIALS=y
48+
- CONFIG_FLASH=y
49+
- CONFIG_FLASH_MAP=y
50+
- CONFIG_NVS=y
51+
- CONFIG_SETTINGS=y
4652
platform_allow:
4753
- nrf7002dk/nrf5340/cpuapp
4854
- nrf7002dk/nrf5340/cpuapp/ns

subsys/net/lib/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ add_subdirectory_ifdef(CONFIG_NET_SHELL shell)
1818
add_subdirectory_ifdef(CONFIG_NET_TRICKLE trickle)
1919
add_subdirectory_ifdef(CONFIG_NET_DHCPV6 dhcpv6)
2020
add_subdirectory_ifdef(CONFIG_PROMETHEUS prometheus)
21+
add_subdirectory_ifdef(CONFIG_WIFI_CREDENTIALS wifi_credentials)
2122

2223
if (CONFIG_NET_DHCPV4 OR CONFIG_NET_DHCPV4_SERVER)
2324
add_subdirectory(dhcpv4)

subsys/net/lib/Kconfig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,6 @@ source "subsys/net/lib/zperf/Kconfig"
5151

5252
source "subsys/net/lib/prometheus/Kconfig"
5353

54+
source "subsys/net/lib/wifi_credentials/Kconfig"
55+
5456
endmenu
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#
2+
# Copyright (c) 2024 Nordic Semiconductor ASA
3+
#
4+
# SPDX-License-Identifier: Apache-2.0
5+
#
6+
7+
zephyr_library_named(wifi_credentials)
8+
zephyr_library_sources(wifi_credentials.c)
9+
10+
if (CONFIG_WIFI_CREDENTIALS_BACKEND_PSA)
11+
zephyr_library_include_directories(
12+
$<TARGET_PROPERTY:tfm,TFM_BINARY_DIR>/api_ns/interface/include
13+
)
14+
endif()
15+
16+
zephyr_library_sources_ifdef(
17+
CONFIG_WIFI_CREDENTIALS_BACKEND_SETTINGS
18+
wifi_credentials_backend_settings.c)
19+
20+
zephyr_library_sources_ifdef(
21+
CONFIG_WIFI_CREDENTIALS_BACKEND_PSA
22+
wifi_credentials_backend_psa.c)
23+
24+
zephyr_library_sources_ifdef(
25+
CONFIG_WIFI_CREDENTIALS_BACKEND_NONE
26+
wifi_credentials_backend_none.c)
27+
28+
zephyr_library_sources_ifdef(
29+
CONFIG_WIFI_CREDENTIALS_SHELL
30+
wifi_credentials_shell.c)
31+
32+
if(WIFI_CREDENTIALS_STATIC_SSID)
33+
message(WARNING
34+
"Static Wi-Fi configuration is used, please remove before deployment!"
35+
)
36+
endif()
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
#
2+
# Copyright (c) 2024 Nordic Semiconductor ASA
3+
#
4+
# SPDX-License-Identifier: Apache-2.0
5+
#
6+
7+
menuconfig WIFI_CREDENTIALS
8+
bool "WIFI credentials management"
9+
select EXPERIMENTAL
10+
help
11+
Enable WiFi credentials management subsystem.
12+
13+
if WIFI_CREDENTIALS
14+
15+
module = WIFI_CREDENTIALS
16+
module-str = wifi_credentials
17+
source "subsys/logging/Kconfig.template.log_config"
18+
19+
choice WIFI_CREDENTIALS_BACKEND
20+
prompt "WiFi credentials backend"
21+
default WIFI_CREDENTIALS_BACKEND_PSA if BUILD_WITH_TFM
22+
default WIFI_CREDENTIALS_BACKEND_SETTINGS
23+
default WIFI_CREDENTIALS_BACKEND_NONE if WIFI_CREDENTIALS_STATIC
24+
help
25+
Selects whether to use PSA Protected Storage or the Zephyr settings subsystem
26+
for credentials storage.
27+
28+
config WIFI_CREDENTIALS_BACKEND_SETTINGS
29+
bool "Zephyr Settings"
30+
depends on SETTINGS
31+
depends on !SETTINGS_NONE
32+
33+
config WIFI_CREDENTIALS_BACKEND_PSA
34+
bool "PSA Protected Storage"
35+
depends on BUILD_WITH_TFM
36+
37+
config WIFI_CREDENTIALS_BACKEND_NONE
38+
bool "No credentials storage"
39+
depends on WIFI_CREDENTIALS_STATIC
40+
41+
endchoice
42+
43+
config WIFI_CREDENTIALS_MAX_ENTRIES
44+
int "Number of supported WiFi credentials"
45+
default 2
46+
help
47+
This detemines how many different WiFi networks can be configured at a time.
48+
49+
config WIFI_CREDENTIALS_SAE_PASSWORD_LENGTH
50+
int "Max. length of SAE password"
51+
default 128
52+
help
53+
There is no official limit on SAE password length,
54+
but for example Linux 6.0 has a hardcoded limit of 128 bytes.
55+
56+
config WIFI_CREDENTIALS_SHELL
57+
bool "Shell commands to manage Wi-Fi credentials"
58+
default y
59+
depends on SHELL
60+
depends on !WIFI_CREDENTIALS_BACKEND_NONE
61+
62+
endif # WIFI_CREDENTIALS
63+
64+
if WIFI_CREDENTIALS_BACKEND_PSA
65+
66+
config WIFI_CREDENTIALS_BACKEND_PSA_OFFSET
67+
int "PSA_KEY_ID range offset"
68+
default 0
69+
help
70+
The PSA specification mandates to set key identifiers for keys
71+
with persistent lifetime. The users of the PSA API are responsible (WIFI credentials
72+
management is user of PSA API) to provide correct and unique identifiers.
73+
74+
endif # WIFI_CREDENTIALS_BACKEND_PSA
75+
76+
config WIFI_CREDENTIALS_STATIC
77+
bool "Static Wi-Fi network configuration"
78+
79+
if WIFI_CREDENTIALS_STATIC
80+
81+
config WIFI_CREDENTIALS_STATIC_SSID
82+
string "SSID of statically configured WiFi network"
83+
84+
config WIFI_CREDENTIALS_STATIC_PASSWORD
85+
string "Password of statically configured Wi-Fi network"
86+
default ""
87+
88+
choice WIFI_CREDENTIALS_STATIC_TYPE
89+
prompt "Static Wi-Fi network security type"
90+
default WIFI_CREDENTIALS_STATIC_TYPE_PSK
91+
92+
config WIFI_CREDENTIALS_STATIC_TYPE_OPEN
93+
bool "OPEN"
94+
95+
config WIFI_CREDENTIALS_STATIC_TYPE_PSK
96+
bool "WPA2-PSK"
97+
98+
config WIFI_CREDENTIALS_STATIC_TYPE_PSK_SHA256
99+
bool "WPA2-PSK-SHA256"
100+
101+
config WIFI_CREDENTIALS_STATIC_TYPE_SAE
102+
bool "SAE"
103+
104+
config WIFI_CREDENTIALS_STATIC_TYPE_WPA_PSK
105+
bool "WPA-PSK"
106+
107+
endchoice
108+
109+
endif

0 commit comments

Comments
 (0)