forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebcontentdecryptionmodulesession_impl.cc
529 lines (462 loc) · 20.5 KB
/
webcontentdecryptionmodulesession_impl.cc
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "webcontentdecryptionmodulesession_impl.h"
#include <memory>
#include "base/bind.h"
#include "base/callback_helpers.h"
#include "base/logging.h"
#include "base/metrics/histogram_functions.h"
#include "base/numerics/safe_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "media/base/cdm_key_information.h"
#include "media/base/cdm_promise.h"
#include "media/base/content_decryption_module.h"
#include "media/base/key_system_names.h"
#include "media/base/key_systems.h"
#include "media/base/limits.h"
#include "media/blink/cdm_result_promise.h"
#include "media/blink/cdm_result_promise_helper.h"
#include "media/blink/cdm_session_adapter.h"
#include "media/blink/webmediaplayer_util.h"
#include "media/cdm/cenc_utils.h"
#include "media/cdm/json_web_key.h"
#include "third_party/blink/public/platform/web_data.h"
#include "third_party/blink/public/platform/web_encrypted_media_key_information.h"
#include "third_party/blink/public/platform/web_string.h"
#include "third_party/blink/public/platform/web_url.h"
#include "third_party/blink/public/platform/web_vector.h"
namespace media {
namespace {
const char kCloseSessionUMAName[] = "CloseSession";
const char kGenerateRequestUMAName[] = "GenerateRequest";
const char kLoadSessionUMAName[] = "LoadSession";
const char kRemoveSessionUMAName[] = "RemoveSession";
const char kUpdateSessionUMAName[] = "UpdateSession";
const char kKeyStatusSystemCodeUMAName[] = "KeyStatusSystemCode";
blink::WebContentDecryptionModuleSession::Client::MessageType
convertMessageType(CdmMessageType message_type) {
switch (message_type) {
case CdmMessageType::LICENSE_REQUEST:
return blink::WebContentDecryptionModuleSession::Client::MessageType::
kLicenseRequest;
case CdmMessageType::LICENSE_RENEWAL:
return blink::WebContentDecryptionModuleSession::Client::MessageType::
kLicenseRenewal;
case CdmMessageType::LICENSE_RELEASE:
return blink::WebContentDecryptionModuleSession::Client::MessageType::
kLicenseRelease;
case CdmMessageType::INDIVIDUALIZATION_REQUEST:
return blink::WebContentDecryptionModuleSession::Client::MessageType::
kIndividualizationRequest;
}
NOTREACHED();
return blink::WebContentDecryptionModuleSession::Client::MessageType::
kLicenseRequest;
}
CdmSessionType convertSessionType(
blink::WebEncryptedMediaSessionType session_type) {
switch (session_type) {
case blink::WebEncryptedMediaSessionType::kTemporary:
return CdmSessionType::TEMPORARY_SESSION;
case blink::WebEncryptedMediaSessionType::kPersistentLicense:
return CdmSessionType::PERSISTENT_LICENSE_SESSION;
case blink::WebEncryptedMediaSessionType::kPersistentReleaseMessage:
return CdmSessionType::PERSISTENT_RELEASE_MESSAGE_SESSION;
case blink::WebEncryptedMediaSessionType::kUnknown:
break;
}
NOTREACHED();
return CdmSessionType::TEMPORARY_SESSION;
}
bool SanitizeInitData(EmeInitDataType init_data_type,
const unsigned char* init_data,
size_t init_data_length,
std::vector<uint8_t>* sanitized_init_data,
std::string* error_message) {
DCHECK_GT(init_data_length, 0u);
if (init_data_length > limits::kMaxInitDataLength) {
error_message->assign("Initialization data too long.");
return false;
}
switch (init_data_type) {
case EmeInitDataType::WEBM:
// |init_data| for WebM is a single key.
if (init_data_length > limits::kMaxKeyIdLength) {
error_message->assign("Initialization data for WebM is too long.");
return false;
}
sanitized_init_data->assign(init_data, init_data + init_data_length);
return true;
case EmeInitDataType::CENC:
sanitized_init_data->assign(init_data, init_data + init_data_length);
if (!ValidatePsshInput(*sanitized_init_data)) {
error_message->assign("Initialization data for CENC is incorrect.");
return false;
}
return true;
case EmeInitDataType::KEYIDS: {
// Extract the keys and then rebuild the message. This ensures that any
// extra data in the provided JSON is dropped.
std::string init_data_string(init_data, init_data + init_data_length);
KeyIdList key_ids;
if (!ExtractKeyIdsFromKeyIdsInitData(init_data_string, &key_ids,
error_message))
return false;
for (const auto& key_id : key_ids) {
if (key_id.size() < limits::kMinKeyIdLength ||
key_id.size() > limits::kMaxKeyIdLength) {
error_message->assign("Incorrect key size.");
return false;
}
}
CreateKeyIdsInitData(key_ids, sanitized_init_data);
return true;
}
case EmeInitDataType::UNKNOWN:
break;
}
NOTREACHED();
error_message->assign("Initialization data type is not supported.");
return false;
}
bool SanitizeSessionId(const blink::WebString& session_id,
std::string* sanitized_session_id) {
// The user agent should thoroughly validate the sessionId value before
// passing it to the CDM. At a minimum, this should include checking that
// the length and value (e.g. alphanumeric) are reasonable.
if (!session_id.ContainsOnlyASCII())
return false;
sanitized_session_id->assign(session_id.Ascii());
if (sanitized_session_id->length() > limits::kMaxSessionIdLength)
return false;
for (const char c : *sanitized_session_id) {
if (!base::IsAsciiAlpha(c) && !base::IsAsciiDigit(c))
return false;
}
return true;
}
bool SanitizeResponse(const std::string& key_system,
const uint8_t* response,
size_t response_length,
std::vector<uint8_t>* sanitized_response) {
// The user agent should thoroughly validate the response before passing it
// to the CDM. This may include verifying values are within reasonable limits,
// stripping irrelevant data or fields, pre-parsing it, sanitizing it,
// and/or generating a fully sanitized version. The user agent should check
// that the length and values of fields are reasonable. Unknown fields should
// be rejected or removed.
if (response_length > limits::kMaxSessionResponseLength)
return false;
if (IsClearKey(key_system) || IsExternalClearKey(key_system)) {
std::string key_string(response, response + response_length);
KeyIdAndKeyPairs keys;
CdmSessionType session_type = CdmSessionType::TEMPORARY_SESSION;
if (!ExtractKeysFromJWKSet(key_string, &keys, &session_type))
return false;
// Must contain at least one key.
if (keys.empty())
return false;
for (const auto key_pair : keys) {
if (key_pair.first.size() < limits::kMinKeyIdLength ||
key_pair.first.size() > limits::kMaxKeyIdLength) {
return false;
}
}
std::string sanitized_data = GenerateJWKSet(keys, session_type);
sanitized_response->assign(sanitized_data.begin(), sanitized_data.end());
return true;
}
// TODO(jrummell): Verify responses for Widevine.
sanitized_response->assign(response, response + response_length);
return true;
}
// If we need to close the session while destroying this object, we need a
// dummy promise that won't call back into this object (or try to pass
// something back to blink).
class IgnoreResponsePromise : public SimpleCdmPromise {
public:
IgnoreResponsePromise() = default;
~IgnoreResponsePromise() override = default;
// SimpleCdmPromise implementation.
void resolve() final { MarkPromiseSettled(); }
void reject(CdmPromise::Exception exception_code,
uint32_t system_code,
const std::string& error_message) final {
MarkPromiseSettled();
}
};
} // namespace
WebContentDecryptionModuleSessionImpl::WebContentDecryptionModuleSessionImpl(
const scoped_refptr<CdmSessionAdapter>& adapter)
: adapter_(adapter),
has_close_been_called_(false),
is_closed_(false),
is_persistent_session_(false),
weak_ptr_factory_(this) {}
WebContentDecryptionModuleSessionImpl::
~WebContentDecryptionModuleSessionImpl() {
DCHECK(thread_checker_.CalledOnValidThread());
if (!session_id_.empty()) {
adapter_->UnregisterSession(session_id_);
// From http://w3c.github.io/encrypted-media/#mediakeysession-interface
// "If a MediaKeySession object is not closed when it becomes inaccessible
// to the page, the CDM shall close the key session associated with the
// object."
//
// This object is destroyed when the corresponding blink object is no
// longer needed (which may be due to it becoming inaccessible to the
// page), so if the session is not closed and CloseSession() has not yet
// been called, call CloseSession() now. Since this object is being
// destroyed, there is no need for the promise to do anything as this
// session will be gone.
if (!is_closed_ && !has_close_been_called_) {
adapter_->CloseSession(session_id_,
std::make_unique<IgnoreResponsePromise>());
}
}
}
void WebContentDecryptionModuleSessionImpl::SetClientInterface(Client* client) {
client_ = client;
}
blink::WebString WebContentDecryptionModuleSessionImpl::SessionId() const {
return blink::WebString::FromUTF8(session_id_);
}
void WebContentDecryptionModuleSessionImpl::InitializeNewSession(
blink::WebEncryptedMediaInitDataType init_data_type,
const unsigned char* init_data,
size_t init_data_length,
blink::WebEncryptedMediaSessionType session_type,
blink::WebContentDecryptionModuleResult result) {
DCHECK(init_data);
DCHECK(session_id_.empty());
DCHECK(thread_checker_.CalledOnValidThread());
// From https://w3c.github.io/encrypted-media/#generateRequest.
// 6. If the Key System implementation represented by this object's cdm
// implementation value does not support initDataType as an Initialization
// Data Type, return a promise rejected with a NotSupportedError.
// String comparison is case-sensitive.
EmeInitDataType eme_init_data_type = ConvertToEmeInitDataType(init_data_type);
if (!IsSupportedKeySystemWithInitDataType(adapter_->GetKeySystem(),
eme_init_data_type)) {
std::string message =
"The initialization data type is not supported by the key system.";
result.CompleteWithError(
blink::kWebContentDecryptionModuleExceptionNotSupportedError, 0,
blink::WebString::FromUTF8(message));
return;
}
// 10.1 If the init data is not valid for initDataType, reject promise with
// a newly created TypeError.
// 10.2 Let sanitized init data be a validated and sanitized version of init
// data. The user agent must thoroughly validate the Initialization Data
// before passing it to the CDM. This includes verifying that the length
// and values of fields are reasonable, verifying that values are within
// reasonable limits, and stripping irrelevant, unsupported, or unknown
// data or fields. It is recommended that user agents pre-parse,
// sanitize, and/or generate a fully sanitized version of the
// Initialization Data. If the Initialization Data format specified by
// initDataType supports multiple entries, the user agent should remove
// entries that are not needed by the CDM. The user agent must not
// re-order entries within the Initialization Data.
// 10.3 If the preceding step failed, reject promise with a newly created
// TypeError.
std::vector<uint8_t> sanitized_init_data;
std::string message;
if (!SanitizeInitData(eme_init_data_type, init_data, init_data_length,
&sanitized_init_data, &message)) {
result.CompleteWithError(
blink::kWebContentDecryptionModuleExceptionTypeError, 0,
blink::WebString::FromUTF8(message));
return;
}
// 10.4 If sanitized init data is empty, reject promise with a
// NotSupportedError.
if (sanitized_init_data.empty()) {
result.CompleteWithError(
blink::kWebContentDecryptionModuleExceptionNotSupportedError, 0,
"No initialization data provided.");
return;
}
// 10.5 Let session id be the empty string.
// (Done in constructor.)
// 10.6 Let message be null.
// 10.7 Let message type be null.
// (Done by CDM.)
// 10.8 Let cdm be the CDM instance represented by this object's cdm
// instance value.
// 10.9 Use the cdm to execute the following steps:
CdmSessionType cdm_session_type = convertSessionType(session_type);
is_persistent_session_ =
cdm_session_type != CdmSessionType::TEMPORARY_SESSION;
adapter_->InitializeNewSession(
eme_init_data_type, sanitized_init_data, cdm_session_type,
std::unique_ptr<NewSessionCdmPromise>(new NewSessionCdmResultPromise(
result, adapter_->GetKeySystemUMAPrefix(), kGenerateRequestUMAName,
base::Bind(
&WebContentDecryptionModuleSessionImpl::OnSessionInitialized,
weak_ptr_factory_.GetWeakPtr()),
{SessionInitStatus::NEW_SESSION})));
}
void WebContentDecryptionModuleSessionImpl::Load(
const blink::WebString& session_id,
blink::WebContentDecryptionModuleResult result) {
DCHECK(!session_id.IsEmpty());
DCHECK(session_id_.empty());
DCHECK(thread_checker_.CalledOnValidThread());
// From https://w3c.github.io/encrypted-media/#load.
// 8.1 Let sanitized session ID be a validated and/or sanitized version of
// sessionId. The user agent should thoroughly validate the sessionId
// value before passing it to the CDM. At a minimum, this should include
// checking that the length and value (e.g. alphanumeric) are reasonable.
// 8.2 If the preceding step failed, or if sanitized session ID is empty,
// reject promise with a newly created TypeError.
std::string sanitized_session_id;
if (!SanitizeSessionId(session_id, &sanitized_session_id)) {
result.CompleteWithError(
blink::kWebContentDecryptionModuleExceptionTypeError, 0,
"Invalid session ID.");
return;
}
// TODO(jrummell): Now that there are 2 types of persistent sessions, the
// session type should be passed from blink. Type should also be passed in the
// constructor (and removed from initializeNewSession()).
is_persistent_session_ = true;
adapter_->LoadSession(
CdmSessionType::PERSISTENT_LICENSE_SESSION, sanitized_session_id,
std::unique_ptr<NewSessionCdmPromise>(new NewSessionCdmResultPromise(
result, adapter_->GetKeySystemUMAPrefix(), kLoadSessionUMAName,
base::Bind(
&WebContentDecryptionModuleSessionImpl::OnSessionInitialized,
weak_ptr_factory_.GetWeakPtr()),
{SessionInitStatus::NEW_SESSION,
SessionInitStatus::SESSION_NOT_FOUND})));
}
void WebContentDecryptionModuleSessionImpl::Update(
const uint8_t* response,
size_t response_length,
blink::WebContentDecryptionModuleResult result) {
DCHECK(response);
DCHECK(!session_id_.empty());
DCHECK(thread_checker_.CalledOnValidThread());
// From https://w3c.github.io/encrypted-media/#update.
// 6.1 Let sanitized response be a validated and/or sanitized version of
// response copy. The user agent should thoroughly validate the response
// before passing it to the CDM. This may include verifying values are
// within reasonable limits, stripping irrelevant data or fields,
// pre-parsing it, sanitizing it, and/or generating a fully sanitized
// version. The user agent should check that the length and values of
// fields are reasonable. Unknown fields should be rejected or removed.
// 6.2 If the preceding step failed, or if sanitized response is empty,
// reject promise with a newly created TypeError.
std::vector<uint8_t> sanitized_response;
if (!SanitizeResponse(adapter_->GetKeySystem(), response, response_length,
&sanitized_response)) {
result.CompleteWithError(
blink::kWebContentDecryptionModuleExceptionTypeError, 0,
"Invalid response.");
return;
}
adapter_->UpdateSession(
session_id_, sanitized_response,
std::unique_ptr<SimpleCdmPromise>(new CdmResultPromise<>(
result, adapter_->GetKeySystemUMAPrefix() + kUpdateSessionUMAName)));
}
void WebContentDecryptionModuleSessionImpl::Close(
blink::WebContentDecryptionModuleResult result) {
DCHECK(!session_id_.empty());
DCHECK(thread_checker_.CalledOnValidThread());
// close() shouldn't be called if the session is already closed. Since the
// operation is asynchronous, there is a window where close() was called
// just before the closed event arrives. The CDM should handle the case where
// close() is called after it has already closed the session. However, if
// we can tell the session is now closed, simply resolve the promise.
if (is_closed_) {
result.Complete();
return;
}
has_close_been_called_ = true;
adapter_->CloseSession(
session_id_,
std::unique_ptr<SimpleCdmPromise>(new CdmResultPromise<>(
result, adapter_->GetKeySystemUMAPrefix() + kCloseSessionUMAName)));
}
void WebContentDecryptionModuleSessionImpl::Remove(
blink::WebContentDecryptionModuleResult result) {
DCHECK(!session_id_.empty());
DCHECK(thread_checker_.CalledOnValidThread());
// TODO(http://crbug.com/616166). Once all supported CDMs allow remove() on
// temporary sessions, remove this code.
if (!is_persistent_session_ && !IsClearKey(adapter_->GetKeySystem())) {
result.CompleteWithError(
blink::kWebContentDecryptionModuleExceptionTypeError, 0,
"remove() on temporary sessions is not supported by this key system.");
return;
}
adapter_->RemoveSession(
session_id_,
std::unique_ptr<SimpleCdmPromise>(new CdmResultPromise<>(
result, adapter_->GetKeySystemUMAPrefix() + kRemoveSessionUMAName)));
}
void WebContentDecryptionModuleSessionImpl::OnSessionMessage(
CdmMessageType message_type,
const std::vector<uint8_t>& message) {
DCHECK(client_) << "Client not set before message event";
DCHECK(thread_checker_.CalledOnValidThread());
client_->Message(convertMessageType(message_type), message.data(),
message.size());
}
void WebContentDecryptionModuleSessionImpl::OnSessionKeysChange(
bool has_additional_usable_key,
CdmKeysInfo keys_info) {
DCHECK(thread_checker_.CalledOnValidThread());
blink::WebVector<blink::WebEncryptedMediaKeyInformation> keys(
keys_info.size());
for (size_t i = 0; i < keys_info.size(); ++i) {
auto& key_info = keys_info[i];
keys[i].SetId(blink::WebData(reinterpret_cast<char*>(&key_info->key_id[0]),
key_info->key_id.size()));
keys[i].SetStatus(ConvertCdmKeyStatus(key_info->status));
keys[i].SetSystemCode(key_info->system_code);
base::UmaHistogramSparse(
adapter_->GetKeySystemUMAPrefix() + kKeyStatusSystemCodeUMAName,
key_info->system_code);
}
// Now send the event to blink.
client_->KeysStatusesChange(keys, has_additional_usable_key);
}
void WebContentDecryptionModuleSessionImpl::OnSessionExpirationUpdate(
base::Time new_expiry_time) {
DCHECK(thread_checker_.CalledOnValidThread());
// The check works around an issue in base::Time that converts null base::Time
// to |1601-01-01 00:00:00 UTC| in ToJsTime(). See http://crbug.com/679079
client_->ExpirationChanged(new_expiry_time.is_null()
? std::numeric_limits<double>::quiet_NaN()
: new_expiry_time.ToJsTime());
}
void WebContentDecryptionModuleSessionImpl::OnSessionClosed() {
DCHECK(thread_checker_.CalledOnValidThread());
// Only send one closed event to blink.
if (is_closed_)
return;
is_closed_ = true;
client_->Close();
}
void WebContentDecryptionModuleSessionImpl::OnSessionInitialized(
const std::string& session_id,
SessionInitStatus* status) {
DCHECK(thread_checker_.CalledOnValidThread());
// CDM will return NULL if the session to be loaded can't be found.
if (session_id.empty()) {
*status = SessionInitStatus::SESSION_NOT_FOUND;
return;
}
DCHECK(session_id_.empty()) << "Session ID may not be changed once set.";
session_id_ = session_id;
*status =
adapter_->RegisterSession(session_id_, weak_ptr_factory_.GetWeakPtr())
? SessionInitStatus::NEW_SESSION
: SessionInitStatus::SESSION_ALREADY_EXISTS;
}
} // namespace media