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