Debian Codegraph @

src/decrypters/widevineandroid/WVCdmAdapter.cpp

raw
1 /*
2 * Copyright (C) 2023 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
4 *
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
7 */
8
9 #include "WVCdmAdapter.h"
10
11 #include "WVDecrypter.h"
12 #include "decrypters/Helpers.h"
13 #include "utils/FileUtils.h"
14 #include "utils/log.h"
15
16 #include <jni/src/UUID.h>
17 #include <kodi/Filesystem.h>
18
19 using namespace DRM;
20 using namespace jni;
21
22 CWVCdmAdapterA::CWVCdmAdapterA(WV_KEYSYSTEM ks,
23 std::string_view licenseURL,
24 const std::vector<uint8_t>& serverCert,
25 CJNIMediaDrmOnEventListener* listener,
26 CWVDecrypterA* host)
27 : m_keySystem(ks), m_mediaDrm(0), m_licenseUrl(licenseURL), m_host(host)
28 {
29 if (licenseURL.empty())
30 {
31 LOG::LogF(LOGERROR, "No license URL path specified");
32 return;
33 }
34
35 std::string drmName;
36 if (ks == WIDEVINE)
37 drmName = "widevine";
38 else if (ks == PLAYREADY)
39 drmName = "playready";
40 else if (ks == WISEPLAY)
41 drmName = "wiseplay";
42 else
43 drmName = "undefined";
44
45 // The license url come from license_key kodi property
46 // we have to kept only the url without the parameters specified after pipe "|" char
47 std::string licUrl = m_licenseUrl;
48 const size_t urlPipePos = licUrl.find('|');
49 if (urlPipePos != std::string::npos)
50 licUrl.erase(urlPipePos);
51
52 // Build up a CDM path to store decrypter specific stuff, each domain gets it own path
53 // the domain name is hashed to generate a short folder name
54 std::string basePath = FILESYS::PathCombine(FILESYS::GetAddonUserPath(), drmName);
55 basePath = FILESYS::PathCombine(basePath, DRM::GenerateUrlDomainHash(licUrl));
56 basePath += FILESYS::SEPARATOR;
57 m_strBasePath = basePath;
58
59 int64_t mostSigBits(0), leastSigBits(0);
60 const uint8_t* keySystem = GetKeySystem();
61 for (unsigned int i(0); i < 8; ++i)
62 mostSigBits = (mostSigBits << 8) | keySystem[i];
63 for (unsigned int i(8); i < 16; ++i)
64 leastSigBits = (leastSigBits << 8) | keySystem[i];
65
66 CJNIUUID uuid(mostSigBits, leastSigBits);
67 m_mediaDrm = new CJNIMediaDrm(uuid);
68 if (xbmc_jnienv()->ExceptionCheck() || !*m_mediaDrm)
69 {
70 LOG::LogF(LOGERROR, "Unable to initialize MediaDrm");
71 xbmc_jnienv()->ExceptionClear();
72 delete m_mediaDrm, m_mediaDrm = nullptr;
73 return;
74 }
75
76 m_mediaDrm->setOnEventListener(*listener);
77 if (xbmc_jnienv()->ExceptionCheck())
78 {
79 LOG::LogF(LOGERROR, "Exception during installation of EventListener");
80 xbmc_jnienv()->ExceptionClear();
81 m_mediaDrm->release();
82 delete m_mediaDrm, m_mediaDrm = nullptr;
83 return;
84 }
85
86 std::vector<uint8_t> strDeviceId = m_mediaDrm->getPropertyByteArray("deviceUniqueId");
87 xbmc_jnienv()->ExceptionClear();
88 std::string strSecurityLevel = m_mediaDrm->getPropertyString("securityLevel");
89 xbmc_jnienv()->ExceptionClear();
90 std::string strSystemId = m_mediaDrm->getPropertyString("systemId");
91 xbmc_jnienv()->ExceptionClear();
92
93
94 if (m_keySystem == WIDEVINE)
95 {
96 //m_mediaDrm->setPropertyString("sessionSharing", "enable");
97 if (!serverCert.empty())
98 {
99 m_mediaDrm->setPropertyByteArray("serviceCertificate", serverCert);
100 }
101 else
102 LoadServiceCertificate();
103
104 if (xbmc_jnienv()->ExceptionCheck())
105 {
106 LOG::LogF(LOGERROR, "Exception setting Service Certificate");
107 xbmc_jnienv()->ExceptionClear();
108 m_mediaDrm->release();
109 delete m_mediaDrm, m_mediaDrm = nullptr;
110 return;
111 }
112 }
113
114 LOG::Log(LOGDEBUG,
115 "MediaDrm initialized (Device unique ID size: %zu, System ID: %s, Security level: %s)",
116 strDeviceId.size(), strSystemId.c_str(), strSecurityLevel.c_str());
117
118 if (m_licenseUrl.find('|') == std::string::npos)
119 {
120 if (m_keySystem == WIDEVINE)
121 m_licenseUrl += "|Content-Type=application%2Foctet-stream|R{SSM}|";
122 else if (m_keySystem == PLAYREADY)
123 m_licenseUrl += "|Content-Type=text%2Fxml&SOAPAction=http%3A%2F%2Fschemas.microsoft.com%"
124 "2FDRM%2F2007%2F03%2Fprotocols%2FAcquireLicense|R{SSM}|";
125 else
126 m_licenseUrl += "|Content-Type=application/json|R{SSM}|";
127 }
128 }
129
130 CWVCdmAdapterA::~CWVCdmAdapterA()
131 {
132 if (m_mediaDrm)
133 {
134 m_mediaDrm->release();
135 if (xbmc_jnienv()->ExceptionCheck())
136 {
137 LOG::LogF(LOGERROR, "Exception releasing media drm");
138 xbmc_jnienv()->ExceptionClear();
139 }
140 delete m_mediaDrm;
141 m_mediaDrm = nullptr;
142 }
143 }
144
145 void CWVCdmAdapterA::LoadServiceCertificate()
146 {
147 std::string filename = m_strBasePath + "service_certificate";
148 uint8_t* data(nullptr);
149 size_t sz(0);
150 FILE* f = fopen(filename.c_str(), "rb");
151
152 if (f)
153 {
154 fseek(f, 0L, SEEK_END);
155 sz = ftell(f);
156 fseek(f, 0L, SEEK_SET);
157 if (sz > 8 && (data = (uint8_t*)malloc(sz)))
158 fread(data, 1, sz, f);
159 fclose(f);
160 }
161 if (data)
162 {
163 auto now = std::chrono::system_clock::now();
164 uint64_t certTime = *((uint64_t*)data),
165 nowTime =
166 std::chrono::time_point_cast<std::chrono::seconds>(now).time_since_epoch().count();
167
168 if (certTime < nowTime && nowTime - certTime < 86400)
169 m_mediaDrm->setPropertyByteArray("serviceCertificate",
170 std::vector<uint8_t>(data + 8, data + sz));
171 else
172 free(data), data = nullptr;
173 }
174 if (!data)
175 {
176 LOG::Log(LOGDEBUG, "Requesting new Service Certificate");
177 m_mediaDrm->setPropertyString("privacyMode", "enable");
178 }
179 else
180 {
181 LOG::Log(LOGDEBUG, "Use stored Service Certificate");
182 free(data), data = nullptr;
183 }
184 }
185
186 void CWVCdmAdapterA::SaveServiceCertificate()
187 {
188 const std::vector<uint8_t> sc = m_mediaDrm->getPropertyByteArray("serviceCertificate");
189 if (xbmc_jnienv()->ExceptionCheck())
190 {
191 LOG::LogF(LOGWARNING, "Exception retrieving Service Certificate");
192 xbmc_jnienv()->ExceptionClear();
193 return;
194 }
195
196 if (sc.empty())
197 {
198 LOG::LogF(LOGWARNING, "Empty Service Certificate");
199 return;
200 }
201
202 std::string filename = m_strBasePath + "service_certificate";
203 FILE* f = fopen(filename.c_str(), "wb");
204 if (f)
205 {
206 auto now = std::chrono::system_clock::now();
207 uint64_t nowTime =
208 std::chrono::time_point_cast<std::chrono::seconds>(now).time_since_epoch().count();
209 fwrite((uint8_t*)&nowTime, 1, sizeof(uint64_t), f);
210 fwrite(sc.data(), 1, sc.size(), f);
211 fclose(f);
212 }
213 }