|
29 | 29 |
|
30 | 30 | #include <string.h>
|
31 | 31 |
|
| 32 | +#include "py/unicode.h" |
32 | 33 | #include "py/runtime.h"
|
33 | 34 | #include "py/objproperty.h"
|
34 | 35 |
|
@@ -70,6 +71,14 @@ STATIC bool hostname_valid(const char *ptr, size_t len) {
|
70 | 71 | return !(partlen > 63);
|
71 | 72 | }
|
72 | 73 |
|
| 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 | + |
73 | 82 |
|
74 | 83 | //| class Radio:
|
75 | 84 | //| """Native wifi radio.
|
@@ -321,6 +330,9 @@ MP_DEFINE_CONST_FUN_OBJ_1(wifi_radio_stop_station_obj, wifi_radio_stop_station);
|
321 | 330 | //| ``OPEN`` will be used when the password is the empty string,
|
322 | 331 | //| otherwise ``authmode`` will be ``WPA_WPA2_PSK``.
|
323 | 332 | //|
|
| 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 | +//| |
324 | 336 | //| If ``max_connections`` is given, the access point will allow up to
|
325 | 337 | //| that number of stations to connect."""
|
326 | 338 | //| ...
|
@@ -367,7 +379,10 @@ STATIC mp_obj_t wifi_radio_start_ap(size_t n_args, const mp_obj_t *pos_args, mp_
|
367 | 379 | }
|
368 | 380 |
|
369 | 381 | 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 | + } |
371 | 386 | }
|
372 | 387 |
|
373 | 388 | 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,
|
406 | 421 | //| """Connects to the given ssid and waits for an ip address. Reconnections are handled
|
407 | 422 | //| automatically once one connection succeeds.
|
408 | 423 | //|
|
| 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 | +//| |
409 | 427 | //| By default, this will scan all channels and connect to the access point (AP) with the
|
410 | 428 | //| given ``ssid`` and greatest signal strength (rssi).
|
411 | 429 | //|
|
@@ -445,7 +463,10 @@ STATIC mp_obj_t wifi_radio_connect(size_t n_args, const mp_obj_t *pos_args, mp_m
|
445 | 463 | if (args[ARG_password].u_obj != mp_const_none) {
|
446 | 464 | mp_get_buffer_raise(args[ARG_password].u_obj, &password, MP_BUFFER_READ);
|
447 | 465 | 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 | + } |
449 | 470 | }
|
450 | 471 | }
|
451 | 472 |
|
|
0 commit comments