PageRenderTime 47ms CodeModel.GetById 17ms app.highlight 27ms RepoModel.GetById 0ms app.codeStats 1ms

/packages/libogcfpc/src/ogc/es.inc

https://github.com/slibre/freepascal
Pascal | 465 lines | 332 code | 119 blank | 14 comment | 0 complexity | 64b0981771673aa272fb0d43a211ca2c MD5 | raw file
Possible License(s): LGPL-2.0, LGPL-2.1, LGPL-3.0
  1
  2{$IFDEF OGC_INTERFACE}
  3{$ifdef HW_RVL}
  4
  5const
  6  ES_EINVAL = - $1004;
  7  ES_ENOMEM = - $100C;
  8  ES_ENOTINIT = - $1100;
  9  ES_EALIGN = - $1101;
 10  ES_SIG_RSA4096 = $10000;
 11  ES_SIG_RSA2048 = $10001;
 12  ES_SIG_ECDSA = $10002;
 13  ES_CERT_RSA4096 = 0;
 14  ES_CERT_RSA2048 = 1;
 15  ES_CERT_ECDSA = 2;
 16  ES_KEY_COMMON = 4;
 17  ES_KEY_SDCARD = 6;
 18
 19type
 20  sigtype = cuint32;
 21  sig_header = sigtype;
 22  signed_blob = sig_header;
 23  psigned_blob = ^signed_blob;
 24
 25  sha1 = array [0..19] of cuint8;
 26  psha1 = ^sha1;
 27  aeskey = array [0..15] of cuint8;
 28
 29
 30  _sig_rsa2048 = packed record
 31    type_ : sigtype;
 32    sig : array [0..255] of cuint8;
 33    fill : array [0..59] of cuint8;
 34  end;
 35  sig_rsa2048 = _sig_rsa2048;
 36
 37
 38type
 39  _sig_rsa4096 = packed record
 40    type_ : sigtype;
 41    sig : array [0..511] of cuint8;
 42    fill : array [0..59] of cuint8;
 43  end;
 44  sig_rsa4096 = _sig_rsa4096;
 45
 46
 47type
 48  _sig_ecdsa = packed record
 49    type_ : sigtype;
 50    sig : array [0..59] of cuint8;
 51    fill : array [0..63] of cuint8;
 52  end;
 53  sig_ecdsa = _sig_ecdsa;
 54
 55
 56type
 57  sig_issuer = array [0..63] of cchar;
 58
 59
 60type
 61  _tiklimit = packed record
 62    tag : cuint32;
 63    value : cuint32;
 64  end;
 65  tiklimit = _tiklimit;
 66
 67
 68type
 69  _tikview = packed record
 70    view : cuint32;
 71    ticketid : cuint64;
 72    devicetype : cuint32;
 73    titleid : cuint64;
 74    access_mask : cuint16;
 75    reserved : array [0..59] of cuint8;
 76    cidx_mask : array [0..63] of cuint8;
 77    padding : cuint16;
 78    limits : array [0..7] of tiklimit;
 79  end;
 80  tikview = _tikview;
 81  ptikview = ^_tikview;
 82
 83
 84type
 85  _tik = packed record
 86    issuer : sig_issuer;
 87    fill : array [0..62] of cuint8;  //TODO: not really fill
 88    cipher_title_key : aeskey;
 89    fill2 : cuint8;
 90    ticketid : cuint64;
 91    devicetype : cuint32;
 92    titleid : cuint64;
 93    access_mask : cuint16;
 94    reserved : array [0..59] of cuint8;
 95    cidx_mask : array [0..63] of cuint8;
 96    padding : cuint16;
 97    limits : array [0..7] of tiklimit;
 98  end;
 99  tik = _tik;
