return 0;
}
-static const int default_ciphersuites[] =
+#define AES_GCM_CIPHERS(v) \
+ MBEDTLS_TLS_##v##_WITH_AES_128_GCM_SHA256, \
+ MBEDTLS_TLS_##v##_WITH_AES_256_GCM_SHA384
+
+#define AES_CBC_CIPHERS(v) \
+ MBEDTLS_TLS_##v##_WITH_AES_128_CBC_SHA, \
+ MBEDTLS_TLS_##v##_WITH_AES_256_CBC_SHA
+
+#define AES_CIPHERS(v) \
+ AES_GCM_CIPHERS(v), \
+ AES_CBC_CIPHERS(v)
+
+static const int default_ciphersuites_server[] =
{
-#if defined(MBEDTLS_AES_C)
-#if defined(MBEDTLS_SHA2_C)
- MBEDTLS_TLS_RSA_WITH_AES_256_CBC_SHA256,
-#endif /* MBEDTLS_SHA2_C */
-#if defined(MBEDTLS_GCM_C) && defined(MBEDTLS_SHA4_C)
- MBEDTLS_TLS_RSA_WITH_AES_256_GCM_SHA384,
-#endif /* MBEDTLS_SHA2_C */
- MBEDTLS_TLS_RSA_WITH_AES_256_CBC_SHA,
-#endif
-#if defined(MBEDTLS_CAMELLIA_C)
-#if defined(MBEDTLS_SHA2_C)
- MBEDTLS_TLS_RSA_WITH_CAMELLIA_256_CBC_SHA256,
-#endif /* MBEDTLS_SHA2_C */
- MBEDTLS_TLS_RSA_WITH_CAMELLIA_256_CBC_SHA,
-#endif
-#if defined(MBEDTLS_AES_C)
-#if defined(MBEDTLS_SHA2_C)
- MBEDTLS_TLS_RSA_WITH_AES_128_CBC_SHA256,
-#endif /* MBEDTLS_SHA2_C */
-#if defined(MBEDTLS_GCM_C) && defined(MBEDTLS_SHA2_C)
- MBEDTLS_TLS_RSA_WITH_AES_128_GCM_SHA256,
-#endif /* MBEDTLS_SHA2_C */
- MBEDTLS_TLS_RSA_WITH_AES_128_CBC_SHA,
-#endif
-#if defined(MBEDTLS_CAMELLIA_C)
-#if defined(MBEDTLS_SHA2_C)
- MBEDTLS_TLS_RSA_WITH_CAMELLIA_128_CBC_SHA256,
-#endif /* MBEDTLS_SHA2_C */
- MBEDTLS_TLS_RSA_WITH_CAMELLIA_128_CBC_SHA,
-#endif
-#if defined(MBEDTLS_DES_C)
+ MBEDTLS_TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256,
+ AES_GCM_CIPHERS(ECDHE_ECDSA),
+ MBEDTLS_TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
+ AES_GCM_CIPHERS(ECDHE_RSA),
+ AES_CBC_CIPHERS(ECDHE_RSA),
+ AES_CIPHERS(RSA),
+ 0
+};
+
+static const int default_ciphersuites_client[] =
+{
+ MBEDTLS_TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256,
+ AES_GCM_CIPHERS(ECDHE_ECDSA),
+ MBEDTLS_TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
+ AES_GCM_CIPHERS(ECDHE_RSA),
+ MBEDTLS_TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
+ AES_GCM_CIPHERS(DHE_RSA),
+ AES_CBC_CIPHERS(ECDHE_ECDSA),
+ AES_CBC_CIPHERS(ECDHE_RSA),
+ AES_CBC_CIPHERS(DHE_RSA),
+ MBEDTLS_TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA,
+ AES_CIPHERS(RSA),
MBEDTLS_TLS_RSA_WITH_3DES_EDE_CBC_SHA,
-#endif
0
};
+
__hidden struct ustream_ssl_ctx *
__ustream_ssl_context_new(bool server)
{
mbedtls_x509_crt_init(&ctx->cert);
mbedtls_x509_crt_init(&ctx->ca_cert);
+#if defined(MBEDTLS_SSL_CACHE_C)
+ mbedtls_ssl_cache_init(&ctx->cache);
+ mbedtls_ssl_cache_set_timeout(&ctx->cache, 30 * 60);
+ mbedtls_ssl_cache_set_max_entries(&ctx->cache, 5);
+#endif
+
conf = &ctx->conf;
mbedtls_ssl_config_init(conf);
- mbedtls_ssl_conf_ciphersuites(conf, default_ciphersuites);
- if (server)
- ep = MBEDTLS_SSL_IS_SERVER;
- else
- ep = MBEDTLS_SSL_IS_CLIENT;
+ ep = server ? MBEDTLS_SSL_IS_SERVER : MBEDTLS_SSL_IS_CLIENT;
mbedtls_ssl_config_defaults(conf, ep, MBEDTLS_SSL_TRANSPORT_STREAM,
MBEDTLS_SSL_PRESET_DEFAULT);
mbedtls_ssl_conf_authmode(conf, MBEDTLS_SSL_VERIFY_NONE);
mbedtls_ssl_conf_rng(conf, _urandom, NULL);
+ if (server) {
+ mbedtls_ssl_conf_ciphersuites(conf, default_ciphersuites_server);
+ mbedtls_ssl_conf_min_version(conf, MBEDTLS_SSL_MAJOR_VERSION_3,
+ MBEDTLS_SSL_MINOR_VERSION_3);
+ } else
+ mbedtls_ssl_conf_ciphersuites(conf, default_ciphersuites_client);
+
+#if defined(MBEDTLS_SSL_CACHE_C)
+ mbedtls_ssl_conf_session_cache(conf, &ctx->cache,
+ mbedtls_ssl_cache_get,
+ mbedtls_ssl_cache_set);
+#endif
return ctx;
}
if (!ctx->cert.version)
return;
- if (!ctx->server) {
- mbedtls_ssl_conf_ca_chain(&ctx->conf, &ctx->cert, NULL);
- return;
- }
-
if (!ctx->key.pk_info)
return;
- if (ctx->cert.next)
- mbedtls_ssl_conf_ca_chain(&ctx->conf, ctx->cert.next, NULL);
mbedtls_ssl_conf_own_cert(&ctx->conf, &ctx->cert, &ctx->key);
}
return 0;
}
+__hidden int __ustream_ssl_set_ciphers(struct ustream_ssl_ctx *ctx, const char *ciphers)
+{
+ int *ciphersuites = NULL, *tmp, id;
+ char *cipherstr, *p, *last, c;
+ size_t len = 0;
+
+ if (ciphers == NULL)
+ return -1;
+
+ cipherstr = strdup(ciphers);
+
+ if (cipherstr == NULL)
+ return -1;
+
+ for (p = cipherstr, last = p;; p++) {
+ if (*p == ':' || *p == 0) {
+ c = *p;
+ *p = 0;
+
+ id = mbedtls_ssl_get_ciphersuite_id(last);
+
+ if (id != 0) {
+ tmp = realloc(ciphersuites, (len + 2) * sizeof(int));
+
+ if (tmp == NULL) {
+ free(ciphersuites);
+ free(cipherstr);
+
+ return -1;
+ }
+
+ ciphersuites = tmp;
+ ciphersuites[len++] = id;
+ ciphersuites[len] = 0;
+ }
+
+ if (c == 0)
+ break;
+
+ last = p + 1;
+ }
+
+ /*
+ * mbedTLS expects cipher names with dashes while many sources elsewhere
+ * like the Firefox wiki or Wireshark specify ciphers with underscores,
+ * so simply convert all underscores to dashes to accept both notations.
+ */
+ else if (*p == '_') {
+ *p = '-';
+ }
+ }
+
+ free(cipherstr);
+
+ if (len == 0)
+ return -1;
+
+ mbedtls_ssl_conf_ciphersuites(&ctx->conf, ciphersuites);
+ free(ctx->ciphersuites);
+
+ ctx->ciphersuites = ciphersuites;
+
+ return 0;
+}
+
__hidden void __ustream_ssl_context_free(struct ustream_ssl_ctx *ctx)
{
+#if defined(MBEDTLS_SSL_CACHE_C)
+ mbedtls_ssl_cache_free(&ctx->cache);
+#endif
mbedtls_pk_free(&ctx->key);
mbedtls_x509_crt_free(&ctx->ca_cert);
mbedtls_x509_crt_free(&ctx->cert);
mbedtls_ssl_config_free(&ctx->conf);
+ free(ctx->ciphersuites);
free(ctx);
}
if (ssl_do_wait(ret))
return U_SSL_PENDING;
+ if (ret == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY)
+ return 0;
+
ustream_ssl_error(us, ret);
return U_SSL_ERROR;
}