1255767Sdes/* $OpenBSD: kexecdhs.c,v 1.5 2013/07/19 07:37:48 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 29218767Sdes#include <sys/types.h> 30218767Sdes#include <string.h> 31218767Sdes#include <signal.h> 32218767Sdes 33218767Sdes#include "xmalloc.h" 34218767Sdes#include "buffer.h" 35218767Sdes#include "key.h" 36218767Sdes#include "cipher.h" 37218767Sdes#include "kex.h" 38218767Sdes#include "log.h" 39218767Sdes#include "packet.h" 40218767Sdes#include "dh.h" 41218767Sdes#include "ssh2.h" 42218767Sdes#ifdef GSSAPI 43218767Sdes#include "ssh-gss.h" 44218767Sdes#endif 45218767Sdes#include "monitor_wrap.h" 46218767Sdes 47218767Sdes#ifdef OPENSSL_HAS_ECC 48218767Sdes 49218767Sdes#include <openssl/ecdh.h> 50218767Sdes 51218767Sdesvoid 52218767Sdeskexecdh_server(Kex *kex) 53218767Sdes{ 54218767Sdes EC_POINT *client_public; 55218767Sdes EC_KEY *server_key; 56218767Sdes const EC_GROUP *group; 57218767Sdes BIGNUM *shared_secret; 58218767Sdes Key *server_host_private, *server_host_public; 59218767Sdes u_char *server_host_key_blob = NULL, *signature = NULL; 60218767Sdes u_char *kbuf, *hash; 61218767Sdes u_int klen, slen, sbloblen, hashlen; 62218767Sdes 63255767Sdes if ((server_key = EC_KEY_new_by_curve_name(kex->ec_nid)) == NULL) 64218767Sdes fatal("%s: EC_KEY_new_by_curve_name failed", __func__); 65218767Sdes if (EC_KEY_generate_key(server_key) != 1) 66218767Sdes fatal("%s: EC_KEY_generate_key failed", __func__); 67218767Sdes group = EC_KEY_get0_group(server_key); 68218767Sdes 69218767Sdes#ifdef DEBUG_KEXECDH 70218767Sdes fputs("server private key:\n", stderr); 71218767Sdes key_dump_ec_key(server_key); 72218767Sdes#endif 73218767Sdes 74218767Sdes if (kex->load_host_public_key == NULL || 75218767Sdes kex->load_host_private_key == NULL) 76218767Sdes fatal("Cannot load hostkey"); 77218767Sdes server_host_public = kex->load_host_public_key(kex->hostkey_type); 78218767Sdes if (server_host_public == NULL) 79218767Sdes fatal("Unsupported hostkey type %d", kex->hostkey_type); 80218767Sdes server_host_private = kex->load_host_private_key(kex->hostkey_type); 81218767Sdes 82218767Sdes debug("expecting SSH2_MSG_KEX_ECDH_INIT"); 83218767Sdes packet_read_expect(SSH2_MSG_KEX_ECDH_INIT); 84218767Sdes if ((client_public = EC_POINT_new(group)) == NULL) 85218767Sdes fatal("%s: EC_POINT_new failed", __func__); 86218767Sdes packet_get_ecpoint(group, client_public); 87218767Sdes packet_check_eom(); 88218767Sdes 89218767Sdes if (key_ec_validate_public(group, client_public) != 0) 90218767Sdes fatal("%s: invalid client public key", __func__); 91218767Sdes 92218767Sdes#ifdef DEBUG_KEXECDH 93218767Sdes fputs("client public key:\n", stderr); 94218767Sdes key_dump_ec_point(group, client_public); 95218767Sdes#endif 96218767Sdes 97218767Sdes /* Calculate shared_secret */ 98218767Sdes klen = (EC_GROUP_get_degree(group) + 7) / 8; 99218767Sdes kbuf = xmalloc(klen); 100218767Sdes if (ECDH_compute_key(kbuf, klen, client_public, 101218767Sdes server_key, NULL) != (int)klen) 102218767Sdes fatal("%s: ECDH_compute_key failed", __func__); 103218767Sdes 104218767Sdes#ifdef DEBUG_KEXDH 105218767Sdes dump_digest("shared secret", kbuf, klen); 106218767Sdes#endif 107218767Sdes if ((shared_secret = BN_new()) == NULL) 108218767Sdes fatal("%s: BN_new failed", __func__); 109218767Sdes if (BN_bin2bn(kbuf, klen, shared_secret) == NULL) 110218767Sdes fatal("%s: BN_bin2bn failed", __func__); 111218767Sdes memset(kbuf, 0, klen); 112255767Sdes free(kbuf); 113218767Sdes 114218767Sdes /* calc H */ 115218767Sdes key_to_blob(server_host_public, &server_host_key_blob, &sbloblen); 116218767Sdes kex_ecdh_hash( 117218767Sdes kex->evp_md, 118218767Sdes group, 119218767Sdes kex->client_version_string, 120218767Sdes kex->server_version_string, 121218767Sdes buffer_ptr(&kex->peer), buffer_len(&kex->peer), 122218767Sdes buffer_ptr(&kex->my), buffer_len(&kex->my), 123218767Sdes server_host_key_blob, sbloblen, 124218767Sdes client_public, 125218767Sdes EC_KEY_get0_public_key(server_key), 126218767Sdes shared_secret, 127218767Sdes &hash, &hashlen 128218767Sdes ); 129218767Sdes EC_POINT_clear_free(client_public); 130218767Sdes 131218767Sdes /* save session id := H */ 132218767Sdes if (kex->session_id == NULL) { 133218767Sdes kex->session_id_len = hashlen; 134218767Sdes kex->session_id = xmalloc(kex->session_id_len); 135218767Sdes memcpy(kex->session_id, hash, kex->session_id_len); 136218767Sdes } 137218767Sdes 138218767Sdes /* sign H */ 139255767Sdes kex->sign(server_host_private, server_host_public, &signature, &slen, 140255767Sdes hash, hashlen); 141218767Sdes 142218767Sdes /* destroy_sensitive_data(); */ 143218767Sdes 144218767Sdes /* send server hostkey, ECDH pubkey 'Q_S' and signed H */ 145218767Sdes packet_start(SSH2_MSG_KEX_ECDH_REPLY); 146218767Sdes packet_put_string(server_host_key_blob, sbloblen); 147218767Sdes packet_put_ecpoint(group, EC_KEY_get0_public_key(server_key)); 148218767Sdes packet_put_string(signature, slen); 149218767Sdes packet_send(); 150218767Sdes 151255767Sdes free(signature); 152255767Sdes free(server_host_key_blob); 153218767Sdes /* have keys, free server key */ 154218767Sdes EC_KEY_free(server_key); 155218767Sdes 156218767Sdes kex_derive_keys(kex, hash, hashlen, shared_secret); 157218767Sdes BN_clear_free(shared_secret); 158218767Sdes kex_finish(kex); 159218767Sdes} 160218767Sdes#else /* OPENSSL_HAS_ECC */ 161218767Sdesvoid 162218767Sdeskexecdh_server(Kex *kex) 163218767Sdes{ 164218767Sdes fatal("ECC support is not enabled"); 165218767Sdes} 166218767Sdes#endif /* OPENSSL_HAS_ECC */ 167