xref: /openbsd-src/usr.bin/ssh/ssh_api.c (revision c90a81c56dcebd6a1b73fe4aff9b03385b8e63b3)
1 /* $OpenBSD: ssh_api.c,v 1.9 2018/12/27 03:25:25 djm Exp $ */
2 /*
3  * Copyright (c) 2012 Markus Friedl.  All rights reserved.
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 #include "ssh_api.h"
19 #include "compat.h"
20 #include "log.h"
21 #include "authfile.h"
22 #include "sshkey.h"
23 #include "misc.h"
24 #include "ssh2.h"
25 #include "version.h"
26 #include "myproposal.h"
27 #include "ssherr.h"
28 #include "sshbuf.h"
29 
30 #include <string.h>
31 
32 int	_ssh_exchange_banner(struct ssh *);
33 int	_ssh_send_banner(struct ssh *, struct sshbuf *);
34 int	_ssh_read_banner(struct ssh *, struct sshbuf *);
35 int	_ssh_order_hostkeyalgs(struct ssh *);
36 int	_ssh_verify_host_key(struct sshkey *, struct ssh *);
37 struct sshkey *_ssh_host_public_key(int, int, struct ssh *);
38 struct sshkey *_ssh_host_private_key(int, int, struct ssh *);
39 int	_ssh_host_key_sign(struct sshkey *, struct sshkey *,
40     u_char **, size_t *, const u_char *, size_t, const char *, u_int);
41 
42 /*
43  * stubs for the server side implementation of kex.
44  * disable privsep so our stubs will never be called.
45  */
46 int	use_privsep = 0;
47 int	mm_sshkey_sign(struct sshkey *, u_char **, u_int *,
48     u_char *, u_int, char *, u_int);
49 DH	*mm_choose_dh(int, int, int);
50 
51 /* Define these two variables here so that they are part of the library */
52 u_char *session_id2 = NULL;
53 u_int session_id2_len = 0;
54 
55 int
56 mm_sshkey_sign(struct sshkey *key, u_char **sigp, u_int *lenp,
57     u_char *data, u_int datalen, char *alg, u_int compat)
58 {
59 	return (-1);
60 }
61 
62 DH *
63 mm_choose_dh(int min, int nbits, int max)
64 {
65 	return (NULL);
66 }
67 
68 /* API */
69 
70 int
71 ssh_init(struct ssh **sshp, int is_server, struct kex_params *kex_params)
72 {
73         char *myproposal[PROPOSAL_MAX] = { KEX_CLIENT };
74 	struct ssh *ssh;
75 	char **proposal;
76 	static int called;
77 	int r;
78 
79 	if (!called) {
80 		OpenSSL_add_all_algorithms();
81 		called = 1;
82 	}
83 
84 	if ((ssh = ssh_packet_set_connection(NULL, -1, -1)) == NULL)
85 		return SSH_ERR_ALLOC_FAIL;
86 	if (is_server)
87 		ssh_packet_set_server(ssh);
88 
89 	/* Initialize key exchange */
90 	proposal = kex_params ? kex_params->proposal : myproposal;
91 	if ((r = kex_ready(ssh, proposal)) != 0) {
92 		ssh_free(ssh);
93 		return r;
94 	}
95 	ssh->kex->server = is_server;
96 	if (is_server) {
97 #ifdef WITH_OPENSSL
98 		ssh->kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
99 		ssh->kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
100 		ssh->kex->kex[KEX_DH_GRP14_SHA256] = kexdh_server;
101 		ssh->kex->kex[KEX_DH_GRP16_SHA512] = kexdh_server;
102 		ssh->kex->kex[KEX_DH_GRP18_SHA512] = kexdh_server;
103 		ssh->kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
104 		ssh->kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
105 		ssh->kex->kex[KEX_ECDH_SHA2] = kexecdh_server;
106 #endif /* WITH_OPENSSL */
107 		ssh->kex->kex[KEX_C25519_SHA256] = kexc25519_server;
108 		ssh->kex->load_host_public_key=&_ssh_host_public_key;
109 		ssh->kex->load_host_private_key=&_ssh_host_private_key;
110 		ssh->kex->sign=&_ssh_host_key_sign;
111 	} else {
112 #ifdef WITH_OPENSSL
113 		ssh->kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client;
114 		ssh->kex->kex[KEX_DH_GRP14_SHA1] = kexdh_client;
115 		ssh->kex->kex[KEX_DH_GRP14_SHA256] = kexdh_client;
116 		ssh->kex->kex[KEX_DH_GRP16_SHA512] = kexdh_client;
117 		ssh->kex->kex[KEX_DH_GRP18_SHA512] = kexdh_client;
118 		ssh->kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;
119 		ssh->kex->kex[KEX_DH_GEX_SHA256] = kexgex_client;
120 		ssh->kex->kex[KEX_ECDH_SHA2] = kexecdh_client;
121 #endif /* WITH_OPENSSL */
122 		ssh->kex->kex[KEX_C25519_SHA256] = kexc25519_client;
123 		ssh->kex->verify_host_key =&_ssh_verify_host_key;
124 	}
125 	*sshp = ssh;
126 	return 0;
127 }
128 
129 void
130 ssh_free(struct ssh *ssh)
131 {
132 	struct key_entry *k;
133 
134 	ssh_packet_close(ssh);
135 	/*
136 	 * we've only created the public keys variants in case we
137 	 * are a acting as a server.
138 	 */
139 	while ((k = TAILQ_FIRST(&ssh->public_keys)) != NULL) {
140 		TAILQ_REMOVE(&ssh->public_keys, k, next);
141 		if (ssh->kex && ssh->kex->server)
142 			sshkey_free(k->key);
143 		free(k);
144 	}
145 	while ((k = TAILQ_FIRST(&ssh->private_keys)) != NULL) {
146 		TAILQ_REMOVE(&ssh->private_keys, k, next);
147 		free(k);
148 	}
149 	if (ssh->kex)
150 		kex_free(ssh->kex);
151 	free(ssh);
152 }
153 
154 void
155 ssh_set_app_data(struct ssh *ssh, void *app_data)
156 {
157 	ssh->app_data = app_data;
158 }
159 
160 void *
161 ssh_get_app_data(struct ssh *ssh)
162 {
163 	return ssh->app_data;
164 }
165 
166 /* Returns < 0 on error, 0 otherwise */
167 int
168 ssh_add_hostkey(struct ssh *ssh, struct sshkey *key)
169 {
170 	struct sshkey *pubkey = NULL;
171 	struct key_entry *k = NULL, *k_prv = NULL;
172 	int r;
173 
174 	if (ssh->kex->server) {
175 		if ((r = sshkey_from_private(key, &pubkey)) != 0)
176 			return r;
177 		if ((k = malloc(sizeof(*k))) == NULL ||
178 		    (k_prv = malloc(sizeof(*k_prv))) == NULL) {
179 			free(k);
180 			sshkey_free(pubkey);
181 			return SSH_ERR_ALLOC_FAIL;
182 		}
183 		k_prv->key = key;
184 		TAILQ_INSERT_TAIL(&ssh->private_keys, k_prv, next);
185 
186 		/* add the public key, too */
187 		k->key = pubkey;
188 		TAILQ_INSERT_TAIL(&ssh->public_keys, k, next);
189 		r = 0;
190 	} else {
191 		if ((k = malloc(sizeof(*k))) == NULL)
192 			return SSH_ERR_ALLOC_FAIL;
193 		k->key = key;
194 		TAILQ_INSERT_TAIL(&ssh->public_keys, k, next);
195 		r = 0;
196 	}
197 
198 	return r;
199 }
200 
201 int
202 ssh_set_verify_host_key_callback(struct ssh *ssh,
203     int (*cb)(struct sshkey *, struct ssh *))
204 {
205 	if (cb == NULL || ssh->kex == NULL)
206 		return SSH_ERR_INVALID_ARGUMENT;
207 
208 	ssh->kex->verify_host_key = cb;
209 
210 	return 0;
211 }
212 
213 int
214 ssh_input_append(struct ssh *ssh, const u_char *data, size_t len)
215 {
216 	return sshbuf_put(ssh_packet_get_input(ssh), data, len);
217 }
218 
219 int
220 ssh_packet_next(struct ssh *ssh, u_char *typep)
221 {
222 	int r;
223 	u_int32_t seqnr;
224 	u_char type;
225 
226 	/*
227 	 * Try to read a packet. Return SSH_MSG_NONE if no packet or not
228 	 * enough data.
229 	 */
230 	*typep = SSH_MSG_NONE;
231 	if (sshbuf_len(ssh->kex->client_version) == 0 ||
232 	    sshbuf_len(ssh->kex->server_version) == 0)
233 		return _ssh_exchange_banner(ssh);
234 	/*
235 	 * If we enough data and a dispatch function then
236 	 * call the function and get the next packet.
237 	 * Otherwise return the packet type to the caller so it
238 	 * can decide how to go on.
239 	 *
240 	 * We will only call the dispatch function for:
241 	 *     20-29    Algorithm negotiation
242 	 *     30-49    Key exchange method specific (numbers can be reused for
243 	 *              different authentication methods)
244 	 */
245 	for (;;) {
246 		if ((r = ssh_packet_read_poll2(ssh, &type, &seqnr)) != 0)
247 			return r;
248 		if (type > 0 && type < DISPATCH_MAX &&
249 		    type >= SSH2_MSG_KEXINIT && type <= SSH2_MSG_TRANSPORT_MAX &&
250 		    ssh->dispatch[type] != NULL) {
251 			if ((r = (*ssh->dispatch[type])(type, seqnr, ssh)) != 0)
252 				return r;
253 		} else {
254 			*typep = type;
255 			return 0;
256 		}
257 	}
258 }
259 
260 const u_char *
261 ssh_packet_payload(struct ssh *ssh, size_t *lenp)
262 {
263 	return sshpkt_ptr(ssh, lenp);
264 }
265 
266 int
267 ssh_packet_put(struct ssh *ssh, int type, const u_char *data, size_t len)
268 {
269 	int r;
270 
271 	if ((r = sshpkt_start(ssh, type)) != 0 ||
272 	    (r = sshpkt_put(ssh, data, len)) != 0 ||
273 	    (r = sshpkt_send(ssh)) != 0)
274 		return r;
275 	return 0;
276 }
277 
278 const u_char *
279 ssh_output_ptr(struct ssh *ssh, size_t *len)
280 {
281 	struct sshbuf *output = ssh_packet_get_output(ssh);
282 
283 	*len = sshbuf_len(output);
284 	return sshbuf_ptr(output);
285 }
286 
287 int
288 ssh_output_consume(struct ssh *ssh, size_t len)
289 {
290 	return sshbuf_consume(ssh_packet_get_output(ssh), len);
291 }
292 
293 int
294 ssh_output_space(struct ssh *ssh, size_t len)
295 {
296 	return (0 == sshbuf_check_reserve(ssh_packet_get_output(ssh), len));
297 }
298 
299 int
300 ssh_input_space(struct ssh *ssh, size_t len)
301 {
302 	return (0 == sshbuf_check_reserve(ssh_packet_get_input(ssh), len));
303 }
304 
305 /* Read other side's version identification. */
306 int
307 _ssh_read_banner(struct ssh *ssh, struct sshbuf *banner)
308 {
309 	struct sshbuf *input = ssh_packet_get_input(ssh);
310 	const char *mismatch = "Protocol mismatch.\r\n";
311 	const u_char *s = sshbuf_ptr(input);
312 	u_char c;
313 	char *cp, *remote_version;
314 	int r, remote_major, remote_minor, expect_nl;
315 	size_t n, j;
316 
317 	for (j = n = 0;;) {
318 		sshbuf_reset(banner);
319 		expect_nl = 0;
320 		for (;;) {
321 			if (j >= sshbuf_len(input))
322 				return 0; /* insufficient data in input buf */
323 			c = s[j++];
324 			if (c == '\r') {
325 				expect_nl = 1;
326 				continue;
327 			}
328 			if (c == '\n')
329 				break;
330 			if (expect_nl)
331 				goto bad;
332 			if ((r = sshbuf_put_u8(banner, c)) != 0)
333 				return r;
334 			if (sshbuf_len(banner) > SSH_MAX_BANNER_LEN)
335 				goto bad;
336 		}
337 		if (sshbuf_len(banner) >= 4 &&
338 		    memcmp(sshbuf_ptr(banner), "SSH-", 4) == 0)
339 			break;
340 		if ((cp = sshbuf_dup_string(banner)) == NULL)
341 			return SSH_ERR_ALLOC_FAIL;
342 		debug("%s: %s", __func__, cp);
343 		free(cp);
344 		/* Accept lines before banner only on client */
345 		if (ssh->kex->server || ++n > SSH_MAX_PRE_BANNER_LINES) {
346   bad:
347 			if ((r = sshbuf_put(ssh_packet_get_output(ssh),
348 			   mismatch, strlen(mismatch))) != 0)
349 				return r;
350 			return SSH_ERR_NO_PROTOCOL_VERSION;
351 		}
352 	}
353 	if ((r = sshbuf_consume(input, j)) != 0)
354 		return r;
355 
356 	if ((cp = sshbuf_dup_string(banner)) == NULL)
357 		return SSH_ERR_ALLOC_FAIL;
358 	/* XXX remote version must be the same size as banner for sscanf */
359 	if ((remote_version = calloc(1, sshbuf_len(banner))) == NULL)
360 		return SSH_ERR_ALLOC_FAIL;
361 
362 	/*
363 	 * Check that the versions match.  In future this might accept
364 	 * several versions and set appropriate flags to handle them.
365 	 */
366 	if (sscanf(cp, "SSH-%d.%d-%[^\n]\n",
367 	    &remote_major, &remote_minor, remote_version) != 3)
368 		return SSH_ERR_INVALID_FORMAT;
369 	debug("Remote protocol version %d.%d, remote software version %.100s",
370 	    remote_major, remote_minor, remote_version);
371 
372 	ssh->compat = compat_datafellows(remote_version);
373 	if  (remote_major == 1 && remote_minor == 99) {
374 		remote_major = 2;
375 		remote_minor = 0;
376 	}
377 	if (remote_major != 2)
378 		return SSH_ERR_PROTOCOL_MISMATCH;
379 	debug("Remote version string %.100s", cp);
380 	free(cp);
381 	return 0;
382 }
383 
384 /* Send our own protocol version identification. */
385 int
386 _ssh_send_banner(struct ssh *ssh, struct sshbuf *banner)
387 {
388 	char *cp;
389 	int r;
390 
391 	if ((r = sshbuf_putf(banner, "SSH-2.0-%.100s\r\n", SSH_VERSION)) != 0)
392 		return r;
393 	if ((r = sshbuf_putb(ssh_packet_get_output(ssh), banner)) != 0)
394 		return r;
395 	/* Remove trailing \r\n */
396 	if ((r = sshbuf_consume_end(banner, 2)) != 0)
397 		return r;
398 	if ((cp = sshbuf_dup_string(banner)) == NULL)
399 		return SSH_ERR_ALLOC_FAIL;
400 	debug("Local version string %.100s", cp);
401 	free(cp);
402 	return 0;
403 }
404 
405 int
406 _ssh_exchange_banner(struct ssh *ssh)
407 {
408 	struct kex *kex = ssh->kex;
409 	int r;
410 
411 	/*
412 	 * if _ssh_read_banner() cannot parse a full version string
413 	 * it will return NULL and we end up calling it again.
414 	 */
415 
416 	r = 0;
417 	if (kex->server) {
418 		if (sshbuf_len(ssh->kex->server_version) == 0)
419 			r = _ssh_send_banner(ssh, ssh->kex->server_version);
420 		if (r == 0 &&
421 		    sshbuf_len(ssh->kex->server_version) != 0 &&
422 		    sshbuf_len(ssh->kex->client_version) == 0)
423 			r = _ssh_read_banner(ssh, ssh->kex->client_version);
424 	} else {
425 		if (sshbuf_len(ssh->kex->server_version) == 0)
426 			r = _ssh_read_banner(ssh, ssh->kex->server_version);
427 		if (r == 0 &&
428 		    sshbuf_len(ssh->kex->server_version) != 0 &&
429 		    sshbuf_len(ssh->kex->client_version) == 0)
430 			r = _ssh_send_banner(ssh, ssh->kex->client_version);
431 	}
432 	if (r != 0)
433 		return r;
434 	/* start initial kex as soon as we have exchanged the banners */
435 	if (sshbuf_len(ssh->kex->server_version) != 0 &&
436 	    sshbuf_len(ssh->kex->client_version) != 0) {
437 		if ((r = _ssh_order_hostkeyalgs(ssh)) != 0 ||
438 		    (r = kex_send_kexinit(ssh)) != 0)
439 			return r;
440 	}
441 	return 0;
442 }
443 
444 struct sshkey *
445 _ssh_host_public_key(int type, int nid, struct ssh *ssh)
446 {
447 	struct key_entry *k;
448 
449 	debug3("%s: need %d", __func__, type);
450 	TAILQ_FOREACH(k, &ssh->public_keys, next) {
451 		debug3("%s: check %s", __func__, sshkey_type(k->key));
452 		if (k->key->type == type &&
453 		    (type != KEY_ECDSA || k->key->ecdsa_nid == nid))
454 			return (k->key);
455 	}
456 	return (NULL);
457 }
458 
459 struct sshkey *
460 _ssh_host_private_key(int type, int nid, struct ssh *ssh)
461 {
462 	struct key_entry *k;
463 
464 	debug3("%s: need %d", __func__, type);
465 	TAILQ_FOREACH(k, &ssh->private_keys, next) {
466 		debug3("%s: check %s", __func__, sshkey_type(k->key));
467 		if (k->key->type == type &&
468 		    (type != KEY_ECDSA || k->key->ecdsa_nid == nid))
469 			return (k->key);
470 	}
471 	return (NULL);
472 }
473 
474 int
475 _ssh_verify_host_key(struct sshkey *hostkey, struct ssh *ssh)
476 {
477 	struct key_entry *k;
478 
479 	debug3("%s: need %s", __func__, sshkey_type(hostkey));
480 	TAILQ_FOREACH(k, &ssh->public_keys, next) {
481 		debug3("%s: check %s", __func__, sshkey_type(k->key));
482 		if (sshkey_equal_public(hostkey, k->key))
483 			return (0);	/* ok */
484 	}
485 	return (-1);	/* failed */
486 }
487 
488 /* offer hostkey algorithms in kexinit depending on registered keys */
489 int
490 _ssh_order_hostkeyalgs(struct ssh *ssh)
491 {
492 	struct key_entry *k;
493 	char *orig, *avail, *oavail = NULL, *alg, *replace = NULL;
494 	char **proposal;
495 	size_t maxlen;
496 	int ktype, r;
497 
498 	/* XXX we de-serialize ssh->kex->my, modify it, and change it */
499 	if ((r = kex_buf2prop(ssh->kex->my, NULL, &proposal)) != 0)
500 		return r;
501 	orig = proposal[PROPOSAL_SERVER_HOST_KEY_ALGS];
502 	if ((oavail = avail = strdup(orig)) == NULL) {
503 		r = SSH_ERR_ALLOC_FAIL;
504 		goto out;
505 	}
506 	maxlen = strlen(avail) + 1;
507 	if ((replace = calloc(1, maxlen)) == NULL) {
508 		r = SSH_ERR_ALLOC_FAIL;
509 		goto out;
510 	}
511 	*replace = '\0';
512 	while ((alg = strsep(&avail, ",")) && *alg != '\0') {
513 		if ((ktype = sshkey_type_from_name(alg)) == KEY_UNSPEC)
514 			continue;
515 		TAILQ_FOREACH(k, &ssh->public_keys, next) {
516 			if (k->key->type == ktype ||
517 			    (sshkey_is_cert(k->key) && k->key->type ==
518 			    sshkey_type_plain(ktype))) {
519 				if (*replace != '\0')
520 					strlcat(replace, ",", maxlen);
521 				strlcat(replace, alg, maxlen);
522 				break;
523 			}
524 		}
525 	}
526 	if (*replace != '\0') {
527 		debug2("%s: orig/%d    %s", __func__, ssh->kex->server, orig);
528 		debug2("%s: replace/%d %s", __func__, ssh->kex->server, replace);
529 		free(orig);
530 		proposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = replace;
531 		replace = NULL;	/* owned by proposal */
532 		r = kex_prop2buf(ssh->kex->my, proposal);
533 	}
534  out:
535 	free(oavail);
536 	free(replace);
537 	kex_prop_free(proposal);
538 	return r;
539 }
540 
541 int
542 _ssh_host_key_sign(struct sshkey *privkey, struct sshkey *pubkey,
543     u_char **signature, size_t *slen, const u_char *data, size_t dlen,
544     const char *alg, u_int compat)
545 {
546 	return sshkey_sign(privkey, signature, slen, data, dlen, alg, compat);
547 }
548