blob: 52b7522956d87e27e9765956e115a18327c8ff15 [file] [log] [blame]
markus@openbsd.org57d10cb2015-01-19 20:16:15 +00001/* $OpenBSD: kexdhc.c,v 1.17 2015/01/19 20:16:15 markus 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>
31
Damien Miller4499f4c2010-11-20 15:15:49 +110032#include <openssl/dh.h>
33
Damien Millerded319c2006-09-01 15:38:36 +100034#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100035#include <stdio.h>
Damien Millere3476ed2006-07-24 14:13:33 +100036#include <string.h>
Damien Millerd7834352006-08-05 12:39:39 +100037#include <signal.h>
Damien Millere3476ed2006-07-24 14:13:33 +100038
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000039#include "sshkey.h"
Damien Millerd7834352006-08-05 12:39:39 +100040#include "cipher.h"
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000041#include "digest.h"
Damien Miller8e7fb332003-02-24 12:03:03 +110042#include "kex.h"
43#include "log.h"
44#include "packet.h"
45#include "dh.h"
46#include "ssh2.h"
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000047#include "dispatch.h"
48#include "compat.h"
49#include "ssherr.h"
50#include "sshbuf.h"
Damien Miller8e7fb332003-02-24 12:03:03 +110051
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000052static int input_kex_dh(int, u_int32_t, void *);
53
54int
55kexdh_client(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 and send 'e', client 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 }
Damien Miller8e7fb332003-02-24 12:03:03 +110076 debug("sending SSH2_MSG_KEXDH_INIT");
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000077 if ((r = dh_gen_key(kex->dh, kex->we_need * 8)) != 0 ||
78 (r = sshpkt_start(ssh, SSH2_MSG_KEXDH_INIT)) != 0 ||
79 (r = sshpkt_put_bignum2(ssh, kex->dh->pub_key)) != 0 ||
80 (r = sshpkt_send(ssh)) != 0)
81 goto out;
Damien Miller8e7fb332003-02-24 12:03:03 +110082#ifdef DEBUG_KEXDH
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000083 DHparams_print_fp(stderr, kex->dh);
Damien Miller8e7fb332003-02-24 12:03:03 +110084 fprintf(stderr, "pub= ");
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000085 BN_print_fp(stderr, kex->dh->pub_key);
Damien Miller8e7fb332003-02-24 12:03:03 +110086 fprintf(stderr, "\n");
87#endif
Damien Miller8e7fb332003-02-24 12:03:03 +110088 debug("expecting SSH2_MSG_KEXDH_REPLY");
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000089 ssh_dispatch_set(ssh, SSH2_MSG_KEXDH_REPLY, &input_kex_dh);
90 r = 0;
91 out:
92 return r;
93}
Damien Miller8e7fb332003-02-24 12:03:03 +110094
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000095static int
96input_kex_dh(int type, u_int32_t seq, void *ctxt)
97{
98 struct ssh *ssh = ctxt;
99 struct kex *kex = ssh->kex;
100 BIGNUM *dh_server_pub = NULL, *shared_secret = NULL;
101 struct sshkey *server_host_key = NULL;
102 u_char *kbuf = NULL, *server_host_key_blob = NULL, *signature = NULL;
103 u_char hash[SSH_DIGEST_MAX_LENGTH];
104 size_t klen = 0, slen, sbloblen, hashlen;
105 int kout, r;
106
107 if (kex->verify_host_key == NULL) {
108 r = SSH_ERR_INVALID_ARGUMENT;
109 goto out;
110 }
Damien Miller8e7fb332003-02-24 12:03:03 +1100111 /* key, cert */
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000112 if ((r = sshpkt_get_string(ssh, &server_host_key_blob,
113 &sbloblen)) != 0 ||
114 (r = sshkey_from_blob(server_host_key_blob, sbloblen,
115 &server_host_key)) != 0)
116 goto out;
117 if (server_host_key->type != kex->hostkey_type) {
118 r = SSH_ERR_KEY_TYPE_MISMATCH;
119 goto out;
120 }
121 if (kex->verify_host_key(server_host_key, ssh) == -1) {
122 r = SSH_ERR_SIGNATURE_INVALID;
123 goto out;
124 }
Damien Millerad6b14d2006-06-13 13:00:41 +1000125 /* DH parameter f, server public DH key */
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000126 if ((dh_server_pub = BN_new()) == NULL) {
127 r = SSH_ERR_ALLOC_FAIL;
128 goto out;
129 }
130 /* signed H */
131 if ((r = sshpkt_get_bignum2(ssh, dh_server_pub)) != 0 ||
132 (r = sshpkt_get_string(ssh, &signature, &slen)) != 0 ||
133 (r = sshpkt_get_end(ssh)) != 0)
134 goto out;
Damien Miller8e7fb332003-02-24 12:03:03 +1100135#ifdef DEBUG_KEXDH
136 fprintf(stderr, "dh_server_pub= ");
137 BN_print_fp(stderr, dh_server_pub);
138 fprintf(stderr, "\n");
139 debug("bits %d", BN_num_bits(dh_server_pub));
140#endif
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000141 if (!dh_pub_is_valid(kex->dh, dh_server_pub)) {
142 sshpkt_disconnect(ssh, "bad server public DH value");
143 r = SSH_ERR_MESSAGE_INCOMPLETE;
144 goto out;
145 }
Damien Miller8e7fb332003-02-24 12:03:03 +1100146
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000147 klen = DH_size(kex->dh);
148 if ((kbuf = malloc(klen)) == NULL ||
149 (shared_secret = BN_new()) == NULL) {
150 r = SSH_ERR_ALLOC_FAIL;
151 goto out;
152 }
153 if ((kout = DH_compute_key(kbuf, dh_server_pub, kex->dh)) < 0 ||
154 BN_bin2bn(kbuf, kout, shared_secret) == NULL) {
155 r = SSH_ERR_LIBCRYPTO_ERROR;
156 goto out;
157 }
Damien Miller8e7fb332003-02-24 12:03:03 +1100158#ifdef DEBUG_KEXDH
159 dump_digest("shared secret", kbuf, kout);
160#endif
Damien Miller8e7fb332003-02-24 12:03:03 +1100161
162 /* calc and verify H */
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000163 hashlen = sizeof(hash);
164 if ((r = kex_dh_hash(
Damien Miller8e7fb332003-02-24 12:03:03 +1100165 kex->client_version_string,
166 kex->server_version_string,
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000167 sshbuf_ptr(kex->my), sshbuf_len(kex->my),
168 sshbuf_ptr(kex->peer), sshbuf_len(kex->peer),
Damien Miller8e7fb332003-02-24 12:03:03 +1100169 server_host_key_blob, sbloblen,
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000170 kex->dh->pub_key,
Damien Miller8e7fb332003-02-24 12:03:03 +1100171 dh_server_pub,
Damien Miller19bb3a52005-11-05 15:19:35 +1100172 shared_secret,
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000173 hash, &hashlen)) != 0)
174 goto out;
Damien Miller8e7fb332003-02-24 12:03:03 +1100175
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000176 if ((r = sshkey_verify(server_host_key, signature, slen, hash, hashlen,
177 ssh->compat)) != 0)
178 goto out;
Damien Miller8e7fb332003-02-24 12:03:03 +1100179
180 /* save session id */
181 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
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000191 if ((r = kex_derive_keys_bn(ssh, hash, hashlen, shared_secret)) == 0)
192 r = kex_send_newkeys(ssh);
193 out:
194 explicit_bzero(hash, sizeof(hash));
195 DH_free(kex->dh);
196 kex->dh = NULL;
197 if (dh_server_pub)
198 BN_clear_free(dh_server_pub);
199 if (kbuf) {
200 explicit_bzero(kbuf, klen);
201 free(kbuf);
202 }
203 if (shared_secret)
204 BN_clear_free(shared_secret);
205 sshkey_free(server_host_key);
206 free(server_host_key_blob);
207 free(signature);
208 return r;
Damien Miller8e7fb332003-02-24 12:03:03 +1100209}
Damien Miller72ef7c12015-01-15 02:21:31 +1100210#endif /* WITH_OPENSSL */