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