Update incorrect headers, add System2 Header class, flavor small classes by a bit of 'printDebug' methods
continuous-integration/drone/push Build is passing Details

master
Dmitry Isaenko 2022-12-25 21:09:38 +03:00
parent 14479963d5
commit 1a4fd7d823
33 changed files with 369 additions and 39 deletions

View File

@ -4,7 +4,7 @@ name: default
steps:
- name: build-install-locally
image: maven:3-jdk-11
image: maven:3-openjdk-17
commands:
- mvn -B -DskipTests clean install
- mvn test -B

View File

@ -1,5 +1,5 @@
/*
Copyright 2018-2022 Dmitry Isaenko
Copyright 2019-2022 Dmitry Isaenko
This file is part of libKonogonka.
@ -18,26 +18,41 @@
*/
package libKonogonka.Tools.NCA.NCASectionTableBlock;
import libKonogonka.Converter;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import static libKonogonka.Converter.getLEint;
public class BucketTreeHeader {
private final static Logger log = LogManager.getLogger(BucketTreeHeader.class);
private final String magic;
private final int version;
private final int entryCount;
private final byte[] unknown;
BucketTreeHeader(byte[] rawBytes){
magic = new String(Arrays.copyOfRange(rawBytes, 0x0, 0x4), StandardCharsets.US_ASCII);
version = getLEint(rawBytes, 0x4);
entryCount = getLEint(rawBytes, 0x8);
unknown = Arrays.copyOfRange(rawBytes, 0xc, 0x10);
this.magic = new String(Arrays.copyOfRange(rawBytes, 0x0, 0x4), StandardCharsets.US_ASCII);
this.version = getLEint(rawBytes, 0x4);
this.entryCount = getLEint(rawBytes, 0x8);
this.unknown = Arrays.copyOfRange(rawBytes, 0xc, 0x10);
}
public String getMagic() {return magic;}
public int getVersion() {return version;}
public int getEntryCount() {return entryCount;}
public byte[] getUnknown() {return unknown;}
public void printDebug(){
log.debug("BucketTreeHeader\n" +
"Magic : " + magic + "\n" +
"Version : " + version + "\n" +
"EntryCount :" + entryCount + "\n" +
"Unknown :" + Converter.byteArrToHexStringAsLE(unknown) + "\n"
);
}
}

View File

@ -1,5 +1,5 @@
/*
Copyright 2018-2022 Dmitry Isaenko
Copyright 2019-2022 Dmitry Isaenko
This file is part of libKonogonka.
@ -18,11 +18,17 @@
*/
package libKonogonka.Tools.NCA.NCASectionTableBlock;
import libKonogonka.Converter;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.util.Arrays;
import static libKonogonka.Converter.getLElong;
public class CompressionInfo {
private final static Logger log = LogManager.getLogger(CompressionInfo.class);
private final long offset;
private final long size;
private final BucketTreeHeader bktr;
@ -42,4 +48,12 @@ public class CompressionInfo {
public int getBktrEntryCount() { return bktr.getEntryCount(); }
public byte[] getBktrUnknown() { return bktr.getUnknown(); }
public byte[] getUnknown() {return unknown;}
public void printDebug(){
log.debug("CompressionInfo:\n" +
"Offset : " + offset + "\n" +
"Size : " + size + "\n");
bktr.printDebug();
log.debug("\nUnknown : " + Converter.byteArrToHexStringAsLE(unknown));
}
}

View File

@ -1,5 +1,5 @@
/*
Copyright 2018-2022 Dmitry Isaenko
Copyright 2019-2022 Dmitry Isaenko
This file is part of libKonogonka.
@ -18,21 +18,36 @@
*/
package libKonogonka.Tools.NCA.NCASectionTableBlock;
import libKonogonka.Converter;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.util.Arrays;
import static libKonogonka.Converter.getLElong;
public class MetaDataHashDataInfo {
private final static Logger log = LogManager.getLogger(MetaDataHashDataInfo.class);
private final long offset;
private final long size;
private final byte[] tableHash;
MetaDataHashDataInfo(byte[] rawTable){
offset = getLElong(rawTable, 0);
size = getLElong(rawTable, 0x8);
tableHash = Arrays.copyOfRange(rawTable, 0x10, 0x20);
this.offset = getLElong(rawTable, 0);
this.size = getLElong(rawTable, 0x8);
this.tableHash = Arrays.copyOfRange(rawTable, 0x10, 0x20);
}
public long getOffset() {return offset;}
public long getSize() {return size;}
public byte[] getTableHash() {return tableHash;}
public void printDebug(){
log.debug("MetaDataHashDataInfo:\n" +
"Offset : " + offset + "\n" +
"Size : " + size + "\n" +
"Table Hash : " + Converter.byteArrToHexStringAsLE(tableHash) + "\n"
);
}
}

View File

@ -290,6 +290,10 @@ public class NcaFsHeader {
"Unknown End Padding : " + byteArrToHexStringAsLE(unknownEndPadding) + "\n" +
"################################################################################################\n"
);
sparseInfo.printDebug();
compressionInfo.printDebug();
metaDataHashDataInfo.printDebug();
}
}

