/[rdesktop]/sourceforge.net/trunk/rdesktop/secure.c
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /sourceforge.net/trunk/rdesktop/secure.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 10 by matty, Tue Aug 15 10:23:24 2000 UTC revision 24 by matty, Sat Jan 6 03:12:10 2001 UTC
# Line 45  static uint8 sec_crypted_random[64]; Line 45  static uint8 sec_crypted_random[64];
45   * a client and server salt) and a global salt value used for padding.   * a client and server salt) and a global salt value used for padding.
46   * Both SHA1 and MD5 algorithms are used.   * Both SHA1 and MD5 algorithms are used.
47   */   */
48  void sec_hash_48(uint8 *out, uint8 *in, uint8 *salt1, uint8 *salt2, uint8 salt)  void sec_hash_48(uint8 *out, uint8 *in, uint8 *salt1, uint8 *salt2,
49                     uint8 salt)
50  {  {
51          uint8 shasig[20];          uint8 shasig[20];
52          uint8 pad[4];          uint8 pad[4];
# Line 55  void sec_hash_48(uint8 *out, uint8 *in, Line 56  void sec_hash_48(uint8 *out, uint8 *in,
56    
57          for (i = 0; i < 3; i++)          for (i = 0; i < 3; i++)
58          {          {
59                  memset(pad, salt+i, i+1);                  memset(pad, salt + i, i + 1);
60    
61                  SHA1_Init(&sha);                  SHA1_Init(&sha);
62                  SHA1_Update(&sha, pad, i+1);                  SHA1_Update(&sha, pad, i + 1);
63                  SHA1_Update(&sha, in, 48);                  SHA1_Update(&sha, in, 48);
64                  SHA1_Update(&sha, salt1, 32);                  SHA1_Update(&sha, salt1, 32);
65                  SHA1_Update(&sha, salt2, 32);                  SHA1_Update(&sha, salt2, 32);
# Line 67  void sec_hash_48(uint8 *out, uint8 *in, Line 68  void sec_hash_48(uint8 *out, uint8 *in,
68                  MD5_Init(&md5);                  MD5_Init(&md5);
69                  MD5_Update(&md5, in, 48);                  MD5_Update(&md5, in, 48);
70                  MD5_Update(&md5, shasig, 20);                  MD5_Update(&md5, shasig, 20);
71                  MD5_Final(&out[i*16], &md5);                  MD5_Final(&out[i * 16], &md5);
72          }          }
73  }  }
74    
# Line 103  static void sec_generate_keys(uint8 *cli Line 104  static void sec_generate_keys(uint8 *cli
104          uint8 input[48];          uint8 input[48];
105    
106          /* Construct input data to hash */          /* Construct input data to hash */
107          memcpy(input,    client_key, 24);          memcpy(input, client_key, 24);
108          memcpy(input+24, server_key, 24);          memcpy(input + 24, server_key, 24);
109    
110          /* Generate session key - two rounds of sec_hash_48 */          /* Generate session key - two rounds of sec_hash_48 */
111          sec_hash_48(temp_hash,   input,     client_key, server_key, 65);          sec_hash_48(temp_hash, input, client_key, server_key, 65);
112          sec_hash_48(session_key, temp_hash, client_key, server_key, 88);          sec_hash_48(session_key, temp_hash, client_key, server_key, 88);
113    
114          /* Store first 8 bytes of session key, for generating signatures */          /* Store first 8 bytes of session key, for generating signatures */
115          memcpy(sec_sign_key, session_key, 8);          memcpy(sec_sign_key, session_key, 8);
116    
117          /* Generate RC4 keys */          /* Generate RC4 keys */
118          sec_hash_16(sec_decrypt_key, &session_key[16], client_key, server_key);          sec_hash_16(sec_decrypt_key, &session_key[16], client_key,
119          sec_hash_16(sec_encrypt_key, &session_key[32], client_key, server_key);                      server_key);
120            sec_hash_16(sec_encrypt_key, &session_key[32], client_key,
121                        server_key);
122    
123          if (rc4_key_size == 1)          if (rc4_key_size == 1)
124          {          {
# Line 140  static void sec_generate_keys(uint8 *cli Line 143  static void sec_generate_keys(uint8 *cli
143          RC4_set_key(&rc4_encrypt_key, rc4_key_len, sec_encrypt_key);          RC4_set_key(&rc4_encrypt_key, rc4_key_len, sec_encrypt_key);
144  }  }
145    
146  static uint8 pad_54[40] =  static uint8 pad_54[40] = {
147  {          54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54,
148          54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,                  54, 54, 54,
149          54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54          54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54,
150                    54, 54, 54
151  };  };
152    
153  static uint8 pad_92[48] =  static uint8 pad_92[48] = {
154  {          92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92,
155          92,92,92,92,92,92,92,92,92,92,92,92,92,92,92,92,92,92,92,92,92,92,92,92,                  92, 92, 92, 92, 92, 92, 92,
156          92,92,92,92,92,92,92,92,92,92,92,92,92,92,92,92,92,92,92,92,92,92,92,92          92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92,
157                    92, 92, 92, 92, 92, 92, 92
158  };  };
159    
160  /* Output a uint32 into a buffer (little-endian) */  /* Output a uint32 into a buffer (little-endian) */
# Line 163  void buf_out_uint32(uint8 *buffer, uint3 Line 168  void buf_out_uint32(uint8 *buffer, uint3
168    
169  /* Generate a signature hash, using a combination of SHA1 and MD5 */  /* Generate a signature hash, using a combination of SHA1 and MD5 */
170  void sec_sign(uint8 *signature, uint8 *session_key, int length,  void sec_sign(uint8 *signature, uint8 *session_key, int length,
171                  uint8 *data, int datalen)                uint8 *data, int datalen)
172  {  {
173          uint8 shasig[20];          uint8 shasig[20];
174          uint8 md5sig[16];          uint8 md5sig[16];
# Line 171  void sec_sign(uint8 *signature, uint8 *s Line 176  void sec_sign(uint8 *signature, uint8 *s
176          SHA_CTX sha;          SHA_CTX sha;
177          MD5_CTX md5;          MD5_CTX md5;
178    
179          buf_out_uint32(lenhdr, datalen);          buf_out_uint32(lenhdr, datalen);
180    
181          SHA1_Init(&sha);          SHA1_Init(&sha);
182          SHA1_Update(&sha, session_key, length);          SHA1_Update(&sha, session_key, length);
# Line 249  static void sec_decrypt(uint8 *data, int Line 254  static void sec_decrypt(uint8 *data, int
254  }  }
255    
256  /* Read in a NUMBER from a buffer */  /* Read in a NUMBER from a buffer */
257  static void sec_read_number(NUMBER *num, uint8 *buffer, int len)  static void sec_read_number(NUMBER * num, uint8 *buffer, int len)
258  {  {
259          INT *data = num->n_part;          INT *data = num->n_part;
260          int i, j;          int i, j;
261    
262          for (i = 0, j = 0; j < len; i++, j += 2)          for (i = 0, j = 0; j < len; i++, j += 2)
263                  data[i] = buffer[j] | (buffer[j+1] << 8);                  data[i] = buffer[j] | (buffer[j + 1] << 8);
264    
265          num->n_len = i;          num->n_len = i;
266  }  }
267    
268  /* Write a NUMBER to a buffer */  /* Write a NUMBER to a buffer */
269  static void sec_write_number(NUMBER *num, uint8 *buffer, int len)  static void sec_write_number(NUMBER * num, uint8 *buffer, int len)
270  {  {
271          INT *data = num->n_part;          INT *data = num->n_part;
272          int i, j;          int i, j;
# Line 269  static void sec_write_number(NUMBER *num Line 274  static void sec_write_number(NUMBER *num
274          for (i = 0, j = 0; j < len; i++, j += 2)          for (i = 0, j = 0; j < len; i++, j += 2)
275          {          {
276                  buffer[j] = data[i] & 0xff;                  buffer[j] = data[i] & 0xff;
277                  buffer[j+1] = data[i] >> 8;                  buffer[j + 1] = data[i] >> 8;
278          }          }
279  }  }
280    
# Line 297  STREAM sec_init(uint32 flags, int maxlen Line 302  STREAM sec_init(uint32 flags, int maxlen
302          STREAM s;          STREAM s;
303    
304          hdrlen = (flags & SEC_ENCRYPT) ? 12 : 4;          hdrlen = (flags & SEC_ENCRYPT) ? 12 : 4;
305          s = mcs_init(maxlen + hdrlen);          s = mcs_init(maxlen + hdrlen);
306          s_push_layer(s, sec_hdr, hdrlen);          s_push_layer(s, sec_hdr, hdrlen);
307    
308          return s;          return s;
# Line 318  void sec_send(STREAM s, uint32 flags) Line 323  void sec_send(STREAM s, uint32 flags)
323    
324  #if RDP_DEBUG  #if RDP_DEBUG
325                  DEBUG("Sending encrypted packet:\n");                  DEBUG("Sending encrypted packet:\n");
326                  hexdump(s->p+8, datalen);                  hexdump(s->p + 8, datalen);
327  #endif  #endif
328    
329                  sec_sign(s->p, sec_sign_key, 8, s->p+8, datalen);                  sec_sign(s->p, sec_sign_key, 8, s->p + 8, datalen);
330                  sec_encrypt(s->p+8, datalen);                  sec_encrypt(s->p + 8, datalen);
331          }          }
332    
333          mcs_send(s);          mcs_send(s);
# Line 355  static void sec_out_mcs_data(STREAM s) Line 360  static void sec_out_mcs_data(STREAM s)
360          out_uint8(s, 0x7c);          out_uint8(s, 0x7c);
361          out_uint16_be(s, 1);          out_uint16_be(s, 1);
362    
363          out_uint16_be(s, (158 | 0x8000)); /* remaining length */          out_uint16_be(s, (158 | 0x8000));       /* remaining length */
364    
365          out_uint16_be(s, 8);    /* length? */          out_uint16_be(s, 8);    /* length? */
366          out_uint16_be(s, 16);          out_uint16_be(s, 16);
# Line 363  static void sec_out_mcs_data(STREAM s) Line 368  static void sec_out_mcs_data(STREAM s)
368          out_uint16_le(s, 0xc001);          out_uint16_le(s, 0xc001);
369          out_uint8(s, 0);          out_uint8(s, 0);
370    
371          out_uint32_le(s, 0x61637544); /* "Duca" ?! */          out_uint32_le(s, 0x61637544);   /* "Duca" ?! */
372          out_uint16_be(s, (144 | 0x8000)); /* remaining length */          out_uint16_be(s, (144 | 0x8000));       /* remaining length */
373    
374          /* Client information */          /* Client information */
375          out_uint16_le(s, SEC_TAG_CLI_INFO);          out_uint16_le(s, SEC_TAG_CLI_INFO);
# Line 376  static void sec_out_mcs_data(STREAM s) Line 381  static void sec_out_mcs_data(STREAM s)
381          out_uint16_le(s, 0xca01);          out_uint16_le(s, 0xca01);
382          out_uint16_le(s, 0xaa03);          out_uint16_le(s, 0xaa03);
383          out_uint32_le(s, keylayout);          out_uint32_le(s, keylayout);
384          out_uint32_le(s, 419);   /* client build? we are 419 compatible :-) */          out_uint32_le(s, 419);  /* client build? we are 419 compatible :-) */
385    
386          /* Unicode name of client, padded to 32 bytes */          /* Unicode name of client, padded to 32 bytes */
387          rdp_out_unistr(s, hostname, hostlen);          rdp_out_unistr(s, hostname, hostlen);
388          out_uint8s(s, 30-hostlen);          out_uint8s(s, 30 - hostlen);
389    
390          out_uint32_le(s, 4);          out_uint32_le(s, 4);
391          out_uint32(s, 0);          out_uint32(s, 0);
392          out_uint32_le(s, 12);          out_uint32_le(s, 12);
393          out_uint8s(s, 64); /* reserved? 4 + 12 doublewords */          out_uint8s(s, 64);      /* reserved? 4 + 12 doublewords */
394    
395          out_uint16(s, 0xca01);          out_uint16(s, 0xca01);
396          out_uint16(s, 0);          out_uint16(s, 0);
# Line 416  static BOOL sec_parse_public_key(STREAM Line 421  static BOOL sec_parse_public_key(STREAM
421                  return False;                  return False;
422          }          }
423    
424          in_uint8s(s, 8); /* modulus_bits, unknown */          in_uint8s(s, 8);        /* modulus_bits, unknown */
425          in_uint8p(s, *exponent, SEC_EXPONENT_SIZE);          in_uint8p(s, *exponent, SEC_EXPONENT_SIZE);
426          in_uint8p(s, *modulus, SEC_MODULUS_SIZE);          in_uint8p(s, *modulus, SEC_MODULUS_SIZE);
427          in_uint8s(s, SEC_PADDING_SIZE);          in_uint8s(s, SEC_PADDING_SIZE);
# Line 426  static BOOL sec_parse_public_key(STREAM Line 431  static BOOL sec_parse_public_key(STREAM
431    
432  /* Parse a crypto information structure */  /* Parse a crypto information structure */
433  static BOOL sec_parse_crypt_info(STREAM s, uint32 *rc4_key_size,  static BOOL sec_parse_crypt_info(STREAM s, uint32 *rc4_key_size,
434                  uint8 **server_random, uint8 **modulus, uint8 **exponent)                                   uint8 **server_random, uint8 **modulus,
435                                     uint8 **exponent)
436  {  {
437          uint32 crypt_level, random_len, rsa_info_len;          uint32 crypt_level, random_len, rsa_info_len;
438          uint16 tag, length;          uint16 tag, length;
439          uint8 *next_tag, *end;          uint8 *next_tag, *end;
440    
441          in_uint32_le(s, *rc4_key_size); /* 1 = 40-bit, 2 = 128-bit */          in_uint32_le(s, *rc4_key_size); /* 1 = 40-bit, 2 = 128-bit */
442          in_uint32_le(s, crypt_level);  /* 1 = low, 2 = medium, 3 = high */          in_uint32_le(s, crypt_level);   /* 1 = low, 2 = medium, 3 = high */
443          in_uint32_le(s, random_len);          in_uint32_le(s, random_len);
444          in_uint32_le(s, rsa_info_len);          in_uint32_le(s, rsa_info_len);
445    
# Line 450  static BOOL sec_parse_crypt_info(STREAM Line 456  static BOOL sec_parse_crypt_info(STREAM
456          if (end > s->end)          if (end > s->end)
457                  return False;                  return False;
458    
459          in_uint8s(s, 12); /* unknown */          in_uint8s(s, 12);       /* unknown */
460    
461          while (s->p < end)          while (s->p < end)
462          {          {
# Line 462  static BOOL sec_parse_crypt_info(STREAM Line 468  static BOOL sec_parse_crypt_info(STREAM
468                  switch (tag)                  switch (tag)
469                  {                  {
470                          case SEC_TAG_PUBKEY:                          case SEC_TAG_PUBKEY:
471                                  if (!sec_parse_public_key(s, modulus, exponent))                                  if (!sec_parse_public_key
472                                        (s, modulus, exponent))
473                                          return False;                                          return False;
474    
475                                  break;                                  break;
# Line 490  static void sec_process_crypt_info(STREA Line 497  static void sec_process_crypt_info(STREA
497          uint32 rc4_key_size;          uint32 rc4_key_size;
498    
499          if (!sec_parse_crypt_info(s, &rc4_key_size, &server_random,          if (!sec_parse_crypt_info(s, &rc4_key_size, &server_random,
500                                          &modulus, &exponent))                                    &modulus, &exponent))
501                  return;                  return;
502    
503          /* Generate a client random, and hence determine encryption keys */          /* Generate a client random, and hence determine encryption keys */
504          generate_random(client_random);          generate_random(client_random);
505          sec_rsa_encrypt(sec_crypted_random, client_random,          sec_rsa_encrypt(sec_crypted_random, client_random,
506                                  SEC_RANDOM_SIZE, modulus, exponent);                          SEC_RANDOM_SIZE, modulus, exponent);
507          sec_generate_keys(client_random, server_random, rc4_key_size);          sec_generate_keys(client_random, server_random, rc4_key_size);
508  }  }
509    
# Line 506  static void sec_process_mcs_data(STREAM Line 513  static void sec_process_mcs_data(STREAM
513          uint16 tag, length;          uint16 tag, length;
514          uint8 *next_tag;          uint8 *next_tag;
515    
516          in_uint8s(s, 23); /* header */          in_uint8s(s, 23);       /* header */
517    
518          while (s->p < s->end)          while (s->p < s->end)
519          {          {
# Line 554  STREAM sec_recv() Line 561  STREAM sec_recv()
561    
562                  if (sec_flags & SEC_ENCRYPT)                  if (sec_flags & SEC_ENCRYPT)
563                  {                  {
564                          in_uint8s(s, 8); /* signature */                          in_uint8s(s, 8);        /* signature */
565                          sec_decrypt(s->p, s->end - s->p);                          sec_decrypt(s->p, s->end - s->p);
566                  }                  }
567    

Legend:
Removed from v.10  
changed lines
  Added in v.24

  ViewVC Help
Powered by ViewVC 1.1.26