summaryrefslogtreecommitdiff
path: root/dl3.c
blob: 38b375dc84489608387707dfa1eb39ffee12df31 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>

#include "dl_pkts.h"
#include "libusb.h"

#include "pkts.h"
#include "hdcp.h"
#include <openssl/bn.h>
#include <openssl/rsa.h>
#include <openssl/evp.h>
#include <openssl/err.h>
#include <openssl/sha.h>
#include <openssl/rand.h>

#include <gcrypt.h>

struct hdcp_session_info {
	gcry_sexp_t pub_key;
};
static int VID = 0x17e9;
static int PID = 0x4301;

/* make km all 0s for now */
static unsigned char mykm[16] = { 0xcd, 0xef, 0x65, 0x33, 0x69, 0x23, 0xfa, 0x3e,
				0x60, 0xee, 0xdd, 0x5c, 0xce, 0xfb, 0x39, 0x19 };

static int create_gcrypt_pubkey(struct hdcp_session_info *info,
				const uint8_t *n,
				const uint8_t *e,
				int n_len, int e_len)
{
	char buf[65536];
	char temp[10];
	int ret;
	int i;

	buf[0] = 0;
	strcat(buf, "(public-key\n");
	strcat(buf, " (rsa\n");
	strcat(buf, "  (n #");

	for (i = 0; i < n_len; i++) {
		sprintf(temp,"%02x", n[i]);
		strcat(buf, temp);
	}
	strcat(buf, "#)\n");
	strcat(buf, "  (e #");
	for (i = 0; i < e_len; i++) {
		sprintf(temp,"%02x", e[i]);
		strcat(buf, temp);
	}
	strcat(buf, "#)\n");
	strcat(buf, " )\n");
	strcat(buf, ")\n");

	printf("pub key %s\n", buf);

	ret = gcry_sexp_sscan(&info->pub_key, NULL, buf, strlen(buf));
	if (ret != 0)
		return -1;
	return 0;
}

static int create_gcrypt_km(gcry_sexp_t *km,
			    const uint8_t *km_str,
			    int km_len)
{
	char buf[65536];
	char temp[10];
	int i, ret;
	buf[0] = 0;

	strcat(buf, "(data\n");
	strcat(buf, "    (flags oaep)\n");
	strcat(buf, "    (hash-algo sha256)\n");
	strcat(buf, "    (labal \"km\")\n");
	strcat(buf, "    (value #");
	for (i = 0; i < km_len; i++) {
		sprintf(temp,"%02x", km_str[i]);
		strcat(buf, temp);
	}
	strcat(buf, "#))\n");

	printf("km buf %s\n", buf);

	ret = gcry_sexp_sscan(km, NULL, buf, strlen(buf));
	if (ret != 0)
		return -1;
	return 0;
}

static void decode_cert(struct hdcp_session_info *info, const uint8_t *buf, int len)
{
	const uint8_t *start = buf + 2;
	BIGNUM *e, *m;
	int i, ret;
	EVP_PKEY *pRsaKey = EVP_PKEY_new();
	EVP_PKEY_CTX *ctx;
	EVP_MD_CTX *mdctx = NULL;

	RSA *rsa = RSA_new();
	printf("recv id: %02x %02x %02x %02x %02x\n",
	       start[0], start[1], start[2], start[3], start[4]);

	printf("rect pub key:\n");

	m = BN_bin2bn(ddccert_m, 384, NULL);

	e = BN_bin2bn(ddccert_e, 1, NULL);

	rsa->n = m;
	rsa->e = e;

	EVP_PKEY_assign_RSA(pRsaKey, rsa);

	for (i = 5; i < 5 + 128; i++) {
		printf("%02x", start[i]);
	}
	printf("\n");
	for (i = 133; i < 133 + 3; i++) {
		printf("%02x", start[i]);
	}
	printf("\n");
	printf("proto descrip + rsvd: \n");
	for (i = 136; i < 136 + 2; i++) {
		printf("%02x", start[i]);
	}
	printf("\nllc sig: \n");
	for (i = 138; i < 136 + 384; i++) {
		printf("%02x", start[i]);
	}
	printf("\n");

	if (info) {
	  ret = create_gcrypt_pubkey(info,
				     &start[5],
				     &start[133],
				     128, 3);
	  if (ret != 0) {
	    fprintf(stderr, "failed to create pubkey %d\n", ret);
	  }
	}

	ctx = EVP_PKEY_CTX_new(pRsaKey, NULL);
	if (EVP_PKEY_verify_init(ctx) <= 0) {
		printf("fail 1\n");
		return;
	}

	mdctx = EVP_MD_CTX_create();
	EVP_DigestVerifyInit(mdctx, NULL, EVP_sha256(), NULL, pRsaKey);

	EVP_DigestVerifyUpdate(mdctx, start, 138);
	ret = EVP_DigestVerifyFinal(mdctx, start + 138, 384);
#if 0
	if (EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING) <= 0) {
		printf("fail passing\n");
		return;
	}
