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
  
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
  
357
  
358
  
359
  
360
  
361
  
362
  
363
  
364
  
365
  
366
  
367
  
368
  
369
  
370
  
371
  
372
  
373
  
374
  
375
  
376
  
377
  
378
  
379
  
380
  
381
  
382
  
383
  
384
  
385
  
386
  
387
  
388
  
389
  
390
  
391
  
392
  
393
  
394
  
395
  
396
  
397
  
398
  
399
  
400
  
401
  
402
  
403
  
404
  
405
  
406
  
407
  
408
  
409
  
410
  
411
  
412
  
413
  
414
  
415
  
416
  
417
  
418
  
419
  
420
  
421
  
422
  
423
  
424
  
425
  
426
  
427
  
428
  
429
  
430
  
431
  
432
  
433
  
434
  
435
  
436
  
437
  
438
  
439
  
440
  
441
  
442
  
443
  
444
  
445
  
446
  
447
  
448
  
449
  
450
  
451
  
452
  
453
  
454
  
455
  
456
  
457
  
458
  
459
  
460
  
461
  
462
  
463
  
464
  
465
  
466
  
467
  
468
  
469
  
470
  
471
  
472
  
473
  
474
  
475
  
476
  
477
  
478
  
479
  
480
  
481
  
482
  
483
  
484
  
485
  
486
  
487
  
488
  
489
  
490
  
491
  
492
  
493
  
494
  
495
  
496
  
497
  
498
  
499
  
500
  
501
  
502
  
503
  
504
  
505
  
506
  
507
  
508
  
509
  
510
  
511
  
512
  
513
  
514
  
515
  
516
  
517
  
518
  
519
  
520
  
521
  
522
  
523
  
524
  
525
  
526
  
527
  
528
  
529
  
530
  
531
  
532
  
533
  
534
  
535
  
536
  
537
  
538
  
539
  
540
  
541
  
542
  
543
  
544
  
545
  
546
  
#pike 7.8 
#pragma no_deprecation_warnings 
 
#if constant(Crypto.Hash) 
 
//! Encryption and MAC algorithms used in SSL. 
 
import .Constants; 
 
//! Cipher algorithm interface. 
class CipherAlgorithm { 
  this_program set_encrypt_key(string); 
  this_program set_decrypt_key(string); 
  //! Set the key used for encryption/decryption, and 
  //! enter encryption mode. 
 
  int(0..) block_size(); 
  //! Return the block size for this crypto. 
 
  optional string crypt(string); 
  optional string unpad(string); 
  optional string pad(); 
 
  optional this_program set_iv(string); 
} 
 
//! Message Authentication Code interface. 
class MACAlgorithm { 
  //! @param packet 
  //!   @[Packet] to generate a MAC hash for. 
  //! @param seq_num 
  //!   Sequence number for the packet in the stream. 
  //! @returns 
  //!   Returns the MAC hash for the @[packet]. 
  string hash(object packet, Gmp.mpz seq_num); 
 
  //! Hashes the data with the hash algorithm and retuns it as a raw 
  //! binary string. 
  string hash_raw(string data); 
 
  //! The length of the header prefixed by @[hash()]. 
  constant hash_header_size = 13; 
} 
 
//! Cipher specification. 
class CipherSpec { 
  //! The algorithm to use for the bulk of the transfered data. 
  program(CipherAlgorithm) bulk_cipher_algorithm; 
 
  int cipher_type; 
 
  //! The Message Authentication Code to use for the packets. 
  program(MACAlgorithm) mac_algorithm; 
 
  //! The number of bytes in the MAC hashes. 
  int hash_size; 
 
  //! The number of bytes of key material used on initialization. 
  int key_material; 
 
  //! The number of bytes of random data needed for initialization vectors. 
  int iv_size; 
 
  //! The effective number of bits in @[key_material]. 
  //! 
  //! This is typically @expr{key_material * 8@}, but for eg @[DES] 
  //! this is @expr{key_material * 7@}. 
  int key_bits; 
 
  //! The function used to sign packets. 
  function(object,string,ADT.struct:ADT.struct) sign; 
 
  //! The function used to verify the signature for packets. 
  function(object,string,ADT.struct,Gmp.mpz:int(0..1)) verify; 
} 
 
//! MAC using SHA. 
//! 
//! @note 
//!   Note: This uses the algorithm from the SSL 3.0 draft. 
class MACsha 
{ 
  inherit MACAlgorithm; 
 
  protected constant pad_1 =  "6666666666666666666666666666666666666666"; 
  protected constant pad_2 = ("\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\" 
                           "\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"); 
 
