1296781Sdes/* $OpenBSD: kexecdhs.c,v 1.15 2015/12/04 16:41:28 markus Exp $ */ 2218767Sdes/* 3218767Sdes * Copyright (c) 2001 Markus Friedl. All rights reserved. 4218767Sdes * Copyright (c) 2010 Damien Miller. All rights reserved. 5218767Sdes * 6218767Sdes * Redistribution and use in source and binary forms, with or without 7218767Sdes * modification, are permitted provided that the following conditions 8218767Sdes * are met: 9218767Sdes * 1. Redistributions of source code must retain the above copyright 10218767Sdes * notice, this list of conditions and the following disclaimer. 11218767Sdes * 2. Redistributions in binary form must reproduce the above copyright 12218767Sdes * notice, this list of conditions and the following disclaimer in the 13218767Sdes * documentation and/or other materials provided with the distribution. 14218767Sdes * 15218767Sdes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16218767Sdes * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17218767Sdes * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18218767Sdes * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19218767Sdes * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20218767Sdes * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21218767Sdes * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22218767Sdes * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23218767Sdes * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24218767Sdes * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25218767Sdes */ 26218767Sdes 27218767Sdes#include "includes.h" 28218767Sdes 29295367Sdes#if defined(WITH_OPENSSL) && defined(OPENSSL_HAS_ECC) 30295367Sdes 31218767Sdes#include <sys/types.h> 32218767Sdes#include <string.h> 33218767Sdes#include <signal.h> 34218767Sdes 35295367Sdes#include <openssl/ecdh.h> 36295367Sdes 37295367Sdes#include "sshkey.h" 38218767Sdes#include "cipher.h" 39295367Sdes#include "digest.h" 40218767Sdes#include "kex.h" 41218767Sdes#include "log.h" 42218767Sdes#include "packet.h" 43218767Sdes#include "ssh2.h" 44218767Sdes 45295367Sdes#include "dispatch.h" 46295367Sdes#include "compat.h" 47295367Sdes#include "ssherr.h" 48295367Sdes#include "sshbuf.h" 49218767Sdes 50295367Sdesstatic int input_kex_ecdh_init(int, u_int32_t, void *); 51218767Sdes 52295367Sdesint 53295367Sdeskexecdh_server(struct ssh *ssh) 54218767Sdes{ 55295367Sdes debug("expecting SSH2_MSG_KEX_ECDH_INIT"); 56295367Sdes ssh_dispatch_set(ssh, SSH2_MSG_KEX_ECDH_INIT, &input_kex_ecdh_init); 57295367Sdes return 0; 58295367Sdes} 59295367Sdes 60295367Sdesstatic int 61295367Sdesinput_kex_ecdh_init(int type, u_int32_t seq, void *ctxt) 62295367Sdes{ 63295367Sdes struct ssh *ssh = ctxt; 64295367Sdes struct kex *kex = ssh->kex; 65218767Sdes EC_POINT *client_public; 66295367Sdes EC_KEY *server_key = NULL; 67218767Sdes const EC_GROUP *group; 68295367Sdes const EC_POINT *public_key; 69295367Sdes BIGNUM *shared_secret = NULL; 70295367Sdes struct sshkey *server_host_private, *server_host_public; 71218767Sdes u_char *server_host_key_blob = NULL, *signature = NULL; 72295367Sdes u_char *kbuf = NULL; 73295367Sdes u_char hash[SSH_DIGEST_MAX_LENGTH]; 74295367Sdes size_t slen, sbloblen; 75295367Sdes size_t klen = 0, hashlen; 76295367Sdes int r; 77218767Sdes 78295367Sdes if ((server_key = EC_KEY_new_by_curve_name(kex->ec_nid)) == NULL) { 79295367Sdes r = SSH_ERR_ALLOC_FAIL; 80295367Sdes goto out; 81295367Sdes } 82295367Sdes if (EC_KEY_generate_key(server_key) != 1) { 83295367Sdes r = SSH_ERR_LIBCRYPTO_ERROR; 84295367Sdes goto out; 85295367Sdes } 86218767Sdes group = EC_KEY_get0_group(server_key); 87218767Sdes 88218767Sdes#ifdef DEBUG_KEXECDH 89218767Sdes fputs("server private key:\n", stderr); 90295367Sdes sshkey_dump_ec_key(server_key); 91218767Sdes#endif 92218767Sdes 93218767Sdes if (kex->load_host_public_key == NULL || 94295367Sdes kex->load_host_private_key == NULL) { 95295367Sdes r = SSH_ERR_INVALID_ARGUMENT; 96295367Sdes goto out; 97295367Sdes } 98295367Sdes server_host_public = kex->load_host_public_key(kex->hostkey_type, 99295367Sdes kex->hostkey_nid, ssh); 100295367Sdes server_host_private = kex->load_host_private_key(kex->hostkey_type, 101295367Sdes kex->hostkey_nid, ssh); 102295367Sdes if (server_host_public == NULL) { 103295367Sdes r = SSH_ERR_NO_HOSTKEY_LOADED; 104295367Sdes goto out; 105295367Sdes } 106295367Sdes if ((client_public = EC_POINT_new(group)) == NULL) { 107295367Sdes r = SSH_ERR_ALLOC_FAIL; 108295367Sdes goto out; 109295367Sdes } 110295367Sdes if ((r = sshpkt_get_ec(ssh, client_public, group)) != 0 || 111295367Sdes (r = sshpkt_get_end(ssh)) != 0) 112295367Sdes goto out; 113218767Sdes 114218767Sdes#ifdef DEBUG_KEXECDH 115218767Sdes fputs("client public key:\n", stderr); 116295367Sdes sshkey_dump_ec_point(group, client_public); 117218767Sdes#endif 118295367Sdes if (sshkey_ec_validate_public(group, client_public) != 0) { 119295367Sdes sshpkt_disconnect(ssh, "invalid client public key"); 120295367Sdes r = SSH_ERR_MESSAGE_INCOMPLETE; 121295367Sdes goto out; 122295367Sdes } 123218767Sdes 124218767Sdes /* Calculate shared_secret */ 125218767Sdes klen = (EC_GROUP_get_degree(group) + 7) / 8; 126295367Sdes if ((kbuf = malloc(klen)) == NULL || 127295367Sdes (shared_secret = BN_new()) == NULL) { 128295367Sdes r = SSH_ERR_ALLOC_FAIL; 129295367Sdes goto out; 130295367Sdes } 131218767Sdes if (ECDH_compute_key(kbuf, klen, client_public, 132295367Sdes server_key, NULL) != (int)klen || 133295367Sdes BN_bin2bn(kbuf, klen, shared_secret) == NULL) { 134295367Sdes r = SSH_ERR_LIBCRYPTO_ERROR; 135295367Sdes goto out; 136295367Sdes } 137218767Sdes 138295367Sdes#ifdef DEBUG_KEXECDH 139218767Sdes dump_digest("shared secret", kbuf, klen); 140218767Sdes#endif 141218767Sdes /* calc H */ 142295367Sdes if ((r = sshkey_to_blob(server_host_public, &server_host_key_blob, 143295367Sdes &sbloblen)) != 0) 144295367Sdes goto out; 145295367Sdes hashlen = sizeof(hash); 146295367Sdes if ((r = kex_ecdh_hash( 147262566Sdes kex->hash_alg, 148218767Sdes group, 149218767Sdes kex->client_version_string, 150218767Sdes kex->server_version_string, 151295367Sdes sshbuf_ptr(kex->peer), sshbuf_len(kex->peer), 152295367Sdes sshbuf_ptr(kex->my), sshbuf_len(kex->my), 153218767Sdes server_host_key_blob, sbloblen, 154218767Sdes client_public, 155218767Sdes EC_KEY_get0_public_key(server_key), 156218767Sdes shared_secret, 157295367Sdes hash, &hashlen)) != 0) 158295367Sdes goto out; 159218767Sdes 160218767Sdes /* save session id := H */ 161218767Sdes if (kex->session_id == NULL) { 162218767Sdes kex->session_id_len = hashlen; 163295367Sdes kex->session_id = malloc(kex->session_id_len); 164295367Sdes if (kex->session_id == NULL) { 165295367Sdes r = SSH_ERR_ALLOC_FAIL; 166295367Sdes goto out; 167295367Sdes } 168218767Sdes memcpy(kex->session_id, hash, kex->session_id_len); 169218767Sdes } 170218767Sdes 171218767Sdes /* sign H */ 172296781Sdes if ((r = kex->sign(server_host_private, server_host_public, &signature, 173296781Sdes &slen, hash, hashlen, kex->hostkey_alg, ssh->compat)) < 0) 174295367Sdes goto out; 175218767Sdes 176218767Sdes /* destroy_sensitive_data(); */ 177218767Sdes 178295367Sdes public_key = EC_KEY_get0_public_key(server_key); 179218767Sdes /* send server hostkey, ECDH pubkey 'Q_S' and signed H */ 180295367Sdes if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_ECDH_REPLY)) != 0 || 181295367Sdes (r = sshpkt_put_string(ssh, server_host_key_blob, sbloblen)) != 0 || 182295367Sdes (r = sshpkt_put_ec(ssh, public_key, group)) != 0 || 183295367Sdes (r = sshpkt_put_string(ssh, signature, slen)) != 0 || 184295367Sdes (r = sshpkt_send(ssh)) != 0) 185295367Sdes goto out; 186218767Sdes 187295367Sdes if ((r = kex_derive_keys_bn(ssh, hash, hashlen, shared_secret)) == 0) 188295367Sdes r = kex_send_newkeys(ssh); 189295367Sdes out: 190295367Sdes explicit_bzero(hash, sizeof(hash)); 191295367Sdes if (kex->ec_client_key) { 192295367Sdes EC_KEY_free(kex->ec_client_key); 193295367Sdes kex->ec_client_key = NULL; 194295367Sdes } 195295367Sdes if (server_key) 196295367Sdes EC_KEY_free(server_key); 197295367Sdes if (kbuf) { 198295367Sdes explicit_bzero(kbuf, klen); 199295367Sdes free(kbuf); 200295367Sdes } 201295367Sdes if (shared_secret) 202295367Sdes BN_clear_free(shared_secret); 203295367Sdes free(server_host_key_blob); 204255767Sdes free(signature); 205295367Sdes return r; 206295367Sdes} 207295367Sdes#endif /* defined(WITH_OPENSSL) && defined(OPENSSL_HAS_ECC) */ 208218767Sdes 209