1
  
2
  
3
  
4
  
5
  
6
  
7
  
8
  
9
  
10
  
11
  
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
  
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
  
95
  
96
  
97
  
98
  
99
  
100
  
101
  
102
  
103
  
104
  
105
  
106
  
107
  
108
  
109
  
110
  
111
  
112
  
113
  
114
  
115
  
116
  
117
  
118
  
119
  
120
  
121
  
122
  
123
  
124
  
125
  
126
  
127
  
128
  
129
  
130
  
131
  
132
  
133
  
134
  
135
  
136
  
137
  
138
  
139
  
140
  
141
  
142
  
143
  
144
  
145
  
146
  
147
  
148
  
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
  
197
  
198
  
199
  
200
  
201
  
202
  
203
  
204
  
205
  
206
  
207
  
208
  
209
  
210
  
211
  
212
  
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
  
249
  
250
  
251
  
252
  
253
  
254
  
255
  
256
  
257
  
258
  
259
  
260
  
261
  
262
  
263
  
264
  
265
  
266
  
267
  
268
  
269
  
270
  
271
  
272
  
273
  
274
  
275
  
276
  
277
  
278
  
279
  
280
  
281
  
282
  
283
  
284
  
285
  
286
  
287
  
288
  
289
  
#pike 7.8 
#pragma no_deprecation_warnings 
// #pragma strict_types 
 
//! The most important information in a session object is a 
//! choice of encryption algorithms and a "master secret" created by 
//! keyexchange with a client. Each connection can either do a full key 
//! exchange to established a new session, or reuse a previously 
//! established session. That is why we have the session abstraction and 
//! the session cache. Each session is used by one or more connections, in 
//! sequence or simultaneously. 
//! 
//! It is also possible to change to a new session in the middle of a 
//! connection. 
 
#if constant(SSL.Cipher.CipherSpec) 
 
import .Constants; 
protected constant Struct = ADT.struct; 
 
//! Identifies the session to the server 
string identity; 
 
//! Always COMPRESSION_null. 
int compression_algorithm; 
 
//! Constant defining a choice of keyexchange, encryption and mac 
//! algorithm. 
int cipher_suite; 
 
//! Information about the encryption method derived from the 
//! cipher_suite. 
.Cipher.CipherSpec cipher_spec; 
 
//! Key exchange method, also derived from the cipher_suite. 
int ke_method; 
 
//! 48 byte secret shared between the client and the server. Used for 
//! deriving the actual keys. 
string master_secret; 
 
//! information about the certificate in use by the peer, such as issuing authority, and verification status. 
mapping cert_data; 
 
array(int) version; 
 
//! the peer certificate chain 
array(string) peer_certificate_chain; 
 
//! our certificate chain 
array(string) certificate_chain; 
 
//! The server's private key 
Crypto.RSA rsa; 
 
//! The server's dsa private key 
Crypto.DSA dsa; 
 
//! Sets the proper authentication method and cipher specification 
//! for the given cipher @[suite] and @[verison]. 
void set_cipher_suite(int suite, ProtocolVersion|int version) 
{ 
  array res = .Cipher.lookup(suite, version); 
  cipher_suite = suite; 
  ke_method = [int]res[0]; 
  cipher_spec = [object(.Cipher.CipherSpec)]res[1]; 
#ifdef SSL3_DEBUG 
  werror("SSL.session: cipher_spec %O\n", 
         mkmapping(indices(cipher_spec), values(cipher_spec))); 
#endif 
} 
 
//! Sets the compression method. Currently only @[COMPRESSION_null] is 
//! supported. 
void set_compression_method(int compr) 
{ 
  if (compr != COMPRESSION_null) 
    error( "Method not supported\n" ); 
  compression_algorithm = compr; 
} 
 
protected string generate_key_block(string client_random, string server_random, 
                          array(int) version) 
{ 
  int required = 2 * ( cipher_spec->key_material + 
                       cipher_spec->hash_size + 
                       cipher_spec->iv_size ); 
  string key = ""; 
 
  if(version[1] == PROTOCOL_SSL_3_0) { 
    .Cipher.MACsha sha = .Cipher.MACsha(); 
    .Cipher.MACmd5 md5 = .Cipher.MACmd5(); 
    int i = 0; 
 
    while (sizeof(key) < required) 
      { 
        i++; 
        string cookie = replace(allocate(i), 0, sprintf("%c", 64+i)) * ""; 
#ifdef SSL3_DEBUG 
      werror("cookie %O\n", cookie); 
#endif 
      key += md5->hash_raw(master_secret + 
                             sha->hash_raw(cookie + master_secret + 
                                           server_random + client_random)); 
      } 
  } 
  else if (version[1] >= PROTOCOL_TLS_1_0) { 
    // TLS 1.0 or later. 
    key = .Cipher.prf(master_secret, "key expansion", 
                      server_random+client_random, required); 
  } 
 
#ifdef SSL3_DEBUG 
  werror("key_block: %O\n", key); 
#endif 
  return key; 
} 
 
#ifdef SSL3_DEBUG 
protected void printKey(string name, string key) { 
 
  string res=""; 
  res+=sprintf("%s:  len:%d type:%d \t\t",name,sizeof(key),0); 
  /* return; */ 
  for(int i=0;i<sizeof(key);i++) { 
    int d=key[i]; 
    res+=sprintf("%02x ",d&0xff); 
  } 
  res+="\n"; 
  werror(res); 
} 
#endif 
 
