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