]> git.sesse.net Git - rdpsrv/blob - secure.c
Send encodings _correctly_ this time...
[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 static 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 static 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 uint16 g_server_rdp_version = 0;
55
56 /*
57  * General purpose 48-byte transformation, using two 32-byte salts (generally,
58  * a client and server salt) and a global salt value used for padding.
59  * Both SHA1 and MD5 algorithms are used.
60  */
61 void
62 sec_hash_48(uint8 * out, uint8 * in, uint8 * salt1, uint8 * salt2, uint8 salt)
63 {
64         uint8 shasig[20];
65         uint8 pad[4];
66         SHA_CTX sha;
67         MD5_CTX md5;
68         int i;
69
70         for (i = 0; i < 3; i++)
71         {
72                 memset(pad, salt + i, i + 1);
73
74                 SHA1_Init(&sha);
75                 SHA1_Update(&sha, pad, i + 1);
76                 SHA1_Update(&sha, in, 48);
77                 SHA1_Update(&sha, salt1, 32);
78                 SHA1_Update(&sha, salt2, 32);
79                 SHA1_Final(shasig, &sha);
80
81                 MD5_Init(&md5);
82                 MD5_Update(&md5, in, 48);
83                 MD5_Update(&md5, shasig, 20);
84                 MD5_Final(&out[i * 16], &md5);
85         }
86 }
87
88 /*
89  * Weaker 16-byte transformation, also using two 32-byte salts, but
90  * only using a single round of MD5.
91  */
92 void
93 sec_hash_16(uint8 * out, uint8 * in, uint8 * salt1, uint8 * salt2)
94 {
95         MD5_CTX md5;
96
97         MD5_Init(&md5);
98         MD5_Update(&md5, in, 16);
99         MD5_Update(&md5, salt1, 32);
100         MD5_Update(&md5, salt2, 32);
101         MD5_Final(out, &md5);
102 }
103
104 /* Reduce key entropy from 64 to 40 bits */
105 static void
106 sec_make_40bit(uint8 * key)
107 {
108         key[0] = 0xd1;
109         key[1] = 0x26;
110         key[2] = 0x9e;
111 }
112
113 /* Generate a session key and RC4 keys, given client and server randoms */
114 static void
115 sec_generate_keys(uint8 * client_key, uint8 * server_key, int rc4_key_size)
116 {
117         uint8 session_key[48];
118         uint8 temp_hash[48];
119         uint8 input[48];
120
121         /* Construct input data to hash */
122         memcpy(input, client_key, 24);
123         memcpy(input + 24, server_key, 24);
124
125         /* Generate session key - two rounds of sec_hash_48 */
126         sec_hash_48(temp_hash, input, client_key, server_key, 65);
127         sec_hash_48(session_key, temp_hash, client_key, server_key, 88);
128
129         /* Store first 16 bytes of session key, for generating signatures */
130         memcpy(sec_sign_key, session_key, 16);
131
132         /* Generate RC4 keys */
133         sec_hash_16(sec_decrypt_key, &session_key[16], client_key, server_key);
134         sec_hash_16(sec_encrypt_key, &session_key[32], client_key, server_key);
135
136         if (rc4_key_size == 1)
137         {
138                 DEBUG(("40-bit encryption enabled\n"));
139                 sec_make_40bit(sec_sign_key);
140                 sec_make_40bit(sec_decrypt_key);
141                 sec_make_40bit(sec_encrypt_key);
142                 rc4_key_len = 8;
143         }
144         else
145         {
146                 DEBUG(("rc_4_key_size == %d, 128-bit encryption enabled\n", rc4_key_size));
147                 rc4_key_len = 16;
148         }
149
150         /* Save initial RC4 keys as update keys */
151         memcpy(sec_decrypt_update_key, sec_decrypt_key, 16);
152         memcpy(sec_encrypt_update_key, sec_encrypt_key, 16);
153
154         /* Initialise RC4 state arrays */
155         RC4_set_key(&rc4_decrypt_key, rc4_key_len, sec_decrypt_key);
156         RC4_set_key(&rc4_encrypt_key, rc4_key_len, sec_encrypt_key);
157 }
158
159 static uint8 pad_54[40] = {
160         54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54,
161         54, 54, 54,
162         54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54,
163         54, 54, 54
164 };
165
166 static uint8 pad_92[48] = {
167         92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92,
168         92, 92, 92, 92, 92, 92, 92,
169         92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92,
170         92, 92, 92, 92, 92, 92, 92
171 };
172
173 /* Output a uint32 into a buffer (little-endian) */
174 void
175 buf_out_uint32(uint8 * buffer, uint32 value)
176 {
177         buffer[0] = (value) & 0xff;
178         buffer[1] = (value >> 8) & 0xff;
179         buffer[2] = (value >> 16) & 0xff;
180         buffer[3] = (value >> 24) & 0xff;
181 }
182
183 /* Generate a signature hash, using a combination of SHA1 and MD5 */
184 void
185 sec_sign(uint8 * signature, int siglen, uint8 * session_key, int keylen, uint8 * data, int datalen)
186 {
187         uint8 shasig[20];
188         uint8 md5sig[16];
189         uint8 lenhdr[4];
190         SHA_CTX sha;
191         MD5_CTX md5;
192
193         buf_out_uint32(lenhdr, datalen);
194
195         SHA1_Init(&sha);
196         SHA1_Update(&sha, session_key, keylen);
197         SHA1_Update(&sha, pad_54, 40);
198         SHA1_Update(&sha, lenhdr, 4);
199         SHA1_Update(&sha, data, datalen);
200         SHA1_Final(shasig, &sha);
201
202         MD5_Init(&md5);
203         MD5_Update(&md5, session_key, keylen);
204         MD5_Update(&md5, pad_92, 48);
205         MD5_Update(&md5, shasig, 20);
206         MD5_Final(md5sig, &md5);
207
208         memcpy(signature, md5sig, siglen);
209 }
210
211 /* Update an encryption key - similar to the signing process */
212 static void
213 sec_update(uint8 * key, uint8 * update_key)
214 {
215         uint8 shasig[20];
216         SHA_CTX sha;
217         MD5_CTX md5;
218         RC4_KEY update;
219
220         SHA1_Init(&sha);
221         SHA1_Update(&sha, update_key, rc4_key_len);
222         SHA1_Update(&sha, pad_54, 40);
223         SHA1_Update(&sha, key, rc4_key_len);
224         SHA1_Final(shasig, &sha);
225
226         MD5_Init(&md5);
227         MD5_Update(&md5, update_key, rc4_key_len);
228         MD5_Update(&md5, pad_92, 48);
229         MD5_Update(&md5, shasig, 20);
230         MD5_Final(key, &md5);
231
232         RC4_set_key(&update, rc4_key_len, key);
233         RC4(&update, rc4_key_len, key, key);
234
235         if (rc4_key_len == 8)
236                 sec_make_40bit(key);
237 }
238
239 /* Encrypt data using RC4 */
240 static void
241 sec_encrypt(uint8 * data, int length)
242 {
243         static int use_count;
244
245         if (use_count == 4096)
246         {
247                 sec_update(sec_encrypt_key, sec_encrypt_update_key);
248                 RC4_set_key(&rc4_encrypt_key, rc4_key_len, sec_encrypt_key);
249                 use_count = 0;
250         }
251
252         RC4(&rc4_encrypt_key, length, data, data);
253         use_count++;
254 }
255
256 /* Decrypt data using RC4 */
257 void
258 sec_decrypt(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         RC4(&rc4_decrypt_key, length, data, data);
270         use_count++;
271 }
272
273 static void
274 reverse(uint8 * p, int len)
275 {
276         int i, j;
277         uint8 temp;
278
279         for (i = 0, j = len - 1; i < j; i++, j--)
280         {
281                 temp = p[i];
282                 p[i] = p[j];
283                 p[j] = temp;
284         }
285 }
286
287 /* Perform an RSA public key encryption operation */
288 static void
289 sec_rsa_encrypt(uint8 * out, uint8 * in, int len, uint8 * modulus, uint8 * exponent)
290 {
291         BN_CTX *ctx;
292         BIGNUM mod, exp, x, y;
293         uint8 inr[SEC_MODULUS_SIZE];
294         int outlen;
295
296         reverse(modulus, SEC_MODULUS_SIZE);
297         reverse(exponent, SEC_EXPONENT_SIZE);
298         memcpy(inr, in, len);
299         reverse(inr, len);
300
301         ctx = BN_CTX_new();
302         BN_init(&mod);
303         BN_init(&exp);
304         BN_init(&x);
305         BN_init(&y);
306
307         BN_bin2bn(modulus, SEC_MODULUS_SIZE, &mod);
308         BN_bin2bn(exponent, SEC_EXPONENT_SIZE, &exp);
309         BN_bin2bn(inr, len, &x);
310         BN_mod_exp(&y, &x, &exp, &mod, ctx);
311         outlen = BN_bn2bin(&y, out);
312         reverse(out, outlen);
313         if (outlen < SEC_MODULUS_SIZE)
314                 memset(out + outlen, 0, SEC_MODULUS_SIZE - outlen);
315
316         BN_free(&y);
317         BN_clear_free(&x);
318         BN_free(&exp);
319         BN_free(&mod);
320         BN_CTX_free(ctx);
321 }
322
323 /* Initialise secure transport packet */
324 STREAM
325 sec_init(uint32 flags, int maxlen)
326 {
327         int hdrlen;
328         STREAM s;
329
330         /*if (!g_licence_issued) */
331                 hdrlen = (flags & SEC_ENCRYPT) ? 12 : 4;
332 /*      else
333                 hdrlen = (flags & SEC_ENCRYPT) ? 12 : 0; */
334         s = mcs_init(maxlen + hdrlen);
335         s_push_layer(s, sec_hdr, hdrlen);
336
337         return s;
338 }
339
340 /* Transmit secure transport packet over specified channel */
341 void
342 sec_send_to_channel(STREAM s, uint32 flags, uint16 channel)
343 {
344         int datalen;
345
346         DEBUG(("sending packet to channel %u", channel));
347         
348         s_pop_layer(s, sec_hdr);
349 //      if (!g_licence_issued || (flags & SEC_ENCRYPT))
350                 out_uint32_le(s, flags);
351
352         if (flags & SEC_ENCRYPT)
353         {
354                 flags &= ~SEC_ENCRYPT;
355                 datalen = s->end - s->p - 8;
356
357 #if WITH_DEBUG
358                 DEBUG(("Sending encrypted packet:\n"));
359                 hexdump(s->p + 8, datalen);
360 #endif
361
362                 sec_sign(s->p, 8, sec_sign_key, rc4_key_len, s->p + 8, datalen);
363                 sec_encrypt(s->p + 8, datalen);
364         }
365
366         mcs_send_to_channel(s, channel);
367 }
368
369 /* Transmit secure transport packet */
370
371 void
372 sec_send(STREAM s, uint32 flags)
373 {
374         sec_send_to_channel(s, flags, MCS_GLOBAL_CHANNEL);
375 }
376
377
378 /* Transfer the client random to the server */
379 static void
380 sec_establish_key(void)
381 {
382         uint32 length = SEC_MODULUS_SIZE + SEC_PADDING_SIZE;
383         uint32 flags = SEC_CLIENT_RANDOM;
384         STREAM s;
385
386         s = sec_init(flags, 76);
387
388         out_uint32_le(s, length);
389         out_uint8p(s, sec_crypted_random, SEC_MODULUS_SIZE);
390         out_uint8s(s, SEC_PADDING_SIZE);
391
392         s_mark_end(s);
393         sec_send(s, flags);
394 }
395
396 /* Parse a public key structure */
397 static BOOL
398 sec_parse_public_key(STREAM s, uint8 ** modulus, uint8 ** exponent)
399 {
400         uint32 magic, modulus_len;
401
402         in_uint32_le(s, magic);
403         if (magic != SEC_RSA_MAGIC)
404         {
405                 error("RSA magic 0x%x\n", magic);
406                 return False;
407         }
408
409         in_uint32_le(s, modulus_len);
410         if (modulus_len != SEC_MODULUS_SIZE + SEC_PADDING_SIZE)
411         {
412                 error("modulus len 0x%x\n", modulus_len);
413                 return False;
414         }
415
416         in_uint8s(s, 8);        /* modulus_bits, unknown */
417         in_uint8p(s, *exponent, SEC_EXPONENT_SIZE);
418         in_uint8p(s, *modulus, SEC_MODULUS_SIZE);
419         in_uint8s(s, SEC_PADDING_SIZE);
420
421         return s_check(s);
422 }
423
424 static BOOL
425 sec_parse_x509_key(X509 * cert)
426 {
427         EVP_PKEY *epk = NULL;
428         /* By some reason, Microsoft sets the OID of the Public RSA key to
429            the oid for "MD5 with RSA Encryption" instead of "RSA Encryption"
430
431            Kudos to Richard Levitte for the following (. intiutive .) 
432            lines of code that resets the OID and let's us extract the key. */
433         if (OBJ_obj2nid(cert->cert_info->key->algor->algorithm) == NID_md5WithRSAEncryption)
434         {
435                 DEBUG_RDP5(("Re-setting algorithm type to RSA in server certificate\n"));
436                 cert->cert_info->key->algor->algorithm = OBJ_nid2obj(NID_rsaEncryption);
437         }
438         epk = X509_get_pubkey(cert);
439         if (NULL == epk)
440         {
441                 error("Failed to extract public key from certificate\n");
442                 return False;
443         }
444
445         server_public_key = (RSA *) epk->pkey.ptr;
446
447         return True;
448 }
449
450
451 /* Parse a crypto information structure */
452 static BOOL
453 sec_parse_crypt_info(STREAM s, uint32 * rc4_key_size,
454                      uint8 ** server_random, uint8 ** modulus, uint8 ** exponent)
455 {
456         uint32 crypt_level, random_len, rsa_info_len;
457         uint32 cacert_len, cert_len, flags;
458         X509 *cacert, *server_cert;
459         uint16 tag, length;
460         uint8 *next_tag, *end;
461
462         in_uint32_le(s, *rc4_key_size); /* 1 = 40-bit, 2 = 128-bit */
463         in_uint32_le(s, crypt_level);   /* 1 = low, 2 = medium, 3 = high */
464         if (crypt_level == 0)   /* no encryption */
465                 return False;
466         in_uint32_le(s, random_len);
467         in_uint32_le(s, rsa_info_len);
468
469         if (random_len != SEC_RANDOM_SIZE)
470         {
471                 error("random len %d, expected %d\n", random_len, SEC_RANDOM_SIZE);
472                 return False;
473         }
474
475         in_uint8p(s, *server_random, random_len);
476
477         /* RSA info */
478         end = s->p + rsa_info_len;
479         if (end > s->end)
480                 return False;
481
482         in_uint32_le(s, flags); /* 1 = RDP4-style, 0x80000002 = X.509 */
483         if (flags & 1)
484         {
485                 DEBUG_RDP5(("We're going for the RDP4-style encryption\n"));
486                 in_uint8s(s, 8);        /* unknown */
487
488                 while (s->p < end)
489                 {
490                         in_uint16_le(s, tag);
491                         in_uint16_le(s, length);
492
493                         next_tag = s->p + length;
494
495                         switch (tag)
496                         {
497                                 case SEC_TAG_PUBKEY:
498                                         if (!sec_parse_public_key(s, modulus, exponent))
499                                                 return False;
500                                         DEBUG_RDP5(("Got Public key, RDP4-style\n"));
501
502                                         break;
503
504                                 case SEC_TAG_KEYSIG:
505                                         /* Is this a Microsoft key that we just got? */
506                                         /* Care factor: zero! */
507                                         /* Actually, it would probably be a good idea to check if the public key is signed with this key, and then store this 
508                                            key as a known key of the hostname. This would prevent some MITM-attacks. */
509                                         break;
510
511                                 default:
512                                         unimpl("crypt tag 0x%x\n", tag);
513                         }
514
515                         s->p = next_tag;
516                 }
517         }
518         else
519         {
520                 uint32 certcount;
521
522                 DEBUG_RDP5(("We're going for the RDP5-style encryption\n"));
523                 in_uint32_le(s, certcount); /* Number of certificates */
524
525                 if(certcount < 2) 
526                 {
527                         error("Server didn't send enough X509 certificates\n");
528                         return False;
529                 }
530
531                 for(; certcount > 2; certcount--)
532                 { /* ignore all the certificates between the root and the signing CA */
533                         uint32 ignorelen;
534                         X509 *ignorecert;
535
536                         DEBUG_RDP5(("Ignored certs left: %d\n", certcount));
537
538                         in_uint32_le(s, ignorelen);
539                         DEBUG_RDP5(("Ignored Certificate length is %d\n", ignorelen));
540                         ignorecert = d2i_X509(NULL, &(s->p), ignorelen);
541
542                         if(ignorecert == NULL)
543                         { /* XXX: error out? */
544                                 DEBUG_RDP5(("got a bad cert: this will probably screw up the rest of the communication\n"));
545                         }
546
547 #ifdef WITH_DEBUG_RDP5
548                         DEBUG_RDP5(("cert #%d (ignored):\n",certcount));
549                         X509_print_fp(stdout, ignorecert);
550 #endif
551                 }
552
553                 /* Do da funky X.509 stuffy 
554
555                    "How did I find out about this?  I looked up and saw a
556                    bright light and when I came to I had a scar on my forehead
557                    and knew about X.500"
558                    - Peter Gutman in a early version of 
559                    http://www.cs.auckland.ac.nz/~pgut001/pubs/x509guide.txt
560                  */
561
562                 in_uint32_le(s, cacert_len);
563                 DEBUG_RDP5(("CA Certificate length is %d\n", cacert_len));
564                 cacert = d2i_X509(NULL, &(s->p), cacert_len);
565                 /* Note: We don't need to move s->p here - d2i_X509 is
566                    "kind" enough to do it for us */
567                 if (NULL == cacert)
568                 {
569                         error("Couldn't load CA Certificate from server\n");
570                         return False;
571                 }
572
573                 /* Currently, we don't use the CA Certificate. 
574                    FIXME: 
575                    *) Verify the server certificate (server_cert) with the 
576                    CA certificate.
577                    *) Store the CA Certificate with the hostname of the 
578                    server we are connecting to as key, and compare it
579                    when we connect the next time, in order to prevent
580                    MITM-attacks.
581                  */
582
583                 in_uint32_le(s, cert_len);
584                 DEBUG_RDP5(("Certificate length is %d\n", cert_len));
585                 server_cert = d2i_X509(NULL, &(s->p), cert_len);
586                 if (NULL == server_cert)
587                 {
588                         error("Couldn't load Certificate from server\n");
589                         return False;
590                 }
591
592                 in_uint8s(s, 16);       /* Padding */
593
594                 /* Note: Verifying the server certificate must be done here, 
595                    before sec_parse_public_key since we'll have to apply
596                    serious violence to the key after this */
597
598                 if (!sec_parse_x509_key(server_cert))
599                 {
600                         DEBUG_RDP5(("Didn't parse X509 correctly\n"));
601                         return False;
602                 }
603                 return True;    /* There's some garbage here we don't care about */
604         }
605         return s_check_end(s);
606 }
607
608 /* Process crypto information blob */
609 static void
610 sec_process_crypt_info(STREAM s)
611 {
612         uint8 *server_random, *modulus, *exponent;
613         uint8 client_random[SEC_RANDOM_SIZE];
614         uint32 rc4_key_size;
615         uint8 inr[SEC_MODULUS_SIZE];
616
617         if (!sec_parse_crypt_info(s, &rc4_key_size, &server_random, &modulus, &exponent))
618         {
619                 DEBUG(("Failed to parse crypt info\n"));
620                 return;
621         }
622
623         DEBUG(("Generating client random\n"));
624         /* Generate a client random, and hence determine encryption keys */
625         /* This is what the MS client do: */
626         memset(inr, 0, SEC_RANDOM_SIZE);
627         /*  *ARIGL!* Plaintext attack, anyone?
628            I tried doing:
629            generate_random(inr);
630            ..but that generates connection errors now and then (yes, 
631            "now and then". Something like 0 to 3 attempts needed before a 
632            successful connection. Nice. Not! 
633          */
634
635         generate_random(client_random);
636         if (NULL != server_public_key)
637         {                       /* Which means we should use 
638                                    RDP5-style encryption */
639
640                 memcpy(inr + SEC_RANDOM_SIZE, client_random, SEC_RANDOM_SIZE);
641                 reverse(inr + SEC_RANDOM_SIZE, SEC_RANDOM_SIZE);
642
643                 RSA_public_encrypt(SEC_MODULUS_SIZE,
644                                    inr, sec_crypted_random, server_public_key, RSA_NO_PADDING);
645
646                 reverse(sec_crypted_random, SEC_MODULUS_SIZE);
647
648         }
649         else
650         {                       /* RDP4-style encryption */
651                 sec_rsa_encrypt(sec_crypted_random,
652                                 client_random, SEC_RANDOM_SIZE, modulus, exponent);
653         }
654         sec_generate_keys(client_random, server_random, rc4_key_size);
655 }
656
657
658 /* Process SRV_INFO, find RDP version supported by server */
659 static void
660 sec_process_srv_info(STREAM s)
661 {
662         in_uint16_le(s, g_server_rdp_version);
663         DEBUG_RDP5(("Server RDP version is %d\n", g_server_rdp_version));
664 /*      if (1 == g_server_rdp_version)
665                 g_use_rdp5 = 0; */
666 }
667
668
669 /* Process connect response data blob */
670 void
671 sec_process_mcs_data(STREAM s)
672 {
673         uint16 tag, length;
674         uint8 *next_tag;
675         uint8 len;
676
677         in_uint8s(s, 21);       /* header (T.124 stuff, probably) */
678         in_uint8(s, len);
679         if (len & 0x80)
680                 in_uint8(s, len);
681
682         while (s->p < s->end)
683         {
684                 in_uint16_le(s, tag);
685                 in_uint16_le(s, length);
686
687                 if (length <= 4)
688                         return;
689
690                 next_tag = s->p + length - 4;
691
692                 switch (tag)
693                 {
694                         case SEC_TAG_SRV_INFO:
695                                 sec_process_srv_info(s);
696                                 break;
697
698                         case SEC_TAG_SRV_CRYPT:
699                                 sec_process_crypt_info(s);
700                                 break;
701
702                         case SEC_TAG_SRV_CHANNELS:
703                                 /* FIXME: We should parse this information and
704                                    use it to map RDP5 channels to MCS 
705                                    channels */
706                                 break;
707
708                         default:
709                                 unimpl("response tag 0x%x\n", tag);
710                 }
711
712                 s->p = next_tag;
713         }
714 }
715
716 /* Receive secure transport packet */
717 STREAM
718 sec_recv(void)
719 {
720         uint32 sec_flags;
721         uint16 channel;
722         STREAM s;
723
724         while ((s = mcs_recv(&channel)) != NULL)
725         {
726                 if (/*g_encryption || !g_licence_issued*/ 1)
727                 {
728                         in_uint32_le(s, sec_flags);
729                         printf("sec_flags=%x\n", sec_flags);
730                         
731                         if (sec_flags & SEC_ENCRYPT)
732                         {
733                                 printf("encrypt\n");
734                                 in_uint8s(s, 8);        /* signature */
735                                 sec_decrypt(s->p, s->end - s->p);
736                         }
737
738                         if (sec_flags & SEC_LICENCE_NEG)
739                         {
740                                 printf("SEC_LICENSE_NEG unknown\n");
741                                 //licence_process(s);
742                                 continue;
743                         }
744
745                         if (sec_flags & SEC_LOGON_INFO) 
746                         {
747                                 printf("Received logon packet!\n");
748                                 rdp_get_logon_info(s);
749                         }
750                 }
751
752                 printf("Received MCS data on ch %u\n", channel);
753                 if (channel != MCS_GLOBAL_CHANNEL)
754                 {
755                         channel_process(s, channel);
756                         continue;
757                 }
758
759                 return s;
760         }
761
762         return NULL;
763 }
764
765 /* Disconnect a connection */
766 void
767 sec_disconnect(void)
768 {
769         mcs_disconnect();
770 }