2018-07-25 12:30:50 -07:00
|
|
|
// Copyright 2018 yuzu emulator team
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2019-04-10 10:59:41 -07:00
|
|
|
#include "core/crypto/key_manager.h"
|
|
|
|
#include "core/hle/ipc_helpers.h"
|
2020-04-16 19:43:33 -07:00
|
|
|
#include "core/hle/service/es/es.h"
|
2018-07-25 12:30:50 -07:00
|
|
|
#include "core/hle/service/service.h"
|
|
|
|
|
|
|
|
namespace Service::ES {
|
|
|
|
|
2019-04-10 10:59:41 -07:00
|
|
|
constexpr ResultCode ERROR_INVALID_ARGUMENT{ErrorModule::ETicket, 2};
|
|
|
|
constexpr ResultCode ERROR_INVALID_RIGHTS_ID{ErrorModule::ETicket, 3};
|
|
|
|
|
2018-07-25 12:30:50 -07:00
|
|
|
class ETicket final : public ServiceFramework<ETicket> {
|
|
|
|
public:
|
|
|
|
explicit ETicket() : ServiceFramework{"es"} {
|
2018-10-19 01:12:26 -07:00
|
|
|
// clang-format off
|
2018-07-25 12:30:50 -07:00
|
|
|
static const FunctionInfo functions[] = {
|
2019-04-10 10:59:41 -07:00
|
|
|
{1, &ETicket::ImportTicket, "ImportTicket"},
|
2018-07-25 12:30:50 -07:00
|
|
|
{2, nullptr, "ImportTicketCertificateSet"},
|
|
|
|
{3, nullptr, "DeleteTicket"},
|
|
|
|
{4, nullptr, "DeletePersonalizedTicket"},
|
|
|
|
{5, nullptr, "DeleteAllCommonTicket"},
|
|
|
|
{6, nullptr, "DeleteAllPersonalizedTicket"},
|
|
|
|
{7, nullptr, "DeleteAllPersonalizedTicketEx"},
|
2019-04-10 11:00:39 -07:00
|
|
|
{8, &ETicket::GetTitleKey, "GetTitleKey"},
|
2019-04-10 11:02:27 -07:00
|
|
|
{9, &ETicket::CountCommonTicket, "CountCommonTicket"},
|
2019-04-10 11:03:08 -07:00
|
|
|
{10, &ETicket::CountPersonalizedTicket, "CountPersonalizedTicket"},
|
2020-06-26 17:43:22 -07:00
|
|
|
{11, &ETicket::ListCommonTicketRightsIds, "ListCommonTicketRightsIds"},
|
|
|
|
{12, &ETicket::ListPersonalizedTicketRightsIds, "ListPersonalizedTicketRightsIds"},
|
2018-07-25 12:30:50 -07:00
|
|
|
{13, nullptr, "ListMissingPersonalizedTicket"},
|
2019-04-10 11:06:17 -07:00
|
|
|
{14, &ETicket::GetCommonTicketSize, "GetCommonTicketSize"},
|
2019-04-10 11:07:00 -07:00
|
|
|
{15, &ETicket::GetPersonalizedTicketSize, "GetPersonalizedTicketSize"},
|
2019-04-10 11:07:49 -07:00
|
|
|
{16, &ETicket::GetCommonTicketData, "GetCommonTicketData"},
|
2019-04-10 11:08:19 -07:00
|
|
|
{17, &ETicket::GetPersonalizedTicketData, "GetPersonalizedTicketData"},
|
2018-07-25 12:30:50 -07:00
|
|
|
{18, nullptr, "OwnTicket"},
|
|
|
|
{19, nullptr, "GetTicketInfo"},
|
|
|
|
{20, nullptr, "ListLightTicketInfo"},
|
|
|
|
{21, nullptr, "SignData"},
|
|
|
|
{22, nullptr, "GetCommonTicketAndCertificateSize"},
|
|
|
|
{23, nullptr, "GetCommonTicketAndCertificateData"},
|
|
|
|
{24, nullptr, "ImportPrepurchaseRecord"},
|
|
|
|
{25, nullptr, "DeletePrepurchaseRecord"},
|
|
|
|
{26, nullptr, "DeleteAllPrepurchaseRecord"},
|
|
|
|
{27, nullptr, "CountPrepurchaseRecord"},
|
2018-10-19 01:12:26 -07:00
|
|
|
{28, nullptr, "ListPrepurchaseRecordRightsIds"},
|
2018-07-25 12:30:50 -07:00
|
|
|
{29, nullptr, "ListPrepurchaseRecordInfo"},
|
2018-10-19 01:12:26 -07:00
|
|
|
{30, nullptr, "CountTicket"},
|
|
|
|
{31, nullptr, "ListTicketRightsIds"},
|
|
|
|
{32, nullptr, "CountPrepurchaseRecordEx"},
|
|
|
|
{33, nullptr, "ListPrepurchaseRecordRightsIdsEx"},
|
|
|
|
{34, nullptr, "GetEncryptedTicketSize"},
|
|
|
|
{35, nullptr, "GetEncryptedTicketData"},
|
|
|
|
{36, nullptr, "DeleteAllInactiveELicenseRequiredPersonalizedTicket"},
|
2019-11-12 05:54:58 -08:00
|
|
|
{37, nullptr, "OwnTicket2"},
|
|
|
|
{38, nullptr, "OwnTicket3"},
|
2020-06-26 17:48:29 -07:00
|
|
|
{501, nullptr, "Unknown501"},
|
|
|
|
{502, nullptr, "Unknown502"},
|
2018-10-19 01:12:26 -07:00
|
|
|
{503, nullptr, "GetTitleKey"},
|
2020-06-26 17:48:29 -07:00
|
|
|
{504, nullptr, "Unknown504"},
|
|
|
|
{508, nullptr, "Unknown508"},
|
|
|
|
{509, nullptr, "Unknown509"},
|
|
|
|
{510, nullptr, "Unknown510"},
|
|
|
|
{511, nullptr, "Unknown511"},
|
|
|
|
{1001, nullptr, "Unknown1001"},
|
|
|
|
{1002, nullptr, "Unknown1001"},
|
|
|
|
{1003, nullptr, "Unknown1003"},
|
|
|
|
{1004, nullptr, "Unknown1004"},
|
|
|
|
{1005, nullptr, "Unknown1005"},
|
|
|
|
{1006, nullptr, "Unknown1006"},
|
|
|
|
{1007, nullptr, "Unknown1007"},
|
|
|
|
{1009, nullptr, "Unknown1009"},
|
|
|
|
{1010, nullptr, "Unknown1010"},
|
|
|
|
{1011, nullptr, "Unknown1011"},
|
|
|
|
{1012, nullptr, "Unknown1012"},
|
|
|
|
{1013, nullptr, "Unknown1013"},
|
|
|
|
{1014, nullptr, "Unknown1014"},
|
|
|
|
{1015, nullptr, "Unknown1015"},
|
|
|
|
{1016, nullptr, "Unknown1016"},
|
|
|
|
{1017, nullptr, "Unknown1017"},
|
|
|
|
{1018, nullptr, "Unknown1018"},
|
|
|
|
{1019, nullptr, "Unknown1019"},
|
|
|
|
{1020, nullptr, "Unknown1020"},
|
|
|
|
{1021, nullptr, "Unknown1021"},
|
|
|
|
{1501, nullptr, "Unknown1501"},
|
|
|
|
{1502, nullptr, "Unknown1502"},
|
|
|
|
{1503, nullptr, "Unknown1503"},
|
|
|
|
{1504, nullptr, "Unknown1504"},
|
|
|
|
{1505, nullptr, "Unknown1505"},
|
|
|
|
{2000, nullptr, "Unknown2000"},
|
|
|
|
{2001, nullptr, "Unknown2001"},
|
|
|
|
{2100, nullptr, "Unknown2100"},
|
|
|
|
{2501, nullptr, "Unknown2501"},
|
|
|
|
{2502, nullptr, "Unknown2502"},
|
|
|
|
{3001, nullptr, "Unknown3001"},
|
|
|
|
{3002, nullptr, "Unknown3002"},
|
2018-07-25 12:30:50 -07:00
|
|
|
};
|
2018-10-19 01:12:26 -07:00
|
|
|
// clang-format on
|
2018-07-25 12:30:50 -07:00
|
|
|
RegisterHandlers(functions);
|
2019-04-17 08:29:21 -07:00
|
|
|
|
|
|
|
keys.PopulateTickets();
|
|
|
|
keys.SynthesizeTickets();
|
2018-07-25 12:30:50 -07:00
|
|
|
}
|
2019-04-10 10:59:41 -07:00
|
|
|
|
|
|
|
private:
|
|
|
|
bool CheckRightsId(Kernel::HLERequestContext& ctx, const u128& rights_id) {
|
|
|
|
if (rights_id == u128{}) {
|
|
|
|
LOG_ERROR(Service_ETicket, "The rights ID was invalid!");
|
|
|
|
IPC::ResponseBuilder rb{ctx, 2};
|
|
|
|
rb.Push(ERROR_INVALID_RIGHTS_ID);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ImportTicket(Kernel::HLERequestContext& ctx) {
|
|
|
|
const auto ticket = ctx.ReadBuffer();
|
|
|
|
const auto cert = ctx.ReadBuffer(1);
|
|
|
|
|
2019-04-16 06:12:04 -07:00
|
|
|
if (ticket.size() < sizeof(Core::Crypto::Ticket)) {
|
2019-04-10 10:59:41 -07:00
|
|
|
LOG_ERROR(Service_ETicket, "The input buffer is not large enough!");
|
|
|
|
IPC::ResponseBuilder rb{ctx, 2};
|
|
|
|
rb.Push(ERROR_INVALID_ARGUMENT);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-04-16 06:12:04 -07:00
|
|
|
Core::Crypto::Ticket raw{};
|
|
|
|
std::memcpy(&raw, ticket.data(), sizeof(Core::Crypto::Ticket));
|
2019-04-10 10:59:41 -07:00
|
|
|
|
|
|
|
if (!keys.AddTicketPersonalized(raw)) {
|
|
|
|
LOG_ERROR(Service_ETicket, "The ticket could not be imported!");
|
|
|
|
IPC::ResponseBuilder rb{ctx, 2};
|
|
|
|
rb.Push(ERROR_INVALID_ARGUMENT);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
IPC::ResponseBuilder rb{ctx, 2};
|
|
|
|
rb.Push(RESULT_SUCCESS);
|
|
|
|
}
|
|
|
|
|
2019-04-10 11:00:39 -07:00
|
|
|
void GetTitleKey(Kernel::HLERequestContext& ctx) {
|
|
|
|
IPC::RequestParser rp{ctx};
|
|
|
|
const auto rights_id = rp.PopRaw<u128>();
|
|
|
|
|
|
|
|
LOG_DEBUG(Service_ETicket, "called, rights_id={:016X}{:016X}", rights_id[1], rights_id[0]);
|
|
|
|
|
|
|
|
if (!CheckRightsId(ctx, rights_id))
|
|
|
|
return;
|
|
|
|
|
|
|
|
const auto key =
|
|
|
|
keys.GetKey(Core::Crypto::S128KeyType::Titlekey, rights_id[1], rights_id[0]);
|
|
|
|
|
|
|
|
if (key == Core::Crypto::Key128{}) {
|
|
|
|
LOG_ERROR(Service_ETicket,
|
|
|
|
"The titlekey doesn't exist in the KeyManager or the rights ID was invalid!");
|
|
|
|
IPC::ResponseBuilder rb{ctx, 2};
|
|
|
|
rb.Push(ERROR_INVALID_RIGHTS_ID);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.WriteBuffer(key.data(), key.size());
|
|
|
|
|
|
|
|
IPC::ResponseBuilder rb{ctx, 2};
|
|
|
|
rb.Push(RESULT_SUCCESS);
|
|
|
|
}
|
|
|
|
|
2019-04-10 11:02:27 -07:00
|
|
|
void CountCommonTicket(Kernel::HLERequestContext& ctx) {
|
|
|
|
LOG_DEBUG(Service_ETicket, "called");
|
|
|
|
|
2019-09-29 13:45:09 -07:00
|
|
|
const u32 count = static_cast<u32>(keys.GetCommonTickets().size());
|
2019-04-10 11:02:27 -07:00
|
|
|
|
|
|
|
IPC::ResponseBuilder rb{ctx, 3};
|
|
|
|
rb.Push(RESULT_SUCCESS);
|
|
|
|
rb.Push<u32>(count);
|
|
|
|
}
|
|
|
|
|
2019-04-10 11:03:08 -07:00
|
|
|
void CountPersonalizedTicket(Kernel::HLERequestContext& ctx) {
|
|
|
|
LOG_DEBUG(Service_ETicket, "called");
|
|
|
|
|
2019-09-29 13:45:09 -07:00
|
|
|
const u32 count = static_cast<u32>(keys.GetPersonalizedTickets().size());
|
2019-04-10 11:03:08 -07:00
|
|
|
|
|
|
|
IPC::ResponseBuilder rb{ctx, 3};
|
|
|
|
rb.Push(RESULT_SUCCESS);
|
|
|
|
rb.Push<u32>(count);
|
|
|
|
}
|
|
|
|
|
2020-06-26 17:43:22 -07:00
|
|
|
void ListCommonTicketRightsIds(Kernel::HLERequestContext& ctx) {
|
2019-04-10 11:04:17 -07:00
|
|
|
u32 out_entries;
|
|
|
|
if (keys.GetCommonTickets().empty())
|
|
|
|
out_entries = 0;
|
|
|
|
else
|
2019-09-29 13:45:09 -07:00
|
|
|
out_entries = static_cast<u32>(ctx.GetWriteBufferSize() / sizeof(u128));
|
2019-04-10 11:04:17 -07:00
|
|
|
|
|
|
|
LOG_DEBUG(Service_ETicket, "called, entries={:016X}", out_entries);
|
|
|
|
|
|
|
|
keys.PopulateTickets();
|
|
|
|
const auto tickets = keys.GetCommonTickets();
|
|
|
|
std::vector<u128> ids;
|
|
|
|
std::transform(tickets.begin(), tickets.end(), std::back_inserter(ids),
|
|
|
|
[](const auto& pair) { return pair.first; });
|
|
|
|
|
2019-09-29 13:45:09 -07:00
|
|
|
out_entries = static_cast<u32>(std::min<std::size_t>(ids.size(), out_entries));
|
2019-04-10 11:04:17 -07:00
|
|
|
ctx.WriteBuffer(ids.data(), out_entries * sizeof(u128));
|
|
|
|
|
|
|
|
IPC::ResponseBuilder rb{ctx, 3};
|
|
|
|
rb.Push(RESULT_SUCCESS);
|
|
|
|
rb.Push<u32>(out_entries);
|
|
|
|
}
|
|
|
|
|
2020-06-26 17:43:22 -07:00
|
|
|
void ListPersonalizedTicketRightsIds(Kernel::HLERequestContext& ctx) {
|
2019-04-10 11:05:12 -07:00
|
|
|
u32 out_entries;
|
|
|
|
if (keys.GetPersonalizedTickets().empty())
|
|
|
|
out_entries = 0;
|
|
|
|
else
|
2019-09-29 13:45:09 -07:00
|
|
|
out_entries = static_cast<u32>(ctx.GetWriteBufferSize() / sizeof(u128));
|
2019-04-10 11:05:12 -07:00
|
|
|
|
|
|
|
LOG_DEBUG(Service_ETicket, "called, entries={:016X}", out_entries);
|
|
|
|
|
|
|
|
keys.PopulateTickets();
|
|
|
|
const auto tickets = keys.GetPersonalizedTickets();
|
|
|
|
std::vector<u128> ids;
|
|
|
|
std::transform(tickets.begin(), tickets.end(), std::back_inserter(ids),
|
|
|
|
[](const auto& pair) { return pair.first; });
|
|
|
|
|
2019-09-29 13:45:09 -07:00
|
|
|
out_entries = static_cast<u32>(std::min<std::size_t>(ids.size(), out_entries));
|
2019-04-10 11:05:12 -07:00
|
|
|
ctx.WriteBuffer(ids.data(), out_entries * sizeof(u128));
|
|
|
|
|
|
|
|
IPC::ResponseBuilder rb{ctx, 3};
|
|
|
|
rb.Push(RESULT_SUCCESS);
|
|
|
|
rb.Push<u32>(out_entries);
|
|
|
|
}
|
|
|
|
|
2019-04-10 11:06:17 -07:00
|
|
|
void GetCommonTicketSize(Kernel::HLERequestContext& ctx) {
|
|
|
|
IPC::RequestParser rp{ctx};
|
|
|
|
const auto rights_id = rp.PopRaw<u128>();
|
|
|
|
|
|
|
|
LOG_DEBUG(Service_ETicket, "called, rights_id={:016X}{:016X}", rights_id[1], rights_id[0]);
|
|
|
|
|
|
|
|
if (!CheckRightsId(ctx, rights_id))
|
|
|
|
return;
|
|
|
|
|
|
|
|
const auto ticket = keys.GetCommonTickets().at(rights_id);
|
|
|
|
|
|
|
|
IPC::ResponseBuilder rb{ctx, 4};
|
|
|
|
rb.Push(RESULT_SUCCESS);
|
2019-04-16 06:12:04 -07:00
|
|
|
rb.Push<u64>(ticket.GetSize());
|
2019-04-10 11:06:17 -07:00
|
|
|
}
|
|
|
|
|
2019-04-10 11:07:00 -07:00
|
|
|
void GetPersonalizedTicketSize(Kernel::HLERequestContext& ctx) {
|
|
|
|
IPC::RequestParser rp{ctx};
|
|
|
|
const auto rights_id = rp.PopRaw<u128>();
|
|
|
|
|
|
|
|
LOG_DEBUG(Service_ETicket, "called, rights_id={:016X}{:016X}", rights_id[1], rights_id[0]);
|
|
|
|
|
|
|
|
if (!CheckRightsId(ctx, rights_id))
|
|
|
|
return;
|
|
|
|
|
|
|
|
const auto ticket = keys.GetPersonalizedTickets().at(rights_id);
|
|
|
|
|
|
|
|
IPC::ResponseBuilder rb{ctx, 4};
|
|
|
|
rb.Push(RESULT_SUCCESS);
|
2019-04-16 06:12:04 -07:00
|
|
|
rb.Push<u64>(ticket.GetSize());
|
2019-04-10 11:07:00 -07:00
|
|
|
}
|
|
|
|
|
2019-04-10 11:07:49 -07:00
|
|
|
void GetCommonTicketData(Kernel::HLERequestContext& ctx) {
|
|
|
|
IPC::RequestParser rp{ctx};
|
|
|
|
const auto rights_id = rp.PopRaw<u128>();
|
|
|
|
|
|
|
|
LOG_DEBUG(Service_ETicket, "called, rights_id={:016X}{:016X}", rights_id[1], rights_id[0]);
|
|
|
|
|
|
|
|
if (!CheckRightsId(ctx, rights_id))
|
|
|
|
return;
|
|
|
|
|
|
|
|
const auto ticket = keys.GetCommonTickets().at(rights_id);
|
|
|
|
|
2019-05-26 10:01:42 -07:00
|
|
|
const auto write_size = std::min<u64>(ticket.GetSize(), ctx.GetWriteBufferSize());
|
2019-04-16 06:12:04 -07:00
|
|
|
ctx.WriteBuffer(&ticket, write_size);
|
2019-04-10 11:07:49 -07:00
|
|
|
|
|
|
|
IPC::ResponseBuilder rb{ctx, 4};
|
|
|
|
rb.Push(RESULT_SUCCESS);
|
|
|
|
rb.Push<u64>(write_size);
|
|
|
|
}
|
|
|
|
|
2019-04-10 11:08:19 -07:00
|
|
|
void GetPersonalizedTicketData(Kernel::HLERequestContext& ctx) {
|
|
|
|
IPC::RequestParser rp{ctx};
|
|
|
|
const auto rights_id = rp.PopRaw<u128>();
|
|
|
|
|
|
|
|
LOG_DEBUG(Service_ETicket, "called, rights_id={:016X}{:016X}", rights_id[1], rights_id[0]);
|
|
|
|
|
|
|
|
if (!CheckRightsId(ctx, rights_id))
|
|
|
|
return;
|
|
|
|
|
|
|
|
const auto ticket = keys.GetPersonalizedTickets().at(rights_id);
|
|
|
|
|
2019-05-26 10:01:42 -07:00
|
|
|
const auto write_size = std::min<u64>(ticket.GetSize(), ctx.GetWriteBufferSize());
|
2019-04-16 06:12:04 -07:00
|
|
|
ctx.WriteBuffer(&ticket, write_size);
|
2019-04-10 11:08:19 -07:00
|
|
|
|
|
|
|
IPC::ResponseBuilder rb{ctx, 4};
|
|
|
|
rb.Push(RESULT_SUCCESS);
|
|
|
|
rb.Push<u64>(write_size);
|
|
|
|
}
|
|
|
|
|
2020-05-20 12:28:16 -07:00
|
|
|
Core::Crypto::KeyManager& keys = Core::Crypto::KeyManager::instance();
|
2018-07-25 12:30:50 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
void InstallInterfaces(SM::ServiceManager& service_manager) {
|
|
|
|
std::make_shared<ETicket>()->InstallAsService(service_manager);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Service::ES
|