Skip to content

Commit 4b41a49

Browse files
authored
Merge pull request #7866 from rich123/allow-64-char-wifi-passwords
Allow 64 char WiFi passwords in wifi.radio.connect
2 parents 09c2c5e + df41bd9 commit 4b41a49

File tree

2 files changed

+31
-11
lines changed

2 files changed

+31
-11
lines changed

locale/circuitpython.pot

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ msgstr ""
9898
#: ports/raspberrypi/common-hal/analogio/AnalogOut.c
9999
#: ports/raspberrypi/common-hal/rtc/RTC.c ports/stm/common-hal/alarm/__init__.c
100100
#: ports/stm/common-hal/canio/Listener.c ports/stm/common-hal/rtc/RTC.c
101+
#: shared-bindings/audiobusio/I2SOut.c shared-bindings/audiobusio/PDMIn.c
102+
#: shared-bindings/keypad/KeyMatrix.c shared-bindings/keypad/Keys.c
103+
#: shared-bindings/keypad/ShiftRegisterKeys.c
101104
msgid "%q"
102105
msgstr ""
103106

@@ -193,7 +196,7 @@ msgstr ""
193196
msgid "%q must be array of type 'H'"
194197
msgstr ""
195198

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

@@ -1117,10 +1120,6 @@ msgstr ""
11171120
msgid "I2C peripheral in use"
11181121
msgstr ""
11191122

1120-
#: shared-bindings/audiobusio/I2SOut.c
1121-
msgid "I2SOut not available"
1122-
msgstr ""
1123-
11241123
#: ports/raspberrypi/bindings/rp2pio/StateMachine.c
11251124
msgid "In-buffer elements must be <= 4 bytes long"
11261125
msgstr ""
@@ -1282,6 +1281,10 @@ msgstr ""
12821281
msgid "Invalid format chunk size"
12831282
msgstr ""
12841283

1284+
#: shared-bindings/wifi/Radio.c
1285+
msgid "Invalid hex password"
1286+
msgstr ""
1287+
12851288
#: ports/espressif/common-hal/wifi/Radio.c
12861289
msgid "Invalid multicast MAC address"
12871290
msgstr ""
@@ -1708,10 +1711,6 @@ msgstr ""
17081711
msgid "Oversample must be multiple of 8."
17091712
msgstr ""
17101713

1711-
#: shared-bindings/audiobusio/PDMIn.c
1712-
msgid "PDMIn not available"
1713-
msgstr ""
1714-
17151714
#: shared-bindings/pwmio/PWMOut.c
17161715
msgid ""
17171716
"PWM frequency not writable when variable_frequency is False on construction."

shared-bindings/wifi/Radio.c

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
#include <string.h>
3131

32+
#include "py/unicode.h"
3233
#include "py/runtime.h"
3334
#include "py/objproperty.h"
3435

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

74+
STATIC void validate_hex_password(const uint8_t *buf, size_t len) {
75+
for (size_t i = 0; i < len; i++) {
76+
if (!unichar_isxdigit(buf[i])) {
77+
mp_raise_ValueError_varg(translate("Invalid hex password"));
78+
}
79+
}
80+
}
81+
7382

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

369381
if (authmodes != AUTHMODE_OPEN) {
370-
mp_arg_validate_length_range(password.len, 8, 63, MP_QSTR_password);
382+
mp_arg_validate_length_range(password.len, 8, 64, MP_QSTR_password);
383+
if (password.len == 64) {
384+
validate_hex_password(password.buf, password.len);
385+
}
371386
}
372387

373388
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);
@@ -406,6 +421,9 @@ MP_PROPERTY_GETTER(wifi_radio_ap_active_obj,
406421
//| """Connects to the given ssid and waits for an ip address. Reconnections are handled
407422
//| automatically once one connection succeeds.
408423
//|
424+
//| The length of ``password`` must be 0 if there is no password, 8-63 characters if it is ASCII,
425+
//| or exactly 64 hexadecimal characters if it is the hex form of the 256-bit key.
426+
//|
409427
//| By default, this will scan all channels and connect to the access point (AP) with the
410428
//| given ``ssid`` and greatest signal strength (rssi).
411429
//|
@@ -445,7 +463,10 @@ STATIC mp_obj_t wifi_radio_connect(size_t n_args, const mp_obj_t *pos_args, mp_m
445463
if (args[ARG_password].u_obj != mp_const_none) {
446464
mp_get_buffer_raise(args[ARG_password].u_obj, &password, MP_BUFFER_READ);
447465
if (password.len != 0) {
448-
mp_arg_validate_length_range(password.len, 8, 63, MP_QSTR_password);
466+
mp_arg_validate_length_range(password.len, 8, 64, MP_QSTR_password);
467+
if (password.len == 64) {
468+
validate_hex_password(password.buf, password.len);
469+
}
449470
}
450471
}
451472

0 commit comments

Comments
 (0)