GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.bin/ssh/lib/../kexdhs.c Lines: 0 86 0.0 %
Date: 2017-11-07 Branches: 0 60 0.0 %

Line Branch Exec Source
1
/* $OpenBSD: kexdhs.c,v 1.25 2017/05/30 14:23:52 markus Exp $ */
2
/*
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
27
#include <sys/types.h>
28
#include <string.h>
29
#include <signal.h>
30
31
#include <openssl/dh.h>
32
33
#include "sshkey.h"
34
#include "cipher.h"
35
#include "digest.h"
36
#include "kex.h"
37
#include "log.h"
38
#include "packet.h"
39
#include "dh.h"
40
#include "ssh2.h"
41
42
#include "dispatch.h"
43
#include "compat.h"
44
#include "ssherr.h"
45
#include "sshbuf.h"
46
47
static int input_kex_dh_init(int, u_int32_t, struct ssh *);
48
49
int
50
kexdh_server(struct ssh *ssh)
51
{
52
	struct kex *kex = ssh->kex;
53
	int r;
54
55
	/* generate server DH public key */
56
	switch (kex->kex_type) {
57
	case KEX_DH_GRP1_SHA1:
58
		kex->dh = dh_new_group1();
59
		break;
60
	case KEX_DH_GRP14_SHA1:
61
	case KEX_DH_GRP14_SHA256:
62
		kex->dh = dh_new_group14();
63
		break;
64
	case KEX_DH_GRP16_SHA512:
65
		kex->dh = dh_new_group16();
66
		break;
67
	case KEX_DH_GRP18_SHA512:
68
		kex->dh = dh_new_group18();
69
		break;
70
	default:
71
		r = SSH_ERR_INVALID_ARGUMENT;
72
		goto out;
73
	}
74
	if (kex->dh == NULL) {
75
		r = SSH_ERR_ALLOC_FAIL;
76
		goto out;
77
	}
78
	if ((r = dh_gen_key(kex->dh, kex->we_need * 8)) != 0)
79
		goto out;
80
81
	debug("expecting SSH2_MSG_KEXDH_INIT");
82
	ssh_dispatch_set(ssh, SSH2_MSG_KEXDH_INIT, &input_kex_dh_init);
83
	r = 0;
84
 out:
85
	return r;
