From patchwork Mon May 21 12:02:44 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hauke Mehrtens X-Patchwork-Id: 917581 X-Patchwork-Delegate: blogic@openwrt.org Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=lists.openwrt.org (client-ip=2607:7c80:54:e::133; helo=bombadil.infradead.org; envelope-from=openwrt-devel-bounces+incoming=patchwork.ozlabs.org@lists.openwrt.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=hauke-m.de Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="Wf1tVYKC"; dkim-atps=neutral Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:e::133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 40qHXK43b6z9s1w for ; Mon, 21 May 2018 22:05:05 +1000 (AEST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:Cc:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Subject:Message-Id: Date:To:From:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References: List-Owner; bh=S0z4Se2xvRqcKe9TZCS+d59a1+z5yRJWlyySCyTcuAM=; b=Wf1tVYKCthVJ2I Z+HSNwJfII58PtF99COTpkbcdo9JxXaDeJZTPDv0xw0yHxusyceKRrBHFYC+VECdZNeNq4poBHr7i Mz3Jx6ogv/5tA7YxKNt9301mV7iew+x8abHPuZIlBFK4ZENjJ+0fl4BiJh9UtFFoxJSHb0puGRR3s sxg11Zvt3OYKyYoToug0C6+zEsaRMtYphgsfyIwW48yigF6YRL6M62N4hHtRexVHKOzcKS4KmyFlm TXfh95xjcevSGIJKb0xKdsnXHjoxLaGIT1AB2mj8Cm3nQeyo2oFoWtFd4XalzwP1cuull/z6JrhPz 1utB/NCAuPaTbz0lt5Jg==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.90_1 #2 (Red Hat Linux)) id 1fKjYb-0003V2-3r; Mon, 21 May 2018 12:04:53 +0000 Received: from mx1.mailbox.org ([80.241.60.212]) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1fKjWq-0001cR-Mw for openwrt-devel@lists.openwrt.org; Mon, 21 May 2018 12:03:45 +0000 Received: from smtp1.mailbox.org (smtp1.mailbox.org [80.241.60.240]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.mailbox.org (Postfix) with ESMTPS id 308D447CF8; Mon, 21 May 2018 14:02:53 +0200 (CEST) X-Virus-Scanned: amavisd-new at heinlein-support.de Received: from smtp1.mailbox.org ([80.241.60.240]) by gerste.heinlein-support.de (gerste.heinlein-support.de [91.198.250.173]) (amavisd-new, port 10030) with ESMTP id Ofy9NJeY85X9; Mon, 21 May 2018 14:02:50 +0200 (CEST) From: Hauke Mehrtens To: openwrt-devel@lists.openwrt.org Date: Mon, 21 May 2018 14:02:44 +0200 Message-Id: <20180521120244.4500-1-hauke@hauke-m.de> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20180521_050304_932122_5E17928C X-CRM114-Status: GOOD ( 15.61 ) X-Spam-Score: -0.7 (/) X-Spam-Report: SpamAssassin version 3.4.1 on bombadil.infradead.org summary: Content analysis details: (-0.7 points) pts rule name description ---- ---------------------- -------------------------------------------------- -0.7 RCVD_IN_DNSWL_LOW RBL: Sender listed at http://www.dnswl.org/, low trust [80.241.60.212 listed in list.dnswl.org] -0.0 SPF_PASS SPF: sender matches SPF record Subject: [OpenWrt-Devel] [PATCH][ustream-ssl] mbedtls: Add support for a session cache X-BeenThere: openwrt-devel@lists.openwrt.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Hauke Mehrtens MIME-Version: 1.0 Sender: "openwrt-devel" Errors-To: openwrt-devel-bounces+incoming=patchwork.ozlabs.org@lists.openwrt.org This allows the client to reuse the settings from a previous session and no full key exchange is needed. The partially key exchange takes less than 0.1 seconds compared to over a second needed for a full key exchange. Signed-off-by: Hauke Mehrtens --- ustream-mbedtls.c | 14 ++++++++++++++ ustream-mbedtls.h | 7 +++++++ 2 files changed, 21 insertions(+) diff --git a/ustream-mbedtls.c b/ustream-mbedtls.c index e176afe..0b747d2 100644 --- a/ustream-mbedtls.c +++ b/ustream-mbedtls.c @@ -138,6 +138,12 @@ __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); @@ -154,6 +160,11 @@ __ustream_ssl_context_new(bool server) mbedtls_ssl_conf_authmode(conf, MBEDTLS_SSL_VERIFY_NONE); mbedtls_ssl_conf_rng(conf, _urandom, NULL); +#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; } @@ -214,6 +225,9 @@ __hidden int __ustream_ssl_set_key_file(struct ustream_ssl_ctx *ctx, const char __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); diff --git a/ustream-mbedtls.h b/ustream-mbedtls.h index a489867..70bd4ea 100644 --- a/ustream-mbedtls.h +++ b/ustream-mbedtls.h @@ -28,11 +28,18 @@ #include #include +#if defined(MBEDTLS_SSL_CACHE_C) +#include +#endif + struct ustream_ssl_ctx { mbedtls_ssl_config conf; mbedtls_pk_context key; mbedtls_x509_crt ca_cert; mbedtls_x509_crt cert; +#if defined(MBEDTLS_SSL_CACHE_C) + mbedtls_ssl_cache_context cache; +#endif bool server; };