Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow 64 char WiFi passwords in wifi.radio.connect #7866

Merged
merged 2 commits into from
Apr 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion data/nvm.toml
17 changes: 8 additions & 9 deletions locale/circuitpython.pot
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ msgstr ""
#: ports/raspberrypi/common-hal/analogio/AnalogOut.c
#: ports/raspberrypi/common-hal/rtc/RTC.c ports/stm/common-hal/alarm/__init__.c
#: ports/stm/common-hal/canio/Listener.c ports/stm/common-hal/rtc/RTC.c
#: shared-bindings/audiobusio/I2SOut.c shared-bindings/audiobusio/PDMIn.c
#: shared-bindings/keypad/KeyMatrix.c shared-bindings/keypad/Keys.c
#: shared-bindings/keypad/ShiftRegisterKeys.c
msgid "%q"
msgstr ""

Expand Down Expand Up @@ -193,7 +196,7 @@ msgstr ""
msgid "%q must be array of type 'H'"
msgstr ""

#: shared-bindings/synthio/MidiTrack.c shared-bindings/synthio/__init__.c
#: shared-module/synthio/__init__.c
msgid "%q must be array of type 'h'"
msgstr ""

Expand Down Expand Up @@ -1117,10 +1120,6 @@ msgstr ""
msgid "I2C peripheral in use"
msgstr ""

#: shared-bindings/audiobusio/I2SOut.c
msgid "I2SOut not available"
msgstr ""

#: ports/raspberrypi/bindings/rp2pio/StateMachine.c
msgid "In-buffer elements must be <= 4 bytes long"
msgstr ""
Expand Down Expand Up @@ -1282,6 +1281,10 @@ msgstr ""
msgid "Invalid format chunk size"
msgstr ""

#: shared-bindings/wifi/Radio.c
msgid "Invalid hex password"
msgstr ""

#: ports/espressif/common-hal/wifi/Radio.c
msgid "Invalid multicast MAC address"
msgstr ""
Expand Down Expand Up @@ -1708,10 +1711,6 @@ msgstr ""
msgid "Oversample must be multiple of 8."
msgstr ""

#: shared-bindings/audiobusio/PDMIn.c
msgid "PDMIn not available"
msgstr ""

#: shared-bindings/pwmio/PWMOut.c
msgid ""
"PWM frequency not writable when variable_frequency is False on construction."
Expand Down
25 changes: 23 additions & 2 deletions shared-bindings/wifi/Radio.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

#include <string.h>

#include "py/unicode.h"
#include "py/runtime.h"
#include "py/objproperty.h"

Expand Down Expand Up @@ -70,6 +71,14 @@ STATIC bool hostname_valid(const char *ptr, size_t len) {
return !(partlen > 63);
}

STATIC void validate_hex_password(const uint8_t *buf, size_t len) {
for (size_t i = 0; i < len; i++) {
if (!unichar_isxdigit(buf[i])) {
mp_raise_ValueError_varg(translate("Invalid hex password"));
}
}
}


//| class Radio:
//| """Native wifi radio.
Expand Down Expand Up @@ -321,6 +330,9 @@ MP_DEFINE_CONST_FUN_OBJ_1(wifi_radio_stop_station_obj, wifi_radio_stop_station);
//| ``OPEN`` will be used when the password is the empty string,
//| otherwise ``authmode`` will be ``WPA_WPA2_PSK``.
//|
//| The length of ``password`` must be 8-63 characters if it is ASCII,
//| or exactly 64 hexadecimal characters if it is the hex form of the 256-bit key.
//|
//| If ``max_connections`` is given, the access point will allow up to
//| that number of stations to connect."""
//| ...
Expand Down Expand Up @@ -367,7 +379,10 @@ STATIC mp_obj_t wifi_radio_start_ap(size_t n_args, const mp_obj_t *pos_args, mp_
}

if (authmodes != AUTHMODE_OPEN) {
mp_arg_validate_length_range(password.len, 8, 63, MP_QSTR_password);
mp_arg_validate_length_range(password.len, 8, 64, MP_QSTR_password);
if (password.len == 64) {
validate_hex_password(password.buf, password.len);
}
}

common_hal_wifi_radio_start_ap(self, ssid.buf, ssid.len, password.buf, password.len, args[ARG_channel].u_int, authmodes, args[ARG_max_connections].u_int);
Expand Down Expand Up @@ -406,6 +421,9 @@ MP_PROPERTY_GETTER(wifi_radio_ap_active_obj,
//| """Connects to the given ssid and waits for an ip address. Reconnections are handled
//| automatically once one connection succeeds.
//|
//| The length of ``password`` must be 0 if there is no password, 8-63 characters if it is ASCII,
//| or exactly 64 hexadecimal characters if it is the hex form of the 256-bit key.
//|
//| By default, this will scan all channels and connect to the access point (AP) with the
//| given ``ssid`` and greatest signal strength (rssi).
//|
Expand Down Expand Up @@ -445,7 +463,10 @@ STATIC mp_obj_t wifi_radio_connect(size_t n_args, const mp_obj_t *pos_args, mp_m
if (args[ARG_password].u_obj != mp_const_none) {
mp_get_buffer_raise(args[ARG_password].u_obj, &password, MP_BUFFER_READ);
if (password.len != 0) {
mp_arg_validate_length_range(password.len, 8, 63, MP_QSTR_password);
mp_arg_validate_length_range(password.len, 8, 64, MP_QSTR_password);
if (password.len == 64) {
validate_hex_password(password.buf, password.len);
}
}
}

Expand Down