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