  protected Crypto.Hash algorithm = Crypto.SHA1; 
  protected string secret; 
 
  //! The length of the header prefixed by @[hash()]. 
  constant hash_header_size = 11; 
 
  string hash_raw(string data) 
  { 
    return algorithm->hash(data); 
  } 
 
  //! 
  string hash(object packet, Gmp.mpz seq_num) 
  { 
    string s = sprintf("%~8s%c%2c%s", 
                       "\0\0\0\0\0\0\0\0", seq_num->digits(256), 
                       packet->content_type, sizeof(packet->fragment), 
                       packet->fragment); 
    return hash_raw(secret + pad_2 + 
                    hash_raw(secret + pad_1 + s)); 
  } 
 
  //! 
  string hash_master(string data, string|void s) 
  { 
    s = s || secret; 
    return hash_raw(s + pad_2 + 
                hash_raw(data + s + pad_1)); 
  } 
 
  //! 
  protected void create (string|void s) 
  { 
    secret = s || ""; 
  } 
} 
 
//! MAC using MD5. 
//! 
//! @note 
//!   Note: This uses the algorithm from the SSL 3.0 draft. 
class MACmd5 { 
  inherit MACsha; 
 
  protected constant pad_1 =  "666666666666666666666666666666666666666666666666"; 
  protected constant pad_2 = ("\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\" 
                           "\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"); 
 
  protected Crypto.Hash algorithm = Crypto.MD5; 
} 
 
//! HMAC using SHA. 
//! 
//! This is the MAC algorithm used by TLS 1.0 and later. 
class MAChmac_sha { 
  inherit MACAlgorithm; 
 
  protected string secret; 
  protected Crypto.HMAC hmac; 
 
  //! The length of the header prefixed by @[hash()]. 
  constant hash_header_size = 13; 
 
  string hash_raw(string data) 
  { 
    return hmac(secret)(data); 
  } 
 
  //! 
  string hash(object packet, Gmp.mpz seq_num) { 
 
    string s = sprintf("%~8s%c%c%c%2c%s", 
                       "\0\0\0\0\0\0\0\0", seq_num->digits(256), 
                       packet->content_type, 
                       packet->protocol_version[0],packet->protocol_version[1], 
                       sizeof(packet->fragment), 
                       packet->fragment); 
 
    return hmac(secret)(s); 
  } 
 
  //! 
  protected void create(string|void s) { 
    secret = s || ""; 
    hmac=Crypto.HMAC(Crypto.SHA1); 
  } 
} 
 
//! HMAC using MD5. 
//! 
//! This is the MAC algorithm used by TLS 1.0 and later. 
class MAChmac_md5 { 
  inherit MAChmac_sha; 
 
  //! 
  protected void create(string|void s) { 
    secret = s || ""; 
    hmac=Crypto.HMAC(Crypto.MD5); 
  } 
} 
 
// Hashfn is either a Crypto.MD5 or Crypto.SHA 
protected string P_hash(Crypto.Hash hashfn, int hlen, string secret, 
                     string seed, int len) { 
 
  Crypto.HMAC hmac=Crypto.HMAC(hashfn); 
  string temp=seed; 
  string res=""; 
 
  int noblocks=(int)ceil((1.0*len)/hlen); 
 
  for(int i=0 ; i<noblocks ; i++) { 
    temp=hmac(secret)(temp); 
    res+=hmac(secret)(temp+seed); 
  } 
  return res[..(len-1)]; 
} 
 
//! The Pseudo Random Function used to derive the secret keys. 
string prf(string secret,string label,string seed,int len) { 
 
  string s1=secret[..(int)(ceil(sizeof(secret)/2.0)-1)]; 
  string s2=secret[(int)(floor(sizeof(secret)/2.0))..]; 
 
  string a=P_hash(Crypto.MD5,16,s1,label+seed,len); 
  string b=P_hash(Crypto.SHA1,20,s2,label+seed,len); 
 
  return a ^ b; 
} 
 
//! 
class DES 
{ 
  inherit Crypto.CBC; 
 
  protected void create() { ::create(Crypto.DES()); } 
 
  this_program set_encrypt_key(string k) 
  { 
    ::set_encrypt_key(Crypto.DES->fix_parity(k)); 
    return this; 
  } 
 
  this_program set_decrypt_key(string k) 
  { 
    ::set_decrypt_key(Crypto.DES->fix_parity(k)); 
    return this; 
  } 
} 
 
//! 
class DES3 
{ 
  inherit Crypto.CBC; 
 
  protected void create() { 
    ::create(Crypto.DES3()); 
  } 
 
  this_program set_encrypt_key(string k) 
  { 
    ::set_encrypt_key(Crypto.DES3->fix_parity(k)); 
    return this; 
  } 
 
