xref: /openbsd-src/usr.bin/ssh/ssh_api.c (revision 746bf85ef77f47f1e658d909fa3ddb3e26aa65bd)
1 /* $OpenBSD: ssh_api.c,v 1.15 2019/01/21 10:38:54 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 ssh *, struct sshkey *, struct sshkey *,
40     u_char **, size_t *, const u_char *, size_t, const char *);
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] = kex_gen_server;
99 		ssh->kex->kex[KEX_DH_GRP14_SHA1] = kex_gen_server;
100 		ssh->kex->kex[KEX_DH_GRP14_SHA256] = kex_gen_server;
101 		ssh->kex->kex[KEX_DH_GRP16_SHA512] = kex_gen_server;
102 		ssh->kex->kex[KEX_DH_GRP18_SHA512] = kex_gen_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] = kex_gen_server;
106 #endif /* WITH_OPENSSL */
107 		ssh->kex->kex[KEX_C25519_SHA256] = kex_gen_server;
108 		ssh->kex->kex[KEX_KEM_SNTRUP4591761X25519_SHA512] = kex_gen_server;
109 		ssh->kex->load_host_public_key=&_ssh_host_public_key;
110 		ssh->kex->load_host_private_key=&_ssh_host_private_key;
111 		ssh->kex->sign=&_ssh_host_key_sign;
112 	} else {
113 #ifdef WITH_OPENSSL
114 		ssh->kex->kex[KEX_DH_GRP1_SHA1] = kex_gen_client;
115 		ssh->kex->kex[KEX_DH_GRP14_SHA1] = kex_gen_client;
116 		ssh->kex->kex[KEX_DH_GRP14_SHA256] = kex_gen_client;
117 		ssh->kex->kex[KEX_DH_GRP16_SHA512] = kex_gen_client;
118 		ssh->kex->kex[KEX_DH_GRP18_SHA512] = kex_gen_client;
119 		ssh->kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;
120 		ssh->kex->kex[KEX_DH_GEX_SHA256] = kexgex_client;
121 		ssh->kex->kex[KEX_ECDH_SHA2] = kex_gen_client;
122 #endif /* WITH_OPENSSL */
123 		ssh->kex->kex[KEX_C25519_SHA256] = kex_gen_client;
124 		ssh->kex->kex[KEX_KEM_SNTRUP4591761X25519_SHA512] = kex_gen_client;
125 		ssh->kex->verify_host_key =&_ssh_verify_host_key;
126 	}
127 	*sshp = ssh;
128 	return 0;
129 }
130 
131 void
132 ssh_free(struct ssh *ssh)
133 {
134 	struct key_entry *k;
135 
136 	ssh_packet_close(ssh);
137 	/*
138 	 * we've only created the public keys variants in case we
139 	 * are a acting as a server.
140 	 */
141 	while ((k = TAILQ_FIRST(&ssh->public_keys)) != NULL) {
142 		TAILQ_REMOVE(&ssh->public_keys, k, next);
143 		if (ssh->kex && ssh->kex->server)
144 			sshkey_free(k->key);
145 		free(k);
146 	}
147 	while ((k = TAILQ_FIRST(&ssh->private_keys)) != NULL) {
148 		TAILQ_REMOVE(&ssh->private_keys, k, next);
149 		free(k);
150 	}
151 	if (ssh->kex)
152 		kex_free(ssh->kex);
153 	free(ssh);
154 }
155 
156 void
157 ssh_set_app_data(struct ssh *ssh, void *app_data)
158 {
159 	ssh->app_data = app_data;
160 }
161 
162 void *
163 ssh_get_app_data(struct ssh *ssh)
164 {
165 	return ssh->app_data;
166 }
167 
168 /* Returns < 0 on error, 0 otherwise */
169 int
170 ssh_add_hostkey(struct ssh *ssh, struct sshkey *key)
171 {
172 	struct sshkey *pubkey = NULL;
173 	struct key_entry *k = NULL, *k_prv = NULL;
174 	int r;
175 
176 	if (ssh->kex->server) {
177 		if ((r = sshkey_from_private(key, &pubkey)) != 0)
178 			return r;
179 		if ((k = malloc(sizeof(*k))) == NULL ||
180 		    (k_prv = malloc(sizeof(*k_prv))) == NULL) {
181 			free(k);
182 			sshkey_free(pubkey);
183 			return SSH_ERR_ALLOC_FAIL;
184 		}
185 		k_prv->key = key;
186 		TAILQ_INSERT_TAIL(&ssh->private_keys, k_prv, next);
187 
188 		/* add the public key, too */
189 		k->key = pubkey;
190 		TAILQ_INSERT_TAIL(&ssh->public_keys, k, next);
191 		r = 0;
192 	} else {
193 		if ((k = malloc(sizeof(*k))) == NULL)
194 			return SSH_ERR_ALLOC_FAIL;
195 		k->key = key;
196 		TAILQ_INSERT_TAIL(&ssh->public_keys, k, next);
197 		r = 0;
198 	}
199 
200 	return r;
201 }
202 
203 int
204 ssh_set_verify_host_key_callback(struct ssh *ssh,
205     int (*cb)(struct sshkey *, struct ssh *))
206 {
207 	if (cb == NULL || ssh->kex == NULL)
208 		return SSH_ERR_INVALID_ARGUMENT;
209 
210 	ssh->kex->verify_host_key = cb;
211 
212 	return 0;
213 }
214 
215 int
216 ssh_input_append(struct ssh *ssh, const u_char *data, size_t len)
217 {
218 	return sshbuf_put(ssh_packet_get_input(ssh), data, len);
219 }
220 
221 int
222 ssh_packet_next(struct ssh *ssh, u_char *typep)
223 {
224 	int r;
225 	u_int32_t seqnr;
226 	u_char type;
227 
228 	/*
229 	 * Try to read a packet. Return SSH_MSG_NONE if no packet or not
230 	 * enough data.
231 	 */
232 	*typep = SSH_MSG_NONE;
233 	if (sshbuf_len(ssh->kex->client_version) == 0 ||
234 	    sshbuf_len(ssh->kex->server_version) == 0)
235 		return _ssh_exchange_banner(ssh);
236 	/*
237 	 * If we enough data and a dispatch function then
238 	 * call the function and get the next packet.
239 	 * Otherwise return the packet type to the caller so it
240 	 * can decide how to go on.
241 	 *
242 	 * We will only call the dispatch function for:
243 	 *     20-29    Algorithm negotiation
244 	 *     30-49    Key exchange method specific (numbers can be reused for
245 	 *              different authentication methods)
246 	 */
247 	for (;;) {
248 		if ((r = ssh_packet_read_poll2(ssh, &type, &seqnr)) != 0)
249 			return r;
250 		if (type > 0 && type < DISPATCH_MAX &&
251 		    type >= SSH2_MSG_KEXINIT && type <= SSH2_MSG_TRANSPORT_MAX &&
252 		    ssh->dispatch[type] != NULL) {
253 			if ((r = (*ssh->dispatch[type])(type, seqnr, ssh)) != 0)
254 				return r;
255 		} else {
256 			*typep = type;
257 			return 0;
258 		}
259 	}
260 }
261 
262 const u_char *
263 ssh_packet_payload(struct ssh *ssh, size_t *lenp)
264 {
265 	return sshpkt_ptr(ssh, lenp);
266 }
267 
268 int
269 ssh_packet_put(struct ssh *ssh, int type, const u_char *data, size_t len)
270 {
271 	int r;
272 
273 	if ((r = sshpkt_start(ssh, type)) != 0 ||
274 	    (r = sshpkt_put(ssh, data, len)) != 0 ||
275 	    (r = sshpkt_send(ssh)) != 0)
276 		return r;
277 	return 0;
278 }
279 
280 const u_char *
281 ssh_output_ptr(struct ssh *ssh, size_t *len)
282 {
283 	struct sshbuf *output = ssh_packet_get_output(ssh);
284 
285 	*len = sshbuf_len(output);
286 	return sshbuf_ptr(output);
287 }
288 
289 int
290 ssh_output_consume(struct ssh *ssh, size_t len)
291 {
292 	return sshbuf_consume(ssh_packet_get_output(ssh), len);
293 }
294 
295 int
296 ssh_output_space(struct ssh *ssh, size_t len)
297 {
298 	return (0 == sshbuf_check_reserve(ssh_packet_get_output(ssh), len));
299 }
300 
301 int
302 ssh_input_space(struct ssh *ssh, size_t len)
303 {
304 	return (0 == sshbuf_check_reserve(ssh_packet_get_input(ssh), len));
305 }
306 
307 /* Read other side's version identification. */
308 int
309 _ssh_read_banner(struct ssh *ssh, struct sshbuf *banner)
310 {
311 	struct sshbuf *input = ssh_packet_get_input(ssh);
312 	const char *mismatch = "Protocol mismatch.\r\n";
313 	const u_char *s = sshbuf_ptr(input);
314 	u_char c;
315 	char *cp, *remote_version;
316 	int r, remote_major, remote_minor, expect_nl;
317 	size_t n, j;
318 
319 	for (j = n = 0;;) {
320 		sshbuf_reset(banner);
321 		expect_nl = 0;
322 		for (;;) {
323 			if (j >= sshbuf_len(input))
324 				return 0; /* insufficient data in input buf */
325 			c = s[j++];
326 			if (c == '\r') {
327 				expect_nl = 1;
328 				continue;
329 			}
330 			if (c == '\n')
331 				break;
332 			if (expect_nl)
333 				goto bad;
334 			if ((r = sshbuf_put_u8(banner, c)) != 0)
335 				return r;
336 			if (sshbuf_len(banner) > SSH_MAX_BANNER_LEN)
337 				goto bad;
338 		}
339 		if (sshbuf_len(banner) >= 4 &&
340 		    memcmp(sshbuf_ptr(banner), "SSH-", 4) == 0)
341 			break;
342 		if ((cp = sshbuf_dup_string(banner)) == NULL)
343 			return SSH_ERR_ALLOC_FAIL;
344 		debug("%s: %s", __func__, cp);
345 		free(cp);
346 		/* Accept lines before banner only on client */
347 		if (ssh->kex->server || ++n > SSH_MAX_PRE_BANNER_LINES) {
348   bad:
349 			if ((r = sshbuf_put(ssh_packet_get_output(ssh),
350 			   mismatch, strlen(mismatch))) != 0)
351 				return r;
352 			return SSH_ERR_NO_PROTOCOL_VERSION;
353 		}
354 	}
355 	if ((r = sshbuf_consume(input, j)) != 0)
356 		return r;
357 
358 	if ((cp = sshbuf_dup_string(banner)) == NULL)
359 		return SSH_ERR_ALLOC_FAIL;
360 	/* XXX remote version must be the same size as banner for sscanf */
361 	if ((remote_version = calloc(1, sshbuf_len(banner))) == NULL)
362 		return SSH_ERR_ALLOC_FAIL;
363 
364 	/*
365 	 * Check that the versions match.  In future this might accept
366 	 * several versions and set appropriate flags to handle them.
367 	 */
368 	if (sscanf(cp, "SSH-%d.%d-%[^\n]\n",
369 	    &remote_major, &remote_minor, remote_version) != 3)
370 		return SSH_ERR_INVALID_FORMAT;
371 	debug("Remote protocol version %d.%d, remote software version %.100s",
372 	    remote_major, remote_minor, remote_version);
373 
374 	ssh->compat = compat_datafellows(remote_version);
375 	if  (remote_major == 1 && remote_minor == 99) {
376 		remote_major = 2;
377 		remote_minor = 0;
378 	}
379 	if (remote_major != 2)
380 		return SSH_ERR_PROTOCOL_MISMATCH;
381 	debug("Remote version string %.100s", cp);
382 	free(cp);
383 	return 0;
384 }
385 
386 /* Send our own protocol version identification. */
387 int
388 _ssh_send_banner(struct ssh *ssh, struct sshbuf *banner)
389 {
390 	char *cp;
391 	int r;
392 
393 	if ((r = sshbuf_putf(banner, "SSH-2.0-%.100s\r\n", SSH_VERSION)) != 0)
394 		return r;
395 	if ((r = sshbuf_putb(ssh_packet_get_output(ssh), banner)) != 0)
396 		return r;
397 	/* Remove trailing \r\n */
398 	if ((r = sshbuf_consume_end(banner, 2)) != 0)
399 		return r;
400 	if ((cp = sshbuf_dup_string(banner)) == NULL)
401 		return SSH_ERR_ALLOC_FAIL;
402 	debug("Local version string %.100s", cp);
403 	free(cp);
404 	return 0;
405 }
406 
407 int
408 _ssh_exchange_banner(struct ssh *ssh)
409 {
410 	struct kex *kex = ssh->kex;
411 	int r;
412 
413 	/*
414 	 * if _ssh_read_banner() cannot parse a full version string
415 	 * it will return NULL and we end up calling it again.
416 	 */
417 
418 	r = 0;
419 	if (kex->server) {
420 		if (sshbuf_len(ssh->kex->server_version) == 0)
421 			r = _ssh_send_banner(ssh, ssh->kex->server_version);
422 		if (r == 0 &&
423 		    sshbuf_len(ssh->kex->server_version) != 0 &&
424 		    sshbuf_len(ssh->kex->client_version) == 0)
425 			r = _ssh_read_banner(ssh, ssh->kex->client_version);
426 	} else {
427 		if (sshbuf_len(ssh->kex->server_version) == 0)
428 			r = _ssh_read_banner(ssh, ssh->kex->server_version);
429 		if (r == 0 &&
430 		    sshbuf_len(ssh->kex->server_version) != 0 &&
431 		    sshbuf_len(ssh->kex->client_version) == 0)
432 			r = _ssh_send_banner(ssh, ssh->kex->client_version);
433 	}
434 	if (r != 0)
435 		return r;
436 	/* start initial kex as soon as we have exchanged the banners */
437 	if (sshbuf_len(ssh->kex->server_version) != 0 &&
438 	    sshbuf_len(ssh->kex->client_version) != 0) {
439 		if ((r = _ssh_order_hostkeyalgs(ssh)) != 0 ||
440 		    (r = kex_send_kexinit(ssh)) != 0)
441 			return r;
442 	}
443 	return 0;
444 }
445 
446 struct sshkey *
447 _ssh_host_public_key(int type, int nid, struct ssh *ssh)
448 {
449 	struct key_entry *k;
450 
451 	debug3("%s: need %d", __func__, type);
452 	TAILQ_FOREACH(k, &ssh->public_keys, next) {
453 		debug3("%s: check %s", __func__, sshkey_type(k->key));
454 		if (k->key->type == type &&
455 		    (type != KEY_ECDSA || k->key->ecdsa_nid == nid))
456 			return (k->key);
457 	}
458 	return (NULL);
459 }
460 
461 struct sshkey *
462 _ssh_host_private_key(int type, int nid, struct ssh *ssh)
463 {
464 	struct key_entry *k;
465 
466 	debug3("%s: need %d", __func__, type);
467 	TAILQ_FOREACH(k, &ssh->private_keys, next) {
468 		debug3("%s: check %s", __func__, sshkey_type(k->key));
469 		if (k->key->type == type &&
470 		    (type != KEY_ECDSA || k->key->ecdsa_nid == nid))
471 			return (k->key);
472 	}
473 	return (NULL);
474 }
475 
476 int
477 _ssh_verify_host_key(struct sshkey *hostkey, struct ssh *ssh)
478 {
479 	struct key_entry *k;
480 
481 	debug3("%s: need %s", __func__, sshkey_type(hostkey));
482 	TAILQ_FOREACH(k, &ssh->public_keys, next) {
483 		debug3("%s: check %s", __func__, sshkey_type(k->key));
484 		if (sshkey_equal_public(hostkey, k->key))
485 			return (0);	/* ok */
486 	}
487 	return (-1);	/* failed */
488 }
489 
490 /* offer hostkey algorithms in kexinit depending on registered keys */
491 int
492 _ssh_order_hostkeyalgs(struct ssh *ssh)
493 {
494 	struct key_entry *k;
495 	char *orig, *avail, *oavail = NULL, *alg, *replace = NULL;
496 	char **proposal;
497 	size_t maxlen;
498 	int ktype, r;
499 
500 	/* XXX we de-serialize ssh->kex->my, modify it, and change it */
501 	if ((r = kex_buf2prop(ssh->kex->my, NULL, &proposal)) != 0)
502 		return r;
503 	orig = proposal[PROPOSAL_SERVER_HOST_KEY_ALGS];
504 	if ((oavail = avail = strdup(orig)) == NULL) {
505 		r = SSH_ERR_ALLOC_FAIL;
506 		goto out;
507 	}
508 	maxlen = strlen(avail) + 1;
509 	if ((replace = calloc(1, maxlen)) == NULL) {
510 		r = SSH_ERR_ALLOC_FAIL;
511 		goto out;
512 	}
513 	*replace = '\0';
514 	while ((alg = strsep(&avail, ",")) && *alg != '\0') {
515 		if ((ktype = sshkey_type_from_name(alg)) == KEY_UNSPEC)
516 			continue;
517 		TAILQ_FOREACH(k, &ssh->public_keys, next) {
518 			if (k->key->type == ktype ||
519 			    (sshkey_is_cert(k->key) && k->key->type ==
520 			    sshkey_type_plain(ktype))) {
521 				if (*replace != '\0')
522 					strlcat(replace, ",", maxlen);
523 				strlcat(replace, alg, maxlen);
524 				break;
525 			}
526 		}
527 	}
528 	if (*replace != '\0') {
529 		debug2("%s: orig/%d    %s", __func__, ssh->kex->server, orig);
530 		debug2("%s: replace/%d %s", __func__, ssh->kex->server, replace);
531 		free(orig);
532 		proposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = replace;
533 		replace = NULL;	/* owned by proposal */
534 		r = kex_prop2buf(ssh->kex->my, proposal);
535 	}
536  out:
537 	free(oavail);
538 	free(replace);
539 	kex_prop_free(proposal);
540 	return r;
541 }
542 
543 int
544 _ssh_host_key_sign(struct ssh *ssh, struct sshkey *privkey,
545     struct sshkey *pubkey, u_char **signature, size_t *slen,
546     const u_char *data, size_t dlen, const char *alg)
547 {
548 	return sshkey_sign(privkey, signature, slen, data, dlen,
549 	    alg, ssh->compat);
550 }
551