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