View File

@ -1,5 +1,5 @@
/*
Copyright 2018-2022 Dmitry Isaenko
Copyright 2019-2022 Dmitry Isaenko
This file is part of libKonogonka.
@ -18,11 +18,17 @@
*/
package libKonogonka.Tools.NCA.NCASectionTableBlock;
import libKonogonka.Converter;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.util.Arrays;
import static libKonogonka.Converter.getLElong;
public class SparseInfo {
private final static Logger log = LogManager.getLogger(SparseInfo.class);
private final long offset;
private final long size;
private final BucketTreeHeader bktr;
@ -48,4 +54,15 @@ public class SparseInfo {
public long getPhysicalOffset() {return physicalOffset;}
public byte[] getGeneration() {return generation;}
public byte[] getUnknown() {return unknown;}
public void printDebug(){
log.debug("SparseInfo:\n" +
"Offset : " + offset + "\n" +
"Size : " + size + "\n");
bktr.printDebug();
log.debug(
"\nPhysicalOffset : " + physicalOffset + "\n" +
"Generation : " + Converter.byteArrToHexStringAsLE(generation) + "\n" +
"Unknown : " + Converter.byteArrToHexStringAsLE(unknown) + "\n");
}
}

View File

@ -1,5 +1,5 @@
/*
Copyright 2018-2022 Dmitry Isaenko
Copyright 2019-2022 Dmitry Isaenko
This file is part of libKonogonka.

View File

@ -1,5 +1,5 @@
/*
Copyright 2018-2022 Dmitry Isaenko
Copyright 2019-2022 Dmitry Isaenko
This file is part of libKonogonka.

View File

@ -1,5 +1,5 @@
/*
Copyright 2018-2022 Dmitry Isaenko
Copyright 2019-2022 Dmitry Isaenko
This file is part of libKonogonka.

View File

@ -1,5 +1,5 @@
/*
Copyright 2018-2022 Dmitry Isaenko
Copyright 2019-2022 Dmitry Isaenko
This file is part of libKonogonka.

View File

@ -1,5 +1,5 @@
/*
Copyright 2018-2022 Dmitry Isaenko
Copyright 2019-2022 Dmitry Isaenko
This file is part of libKonogonka.

View File

@ -1,5 +1,5 @@
/*
Copyright 2018-2022 Dmitry Isaenko
Copyright 2019-2022 Dmitry Isaenko
This file is part of libKonogonka.

View File

@ -1,5 +1,5 @@
/*
Copyright 2018-2022 Dmitry Isaenko
Copyright 2019-2022 Dmitry Isaenko
This file is part of libKonogonka.

View File

@ -1,5 +1,5 @@
/*
Copyright 2018-2022 Dmitry Isaenko
Copyright 2019-2022 Dmitry Isaenko
This file is part of libKonogonka.

View File

@ -1,5 +1,5 @@
/*
Copyright 2018-2022 Dmitry Isaenko
Copyright 2019-2022 Dmitry Isaenko
This file is part of libKonogonka.

View File

@ -0,0 +1,142 @@
/*
Copyright 2019-2022 Dmitry Isaenko
This file is part of libKonogonka.
libKonogonka is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
libKonogonka is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with libKonogonka. If not, see <https://www.gnu.org/licenses/>.
*/
package libKonogonka.Tools.other.System2;
import libKonogonka.Converter;
import libKonogonka.RainbowDump;
import libKonogonka.ctraes.AesCtrDecrypt;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
public class System2Header {
private final static Logger log = LogManager.getLogger(System2Header.class);
private final byte[] headerCtr;
private byte[] section0Ctr;
private byte[] section1Ctr;
private byte[] section2Ctr;
private byte[] section3Ctr;
private String magic;
private int baseOffset;
private byte[] zeroOrReserved; // Or base offset always been int8?
private byte package2Version;
private byte bootloaderVersion;
private byte[] padding;
private int section0size;
private int section1size;
private int section2size;
private int section3size;
private int section0offset;
private int section1offset;
private int section2offset;
private int section3offset;
private byte[] sha256overEncryptedSection0;
private byte[] sha256overEncryptedSection1;
private byte[] sha256overEncryptedSection2;
private byte[] sha256overEncryptedSection3;
private HashMap<String, String> package2Keys;
private byte[] decodedHeaderBytes;
public System2Header(byte[] headerBytes, HashMap<String, String> keys) throws Exception{
this.headerCtr = Arrays.copyOfRange(headerBytes, 0, 0x10);
collectKeys(keys);
decodeEncrypted(headerBytes);
buildHeader();
}
private void collectKeys(HashMap<String, String> keys){
package2Keys = new HashMap<>();
for (String key: keys.keySet()){
if (key.matches("package2_key_[0-f][0-f]"))
package2Keys.put(key, keys.get(key));
}
}
private void decodeEncrypted(byte[] headerBytes) throws Exception{
for (Map.Entry<String, String> entry: package2Keys.entrySet()){
AesCtrDecrypt decrypt = new AesCtrDecrypt(entry.getValue(), headerCtr, 0x100);
decodedHeaderBytes = decrypt.decryptNext(headerBytes);
byte[] magicBytes = Arrays.copyOfRange(decodedHeaderBytes, 0x50, 0x54);
magic = new String(magicBytes, StandardCharsets.US_ASCII);
if (magic.equals("PK21")) {
log.debug("Header key used "+entry.getKey() + " = " + entry.getValue());
return;
}
}
throw new Exception("Header hasn't been decoded. No appropriate package2_key_XX?");
}
private void buildHeader(){
section0Ctr = Arrays.copyOfRange(decodedHeaderBytes, 0x10, 0x20);
section1Ctr = Arrays.copyOfRange(decodedHeaderBytes, 0x20, 0x30);
section2Ctr = Arrays.copyOfRange(decodedHeaderBytes, 0x30, 0x40);
section3Ctr = Arrays.copyOfRange(decodedHeaderBytes, 0x40, 0x50);
baseOffset = Converter.getLEint(decodedHeaderBytes, 0x54);
zeroOrReserved = Arrays.copyOfRange(decodedHeaderBytes, 0x58, 0x5c);
package2Version = decodedHeaderBytes[0x5c];
bootloaderVersion = decodedHeaderBytes[0x5d];
padding = Arrays.copyOfRange(decodedHeaderBytes, 0x5e, 0x60);
section0size = Converter.getLEint(decodedHeaderBytes, 0x60);
section1size = Converter.getLEint(decodedHeaderBytes, 0x64);
section2size = Converter.getLEint(decodedHeaderBytes, 0x68);
section3size = Converter.getLEint(decodedHeaderBytes, 0x6c);
section0offset = Converter.getLEint(decodedHeaderBytes, 0x70);
section1offset = Converter.getLEint(decodedHeaderBytes, 0x74);
section2offset = Converter.getLEint(decodedHeaderBytes, 0x78);
section3offset = Converter.getLEint(decodedHeaderBytes, 0x7c);
sha256overEncryptedSection0 = Arrays.copyOfRange(decodedHeaderBytes, 0x80, 0xa0);
sha256overEncryptedSection1 = Arrays.copyOfRange(decodedHeaderBytes, 0xa0, 0xc0);
sha256overEncryptedSection2 = Arrays.copyOfRange(decodedHeaderBytes, 0xc0, 0xe0);
sha256overEncryptedSection3 = Arrays.copyOfRange(decodedHeaderBytes, 0xe0, 0x100);
}
public void printDebug(){
log.debug("== System2 Header ==\n" +
"Header CTR : " + Converter.byteArrToHexStringAsLE(headerCtr) + "\n" +
"Section 0 CTR : " + Converter.byteArrToHexStringAsLE(section0Ctr) + "\n" +
"Section 1 CTR : " + Converter.byteArrToHexStringAsLE(section1Ctr) + "\n" +
"Section 2 CTR : " + Converter.byteArrToHexStringAsLE(section2Ctr) + "\n" +
"Section 3 CTR : " + Converter.byteArrToHexStringAsLE(section3Ctr) + "\n" +
"Magic PK21 : " + magic + "\n" +
"Offset : " + RainbowDump.formatDecHexString(baseOffset) + "\n" +
"Zero/reserved : " + Converter.byteArrToHexStringAsLE(zeroOrReserved) + "\n" +
"Package2 version : " + RainbowDump.formatDecHexString(package2Version) + "\n" +
"Bootloader version : " + RainbowDump.formatDecHexString(bootloaderVersion) + "\n" +
"Padding : " + Converter.byteArrToHexStringAsLE(padding) + "\n" +
"Section 0 size : " + RainbowDump.formatDecHexString(section0size) + "\n" +
"Section 1 size : " + RainbowDump.formatDecHexString(section1size) + "\n" +
"Section 2 size : " + RainbowDump.formatDecHexString(section2size) + "\n" +
"Section 3 size : " + RainbowDump.formatDecHexString(section3size) + "\n" +
"Section 0 offset : " + RainbowDump.formatDecHexString(section0offset) + "\n" +
"Section 1 offset : " + RainbowDump.formatDecHexString(section1offset) + "\n" +
"Section 2 offset : " + RainbowDump.formatDecHexString(section2offset) + "\n" +
"Section 3 offset : " + RainbowDump.formatDecHexString(section3offset) + "\n" +
"SHA256 ov.enc.sec 0 : " + Converter.byteArrToHexStringAsLE(sha256overEncryptedSection0) + "\n" +
"SHA256 ov.enc.sec 1 : " + Converter.byteArrToHexStringAsLE(sha256overEncryptedSection1) + "\n" +
"SHA256 ov.enc.sec 2 : " + Converter.byteArrToHexStringAsLE(sha256overEncryptedSection2) + "\n" +
"SHA256 ov.enc.sec 3 : " + Converter.byteArrToHexStringAsLE(sha256overEncryptedSection3) + "\n"
);
}
}

View File

@ -0,0 +1,56 @@
/*
Copyright 2019-2022 Dmitry Isaenko
This file is part of libKonogonka.
libKonogonka is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
libKonogonka is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with libKonogonka. If not, see <https://www.gnu.org/licenses/>.
*/
package libKonogonka.Tools.other.System2;
import libKonogonka.KeyChainHolder;
import java.io.BufferedInputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
public class System2Provider {
private byte[] Rsa2048signature;
private System2Header header;
// ...
private final String pathToFile;
private final KeyChainHolder keyChainHolder;
public System2Provider(String pathToFile, KeyChainHolder keyChainHolder) throws Exception{
this.pathToFile = pathToFile;
this.keyChainHolder = keyChainHolder;
readHeaderCtr();
}
private void readHeaderCtr() throws Exception{
try (BufferedInputStream stream = new BufferedInputStream(Files.newInputStream(Paths.get(pathToFile)))){
if (0x100 != stream.skip(0x100))
throw new Exception("Can't skip RSA-2048 signature offset (0x100)");
byte[] headerBytes = new byte[0x100];
if (0x100 != stream.read(headerBytes))
throw new Exception("System2 header is too small");
this.header = new System2Header(headerBytes, keyChainHolder.getRawKeySet());
}
}
public System2Header getHeader() {
return header;
}
}

View File

@ -34,8 +34,8 @@ public class AesCtr {
BCinitialized = true;
}
private Cipher cipher;
private SecretKeySpec key;
private final Cipher cipher;
private final SecretKeySpec key;
public AesCtr(byte[] keyArray) throws Exception{
if ( ! BCinitialized)

View File

@ -1,5 +1,5 @@
/*
Copyright 2018-2022 Dmitry Isaenko
Copyright 2019-2022 Dmitry Isaenko
This file is part of libKonogonka.
@ -18,7 +18,6 @@
*/
package libKonogonka.ctraes;
import libKonogonka.Converter;
import libKonogonka.RainbowDump;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

View File

@ -0,0 +1,69 @@
/*
Copyright 2019-2022 Dmitry Isaenko
This file is part of libKonogonka.
libKonogonka is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
libKonogonka is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with libKonogonka. If not, see <https://www.gnu.org/licenses/>.
*/
package libKonogonka.ctraes;
/**
* Simplify decryption of the CTR
*/
public class AesCtrDecrypt {
private long realMediaOffset;
private byte[] IVarray;
private AesCtr aesCtr;
private final byte[] initialKey;
private final byte[] initialSectionCTR;
private final long initialRealMediaOffset;
public AesCtrDecrypt(String key, byte[] sectionCTR, long realMediaOffset) throws Exception{
this.initialKey = hexStrToByteArray(key);
this.initialSectionCTR = sectionCTR;
this.initialRealMediaOffset = realMediaOffset;
reset();
}
public void skipNext(){
realMediaOffset += 0x200;
}
public void skipNext(long blocksNum){
realMediaOffset += blocksNum * 0x200;
}
public byte[] decryptNext(byte[] encryptedBlock) throws Exception{
byte[] decryptedBlock = aesCtr.decrypt(encryptedBlock, IVarray);
realMediaOffset += 0x200;
return decryptedBlock;
}
public void reset() throws Exception{
realMediaOffset = initialRealMediaOffset;
aesCtr = new AesCtr(initialKey);
IVarray = initialSectionCTR;//Converter.flip();
}
private byte[] hexStrToByteArray(String s) {
int len = s.length();
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
+ Character.digit(s.charAt(i+1), 16));
}
return data;
}
}