//! Generates keys appropriate for the SSL version given in @[version], 
//! based on the @[client_random] and @[server_random]. 
//! @returns 
//!   @array 
//!     @elem string 0 
//!       Client write MAC secret 
//!     @elem string 1 
//!       Server write MAC secret 
//!     @elem string 2 
//!       Client write key 
//!     @elem string 3 
//!       Server write key 
//!     @elem string 4 
//!       Client write IV 
//!     @elem string 5 
//!       Server write IV 
//!  @endarray 
array(string) generate_keys(string client_random, string server_random, 
                            array(int) version) 
{ 
  Struct key_data = Struct(generate_key_block(client_random, server_random, 
                                              version)); 
  array(string) keys = allocate(6); 
 
#ifdef SSL3_DEBUG 
  werror("client_random: %O\nserver_random: %O\nversion: %d.%d\n", 
         client_random, server_random, version[0], version[1]); 
#endif 
  // client_write_MAC_secret 
  keys[0] = key_data->get_fix_string(cipher_spec->hash_size); 
  // server_write_MAC_secret 
  keys[1] = key_data->get_fix_string(cipher_spec->hash_size); 
 
  keys[2] = key_data->get_fix_string(cipher_spec->key_material); 
  keys[3] = key_data->get_fix_string(cipher_spec->key_material); 
  if (cipher_spec->iv_size) 
  { 
    keys[4] = key_data->get_fix_string(cipher_spec->iv_size); 
    keys[5] = key_data->get_fix_string(cipher_spec->iv_size); 
  } 
 
#ifdef SSL3_DEBUG 
  printKey( "client_write_MAC_secret",keys[0]); 
  printKey( "server_write_MAC_secret",keys[1]); 
  printKey( "keys[2]",keys[2]); 
  printKey( "keys[3]",keys[3]); 
 
  if(cipher_spec->iv_size) { 
    printKey( "keys[4]",keys[4]); 
    printKey( "keys[5]",keys[5]); 
 
  } else { 
    werror("No IVs!!\n"); 
  } 
#endif 
 
  return keys; 
} 
 
//! Computes a new set of encryption states, derived from the 
//! client_random, server_random and master_secret strings. 
//! 
//! @returns 
//!   @array 
//!     @elem SSL.state read_state 
//!       Read state 
//!     @elem SSL.state write_state 
//!       Write state 
//!   @endarray 
array(.state) new_server_states(string client_random, string server_random, 
                                array(int) version) 
{ 
  .state write_state = .state(this); 
  .state read_state = .state(this); 
  array(string) keys = generate_keys(client_random, server_random,version); 
 
  if (cipher_spec->mac_algorithm) 
  { 
    read_state->mac = cipher_spec->mac_algorithm(keys[0]); 
    write_state->mac = cipher_spec->mac_algorithm(keys[1]); 
  } 
  if (cipher_spec->bulk_cipher_algorithm) 
  { 
    read_state->crypt = cipher_spec->bulk_cipher_algorithm(); 
    read_state->crypt->set_decrypt_key(keys[2]); 
    write_state->crypt = cipher_spec->bulk_cipher_algorithm(); 
    write_state->crypt->set_encrypt_key(keys[3]); 
    if (cipher_spec->cipher_type == CIPHER_block) 
    { // Crypto.Buffer takes care of splitting input into blocks 
      read_state->crypt = Crypto.Buffer(read_state->crypt); 
      write_state->crypt = Crypto.Buffer(write_state->crypt); 
    } 
    if (cipher_spec->iv_size) 
    { 
      if (version[1] >= PROTOCOL_TLS_1_1) { 
        // TLS 1.1 and later have an explicit IV. 
        read_state->tls_iv = write_state->tls_iv = cipher_spec->iv_size; 
      } 
      read_state->crypt->set_iv(keys[4]); 
      write_state->crypt->set_iv(keys[5]); 
    } 
  } 
  return ({ read_state, write_state }); 
} 
 
//! Computes a new set of encryption states, derived from the 
//! client_random, server_random and master_secret strings. 
//! 
//! @returns 
//!   @array 
//!     @elem SSL.state read_state 
//!       Read state 
//!     @elem SSL.state write_state 
//!       Write state 
//!   @endarray 
array(.state) new_client_states(string client_random, string server_random, 
                                array(int) version) 
{ 
  .state write_state = .state(this); 
  .state read_state = .state(this); 
  array(string) keys = generate_keys(client_random, server_random,version); 
 
  if (cipher_spec->mac_algorithm) 
  { 
    read_state->mac = cipher_spec->mac_algorithm(keys[1]); 
    write_state->mac = cipher_spec->mac_algorithm(keys[0]); 
  } 
  if (cipher_spec->bulk_cipher_algorithm) 
  { 
    read_state->crypt = cipher_spec->bulk_cipher_algorithm(); 
    read_state->crypt->set_decrypt_key(keys[3]); 
    write_state->crypt = cipher_spec->bulk_cipher_algorithm(); 
    write_state->crypt->set_encrypt_key(keys[2]); 
    if (cipher_spec->cipher_type == CIPHER_block) 
    { // Crypto.Buffer takes care of splitting input into blocks 
      read_state->crypt = Crypto.Buffer(read_state->crypt); 
      write_state->crypt = Crypto.Buffer(write_state->crypt); 
    } 
    if (cipher_spec->iv_size) 
    { 
      if (version[1] >= PROTOCOL_TLS_1_1) { 
        // TLS 1.1 and later have an explicit IV. 
        read_state->tls_iv = write_state->tls_iv = cipher_spec->iv_size; 
      } 
      read_state->crypt->set_iv(keys[5]); 
      write_state->crypt->set_iv(keys[4]); 
    } 
  } 
  return ({ read_state, write_state }); 
} 
 
#else // constant(SSL.Cipher.CipherSpec) 
constant this_program_does_not_exist = 1; 
#endif