|
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 | //| ...
|
@@ -368,8 +380,8 @@ STATIC mp_obj_t wifi_radio_start_ap(size_t n_args, const mp_obj_t *pos_args, mp_
|
368 | 380 |
|
369 | 381 | if (authmodes != AUTHMODE_OPEN) {
|
370 | 382 | mp_arg_validate_length_range(password.len, 8, 64, MP_QSTR_password);
|
371 |
| - if (password.len==64) { |
372 |
| - mp_arg_validate_valid_hex_password(password.len, password.buf); |
| 383 | + if (password.len == 64) { |
| 384 | + validate_hex_password(password.buf, password.len); |
373 | 385 | }
|
374 | 386 | }
|
375 | 387 |
|
@@ -409,6 +421,9 @@ MP_PROPERTY_GETTER(wifi_radio_ap_active_obj,
|
409 | 421 | //| """Connects to the given ssid and waits for an ip address. Reconnections are handled
|
410 | 422 | //| automatically once one connection succeeds.
|
411 | 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 | +//| |
412 | 427 | //| By default, this will scan all channels and connect to the access point (AP) with the
|
413 | 428 | //| given ``ssid`` and greatest signal strength (rssi).
|
414 | 429 | //|
|
@@ -449,8 +464,8 @@ STATIC mp_obj_t wifi_radio_connect(size_t n_args, const mp_obj_t *pos_args, mp_m
|
449 | 464 | mp_get_buffer_raise(args[ARG_password].u_obj, &password, MP_BUFFER_READ);
|
450 | 465 | if (password.len != 0) {
|
451 | 466 | mp_arg_validate_length_range(password.len, 8, 64, MP_QSTR_password);
|
452 |
| - if (password.len==64) { |
453 |
| - mp_arg_validate_valid_hex_password(password.len, password.buf); |
| 467 | + if (password.len == 64) { |
| 468 | + validate_hex_password(password.buf, password.len); |
454 | 469 | }
|
455 | 470 | }
|
456 | 471 | }
|
|
0 commit comments