1 | /*
|
---|
2 | * module-cccam.h
|
---|
3 | *
|
---|
4 | * Created on: 23.04.2010
|
---|
5 | * Author: alno
|
---|
6 | */
|
---|
7 | #ifndef MODULECCCAM_H_
|
---|
8 | #define MODULECCCAM_H_
|
---|
9 |
|
---|
10 | #include "module-datastruct-llist.h"
|
---|
11 | #include "globals.h"
|
---|
12 |
|
---|
13 | #include "cscrypt/rc6.h"
|
---|
14 | #include "cscrypt/idea.h"
|
---|
15 |
|
---|
16 | #define CC_MAXMSGSIZE 0x400 //by Project::Keynation: Buffer size is limited on "O" CCCam to 1024 bytes
|
---|
17 | #define CC_MAX_PROV 32
|
---|
18 | #define SWAPC(X, Y) do { char p; p = *X; *X = *Y; *Y = p; } while(0)
|
---|
19 |
|
---|
20 | #if (defined(WIN32) || defined(OS_CYGWIN32)) && !defined(MSG_WAITALL)
|
---|
21 | # define MSG_WAITALL 0
|
---|
22 | #endif
|
---|
23 |
|
---|
24 | #define MINIMIZE_NONE 0
|
---|
25 | #define MINIMIZE_HOPS 1
|
---|
26 | #define MINIMIZE_CAID 2
|
---|
27 | #define MINIMIZE_TRANSPARENT 3
|
---|
28 |
|
---|
29 | #define CCCAM_MODE_NOTINIT 0
|
---|
30 | #define CCCAM_MODE_NORMAL 1
|
---|
31 | #define CCCAM_MODE_SHUTDOWN 0xFF
|
---|
32 |
|
---|
33 | #define QUITERROR 1
|
---|
34 |
|
---|
35 | typedef enum {
|
---|
36 | DECRYPT, ENCRYPT
|
---|
37 | } cc_crypt_mode_t;
|
---|
38 |
|
---|
39 | typedef enum {
|
---|
40 | MSG_CLI_DATA = 0,
|
---|
41 | MSG_CW_ECM = 1,
|
---|
42 | MSG_EMM_ACK = 2,
|
---|
43 | MSG_CARD_REMOVED = 4,
|
---|
44 | MSG_CMD_05 = 5,
|
---|
45 | MSG_KEEPALIVE = 6,
|
---|
46 | MSG_NEW_CARD = 7,
|
---|
47 | MSG_SRV_DATA = 8,
|
---|
48 | MSG_CMD_0A = 0x0a,
|
---|
49 | MSG_CMD_0B = 0x0b,
|
---|
50 | MSG_CMD_0C = 0x0c, // CCCam 2.2.x fake client checks
|
---|
51 | MSG_CMD_0D = 0x0d, // "
|
---|
52 | MSG_CMD_0E = 0x0e, // "
|
---|
53 | MSG_NEW_CARD_SIDINFO = 0x0f,
|
---|
54 | MSG_CW_NOK1 = 0xfe, //Node no more available
|
---|
55 | MSG_CW_NOK2 = 0xff, //No decoding
|
---|
56 | MSG_NO_HEADER = 0xffff
|
---|
57 | } cc_msg_type_t;
|
---|
58 |
|
---|
59 | struct cc_crypt_block {
|
---|
60 | uint8_t keytable[256];
|
---|
61 | uint8_t state;
|
---|
62 | uint8_t counter;
|
---|
63 | uint8_t sum;
|
---|
64 | };
|
---|
65 |
|
---|
66 | struct cc_srvid {
|
---|
67 | uint16_t sid;
|
---|
68 | uint8_t ecmlen;
|
---|
69 | };
|
---|
70 |
|
---|
71 | struct cc_provider {
|
---|
72 | uint32_t prov; //provider
|
---|
73 | uint8_t sa[4]; //shared address
|
---|
74 | };
|
---|
75 |
|
---|
76 | typedef enum {
|
---|
77 | CT_LOCALCARD = 1,
|
---|
78 | CT_CARD_BY_SERVICE_READER = 2,
|
---|
79 | CT_CARD_BY_SERVICE_USER = 3,
|
---|
80 | CT_CARD_BY_CAID = 4,
|
---|
81 | CT_REMOTECARD = 5
|
---|
82 | } cc_card_type;
|
---|
83 |
|
---|
84 | struct cc_card {
|
---|
85 | uint32_t id; // cccam card (share) id - reader
|
---|
86 | uint32_t remote_id;
|
---|
87 | uint16_t caid;
|
---|
88 | uint8_t hop;
|
---|
89 | uint8_t reshare;
|
---|
90 | uint8_t hexserial[8]; // card serial (for au)
|
---|
91 | LLIST *providers; // providers (struct cc_provider)
|
---|
92 | LLIST *badsids; // sids that have failed to decode (struct cc_srvid)
|
---|
93 | time_t time;
|
---|
94 | LLIST *goodsids; //sids that could decoded (struct cc_srvid)
|
---|
95 | LLIST *remote_nodes; //remote note id, 8 bytes
|
---|
96 | struct s_reader *origin_reader;
|
---|
97 | uint32_t origin_id;
|
---|
98 | cc_card_type card_type;
|
---|
99 | struct s_sidtab *sidtab; //pointer to sidtab entry if card_type = CT_CARD_BY_SERVICE
|
---|
100 | uint64_t grp;
|
---|
101 | uint8_t rdr_reshare;
|
---|
102 | };
|
---|
103 |
|
---|
104 | struct cc_auto_blocked {
|
---|
105 | uint16_t caid;
|
---|
106 | uint32_t prov;
|
---|
107 | struct cc_srvid srvid;
|
---|
108 | time_t time;
|
---|
109 | };
|
---|
110 |
|
---|
111 | struct cc_current_card {
|
---|
112 | struct cc_card *card;
|
---|
113 | uint32_t prov;
|
---|
114 | struct cc_srvid srvid;
|
---|
115 | };
|
---|
116 |
|
---|
117 | typedef enum {
|
---|
118 | MODE_UNKNOWN = 0,
|
---|
119 | MODE_PLAIN = 1,
|
---|
120 | MODE_AES = 2,
|
---|
121 | MODE_CC_CRYPT = 3,
|
---|
122 | MODE_RC4_CRYPT = 4,
|
---|
123 | MODE_LEN0 = 5,
|
---|
124 | } cc_cmd05_mode;
|
---|
125 |
|
---|
126 | typedef enum {
|
---|
127 | MODE_CMD_0x0C_NONE = 0,
|
---|
128 | MODE_CMD_0x0C_RC6 = 1,
|
---|
129 | MODE_CMD_0x0C_RC4 = 2,
|
---|
130 | MODE_CMD_0x0C_CC_CRYPT = 3,
|
---|
131 | MODE_CMD_0x0C_AES = 4,
|
---|
132 | MODE_CMD_0x0C_IDEA = 5,
|
---|
133 | } cc_cmd0c_mode;
|
---|
134 |
|
---|
135 |
|
---|
136 | struct cc_extended_ecm_idx {
|
---|
137 | uint8_t send_idx;
|
---|
138 | uint16_t ecm_idx;
|
---|
139 | struct cc_card *card;
|
---|
140 | struct cc_srvid srvid;
|
---|
141 | } EXTENDED_ECM_IDX;
|
---|
142 |
|
---|
143 | struct cc_data {
|
---|
144 | uint8_t g_flag;
|
---|
145 | char *prefix;
|
---|
146 |
|
---|
147 | struct cc_crypt_block block[2]; // crypto state blocks
|
---|
148 |
|
---|
149 | uint8_t node_id[8], // client node id
|
---|
150 | peer_node_id[8], // server node id
|
---|
151 | peer_version[8], // server version
|
---|
152 | dcw[16]; // control words
|
---|
153 | uint8_t cmd0b_aeskey[16];
|
---|
154 | uint8_t cmd05_aeskey[16];
|
---|
155 | struct cc_crypt_block cmd05_cryptkey;
|
---|
156 |
|
---|
157 | uint8_t is_oscam_cccam;
|
---|
158 | uint8_t cmd05_active;
|
---|
159 | int32_t cmd05_data_len;
|
---|
160 | uint8_t cmd05_data[256];
|
---|
161 | cc_cmd05_mode cmd05_mode;
|
---|
162 | int32_t cmd05_offset;
|
---|
163 |
|
---|
164 | cc_cmd0c_mode cmd0c_mode;
|
---|
165 | struct cc_crypt_block cmd0c_cryptkey;
|
---|
166 | RC6KEY cmd0c_RC6_cryptkey;
|
---|
167 | AES_KEY cmd0c_AES_key;
|
---|
168 | IDEA_KEY_SCHEDULE cmd0c_IDEA_dkey;
|
---|
169 |
|
---|
170 | uint8_t receive_buffer[CC_MAXMSGSIZE];
|
---|
171 |
|
---|
172 | LLIST *cards; // cards list
|
---|
173 |
|
---|
174 | int32_t max_ecms;
|
---|
175 | int32_t ecm_counter;
|
---|
176 | int32_t card_added_count;
|
---|
177 | int32_t card_removed_count;
|
---|
178 | uint8_t just_logged_in; //true for checking NOK direct after login
|
---|
179 | uint8_t key_table; //key for CMD 0B
|
---|
180 |
|
---|
181 | LLIST *pending_emms; //pending emm list
|
---|
182 |
|
---|
183 | uint32_t recv_ecmtask;
|
---|
184 |
|
---|
185 | struct cc_card *last_emm_card;
|
---|
186 | int32_t server_ecm_pending; //initialized by server
|
---|
187 | uint16_t server_ecm_idx;
|
---|
188 |
|
---|
189 | pthread_mutex_t lockcmd;
|
---|
190 | pthread_mutex_t ecm_busy;
|
---|
191 | pthread_mutex_t cards_busy;
|
---|
192 | struct timeb ecm_time;
|
---|
193 | time_t answer_on_keepalive;
|
---|
194 | uint8_t last_msg;
|
---|
195 | uint8_t cmd05NOK;
|
---|
196 |
|
---|
197 | char remote_version[7];
|
---|
198 | char remote_build[7];
|
---|
199 | char remote_oscam[200];
|
---|
200 | uint8_t cccam220;
|
---|
201 |
|
---|
202 | uint8_t mode;
|
---|
203 |
|
---|
204 | //Extended Mode for SPECIAL clients:
|
---|
205 | uint8_t extended_mode;
|
---|
206 | LLIST *extended_ecm_idx;
|
---|
207 |
|
---|
208 | //stats:
|
---|
209 | int num_hop1;
|
---|
210 | int num_hop2;
|
---|
211 | int num_hopx;
|
---|
212 |
|
---|
213 | int num_reshare0;
|
---|
214 | int num_reshare1;
|
---|
215 | int num_reshare2;
|
---|
216 | int num_resharex;
|
---|
217 | };
|
---|
218 |
|
---|
219 | int32_t cc_cli_init();
|
---|
220 | int32_t cc_cli_init_int(struct s_client *cl);
|
---|
221 | void cc_cleanup(struct s_client *cl);
|
---|
222 | int32_t cc_cli_connect(struct s_client *cl);
|
---|
223 | int32_t cc_get_nxt_ecm(struct s_client *cl);
|
---|
224 | int32_t cc_send_pending_emms(struct s_client *cl);
|
---|
225 | void cc_rc4_crypt(struct cc_crypt_block *block, uint8_t *data, int32_t len,
|
---|
226 | cc_crypt_mode_t mode);
|
---|
227 | void free_extended_ecm_idx(struct cc_data *cc);
|
---|
228 | void cc_free_card(struct cc_card *card);
|
---|
229 | int32_t cc_UA_valid(uint8_t *ua);
|
---|
230 | void cc_UA_cccam2oscam(uint8_t *in, uint8_t *out, uint16_t caid);
|
---|
231 | int32_t cc_cmd_send(struct s_client *cl, uint8_t *buf, int32_t len, cc_msg_type_t cmd);
|
---|
232 | int32_t sid_eq(struct cc_srvid *srvid1, struct cc_srvid *srvid2);
|
---|
233 | int32_t same_card(struct cc_card *card1, struct cc_card *card2);
|
---|
234 | int32_t same_card2(struct cc_card *card1, struct cc_card *card2);
|
---|
235 | void cc_UA_oscam2cccam(uint8_t *in, uint8_t *out, uint16_t caid);
|
---|
236 | void cc_SA_oscam2cccam(uint8_t *in, uint8_t *out);
|
---|
237 | void cc_free_cardlist(LLIST *card_list, int32_t destroy_list);
|
---|
238 |
|
---|
239 | #endif /* MODULECCCAM_H_ */
|
---|