blob: b2d2c858f84d5e4c17e522642757e6484218d85f [file] [log] [blame]
djm@openbsd.org5104db72015-01-26 06:10:03 +00001/* $OpenBSD: kexc25519s.c,v 1.8 2015/01/26 06:10:03 djm Exp $ */
Damien Miller094003f2013-11-04 22:59:27 +11002/*
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 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include "includes.h"
28
29#include <sys/types.h>
30#include <string.h>
31#include <signal.h>
32
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000033#include "sshkey.h"
Damien Miller094003f2013-11-04 22:59:27 +110034#include "cipher.h"
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000035#include "digest.h"
Damien Miller094003f2013-11-04 22:59:27 +110036#include "kex.h"
37#include "log.h"
38#include "packet.h"
39#include "ssh2.h"
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000040#include "sshbuf.h"
41#include "ssherr.h"
Damien Miller094003f2013-11-04 22:59:27 +110042
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000043static int input_kex_c25519_init(int, u_int32_t, void *);
44
45int
46kexc25519_server(struct ssh *ssh)
Damien Miller094003f2013-11-04 22:59:27 +110047{
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000048 debug("expecting SSH2_MSG_KEX_ECDH_INIT");
49 ssh_dispatch_set(ssh, SSH2_MSG_KEX_ECDH_INIT, &input_kex_c25519_init);
50 return 0;
51}
52
53static int
54input_kex_c25519_init(int type, u_int32_t seq, void *ctxt)
55{
56 struct ssh *ssh = ctxt;
57 struct kex *kex = ssh->kex;
58 struct sshkey *server_host_private, *server_host_public;
59 struct sshbuf *shared_secret = NULL;
Damien Miller094003f2013-11-04 22:59:27 +110060 u_char *server_host_key_blob = NULL, *signature = NULL;
61 u_char server_key[CURVE25519_SIZE];
62 u_char *client_pubkey = NULL;
63 u_char server_pubkey[CURVE25519_SIZE];
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000064 u_char hash[SSH_DIGEST_MAX_LENGTH];
65 size_t slen, pklen, sbloblen, hashlen;
66 int r;
Damien Miller094003f2013-11-04 22:59:27 +110067
68 /* generate private key */
69 kexc25519_keygen(server_key, server_pubkey);
70#ifdef DEBUG_KEXECDH
71 dump_digest("server private key:", server_key, sizeof(server_key));
72#endif
Damien Miller094003f2013-11-04 22:59:27 +110073 if (kex->load_host_public_key == NULL ||
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000074 kex->load_host_private_key == NULL) {
75 r = SSH_ERR_INVALID_ARGUMENT;
76 goto out;
77 }
djm@openbsd.org5104db72015-01-26 06:10:03 +000078 server_host_public = kex->load_host_public_key(kex->hostkey_type,
79 kex->hostkey_nid, ssh);
80 server_host_private = kex->load_host_private_key(kex->hostkey_type,
81 kex->hostkey_nid, ssh);
djm@openbsd.orge2cc6be2015-01-20 07:55:33 +000082 if (server_host_public == NULL) {
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000083 r = SSH_ERR_NO_HOSTKEY_LOADED;
84 goto out;
85 }
Damien Miller094003f2013-11-04 22:59:27 +110086
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000087 if ((r = sshpkt_get_string(ssh, &client_pubkey, &pklen)) != 0 ||
88 (r = sshpkt_get_end(ssh)) != 0)
89 goto out;
90 if (pklen != CURVE25519_SIZE) {
91 r = SSH_ERR_SIGNATURE_INVALID;
92 goto out;
93 }
Damien Miller094003f2013-11-04 22:59:27 +110094#ifdef DEBUG_KEXECDH
95 dump_digest("client public key:", client_pubkey, CURVE25519_SIZE);
96#endif
97
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000098 if ((shared_secret = sshbuf_new()) == NULL) {
99 r = SSH_ERR_ALLOC_FAIL;
100 goto out;
101 }
102 if ((r = kexc25519_shared_key(server_key, client_pubkey,
103 shared_secret)) < 0)
104 goto out;
Damien Miller094003f2013-11-04 22:59:27 +1100105
106 /* calc H */
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000107 if ((r = sshkey_to_blob(server_host_public, &server_host_key_blob,
108 &sbloblen)) != 0)
109 goto out;
110 hashlen = sizeof(hash);
111 if ((r = kex_c25519_hash(
Damien Millerb3051d02014-01-10 10:58:53 +1100112 kex->hash_alg,
Damien Miller094003f2013-11-04 22:59:27 +1100113 kex->client_version_string,
114 kex->server_version_string,
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000115 sshbuf_ptr(kex->peer), sshbuf_len(kex->peer),
116 sshbuf_ptr(kex->my), sshbuf_len(kex->my),
Damien Miller094003f2013-11-04 22:59:27 +1100117 server_host_key_blob, sbloblen,
118 client_pubkey,
119 server_pubkey,
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000120 sshbuf_ptr(shared_secret), sshbuf_len(shared_secret),
121 hash, &hashlen)) < 0)
122 goto out;
Damien Miller094003f2013-11-04 22:59:27 +1100123
124 /* save session id := H */
125 if (kex->session_id == NULL) {
126 kex->session_id_len = hashlen;
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000127 kex->session_id = malloc(kex->session_id_len);
128 if (kex->session_id == NULL) {
129 r = SSH_ERR_ALLOC_FAIL;
130 goto out;
131 }
Damien Miller094003f2013-11-04 22:59:27 +1100132 memcpy(kex->session_id, hash, kex->session_id_len);
133 }
134
135 /* sign H */
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000136 if ((r = kex->sign(server_host_private, server_host_public,
137 &signature, &slen, hash, hashlen, ssh->compat)) < 0)
138 goto out;
Damien Miller094003f2013-11-04 22:59:27 +1100139
140 /* send server hostkey, ECDH pubkey 'Q_S' and signed H */
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000141 if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_ECDH_REPLY)) != 0 ||
142 (r = sshpkt_put_string(ssh, server_host_key_blob, sbloblen)) != 0 ||
143 (r = sshpkt_put_string(ssh, server_pubkey, sizeof(server_pubkey))) != 0 ||
144 (r = sshpkt_put_string(ssh, signature, slen)) != 0 ||
145 (r = sshpkt_send(ssh)) != 0)
146 goto out;
Damien Miller094003f2013-11-04 22:59:27 +1100147
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000148 if ((r = kex_derive_keys(ssh, hash, hashlen, shared_secret)) == 0)
149 r = kex_send_newkeys(ssh);
150out:
151 explicit_bzero(hash, sizeof(hash));
152 explicit_bzero(server_key, sizeof(server_key));
Damien Miller094003f2013-11-04 22:59:27 +1100153 free(server_host_key_blob);
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000154 free(signature);
Damien Miller094003f2013-11-04 22:59:27 +1100155 free(client_pubkey);
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000156 sshbuf_free(shared_secret);
157 return r;
Damien Miller094003f2013-11-04 22:59:27 +1100158}