mirror of
https://gitlab.com/suyu-emu/suyu.git
synced 2024-03-15 23:15:44 +00:00
key_manager: Convert Ticket union to std::variant
This commit is contained in:
parent
d9ef20e5a5
commit
50d5414075
|
@ -56,6 +56,13 @@ const std::map<std::pair<S128KeyType, u64>, std::string> KEYS_VARIABLE_LENGTH{
|
||||||
{{S128KeyType::KeyblobMAC, 0}, "keyblob_mac_key_"},
|
{{S128KeyType::KeyblobMAC, 0}, "keyblob_mac_key_"},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
template <std::size_t Size>
|
||||||
|
bool IsAllZeroArray(const std::array<u8, Size>& array) {
|
||||||
|
return std::all_of(array.begin(), array.end(), [](const auto& elem) { return elem == 0; });
|
||||||
|
}
|
||||||
|
} // namespace
|
||||||
|
|
||||||
u64 GetSignatureTypeDataSize(SignatureType type) {
|
u64 GetSignatureTypeDataSize(SignatureType type) {
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case SignatureType::RSA_4096_SHA1:
|
case SignatureType::RSA_4096_SHA1:
|
||||||
|
@ -85,47 +92,61 @@ u64 GetSignatureTypePaddingSize(SignatureType type) {
|
||||||
UNREACHABLE();
|
UNREACHABLE();
|
||||||
}
|
}
|
||||||
|
|
||||||
TicketData& Ticket::GetData() {
|
SignatureType Ticket::GetSignatureType() const {
|
||||||
switch (sig_type) {
|
if (auto ticket = std::get_if<RSA4096Ticket>(&data)) {
|
||||||
case SignatureType::RSA_4096_SHA1:
|
return ticket->sig_type;
|
||||||
case SignatureType::RSA_4096_SHA256:
|
|
||||||
return rsa_4096.data;
|
|
||||||
case SignatureType::RSA_2048_SHA1:
|
|
||||||
case SignatureType::RSA_2048_SHA256:
|
|
||||||
return rsa_2048.data;
|
|
||||||
case SignatureType::ECDSA_SHA1:
|
|
||||||
case SignatureType::ECDSA_SHA256:
|
|
||||||
return ecdsa.data;
|
|
||||||
}
|
}
|
||||||
|
if (auto ticket = std::get_if<RSA2048Ticket>(&data)) {
|
||||||
|
return ticket->sig_type;
|
||||||
|
}
|
||||||
|
if (auto ticket = std::get_if<ECDSATicket>(&data)) {
|
||||||
|
return ticket->sig_type;
|
||||||
|
}
|
||||||
|
|
||||||
|
UNREACHABLE();
|
||||||
|
}
|
||||||
|
|
||||||
|
TicketData& Ticket::GetData() {
|
||||||
|
if (auto ticket = std::get_if<RSA4096Ticket>(&data)) {
|
||||||
|
return ticket->data;
|
||||||
|
}
|
||||||
|
if (auto ticket = std::get_if<RSA2048Ticket>(&data)) {
|
||||||
|
return ticket->data;
|
||||||
|
}
|
||||||
|
if (auto ticket = std::get_if<ECDSATicket>(&data)) {
|
||||||
|
return ticket->data;
|
||||||
|
}
|
||||||
|
|
||||||
UNREACHABLE();
|
UNREACHABLE();
|
||||||
}
|
}
|
||||||
|
|
||||||
const TicketData& Ticket::GetData() const {
|
const TicketData& Ticket::GetData() const {
|
||||||
switch (sig_type) {
|
if (auto ticket = std::get_if<RSA4096Ticket>(&data)) {
|
||||||
case SignatureType::RSA_4096_SHA1:
|
return ticket->data;
|
||||||
case SignatureType::RSA_4096_SHA256:
|
|
||||||
return rsa_4096.data;
|
|
||||||
case SignatureType::RSA_2048_SHA1:
|
|
||||||
case SignatureType::RSA_2048_SHA256:
|
|
||||||
return rsa_2048.data;
|
|
||||||
case SignatureType::ECDSA_SHA1:
|
|
||||||
case SignatureType::ECDSA_SHA256:
|
|
||||||
return ecdsa.data;
|
|
||||||
}
|
}
|
||||||
|
if (auto ticket = std::get_if<RSA2048Ticket>(&data)) {
|
||||||
|
return ticket->data;
|
||||||
|
}
|
||||||
|
if (auto ticket = std::get_if<ECDSATicket>(&data)) {
|
||||||
|
return ticket->data;
|
||||||
|
}
|
||||||
|
|
||||||
UNREACHABLE();
|
UNREACHABLE();
|
||||||
}
|
}
|
||||||
|
|
||||||
u64 Ticket::GetSize() const {
|
u64 Ticket::GetSize() const {
|
||||||
|
const auto sig_type = GetSignatureType();
|
||||||
|
|
||||||
return sizeof(SignatureType) + GetSignatureTypeDataSize(sig_type) +
|
return sizeof(SignatureType) + GetSignatureTypeDataSize(sig_type) +
|
||||||
GetSignatureTypePaddingSize(sig_type) + sizeof(TicketData);
|
GetSignatureTypePaddingSize(sig_type) + sizeof(TicketData);
|
||||||
}
|
}
|
||||||
|
|
||||||
Ticket Ticket::SynthesizeCommon(Key128 title_key, std::array<u8, 16> rights_id) {
|
Ticket Ticket::SynthesizeCommon(Key128 title_key, const std::array<u8, 16>& rights_id) {
|
||||||
Ticket out{};
|
RSA2048Ticket out{};
|
||||||
out.sig_type = SignatureType::RSA_2048_SHA256;
|
out.sig_type = SignatureType::RSA_2048_SHA256;
|
||||||
out.GetData().rights_id = rights_id;
|
out.data.rights_id = rights_id;
|
||||||
out.GetData().title_key_common = title_key;
|
out.data.title_key_common = title_key;
|
||||||
return out;
|
return Ticket{out};
|
||||||
}
|
}
|
||||||
|
|
||||||
Key128 GenerateKeyEncryptionKey(Key128 source, Key128 master, Key128 kek_seed, Key128 key_seed) {
|
Key128 GenerateKeyEncryptionKey(Key128 source, Key128 master, Key128 kek_seed, Key128 key_seed) {
|
||||||
|
@ -208,14 +229,13 @@ void KeyManager::DeriveGeneralPurposeKeys(std::size_t crypto_revision) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
RSAKeyPair<2048> KeyManager::GetETicketRSAKey() {
|
RSAKeyPair<2048> KeyManager::GetETicketRSAKey() const {
|
||||||
if (eticket_extended_kek == std::array<u8, 576>{} || !HasKey(S128KeyType::ETicketRSAKek))
|
if (IsAllZeroArray(eticket_extended_kek) || !HasKey(S128KeyType::ETicketRSAKek))
|
||||||
return {};
|
return {};
|
||||||
|
|
||||||
const auto eticket_final = GetKey(S128KeyType::ETicketRSAKek);
|
const auto eticket_final = GetKey(S128KeyType::ETicketRSAKek);
|
||||||
|
|
||||||
std::vector<u8> extended_iv(0x10);
|
std::vector<u8> extended_iv(eticket_extended_kek.begin(), eticket_extended_kek.begin() + 0x10);
|
||||||
std::memcpy(extended_iv.data(), eticket_extended_kek.data(), extended_iv.size());
|
|
||||||
std::array<u8, 0x230> extended_dec{};
|
std::array<u8, 0x230> extended_dec{};
|
||||||
AESCipher<Key128> rsa_1(eticket_final, Mode::CTR);
|
AESCipher<Key128> rsa_1(eticket_final, Mode::CTR);
|
||||||
rsa_1.SetIV(extended_iv);
|
rsa_1.SetIV(extended_iv);
|
||||||
|
@ -1001,13 +1021,14 @@ void KeyManager::PopulateTickets() {
|
||||||
|
|
||||||
void KeyManager::SynthesizeTickets() {
|
void KeyManager::SynthesizeTickets() {
|
||||||
for (const auto& key : s128_keys) {
|
for (const auto& key : s128_keys) {
|
||||||
if (key.first.type == S128KeyType::Titlekey) {
|
if (key.first.type != S128KeyType::Titlekey) {
|
||||||
u128 rights_id{key.first.field1, key.first.field2};
|
continue;
|
||||||
Key128 rights_id_2;
|
|
||||||
std::memcpy(rights_id_2.data(), rights_id.data(), rights_id_2.size());
|
|
||||||
const auto ticket = Ticket::SynthesizeCommon(key.second, rights_id_2);
|
|
||||||
common_tickets.insert_or_assign(rights_id, ticket);
|
|
||||||
}
|
}
|
||||||
|
u128 rights_id{key.first.field1, key.first.field2};
|
||||||
|
Key128 rights_id_2;
|
||||||
|
std::memcpy(rights_id_2.data(), rights_id.data(), rights_id_2.size());
|
||||||
|
const auto ticket = Ticket::SynthesizeCommon(key.second, rights_id_2);
|
||||||
|
common_tickets.insert_or_assign(rights_id, ticket);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -9,6 +9,7 @@
|
||||||
#include <optional>
|
#include <optional>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
#include <variant>
|
||||||
#include <boost/container/flat_map.hpp>
|
#include <boost/container/flat_map.hpp>
|
||||||
#include <fmt/format.h>
|
#include <fmt/format.h>
|
||||||
#include "common/common_funcs.h"
|
#include "common/common_funcs.h"
|
||||||
|
@ -73,33 +74,36 @@ struct TicketData {
|
||||||
};
|
};
|
||||||
static_assert(sizeof(TicketData) == 0x2C0, "TicketData has incorrect size.");
|
static_assert(sizeof(TicketData) == 0x2C0, "TicketData has incorrect size.");
|
||||||
|
|
||||||
struct Ticket {
|
struct RSA4096Ticket {
|
||||||
SignatureType sig_type;
|
SignatureType sig_type;
|
||||||
union {
|
std::array<u8, 0x200> sig_data;
|
||||||
struct {
|
INSERT_PADDING_BYTES(0x3C);
|
||||||
std::array<u8, 0x200> sig_data;
|
TicketData data;
|
||||||
INSERT_PADDING_BYTES(0x3C);
|
};
|
||||||
TicketData data;
|
|
||||||
} rsa_4096;
|
|
||||||
|
|
||||||
struct {
|
struct RSA2048Ticket {
|
||||||
std::array<u8, 0x100> sig_data;
|
SignatureType sig_type;
|
||||||
INSERT_PADDING_BYTES(0x3C);
|
std::array<u8, 0x100> sig_data;
|
||||||
TicketData data;
|
INSERT_PADDING_BYTES(0x3C);
|
||||||
} rsa_2048;
|
TicketData data;
|
||||||
|
};
|
||||||
|
|
||||||
struct {
|
struct ECDSATicket {
|
||||||
std::array<u8, 0x3C> sig_data;
|
SignatureType sig_type;
|
||||||
INSERT_PADDING_BYTES(0x40);
|
std::array<u8, 0x3C> sig_data;
|
||||||
TicketData data;
|
INSERT_PADDING_BYTES(0x40);
|
||||||
} ecdsa;
|
TicketData data;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct Ticket {
|
||||||
|
std::variant<RSA4096Ticket, RSA2048Ticket, ECDSATicket> data;
|
||||||
|
|
||||||
|
SignatureType GetSignatureType() const;
|
||||||
TicketData& GetData();
|
TicketData& GetData();
|
||||||
const TicketData& GetData() const;
|
const TicketData& GetData() const;
|
||||||
u64 GetSize() const;
|
u64 GetSize() const;
|
||||||
|
|
||||||
static Ticket SynthesizeCommon(Key128 title_key, std::array<u8, 0x10> rights_id);
|
static Ticket SynthesizeCommon(Key128 title_key, const std::array<u8, 0x10>& rights_id);
|
||||||
};
|
};
|
||||||
|
|
||||||
static_assert(sizeof(Key128) == 16, "Key128 must be 128 bytes big.");
|
static_assert(sizeof(Key128) == 16, "Key128 must be 128 bytes big.");
|
||||||
|
@ -120,6 +124,12 @@ bool operator==(const RSAKeyPair<bit_size, byte_size>& lhs,
|
||||||
std::tie(rhs.encryption_key, rhs.decryption_key, rhs.modulus, rhs.exponent);
|
std::tie(rhs.encryption_key, rhs.decryption_key, rhs.modulus, rhs.exponent);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <size_t bit_size, size_t byte_size>
|
||||||
|
bool operator!=(const RSAKeyPair<bit_size, byte_size>& lhs,
|
||||||
|
const RSAKeyPair<bit_size, byte_size>& rhs) {
|
||||||
|
return !(lhs == rhs);
|
||||||
|
}
|
||||||
|
|
||||||
enum class KeyCategory : u8 {
|
enum class KeyCategory : u8 {
|
||||||
Standard,
|
Standard,
|
||||||
Title,
|
Title,
|
||||||
|
@ -268,7 +278,7 @@ private:
|
||||||
|
|
||||||
void DeriveGeneralPurposeKeys(std::size_t crypto_revision);
|
void DeriveGeneralPurposeKeys(std::size_t crypto_revision);
|
||||||
|
|
||||||
RSAKeyPair<2048> GetETicketRSAKey();
|
RSAKeyPair<2048> GetETicketRSAKey() const;
|
||||||
|
|
||||||
void SetKeyWrapped(S128KeyType id, Key128 key, u64 field1 = 0, u64 field2 = 0);
|
void SetKeyWrapped(S128KeyType id, Key128 key, u64 field1 = 0, u64 field2 = 0);
|
||||||
void SetKeyWrapped(S256KeyType id, Key256 key, u64 field1 = 0, u64 field2 = 0);
|
void SetKeyWrapped(S256KeyType id, Key256 key, u64 field1 = 0, u64 field2 = 0);
|
||||||
|
|
|
@ -234,7 +234,7 @@ private:
|
||||||
|
|
||||||
const auto ticket = keys.GetCommonTickets().at(rights_id);
|
const auto ticket = keys.GetCommonTickets().at(rights_id);
|
||||||
|
|
||||||
const auto write_size = std::min(ticket.GetSize(), ctx.GetWriteBufferSize());
|
const auto write_size = std::min<u64>(ticket.GetSize(), ctx.GetWriteBufferSize());
|
||||||
ctx.WriteBuffer(&ticket, write_size);
|
ctx.WriteBuffer(&ticket, write_size);
|
||||||
|
|
||||||
IPC::ResponseBuilder rb{ctx, 4};
|
IPC::ResponseBuilder rb{ctx, 4};
|
||||||
|
@ -253,7 +253,7 @@ private:
|
||||||
|
|
||||||
const auto ticket = keys.GetPersonalizedTickets().at(rights_id);
|
const auto ticket = keys.GetPersonalizedTickets().at(rights_id);
|
||||||
|
|
||||||
const auto write_size = std::min(ticket.GetSize(), ctx.GetWriteBufferSize());
|
const auto write_size = std::min<u64>(ticket.GetSize(), ctx.GetWriteBufferSize());
|
||||||
ctx.WriteBuffer(&ticket, write_size);
|
ctx.WriteBuffer(&ticket, write_size);
|
||||||
|
|
||||||
IPC::ResponseBuilder rb{ctx, 4};
|
IPC::ResponseBuilder rb{ctx, 4};
|
||||||
|
|
Loading…
Reference in a new issue