blob: de7c05b17249152265f5506c397d5d479ca21aec [file] [log] [blame]
Adam Langleyd0592972015-03-30 14:49:51 -07001/* $OpenBSD: kexdhs.c,v 1.22 2015/01/26 06:10:03 djm Exp $ */
Greg Hartmanbd77cf72015-02-25 13:21:06 -08002/*
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"
27
Adam Langleyd0592972015-03-30 14:49:51 -070028#ifdef WITH_OPENSSL
29
Greg Hartmanbd77cf72015-02-25 13:21:06 -080030#include <sys/types.h>
31
32#include <stdarg.h>
33#include <string.h>
34#include <signal.h>
35
36#include <openssl/dh.h>
37
Adam Langleyd0592972015-03-30 14:49:51 -070038#include "sshkey.h"
Greg Hartmanbd77cf72015-02-25 13:21:06 -080039#include "cipher.h"
Adam Langleyd0592972015-03-30 14:49:51 -070040#include "digest.h"
Greg Hartmanbd77cf72015-02-25 13:21:06 -080041#include "kex.h"
42#include "log.h"
43#include "packet.h"
44#include "dh.h"
45#include "ssh2.h"
Greg Hartmanbd77cf72015-02-25 13:21:06 -080046
Adam Langleyd0592972015-03-30 14:49:51 -070047#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)
Greg Hartmanbd77cf72015-02-25 13:21:06 -080056{
Adam Langleyd0592972015-03-30 14:49:51 -070057 struct kex *kex = ssh->kex;
58 int r;
Greg Hartmanbd77cf72015-02-25 13:21:06 -080059
60 /* generate server DH public key */
61 switch (kex->kex_type) {
62 case KEX_DH_GRP1_SHA1:
Adam Langleyd0592972015-03-30 14:49:51 -070063 kex->dh = dh_new_group1();
Greg Hartmanbd77cf72015-02-25 13:21:06 -080064 break;
65 case KEX_DH_GRP14_SHA1:
Adam Langleyd0592972015-03-30 14:49:51 -070066 kex->dh = dh_new_group14();
Greg Hartmanbd77cf72015-02-25 13:21:06 -080067 break;
68 default:
Adam Langleyd0592972015-03-30 14:49:51 -070069 r = SSH_ERR_INVALID_ARGUMENT;
70 goto out;
Greg Hartmanbd77cf72015-02-25 13:21:06 -080071 }
Adam Langleyd0592972015-03-30 14:49:51 -070072 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;
Greg Hartmanbd77cf72015-02-25 13:21:06 -080078
79 debug("expecting SSH2_MSG_KEXDH_INIT");
Adam Langleyd0592972015-03-30 14:49:51 -070080 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;
Greg Hartmanbd77cf72015-02-25 13:21:06 -080098
99 if (kex->load_host_public_key == NULL ||
Adam Langleyd0592972015-03-30 14:49:51 -0700100 kex->load_host_private_key == NULL) {
101 r = SSH_ERR_INVALID_ARGUMENT;
102 goto out;
103 }
104 server_host_public = kex->load_host_public_key(kex->hostkey_type,
105 kex->hostkey_nid, ssh);
106 server_host_private = kex->load_host_private_key(kex->hostkey_type,
107 kex->hostkey_nid, ssh);
108 if (server_host_public == NULL) {
109 r = SSH_ERR_NO_HOSTKEY_LOADED;
110 goto out;
111 }
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800112
113 /* key, cert */
Adam Langleyd0592972015-03-30 14:49:51 -0700114 if ((dh_client_pub = BN_new()) == NULL) {
115 r = SSH_ERR_ALLOC_FAIL;
116 goto out;
117 }
118 if ((r = sshpkt_get_bignum2(ssh, dh_client_pub)) != 0 ||
119 (r = sshpkt_get_end(ssh)) != 0)
120 goto out;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800121
122#ifdef DEBUG_KEXDH
123 fprintf(stderr, "dh_client_pub= ");
124 BN_print_fp(stderr, dh_client_pub);
125 fprintf(stderr, "\n");
126 debug("bits %d", BN_num_bits(dh_client_pub));
127#endif
128
129#ifdef DEBUG_KEXDH
Adam Langleyd0592972015-03-30 14:49:51 -0700130 DHparams_print_fp(stderr, kex->dh);
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800131 fprintf(stderr, "pub= ");
Adam Langleyd0592972015-03-30 14:49:51 -0700132 BN_print_fp(stderr, kex->dh->pub_key);
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800133 fprintf(stderr, "\n");
134#endif
Adam Langleyd0592972015-03-30 14:49:51 -0700135 if (!dh_pub_is_valid(kex->dh, dh_client_pub)) {
136 sshpkt_disconnect(ssh, "bad client public DH value");
137 r = SSH_ERR_MESSAGE_INCOMPLETE;
138 goto out;
139 }
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800140
Adam Langleyd0592972015-03-30 14:49:51 -0700141 klen = DH_size(kex->dh);
142 if ((kbuf = malloc(klen)) == NULL ||
143 (shared_secret = BN_new()) == NULL) {
144 r = SSH_ERR_ALLOC_FAIL;
145 goto out;
146 }
147 if ((kout = DH_compute_key(kbuf, dh_client_pub, kex->dh)) < 0 ||
148 BN_bin2bn(kbuf, kout, shared_secret) == NULL) {
149 r = SSH_ERR_LIBCRYPTO_ERROR;
150 goto out;
151 }
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800152#ifdef DEBUG_KEXDH
153 dump_digest("shared secret", kbuf, kout);
154#endif
Adam Langleyd0592972015-03-30 14:49:51 -0700155 if ((r = sshkey_to_blob(server_host_public, &server_host_key_blob,
156 &sbloblen)) != 0)
157 goto out;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800158 /* calc H */
Adam Langleyd0592972015-03-30 14:49:51 -0700159 hashlen = sizeof(hash);
160 if ((r = kex_dh_hash(
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800161 kex->client_version_string,
162 kex->server_version_string,
Adam Langleyd0592972015-03-30 14:49:51 -0700163 sshbuf_ptr(kex->peer), sshbuf_len(kex->peer),
164 sshbuf_ptr(kex->my), sshbuf_len(kex->my),
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800165 server_host_key_blob, sbloblen,
166 dh_client_pub,
Adam Langleyd0592972015-03-30 14:49:51 -0700167 kex->dh->pub_key,
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800168 shared_secret,
Adam Langleyd0592972015-03-30 14:49:51 -0700169 hash, &hashlen)) != 0)
170 goto out;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800171
172 /* save session id := H */
173 if (kex->session_id == NULL) {
174 kex->session_id_len = hashlen;
Adam Langleyd0592972015-03-30 14:49:51 -0700175 kex->session_id = malloc(kex->session_id_len);
176 if (kex->session_id == NULL) {
177 r = SSH_ERR_ALLOC_FAIL;
178 goto out;
179 }
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800180 memcpy(kex->session_id, hash, kex->session_id_len);
181 }
182
183 /* sign H */
Adam Langleyd0592972015-03-30 14:49:51 -0700184 if ((r = kex->sign(server_host_private, server_host_public,
185 &signature, &slen, hash, hashlen, ssh->compat)) < 0)
186 goto out;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800187
188 /* destroy_sensitive_data(); */
189
190 /* send server hostkey, DH pubkey 'f' and singed H */
Adam Langleyd0592972015-03-30 14:49:51 -0700191 if ((r = sshpkt_start(ssh, SSH2_MSG_KEXDH_REPLY)) != 0 ||
192 (r = sshpkt_put_string(ssh, server_host_key_blob, sbloblen)) != 0 ||
193 (r = sshpkt_put_bignum2(ssh, kex->dh->pub_key)) != 0 || /* f */
194 (r = sshpkt_put_string(ssh, signature, slen)) != 0 ||
195 (r = sshpkt_send(ssh)) != 0)
196 goto out;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800197
Adam Langleyd0592972015-03-30 14:49:51 -0700198 if ((r = kex_derive_keys_bn(ssh, hash, hashlen, shared_secret)) == 0)
199 r = kex_send_newkeys(ssh);
200 out:
201 explicit_bzero(hash, sizeof(hash));
202 DH_free(kex->dh);
203 kex->dh = NULL;
204 if (dh_client_pub)
205 BN_clear_free(dh_client_pub);
206 if (kbuf) {
207 explicit_bzero(kbuf, klen);
208 free(kbuf);
209 }
210 if (shared_secret)
211 BN_clear_free(shared_secret);
212 free(server_host_key_blob);
213 free(signature);
214 return r;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800215}
Adam Langleyd0592972015-03-30 14:49:51 -0700216#endif /* WITH_OPENSSL */