keyexchange.c 9.32 KB
Newer Older
Niels Möller's avatar
Niels Möller committed
1
2
/* keyexchange.c
 *
Niels Möller's avatar
Niels Möller committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 *
 *
 * $Id$ */

/* lsh, an implementation of the ssh protocol
 *
 * Copyright (C) 1998 Niels Mller
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
Niels Möller's avatar
Niels Möller committed
24
25
 */

26
27
28
#include "abstract_io.h"
#include "connection.h"
#include "format.h"
Niels Möller's avatar
Niels Möller committed
29
30
#include "keyexchange.h"
#include "parse.h"
Niels Möller's avatar
Niels Möller committed
31
#include "publickey_crypto.h"
32
33
#include "ssh.h"
#include "xalloc.h"
Niels Möller's avatar
Niels Möller committed
34
35
36

#define NLISTS 10

Niels Möller's avatar
Niels Möller committed
37
struct kexinit *parse_kexinit(struct lsh_string *packet)
Niels Möller's avatar
Niels Möller committed
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
{
  struct kexinit *res;
  struct simple_buffer buffer;
  struct simple_buffer sub_buffer;
  UINT8 msg_number;
  UINT32 reserved;
  
  int *lists[NLISTS];
  int i;
  
  simple_buffer_init(&buffer, packet->length, packet->data);

  if (!parse_uint8(&buffer, &msg_number)
      || (msg_number != SSH_MSG_KEXINIT) )
    return 0;

54
  res = xalloc(sizeof(struct kexinit));
Niels Möller's avatar
Niels Möller committed
55

56
  if (!parse_octets(&buffer, 16, res->cookie))
Niels Möller's avatar
Niels Möller committed
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
    {
      lsh_free(res);
      return NULL;
    }
  
  for (i = 0; i<NLISTS; i++)
    {
      if (!parse_sub_buffer(&buffer, &sub_buffer)
	  || ! (lists[i] = parse_atom_list(&sub_buffer)))
	break;
    }

  if ( (i<NLISTS)
       || !parse_boolean(&buffer, &res->first_kex_packet_follows)
       || !parse_uint32(&buffer, &reserved)
       || reserved || !parse_eod(&buffer) )
    {
      /* Bad format */
      int j;
      for (j = 0; j<i; j++)
	lsh_free(lists[i]);
      lsh_free(res);
      return NULL;
    }
  
82
  res->kex_algorithms = lists[0];
Niels Möller's avatar
Niels Möller committed
83
  res->server_host_key_algorithms = lists[1];
Niels Möller's avatar
Niels Möller committed
84
85
86
87

  for (i=0; i<KEX_PARAMETERS; i++)
    res->parameters[i] = lists[2 + i];

88
89
  res->languages_client_to_server = lists[8];
  res->languages_server_to_client = lists[9];
Niels Möller's avatar
Niels Möller committed
90
91
92

  return res;
}
Niels Möller's avatar
Niels Möller committed
93

Niels Möller's avatar
Niels Möller committed
94
#if 0
Niels Möller's avatar
Niels Möller committed
95
96
97
98
99
100
101
102
103
104
struct abstract_write *make_packet_kexinit(struct handle_kexinit *handler)
{
  struct handle_kexinit_packet *closure
    = xalloc(sizeof(struct handle_kexinit_packet));

  closure->super.write = do_handle_kexinit;
  closure->handler = handler;

  return &closure->super;
}
Niels Möller's avatar
Niels Möller committed
105
#endif
Niels Möller's avatar
Niels Möller committed
106

Niels Möller's avatar
Niels Möller committed
107
108
109
110
111
112
113
struct lsh_string *format_kex(struct kexinit *kex)
{
  return ssh_format("%c%ls%A%A%A%A%A%A%A%A%A%A%c%i",
		    SSH_MSG_KEXINIT,
		    16, kex->cookie,
		    kex->kex_algorithms,
		    kex->server_host_key_algorithms,
Niels Möller's avatar
Niels Möller committed
114
115
116
117
118
119
		    kex->parameters[KEX_ENCRYPTION_CLIENT_TO_SERVER],
		    kex->parameters[KEX_ENCRYPTION_SERVER_TO_CLIENT],
		    kex->parameters[KEX_MAC_CLIENT_TO_SERVER],
		    kex->parameters[KEX_MAC_SERVER_TO_CLIENT],
		    kex->parameters[KEX_COMPRESSION_CLIENT_TO_SERVER],
		    kex->parameters[KEX_COMPRESSION_SERVER_TO_CLIENT],
Niels Möller's avatar
Niels Möller committed
120
121
122
123
124
125
126
127
128
129
130
		    kex->languages_client_to_server,
		    kex->languages_server_to_client,
		    kex->first_kex_packet_follows, 0);
}
  

