1 /* $NetBSD: kexecdh.c,v 1.2 2011/07/25 03:03:10 christos Exp $ */ 2 /* $OpenBSD: kexecdh.c,v 1.3 2010/09/22 05:01:29 djm Exp $ */ 3 /* 4 * Copyright (c) 2001 Markus Friedl. All rights reserved. 5 * Copyright (c) 2010 Damien Miller. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28 #include "includes.h" 29 __RCSID("$NetBSD: kexecdh.c,v 1.2 2011/07/25 03:03:10 christos Exp $"); 30 #include <sys/types.h> 31 32 #include <signal.h> 33 #include <string.h> 34 35 #include <openssl/bn.h> 36 #include <openssl/evp.h> 37 #include <openssl/ec.h> 38 #include <openssl/ecdh.h> 39 40 #include "buffer.h" 41 #include "ssh2.h" 42 #include "key.h" 43 #include "cipher.h" 44 #include "kex.h" 45 #include "log.h" 46 47 int 48 kex_ecdh_name_to_nid(const char *kexname) 49 { 50 if (strlen(kexname) < sizeof(KEX_ECDH_SHA2_STEM) - 1) 51 fatal("%s: kexname too short \"%s\"", __func__, kexname); 52 return key_curve_name_to_nid(kexname + sizeof(KEX_ECDH_SHA2_STEM) - 1); 53 } 54 55 const EVP_MD * 56 kex_ecdh_name_to_evpmd(const char *kexname) 57 { 58 int nid = kex_ecdh_name_to_nid(kexname); 59 60 if (nid == -1) 61 fatal("%s: unsupported ECDH curve \"%s\"", __func__, kexname); 62 return key_ec_nid_to_evpmd(nid); 63 } 64 65 void 66 kex_ecdh_hash( 67 const EVP_MD *evp_md, 68 const EC_GROUP *ec_group, 69 char *client_version_string, 70 char *server_version_string, 71 char *ckexinit, int ckexinitlen, 72 char *skexinit, int skexinitlen, 73 u_char *serverhostkeyblob, int sbloblen, 74 const EC_POINT *client_dh_pub, 75 const EC_POINT *server_dh_pub, 76 const BIGNUM *shared_secret, 77 u_char **hash, u_int *hashlen) 78 { 79 Buffer b; 80 EVP_MD_CTX md; 81 static u_char digest[EVP_MAX_MD_SIZE]; 82 83 buffer_init(&b); 84 buffer_put_cstring(&b, client_version_string); 85 buffer_put_cstring(&b, server_version_string); 86 87 /* kexinit messages: fake header: len+SSH2_MSG_KEXINIT */ 88 buffer_put_int(&b, ckexinitlen+1); 89 buffer_put_char(&b, SSH2_MSG_KEXINIT); 90 buffer_append(&b, ckexinit, ckexinitlen); 91 buffer_put_int(&b, skexinitlen+1); 92 buffer_put_char(&b, SSH2_MSG_KEXINIT); 93 buffer_append(&b, skexinit, skexinitlen); 94 95 buffer_put_string(&b, serverhostkeyblob, sbloblen); 96 buffer_put_ecpoint(&b, ec_group, client_dh_pub); 97 buffer_put_ecpoint(&b, ec_group, server_dh_pub); 98 buffer_put_bignum2(&b, shared_secret); 99 100 #ifdef DEBUG_KEX 101 buffer_dump(&b); 102 #endif 103 EVP_DigestInit(&md, evp_md); 104 EVP_DigestUpdate(&md, buffer_ptr(&b), buffer_len(&b)); 105 EVP_DigestFinal(&md, digest, NULL); 106 107 buffer_free(&b); 108 109 #ifdef DEBUG_KEX 110 dump_digest("hash", digest, EVP_MD_size(evp_md)); 111 #endif 112 *hash = digest; 113 *hashlen = EVP_MD_size(evp_md); 114 } 115 116