86
}
87
88
int
89
input_kex_dh_init(int type, u_int32_t seq, struct ssh *ssh)
90
{
91
	struct kex *kex = ssh->kex;
92
	BIGNUM *shared_secret = NULL, *dh_client_pub = NULL;
93
	struct sshkey *server_host_public, *server_host_private;
94
	u_char *kbuf = NULL, *signature = NULL, *server_host_key_blob = NULL;
95
	u_char hash[SSH_DIGEST_MAX_LENGTH];
96
	size_t sbloblen, slen;
97
	size_t klen = 0, hashlen;
98
	int kout, r;
99
100
	if (kex->load_host_public_key == NULL ||
101
	    kex->load_host_private_key == NULL) {
102
		r = SSH_ERR_INVALID_ARGUMENT;
103
		goto out;
104
	}
105
	server_host_public = kex->load_host_public_key(kex->hostkey_type,
106
	    kex->hostkey_nid, ssh);
107
	server_host_private = kex->load_host_private_key(kex->hostkey_type,
108
	    kex->hostkey_nid, ssh);
109
	if (server_host_public == NULL) {
110
		r = SSH_ERR_NO_HOSTKEY_LOADED;
111
		goto out;
112
	}
113
114
	/* key, cert */
115
	if ((dh_client_pub = BN_new()) == NULL) {
116
		r = SSH_ERR_ALLOC_FAIL;
117
		goto out;
118
	}
119
	if ((r = sshpkt_get_bignum2(ssh, dh_client_pub)) != 0 ||
120
	    (r = sshpkt_get_end(ssh)) != 0)
121
		goto out;
122
123
#ifdef DEBUG_KEXDH
124
	fprintf(stderr, "dh_client_pub= ");
125
	BN_print_fp(stderr, dh_client_pub);
126
	fprintf(stderr, "\n");
127
	debug("bits %d", BN_num_bits(dh_client_pub));
128
#endif
129
130
#ifdef DEBUG_KEXDH
131
	DHparams_print_fp(stderr, kex->dh);
132
	fprintf(stderr, "pub= ");
133
	BN_print_fp(stderr, kex->dh->pub_key);
134
	fprintf(stderr, "\n");
135
#endif
136
	if (!dh_pub_is_valid(kex->dh, dh_client_pub)) {
137
		sshpkt_disconnect(ssh, "bad client public DH value");
138
		r = SSH_ERR_MESSAGE_INCOMPLETE;
139
		goto out;
140
	}
141
142
	klen = DH_size(kex->dh);
143
	if ((kbuf = malloc(klen)) == NULL ||
144
	    (shared_secret = BN_new()) == NULL) {
145
		r = SSH_ERR_ALLOC_FAIL;
146
		goto out;
147
	}
148
	if ((kout = DH_compute_key(kbuf, dh_client_pub, kex->dh)) < 0 ||
149
	    BN_bin2bn(kbuf, kout, shared_secret) == NULL) {
150
		r = SSH_ERR_LIBCRYPTO_ERROR;
151
		goto out;
152
	}
153
#ifdef DEBUG_KEXDH
154
	dump_digest("shared secret", kbuf, kout);
155
#endif
156
	if ((r = sshkey_to_blob(server_host_public, &server_host_key_blob,
157
	    &sbloblen)) != 0)
158
		goto out;
159
	/* calc H */
160
	hashlen = sizeof(hash);
161
	if ((r = kex_dh_hash(
162
	    kex->hash_alg,
163
	    kex->client_version_string,
164
	    kex->server_version_string,
165
	    sshbuf_ptr(kex->peer), sshbuf_len(kex->peer),
166
	    sshbuf_ptr(kex->my), sshbuf_len(kex->my),
167
	    server_host_key_blob, sbloblen,
168
	    dh_client_pub,
169
	    kex->dh->pub_key,
170
	    shared_secret,
171
	    hash, &hashlen)) != 0)
172
		goto out;
173
174
	/* save session id := H */
175
	if (kex->session_id == NULL) {
176
		kex->session_id_len = hashlen;
177
		kex->session_id = malloc(kex->session_id_len);
178
		if (kex->session_id == NULL) {
179
			r = SSH_ERR_ALLOC_FAIL;
180
			goto out;
181
		}
182
		memcpy(kex->session_id, hash, kex->session_id_len);
183
	}
184
185
	/* sign H */
186
	if ((r = kex->sign(server_host_private, server_host_public, &signature,
187
	     &slen, hash, hashlen, kex->hostkey_alg, ssh->compat)) < 0)
188
		goto out;
189
190
	/* destroy_sensitive_data(); */
191
192
	/* send server hostkey, DH pubkey 'f' and singed H */
193
	if ((r = sshpkt_start(ssh, SSH2_MSG_KEXDH_REPLY)) != 0 ||
194
	    (r = sshpkt_put_string(ssh, server_host_key_blob, sbloblen)) != 0 ||
195
	    (r = sshpkt_put_bignum2(ssh, kex->dh->pub_key)) != 0 ||	/* f */
196
	    (r = sshpkt_put_string(ssh, signature, slen)) != 0 ||
197
	    (r = sshpkt_send(ssh)) != 0)
198
		goto out;
199
200
	if ((r = kex_derive_keys_bn(ssh, hash, hashlen, shared_secret)) == 0)
201
		r = kex_send_newkeys(ssh);
202
 out:
203
	explicit_bzero(hash, sizeof(hash));
204
	DH_free(kex->dh);
205
	kex->dh = NULL;
206
	if (dh_client_pub)
207
		BN_clear_free(dh_client_pub);
208
	if (kbuf) {
209
		explicit_bzero(kbuf, klen);
210
		free(kbuf);
211
	}
212
	if (shared_secret)
213
		BN_clear_free(shared_secret);
214
	free(server_host_key_blob);
215
	free(signature);
216
	return r;
217
}