mirror of
https://github.com/cesanta/mongoose.git
synced 2026-11-06 15:51:25 +00:00
TLS API refactor
This commit is contained in:
1 parent
d9cbcb5fa8
commit
0613cc62f4
26 files changed
+1180
-1088
No files matched your search
+113
-65
@@ -2,7 +2,7 @@
|
||||
#include "printf.h"
|
||||
#include "tls.h"
|
||||
|
||||
#if MG_ENABLE_MBEDTLS
|
||||
#if MG_TLS == MG_TLS_MBED
|
||||
|
||||
#if defined(MBEDTLS_VERSION_NUMBER) && MBEDTLS_VERSION_NUMBER >= 0x03000000
|
||||
#define MGRNG , rng_get, NULL
|
||||
@@ -13,11 +13,7 @@
|
||||
void mg_tls_free(struct mg_connection *c) {
|
||||
struct mg_tls *tls = (struct mg_tls *) c->tls;
|
||||
if (tls != NULL) {
|
||||
free(tls->cafile);
|
||||
mbedtls_ssl_free(&tls->ssl);
|
||||
mbedtls_pk_free(&tls->pk);
|
||||
mbedtls_x509_crt_free(&tls->ca);
|
||||
mbedtls_x509_crt_free(&tls->cert);
|
||||
mbedtls_ssl_config_free(&tls->conf);
|
||||
free(tls);
|
||||
c->tls = NULL;
|
||||
@@ -70,7 +66,7 @@ static void debug_cb(void *c, int lev, const char *s, int n, const char *s2) {
|
||||
(void) s;
|
||||
}
|
||||
|
||||
#if defined(MBEDTLS_VERSION_NUMBER) && MBEDTLS_VERSION_NUMBER >= 0x03000000
|
||||
#ifdef MBEDTLS_SSL_SESSION_TICKETS
|
||||
static int rng_get(void *p_rng, unsigned char *buf, size_t len) {
|
||||
(void) p_rng;
|
||||
mg_random(buf, len);
|
||||
@@ -78,28 +74,20 @@ static int rng_get(void *p_rng, unsigned char *buf, size_t len) {
|
||||
}
|
||||
#endif
|
||||
|
||||
static struct mg_str mg_loadfile(struct mg_fs *fs, const char *path) {
|
||||
size_t n = 0;
|
||||
if (path[0] == '-') return mg_str(path);
|
||||
char *p = mg_file_read(fs, path, &n);
|
||||
return mg_str_n(p, n);
|
||||
}
|
||||
|
||||
void mg_tls_init(struct mg_connection *c, const struct mg_tls_opts *opts) {
|
||||
struct mg_fs *fs = opts->fs == NULL ? &mg_fs_posix : opts->fs;
|
||||
void mg_tls_init(struct mg_connection *c, struct mg_str hostname) {
|
||||
struct mg_tls_ctx *ctx = (struct mg_tls_ctx *) c->mgr->tls_ctx;
|
||||
struct mg_tls *tls = (struct mg_tls *) calloc(1, sizeof(*tls));
|
||||
int rc = 0;
|
||||
|
||||
c->tls = tls;
|
||||
if (c->tls == NULL) {
|
||||
mg_error(c, "TLS OOM");
|
||||
goto fail;
|
||||
}
|
||||
|
||||
MG_DEBUG(("%lu Setting TLS", c->id));
|
||||
mbedtls_ssl_init(&tls->ssl);
|
||||
mbedtls_ssl_config_init(&tls->conf);
|
||||
mbedtls_x509_crt_init(&tls->ca);
|
||||
mbedtls_x509_crt_init(&tls->cert);
|
||||
mbedtls_pk_init(&tls->pk);
|
||||
mbedtls_ssl_conf_dbg(&tls->conf, debug_cb, c);
|
||||
#if defined(MG_MBEDTLS_DEBUG_LEVEL)
|
||||
mbedtls_debug_set_threshold(MG_MBEDTLS_DEBUG_LEVEL);
|
||||
@@ -112,60 +100,47 @@ void mg_tls_init(struct mg_connection *c, const struct mg_tls_opts *opts) {
|
||||
goto fail;
|
||||
}
|
||||
mbedtls_ssl_conf_rng(&tls->conf, mbed_rng, c);
|
||||
if (opts->ca == NULL || strcmp(opts->ca, "*") == 0) {
|
||||
mbedtls_ssl_conf_authmode(&tls->conf, MBEDTLS_SSL_VERIFY_NONE);
|
||||
} else if (opts->ca != NULL && opts->ca[0] != '\0') {
|
||||
#if defined(MBEDTLS_X509_CA_CHAIN_ON_DISK)
|
||||
tls->cafile = strdup(opts->ca);
|
||||
rc = mbedtls_ssl_conf_ca_chain_file(&tls->conf, tls->cafile, NULL);
|
||||
if (rc != 0) {
|
||||
mg_error(c, "parse on-disk chain(%s) err %#x", tls->cafile, -rc);
|
||||
goto fail;
|
||||
}
|
||||
#else
|
||||
struct mg_str s = mg_loadfile(fs, opts->ca);
|
||||
rc = mbedtls_x509_crt_parse(&tls->ca, (uint8_t *) s.ptr, s.len + 1);
|
||||
if (opts->ca[0] != '-') free((char *) s.ptr);
|
||||
if (rc != 0) {
|
||||
mg_error(c, "parse(%s) err %#x", opts->ca, -rc);
|
||||
goto fail;
|
||||
}
|
||||
mbedtls_ssl_conf_ca_chain(&tls->conf, &tls->ca, NULL);
|
||||
#endif
|
||||
if (opts->srvname.len > 0) {
|
||||
char *x = mg_mprintf("%.*s", (int) opts->srvname.len, opts->srvname.ptr);
|
||||
mbedtls_ssl_set_hostname(&tls->ssl, x);
|
||||
free(x);
|
||||
}
|
||||
|
||||
if (c->is_client && ctx->client_ca.version) {
|
||||
mbedtls_ssl_conf_ca_chain(&tls->conf, &ctx->client_ca, NULL);
|
||||
mbedtls_ssl_conf_authmode(&tls->conf, MBEDTLS_SSL_VERIFY_REQUIRED);
|
||||
if (hostname.ptr != NULL && hostname.ptr[0] != '\0') {
|
||||
struct mg_addr addr;
|
||||
if (!mg_aton(hostname, &addr)) { // if srvname is not an IP address
|
||||
char *host = mg_mprintf("%.*s", (int) hostname.len, hostname.ptr);
|
||||
mbedtls_ssl_set_hostname(&tls->ssl, host);
|
||||
free(host);
|
||||
}
|
||||
}
|
||||
} else if (!c->is_client && ctx->server_ca.version) {
|
||||
mbedtls_ssl_conf_ca_chain(&tls->conf, &ctx->server_ca, NULL);
|
||||
mbedtls_ssl_conf_authmode(&tls->conf, MBEDTLS_SSL_VERIFY_REQUIRED);
|
||||
} else {
|
||||
mbedtls_ssl_conf_authmode(&tls->conf, MBEDTLS_SSL_VERIFY_NONE);
|
||||
}
|
||||
if (opts->cert != NULL && opts->cert[0] != '\0') {
|
||||
struct mg_str s = mg_loadfile(fs, opts->cert);
|
||||
const char *key = opts->certkey == NULL ? opts->cert : opts->certkey;
|
||||
rc = mbedtls_x509_crt_parse(&tls->cert, (uint8_t *) s.ptr, s.len + 1);
|
||||
if (opts->cert[0] != '-') free((char *) s.ptr);
|
||||
if (rc != 0) {
|
||||
mg_error(c, "parse(%s) err %#x", opts->cert, -rc);
|
||||
goto fail;
|
||||
}
|
||||
s = mg_loadfile(fs, key);
|
||||
rc = mbedtls_pk_parse_key(&tls->pk, (uint8_t *) s.ptr, s.len + 1, NULL,
|
||||
0 MGRNG);
|
||||
if (key[0] != '-') free((char *) s.ptr);
|
||||
if (rc != 0) {
|
||||
mg_error(c, "tls key(%s) %#x", key, -rc);
|
||||
goto fail;
|
||||
}
|
||||
rc = mbedtls_ssl_conf_own_cert(&tls->conf, &tls->cert, &tls->pk);
|
||||
if (rc != 0) {
|
||||
mg_error(c, "own cert %#x", -rc);
|
||||
goto fail;
|
||||
}
|
||||
if (c->is_client && ctx->client_cert.version &&
|
||||
(rc = mbedtls_ssl_conf_own_cert(&tls->conf, &ctx->client_cert,
|
||||
&ctx->client_key)) != 0) {
|
||||
mg_error(c, "own cert %#x", -rc);
|
||||
goto fail;
|
||||
}
|
||||
if (!c->is_client && ctx->server_cert.version &&
|
||||
(rc = mbedtls_ssl_conf_own_cert(&tls->conf, &ctx->server_cert,
|
||||
&ctx->server_key)) != 0) {
|
||||
mg_error(c, "own cert %#x", -rc);
|
||||
goto fail;
|
||||
}
|
||||
#ifdef MBEDTLS_SSL_SESSION_TICKETS
|
||||
mbedtls_ssl_conf_session_tickets_cb(&tls->conf, mbedtls_ssl_ticket_write,
|
||||
mbedtls_ssl_ticket_parse,
|
||||
&ctx->ticket_ctx);
|
||||
#endif
|
||||
|
||||
if ((rc = mbedtls_ssl_setup(&tls->ssl, &tls->conf)) != 0) {
|
||||
mg_error(c, "setup err %#x", -rc);
|
||||
goto fail;
|
||||
}
|
||||
|
||||
c->tls = tls;
|
||||
c->is_tls = 1;
|
||||
c->is_tls_hs = 1;
|
||||
@@ -200,4 +175,77 @@ long mg_tls_send(struct mg_connection *c, const void *buf, size_t len) {
|
||||
if (n <= 0) return MG_IO_ERR;
|
||||
return n;
|
||||
}
|
||||
|
||||
static bool load_cert(struct mg_str str, mbedtls_x509_crt *p) {
|
||||
int rc;
|
||||
if (str.ptr == NULL || str.ptr[0] == '\0' || str.ptr[0] == '*') return true;
|
||||
if (str.ptr[0] == '-') str.len++; // PEM, include trailing NUL
|
||||
if ((rc = mbedtls_x509_crt_parse(p, (uint8_t *) str.ptr, str.len)) != 0) {
|
||||
MG_ERROR(("cert err %#x", -rc));
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool load_key(struct mg_str str, mbedtls_pk_context *p) {
|
||||
int rc;
|
||||
if (str.ptr == NULL || str.ptr[0] == '\0' || str.ptr[0] == '*') return true;
|
||||
if (str.ptr[0] == '-') str.len++; // PEM, include trailing NUL
|
||||
if ((rc = mbedtls_pk_parse_key(p, (uint8_t *) str.ptr, str.len, NULL,
|
||||
0 MGRNG)) != 0) {
|
||||
MG_ERROR(("key err %#x", -rc));
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void mg_tls_ctx_init(struct mg_mgr *mgr, const struct mg_tls_opts *opts) {
|
||||
struct mg_tls_ctx *ctx = (struct mg_tls_ctx *) calloc(1, sizeof(*ctx));
|
||||
if (ctx == NULL) goto fail;
|
||||
MG_DEBUG(("Setting up TLS context"));
|
||||
|
||||
#if defined(MG_MBEDTLS_DEBUG_LEVEL)
|
||||
mbedtls_debug_set_threshold(MG_MBEDTLS_DEBUG_LEVEL);
|
||||
#endif
|
||||
|
||||
if (!load_cert(opts->client_ca, &ctx->client_ca)) goto fail;
|
||||
if (!load_cert(opts->server_ca, &ctx->server_ca)) goto fail;
|
||||
if (!load_cert(opts->client_cert, &ctx->client_cert)) goto fail;
|
||||
if (!load_cert(opts->server_cert, &ctx->server_cert)) goto fail;
|
||||
if (!load_key(opts->server_key, &ctx->server_key)) goto fail;
|
||||
if (!load_key(opts->client_key, &ctx->client_key)) goto fail;
|
||||
|
||||
#ifdef MBEDTLS_SSL_SESSION_TICKETS
|
||||
{
|
||||
int rc;
|
||||
mbedtls_ssl_ticket_init(&ctx->ticket_ctx);
|
||||
if ((rc = mbedtls_ssl_ticket_setup(&ctx->ticket_ctx, rng_get, NULL,
|
||||
MBEDTLS_CIPHER_AES_128_GCM, 86400)) !=
|
||||
0) {
|
||||
MG_ERROR(("setup session tickets err %#x", -rc));
|
||||
goto fail;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
mgr->tls_ctx = ctx;
|
||||
return;
|
||||
fail:
|
||||
mg_tls_ctx_free(mgr);
|
||||
}
|
||||
|
||||
void mg_tls_ctx_free(struct mg_mgr *mgr) {
|
||||
struct mg_tls_ctx *ctx = (struct mg_tls_ctx *) mgr->tls_ctx;
|
||||
if (ctx != NULL) {
|
||||
mbedtls_x509_crt_free(&ctx->server_cert);
|
||||
mbedtls_pk_free(&ctx->server_key);
|
||||
mbedtls_x509_crt_free(&ctx->client_cert);
|
||||
mbedtls_pk_free(&ctx->client_key);
|
||||
mbedtls_x509_crt_free(&ctx->client_ca);
|
||||
#ifdef MBEDTLS_SSL_SESSION_TICKETS
|
||||
mbedtls_ssl_ticket_free(&ctx->ticket_ctx);
|
||||
#endif
|
||||
free(ctx);
|
||||
mgr->tls_ctx = NULL;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
Reference in new issue
Block a user