-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMediaSession.cpp
More file actions
422 lines (345 loc) · 12.3 KB
/
Copy pathMediaSession.cpp
File metadata and controls
422 lines (345 loc) · 12.3 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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/*
* Copyright 2014 Fraunhofer FOKUS
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "MediaSession.h"
#include <assert.h>
#include <iostream>
#ifdef OPTEE_AES128
#include <aes_crypto.h>
#else
#include <openssl/aes.h>
#include <openssl/evp.h>
#include <openssl/modes.h>
#include <openssl/opensslv.h>
#endif
#include <pthread.h>
#include <sstream>
#include <string>
#include <string.h>
#include <core/core.h>
#ifdef OPTEE_AES128
#define AES_BLOCK_SIZE CTR_AES_BLOCK_SIZE
#endif
#define DESTINATION_URL_PLACEHOLDER "http://no-valid-license-server"
#define NYI_KEYSYSTEM "keysystem-placeholder"
#define K_DECRYPTION_KEY_SIZE 16
using namespace std;
using namespace WPEFramework;
namespace CDMi {
uint32_t MediaKeySession::s_sessionCnt = 10;
#ifdef OPTEE_AES128
static uint16_t tee_session_init = 0;
#endif
const char* MediaKeySession::CreateSessionId() {
stringstream strs;
strs << s_sessionCnt;
char *buffer = new char[strs.str().length()]();
strcpy(buffer, strs.str().c_str());
s_sessionCnt++;
return const_cast<char*>(buffer);
}
MediaKeySession::MediaKeySession(const uint8_t *f_pbInitData, uint32_t f_cbInitData) {
m_sessionId = MediaKeySession::CreateSessionId();
cout << "creating mediakeysession with id: " << m_sessionId << endl;
std::string initData(reinterpret_cast<const char*>(f_pbInitData), f_cbInitData);
std::string clearKeyInitData;
if (!ParseClearKeyInitializationData(initData, &clearKeyInitData))
clearKeyInitData = initData;
#ifdef OPTEE_AES128
else {
if(!tee_session_init) {
TEE_crypto_init();
std::cout << "ClearKey CDMi: TEE initialized." << std::endl;
}
tee_session_init++;
}
#endif
}
MediaKeySession::~MediaKeySession() {
#ifdef OPTEE_AES128
tee_session_init--;
if(tee_session_init == 0)
TEE_crypto_close();
#endif
}
void MediaKeySession::Run(const IMediaKeySessionCallback *f_piMediaKeySessionCallback) {
int ret;
pthread_t thread;
cout << "#mediakeysession.Run" << endl;
if (f_piMediaKeySessionCallback) {
m_piCallback = const_cast<IMediaKeySessionCallback*>(f_piMediaKeySessionCallback);
ret = pthread_create(&thread, nullptr, MediaKeySession::_CallRunThread, this);
if (ret == 0)
pthread_detach(thread);
else {
cout << "#mediakeysession.Run: err: could not create thread" << endl;
return;
}
} else
cout << "#mediakeysession.Run: err: MediaKeySessionCallback NULL?" << endl;
}
void* MediaKeySession::_CallRunThread(void *arg) {
return ((MediaKeySession*)arg)->RunThread(1);
}
void* MediaKeySession::_CallRunThread2(void *arg) {
return ((MediaKeySession*)arg)->RunThread(2);
}
void* MediaKeySession::RunThread(int f_i) {
cout << "#mediakeysession._RunThread" << endl;
if (f_i == 1) {
std::string message = KeyIdsToJSON();
m_piCallback->OnKeyMessage((const uint8_t*)message.c_str(), message.size(), const_cast<char*>(DESTINATION_URL_PLACEHOLDER));
}
return (nullptr);
}
CDMi_RESULT MediaKeySession::Load(void) {
return CDMi_S_FALSE;
}
void MediaKeySession::Update(
const uint8_t *f_pbKeyMessageResponse,
uint32_t f_cbKeyMessageResponse) {
int ret;
pthread_t thread;
cout << "#mediakeysession.Run" << endl;
std::string keyString(reinterpret_cast<const char*>(f_pbKeyMessageResponse), f_cbKeyMessageResponse);
// Session type is set to "0". We keep the function signature to
// match Chromium's ExtractKeysFromJWKSet(...) function
media::ExtractKeysFromJWKSet(keyString, m_keys, 0);
ret = pthread_create(&thread, NULL, MediaKeySession::_CallRunThread2, this);
if (!ret) {
pthread_detach(thread);
} else {
cout << "#mediakeysession.Run: err: could not create thread" << endl;
return;
}
if (m_piCallback) {
for (auto& keyIdWithKey : m_keys)
m_piCallback->OnKeyStatusUpdate("KeyUsable", reinterpret_cast<const uint8_t*>(keyIdWithKey.first.c_str()), keyIdWithKey.first.size());
m_piCallback->OnKeyStatusesUpdated();
}
}
CDMi_RESULT MediaKeySession::Remove(void) {
return CDMi_S_FALSE;
}
CDMi_RESULT MediaKeySession::Close(void) {
return CDMi_SUCCESS;
}
const char* MediaKeySession::GetSessionId(void) const {
cout <<"Inside GetSessionId"<< endl;
return m_sessionId;
}
const char* MediaKeySession::GetKeySystem(void) const {
// FIXME:(fhg):
return NYI_KEYSYSTEM;
}
CDMi_RESULT MediaKeySession::Decrypt(
const uint8_t *f_pbSessionKey,
uint32_t f_cbSessionKey,
const uint32_t *f_pdwSubSampleMapping,
uint32_t f_cdwSubSampleMapping,
const uint8_t *f_pbIV,
uint32_t f_cbIV,
const uint8_t *f_pbData,
uint32_t f_cbData,
uint32_t *f_pcbOpaqueClearContent,
uint8_t **f_ppbOpaqueClearContent,
const uint8_t keyIdLength,
const uint8_t* keyId,
bool initWithLast15) {
std::string kid((const char*)keyId, keyIdLength);
uint8_t *out; /* Faked secure buffer */
const char *key;
uint8_t ivec[AES_BLOCK_SIZE] = { 0 };
uint8_t ecount_buf[AES_BLOCK_SIZE] = { 0 };
unsigned int block_offset = 0;
assert(f_cbIV <= AES_BLOCK_SIZE);
if (!f_pcbOpaqueClearContent) {
cout << "ERROR: f_pcbOpaqueClearContent is NULL" << endl;
return CDMi_S_FALSE;
}
if (m_keys.size() != 1)
cout << "FIXME: We support only one key at the moment. Number keys: " << m_keys.size()<< endl;
if (m_keys[kid].size() != K_DECRYPTION_KEY_SIZE) {
cout << "ERROR: Wrong key size" << endl;
return CDMi_S_FALSE;
}
/* complete all validation and create memory for out data */
out = (uint8_t*) malloc(f_cbData * sizeof(uint8_t));
key = m_keys[kid].c_str();
memcpy(&(ivec[0]), f_pbIV, f_cbIV);
#ifdef OPTEE_AES128
int result = TEE_AES_ctr128_encrypt(f_pbData, out, f_cbData, key, ivec, ecount_buf, &block_offset, 0, false/*secure on/off*/);
if(result != CDMi_SUCCESS) {
std::cout << "ClearKey CDMi: Failure: On Decryption. result = " << result << std::endl;
goto fail;
}
#else
AES_KEY aesKey;
AES_set_encrypt_key(reinterpret_cast<const unsigned char*>(key), strlen(key) * 8, &aesKey);
#if OPENSSL_VERSION_NUMBER > 0x10100000L
CRYPTO_ctr128_encrypt(reinterpret_cast<const unsigned char*>(f_pbData), out, f_cbData, &aesKey, ivec, ecount_buf, &block_offset, (block128_f)AES_encrypt);
#else
AES_ctr128_encrypt(reinterpret_cast<const unsigned char*>(f_pbData), out, f_cbData, &aesKey, ivec, ecount_buf, &block_offset);
#endif
#endif
/* Return clear content */
*f_pcbOpaqueClearContent = f_cbData;
*f_ppbOpaqueClearContent = out;
return CDMi_SUCCESS;
fail:
free(out);
return CDMi_S_FALSE;
}
CDMi_RESULT MediaKeySession::ReleaseClearContent(
const uint8_t *f_pbSessionKey,
uint32_t f_cbSessionKey,
const uint32_t f_cbClearContentOpaque,
uint8_t *f_pbClearContentOpaque ){
free(f_pbClearContentOpaque);
return CDMi_SUCCESS;
}
std::string MediaKeySession::KeyIdsToJSON() {
/* FIXME: This JSON consturctor is for proof of concept only.
* We need to add a proper JSON library.
*/
ostringstream result;
std::string sep = "";
result << "{\"" << media::kKeyIdsTag << "\" : [";
for (auto& kid : m_kids) {
result << sep << "\"" << media::Base64Encode(kid.c_str(), kid.size()) << "\"";
sep = ",";
}
result << "],\"" << media::kTypeTag << "\":\"" << media::kTemporarySession << "\"}";
return result.str();
}
bool MediaKeySession::ParseClearKeyInitializationData(const std::string& initData, std::string* output)
{
bool result = false;
const char identifier[] = { '"', 'k', 'i', 'd', 's', '"', ':', '\0' };
/* keyids type */
if(initData.find(identifier) != std::string::npos) {
result = ParseKeyIdsInitData(initData, output) > 0 ? true : false;
}
else {
result = ParseCENCInitData(initData, output) > 0 ? true : false;
}
return result;
}
using JSONStringArray = Core::JSON::ArrayType<Core::JSON::String>;
bool MediaKeySession::ParseKeyIdsInitData(const std::string& initData, std::string* output) {
class InitData : public Core::JSON::Container {
public:
InitData() : Core::JSON::Container() , KeyIds() {
Add(_T("kids"), &KeyIds);
}
virtual ~InitData() {
}
public:
JSONStringArray KeyIds;
} jsonData;
output->clear();
jsonData.FromString(initData);
JSONStringArray::ConstIterator index(static_cast<const InitData&>(jsonData).KeyIds.Elements());
TRACE_L1("Clearkey CDMi: initdata keyid(s):");
while (index.Next() == true) {
TRACE_L1("%s", index.Current().Value().c_str());
std::string keyId = media::Base64Decode(index.Current().Value());
m_kids.insert(keyId);
}
return m_kids.size() > 0 ? true : false;
}
bool MediaKeySession::ParseCENCInitData(const std::string& initData, std::string* output)
{
BufferReader input(reinterpret_cast<const uint8_t*>(initData.data()), initData.length());
static const uint8_t clearKeySystemId[] = {
0x10, 0x77, 0xef, 0xec, 0xc0, 0xb2, 0x4d, 0x02,
0xac, 0xe3, 0x3c, 0x1e, 0x52, 0xe2, 0xfb, 0x4b,
};
// one PSSH box consists of:
// 4 byte size of the atom, inclusive. (0 means the rest of the buffer.)
// 4 byte atom type, "pssh".
// (optional, if size == 1) 8 byte size of the atom, inclusive.
// 1 byte version, value 0 or 1. (skip if larger.)
// 3 byte flags, value 0. (ignored.)
// 16 byte system id.
// (optional, if version == 1) 4 byte key ID count. (K)
// (optional, if version == 1) K * 16 byte key ID.
// 4 byte size of PSSH data, exclusive. (N)
// N byte PSSH data.
while (!input.IsEOF()) {
size_t startPosition = input.pos();
// The atom size, used for skipping.
uint64_t atomSize;
if (!input.Read4Into8(&atomSize))
return false;
std::vector<uint8_t> atomType;
if (!input.ReadVec(&atomType, 4))
return false;
if (atomSize == 1) {
if (!input.Read8(&atomSize))
return false;
} else if (atomSize == 0)
atomSize = input.size() - startPosition;
if (memcmp(&atomType[0], "pssh", 4)) {
if (!input.SkipBytes(atomSize - (input.pos() - startPosition)))
return false;
continue;
}
uint8_t version;
if (!input.Read1(&version))
return false;
if (version > 1) {
if (!input.SkipBytes(atomSize - (input.pos() - startPosition)))
return false;
continue;
}
// flags
if (!input.SkipBytes(3))
return false;
// system id
std::vector<uint8_t> systemId;
if (!input.ReadVec(&systemId, sizeof(clearKeySystemId)))
return false;
if (memcmp(&systemId[0], clearKeySystemId, sizeof(clearKeySystemId))) {
// skip non-Playready PSSH boxes.
if (!input.SkipBytes(atomSize - (input.pos() - startPosition)))
return false;
continue;
}
if (version == 1) {
// v1 has additional fields for key IDs. We can skip them.
uint32_t numKeyIds;
if (!input.Read4(&numKeyIds))
return false;
for (uint32_t i = 0; i < numKeyIds; i++) {
std::string keyId;
if (!input.ReadString(&keyId, 16))
return false;
m_kids.insert(keyId);
}
}
// size of PSSH data
uint32_t dataLength;
if (!input.Read4(&dataLength))
return false;
output->clear();
if (!input.ReadString(output, dataLength))
return false;
return true;
}
// we did not find a matching record
return false;
}
} // namespace CDMi