blob: adf70babd1e0672d65478619ee6ccf32db71a287 [file] [log] [blame]
djm@openbsd.org0a843d92018-12-27 03:25:24 +00001/* $OpenBSD: kexdhs.c,v 1.29 2018/12/27 03:25:25 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
Damien Miller48f54b92018-09-13 12:13:50 +100038#include "openbsd-compat/openssl-compat.h"
39
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000040#include "sshkey.h"
Damien Millerd7834352006-08-05 12:39:39 +100041#include "cipher.h"
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000042#include "digest.h"
Damien Miller8e7fb332003-02-24 12:03:03 +110043#include "kex.h"
44#include "log.h"
45#include "packet.h"
46#include "dh.h"
47#include "ssh2.h"
Damien Miller8e7fb332003-02-24 12:03:03 +110048
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000049#include "dispatch.h"
50#include "compat.h"
51#include "ssherr.h"
52#include "sshbuf.h"
53
markus@openbsd.org2ae666a2017-05-30 14:23:52 +000054static int input_kex_dh_init(int, u_int32_t, struct ssh *);
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000055
56int
57kexdh_server(struct ssh *ssh)
Damien Miller8e7fb332003-02-24 12:03:03 +110058{
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000059 struct kex *kex = ssh->kex;
60 int r;
Damien Miller8e7fb332003-02-24 12:03:03 +110061
62 /* generate server DH public key */
Damien Millerf675fc42004-06-15 10:30:09 +100063 switch (kex->kex_type) {
64 case KEX_DH_GRP1_SHA1:
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000065 kex->dh = dh_new_group1();
Damien Millerf675fc42004-06-15 10:30:09 +100066 break;
67 case KEX_DH_GRP14_SHA1:
djm@openbsd.org0e8eeec2016-05-02 10:26:04 +000068 case KEX_DH_GRP14_SHA256:
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000069 kex->dh = dh_new_group14();
Damien Millerf675fc42004-06-15 10:30:09 +100070 break;
djm@openbsd.org0e8eeec2016-05-02 10:26:04 +000071 case KEX_DH_GRP16_SHA512:
72 kex->dh = dh_new_group16();
73 break;
74 case KEX_DH_GRP18_SHA512:
75 kex->dh = dh_new_group18();
76 break;
Damien Millerf675fc42004-06-15 10:30:09 +100077 default:
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000078 r = SSH_ERR_INVALID_ARGUMENT;
79 goto out;
Damien Millerf675fc42004-06-15 10:30:09 +100080 }
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000081 if (kex->dh == NULL) {
82 r = SSH_ERR_ALLOC_FAIL;
83 goto out;
84 }
85 if ((r = dh_gen_key(kex->dh, kex->we_need * 8)) != 0)
86 goto out;
Damien Miller8e7fb332003-02-24 12:03:03 +110087
88 debug("expecting SSH2_MSG_KEXDH_INIT");
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000089 ssh_dispatch_set(ssh, SSH2_MSG_KEXDH_INIT, &input_kex_dh_init);
90 r = 0;
91 out:
92 return r;
93}
94
95int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +000096input_kex_dh_init(int type, u_int32_t seq, struct ssh *ssh)
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000097{
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000098 struct kex *kex = ssh->kex;
99 BIGNUM *shared_secret = NULL, *dh_client_pub = NULL;
djm@openbsd.org482d23b2018-09-13 02:08:33 +0000100 const BIGNUM *pub_key;
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000101 struct sshkey *server_host_public, *server_host_private;
102 u_char *kbuf = NULL, *signature = NULL, *server_host_key_blob = NULL;
103 u_char hash[SSH_DIGEST_MAX_LENGTH];
104 size_t sbloblen, slen;
105 size_t klen = 0, hashlen;
106 int kout, r;
Damien Miller8e7fb332003-02-24 12:03:03 +1100107
Damien Miller0a80ca12010-02-27 07:55:05 +1100108 if (kex->load_host_public_key == NULL ||
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000109 kex->load_host_private_key == NULL) {
110 r = SSH_ERR_INVALID_ARGUMENT;
111 goto out;
112 }
djm@openbsd.org5104db72015-01-26 06:10:03 +0000113 server_host_public = kex->load_host_public_key(kex->hostkey_type,
114 kex->hostkey_nid, ssh);
115 server_host_private = kex->load_host_private_key(kex->hostkey_type,
116 kex->hostkey_nid, ssh);
djm@openbsd.orge2cc6be2015-01-20 07:55:33 +0000117 if (server_host_public == NULL) {
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000118 r = SSH_ERR_NO_HOSTKEY_LOADED;
119 goto out;
120 }
Damien Miller8e7fb332003-02-24 12:03:03 +1100121
122 /* key, cert */
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000123 if ((dh_client_pub = BN_new()) == NULL) {
124 r = SSH_ERR_ALLOC_FAIL;
125 goto out;
126 }
djm@openbsd.org482d23b2018-09-13 02:08:33 +0000127 DH_get0_key(kex->dh, &pub_key, NULL);
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000128 if ((r = sshpkt_get_bignum2(ssh, dh_client_pub)) != 0 ||
129 (r = sshpkt_get_end(ssh)) != 0)
130 goto out;
Damien Miller8e7fb332003-02-24 12:03:03 +1100131
132#ifdef DEBUG_KEXDH
133 fprintf(stderr, "dh_client_pub= ");
134 BN_print_fp(stderr, dh_client_pub);
135 fprintf(stderr, "\n");
136 debug("bits %d", BN_num_bits(dh_client_pub));
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000137 DHparams_print_fp(stderr, kex->dh);
Damien Miller8e7fb332003-02-24 12:03:03 +1100138 fprintf(stderr, "pub= ");
djm@openbsd.org482d23b2018-09-13 02:08:33 +0000139 BN_print_fp(stderr, pub_key);
Damien Miller8e7fb332003-02-24 12:03:03 +1100140 fprintf(stderr, "\n");
141#endif
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000142 if (!dh_pub_is_valid(kex->dh, dh_client_pub)) {
143 sshpkt_disconnect(ssh, "bad client public DH value");
144 r = SSH_ERR_MESSAGE_INCOMPLETE;
145 goto out;
146 }
Damien Miller8e7fb332003-02-24 12:03:03 +1100147
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000148 klen = DH_size(kex->dh);
149 if ((kbuf = malloc(klen)) == NULL ||
150 (shared_secret = BN_new()) == NULL) {
151 r = SSH_ERR_ALLOC_FAIL;
152 goto out;
153 }
154 if ((kout = DH_compute_key(kbuf, dh_client_pub, kex->dh)) < 0 ||
155 BN_bin2bn(kbuf, kout, shared_secret) == NULL) {
156 r = SSH_ERR_LIBCRYPTO_ERROR;
157 goto out;
158 }
Damien Miller8e7fb332003-02-24 12:03:03 +1100159#ifdef DEBUG_KEXDH
160 dump_digest("shared secret", kbuf, kout);
161#endif
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000162 if ((r = sshkey_to_blob(server_host_public, &server_host_key_blob,
163 &sbloblen)) != 0)
164 goto out;
Damien Miller8e7fb332003-02-24 12:03:03 +1100165 /* calc H */
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000166 hashlen = sizeof(hash);
167 if ((r = kex_dh_hash(
djm@openbsd.org0e8eeec2016-05-02 10:26:04 +0000168 kex->hash_alg,
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000169 kex->client_version,
170 kex->server_version,
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000171 sshbuf_ptr(kex->peer), sshbuf_len(kex->peer),
172 sshbuf_ptr(kex->my), sshbuf_len(kex->my),
Damien Miller8e7fb332003-02-24 12:03:03 +1100173 server_host_key_blob, sbloblen,
174 dh_client_pub,
djm@openbsd.org482d23b2018-09-13 02:08:33 +0000175 pub_key,
Damien Miller19bb3a52005-11-05 15:19:35 +1100176 shared_secret,
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000177 hash, &hashlen)) != 0)
178 goto out;
Damien Miller8e7fb332003-02-24 12:03:03 +1100179
180 /* save session id := H */
Damien Miller8e7fb332003-02-24 12:03:03 +1100181 if (kex->session_id == NULL) {
Damien Miller19bb3a52005-11-05 15:19:35 +1100182 kex->session_id_len = hashlen;
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000183 kex->session_id = malloc(kex->session_id_len);
184 if (kex->session_id == NULL) {
185 r = SSH_ERR_ALLOC_FAIL;
186 goto out;
187 }
Damien Miller8e7fb332003-02-24 12:03:03 +1100188 memcpy(kex->session_id, hash, kex->session_id_len);
189 }
190
191 /* sign H */
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000192 if ((r = kex->sign(server_host_private, server_host_public, &signature,
193 &slen, hash, hashlen, kex->hostkey_alg, ssh->compat)) < 0)
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000194 goto out;
Damien Miller8e7fb332003-02-24 12:03:03 +1100195
196 /* destroy_sensitive_data(); */
197
djm@openbsd.org001aa552018-04-10 00:10:49 +0000198 /* send server hostkey, DH pubkey 'f' and signed H */
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000199 if ((r = sshpkt_start(ssh, SSH2_MSG_KEXDH_REPLY)) != 0 ||
200 (r = sshpkt_put_string(ssh, server_host_key_blob, sbloblen)) != 0 ||
djm@openbsd.org482d23b2018-09-13 02:08:33 +0000201 (r = sshpkt_put_bignum2(ssh, pub_key)) != 0 || /* f */
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000202 (r = sshpkt_put_string(ssh, signature, slen)) != 0 ||
203 (r = sshpkt_send(ssh)) != 0)
204 goto out;
Damien Miller8e7fb332003-02-24 12:03:03 +1100205
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000206 if ((r = kex_derive_keys_bn(ssh, hash, hashlen, shared_secret)) == 0)
207 r = kex_send_newkeys(ssh);
208 out:
209 explicit_bzero(hash, sizeof(hash));
210 DH_free(kex->dh);
211 kex->dh = NULL;
jsing@openbsd.org7cd31632018-02-07 02:06:50 +0000212 BN_clear_free(dh_client_pub);
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000213 if (kbuf) {
214 explicit_bzero(kbuf, klen);
215 free(kbuf);
216 }
jsing@openbsd.org7cd31632018-02-07 02:06:50 +0000217 BN_clear_free(shared_secret);
Darren Tuckera627d422013-06-02 07:31:17 +1000218 free(server_host_key_blob);
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000219 free(signature);
220 return r;
Damien Miller8e7fb332003-02-24 12:03:03 +1100221}
Damien Miller72ef7c12015-01-15 02:21:31 +1100222#endif /* WITH_OPENSSL */