mirror of
https://github.com/polhenarejos/pico-keys-sdk
synced 2026-05-26 08:05:10 +02:00
Fix windows build.
Signed-off-by: Pol Henarejos <pol.henarejos@cttc.es>
This commit is contained in:
@@ -21,6 +21,7 @@
|
||||
#define _PTHREAD_H_
|
||||
#include <windows.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
typedef HANDLE pthread_t;
|
||||
typedef CRITICAL_SECTION pthread_mutex_t;
|
||||
@@ -51,6 +52,11 @@ static inline int pthread_mutex_destroy(pthread_mutex_t *m) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
static inline int pthread_detach(pthread_t t) {
|
||||
CloseHandle(t);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Thread
|
||||
static DWORD WINAPI thread_entry(LPVOID param) {
|
||||
void **args = (void **)param;
|
||||
|
||||
@@ -25,7 +25,9 @@
|
||||
#endif
|
||||
#include "random.h"
|
||||
#include "mbedtls/ecdsa.h"
|
||||
#ifndef _MSC_VER
|
||||
#include <stdalign.h>
|
||||
#endif
|
||||
|
||||
#ifdef PICO_RP2350
|
||||
|
||||
|
||||
@@ -98,7 +98,7 @@ int select_app(const uint8_t *aid, size_t aid_len) {
|
||||
}
|
||||
|
||||
|
||||
__attribute__((weak)) int picokey_init(void) {
|
||||
WEAK int picokey_init(void) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -49,6 +49,19 @@ int gettimeofday(struct timeval* tp, struct timezone* tzp)
|
||||
tp->tv_usec = (long)(system_time.wMilliseconds * 1000);
|
||||
return 0;
|
||||
}
|
||||
int settimeofday(const struct timeval* tp, const struct timezone* tzp)
|
||||
{
|
||||
(void)tzp;
|
||||
SYSTEMTIME st;
|
||||
FILETIME ft;
|
||||
uint64_t time;
|
||||
|
||||
time = ((uint64_t)tp->tv_sec * 10000000L) + ((uint64_t)tp->tv_usec * 10) + ((uint64_t)116444736000000000ULL);
|
||||
ft.dwLowDateTime = (uint32_t)(time & 0xFFFFFFFF);
|
||||
ft.dwHighDateTime = (uint32_t)(time >> 32);
|
||||
FileTimeToSystemTime(&ft, &st);
|
||||
return SetSystemTime(&st);
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
@@ -25,7 +25,9 @@
|
||||
#include "compat/esp_compat.h"
|
||||
#include <time.h>
|
||||
#else
|
||||
#ifndef _MSC_VER
|
||||
#include <sys/time.h>
|
||||
#endif
|
||||
#include <time.h>
|
||||
#include "compat/board.h"
|
||||
#endif
|
||||
|
||||
@@ -54,6 +54,12 @@
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#define WEAK
|
||||
#else
|
||||
#define WEAK __attribute__((weak))
|
||||
#endif
|
||||
|
||||
extern int picokey_init(void);
|
||||
|
||||
extern void low_flash_init_core1(void);
|
||||
@@ -93,14 +99,14 @@ static inline uint32_t get_uint32_be(const uint8_t *b) {
|
||||
static inline uint32_t get_uint32_le(const uint8_t *b) {
|
||||
return make_uint32_le(b[0], b[1], b[2], b[3]);
|
||||
}
|
||||
static inline uint32_t put_uint32_be(uint32_t n, uint8_t *b) {
|
||||
static inline uint8_t put_uint32_be(uint32_t n, uint8_t *b) {
|
||||
*b++ = (n >> 24) & 0xff;
|
||||
*b++ = (n >> 16) & 0xff;
|
||||
*b++ = (n >> 8) & 0xff;
|
||||
*b = n & 0xff;
|
||||
return 4;
|
||||
}
|
||||
static inline uint32_t put_uint32_le(uint32_t n, uint8_t *b) {
|
||||
static inline uint8_t put_uint32_le(uint32_t n, uint8_t *b) {
|
||||
*b++ = n & 0xff;
|
||||
*b++ = (n >> 8) & 0xff;
|
||||
*b++ = (n >> 16) & 0xff;
|
||||
|
||||
@@ -88,7 +88,7 @@ const uint8_t atr_rescue[] = {
|
||||
};
|
||||
|
||||
extern const uint8_t *ccid_atr;
|
||||
__attribute__((weak)) int set_atr(void) {
|
||||
WEAK int set_atr(void) {
|
||||
ccid_atr = atr_rescue;
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -182,7 +182,7 @@ static int driver_write_ccid(uint8_t itf, const uint8_t *tx_buffer, uint16_t buf
|
||||
}
|
||||
uint32_t written = tud_vendor_n_write(itf, tx_buffer, buffer_size);
|
||||
if (written > 0) {
|
||||
tud_vendor_n_flush(itf);
|
||||
tud_vendor_n_write_flush(itf);
|
||||
|
||||
ccid_tx[itf].r_ptr += (uint16_t)written;
|
||||
if (ccid_tx[itf].r_ptr >= ccid_tx[itf].w_ptr) {
|
||||
@@ -365,13 +365,13 @@ void ccid_init(void) {
|
||||
ccid_init_buffers();
|
||||
}
|
||||
|
||||
#ifndef ENABLE_EMULATION
|
||||
|
||||
void tud_vendor_tx_cb(uint8_t itf, uint32_t sent_bytes) {
|
||||
(void) sent_bytes;
|
||||
tud_vendor_n_write_flush(itf);
|
||||
}
|
||||
|
||||
#ifndef ENABLE_EMULATION
|
||||
|
||||
static void ccid_init_cb(void) {
|
||||
vendord_init();
|
||||
}
|
||||
|
||||
@@ -14,7 +14,14 @@
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#ifndef WIN32_LEAN_AND_MEAN
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#endif
|
||||
#include <winsock2.h>
|
||||
#include <ws2tcpip.h>
|
||||
#include <windows.h>
|
||||
#endif
|
||||
#include "emulation.h"
|
||||
#include <stdio.h>
|
||||
#ifndef _MSC_VER
|
||||
@@ -29,7 +36,6 @@ typedef int socket_t;
|
||||
#define INVALID_SOCKET (-1)
|
||||
#define SOCKET_ERROR (-1)
|
||||
#else
|
||||
#include <ws2tcpip.h>
|
||||
#define O_NONBLOCK _O_NONBLOCK
|
||||
#define close closesocket
|
||||
typedef SOCKET socket_t;
|
||||
@@ -58,6 +64,16 @@ uint16_t emul_rx_size = 0, emul_tx_size = 0;
|
||||
extern int cbor_parse(uint8_t cmd, const uint8_t *data, size_t len);
|
||||
pthread_t hcore0, hcore1;
|
||||
|
||||
#ifdef _MSC_VER
|
||||
static void log_sock_error(const char *ctx) {
|
||||
fprintf(stderr, "%s failed (WSAGetLastError=%d)\n", ctx, WSAGetLastError());
|
||||
}
|
||||
#else
|
||||
static void log_sock_error(const char *ctx) {
|
||||
fprintf(stderr, "%s failed (errno=%d)\n", ctx, errno);
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifndef _MSC_VER
|
||||
static int msleep(long msec) {
|
||||
struct timespec ts;
|
||||
@@ -89,7 +105,7 @@ int emul_init(const char *host, uint16_t port) {
|
||||
}
|
||||
#endif
|
||||
if ((ccid_sock = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) {
|
||||
perror("socket");
|
||||
log_sock_error("socket(ccid)");
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -99,13 +115,13 @@ int emul_init(const char *host, uint16_t port) {
|
||||
// Convert IPv4 and IPv6 addresses from text to binary
|
||||
// form
|
||||
if (inet_pton(AF_INET, host, &serv_addr.sin_addr) <= 0) {
|
||||
perror("inet_pton");
|
||||
log_sock_error("inet_pton(ccid)");
|
||||
close(ccid_sock);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (connect(ccid_sock, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
|
||||
perror("connect");
|
||||
log_sock_error("connect(ccid)");
|
||||
close(ccid_sock);
|
||||
return -1;
|
||||
}
|
||||
@@ -128,12 +144,12 @@ int emul_init(const char *host, uint16_t port) {
|
||||
struct sockaddr_in server_sockaddr;
|
||||
|
||||
if ((hid_server_sock = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) {
|
||||
perror("socket");
|
||||
log_sock_error("socket(hid_server)");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (setsockopt(hid_server_sock, SOL_SOCKET, SO_REUSEADDR, (void *) &yes, sizeof yes) != 0) {
|
||||
perror("setsockopt");
|
||||
log_sock_error("setsockopt(SO_REUSEADDR)");
|
||||
close(hid_server_sock);
|
||||
return 1;
|
||||
}
|
||||
@@ -153,16 +169,17 @@ int emul_init(const char *host, uint16_t port) {
|
||||
|
||||
if (bind(hid_server_sock, (struct sockaddr *) &server_sockaddr,
|
||||
sizeof server_sockaddr) != 0) {
|
||||
perror("bind");
|
||||
log_sock_error("bind(hid_server)");
|
||||
close(hid_server_sock);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (listen(hid_server_sock, 0) != 0) {
|
||||
perror("listen");
|
||||
if (listen(hid_server_sock, SOMAXCONN) != 0) {
|
||||
log_sock_error("listen(hid_server)");
|
||||
close(hid_server_sock);
|
||||
return 1;
|
||||
}
|
||||
fprintf(stderr, "HID server listening on 0.0.0.0:%u\n", hid_port);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -52,7 +52,7 @@ static inline uint32_t tud_vendor_n_read(uint8_t itf, uint8_t *buffer, uint32_t
|
||||
}
|
||||
extern void tud_vendor_tx_cb(uint8_t itf, uint32_t sent_bytes);
|
||||
extern uint32_t tud_vendor_n_write(uint8_t itf, const uint8_t *buffer, uint32_t n);
|
||||
static inline uint32_t tud_vendor_n_flush(uint8_t itf) {
|
||||
static inline uint32_t tud_vendor_n_write_flush(uint8_t itf) {
|
||||
(void) itf;
|
||||
return emul_tx_size;
|
||||
}
|
||||
|
||||
@@ -18,7 +18,14 @@
|
||||
#include "picokeys.h"
|
||||
#include "pico_time.h"
|
||||
#include "rest.h"
|
||||
#ifdef _WIN32
|
||||
#include <string.h>
|
||||
#define strcasecmp _stricmp
|
||||
#define strncasecmp _strnicmp
|
||||
#define strdup _strdup
|
||||
#else
|
||||
#include <strings.h>
|
||||
#endif
|
||||
#include "random.h"
|
||||
#include "crypto_utils.h"
|
||||
#include "serial.h"
|
||||
@@ -142,7 +149,7 @@ void rest_session_clear_all(void) {
|
||||
memset(rest_sessions, 0, sizeof(rest_sessions));
|
||||
}
|
||||
|
||||
#ifdef DEBUG_APDU
|
||||
#if DEBUG_APDU
|
||||
void rest_debug_dump_payload(const char *tag, const char *buffer, size_t len) {
|
||||
size_t i;
|
||||
if (buffer == NULL) {
|
||||
@@ -291,13 +298,14 @@ bool rest_content_type_is_json(const char *content_type) {
|
||||
return strncasecmp(content_type, "application/json", 16) == 0;
|
||||
}
|
||||
|
||||
__attribute__((weak)) const rest_route_t *rest_get_routes(size_t *count) {
|
||||
#ifndef _MSC_VER
|
||||
WEAK const rest_route_t *rest_get_routes(size_t *count) {
|
||||
if (count != NULL) {
|
||||
*count = 0;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
static int x25519_hkdf_derive_key32(const uint8_t sk[32], const uint8_t pk[32], const uint8_t *salt, size_t salt_len, const uint8_t *info, size_t info_len, uint8_t out_key[32]) {
|
||||
int ret = -1;
|
||||
|
||||
@@ -183,7 +183,7 @@ extern void rest_session_clear_all(void);
|
||||
extern int rest_session_derive_key(const rest_session_t *session, uint8_t sk[32]);
|
||||
extern int rest_session_derive_shared(const rest_session_t *session, uint8_t derived_key[32]);
|
||||
|
||||
#ifdef DEBUG_APDU
|
||||
#if DEBUG_APDU
|
||||
extern void rest_debug_dump_payload(const char *tag, const char *buffer, size_t len);
|
||||
#define REST_DEBUG_LOG(...) printf(__VA_ARGS__)
|
||||
#else
|
||||
|
||||
@@ -23,7 +23,19 @@
|
||||
#include "serial.h"
|
||||
|
||||
#include <ctype.h>
|
||||
#ifdef _WIN32
|
||||
#include "compat/pthread_win32.h"
|
||||
typedef SOCKET socket_t;
|
||||
typedef int socklen_t;
|
||||
#define close closesocket
|
||||
#include <string.h>
|
||||
#define strcasecmp _stricmp
|
||||
#define strncasecmp _strnicmp
|
||||
#else
|
||||
#include <strings.h>
|
||||
typedef int socket_t;
|
||||
#define INVALID_SOCKET (-1)
|
||||
#endif
|
||||
#include "mbedtls/md.h"
|
||||
#include "mbedtls/hkdf.h"
|
||||
#include "crypto_utils.h"
|
||||
@@ -48,11 +60,10 @@
|
||||
#ifndef ENABLE_EMULATION
|
||||
static struct tcp_pcb *listener_pcb = NULL;
|
||||
#else
|
||||
static int listener_sock = -1;
|
||||
#ifndef _MSC_VER
|
||||
extern socket_t tls_listener_sock;
|
||||
static socket_t listener_sock = -1;
|
||||
static pthread_t rest_thread;
|
||||
#endif
|
||||
#endif
|
||||
static rest_conn_t conns[REST_MAX_CONNS];
|
||||
|
||||
typedef struct {
|
||||
@@ -196,7 +207,7 @@ void rest_task(void) {
|
||||
|
||||
static rest_conn_t *alloc_conn(
|
||||
#ifdef ENABLE_EMULATION
|
||||
int sock
|
||||
socket_t sock
|
||||
#else
|
||||
struct tcp_pcb *pcb
|
||||
#endif
|
||||
@@ -244,9 +255,10 @@ void rest_close_conn(rest_conn_t *conn) {
|
||||
mbedtls_ssl_free(&conn->ssl);
|
||||
}
|
||||
if (conn->sock >= 0) {
|
||||
#ifndef _MSC_VER
|
||||
(void)close(conn->sock);
|
||||
#ifdef _MSC_VER
|
||||
shutdown((SOCKET)conn->sock, 1);
|
||||
#endif
|
||||
(void)close(conn->sock);
|
||||
}
|
||||
clear_conn(conn);
|
||||
#else
|
||||
@@ -382,11 +394,7 @@ static void send_response(rest_conn_t *conn, int status_code, const char *status
|
||||
}
|
||||
#ifdef ENABLE_EMULATION
|
||||
while (sent_total < (size_t)header_len) {
|
||||
#ifndef _MSC_VER
|
||||
ssize_t n = send(conn->sock, headers_buf + sent_total, (size_t)header_len - sent_total, 0);
|
||||
#else
|
||||
int n = -1;
|
||||
#endif
|
||||
int n = send((socket_t)conn->sock, headers_buf + sent_total, (size_t)header_len - sent_total, 0);
|
||||
if (n <= 0) {
|
||||
rest_close_conn(conn);
|
||||
return;
|
||||
@@ -395,11 +403,7 @@ static void send_response(rest_conn_t *conn, int status_code, const char *status
|
||||
}
|
||||
sent_total = 0;
|
||||
while (sent_total < body_len) {
|
||||
#ifndef _MSC_VER
|
||||
ssize_t n = send(conn->sock, body + sent_total, body_len - sent_total, 0);
|
||||
#else
|
||||
int n = -1;
|
||||
#endif
|
||||
int n = send((socket_t)conn->sock, body + sent_total, body_len - sent_total, 0);
|
||||
if (n <= 0) {
|
||||
rest_close_conn(conn);
|
||||
return;
|
||||
@@ -833,7 +837,7 @@ void rest_check_and_load_credentials(void) {
|
||||
mbedtls_ecp_write_key_ext(&ecdsa, &olen, pkey, sizeof(pkey));
|
||||
mbedtls_ecdsa_free(&ecdsa);
|
||||
}
|
||||
file_put_data(ef, pkey, olen);
|
||||
file_put_data(ef, pkey, (uint16_t)olen);
|
||||
mbedtls_platform_zeroize(pkey, sizeof(pkey));
|
||||
printf("TLS key generated and stored, length: %u bytes\n", (unsigned)olen);
|
||||
}
|
||||
@@ -883,7 +887,7 @@ void rest_check_and_load_credentials(void) {
|
||||
|
||||
ret = mbedtls_x509write_crt_pem(&crt, cert_pem, sizeof(cert_pem), random_fill_iterator, NULL);
|
||||
if (ret == 0) {
|
||||
file_put_data(ef, cert_pem, strlen((char *)cert_pem) + 1);
|
||||
file_put_data(ef, cert_pem, (uint16_t)strlen((char *)cert_pem) + 1);
|
||||
printf("TLS certificate generated and stored, length: %u bytes\n", (unsigned)strlen((char *)cert_pem));
|
||||
}
|
||||
out:
|
||||
@@ -1070,7 +1074,6 @@ err_t rest_server_init(rest_conn_type_t conn_type) {
|
||||
}
|
||||
#else
|
||||
static int emulation_rest_port(void) {
|
||||
#ifndef _MSC_VER
|
||||
const char *port_env = getenv("PICO_REST_PORT");
|
||||
long v;
|
||||
if (port_env == NULL || *port_env == '\0') {
|
||||
@@ -1082,21 +1085,17 @@ static int emulation_rest_port(void) {
|
||||
return REST_PORT;
|
||||
}
|
||||
return (int)v;
|
||||
#else
|
||||
return REST_PORT;
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifndef _MSC_VER
|
||||
static void *rest_emulation_thread(void *arg) {
|
||||
struct sockaddr_in peer;
|
||||
int listen_fd = (int)(intptr_t)arg;
|
||||
socket_t listen_fd = (socket_t)(uintptr_t)arg;
|
||||
|
||||
while (true) {
|
||||
socklen_t peer_len = sizeof(peer);
|
||||
int accepted = accept(listen_fd, (struct sockaddr *)&peer, &peer_len);
|
||||
socket_t accepted = accept(listen_fd, (struct sockaddr *)&peer, &peer_len);
|
||||
rest_conn_t *conn;
|
||||
if (accepted < 0) {
|
||||
if (accepted == INVALID_SOCKET) {
|
||||
continue;
|
||||
}
|
||||
conn = alloc_conn(accepted);
|
||||
@@ -1121,7 +1120,7 @@ static void *rest_emulation_thread(void *arg) {
|
||||
}
|
||||
}
|
||||
else {
|
||||
ssize_t n = recv(conn->sock, conn->request + conn->request_len, REST_MAX_REQUEST_SIZE - conn->request_len, 0);
|
||||
int n = recv((socket_t)conn->sock, conn->request + conn->request_len, (int)(REST_MAX_REQUEST_SIZE - conn->request_len), 0);
|
||||
if (n <= 0) {
|
||||
rest_close_conn(conn);
|
||||
break;
|
||||
@@ -1137,61 +1136,70 @@ static void *rest_emulation_thread(void *arg) {
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static err_t rest_server_init_conn(socket_t *list_sock, int port, rest_conn_type_t conn_type, const tls_credentials_t *tls_creds) {
|
||||
struct sockaddr_in addr;
|
||||
#ifdef _MSC_VER
|
||||
char one = 1;
|
||||
#else
|
||||
int one = 1;
|
||||
#endif
|
||||
|
||||
static err_t rest_server_init_conn(int *listener_sock, int port, rest_conn_type_t conn_type, const tls_credentials_t *tls_credentials) {
|
||||
#ifndef _MSC_VER
|
||||
struct sockaddr_in addr;
|
||||
int one = 1;
|
||||
|
||||
if (*listener_sock >= 0) {
|
||||
if (*list_sock != INVALID_SOCKET) {
|
||||
printf("Listener socket for port %d already initialized\n", port);
|
||||
return ERR_OK;
|
||||
}
|
||||
if (conn_type & REST_CONN_TLS) {
|
||||
if (tls_credentials == NULL || tls_credentials->tls_key_pem == NULL || tls_credentials->tls_cert_pem == NULL) {
|
||||
if (tls_creds == NULL || tls_creds->tls_key_pem == NULL || tls_creds->tls_cert_pem == NULL) {
|
||||
return ERR_VAL;
|
||||
}
|
||||
if (tls_init_tls_context(tls_credentials) != 0) {
|
||||
if (tls_init_tls_context(tls_creds) != 0) {
|
||||
return ERR_VAL;
|
||||
}
|
||||
}
|
||||
*listener_sock = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (*listener_sock < 0) {
|
||||
*list_sock = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (*list_sock == INVALID_SOCKET) {
|
||||
printf("Failed to create listener socket for port %d\n", port);
|
||||
return -1;
|
||||
}
|
||||
if (setsockopt(*listener_sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) != 0) {
|
||||
(void)close(*listener_sock);
|
||||
*listener_sock = -1;
|
||||
if (setsockopt(*list_sock, SOL_SOCKET, SO_REUSEADDR, (const char *)&one, sizeof(one)) != 0) {
|
||||
printf("Failed to set SO_REUSEADDR on listener socket for port %d\n", port);
|
||||
(void)close(*list_sock);
|
||||
*list_sock = INVALID_SOCKET;
|
||||
return -1;
|
||||
}
|
||||
memset(&addr, 0, sizeof(addr));
|
||||
addr.sin_family = AF_INET;
|
||||
addr.sin_port = htons((uint16_t)port);
|
||||
addr.sin_addr.s_addr = htonl(INADDR_ANY);
|
||||
if (bind(*listener_sock, (struct sockaddr *)&addr, sizeof(addr)) != 0) {
|
||||
(void)close(*listener_sock);
|
||||
*listener_sock = -1;
|
||||
if (bind(*list_sock, (struct sockaddr *)&addr, sizeof(addr)) != 0) {
|
||||
printf("Failed bind %d\n", errno);
|
||||
(void)close(*list_sock);
|
||||
*list_sock = INVALID_SOCKET;
|
||||
return -1;
|
||||
}
|
||||
if (listen(*listener_sock, REST_MAX_CONNS) != 0) {
|
||||
(void)close(*listener_sock);
|
||||
*listener_sock = -1;
|
||||
if (listen(*list_sock, REST_MAX_CONNS) != 0) {
|
||||
printf("Failed listen %d\n", errno);
|
||||
(void)close(*list_sock);
|
||||
*list_sock = INVALID_SOCKET;
|
||||
return -1;
|
||||
}
|
||||
if (pthread_create(&rest_thread, NULL, rest_emulation_thread, (void *)(intptr_t)(*listener_sock)) != 0) {
|
||||
(void)close(*listener_sock);
|
||||
*listener_sock = -1;
|
||||
if (pthread_create(&rest_thread, NULL, rest_emulation_thread, (void *)(uintptr_t)(*list_sock)) != 0) {
|
||||
(void)close(*list_sock);
|
||||
*list_sock = INVALID_SOCKET;
|
||||
return -1;
|
||||
}
|
||||
(void)pthread_detach(rest_thread);
|
||||
return ERR_OK;
|
||||
#else
|
||||
return -1;
|
||||
#endif
|
||||
}
|
||||
|
||||
err_t rest_server_init(rest_conn_type_t conn_type) {
|
||||
|
||||
#ifdef _MSC_VER
|
||||
WSADATA wsaData;
|
||||
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
|
||||
printf("winsock initialization failure\n");
|
||||
}
|
||||
#endif
|
||||
if (conn_type & REST_CONN_PLAIN) {
|
||||
if (rest_server_init_conn(&listener_sock, emulation_rest_port(), REST_CONN_PLAIN, NULL) != ERR_OK) {
|
||||
return -1;
|
||||
|
||||
@@ -53,7 +53,7 @@ typedef enum {
|
||||
typedef struct {
|
||||
bool in_use;
|
||||
#ifdef ENABLE_EMULATION
|
||||
int sock;
|
||||
intptr_t sock;
|
||||
#else
|
||||
struct tcp_pcb *pcb;
|
||||
#endif
|
||||
|
||||
@@ -18,7 +18,13 @@
|
||||
#include "picokeys.h"
|
||||
#include "rest_server_tls.h"
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#include <string.h>
|
||||
#define strcasecmp _stricmp
|
||||
#define strncasecmp _strnicmp
|
||||
#else
|
||||
#include <strings.h>
|
||||
#endif
|
||||
|
||||
extern void rest_close_conn(rest_conn_t *conn);
|
||||
extern void rest_handle_request(rest_conn_t *conn);
|
||||
@@ -37,12 +43,12 @@ mbedtls_x509_crt tls_cert;
|
||||
mbedtls_pk_context tls_key;
|
||||
tls_credentials_t tls_credentials = {0};
|
||||
|
||||
int tls_init_tls_context(const tls_credentials_t *tls_credentials) {
|
||||
int tls_init_tls_context(const tls_credentials_t *tls_creds) {
|
||||
int ret;
|
||||
if (tls_ctx_ready) {
|
||||
return 0;
|
||||
}
|
||||
if (tls_credentials == NULL || tls_credentials->tls_key_pem == NULL || tls_credentials->tls_cert_pem == NULL) {
|
||||
if (tls_creds == NULL || tls_creds->tls_key_pem == NULL || tls_creds->tls_cert_pem == NULL) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -51,11 +57,11 @@ int tls_init_tls_context(const tls_credentials_t *tls_credentials) {
|
||||
mbedtls_pk_init(&tls_key);
|
||||
mbedtls_pk_setup(&tls_key, mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY));
|
||||
|
||||
ret = mbedtls_x509_crt_parse(&tls_cert, (const unsigned char *)tls_credentials->tls_cert_pem, tls_credentials->tls_cert_pem_len);
|
||||
ret = mbedtls_x509_crt_parse(&tls_cert, (const unsigned char *)tls_creds->tls_cert_pem, tls_creds->tls_cert_pem_len);
|
||||
if (ret != 0) {
|
||||
return ret;
|
||||
}
|
||||
ret = mbedtls_ecp_read_key(MBEDTLS_ECP_DP_SECP256R1, mbedtls_pk_ec(tls_key), (const unsigned char *)tls_credentials->tls_key_pem, tls_credentials->tls_key_pem_len);
|
||||
ret = mbedtls_ecp_read_key(MBEDTLS_ECP_DP_SECP256R1, mbedtls_pk_ec(tls_key), (const unsigned char *)tls_creds->tls_key_pem, tls_creds->tls_key_pem_len);
|
||||
if (ret != 0) {
|
||||
return ret;
|
||||
}
|
||||
@@ -147,15 +153,26 @@ static int request_is_complete(const char *request, size_t request_len, size_t *
|
||||
|
||||
#ifdef ENABLE_EMULATION
|
||||
|
||||
#ifndef _MSC_VER
|
||||
#ifdef _MSC_VER
|
||||
#include "compat/pthread_win32.h"
|
||||
typedef SOCKET socket_t;
|
||||
typedef int socklen_t;
|
||||
#define close closesocket
|
||||
#include <string.h>
|
||||
#define strcasecmp _stricmp
|
||||
#define strncasecmp _strnicmp
|
||||
#else
|
||||
#include <strings.h>
|
||||
typedef int socket_t;
|
||||
#include <arpa/inet.h>
|
||||
#include <errno.h>
|
||||
#include <netinet/in.h>
|
||||
#include <pthread.h>
|
||||
#include <sys/socket.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
int tls_listener_sock = -1;
|
||||
socket_t tls_listener_sock = -1;
|
||||
|
||||
int emulation_rest_tls_port(void) {
|
||||
const char *port_env = getenv("PICO_REST_TLS_PORT");
|
||||
@@ -172,32 +189,49 @@ int emulation_rest_tls_port(void) {
|
||||
}
|
||||
|
||||
int tls_send_cb(void *ctx, const unsigned char *buf, size_t len) {
|
||||
const int fd = *(const int *)ctx;
|
||||
ssize_t r = send(fd, buf, len, 0);
|
||||
const socket_t fd = (socket_t)(*(const intptr_t *)ctx);
|
||||
int r = send(fd, (const char *)buf, (int)len, 0);
|
||||
if (r >= 0) {
|
||||
return (int)r;
|
||||
return r;
|
||||
}
|
||||
#ifdef _MSC_VER
|
||||
{
|
||||
int e = WSAGetLastError();
|
||||
if (e == WSAEWOULDBLOCK || e == WSAEINTR) {
|
||||
return MBEDTLS_ERR_SSL_WANT_WRITE;
|
||||
}
|
||||
}
|
||||
#else
|
||||
if (errno == EWOULDBLOCK || errno == EAGAIN || errno == EINTR) {
|
||||
return MBEDTLS_ERR_SSL_WANT_WRITE;
|
||||
}
|
||||
#endif
|
||||
return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
||||
}
|
||||
|
||||
int tls_recv_cb(void *ctx, unsigned char *buf, size_t len) {
|
||||
const int fd = *(const int *)ctx;
|
||||
ssize_t r = recv(fd, buf, len, 0);
|
||||
const socket_t fd = (socket_t)(*(const intptr_t *)ctx);
|
||||
int r = recv(fd, (char *)buf, (int)len, 0);
|
||||
if (r > 0) {
|
||||
return (int)r;
|
||||
return r;
|
||||
}
|
||||
if (r == 0) {
|
||||
return MBEDTLS_ERR_SSL_CONN_EOF;
|
||||
}
|
||||
#ifdef _MSC_VER
|
||||
{
|
||||
int e = WSAGetLastError();
|
||||
if (e == WSAEWOULDBLOCK || e == WSAEINTR) {
|
||||
return MBEDTLS_ERR_SSL_WANT_READ;
|
||||
}
|
||||
}
|
||||
#else
|
||||
if (errno == EWOULDBLOCK || errno == EAGAIN || errno == EINTR) {
|
||||
return MBEDTLS_ERR_SSL_WANT_READ;
|
||||
}
|
||||
#endif
|
||||
return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
||||
}
|
||||
#endif
|
||||
|
||||
#else
|
||||
|
||||
|
||||
@@ -52,7 +52,6 @@ extern int tls_init_tls_context(const tls_credentials_t *tls_credentials);
|
||||
|
||||
#ifdef ENABLE_EMULATION
|
||||
extern int emulation_rest_tls_port(void);
|
||||
extern int tls_listener_sock;
|
||||
extern void tls_handle_client(int client_fd);
|
||||
#else
|
||||
extern struct tcp_pcb *tls_listener_pcb;
|
||||
|
||||
Reference in New Issue
Block a user