2018-03-10 12:22:10 +01:00
|
|
|
/****************************************************************************
|
2019-04-10 20:13:38 +02:00
|
|
|
* Copyright (C) 2016-2019 Maschell
|
2018-03-10 12:22:10 +01:00
|
|
|
*
|
|
|
|
* This program 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.
|
|
|
|
*
|
|
|
|
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
****************************************************************************/
|
2017-05-17 19:20:42 +02:00
|
|
|
package de.mas.wiiu.jnus;
|
|
|
|
|
2019-04-10 18:47:22 +02:00
|
|
|
import java.io.IOException;
|
2017-05-17 19:20:42 +02:00
|
|
|
import java.io.InputStream;
|
2019-04-10 18:47:22 +02:00
|
|
|
import java.text.ParseException;
|
|
|
|
import java.util.Optional;
|
|
|
|
import java.util.function.Supplier;
|
2017-05-17 19:20:42 +02:00
|
|
|
|
|
|
|
import de.mas.wiiu.jnus.entities.Ticket;
|
|
|
|
import de.mas.wiiu.jnus.entities.content.Content;
|
|
|
|
import de.mas.wiiu.jnus.entities.fst.FST;
|
2019-04-14 14:50:01 +02:00
|
|
|
import de.mas.wiiu.jnus.entities.fst.FSTEntry;
|
|
|
|
import de.mas.wiiu.jnus.implementations.FSTDataProviderNUSTitle;
|
|
|
|
import de.mas.wiiu.jnus.interfaces.FSTDataProvider;
|
2019-04-10 18:55:40 +02:00
|
|
|
import de.mas.wiiu.jnus.interfaces.NUSDataProvider;
|
2017-05-17 19:20:42 +02:00
|
|
|
import de.mas.wiiu.jnus.utils.StreamUtils;
|
|
|
|
import de.mas.wiiu.jnus.utils.cryptography.AESDecryption;
|
|
|
|
|
2019-04-10 18:47:22 +02:00
|
|
|
public class NUSTitleLoader {
|
|
|
|
private NUSTitleLoader() {
|
2017-05-17 19:20:42 +02:00
|
|
|
// should be empty
|
|
|
|
}
|
|
|
|
|
2019-04-10 20:13:38 +02:00
|
|
|
public static NUSTitle loadNusTitle(NUSTitleConfig config, Supplier<NUSDataProvider> dataProviderFunction) throws IOException, ParseException {
|
2019-04-10 18:47:22 +02:00
|
|
|
NUSDataProvider dataProvider = dataProviderFunction.get();
|
2017-05-17 19:20:42 +02:00
|
|
|
|
2019-04-18 11:18:32 +02:00
|
|
|
NUSTitle result = new NUSTitle(dataProvider);
|
2017-05-17 19:20:42 +02:00
|
|
|
|
2018-12-06 15:57:36 +01:00
|
|
|
if (config.isNoDecryption()) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-05-17 19:20:42 +02:00
|
|
|
Ticket ticket = config.getTicket();
|
|
|
|
if (ticket == null) {
|
2019-04-10 18:43:44 +02:00
|
|
|
Optional<byte[]> ticketOpt = dataProvider.getRawTicket();
|
2019-04-10 20:13:38 +02:00
|
|
|
if (ticketOpt.isPresent()) {
|
2019-04-10 18:43:44 +02:00
|
|
|
ticket = Ticket.parseTicket(ticketOpt.get(), config.getCommonKey());
|
|
|
|
}
|
|
|
|
}
|
2019-04-10 20:13:38 +02:00
|
|
|
if (ticket == null) {
|
|
|
|
new ParseException("Failed to get ticket data", 0);
|
2017-05-17 19:20:42 +02:00
|
|
|
}
|
2019-04-18 11:18:32 +02:00
|
|
|
result.setTicket(Optional.of(ticket));
|
2017-05-17 19:20:42 +02:00
|
|
|
|
2019-04-14 14:50:01 +02:00
|
|
|
// If we have just content, we don't have a FST.
|
2019-04-18 11:18:32 +02:00
|
|
|
if (result.getTMD().getAllContents().size() == 1) {
|
2019-04-14 14:50:01 +02:00
|
|
|
// The only way to check if the key is right, is by trying to decrypt the whole thing.
|
|
|
|
FSTDataProvider dp = new FSTDataProviderNUSTitle(result);
|
|
|
|
for (FSTEntry children : dp.getRoot().getChildren()) {
|
|
|
|
dp.readFile(children);
|
|
|
|
}
|
2019-04-18 11:18:32 +02:00
|
|
|
|
2019-04-14 14:50:01 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
// If we have more than one content, the index 0 is the FST.
|
2019-04-18 11:18:32 +02:00
|
|
|
Content fstContent = result.getTMD().getContentByIndex(0);
|
2017-05-17 19:20:42 +02:00
|
|
|
|
2019-04-26 16:42:41 +02:00
|
|
|
InputStream fstContentEncryptedStream = dataProvider.readContentAsStream(fstContent);
|
2017-05-17 19:20:42 +02:00
|
|
|
|
|
|
|
byte[] fstBytes = StreamUtils.getBytesFromStream(fstContentEncryptedStream, (int) fstContent.getEncryptedFileSize());
|
2019-04-14 14:50:01 +02:00
|
|
|
|
2017-05-17 19:20:42 +02:00
|
|
|
if (fstContent.isEncrypted()) {
|
|
|
|
AESDecryption aesDecryption = new AESDecryption(ticket.getDecryptedKey(), new byte[0x10]);
|
2019-04-10 19:59:56 +02:00
|
|
|
if (fstBytes.length % 0x10 != 0) {
|
|
|
|
throw new IOException("FST length is not align to 16");
|
|
|
|
}
|
2017-05-17 19:20:42 +02:00
|
|
|
fstBytes = aesDecryption.decrypt(fstBytes);
|
|
|
|
}
|
|
|
|
|
2019-04-26 10:08:09 +02:00
|
|
|
FST fst = FST.parseFST(fstBytes);
|
2019-04-18 11:18:32 +02:00
|
|
|
result.setFST(Optional.of(fst));
|
2017-05-17 19:20:42 +02:00
|
|
|
|
2019-04-26 10:08:09 +02:00
|
|
|
// The dataprovider may need the FST to calculate the offset of a content
|
|
|
|
// on the partition.
|
|
|
|
dataProvider.setFST(fst);
|
|
|
|
|
2017-05-17 19:20:42 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|