blob: a2af8cb08fc1436373a224626d571856ae4176c7 [file] [log] [blame]
djm@openbsd.orgb1b2ff42019-01-21 10:07:22 +00001/* $OpenBSD: kexdhc.c,v 1.29 2019/01/21 10:07:22 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>
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
Damien Miller48f54b92018-09-13 12:13:50 +100039#include "openbsd-compat/openssl-compat.h"
40
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000041#include "sshkey.h"
Damien Millerd7834352006-08-05 12:39:39 +100042#include "cipher.h"
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000043#include "digest.h"
djm@openbsd.orge93bd982019-01-21 10:00:23 +000044#include "dh.h"
Damien Miller8e7fb332003-02-24 12:03:03 +110045#include "kex.h"
46#include "log.h"
47#include "packet.h"
Damien Miller8e7fb332003-02-24 12:03:03 +110048#include "ssh2.h"
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000049#include "dispatch.h"
50#include "compat.h"
51#include "ssherr.h"
52#include "sshbuf.h"
Damien Miller8e7fb332003-02-24 12:03:03 +110053
markus@openbsd.org2ae666a2017-05-30 14:23:52 +000054static int input_kex_dh(int, u_int32_t, struct ssh *);
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000055
56int
57kexdh_client(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;
djm@openbsd.org482d23b2018-09-13 02:08:33 +000061 const BIGNUM *pub_key;
Damien Miller8e7fb332003-02-24 12:03:03 +110062
63 /* generate and send 'e', client DH public key */
djm@openbsd.orge93bd982019-01-21 10:00:23 +000064 if ((r = kex_dh_keygen(kex)) != 0)
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000065 goto out;
Damien Miller8e7fb332003-02-24 12:03:03 +110066 debug("sending SSH2_MSG_KEXDH_INIT");
djm@openbsd.org482d23b2018-09-13 02:08:33 +000067 DH_get0_key(kex->dh, &pub_key, NULL);
68 if ((r = sshpkt_start(ssh, SSH2_MSG_KEXDH_INIT)) != 0 ||
69 (r = sshpkt_put_bignum2(ssh, pub_key)) != 0 ||
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000070 (r = sshpkt_send(ssh)) != 0)
71 goto out;
Damien Miller8e7fb332003-02-24 12:03:03 +110072#ifdef DEBUG_KEXDH
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000073 DHparams_print_fp(stderr, kex->dh);
Damien Miller8e7fb332003-02-24 12:03:03 +110074 fprintf(stderr, "pub= ");
djm@openbsd.org482d23b2018-09-13 02:08:33 +000075 BN_print_fp(stderr, pub_key);
Damien Miller8e7fb332003-02-24 12:03:03 +110076 fprintf(stderr, "\n");
77#endif
Damien Miller8e7fb332003-02-24 12:03:03 +110078 debug("expecting SSH2_MSG_KEXDH_REPLY");
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000079 ssh_dispatch_set(ssh, SSH2_MSG_KEXDH_REPLY, &input_kex_dh);
80 r = 0;
81 out:
82 return r;
83}
Damien Miller8e7fb332003-02-24 12:03:03 +110084
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000085static int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +000086input_kex_dh(int type, u_int32_t seq, struct ssh *ssh)
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000087{
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000088 struct kex *kex = ssh->kex;
djm@openbsd.orgdec5e9d2019-01-21 10:03:37 +000089 BIGNUM *dh_server_pub = NULL;
djm@openbsd.org482d23b2018-09-13 02:08:33 +000090 const BIGNUM *pub_key;
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000091 struct sshkey *server_host_key = NULL;
djm@openbsd.orgdec5e9d2019-01-21 10:03:37 +000092 struct sshbuf *shared_secret = NULL;
93 u_char *server_host_key_blob = NULL, *signature = NULL;
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000094 u_char hash[SSH_DIGEST_MAX_LENGTH];
djm@openbsd.orgdec5e9d2019-01-21 10:03:37 +000095 size_t slen, sbloblen, hashlen;
96 int r;
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000097
Damien Miller8e7fb332003-02-24 12:03:03 +110098 /* key, cert */
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000099 if ((r = sshpkt_get_string(ssh, &server_host_key_blob,
100 &sbloblen)) != 0 ||
101 (r = sshkey_from_blob(server_host_key_blob, sbloblen,
102 &server_host_key)) != 0)
103 goto out;
djm@openbsd.orgb1b2ff42019-01-21 10:07:22 +0000104 if ((r = kex_verify_host_key(ssh, server_host_key)) != 0)
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000105 goto out;
djm@openbsd.org7be85722019-01-21 09:54:11 +0000106 /* DH parameter f, server public DH key, signed H */
107 if ((r = sshpkt_get_bignum2(ssh, &dh_server_pub)) != 0 ||
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000108 (r = sshpkt_get_string(ssh, &signature, &slen)) != 0 ||
109 (r = sshpkt_get_end(ssh)) != 0)
110 goto out;
djm@openbsd.orgdec5e9d2019-01-21 10:03:37 +0000111 if ((shared_secret = sshbuf_new()) == NULL) {
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000112 r = SSH_ERR_ALLOC_FAIL;
113 goto out;
114 }
djm@openbsd.orgdec5e9d2019-01-21 10:03:37 +0000115 if ((r = kex_dh_compute_key(kex, dh_server_pub, shared_secret)) != 0)
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000116 goto out;
Damien Miller8e7fb332003-02-24 12:03:03 +1100117
118 /* calc and verify H */
djm@openbsd.org482d23b2018-09-13 02:08:33 +0000119 DH_get0_key(kex->dh, &pub_key, NULL);
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000120 hashlen = sizeof(hash);
121 if ((r = kex_dh_hash(
djm@openbsd.org0e8eeec2016-05-02 10:26:04 +0000122 kex->hash_alg,
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000123 kex->client_version,
124 kex->server_version,
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000125 sshbuf_ptr(kex->my), sshbuf_len(kex->my),
126 sshbuf_ptr(kex->peer), sshbuf_len(kex->peer),
Damien Miller8e7fb332003-02-24 12:03:03 +1100127 server_host_key_blob, sbloblen,
djm@openbsd.org482d23b2018-09-13 02:08:33 +0000128 pub_key,
Damien Miller8e7fb332003-02-24 12:03:03 +1100129 dh_server_pub,
djm@openbsd.orgdec5e9d2019-01-21 10:03:37 +0000130 sshbuf_ptr(shared_secret), sshbuf_len(shared_secret),
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000131 hash, &hashlen)) != 0)
132 goto out;
Damien Miller8e7fb332003-02-24 12:03:03 +1100133
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000134 if ((r = sshkey_verify(server_host_key, signature, slen, hash, hashlen,
djm@openbsd.org04c7e282017-12-18 02:25:15 +0000135 kex->hostkey_alg, ssh->compat)) != 0)
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000136 goto out;
Damien Miller8e7fb332003-02-24 12:03:03 +1100137
djm@openbsd.orgdec5e9d2019-01-21 10:03:37 +0000138 if ((r = kex_derive_keys(ssh, hash, hashlen, shared_secret)) == 0)
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000139 r = kex_send_newkeys(ssh);
140 out:
141 explicit_bzero(hash, sizeof(hash));
142 DH_free(kex->dh);
143 kex->dh = NULL;
jsing@openbsd.org7cd31632018-02-07 02:06:50 +0000144 BN_clear_free(dh_server_pub);
djm@openbsd.orgdec5e9d2019-01-21 10:03:37 +0000145 sshbuf_free(shared_secret);
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000146 sshkey_free(server_host_key);
147 free(server_host_key_blob);
148 free(signature);
149 return r;
Damien Miller8e7fb332003-02-24 12:03:03 +1100150}
Damien Miller72ef7c12015-01-15 02:21:31 +1100151#endif /* WITH_OPENSSL */