|
@@ -0,0 +1,420 @@
|
|
1
|
+/*
|
|
2
|
+ * newcamd protocol
|
|
3
|
+ * Most of the code is copied from getstream's newcamd protocol implementation.
|
|
4
|
+ *
|
|
5
|
+ * This program is free software; you can redistribute it and/or modify
|
|
6
|
+ * it under the terms of the GNU General Public License version 2
|
|
7
|
+ * as published by the Free Software Foundation.
|
|
8
|
+ *
|
|
9
|
+ * This program is distributed in the hope that it will be useful,
|
|
10
|
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
11
|
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
12
|
+ * GNU General Public License for more details.
|
|
13
|
+ *
|
|
14
|
+ * You should have received a copy of the GNU General Public License
|
|
15
|
+ * along with this program; if not, write to the Free Software
|
|
16
|
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
|
|
17
|
+ */
|
|
18
|
+
|
|
19
|
+#define _XOPEN_SOURCE 700 // Needed to pull crypt() from unistd.h
|
|
20
|
+#define _GNU_SOURCE 1 // Needed to pull crypt_r() from crypt.h
|
|
21
|
+
|
|
22
|
+#include <stdlib.h>
|
|
23
|
+#include <string.h>
|
|
24
|
+#include <unistd.h>
|
|
25
|
+#include <crypt.h>
|
|
26
|
+
|
|
27
|
+#include <openssl/des.h>
|
|
28
|
+
|
|
29
|
+#include "libfuncs/libfuncs.h"
|
|
30
|
+
|
|
31
|
+#include "data.h"
|
|
32
|
+#include "util.h"
|
|
33
|
+#include "camd.h"
|
|
34
|
+
|
|
35
|
+#define NEWCAMD_PROTO_VER 525
|
|
36
|
+#define NEWCAMD_HDR_LEN 8
|
|
37
|
+#define NEWCAMD_FIRST_CMD_NO 0xe0
|
|
38
|
+
|
|
39
|
+typedef enum {
|
|
40
|
+ MSG_CLIENT_2_SERVER_LOGIN = NEWCAMD_FIRST_CMD_NO,
|
|
41
|
+ MSG_CLIENT_2_SERVER_LOGIN_ACK,
|
|
42
|
+ MSG_CLIENT_2_SERVER_LOGIN_NAK,
|
|
43
|
+ MSG_CARD_DATA_REQ,
|
|
44
|
+ MSG_CARD_DATA,
|
|
45
|
+ MSG_SERVER_2_CLIENT_NAME,
|
|
46
|
+ MSG_SERVER_2_CLIENT_NAME_ACK,
|
|
47
|
+ MSG_SERVER_2_CLIENT_NAME_NAK,
|
|
48
|
+ MSG_SERVER_2_CLIENT_LOGIN,
|
|
49
|
+ MSG_SERVER_2_CLIENT_LOGIN_ACK,
|
|
50
|
+ MSG_SERVER_2_CLIENT_LOGIN_NAK,
|
|
51
|
+ MSG_ADMIN,
|
|
52
|
+ MSG_ADMIN_ACK,
|
|
53
|
+ MSG_ADMIN_LOGIN,
|
|
54
|
+ MSG_ADMIN_LOGIN_ACK,
|
|
55
|
+ MSG_ADMIN_LOGIN_NAK,
|
|
56
|
+ MSG_ADMIN_COMMAND,
|
|
57
|
+ MSG_ADMIN_COMMAND_ACK,
|
|
58
|
+ MSG_ADMIN_COMMAND_NAK,
|
|
59
|
+ MSG_KEEPALIVE = NEWCAMD_FIRST_CMD_NO + 0x1d
|
|
60
|
+} net_msg_type_t;
|
|
61
|
+
|
|
62
|
+static void set_odd_parity(uint8_t *key) {
|
|
63
|
+ DES_set_odd_parity((DES_cblock *)&key[0]);
|
|
64
|
+ DES_set_odd_parity((DES_cblock *)&key[8]);
|
|
65
|
+}
|
|
66
|
+
|
|
67
|
+static void des_schedule_key(triple_des_t *td) {
|
|
68
|
+ DES_key_sched((DES_cblock *)&td->des_key[0],&td->ks1);
|
|
69
|
+ DES_key_sched((DES_cblock *)&td->des_key[8],&td->ks2);
|
|
70
|
+}
|
|
71
|
+
|
|
72
|
+static void des_key_spread(triple_des_t *td, uint8_t *normal) {
|
|
73
|
+ td->des_key[0] = normal[0] & 0xfe;
|
|
74
|
+ td->des_key[1] = ((normal[0] << 7) | (normal[1] >> 1)) & 0xfe;
|
|
75
|
+ td->des_key[2] = ((normal[1] << 6) | (normal[2] >> 2)) & 0xfe;
|
|
76
|
+ td->des_key[3] = ((normal[2] << 5) | (normal[3] >> 3)) & 0xfe;
|
|
77
|
+ td->des_key[4] = ((normal[3] << 4) | (normal[4] >> 4)) & 0xfe;
|
|
78
|
+ td->des_key[5] = ((normal[4] << 3) | (normal[5] >> 5)) & 0xfe;
|
|
79
|
+ td->des_key[6] = ((normal[5] << 2) | (normal[6] >> 6)) & 0xfe;
|
|
80
|
+ td->des_key[7] = normal[6] << 1;
|
|
81
|
+ td->des_key[8] = normal[7] & 0xfe;
|
|
82
|
+ td->des_key[9] = ((normal[7] << 7) | (normal[8] >> 1)) & 0xfe;
|
|
83
|
+ td->des_key[10] = ((normal[8] << 6) | (normal[9] >> 2)) & 0xfe;
|
|
84
|
+ td->des_key[11] = ((normal[9] << 5) | (normal[10] >> 3)) & 0xfe;
|
|
85
|
+ td->des_key[12] = ((normal[10] << 4) | (normal[11] >> 4)) & 0xfe;
|
|
86
|
+ td->des_key[13] = ((normal[11] << 3) | (normal[12] >> 5)) & 0xfe;
|
|
87
|
+ td->des_key[14] = ((normal[12] << 2) | (normal[13] >> 6)) & 0xfe;
|
|
88
|
+ td->des_key[15] = normal[13] << 1;
|
|
89
|
+ set_odd_parity(td->des_key);
|
|
90
|
+};
|
|
91
|
+
|
|
92
|
+static uint8_t xor_sum(const uint8_t *mem, int len) {
|
|
93
|
+ uint8_t cs = 0;
|
|
94
|
+ while (len > 0) {
|
|
95
|
+ cs ^= *mem++;
|
|
96
|
+ len--;
|
|
97
|
+ }
|
|
98
|
+ return cs;
|
|
99
|
+}
|
|
100
|
+
|
|
101
|
+static int pad_message(uint8_t *data, int len) {
|
|
102
|
+ DES_cblock padBytes;
|
|
103
|
+ uint8_t noPadBytes = (8 - ((len - 1) % 8)) % 8;
|
|
104
|
+ if (len + noPadBytes + 1 >= NEWCAMD_MSG_SIZE - 8)
|
|
105
|
+ return -1;
|
|
106
|
+ DES_random_key(&padBytes);
|
|
107
|
+ memcpy(data + len, padBytes, noPadBytes);
|
|
108
|
+ len += noPadBytes;
|
|
109
|
+ data[len] = xor_sum(data + 2, len - 2);
|
|
110
|
+ return len + 1;
|
|
111
|
+}
|
|
112
|
+
|
|
113
|
+static const uint8_t *triple_des_encrypt(triple_des_t *td, uint8_t *data, int len, uint8_t *crypted) {
|
|
114
|
+ DES_cblock ivec;
|
|
115
|
+ DES_random_key(&ivec);
|
|
116
|
+ memcpy(crypted + len, ivec, sizeof(ivec));
|
|
117
|
+ DES_ede2_cbc_encrypt(data + 2, crypted + 2, len - 2, &td->ks1, &td->ks2, (DES_cblock *)ivec, DES_ENCRYPT);
|
|
118
|
+ return crypted;
|
|
119
|
+}
|
|
120
|
+
|
|
121
|
+static void triple_des_decrypt(triple_des_t *td, uint8_t *data, int len) {
|
|
122
|
+ if ((len-2) % 8 || (len-2) < 16)
|
|
123
|
+ return;
|
|
124
|
+ DES_cblock ivec;
|
|
125
|
+ len -= sizeof(ivec);
|
|
126
|
+ memcpy(ivec, data+len, sizeof(ivec));
|
|
127
|
+ DES_ede2_cbc_encrypt(data+2, data+2, len-2, &td->ks1, &td->ks2, (DES_cblock *)ivec, DES_DECRYPT);
|
|
128
|
+}
|
|
129
|
+
|
|
130
|
+static void prepare_login_key(struct camd *c, const uint8_t *rkey) {
|
|
131
|
+ unsigned int i;
|
|
132
|
+ uint8_t tmpkey[14];
|
|
133
|
+ for(i = 0; i < sizeof(tmpkey); i++)
|
|
134
|
+ tmpkey[i] = rkey[i] ^ c->newcamd.bin_des_key[i];
|
|
135
|
+ des_key_spread(&c->newcamd.td_key, tmpkey);
|
|
136
|
+}
|
|
137
|
+
|
|
138
|
+static int newcamd_connect(struct camd *c);
|
|
139
|
+
|
|
140
|
+static uint8_t newcamd_send_msg(struct camd *c, const uint8_t *data, int data_len, uint16_t service_id, uint8_t useMsgId) {
|
|
141
|
+ uint8_t netbuf[NEWCAMD_MSG_SIZE];
|
|
142
|
+
|
|
143
|
+ if (newcamd_connect(c) < 0)
|
|
144
|
+ return 0;
|
|
145
|
+
|
|
146
|
+ if (data_len < 3 || (data_len + NEWCAMD_HDR_LEN + 4) > NEWCAMD_MSG_SIZE) {
|
|
147
|
+ ts_LOGf("ERR | [%s] Bad message size.\n", c->ops.ident);
|
|
148
|
+ return 0; // false
|
|
149
|
+ }
|
|
150
|
+
|
|
151
|
+ memset(&netbuf[2], 0, NEWCAMD_HDR_LEN + 2);
|
|
152
|
+ memcpy(&netbuf[NEWCAMD_HDR_LEN + 4], data, data_len);
|
|
153
|
+
|
|
154
|
+ netbuf[NEWCAMD_HDR_LEN + 4 + 1] = (data[1] & 0xF0) | (((data_len - 3) >> 8) & 0x0F);
|
|
155
|
+ netbuf[NEWCAMD_HDR_LEN + 4 + 2] = (data_len - 3) & 0xFF;
|
|
156
|
+
|
|
157
|
+ data_len += 4;
|
|
158
|
+ netbuf[4] = service_id >> 8;
|
|
159
|
+ netbuf[5] = service_id & 0xFF;
|
|
160
|
+
|
|
161
|
+ data_len += NEWCAMD_HDR_LEN;
|
|
162
|
+
|
|
163
|
+ if (useMsgId) {
|
|
164
|
+ c->newcamd.msg_id++;
|
|
165
|
+ netbuf[2] = c->newcamd.msg_id >> 8;
|
|
166
|
+ netbuf[3] = c->newcamd.msg_id & 0xFF;
|
|
167
|
+ }
|
|
168
|
+
|
|
169
|
+ if ((data_len = pad_message(netbuf, data_len)) < 0) {
|
|
170
|
+ ts_LOGf("ERR | [%s] Pad_message failed.\n", c->ops.ident);
|
|
171
|
+ return 0;
|
|
172
|
+ }
|
|
173
|
+
|
|
174
|
+ if ((data = triple_des_encrypt(&c->newcamd.td_key, netbuf, data_len, netbuf)) == 0) {
|
|
175
|
+ ts_LOGf("ERR | [%s] Encrypt failed.\n", c->ops.ident);
|
|
176
|
+ return 0;
|
|
177
|
+ }
|
|
178
|
+
|
|
179
|
+ data_len += sizeof(DES_cblock);
|
|
180
|
+ netbuf[0] = (data_len - 2) >> 8;
|
|
181
|
+ netbuf[1] = (data_len - 2) & 0xFF;
|
|
182
|
+
|
|
183
|
+ return fdwrite(c->server_fd, (char *)netbuf, data_len);
|
|
184
|
+}
|
|
185
|
+
|
|
186
|
+static int newcamd_recv_msg(struct camd *c, uint8_t *data, uint8_t useMsgId) {
|
|
187
|
+ uint8_t *netbuf = c->newcamd.buf;
|
|
188
|
+
|
|
189
|
+ if (fdread(c->server_fd, (char *)netbuf, 2) != 2) {
|
|
190
|
+ ts_LOGf("ERR | [%s] Failed to read message.\n", c->ops.ident);
|
|
191
|
+ return 0;
|
|
192
|
+ }
|
|
193
|
+
|
|
194
|
+ int mlen = ((netbuf[0] << 8) | netbuf[1]) & 0xFFFF;
|
|
195
|
+ if (mlen > NEWCAMD_MSG_SIZE - 2) {
|
|
196
|
+ ts_LOGf("ERR | [%s] Buffer overflow [mlen = %d]\n", c->ops.ident, mlen);
|
|
197
|
+ return 0;
|
|
198
|
+ }
|
|
199
|
+
|
|
200
|
+ if (fdread(c->server_fd, (char *)netbuf+2, mlen) != mlen) {
|
|
201
|
+ ts_LOGf("ERR | [%s] Failed to read message.\n", c->ops.ident);
|
|
202
|
+ return 0;
|
|
203
|
+ }
|
|
204
|
+
|
|
205
|
+ mlen += 2;
|
|
206
|
+ triple_des_decrypt(&c->newcamd.td_key, netbuf, mlen);
|
|
207
|
+ mlen -= sizeof(DES_cblock);
|
|
208
|
+ if (xor_sum(netbuf + 2, mlen - 2)) {
|
|
209
|
+ ts_LOGf("ERR | [%s] Checksum error.\n", c->ops.ident);
|
|
210
|
+ return 0;
|
|
211
|
+ }
|
|
212
|
+
|
|
213
|
+ int retlen = (((netbuf[5 + NEWCAMD_HDR_LEN] << 8) | netbuf[6 + NEWCAMD_HDR_LEN]) & 0x0FFF) + 3;
|
|
214
|
+
|
|
215
|
+ if (useMsgId) {
|
|
216
|
+ uint16_t tmp = ((netbuf[2] << 8) | netbuf[3]) & 0xFFFF;
|
|
217
|
+ if (c->newcamd.msg_id != tmp) {
|
|
218
|
+ ts_LOGf("ERR | [%s] Bad msg_id %04X != %04X\n", c->ops.ident, c->newcamd.msg_id, tmp);
|
|
219
|
+ return -2;
|
|
220
|
+ }
|
|
221
|
+ }
|
|
222
|
+
|
|
223
|
+ memmove(data, netbuf + 4 + NEWCAMD_HDR_LEN, retlen);
|
|
224
|
+
|
|
225
|
+ return retlen;
|
|
226
|
+}
|
|
227
|
+
|
|
228
|
+static uint8_t newcamd_send_cmd(struct camd *c, net_msg_type_t cmd) {
|
|
229
|
+ uint8_t data[3] = { 0, 0, 0 };
|
|
230
|
+ data[0] = cmd;
|
|
231
|
+ return newcamd_send_msg(c, data, sizeof(data), 0, 0);
|
|
232
|
+}
|
|
233
|
+
|
|
234
|
+static int newcamd_recv_cmd(struct camd *c) {
|
|
235
|
+ uint8_t buffer[NEWCAMD_MSG_SIZE];
|
|
236
|
+ if (newcamd_recv_msg(c, buffer, 0) != 3)
|
|
237
|
+ return -1;
|
|
238
|
+ return buffer[0];
|
|
239
|
+}
|
|
240
|
+
|
|
241
|
+static void newcamd_init_card_data(struct camd *c, struct newcamd *nc, uint8_t *buffer) {
|
|
242
|
+ int i;
|
|
243
|
+ char msg_buffer[32];
|
|
244
|
+
|
|
245
|
+ nc->caid = (buffer[4] << 8) | buffer[5];
|
|
246
|
+
|
|
247
|
+ for(i = 0; i < 8; i++)
|
|
248
|
+ sprintf(&msg_buffer[i*2], "%02X", buffer[6 + i]);
|
|
249
|
+
|
|
250
|
+ ts_LOGf("CAM | [%s] Card info: CAID 0x%04X Admin=%s srvUA=%s\n",
|
|
251
|
+ c->ops.ident, nc->caid, (buffer[3]==1) ? "YES" : "NO", msg_buffer);
|
|
252
|
+ memcpy(nc->ua, &buffer[6], 8);
|
|
253
|
+
|
|
254
|
+ // Parse providers
|
|
255
|
+ uint8_t is_ok = 0;
|
|
256
|
+ nc->num_of_provs = buffer[14];
|
|
257
|
+ for (i = 0; i < nc->num_of_provs; i++) {
|
|
258
|
+ if (nc->prov_ident_manual == 0) {
|
|
259
|
+ memcpy(nc->provs_ident[i], &buffer[15+11*i], 3);
|
|
260
|
+ memcpy(nc->provs_id[i], &buffer[18+11*i], 8);
|
|
261
|
+ } else {
|
|
262
|
+ if (!memcmp(nc->provs_ident[0], &buffer[15+11*i], 3)) {
|
|
263
|
+ is_ok = 1;
|
|
264
|
+ memcpy(nc->provs_id[0], &buffer[18+11*i], 8);
|
|
265
|
+ } else {
|
|
266
|
+ continue;
|
|
267
|
+ }
|
|
268
|
+ }
|
|
269
|
+ uint8_t debug_idx = (nc->prov_ident_manual == 1) ? 0 : i;
|
|
270
|
+ ts_LOGf("CAM | [%s] Card info: Provider %d : %02X%02X%02X : %02X%02X%02X%02X%02X%02X%02X%02X\n",
|
|
271
|
+ c->ops.ident,
|
|
272
|
+ debug_idx,
|
|
273
|
+ nc->provs_ident[debug_idx][0], nc->provs_ident[debug_idx][1],
|
|
274
|
+ nc->provs_ident[debug_idx][2], nc->provs_id[debug_idx][0],
|
|
275
|
+ nc->provs_id[debug_idx][1], nc->provs_id[debug_idx][2],
|
|
276
|
+ nc->provs_id[debug_idx][3], nc->provs_id[debug_idx][4],
|
|
277
|
+ nc->provs_id[debug_idx][5], nc->provs_id[debug_idx][6],
|
|
278
|
+ nc->provs_id[debug_idx][7]);
|
|
279
|
+
|
|
280
|
+ if (nc->prov_ident_manual == 1 && is_ok == 1) {
|
|
281
|
+ nc->num_of_provs = 1;
|
|
282
|
+ break;
|
|
283
|
+ }
|
|
284
|
+ }
|
|
285
|
+}
|
|
286
|
+
|
|
287
|
+static int newcamd_login(struct camd *c) {
|
|
288
|
+ uint8_t *buffer = c->newcamd.buf;
|
|
289
|
+
|
|
290
|
+ c->newcamd.caid = 0;
|
|
291
|
+ c->newcamd.msg_id = 0;
|
|
292
|
+
|
|
293
|
+ uint8_t rand_data[14];
|
|
294
|
+ if (fdread(c->server_fd, (char *)rand_data, sizeof(rand_data)) != 14) {
|
|
295
|
+ ts_LOGf("ERR | [%s] Can't read protocol handshake.\n", c->ops.ident);
|
|
296
|
+ return 0;
|
|
297
|
+ }
|
|
298
|
+
|
|
299
|
+ struct crypt_data crypt_data;
|
|
300
|
+ crypt_data.initialized = 0;
|
|
301
|
+ char *crPasswd = crypt_r(c->pass, "$1$abcdefgh$", &crypt_data);
|
|
302
|
+
|
|
303
|
+ const int userLen = strlen(c->user) + 1;
|
|
304
|
+ const int passLen = strlen(crPasswd) + 1;
|
|
305
|
+
|
|
306
|
+ // prepare login message
|
|
307
|
+ buffer[0] = MSG_CLIENT_2_SERVER_LOGIN;
|
|
308
|
+ buffer[1] = 0;
|
|
309
|
+ buffer[2] = userLen + passLen;
|
|
310
|
+ memcpy(&buffer[3], c->user, userLen);
|
|
311
|
+ memcpy(&buffer[3 + userLen], crPasswd, passLen);
|
|
312
|
+
|
|
313
|
+ prepare_login_key(c, rand_data);
|
|
314
|
+ des_schedule_key(&c->newcamd.td_key);
|
|
315
|
+
|
|
316
|
+ if (!newcamd_send_msg(c, buffer, buffer[2] + 3, 0, 1) ||
|
|
317
|
+ newcamd_recv_cmd(c) != MSG_CLIENT_2_SERVER_LOGIN_ACK)
|
|
318
|
+ {
|
|
319
|
+ ts_LOGf("ERR | [%s] Login failed. Check user/pass/des-key.\n", c->ops.ident);
|
|
320
|
+ sleep(1);
|
|
321
|
+ return 0;
|
|
322
|
+ }
|
|
323
|
+
|
|
324
|
+ // Prepare session key
|
|
325
|
+ uint8_t tmpkey[14];
|
|
326
|
+ memcpy(tmpkey, c->newcamd.bin_des_key, sizeof(tmpkey));
|
|
327
|
+ int i;
|
|
328
|
+ for(i = 0; i < (passLen - 1); ++i)
|
|
329
|
+ tmpkey[i % 14] ^= crPasswd[i];
|
|
330
|
+ des_key_spread(&c->newcamd.td_key, tmpkey);
|
|
331
|
+ des_schedule_key(&c->newcamd.td_key);
|
|
332
|
+
|
|
333
|
+ if (!newcamd_send_cmd(c, MSG_CARD_DATA_REQ) || newcamd_recv_msg(c, buffer, 0) <= 0) {
|
|
334
|
+ ts_LOGf("ERR | [%s] MSG_CARD_DATA_REQ error.\n", c->ops.ident);
|
|
335
|
+ return 0;
|
|
336
|
+ }
|
|
337
|
+
|
|
338
|
+ if (buffer[0] == MSG_CARD_DATA) {
|
|
339
|
+ newcamd_init_card_data(c, &c->newcamd, buffer);
|
|
340
|
+ } else {
|
|
341
|
+ ts_LOGf("ERR | [%s] MSG_CARD_DATA response error.\n", c->ops.ident);
|
|
342
|
+ }
|
|
343
|
+
|
|
344
|
+ return 1;
|
|
345
|
+}
|
|
346
|
+
|
|
347
|
+static int newcamd_connect(struct camd *c) {
|
|
348
|
+ if (c->server_fd < 0) {
|
|
349
|
+ c->server_fd = camd_tcp_connect(c->server_addr, c->server_port);
|
|
350
|
+ if (!newcamd_login(c))
|
|
351
|
+ shutdown_fd(&c->server_fd);
|
|
352
|
+ }
|
|
353
|
+ return c->server_fd;
|
|
354
|
+}
|
|
355
|
+
|
|
356
|
+static void newcamd_disconnect(struct camd *c) {
|
|
357
|
+ shutdown_fd(&c->server_fd);
|
|
358
|
+}
|
|
359
|
+
|
|
360
|
+static int newcamd_reconnect(struct camd *c) {
|
|
361
|
+ newcamd_disconnect(c);
|
|
362
|
+ return newcamd_connect(c);
|
|
363
|
+}
|
|
364
|
+
|
|
365
|
+static int newcamd_do_ecm(struct camd *c, struct camd_msg *msg) {
|
|
366
|
+ return newcamd_send_msg(c, msg->data, msg->data_len, msg->service_id, 1);
|
|
367
|
+}
|
|
368
|
+
|
|
369
|
+static int newcamd_do_emm(struct camd *c, struct camd_msg *msg) {
|
|
370
|
+ uint8_t *buf = c->newcamd.buf;
|
|
371
|
+ int ret;
|
|
372
|
+
|
|
373
|
+ ret = newcamd_send_msg(c, msg->data, msg->data_len, msg->service_id, 1);
|
|
374
|
+ if (!ret)
|
|
375
|
+ return ret;
|
|
376
|
+
|
|
377
|
+ int data_len = newcamd_recv_msg(c, buf, 1);
|
|
378
|
+ if (data_len >= 3) {
|
|
379
|
+ if (buf[1] & 0x10)
|
|
380
|
+ return 1; // OK
|
|
381
|
+ ts_LOGf("ERR | [%s] EMM rejected by card.\n", c->ops.ident);
|
|
382
|
+ } else {
|
|
383
|
+ ts_LOGf("ERR | [%s] EMM unexpected server response (data_len=%d, buf[1]=0x%02x).\n",
|
|
384
|
+ c->ops.ident, data_len, buf[1]);
|
|
385
|
+ }
|
|
386
|
+
|
|
387
|
+ return 0; // Error
|
|
388
|
+}
|
|
389
|
+
|
|
390
|
+static int newcamd_get_cw(struct camd *c, uint16_t *ca_id, uint16_t *idx, uint8_t *cw) {
|
|
391
|
+ int ret;
|
|
392
|
+ uint8_t *buf = c->newcamd.buf;
|
|
393
|
+
|
|
394
|
+ while((ret = newcamd_recv_msg(c, buf, 1)) == -2)
|
|
395
|
+ ts_LOGf("ERR | [%s] msg_id sync error. retrying...\n", c->ops.ident);
|
|
396
|
+
|
|
397
|
+ if (ret != 19) {
|
|
398
|
+ if (ret == 3)
|
|
399
|
+ ts_LOGf("ERR | [%s] Card was not able to decode the channel.\n", c->ops.ident);
|
|
400
|
+ else
|
|
401
|
+ ts_LOGf("ERR | [%s] Unexpected CAMD server response (code %d).\n", c->ops.ident, ret);
|
|
402
|
+ return 0;
|
|
403
|
+ }
|
|
404
|
+
|
|
405
|
+ *ca_id = c->newcamd.caid;
|
|
406
|
+ *idx = 0; // FIXME
|
|
407
|
+ memcpy(cw, c->newcamd.buf + 3, 16);
|
|
408
|
+ return 1;
|
|
409
|
+}
|
|
410
|
+
|
|
411
|
+void camd_proto_newcamd(struct camd_ops *ops) {
|
|
412
|
+ strcpy(ops->ident, "newcamd");
|
|
413
|
+ ops->proto = CAMD_NEWCAMD;
|
|
414
|
+ ops->connect = newcamd_connect;
|
|
415
|
+ ops->disconnect = newcamd_disconnect;
|
|
416
|
+ ops->reconnect = newcamd_reconnect;
|
|
417
|
+ ops->do_emm = newcamd_do_emm;
|
|
418
|
+ ops->do_ecm = newcamd_do_ecm;
|
|
419
|
+ ops->get_cw = newcamd_get_cw;
|
|
420
|
+}
|