]> git.sesse.net Git - rdpsrv/blobdiff - secure.c
Process presented licenses, and generate RC4 keys.
[rdpsrv] / secure.c
index 7c90ee262538055b270f697b0c4d33008b473a64..a52a134ffdb671a23987e3c23952ff683e55b21c 100644 (file)
--- a/secure.c
+++ b/secure.c
@@ -156,6 +156,19 @@ sec_generate_keys(uint8 * client_key, uint8 * server_key, int rc4_key_size)
        /* Initialise RC4 state arrays */
        RC4_set_key(&rc4_decrypt_key, rc4_key_len, sec_decrypt_key);
        RC4_set_key(&rc4_encrypt_key, rc4_key_len, sec_encrypt_key);
+
+       {
+               int i;
+               printf("sec_decrypt_key: ");
+               for (i = 0; i < 16; ++i)
+                       printf("0x%02x ", sec_decrypt_key[i]);
+               printf("\n");
+               
+               printf("sec_encrypt_key: ");
+               for (i = 0; i < 16; ++i)
+                       printf("0x%02x ", sec_encrypt_key[i]);
+               printf("\n");
+       }
 }
 
 static uint8 pad_54[40] = {
@@ -246,12 +259,12 @@ sec_encrypt(uint8 * data, int length)
 
        if (use_count == 4096)
        {
-               sec_update(sec_encrypt_key, sec_encrypt_update_key);
-               RC4_set_key(&rc4_encrypt_key, rc4_key_len, sec_encrypt_key);
+               sec_update(sec_decrypt_key, sec_decrypt_update_key);
+               RC4_set_key(&rc4_decrypt_key, rc4_key_len, sec_decrypt_key);
                use_count = 0;
        }
 
-       RC4(&rc4_encrypt_key, length, data, data);
+       RC4(&rc4_decrypt_key, length, data, data);
        use_count++;
 }
 
@@ -263,12 +276,12 @@ sec_decrypt(uint8 * data, int length)
 
        if (use_count == 4096)
        {
-               sec_update(sec_decrypt_key, sec_decrypt_update_key);
-               RC4_set_key(&rc4_decrypt_key, rc4_key_len, sec_decrypt_key);
+               sec_update(sec_encrypt_key, sec_encrypt_update_key);
+               RC4_set_key(&rc4_encrypt_key, rc4_key_len, sec_encrypt_key);
                use_count = 0;
        }
 
-       RC4(&rc4_decrypt_key, length, data, data);
+       RC4(&rc4_encrypt_key, length, data, data);
        use_count++;
 }
 
@@ -715,6 +728,86 @@ sec_process_mcs_data(STREAM s)
        }
 }
 
