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