2 * Copyright (C) 2015 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
27 #include "CDMSessionClearKey.h"
29 #include "ArrayValue.h"
30 #include "CryptoAlgorithm.h"
31 #include "CryptoAlgorithmIdentifier.h"
32 #include "CryptoKeyDataOctetSequence.h"
33 #include "Dictionary.h"
34 #include "JSMainThreadExecState.h"
36 #include "TextEncoding.h"
38 #include "WebKitMediaKeyError.h"
39 #include <runtime/JSGlobalObject.h>
40 #include <runtime/JSLock.h>
41 #include <runtime/JSONObject.h>
42 #include <runtime/VM.h>
43 #include <wtf/NeverDestroyed.h>
44 #include <wtf/text/Base64.h>
46 #if ENABLE(LEGACY_ENCRYPTED_MEDIA)
52 static VM& clearKeyVM()
54 static NeverDestroyed<RefPtr<VM>> vm;
56 vm.get() = VM::create();
61 CDMSessionClearKey::CDMSessionClearKey(CDMSessionClient* client)
63 , m_sessionId(createCanonicalUUIDString())
67 CDMSessionClearKey::~CDMSessionClearKey()
71 RefPtr<Uint8Array> CDMSessionClearKey::generateKeyRequest(const String& mimeType, Uint8Array* initData, String& destinationURL, unsigned short& errorCode, uint32_t& systemCode)
73 UNUSED_PARAM(mimeType);
74 UNUSED_PARAM(destinationURL);
75 UNUSED_PARAM(systemCode);
78 errorCode = WebKitMediaKeyError::MEDIA_KEYERR_CLIENT;
81 m_initData = initData;
83 bool sawError = false;
84 String keyID = UTF8Encoding().decode(reinterpret_cast_ptr<char*>(m_initData->baseAddress()), m_initData->byteLength(), true, sawError);
86 errorCode = WebKitMediaKeyError::MEDIA_KEYERR_CLIENT;
93 void CDMSessionClearKey::releaseKeys()
98 bool CDMSessionClearKey::update(Uint8Array* rawKeysData, RefPtr<Uint8Array>& nextMessage, unsigned short& errorCode, uint32_t& systemCode)
100 UNUSED_PARAM(nextMessage);
101 UNUSED_PARAM(systemCode);
105 String rawKeysString = String::fromUTF8(rawKeysData->data(), rawKeysData->length());
106 if (rawKeysString.isEmpty()) {
107 LOG(Media, "CDMSessionClearKey::update(%p) - failed: empty message", this);
111 VM& vm = clearKeyVM();
112 JSLockHolder lock(vm);
113 auto scope = DECLARE_THROW_SCOPE(vm);
114 JSGlobalObject* globalObject = JSGlobalObject::create(vm, JSGlobalObject::createStructure(vm, jsNull()));
115 ExecState* exec = globalObject->globalExec();
117 JSLockHolder locker(clearKeyVM());
118 JSValue keysDataObject = JSONParse(exec, rawKeysString);
119 if (scope.exception() || !keysDataObject) {
120 LOG(Media, "CDMSessionClearKey::update(%p) - failed: invalid JSON", this);
123 Dictionary keysDataDictionary(exec, keysDataObject);
124 ArrayValue keysArray;
126 if (!keysDataDictionary.get("keys", keysArray) || keysArray.isUndefinedOrNull() || !keysArray.length(length) || !length) {
127 LOG(Media, "CDMSessionClearKey::update(%p) - failed: keys array missing or empty", this);
131 bool foundValidKey = false;
132 for (size_t i = 0; i < length; ++i) {
133 Dictionary keyDictionary;
134 if (!keysArray.get(i, keyDictionary) || keyDictionary.isUndefinedOrNull()) {
135 LOG(Media, "CDMSessionClearKey::update(%p) - failed: null keyDictionary", this);
140 if (!keyDictionary.get("alg", algorithm) || !equalLettersIgnoringASCIICase(algorithm, "a128kw")) {
141 LOG(Media, "CDMSessionClearKey::update(%p) - failed: algorithm unsupported", this);
146 if (!keyDictionary.get("kty", keyType) || !equalLettersIgnoringASCIICase(keyType, "oct")) {
147 LOG(Media, "CDMSessionClearKey::update(%p) - failed: keyType unsupported", this);
152 if (!keyDictionary.get("kid", keyId) || keyId.isEmpty()) {
153 LOG(Media, "CDMSessionClearKey::update(%p) - failed: keyId missing or empty", this);
158 if (!keyDictionary.get("k", rawKeyData) || rawKeyData.isEmpty()) {
159 LOG(Media, "CDMSessionClearKey::update(%p) - failed: key missing or empty", this);
163 Vector<uint8_t> keyData;
164 if (!base64Decode(rawKeyData, keyData) || keyData.isEmpty()) {
165 LOG(Media, "CDMSessionClearKey::update(%p) - failed: unable to base64 decode key", this);
169 m_cachedKeys.set(keyId, WTFMove(keyData));
170 foundValidKey = true;
178 errorCode = WebKitMediaKeyError::MEDIA_KEYERR_CLIENT;
182 RefPtr<ArrayBuffer> CDMSessionClearKey::cachedKeyForKeyID(const String& keyId) const
184 if (!m_cachedKeys.contains(keyId))
187 auto keyData = m_cachedKeys.get(keyId);
188 RefPtr<Uint8Array> keyDataArray = Uint8Array::create(keyData.data(), keyData.size());
189 return keyDataArray->buffer();