/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

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