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