2018-03-10 12:22:10 +01:00
|
|
|
/****************************************************************************
|
2020-07-10 17:29:07 +02:00
|
|
|
* Copyright (C) 2016-2020 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;
|
|
|
|
|
2020-07-10 17:29:07 +02:00
|
|
|
import java.io.FileNotFoundException;
|
2019-04-10 18:47:22 +02:00
|
|
|
import java.io.IOException;
|
2020-07-10 17:29:07 +02:00
|
|
|
import java.security.NoSuchProviderException;
|
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
|
|
|
|
2020-07-10 17:29:07 +02:00
|
|
|
import de.mas.wiiu.jnus.entities.TMD;
|
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;
|
2020-07-10 17:29:07 +02:00
|
|
|
import de.mas.wiiu.jnus.interfaces.ContentDecryptor;
|
|
|
|
import de.mas.wiiu.jnus.interfaces.ContentEncryptor;
|
2019-04-14 14:50:01 +02:00
|
|
|
import de.mas.wiiu.jnus.interfaces.FSTDataProvider;
|
2020-07-10 17:29:07 +02:00
|
|
|
import de.mas.wiiu.jnus.interfaces.NUSDataProcessor;
|
2019-04-10 18:55:40 +02:00
|
|
|
import de.mas.wiiu.jnus.interfaces.NUSDataProvider;
|
2020-07-10 17:29:07 +02:00
|
|
|
import de.mas.wiiu.jnus.interfaces.TriFunction;
|
|
|
|
import de.mas.wiiu.jnus.utils.cryptography.NUSDecryption;
|
|
|
|
import de.mas.wiiu.jnus.utils.cryptography.NUSEncryption;
|
2017-05-17 19:20:42 +02:00
|
|
|
|
2019-04-10 18:47:22 +02:00
|
|
|
public class NUSTitleLoader {
|
|
|
|
private NUSTitleLoader() {
|
2017-05-17 19:20:42 +02:00
|
|
|
// should be empty
|
|
|
|
}
|
|
|
|
|
2020-07-10 17:29:07 +02:00
|
|
|
public static NUSTitle loadNusTitle(NUSTitleConfig config, Supplier<NUSDataProvider> dataProviderFunction,
|
|
|
|
TriFunction<NUSDataProvider, Optional<ContentDecryptor>, Optional<ContentEncryptor>, NUSDataProcessor> dataProcessorFunction)
|
|
|
|
throws IOException, ParseException {
|
2019-04-10 18:47:22 +02:00
|
|
|
NUSDataProvider dataProvider = dataProviderFunction.get();
|
2017-05-17 19:20:42 +02:00
|
|
|
|
2020-07-10 17:29:07 +02:00
|
|
|
TMD tmd = TMD.parseTMD(dataProvider.getRawTMD().orElseThrow(() -> new FileNotFoundException("No TMD data found")));
|
2017-05-17 19:20:42 +02:00
|
|
|
|
2018-12-06 15:57:36 +01:00
|
|
|
if (config.isNoDecryption()) {
|
2020-07-10 17:29:07 +02:00
|
|
|
NUSTitle result = NUSTitle.create(tmd, dataProcessorFunction.apply(dataProvider, Optional.empty(), Optional.empty()), Optional.empty(),
|
|
|
|
Optional.empty());
|
2018-12-06 15:57:36 +01:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-07-10 17:29:07 +02:00
|
|
|
Optional<Ticket> ticket = Optional.empty();
|
|
|
|
Optional<ContentDecryptor> decryption = Optional.empty();
|
|
|
|
Optional<ContentEncryptor> encryption = Optional.empty();
|
2019-05-04 12:05:34 +02:00
|
|
|
if (config.isTicketNeeded()) {
|
2020-07-10 17:29:07 +02:00
|
|
|
Ticket ticketT = config.getTicket();
|
|
|
|
if (ticketT == null) {
|
2019-05-04 12:05:34 +02:00
|
|
|
Optional<byte[]> ticketOpt = dataProvider.getRawTicket();
|
|
|
|
if (ticketOpt.isPresent()) {
|
2020-07-10 17:29:07 +02:00
|
|
|
ticketT = Ticket.parseTicket(ticketOpt.get(), config.getCommonKey());
|
2019-05-04 12:05:34 +02:00
|
|
|
}
|
2019-04-10 18:43:44 +02:00
|
|
|
}
|
2020-07-10 17:29:07 +02:00
|
|
|
if (ticketT == null) {
|
|
|
|
throw new ParseException("Failed to get ticket data", 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
ticket = Optional.of(ticketT);
|
|
|
|
|
|
|
|
decryption = Optional.of(new NUSDecryption(ticketT));
|
|
|
|
try {
|
|
|
|
encryption = Optional.of(new NUSEncryption(ticketT));
|
|
|
|
} catch (NoSuchProviderException e) {
|
|
|
|
throw new IOException(e);
|
2019-05-04 12:05:34 +02:00
|
|
|
}
|
2019-04-10 18:43:44 +02:00
|
|
|
}
|
2017-05-17 19:20:42 +02:00
|
|
|
|
2020-07-10 17:29:07 +02:00
|
|
|
NUSDataProcessor dpp = dataProcessorFunction.apply(dataProvider, decryption, encryption);
|
|
|
|
|
2019-04-14 14:50:01 +02:00
|
|
|
// If we have just content, we don't have a FST.
|
2020-07-10 17:29:07 +02:00
|
|
|
if (tmd.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.
|
2020-07-10 17:29:07 +02:00
|
|
|
NUSTitle result = NUSTitle.create(tmd, dpp, ticket, Optional.empty());
|
|
|
|
|
2019-04-14 14:50:01 +02:00
|
|
|
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.
|
2020-07-10 17:29:07 +02:00
|
|
|
Content fstContent = tmd.getContentByIndex(0);
|
2017-05-17 19:20:42 +02:00
|
|
|
|
2020-07-10 17:29:07 +02:00
|
|
|
byte[] fstBytes = dpp.readPlainDecryptedContent(fstContent, true);
|
2019-04-26 10:08:09 +02:00
|
|
|
FST fst = FST.parseFST(fstBytes);
|
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);
|
|
|
|
|
2020-07-10 17:29:07 +02:00
|
|
|
return NUSTitle.create(tmd, dpp, ticket, Optional.of(fst));
|
2017-05-17 19:20:42 +02:00
|
|
|
}
|
2020-07-10 17:29:07 +02:00
|
|
|
|
2017-05-17 19:20:42 +02:00
|
|
|
}
|