/[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 64 by astrand, Thu Jul 18 16:38:31 2002 UTC revision 309 by jsorg71, Tue Feb 4 05:32:13 2003 UTC
# Line 1  Line 1 
1  /*  /*
2     rdesktop: A Remote Desktop Protocol client.     rdesktop: A Remote Desktop Protocol client.
3     Protocol services - RDP encryption and licensing     Protocol services - RDP encryption and licensing
4     Copyright (C) Matthew Chapman 1999-2001     Copyright (C) Matthew Chapman 1999-2002
5    
6     This program is free software; you can redistribute it and/or modify     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by     it under the terms of the GNU General Public License as published by
# Line 38  extern int height; Line 38  extern int height;
38  extern int keylayout;  extern int keylayout;
39  extern BOOL encryption;  extern BOOL encryption;
40  extern BOOL licence_issued;  extern BOOL licence_issued;
41    extern int server_bpp;
42    
43  static int rc4_key_len;  static int rc4_key_len;
44  static RC4_KEY rc4_decrypt_key;  static RC4_KEY rc4_decrypt_key;
# Line 127  sec_generate_keys(uint8 * client_key, ui Line 128  sec_generate_keys(uint8 * client_key, ui
128          memcpy(sec_sign_key, session_key, 16);          memcpy(sec_sign_key, session_key, 16);
129    
130          /* Generate RC4 keys */          /* Generate RC4 keys */
131          sec_hash_16(sec_decrypt_key, &session_key[16], client_key,          sec_hash_16(sec_decrypt_key, &session_key[16], client_key, server_key);
132                      server_key);          sec_hash_16(sec_encrypt_key, &session_key[32], client_key, server_key);
         sec_hash_16(sec_encrypt_key, &session_key[32], client_key,  
                     server_key);  
133    
134          if (rc4_key_size == 1)          if (rc4_key_size == 1)
135          {          {
# Line 181  buf_out_uint32(uint8 * buffer, uint32 va Line 180  buf_out_uint32(uint8 * buffer, uint32 va
180    
181  /* Generate a signature hash, using a combination of SHA1 and MD5 */  /* Generate a signature hash, using a combination of SHA1 and MD5 */
182  void  void
183  sec_sign(uint8 * signature, int siglen, uint8 * session_key, int keylen,  sec_sign(uint8 * signature, int siglen, uint8 * session_key, int keylen, uint8 * data, int datalen)
          uint8 * data, int datalen)  
184  {  {
185          uint8 shasig[20];          uint8 shasig[20];
186          uint8 md5sig[16];          uint8 md5sig[16];
# Line 286  reverse(uint8 * p, int len) Line 284  reverse(uint8 * p, int len)
284    
285  /* Perform an RSA public key encryption operation */  /* Perform an RSA public key encryption operation */
286  static void  static void
287  sec_rsa_encrypt(uint8 * out, uint8 * in, int len,  sec_rsa_encrypt(uint8 * out, uint8 * in, int len, uint8 * modulus, uint8 * exponent)
                 uint8 * modulus, uint8 * exponent)  
288  {  {
289          BN_CTX ctx;          BN_CTX *ctx;
290          BIGNUM mod, exp, x, y;          BIGNUM mod, exp, x, y;
291          uint8 inr[SEC_MODULUS_SIZE];          uint8 inr[SEC_MODULUS_SIZE];
292          int outlen;          int outlen;
# Line 299  sec_rsa_encrypt(uint8 * out, uint8 * in, Line 296  sec_rsa_encrypt(uint8 * out, uint8 * in,
296          memcpy(inr, in, len);          memcpy(inr, in, len);
297          reverse(inr, len);          reverse(inr, len);
298    
299          BN_CTX_init(&ctx);          ctx = BN_CTX_new();
300          BN_init(&mod);          BN_init(&mod);
301          BN_init(&exp);          BN_init(&exp);
302          BN_init(&x);          BN_init(&x);
# Line 308  sec_rsa_encrypt(uint8 * out, uint8 * in, Line 305  sec_rsa_encrypt(uint8 * out, uint8 * in,
305          BN_bin2bn(modulus, SEC_MODULUS_SIZE, &mod);          BN_bin2bn(modulus, SEC_MODULUS_SIZE, &mod);
306          BN_bin2bn(exponent, SEC_EXPONENT_SIZE, &exp);          BN_bin2bn(exponent, SEC_EXPONENT_SIZE, &exp);
307          BN_bin2bn(inr, len, &x);          BN_bin2bn(inr, len, &x);
308          BN_mod_exp(&y, &x, &exp, &mod, &ctx);          BN_mod_exp(&y, &x, &exp, &mod, ctx);
309          outlen = BN_bn2bin(&y, out);          outlen = BN_bn2bin(&y, out);
310          reverse(out, outlen);          reverse(out, outlen);
311          if (outlen < SEC_MODULUS_SIZE)          if (outlen < SEC_MODULUS_SIZE)
# Line 318  sec_rsa_encrypt(uint8 * out, uint8 * in, Line 315  sec_rsa_encrypt(uint8 * out, uint8 * in,
315          BN_clear_free(&x);          BN_clear_free(&x);
316          BN_free(&exp);          BN_free(&exp);
317          BN_free(&mod);          BN_free(&mod);
318          BN_CTX_free(&ctx);          BN_CTX_free(ctx);
319  }  }
320    
321  /* Initialise secure transport packet */  /* Initialise secure transport packet */
# Line 358  sec_send(STREAM s, uint32 flags) Line 355  sec_send(STREAM s, uint32 flags)
355                  hexdump(s->p + 8, datalen);                  hexdump(s->p + 8, datalen);
356  #endif  #endif
357    
358                  sec_sign(s->p, 8, sec_sign_key, rc4_key_len, s->p + 8,                  sec_sign(s->p, 8, sec_sign_key, rc4_key_len, s->p + 8, datalen);
                          datalen);  
359                  sec_encrypt(s->p + 8, datalen);                  sec_encrypt(s->p + 8, datalen);
360          }          }
361    
# Line 368  sec_send(STREAM s, uint32 flags) Line 364  sec_send(STREAM s, uint32 flags)
364    
365  /* Transfer the client random to the server */  /* Transfer the client random to the server */
366  static void  static void
367  sec_establish_key()  sec_establish_key(void)
368  {  {
369          uint32 length = SEC_MODULUS_SIZE + SEC_PADDING_SIZE;          uint32 length = SEC_MODULUS_SIZE + SEC_PADDING_SIZE;
370          uint32 flags = SEC_CLIENT_RANDOM;          uint32 flags = SEC_CLIENT_RANDOM;
# Line 390  sec_out_mcs_data(STREAM s) Line 386  sec_out_mcs_data(STREAM s)
386  {  {
387          int hostlen = 2 * strlen(hostname);          int hostlen = 2 * strlen(hostname);
388    
389            if (hostlen > 30)
390                    hostlen = 30;
391    
392          out_uint16_be(s, 5);    /* unknown */          out_uint16_be(s, 5);    /* unknown */
393          out_uint16_be(s, 0x14);          out_uint16_be(s, 0x14);
394          out_uint8(s, 0x7c);          out_uint8(s, 0x7c);
# Line 427  sec_out_mcs_data(STREAM s) Line 426  sec_out_mcs_data(STREAM s)
426          out_uint32_le(s, 12);          out_uint32_le(s, 12);
427          out_uint8s(s, 64);      /* reserved? 4 + 12 doublewords */          out_uint8s(s, 64);      /* reserved? 4 + 12 doublewords */
428    
429          out_uint16(s, 0xca01);          if (server_bpp == 16)
430            {
431                    out_uint16_le(s, 0xca03); /* 16 bit */
432            }
433            else
434            {
435                    out_uint16_le(s, 0xca01); /* 8 bit */
436            }
437          out_uint16(s, 0);          out_uint16(s, 0);
438    
439          /* Client encryption settings */          /* Client encryption settings */
440          out_uint16_le(s, SEC_TAG_CLI_CRYPT);          out_uint16_le(s, SEC_TAG_CLI_CRYPT);
441          out_uint16(s, 8);       /* length */          out_uint16_le(s, 8);    /* length */
442          out_uint32_le(s, encryption ? 0x3 : 0); /* encryption supported, 128-bit supported */          out_uint32_le(s, encryption ? 0x3 : 0); /* encryption supported, 128-bit supported */
443          s_mark_end(s);          s_mark_end(s);
444  }  }
# Line 468  sec_parse_public_key(STREAM s, uint8 ** Line 474  sec_parse_public_key(STREAM s, uint8 **
474  /* Parse a crypto information structure */  /* Parse a crypto information structure */
475  static BOOL  static BOOL
476  sec_parse_crypt_info(STREAM s, uint32 * rc4_key_size,  sec_parse_crypt_info(STREAM s, uint32 * rc4_key_size,
477                       uint8 ** server_random, uint8 ** modulus,                       uint8 ** server_random, uint8 ** modulus, uint8 ** exponent)
                      uint8 ** exponent)  
478  {  {
479          uint32 crypt_level, random_len, rsa_info_len;          uint32 crypt_level, random_len, rsa_info_len;
480          uint16 tag, length;          uint16 tag, length;
# Line 477  sec_parse_crypt_info(STREAM s, uint32 * Line 482  sec_parse_crypt_info(STREAM s, uint32 *
482    
483          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 */
484          in_uint32_le(s, crypt_level);   /* 1 = low, 2 = medium, 3 = high */          in_uint32_le(s, crypt_level);   /* 1 = low, 2 = medium, 3 = high */
485            if (crypt_level == 0)   /* no encryptation */
486                    return False;
487          in_uint32_le(s, random_len);          in_uint32_le(s, random_len);
488          in_uint32_le(s, rsa_info_len);          in_uint32_le(s, rsa_info_len);
489    
# Line 505  sec_parse_crypt_info(STREAM s, uint32 * Line 512  sec_parse_crypt_info(STREAM s, uint32 *
512                  switch (tag)                  switch (tag)
513                  {                  {
514                          case SEC_TAG_PUBKEY:                          case SEC_TAG_PUBKEY:
515                                  if (!sec_parse_public_key                                  if (!sec_parse_public_key(s, modulus, exponent))
                                     (s, modulus, exponent))  
516                                          return False;                                          return False;
517    
518                                  break;                                  break;
# Line 534  sec_process_crypt_info(STREAM s) Line 540  sec_process_crypt_info(STREAM s)
540          uint8 client_random[SEC_RANDOM_SIZE];          uint8 client_random[SEC_RANDOM_SIZE];
541          uint32 rc4_key_size;          uint32 rc4_key_size;
542    
543          if (!sec_parse_crypt_info(s, &rc4_key_size, &server_random,          if (!sec_parse_crypt_info(s, &rc4_key_size, &server_random, &modulus, &exponent))
                                   &modulus, &exponent))  
544                  return;                  return;
545    
546          /* Generate a client random, and hence determine encryption keys */          /* Generate a client random, and hence determine encryption keys */
547          generate_random(client_random);          generate_random(client_random);
548          sec_rsa_encrypt(sec_crypted_random, client_random,          sec_rsa_encrypt(sec_crypted_random, client_random, SEC_RANDOM_SIZE, modulus, exponent);
                         SEC_RANDOM_SIZE, modulus, exponent);  
549          sec_generate_keys(client_random, server_random, rc4_key_size);          sec_generate_keys(client_random, server_random, rc4_key_size);
550  }  }
551    
# Line 551  sec_process_mcs_data(STREAM s) Line 555  sec_process_mcs_data(STREAM s)
555  {  {
556          uint16 tag, length;          uint16 tag, length;
557          uint8 *next_tag;          uint8 *next_tag;
558            uint8 len;
559    
560          in_uint8s(s, 23);       /* header */          in_uint8s(s, 21);       /* header */
561            in_uint8(s, len);
562            if (len & 0x80)
563                    in_uint8(s, len);
564    
565          while (s->p < s->end)          while (s->p < s->end)
566          {          {
# Line 584  sec_process_mcs_data(STREAM s) Line 592  sec_process_mcs_data(STREAM s)
592    
593  /* Receive secure transport packet */  /* Receive secure transport packet */
594  STREAM  STREAM
595  sec_recv()  sec_recv(void)
596  {  {
597          uint32 sec_flags;          uint32 sec_flags;
598          STREAM s;          STREAM s;
# Line 631  sec_connect(char *server) Line 639  sec_connect(char *server)
639          sec_process_mcs_data(&mcs_data);          sec_process_mcs_data(&mcs_data);
640          if (encryption)          if (encryption)
641                  sec_establish_key();                  sec_establish_key();
642            xfree(mcs_data.data);
643          return True;          return True;
644  }  }
645    
646  /* Disconnect a connection */  /* Disconnect a connection */
647  void  void
648  sec_disconnect()  sec_disconnect(void)
649  {  {
650          mcs_disconnect();          mcs_disconnect();
651  }  }

Legend:
Removed from v.64  
changed lines
  Added in v.309

  ViewVC Help
Powered by ViewVC 1.1.26