-
Notifications
You must be signed in to change notification settings - Fork 258
Expand file tree
/
Copy pathWVCdmAdapter.cpp
More file actions
349 lines (305 loc) · 11.1 KB
/
WVCdmAdapter.cpp
File metadata and controls
349 lines (305 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/*
* Copyright (C) 2023 Team Kodi
* This file is part of Kodi - https://kodi.tv
*
* SPDX-License-Identifier: GPL-2.0-or-later
* See LICENSES/README.md for more information.
*/
#include "WVCdmAdapter.h"
#include "WVDecrypter.h"
#include "decrypters/HelperWv.h"
#include "decrypters/Helpers.h"
#include "utils/StringUtils.h"
#include "utils/FileUtils.h"
#include "utils/log.h"
#include <androidjni/UUID.h>
using namespace DRM;
using namespace UTILS;
CMediaDrmOnEventListener::CMediaDrmOnEventListener(
CMediaDrmOnEventCallback* decrypterEventCallback,
std::shared_ptr<CJNIClassLoader> classLoader)
: CJNIMediaDrmOnEventListener(*classLoader)
{
m_decrypterEventCallback = decrypterEventCallback;
}
void CMediaDrmOnEventListener::onEvent(const CJNIMediaDrm& mediaDrm,
const std::vector<char>& sessionId,
int event,
int extra,
const std::vector<char>& data)
{
m_decrypterEventCallback->OnMediaDrmEvent(mediaDrm, sessionId, event, extra, data);
}
CMediaDrmOnKeyStatusChangeListener::CMediaDrmOnKeyStatusChangeListener(
CMediaDrmOnKeyStatusChangeCallback* decrypterOnKeyStatusChangeCallback,
std::shared_ptr<CJNIClassLoader> classLoader)
: CJNIMediaDrmOnKeyStatusChangeListener(*classLoader)
{
m_decrypterOnKeyStatusChangeCallback = decrypterOnKeyStatusChangeCallback;
}
void CMediaDrmOnKeyStatusChangeListener::onKeyStatusChange(
const CJNIMediaDrm& mediaDrm,
const std::vector<char>& sessionId,
const CJNIList<CJNIMediaDrmKeyStatus>& keyInformation,
bool hasNewUsableKey)
{
m_decrypterOnKeyStatusChangeCallback->OnKeyStatusChange(mediaDrm, sessionId, keyInformation,
hasNewUsableKey);
}
CWVCdmAdapterA::CWVCdmAdapterA(std::string_view keySystem,
const DRM::Config& config,
std::shared_ptr<CJNIClassLoader> jniClassLoader,
CWVDecrypterA* host)
: m_keySystem(keySystem), m_config(config), m_host(host)
{
// The license url come from license_key kodi property
// we have to kept only the url without the parameters specified after pipe "|" char
std::string licUrl = m_config.license.serverUri;
const size_t urlPipePos = licUrl.find('|');
if (urlPipePos != std::string::npos)
licUrl.erase(urlPipePos);
// Build up a CDM path to store decrypter specific stuff, each domain gets it own path
// the domain name is hashed to generate a short folder name
std::string drmName = DRM::KeySystemToDrmName(m_keySystem);
std::string basePath = FILESYS::PathCombine(FILESYS::GetAddonUserPath(), drmName);
basePath = FILESYS::PathCombine(basePath, DRM::GenerateUrlDomainHash(licUrl));
basePath += FILESYS::SEPARATOR;
m_strBasePath = basePath;
int64_t mostSigBits(0), leastSigBits(0);
const uint8_t* systemUuid = DRM::KeySystemToUUID(m_keySystem);
if (!systemUuid)
{
LOG::LogF(LOGERROR, "Unable to get the system UUID");
return;
}
for (unsigned int i(0); i < 8; ++i)
mostSigBits = (mostSigBits << 8) | systemUuid[i];
for (unsigned int i(8); i < 16; ++i)
leastSigBits = (leastSigBits << 8) | systemUuid[i];
CJNIUUID uuid(mostSigBits, leastSigBits);
m_cdmAdapter = std::make_shared<CJNIMediaDrm>(uuid);
if (xbmc_jnienv()->ExceptionCheck() || !*m_cdmAdapter.get())
{
LOG::LogF(LOGERROR, "Unable to initialize MediaDrm");
xbmc_jnienv()->ExceptionClear();
return;
}
// Create media drm EventListener (unique_ptr explanation on class comment)
m_mediaDrmEventListener = std::make_unique<CMediaDrmOnEventListener>(this, jniClassLoader);
m_cdmAdapter->setOnEventListener(*m_mediaDrmEventListener.get());
if (xbmc_jnienv()->ExceptionCheck())
{
xbmc_jnienv()->ExceptionClear();
LOG::LogF(LOGERROR, "Failed to create CMediaDrmOnEventListener");
m_cdmAdapter->release();
return;
}
// Create media drm OnKeyStatusChangeListener (unique_ptr explanation on class comment)
m_mediaDrmOnKeyChangeListener =
std::make_unique<CMediaDrmOnKeyStatusChangeListener>(this, jniClassLoader);
CJNIMediaDrmKeyStatus::PopulateStaticFields(); // should be done by CJNIContext but currently not implemented for add-ons case
m_cdmAdapter->setOnKeyStatusChangeListener(*m_mediaDrmOnKeyChangeListener.get());
if (xbmc_jnienv()->ExceptionCheck())
{
xbmc_jnienv()->ExceptionClear();
LOG::LogF(LOGERROR, "Failed to create CMediaDrmOnKeyStatusChangeListener");
m_cdmAdapter->release();
return;
}
std::vector<uint8_t> strDeviceId = m_cdmAdapter->getPropertyByteArray("deviceUniqueId");
xbmc_jnienv()->ExceptionClear();
std::string strSecurityLevel = m_cdmAdapter->getPropertyString("securityLevel");
xbmc_jnienv()->ExceptionClear();
std::string strSystemId = m_cdmAdapter->getPropertyString("systemId");
xbmc_jnienv()->ExceptionClear();
if (m_keySystem == DRM::KS_WIDEVINE)
{
//m_cdmAdapter->setPropertyString("sessionSharing", "enable");
if (!m_config.license.serverCert.empty())
{
m_cdmAdapter->setPropertyByteArray("serviceCertificate", m_config.license.serverCert);
}
else
LoadServiceCertificate();
if (xbmc_jnienv()->ExceptionCheck())
{
LOG::LogF(LOGERROR, "Exception setting Service Certificate");
xbmc_jnienv()->ExceptionClear();
m_cdmAdapter->release();
return;
}
}
LOG::Log(LOGDEBUG,
"MediaDrm initialized (Device unique ID size: %zu, System ID: %s, Security level: %s)",
strDeviceId.size(), strSystemId.c_str(), strSecurityLevel.c_str());
}
CWVCdmAdapterA::~CWVCdmAdapterA()
{
if (m_cdmAdapter)
{
m_cdmAdapter->release();
if (xbmc_jnienv()->ExceptionCheck())
{
LOG::LogF(LOGERROR, "Exception releasing media drm");
xbmc_jnienv()->ExceptionClear();
}
}
}
void CWVCdmAdapterA::LoadServiceCertificate()
{
std::string filename = m_strBasePath + "service_certificate";
uint8_t* data(nullptr);
size_t sz(0);
FILE* f = fopen(filename.c_str(), "rb");
if (f)
{
fseek(f, 0L, SEEK_END);
sz = ftell(f);
fseek(f, 0L, SEEK_SET);
if (sz > 8 && (data = (uint8_t*)malloc(sz)))
fread(data, 1, sz, f);
fclose(f);
}
if (data)
{
auto now = std::chrono::system_clock::now();
uint64_t certTime = *((uint64_t*)data),
nowTime =
std::chrono::time_point_cast<std::chrono::seconds>(now).time_since_epoch().count();
if (certTime < nowTime && nowTime - certTime < 86400)
m_cdmAdapter->setPropertyByteArray("serviceCertificate",
std::vector<uint8_t>(data + 8, data + sz));
else
free(data), data = nullptr;
}
if (!data)
{
LOG::Log(LOGDEBUG, "Requesting new Service Certificate");
m_cdmAdapter->setPropertyString("privacyMode", "enable");
}
else
{
LOG::Log(LOGDEBUG, "Use stored Service Certificate");
free(data), data = nullptr;
}
}
void CWVCdmAdapterA::OnMediaDrmEvent(const CJNIMediaDrm& mediaDrm,
const std::vector<char>& sessionId,
int event,
int extra,
const std::vector<char>& data)
{
LOG::Log(LOGDEBUG, "MediaDrm event: type %i arrived", event);
CdmMessageType type;
if (event == CJNIMediaDrm::EVENT_KEY_REQUIRED)
type = CdmMessageType::EVENT_KEY_REQUIRED;
else
return;
CdmMessage cdmMsg;
cdmMsg.sessionId.assign(sessionId.data(), sessionId.data() + sessionId.size());
cdmMsg.type = type;
cdmMsg.data.assign(data.data(), data.data() + data.size());
cdmMsg.status = extra;
// Send the message to attached CWVCencSingleSampleDecrypterA instances
NotifyObservers(cdmMsg);
}
void CWVCdmAdapterA::OnKeyStatusChange(const CJNIMediaDrm& mediaDrm,
const std::vector<char>& sessionId,
const CJNIList<CJNIMediaDrmKeyStatus>& keyInformation,
bool hasNewUsableKey)
{
std::vector<DRM::KeyInfo> adpKeysInfo;
for (int i = 0; i < keyInformation.size(); ++i)
{
CJNIMediaDrmKeyStatus keyInfo = keyInformation.get(i);
DRM::KeyInfo adpKeyInfo;
adpKeyInfo.kid = keyInfo.getKeyId();
const int statusCode = keyInfo.getStatusCode();
if (statusCode == CJNIMediaDrmKeyStatus::STATUS_USABLE)
adpKeyInfo.status = KeyStatus::USABLE;
else if (statusCode == CJNIMediaDrmKeyStatus::STATUS_EXPIRED)
adpKeyInfo.status = KeyStatus::EXPIRED;
else if (statusCode == CJNIMediaDrmKeyStatus::STATUS_OUTPUT_NOT_ALLOWED)
adpKeyInfo.status = KeyStatus::OUTPUT_NOT_ALLOWED;
else if (statusCode == CJNIMediaDrmKeyStatus::STATUS_PENDING)
adpKeyInfo.status = KeyStatus::PENDING;
else if (statusCode == CJNIMediaDrmKeyStatus::STATUS_INTERNAL_ERROR)
adpKeyInfo.status = KeyStatus::INTERNAL_ERROR;
else if (statusCode == CJNIMediaDrmKeyStatus::STATUS_USABLE_IN_FUTURE)
adpKeyInfo.status = KeyStatus::USABLE_IN_FUTURE;
else
{
LOG::LogF(LOGERROR, "Unhandled getStatusCode %i, KID ignored", statusCode);
continue;
}
adpKeysInfo.emplace_back(adpKeyInfo);
}
NotifyOnKeyStatusChange({sessionId.cbegin(), sessionId.cend()}, adpKeysInfo);
}
void CWVCdmAdapterA::SaveServiceCertificate()
{
const std::vector<uint8_t> sc = m_cdmAdapter->getPropertyByteArray("serviceCertificate");
if (xbmc_jnienv()->ExceptionCheck())
{
LOG::LogF(LOGWARNING, "Exception retrieving Service Certificate");
xbmc_jnienv()->ExceptionClear();
return;
}
if (sc.empty())
{
LOG::LogF(LOGWARNING, "Empty Service Certificate");
return;
}
std::string filename = m_strBasePath + "service_certificate";
FILE* f = fopen(filename.c_str(), "wb");
if (f)
{
auto now = std::chrono::system_clock::now();
uint64_t nowTime =
std::chrono::time_point_cast<std::chrono::seconds>(now).time_since_epoch().count();
fwrite((uint8_t*)&nowTime, 1, sizeof(uint64_t), f);
fwrite(sc.data(), 1, sc.size(), f);
fclose(f);
}
}
const DRM::Config& CWVCdmAdapterA::GetConfig()
{
return m_config;
}
std::string_view CWVCdmAdapterA::GetKeySystem()
{
return m_keySystem;
}
std::string_view CWVCdmAdapterA::GetLibraryPath() const
{
return m_host->GetLibraryPath();
}
void CWVCdmAdapterA::AttachObserver(IWVObserver* observer)
{
std::lock_guard<std::mutex> lock(m_observer_mutex);
m_observers.emplace_back(observer);
}
void CWVCdmAdapterA::DetachObserver(IWVObserver* observer)
{
std::lock_guard<std::mutex> lock(m_observer_mutex);
m_observers.remove(observer);
}
void CWVCdmAdapterA::NotifyObservers(const CdmMessage& message)
{
std::lock_guard<std::mutex> lock(m_observer_mutex);
for (IWVObserver* observer : m_observers)
{
if (observer)
observer->OnNotify(message);
}
}
void CWVCdmAdapterA::NotifyOnKeyStatusChange(const std::string& sessionId,
const std::vector<DRM::KeyInfo>& keysInfo)
{
std::lock_guard<std::mutex> lock(m_observer_mutex);
for (IWVObserver* observer : m_observers)
{
if (observer)
observer->OnKeyStatusChangeNotify(sessionId, keysInfo);
}
}