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