-
Notifications
You must be signed in to change notification settings - Fork 1
/
tools.c
430 lines (350 loc) · 7.25 KB
/
tools.c
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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
// Copyright 2007,2008 Segher Boessenkool <segher@kernel.crashing.org>
// Licensed under the terms of the GNU GPL, version 2
// http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
#include "tools.h"
#include <stddef.h> // to accommodate certain broken versions of openssl
#include <openssl/md5.h>
#include <openssl/aes.h>
#include <openssl/sha.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
//
// basic data types
//
u16 be16(const u8 *p)
{
return (p[0] << 8) | p[1];
}
u32 be32(const u8 *p)
{
return (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
}
u64 be64(const u8 *p)
{
return ((u64)be32(p) << 32) | be32(p + 4);
}
u64 be34(const u8 *p)
{
return 4 * (u64)be32(p);
}
void wbe16(u8 *p, u16 x)
{
p[0] = x >> 8;
p[1] = x;
}
void wbe32(u8 *p, u32 x)
{
wbe16(p, x >> 16);
wbe16(p + 2, x);
}
void wbe64(u8 *p, u64 x)
{
wbe32(p, x >> 32);
wbe32(p + 4, x);
}
//
// crypto
//
void md5(u8 *data, u32 len, u8 *hash)
{
MD5(data, len, hash);
}
void sha(u8 *data, u32 len, u8 *hash)
{
SHA1(data, len, hash);
}
void get_key(const char *name, u8 *key, u32 len)
{
char path[256];
char *home;
FILE *fp;
home = getenv("HOME");
if (home == 0)
fatal("cannot find HOME");
snprintf(path, sizeof path, "%s/.wii/%s", home, name);
fp = fopen(path, "rb");
if (fp == 0)
fatal("cannot open %s", name);
if (fread(key, len, 1, fp) != 1)
fatal("error reading %s", name);
fclose(fp);
}
void aes_cbc_dec(u8 *key, u8 *iv, u8 *in, u32 len, u8 *out)
{
AES_KEY aes_key;
AES_set_decrypt_key(key, 128, &aes_key);
AES_cbc_encrypt(in, out, len, &aes_key, iv, AES_DECRYPT);
}
void aes_cbc_enc(u8 *key, u8 *iv, u8 *in, u32 len, u8 *out)
{
AES_KEY aes_key;
AES_set_encrypt_key(key, 128, &aes_key);
AES_cbc_encrypt(in, out, len, &aes_key, iv, AES_ENCRYPT);
}
void decrypt_title_key(u8 *tik, u8 *title_key)
{
u8 common_key[16];
u8 iv[16];
get_key("common-key", common_key, 16);
memset(iv, 0, sizeof iv);
memcpy(iv, tik + 0x01dc, 8);
aes_cbc_dec(common_key, iv, tik + 0x01bf, 16, title_key);
}
static u8 root_key[0x204];
static u8 *get_root_key(void)
{
get_key("root-key", root_key, sizeof root_key);
return root_key;
}
static u32 get_sig_len(u8 *sig)
{
u32 type;
type = be32(sig);
switch (type - 0x10000) {
case 0:
return 0x240;
case 1:
return 0x140;
case 2:
return 0x80;
}
fprintf(stderr, "get_sig_len(): unhandled sig type %08x\n", type);
return 0;
}
static u32 get_sub_len(u8 *sub)
{
u32 type;
type = be32(sub + 0x40);
switch (type) {
case 0:
return 0x2c0;
case 1:
return 0x1c0;
case 2:
return 0x100;
}
fprintf(stderr, "get_sub_len(): unhandled sub type %08x\n", type);
return 0;
}
int check_ec(u8 *ng, u8 *ap, u8 *sig, u8 *sig_hash)
{
u8 ap_hash[20];
u8 *ng_Q, *ap_R, *ap_S;
u8 *ap_Q, *sig_R, *sig_S;
ng_Q = ng + 0x0108;
ap_R = ap + 0x04;
ap_S = ap + 0x22;
SHA1(ap + 0x80, 0x100, ap_hash);
ap_Q = ap + 0x0108;
sig_R = sig;
sig_S = sig + 30;
return check_ecdsa(ng_Q, ap_R, ap_S, ap_hash)
&& check_ecdsa(ap_Q, sig_R, sig_S, sig_hash);
}
static int check_rsa(u8 *h, u8 *sig, u8 *key, u32 n)
{
u8 correct[0x200];
u8 x[0x200];
static const u8 ber[16] = "\x00\x30\x21\x30\x09\x06\x05\x2b"
"\x0e\x03\x02\x1a\x05\x00\x04\x14";
//fprintf(stderr, "n = %x\n", n);
//fprintf(stderr, "key:\n");
//hexdump(key, n);
//fprintf(stderr, "sig:\n");
//hexdump(sig, n);
correct[0] = 0;
correct[1] = 1;
memset(correct + 2, 0xff, n - 38);
memcpy(correct + n - 36, ber, 16);
memcpy(correct + n - 20, h, 20);
//fprintf(stderr, "correct is:\n");
//hexdump(correct, n);
bn_exp(x, sig, key, n, key + n, 4);
//fprintf(stderr, "x is:\n");
//hexdump(x, n);
if (memcmp(correct, x, n) == 0)
return 0;
return -5;
}
static int check_hash(u8 *h, u8 *sig, u8 *key)
{
u32 type;
type = be32(sig) - 0x10000;
if (type != be32(key + 0x40))
return -6;
switch (type) {
case 1:
return check_rsa(h, sig + 4, key + 0x88, 0x100);
}
return -7;
}
static u8 *find_cert_in_chain(u8 *sub, u8 *cert, u32 cert_len)
{
char parent[64];
char *child;
u32 sig_len, sub_len;
u8 *p;
u8 *issuer;
strncpy(parent, sub, sizeof parent);
parent[sizeof parent - 1] = 0;
child = strrchr(parent, '-');
if (child)
*child++ = 0;
else {
*parent = 0;
child = sub;
}
for (p = cert; p < cert + cert_len; p += sig_len + sub_len) {
sig_len = get_sig_len(p);
if (sig_len == 0)
return 0;
issuer = p + sig_len;
sub_len = get_sub_len(issuer);
if (sub_len == 0)
return 0;
if (strcmp(parent, issuer) == 0
&& strcmp(child, issuer + 0x44) == 0)
return p;
}
return 0;
}
int check_cert_chain(u8 *data, u32 data_len, u8 *cert, u32 cert_len)
{
u8 *sig;
u8 *sub;
u32 sig_len;
u32 sub_len;
u8 h[20];
u8 *key_cert;
u8 *key;
int ret;
sig = data;
sig_len = get_sig_len(sig);
if (sig_len == 0)
return -1;
sub = data + sig_len;
sub_len = data_len - sig_len;
if (sub_len == 0)
return -2;
for (;;) {
fprintf(stderr, ">>>>>> checking sig by %s...\n", sub);
if (strcmp(sub, "Root") == 0) {
key = get_root_key();
sha(sub, sub_len, h);
if (be32(sig) != 0x10000)
return -8;
return check_rsa(h, sig + 4, key, 0x200);
}
key_cert = find_cert_in_chain(sub, cert, cert_len);
if (key_cert == 0)
return -3;
key = key_cert + get_sig_len(key_cert);
sha(sub, sub_len, h);
ret = check_hash(h, sig, key);
if (ret)
return ret;
sig = key_cert;
sig_len = get_sig_len(sig);
if (sig_len == 0)
return -4;
sub = sig + sig_len;
sub_len = get_sub_len(sub);
if (sub_len == 0)
return -5;
}
}
//
// compression
//
void do_yaz0(u8 *in, u32 in_size, u8 *out, u32 out_size)
{
u32 nout;
u8 bits;
u32 nbits;
u32 n, d, i;
bits = 0;
nbits = 0;
in += 0x10;
for (nout = 0; nout < out_size; ) {
if (nbits == 0) {
bits = *in++;
nbits = 8;
}
if ((bits & 0x80) != 0) {
*out++ = *in++;
nout++;
} else {
n = *in++;
d = *in++;
d |= (n << 8) & 0xf00;
n >>= 4;
if (n == 0)
n = 0x10 + *in++;
n += 2;
d++;
for (i = 0; i < n; i++) {
*out = *(out - d);
out++;
}
nout += n;
}
nbits--;
bits <<= 1;
};
}
//
// error handling
//
void fatal(const char *s, ...)
{
char message[256];
va_list ap;
va_start(ap, s);
vsnprintf(message, sizeof message, s, ap);
perror(message);
exit(1);
}
//
// output formatting
//
void print_bytes(u8 *x, u32 n)
{
u32 i;
for (i = 0; i < n; i++)
fprintf(stderr, "%02x", x[i]);
}
void hexdump(u8 *x, u32 n)
{
u32 i, j;
for (i = 0; i < n; i += 16) {
fprintf(stderr, "%04x:", i);
for (j = 0; j < 16 && i + j < n; j++) {
if ((j & 3) == 0)
fprintf(stderr, " ");
fprintf(stderr, "%02x", *x++);
}
fprintf(stderr, "\n");
}
}
void dump_tmd(u8 *tmd)
{
u32 i, n;
u8 *p;
printf(" issuer: %s\n", tmd + 0x140);
printf(" sys_version: %016llx\n", be64(tmd + 0x0184));
printf(" title_id: %016llx\n", be64(tmd + 0x018c));
printf(" title_type: %08x\n", be32(tmd + 0x0194));
printf(" group_id: %04x\n", be16(tmd + 0x0198));
printf("title_version: %04x\n", be16(tmd + 0x01dc));
printf(" num_contents: %04x\n", be16(tmd + 0x01de));
printf(" boot_index: %04x\n", be16(tmd + 0x01e0));
n = be16(tmd + 0x01de);
p = tmd + 0x01e4;
for (i = 0; i < n; i++) {
printf("cid %08x index %04x type %04x size %08llx\n",
be32(p), be16(p + 4), be16(p + 6), be64(p + 8));
p += 0x24;
}
}