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
|
// 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 "base/bind.h"
#include "base/callback_helpers.h"
#include "base/logging.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/key_systems.h"
#include "media/base/limits.h"
#include "media/base/media_keys.h"
#include "media/blink/cdm_result_promise.h"
#include "media/blink/cdm_session_adapter.h"
#include "media/blink/new_session_cdm_result_promise.h"
#include "media/blink/webmediaplayer_util.h"
#include "media/cdm/cenc_utils.h"
#include "media/cdm/json_web_key.h"
#include "third_party/WebKit/public/platform/WebData.h"
#include "third_party/WebKit/public/platform/WebEncryptedMediaKeyInformation.h"
#include "third_party/WebKit/public/platform/WebString.h"
#include "third_party/WebKit/public/platform/WebURL.h"
#include "third_party/WebKit/public/platform/WebVector.h"
namespace media {
const char kCloseSessionUMAName[] = "CloseSession";
const char kGenerateRequestUMAName[] = "GenerateRequest";
const char kLoadSessionUMAName[] = "LoadSession";
const char kRemoveSessionUMAName[] = "RemoveSession";
const char kUpdateSessionUMAName[] = "UpdateSession";
static blink::WebContentDecryptionModuleSession::Client::MessageType
convertMessageType(MediaKeys::MessageType message_type) {
switch (message_type) {
case media::MediaKeys::LICENSE_REQUEST:
return blink::WebContentDecryptionModuleSession::Client::MessageType::
LicenseRequest;
case media::MediaKeys::LICENSE_RENEWAL:
return blink::WebContentDecryptionModuleSession::Client::MessageType::
LicenseRenewal;
case media::MediaKeys::LICENSE_RELEASE:
return blink::WebContentDecryptionModuleSession::Client::MessageType::
LicenseRelease;
}
NOTREACHED();
return blink::WebContentDecryptionModuleSession::Client::MessageType::
LicenseRequest;
}
static blink::WebEncryptedMediaKeyInformation::KeyStatus convertStatus(
media::CdmKeyInformation::KeyStatus status) {
switch (status) {
case media::CdmKeyInformation::USABLE:
return blink::WebEncryptedMediaKeyInformation::KeyStatus::Usable;
case media::CdmKeyInformation::INTERNAL_ERROR:
return blink::WebEncryptedMediaKeyInformation::KeyStatus::InternalError;
case media::CdmKeyInformation::EXPIRED:
return blink::WebEncryptedMediaKeyInformation::KeyStatus::Expired;
case media::CdmKeyInformation::OUTPUT_NOT_ALLOWED:
return blink::WebEncryptedMediaKeyInformation::KeyStatus::
OutputNotAllowed;
case media::CdmKeyInformation::OUTPUT_DOWNSCALED:
return blink::WebEncryptedMediaKeyInformation::KeyStatus::
OutputDownscaled;
case media::CdmKeyInformation::KEY_STATUS_PENDING:
return blink::WebEncryptedMediaKeyInformation::KeyStatus::StatusPending;
}
NOTREACHED();
return blink::WebEncryptedMediaKeyInformation::KeyStatus::InternalError;
}
static MediaKeys::SessionType convertSessionType(
blink::WebEncryptedMediaSessionType session_type) {
switch (session_type) {
case blink::WebEncryptedMediaSessionType::Temporary:
return MediaKeys::TEMPORARY_SESSION;
case blink::WebEncryptedMediaSessionType::PersistentLicense:
return MediaKeys::PERSISTENT_LICENSE_SESSION;
case blink::WebEncryptedMediaSessionType::PersistentReleaseMessage:
return MediaKeys::PERSISTENT_RELEASE_MESSAGE_SESSION;
case blink::WebEncryptedMediaSessionType::Unknown:
break;
}
NOTREACHED();
return MediaKeys::TEMPORARY_SESSION;
}
static bool SanitizeInitData(EmeInitDataType init_data_type,
const unsigned char* init_data,
size_t init_data_length,
std::vector<uint8>* sanitized_init_data,
std::string* error_message) {
if (init_data_length > limits::kMaxInitDataLength) {
error_message->assign("Initialization data too long.");
return false;
}
switch (init_data_type) {
case EmeInitDataType::WEBM:
sanitized_init_data->assign(init_data, init_data + init_data_length);
return true;
case EmeInitDataType::CENC:
if (!ValidatePsshInput(init_data, init_data_length)) {
error_message->assign("Initialization data for CENC is incorrect.");
return false;
}
sanitized_init_data->assign(init_data, init_data + init_data_length);
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;
}
WebContentDecryptionModuleSessionImpl::WebContentDecryptionModuleSessionImpl(
const scoped_refptr<CdmSessionAdapter>& adapter)
: adapter_(adapter), is_closed_(false), weak_ptr_factory_(this) {
}
WebContentDecryptionModuleSessionImpl::
~WebContentDecryptionModuleSessionImpl() {
if (!session_id_.empty())
adapter_->UnregisterSession(session_id_);
}
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(session_id_.empty());
// From https://w3c.github.io/encrypted-media/#generateRequest.
// 5. 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 new DOMException whose name
// is 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::WebContentDecryptionModuleExceptionNotSupportedError, 0,
blink::WebString::fromUTF8(message));
return;
}
// 9.1 If the init data is not valid for initDataType, reject promise with a
// new DOMException whose name is InvalidAccessError.
// 9.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 support
// multiple entries, the user agent should remove entries that are not
// needed by the CDM.
// 9.3 If the previous step failed, reject promise with a new DOMException
// whose name is InvalidAccessError.
std::vector<uint8> sanitized_init_data;
std::string message;
if (!SanitizeInitData(eme_init_data_type, init_data, init_data_length,
&sanitized_init_data, &message)) {
result.completeWithError(
blink::WebContentDecryptionModuleExceptionInvalidAccessError, 0,
blink::WebString::fromUTF8(message));
return;
}
// 9.4 Let session id be the empty string.
// (Done in constructor.)
// 9.5 Let message be null.
// (Done by CDM.)
// 9.6 Let cdm be the CDM instance represented by this object's cdm
// instance value.
// 9.7 Use the cdm to execute the following steps:
adapter_->InitializeNewSession(
eme_init_data_type, vector_as_array(&sanitized_init_data),
base::saturated_cast<int>(sanitized_init_data.size()),
convertSessionType(session_type),
scoped_ptr<NewSessionCdmPromise>(new NewSessionCdmResultPromise(
result, adapter_->GetKeySystemUMAPrefix() + kGenerateRequestUMAName,
base::Bind(
&WebContentDecryptionModuleSessionImpl::OnSessionInitialized,
base::Unretained(this)))));
}
void WebContentDecryptionModuleSessionImpl::load(
const blink::WebString& session_id,
blink::WebContentDecryptionModuleResult result) {
DCHECK(!session_id.isEmpty());
DCHECK(session_id_.empty());
// 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()).
adapter_->LoadSession(
MediaKeys::PERSISTENT_LICENSE_SESSION, base::UTF16ToASCII(session_id),
scoped_ptr<NewSessionCdmPromise>(new NewSessionCdmResultPromise(
result, adapter_->GetKeySystemUMAPrefix() + kLoadSessionUMAName,
base::Bind(
&WebContentDecryptionModuleSessionImpl::OnSessionInitialized,
base::Unretained(this)))));
}
void WebContentDecryptionModuleSessionImpl::update(
const uint8* response,
size_t response_length,
blink::WebContentDecryptionModuleResult result) {
DCHECK(response);
DCHECK(!session_id_.empty());
adapter_->UpdateSession(
session_id_, response, base::saturated_cast<int>(response_length),
scoped_ptr<SimpleCdmPromise>(new CdmResultPromise<>(
result, adapter_->GetKeySystemUMAPrefix() + kUpdateSessionUMAName)));
}
void WebContentDecryptionModuleSessionImpl::close(
blink::WebContentDecryptionModuleResult result) {
DCHECK(!session_id_.empty());
adapter_->CloseSession(
session_id_,
scoped_ptr<SimpleCdmPromise>(new CdmResultPromise<>(
result, adapter_->GetKeySystemUMAPrefix() + kCloseSessionUMAName)));
}
void WebContentDecryptionModuleSessionImpl::remove(
blink::WebContentDecryptionModuleResult result) {
DCHECK(!session_id_.empty());
adapter_->RemoveSession(
session_id_,
scoped_ptr<SimpleCdmPromise>(new CdmResultPromise<>(
result, adapter_->GetKeySystemUMAPrefix() + kRemoveSessionUMAName)));
}
void WebContentDecryptionModuleSessionImpl::OnSessionMessage(
MediaKeys::MessageType message_type,
const std::vector<uint8>& message) {
DCHECK(client_) << "Client not set before message event";
client_->message(convertMessageType(message_type), vector_as_array(&message),
message.size());
}
void WebContentDecryptionModuleSessionImpl::OnSessionKeysChange(
bool has_additional_usable_key,
CdmKeysInfo keys_info) {
blink::WebVector<blink::WebEncryptedMediaKeyInformation> keys(
keys_info.size());
for (size_t i = 0; i < keys_info.size(); ++i) {
const 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(convertStatus(key_info->status));
keys[i].setSystemCode(key_info->system_code);
}
// Now send the event to blink.
client_->keysStatusesChange(keys, has_additional_usable_key);
}
void WebContentDecryptionModuleSessionImpl::OnSessionExpirationUpdate(
const base::Time& new_expiry_time) {
client_->expirationChanged(new_expiry_time.ToJsTime());
}
void WebContentDecryptionModuleSessionImpl::OnSessionClosed() {
if (is_closed_)
return;
is_closed_ = true;
client_->close();
}
blink::WebContentDecryptionModuleResult::SessionStatus
WebContentDecryptionModuleSessionImpl::OnSessionInitialized(
const std::string& session_id) {
// CDM will return NULL if the session to be loaded can't be found.
if (session_id.empty())
return blink::WebContentDecryptionModuleResult::SessionNotFound;
DCHECK(session_id_.empty()) << "Session ID may not be changed once set.";
session_id_ = session_id;
return adapter_->RegisterSession(session_id_, weak_ptr_factory_.GetWeakPtr())
? blink::WebContentDecryptionModuleResult::NewSession
: blink::WebContentDecryptionModuleResult::SessionAlreadyExists;
}
} // namespace media
|