Add support for SSL client certificate (load_cert_chain)
Tested with badssl.com: 1. Get client certificates from https://badssl.com/download/ 2. Convert public portion with `openssl x509 -in badssl.com-client.pem -out CIRCUITPY/cert.pem` 3. Convert private portion with `openssl rsa -in badssl.com-client.pem -out CIRCUITPY/privkey.pem` and the password `badssl.com` 4. Put wifi settings in CIRCUITPY/.env 5. Run the below Python script: ```py import os import wifi import socketpool import ssl import adafruit_requests TEXT_URL = "https://client.badssl.com/" wifi.radio.connect(os.getenv('WIFI_SSID'), os.getenv('WIFI_PASSWORD')) pool = socketpool.SocketPool(wifi.radio) context = ssl.create_default_context() requests = adafruit_requests.Session(pool, context) print(f"Fetching from {TEXT_URL} without certificate (should fail)") response = requests.get(TEXT_URL) print(f"{response.status_code=}, should be 400 Bad Request") input("hit enter to continue\r") print("Loading client certificate") context.load_cert_chain("/cert.pem", "privkey.pem") requests = adafruit_requests.Session(pool, context) print(f"Fetching from {TEXT_URL} with certificate (should succeed)") response = requests.get(TEXT_URL) print(f"{response.status_code=}, should be 200 OK") ```
This commit is contained in:
parent
14c9028c1f
commit
c98174eea5
@ -87,3 +87,10 @@ bool common_hal_ssl_sslcontext_get_check_hostname(ssl_sslcontext_obj_t *self) {
|
||||
void common_hal_ssl_sslcontext_set_check_hostname(ssl_sslcontext_obj_t *self, bool value) {
|
||||
self->ssl_config.skip_common_name = !value;
|
||||
}
|
||||
|
||||
void common_hal_ssl_sslcontext_load_cert_chain(ssl_sslcontext_obj_t *self, mp_buffer_info_t *cert_buf, mp_buffer_info_t *key_buf) {
|
||||
self->ssl_config.clientcert_buf = cert_buf->buf;
|
||||
self->ssl_config.clientcert_bytes = cert_buf->len + 1;
|
||||
self->ssl_config.clientkey_buf = key_buf->buf;
|
||||
self->ssl_config.clientkey_bytes = key_buf->len + 1;
|
||||
}
|
||||
|
@ -39,12 +39,10 @@ void common_hal_ssl_sslcontext_construct(ssl_sslcontext_obj_t *self) {
|
||||
|
||||
void common_hal_ssl_sslcontext_load_verify_locations(ssl_sslcontext_obj_t *self,
|
||||
const char *cadata) {
|
||||
mp_raise_NotImplementedError(NULL);
|
||||
|
||||
// self->crt_bundle_attach = NULL;
|
||||
// self->use_global_ca_store = false;
|
||||
// self->cacert_buf = (const unsigned char *)cadata;
|
||||
// self->cacert_bytes = strlen(cadata) + 1;
|
||||
self->crt_bundle_attach = NULL;
|
||||
self->use_global_ca_store = false;
|
||||
self->cacert_buf = (const unsigned char *)cadata;
|
||||
self->cacert_bytes = strlen(cadata) + 1;
|
||||
}
|
||||
|
||||
void common_hal_ssl_sslcontext_set_default_verify_paths(ssl_sslcontext_obj_t *self) {
|
||||
@ -61,3 +59,8 @@ bool common_hal_ssl_sslcontext_get_check_hostname(ssl_sslcontext_obj_t *self) {
|
||||
void common_hal_ssl_sslcontext_set_check_hostname(ssl_sslcontext_obj_t *self, bool value) {
|
||||
self->check_name = value;
|
||||
}
|
||||
|
||||
void common_hal_ssl_sslcontext_load_cert_chain(ssl_sslcontext_obj_t *self, mp_buffer_info_t *cert_buf, mp_buffer_info_t *key_buf) {
|
||||
self->cert_buf = *cert_buf;
|
||||
self->key_buf = *key_buf;
|
||||
}
|
||||
|
@ -36,4 +36,5 @@ typedef struct {
|
||||
const unsigned char *cacert_buf;
|
||||
size_t cacert_bytes;
|
||||
int (*crt_bundle_attach)(mbedtls_ssl_config *conf);
|
||||
mp_buffer_info_t cert_buf, key_buf;
|
||||
} ssl_sslcontext_obj_t;
|
||||
|
@ -197,6 +197,21 @@ ssl_sslsocket_obj_t *common_hal_ssl_sslcontext_wrap_socket(ssl_sslcontext_obj_t
|
||||
|
||||
mbedtls_ssl_set_bio(&o->ssl, &o->sock, _mbedtls_ssl_send, _mbedtls_ssl_recv, NULL);
|
||||
|
||||
if (self->cert_buf.buf != NULL) {
|
||||
ret = mbedtls_pk_parse_key(&o->pkey, self->key_buf.buf, self->key_buf.len + 1, NULL, 0);
|
||||
if (ret != 0) {
|
||||
goto cleanup;
|
||||
}
|
||||
ret = mbedtls_x509_crt_parse(&o->cert, self->cert_buf.buf, self->cert_buf.len + 1);
|
||||
if (ret != 0) {
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
ret = mbedtls_ssl_conf_own_cert(&o->conf, &o->cert, &o->pkey);
|
||||
if (ret != 0) {
|
||||
goto cleanup;
|
||||
}
|
||||
}
|
||||
return o;
|
||||
cleanup:
|
||||
mbedtls_pk_free(&o->pkey);
|
||||
|
@ -77,6 +77,7 @@
|
||||
#define MBEDTLS_AES_C
|
||||
#define MBEDTLS_ASN1_PARSE_C
|
||||
#define MBEDTLS_ASN1_WRITE_C
|
||||
#define MBEDTLS_BASE64_C
|
||||
#define MBEDTLS_BIGNUM_C
|
||||
#define MBEDTLS_CIPHER_C
|
||||
#define MBEDTLS_CTR_DRBG_C
|
||||
@ -90,6 +91,7 @@
|
||||
#define MBEDTLS_MD5_C
|
||||
#define MBEDTLS_OID_C
|
||||
#define MBEDTLS_PKCS5_C
|
||||
#define MBEDTLS_PEM_PARSE_C
|
||||
#define MBEDTLS_PK_C
|
||||
#define MBEDTLS_PK_PARSE_C
|
||||
#define MBEDTLS_PLATFORM_C
|
||||
|
@ -27,6 +27,7 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "extmod/vfs.h"
|
||||
#include "py/objtuple.h"
|
||||
#include "py/objlist.h"
|
||||
#include "py/objproperty.h"
|
||||
@ -52,6 +53,47 @@ STATIC mp_obj_t ssl_sslcontext_make_new(const mp_obj_type_t *type, size_t n_args
|
||||
return MP_OBJ_FROM_PTR(s);
|
||||
}
|
||||
|
||||
//| def load_cert_chain(self, certfile: str, keyfile: str) -> None:
|
||||
//| """Load a private key and the corresponding certificate.
|
||||
//|
|
||||
//| The certfile string must be the path to a single file in PEM format
|
||||
//| containing the certificate as well as any number of CA certificates
|
||||
//| needed to establish the certificate's authenticity. The keyfile string
|
||||
//| must point to a file containing the private key.
|
||||
//| """
|
||||
|
||||
STATIC void get_file_contents(mp_obj_t name_obj, mp_buffer_info_t *bufinfo) {
|
||||
mp_obj_t file = mp_call_function_2(MP_OBJ_FROM_PTR(&mp_builtin_open_obj), name_obj, MP_OBJ_NEW_QSTR(MP_QSTR_rb));
|
||||
mp_obj_t dest[2];
|
||||
mp_load_method(file, MP_QSTR_read, dest);
|
||||
mp_obj_t result = mp_call_method_n_kw(0, 0, dest);
|
||||
mp_get_buffer_raise(result, bufinfo, MP_BUFFER_READ);
|
||||
}
|
||||
|
||||
STATIC mp_obj_t ssl_sslcontext_load_cert_chain(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
||||
enum { ARG_certfile, ARG_keyfile };
|
||||
static const mp_arg_t allowed_args[] = {
|
||||
{ MP_QSTR_certfile, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = mp_const_none} },
|
||||
{ MP_QSTR_keyfile, MP_ARG_OBJ, {.u_obj = mp_const_none} },
|
||||
};
|
||||
ssl_sslcontext_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
|
||||
|
||||
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
||||
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
||||
|
||||
mp_buffer_info_t cert_buf, key_buf;
|
||||
get_file_contents(args[ARG_certfile].u_obj, &cert_buf);
|
||||
if (args[ARG_keyfile].u_obj != mp_const_none) {
|
||||
get_file_contents(args[ARG_keyfile].u_obj, &key_buf);
|
||||
} else {
|
||||
key_buf = cert_buf;
|
||||
}
|
||||
|
||||
common_hal_ssl_sslcontext_load_cert_chain(self, &cert_buf, &key_buf);
|
||||
return mp_const_none;
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(ssl_sslcontext_load_cert_chain_obj, 1, ssl_sslcontext_load_cert_chain);
|
||||
|
||||
//| def load_verify_locations(self, cadata: Optional[str] = None) -> None:
|
||||
//| """Load a set of certification authority (CA) certificates used to validate
|
||||
//| other peers' certificates."""
|
||||
@ -146,6 +188,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_KW(ssl_sslcontext_wrap_socket_obj, 1, ssl_sslcont
|
||||
|
||||
STATIC const mp_rom_map_elem_t ssl_sslcontext_locals_dict_table[] = {
|
||||
{ MP_ROM_QSTR(MP_QSTR_wrap_socket), MP_ROM_PTR(&ssl_sslcontext_wrap_socket_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_load_cert_chain), MP_ROM_PTR(&ssl_sslcontext_load_cert_chain_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_load_verify_locations), MP_ROM_PTR(&ssl_sslcontext_load_verify_locations_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_set_default_verify_paths), MP_ROM_PTR(&ssl_sslcontext_set_default_verify_paths_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_check_hostname), MP_ROM_PTR(&ssl_sslcontext_check_hostname_obj) },
|
||||
|
@ -46,5 +46,6 @@ void common_hal_ssl_sslcontext_set_default_verify_paths(ssl_sslcontext_obj_t *se
|
||||
|
||||
bool common_hal_ssl_sslcontext_get_check_hostname(ssl_sslcontext_obj_t *self);
|
||||
void common_hal_ssl_sslcontext_set_check_hostname(ssl_sslcontext_obj_t *self, bool value);
|
||||
void common_hal_ssl_sslcontext_load_cert_chain(ssl_sslcontext_obj_t *self, mp_buffer_info_t *cert_buf, mp_buffer_info_t *key_buf);
|
||||
|
||||
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_SSL_SSLCONTEXT_H
|
||||
|
Loading…
Reference in New Issue
Block a user