Commit 67126da8 by Carlos Martín Nieto Committed by Edward Thomson

ssh: verify the remote's host key against known_hosts if it exists

It turns out this has been available in libssh2 for a long time and we should
have been verifying this the whole time.
parent 3b7d756c
...@@ -454,54 +454,142 @@ static int _git_ssh_session_create( ...@@ -454,54 +454,142 @@ static int _git_ssh_session_create(
return 0; return 0;
} }
#define SSH_DEFAULT_PORT "22" /*
* Returns the typemask argument to pass to libssh2_knownhost_check{,p} based on
* the type of key that libssh2_session_hostkey returns.
*/
static int fingerprint_type_mask(int keytype)
{
int mask = LIBSSH2_KNOWNHOST_TYPE_PLAIN | LIBSSH2_KNOWNHOST_KEYENC_RAW;
return mask;
static int _git_ssh_setup_conn( switch (keytype) {
ssh_subtransport *t, case LIBSSH2_HOSTKEY_TYPE_RSA:
const char *url, mask |= LIBSSH2_KNOWNHOST_KEY_SSHRSA;
const char *cmd, break;
git_smart_subtransport_stream **stream) case LIBSSH2_HOSTKEY_TYPE_DSS:
mask |= LIBSSH2_KNOWNHOST_KEY_SSHDSS;
break;
#ifdef LIBSSH2_HOSTKEY_TYPE_ECDSA_256
case LIBSSH2_HOSTKEY_TYPE_ECDSA_256:
mask |= LIBSSH2_KNOWNHOST_KEY_ECDSA_256;
break;
case LIBSSH2_HOSTKEY_TYPE_ECDSA_384:
mask |= LIBSSH2_KNOWNHOST_KEY_ECDSA_384;
break;
case LIBSSH2_HOSTKEY_TYPE_ECDSA_521:
mask |= LIBSSH2_KNOWNHOST_KEY_ECDSA_521;
break;
#endif
#ifdef LIBSSH2_HOSTKEY_TYPE_ED25519
case LIBSSH2_HOSTKEY_TYPE_ED25519:
mask |= LIBSSH2_KNOWNHOST_KEY_ED25519;
break;
#endif
}
return mask;
}
#define KNOWN_HOSTS_FILE ".ssh/known_hosts"
/*
* Check the host against the user's known_hosts file.
*
* Returns 1/0 for valid/''not-valid or <0 for an error
*/
static int check_against_known_hosts(
LIBSSH2_SESSION *session,
const char *hostname,
int port,
const char *key,
size_t key_len,
int key_type)
{ {
int auth_methods, error = 0; int error, check, typemask, ret = 0;
ssh_stream *s; git_str path = GIT_STR_INIT, home = GIT_STR_INIT;
git_credential *cred = NULL; LIBSSH2_KNOWNHOSTS *known_hosts = NULL;
LIBSSH2_SESSION *session=NULL; struct libssh2_knownhost *host = NULL;
LIBSSH2_CHANNEL *channel=NULL;
t->current_stream = NULL; if ((error = git__getenv(&home, "HOME")) < 0) {
return error;
}
*stream = NULL; if ((error = git_str_joinpath(&path, git_str_cstr(&home), KNOWN_HOSTS_FILE)) < 0) {
if (ssh_stream_alloc(t, cmd, stream) < 0) ret = error;
return -1; goto out;
}
s = (ssh_stream *)*stream; if ((known_hosts = libssh2_knownhost_init(session)) == NULL) {
s->session = NULL; ssh_error(session, "error initializing known hosts");
s->channel = NULL; ret = -1;
goto out;
}
if (git_net_str_is_url(url)) /*
error = git_net_url_parse(&s->url, url); * Try to read the file and consider not finding it as not trusting the
else * host rather than an error.
error = git_net_url_parse_scp(&s->url, url); */
error = libssh2_knownhost_readfile(known_hosts, git_str_cstr(&path), LIBSSH2_KNOWNHOST_FILE_OPENSSH);
if (error == LIBSSH2_ERROR_FILE) {
ret = 0;
goto out;
}
if (error < 0) {
ssh_error(session, "error reading known_hosts");
ret = -1;
goto out;
}
if (error < 0) typemask = fingerprint_type_mask(key_type);
goto done; check = libssh2_knownhost_checkp(known_hosts, hostname, port, key, key_len, typemask, &host);
if (check == LIBSSH2_KNOWNHOST_CHECK_FAILURE) {
ssh_error(session, "error checking for known host");
ret = -1;
goto out;
}
if ((error = git_socket_stream_new(&s->io, s->url.host, s->url.port)) < 0 || ret = check == LIBSSH2_KNOWNHOST_CHECK_MATCH ? 1 : 0;
(error = git_stream_connect(s->io)) < 0)
goto done;
if ((error = _git_ssh_session_create(&session, s->io)) < 0) out:
goto done; libssh2_knownhost_free(known_hosts);
git_str_clear(&path);
git_str_clear(&home);
if (t->owner->connect_opts.callbacks.certificate_check != NULL) { return ret;
git_cert_hostkey cert = {{ 0 }}, *cert_ptr; }
/*
* Perform the check for the session's certificate against known hosts if
* possible and then ask the user if they have a callback.
*
* Returns 1/0 for valid/not-valid or <0 for an error
*/
static int check_certificate(
LIBSSH2_SESSION *session,
git_transport_certificate_check_cb check_cb,
void *check_cb_payload,
const char *host,
const char *portstr)
{
git_cert_hostkey cert = {{ 0 }};
const char *key; const char *key;
size_t cert_len; size_t cert_len;
int cert_type; int cert_type, port, cert_valid = 0, error = 0;
cert.parent.cert_type = GIT_CERT_HOSTKEY_LIBSSH2; if ((key = libssh2_session_hostkey(session, &cert_len, &cert_type)) == NULL) {
ssh_error(session, "failed to retrieve hostkey");
return -1;
}
key = libssh2_session_hostkey(session, &cert_len, &cert_type); /* Try to parse the port as a number, if we can't then fall back to default */
if (git__strntol32(&port, portstr, strlen(portstr), NULL, 10) < 0)
port = -1;
if ((cert_valid = check_against_known_hosts(session, host, port, key, cert_len, cert_type)) < 0)
return -1;
cert.parent.cert_type = GIT_CERT_HOSTKEY_LIBSSH2;
if (key != NULL) { if (key != NULL) {
cert.type |= GIT_CERT_SSH_RAW; cert.type |= GIT_CERT_SSH_RAW;
cert.hostkey = key; cert.hostkey = key;
...@@ -558,28 +646,75 @@ static int _git_ssh_setup_conn( ...@@ -558,28 +646,75 @@ static int _git_ssh_setup_conn(
if (cert.type == 0) { if (cert.type == 0) {
git_error_set(GIT_ERROR_SSH, "unable to get the host key"); git_error_set(GIT_ERROR_SSH, "unable to get the host key");
error = -1; return -1;
goto done;
} }
/* We don't currently trust any hostkeys */
git_error_clear(); git_error_clear();
error = 0;
if (!cert_valid) {
git_error_set(GIT_ERROR_SSH, "invalid or unknown remote ssh hostkey");
error = GIT_ECERTIFICATE;
}
cert_ptr = &cert; if (check_cb != NULL) {
git_cert_hostkey *cert_ptr = &cert;
git_error_state previous_error = {0};
error = t->owner->connect_opts.callbacks.certificate_check( git_error_state_capture(&previous_error, error);
(git_cert *)cert_ptr, error = check_cb((git_cert *) cert_ptr, cert_valid, host, check_cb_payload);
0, if (error == GIT_PASSTHROUGH) {
s->url.host, error = git_error_state_restore(&previous_error);
t->owner->connect_opts.callbacks.payload); } else if (error < 0 && !git_error_last()) {
git_error_set(GIT_ERROR_NET, "user canceled hostkey check");
}
if (error < 0 && error != GIT_PASSTHROUGH) { git_error_state_free(&previous_error);
if (!git_error_last()) }
git_error_set(GIT_ERROR_NET, "user cancelled hostkey check");
return error;
}
#define SSH_DEFAULT_PORT "22"
static int _git_ssh_setup_conn(
ssh_subtransport *t,
const char *url,
const char *cmd,
git_smart_subtransport_stream **stream)
{
int auth_methods, error = 0;
ssh_stream *s;
git_credential *cred = NULL;
LIBSSH2_SESSION *session=NULL;
LIBSSH2_CHANNEL *channel=NULL;
t->current_stream = NULL;
*stream = NULL;
if (ssh_stream_alloc(t, cmd, stream) < 0)
return -1;
s = (ssh_stream *)*stream;
s->session = NULL;
s->channel = NULL;
if (git_net_str_is_url(url))
error = git_net_url_parse(&s->url, url);
else
error = git_net_url_parse_scp(&s->url, url);
if (error < 0)
goto done;
if ((error = git_socket_stream_new(&s->io, s->url.host, s->url.port)) < 0 ||
(error = git_stream_connect(s->io)) < 0)
goto done;
if ((error = _git_ssh_session_create(&session, s->io)) < 0)
goto done;
if ((error = check_certificate(session, t->owner->connect_opts.callbacks.certificate_check, t->owner->connect_opts.callbacks.payload, s->url.host, s->url.port)) < 0)
goto done; goto done;
}
}
/* we need the username to ask for auth methods */ /* we need the username to ask for auth methods */
if (!s->url.username) { if (!s->url.username) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment