1 /**
2  * xrdp: A Remote Desktop Protocol server.
3  *
4  * Copyright (C) Jay Sorg 2004-2014
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * libxrdp header
19  */
20 
21 #if !defined(LIBXRDP_H)
22 #define LIBXRDP_H
23 
24 #include "arch.h"
25 #include "parse.h"
26 #include "trans.h"
27 #include "xrdp_constants.h"
28 #include "defines.h"
29 #include "os_calls.h"
30 #include "ssl_calls.h"
31 #include "list.h"
32 #include "log.h"
33 #include "file.h"
34 #include "libxrdpinc.h"
35 #include "xrdp_client_info.h"
36 #include "log.h"
37 
38 
39 /* iso */
40 struct xrdp_iso
41 {
42     struct xrdp_mcs *mcs_layer; /* owner */
43     int rdpNegData; /* bool */
44     int requestedProtocol;
45     int selectedProtocol;
46     int failureCode;
47     struct trans *trans;
48 };
49 
50 /* used in mcs */
51 struct mcs_channel_item
52 {
53     char name[16];
54     int flags;
55     int chanid;
56     int disabled;
57     int pad0;
58 };
59 
60 /* mcs */
61 struct xrdp_mcs
62 {
63     struct xrdp_sec *sec_layer; /* owner */
64     struct xrdp_iso *iso_layer;
65     int userid;
66     int chanid;
67     struct stream *client_mcs_data;
68     struct stream *server_mcs_data;
69     struct list *channel_list;
70 };
71 
72 /* fastpath */
73 struct xrdp_fastpath
74 {
75     struct xrdp_sec *sec_layer; /* owner */
76     struct trans *trans;
77     struct xrdp_session *session;
78     int numEvents;
79     int secFlags;
80 };
81 
82 /* Encryption Methods */
83 #define CRYPT_METHOD_NONE              0x00000000
84 #define CRYPT_METHOD_40BIT             0x00000001
85 #define CRYPT_METHOD_128BIT            0x00000002
86 #define CRYPT_METHOD_56BIT             0x00000008
87 #define CRYPT_METHOD_FIPS              0x00000010
88 
89 /* Encryption Levels */
90 #define CRYPT_LEVEL_NONE               0x00000000
91 #define CRYPT_LEVEL_LOW                0x00000001
92 #define CRYPT_LEVEL_CLIENT_COMPATIBLE  0x00000002
93 #define CRYPT_LEVEL_HIGH               0x00000003
94 #define CRYPT_LEVEL_FIPS               0x00000004
95 
96 
97 /* sec */
98 struct xrdp_sec
99 {
100     struct xrdp_rdp *rdp_layer; /* owner */
101     struct xrdp_mcs *mcs_layer;
102     struct xrdp_fastpath *fastpath_layer;
103     struct xrdp_channel *chan_layer;
104     char server_random[32];
105     char client_random[256];
106     char client_crypt_random[256 + 8]; /* 64 + 8, 256 + 8 */
107     struct stream client_mcs_data;
108     struct stream server_mcs_data;
109     int decrypt_use_count;
110     int encrypt_use_count;
111     char decrypt_key[16];
112     char encrypt_key[16];
113     char decrypt_update_key[16];
114     char encrypt_update_key[16];
115     int crypt_method;
116     int rc4_key_len; /* 8 = 40 bit, 16 = 128 bit */
117     int crypt_level;
118     char sign_key[16];
119     void *decrypt_rc4_info;
120     void *encrypt_rc4_info;
121     char pub_exp[4];
122     char pub_mod[256];
123     char pub_sig[64];
124     char pri_exp[256];
125     int rsa_key_bytes; /* 64 or 256 , 0 = no rdp security */
126     char fips_encrypt_key[24];
127     char fips_decrypt_key[24];
128     char fips_sign_key[20];
129     void *encrypt_fips_info;
130     void *decrypt_fips_info;
131     void *sign_fips_info;
132     int is_security_header_present; /* boolean */
133 };
134 
135 struct xrdp_drdynvc
136 {
137     int chan_id;
138     int status; /* see XRDP_DRDYNVC_STATUS_* */
139     int flags;
140     int pad0;
141     int (*open_response)(intptr_t id, int chan_id, int creation_status);
142     int (*close_response)(intptr_t id, int chan_id);
143     int (*data_first)(intptr_t id, int chan_id, char *data, int bytes, int total_bytes);
144     int (*data)(intptr_t id, int chan_id, char *data, int bytes);
145 };
146 
147 /* channel */
148 struct xrdp_channel
149 {
150     struct xrdp_sec *sec_layer;
151     struct xrdp_mcs *mcs_layer;
152     int drdynvc_channel_id;
153     int drdynvc_state;
154     struct stream *s;
155     struct xrdp_drdynvc drdynvcs[256];
156 };
157 
158 /* rdp */
159 struct xrdp_rdp
160 {
161     struct xrdp_session *session;
162     struct xrdp_sec *sec_layer;
163     int share_id;
164     int mcs_channel;
165     struct xrdp_client_info client_info;
166     struct xrdp_mppc_enc *mppc_enc;
167     void *rfx_enc;
168 };
169 
170 /* state */
171 struct xrdp_orders_state
172 {
173     int last_order; /* last order sent */
174 
175     int clip_left;  /* RDP_ORDER_BOUNDS, RDP_ORDER_LASTBOUNDS */
176     int clip_top;
177     int clip_right;
178     int clip_bottom;
179 
180     int rect_x; /* RDP_ORDER_RECT */
181     int rect_y;
182     int rect_cx;
183     int rect_cy;
184     int rect_color;
185 
186     int scr_blt_x; /* RDP_ORDER_SCREENBLT */
187     int scr_blt_y;
188     int scr_blt_cx;
189     int scr_blt_cy;
190     int scr_blt_rop;
191     int scr_blt_srcx;
192     int scr_blt_srcy;
193 
194     int pat_blt_x; /* RDP_ORDER_PATBLT */
195     int pat_blt_y;
196     int pat_blt_cx;
197     int pat_blt_cy;
198     int pat_blt_rop;
199     int pat_blt_bg_color;
200     int pat_blt_fg_color;
201     struct xrdp_brush pat_blt_brush;
202 
203     int dest_blt_x; /* RDP_ORDER_DESTBLT */
204     int dest_blt_y;
205     int dest_blt_cx;
206     int dest_blt_cy;
207     int dest_blt_rop;
208 
209     int line_mix_mode; /* RDP_ORDER_LINE */
210     int line_startx;
211     int line_starty;
212     int line_endx;
213     int line_endy;
214     int line_bg_color;
215     int line_rop;
216     struct xrdp_pen line_pen;
217 
218     int mem_blt_color_table; /* RDP_ORDER_MEMBLT */
219     int mem_blt_cache_id;
220     int mem_blt_x;
221     int mem_blt_y;
222     int mem_blt_cx;
223     int mem_blt_cy;
224     int mem_blt_rop;
225     int mem_blt_srcx;
226     int mem_blt_srcy;
227     int mem_blt_cache_idx;
228 
229     int text_font; /* RDP_ORDER_TEXT2 */
230     int text_flags;
231     int text_unknown;
232     int text_mixmode;
233     int text_fg_color;
234     int text_bg_color;
235     int text_clip_left;
236     int text_clip_top;
237     int text_clip_right;
238     int text_clip_bottom;
239     int text_box_left;
240     int text_box_top;
241     int text_box_right;
242     int text_box_bottom;
243     int text_x;
244     int text_y;
245     int text_len;
246     char *text_data;
247 
248     int com_blt_srcidx;    /* RDP_ORDER_COMPOSITE */  /* 2 */
249     int com_blt_srcformat;                            /* 2 */
250     int com_blt_srcwidth;                             /* 2 */
251     int com_blt_srcrepeat;                            /* 1 */
252     int com_blt_srctransform[10];                     /* 40 */
253     int com_blt_mskflags;                             /* 1 */
254     int com_blt_mskidx;                               /* 2 */
255     int com_blt_mskformat;                            /* 2 */
256     int com_blt_mskwidth;                             /* 2 */
257     int com_blt_mskrepeat;                            /* 1 */
258     int com_blt_op;                                   /* 1 */
259     int com_blt_srcx;                                 /* 2 */
260     int com_blt_srcy;                                 /* 2 */
261     int com_blt_mskx;                                 /* 2 */
262     int com_blt_msky;                                 /* 2 */
263     int com_blt_dstx;                                 /* 2 */
264     int com_blt_dsty;                                 /* 2 */
265     int com_blt_width;                                /* 2 */
266     int com_blt_height;                               /* 2 */
267     int com_blt_dstformat;                            /* 2 */
268 
269 };
270 
271 /* orders */
272 struct xrdp_orders
273 {
274     struct stream *out_s;
275     struct xrdp_rdp *rdp_layer;
276     struct xrdp_session *session;
277     struct xrdp_wm *wm;
278 
279     char *order_count_ptr; /* pointer to count, set when sending */
280     int order_count;
281     int order_level; /* inc for every call to xrdp_orders_init */
282     struct xrdp_orders_state orders_state;
283     void *jpeg_han;
284     int rfx_min_pixel;
285     /* shared */
286     struct stream *s;
287     struct stream *temp_s;
288 };
289 
290 #define PROTO_RDP_40 1
291 #define PROTO_RDP_50 2
292 
293 struct xrdp_mppc_enc
294 {
295     int    protocol_type;    /* PROTO_RDP_40, PROTO_RDP_50 etc */
296     char  *historyBuffer;    /* contains uncompressed data */
297     char  *outputBuffer;     /* contains compressed data */
298     char  *outputBufferPlus;
299     int    historyOffset;    /* next free slot in historyBuffer */
300     int    buf_len;          /* length of historyBuffer, protocol dependent */
301     int    bytes_in_opb;     /* compressed bytes available in outputBuffer */
302     int    flags;            /* PACKET_COMPRESSED, PACKET_AT_FRONT, PACKET_FLUSHED etc */
303     int    flagsHold;
304     int    first_pkt;        /* this is the first pkt passing through enc */
305     tui16 *hash_table;
306 };
307 
308 int
309 compress_rdp(struct xrdp_mppc_enc *enc, tui8 *srcData, int len);
310 struct xrdp_mppc_enc *
311 mppc_enc_new(int protocol_type);
312 void
313 mppc_enc_free(struct xrdp_mppc_enc *enc);
314 
315 /* xrdp_tcp.c */
316 struct xrdp_tcp *
317 xrdp_tcp_create(struct xrdp_iso *owner, struct trans *trans);
318 void
319 xrdp_tcp_delete(struct xrdp_tcp *self);
320 int
321 xrdp_tcp_init(struct xrdp_tcp *self, struct stream *s);
322 int
323 xrdp_tcp_recv(struct xrdp_tcp *self, struct stream *s, int len);
324 int
325 xrdp_tcp_send(struct xrdp_tcp *self, struct stream *s);
326 
327 /* xrdp_iso.c */
328 struct xrdp_iso *
329 xrdp_iso_create(struct xrdp_mcs *owner, struct trans *trans);
330 void
331 xrdp_iso_delete(struct xrdp_iso *self);
332 int
333 xrdp_iso_init(struct xrdp_iso *self, struct stream *s);
334 int
335 xrdp_iso_recv(struct xrdp_iso *self, struct stream *s);
336 int
337 xrdp_iso_send(struct xrdp_iso *self, struct stream *s);
338 int
339 xrdp_iso_incoming(struct xrdp_iso *self);
340 int
341 xrdp_iso_detect_tpkt(struct xrdp_iso *self, struct stream *s);
342 
343 /* xrdp_mcs.c */
344 struct xrdp_mcs *
345 xrdp_mcs_create(struct xrdp_sec *owner, struct trans *trans,
346                 struct stream *client_mcs_data,
347                 struct stream *server_mcs_data);
348 void
349 xrdp_mcs_delete(struct xrdp_mcs *self);
350 int
351 xrdp_mcs_init(struct xrdp_mcs *self, struct stream *s);
352 int
353 xrdp_mcs_recv(struct xrdp_mcs *self, struct stream *s, int *chan);
354 int
355 xrdp_mcs_send(struct xrdp_mcs *self, struct stream *s, int chan);
356 int
357 xrdp_mcs_incoming(struct xrdp_mcs *self);
358 int
359 xrdp_mcs_disconnect(struct xrdp_mcs *self);
360 
361 /* xrdp_sec.c */
362 struct xrdp_sec *
363 xrdp_sec_create(struct xrdp_rdp *owner, struct trans *trans);
364 void
365 xrdp_sec_delete(struct xrdp_sec *self);
366 int
367 xrdp_sec_init(struct xrdp_sec *self, struct stream *s);
368 int
369 xrdp_sec_get_fastpath_bytes(struct xrdp_sec *self);
370 int
371 xrdp_sec_init_fastpath(struct xrdp_sec *self, struct stream *s);
372 int
373 xrdp_sec_send_fastpath(struct xrdp_sec *self, struct stream *s);
374 int
375 xrdp_sec_recv_fastpath(struct xrdp_sec *self, struct stream *s);
376 int
377 xrdp_sec_recv(struct xrdp_sec *self, struct stream *s, int *chan);
378 int
379 xrdp_sec_send(struct xrdp_sec *self, struct stream *s, int chan);
380 int
381 xrdp_sec_process_mcs_data(struct xrdp_sec *self);
382 int
383 xrdp_sec_incoming(struct xrdp_sec *self);
384 int
385 xrdp_sec_disconnect(struct xrdp_sec *self);
386 
387 /* xrdp_rdp.c */
388 struct xrdp_rdp *
389 xrdp_rdp_create(struct xrdp_session *session, struct trans *trans);
390 void
391 xrdp_rdp_delete(struct xrdp_rdp *self);
392 int
393 xrdp_rdp_init(struct xrdp_rdp *self, struct stream *s);
394 int
395 xrdp_rdp_init_data(struct xrdp_rdp *self, struct stream *s);
396 int
397 xrdp_rdp_get_fastpath_bytes(struct xrdp_rdp *self);
398 int
399 xrdp_rdp_init_fastpath(struct xrdp_rdp *self, struct stream *s);
400 int
401 xrdp_rdp_recv(struct xrdp_rdp *self, struct stream *s, int *code);
402 int
403 xrdp_rdp_send(struct xrdp_rdp *self, struct stream *s, int pdu_type);
404 int
405 xrdp_rdp_send_data(struct xrdp_rdp *self, struct stream *s,
406                    int data_pdu_type);
407 int
408 xrdp_rdp_send_fastpath(struct xrdp_rdp *self, struct stream *s,
409                        int data_pdu_type);
410 int
411 xrdp_rdp_send_data_update_sync(struct xrdp_rdp *self);
412 int
413 xrdp_rdp_incoming(struct xrdp_rdp *self);
414 int
415 xrdp_rdp_process_data(struct xrdp_rdp *self, struct stream *s);
416 int
417 xrdp_rdp_disconnect(struct xrdp_rdp *self);
418 int
419 xrdp_rdp_send_deactivate(struct xrdp_rdp *self);
420 int
421 xrdp_rdp_send_session_info(struct xrdp_rdp *self, const char *data,
422                            int data_bytes);
423 
424 /* xrdp_orders.c */
425 struct xrdp_orders *
426 xrdp_orders_create(struct xrdp_session *session,
427                    struct xrdp_rdp *rdp_layer);
428 void
429 xrdp_orders_delete(struct xrdp_orders *self);
430 int
431 xrdp_orders_reset(struct xrdp_orders *self);
432 int
433 xrdp_orders_init(struct xrdp_orders *self);
434 int
435 xrdp_orders_send(struct xrdp_orders *self);
436 int
437 xrdp_orders_force_send(struct xrdp_orders *self);
438 int
439 xrdp_orders_check(struct xrdp_orders *self, int max_size);
440 int
441 xrdp_orders_rect(struct xrdp_orders *self, int x, int y, int cx, int cy,
442                  int color, struct xrdp_rect *rect);
443 int
444 xrdp_orders_screen_blt(struct xrdp_orders *self, int x, int y,
445                        int cx, int cy, int srcx, int srcy,
446                        int rop, struct xrdp_rect *rect);
447 int
448 xrdp_orders_pat_blt(struct xrdp_orders *self, int x, int y,
449                     int cx, int cy, int rop, int bg_color,
450                     int fg_color, struct xrdp_brush *brush,
451                     struct xrdp_rect *rect);
452 int
453 xrdp_orders_dest_blt(struct xrdp_orders *self, int x, int y,
454                      int cx, int cy, int rop,
455                      struct xrdp_rect *rect);
456 int
457 xrdp_orders_line(struct xrdp_orders *self, int mix_mode,
458                  int startx, int starty,
459                  int endx, int endy, int rop, int bg_color,
460                  struct xrdp_pen *pen,
461                  struct xrdp_rect *rect);
462 int
463 xrdp_orders_mem_blt(struct xrdp_orders *self, int cache_id,
464                     int color_table, int x, int y, int cx, int cy,
465                     int rop, int srcx, int srcy,
466                     int cache_idx, struct xrdp_rect *rect);
467 int
468 xrdp_orders_composite_blt(struct xrdp_orders *self, int srcidx,
469                           int srcformat, int srcwidth,
470                           int srcrepeat, int *srctransform, int mskflags,
471                           int mskidx, int mskformat, int mskwidth,
472                           int mskrepeat, int op, int srcx, int srcy,
473                           int mskx, int msky, int dstx, int dsty,
474                           int width, int height, int dstformat,
475                           struct xrdp_rect *rect);
476 int
477 xrdp_orders_text(struct xrdp_orders *self,
478                  int font, int flags, int mixmode,
479                  int fg_color, int bg_color,
480                  int clip_left, int clip_top,
481                  int clip_right, int clip_bottom,
482                  int box_left, int box_top,
483                  int box_right, int box_bottom,
484                  int x, int y, char *data, int data_len,
485                  struct xrdp_rect *rect);
486 int
487 xrdp_orders_send_palette(struct xrdp_orders *self, int *palette,
488                          int cache_id);
489 int
490 xrdp_orders_send_raw_bitmap(struct xrdp_orders *self,
491                             int width, int height, int bpp, char *data,
492                             int cache_id, int cache_idx);
493 int
494 xrdp_orders_send_bitmap(struct xrdp_orders *self,
495                         int width, int height, int bpp, char *data,
496                         int cache_id, int cache_idx);
497 int
498 xrdp_orders_send_font(struct xrdp_orders *self,
499                       struct xrdp_font_char *font_char,
500                       int font_index, int char_index);
501 int
502 xrdp_orders_send_raw_bitmap2(struct xrdp_orders *self,
503                              int width, int height, int bpp, char *data,
504                              int cache_id, int cache_idx);
505 int
506 xrdp_orders_send_bitmap2(struct xrdp_orders *self,
507                          int width, int height, int bpp, char *data,
508                          int cache_id, int cache_idx, int hints);
509 int
510 xrdp_orders_send_bitmap3(struct xrdp_orders *self,
511                          int width, int height, int bpp, char *data,
512                          int cache_id, int cache_idx, int hints);
513 int
514 xrdp_orders_send_brush(struct xrdp_orders *self, int width, int height,
515                        int bpp, int type, int size, char *data, int cache_id);
516 int
517 xrdp_orders_send_create_os_surface(struct xrdp_orders *self, int id,
518                                    int width, int height,
519                                    struct list *del_list);
520 int
521 xrdp_orders_send_switch_os_surface(struct xrdp_orders *self, int id);
522 
523 /* xrdp_bitmap_compress.c */
524 int
525 xrdp_bitmap_compress(char *in_data, int width, int height,
526                      struct stream *s, int bpp, int byte_limit,
527                      int start_line, struct stream *temp_s,
528                      int e);
529 int
530 xrdp_bitmap32_compress(char *in_data, int width, int height,
531                        struct stream *s, int bpp, int byte_limit,
532                        int start_line, struct stream *temp_s,
533                        int e, int flags);
534 int
535 xrdp_jpeg_compress(void *handle, char *in_data, int width, int height,
536                    struct stream *s, int bpp, int byte_limit,
537                    int start_line, struct stream *temp_s,
538                    int e, int quality);
539 
540 int
541 xrdp_codec_jpeg_compress(void *handle,
542                          int   format,   /* input data format */
543                          char *inp_data, /* input data */
544                          int   width,    /* width of inp_data */
545                          int   height,   /* height of inp_data */
546                          int   stride,   /* inp_data stride, in bytes*/
547                          int   x,        /* x loc in inp_data */
548                          int   y,        /* y loc in inp_data */
549                          int   cx,       /* width of area to compress */
550                          int   cy,       /* height of area to compress */
551                          int   quality,  /* higher numbers compress less */
552                          char *out_data, /* dest for jpg image */
553                          int  *io_len    /* length of out_data and on return
554                                             len of compressed data */
555                         );
556 
557 void *
558 xrdp_jpeg_init(void);
559 int
560 xrdp_jpeg_deinit(void *handle);
561 
562 /* xrdp_channel.c */
563 struct xrdp_channel *
564 xrdp_channel_create(struct xrdp_sec *owner, struct xrdp_mcs *mcs_layer);
565 void
566 xrdp_channel_delete(struct xrdp_channel *self);
567 int
568 xrdp_channel_init(struct xrdp_channel *self, struct stream *s);
569 int
570 xrdp_channel_send(struct xrdp_channel *self, struct stream *s, int channel_id,
571                   int total_data_len, int flags);
572 int
573 xrdp_channel_process(struct xrdp_channel *self, struct stream *s,
574                      int chanid);
575 int
576 xrdp_channel_drdynvc_start(struct xrdp_channel *self);
577 int
578 xrdp_channel_drdynvc_open(struct xrdp_channel *self, const char *name,
579                           int flags, struct xrdp_drdynvc_procs *procs,
580                           int *chan_id);
581 int
582 xrdp_channel_drdynvc_close(struct xrdp_channel *self, int chan_id);
583 int
584 xrdp_channel_drdynvc_data_first(struct xrdp_channel *self, int chan_id,
585                                 const char *data, int data_bytes,
586                                 int total_data_bytes);
587 int
588 xrdp_channel_drdynvc_data(struct xrdp_channel *self, int chan_id,
589                           const char *data, int data_bytes);
590 
591 /* xrdp_fastpath.c */
592 struct xrdp_fastpath *
593 xrdp_fastpath_create(struct xrdp_sec *owner, struct trans *trans);
594 void
595 xrdp_fastpath_delete(struct xrdp_fastpath *self);
596 int
597 xrdp_fastpath_recv(struct xrdp_fastpath *self, struct stream *s);
598 int
599 xrdp_fastpath_process_input_event(struct xrdp_fastpath *self, struct stream *s);
600 int
601 xrdp_fastpath_init(struct xrdp_fastpath *self, struct stream *s);
602 int
603 xrdp_fastpath_send(struct xrdp_fastpath *self, struct stream *s);
604 
605 /* xrdp_caps.c */
606 int
607 xrdp_caps_send_demand_active(struct xrdp_rdp *self);
608 int
609 xrdp_caps_process_confirm_active(struct xrdp_rdp *self, struct stream *s);
610 #endif
611