djm@openbsd.org | 5104db7 | 2015-01-26 06:10:03 +0000 | [diff] [blame] | 1 | /* $OpenBSD: kexc25519c.c,v 1.7 2015/01/26 06:10:03 djm Exp $ */ |
Damien Miller | 094003f | 2013-11-04 22:59:27 +1100 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2001 Markus Friedl. All rights reserved. |
| 4 | * Copyright (c) 2010 Damien Miller. All rights reserved. |
| 5 | * Copyright (c) 2013 Aris Adamantiadis. 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 | |
| 30 | #include <sys/types.h> |
| 31 | |
| 32 | #include <stdio.h> |
| 33 | #include <string.h> |
| 34 | #include <signal.h> |
| 35 | |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 36 | #include "sshkey.h" |
Damien Miller | 094003f | 2013-11-04 22:59:27 +1100 | [diff] [blame] | 37 | #include "cipher.h" |
| 38 | #include "kex.h" |
| 39 | #include "log.h" |
| 40 | #include "packet.h" |
| 41 | #include "ssh2.h" |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 42 | #include "sshbuf.h" |
| 43 | #include "digest.h" |
| 44 | #include "ssherr.h" |
Damien Miller | 094003f | 2013-11-04 22:59:27 +1100 | [diff] [blame] | 45 | |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 46 | static int |
| 47 | input_kex_c25519_reply(int type, u_int32_t seq, void *ctxt); |
| 48 | |
| 49 | int |
| 50 | kexc25519_client(struct ssh *ssh) |
Damien Miller | 094003f | 2013-11-04 22:59:27 +1100 | [diff] [blame] | 51 | { |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 52 | struct kex *kex = ssh->kex; |
| 53 | int r; |
Damien Miller | 094003f | 2013-11-04 22:59:27 +1100 | [diff] [blame] | 54 | |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 55 | kexc25519_keygen(kex->c25519_client_key, kex->c25519_client_pubkey); |
Damien Miller | 094003f | 2013-11-04 22:59:27 +1100 | [diff] [blame] | 56 | #ifdef DEBUG_KEXECDH |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 57 | dump_digest("client private key:", kex->c25519_client_key, |
| 58 | sizeof(kex->c25519_client_key)); |
Damien Miller | 094003f | 2013-11-04 22:59:27 +1100 | [diff] [blame] | 59 | #endif |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 60 | if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_ECDH_INIT)) != 0 || |
| 61 | (r = sshpkt_put_string(ssh, kex->c25519_client_pubkey, |
| 62 | sizeof(kex->c25519_client_pubkey))) != 0 || |
| 63 | (r = sshpkt_send(ssh)) != 0) |
| 64 | return r; |
Damien Miller | 094003f | 2013-11-04 22:59:27 +1100 | [diff] [blame] | 65 | |
| 66 | debug("expecting SSH2_MSG_KEX_ECDH_REPLY"); |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 67 | ssh_dispatch_set(ssh, SSH2_MSG_KEX_ECDH_REPLY, &input_kex_c25519_reply); |
| 68 | return 0; |
| 69 | } |
| 70 | |
| 71 | static int |
| 72 | input_kex_c25519_reply(int type, u_int32_t seq, void *ctxt) |
| 73 | { |
| 74 | struct ssh *ssh = ctxt; |
| 75 | struct kex *kex = ssh->kex; |
| 76 | struct sshkey *server_host_key = NULL; |
| 77 | struct sshbuf *shared_secret = NULL; |
| 78 | u_char *server_pubkey = NULL; |
| 79 | u_char *server_host_key_blob = NULL, *signature = NULL; |
| 80 | u_char hash[SSH_DIGEST_MAX_LENGTH]; |
| 81 | size_t slen, pklen, sbloblen, hashlen; |
| 82 | int r; |
| 83 | |
| 84 | if (kex->verify_host_key == NULL) { |
| 85 | r = SSH_ERR_INVALID_ARGUMENT; |
| 86 | goto out; |
| 87 | } |
Damien Miller | 094003f | 2013-11-04 22:59:27 +1100 | [diff] [blame] | 88 | |
| 89 | /* hostkey */ |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 90 | if ((r = sshpkt_get_string(ssh, &server_host_key_blob, |
| 91 | &sbloblen)) != 0 || |
| 92 | (r = sshkey_from_blob(server_host_key_blob, sbloblen, |
| 93 | &server_host_key)) != 0) |
| 94 | goto out; |
djm@openbsd.org | 5104db7 | 2015-01-26 06:10:03 +0000 | [diff] [blame] | 95 | if (server_host_key->type != kex->hostkey_type || |
| 96 | (kex->hostkey_type == KEY_ECDSA && |
| 97 | server_host_key->ecdsa_nid != kex->hostkey_nid)) { |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 98 | r = SSH_ERR_KEY_TYPE_MISMATCH; |
| 99 | goto out; |
| 100 | } |
| 101 | if (kex->verify_host_key(server_host_key, ssh) == -1) { |
| 102 | r = SSH_ERR_SIGNATURE_INVALID; |
| 103 | goto out; |
| 104 | } |
Damien Miller | 094003f | 2013-11-04 22:59:27 +1100 | [diff] [blame] | 105 | |
| 106 | /* Q_S, server public key */ |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 107 | /* signed H */ |
| 108 | if ((r = sshpkt_get_string(ssh, &server_pubkey, &pklen)) != 0 || |
| 109 | (r = sshpkt_get_string(ssh, &signature, &slen)) != 0 || |
| 110 | (r = sshpkt_get_end(ssh)) != 0) |
| 111 | goto out; |
| 112 | if (pklen != CURVE25519_SIZE) { |
| 113 | r = SSH_ERR_SIGNATURE_INVALID; |
| 114 | goto out; |
| 115 | } |
Damien Miller | 094003f | 2013-11-04 22:59:27 +1100 | [diff] [blame] | 116 | |
| 117 | #ifdef DEBUG_KEXECDH |
| 118 | dump_digest("server public key:", server_pubkey, CURVE25519_SIZE); |
| 119 | #endif |
| 120 | |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 121 | if ((shared_secret = sshbuf_new()) == NULL) { |
| 122 | r = SSH_ERR_ALLOC_FAIL; |
| 123 | goto out; |
| 124 | } |
| 125 | if ((r = kexc25519_shared_key(kex->c25519_client_key, server_pubkey, |
| 126 | shared_secret)) < 0) |
| 127 | goto out; |
Damien Miller | 094003f | 2013-11-04 22:59:27 +1100 | [diff] [blame] | 128 | |
| 129 | /* calc and verify H */ |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 130 | hashlen = sizeof(hash); |
| 131 | if ((r = kex_c25519_hash( |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 132 | kex->hash_alg, |
Damien Miller | 094003f | 2013-11-04 22:59:27 +1100 | [diff] [blame] | 133 | kex->client_version_string, |
| 134 | kex->server_version_string, |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 135 | sshbuf_ptr(kex->my), sshbuf_len(kex->my), |
| 136 | sshbuf_ptr(kex->peer), sshbuf_len(kex->peer), |
Damien Miller | 094003f | 2013-11-04 22:59:27 +1100 | [diff] [blame] | 137 | server_host_key_blob, sbloblen, |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 138 | kex->c25519_client_pubkey, |
Damien Miller | 094003f | 2013-11-04 22:59:27 +1100 | [diff] [blame] | 139 | server_pubkey, |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 140 | sshbuf_ptr(shared_secret), sshbuf_len(shared_secret), |
| 141 | hash, &hashlen)) < 0) |
| 142 | goto out; |
| 143 | |
| 144 | if ((r = sshkey_verify(server_host_key, signature, slen, hash, hashlen, |
| 145 | ssh->compat)) != 0) |
| 146 | goto out; |
Damien Miller | 094003f | 2013-11-04 22:59:27 +1100 | [diff] [blame] | 147 | |
| 148 | /* save session id */ |
| 149 | if (kex->session_id == NULL) { |
| 150 | kex->session_id_len = hashlen; |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 151 | kex->session_id = malloc(kex->session_id_len); |
| 152 | if (kex->session_id == NULL) { |
| 153 | r = SSH_ERR_ALLOC_FAIL; |
| 154 | goto out; |
| 155 | } |
Damien Miller | 094003f | 2013-11-04 22:59:27 +1100 | [diff] [blame] | 156 | memcpy(kex->session_id, hash, kex->session_id_len); |
| 157 | } |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 158 | |
| 159 | if ((r = kex_derive_keys(ssh, hash, hashlen, shared_secret)) == 0) |
| 160 | r = kex_send_newkeys(ssh); |
| 161 | out: |
| 162 | explicit_bzero(hash, sizeof(hash)); |
| 163 | explicit_bzero(kex->c25519_client_key, sizeof(kex->c25519_client_key)); |
| 164 | free(server_host_key_blob); |
| 165 | free(server_pubkey); |
| 166 | free(signature); |
| 167 | sshkey_free(server_host_key); |
| 168 | sshbuf_free(shared_secret); |
| 169 | return r; |
Damien Miller | 094003f | 2013-11-04 22:59:27 +1100 | [diff] [blame] | 170 | } |