Editing SELF - SPRX
Jump to navigation
Jump to search
The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then publish the changes below to finish undoing the edit.
Latest revision | Your text | ||
Line 1: | Line 1: | ||
SELF stands for Signed Executable and Linkable Format. | {{Wikify}} | ||
SELF stands for Signed Executable and Linkable Format. It is the format used by the executables on the PS3 and PS Vita. | |||
[[File:Self.png|thumb|alt=A screenshot of f0f's presentation at CCC2010.]] | [[File:Self.png|thumb|alt=A screenshot of f0f's presentation at CCC2010.]] | ||
Line 6: | Line 8: | ||
= Introduction = | = Introduction = | ||
It consists of a CF header with an extended header followed by the encapsulated ELF file. ELF sections can be compressed using gzip. SELFs are encrypted and signed, unlike fSELFs. ELF sections are encrypted using AES CTR (maybe more algos?) and signed using ECDSA + HMAC-SHA1. SELF file has a specific header called Extended header where it stores all the parameters for this program. | |||
*Extended Header | *Extended Header | ||
It consists of information regarding the structure and offsets of the SELF. The first part is in plaintext until you reach Encryption Root Header. | |||
*Encryption Root Header | *Encryption Root Header | ||
Encryption Root Header is encrypted under | Encryption Root Header is itself encrypted under AES256CBC. This part contains key and ivec to further decrypt the header using AES128CTR. | ||
* | *Metadata | ||
Certification Header, Certification | The Certification Header, Segment Certification Headers, Section Hashes, Capabilities and Signature are all encrypted under this AES128CTR layer and are decrypted with the key above. | ||
*Certification Header | *Certification Header | ||
Certification Header contains the info required to authenticate the header and the structure of the metadata. The signature is ECDSA of the SHA1 hash of the SELF file starting at offset 0x0 and ending at 0x0+signatureInputLength. | |||
*Data | *Data Sections | ||
The data sections might be encrypted using AES128CTR and/or compressed. HMAC-SHA1 is used to ensure that they have not been modified. | |||
---- | |||
= Cryptography = | = Cryptography = | ||
Line 29: | Line 33: | ||
Basically here are the steps being involved by the loaders: | Basically here are the steps being involved by the loaders: | ||
Loaders all have a static key and iv called respectively <abbr title="ERK - 256bit Encryption Round Key">erk</abbr> and <abbr title="RIV - 128bit Reset Initialization Vector">riv</abbr>. Those are keys for the first decryption step which | Loaders all have a static key and iv called respectively <abbr title="ERK - 256bit Encryption Round Key">erk</abbr> and <abbr title="RIV - 128bit Reset Initialization Vector">riv</abbr>. Those are keys for the first decryption step which are used to decrypt the very first 0x40 bytes of the SELF's metadata using <abbr title="AES256CBC - Advanced Encryption Standard - 256 bit - Cipher-block chaining">AES256CBC</abbr>. | ||
Then the result is used as key and iv to decrypt the rest of the | Then the result is used as key and iv to decrypt the rest of the metadata using <abbr title="AESCTR - Advanced Encryption Standard - Counter Mode">AESCTR</abbr>. Finally the decrypted metadata contains the key and iv for each data sections which are still decrypted through AES128CTR. This security model is based on the fact that the first 0x40 bytes of the SELF's metadata once decrypted by the static AES256CBC key in the loader should never be the same from one binary to the other. The same goes for any other value used as an AES128CTR key or iv. | ||
Loaders are also involved with inflating the binaries using zlib. | Loaders are also involved with inflating the binaries using zlib. | ||
The SELF authenticity is based on other independent steps, <abbr title="HMAC-SHA1 - Hash-based Message Authentication Code - Secure Hash Algorithm 1">HMAC-SHA1</abbr> | The SELF authenticity is based on other independent steps, <abbr title="HMAC-SHA1 - Hash-based Message Authentication Code - Secure Hash Algorithm 1">HMAC-SHA1</abbr> of the data sections and <abbr title="ECDSA - Elliptic Curve Digital Signature Algorithm">ECDSA</abbr> for the actual signature in the header. | ||
== Short references == | == Short references == | ||
Line 45: | Line 49: | ||
More indepth Online course about encryption in generic (also AES/ECDSA): [https://engineering.purdue.edu/kak/compsec/Lectures.html Lecture Notes on Computer and Network Security by Avinash Kak] | More indepth Online course about encryption in generic (also AES/ECDSA): [https://engineering.purdue.edu/kak/compsec/Lectures.html Lecture Notes on Computer and Network Security by Avinash Kak] | ||
= File Format = | = File Format = | ||
Notes: | Notes: | ||
* Warning: PS3 uses big endian, | * Warning: PS3 uses big endian, PSVita uses little endian. | ||
* Encapsulated ELF header fields are useless (only the EI_CLASS EI_DATA and EI_VERSION fields are checked). | * Encapsulated ELF header fields are useless (only the EI_CLASS EI_DATA and EI_VERSION fields are checked). | ||
== PS3 early | == PS3 early type 3 SELFs == | ||
<pre> | <pre> | ||
SDK 0.60: No sce version | SDK 0.60: No sce version and digest | ||
SDK 0.8X: No sce version, with 0x30 | SDK 0.8X: No sce version, with 0x30 digest | ||
SDK 0.9X: With sce version and 0x40 | SDK 0.9X: With sce version and 0x40 digest (scetool produces this type) | ||
</pre> | </pre> | ||
== Extended Header == | == Extended Header == | ||
Extended Header offsets are relative to | Extended Header offsets are relative to Certified File start. | ||
=== Struct === | === Struct === | ||
Line 108: | Line 76: | ||
uint64_t phdr_offset; | uint64_t phdr_offset; | ||
uint64_t shdr_offset; | uint64_t shdr_offset; | ||
uint64_t | uint64_t sections_hdr_offset; | ||
uint64_t version_hdr_offset; | uint64_t version_hdr_offset; | ||
uint64_t supplemental_hdr_offset; | uint64_t supplemental_hdr_offset; | ||
Line 121: | Line 89: | ||
! field !! offset !! type !! notes | ! field !! offset !! type !! notes | ||
|- | |- | ||
| Extended Header version || 0x0 || u64 || 3 for PS3, 4 for | | Extended Header version || 0x0 || u64 || 3 for PS3, 4 for PSVita | ||
|- | |- | ||
| Program Identification Header offset || 0x8 || u64 || Offset to Program Identification Header. | | Program Identification Header offset || 0x8 || u64 || Offset to Program Identification Header. | ||
|- | |- | ||
| ELF Header offset || 0x10 || u64 || Offset to ELF | | ELF Header offset || 0x10 || u64 || Offset to ELF header. | ||
|- | |- | ||
| Program Header offset || 0x18 || u64 || Offset to ELF Program Header. | | Program Header offset || 0x18 || u64 || Offset to ELF Program Header. | ||
Line 131: | Line 99: | ||
| Section Header offset || 0x20 || u64 || Offset to ELF Section Header. | | Section Header offset || 0x20 || u64 || Offset to ELF Section Header. | ||
|- | |- | ||
| | | Program Extended Header offset || 0x28 || u64 || Offset to Program Extended Header. | ||
|- | |- | ||
| Version Header offset || 0x30 || u64 || Offset to Version Header. | | Version Header offset || 0x30 || u64 || Offset to Version Header. | ||
Line 144: | Line 112: | ||
=== Comments === | === Comments === | ||
The real ELF data is located after the | The real ELF data is located after the extended header (see header size). It is encrypted, unless attribute is 0x8000. unfself works by cutting the SCE header from the (fake)SELF and if needed decompressing sections. | ||
== Program Identification Header == | == Program Identification Header == | ||
Temp name was App Info. Official name is Program Identification Header. | |||
=== Struct === | === Struct === | ||
Line 155: | Line 123: | ||
typedef struct { | typedef struct { | ||
uint64_t program_authority_id; | uint64_t program_authority_id; | ||
uint32_t | uint32_t program_vendor_id; | ||
uint32_t program_type; | uint32_t program_type; | ||
uint64_t program_sceversion; | uint64_t program_sceversion; | ||
Line 167: | Line 135: | ||
! field !! offset !! type !! notes !! example | ! field !! offset !! type !! notes !! example | ||
|- | |- | ||
| program_authority_id || 0x00 || u64 || See [[ | | program_authority_id || 0x00 || u64 || See [[Authority_ID|authority_id]] || ex: 21 00 00 10 1C CA 01 30 | ||
|- | |- | ||
| | | program_vendor_id || 0x08 || u32 || See [[Vendor_ID|vendor_id]] || ex: 00 00 00 00 | ||
|- | |- | ||
| program_type || 0x0C || u32 || See [[Program Type]] || ex: 08 00 00 00 | | program_type || 0x0C || u32 || See [[SELF_Types|Program Type]] || ex: 08 00 00 00 | ||
|- | |- | ||
| program_sceversion || 0x10 || u64 || ?SDK version or app version? [[Revision versus Version|version]] ex: 01.02 is translated by make_fself.exe to 02 01 00 00 || ex: 0x0000036000000000 (3.600.011), 0x0000009450000000 (0.945.040) | | program_sceversion || 0x10 || u64 || ?SDK version or app version? [[Revision versus Version|version]] ex: 01.02 is translated by make_fself.exe to 02 01 00 00 || ex: 0x0000036000000000 (3.600.011), 0x0000009450000000 (0.945.040) | ||
Line 211: | Line 179: | ||
[http://www.openwatcom.com/ftp/devel/docs/elf-64-gen.pdf ELF-64 Object File Format] | [http://www.openwatcom.com/ftp/devel/docs/elf-64-gen.pdf ELF-64 Object File Format] | ||
==== | ==== PSVita ==== | ||
<source lang="C"> | <source lang="C"> | ||
Line 251: | Line 219: | ||
| e_version || 0x14 || 4 || Must be 0x1. | | e_version || 0x14 || 4 || Must be 0x1. | ||
|- | |- | ||
| e_entry || 0x18 || 4 || Address to jump to in order to start program (warning | | e_entry || 0x18 || 4 || Address to jump to in order to start program (warning not always set) | ||
|- | |- | ||
| e_phoff || 0x1C || 4 || Boundary checked, but unused (already given by SELF header). ?and for fSELF? | | e_phoff || 0x1C || 4 || Boundary checked, but unused (already given by SELF header). ?and for fSELF? | ||
Line 272: | Line 240: | ||
|} | |} | ||
See also [https://wiki.henkaku.xyz/vita/images/a/a2/Vita_SDK_specifications.pdf | See also specifications: | ||
[https://wiki.henkaku.xyz/vita/images/a/a2/Vita_SDK_specifications.pdf yifanlu specs] | |||
=== SCE specific ELF types (e_type) === | === SCE specific ELF types (e_type) === | ||
Line 278: | Line 247: | ||
<source lang="C"> | <source lang="C"> | ||
/* SCE-specific definitions for e_type: */ | /* SCE-specific definitions for e_type: */ | ||
#define ET_SCE_EXEC 0xFE00 /* SCE Executable | #define ET_SCE_EXEC 0xFE00 /* SCE Executable */ | ||
#define ET_SCE_RELEXEC 0xFE04 /* SCE Relocatable Executable | #define ET_SCE_RELEXEC 0xFE04 /* SCE Relocatable Executable */ | ||
#define ET_SCE_STUBLIB 0xFE0C /* SCE SDK Stubs */ | #define ET_SCE_STUBLIB 0xFE0C /* SCE SDK Stubs */ | ||
#define ET_SCE_DYNAMIC 0xFE18 /* ? */ | #define ET_SCE_DYNAMIC 0xFE18 /* ? */ | ||
#define ET_SCE_IOPRELEXEC 0xFF80 /* SCE IOP Relocatable Executable */ | #define ET_SCE_IOPRELEXEC 0xFF80 /* SCE IOP Relocatable Executable */ | ||
Line 289: | Line 257: | ||
#define ET_SCE_PSPRELEXEC 0xFFA0 /* SCE PSP Relocatable Executable */ | #define ET_SCE_PSPRELEXEC 0xFFA0 /* SCE PSP Relocatable Executable */ | ||
#define ET_SCE_PPURELEXEC 0xFFA4 /* SCE PPU Relocatable Executable */ | #define ET_SCE_PPURELEXEC 0xFFA4 /* SCE PPU Relocatable Executable */ | ||
#define ET_SCE_ARMRELEXEC 0xFFA5 /* ?SCE ARM Relocatable Executable ( | #define ET_SCE_ARMRELEXEC 0xFFA5 /* ?SCE ARM Relocatable Executable (PSVita FW <=0.931) */ | ||
#define ET_SCE_PSPOVERLAY 0xFFA8 /* ? */ | #define ET_SCE_PSPOVERLAY 0xFFA8 /* ? */ | ||
</source> | </source> | ||
Line 299: | Line 267: | ||
</source> | </source> | ||
== ELF Program | == ELF Program Header == | ||
=== Struct === | === Struct === | ||
Line 319: | Line 287: | ||
</source> | </source> | ||
==== | ==== PSVita ==== | ||
<source lang="C"> | <source lang="C"> | ||
Line 335: | Line 303: | ||
</source> | </source> | ||
See Spec here: [http://www.sco.com/developers/gabi/latest/ch5.pheader.html ELF Program | See Spec here: [http://www.sco.com/developers/gabi/latest/ch5.pheader.html ELF Program Headers] | ||
=== | === Processor specific segment types (p_type) === | ||
<source lang=" | <source lang="C"> | ||
*PT_SCE_IOPMOD = 0x70000080 | |||
*PT_SCE_EEMOD = 0x70000090 | |||
*PT_SCE_PSPREL = 0x700000A0 | |||
*PT_SCE_PPURELA = 0x700000A4 | |||
*PT_SCE_SEGSYM = 0x700000A8 | |||
</source> | </source> | ||
=== | === Processor specific segment flags (p_flags) === | ||
<source lang="C"> | <source lang="C"> | ||
Line 423: | Line 345: | ||
</source> | </source> | ||
=== | === Comments === | ||
== | Processor specific section types (sh_type): | ||
*SHT_SCE_IOPMOD = 0x70000080 | |||
*SHT_SCE_EEMOD = 0x70000090 | |||
*SHT_SCE_PSPREL = 0x700000a0 | |||
*SHT_SCE_PPURELA = 0x700000a4 | |||
== Program Extended Header == | |||
A table which maps each phdr entry to the actual offset/size within the encrypted CF. Indeed, because sections can be compressed, they might not match the values listed within the ELF phdr/shdr. | |||
There is one of these entries for each ELF phdr ( | There is one of these entries (Section/Segment Information) for each ELF phdr (Program Header) entry in the ELF file so that the console knows where to decrypt the data from (because it might also be compressed). | ||
=== Struct === | === Struct === | ||
Line 451: | Line 368: | ||
uint32_t unknown; | uint32_t unknown; | ||
uint64_t encryption; | uint64_t encryption; | ||
} __attribute__((packed)) | } __attribute__((packed)) program_ext_header; | ||
</source> | </source> | ||
Line 463: | Line 380: | ||
| Size || 0x08 || u64 || Size of data | | Size || 0x08 || u64 || Size of data | ||
|- | |- | ||
| | | Compression || 0x10 || u32 || 1 = uncompressed, 2 = compressed | ||
|- | |- | ||
| Unknown || 0x14 || u32 || Always 0, as far as I know. | | Unknown || 0x14 || u32 || Always 0, as far as I know. | ||
|- | |- | ||
| | | Encryption || 0x18 || u64 || 1 = encrypted, 2 = unencrypted | ||
|} | |} | ||
== Version Header == | == Version Header == | ||
Line 490: | Line 397: | ||
uint32_t subheader_type; // 1 - sceversion | uint32_t subheader_type; // 1 - sceversion | ||
uint32_t present; // 0 = false, 1 = true | uint32_t present; // 0 = false, 1 = true | ||
uint32_t size; // | uint32_t size; // ex: 0 | ||
uint32_t unknown4; | uint32_t unknown4; | ||
} __attribute__((packed)) version_header; | } __attribute__((packed)) version_header; | ||
Line 513: | Line 420: | ||
=== Comments === | === Comments === | ||
== Supplemental Header | == Supplemental Header == | ||
Temp name was Control Information. Official name is Supplemental Header. | |||
=== Struct === | === Struct === | ||
<source lang="C"> | <source lang="C"> | ||
typedef struct { | typedef struct { | ||
uint32_t type; // 1=PS3 | uint32_t type; // 1=PS3 control flags; 2=PS3 ELF digest info; 3=PS3 NPDRM info, 4=PSVita ELF digest info; 5=PSVita NPDRM info; 6=PSVita boot param info; 7=PSVita shared secret info | ||
uint32_t size; | uint32_t size; | ||
uint64_t next; // 1 if another Supplemental Header element follows else 0 | uint64_t next; // 1 if another Supplemental Header element follows else 0 | ||
Line 544: | Line 435: | ||
// type 1, 0x30 bytes | // type 1, 0x30 bytes | ||
struct { // 0x20 bytes of data | struct { // 0x20 bytes of data | ||
uint32_t ctrl_flag1; // [[Control Flags|ctrl_flag]] 0x80000000(all?); 0x40000000(root); 0x20000000(dbg); 0x00000000(normal?) | |||
} | uint32_t unknown2; | ||
uint32_t unknown3; | |||
uint32_t unknown4; | |||
uint32_t unknown5; | |||
uint32_t unknown6; | |||
uint32_t unknown7; // ex: 0;8;9;0xC | |||
uint32_t unknown8; // ex: 0;1;2;4 | |||
} PS3_control_flags; | |||
// type 2, 0x40 bytes | // type 2, 0x40 bytes | ||
struct { // 0x30 bytes of data | struct { // 0x30 bytes of data | ||
uint8_t constant[0x14]; // same for every PS3 | uint8_t constant[0x14]; // same for every PSVita/PS3 SELF, hardcoded in make_fself.exe: 627CB1808AB938E32C8C091708726A579E2586E4 | ||
uint8_t elf_digest[0x14]; // SHA-1. Hash F2C552BF716ED24759CBE8A0A9A6DB9965F3811C is | uint8_t elf_digest[0x14]; // on PSVita: SHA-256 of source ELF file, on PS3: SHA-1. Hash F2C552BF716ED24759CBE8A0A9A6DB9965F3811C is blackisted by appldr | ||
uint64_t required_system_version; // filled on Sony | uint64_t required_system_version; // filled on Sony auth server, contains decimal PS3_SYSTEM_VER value from PARAM.SFO | ||
} | } PS3_elf_digest_40; | ||
// type 2, 0x30 bytes | // type 2, 0x30 bytes | ||
Line 558: | Line 456: | ||
uint8_t constant_or_elf_digest[0x14]; | uint8_t constant_or_elf_digest[0x14]; | ||
uint8_t padding[0xC]; | uint8_t padding[0xC]; | ||
} | } PS3_elf_digest_30; | ||
// type 3, 0x90 bytes | // type 3, 0x90 bytes | ||
struct { // 0x80 bytes of data | struct { // 0x80 bytes of data | ||
uint32_t magic; // 4E 50 44 00 ("NPD.") | |||
} | uint32_t license_version; | ||
uint32_t drm_type; // [[License Types|license_type]] | |||
uint32_t app_type; // [[App Types|app_type]] | |||
uint8_t content_id[0x30]; | |||
uint8_t digest[0x10]; // sha-1 hash of debug self/sprx created with make_fself_npdrm | |||
uint8_t inv_digest[0x10]; // hash_cid_fname | |||
uint8_t xor_digest[0x10]; // hash_cid | |||
uint8_t padding[0x10]; | |||
} PS3_npdrm_info; | |||
// type 4, 0x50 bytes | // type 4, 0x50 bytes | ||
struct { // 0x40 bytes of data | struct { // 0x40 bytes of data | ||
uint8_t constant[0x14]; // same for every PS3 | uint8_t constant[0x14]; // same for every PSVita/PS3 SELF, hardcoded in make_fself.exe: 627CB1808AB938E32C8C091708726A579E2586E4 | ||
uint8_t elf_digest[0x20]; // SHA-256 of source ELF file | uint8_t elf_digest[0x20]; // on PSVita: SHA-256 of source ELF file, on PS3: SHA-1 | ||
uint8_t padding[8]; | uint8_t padding[8]; | ||
uint32_t min_required_fw; // ex: 0x363 for 3.63 | uint32_t min_required_fw; // ex: 0x363 for 3.63 | ||
} | } PSVita_elf_digest_info; | ||
// type 5, 0x110 bytes | // type 5, 0x110 bytes | ||
struct { // | struct { // 0x80 bytes of data | ||
uint32_t magic; // 7F 44 52 4D (".DRM") | uint32_t magic; // 7F 44 52 4D (".DRM") | ||
uint32_t finalized_flag; // ex: 80 00 00 01 | uint32_t finalized_flag; // ex: 80 00 00 01 | ||
uint32_t drm_type; // [[ | uint32_t drm_type; // [[License Types|license_type]] ex: 2 local, 0xD free with license | ||
uint32_t padding; | uint32_t padding; | ||
uint8_t content_id[0x30]; | uint8_t content_id[0x30]; | ||
uint8_t digest[0x10]; // ?sha-1 hash of debug | uint8_t digest[0x10]; // ?sha-1 hash of debug self/sprx created using make_fself_npdrm? | ||
uint8_t padding_78[0x78]; | uint8_t padding_78[0x78]; | ||
uint8_t hash_signature[0x38]; // unknown hash/signature | |||
} | } PSVita_npdrm_info; | ||
// type 6, 0x110 bytes | // type 6, 0x110 bytes | ||
struct { // 0x100 bytes of data | struct { // 0x100 bytes of data | ||
uint8_t boot_param[ | uint32_t is_used; // 0=false, 1=true | ||
} | uint8_t boot_param[0x9C]; // ex: starting with 02 00 00 00 | ||
} PSVita_boot_param_info; | |||
// type 7, 0x50 bytes | // type 7, 0x50 bytes | ||
struct { // 0x40 bytes of data | struct { // 0x40 bytes of data | ||
uint8_t shared_secret_0[0x10]; // ex: 0x7E7FD126A7B9614940607EE1BF9DDF5E or full of zeroes | |||
} | uint8_t shared_secret_1[0x10]; // ex: full of zeroes | ||
uint8_t shared_secret_2[0x10]; // ex: full of zeroes | |||
uint8_t shared_secret_3[0x10]; // ex: full of zeroes | |||
} PSVita_shared_secret_info; | |||
}; | }; | ||
} __attribute__((packed)) supplemental_header; | } __attribute__((packed)) supplemental_header; | ||
Line 602: | Line 512: | ||
=== Comments === | === Comments === | ||
* | There are 3 checksums in ps3_npdrm_info: | ||
* | *digest is the sha1 checksum of the entire SELF file. | ||
*inv_digest is the inverse of digest. | |||
*xor_digest is digest XORed with 0xAAAAAA..AAAAAB. | |||
The PSJailbreak payload ignores the actual checksums, but checks that the 3rd checksum is the 2nd checksum XORed with 0xAAAAAA..AAAAAB. | |||
Notes: | |||
*self_control_flags is also known as plaintext_capability. | |||
*loader uses supplemental_header_table to handle the data: | |||
<source lang="C"> | <source lang="C"> | ||
typedef struct { | typedef struct { | ||
uint256_t control_flags; /* self_control_flags */ | |||
uint8[0x14] elf_digest; /* sha1 hash of the ELF file */ | uint8[0x14] elf_digest; /* sha1 hash of the ELF file */ | ||
uint32_t unknown_0; /* seems to be padding */ | uint32_t unknown_0; /* seems to be padding */ | ||
uint64_t | uint64_t PS3_SYSTEM_VER; /* required_system_vesion, decimal format */ | ||
} | } SUPPLEMENTAL_HEADER_TABLE; | ||
</source> | |||
== Encryption Root Header == | |||
Temp name was Metadata Information. Official name is encryption_root_header. | |||
This is not present in fSELF. | |||
=== Struct === | |||
<source lang="C"> | |||
typedef struct { | |||
uint8_t key[16]; | |||
uint8_t key_pad[16]; | |||
uint8_t iv[16]; | |||
uint8_t iv_pad[16]; | |||
} __attribute__((packed)) encryption_root_header; | |||
</source> | |||
=== Comments === | |||
Notes: | |||
*The key and ivec fields are encrypted using AES256CBC. | |||
== Certification Header == | |||
Temp name was Metadata Header. Official name is certification_header. | |||
This is only present if the Encryption Root Header is present. | |||
The Certification Header is located after the Encryption Root Header in the SELF file. It is decrypted using AES128CTR with the key and ivec entries from the Encryption Root Header. | |||
=== Struct === | |||
<source lang="C"> | |||
typedef struct { | |||
uint64_t signatureInputLength; | |||
uint32_t unknown02; // Might be signature algorithm. It always = 1 (ECDSA) | |||
uint32_t sectionCount; | |||
uint32_t keyCount; | |||
uint32_t optHeaderSize; | |||
uint64_t unknown06; | |||
} __attribute__((packed)) certification_header; | |||
</source> | |||
=== Comments === | |||
Notes: | |||
*The signature input length is the number of bytes which are used to generate the SHA-1 which is used to generate the ECDSA signature. The length should be eveything from the beginning until the signature itself. The decrypted version of the input data is used. | |||
== Segment Certification Header == | |||
Temp name was Metadata Section Headers. Official name is segment_certification_header. | |||
This is only present if the Certification Header is present. | |||
The Segment Certification Headers are located after the Certification Header in the SELF file. | |||
=== Struct === | |||
<source lang="C"> | |||
typedef struct { | |||
uint64_t data_offset; | |||
uint64_t data_size; | |||
uint32_t type; // 1 = shdr, 2 = phdr, 3 = sceversion | |||
uint32_t program_idx; // 0,1,2,3,etc for phdr, always 3 for shdrs, sceversion shdr number for sceversion | |||
uint32_t hash_algorithm; // 2 = sha1_hmac, 3 = sha1 | |||
uint32_t hash_idx; | |||
uint32_t enc_algorithm; // 1 = plain, 2 = aes128cbccfb, 3 = aes128ctr | |||
uint32_t key_idx; | |||
uint32_t iv_idx; | |||
uint32_t compressed; // 1 = no, 2 = yes | |||
} __attribute__((packed)) segment_certification_header; | |||
</source> | |||
=== Comments === | |||
Notes: | |||
*The number of sections is indicated by the sectionCount entry in the Certification Header. | |||
*They are decrypted using AES128CTR with the key and ivec entries from the Encryption Root Header. | |||
*Section data is decrypted using AES128CTR with the key and ivec from the metadata keys specified by keyIndex and ivecIndex. | |||
*Section data will also need to be uncompressed using zlib. | |||
*The data_offset of the Segment Certification Header matches in general the Segment Extended Header offset. | |||
== Section Hash == | |||
Temp name was Metadata Keys. | |||
*The Section Hashes are located after the Segment Certification Headers in the SELF file. | |||
=== Struct === | |||
<source lang="C"> | |||
typedef struct { | |||
uint8_t sha1[20]; | |||
uint8_t padding[12]; | |||
uint8_t hmac_key[64]; | |||
} __attribute__((packed)) SECTION_HASH; | |||
</source> | |||
=== Comments === | |||
Notes: | |||
*The number of keys is indicated by the keyCount entry in the Certification Header. | |||
*They are decrypted using AES128CTR with the key and ivec entries from the Encryption Root Header. | |||
*If the sha1Index points to a key, then key[sha1Index] and key[sha1Index+1] form the 160-bit hash. key[sha1Index+2] to key[key[sha1Index+6] form the 512-bit key for the HMAC-SHA1. The HMAC-SHA1 is calculated on the decrypted data and before the decompression. | |||
== Optional Header Table == | |||
Temp name was Signature Info, Capabilities Info. Official name is optional_header_table. | |||
=== Struct === | |||
<source lang="C"> | |||
typedef struct { | |||
uint32_t type; // 1=capability_header, 2=individual_seed_header, 3=attribute_header | |||
uint32_t size; | |||
uint64_t next; // 1 if another optional_header structure follows else 0 | |||
union { | |||
// type 1 | |||
struct { // 0x20 bytes of data | |||
uint8_t capability[0x20]; | |||
} capability_header; | |||
// type 2 | |||
struct { // 0x100 bytes of data | |||
uint8_t individual_seed[0x100]; | |||
} individual_seed_header; | |||
// type 3 | |||
struct { // 0x20 bytes of data | |||
uint8_t attribute[0x20]; | |||
} attribute_header; | |||
}; | |||
} __attribute__((packed)) optional_header; | |||
</source> | </source> | ||
= | === Comments === | ||
* | * Type 1 contains capability flags also known as encrypted_capability. See [[Capability Flags|capabilities]]. | ||
= | == Signature == | ||
The signature is located after the Signature Information in the SELF file. | |||
It is even present if the Signature Information is not present. | |||
=== | === Struct === | ||
<source lang="C"> | |||
typedef struct { | |||
uint8_t r[21]; | |||
uint8_t s[21]; | |||
uint8_t padding[6]; | |||
} __attribute__((packed)) SIGNATURE; | |||
</source> | |||
=== | === Comments === | ||
Notes: | |||
*It is decrypted using AES128CTR with the key and ivec entries from the Encryption Root Header. | |||
== Segment Extended Header == | |||
Temp name was SELF Section Info. Official name is segment_ext_header. | |||
=== | === Struct === | ||
<source lang="C"> | |||
typedef struct { | |||
void *data; | |||
uint64_t size; | |||
uint64_t offset; | |||
} segment_ext_header; | |||
</source> | |||
= Extracting an ELF = | |||
=== ELF Header === | |||
Elf64_Ehdr elfHeader; | |||
fseek ( selfFile, fix64 ( selfHeader.elfHeaderOffset ), SEEK_SET ); | |||
fread ( &elfHeader, sizeof ( Elf64_Ehdr ), 1, selfFile ); | |||
fseek ( elfFile, 0, SEEK_SET ); | |||
fwrite ( &elfHeader, sizeof ( Elf64_Ehdr ), 1, elfFile ); | |||
=== Section Headers === | |||
Elf64_Shdr elfSectionHeaders[100]; | |||
fseek ( selfFile, fix64 ( selfHeader.elfSectionHeadersOffset ), SEEK_SET ); | |||
fread ( elfSectionHeaders, sizeof ( Elf64_Shdr ), fix16 ( elfHeader.e_shnum ), selfFile ); | |||
fseek ( elfFile, fix64 ( elfHeader.e_shoff ), SEEK_SET ); | |||
fwrite ( elfSectionHeaders, sizeof ( Elf64_Shdr ), fix16 ( elfHeader.e_shnum ), elfFile ); | |||
=== Section Data === | |||
Notes: | |||
*Unknown, manually copying the data over works for now. | |||
*There should be a section data offset somewhere. | |||
=== Program Headers === | |||
Elf64_Phdr elfProgramHeaders[100]; | |||
fseek ( selfFile, fix64 ( selfHeader.elfProgramHeadersOffset ), SEEK_SET ); | |||
fread ( elfProgramHeaders, sizeof ( Elf64_Phdr ), fix16 ( elfHeader.e_phnum ), selfFile ); | |||
fseek ( elfFile, fix64 ( elfHeader.e_phoff ), SEEK_SET ); | |||
fwrite ( elfProgramHeaders, sizeof ( Elf64_Phdr ), fix16 ( elfHeader.e_phnum ), elfFile ); | |||
=== | === Program Data === | ||
Notes: | |||
*Load the Encryption Root Header and decrypt the key and ivec entries using AES256CBC using erk and riv. | |||
*Load the Certification Header and decrypt it using AES128CTR with the key and ivec entries from the Encryption Root Header. | |||
*Load sectionCount Segment Certification Headers and decrypt them using AES128CTR with the key and ivec entries from the Encryption Root Header. | |||
*Load keyCount metadata keys and decrypt them using AES128CTR with the key and ivec entries from the Encryption Root Header. | |||
*For each metadata section: | |||
**In the SELF file, fseek to dataOffset and read in dataSize bytes. | |||
**Decrypt the data using AES128CTR with the key and ivec from the metadata keys specified by keyIndex and ivecIndex in the Segment Certification Header. | |||
**Uncompress the data using zlib. | |||
**Write it to the ELF file as the program section specified by program_idx in the Segment Certification Header. | |||
=== | === Capabilities Flags === | ||
----------------------- | |||
appldr | |||
0x17 = 0x78 | |||
xsetting | |||
0x17 = 0x3B | |||
0x1B = 0x01 | |||
0x1D = 0x02 | |||
ps3swu | |||
0x17 = 0x7B | |||
0x1B = 0x01 | |||
0x1D = 0x11 | |||
0x1E = 0x60 | |||
lv2 | |||
0x17 = 0x7B | |||
0x1B = 0x01 | |||
lv1 | |||
0x17 = 0x7B | |||
0x1B = 0x01 | |||
libfs | |||
0x17 = 0x7B | |||
0x1B = 0x01 | |||
icolaunch | |||
0x17 = 0x3B | |||
0x1B = 0x01 | |||
0x1D = 0x04 | |||
hddcopy | |||
0x17 = 0x7B | |||
0x1B = 0x01 | |||
0x1D = 0x08 | |||
flowers | |||
0x17 = 0x3B | |||
0x1B = 0x01 | |||
0x1E = 0x20 | |||
fdm_spu | |||
0x17 = 0x38 | |||
emu_drm | |||
0x17 = 0x3B | |||
0x1D = 0x02 | |||
bdj | |||
0x0F = 0x01 //qa-bdp type1 | |||
0x17 = 0x27 | |||
0x1D = 0x02 | |||
swagner | |||
0x0F = 0x02 //qa-bdp type2 | |||
0x17 = 0x3F | |||
0x1D = 0x02 | |||
--------------------- | |||
0x0C = 0x00000001 / 0x00000002 // qa_bdp_type_flags<br> | |||
0x14 = 0x00000038 / 0x0000003B / 0x00000078 / 0x0000007B / 0x00000027<br> | |||
0x18 = 0x00000001 <br> | |||
0x1C = 0x00002000 / 0x00020000 / 0x00040000 / 0x00080000 / 0x00116000<br> | |||
0x14: | |||
#define CAP_FLAG_REFTOOL 0x08 // DEH | |||
#define CAP_FLAG_DEBUG 0x10 // DEX | |||
#define CAP_FLAG_RETAIL 0x20 // CEX | |||
#define CAP_FLAG_SYSDBG 0x40 // ARCADE | |||
Some more cap flags: | |||
https://web.archive.org/web/20161126102609/http://pastie.org/3090973 | |||
and https://web.archive.org/web/20161126102716/http://pastie.org/3090976 (appldr 356 white(?)list) | |||
<abbr title="ECDSA - Elliptic Curve Digital Signature Algorithm">ECDSA | |||
{{File Formats}}<noinclude> | {{File Formats}}<noinclude>[[Category:Main]]</noinclude> | ||
[[Category:Main]] | |||
</noinclude> |