JNUSLib/src/main/java/de/mas/wiiu/jnus/NUSTitleLoader.java

112 lines
4.6 KiB
Java
Raw Normal View History

/****************************************************************************
2020-07-10 17:29:07 +02:00
* Copyright (C) 2016-2020 Maschell
*
* 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/>.
****************************************************************************/
package de.mas.wiiu.jnus;
2020-07-10 17:29:07 +02:00
import java.io.FileNotFoundException;
import java.io.IOException;
2020-07-10 17:29:07 +02:00
import java.security.NoSuchProviderException;
import java.text.ParseException;
import java.util.Optional;
import java.util.function.Supplier;
2020-07-10 17:29:07 +02:00
import de.mas.wiiu.jnus.entities.TMD;
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;
public class NUSTitleLoader {
private NUSTitleLoader() {
// 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 {
NUSDataProvider dataProvider = dataProviderFunction.get();
2020-07-10 17:29:07 +02:00
TMD tmd = TMD.parseTMD(dataProvider.getRawTMD().orElseThrow(() -> new FileNotFoundException("No TMD data found")));
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());
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
}
}
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
}
}
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-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);
2020-07-10 17:29:07 +02:00
byte[] fstBytes = dpp.readPlainDecryptedContent(fstContent, true);
FST fst = FST.parseFST(fstBytes);
// 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));
}
2020-07-10 17:29:07 +02:00
}