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