2018-03-10 12:22:10 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Copyright (C) 2016-2018 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/>.
|
|
|
|
****************************************************************************/
|
2017-05-17 19:20:42 +02:00
|
|
|
package de.mas.wiiu.jnus.implementations;
|
2016-12-12 21:01:12 +01:00
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
2018-12-06 16:04:35 +01:00
|
|
|
import java.util.Arrays;
|
2016-12-12 21:01:12 +01:00
|
|
|
|
2017-05-17 19:20:42 +02:00
|
|
|
import de.mas.wiiu.jnus.NUSTitle;
|
|
|
|
import de.mas.wiiu.jnus.Settings;
|
|
|
|
import de.mas.wiiu.jnus.entities.content.Content;
|
|
|
|
import de.mas.wiiu.jnus.utils.FileUtils;
|
2018-12-06 16:04:35 +01:00
|
|
|
import de.mas.wiiu.jnus.utils.HashUtil;
|
2017-05-17 19:20:42 +02:00
|
|
|
import de.mas.wiiu.jnus.utils.Utils;
|
2016-12-12 21:01:12 +01:00
|
|
|
import lombok.Getter;
|
2017-05-17 19:20:42 +02:00
|
|
|
import lombok.NonNull;
|
2016-12-12 21:01:12 +01:00
|
|
|
import lombok.extern.java.Log;
|
|
|
|
|
|
|
|
@Log
|
|
|
|
/**
|
2018-03-10 12:22:10 +01:00
|
|
|
* Service Methods for loading NUS/Content data from different sources
|
2017-05-17 19:20:42 +02:00
|
|
|
*
|
2016-12-12 21:01:12 +01:00
|
|
|
* @author Maschell
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public abstract class NUSDataProvider {
|
2017-05-17 19:20:42 +02:00
|
|
|
@Getter private final NUSTitle NUSTitle;
|
|
|
|
|
|
|
|
public NUSDataProvider(NUSTitle title) {
|
|
|
|
this.NUSTitle = title;
|
|
|
|
}
|
|
|
|
|
2016-12-12 21:01:12 +01:00
|
|
|
/**
|
2018-03-10 12:22:10 +01:00
|
|
|
* Saves the given content encrypted with his .h3 file in the given directory. The Target directory will be created if it's missing. If the content is not
|
|
|
|
* hashed, no .h3 will be saved
|
2017-05-17 19:20:42 +02:00
|
|
|
*
|
|
|
|
* @param content
|
|
|
|
* Content that should be saved
|
|
|
|
* @param outputFolder
|
|
|
|
* Target directory where the files will be stored in.
|
2016-12-12 21:01:12 +01:00
|
|
|
* @throws IOException
|
|
|
|
*/
|
2017-05-17 19:20:42 +02:00
|
|
|
public void saveEncryptedContentWithH3Hash(@NonNull Content content, @NonNull String outputFolder) throws IOException {
|
2016-12-12 21:01:12 +01:00
|
|
|
saveContentH3Hash(content, outputFolder);
|
|
|
|
saveEncryptedContent(content, outputFolder);
|
|
|
|
}
|
2017-05-17 19:20:42 +02:00
|
|
|
|
2016-12-12 21:01:12 +01:00
|
|
|
/**
|
2018-03-10 12:22:10 +01:00
|
|
|
* Saves the .h3 file of the given content into the given directory. The Target directory will be created if it's missing. If the content is not hashed, no
|
|
|
|
* .h3 will be saved
|
2017-05-17 19:20:42 +02:00
|
|
|
*
|
|
|
|
* @param content
|
|
|
|
* The content of which the h3 hashes should be saved
|
2016-12-12 21:01:12 +01:00
|
|
|
* @param outputFolder
|
2018-12-06 16:04:35 +01:00
|
|
|
* @return
|
2016-12-12 21:01:12 +01:00
|
|
|
* @throws IOException
|
|
|
|
*/
|
2017-05-17 19:20:42 +02:00
|
|
|
public void saveContentH3Hash(@NonNull Content content, @NonNull String outputFolder) throws IOException {
|
|
|
|
if (!content.isHashed()) {
|
2016-12-12 21:01:12 +01:00
|
|
|
return;
|
|
|
|
}
|
2017-05-17 19:20:42 +02:00
|
|
|
String h3Filename = String.format("%08X%s", content.getID(), Settings.H3_EXTENTION);
|
2016-12-12 21:01:12 +01:00
|
|
|
File output = new File(outputFolder + File.separator + h3Filename);
|
2018-12-06 16:04:35 +01:00
|
|
|
|
|
|
|
if (output.exists()) {
|
|
|
|
if (Arrays.equals(content.getSHA2Hash(), HashUtil.hashSHA1(output))) {
|
|
|
|
log.info(h3Filename + " already exists");
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
if (Arrays.equals(content.getSHA2Hash(), Arrays.copyOf(HashUtil.hashSHA256(output), 20))) { // 0005000c1f941200 used sha256 instead of SHA1
|
|
|
|
log.info(h3Filename + " already exists");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
log.warning(h3Filename + " already exists but hash is differrent than expected.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
byte[] hash = getContentH3Hash(content);
|
|
|
|
if (hash == null || hash.length == 0) {
|
2016-12-12 21:01:12 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-06 16:04:35 +01:00
|
|
|
log.warning("Saving " + h3Filename + " ");
|
2017-05-17 19:20:42 +02:00
|
|
|
|
2016-12-12 21:01:12 +01:00
|
|
|
FileUtils.saveByteArrayToFile(output, hash);
|
2018-12-06 16:04:35 +01:00
|
|
|
|
2016-12-12 21:01:12 +01:00
|
|
|
}
|
2017-05-17 19:20:42 +02:00
|
|
|
|
2016-12-12 21:01:12 +01:00
|
|
|
/**
|
2018-03-10 12:22:10 +01:00
|
|
|
* Saves the given content encrypted in the given directory. The Target directory will be created if it's missing. If the content is not encrypted at all,
|
|
|
|
* it will be just saved anyway.
|
2017-05-17 19:20:42 +02:00
|
|
|
*
|
|
|
|
* @param content
|
|
|
|
* Content that should be saved
|
|
|
|
* @param outputFolder
|
|
|
|
* Target directory where the files will be stored in.
|
2016-12-12 21:01:12 +01:00
|
|
|
* @throws IOException
|
|
|
|
*/
|
2017-05-17 19:20:42 +02:00
|
|
|
public void saveEncryptedContent(@NonNull Content content, @NonNull String outputFolder) throws IOException {
|
2016-12-12 21:01:12 +01:00
|
|
|
Utils.createDir(outputFolder);
|
|
|
|
InputStream inputStream = getInputStreamFromContent(content, 0);
|
2017-05-17 19:20:42 +02:00
|
|
|
if (inputStream == null) {
|
2016-12-12 21:01:12 +01:00
|
|
|
log.info("Couldn't save encrypted content. Input stream was null");
|
|
|
|
return;
|
|
|
|
}
|
2017-05-17 19:20:42 +02:00
|
|
|
|
2016-12-12 21:01:12 +01:00
|
|
|
File output = new File(outputFolder + File.separator + content.getFilename());
|
2017-05-17 19:20:42 +02:00
|
|
|
if (output.exists()) {
|
|
|
|
if (output.length() == content.getEncryptedFileSizeAligned()) {
|
2016-12-12 21:01:12 +01:00
|
|
|
log.info("Encrypted content alreadys exists, skipped");
|
|
|
|
return;
|
2017-05-17 19:20:42 +02:00
|
|
|
} else {
|
2016-12-12 21:01:12 +01:00
|
|
|
log.info("Encrypted content alreadys exists, but the length is not as expected. Saving it again");
|
|
|
|
}
|
|
|
|
}
|
2017-05-17 19:20:42 +02:00
|
|
|
FileUtils.saveInputStreamToFile(output, inputStream, content.getEncryptedFileSizeAligned());
|
2016-12-12 21:01:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param content
|
|
|
|
* @param offset
|
|
|
|
* @return
|
|
|
|
* @throws IOException
|
|
|
|
*/
|
2017-05-17 19:20:42 +02:00
|
|
|
public abstract InputStream getInputStreamFromContent(Content content, long offset) throws IOException;
|
2016-12-12 21:01:12 +01:00
|
|
|
|
2017-05-17 19:20:42 +02:00
|
|
|
// TODO: JavaDocs
|
2016-12-12 21:01:12 +01:00
|
|
|
public abstract byte[] getContentH3Hash(Content content) throws IOException;
|
2017-05-17 19:20:42 +02:00
|
|
|
|
2016-12-12 21:01:12 +01:00
|
|
|
public abstract byte[] getRawTMD() throws IOException;
|
2017-05-17 19:20:42 +02:00
|
|
|
|
2016-12-12 21:01:12 +01:00
|
|
|
public abstract byte[] getRawTicket() throws IOException;
|
2017-05-17 19:20:42 +02:00
|
|
|
|
2016-12-12 21:01:12 +01:00
|
|
|
public abstract byte[] getRawCert() throws IOException;
|
2017-05-17 19:20:42 +02:00
|
|
|
|
2016-12-12 21:01:12 +01:00
|
|
|
public abstract void cleanup() throws IOException;
|
2017-05-17 19:20:42 +02:00
|
|
|
|
2016-12-12 21:01:12 +01:00
|
|
|
}
|