commit
5fb3137bdc
@ -0,0 +1,40 @@
|
||||
// Copyright 2018 Citra Emulator Project
|
||||
// Licensed under GPLv2 or any later version
|
||||
// Refer to the license.txt file included.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "common/assert.h"
|
||||
#include "common/common_types.h"
|
||||
|
||||
namespace FileSys {
|
||||
|
||||
enum TMDSignatureType : u32 {
|
||||
Rsa4096Sha1 = 0x10000,
|
||||
Rsa2048Sha1 = 0x10001,
|
||||
EllipticSha1 = 0x10002,
|
||||
Rsa4096Sha256 = 0x10003,
|
||||
Rsa2048Sha256 = 0x10004,
|
||||
EcdsaSha256 = 0x10005
|
||||
};
|
||||
|
||||
inline u32 GetSignatureSize(u32 signature_type) {
|
||||
switch (signature_type) {
|
||||
case Rsa4096Sha1:
|
||||
case Rsa4096Sha256:
|
||||
return 0x200;
|
||||
|
||||
case Rsa2048Sha1:
|
||||
case Rsa2048Sha256:
|
||||
return 0x100;
|
||||
|
||||
case EllipticSha1:
|
||||
case EcdsaSha256:
|
||||
return 0x3C;
|
||||
}
|
||||
|
||||
UNREACHABLE();
|
||||
return 0;
|
||||
}
|
||||
|
||||
} // namespace FileSys
|
@ -0,0 +1,56 @@
|
||||
// Copyright 2018 Citra Emulator Project
|
||||
// Licensed under GPLv2 or any later version
|
||||
// Refer to the license.txt file included.
|
||||
|
||||
#include <algorithm>
|
||||
#include <cryptopp/aes.h>
|
||||
#include <cryptopp/modes.h>
|
||||
#include "common/alignment.h"
|
||||
#include "core/file_sys/cia_common.h"
|
||||
#include "core/file_sys/ticket.h"
|
||||
#include "core/hw/aes/key.h"
|
||||
#include "core/loader/loader.h"
|
||||
|
||||
namespace FileSys {
|
||||
|
||||
Loader::ResultStatus Ticket::Load(const std::vector<u8> file_data, std::size_t offset) {
|
||||
std::size_t total_size = static_cast<std::size_t>(file_data.size() - offset);
|
||||
if (total_size < sizeof(u32))
|
||||
return Loader::ResultStatus::Error;
|
||||
|
||||
std::memcpy(&signature_type, &file_data[offset], sizeof(u32));
|
||||
|
||||
// Signature lengths are variable, and the body follows the signature
|
||||
u32 signature_size = GetSignatureSize(signature_type);
|
||||
|
||||
// The ticket body start position is rounded to the nearest 0x40 after the signature
|
||||
std::size_t body_start = Common::AlignUp(signature_size + sizeof(u32), 0x40);
|
||||
std::size_t body_end = body_start + sizeof(Body);
|
||||
|
||||
if (total_size < body_end)
|
||||
return Loader::ResultStatus::Error;
|
||||
|
||||
// Read signature + ticket body
|
||||
ticket_signature.resize(signature_size);
|
||||
memcpy(ticket_signature.data(), &file_data[offset + sizeof(u32)], signature_size);
|
||||
memcpy(&ticket_body, &file_data[offset + body_start], sizeof(Body));
|
||||
|
||||
return Loader::ResultStatus::Success;
|
||||
}
|
||||
|
||||
boost::optional<std::array<u8, 16>> Ticket::GetTitleKey() const {
|
||||
HW::AES::InitKeys();
|
||||
std::array<u8, 16> ctr{};
|
||||
std::memcpy(ctr.data(), &ticket_body.title_id, sizeof(u64));
|
||||
HW::AES::SelectCommonKeyIndex(ticket_body.common_key_index);
|
||||
if (!HW::AES::IsNormalKeyAvailable(HW::AES::KeySlotID::TicketCommonKey)) {
|
||||
return boost::none;
|
||||
}
|
||||
auto key = HW::AES::GetNormalKey(HW::AES::KeySlotID::TicketCommonKey);
|
||||
auto title_key = ticket_body.title_key;
|
||||
CryptoPP::CBC_Mode<CryptoPP::AES>::Decryption{key.data(), key.size(), ctr.data()}.ProcessData(
|
||||
title_key.data(), title_key.data(), title_key.size());
|
||||
return title_key;
|
||||
}
|
||||
|
||||
} // namespace FileSys
|
@ -0,0 +1,58 @@
|
||||
// Copyright 2018 Citra Emulator Project
|
||||
// Licensed under GPLv2 or any later version
|
||||
// Refer to the license.txt file included.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <array>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <boost/optional.hpp>
|
||||
#include "common/common_funcs.h"
|
||||
#include "common/common_types.h"
|
||||
#include "common/swap.h"
|
||||
|
||||
namespace Loader {
|
||||
enum class ResultStatus;
|
||||
}
|
||||
|
||||
namespace FileSys {
|
||||
class Ticket {
|
||||
public:
|
||||
#pragma pack(push, 1)
|
||||
struct Body {
|
||||
std::array<u8, 0x40> issuer;
|
||||
std::array<u8, 0x3C> ecc_public_key;
|
||||
u8 version;
|
||||
u8 ca_crl_version;
|
||||
u8 signer_crl_version;
|
||||
std::array<u8, 0x10> title_key;
|
||||
INSERT_PADDING_BYTES(1);
|
||||
u64_be ticket_id;
|
||||
u32_be console_id;
|
||||
u64_be title_id;
|
||||
INSERT_PADDING_BYTES(2);
|
||||
u16_be ticket_title_version;
|
||||
INSERT_PADDING_BYTES(8);
|
||||
u8 license_type;
|
||||
u8 common_key_index;
|
||||
INSERT_PADDING_BYTES(0x2A);
|
||||
u32_be eshop_account_id;
|
||||
INSERT_PADDING_BYTES(1);
|
||||
u8 audit;
|
||||
INSERT_PADDING_BYTES(0x42);
|
||||
std::array<u8, 0x40> limits;
|
||||
std::array<u8, 0xAC> content_index;
|
||||
};
|
||||
static_assert(sizeof(Body) == 0x210, "Ticket body structure size is wrong");
|
||||
#pragma pack(pop)
|
||||
|
||||
Loader::ResultStatus Load(const std::vector<u8> file_data, std::size_t offset = 0);
|
||||
boost::optional<std::array<u8, 16>> GetTitleKey() const;
|
||||
|
||||
private:
|
||||
Body ticket_body;
|
||||
u32_be signature_type;
|
||||
std::vector<u8> ticket_signature;
|
||||
};
|
||||
} // namespace FileSys
|
Loading…
Reference in New Issue