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