2018-07-25 19:30:50 +00:00
|
|
|
// Copyright 2018 yuzu emulator team
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2019-04-10 17:59:41 +00:00
|
|
|
#include "core/crypto/key_manager.h"
|
|
|
|
#include "core/hle/ipc_helpers.h"
|
2018-07-25 19:30:50 +00:00
|
|
|
#include "core/hle/service/service.h"
|
|
|
|
|
|
|
|
namespace Service::ES {
|
|
|
|
|
2019-04-10 17:59:41 +00:00
|
|
|
constexpr ResultCode ERROR_INVALID_ARGUMENT{ErrorModule::ETicket, 2};
|
|
|
|
constexpr ResultCode ERROR_INVALID_RIGHTS_ID{ErrorModule::ETicket, 3};
|
|
|
|
|
2018-07-25 19:30:50 +00:00
|
|
|
class ETicket final : public ServiceFramework<ETicket> {
|
|
|
|
public:
|
|
|
|
explicit ETicket() : ServiceFramework{"es"} {
|
2018-10-19 08:12:26 +00:00
|
|
|
// clang-format off
|
2018-07-25 19:30:50 +00:00
|
|
|
static const FunctionInfo functions[] = {
|
2019-04-10 17:59:41 +00:00
|
|
|
{1, &ETicket::ImportTicket, "ImportTicket"},
|
2018-07-25 19:30:50 +00:00
|
|
|
{2, nullptr, "ImportTicketCertificateSet"},
|
|
|
|
{3, nullptr, "DeleteTicket"},
|
|
|
|
{4, nullptr, "DeletePersonalizedTicket"},
|
|
|
|
{5, nullptr, "DeleteAllCommonTicket"},
|
|
|
|
{6, nullptr, "DeleteAllPersonalizedTicket"},
|
|
|
|
{7, nullptr, "DeleteAllPersonalizedTicketEx"},
|
2019-04-10 18:00:39 +00:00
|
|
|
{8, &ETicket::GetTitleKey, "GetTitleKey"},
|
2019-04-10 18:02:27 +00:00
|
|
|
{9, &ETicket::CountCommonTicket, "CountCommonTicket"},
|
2019-04-10 18:03:08 +00:00
|
|
|
{10, &ETicket::CountPersonalizedTicket, "CountPersonalizedTicket"},
|
2019-04-10 18:04:17 +00:00
|
|
|
{11, &ETicket::ListCommonTicket, "ListCommonTicket"},
|
2019-04-10 18:05:12 +00:00
|
|
|
{12, &ETicket::ListPersonalizedTicket, "ListPersonalizedTicket"},
|
2018-07-25 19:30:50 +00:00
|
|
|
{13, nullptr, "ListMissingPersonalizedTicket"},
|
2019-04-10 18:06:17 +00:00
|
|
|
{14, &ETicket::GetCommonTicketSize, "GetCommonTicketSize"},
|
2019-04-10 18:07:00 +00:00
|
|
|
{15, &ETicket::GetPersonalizedTicketSize, "GetPersonalizedTicketSize"},
|
2019-04-10 18:07:49 +00:00
|
|
|
{16, &ETicket::GetCommonTicketData, "GetCommonTicketData"},
|
2019-04-10 18:08:19 +00:00
|
|
|
{17, &ETicket::GetPersonalizedTicketData, "GetPersonalizedTicketData"},
|
2018-07-25 19:30:50 +00: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 08:12:26 +00:00
|
|
|
{28, nullptr, "ListPrepurchaseRecordRightsIds"},
|
2018-07-25 19:30:50 +00:00
|
|
|
{29, nullptr, "ListPrepurchaseRecordInfo"},
|
2018-10-19 08:12:26 +00:00
|
|
|
{30, nullptr, "CountTicket"},
|
|
|
|
{31, nullptr, "ListTicketRightsIds"},
|
|
|
|
{32, nullptr, "CountPrepurchaseRecordEx"},
|
|
|
|
{33, nullptr, "ListPrepurchaseRecordRightsIdsEx"},
|
|
|
|
{34, nullptr, "GetEncryptedTicketSize"},
|
|
|
|
{35, nullptr, "GetEncryptedTicketData"},
|
|
|
|
{36, nullptr, "DeleteAllInactiveELicenseRequiredPersonalizedTicket"},
|
|
|
|
{503, nullptr, "GetTitleKey"},
|
2018-07-25 19:30:50 +00:00
|
|
|
};
|
2018-10-19 08:12:26 +00:00
|
|
|
// clang-format on
|
2018-07-25 19:30:50 +00:00
|
|
|
RegisterHandlers(functions);
|
|
|
|
}
|
2019-04-10 17:59:41 +00: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) {
|
|
|
|
IPC::RequestParser rp{ctx};
|
|
|
|
const auto ticket = ctx.ReadBuffer();
|
|
|
|
const auto cert = ctx.ReadBuffer(1);
|
|
|
|
|
|
|
|
if (ticket.size() < sizeof(Core::Crypto::TicketRaw)) {
|
|
|
|
LOG_ERROR(Service_ETicket, "The input buffer is not large enough!");
|
|
|
|
IPC::ResponseBuilder rb{ctx, 2};
|
|
|
|
rb.Push(ERROR_INVALID_ARGUMENT);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Core::Crypto::TicketRaw raw;
|
|
|
|
std::memcpy(raw.data(), ticket.data(), sizeof(Core::Crypto::TicketRaw));
|
|
|
|
|
|
|
|
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 18:00:39 +00: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 18:02:27 +00:00
|
|
|
void CountCommonTicket(Kernel::HLERequestContext& ctx) {
|
|
|
|
LOG_DEBUG(Service_ETicket, "called");
|
|
|
|
|
|
|
|
keys.PopulateTickets();
|
|
|
|
const auto count = keys.GetCommonTickets().size();
|
|
|
|
|
|
|
|
IPC::ResponseBuilder rb{ctx, 3};
|
|
|
|
rb.Push(RESULT_SUCCESS);
|
|
|
|
rb.Push<u32>(count);
|
|
|
|
}
|
|
|
|
|
2019-04-10 18:03:08 +00:00
|
|
|
void CountPersonalizedTicket(Kernel::HLERequestContext& ctx) {
|
|
|
|
LOG_DEBUG(Service_ETicket, "called");
|
|
|
|
|
|
|
|
keys.PopulateTickets();
|
|
|
|
const auto count = keys.GetPersonalizedTickets().size();
|
|
|
|
|
|
|
|
IPC::ResponseBuilder rb{ctx, 3};
|
|
|
|
rb.Push(RESULT_SUCCESS);
|
|
|
|
rb.Push<u32>(count);
|
|
|
|
}
|
|
|
|
|
2019-04-10 18:04:17 +00:00
|
|
|
void ListCommonTicket(Kernel::HLERequestContext& ctx) {
|
|
|
|
u32 out_entries;
|
|
|
|
if (keys.GetCommonTickets().empty())
|
|
|
|
out_entries = 0;
|
|
|
|
else
|
|
|
|
out_entries = ctx.GetWriteBufferSize() / sizeof(u128);
|
|
|
|
|
|
|
|
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; });
|
|
|
|
|
|
|
|
out_entries = std::min<u32>(ids.size(), out_entries);
|
|
|
|
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 18:05:12 +00:00
|
|
|
void ListPersonalizedTicket(Kernel::HLERequestContext& ctx) {
|
|
|
|
u32 out_entries;
|
|
|
|
if (keys.GetPersonalizedTickets().empty())
|
|
|
|
out_entries = 0;
|
|
|
|
else
|
|
|
|
out_entries = ctx.GetWriteBufferSize() / sizeof(u128);
|
|
|
|
|
|
|
|
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; });
|
|
|
|
|
|
|
|
out_entries = std::min<u32>(ids.size(), out_entries);
|
|
|
|
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 18:06:17 +00: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);
|
|
|
|
rb.Push<u64>(ticket.size());
|
|
|
|
}
|
|
|
|
|
2019-04-10 18:07:00 +00: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);
|
|
|
|
rb.Push<u64>(ticket.size());
|
|
|
|
}
|
|
|
|
|
2019-04-10 18:07:49 +00: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);
|
|
|
|
|
|
|
|
const auto write_size = std::min(ticket.size(), ctx.GetWriteBufferSize());
|
|
|
|
ctx.WriteBuffer(ticket.data(), write_size);
|
|
|
|
|
|
|
|
IPC::ResponseBuilder rb{ctx, 4};
|
|
|
|
rb.Push(RESULT_SUCCESS);
|
|
|
|
rb.Push<u64>(write_size);
|
|
|
|
}
|
|
|
|
|
2019-04-10 18:08:19 +00: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);
|
|
|
|
|
|
|
|
const auto write_size = std::min(ticket.size(), ctx.GetWriteBufferSize());
|
|
|
|
ctx.WriteBuffer(ticket.data(), write_size);
|
|
|
|
|
|
|
|
IPC::ResponseBuilder rb{ctx, 4};
|
|
|
|
rb.Push(RESULT_SUCCESS);
|
|
|
|
rb.Push<u64>(write_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
Core::Crypto::KeyManager keys;
|
2018-07-25 19:30:50 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
void InstallInterfaces(SM::ServiceManager& service_manager) {
|
|
|
|
std::make_shared<ETicket>()->InstallAsService(service_manager);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Service::ES
|