int initiate_keyexchange(struct ssh_connection *connection,
			 struct kexinit *kex,
			 struct lsh_string *first_packet)
{
  int res;
Niels Möller's avatar
Niels Möller committed
131
  lsh_string *s;
Niels Möller's avatar
Niels Möller committed
132
133
  
  kex->first_kex_packet_follows = !!first_packet;
Niels Möller's avatar
Niels Möller committed
134
  connection->kexinits[connection->type] = kex;
Niels Möller's avatar
Niels Möller committed
135

Niels Möller's avatar
Niels Möller committed
136
137
138
139
140
141
142
  s = format_kex(kex);

  /* Save value for later signing */
  connection->literal_kexinits[connection->type] = s; 

  res = A_WRITE(connection->write, lsh_string_dup(s));
  
143
  if ( (res == WRITE_OK) && first_packet)
Niels Möller's avatar
Niels Möller committed
144
145
146
147
148
    return A_WRITE(connection->write, first_packet);
  else
    return res;
}

Niels Möller's avatar
Niels Möller committed
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
int select_algorithm(int *server_list, int *client_list)
{
  /* FIXME: This quadratic complexity algorithm should do as long as
   * the lists are short. */
  int i, j;

  for(i = 0; client_list[i] >= 0; i++)
    {
      if (!client_list[i])
	/* Unknown algorithm */
	continue;
      for(j = 0; server_list[j] > 0; j++)
	if (client_list[i] = server_list[j])
	  return client_list[i];
    }

  return 0;
}

int send_disconnect(struct ssh_conection, char *msg)
{
  return A_WRITE(connection->write,
		 ssh_format("%c%i%z%z",
			    SSH_MSG_DISCONNECT,
			    SSH_DISCONNECT_KEY_EXCHANGE_FAILED,
			    msg, ""));
}

static int do_handle_kexinit(struct packet_hander *c,
			     struct ssh_connection *connection,
			     struct lsh_string *packet)
{
  struct handle_kexinit *closure = (struct handle_kexinit_packet *) c;
  struct kexinit *msg = parse_kexinit(packet);

  int kex_algorithm;
  int hostkey_algorithm;

  int parameters[KEX_PARAMETERS];
  void **algorithms;

  struct packet_handler newkeys;

  int i;
  
  if (!msg)
    return 0;

Niels Möller's avatar
Niels Möller committed
197
  /* Save value for later signing */
Niels Möller's avatar
Niels Möller committed
198
  connection->literal_kexinits[!connection->type] = packet;
Niels Möller's avatar
Niels Möller committed
199
  
Niels Möller's avatar
Niels Möller committed
200
201
202
203
204
205
  connection->kexinits[!connection->type] = msg;
  
  /* Have we sent a kexinit message? */
  if (!connection->kexinits[connection->type])
    {
      int res;
Niels Möller's avatar
Niels Möller committed
206
      struct lsh_string *packet;
Niels Möller's avatar
Niels Möller committed
207
208
      struct kexinit *sent =  GENERATE_KEXINIT(closure->init);
      connection->kexinits[connection->type] = sent;
Niels Möller's avatar
Niels Möller committed
209
210
211
212
      packet = format_kex(sent);
      connection->kexinits[connection->type] = lsh_string_dup(packet); 
      
      res = A_WRITE(connection->write, packet);
Niels Möller's avatar
Niels Möller committed
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
      if (res != WRITE_OK)
	return res;
    }

  /* Select key exchange algorithms */

