1 /**
2  * xrdp: A Remote Desktop Protocol server.
3  *
4  * Copyright (C) Laxmikant Rashinkar 2013 LK.Rashinkar@gmail.com
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  */
19 
20 /*
21  * smartcard redirection support
22  */
23 
24 #ifndef _SMARTCARD_C
25 #define _SMARTCARD_C
26 
27 #include "parse.h"
28 #include "irp.h"
29 #include "trans.h"
30 
31 #define SCARD_SHARE_EXCLUSIVE       0x00000001
32 #define SCARD_SHARE_SHARED          0x00000002
33 #define SCARD_SHARE_DIRECT          0x00000003
34 
35 /* see [MS-RDPESC] 2.2.5 protocol identifier - Table A */
36 #define SCARD_PROTOCOL_UNDEFINED    0x00000000
37 #define SCARD_PROTOCOL_T0           0x00000001
38 #define SCARD_PROTOCOL_T1           0x00000002
39 #define SCARD_PROTOCOL_Tx           0x00000003
40 #define SCARD_PROTOCOL_RAW          0x00010000
41 
42 /* see [MS-RDPESC] 2.2.5 protocol identifier - Table B */
43 #define SCARD_PROTOCOL_DEFAULT      0x80000000
44 #define SCARD_PROTOCOL_OPTIMAL      0x00000000
45 
46 /* initialization type */
47 #define SCARD_LEAVE_CARD            0x00000000 /* do not do anything      */
48 #define SCARD_RESET_CARD            0x00000001 /* reset smart card        */
49 #define SCARD_UNPOWER_CARD          0x00000002 /* turn off and reset card */
50 
51 struct xrdp_scard_io_request
52 {
53     tui32 dwProtocol;
54     tui32 cbPciLength;
55     int extra_bytes;
56     char *extra_data;
57 };
58 
59 typedef struct reader_state
60 {
61     char   reader_name[128];
62     tui32  current_state;
63     tui32  event_state;
64     tui32  atr_len; /* number of bytes in atr[] */
65     tui8   atr[36];
66 
67     /*
68      * share mode flag, can be one of:
69      *  SCARD_SHARE_EXCLUSIVE  app not willing to share smartcard with other apps
70      *  SCARD_SHARE_SHARED     app willing to share smartcard with other apps
71      *  SCARD_SHARE_DIRECT     app demands direct control of smart card, hence
72      *                         it is not available to other readers
73      */
74     tui32  dwShareMode;
75 
76     /*
77      * This field MUST have a value from Table A which is logically
78      * OR'ed with a value from Table B.
79      */
80     tui32  dwPreferredProtocols;
81 
82     /*
83      * initialization type, must be one of the initialization type
84      * defined above
85      */
86     tui32  init_type;
87 
88     /* required by scard_send_transmit(), scard_send_control() */
89     tui32 map0;
90     tui32 map1;
91     tui32 map2;
92     tui32 map3;
93     tui32 map4;
94     tui32 map5;
95     tui32 map6;
96 
97     tui32 dwProtocol;
98     tui32 cbPciLength;
99     tui32 cbSendLength;
100     tui32 cbRecvLength;
101     tui32 dwControlCode;
102     tui32 cbOutBufferSize;
103     tui32 dwAttribId;
104     tui32 dwAttrLen;
105 
106 } READER_STATE;
107 
108 void scard_device_announce(tui32 device_id);
109 int  scard_get_wait_objs(tbus *objs, int *count, int *timeout);
110 int  scard_check_wait_objs(void);
111 int  scard_init(void);
112 int  scard_deinit(void);
113 int  scard_send_establish_context(void *user_data, int scope);
114 int  scard_send_release_context(void *user_data,
115                                 char *context, int context_bytes);
116 int  scard_send_is_valid_context(void *user_data,
117                                  char *context, int context_bytes);
118 int  scard_send_list_readers(void *user_data,
119                              char *context, int context_bytes,
120                              char *groups, int cchReaders, int wide);
121 
122 int  scard_send_get_status_change(void *user_data,
123                                   char *context, int context_bytes,
124                                   int wide, tui32 timeout,
125                                   tui32 num_readers, READER_STATE *rsa);
126 
127 int  scard_send_connect(void *user_data,
128                         char *context, int context_bytes, int wide,
129                         READER_STATE *rs);
130 
131 int  scard_send_reconnect(void *user_data,
132                           char *context, int context_bytes,
133                           char *card, int card_bytes,
134                           READER_STATE *rs);
135 
136 int  scard_send_begin_transaction(void *user_data,
137                                   char *context, int context_bytes,
138                                   char *card, int card_bytes);
139 int  scard_send_end_transaction(void *user_data,
140                                 char *context, int context_bytes,
141                                 char *card, int card_bytes,
142                                 tui32 dwDisposition);
143 int  scard_send_status(void *user_data, int wide,
144                        char *context, int context_bytes,
145                        char *card, int card_bytes,
146                        int cchReaderLen, int cbAtrLen);
147 int  scard_send_disconnect(void *user_data,
148                            char *context, int context_bytes,
149                            char *card, int card_bytes,
150                            int dwDisposition);
151 
152 int  scard_send_transmit(void *user_data,
153                          char *context, int context_bytes,
154                          char *card, int card_bytes,
155                          char *send_data, int send_bytes, int recv_bytes,
156                          struct xrdp_scard_io_request *send_ior,
157                          struct xrdp_scard_io_request *recv_ior);
158 
159 int  scard_send_control(void *user_data,
160                         char *context, int context_bytes,
161                         char *card, int card_bytes,
162                         char *send_data, int send_bytes,
163                         int recv_bytes, int control_code);
164 
165 int  scard_send_cancel(void *user_data,
166                        char *context, int context_bytes);
167 
168 int  scard_send_get_attrib(void *user_data, char *card, int card_bytes,
169                            READER_STATE *rs);
170 
171 /*
172  * Notes:
173  *      SCardTransmit - partially done
174  *      SCardControl - partially done
175  *      SCardListReaderGroups - not supported
176  *      SCardSetAttrib - not supported
177  */
178 #endif /* end #ifndef _SMARTCARD_C */
179