-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathTicket.cpp
More file actions
313 lines (274 loc) · 18.3 KB
/
Ticket.cpp
File metadata and controls
313 lines (274 loc) · 18.3 KB
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
#include <cstring>
#include <cstdlib>
#include <memory>
#include <exception>
#include <stdexcept>
#include <new>
#include <openssl/ssl.h>
#include <openssl/rsa.h>
#include <openssl/sha.h>
#include <openssl/bn.h>
#include <openssl/rand.h>
#include <openssl/evp.h>
#include "Ticket.hpp"
#include "Base64.hpp"
namespace {
static const u8 stubticket[] = {0x00, 0x01, 0x00, 0x04, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0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 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x52, 0x6F, 0x6F, 0x74, 0x2D, 0x43, 0x41, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x33, 0x2D, 0x58, 0x53, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x63, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x54, 0x69, 0x74, 0x6C, 0x65, 0x49, 0x64, 0x21, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x14, 0x00, 0x00, 0x00, 0xAC, 0x00, 0x00, 0x00, 0x14, 0x00, 0x01, 0x00, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x28, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x84, 0x00, 0x00, 0x00, 0x84, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
class StaticPubRSAKey {
private:
static const u8 exp[3];
RSA* key;
public:
StaticPubRSAKey(const u8* modulus, size_t length) {
key = RSA_new();
BIGNUM* foo = BN_bin2bn(modulus, length, NULL);
BIGNUM* bar = BN_bin2bn(exp, sizeof(exp), NULL);
if(!key || !foo || !bar) {
fprintf(stderr,
"Critical static constructor failed to initializing key.\n"
"Program will now quit.\n");
BN_clear_free(foo);
BN_clear_free(bar);
exit(1);
}
if(!RSA_set0_key(key, foo, bar, NULL)) {
fprintf(stderr,
"Critical static constructor failed to set key.\n"
"Program will now quit.\n");
BN_clear_free(foo);
BN_clear_free(bar);
exit(1);
}
}
~StaticPubRSAKey() {
RSA_free(key);
}
inline bool RawEncrypt(void* data, void* encdata) {
return (bool)RSA_public_encrypt(256, (u8*)data, (u8*)encdata, key, RSA_NO_PADDING);
}
inline bool VerifySHA256(const u8* digest, const u8* signature) {
return (bool)RSA_verify(NID_sha256, digest, SHA256_DIGEST_LENGTH, signature, RSA_size(key), key);
}
};
const u8 StaticPubRSAKey::exp[3] = {0x01, 0x00, 0x01};
static const u8 cdn_modulus[256] = {0xD2, 0x4C, 0xB2, 0xE4, 0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x2D, 0x2A, 0x4C, 0x17, 0x66, 0xF2, 0xC3, 0x39, 0x12, 0xC5, 0x8F, 0x14, 0xA8, 0x03, 0xE3, 0x6F, 0xCC, 0xDC, 0xCC, 0xDC, 0x13, 0xFD, 0x7A, 0xE7, 0x7C, 0x7A, 0x78, 0xD9, 0x97, 0xE6, 0xAC, 0xC3, 0x55, 0x57, 0xE0, 0xD3, 0xE9, 0xEB, 0x64, 0xB4, 0x3C, 0x92, 0xF4, 0xC5, 0x0D, 0x67, 0xA6, 0x02, 0xDE, 0xB3, 0x91, 0xB0, 0x66, 0x61, 0xCD, 0x32, 0x88, 0x0B, 0xD6, 0x49, 0x12, 0xAF, 0x1C, 0xBC, 0xB7, 0x16, 0x2A, 0x06, 0xF0, 0x25, 0x65, 0xD3, 0xB0, 0xEC, 0xE4, 0xFC, 0xEC, 0xDD, 0xAE, 0x8A, 0x49, 0x34, 0xDB, 0x8E, 0xE6, 0x7F, 0x30, 0x17, 0x98, 0x62, 0x21, 0x15, 0x5D, 0x13, 0x1C, 0x6C, 0x3F, 0x09, 0xAB, 0x19, 0x45, 0xC2, 0x06, 0xAC, 0x70, 0xC9, 0x42, 0xB3, 0x6F, 0x49, 0xA1, 0x18, 0x3B, 0xCD, 0x78, 0xB6, 0xE4, 0xB4, 0x7C, 0x6C, 0x5C, 0xAC, 0x0F, 0x8D, 0x62, 0xF8, 0x97, 0xC6, 0x95, 0x3D, 0xD1, 0x2F, 0x28, 0xB7, 0x0C, 0x5B, 0x7D, 0xF7, 0x51, 0x81, 0x9A, 0x98, 0x34, 0x65, 0x26, 0x25};
static StaticPubRSAKey cdnkey(cdn_modulus, sizeof(cdn_modulus));
static StaticPubRSAKey XS0000000c(XS0000000c_modulus, sizeof(XS0000000c_modulus));
static bool KeyEncrypt(u8* out, const u8* key, const u8* iv) {
struct raw_keyiv_decrypted_buffer {
u8 foo[2]; // = {0x00, 0x01}
u8 bar[221]; // all 0xFF
u8 foobar; // 0x00
u8 key[16]; // random
u8 iv[16]; // random
} keydata;
keydata.foo[0] = keydata.foobar = 0;
keydata.foo[1] = 1;
memset(keydata.bar, 0xFF, sizeof(keydata.bar));
memcpy(keydata.key, key, 16);
memcpy(keydata.iv, iv, 16);
return cdnkey.RawEncrypt(&keydata, out);
}
static bool TicketEncrypt(const u8* data, size_t length, u8* out, const u8* key, const u8* iv) {
EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
if(!ctx) return false;
bool ret = false;
do {
if(!EVP_CipherInit_ex(ctx, EVP_aes_128_cbc(), NULL, key, iv, 1)) break;
if(EVP_CIPHER_CTX_key_length(ctx) != 16 || EVP_CIPHER_CTX_iv_length(ctx) != 16) break;
EVP_CIPHER_CTX_set_padding(ctx, 0);
int foo;
if(!EVP_CipherUpdate(ctx, out, &foo, data, length) && !EVP_CipherFinal_ex(ctx, out + foo, &foo)) break;
ret = true;
} while(0);
EVP_CIPHER_CTX_free(ctx);
return ret;
}
}
#pragma pack(push,1)
struct ContentIndexHeader {
u8 Unk1[4];
u32 ContentIndexFullSize;
u32 DataHeaderRelativePosition;
u8 Unk2[4];
u8 Unk3[4];
};
struct ContentIndexDataHeader {
u32 DataRelativePosition;
u32 MaxEntryCount;
u32 SizePerEntry;
u32 TotalDataSize;
u16 DataType;
u8 Unk[2]; // or padding
};
#pragma pack(pop)
NintendoData::Ticket& NintendoData::Ticket::operator=(const Ticket& other) {
if(this == &other) return *this;
if(this->TotalSize() != other.TotalSize()) {
u8* tmp = (u8*)malloc(other.TotalSize());
if(!tmp) throw std::bad_alloc();
this->~Ticket();
this->rawticket = tmp;
this->header = (struct Header*)&this->rawticket[(uptr)other.header - (uptr)other.rawticket];
this->rightfieldcount = other.rightfieldcount;
if (other.rights)
this->rights = (struct TicketRightsField*)&this->rawticket[(uptr)other.rights - (uptr)other.rawticket];
else this->rights = nullptr;
}
memcpy((void*)this->rawticket, other.rawticket, other.TotalSize());
return *this;
}
NintendoData::Ticket& NintendoData::Ticket::operator=(Ticket&& other) {
if(this == &other) return *this;
this->~Ticket();
this->rawticket = other.rawticket;
this->header = other.header;
this->rightfieldcount = other.rightfieldcount;
this->rights = other.rights;
other.rawticket = (u8*)malloc(sizeof(stubticket));
if (!other.rawticket) throw std::bad_alloc();
memcpy(other.rawticket, stubticket, sizeof(stubticket));
other.header = (struct Header*)&other.rawticket[0x240];
other.rightfieldcount = 1;
other.rights = (struct TicketRightsField*)&other.rawticket[0x2CC];
return *this;
}
void NintendoData::Ticket::GetWrappedTicket(char*& out_b64_encticket, char*& out_b64_encticketkey) const {
u8 key[16] = {0};
u8 iv[16] = {0};
size_t ticketlen = TotalSize();
size_t ticketpaddedlen = (ticketlen + 15) & ~0xF;
std::unique_ptr<u8[]> encticket(new u8[ticketpaddedlen]);
std::unique_ptr<u8[]> enckeyiv(new u8[256]);
out_b64_encticket = NULL;
out_b64_encticketkey = NULL;
memcpy(encticket.get(), rawticket, ticketlen);
memset(&encticket.get()[ticketlen], 0, ticketpaddedlen - ticketlen);
try {
RAND_bytes(key, 16);
RAND_bytes(iv, 16);
if(!TicketEncrypt(rawticket, ticketpaddedlen, encticket.get(), key, iv))
throw std::runtime_error("Error on encryption of ticket.");
if(!KeyEncrypt(enckeyiv.get(), key, iv))
throw std::runtime_error("Error on encryption of key and iv.");
Base64::Encode(encticket.get(), ticketpaddedlen, out_b64_encticket);
Base64::Encode(enckeyiv.get(), 256, out_b64_encticketkey);
} catch(...) { //ensure clean up ...
free(out_b64_encticket);
free(out_b64_encticketkey);
out_b64_encticket = NULL;
out_b64_encticketkey = NULL;
throw; //rethrow
}
}
bool NintendoData::Ticket::VerifySign() const {
if((enum SignatureType)Endian::Be((const u32*)rawticket) != SignatureType::RSA_2048_SHA256)
throw std::runtime_error("Currently lacking support for other signature types for now.");
if(memcmp(header->Issuer, "Root-CA00000003-XS0000000c", strlen("Root-CA00000003-XS0000000c")))
throw std::runtime_error("Currently lacking support for other signature issues other than Root-CA00000003-XS0000000c");
u8 digest[SHA256_DIGEST_LENGTH];
SHA256((u8*)header, header->HeaderFullSize(), digest);
return XS0000000c.VerifySHA256(digest, &rawticket[4]);
}
bool NintendoData::Ticket::RightsToContentIndex(int index) const {
if (index < 0 || index >= 0x10000) return false;
if (rightfieldcount == 0) return index < 256;
bool hasright = false;
for (u32 i = 0; i < rightfieldcount; i++) {
u16 indexoffset = Endian::Be(&rights[i].IndexOffset);
if (index < indexoffset) break;
u16 bitpos = index - indexoffset;
if (bitpos >= 1024) continue; // not in this field
if (rights[i].RightsBitfield[bitpos / 8] & (1 << (bitpos % 8))) {
hasright = true;
break;
}
}
return hasright;
}
void NintendoData::Ticket::StripPersonalization() {
header->TicketID = 0;
header->ConsoleID = 0;
header->eShopID = 0;
}
size_t NintendoData::Ticket::GetSignSize(const void* ptr, size_t ptrlen) {
if(!ptr || ptrlen < 4) throw std::invalid_argument("Invalid Ticket pointer.");
size_t signsize;
switch((enum SignatureType)Endian::Be((const u32*)ptr)) {
case SignatureType::RSA_4096_SHA1:
case SignatureType::RSA_4096_SHA256:
signsize = 0x240u;
break;
case SignatureType::RSA_2048_SHA1:
case SignatureType::RSA_2048_SHA256:
signsize = 0x140u;
break;
case SignatureType::ECDSA_SHA1:
case SignatureType::ECDSA_SHA256:
signsize = 0x80u;
break;
default:
throw std::invalid_argument("Invalid Ticket Signature Type.");
}
return signsize;
}
size_t NintendoData::Ticket::GetTotalSize(const void* ptr, size_t ptrlen) {
size_t signsize = GetSignSize(ptr, ptrlen);
u64 minexpectedlen = signsize + MinimumNoSignSize();
if(minexpectedlen > ptrlen) throw std::invalid_argument("Ticket too small.");
const struct Header* tmp_header = (const struct Header*)&((const u8*)ptr)[signsize];
return signsize + tmp_header->HeaderFullSize();
}
NintendoData::Ticket::Ticket() {
rawticket = (u8*)malloc(sizeof(stubticket));
if (!rawticket) throw std::bad_alloc();
memcpy(rawticket, stubticket, sizeof(stubticket));
header = (struct Header*)&rawticket[0x240];
rightfieldcount = 1;
rights = (struct TicketRightsField*)&rawticket[0x2CC];
}
// if we want to get technical, tickets here can exceed > 4GiB, but that's crazy talk
NintendoData::Ticket::Ticket(const void* ptr, size_t ptrlen, bool mustbesigned) {
size_t signsize = GetSignSize(ptr, ptrlen);
u64 minexpectedlen = signsize + MinimumNoSignSize();
if(minexpectedlen > ptrlen) throw std::invalid_argument("Ticket too small.");
const struct Header* tmp_header = (const struct Header*)&((const u8*)ptr)[signsize];
minexpectedlen = signsize + tmp_header->HeaderFullSize();
if(minexpectedlen > ptrlen) throw std::invalid_argument("Ticket too small.");
const struct ContentIndexHeader* cheader = (const struct ContentIndexHeader*)&tmp_header->ContentIndex[0];
if (Endian::Be((u32*)&cheader->Unk1[0]) != 0x10014 ||
Endian::Be(cheader->ContentIndexFullSize) < 0x14 ||
Endian::Be((u32*)&cheader->Unk2[0]) != 0x10014 ||
Endian::Be((u32*)&cheader->Unk3[0]) != 0)
throw std::invalid_argument("Ticket has unsupported or invalid content index values.");
u32 cindex_size = tmp_header->ContentIndexSize();
if (Endian::Be(cheader->DataHeaderRelativePosition) < 0x14 ||
(Endian::Be(cheader->DataHeaderRelativePosition) < cindex_size && Endian::Be(cheader->DataHeaderRelativePosition) + sizeof(ContentIndexDataHeader) > tmp_header->ContentIndexSize()))
throw std::invalid_argument("Ticket has unsupported or invalid content index values.");
const struct ContentIndexDataHeader* dheader = (const struct ContentIndexDataHeader*)&tmp_header->ContentIndex[Endian::Be(cheader->DataHeaderRelativePosition)];
u32 dataposition = Endian::Be(dheader->DataRelativePosition);
u32 count = std::min<u32>((cindex_size - dataposition) / sizeof(struct TicketRightsField), Endian::Be(dheader->MaxEntryCount));
rawticket = (u8*)malloc(minexpectedlen);
if(!rawticket) throw std::runtime_error("Can't allocate memory for Ticket.");
header = (struct Header*)&rawticket[signsize];
memcpy((void*)rawticket, ptr, minexpectedlen);
try {
if(mustbesigned && !VerifySign())
throw std::invalid_argument("Ticket is not properly signed.");
} catch(...) {
free((void*)rawticket);
throw;
}
dheader = (const struct ContentIndexDataHeader*)&header->ContentIndex[Endian::Be(cheader->DataHeaderRelativePosition)];
if (count == 0 || Endian::Be(dheader->DataType) != 3) {
rightfieldcount = 0;
rights = NULL;
} else {
rightfieldcount = count;
rights = (const struct TicketRightsField*)&header->ContentIndex[dataposition];
}
}
NintendoData::Ticket::~Ticket() {
free(rawticket);
}