Commit 576598ef authored by Niels Möller's avatar Niels Möller
Browse files

Minor bugfixes, to make the code compile.

Rev: doc/HACKING:1.2
Rev: src/Makefile.in:1.9
Rev: src/abstract_crypto.h:1.7
Rev: src/atoms.c:1.2
Rev: src/atoms.h:1.2
Rev: src/bignum.c:1.6
Rev: src/bignum.h:1.6
Rev: src/client.c:1.9
Rev: src/format.c:1.9
Rev: src/getopt.c:1.2
Rev: src/keyexchange.c:1.4
Rev: src/keyexchange.h:1.6
Rev: src/pad.c:1.10
Rev: src/parse.c:1.11
Rev: src/publickey_crypto.c:1.4
Rev: src/publickey_crypto.h:1.3
Rev: src/randomness.c:1.2
Rev: src/unpad.c:1.9
parent dd5f2a31
......@@ -83,7 +83,7 @@ implementation of this type looks as follows:
lsh_string_free(packet);
return A_WRITE(closure->super.next, new);
return A_WRITE(closure->next, new);
}
Note the last line; the function passes a newly created packet on to
......
......@@ -34,8 +34,10 @@ binprefix =
LOADLIBES = $(LIBS) lib/algorithms.a
COMMON_SRCS = atoms.c bignum.c connection.c crypto.c \
debug.c encrypt.c format.c io.c packet_dispatch.c \
pad.c parse.c read_data.c read_line.c read_packet.c \
debug.c encrypt.c format.c getopt.c io.c keyexchange.c \
packet_dispatch.c pad.c parse.c publickey_crypto.c \
randomness.c \
read_data.c read_line.c read_packet.c \
server_keyexchange.c unpad.c void.c \
werror.c write_buffer.c xalloc.c
......
......@@ -102,16 +102,14 @@ struct verifier
struct signature_algorithm
{
struct signer *
(struct make_signer *)(struct signature_algorithm *closure,
UINT32 public_length,
UINT8 *public,
UINT32 secret_length,
UINT8 *secret);
struct verifier *
(struct make_verifier *)(struct signature_algorithm *closure,
UINT32 public_length,
UINT8 *public);
struct signer * (*make_signer)(struct signature_algorithm *closure,
UINT32 public_length,
UINT8 *public,
UINT32 secret_length,
UINT8 *secret);
struct verifier * (*make_verifier)(struct signature_algorithm *closure,
UINT32 public_length,
UINT8 *public);
};
#endif /* LSH_ABSTRACT_CRYPTO_H_INCLUDED */
......@@ -22,7 +22,7 @@ UINT32 get_atom_length(int atom)
UINT8 *get_atom_name(int atom)
{ return atom_table[atom].name; }
UINT32 lookup_atom(UINT8 *name, UINT32 length)
UINT32 lookup_atom(UINT32 length, UINT8 *name)
{
struct atom_assoc *pair = gperf_atom(name, length);
......
......@@ -10,6 +10,6 @@
UINT32 get_atom_length(int atom);
UINT8 *get_atom_name(int atom);
UINT32 lookup_atom(UINT8 *name, UINT32 length);
UINT32 lookup_atom(UINT32 length, UINT8 *name);
#endif /* LSH_ATOMS_H_INCLUDED */
......@@ -181,7 +181,7 @@ UINT32 bignum_format_u(mpz_t n, UINT8 *data)
void bignum_random(mpz_t x, struct randomness *random, mpz_t n)
{
/* Add a few bits extra */
int length = (mpz_sizeinbase(n) + 17) / 8;
int length = (mpz_sizeinbase(n, 2) + 17) / 8;
UINT8 *data = alloca(length);
RANDOM(random, length, data);
......
......@@ -11,9 +11,14 @@
#include "lsh_types.h"
#include "randomness.h"
void bignum_parse(mpz_t n, UINT32 length, UINT8 *data);
UINT32 bignum_format_length(mpz_t n);
UINT32 bignum_format(mpz_t n, UINT8 *data);
void bignum_parse_s(mpz_t n, UINT32 length, UINT8 *data);
void bignum_parse_u(mpz_t n, UINT32 length, UINT8 *data);
UINT32 bignum_format_s(mpz_t n, UINT8 *data);
UINT32 bignum_format_s_length(mpz_t n);
UINT32 bignum_format_u(mpz_t n, UINT8 *data);
UINT32 bignum_format_u_length(mpz_t n);
/* Generates a random number in the interval 0 <= x < n */
void bignum_random(mpz_t x, struct randomness *random, mpz_t n);
......
......@@ -60,6 +60,11 @@ struct client_line_handler
struct ssh_connection *connection;
};
struct abstract_write *make_client_dispatch(struct ssh_connection *c)
{ /* FIXME: HERE */
return make_packet_void();
}
static struct read_handler *do_line(struct line_handler **h,
UINT32 length,
UINT8 *line)
......@@ -76,7 +81,7 @@ static struct read_handler *do_line(struct line_handler **h,
struct read_handler *new
= make_read_packet
(make_packet_debug
(make_packet_unpad(make_client_dispatch(connection)),
(make_packet_unpad(make_client_dispatch(closure->connection)),
stderr),
closure->connection->max_packet);
......
......@@ -118,9 +118,10 @@ UINT32 ssh_vformat_length(char *f, va_list args)
case 'A':
{
int *atom = va_arg(args, int *);
int i;
while (*atom > 0)
length += get_atom_length(atom++) + 1;
for(i = 0; atom[i] > 0; i++)
length += get_atom_length(atom[i]) + 1;
/* One ','-character less than the number of atoms */
length--;
......@@ -149,10 +150,10 @@ UINT32 ssh_vformat_length(char *f, va_list args)
#endif
case 'n':
{
bignum *n = va_arg(args, bignum *);
MP_INT *n = va_arg(args, MP_INT*);
/* Calculate length of written number */
length += bignum_format_length(n);
length += bignum_format_s_length(n);
if (!literal)
length += 4;
......@@ -296,8 +297,9 @@ void ssh_vformat(char *f, UINT8 *buffer, va_list args)
memcpy(buffer, get_atom_name(atom), length);
buffer += length;
f++;
break;
}
break;
#if 0
case 'A':
{
int atom;
......@@ -330,29 +332,29 @@ void ssh_vformat(char *f, UINT8 *buffer, va_list args)
f++;
break;
}
#if 0
#endif
case 'A':
{
int *atom = va_arg(args, int *);
UINT8 *start = buffer; /* Where to store the length */
int i;
if (!literal)
buffer += 4;
if (*atom > 0)
if (atom[0] > 0)
{
UINT32 length = get_atom_length(*atom);
memcpy(buffer, get_atom_name(*atom), length);
buffer += length;
atom ++;
while (*atom > 0)
for (i = 1; atom[i] > 0; i++)
{
*buffer++ = ',';
length = get_atom_length(atom);
memcpy(buffer, get_atom_name(atom), length);
length = get_atom_length(atom[i]);
memcpy(buffer, get_atom_name(atom[i]), length);
buffer += length;
atom++;
}
}
......@@ -364,11 +366,10 @@ void ssh_vformat(char *f, UINT8 *buffer, va_list args)
f++;
break;
}
#endif
case 'n':
{
bignum *n = va_arg(args, bignum *);
UINT32 length = bignum_format(n, buffer);
MP_INT *n = va_arg(args, MP_INT *);
UINT32 length = bignum_format_s(n, buffer);
buffer += length;
......
......@@ -688,6 +688,7 @@ _getopt_internal (argc, argv, optstring, longopts, longind, long_only)
else
{
if (opterr)
{
if (argv[optind - 1][1] == '-')
/* --option */
fprintf (stderr,
......@@ -698,7 +699,7 @@ _getopt_internal (argc, argv, optstring, longopts, longind, long_only)
fprintf (stderr,
_("%s: option `%c%s' doesn't allow an argument\n"),
argv[0], argv[optind - 1][0], pfound->name);
}
nextchar += strlen (nextchar);
optopt = pfound->val;
......
......@@ -2,9 +2,13 @@
*
*/
#include "abstract_io.h"
#include "connection.h"
#include "format.h"
#include "keyexchange.h"
#include "parse.h"
#include "ssh.h"
#include "xalloc.h"
#define NLISTS 10
......@@ -25,9 +29,9 @@ struct kexinit *parse_kexinit(struct lsh_string *packet)
|| (msg_number != SSH_MSG_KEXINIT) )
return 0;
res = xalloc(sizeof(keyexinit));
res = xalloc(sizeof(struct kexinit));
if (!parse_octets(&buffer, 16, &res->cookie))
if (!parse_octets(&buffer, 16, res->cookie))
{
lsh_free(res);
return NULL;
......@@ -53,16 +57,16 @@ struct kexinit *parse_kexinit(struct lsh_string *packet)
return NULL;
}
res->kex_algorithms = list[0];
res->kex_algorithms = lists[0];
res->server_host_key_algorithms = lists[1];
res->encryption_algorithms_client_to_server = list[2];
res->encryption_algorithms_server_to_client = list[3];
res->mac_algorithms_client_to_server = list[4];
res->mac_algorithms_server_to_client = list[5];
res->compression_algorithms_client_to_server = list[6];
res->compression_algorithms_server_to_client = list[7];
res->languages_client_to_server = list[8];
res->languages_server_to_client = list[9];
res->encryption_algorithms_client_to_server = lists[2];
res->encryption_algorithms_server_to_client = lists[3];
res->mac_algorithms_client_to_server = lists[4];
res->mac_algorithms_server_to_client = lists[5];
res->compression_algorithms_client_to_server = lists[6];
res->compression_algorithms_server_to_client = lists[7];
res->languages_client_to_server = lists[8];
res->languages_server_to_client = lists[9];
return res;
}
......
......@@ -6,6 +6,7 @@
#define LSH_KEYEXCHANGE_H_INCLUDED
#include "lsh_types.h"
#include "abstract_io.h"
struct kexinit
{
......@@ -22,13 +23,13 @@ struct kexinit
int *languages_client_to_server;
int *languages_server_to_client;
int first_kex_packet_follows;
}
};
struct handle_kexinit
{
int (*f)(struct handle_kexinit *closure,
struct kexinit *msg);
}
};
#define HANDLE_KEXINIT(handler, msg) ((handler)->f((handler), (msg)))
......
......@@ -2,9 +2,11 @@
*
*/
#include <assert.h>
#include "pad.h"
#include "randomness.h"
#include "xalloc.h"
#include <assert.h>
static int do_pad(struct abstract_write **w,
struct lsh_string *packet)
......
......@@ -2,9 +2,10 @@
*
*/
#include "format.h"
#include "parse.h"
#include "werror.h"
#include "xalloc.h"
#include "format.h"
void simple_buffer_init(struct simple_buffer *buffer,
UINT32 capacity, UINT8 *data)
......@@ -60,7 +61,7 @@ struct lsh_string *parse_string_copy(struct simple_buffer *buffer)
UINT32 length;
UINT8 *start;
if (!parse_string(buffer, &length, &data))
if (!parse_string(buffer, &length, &start))
return NULL;
return ssh_format("%ls", length, start);
......@@ -107,7 +108,7 @@ int parse_bignum(struct simple_buffer *buffer, mpz_t result)
if (!parse_string(buffer, &length, &digits))
return 0;
bignum_parse(result, length, digits);
bignum_parse_s(result, length, digits);
return 1;
}
......@@ -121,7 +122,7 @@ int parse_atom(struct simple_buffer *buffer, int *result)
|| length > 64)
return 0;
*result = lookup_atom(data, start);
*result = lookup_atom(length, start);
return 1;
}
......@@ -148,7 +149,7 @@ int parse_next_atom(struct simple_buffer *buffer, int *result)
return 0;
}
*result = lookup_atom(HERE, i);
*result = lookup_atom(i, HERE);
ADVANCE(i+1); /* If the atom was terminated at the end of the
* buffer, rather than by a comma, this points beyond
* the end of the buffer */
......
......@@ -2,10 +2,16 @@
*
*/
#include "bignum.h"
#include "sha.h"
#include "atoms.h"
#include "bignum.h"
#include "crypto.h"
#include "format.h"
#include "parse.h"
#include "publickey_crypto.h"
#include "sha.h"
#include "ssh.h"
#include "werror.h"
#include "xalloc.h"
/* DSS signatures */
......@@ -22,7 +28,7 @@ struct dss_signer
struct signer super;
struct randomness *random;
struct dss_public public;
mpz_t a;
mpz_t a; /* Private key */
};
struct dss_verifier
......@@ -40,24 +46,22 @@ struct dss_algorithm
static void dss_hash(mpz_t h, UINT32 length, UINT8 *msg)
{
/* Compute hash */
hash = MAKE_HASH(&sha_algorithm);
digest = alloca(hash->hash_size);
HASH_UPDATE(hash, length, data);
struct hash_instance *hash = MAKE_HASH(&sha_algorithm);
UINT8 *digest = alloca(hash->hash_size);
HASH_UPDATE(hash, length, msg);
HASH_DIGEST(hash, digest);
bignum_parse_u(h, digest, hash->hash_size);
bignum_parse_u(h, hash->hash_size, digest);
lsh_free(hash);
}
static struct lsh_string *do_dss_sign(struct signer *s,
static struct lsh_string *do_dss_sign(struct signer *c,
UINT32 length,
UINT8 *msg)
{
struct dss_signer *closure = (struct dss_signer *) s;
struct dss_signer *closure = (struct dss_signer *) c;
mpz_t k, r, s, tmp;
struct hash_instance *hash;
UINT8 *digest;
struct lsh_string *signature;
/* Select k, 0<k<q, randomly */
......@@ -65,7 +69,7 @@ static struct lsh_string *do_dss_sign(struct signer *s,
mpz_sub_ui(tmp, tmp, 1);
mpz_init(k);
bignum_random(k, closure.random, tmp);
bignum_random(k, closure->random, tmp);
mpz_add_ui(k, k, 1);
/* Compute r = (g^k (mod p)) (mod q) */
......@@ -88,7 +92,7 @@ static struct lsh_string *do_dss_sign(struct signer *s,
/* Compute signature s = k^-1(h + ar) (mod q) */
mpz_init(s);
mpz_mul(s, r, closure->secret);
mpz_mul(s, r, closure->a);
mpz_tdiv_r(s, s, closure->public.q);
mpz_add(s, s, tmp);
mpz_mul(s, s, k);
......@@ -105,13 +109,13 @@ static struct lsh_string *do_dss_sign(struct signer *s,
return signature;
}
ind do_dss_verify(struct verifier *closure,
int do_dss_verify(struct verifier *c,
UINT32 length,
UINT8 *msg,
UINT32 signature_length,
UINT8 * signature_data)
{
struct dss_signer *closure = (struct dss_signer *) s;
struct dss_signer *closure = (struct dss_signer *) c;
struct simple_buffer buffer;
int res;
......@@ -119,7 +123,7 @@ ind do_dss_verify(struct verifier *closure,
int atom;
mpz_t r, s;
mpz_t w, tmp, u, v;
mpz_t w, tmp, v;
simple_buffer_init(&buffer, signature_length, signature_data);
if (!parse_atom(&buffer, &atom)
......@@ -143,7 +147,7 @@ ind do_dss_verify(struct verifier *closure,
/* Compute w = s^-1 (mod q) */
mpz_init(w);
if (!mpz_invert(s, closure->public.q))
if (!mpz_invert(w, s, closure->public.q))
{
werror("do_dss_verify: s non-invertible.\n");
mpz_clear(r);
......@@ -194,16 +198,16 @@ int parse_dss_public(struct simple_buffer *buffer, struct dss_public *public)
mpz_init(public->y);
#endif
return (parse_bignum(&buffer, public.p)
&& parse_bignum(&buffer, public->p)
return (parse_bignum(buffer, public->p)
&& parse_bignum(buffer, public->p)
&& (mpz_sgn(public->p) == 1)
&& parse_bignum(&buffer, public->q)
&& parse_bignum(buffer, public->q)
&& (mpz_sgn(public->q) == 1)
&& (mpz_cmp(public->q, public->p) < 0) /* q < p */
&& parse_bignum(&buffer, public->g)
&& parse_bignum(buffer, public->g)
&& (mpz_sgn(public->g) == 1)
&& (mpz_cmp(public->g, public->p) < 0) /* g < p */
&& parse_bignum(&buffer, public->y)
&& parse_bignum(buffer, public->y)
&& (mpz_sgn(public->y) == 1)
&& (mpz_cmp(public->y, public->p) < 0) /* y < p */ );
#if 0
......@@ -220,17 +224,20 @@ int parse_dss_public(struct simple_buffer *buffer, struct dss_public *public)
struct signer *make_dss_signer(struct signature_algorithm *closure,
UINT32 public_length,
UINT8 *public,
UINT32 secret_length,
UINT8 *secret)
UINT32 private_length,
UINT8 *private)
{
struct dss_signer *res;
struct simple_buffer buffer;
struct simple_buffer public_buffer;
struct simple_buffer private_buffer;
int atom;
simple_buffer_init(&buffer, signature_length, signature_data);
if (!parse_atom(&buffer, &atom)
simple_buffer_init(&public_buffer, public_length, public);
if (!parse_atom(&public_buffer, &atom)
|| (atom != ATOM_SSH_DSS) )
return 0;
simple_buffer_init(&private_buffer, private_length, private);
res = xalloc(sizeof(struct dss_signer));
......@@ -238,18 +245,19 @@ struct signer *make_dss_signer(struct signature_algorithm *closure,
mpz_init(res->public.q);
mpz_init(res->public.g);
mpz_init(res->public.y);
mpz_init(res->secret);
mpz_init(res->a);
if (! (parse_dss_public(&buffer, &res->public)
&& parse_bignum(&buffer, res->secret)
if (! (parse_dss_public(&public_buffer, &res->public)
&& parse_bignum(&private_buffer, res->a)
/* FIXME: Perhaps do some more sanity checks? */
&& mpz_sign(res->secret) == 1))
&& (mpz_sgn(res->a) == 1)
&& parse_eod(&private_buffer) ))
{
mpz_clear(res->public.p);
mpz_clear(res->public.q);
mpz_clear(res->public.g);
mpz_clear(res->public.y);
mpz_clear(res->secret);
mpz_clear(res->a);
lsh_free(res);
return NULL;
}
......@@ -293,9 +301,9 @@ struct verifier *make_dss_verifier(struct signature_algorithm *closure,
return &res->super;
}
struct make_dss_algorithm(struct randomness *random)
struct signature_algorithm *make_dss_algorithm(struct randomness *random)
{
struct dss_algorithm *res = xalloc(sizeof(struct dss_algorithm));
struct dss_algorithm *dss = xalloc(sizeof(struct dss_algorithm));
dss->super.make_signer = make_dss_signer;
dss->super.make_verifier = make_dss_verifier;
......@@ -352,8 +360,9 @@ struct group *make_zn(mpz_t p)
res->super.combine = zn_combine;
res->super.power = zn_power; /* Pretty Mutation! Magical Recall! */
mpz_init_set(res->modulo, n);
mpz_init_set(res->super.order, n);
mpz_init_set(res->modulo, p);
mpz_init_set(res->super.order, p);
mpz_sub_ui(res->super.order, res->super.order, 1);
return &res->super;
}
......@@ -361,7 +370,7 @@ struct group *make_zn(mpz_t p)
struct diffie_hellman_instance *
make_diffie_hellman_instance(struct diffie_hellman_method *m,
struct connection *c)
struct ssh_connection *c)
{
struct diffie_hellman_instance *res
= xalloc(sizeof(struct diffie_hellman_instance));
......@@ -371,7 +380,7 @@ make_diffie_hellman_instance(struct diffie_hellman_method *m,
mpz_init(res->secret);
res->method = m;
res->hash = make_hash(m->H);
res->hash = MAKE_HASH(m->H);
HASH_UPDATE(res->hash,
c->client_version->length,
c->client_version->data);
......@@ -381,7 +390,7 @@ make_diffie_hellman_instance(struct diffie_hellman_method *m,
return res;
}
struct group *make_dh1(struct randomness *r)
struct diffie_hellman_method *make_dh1(struct randomness *r)
{
struct diffie_hellman_method *res
= xalloc(sizeof(struct diffie_hellman_method));
......@@ -401,7 +410,7 @@ struct group *make_dh1(struct randomness *r)
mpz_init_set_ui(res->generator, 2);
res->hash_algorithm = &sha_algorithm;
res->H = &sha_algorithm;
res->random = r;
return res;
......@@ -412,14 +421,14 @@ void dh_generate_secret(struct diffie_hellman_instance *self,
{
mpz_t tmp;
/* Generate a random number, 1 < x < O(G) */
mpz_init_set(tmp, self->method->G->order);
mpz_sub_ui(tmp, tmp, 2);
bignum_random(self->secret, tmp);
mpz_add_ui(self->secret, self->secret, 2);
/* Generate a random number, 1 < x <= p-1 = O(G) */
mpz_init_set(tmp, self->method->G->order);
mpz_sub_ui(tmp, tmp, 1);
bignum_random(self->secret, self->method->random, tmp);
mpz_add_ui(self->secret, self->secret, 1);
mpz_clear(tmp);
GROUP_POWER(self->methog->G, r, self->method->generator, self->secret);
GROUP_POWER(self->method->G, r, self->method->generator, self->secret);
}
struct lsh_string *dh_make_client_msg(struct diffie_hellman_instance *self)
......@@ -440,7 +449,7 @@ int dh_process_client_msg(struct diffie_hellman_instance *self,
&& (msg_number == SSH_MSG_KEXDH_INIT)
&& parse_bignum(&buffer, self->e)
&& (mpz_cmp_ui(self->e, 1) > 0)
&& (mpz_cmp(self->e, self->method->G->order) < 0)
&& (mpz_cmp(self->e, self->method->G->order) <= 0)
&& parse_eod(&buffer) );
}
......@@ -453,14 +462,14 @@ void dh_hash_update(struct diffie_hellman_instance *self,
/* Hashes server key, e and f */
void dh_hash_digest(struct diffie_hellman_instance *self, UINT8 *digest)
{