ssh_api.c 15 KB

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