  this_program set_decrypt_key(string k) 
  { 
    ::set_decrypt_key(Crypto.DES3->fix_parity(k)); 
    return this; 
  } 
} 
 
//! 
class IDEA 
{ 
  inherit Crypto.CBC; 
  protected void create() { ::create(Crypto.IDEA()); } 
} 
 
//! 
class AES 
{ 
  inherit Crypto.CBC; 
  protected void create() { ::create(Crypto.AES()); } 
} 
 
//! Signing using RSA. 
ADT.struct rsa_sign(object context, string cookie, ADT.struct struct) 
{ 
  /* Exactly how is the signature process defined? */ 
 
  string params = cookie + struct->contents(); 
  string digest = Crypto.MD5->hash(params) + Crypto.SHA1->hash(params); 
 
  object s = context->rsa->raw_sign(digest); 
 
  struct->put_bignum(s); 
  return struct; 
} 
 
//! Verify an RSA signature. 
int(0..1) rsa_verify(object context, string cookie, ADT.struct struct, 
               Gmp.mpz signature) 
{ 
  /* Exactly how is the signature process defined? */ 
 
  string params = cookie + struct->contents(); 
  string digest = Crypto.MD5->hash(params) + Crypto.SHA1->hash(params); 
 
  return context->rsa->raw_verify(digest, signature); 
} 
 
//! Signing using DSA. 
ADT.struct dsa_sign(object context, string cookie, ADT.struct struct) 
{ 
  /* NOTE: The details are not described in the SSL 3 spec. */ 
  string s = context->dsa->sign_ssl(cookie + struct->contents()); 
  struct->put_var_string(s, 2); 
  return struct; 
} 
 
//! The NULL signing method. 
ADT.struct anon_sign(object context, string cookie, ADT.struct struct) 
{ 
  return struct; 
} 
 
//! Diffie-Hellman parameters. 
class DHParameters 
{ 
  Gmp.mpz p, g, order; 
  //! 
 
  /* Default prime and generator, taken from the ssh2 spec: 
   * 
   * "This group was taken from the ISAKMP/Oakley specification, and was 
   *  originally generated by Richard Schroeppel at the University of Arizona. 
   *  Properties of this prime are described in [Orm96]. 
   *... 
   *  [Orm96] Orman, H., "The Oakley Key Determination Protocol", version 1, 
   *  TR97-92, Department of Computer Science Technical Report, University of 
   *  Arizona." 
   */ 
 
  /* p = 2^1024 - 2^960 - 1 + 2^64 * floor( 2^894 Pi + 129093 ) */ 
 
  protected Gmp.mpz orm96() { 
    p = Gmp.mpz("FFFFFFFF FFFFFFFF C90FDAA2 2168C234 C4C6628B 80DC1CD1" 
                "29024E08 8A67CC74 020BBEA6 3B139B22 514A0879 8E3404DD" 
                "EF9519B3 CD3A431B 302B0A6D F25F1437 4FE1356D 6D51C245" 
                "E485B576 625E7EC6 F44C42E9 A637ED6B 0BFF5CB6 F406B7ED" 
                "EE386BFB 5A899FA5 AE9F2411 7C4B1FE6 49286651 ECE65381" 
                "FFFFFFFF FFFFFFFF", 16); 
    order = (p-1) / 2; 
 
    g = Gmp.mpz(2); 
 
    return this; 
  } 
 
  //! 
  protected void create(object ... args) { 
    switch (sizeof(args)) 
    { 
    case 0: 
      orm96(); 
      break; 
    case 3: 
      [p, g, order] = args; 
      break; 
    default: 
      error( "Wrong number of arguments.\n" ); 
    } 
  } 
} 
 
//! Implements Diffie-Hellman key-exchange. 
//! 
//! The following key exchange methods are implemented here: 
//! @[KE_dhe_dss], @[KE_dhe_rsa] and @[KE_dh_anon]. 
class DHKeyExchange 
{ 
  /* Public parameters */ 
  DHParameters parameters; 
 
  Gmp.mpz our; /* Our value */ 
  Gmp.mpz other; /* Other party's value */ 
  Gmp.mpz secret; /* our =  g ^ secret mod p */ 
 
  //! 
  protected void create(DHParameters p) { 
    parameters = p; 
  } 
 
  this_program new_secret(function random) { 
    secret = Gmp.mpz(random( (parameters->order->size() + 10 / 8)), 256) 
      % (parameters->order - 1) + 1; 
 
    our = parameters->g->powm(secret, parameters->p); 
    return this; 
  } 
 
