1/* $OpenBSD: kexdhc.c,v 1.12 2010/11/10 01:33:07 djm Exp $ */ 2/* 3 * Copyright (c) 2001 Markus Friedl. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 */ 25 26#include "includes.h" 27 28#include <sys/types.h> 29 30#ifdef __APPLE_CRYPTO__ 31#include "ossl-dh.h" 32#else 33#include <openssl/dh.h> 34#endif 35 36#include <stdarg.h> 37#include <stdio.h> 38#include <string.h> 39#include <signal.h> 40 41#include "xmalloc.h" 42#include "buffer.h" 43#include "key.h" 44#include "cipher.h" 45#include "kex.h" 46#include "log.h" 47#include "packet.h" 48#include "dh.h" 49#include "ssh2.h" 50 51void 52kexdh_client(Kex *kex) 53{ 54 BIGNUM *dh_server_pub = NULL, *shared_secret = NULL; 55 DH *dh; 56 Key *server_host_key; 57 u_char *server_host_key_blob = NULL, *signature = NULL; 58 u_char *kbuf, *hash; 59 u_int klen, slen, sbloblen, hashlen; 60 int kout; 61 62 /* generate and send 'e', client DH public key */ 63 switch (kex->kex_type) { 64 case KEX_DH_GRP1_SHA1: 65 dh = dh_new_group1(); 66 break; 67 case KEX_DH_GRP14_SHA1: 68 dh = dh_new_group14(); 69 break; 70 default: 71 fatal("%s: Unexpected KEX type %d", __func__, kex->kex_type); 72 } 73 dh_gen_key(dh, kex->we_need * 8); 74 packet_start(SSH2_MSG_KEXDH_INIT); 75 packet_put_bignum2(dh->pub_key); 76 packet_send(); 77 78 debug("sending SSH2_MSG_KEXDH_INIT"); 79#ifdef DEBUG_KEXDH 80 DHparams_print_fp(stderr, dh); 81 fprintf(stderr, "pub= "); 82 BN_print_fp(stderr, dh->pub_key); 83 fprintf(stderr, "\n"); 84#endif 85 86 debug("expecting SSH2_MSG_KEXDH_REPLY"); 87 packet_read_expect(SSH2_MSG_KEXDH_REPLY); 88 89 /* key, cert */ 90 server_host_key_blob = packet_get_string(&sbloblen); 91 server_host_key = key_from_blob(server_host_key_blob, sbloblen); 92 if (server_host_key == NULL) 93 fatal("cannot decode server_host_key_blob"); 94 if (server_host_key->type != kex->hostkey_type) 95 fatal("type mismatch for decoded server_host_key_blob"); 96 if (kex->verify_host_key == NULL) 97 fatal("cannot verify server_host_key"); 98 if (kex->verify_host_key(server_host_key) == -1) 99 fatal("server_host_key verification failed"); 100 101 /* DH parameter f, server public DH key */ 102 if ((dh_server_pub = BN_new()) == NULL) 103 fatal("dh_server_pub == NULL"); 104 packet_get_bignum2(dh_server_pub); 105 106#ifdef DEBUG_KEXDH 107 fprintf(stderr, "dh_server_pub= "); 108 BN_print_fp(stderr, dh_server_pub); 109 fprintf(stderr, "\n"); 110 debug("bits %d", BN_num_bits(dh_server_pub)); 111#endif 112 113 /* signed H */ 114 signature = packet_get_string(&slen); 115 packet_check_eom(); 116 117 if (!dh_pub_is_valid(dh, dh_server_pub)) 118 packet_disconnect("bad server public DH value"); 119 120 klen = DH_size(dh); 121 kbuf = xmalloc(klen); 122 if ((kout = DH_compute_key(kbuf, dh_server_pub, dh)) < 0) 123 fatal("DH_compute_key: failed"); 124#ifdef DEBUG_KEXDH 125 dump_digest("shared secret", kbuf, kout); 126#endif 127 if ((shared_secret = BN_new()) == NULL) 128 fatal("kexdh_client: BN_new failed"); 129 if (BN_bin2bn(kbuf, kout, shared_secret) == NULL) 130 fatal("kexdh_client: BN_bin2bn failed"); 131 memset(kbuf, 0, klen); 132 xfree(kbuf); 133 134 /* calc and verify H */ 135 kex_dh_hash( 136 kex->client_version_string, 137 kex->server_version_string, 138 buffer_ptr(&kex->my), buffer_len(&kex->my), 139 buffer_ptr(&kex->peer), buffer_len(&kex->peer), 140 server_host_key_blob, sbloblen, 141 dh->pub_key, 142 dh_server_pub, 143 shared_secret, 144 &hash, &hashlen 145 ); 146 xfree(server_host_key_blob); 147 BN_clear_free(dh_server_pub); 148 DH_free(dh); 149 150 if (key_verify(server_host_key, signature, slen, hash, hashlen) != 1) 151 fatal("key_verify failed for server_host_key"); 152 key_free(server_host_key); 153 xfree(signature); 154 155 /* save session id */ 156 if (kex->session_id == NULL) { 157 kex->session_id_len = hashlen; 158 kex->session_id = xmalloc(kex->session_id_len); 159 memcpy(kex->session_id, hash, kex->session_id_len); 160 } 161 162 kex_derive_keys(kex, hash, hashlen, shared_secret); 163 BN_clear_free(shared_secret); 164 kex_finish(kex); 165} 166