From 475a7a4446b169b46d4fb34f3b022f7b369e5bf9 Mon Sep 17 00:00:00 2001
From: Zach Hilman <zachhilman@gmail.com>
Date: Wed, 10 Apr 2019 14:00:39 -0400
Subject: [PATCH] es: Implement ETicket GetTitleKey (8)

Takes a rights ID as input and returns the associated title key, if it exists.
---
 src/core/hle/service/es/es.cpp | 28 +++++++++++++++++++++++++++-
 1 file changed, 27 insertions(+), 1 deletion(-)

diff --git a/src/core/hle/service/es/es.cpp b/src/core/hle/service/es/es.cpp
index 787927be08..65dfaa2a00 100644
--- a/src/core/hle/service/es/es.cpp
+++ b/src/core/hle/service/es/es.cpp
@@ -23,7 +23,7 @@ public:
             {5, nullptr, "DeleteAllCommonTicket"},
             {6, nullptr, "DeleteAllPersonalizedTicket"},
             {7, nullptr, "DeleteAllPersonalizedTicketEx"},
-            {8, nullptr, "GetTitleKey"},
+            {8, &ETicket::GetTitleKey, "GetTitleKey"},
             {9, nullptr, "CountCommonTicket"},
             {10, nullptr, "CountPersonalizedTicket"},
             {11, nullptr, "ListCommonTicket"},
@@ -96,6 +96,32 @@ private:
         rb.Push(RESULT_SUCCESS);
     }
 
+    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);
+    }
+
 };
 
 void InstallInterfaces(SM::ServiceManager& service_manager) {
-- 
GitLab