  this_program set_other(Gmp.mpz o) { 
    other = o; 
    return this; 
  } 
 
  Gmp.mpz get_shared() { 
    return other->powm(secret, parameters->p); 
  } 
} 
 
//! Lookup the crypto parameters for a cipher suite. 
//! 
//! @param suite 
//!   Cipher suite to lookup. 
//! 
//! @param version 
//!   Minor version of the SSL protocol to support. 
//! 
//! @returns 
//!   Returns @expr{0@} (zero) for unsupported combinations. 
//!   Otherwise returns an array with the following fields: 
//!   @array 
//!     @elem KeyExchangeType 0 
//!       Key exchange method. 
//!     @elem CipherSpec 1 
//!       Initialized @[CipherSpec] for the @[suite]. 
//!   @endarray 
array lookup(int suite, ProtocolVersion|int version) 
{ 
  CipherSpec res = CipherSpec(); 
  int ke_method; 
 
  array algorithms = CIPHER_SUITES[suite]; 
  if (!algorithms) 
    return 0; 
 
  ke_method = algorithms[0]; 
 
  switch(ke_method) 
  { 
  case KE_rsa: 
  case KE_dhe_rsa: 
    res->sign = rsa_sign; 
    res->verify = rsa_verify; 
    break; 
  case KE_dhe_dss: 
    res->sign = dsa_sign; 
    break; 
  case KE_dh_anon: 
    res->sign = anon_sign; 
    break; 
  default: 
    error( "Internal error.\n" ); 
  } 
 
  switch(algorithms[1]) 
  { 
  case CIPHER_rc4_40: 
    res->bulk_cipher_algorithm = Crypto.Arcfour.State; 
    res->cipher_type = CIPHER_stream; 
    res->key_material = 16; 
    res->iv_size = 0; 
    res->key_bits = 40; 
    break; 
  case CIPHER_des40: 
    res->bulk_cipher_algorithm = DES; 
    res->cipher_type = CIPHER_block; 
    res->key_material = 8; 
    res->iv_size = 8; 
    res->key_bits = 40; 
    break; 
  case CIPHER_null: 
    res->bulk_cipher_algorithm = 0; 
    res->cipher_type = CIPHER_stream; 
    res->key_material = 0; 
    res->iv_size = 0; 
    res->key_bits = 0; 
    break; 
  case CIPHER_rc4: 
    res->bulk_cipher_algorithm = Crypto.Arcfour.State; 
    res->cipher_type = CIPHER_stream; 
    res->key_material = 16; 
    res->iv_size = 0; 
    res->key_bits = 128; 
    break; 
  case CIPHER_des: 
    res->bulk_cipher_algorithm = DES; 
    res->cipher_type = CIPHER_block; 
    res->key_material = 8; 
    res->iv_size = 8; 
    res->key_bits = 56; 
    break; 
  case CIPHER_3des: 
    res->bulk_cipher_algorithm = DES3; 
    res->cipher_type = CIPHER_block; 
    res->key_material = 24; 
    res->iv_size = 8; 
    res->key_bits = 168; 
    break; 
  case CIPHER_idea: 
    res->bulk_cipher_algorithm = IDEA; 
    res->cipher_type = CIPHER_block; 
    res->key_material = 16; 
    res->iv_size = 8; 
    res->key_bits = 128; 
    break; 
  case CIPHER_aes: 
    res->bulk_cipher_algorithm = AES; 
    res->cipher_type = CIPHER_block; 
    res->key_material = 16; 
    res->iv_size = 16; 
    res->key_bits = 128; 
    break; 
  case CIPHER_aes256: 
    res->bulk_cipher_algorithm = AES; 
    res->cipher_type = CIPHER_block; 
    res->key_material = 32; 
    res->iv_size = 16; 
    res->key_bits = 256; 
    break; 
  default: 
    return 0; 
  } 
 
  switch(algorithms[2]) 
  { 
  case HASH_sha: 
    if(version >= PROTOCOL_TLS_1_0) 
      res->mac_algorithm = MAChmac_sha; 
    else 
      res->mac_algorithm = MACsha; 
    res->hash_size = 20; 
    break; 
  case HASH_md5: 
    if(version >= PROTOCOL_TLS_1_0) 
      res->mac_algorithm = MAChmac_md5; 
    else 
      res->mac_algorithm = MACmd5; 
    res->hash_size = 16; 
    break; 
  case 0: 
    res->mac_algorithm = 0; 
    res->hash_size = 0; 
    break; 
  default: 
    return 0; 
  } 
 
  return ({ ke_method, res }); 
} 
 
#else // constant(Crypto.Hash) 
constant this_program_does_not_exist = 1; 
#endif