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