View File

@ -19,10 +19,9 @@
package libKonogonka.ctraes;
import libKonogonka.Converter;
import libKonogonka.RainbowDump;
/**
* Simplify decryption of the CTR
* Simplify decryption of the CTR for NCA's AesCtr sections
*/
public class AesCtrDecryptSimple {

View File

@ -1,5 +1,5 @@
/*
Copyright 2018-2022 Dmitry Isaenko
Copyright 2019-2022 Dmitry Isaenko
This file is part of libKonogonka.

View File

@ -1,5 +1,5 @@
/*
Copyright 2018-2022 Dmitry Isaenko
Copyright 2019-2022 Dmitry Isaenko
This file is part of libKonogonka.

View File

@ -1,5 +1,5 @@
/*
Copyright 2018-2022 Dmitry Isaenko
Copyright 2019-2022 Dmitry Isaenko
This file is part of libKonogonka.

View File

@ -1,5 +1,5 @@
/*
Copyright 2018-2022 Dmitry Isaenko
Copyright 2019-2022 Dmitry Isaenko
This file is part of libKonogonka.

View File

@ -1,5 +1,5 @@
/*
Copyright 2018-2022 Dmitry Isaenko
Copyright 2019-2022 Dmitry Isaenko
This file is part of libKonogonka.

View File

@ -1,5 +1,5 @@
/*
Copyright 2018-2022 Dmitry Isaenko
Copyright 2019-2022 Dmitry Isaenko
This file is part of libKonogonka.

View File

@ -1,5 +1,5 @@
/*
Copyright 2018-2022 Dmitry Isaenko
Copyright 2019-2022 Dmitry Isaenko
This file is part of libKonogonka.

View File

@ -1,5 +1,5 @@
/*
Copyright 2018-2022 Dmitry Isaenko
Copyright 2019-2022 Dmitry Isaenko
This file is part of libKonogonka.

View File

@ -1,5 +1,5 @@
/*
Copyright 2018-2022 Dmitry Isaenko
Copyright 2019-2022 Dmitry Isaenko
This file is part of libKonogonka.

View File

@ -1,5 +1,5 @@
/*
Copyright 2018-2022 Dmitry Isaenko
Copyright 2019-2022 Dmitry Isaenko
This file is part of libKonogonka.

View File

@ -1,5 +1,5 @@
/*
Copyright 2018-2022 Dmitry Isaenko
Copyright 2019-2022 Dmitry Isaenko
This file is part of libKonogonka.

View File

@ -1,5 +1,5 @@
/*
Copyright 2018-2022 Dmitry Isaenko
Copyright 2019-2022 Dmitry Isaenko
This file is part of libKonogonka.