keyexchange.c 2.14 KB
Newer Older
Niels Möller's avatar
Niels Möller committed
1
2
3
4
5
6
7
8
9
10
/* keyexchange.c
 *
 */

#include "keyexchange.h"
#include "parse.h"


#define NLISTS 10

Niels Möller's avatar
Niels Möller committed
11
struct kexinit *parse_kexinit(struct lsh_string *packet)
Niels Möller's avatar
Niels Möller committed
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
{
  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;

  res = xalloc(sizeof(keyexinit));

  if (!parse_string(&buffer, 16, &res->cookie))
    {
      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;
    }
  
  res->kex_algorithms = list[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];

  return res;
}
Niels Möller's avatar
Niels Möller committed
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94

static int do_handle_kexinit(struct abstract_write **w,
			     struct lsh_string *packet)
{
  struct handle_kexinit_packet *closure = (struct handle_kexinit_packet *) *w;
  struct kexinit *msg = parse_kexinit(packet);

  if (!msg)
    return 0;

  lsh_free(packet);

  return HANDLE_KEXINIT(closure->handler, msg);
}

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;
}