Hi,
It seems like there are lots of people having issues connecting to HTTPS servers. example.com works fine, but changing HOSTNAME and certificate to something like google.com (with gtsr1.pem certificate) like the following gives a -22 error (Invalid argument) when calling connect() :
Console log:
*** Booting nRF Connect SDK v2.5.1 *** HTTPS client sample started Bringing network interface up Provisioning certificate Connecting to the network +CEREG: 2,"BEB8","09743A06",7 +CSCON: 1 +CGEV: ME PDN ACT 0,0 +CNEC_ESM: 50,0 +CEREG: 5,"BEB8","09743A06",7,,,"11100000","11100000" Network connectivity established and IP address assigned Looking up google.com Resolved 142.250.179.174 (AF_INET) Connecting to google.com:443 connect() failed, err: 22 +CGEV: ME PDN DEACT 0 +CEREG: 0 +CGEV: ME DETACH +CSCON: 0 Network connectivity lost Disconnected from the network
Code:
/* * Copyright (c) 2020 Nordic Semiconductor ASA * * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause */ #include <string.h> #include <zephyr/kernel.h> #include <stdlib.h> #include <zephyr/net/socket.h> #include <zephyr/net/conn_mgr_monitor.h> #include <zephyr/net/conn_mgr_connectivity.h> #include <zephyr/net/tls_credentials.h> #if CONFIG_MODEM_KEY_MGMT #include <modem/modem_key_mgmt.h> #endif #define HTTPS_PORT "443" #define HTTPS_HOSTNAME "example.com" //#define HTTPS_HOSTNAME "gildev.dev" #define HTTPS_HOSTNAME "google.com" #define HTTP_HEAD \ "HEAD / HTTP/1.1\r\n" \ "Host: " HTTPS_HOSTNAME ":" HTTPS_PORT "\r\n" \ "Connection: close\r\n\r\n" #define HTTP_HEAD_LEN (sizeof(HTTP_HEAD) - 1) #define HTTP_HDR_END "\r\n\r\n" #define RECV_BUF_SIZE 2048 #define TLS_SEC_TAG 42 /* Macros used to subscribe to specific Zephyr NET management events. */ #define L4_EVENT_MASK (NET_EVENT_L4_CONNECTED | NET_EVENT_L4_DISCONNECTED) #define CONN_LAYER_EVENT_MASK (NET_EVENT_CONN_IF_FATAL_ERROR) static const char send_buf[] = HTTP_HEAD; static char recv_buf[RECV_BUF_SIZE]; static K_SEM_DEFINE(network_connected_sem, 0, 1); /* Certificate for `example.com` */ static const char cert[] = { //#include "../cert/DigiCertGlobalRootCA.pem" //#include "../cert/isrgrootx1.pem" //#include "../cert/lets-encrypt-r3.pem" #include "../cert/gtsr1.pem" }; /* Zephyr NET management event callback structures. */ static struct net_mgmt_event_callback l4_cb; static struct net_mgmt_event_callback conn_cb; BUILD_ASSERT(sizeof(cert) < KB(4), "Certificate too large"); /* Provision certificate to modem */ int cert_provision(void) { int err; printk("Provisioning certificate\n"); #if CONFIG_MODEM_KEY_MGMT bool exists; int mismatch; /* It may be sufficient for you application to check whether the correct * certificate is provisioned with a given tag directly using modem_key_mgmt_cmp(). * Here, for the sake of the completeness, we check that a certificate exists * before comparing it with what we expect it to be. */ err = modem_key_mgmt_exists(TLS_SEC_TAG, MODEM_KEY_MGMT_CRED_TYPE_CA_CHAIN, &exists); if (err) { printk("Failed to check for certificates err %d\n", err); return err; } if (exists) { mismatch = modem_key_mgmt_cmp(TLS_SEC_TAG, MODEM_KEY_MGMT_CRED_TYPE_CA_CHAIN, cert, strlen(cert)); if (!mismatch) { printk("Certificate match\n"); return 0; } printk("Certificate mismatch\n"); err = modem_key_mgmt_delete(TLS_SEC_TAG, MODEM_KEY_MGMT_CRED_TYPE_CA_CHAIN); if (err) { printk("Failed to delete existing certificate, err %d\n", err); } } printk("Provisioning certificate\n"); /* Provision certificate to the modem */ err = modem_key_mgmt_write(TLS_SEC_TAG, MODEM_KEY_MGMT_CRED_TYPE_CA_CHAIN, cert, sizeof(cert) - 1); if (err) { printk("Failed to provision certificate, err %d\n", err); return err; } #else /* CONFIG_MODEM_KEY_MGMT */ err = tls_credential_add(TLS_SEC_TAG, TLS_CREDENTIAL_CA_CERTIFICATE, cert, sizeof(cert)); if (err < 0) { printk("Failed to register CA certificate: %d\n", err); return err; } #endif /* !CONFIG_MODEM_KEY_MGMT */ return 0; } /* Setup TLS options on a given socket */ int tls_setup(int fd) { int err; int verify; /* Security tag that we have provisioned the certificate with */ const sec_tag_t tls_sec_tag[] = { TLS_SEC_TAG, }; /* Set up TLS peer verification */ enum { NONE = 0, OPTIONAL = 1, REQUIRED = 2, }; verify = REQUIRED; err = setsockopt(fd, SOL_TLS, TLS_PEER_VERIFY, &verify, sizeof(verify)); if (err) { printk("Failed to setup peer verification, err %d\n", errno); return err; } /* Associate the socket with the security tag * we have provisioned the certificate with. */ err = setsockopt(fd, SOL_TLS, TLS_SEC_TAG_LIST, tls_sec_tag, sizeof(tls_sec_tag)); if (err) { printk("Failed to setup TLS sec tag, err %d\n", errno); return err; } err = setsockopt(fd, SOL_TLS, TLS_HOSTNAME, HTTPS_HOSTNAME, sizeof(HTTPS_HOSTNAME) - 1); if (err) { printk("Failed to setup TLS hostname, err %d\n", errno); return err; } return 0; } static void on_net_event_l4_disconnected(void) { printk("Disconnected from the network\n"); } static void on_net_event_l4_connected(void) { k_sem_give(&network_connected_sem); } static void l4_event_handler(struct net_mgmt_event_callback *cb, uint32_t event, struct net_if *iface) { switch (event) { case NET_EVENT_L4_CONNECTED: printk("Network connectivity established and IP address assigned\n"); on_net_event_l4_connected(); break; case NET_EVENT_L4_DISCONNECTED: printk("Network connectivity lost\n"); on_net_event_l4_disconnected(); break; default: break; } } static void connectivity_event_handler(struct net_mgmt_event_callback *cb, uint32_t event, struct net_if *iface) { if (event == NET_EVENT_CONN_IF_FATAL_ERROR) { printk("Fatal error received from the connectivity layer\n"); return; } } static void send_http_request(void) { int err; int fd; char *p; int bytes; size_t off; struct addrinfo *res; struct addrinfo hints = { .ai_flags = AI_NUMERICSERV, /* Let getaddrinfo() set port */ .ai_socktype = SOCK_STREAM, }; char peer_addr[INET6_ADDRSTRLEN]; printk("Looking up %s\n", HTTPS_HOSTNAME); err = getaddrinfo(HTTPS_HOSTNAME, HTTPS_PORT, &hints, &res); if (err) { printk("getaddrinfo() failed, err %d\n", errno); return; } inet_ntop(res->ai_family, &((struct sockaddr_in *)(res->ai_addr))->sin_addr, peer_addr, INET6_ADDRSTRLEN); printk("Resolved %s (%s)\n", peer_addr, net_family2str(res->ai_family)); if (IS_ENABLED(CONFIG_SAMPLE_TFM_MBEDTLS)) { fd = socket(res->ai_family, SOCK_STREAM | SOCK_NATIVE_TLS, IPPROTO_TLS_1_2); } else { fd = socket(res->ai_family, SOCK_STREAM, IPPROTO_TLS_1_2); } if (fd == -1) { printk("Failed to open socket!\n"); goto clean_up; } /* Setup TLS socket options */ err = tls_setup(fd); if (err) { goto clean_up; } printk("Connecting to %s:%d\n", HTTPS_HOSTNAME, ntohs(((struct sockaddr_in *)(res->ai_addr))->sin_port)); err = connect(fd, res->ai_addr, res->ai_addrlen); if (err) { printk("connect() failed, err: %d\n", errno); perror("CONNECTFAIL"); goto clean_up; } off = 0; do { bytes = send(fd, &send_buf[off], HTTP_HEAD_LEN - off, 0); if (bytes < 0) { printk("send() failed, err %d\n", errno); goto clean_up; } off += bytes; } while (off < HTTP_HEAD_LEN); printk("Sent %d bytes\n", off); off = 0; do { bytes = recv(fd, &recv_buf[off], RECV_BUF_SIZE - off, 0); if (bytes < 0) { printk("recv() failed, err %d\n", errno); goto clean_up; } off += bytes; } while (bytes != 0 /* peer closed connection */); printk("Received %d bytes\n", off); /* Make sure recv_buf is NULL terminated (for safe use with strstr) */ if (off < sizeof(recv_buf)) { recv_buf[off] = '\0'; } else { recv_buf[sizeof(recv_buf) - 1] = '\0'; } /* Print HTTP response */ p = strstr(recv_buf, "\r\n"); if (p) { off = p - recv_buf; recv_buf[off + 1] = '\0'; printk("\n>\t %s\n\n", recv_buf); } printk("Finished, closing socket.\n"); clean_up: freeaddrinfo(res); (void)close(fd); } int main(void) { int err; printk("HTTPS client sample started\n\r"); /* Setup handler for Zephyr NET Connection Manager events. */ net_mgmt_init_event_callback(&l4_cb, l4_event_handler, L4_EVENT_MASK); net_mgmt_add_event_callback(&l4_cb); /* Setup handler for Zephyr NET Connection Manager Connectivity layer. */ net_mgmt_init_event_callback(&conn_cb, connectivity_event_handler, CONN_LAYER_EVENT_MASK); net_mgmt_add_event_callback(&conn_cb); printk("Bringing network interface up\n"); /* Connecting to the configured connectivity layer. * Wi-Fi or LTE depending on the board that the sample was built for. */ err = conn_mgr_all_if_up(true); if (err) { printk("conn_mgr_all_if_up, error: %d\n", err); return err; } /* Provision certificates before connecting to the network */ err = cert_provision(); if (err) { return 0; } printk("Connecting to the network\n"); err = conn_mgr_all_if_connect(true); if (err) { printk("conn_mgr_all_if_connect, error: %d\n", err); return 0; } k_sem_take(&network_connected_sem, K_FOREVER); send_http_request(); /* A small delay for the TCP connection teardown */ k_sleep(K_SECONDS(1)); /* The HTTP transaction is done, take the network connection down */ err = conn_mgr_all_if_disconnect(true); if (err) { printk("conn_mgr_all_if_disconnect, error: %d\n", err); } return 0; }
Certificate “gtsr1.pem”:
"-----BEGIN CERTIFICATE-----\n" "MIIFVzCCAz+gAwIBAgINAgPlk28xsBNJiGuiFzANBgkqhkiG9w0BAQwFADBHMQsw\n" "CQYDVQQGEwJVUzEiMCAGA1UEChMZR29vZ2xlIFRydXN0IFNlcnZpY2VzIExMQzEU\n" "MBIGA1UEAxMLR1RTIFJvb3QgUjEwHhcNMTYwNjIyMDAwMDAwWhcNMzYwNjIyMDAw\n" "MDAwWjBHMQswCQYDVQQGEwJVUzEiMCAGA1UEChMZR29vZ2xlIFRydXN0IFNlcnZp\n" "Y2VzIExMQzEUMBIGA1UEAxMLR1RTIFJvb3QgUjEwggIiMA0GCSqGSIb3DQEBAQUA\n" "A4ICDwAwggIKAoICAQC2EQKLHuOhd5s73L+UPreVp0A8of2C+X0yBoJx9vaMf/vo\n" "27xqLpeXo4xL+Sv2sfnOhB2x+cWX3u+58qPpvBKJXqeqUqv4IyfLpLGcY9vXmX7w\n" "Cl7raKb0xlpHDU0QM+NOsROjyBhsS+z8CZDfnWQpJSMHobTSPS5g4M/SCYe7zUjw\n" "TcLCeoiKu7rPWRnWr4+wB7CeMfGCwcDfLqZtbBkOtdh+JhpFAz2weaSUKK0Pfybl\n" "qAj+lug8aJ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"6u9AWpQKXCBfTkBdYiJ23//OYb2MI3jSNwLgjt7RETeJ9r/tSQdirpLsQBqvFAnZ\n" "0E6yove+7u7Y/9waLd64NnHi/Hm3lCXRSHNboTXns5lndcEZOitHTtNCjv0xyBZm\n" "2tIMPNuzjsmhDYAPexZ3FL//2wmUspO8IFgV6dtxQ/PeEMMA3KgqlbbC1j+Qa3bb\n" "bP6MvPJwNQzcmRk13NfIRmPVNnGuV/u3gm3c\n" "-----END CERTIFICATE-----\n"
Using “gts1c3.pem” certificate instead, I get a different error “113: Software caused connection abort”:
*** Booting nRF Connect SDK v2.5.1 *** HTTPS client sample started Bringing network interface up Provisioning certificate Connecting to the network +CEREG: 2,"BEB8","09743A06",7 +CSCON: 1 +CGEV: ME PDN ACT 0,0 +CNEC_ESM: 50,0 +CEREG: 5,"BEB8","09743A06",7,,,"11100000","11100000" Network connectivity established and IP address assigned Looking up google.com Resolved 142.250.179.174 (AF_INET) Connecting to google.com:443 connect() failed, err: 113 +CGEV: ME PDN DEACT 0 +CEREG: 0 +CGEV: ME DETACH +CSCON: 0 Network connectivity lost Disconnected from the network
Do you please have any idea about why this is happening? I have tried enabling other MbedTLS options without any more luck. Kconfig files are the default ones (“prj.conf” and “overlay-tfm_mbedtls.conf”). Thanks!