From patchwork Sun Jan 14 08:44:32 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Selva Nair X-Patchwork-Id: 186 Return-Path: Delivered-To: patchwork@openvpn.net Delivered-To: patchwork@openvpn.net Received: from director6.mail.ord1d.rsapps.net ([172.28.255.1]) by backend31.mail.ord1d.rsapps.net (Dovecot) with LMTP id 203QMWmzW1pnNwAAgoeIoA for ; Sun, 14 Jan 2018 14:45:45 -0500 Received: from director4.mail.ord1c.rsapps.net ([172.28.255.1]) by director6.mail.ord1d.rsapps.net (Dovecot) with LMTP id I3myMWmzW1qmagAAhgvE6Q ; Sun, 14 Jan 2018 14:45:45 -0500 Received: from smtp24.gate.ord1c ([172.28.255.1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) by director4.mail.ord1c.rsapps.net (Dovecot) with LMTP id Wix0HmqzW1p6CQAAsEL7Xg ; Sun, 14 Jan 2018 14:45:46 -0500 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.34.181.88] Authentication-Results: smtp24.gate.ord1c.rsapps.net; iprev=pass policy.iprev="216.34.181.88"; 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=gmail.com; dmarc=fail (p=none; dis=none) header.from=gmail.com X-Classification-ID: 83113842-f963-11e7-9d47-0026b94accf9-1-1 Received: from [216.34.181.88] ([216.34.181.88:9298] helo=lists.sourceforge.net) by smtp24.gate.ord1c.rsapps.net (envelope-from ) (ecelerity 4.2.1.56364 r(Core:4.2.1.14)) with ESMTPS (cipher=DHE-RSA-AES256-GCM-SHA384) id 73/97-26998-963BB5A5; Sun, 14 Jan 2018 14:45:45 -0500 Received: from localhost ([127.0.0.1] helo=sfs-ml-4.v29.ch3.sourceforge.com) by sfs-ml-4.v29.ch3.sourceforge.com with esmtp (Exim 4.89) (envelope-from ) id 1eaoD2-0006U2-6z; Sun, 14 Jan 2018 19:44:48 +0000 Received: from sfi-mx-3.v28.ch3.sourceforge.com ([172.29.28.193] helo=mx.sourceforge.net) by sfs-ml-4.v29.ch3.sourceforge.com with esmtps (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.89) (envelope-from ) id 1eaoD1-0006Tw-So for openvpn-devel@lists.sourceforge.net; Sun, 14 Jan 2018 19:44:47 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sourceforge.net; s=x; h=References:In-Reply-To:Message-Id:Date:Subject:Cc: To:From:Sender:Reply-To:MIME-Version:Content-Type:Content-Transfer-Encoding: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=0hLC2P0zTNaa+q1rC4hFWBiyQk/Z5ya77yxTxTgrl5M=; b=nStG1aT0IlKyGy2o9hN0h0piRK WcNz1A0eAoyCMbgohHVRpQHbpKIthCabviTnaKi6UxMAdTqq2Y1xKe6JSf9MICXOo4wjFgHgjjZ6C TXRQzn5fn1RMCOGQT2adBwB6LpALV3607XUzSij9fkGAJL46UxgV2N2i0vFbQtMrsi1k=; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sf.net; s=x ; h=References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To :MIME-Version:Content-Type:Content-Transfer-Encoding:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=0hLC2P0zTNaa+q1rC4hFWBiyQk/Z5ya77yxTxTgrl5M=; b=B9RUmnd4KI/GJuEpP1/QT8S/mC PWqAggm8ibN2ZnI5lQ+tzz0aa6jMWJuVRMzo9XJcMxVLsUFzxDOoTmhvgDjxwD7f1vOBgeCn+FM3V zVv4pttekENWF3qtFqYITYePftvWhsquHf/NvRNPZum1ne0RyM5XoadfZk6t0HYwoSjM=; Received: from mail-it0-f65.google.com ([209.85.214.65]) by sfi-mx-3.v28.ch3.sourceforge.com with esmtps (TLSv1.2:ECDHE-RSA-AES128-GCM-SHA256:128) (Exim 4.89) id 1eaoD0-0001Fn-Ld for openvpn-devel@lists.sourceforge.net; Sun, 14 Jan 2018 19:44:47 +0000 Received: by mail-it0-f65.google.com with SMTP id c16so14472772itc.5 for ; Sun, 14 Jan 2018 11:44:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=0hLC2P0zTNaa+q1rC4hFWBiyQk/Z5ya77yxTxTgrl5M=; b=ho1wroNG+XcVaXeb0r6auEblu1BVkJ3B7rDyp9OcTtoq6mKcti8GY+1MQpdQkBYiFJ Bl6GeRPV7QmUx5f0DJ7cvqSJbcR2pzC3mehl8tawkQjNOXPFgIZqOI4jyDXFYHgQvkM+ QXNBmajqcOU9ypDeG09w6imn/8YhqZrY6wHlE6Q2ssT2/CQ3D6WQglKJRYaWhLfcg754 M8aV9UX2VI4SzwMrtBEHc0f/1dcMoYlsUfkLCU04ojGx5/G6kulIIGKX4aZ69DOKvlPw FH6otU6+ORA1+WEiX4o1STUTphw+aoW2hEn/jtlJR3L6Ym9SPBrfytzXbb4NWc6gnwes tnWg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=0hLC2P0zTNaa+q1rC4hFWBiyQk/Z5ya77yxTxTgrl5M=; b=C7hhZWCn2mcPwiiUe1DIRbNOkkXKbogNdJGD0orF5OW/fk8TYcPq1HwEZAlCpVnk9O SuyOWfZp6/w4oStAm3SBX37wqfjZiyKjvg5xTMrydutSnXvMRv7iHp8PkS2tDgytR4Fa zG3upTRkwgJA7Cr1bkJlsMCAZuvrD5hJyjpfUH++2OCLNzmooDLejtKnRFBf3SgW1Rz/ 613OwJImYvhd1ytIhhbfp3Hdewn7VQrHaUVG8fELtNGlfvrnLYAIRiRoPDQub1wFW+32 r6ZuKUhosG0BZyslULpRUr86sJQUdtZxTiW91tMVYWOEK2FfiTWvrNB9G9XhENdGhw1y 8IkQ== X-Gm-Message-State: AKwxytfJfAd1RvquNF8+pbUeqI6ZL0Dh9+EeaDAV5FSbUyjxITlABqPk faYKlY82w2rfNHLdZ9FGMxPgoS6x X-Google-Smtp-Source: ACJfBos0gZ/s4OkqFxU/ZnCRakRV1E2Me4Wurzlw6DgrzXCS9/al1YRFTYpy5U4ReJre+PYKN10YzQ== X-Received: by 10.36.151.6 with SMTP id k6mr12032672ite.94.1515959081110; Sun, 14 Jan 2018 11:44:41 -0800 (PST) Received: from saturn.home.sansel.ca (CPE40167ea0e1c2-CM788df74daaa0.cpe.net.cable.rogers.com. [99.228.215.92]) by smtp.gmail.com with ESMTPSA id y64sm15244286ioy.25.2018.01.14.11.44.40 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sun, 14 Jan 2018 11:44:40 -0800 (PST) From: selva.nair@gmail.com To: openvpn-devel@lists.sourceforge.net Date: Sun, 14 Jan 2018 14:44:32 -0500 Message-Id: <1515959073-10376-3-git-send-email-selva.nair@gmail.com> X-Mailer: git-send-email 2.1.4 In-Reply-To: <1515959073-10376-1-git-send-email-selva.nair@gmail.com> References: <1515959073-10376-1-git-send-email-selva.nair@gmail.com> X-Spam-Report: Spam Filtering performed by mx.sourceforge.net. See http://spamassassin.org/tag/ for more details. 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail provider (selva.nair[at]gmail.com) -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at http://www.dnswl.org/, no trust [209.85.214.65 listed in list.dnswl.org] -0.0 SPF_PASS SPF: sender matches SPF record -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature X-Headers-End: 1eaoD0-0001Fn-Ld Subject: [Openvpn-devel] [PATCH 2/3] Allow external EC key through --management-external-key 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: , MIME-Version: 1.0 Errors-To: openvpn-devel-bounces@lists.sourceforge.net X-getmail-retrieved-from-mailbox: Inbox From: Selva Nair - This automatically supports EC certificates through --management-external-cert - EC signature request from management has the same format as for rsa with >RSA_SIGN replaced by >ECDSA_SIGN Response should be of the form 'ecdsa-sig' followed by DER encoded signature as base64 followed by 'END' Signed-off-by: Selva Nair Tested-By: Arne Schwabe --- src/openvpn/manage.c | 30 ++++++++ src/openvpn/manage.h | 3 + src/openvpn/ssl_openssl.c | 169 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 202 insertions(+) diff --git a/src/openvpn/manage.c b/src/openvpn/manage.c index 55b106c..0152dee 100644 --- a/src/openvpn/manage.c +++ b/src/openvpn/manage.c @@ -113,6 +113,8 @@ man_help(void) #ifdef MANAGMENT_EXTERNAL_KEY msg(M_CLIENT, "rsa-sig : Enter an RSA signature in response to >RSA_SIGN challenge"); msg(M_CLIENT, " Enter signature base64 on subsequent lines followed by END"); + msg(M_CLIENT, "ecdsa-sig : Enter an ECDSA signature in response to >ECDSA_SIGN challenge"); + msg(M_CLIENT, " Enter signature base64 on subsequent lines followed by END"); msg(M_CLIENT, "certificate : Enter a client certificate in response to >NEED-CERT challenge"); msg(M_CLIENT, " Enter certificate base64 on subsequent lines followed by END"); #endif @@ -936,6 +938,7 @@ in_extra_dispatch(struct management *man) #endif /* ifdef MANAGEMENT_PF */ #ifdef MANAGMENT_EXTERNAL_KEY case IEC_RSA_SIGN: + case IEC_ECDSA_SIGN: man->connection.ext_key_state = EKS_READY; buffer_list_free(man->connection.ext_key_input); man->connection.ext_key_input = man->connection.in_extra; @@ -1119,6 +1122,22 @@ man_rsa_sig(struct management *man) } static void +man_ecdsa_sig(struct management *man) +{ + struct man_connection *mc = &man->connection; + if (mc->ext_key_state == EKS_SOLICIT) + { + mc->ext_key_state = EKS_INPUT; + mc->in_extra_cmd = IEC_ECDSA_SIGN; + in_extra_reset(mc, IER_NEW); + } + else + { + msg(M_CLIENT, "ERROR: The ecdsa-sig command is not currently available"); + } +} + +static void man_certificate(struct management *man) { struct man_connection *mc = &man->connection; @@ -1516,6 +1535,10 @@ man_dispatch_command(struct management *man, struct status_output *so, const cha { man_rsa_sig(man); } + else if (streq(p[0], "ecdsa-sig")) + { + man_ecdsa_sig(man); + } else if (streq(p[0], "certificate")) { man_certificate(man); @@ -3655,6 +3678,13 @@ management_query_rsa_sig(struct management *man, &man->connection.ext_key_state, &man->connection.ext_key_input); } +char * +management_query_ecdsa_sig(struct management *man, + const char *b64_data) +{ + return management_query_multiline_flatten(man, b64_data, "ECDSA_SIGN", "ecdsa-sign", + &man->connection.ext_key_state, &man->connection.ext_key_input); +} char * management_query_cert(struct management *man, const char *cert_name) diff --git a/src/openvpn/manage.h b/src/openvpn/manage.h index 676be64..0b1ae5b 100644 --- a/src/openvpn/manage.h +++ b/src/openvpn/manage.h @@ -281,6 +281,7 @@ struct man_connection { #define IEC_CLIENT_PF 2 #define IEC_RSA_SIGN 3 #define IEC_CERTIFICATE 4 +#define IEC_ECDSA_SIGN 5 int in_extra_cmd; struct buffer_list *in_extra; #ifdef MANAGEMENT_DEF_AUTH @@ -441,6 +442,8 @@ void management_learn_addr(struct management *management, char *management_query_rsa_sig(struct management *man, const char *b64_data); +char *management_query_ecdsa_sig(struct management *man, const char *b64_data); + char *management_query_cert(struct management *man, const char *cert_name); #endif diff --git a/src/openvpn/ssl_openssl.c b/src/openvpn/ssl_openssl.c index c29dbcf..9379784 100644 --- a/src/openvpn/ssl_openssl.c +++ b/src/openvpn/ssl_openssl.c @@ -1132,6 +1132,160 @@ err: return 0; } +#if OPENSSL_VERSION_NUMBER >= 0x10100001L && !defined(OPENSSL_NO_EC) + +/* called when EC_KEY is destroyed */ +static void +openvpn_extkey_ec_finish(EC_KEY *ec) +{ + /* release the method structure */ + const EC_KEY_METHOD *ec_meth = EC_KEY_get_method(ec); + EC_KEY_METHOD_free((EC_KEY_METHOD *) ec_meth); +} + +/* EC_KEY_METHOD callback: sign(). + * Sign the hash using EC key and return DER encoded signature in sig, + * its length in siglen. Return value is 1 on success, 0 on error. + */ +static int +ecdsa_sign(int type, const unsigned char *dgst, int dgstlen, unsigned char *sig, + unsigned int *siglen, const BIGNUM *kinv, const BIGNUM *r, EC_KEY *ec) +{ + char *in_b64 = NULL; + char *out_b64 = NULL; + int ret = 0; + int len; + + /* convert 'from' to base64 */ + if (openvpn_base64_encode(dgst, dgstlen, &in_b64) <= 0) + { + goto done; + } + + /* call MI for signature */ + if (management) + { + out_b64 = management_query_ecdsa_sig(management, in_b64); + } + if (!out_b64) + { + goto done; + } + + /* decode base64 signature to binary */ + len = ECDSA_size(ec); + *siglen = openvpn_base64_decode(out_b64, sig, len); + if (*siglen > 0) + { + ret = 1; + } + +done: + if (in_b64) + { + free(in_b64); + } + if (out_b64) + { + free(out_b64); + } + return ret; +} + +/* EC_KEY_METHOD callback: sign_setup(). We do no precomputations */ +static int +ecdsa_sign_setup(EC_KEY *ec, BN_CTX *ctx_in, BIGNUM **kinvp, BIGNUM **rp) +{ + return 1; +} + +/* EC_KEY_METHOD callback: sign_sig(). + * Sign the hash and return the result as a newly allocated ECDS_SIG + * struct or NULL on error. + */ +static ECDSA_SIG * +ecdsa_sign_sig(const unsigned char *dgst, int dgstlen, const BIGNUM *in_kinv, + const BIGNUM *in_r, EC_KEY *ec) +{ + ECDSA_SIG *ecsig = NULL; + int len = ECDSA_size(ec); + struct gc_arena gc = gc_new(); + + unsigned char *buf = gc_malloc(len, false, &gc); + if (1 != ecdsa_sign(0, dgst, dgstlen, buf, &len, NULL, NULL, ec)) + { + goto out; + } + /* const char ** should be avoided: not up to us, so we cast our way through */ + ecsig = d2i_ECDSA_SIG(NULL, (const unsigned char **)&buf, len); + +out: + gc_free(&gc); + return ecsig; +} + +static int +tls_ctx_use_external_ec_key(struct tls_root_ctx *ctx, EVP_PKEY *pkey) +{ + EC_KEY *ec = NULL; + EVP_PKEY *privkey = NULL; + EC_KEY_METHOD *ec_method; + + ASSERT(ctx); + + ec_method = EC_KEY_METHOD_new(EC_KEY_OpenSSL()); + if (!ec_method) + { + goto err; + } + + /* Among init methods, we only need the finish method */ + EC_KEY_METHOD_set_init(ec_method, NULL, openvpn_extkey_ec_finish, NULL, NULL, NULL, NULL); + EC_KEY_METHOD_set_sign(ec_method, ecdsa_sign, ecdsa_sign_setup, ecdsa_sign_sig); + + ec = EC_KEY_dup(EVP_PKEY_get0_EC_KEY(pkey)); + if (!ec) + { + EC_KEY_METHOD_free(ec_method); + goto err; + } + if (!EC_KEY_set_method(ec, ec_method)) + { + EC_KEY_METHOD_free(ec_method); + goto err; + } + /* from this point ec_method will get freed when ec is freed */ + + privkey = EVP_PKEY_new(); + if (!EVP_PKEY_assign_EC_KEY(privkey, ec)) + { + goto err; + } + /* from this point ec will get freed when privkey is freed */ + + if (!SSL_CTX_use_PrivateKey(ctx->ctx, privkey)) + { + ec = NULL; /* avoid double freeing it below */ + goto err; + } + + EVP_PKEY_free(privkey); /* this will down ref privkey and ec */ + return 1; + +err: + /* Reach here only when ec and privkey can be independenly freed */ + if (privkey) + { + EVP_PKEY_free(privkey); + } + if(ec) + { + EC_KEY_free(ec); + } + return 0; +} +#endif // OPENSSL_VERSION_NUMBER > 1.1.0 dev + int tls_ctx_use_external_private_key(struct tls_root_ctx *ctx, const char *cert_file, const char *cert_file_inline) @@ -1156,11 +1310,26 @@ tls_ctx_use_external_private_key(struct tls_root_ctx *ctx, goto err; } } +#if OPENSSL_VERSION_NUMBER >= 0x10100001L && !defined(OPENSSL_NO_EC) + else if (EVP_PKEY_get0_EC_KEY(pkey)) + { + if (!tls_ctx_use_external_ec_key(ctx, pkey)) + { + goto err; + } + } + else + { + crypto_msg(M_WARN, "management-external-key requires an RSA or EC certificate"); + goto err; + } +#else else { crypto_msg(M_WARN, "management-external-key requires a RSA certificate"); goto err; } +#endif return 1; err: