Files
libopenssh/ssh/ssh_api.c
Markus Friedl 530367aafa Move the key exchange state and other global variables into
a per connection state struct, add a initial API that could
be used for a proxy (see ssh-proxy.c)

Joint work with Matthias Pitzl, Stefan Rinkes, Bernhard Zaun and
Arne Becker.
2012-01-17 14:32:46 +01:00

347 lines
8.8 KiB
C

#include "ssh1.h" /* For SSH_MSG_NONE */
#include "ssh_api.h"
#include "compat.h"
#include "log.h"
#include "authfile.h"
#include "key.h"
#include "misc.h"
#include "ssh1.h"
#include "ssh2.h"
#include "version.h"
#include "xmalloc.h"
#include <string.h>
void _ssh_exchange_banner(struct session_state *);
char *_ssh_send_banner(struct session_state *);
char *_ssh_read_banner(struct session_state *);
Key *_ssh_host_public_key(int, void *);
Key *_ssh_host_private_key(int, void *);
int _ssh_verify_host_key(Key *, void *);
/*
* stubs for the server side implementation of kex.
* disable privsep so our stubs will never be called.
*/
int use_privsep = 0;
int mm_key_sign(Key *, u_char **, u_int *, u_char *, u_int);
DH *mm_choose_dh(int, int, int);
int
mm_key_sign(Key *key, u_char **sigp, u_int *lenp, u_char *data, u_int datalen)
{
return (-1);
}
DH *
mm_choose_dh(int min, int nbits, int max)
{
return (NULL);
}
/* API */
struct session_state *
ssh_init(int is_server, struct kex_params *kex_params)
{
struct session_state *ssh;
static int called;
if (!called) {
OpenSSL_add_all_algorithms();
called = 1;
}
ssh = ssh_packet_set_connection(NULL, 0, 0);
ssh->server_side = is_server;
/* Initialize key exchange */
ssh->kex = kex_new(ssh, kex_params->proposal);
ssh->kex->server = is_server;
if (is_server) {
ssh->kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
ssh->kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
ssh->kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
ssh->kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
ssh->kex->kex[KEX_ECDH_SHA2] = kexecdh_server;
ssh->kex->load_host_public_key=&_ssh_host_public_key;
ssh->kex->load_host_private_key=&_ssh_host_private_key;
} else {
ssh->kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client;
ssh->kex->kex[KEX_DH_GRP14_SHA1] = kexdh_client;
ssh->kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;
ssh->kex->kex[KEX_DH_GEX_SHA256] = kexgex_client;
ssh->kex->kex[KEX_ECDH_SHA2] = kexecdh_client;
ssh->kex->verify_host_key =&_ssh_verify_host_key;
}
return (ssh);
}
/* Returns -1 on error, 0 otherwise */
int
ssh_add_hostkey(struct session_state* ssh, char *key)
{
Key *parsed_key;
struct key_entry *k;
Buffer key_buf;
if (ssh->kex->server) {
/* Parse private key */
buffer_init(&key_buf);
buffer_append(&key_buf, key, strlen(key));
parsed_key = key_parse_private(&key_buf, "hostkey", "", NULL);
buffer_free(&key_buf);
if (parsed_key != NULL) {
k = xmalloc(sizeof(*k));
k->key = parsed_key;
TAILQ_INSERT_TAIL(&ssh->private_keys, k, next);
/* add the public key, too */
k = xmalloc(sizeof(*k));
k->key = key_from_private(parsed_key);
TAILQ_INSERT_TAIL(&ssh->public_keys, k, next);
return (0);
}
} else {
/* Parse public key */
parsed_key = key_new(KEY_UNSPEC);
if (key_read(parsed_key, &key) == 1) {
k = xmalloc(sizeof(*k));
k->key = parsed_key;
TAILQ_INSERT_TAIL(&ssh->public_keys, k, next);
return (0);
} else {
key_free(parsed_key);
}
}
return (-1);
}
void
ssh_input_append(struct session_state* ssh, const char *data, u_int len)
{
buffer_append(&ssh->input, data, len);
}
int
ssh_packet_get(struct session_state *ssh)
{
int type;
u_int32_t seqnr;
if (ssh->kex->client_version_string == NULL ||
ssh->kex->server_version_string == NULL) {
_ssh_exchange_banner(ssh);
return (SSH_MSG_NONE);
}
/*
* Try to read a packet. Returns SSH_MSG_NONE if no packet or not
* enough data.
*/
type = ssh_packet_read_poll2(ssh, &seqnr);
/*
* If we enough data and we have a dispatch function, call the
* function and return SSH_MSG_NONE. Otherwise return the packet type to
* the caller so it can decide how to go on.
*
* We will only call the dispatch function for:
* 20-29 Algorithm negotiation
* 30-49 Key exchange method specific (numbers can be reused for
* different authentication methods)
*/
if (type > 0 && type < DISPATCH_MAX &&
type >= SSH2_MSG_KEXINIT && type <= SSH2_MSG_TRANSPORT_MAX &&
ssh->dispatch[type] != NULL) {
(*ssh->dispatch[type])(type, seqnr, ssh);
return (SSH_MSG_NONE);
}
return (type);
}
void *
ssh_packet_payload(struct session_state* ssh, u_int *len)
{
*len = buffer_len(&ssh->incoming_packet);
return (buffer_ptr(&ssh->incoming_packet));
}
void
ssh_packet_put(struct session_state* ssh, int type, const char *data, u_int len)
{
ssh_packet_start(ssh, type);
buffer_append(&ssh->outgoing_packet, data, len);
}
void *
ssh_output_ptr(struct session_state* ssh, u_int *len)
{
*len = buffer_len(&ssh->output);
return (buffer_ptr(&ssh->output));
}
void
ssh_output_consume(struct session_state* ssh, u_int len)
{
buffer_consume(&ssh->output, len);
}
int
ssh_output_space(struct session_state* ssh, u_int len)
{
return (buffer_check_alloc(&ssh->output, len));
}
int
ssh_input_space(struct session_state* ssh, u_int len)
{
return (buffer_check_alloc(&ssh->input, len));
}
/* Read other side's version identification. */
char *
_ssh_read_banner(struct session_state *ssh)
{
char c, *s, buf[256], remote_version[256]; /* must be same size! */
int remote_major, remote_minor;
u_int i, n, j, len;
len = buffer_len(&ssh->input);
s = buffer_ptr(&ssh->input);
for (j = n = 0;;) {
for (i = 0; i < sizeof(buf) - 1; i++) {
if (j >= len)
return (NULL);
c = s[j++];
if (c == '\r') {
buf[i] = '\n';
buf[i + 1] = 0;
continue; /**XXX wait for \n */
}
if (c == '\n') {
buf[i + 1] = 0;
break;
}
buf[i] = c;
}
buf[sizeof(buf) - 1] = 0;
if (strncmp(buf, "SSH-", 4) == 0)
break;
debug("ssh_exchange_identification: %s", buf);
if (++n > 65536)
fatal("ssh_exchange_identification: "
"No banner received");
}
buffer_consume(&ssh->input, j);
/*
* Check that the versions match. In future this might accept
* several versions and set appropriate flags to handle them.
*/
if (sscanf(buf, "SSH-%d.%d-%[^\n]\n",
&remote_major, &remote_minor, remote_version) != 3)
fatal("Bad remote protocol version identification: '%.100s'", buf);
debug("Remote protocol version %d.%d, remote software version %.100s",
remote_major, remote_minor, remote_version);
ssh->datafellows = compat_datafellows(remote_version);
if (remote_major == 1 && remote_minor == 99) {
remote_major = 2;
remote_minor = 0;
}
if (remote_major != 2)
fatal("Protocol major versions differ: 2 vs. %d", remote_major);
enable_compat20();
chop(buf);
debug("Remote version string %.100s", buf);
return (xstrdup(buf));
}
/* Send our own protocol version identification. */
char *
_ssh_send_banner(struct session_state *ssh)
{
char buf[256];
snprintf(buf, sizeof buf, "SSH-2.0-%.100s\r\n", SSH_VERSION);
buffer_append(&ssh->output, buf, strlen(buf));
chop(buf);
debug("Local version string %.100s", buf);
return (xstrdup(buf));
}
void
_ssh_exchange_banner(struct session_state *ssh)
{
/*
* if _ssh_read_banner() cannot parse a full version string
* it will return NULL and we end up calling it again.
*/
if (ssh->kex->server) {
if (ssh->kex->server_version_string == NULL)
ssh->kex->server_version_string = _ssh_send_banner(ssh);
if (ssh->kex->server_version_string != NULL &&
ssh->kex->client_version_string == NULL)
ssh->kex->client_version_string = _ssh_read_banner(ssh);
} else {
if (ssh->kex->server_version_string == NULL)
ssh->kex->server_version_string = _ssh_read_banner(ssh);
if (ssh->kex->server_version_string != NULL &&
ssh->kex->client_version_string == NULL)
ssh->kex->client_version_string = _ssh_send_banner(ssh);
}
/* start initial kex as soon as we have exchanged the banners */
if (ssh->kex->server_version_string != NULL &&
ssh->kex->client_version_string != NULL)
kex_send_kexinit(ssh);
}
Key *
_ssh_host_public_key(int type, void *ctxt)
{
struct session_state *ssh = ctxt;
struct key_entry *k;
debug3("%s: need %d", __func__, type);
TAILQ_FOREACH(k, &ssh->public_keys, next) {
debug3("%s: check %s", __func__, key_type(k->key));
if (k->key->type == type)
return (k->key);
}
return (NULL);
}
Key *
_ssh_host_private_key(int type, void *ctxt)
{
struct session_state *ssh = ctxt;
struct key_entry *k;
datafellows = ssh->datafellows; /* XXX */
debug3("%s: need %d", __func__, type);
TAILQ_FOREACH(k, &ssh->private_keys, next) {
debug3("%s: check %s", __func__, key_type(k->key));
if (k->key->type == type)
return (k->key);
}
return (NULL);
}
int
_ssh_verify_host_key(Key *hostkey, void *ctxt)
{
struct session_state *ssh = ctxt;
struct key_entry *k;
debug3("%s: need %s", __func__, key_type(hostkey));
TAILQ_FOREACH(k, &ssh->public_keys, next) {
debug3("%s: check %s", __func__, key_type(k->key));
if (key_equal_public(hostkey, k->key))
return (0); /* ok */
}
return (-1); /* failed */
}