]> git.sesse.net Git - rdpsrv/blob - secure.c
1f7797b023e73fc05601a09ad89dfe9fbc8d0ebf
[rdpsrv] / secure.c
1 /* -*- c-basic-offset: 8 -*-
2    rdesktop: A Remote Desktop Protocol client.
3    Protocol services - RDP encryption and licensing
4    Copyright (C) Matthew Chapman 1999-2002
5
6    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
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include "rdesktop.h"
22
23 #include <openssl/rc4.h>
24 #include <openssl/md5.h>
25 #include <openssl/sha.h>
26 #include <openssl/bn.h>
27 #include <openssl/x509v3.h>
28
29 extern char hostname[16];
30 extern int g_width;
31 extern int g_height;
32 extern int keylayout;
33 extern BOOL g_encryption;
34 extern BOOL g_licence_issued;
35 extern BOOL g_use_rdp5;
36 extern BOOL g_console_session;
37 extern int g_server_bpp;
38 extern uint16 mcs_userid;
39 extern VCHANNEL g_channels[];
40 extern unsigned int g_num_channels;
41
42 int rc4_key_len;
43 static RC4_KEY rc4_decrypt_key;
44 static RC4_KEY rc4_encrypt_key;
45 static RSA *server_public_key;
46
47 uint8 sec_sign_key[16];
48 static uint8 sec_decrypt_key[16];
49 static uint8 sec_encrypt_key[16];
50 static uint8 sec_decrypt_update_key[16];
51 static uint8 sec_encrypt_update_key[16];
52 static uint8 sec_crypted_random[SEC_MODULUS_SIZE];
53
54 RSA *privkey;
55
56 uint16 g_server_rdp_version = 0;
57
58 /*
59  * General purpose 48-byte transformation, using two 32-byte salts (generally,
60  * a client and server salt) and a global salt value used for padding.
61  * Both SHA1 and MD5 algorithms are used.
62  */
63 void
64 sec_hash_48(uint8 * out, uint8 * in, uint8 * salt1, uint8 * salt2, uint8 salt)
65 {
66         uint8 shasig[20];
67         uint8 pad[4];
68         SHA_CTX sha;
69         MD5_CTX md5;
70         int i;
71
72         for (i = 0; i < 3; i++)
73         {
74                 memset(pad, salt + i, i + 1);
75
76                 SHA1_Init(&sha);
77                 SHA1_Update(&sha, pad, i + 1);
78                 SHA1_Update(&sha, in, 48);
79                 SHA1_Update(&sha, salt1, 32);
80                 SHA1_Update(&sha, salt2, 32);
81                 SHA1_Final(shasig, &sha);
82
83                 MD5_Init(&md5);
84                 MD5_Update(&md5, in, 48);
85                 MD5_Update(&md5, shasig, 20);
86                 MD5_Final(&out[i * 16], &md5);
87         }
88 }
89
90 /*
91  * Weaker 16-byte transformation, also using two 32-byte salts, but
92  * only using a single round of MD5.
93  */
94 void
95 sec_hash_16(uint8 * out, uint8 * in, uint8 * salt1, uint8 * salt2)
96 {
97         MD5_CTX md5;
98
99         MD5_Init(&md5);
100         MD5_Update(&md5, in, 16);
101         MD5_Update(&md5, salt1, 32);
102         MD5_Update(&md5, salt2, 32);
103         MD5_Final(out, &md5);
104 }
105
106 /* Reduce key entropy from 64 to 40 bits */
107 static void
108 sec_make_40bit(uint8 * key)
109 {
110         key[0] = 0xd1;
111         key[1] = 0x26;
112         key[2] = 0x9e;
113 }
114
115 /* Generate a session key and RC4 keys, given client and server randoms */
116 static void
117 sec_generate_keys(uint8 * client_key, uint8 * server_key, int rc4_key_size)
118 {
119         uint8 session_key[48];
120         uint8 temp_hash[48];
121         uint8 input[48];
122
123         /* Construct input data to hash */
124         memcpy(input, client_key, 24);
125         memcpy(input + 24, server_key, 24);
126
127         /* Generate session key - two rounds of sec_hash_48 */
128         sec_hash_48(temp_hash, input, client_key, server_key, 65);
129         sec_hash_48(session_key, temp_hash, client_key, server_key, 88);
130
131         /* Store first 16 bytes of session key, for generating signatures */
132         memcpy(sec_sign_key, session_key, 16);
133
134         /* Generate RC4 keys */
135         sec_hash_16(sec_decrypt_key, &session_key[16], client_key, server_key);
136         sec_hash_16(sec_encrypt_key, &session_key[32], client_key, server_key);
137
138         if (rc4_key_size == 1)
139         {
140                 DEBUG(("40-bit encryption enabled\n"));
141                 sec_make_40bit(sec_sign_key);
142                 sec_make_40bit(sec_decrypt_key);
143                 sec_make_40bit(sec_encrypt_key);
144                 rc4_key_len = 8;
145         }
146         else
147         {
148                 DEBUG(("rc_4_key_size == %d, 128-bit encryption enabled\n", rc4_key_size));
149                 rc4_key_len = 16;
150         }
151
152         /* Save initial RC4 keys as update keys */
153         memcpy(sec_decrypt_update_key, sec_decrypt_key, 16);
154         memcpy(sec_encrypt_update_key, sec_encrypt_key, 16);
155
156         /* Initialise RC4 state arrays */
157         RC4_set_key(&rc4_decrypt_key, rc4_key_len, sec_decrypt_key);
158         RC4_set_key(&rc4_encrypt_key, rc4_key_len, sec_encrypt_key);
159
160         {
161                 int i;
162                 printf("sec_decrypt_key: ");
163                 for (i = 0; i < 16; ++i)
164                         printf("0x%02x ", sec_decrypt_key[i]);
165                 printf("\n");
166                 
167                 printf("sec_encrypt_key: ");
168                 for (i = 0; i < 16; ++i)
169                         printf("0x%02x ", sec_encrypt_key[i]);
170                 printf("\n");
171         }
172
173         g_encryption = 1;
174 }
175
176 static uint8 pad_54[40] = {
177         54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54,
178         54, 54, 54,
179         54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54,
180         54, 54, 54
181 };
182
183 static uint8 pad_92[48] = {
184         92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92,
185         92, 92, 92, 92, 92, 92, 92,
186         92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92,
187         92, 92, 92, 92, 92, 92, 92
188 };
189
190 /* Output a uint32 into a buffer (little-endian) */
191 void
192 buf_out_uint32(uint8 * buffer, uint32 value)
193 {
194         buffer[0] = (value) & 0xff;
195         buffer[1] = (value >> 8) & 0xff;
196         buffer[2] = (value >> 16) & 0xff;
197         buffer[3] = (value >> 24) & 0xff;
198 }
199
200 /* Generate a signature hash, using a combination of SHA1 and MD5 */
201 void
202 sec_sign(uint8 * signature, int siglen, uint8 * session_key, int keylen, uint8 * data, int datalen)
203 {
204         uint8 shasig[20];
205         uint8 md5sig[16];
206         uint8 lenhdr[4];
207         SHA_CTX sha;
208         MD5_CTX md5;
209
210         buf_out_uint32(lenhdr, datalen);
211
212         SHA1_Init(&sha);
213         SHA1_Update(&sha, session_key, keylen);
214         SHA1_Update(&sha, pad_54, 40);
215         SHA1_Update(&sha, lenhdr, 4);
216         SHA1_Update(&sha, data, datalen);
217         SHA1_Final(shasig, &sha);
218
219         MD5_Init(&md5);
220         MD5_Update(&md5, session_key, keylen);
221         MD5_Update(&md5, pad_92, 48);
222         MD5_Update(&md5, shasig, 20);
223         MD5_Final(md5sig, &md5);
224
225         memcpy(signature, md5sig, siglen);
226 }
227
228 /* Update an encryption key - similar to the signing process */
229 static void
230 sec_update(uint8 * key, uint8 * update_key)
231 {
232         uint8 shasig[20];
233         SHA_CTX sha;
234         MD5_CTX md5;
235         RC4_KEY update;
236
237         SHA1_Init(&sha);
238         SHA1_Update(&sha, update_key, rc4_key_len);
239         SHA1_Update(&sha, pad_54, 40);
240         SHA1_Update(&sha, key, rc4_key_len);
241         SHA1_Final(shasig, &sha);
242
243         MD5_Init(&md5);
244         MD5_Update(&md5, update_key, rc4_key_len);
245         MD5_Update(&md5, pad_92, 48);
246         MD5_Update(&md5, shasig, 20);
247         MD5_Final(key, &md5);
248
249         RC4_set_key(&update, rc4_key_len, key);
250         RC4(&update, rc4_key_len, key, key);
251
252         if (rc4_key_len == 8)
253                 sec_make_40bit(key);
254 }
255
256 /* Encrypt data using RC4 */
257 void
258 sec_encrypt(uint8 * data, int length)
259 {
260         static int use_count;
261
262         if (use_count == 4096)
263         {
264                 sec_update(sec_decrypt_key, sec_decrypt_update_key);
265                 RC4_set_key(&rc4_decrypt_key, rc4_key_len, sec_decrypt_key);
266                 use_count = 0;
267         }
268
269         printf("RC4-ing %u bytes with DECRYPT, uc=%u\n", length, use_count);
270
271         RC4(&rc4_decrypt_key, length, data, data);
272         use_count++;
273 }
274
275 /* Decrypt data using RC4 */
276 void
277 sec_decrypt(uint8 * data, int length)
278 {
279         static int use_count;
280
281         if (use_count == 4096)
282         {
283                 sec_update(sec_encrypt_key, sec_encrypt_update_key);
284                 RC4_set_key(&rc4_encrypt_key, rc4_key_len, sec_encrypt_key);
285                 use_count = 0;
286         }
287
288         RC4(&rc4_encrypt_key, length, data, data);
289         use_count++;
290 }
291
292 static void
293 reverse(uint8 * p, int len)
294 {
295         int i, j;
296         uint8 temp;
297
298         for (i = 0, j = len - 1; i < j; i++, j--)
299         {
300                 temp = p[i];
301                 p[i] = p[j];
302                 p[j] = temp;
303         }
304 }
305
306 /* Perform an RSA public key encryption operation */
307 static void
308 sec_rsa_encrypt(uint8 * out, uint8 * in, int len, uint8 * modulus, uint8 * exponent)
309 {
310         BN_CTX *ctx;
311         BIGNUM mod, exp, x, y;
312         uint8 inr[SEC_MODULUS_SIZE];
313         int outlen;
314
315         reverse(modulus, SEC_MODULUS_SIZE);
316         reverse(exponent, SEC_EXPONENT_SIZE);
317         memcpy(inr, in, len);
318         reverse(inr, len);
319
320         ctx = BN_CTX_new();
321         BN_init(&mod);
322         BN_init(&exp);
323         BN_init(&x);
324         BN_init(&y);
325
326         BN_bin2bn(modulus, SEC_MODULUS_SIZE, &mod);
327         BN_bin2bn(exponent, SEC_EXPONENT_SIZE, &exp);
328         BN_bin2bn(inr, len, &x);
329         BN_mod_exp(&y, &x, &exp, &mod, ctx);
330         outlen = BN_bn2bin(&y, out);
331         reverse(out, outlen);
332         if (outlen < SEC_MODULUS_SIZE)
333                 memset(out + outlen, 0, SEC_MODULUS_SIZE - outlen);
334
335         BN_free(&y);
336         BN_clear_free(&x);
337         BN_free(&exp);
338         BN_free(&mod);
339         BN_CTX_free(ctx);
340 }
341
342 /* Initialise secure transport packet */
343 STREAM
344 sec_init(uint32 flags, int maxlen)
345 {
346         int hdrlen;
347         STREAM s;
348
349         if (!g_licence_issued) 
350                 hdrlen = (flags & SEC_ENCRYPT) ? 12 : 4;
351         else
352                 hdrlen = (flags & SEC_ENCRYPT) ? 12 : 0; 
353         printf("HDRLEN is %u\n", hdrlen);
354         s = mcs_init(maxlen + hdrlen);
355         s_push_layer(s, sec_hdr, hdrlen);
356
357         return s;
358 }
359
360 /* Transmit secure transport packet over specified channel */
361 void
362 sec_send_to_channel(STREAM s, uint32 flags, uint16 channel)
363 {
364         int datalen;
365
366 //      DEBUG(("sending packet to channel %u\n", channel));
367         
368         s_pop_layer(s, sec_hdr);
369         if (!g_licence_issued || (flags & SEC_ENCRYPT)) {
370                 out_uint32_le(s, flags);
371         }
372
373         if (flags & SEC_ENCRYPT)
374         {
375                 flags &= ~SEC_ENCRYPT;
376                 datalen = s->end - s->p - 8;
377
378 #if WITH_DEBUG && 0
379                 DEBUG(("Sending encrypted packet:\n"));
380                 hexdump(s->p + 8, datalen);
381 #endif
382
383                 sec_sign(s->p, 8, sec_sign_key, rc4_key_len, s->p + 8, datalen);
384                 printf("First byte of signature is 0x%x, at %p\n", (s->p)[0], s->p);
385                 sec_encrypt(s->p + 8, datalen);
386         }
387
388         mcs_send_to_channel(s, channel);
389 }
390
391 /* Transmit secure transport packet */
392
393 void
394 sec_send(STREAM s, uint32 flags)
395 {
396         sec_send_to_channel(s, flags, MCS_GLOBAL_CHANNEL);
397 }
398
399
400 /* Transfer the client random to the server */
401 static void
402 sec_establish_key(void)
403 {
404         uint32 length = SEC_MODULUS_SIZE + SEC_PADDING_SIZE;
405         uint32 flags = SEC_CLIENT_RANDOM;
406         STREAM s;
407
408         s = sec_init(flags, 76);
409
410         out_uint32_le(s, length);
411         out_uint8p(s, sec_crypted_random, SEC_MODULUS_SIZE);
412         out_uint8s(s, SEC_PADDING_SIZE);
413
414         s_mark_end(s);
415         sec_send(s, flags);
416 }
417
418 /* Parse a public key structure */
419 static BOOL
420 sec_parse_public_key(STREAM s, uint8 ** modulus, uint8 ** exponent)
421 {
422         uint32 magic, modulus_len;
423
424         in_uint32_le(s, magic);
425         if (magic != SEC_RSA_MAGIC)
426         {
427                 error("RSA magic 0x%x\n", magic);
428                 return False;
429         }
430
431         in_uint32_le(s, modulus_len);
432         if (modulus_len != SEC_MODULUS_SIZE + SEC_PADDING_SIZE)
433         {
434                 error("modulus len 0x%x\n", modulus_len);
435                 return False;
436         }
437
438         in_uint8s(s, 8);        /* modulus_bits, unknown */
439         in_uint8p(s, *exponent, SEC_EXPONENT_SIZE);
440         in_uint8p(s, *modulus, SEC_MODULUS_SIZE);
441         in_uint8s(s, SEC_PADDING_SIZE);
442
443         return s_check(s);
444 }
445
446 static BOOL
447 sec_parse_x509_key(X509 * cert)
448 {
449         EVP_PKEY *epk = NULL;
450         /* By some reason, Microsoft sets the OID of the Public RSA key to
451            the oid for "MD5 with RSA Encryption" instead of "RSA Encryption"
452
453            Kudos to Richard Levitte for the following (. intiutive .) 
454            lines of code that resets the OID and let's us extract the key. */
455         if (OBJ_obj2nid(cert->cert_info->key->algor->algorithm) == NID_md5WithRSAEncryption)
456         {
457                 DEBUG_RDP5(("Re-setting algorithm type to RSA in server certificate\n"));
458                 cert->cert_info->key->algor->algorithm = OBJ_nid2obj(NID_rsaEncryption);
459         }
460         epk = X509_get_pubkey(cert);
461         if (NULL == epk)
462         {
463                 error("Failed to extract public key from certificate\n");
464                 return False;
465         }
466
467         server_public_key = (RSA *) epk->pkey.ptr;
468
469         return True;
470 }
471
472
473 /* Parse a crypto information structure */
474 static BOOL
475 sec_parse_crypt_info(STREAM s, uint32 * rc4_key_size,
476                      uint8 ** server_random, uint8 ** modulus, uint8 ** exponent)
477 {
478         uint32 crypt_level, random_len, rsa_info_len;
479         uint32 cacert_len, cert_len, flags;
480         X509 *cacert, *server_cert;
481         uint16 tag, length;
482         uint8 *next_tag, *end;
483
484         in_uint32_le(s, *rc4_key_size); /* 1 = 40-bit, 2 = 128-bit */
485         in_uint32_le(s, crypt_level);   /* 1 = low, 2 = medium, 3 = high */
486         if (crypt_level == 0)   /* no encryption */
487                 return False;
488         in_uint32_le(s, random_len);
489         in_uint32_le(s, rsa_info_len);
490
491         if (random_len != SEC_RANDOM_SIZE)
492         {
493                 error("random len %d, expected %d\n", random_len, SEC_RANDOM_SIZE);
494                 return False;
495         }
496
497         in_uint8p(s, *server_random, random_len);
498
499         /* RSA info */
500         end = s->p + rsa_info_len;
501         if (end > s->end)
502                 return False;
503
504         in_uint32_le(s, flags); /* 1 = RDP4-style, 0x80000002 = X.509 */
505         if (flags & 1)
506         {
507                 DEBUG_RDP5(("We're going for the RDP4-style encryption\n"));
508                 in_uint8s(s, 8);        /* unknown */
509
510                 while (s->p < end)
511                 {
512                         in_uint16_le(s, tag);
513                         in_uint16_le(s, length);
514
515                         next_tag = s->p + length;
516
517                         switch (tag)
518                         {
519                                 case SEC_TAG_PUBKEY:
520                                         if (!sec_parse_public_key(s, modulus, exponent))
521                                                 return False;
522                                         DEBUG_RDP5(("Got Public key, RDP4-style\n"));
523
524                                         break;
525
526                                 case SEC_TAG_KEYSIG:
527                                         /* Is this a Microsoft key that we just got? */
528                                         /* Care factor: zero! */
529                                         /* Actually, it would probably be a good idea to check if the public key is signed with this key, and then store this 
530                                            key as a known key of the hostname. This would prevent some MITM-attacks. */
531                                         break;
532
533                                 default:
534                                         unimpl("crypt tag 0x%x\n", tag);
535                         }
536
537                         s->p = next_tag;
538                 }
539         }
540         else
541         {
542                 uint32 certcount;
543
544                 DEBUG_RDP5(("We're going for the RDP5-style encryption\n"));
545                 in_uint32_le(s, certcount); /* Number of certificates */
546
547                 if(certcount < 2) 
548                 {
549                         error("Server didn't send enough X509 certificates\n");
550                         return False;
551                 }
552
553                 for(; certcount > 2; certcount--)
554                 { /* ignore all the certificates between the root and the signing CA */
555                         uint32 ignorelen;
556                         X509 *ignorecert;
557
558                         DEBUG_RDP5(("Ignored certs left: %d\n", certcount));
559
560                         in_uint32_le(s, ignorelen);
561                         DEBUG_RDP5(("Ignored Certificate length is %d\n", ignorelen));
562                         ignorecert = d2i_X509(NULL, &(s->p), ignorelen);
563
564                         if(ignorecert == NULL)
565                         { /* XXX: error out? */
566                                 DEBUG_RDP5(("got a bad cert: this will probably screw up the rest of the communication\n"));
567                         }
568
569 #ifdef WITH_DEBUG_RDP5
570                         DEBUG_RDP5(("cert #%d (ignored):\n",certcount));
571                         X509_print_fp(stdout, ignorecert);
572 #endif
573                 }
574
575                 /* Do da funky X.509 stuffy 
576
577                    "How did I find out about this?  I looked up and saw a
578                    bright light and when I came to I had a scar on my forehead
579                    and knew about X.500"
580                    - Peter Gutman in a early version of 
581                    http://www.cs.auckland.ac.nz/~pgut001/pubs/x509guide.txt
582                  */
583
584                 in_uint32_le(s, cacert_len);
585                 DEBUG_RDP5(("CA Certificate length is %d\n", cacert_len));
586                 cacert = d2i_X509(NULL, &(s->p), cacert_len);
587                 /* Note: We don't need to move s->p here - d2i_X509 is
588                    "kind" enough to do it for us */
589                 if (NULL == cacert)
590                 {
591                         error("Couldn't load CA Certificate from server\n");
592                         return False;
593                 }
594
595                 /* Currently, we don't use the CA Certificate. 
596                    FIXME: 
597                    *) Verify the server certificate (server_cert) with the 
598                    CA certificate.
599                    *) Store the CA Certificate with the hostname of the 
600                    server we are connecting to as key, and compare it
601                    when we connect the next time, in order to prevent
602                    MITM-attacks.
603                  */
604
605                 in_uint32_le(s, cert_len);
606                 DEBUG_RDP5(("Certificate length is %d\n", cert_len));
607                 server_cert = d2i_X509(NULL, &(s->p), cert_len);
608                 if (NULL == server_cert)
609                 {
610                         error("Couldn't load Certificate from server\n");
611                         return False;
612                 }
613
614                 in_uint8s(s, 16);       /* Padding */
615
616                 /* Note: Verifying the server certificate must be done here, 
617                    before sec_parse_public_key since we'll have to apply
618                    serious violence to the key after this */
619
620                 if (!sec_parse_x509_key(server_cert))
621                 {
622                         DEBUG_RDP5(("Didn't parse X509 correctly\n"));
623                         return False;
624                 }
625                 return True;    /* There's some garbage here we don't care about */
626         }
627         return s_check_end(s);
628 }
629
630 /* Process crypto information blob */
631 static void
632 sec_process_crypt_info(STREAM s)
633 {
634         uint8 *server_random, *modulus, *exponent;
635         uint8 client_random[SEC_RANDOM_SIZE];
636         uint32 rc4_key_size;
637         uint8 inr[SEC_MODULUS_SIZE];
638
639         if (!sec_parse_crypt_info(s, &rc4_key_size, &server_random, &modulus, &exponent))
640         {
641                 DEBUG(("Failed to parse crypt info\n"));
642                 return;
643         }
644
645         DEBUG(("Generating client random\n"));
646         /* Generate a client random, and hence determine encryption keys */
647         /* This is what the MS client do: */
648         memset(inr, 0, SEC_RANDOM_SIZE);
649         /*  *ARIGL!* Plaintext attack, anyone?
650            I tried doing:
651            generate_random(inr);
652            ..but that generates connection errors now and then (yes, 
653            "now and then". Something like 0 to 3 attempts needed before a 
654            successful connection. Nice. Not! 
655          */
656
657         generate_random(client_random);
658         if (NULL != server_public_key)
659         {                       /* Which means we should use 
660                                    RDP5-style encryption */
661
662                 memcpy(inr + SEC_RANDOM_SIZE, client_random, SEC_RANDOM_SIZE);
663                 reverse(inr + SEC_RANDOM_SIZE, SEC_RANDOM_SIZE);
664
665                 RSA_public_encrypt(SEC_MODULUS_SIZE,
666                                    inr, sec_crypted_random, server_public_key, RSA_NO_PADDING);
667
668                 reverse(sec_crypted_random, SEC_MODULUS_SIZE);
669
670         }
671         else
672         {                       /* RDP4-style encryption */
673                 sec_rsa_encrypt(sec_crypted_random,
674                                 client_random, SEC_RANDOM_SIZE, modulus, exponent);
675         }
676         sec_generate_keys(client_random, server_random, rc4_key_size);
677 }
678
679
680 /* Process SRV_INFO, find RDP version supported by server */
681 static void
682 sec_process_srv_info(STREAM s)
683 {
684         in_uint16_le(s, g_server_rdp_version);
685         DEBUG_RDP5(("Server RDP version is %d\n", g_server_rdp_version));
686 /*      if (1 == g_server_rdp_version)
687                 g_use_rdp5 = 0; */
688 }
689
690
691 /* Process connect response data blob */
692 void
693 sec_process_mcs_data(STREAM s)
694 {
695         uint16 tag, length;
696         uint8 *next_tag;
697         uint8 len;
698
699         in_uint8s(s, 21);       /* header (T.124 stuff, probably) */
700         in_uint8(s, len);
701         if (len & 0x80)
702                 in_uint8(s, len);
703
704         while (s->p < s->end)
705         {
706                 in_uint16_le(s, tag);
707                 in_uint16_le(s, length);
708
709                 if (length <= 4)
710                         return;
711
712                 next_tag = s->p + length - 4;
713
714                 switch (tag)
715                 {
716                         case SEC_TAG_SRV_INFO:
717                                 sec_process_srv_info(s);
718                                 break;
719
720                         case SEC_TAG_SRV_CRYPT:
721                                 sec_process_crypt_info(s);
722                                 break;
723
724                         case SEC_TAG_SRV_CHANNELS:
725                                 /* FIXME: We should parse this information and
726                                    use it to map RDP5 channels to MCS 
727                                    channels */
728                                 break;
729
730                         default:
731                                 unimpl("response tag 0x%x\n", tag);
732                 }
733
734                 s->p = next_tag;
735         }
736 }
737
738 extern unsigned char cacert[];
739 extern BOOL g_licence_issued;
740
741 unsigned char demand_license[] = {
742         0x01, 0x03, 0x86, 0x00, 0x9c, 0x6e, 0xef, 0x5a, 0x26, 0x45, 0x88, 0x86, 0x0e, 0xdf, 0xa4, 0x4a,
743         0x45, 0xc7, 0x5a, 0x4c, 0xec, 0x33, 0xff, 0x4c, 0xd8, 0x4b, 0xd2, 0x4e, 0xd2, 0x22, 0x16, 0xde,
744         0x1e, 0x5b, 0x06, 0x6e, 0x00, 0x00, 0x05, 0x00, 0x2c, 0x00, 0x00, 0x00, 0x4d, 0x00, 0x69, 0x00,
745         0x63, 0x00, 0x72, 0x00, 0x6f, 0x00, 0x73, 0x00, 0x6f, 0x00, 0x66, 0x00, 0x74, 0x00, 0x20, 0x00,
746         0x43, 0x00, 0x6f, 0x00, 0x72, 0x00, 0x70, 0x00, 0x6f, 0x00, 0x72, 0x00, 0x61, 0x00, 0x74, 0x00,
747         0x69, 0x00, 0x6f, 0x00, 0x6e, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x41, 0x00, 0x30, 0x00,
748         0x32, 0x00, 0x00, 0x00, 0x0d, 0x00, 0x04, 0x00, 0x01, 0x00, 0x00, 0x00, 0x04, 0xf6, 0x00, 0x00,
749         0x01, 0x00, 0x00, 0x00, 0x0e, 0x00, 0x0e, 0x00, 0x6d, 0x69, 0x63, 0x72, 0x6f, 0x73, 0x6f, 0x66,
750         0x74, 0x2e, 0x63, 0x6f, 0x6d, 0x00 
751 };
752 unsigned char result_license[] = {
753         0xff, 0x03, 0x10, 0x00, 0x07, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0xa0, 0x5f, 0x00, 0x00
754 };
755
756 unsigned char demand_active[] = {
757         0x24, 0x01, 0x11, 0x00, 0xea, 0x03, 0xea, 0x03, 0x01, 0x00, 0x04, 0x00, 0x0e, 0x01, 0x52, 0x44,
758         0x50, 0x00, 0x09, 0x00, 0x00, 0x00, 0x09, 0x00, 0x08, 0x00, 0xea, 0x03, 0x40, 0xe4, 0x01, 0x00,
759         0x18, 0x00, 0x01, 0x00, 0x03, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x11, 0x04, 0x00, 0x00,
760         0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x0e, 0x00, 0x04, 0x00, 0x02, 0x00, 0x1c, 0x00, 0x08, 0x00,
761         0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x20, 0x03, 0x58, 0x02, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00,
762         0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x03, 0x00, 0x58, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
763         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x42, 0x0f, 0x00, 0x01, 0x00,
764         0x14, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x22, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00,
765         0x00, 0x00, 0x01, 0x00, 0x01, 0x01, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
766         0x01, 0x00, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0xa1, 0x06, 0x00, 0x00, 0x40, 0x42,
767         0x0f, 0x00, 0x40, 0x42, 0x0f, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0a, 0x00,
768         0x08, 0x00, 0x06, 0x00, 0x00, 0x00, 0x12, 0x00, 0x08, 0x00, 0x01, 0x00, 0x00, 0x00, 0x08, 0x00,
769         0x0a, 0x00, 0x01, 0x00, 0x19, 0x00, 0x19, 0x00, 0x0d, 0x00, 0x58, 0x00, 0x35, 0x00, 0x00, 0x00,
770         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0xed, 0xbf, 0xa5, 0xcd, 0xb1, 0xae, 0xbe,
771         0x88, 0x35, 0x56, 0xe5, 0xc2, 0x01, 0xaf, 0xbe, 0xc8, 0xed, 0xbf, 0xa5, 0x8c, 0x33, 0x40, 0xe4,
772         0x08, 0x30, 0x40, 0xe4, 0x01, 0x00, 0x00, 0x00, 0x08, 0x30, 0x40, 0xe4, 0x00, 0x00, 0x00, 0x00,
773         0xc0, 0xed, 0xbf, 0xa5, 0x34, 0xf5, 0xae, 0xbe, 0x08, 0x30, 0x40, 0xe4, 0xb4, 0xed, 0xbf, 0xa5,
774         0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x0a, 0x00, 0x01, 0x00, 0x19, 0x00, 0x19, 0x00, 0x00, 0x00,
775         0x00, 0x00, 0x00, 0x00
776 };
777
778 #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); }
779 #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); }
780 #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); }
781
782 void
783 process_presented_license(STREAM s)
784 {
785         unsigned char client_license_random[SEC_RANDOM_SIZE];
786         unsigned char client_license_rsa_data[SEC_MODULUS_SIZE];
787
788         uint16 length, license_size;
789         uint32 unknown;
790         uint8 *license, hwid[LICENCE_HWID_SIZE], signature[LICENCE_SIGNATURE_SIZE];
791         int i;
792         
793         EXPECT8(2);                   // version
794         in_uint16_le(s, length);
795
796         EXPECT32(1);                  // unknown
797         EXPECT16(0);
798         EXPECT16(0x0201);
799
800         in_uint8a(s, client_license_random, SEC_RANDOM_SIZE);
801         EXPECT16(0);
802         EXPECT16(SEC_MODULUS_SIZE + SEC_PADDING_SIZE);
803         in_uint8a(s, client_license_rsa_data, SEC_MODULUS_SIZE);
804         in_uint8s(s, SEC_PADDING_SIZE);
805
806         EXPECT16(1);
807         in_uint16_le(s, license_size);
808
809         license = (uint8 *)xmalloc(license_size);
810         in_uint8a(s, license, license_size);
811
812         printf("Received license:\n");
813         for (i = 0; i < license_size; ++i)
814                 printf(" 0x%02x", license[i]);
815         printf("\n");
816
817         EXPECT16(1);
818         EXPECT16(LICENCE_HWID_SIZE);
819         in_uint8a(s, hwid, LICENCE_HWID_SIZE);
820
821         printf("License hardware ID:\n");
822         for (i = 0; i < LICENCE_HWID_SIZE; ++i)
823                 printf(" 0x%02x", hwid[i]);
824         printf("\n");
825
826         in_uint8a(s, signature, LICENCE_SIGNATURE_SIZE);
827
828         if (!s_check_end(s)) {
829                 printf("Unknown data at the end of presented license!");
830         }
831
832         // now we can generate the keys we need
833         licence_generate_keys(client_license_random, demand_license + 4, client_license_rsa_data);
834 //      send_authreq();
835 }
836
837 /* Receive secure transport packet */
838 STREAM
839 sec_recv(void)
840 {
841         uint32 sec_flags;
842         uint16 channel;
843         STREAM s;
844
845         if ((s = mcs_recv(&channel)) != NULL)
846         {
847                 if (/*g_encryption || !g_licence_issued*/ 1)
848                 {
849                         in_uint32_le(s, sec_flags);
850                         printf("sec_flags=%x\n", sec_flags);
851                         
852                         if (sec_flags & SEC_ENCRYPT)
853                         {
854                                 printf("encrypt\n");
855                                 in_uint8s(s, 8);        /* signature */
856                                 sec_decrypt(s->p, s->end - s->p);
857                         }
858
859                         if (sec_flags & SEC_LICENCE_NEG)
860                         {
861                                 uint8 tag;
862                                 in_uint8(s, tag);
863                                 printf("SEC_LICENSE_NEG tag %x\n", tag);
864                                 
865                                 if (tag == LICENCE_TAG_PRESENT) {
866                                         process_presented_license(s);
867
868                                         {
869                                                 STREAM s;
870                                                 s = sec_init(SEC_LICENCE_NEG, sizeof(result_license));
871                                                 out_uint8p(s, result_license, sizeof(result_license));
872                                                 s_mark_end(s);
873                                                 sec_send(s, SEC_LICENCE_NEG);
874                                         }
875                                         
876                                         {
877                                                 STREAM s;
878                                                 printf("Sending DEMAND_ACTIVE (0x%x bytes)\n", sizeof(demand_active));
879                                                 s = sec_init(SEC_ENCRYPT, sizeof(demand_active));
880                                                 out_uint8p(s, demand_active, sizeof(demand_active));
881                                                 s_mark_end(s);
882                                                 sec_send(s, SEC_ENCRYPT);
883                                         }
884                                 }
885                                 
886                                 return NULL;
887                         }
888
889                         if (sec_flags & SEC_LOGON_INFO) 
890                         {
891                                 printf("Received logon packet!\n");
892                                 rdp_get_logon_info(s);
893                         
894                                 // demand a license
895                                 {
896                                         STREAM s;
897
898                                         s = sec_init(SEC_LICENCE_NEG, sizeof(demand_license));
899                                         out_uint8p(s, demand_license, sizeof(demand_license));
900                                         s_mark_end(s);
901                                         sec_send(s, SEC_LICENCE_NEG);
902                                 }
903                                 
904                                 return NULL;
905                         }
906
907                         if (sec_flags & SEC_CLIENT_RANDOM) {
908                                 uint32 length;
909                                 uint8 inr[SEC_MODULUS_SIZE];
910                                 int i;
911                                 
912                                 printf("Receiving the client random!\n");
913                                 in_uint32_le(s, length);
914                                 if (length != SEC_MODULUS_SIZE + SEC_PADDING_SIZE) {
915                                         error("Client random was wrong size, %u bytes\n", length);
916                                 }
917                                 in_uint8a(s, sec_crypted_random, SEC_MODULUS_SIZE);
918                                 in_uint8s(s, SEC_PADDING_SIZE);
919                                 if (!s_check_end(s)) {
920                                         error("Junk after client random\n");
921                                 }
922                                 
923                                 reverse(sec_crypted_random, SEC_MODULUS_SIZE);
924
925                                 RSA_private_decrypt(SEC_MODULUS_SIZE, sec_crypted_random, inr, privkey, RSA_NO_PADDING);
926                         
927                                 reverse(inr + SEC_RANDOM_SIZE, SEC_RANDOM_SIZE);
928                                 
929                                 printf("Unencrypted client random: ");
930                                 for (i = 0; i < SEC_RANDOM_SIZE; ++i) {
931                                         printf("0x%x ", inr[i + SEC_RANDOM_SIZE]);
932                                 }
933                                 printf("\n");
934
935                                 // now we can generate the keys
936                                 sec_generate_keys(inr + SEC_RANDOM_SIZE, cacert, 1);
937                                 return NULL;
938                         }
939                 }
940
941                 printf("Received MCS data on ch %u\n", channel);
942                 if (channel != MCS_GLOBAL_CHANNEL)
943                 {
944                         channel_process(s, channel);
945                         return NULL;
946                 }
947
948                 return s;
949         }
950
951         return NULL;
952 }
953
954 /* Disconnect a connection */
955 void
956 sec_disconnect(void)
957 {
958         mcs_disconnect();
959 }