blob: 0bfa08b07f597693d0432bb34160a0caef18de28 [file] [log] [blame]
djm@openbsd.orge2cc6be2015-01-20 07:55:33 +00001/* $OpenBSD: kexdhs.c,v 1.21 2015/01/20 07:55:33 djm Exp $ */
Damien Miller8e7fb332003-02-24 12:03:03 +11002/*
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"
Damien Miller8e7fb332003-02-24 12:03:03 +110027
Damien Miller72ef7c12015-01-15 02:21:31 +110028#ifdef WITH_OPENSSL
29
Damien Millerd7834352006-08-05 12:39:39 +100030#include <sys/types.h>
Damien Millerded319c2006-09-01 15:38:36 +100031
32#include <stdarg.h>
Damien Millere3476ed2006-07-24 14:13:33 +100033#include <string.h>
Damien Millerd7834352006-08-05 12:39:39 +100034#include <signal.h>
Damien Millere3476ed2006-07-24 14:13:33 +100035
Damien Miller4499f4c2010-11-20 15:15:49 +110036#include <openssl/dh.h>
37
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000038#include "sshkey.h"
Damien Millerd7834352006-08-05 12:39:39 +100039#include "cipher.h"
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000040#include "digest.h"
Damien Miller8e7fb332003-02-24 12:03:03 +110041#include "kex.h"
42#include "log.h"
43#include "packet.h"
44#include "dh.h"
45#include "ssh2.h"
Damien Miller8e7fb332003-02-24 12:03:03 +110046
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000047#include "dispatch.h"
48#include "compat.h"
49#include "ssherr.h"
50#include "sshbuf.h"
51
52static int input_kex_dh_init(int, u_int32_t, void *);
53
54int
55kexdh_server(struct ssh *ssh)
Damien Miller8e7fb332003-02-24 12:03:03 +110056{
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000057 struct kex *kex = ssh->kex;
58 int r;
Damien Miller8e7fb332003-02-24 12:03:03 +110059
60 /* generate server DH public key */
Damien Millerf675fc42004-06-15 10:30:09 +100061 switch (kex->kex_type) {
62 case KEX_DH_GRP1_SHA1:
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000063 kex->dh = dh_new_group1();
Damien Millerf675fc42004-06-15 10:30:09 +100064 break;
65 case KEX_DH_GRP14_SHA1:
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000066 kex->dh = dh_new_group14();
Damien Millerf675fc42004-06-15 10:30:09 +100067 break;
68 default:
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000069 r = SSH_ERR_INVALID_ARGUMENT;
70 goto out;
Damien Millerf675fc42004-06-15 10:30:09 +100071 }
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000072 if (kex->dh == NULL) {
73 r = SSH_ERR_ALLOC_FAIL;
74 goto out;
75 }
76 if ((r = dh_gen_key(kex->dh, kex->we_need * 8)) != 0)
77 goto out;
Damien Miller8e7fb332003-02-24 12:03:03 +110078
79 debug("expecting SSH2_MSG_KEXDH_INIT");
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000080 ssh_dispatch_set(ssh, SSH2_MSG_KEXDH_INIT, &input_kex_dh_init);
81 r = 0;
82 out:
83 return r;
84}
85
86int
87input_kex_dh_init(int type, u_int32_t seq, void *ctxt)
88{
89 struct ssh *ssh = ctxt;
90 struct kex *kex = ssh->kex;
91 BIGNUM *shared_secret = NULL, *dh_client_pub = NULL;
92 struct sshkey *server_host_public, *server_host_private;
93 u_char *kbuf = NULL, *signature = NULL, *server_host_key_blob = NULL;
94 u_char hash[SSH_DIGEST_MAX_LENGTH];
95 size_t sbloblen, slen;
96 size_t klen = 0, hashlen;
97 int kout, r;
Damien Miller8e7fb332003-02-24 12:03:03 +110098
Damien Miller0a80ca12010-02-27 07:55:05 +110099 if (kex->load_host_public_key == NULL ||
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000100 kex->load_host_private_key == NULL) {
101 r = SSH_ERR_INVALID_ARGUMENT;
102 goto out;
103 }
djm@openbsd.orge2cc6be2015-01-20 07:55:33 +0000104 server_host_public = kex->load_host_public_key(kex->hostkey_type, ssh);
105 server_host_private = kex->load_host_private_key(kex->hostkey_type, ssh);
106 if (server_host_public == NULL) {
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000107 r = SSH_ERR_NO_HOSTKEY_LOADED;
108 goto out;
109 }
Damien Miller8e7fb332003-02-24 12:03:03 +1100110
111 /* key, cert */
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000112 if ((dh_client_pub = BN_new()) == NULL) {
113 r = SSH_ERR_ALLOC_FAIL;
114 goto out;
115 }
116 if ((r = sshpkt_get_bignum2(ssh, dh_client_pub)) != 0 ||
117 (r = sshpkt_get_end(ssh)) != 0)
118 goto out;
Damien Miller8e7fb332003-02-24 12:03:03 +1100119
120#ifdef DEBUG_KEXDH
121 fprintf(stderr, "dh_client_pub= ");
122 BN_print_fp(stderr, dh_client_pub);
123 fprintf(stderr, "\n");
124 debug("bits %d", BN_num_bits(dh_client_pub));
125#endif
126
127#ifdef DEBUG_KEXDH
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000128 DHparams_print_fp(stderr, kex->dh);
Damien Miller8e7fb332003-02-24 12:03:03 +1100129 fprintf(stderr, "pub= ");
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000130 BN_print_fp(stderr, kex->dh->pub_key);
Damien Miller8e7fb332003-02-24 12:03:03 +1100131 fprintf(stderr, "\n");
132#endif
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000133 if (!dh_pub_is_valid(kex->dh, dh_client_pub)) {
134 sshpkt_disconnect(ssh, "bad client public DH value");
135 r = SSH_ERR_MESSAGE_INCOMPLETE;
136 goto out;
137 }
Damien Miller8e7fb332003-02-24 12:03:03 +1100138
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000139 klen = DH_size(kex->dh);
140 if ((kbuf = malloc(klen)) == NULL ||
141 (shared_secret = BN_new()) == NULL) {
142 r = SSH_ERR_ALLOC_FAIL;
143 goto out;
144 }
145 if ((kout = DH_compute_key(kbuf, dh_client_pub, kex->dh)) < 0 ||
146 BN_bin2bn(kbuf, kout, shared_secret) == NULL) {
147 r = SSH_ERR_LIBCRYPTO_ERROR;
148 goto out;
149 }
Damien Miller8e7fb332003-02-24 12:03:03 +1100150#ifdef DEBUG_KEXDH
151 dump_digest("shared secret", kbuf, kout);
152#endif
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000153 if ((r = sshkey_to_blob(server_host_public, &server_host_key_blob,
154 &sbloblen)) != 0)
155 goto out;
Damien Miller8e7fb332003-02-24 12:03:03 +1100156 /* calc H */
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000157 hashlen = sizeof(hash);
158 if ((r = kex_dh_hash(
Damien Miller8e7fb332003-02-24 12:03:03 +1100159 kex->client_version_string,
160 kex->server_version_string,
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000161 sshbuf_ptr(kex->peer), sshbuf_len(kex->peer),
162 sshbuf_ptr(kex->my), sshbuf_len(kex->my),
Damien Miller8e7fb332003-02-24 12:03:03 +1100163 server_host_key_blob, sbloblen,
164 dh_client_pub,
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000165 kex->dh->pub_key,
Damien Miller19bb3a52005-11-05 15:19:35 +1100166 shared_secret,
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000167 hash, &hashlen)) != 0)
168 goto out;
Damien Miller8e7fb332003-02-24 12:03:03 +1100169
170 /* save session id := H */
Damien Miller8e7fb332003-02-24 12:03:03 +1100171 if (kex->session_id == NULL) {
Damien Miller19bb3a52005-11-05 15:19:35 +1100172 kex->session_id_len = hashlen;
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000173 kex->session_id = malloc(kex->session_id_len);
174 if (kex->session_id == NULL) {
175 r = SSH_ERR_ALLOC_FAIL;
176 goto out;
177 }
Damien Miller8e7fb332003-02-24 12:03:03 +1100178 memcpy(kex->session_id, hash, kex->session_id_len);
179 }
180
181 /* sign H */
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000182 if ((r = kex->sign(server_host_private, server_host_public,
183 &signature, &slen, hash, hashlen, ssh->compat)) < 0)
184 goto out;
Damien Miller8e7fb332003-02-24 12:03:03 +1100185
186 /* destroy_sensitive_data(); */
187
188 /* send server hostkey, DH pubkey 'f' and singed H */
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000189 if ((r = sshpkt_start(ssh, SSH2_MSG_KEXDH_REPLY)) != 0 ||
190 (r = sshpkt_put_string(ssh, server_host_key_blob, sbloblen)) != 0 ||
191 (r = sshpkt_put_bignum2(ssh, kex->dh->pub_key)) != 0 || /* f */
192 (r = sshpkt_put_string(ssh, signature, slen)) != 0 ||
193 (r = sshpkt_send(ssh)) != 0)
194 goto out;
Damien Miller8e7fb332003-02-24 12:03:03 +1100195
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000196 if ((r = kex_derive_keys_bn(ssh, hash, hashlen, shared_secret)) == 0)
197 r = kex_send_newkeys(ssh);
198 out:
199 explicit_bzero(hash, sizeof(hash));
200 DH_free(kex->dh);
201 kex->dh = NULL;
202 if (dh_client_pub)
203 BN_clear_free(dh_client_pub);
204 if (kbuf) {
205 explicit_bzero(kbuf, klen);
206 free(kbuf);
207 }
208 if (shared_secret)
209 BN_clear_free(shared_secret);
Darren Tuckera627d422013-06-02 07:31:17 +1000210 free(server_host_key_blob);
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000211 free(signature);
212 return r;
Damien Miller8e7fb332003-02-24 12:03:03 +1100213}
Damien Miller72ef7c12015-01-15 02:21:31 +1100214#endif /* WITH_OPENSSL */