#if 1
	if (EVP_PKEY_CTX_set_signature_md(ctx, EVP_sha256()) <= 0) {
		printf("fail sig\n");
		return;
	}
#endif	
	ret = EVP_PKEY_verify(ctx, start + 138, 384, start, 138);
#endif
	printf("ret is %d\n", ret);
}

static void decode_h_prime(struct hdcp_session_info *info,
			   const uint8_t *buf, int len)
{
	const uint8_t *start = buf + 1;
	int i;

	printf("h_prime: ");
	for (i = 0; i < 32; i++) {
		printf("%02x", start[i]);
	}
	printf("\n");

}

static void decode_rx(struct hdcp_session_info *info,
		      const uint8_t *buf, int len)
{
	/**/
	int msglen = buf[2] | (buf[3] << 8);
	int midlen;

	if (buf[0] != 0 || buf[1] != 0)
		return;

	if (buf[4] != 0x4 || buf[7] != 0x80)
		return;

	/* hdcp packet */
	if (buf[18] != 0x84 && buf[20] != 0x30)
		return;

	switch (buf[22]) {
	case HDCP_AKE_SEND_CERT:
		decode_cert(info, buf + 22, len - 22);
		break;
	case HDCP_AKE_SEND_RRX:
		printf("send rrx\n");
		break;
	case HDCP_AKE_SEND_H_PRIME:
		decode_h_prime(info, buf + 22, len - 22);
		break;
	case HDCP_AKE_SEND_PAIRING_INFO:
		printf("send pairing info\n");
		break;
	}

}

static int write_to_usb(libusb_device_handle *handle, int endpoint_out,
			const uint8_t *buf, int len, int *size)
{
	int i = 0, r;
  	do {
		r = libusb_bulk_transfer(handle, endpoint_out, buf, len, size, 1000);
		if (r == LIBUSB_ERROR_PIPE)
			libusb_clear_halt(handle, endpoint_out);
		i++;
	} while ((r == LIBUSB_ERROR_PIPE) && (i < 5));

	printf("tx: %d:", *size);
	for (i = 0; i < *size; i++) {
		printf("%02x ", buf[i]);
	}
	printf("\n");
	return r;
}

static int block_read_usb(libusb_device_handle *handle, int endpoint_in,
			  uint8_t *buf, int *size)
{
	int r, i;

	r = libusb_bulk_transfer(handle, endpoint_in, buf, 1024, size, 1000);
	if (r < 0) {
		printf("libusb_bulk_transfer failed: %s\n", libusb_error_name(r));
	}
	printf("rx: %d:", *size);
	for (i = 0; i < *size; i++) {
		printf("%02x ", buf[i]);
	}
	printf("\n");
	return r;
}