  if (connection->kexinits[0]->kex_algorithms[0]
      == connection->kexinits[1]->kex_algorithms[1])
    {
      /* Use this algorithm */
      kex_algorithm = connection->sent_kexinit->kex_algorithms[0];
    }
  else
    {
      if (msg->first_kex_packet_follows)
	{
	  /* Wrong guess */
	  connection->ignore_one_packet = 1;
	}
      /* FIXME: Ignores that some keyechange algorithms require
       * certain features of the host key algorithms. */
      
      kex_algorithm = select_algorithm(connection->kexinits[0]->kex_algorithm,
				       connection->kexinits[1]->kex_algorithm);
      if  (!kex_algorithm)
	{
	  send_disconnect(connection, "No common key exchange method.\r\n");

	  /* FIXME: We want the disconnect message to be sent
	   * before the socket is closed. How? */
	  return WRITE_CLOSED;
	}

      hostkey_algorithm
	= select_algorithm(connection->kexinits[0]->server_hostkey_algorithms,
			   connection->kexinits[1]->server_hostkey_algorithms);
Niels Möller's avatar
Niels Möller committed
249

Niels Möller's avatar
Niels Möller committed
250
251
252
253
254
255
256
257
258
      for(i = 0; i<KEX_PARAMETERS; i++)
	{
	  parameters[i]
	    = select_algorithm(connection->kexinits[0]->parameters[i],
			       connection->kexinits[1]->parameters[i]);

	  if (!parameters[i])
	    {
	      send_disconnect(connection, "");
Niels Möller's avatar
Niels Möller committed
259
	      return WRITE_CLOSED;
Niels Möller's avatar
Niels Möller committed
260
261
262
263
264
265
266
267
	    }
	}

      algorithms = xalloc(KEX_PARAMETERS*sizeof(void *));

      for (i = 0; i<KEX_PARAMETERS; i++)
	algorithms[i] = ALIST_GET(closure->algorithms, parameters[i]);
      
Niels Möller's avatar
Niels Möller committed
268
269
      newkeys = make_newkeys_handler(ALIST_GET(closure->algorithms,
					       hostkey_algorithm),
Niels Möller's avatar
Niels Möller committed
270
271
272
273
274
275
				     algorithms);

      return KEYEXCHANGE_INIT(ALIST_GET(algorithms, kex_algorithm), connection);
    }
}

276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/* FIXME: THis function can't handle IV:s at all */
struct lsh_string *kex_make_key(struct hash_instance *secret,
				UINT32 key_length,
				int type,
				strut lsh_string *session_id)
{
  /* Indexed by the KEX_* values */
  static const char *tags = "CDEF";
  
  struct lsh_string *key;
  struct hash_instance *hash;
  UINT8 *digest;
  
  key = lsh_string_alloc(key_length);

  if (!key_length)
    return key;
  
  hash = HASH_COPY(secret);
  digest = alloca(hash->digest_size);

  HASH_UPDATE(hash, tags + type, 1); 
  HASH_UPDATE(hash, session_id->length, session_id->data);
  HASH_DIGEST(hash, digest);

  if (key_length < hash->digest_size)
    fatal("Not implemmented\n");

  memcpy(key->data, digest, key_length);
  lsh_free(hash);
  return key;
}
  
struct crypto_instance *kex_make_encrypt(struct hash_instance *secret,
					 struct crypto_algorithm *algorithm,
					 int type,
					 struct ssh_connection *connection)
{
  struct lsh_string *key = kex_make_key(secret, algorithm->key_size,
					type, connection->session_id);
  /* FIXME: No IV. Note that for DES, instantiating the crypto can
   * fail, if the key happens to be weak. */
  struct crypto_instance *crypt
    = MAKE_ENCRYPT(algorithm, key->data);

  lsh_string_free(key);
  return crypt;
}

struct crypto_instance *kex_make_decrypt(struct hash_instance *secret,
					 struct crypto_algorithm *algorithm,
					 int type,
					 struct ssh_connection *connection)
{
  struct lsh_string *key = kex_make_key(secret, algorithm->key_size,
					type, connection->session_id);
  /* FIXME: No IV. Note that for DES, instantiating the crypto can
   * fail, if the key happens to be weak. */
  struct crypto_instance *crypt
    = MAKE_DECRYPT(algorithm, key->data);

  lsh_string_free(key);
  return crypt;
}

struct mac_instance *kex_make_mac(struct hash_instance *secret,
				  struct mac_algorithm *algorithm,
				  int type,
				  struct ssh_connection *connection)
{
  struct lsh_string *key = kex_make_key(secret, algorithm->key_size,
					type, connection->session_id);

  struct mac_instance *mac
    = MAKE_MAC(algorithm, key->data);

  lsh_string_free(key);
  return mac;
}


Niels Möller's avatar
Niels Möller committed
357
358
359
360
361
362
363
364
365
366
static int do_handle_newkeys(struct packet_handler *c,
			     struct ssh_connection *connection,
			     struct lsh_string *packet)
{
  
struct packet_handler *
make_newkeys_handler(struct signature_algorithm *hostkey_algorithm,
		     void *parameters)
{
  
Niels Möller's avatar
Niels Möller committed
367
368