Skip to content

SSL certificate verification #8854

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

Closed
Closed
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
4 changes: 2 additions & 2 deletions extmod/modussl_mbedtls.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ STATIC const mp_obj_type_t ussl_socket_type;
STATIC void mbedtls_debug(void *ctx, int level, const char *file, int line, const char *str) {
(void)ctx;
(void)level;
printf("DBG:%s:%04d: %s\n", file, line, str);
mp_printf(&mp_plat_print, "DBG:%s:%04d: %s\n", file, line, str);
}
#endif

Expand Down Expand Up @@ -175,7 +175,7 @@ STATIC mp_obj_ssl_socket_t *socket_new(mp_obj_t sock, struct ssl_args *args) {
mbedtls_ctr_drbg_init(&o->ctr_drbg);
#ifdef MBEDTLS_DEBUG_C
// Debug level (0-4) 1=warning, 2=info, 3=debug, 4=verbose
mbedtls_debug_set_threshold(0);
mbedtls_debug_set_threshold(3);
#endif

mbedtls_entropy_init(&o->entropy);
Expand Down
17 changes: 13 additions & 4 deletions ports/esp8266/modules/ntptime.py → extmod/ntptime.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import utime

try:
import usocket as socket
except:
Expand All @@ -7,9 +9,6 @@
except:
import struct

# (date(2000, 1, 1) - date(1900, 1, 1)).days * 24*60*60
NTP_DELTA = 3155673600

# The NTP host can be configured at runtime by doing: ntptime.host = 'myhost.org'
host = "pool.ntp.org"

Expand All @@ -26,14 +25,24 @@ def time():
finally:
s.close()
val = struct.unpack("!I", msg[40:44])[0]

EPOCH_YEAR = utime.gmtime(0)[0]
if EPOCH_YEAR == 2000:
# (date(2000, 1, 1) - date(1900, 1, 1)).days * 24*60*60
NTP_DELTA = 3155673600
elif EPOCH_YEAR == 1970:
# (date(1970, 1, 1) - date(1900, 1, 1)).days * 24*60*60
NTP_DELTA = 2208988800
else:
raise Exception("Unsupported epoch: {}".format(EPOCH_YEAR))

return val - NTP_DELTA


# There's currently no timezone support in MicroPython, and the RTC is set in UTC time.
def settime():
t = time()
import machine
import utime

tm = utime.gmtime(t)
machine.RTC().datetime((tm[0], tm[1], tm[2], tm[6] + 1, tm[3], tm[4], tm[5], 0))
2 changes: 1 addition & 1 deletion ports/esp32/boards/manifest.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
freeze("$(PORT_DIR)/modules")
freeze("$(MPY_DIR)/tools", ("upip.py", "upip_utarfile.py"))
freeze("$(MPY_DIR)/ports/esp8266/modules", "ntptime.py")
freeze("$(MPY_DIR)/extmod", "ntptime.py")
freeze("$(MPY_DIR)/drivers/dht", "dht.py")
freeze("$(MPY_DIR)/drivers/onewire")
include("$(MPY_DIR)/extmod/uasyncio/manifest.py")
Expand Down
1 change: 0 additions & 1 deletion ports/esp8266/boards/GENERIC_512K/manifest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
freeze("$(BOARD_DIR)", "_boot.py", opt=3)
freeze("$(PORT_DIR)/modules", ("apa102.py", "ntptime.py", "port_diag.py"))
freeze("$(MPY_DIR)/drivers/dht", "dht.py")
freeze("$(MPY_DIR)/drivers/onewire")
include("$(MPY_DIR)/extmod/webrepl/manifest.py")
Expand Down
1 change: 1 addition & 0 deletions ports/esp8266/boards/manifest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
freeze("$(PORT_DIR)/modules")
freeze("$(MPY_DIR)/extmod", "ntptime.py")
freeze("$(MPY_DIR)/tools", ("upip.py", "upip_utarfile.py"))
freeze("$(MPY_DIR)/drivers/dht", "dht.py")
freeze("$(MPY_DIR)/drivers/onewire")
Expand Down
1 change: 1 addition & 0 deletions ports/rp2/boards/PICO_W/manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

freeze("$(MPY_DIR)/tools", "upip.py")
freeze("$(MPY_DIR)/tools", "upip_utarfile.py")
freeze("$(MPY_DIR)/extmod", "ntptime.py")

if os.path.isdir(convert_path("$(MPY_LIB_DIR)")):
freeze("$(MPY_LIB_DIR)/python-ecosys/urequests", "urequests.py")
6 changes: 6 additions & 0 deletions ports/rp2/mbedtls/mbedtls_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@
#define MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE
#define MBEDTLS_X509_CRT_PARSE_C
#define MBEDTLS_X509_USE_C
#define MBEDTLS_HAVE_TIME
#define MBEDTLS_HAVE_TIME_DATE

// Memory allocation hooks
#include <stdlib.h>
Expand All @@ -103,6 +105,10 @@ void m_tracked_free(void *ptr);
#define MBEDTLS_PLATFORM_STD_FREE m_tracked_free
#define MBEDTLS_PLATFORM_SNPRINTF_MACRO snprintf

// Time hook
time_t rp2_rtctime_seconds(time_t *timer);
#define MBEDTLS_PLATFORM_TIME_MACRO rp2_rtctime_seconds

#include "mbedtls/check_config.h"

#endif /* MICROPY_INCLUDED_MBEDTLS_CONFIG_H */
9 changes: 9 additions & 0 deletions ports/rp2/mbedtls/mbedtls_port.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@

#include "mbedtls_config.h"

#include "hardware/rtc.h"
#include "shared/timeutils/timeutils.h"

extern uint8_t rosc_random_u8(size_t cycles);

int mbedtls_hardware_poll(void *data, unsigned char *output, size_t len, size_t *olen) {
Expand All @@ -39,4 +42,10 @@ int mbedtls_hardware_poll(void *data, unsigned char *output, size_t len, size_t
return 0;
}

time_t rp2_rtctime_seconds(time_t *timer) {
datetime_t t;
rtc_get_datetime(&t);
return timeutils_seconds_since_epoch(t.year, t.month, t.day, t.hour, t.min, t.sec);
}

#endif