100
101
102type
103  _tmd_content = packed record
104    cid : cuint32;
105    index_ : cuint16;
106    type_ : cuint16;
107    size : cuint64;
108    hash : sha1;
109  end;
110  tmd_content = _tmd_content;
111  ptmd_content = ^tmd_content;
112
113
114  _tmd = packed record
115    issuer : sig_issuer;  //0x140
116    version : cuint8;  //0x180
117    ca_crl_version : cuint8;  //0x181
118    signer_crl_version : cuint8;  //0x182
119    fill2 : cuint8;  //0x183
120    sys_version : cuint64;  //0x184
121    title_id : cuint64;  //0x18c
122    title_type : cuint32;  //0x194
123    group_id : cuint16;  //0x198
124    zero : cuint16;  //0x19a
125    region : cuint16;  //0x19c
126    ratings : array [0..15] of cuint8;  //0x19e
127    reserved : array [0..11] of cuint8;  //0x1ae
128    ipc_mask : array [0..11] of cuint8;
129    reserved2 : array [0..17] of cuint8;
130    access_rights : cuint32;
131    title_version : cuint16;
132    num_contents : cuint16;
133    boot_index : cuint16;
134    fill3 : cuint16;
135    // content records follow
136    // C99 flexible array
137    contents: Ptmd_content;
138  end;
139  tmd = _tmd;
140  ptmd = ^tmd;
141
142
143type
144  _tmd_view_content = packed record
145    cid : cuint32;
146    index_ : cuint16;
147    type_ : cuint16;
148    size : cuint64;
149  end;
150  tmd_view_content = _tmd_view_content;
151  ptmd_view_content = ^tmd_view_content;
152
153
154  _tmdview = packed record
155    version : cuint8;  // 0x0000;
156    filler : array [0..2] of cuint8;
157    sys_version : cuint64;  //0x0004
158    title_id : cuint64;  // 0x00c
159    title_type : cuint32;  //0x0014
160    group_id : cuint16;  //0x0018
161    reserved : array [0..61] of cuint8;  //0x001a this is the same reserved 0x3e bytes from the tmd
162    title_version : cuint16;  //0x0058
163    num_contents : cuint16;  //0x005a
164    contents : ptmd_view_content;
165  end;
166  tmd_view = _tmdview;
167  ptmd_view = ^tmd_view;
168
169
170type
171  _cert_header = packed record
172    issuer : sig_issuer;
173    cert_type : cuint32;
174    cert_name : array [0..63] of cchar;
175    cert_id : cuint32;  //???
176  end;
177  cert_header = _cert_header;
178  pcert_header = ^_cert_header;
179
180
181type
182  _cert_rsa2048 = packed record
183    issuer : sig_issuer;
184    cert_type : cuint32;
185    cert_name : array [0..63] of cchar;
186    cert_id : cuint32;
187    modulus : array [0..255] of cuint8;
188    exponent : cuint32;
189    pad : array [0..51] of cuint8;
190  end;
191  cert_rsa2048 = _cert_rsa2048;
192
193
194type
195  _cert_rsa4096 = packed record
196    issuer : sig_issuer;
197    cert_type : cuint32;
198    cert_name : array [0..63] of cchar;
199    cert_id : cuint32;
200    modulus : array [0..511] of cuint8;
201    exponent : cuint32;
202    pad : array [0..51] of cuint8;
203  end;
204  cert_rsa4096 = _cert_rsa4096;
205
206
207type
208  _cert_ecdsa = packed record
209    issuer : sig_issuer;
210    cert_type : cuint32;
211    cert_name : array [0..63] of cchar;
212    cert_id : cuint32;  // ng key id
213    r : array [0..29] of cuint8;
214    s : array [0..29] of cuint8;
215    pad : array [0..59] of cuint8;
216  end;
217  cert_ecdsa = _cert_ecdsa;
218
219
220function TMD_SIZE(x: ptmd): cint; inline;
221
222// backwards compatibility
223function TMD_CONTENTS(x: ptmd): tmd_content; inline;
224
225//TODO: add ECC stuff
226function IS_VALID_SIGNATURE(x: psigned_blob): boolean; inline;
227
228function SIGNATURE_SIZE(x: psigned_blob): cint; inline;
229
230function SIGNATURE_SIG(x: psigned_blob): cint; inline;
231
232function IS_VALID_CERT(x: cert_header): boolean; inline;
233
234function CERTIFICATE_SIZE(x: cert_header): cint; inline;
235
236function SIGNATURE_PAYLOAD(x: psigned_blob): pointer; inline;
237
238function SIGNED_TMD_SIZE(x: psigned_blob): cint; inline;
239
240function SIGNED_TIK_SIZE(x: psigned_blob): cint; inline;
241
242function SIGNED_CERT_SIZE(x: psigned_blob): cint; inline;
243
244function STD_SIGNED_TIK_SIZE(): cint; inline;
245
246const
247  MAX_NUM_TMD_CONTENTS = 512;
248
249function MAX_TMD_SIZE(): cint; inline;
250
251function MAX_SIGNED_TMD_SIZE(): cint; inline;
252
253function __ES_Init: cint32; cdecl; external;
254
255function __ES_Close: cint32; cdecl; external;
256
257function __ES_Reset: cint32; cdecl; external;
258
259function ES_GetTitleID(titleID: pcuint64): cint32; cdecl; external;
260
261function ES_SetUID(uid: cuint64): cint32; cdecl; external;
262
263function ES_GetDataDir(titleID: cuint64; filepath: pcchar): cint32; cdecl; external;
264
265function ES_GetNumTicketViews(titleID: cuint64; cnt: pcuint32): cint32; cdecl; external;
266
267function ES_GetTicketViews(titleID: cuint64; views: Ptikview; cnt: cuint32)
268  : cint32; cdecl; external;
269
270function ES_GetNumOwnedTitles(cnt: pcuint32): cint32; cdecl; external;
271
272function ES_GetOwnedTitles(titles: pcuint64; cnt: cuint32): cint32; cdecl; external;
273
274function ES_GetNumTitles(cnt: pcuint32): cint32; cdecl; external;
275
276function ES_GetTitles(titles: pcuint64; cnt: cuint32): cint32; cdecl; external;
277
278function ES_GetNumStoredTMDContents(stmd: Psigned_blob; tmd_size: cuint32;
279  cnt: pcuint32): cint32; cdecl; external;
280
281function ES_GetStoredTMDContents(stmd: Psigned_blob; tmd_size: cuint32;
282  contents: pcuint32; cnt: cuint32): cint32; cdecl; external;
283
284function ES_GetStoredTMDSize(titleID: cuint64; size: pcuint32): cint32; cdecl; external;
285
286function ES_GetStoredTMD(titleID: cuint64; stmd: Psigned_blob; size: cuint32)
287  : cint32; cdecl; external;
288
289function ES_GetTitleContentsCount(titleID: cuint64; num: pcuint32): cint32; cdecl; external;
290
291function ES_GetTitleContents(titleID: cuint64; data: pcuint8; size: cuint32)
292  : cint32; cdecl; external;
293
294function ES_GetTMDViewSize(titleID: cuint64; size: pcuint32): cint32; cdecl; external;
295
296function ES_GetTMDView(titleID: cuint64; data: pcuint8; size: cuint32): cint32; cdecl; external;
297
298function ES_GetNumSharedContents(cnt: pcuint32): cint32; cdecl; external;
299
300function ES_GetSharedContents(contents: Psha1; cnt: cuint32): cint32; cdecl; external;
301
302function ES_LaunchTitle(titleID: cuint64; view: Ptikview): cint32; cdecl; external;
303
304function ES_LaunchTitleBackground(titleID: cuint64; view: Ptikview): cint32; cdecl; external;
305
306function ES_Identify(certificates: Psigned_blob; certificates_size: cuint32;
307  tmd: Psigned_blob; tmd_size: cuint32; ticket: Psigned_blob;
308  ticket_size: cuint32; keyid: pcuint32): cint32; cdecl; external;
309
310function ES_AddTicket(tik: Psigned_blob; tik_size: cuint32;
311  certificates: Psigned_blob; certificates_size: cuint32; crl: Psigned_blob;
312  crl_size: cuint32): cint32; cdecl; external;
313
314function ES_DeleteTicket(view: Ptikview): cint32; cdecl; external;
315
316function ES_AddTitleTMD(tmd: Psigned_blob; tmd_size: cuint32): cint32; cdecl; external;
317
318function ES_AddTitleStart(tmd: Psigned_blob; tmd_size: cuint32;
319  certificatess: Psigned_blob; certificatess_size: cuint32; crl: Psigned_blob;
320  crl_size: cuint32): cint32; cdecl; external;
321
322function ES_AddContentStart(titleID: cuint64; cid: cuint32): cint32; cdecl; external;
323
324function ES_AddContentData(cid: cint32; data: pcuint8; data_size: cuint32)
325  : cint32; cdecl; external;
326
327function ES_AddContentFinish(cid: cuint32): cint32; cdecl; external;
328
329function ES_AddTitleFinish: cint32; cdecl; external;
330
331function ES_AddTitleCancel: cint32; cdecl; external;
332
333function ES_ImportBoot(tik: Psigned_blob; tik_size: cuint32;
334  tik_certs: Psigned_blob; tik_certs_size: cuint32; tmd: Psigned_blob;
335  tmd_size: cuint32; tmd_certs: Psigned_blob; tmd_certs_size: cuint32;
336  content: pcuint8; content_size: cuint32): cint32; cdecl; external;
337
338function ES_OpenContent(index_: cuint16): cint32; cdecl; external;
339
340function ES_OpenTitleContent(titleID: cuint64; views: Ptikview; index_: cuint16)
341  : cint32; cdecl; external;
342
343function ES_ReadContent(cfd: cint32; data: pcuint8; data_size: cuint32): cint32; cdecl; external;
344
345function ES_SeekContent(cfd, where, whence: cint32): cint32; cdecl; external;
346
347function ES_CloseContent(cfd: cint32): cint32; cdecl; external;
348
349function ES_DeleteTitle(titleID: cuint64): cint32; cdecl; external;
350
351function ES_DeleteTitleContent(titleID: cuint64): cint32; cdecl; external;
352
353function ES_Encrypt(keynum: cuint32; iv, source: pcuint8; size: cuint32;
354  dest: pcuint8): cint32; cdecl; external;
355
356function ES_Decrypt(keynum: cuint32; iv, source: pcuint8; size: cuint32;
357  dest: pcuint8): cint32; cdecl; external;
358
359function ES_Sign(source: pcuint8; size: cuint32; sig, certs: pcuint8): cint32; cdecl; external;
360
361function ES_GetDeviceCert(outbuf: pcuint8): cint32; cdecl; external;
362
363function ES_GetDeviceID(device_id: pcuint32): cint32; cdecl; external;
364
365function ES_GetBoot2Version(version: pcuint32): cint32; cdecl; external;
366
367function ES_NextCert(certs: Psigned_blob): Psigned_blob; cdecl; external;
368
369{$endif HW_RVL}
370
371{$ENDIF}
372
373{$IFDEF OGC_IMPLEMENTATION}
374{$ifdef HW_RVL}
375
376function TMD_SIZE(x: ptmd): cint; inline;
377begin
378  TMD_SIZE := (x^.num_contents) * sizeof(tmd_content) + sizeof(tmd);
379end;
380  // backwards compatibility
381
382function TMD_CONTENTS(x: ptmd): tmd_content; inline;
383begin
384  TMD_CONTENTS := x^.contents^;
385end;
386
387//TODO: add ECC stuff
388
389function IS_VALID_SIGNATURE(x: psigned_blob): boolean; inline;
390begin
391  IS_VALID_SIGNATURE :=	(x^ = ES_SIG_RSA2048) or (x^ = ES_SIG_RSA4096) or (x^ = ES_SIG_ECDSA);
392end;
393
394function SIGNATURE_SIZE(x: psigned_blob): cint; inline;
395begin
396	case x^ of
397    ES_SIG_RSA2048: SIGNATURE_SIZE := sizeof(sig_rsa2048);
398    ES_SIG_RSA4096: SIGNATURE_SIZE := sizeof(sig_rsa4096);
399    ES_SIG_ECDSA: SIGNATURE_SIZE := sizeof(sig_ecdsa);
400  else
401    SIGNATURE_SIZE := 0;
402  end;
403end;
404
405function SIGNATURE_SIG(x: psigned_blob): cint; inline;
406begin
407  SIGNATURE_SIG := cuint8(x^) + 4;
408end;
409
410function IS_VALID_CERT(x: cert_header): boolean; inline;
411begin
412	IS_VALID_CERT := (x.cert_type = ES_CERT_RSA2048) or
413                   (x.cert_type = ES_CERT_RSA4096) or
414	                 (x.cert_type = ES_CERT_ECDSA);
415end;
416
417function CERTIFICATE_SIZE(x: cert_header): cint; inline;
418begin
419  case x.cert_type of
420	  ES_CERT_RSA2048: CERTIFICATE_SIZE := sizeof(cert_rsa2048);
421	  ES_CERT_RSA4096: CERTIFICATE_SIZE := sizeof(cert_rsa4096);
422	  ES_CERT_ECDSA: CERTIFICATE_SIZE := sizeof(cert_ecdsa);
423  else
424    CERTIFICATE_SIZE := 0;
425  end;
426end;
427
428function SIGNATURE_PAYLOAD(x: psigned_blob): pointer; inline;
429begin
430  SIGNATURE_PAYLOAD := pointer(x^ + SIGNATURE_SIZE(x));
431end;
432
433function SIGNED_TMD_SIZE(x: psigned_blob): cint; inline;
434begin
435  SIGNED_TMD_SIZE := TMD_SIZE(ptmd(SIGNATURE_PAYLOAD(x))) + SIGNATURE_SIZE(x);
436end;
437
438function SIGNED_TIK_SIZE(x: psigned_blob): cint; inline;
439begin
440  SIGNED_TIK_SIZE := sizeof(tik) + SIGNATURE_SIZE(x);
441end;
442
443function SIGNED_CERT_SIZE(x: psigned_blob): cint; inline;
444begin
445  SIGNED_CERT_SIZE := CERTIFICATE_SIZE(pcert_header(SIGNATURE_PAYLOAD(x))^) + SIGNATURE_SIZE(x);
446end;
447
448function STD_SIGNED_TIK_SIZE(): cint; inline;
449begin
450  STD_SIGNED_TIK_SIZE := sizeof(tik) + sizeof(sig_rsa2048);
451end;
452
453function MAX_TMD_SIZE(): cint; inline;
454begin
455  MAX_TMD_SIZE := sizeof(tmd) + MAX_NUM_TMD_CONTENTS * sizeof(tmd_content);
456end;
457
458function MAX_SIGNED_TMD_SIZE(): cint; inline;
459begin
460  MAX_SIGNED_TMD_SIZE := MAX_TMD_SIZE + sizeof(sig_rsa2048);
461end;
462
463
464{$endif HW_RVL}
465{$ENDIF}