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