From patchwork Fri Feb 23 07:10:39 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Selva Nair X-Patchwork-Id: 245 Return-Path: Delivered-To: patchwork@openvpn.net Delivered-To: patchwork@openvpn.net Received: from director10.mail.ord1d.rsapps.net ([172.30.191.6]) by backend30.mail.ord1d.rsapps.net (Dovecot) with LMTP id 08oGBJRZkFpRBwAAIUCqbw for ; Fri, 23 Feb 2018 13:12:36 -0500 Received: from proxy11.mail.ord1d.rsapps.net ([172.30.191.6]) by director10.mail.ord1d.rsapps.net (Dovecot) with LMTP id +4yuA5RZkFoxUwAApN4f7A ; Fri, 23 Feb 2018 13:12:36 -0500 Received: from smtp7.gate.ord1c ([172.30.191.6]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) by proxy11.mail.ord1d.rsapps.net (Dovecot) with LMTP id qGx+A5RZkFrWagAAgKDEHA ; Fri, 23 Feb 2018 13:12:36 -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.105.38.7] Authentication-Results: smtp7.gate.ord1c.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=gmail.com; dmarc=fail (p=none; dis=none) header.from=gmail.com X-Classification-ID: 1ef0b956-18c5-11e8-b925-bc305bf04148-1-1 Received: from [216.105.38.7] ([216.105.38.7:42590] helo=lists.sourceforge.net) by smtp7.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 FF/01-01794-299509A5; Fri, 23 Feb 2018 13:12:35 -0500 Received: from [127.0.0.1] (helo=sfs-ml-1.v29.lw.sourceforge.com) by sfs-ml-1.v29.lw.sourceforge.com with esmtp (Exim 4.89) (envelope-from ) id 1epHom-0002Gb-Da; Fri, 23 Feb 2018 18:11:36 +0000 Received: from [172.30.20.202] (helo=mx.sourceforge.net) by sfs-ml-1.v29.lw.sourceforge.com with esmtps (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.89) (envelope-from ) id 1epHoC-000268-Bp for openvpn-devel@lists.sourceforge.net; Fri, 23 Feb 2018 18:11:00 +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=8T3HP7YdxVeX6ixS+W7IJj8QyJOmzr+TdCBxYMdnx6M=; b=XRWgqw1bz+g6GCLInUAwUvL04F WPNU8hmZ2jFOLNctCruUFiaRx8ICAM43NLvzKljGGJ46e7ZE2buvP4y0fRplOu6PEkNn+ueJzNVFo 3eOB9Ua5f5OUa/GMkx+SPbPkZzzF0o1waZ5gYXoozaEAICeOrC1FLQMIew0orUiWKfGw=; 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=8T3HP7YdxVeX6ixS+W7IJj8QyJOmzr+TdCBxYMdnx6M=; b=ckQ/eUcr44yiI14Df3pYOYU27x tvm+DCdcSUIkBMOMl984SWYpJ06tX6zyDzjGNC6NYANrJbB7zuevcVH/hm7rkG2O0TaE83/U1ydFO WePYpffJ4mdqrq7xhnFRaH+cEwBtT6vJzxQkwXPniGGtAZ7VsI6Q7FSNV9YnKrd7I7lo=; Received: from sfi-lb-mx.v20.lw.sourceforge.com ([172.30.20.201] helo=mail-io0-f179.google.com) by sfi-mx-4.v28.lw.sourceforge.com with esmtps (TLSv1.2:ECDHE-RSA-AES128-GCM-SHA256:128) (Exim 4.89) id 1epHo9-00042p-9U for openvpn-devel@lists.sourceforge.net; Fri, 23 Feb 2018 18:10:59 +0000 Received: by mail-io0-f179.google.com with SMTP id q24so3381668ioh.8 for ; Fri, 23 Feb 2018 10:10:58 -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=8T3HP7YdxVeX6ixS+W7IJj8QyJOmzr+TdCBxYMdnx6M=; b=MIni9A+czRw1l+VGHlJzD5gYBJwiaOLkDspkNB1uN1fQAsgLSvzSaYdT9lijLP0Dee ADk3DFZMVH3kQ6LWXAAGxC09t3wzBiR6att43UKWQNlaq6gw24jelGF1IY+kEUTUYyxF 9eq8nppAonm1BkMG93T/aiJH/95SgAWmo3Uks5e234JVGvuFLC332dvp5Gd0FIfeBW0e iaggIop3Sm/jlfTCuCEpgnOJ+x6s8ca7Ta0pHNqf/7LwjM6+BVy8HYjbXI+KhXDulq/8 X3EPADdE0VN5V4OKYD3MQ9wCGuj8nk7KnLWVj0Z6ed2uibnX4ppZZEON8AFSPlsHaMnc v/tA== 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=8T3HP7YdxVeX6ixS+W7IJj8QyJOmzr+TdCBxYMdnx6M=; b=hbPMEnf1JyK9jeLakDJFy60oEAC64lQSxo80Qc/xxpOaVSqsgX5aRUuh9WINx2mM9Z POMewnsOh7b77ZYDhtaDxrgHjnPEKjlYpZ/wYyu+AEmeDh5RKt0pI315PZR/dXxwJ0Ex GNiS8hfCAQ5gYl3g5QMOM2BG8yT1GP7hAbzfQcwhNIx56pkyNLy3I8HObD2pkVXOEQ9a sY0AXMlJ69RFINJ0Bm4gVDGrGql+OXWMIWxeDbUza1npPSi6wQNPDWlpAkqyEwgasWWf F504Jw0Ot9x8fOHLS5BHmUgofoVy8Zq2t3QnPfimUHtE+LTDZZP0ezWcesQuU1SQdWSQ 7m3Q== X-Gm-Message-State: APf1xPAUXjX7/5cmc3Ij+N0nz4Cens+bgGgdyE9NsWxYparpqM48bOYo mMPY6TdvixFookEyAX8u9MhmzhQe X-Google-Smtp-Source: AG47ELtQImJ079oo/dBpQHWtwSayNK5+PjCPK0WdW+O9qDuN7J/9RBnJHADBHETiirlLmiB5UizPxw== X-Received: by 10.107.222.10 with SMTP id v10mr2868581iog.267.1519409452212; Fri, 23 Feb 2018 10:10:52 -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 m5sm1633923iti.2.2018.02.23.10.10.51 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 23 Feb 2018 10:10:51 -0800 (PST) From: selva.nair@gmail.com To: openvpn-devel@lists.sourceforge.net Date: Fri, 23 Feb 2018 13:10:39 -0500 Message-Id: <1519409439-8439-1-git-send-email-selva.nair@gmail.com> X-Mailer: git-send-email 2.1.4 In-Reply-To: <1516770381-29466-4-git-send-email-selva.nair@gmail.com> References: <1516770381-29466-4-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) 1.3 RCVD_IN_RP_RNBL RBL: Relay in RNBL, https://senderscore.org/blacklistlookup/ [99.228.215.92 listed in bl.score.senderscore.com] 0.0 URIBL_BLOCKED ADMINISTRATOR NOTICE: The query to URIBL was blocked. See http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more information. [URIs: runestig.com] 1.0 SPF_SOFTFAIL SPF: sender does not match SPF record (softfail) -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: 1epHo9-00042p-9U Subject: [Openvpn-devel] [PATCH v2 3/3] Support EC certificates with cryptoapicert 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 Requires openssl 1.1.0 or higher Signed-off-by: Selva Nair --- v3 of 2/3 changed the context of one chunk, so sending a v2 rebased to current master. src/openvpn/cryptoapi.c | 199 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 198 insertions(+), 1 deletion(-) diff --git a/src/openvpn/cryptoapi.c b/src/openvpn/cryptoapi.c index 1097286..995b463 100644 --- a/src/openvpn/cryptoapi.c +++ b/src/openvpn/cryptoapi.c @@ -1,5 +1,6 @@ /* * Copyright (c) 2004 Peter 'Luna' Runestig + * Copyright (c) 2018 Selva Nair * All rights reserved. * * Redistribution and use in source and binary forms, with or without modifi- @@ -101,6 +102,9 @@ static ERR_STRING_DATA CRYPTOAPI_str_functs[] = { { 0, NULL } }; +/* index for storing external data in EC_KEY: < 0 means uninitialized */ +static int ec_data_idx = -1; + typedef struct _CAPI_DATA { const CERT_CONTEXT *cert_context; HCRYPTPROV_OR_NCRYPT_KEY_HANDLE crypt_prov; @@ -394,6 +398,190 @@ finish(RSA *rsa) return 1; } +#if (OPENSSL_VERSION_NUMBER >= 0x10100000L) && !defined(OPENSSL_NO_EC) + +static EC_KEY_METHOD *ec_method = NULL; + +/** EC_KEY_METHOD callback: called when the key is freed */ +static void +ec_finish(EC_KEY *ec) +{ + EC_KEY_METHOD_free(ec_method); + ec_method = NULL; + CAPI_DATA *cd = EC_KEY_get_ex_data(ec, ec_data_idx); + CAPI_DATA_free(cd); + EC_KEY_set_ex_data(ec, ec_data_idx, NULL); +} + +/** EC_KEY_METHOD callback sign_setup(): we do nothing here */ +static int +ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp, BIGNUM **rp) +{ + return 1; +} + +/** + * Helper to convert ECDSA signature returned by NCryptSignHash + * to an ECDSA_SIG structure. + * On entry 'buf[]' of length len contains r and s contcatenated. + * Returns a newly allocated ECDSA_SIG or NULL (on error). + */ +static ECDSA_SIG * +ecdsa_bin2sig(unsigned char *buf, int len) +{ + ECDSA_SIG *ecsig = NULL; + DWORD rlen = len/2; + BIGNUM *r = BN_bin2bn(buf, rlen, NULL); + BIGNUM *s = BN_bin2bn(buf+rlen, rlen, NULL); + if (!r || !s) + { + goto err; + } + ecsig = ECDSA_SIG_new(); /* in openssl 1.1 this does not allocate r, s */ + if (!ecsig) + { + goto err; + } + ECDSA_SIG_set0(ecsig, r, s); /* ecsig takes ownership of r and s */ + return ecsig; +err: + BN_free(r); /* it is ok to free NULL BN */ + BN_free(s); + return NULL; +} + +/** EC_KEY_METHOD callback sign_sig(): sign and return an ECDSA_SIG pointer. */ +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; + CAPI_DATA *cd = (CAPI_DATA *)EC_KEY_get_ex_data(ec, ec_data_idx); + + ASSERT(cd->key_spec == CERT_NCRYPT_KEY_SPEC); + + NCRYPT_KEY_HANDLE hkey = cd->crypt_prov; + BYTE buf[512]; /* large enough buffer for signature to avoid malloc */ + DWORD len = _countof(buf); + + msg(D_LOW, "Signing hash using EC key: data size = %d", dgstlen); + + DWORD status = NCryptSignHash(hkey, NULL, (BYTE *)dgst, dgstlen, (BYTE *)buf, len, &len, 0); + if (status != ERROR_SUCCESS) + { + SetLastError(status); + CRYPTOAPIerr(CRYPTOAPI_F_NCRYPT_SIGN_HASH); + } + else + { + /* NCryptSignHash returns r, s concatenated in buf[] */ + ecsig = ecdsa_bin2sig(buf, len); + } + return ecsig; +} + +/** EC_KEY_METHOD callback sign(): sign and return a DER encoded signature */ +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) +{ + ECDSA_SIG *s; + + s = ecdsa_sign_sig(dgst, dgstlen, NULL, NULL, ec); + if (s == NULL) + { + *siglen = 0; + return 0; + } + + /* convert internal signature structure 's' to DER encoded byte array in sig */ + *siglen = i2d_ECDSA_SIG((ECDSA_SIG *)s, &sig); + ECDSA_SIG_free(s); + + return 1; +} + +static int +ssl_ctx_set_eckey(SSL_CTX *ssl_ctx, CAPI_DATA *cd, EVP_PKEY *pkey) +{ + EC_KEY *ec = NULL; + EVP_PKEY *privkey = NULL; + + if (cd->key_spec != CERT_NCRYPT_KEY_SPEC) + { + msg(M_NONFATAL, "ERROR: cryptoapicert with only legacy private key handle available." + " EC certificate not supported."); + goto err; + } + /* create a method struct with default callbacks filled in */ + ec_method = EC_KEY_METHOD_new(EC_KEY_OpenSSL()); + if (!ec_method) + { + goto err; + } + + /* We only need to set finish among init methods, and sign methods */ + EC_KEY_METHOD_set_init(ec_method, NULL, 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) + { + goto err; + } + if (!EC_KEY_set_method(ec, ec_method)) + { + goto err; + } + + /* get an index to store cd as external data */ + if (ec_data_idx < 0) + { + ec_data_idx = EC_KEY_get_ex_new_index(0, "cryptapicert ec key", NULL, NULL, NULL); + if (ec_data_idx < 0) + { + goto err; + } + } + EC_KEY_set_ex_data(ec, ec_data_idx, cd); + + /* cd assigned to ec as ex_data, increase its refcount */ + cd->ref_count++; + + privkey = EVP_PKEY_new(); + if (!EVP_PKEY_assign_EC_KEY(privkey, ec)) + { + EC_KEY_free(ec); + goto err; + } + /* from here on ec will get freed with privkey */ + + if (!SSL_CTX_use_PrivateKey(ssl_ctx, privkey)) + { + goto err; + } + EVP_PKEY_free(privkey); /* this will dn_ref or free ec as well */ + return 1; + +err: + if (privkey) + { + EVP_PKEY_free(privkey); + } + else if (ec) + { + EC_KEY_free(ec); + } + if (ec_method) /* do always set ec_method = NULL after freeing it */ + { + EC_KEY_METHOD_free(ec_method); + ec_method = NULL; + } + return 0; +} + +#endif /* OPENSSL_VERSION_NUMBER >= 1.1.0 */ + static const CERT_CONTEXT * find_certificate_in_store(const char *cert_prop, HCERTSTORE cert_store) { @@ -642,9 +830,18 @@ SSL_CTX_use_CryptoAPI_certificate(SSL_CTX *ssl_ctx, const char *cert_prop) goto err; } } +#if (OPENSSL_VERSION_NUMBER >= 0x10100000L) && !defined(OPENSSL_NO_EC) + else if (EVP_PKEY_id(pkey) == EVP_PKEY_EC) + { + if (!ssl_ctx_set_eckey(ssl_ctx, cd, pkey)) + { + goto err; + } + } +#endif /* OPENSSL_VERSION_NUMBER >= 1.1.0 */ else { - msg(M_WARN, "cryptoapicert requires an RSA certificate"); + msg(M_WARN, "WARNING: cryptoapicert: certificate type not supported"); goto err; } CAPI_DATA_free(cd); /* this will do a ref_count-- */