From patchwork Mon Mar 23 23:42:02 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Tom van Leeuwen X-Patchwork-Id: 1047 Return-Path: Delivered-To: patchwork@openvpn.net Delivered-To: patchwork@openvpn.net Received: from director8.mail.ord1d.rsapps.net ([172.27.255.50]) by backend30.mail.ord1d.rsapps.net with LMTP id aIJxHMTreV7GeQAAIUCqbw for ; Tue, 24 Mar 2020 07:15:16 -0400 Received: from proxy8.mail.iad3a.rsapps.net ([172.27.255.50]) by director8.mail.ord1d.rsapps.net with LMTP id gPuDGcTreV4iPwAAfY0hYg ; Tue, 24 Mar 2020 07:15:16 -0400 Received: from smtp25.gate.iad3a ([172.27.255.50]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) by proxy8.mail.iad3a.rsapps.net with LMTP id KOPrEsTreV6wYwAAsBr/qg ; Tue, 24 Mar 2020 07:15:16 -0400 X-Spam-Threshold: 95 X-Spam-Score: 0 X-Spam-Flag: NO X-Virus-Scanned: OK X-Orig-To: openvpnslackdevel@openvpn.net X-Originating-Ip: [216.105.38.7] Authentication-Results: smtp25.gate.iad3a.rsapps.net; iprev=pass policy.iprev="216.105.38.7"; spf=pass smtp.mailfrom="openvpn-devel-bounces@lists.sourceforge.net" smtp.helo="lists.sourceforge.net"; dkim=fail (signature verification failed) header.d=sourceforge.net; dkim=fail (signature verification failed) header.d=sf.net; dkim=fail (signature verification failed) header.d=technolution.eu; dmarc=none (p=nil; dis=none) header.from=technolution.eu X-Suspicious-Flag: YES X-Classification-ID: bc2029ee-6dc0-11ea-998c-52540086a678-1-1 Received: from [216.105.38.7] ([216.105.38.7:43996] helo=lists.sourceforge.net) by smtp25.gate.iad3a.rsapps.net (envelope-from ) (ecelerity 4.2.38.62370 r(:)) with ESMTPS (cipher=DHE-RSA-AES256-GCM-SHA384) id 4D/4D-00825-3CBE97E5; Tue, 24 Mar 2020 07:15:15 -0400 Received: from [127.0.0.1] (helo=sfs-ml-2.v29.lw.sourceforge.com) by sfs-ml-2.v29.lw.sourceforge.com with esmtp (Exim 4.90_1) (envelope-from ) id 1jGhVu-0004nf-4R; Tue, 24 Mar 2020 11:14:30 +0000 Received: from [172.30.20.202] (helo=mx.sourceforge.net) by sfs-ml-2.v29.lw.sourceforge.com with esmtps (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jGhVq-0004nV-S3 for openvpn-devel@lists.sourceforge.net; Tue, 24 Mar 2020 11:14:27 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sourceforge.net; s=x; h=Content-Transfer-Encoding:Content-Type:MIME-Version :Date:Message-ID:To:Subject:From:Sender:Reply-To:Cc:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:In-Reply-To:References:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=02rfsRTXbJ2OPMiNhcgGcO76GFum7aBmUeTEB94DtoI=; b=aZJzeSrIx6WfDZl7qEjFxDcxpW FfKwNqbEfZjPgOYwGYdiH5cG1YfDnvWqCA3oS6fMjx2nDsQ/VFhikKPUGMsO6/MX4bc4ShD/lFDSq ++ewfTX/89oi+EznJpxPAJ52DzX8Ep3fQe6vfMPP4brqckA2hZiDdlL6l/wqzU2c/qOM=; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sf.net; s=x ; h=Content-Transfer-Encoding:Content-Type:MIME-Version:Date:Message-ID:To: Subject:From:Sender:Reply-To:Cc:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To: References:List-Id:List-Help:List-Unsubscribe:List-Subscribe:List-Post: List-Owner:List-Archive; bh=02rfsRTXbJ2OPMiNhcgGcO76GFum7aBmUeTEB94DtoI=; b=D sSHwreLEXHohLG9fPJlohBPO0AyeNpAsY9inNOh/3geAjbeUHWIBDZAPmeruv1YJwFlovmlmdQAwe e6Bw7EAXHnrGlMP/VpMzgW4RHBubEm3xhtkfno6qqLLHOEgmDOfKNCWZpLdfbjePMwtFkz1Zx1jAx VRU1elO57sDQNmmU=; Received: from mail-wm1-f67.google.com ([209.85.128.67]) by sfi-mx-1.v28.lw.sourceforge.com with esmtps (TLSv1.2:ECDHE-RSA-AES128-GCM-SHA256:128) (Exim 4.92.2) id 1jGhVh-00F6W2-5z for openvpn-devel@lists.sourceforge.net; Tue, 24 Mar 2020 11:14:26 +0000 Received: by mail-wm1-f67.google.com with SMTP id g62so2996676wme.1 for ; Tue, 24 Mar 2020 04:14:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=technolution.eu; s=google; h=from:subject:to:message-id:date:user-agent:mime-version :content-transfer-encoding:content-language; bh=02rfsRTXbJ2OPMiNhcgGcO76GFum7aBmUeTEB94DtoI=; b=E7DwwJGfeuB+DQndqCczLKPdoVBzpAp7+16FgmEjZzrHqSWBjZy6yFAn8yMwtD3T4P 2d6sAARL/dqF9iJuhJMUeOONzbtcrpQfsFY1vysPiIRShtS0Z2huEjlHXJLm5XcBEaNW iaGx0SvIb+opurip2S+zwOb3kUzqzW0JD/9WU= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:subject:to:message-id:date:user-agent :mime-version:content-transfer-encoding:content-language; bh=02rfsRTXbJ2OPMiNhcgGcO76GFum7aBmUeTEB94DtoI=; b=ciL/eJ0EtmX4rL/ZLsRK/YzOwrpyu9LIcG9gOSYs6ccI/H6YanuJNy+zPrnNTR5Sux 9JRu9Z7VMpjklmGF2xC2pYjuAAWDOdfvabZLnaNQ0VnFxoFT+JYcTROFO8duYPfSr9hx 8KH7HfpT6JrCsl3YuXEiKx6zx8khCt6YvweCrVow2FCZsNHLGnAPtSbVnyRpYYXyNe8E EceurgHZH9wg0n+uN5WvS6aBaeSQkR0mXczJIQgG+c4FlOjtQ+O45NsTgilAySkXJl69 E9im/XjWlWpb5hykbzlEYRdqwtcsBNyi/MY6Rn0Boj92dVWypoe+cDS8w2iNpOV6cqdV kNPg== X-Gm-Message-State: ANhLgQ3uXs94xnuiSiRJO2MuihQrMO8hwH104xQUjG5wFqGNk9lWLZLs eWML5+GHewFSnX5fsYrrVz6SHq30wchRbxDTuGi307RE/76iwCs8VKKlORLy3QKMf4+hfem9i3l Pk2VbpKWC9WcEt/kdSS2RRUqmdh7la5d+ X-Google-Smtp-Source: ADFU+vsA3eJ+31MiBls+qeXBiXUr80zZmMr0izTockLNX9BYHjYcPdI2YdFiLFq5gZOq3CIEruM24w== X-Received: by 2002:a1c:4d7:: with SMTP id 206mr4718243wme.5.1585046524548; Tue, 24 Mar 2020 03:42:04 -0700 (PDT) Received: from [172.16.110.145] (90-145-31-194.bbserv.nl. [90.145.31.194]) by smtp.gmail.com with ESMTPSA id j124sm3736610wmb.25.2020.03.24.03.42.03 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 24 Mar 2020 03:42:03 -0700 (PDT) From: Tom van Leeuwen To: openvpn-devel@lists.sourceforge.net Message-ID: Date: Tue, 24 Mar 2020 11:42:02 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.4.1 MIME-Version: 1.0 Content-Language: en-US X-techno-validated: techno-validated X-Spam-Report: Spam Filtering performed by mx.sourceforge.net. See http://spamassassin.org/tag/ for more details. -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at https://www.dnswl.org/, no trust [209.85.128.67 listed in list.dnswl.org] -0.0 RCVD_IN_MSPIKE_H2 RBL: Average reputation (+2) [209.85.128.67 listed in wl.mailspike.net] 0.2 HEADER_FROM_DIFFERENT_DOMAINS From and EnvelopeFrom 2nd level mail domains are different 0.0 SPF_NONE SPF: sender does not publish an SPF Record 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid X-Headers-End: 1jGhVh-00F6W2-5z Subject: [Openvpn-devel] [PATCH] mbedTLS: Make sure TLS session survives move X-BeenThere: openvpn-devel@lists.sourceforge.net X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: openvpn-devel-bounces@lists.sourceforge.net X-getmail-retrieved-from-mailbox: Inbox When an mbedTLS session is moved in move_session(), the contents of the the tls_session is copied to the new session and the old session is reinitialized. This tls_session contains, amongst other things, an mbedtls_ssl_config and bio_ctx structure. However, the mbedtls context has internal pointers to the mbedtls_ssl_config and bio_ctx. When the session is moved, these internal pointers point to the reinitialized session. Since there is no public method to update these internal pointers, this patch allocates the mbedtls_ssl_config and bio_ctx on the heap and stores the pointers in the tls_session instead. diff --git a/src/openvpn/ssl_mbedtls.c b/src/openvpn/ssl_mbedtls.c index 0f0b035b..4f194ad7 100644 --- a/src/openvpn/ssl_mbedtls.c +++ b/src/openvpn/ssl_mbedtls.c @@ -1036,21 +1036,22 @@ key_state_ssl_init(struct key_state_ssl *ks_ssl,      CLEAR(*ks_ssl);        /* Initialise SSL config */ -    mbedtls_ssl_config_init(&ks_ssl->ssl_config); -    mbedtls_ssl_config_defaults(&ks_ssl->ssl_config, ssl_ctx->endpoint, +    ALLOC_OBJ_CLEAR(ks_ssl->ssl_config, mbedtls_ssl_config); +    mbedtls_ssl_config_init(ks_ssl->ssl_config); +    mbedtls_ssl_config_defaults(ks_ssl->ssl_config, ssl_ctx->endpoint,                                  MBEDTLS_SSL_TRANSPORT_STREAM, MBEDTLS_SSL_PRESET_DEFAULT);  #ifdef MBEDTLS_DEBUG_C      mbedtls_debug_set_threshold(3);  #endif -    mbedtls_ssl_conf_dbg(&ks_ssl->ssl_config, my_debug, NULL); -    mbedtls_ssl_conf_rng(&ks_ssl->ssl_config, mbedtls_ctr_drbg_random, +    mbedtls_ssl_conf_dbg(ks_ssl->ssl_config, my_debug, NULL); +    mbedtls_ssl_conf_rng(ks_ssl->ssl_config, mbedtls_ctr_drbg_random,                           rand_ctx_get());   -    mbedtls_ssl_conf_cert_profile(&ks_ssl->ssl_config, &ssl_ctx->cert_profile); +    mbedtls_ssl_conf_cert_profile(ks_ssl->ssl_config, &ssl_ctx->cert_profile);        if (ssl_ctx->allowed_ciphers)      { -        mbedtls_ssl_conf_ciphersuites(&ks_ssl->ssl_config, ssl_ctx->allowed_ciphers); +        mbedtls_ssl_conf_ciphersuites(ks_ssl->ssl_config, ssl_ctx->allowed_ciphers);      }        /* Disable record splitting (for now).  OpenVPN assumes records are sent @@ -1058,35 +1059,35 @@ key_state_ssl_init(struct key_state_ssl *ks_ssl,       * testing.  Since OpenVPN is not susceptible to BEAST, we can just       * disable record splitting as a quick fix. */  #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING) -    mbedtls_ssl_conf_cbc_record_splitting(&ks_ssl->ssl_config, +    mbedtls_ssl_conf_cbc_record_splitting(ks_ssl->ssl_config,                                            MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED);  #endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */        /* Initialise authentication information */      if (is_server)      { -        mbed_ok(mbedtls_ssl_conf_dh_param_ctx(&ks_ssl->ssl_config, +        mbed_ok(mbedtls_ssl_conf_dh_param_ctx(ks_ssl->ssl_config,                                                ssl_ctx->dhm_ctx));      }   -    mbed_ok(mbedtls_ssl_conf_own_cert(&ks_ssl->ssl_config, ssl_ctx->crt_chain, +    mbed_ok(mbedtls_ssl_conf_own_cert(ks_ssl->ssl_config, ssl_ctx->crt_chain,                                        ssl_ctx->priv_key));        /* Initialise SSL verification */  #if P2MP_SERVER      if (session->opt->ssl_flags & SSLF_CLIENT_CERT_OPTIONAL)      { -        mbedtls_ssl_conf_authmode(&ks_ssl->ssl_config, MBEDTLS_SSL_VERIFY_OPTIONAL); +        mbedtls_ssl_conf_authmode(ks_ssl->ssl_config, MBEDTLS_SSL_VERIFY_OPTIONAL);      }      else if (!(session->opt->ssl_flags & SSLF_CLIENT_CERT_NOT_REQUIRED))  #endif      { -        mbedtls_ssl_conf_authmode(&ks_ssl->ssl_config, MBEDTLS_SSL_VERIFY_REQUIRED); +        mbedtls_ssl_conf_authmode(ks_ssl->ssl_config, MBEDTLS_SSL_VERIFY_REQUIRED);      } -    mbedtls_ssl_conf_verify(&ks_ssl->ssl_config, verify_callback, session); +    mbedtls_ssl_conf_verify(ks_ssl->ssl_config, verify_callback, session);        /* TODO: mbed TLS does not currently support sending the CA chain to the client */ -    mbedtls_ssl_conf_ca_chain(&ks_ssl->ssl_config, ssl_ctx->ca_chain, ssl_ctx->crl); +    mbedtls_ssl_conf_ca_chain(ks_ssl->ssl_config, ssl_ctx->ca_chain, ssl_ctx->crl);        /* Initialize minimum TLS version */      { @@ -1103,7 +1104,7 @@ key_state_ssl_init(struct key_state_ssl *ks_ssl,              tls_version_to_major_minor(tls_version_min, &major, &minor);          }   -        mbedtls_ssl_conf_min_version(&ks_ssl->ssl_config, major, minor); +        mbedtls_ssl_conf_min_version(ks_ssl->ssl_config, major, minor);      }        /* Initialize maximum TLS version */ @@ -1116,7 +1117,7 @@ key_state_ssl_init(struct key_state_ssl *ks_ssl,          {              int major, minor;              tls_version_to_major_minor(tls_version_max, &major, &minor); -            mbedtls_ssl_conf_max_version(&ks_ssl->ssl_config, major, minor); +            mbedtls_ssl_conf_max_version(ks_ssl->ssl_config, major, minor);          }      }   @@ -1124,7 +1125,7 @@ key_state_ssl_init(struct key_state_ssl *ks_ssl,      /* Initialize keying material exporter */      if (session->opt->ekm_size)      { -        mbedtls_ssl_conf_export_keys_ext_cb(&ks_ssl->ssl_config, +        mbedtls_ssl_conf_export_keys_ext_cb(ks_ssl->ssl_config,                  mbedtls_ssl_export_keys_cb, session);      }  #endif @@ -1132,11 +1133,11 @@ key_state_ssl_init(struct key_state_ssl *ks_ssl,      /* Initialise SSL context */      ALLOC_OBJ_CLEAR(ks_ssl->ctx, mbedtls_ssl_context);      mbedtls_ssl_init(ks_ssl->ctx); -    mbedtls_ssl_setup(ks_ssl->ctx, &ks_ssl->ssl_config); +    mbedtls_ssl_setup(ks_ssl->ctx, ks_ssl->ssl_config);        /* Initialise BIOs */ -    CLEAR(ks_ssl->bio_ctx); -    mbedtls_ssl_set_bio(ks_ssl->ctx, &ks_ssl->bio_ctx, ssl_bio_write, +    ALLOC_OBJ_CLEAR(ks_ssl->bio_ctx, bio_ctx); +    mbedtls_ssl_set_bio(ks_ssl->ctx, ks_ssl->bio_ctx, ssl_bio_write,                          ssl_bio_read, NULL);  }   @@ -1152,9 +1153,17 @@ key_state_ssl_free(struct key_state_ssl *ks_ssl)              mbedtls_ssl_free(ks_ssl->ctx);              free(ks_ssl->ctx);          } -        mbedtls_ssl_config_free(&ks_ssl->ssl_config); -        buf_free_entries(&ks_ssl->bio_ctx.in); -        buf_free_entries(&ks_ssl->bio_ctx.out); +        if (ks_ssl->ssl_config) +        { +            mbedtls_ssl_config_free(ks_ssl->ssl_config); +            free(ks_ssl->ssl_config); +        } +        if (ks_ssl->bio_ctx) +        { +            buf_free_entries(&ks_ssl->bio_ctx->in); +            buf_free_entries(&ks_ssl->bio_ctx->out); +            free(ks_ssl->bio_ctx); +        }          CLEAR(*ks_ssl);      }  } @@ -1249,7 +1258,7 @@ key_state_read_ciphertext(struct key_state_ssl *ks, struct buffer *buf,          len = maxlen;      }   -    retval = endless_buf_read(&ks->bio_ctx.out, BPTR(buf), len); +    retval = endless_buf_read(&ks->bio_ctx->out, BPTR(buf), len);        /* Error during read, check for retry error */      if (retval < 0) @@ -1294,7 +1303,7 @@ key_state_write_ciphertext(struct key_state_ssl *ks, struct buffer *buf)          return 0;      }   -    retval = endless_buf_write(&ks->bio_ctx.in, BPTR(buf), buf->len); +    retval = endless_buf_write(&ks->bio_ctx->in, BPTR(buf), buf->len);        if (retval < 0)      { diff --git a/src/openvpn/ssl_mbedtls.h b/src/openvpn/ssl_mbedtls.h index c1c676dc..92381f1a 100644 --- a/src/openvpn/ssl_mbedtls.h +++ b/src/openvpn/ssl_mbedtls.h @@ -109,9 +109,9 @@ struct tls_root_ctx {  };    struct key_state_ssl { -    mbedtls_ssl_config ssl_config;      /**< mbedTLS global ssl config */ +    mbedtls_ssl_config *ssl_config;     /**< mbedTLS global ssl config */      mbedtls_ssl_context *ctx;           /**< mbedTLS connection context */ -    bio_ctx bio_ctx; +    bio_ctx *bio_ctx;        /** Keying material exporter cache (RFC 5705). */      uint8_t *exported_key_material;