static int send_buffer(libusb_device_handle *handle, int endpoint_out, int endpoint_in)
{
	unsigned char buf[1024];
	int r, i = 0;
	int size;
	int len2, len;
	gcry_sexp_t pub_key, km, result;
	struct hdcp_session_info info;

	memset(&info, 0, sizeof(info));

	dl3_empty_packet(buf, &len);
	r = write_to_usb(handle, endpoint_out, buf, len, &size);
	if (r != 0)
		return r;

	dl3_msg2_hdr25(buf, &len);
	r = write_to_usb(handle, endpoint_out, buf, len, &size);
	if (r != 0)
		return r;

	memset(buf, 0, 1024);
	dl3_msg2_hdr04(buf, &len);
	len2 = len;
	dl3_packet_hdcp_ake_init(&buf[len], &len);
	len2 += len;
	len2 = ROUND_TO_4(len2);
	r = write_to_usb(handle, endpoint_out, buf, len2, &size);
	if (r != 0)
		return r;

	r = block_read_usb(handle, endpoint_in, buf, &size);
	if (r < 0)
		return r;

	decode_rx(&info, buf, size);

	r = block_read_usb(handle, endpoint_in, buf, &size);	
	if (r < 0)
		return r;

	decode_rx(&info, buf, size);

	r = create_gcrypt_km(&km, mykm, 16);
	if (r != 0)
		fprintf(stderr, "failed to create km\n");

	r = gcry_pk_encrypt(&result, km, info.pub_key);

	GCRY_SEXP list = gcry_sexp_find_token( result, "a", 0);

	gcry_mpi_t out_msg = gcry_sexp_nth_mpi(list, 1, GCRYMPI_FMT_USG);

	gcry_mpi_dump(out_msg);

	unsigned char obuf[1024] = { 0 };
	r = gcry_mpi_print(GCRYMPI_FMT_USG, (unsigned char*) &obuf,
			     sizeof(obuf), &size, out_msg);
	if (r) {
		printf("failed to stringify mpi");
	}

	dl3_packet_hdcp_ake_no_stored_km(buf, &len, obuf);
	len = ROUND_TO_4(len);
	r = write_to_usb(handle, endpoint_out, buf, len, &size);
	if (r != 0)
		return r;

	do {
		r = block_read_usb(handle, endpoint_in, buf, &size);	
		if (r < 0)
			return r;
		decode_rx(&info, buf, size);
		/* find H' */
	} while (1);
	return 0;
}

int test_device(uint16_t vid, uint16_t pid)
{
	libusb_device_handle *handle;
	libusb_device *dev;
	uint8_t bus;
	int r;
	struct libusb_config_descriptor *conf_desc;
	int nb_ifaces;
	int iface;

	handle = libusb_open_device_with_vid_pid(NULL, vid, pid);
	if (handle == NULL) {
		return -1;
	}

	dev = libusb_get_device(handle);
	bus = libusb_get_bus_number(dev);

	r = libusb_get_config_descriptor(dev, 0, &conf_desc);
	if (r < 0)
		return -1;

	nb_ifaces = conf_desc->bNumInterfaces;
	libusb_free_config_descriptor(conf_desc);
	for (iface = 0; iface < nb_ifaces; iface++)
	{
		printf("\nClaiming interface %d...\n", iface);
		r = libusb_claim_interface(handle, iface);
		if (r != LIBUSB_SUCCESS) {
			perror("   Failed.\n");
		}
	}

	printf("num interfaces %d\n", nb_ifaces);

	
				
	send_buffer(handle, 0x02, 0x84);
	printf("\n");
	for (iface = 0; iface<nb_ifaces; iface++) {
		printf("Releasing interface %d...\n", iface);
		libusb_release_interface(handle, iface);
	}



	libusb_close(handle);
	return 0;
}

int main(int argc, char** argv)
{
	int r;

	//	decode_cert(NULL, dplinkcert, 546);
#if 1
	r = libusb_init(NULL);
	if (r < 0)
		return r;
		libusb_set_debug(NULL, LIBUSB_LOG_LEVEL_DEBUG);
	test_device(VID, PID);

	libusb_exit(NULL);
#endif
}