kexecdhs.c revision 218767
11897Swollman/* $OpenBSD: kexecdhs.c,v 1.2 2010/09/22 05:01:29 djm Exp $ */ 21897Swollman/* 31897Swollman * Copyright (c) 2001 Markus Friedl. All rights reserved. 41897Swollman * Copyright (c) 2010 Damien Miller. All rights reserved. 51897Swollman * 61897Swollman * Redistribution and use in source and binary forms, with or without 71897Swollman * modification, are permitted provided that the following conditions 812798Swpaul * are met: 91897Swollman * 1. Redistributions of source code must retain the above copyright 101897Swollman * notice, this list of conditions and the following disclaimer. 111897Swollman * 2. Redistributions in binary form must reproduce the above copyright 1212798Swpaul * notice, this list of conditions and the following disclaimer in the 131897Swollman * documentation and/or other materials provided with the distribution. 141897Swollman * 151897Swollman * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 1612798Swpaul * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 171897Swollman * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 181897Swollman * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 191897Swollman * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 2012798Swpaul * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 211897Swollman * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 221897Swollman * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 231897Swollman * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 2412798Swpaul * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 251897Swollman */ 261897Swollman 271897Swollman#include "includes.h" 281897Swollman 2912798Swpaul#include <sys/types.h> 3012798Swpaul#include <string.h> 3112798Swpaul#include <signal.h> 321897Swollman 3379880Skris#include "xmalloc.h" 3412798Swpaul#include "buffer.h" 351897Swollman#include "key.h" 3679880Skris#include "cipher.h" 3779880Skris#include "kex.h" 3879880Skris#include "log.h" 391897Swollman#include "packet.h" 401897Swollman#include "dh.h" 411897Swollman#include "ssh2.h" 4212798Swpaul#ifdef GSSAPI 431897Swollman#include "ssh-gss.h" 441897Swollman#endif 4512798Swpaul#include "monitor_wrap.h" 461897Swollman 471897Swollman#ifdef OPENSSL_HAS_ECC 481897Swollman 491897Swollman#include <openssl/ecdh.h> 501897Swollman 511897Swollmanvoid 521897Swollmankexecdh_server(Kex *kex) 531897Swollman{ 5412798Swpaul EC_POINT *client_public; 551897Swollman EC_KEY *server_key; 5612798Swpaul const EC_GROUP *group; 5712798Swpaul BIGNUM *shared_secret; 5812798Swpaul Key *server_host_private, *server_host_public; 5917142Sjkh u_char *server_host_key_blob = NULL, *signature = NULL; 6017142Sjkh u_char *kbuf, *hash; 6117142Sjkh u_int klen, slen, sbloblen, hashlen; 6217142Sjkh int curve_nid; 6317142Sjkh 6417142Sjkh if ((curve_nid = kex_ecdh_name_to_nid(kex->name)) == -1) 6517142Sjkh fatal("%s: unsupported ECDH curve \"%s\"", __func__, kex->name); 6617142Sjkh if ((server_key = EC_KEY_new_by_curve_name(curve_nid)) == NULL) 6717142Sjkh fatal("%s: EC_KEY_new_by_curve_name failed", __func__); 6817142Sjkh if (EC_KEY_generate_key(server_key) != 1) 6917142Sjkh fatal("%s: EC_KEY_generate_key failed", __func__); 7017142Sjkh group = EC_KEY_get0_group(server_key); 7117142Sjkh 7217142Sjkh#ifdef DEBUG_KEXECDH 7312798Swpaul fputs("server private key:\n", stderr); 7412798Swpaul key_dump_ec_key(server_key); 7517142Sjkh#endif 7612798Swpaul 7712798Swpaul if (kex->load_host_public_key == NULL || 7812798Swpaul kex->load_host_private_key == NULL) 7912798Swpaul fatal("Cannot load hostkey"); 8012798Swpaul server_host_public = kex->load_host_public_key(kex->hostkey_type); 8112798Swpaul if (server_host_public == NULL) 8212798Swpaul fatal("Unsupported hostkey type %d", kex->hostkey_type); 8312798Swpaul server_host_private = kex->load_host_private_key(kex->hostkey_type); 8412798Swpaul if (server_host_private == NULL) 8512798Swpaul fatal("Missing private key for hostkey type %d", 8612798Swpaul kex->hostkey_type); 8712798Swpaul 8812798Swpaul debug("expecting SSH2_MSG_KEX_ECDH_INIT"); 8912798Swpaul packet_read_expect(SSH2_MSG_KEX_ECDH_INIT); 9012798Swpaul if ((client_public = EC_POINT_new(group)) == NULL) 9112798Swpaul fatal("%s: EC_POINT_new failed", __func__); 9212798Swpaul packet_get_ecpoint(group, client_public); 9312798Swpaul packet_check_eom(); 9412798Swpaul 9512798Swpaul if (key_ec_validate_public(group, client_public) != 0) 9612798Swpaul fatal("%s: invalid client public key", __func__); 9712798Swpaul 981897Swollman#ifdef DEBUG_KEXECDH 998874Srgrimes fputs("client public key:\n", stderr); 1001897Swollman key_dump_ec_point(group, client_public); 1011897Swollman#endif 10212798Swpaul 10312798Swpaul /* Calculate shared_secret */ 10412798Swpaul klen = (EC_GROUP_get_degree(group) + 7) / 8; 10512798Swpaul kbuf = xmalloc(klen); 1061897Swollman if (ECDH_compute_key(kbuf, klen, client_public, 10712798Swpaul server_key, NULL) != (int)klen) 10812798Swpaul fatal("%s: ECDH_compute_key failed", __func__); 10912798Swpaul 11012798Swpaul#ifdef DEBUG_KEXDH 11112798Swpaul dump_digest("shared secret", kbuf, klen); 11212798Swpaul#endif 11312798Swpaul if ((shared_secret = BN_new()) == NULL) 11412798Swpaul fatal("%s: BN_new failed", __func__); 11512798Swpaul if (BN_bin2bn(kbuf, klen, shared_secret) == NULL) 11612798Swpaul fatal("%s: BN_bin2bn failed", __func__); 1171897Swollman memset(kbuf, 0, klen); 11812798Swpaul xfree(kbuf); 11912798Swpaul 12012798Swpaul /* calc H */ 12112798Swpaul key_to_blob(server_host_public, &server_host_key_blob, &sbloblen); 12212798Swpaul kex_ecdh_hash( 12312798Swpaul kex->evp_md, 12412798Swpaul group, 12512798Swpaul kex->client_version_string, 12612798Swpaul kex->server_version_string, 12712798Swpaul buffer_ptr(&kex->peer), buffer_len(&kex->peer), 12812798Swpaul buffer_ptr(&kex->my), buffer_len(&kex->my), 12912798Swpaul server_host_key_blob, sbloblen, 13012798Swpaul client_public, 13112798Swpaul EC_KEY_get0_public_key(server_key), 1321897Swollman shared_secret, 13312798Swpaul &hash, &hashlen 1341897Swollman ); 13512798Swpaul EC_POINT_clear_free(client_public); 13612798Swpaul 1371897Swollman /* save session id := H */ 13812798Swpaul if (kex->session_id == NULL) { 13912798Swpaul kex->session_id_len = hashlen; 14012798Swpaul kex->session_id = xmalloc(kex->session_id_len); 14112798Swpaul memcpy(kex->session_id, hash, kex->session_id_len); 14212798Swpaul } 14312798Swpaul 14412798Swpaul /* sign H */ 1451897Swollman if (PRIVSEP(key_sign(server_host_private, &signature, &slen, 14612798Swpaul hash, hashlen)) < 0) 14712798Swpaul fatal("kexdh_server: key_sign failed"); 14812798Swpaul 14912798Swpaul /* destroy_sensitive_data(); */ 15012798Swpaul 15112798Swpaul /* send server hostkey, ECDH pubkey 'Q_S' and signed H */ 15212798Swpaul packet_start(SSH2_MSG_KEX_ECDH_REPLY); 15312798Swpaul packet_put_string(server_host_key_blob, sbloblen); 15412798Swpaul packet_put_ecpoint(group, EC_KEY_get0_public_key(server_key)); 15512798Swpaul packet_put_string(signature, slen); 15612798Swpaul packet_send(); 15712798Swpaul 15812798Swpaul xfree(signature); 15912798Swpaul xfree(server_host_key_blob); 16012798Swpaul /* have keys, free server key */ 16112798Swpaul EC_KEY_free(server_key); 16212798Swpaul 16312798Swpaul kex_derive_keys(kex, hash, hashlen, shared_secret); 16412798Swpaul BN_clear_free(shared_secret); 16512798Swpaul kex_finish(kex); 16612798Swpaul} 16712798Swpaul#else /* OPENSSL_HAS_ECC */ 16812798Swpaulvoid 16912798Swpaulkexecdh_server(Kex *kex) 17012798Swpaul{ 17112798Swpaul fatal("ECC support is not enabled"); 17212798Swpaul} 17312798Swpaul#endif /* OPENSSL_HAS_ECC */ 17412798Swpaul