+extern unsigned char cacert[];
+
+unsigned char demand_license[] = {
+       0x01, 0x03, 0x86, 0x00, 0x9c, 0x6e, 0xef, 0x5a, 0x26, 0x45, 0x88, 0x86, 0x0e, 0xdf, 0xa4, 0x4a,
+       0x45, 0xc7, 0x5a, 0x4c, 0xec, 0x33, 0xff, 0x4c, 0xd8, 0x4b, 0xd2, 0x4e, 0xd2, 0x22, 0x16, 0xde,
+       0x1e, 0x5b, 0x06, 0x6e, 0x00, 0x00, 0x05, 0x00, 0x2c, 0x00, 0x00, 0x00, 0x4d, 0x00, 0x69, 0x00,
+       0x63, 0x00, 0x72, 0x00, 0x6f, 0x00, 0x73, 0x00, 0x6f, 0x00, 0x66, 0x00, 0x74, 0x00, 0x20, 0x00,
+       0x43, 0x00, 0x6f, 0x00, 0x72, 0x00, 0x70, 0x00, 0x6f, 0x00, 0x72, 0x00, 0x61, 0x00, 0x74, 0x00,
+       0x69, 0x00, 0x6f, 0x00, 0x6e, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x41, 0x00, 0x30, 0x00,
+       0x32, 0x00, 0x00, 0x00, 0x0d, 0x00, 0x04, 0x00, 0x01, 0x00, 0x00, 0x00, 0x04, 0xf6, 0x00, 0x00,
+       0x01, 0x00, 0x00, 0x00, 0x0e, 0x00, 0x0e, 0x00, 0x6d, 0x69, 0x63, 0x72, 0x6f, 0x73, 0x6f, 0x66,
+       0x74, 0x2e, 0x63, 0x6f, 0x6d, 0x00 
+};
+unsigned char authreq_license[] = {
+       0x02, 0x03, 0x26, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x0a, 0x00, 0x37, 0x58, 0x0f, 0xc6,
+       0x59, 0xfb, 0x03, 0xb2, 0x2a, 0xb7, 0xfa, 0x65, 0xac, 0x02, 0x8d, 0x8b, 0xda, 0xf7, 0xc5, 0x20,
+       0x1b, 0xe3, 0x65, 0x26, 0xc4, 0x6a
+};
+unsigned char result_license[] = {
+        0xff, 0x03, 0x10, 0x00, 0x08, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x6a, 0xbd, 0x00, 0x00
+};
+
+#define EXPECT8(value) { in_uint8(s, unknown); if (unknown != (value)) printf("Unknown value on code line %u; expected 0x%x, got 0x%x\n", __LINE__, (value), unknown); }
+#define EXPECT16(value) { in_uint16_le(s, unknown); if (unknown != (value)) printf("Unknown value on code line %u; expected 0x%x, got 0x%x\n", __LINE__, (value), unknown); }
+#define EXPECT32(value) { in_uint32_le(s, unknown); if (unknown != (value)) printf("Unknown value on code line %u; expected 0x%x, got 0x%x\n", __LINE__, (value), unknown); }
+
+void
+process_presented_license(STREAM s)
+{
+       unsigned char client_license_random[SEC_RANDOM_SIZE];
+       unsigned char client_license_rsa_data[SEC_MODULUS_SIZE];
+
+       uint16 length, license_size;
+       uint32 unknown;
+       uint8 *license, hwid[LICENCE_HWID_SIZE], signature[LICENCE_SIGNATURE_SIZE];
+       int i;
+       
+       EXPECT8(2);                   // version
+       in_uint16_le(s, length);
+
+       EXPECT32(1);                  // unknown
+       EXPECT16(0);
+       EXPECT16(0x0201);
+
+       in_uint8a(s, client_license_random, SEC_RANDOM_SIZE);
+       EXPECT16(0);
+       EXPECT16(SEC_MODULUS_SIZE + SEC_PADDING_SIZE);
+       in_uint8a(s, client_license_rsa_data, SEC_MODULUS_SIZE);
+       in_uint8s(s, SEC_PADDING_SIZE);
+
+       EXPECT16(1);
+       in_uint16_le(s, license_size);
+
+       license = (uint8 *)xmalloc(license_size);
+       in_uint8a(s, license, license_size);
+
+       printf("Received license:\n");
+       for (i = 0; i < license_size; ++i)
+               printf(" 0x%02x", license[i]);
+       printf("\n");
+
+       EXPECT16(1);
+       EXPECT16(LICENCE_HWID_SIZE);
+       in_uint8a(s, hwid, LICENCE_HWID_SIZE);
+
+       printf("License hardware ID:\n");
+       for (i = 0; i < LICENCE_HWID_SIZE; ++i)
+               printf(" 0x%02x", hwid[i]);
+       printf("\n");
+
+       in_uint8a(s, signature, LICENCE_SIGNATURE_SIZE);
+
+       if (!s_check_end(s)) {
+               printf("Unknown data at the end of presented license!");
+       }
+
+       // now we can generate the keys we need
+       licence_generate_keys(client_license_random, demand_license + 4, client_license_rsa_data);
+}
+
 /* Receive secure transport packet */
 STREAM
 sec_recv(void)
@@ -739,7 +832,14 @@ sec_recv(void)
 
                        if (sec_flags & SEC_LICENCE_NEG)
                        {
-                               printf("SEC_LICENSE_NEG unknown\n");
+                               uint8 tag;
+                               in_uint8(s, tag);
+                               printf("SEC_LICENSE_NEG tag %x\n", tag);
+                               
+                               if (tag == LICENCE_TAG_PRESENT) {
+                                       process_presented_license(s);
+                               }
+                               
                                //licence_process(s);
                                continue;
                        }
@@ -748,6 +848,18 @@ sec_recv(void)
                        {
                                printf("Received logon packet!\n");
                                rdp_get_logon_info(s);
+                       
+                               // demand a license
+                               {
+                                       STREAM s;
+
+                                       s = sec_init(sizeof(demand_license), SEC_LICENCE_NEG);
+                                       out_uint8p(s, demand_license, sizeof(demand_license));
+                                       s_mark_end(s);
+                                       sec_send(s, SEC_LICENCE_NEG);
+                               }
+                               
+                               continue;
                        }
 
                        if (sec_flags & SEC_CLIENT_RANDOM) {
@@ -777,6 +889,10 @@ sec_recv(void)
                                        printf("0x%x ", inr[i + SEC_RANDOM_SIZE]);
                                }
                                printf("\n");
+
+                               // now we can generate the keys
+                               sec_generate_keys(inr + SEC_RANDOM_SIZE, cacert, 1);
+                               continue;
                        }
                }