/
CDMProxyClearKey.cpp
245 lines (201 loc) · 8.45 KB
/
CDMProxyClearKey.cpp
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
/*
* Copyright (C) 2020 Metrological Group B.V.
* Copyright (C) 2020 Igalia S.L.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "CDMProxyClearKey.h"
#if ENABLE(ENCRYPTED_MEDIA)
#include "Logging.h"
#include <wtf/ByteOrder.h>
namespace WebCore {
namespace {
static bool readUInt32(const uint8_t* buffer, size_t bufferSize, size_t& offset, uint32_t& value)
{
ASSERT_ARG(offset, offset <= bufferSize);
if (bufferSize - offset < sizeof(value))
return false;
value = ntohl(*reinterpret_cast_ptr<const uint32_t*>(buffer + offset));
offset += sizeof(value);
return true;
}
static bool readUInt16(const uint8_t* buffer, size_t bufferSize, size_t& offset, uint16_t& value)
{
ASSERT_ARG(offset, offset <= bufferSize);
if (bufferSize - offset < sizeof(value))
return false;
value = ntohs(*reinterpret_cast_ptr<const uint16_t*>(buffer + offset));
offset += sizeof(value);
return true;
}
} // namespace {}
CDMProxyFactoryClearKey& CDMProxyFactoryClearKey::singleton()
{
static NeverDestroyed<CDMProxyFactoryClearKey> s_factory;
return s_factory;
}
RefPtr<CDMProxy> CDMProxyFactoryClearKey::createCDMProxy(const String& keySystem)
{
ASSERT_UNUSED(keySystem, supportsKeySystem(keySystem));
return adoptRef(new CDMProxyClearKey());
}
bool CDMProxyFactoryClearKey::supportsKeySystem(const String& keySystem)
{
// `org.w3.clearkey` is the only supported key system.
return equalLettersIgnoringASCIICase(keySystem, "org.w3.clearkey");
}
CDMProxyClearKey::~CDMProxyClearKey()
{
closeGCryptHandle();
}
bool CDMProxyClearKey::cencSetCounterVector(const cencDecryptContext& input)
{
uint8_t ctr[ClearKey::AES128CTRBlockSizeInBytes];
if (input.ivSizeInBytes == 8) {
// ISO/IEC 23001-7:2016 Section 9.3
// When an 8-byte IV is indicated, the least significant 8
// bytes of the 16 byte IV (bytes 8 to 15) SHALL be set to
// zero.
memset(ctr + 8, 0, 8);
memcpy(ctr, input.iv, 8);
} else
memcpy(ctr, input.iv, ClearKey::IVSizeInBytes);
if (gcry_error_t cipherError = gcry_cipher_setctr(gCryptHandle(), ctr, ClearKey::IVSizeInBytes)) {
#if !LOG_DISABLED
LOG(EME, "EME - CDMProxyClearKey - ERROR(gcry_cipher_setctr): %s", gpg_strerror(cipherError));
#else
UNUSED_VARIABLE(cipherError);
#endif
return false;
}
return true;
}
bool CDMProxyClearKey::cencSetDecryptionKey(const cencDecryptContext& in)
{
// FIXME: Unnecessary copy, can we avoid this while still exposing
// a non-GStreamer-specific DecryptInput API? These buffers are
// small (16 bytes), so not a huge deal, I guess.
Vector<uint8_t> keyIDVec;
keyIDVec.append(in.keyID, in.keyIDSizeInBytes);
auto keyData = getOrWaitForKeyValue(keyIDVec);
if (!keyData)
return false;
ASSERT(WTF::holds_alternative<Vector<uint8_t>>(keyData.value()));
auto& keyDataValue = WTF::get<Vector<uint8_t>>(keyData.value());
if (gcry_error_t cipherError = gcry_cipher_setkey(gCryptHandle(), keyDataValue.data(), keyDataValue.size())) {
#if !LOG_DISABLED
LOG(EME, "EME - CDMProxyClearKey - ERROR(gcry_cipher_setkey): %s", gpg_strerror(cipherError));
#else
UNUSED_VARIABLE(cipherError);
#endif
return false;
}
return true;
}
bool CDMProxyClearKey::cencDecryptFullSample(cencDecryptContext& in)
{
if (!in.encryptedBufferSizeInBytes)
return true;
LOG(EME, "EME - CDMProxyClearKey - full-sample decryption: %zu encrypted bytes", in.encryptedBufferSizeInBytes);
if (gcry_error_t cipherError = gcry_cipher_decrypt(gCryptHandle(), in.encryptedBuffer, in.encryptedBufferSizeInBytes, 0, 0)) {
#if !LOG_DISABLED
LOG(EME, "EME - CDMProxyClearKey - ERROR(gcry_cipher_decrypt): %s", gpg_strerror(cipherError));
#else
UNUSED_VARIABLE(cipherError);
#endif
return false;
}
return true;
}
bool CDMProxyClearKey::cencDecryptSubsampled(cencDecryptContext& input)
{
unsigned encryptedBufferByteOffset = 0;
size_t subSampleBufferByteOffset = 0;
unsigned subsampleIndex = 0;
while (encryptedBufferByteOffset < input.encryptedBufferSizeInBytes) {
uint16_t subsampleNumClearBytes = 0;
uint32_t subsampleNumEncryptedBytes = 0;
if (subsampleIndex < input.numSubsamples) {
if (!readUInt16(input.subsamplesBuffer, input.subsamplesBufferSizeInBytes, subSampleBufferByteOffset, subsampleNumClearBytes)) {
LOG(EME, "EME - CDMProxyClearKey - could not read number of clear bytes in subsample at index %u", subsampleIndex);
return false;
}
if (!readUInt32(input.subsamplesBuffer, input.subsamplesBufferSizeInBytes, subSampleBufferByteOffset, subsampleNumEncryptedBytes)) {
LOG(EME, "EME - CDMProxyClearKey - could not read number of encrypted bytes in subsample at index %u", subsampleIndex);
return false;
}
subsampleIndex++;
} else {
subsampleNumClearBytes = 0;
subsampleNumEncryptedBytes = input.encryptedBufferSizeInBytes - encryptedBufferByteOffset;
}
// FIXME: These are high-frequency messages, not sure if there's a better logging lib in WebCore.
LOG(EME, "EME - subsample index %u - %u bytes clear (%zu bytes left to decrypt)", subsampleIndex, subsampleNumClearBytes, input.encryptedBufferSizeInBytes - encryptedBufferByteOffset);
encryptedBufferByteOffset += subsampleNumClearBytes;
if (subsampleNumEncryptedBytes) {
LOG(EME, "EME - subsample index %u - %u bytes encrypted (%zu bytes left to decrypt)", subsampleIndex, subsampleNumEncryptedBytes, input.encryptedBufferSizeInBytes - encryptedBufferByteOffset);
if (gcry_error_t cipherError = gcry_cipher_decrypt(gCryptHandle(), input.encryptedBuffer + encryptedBufferByteOffset, subsampleNumEncryptedBytes, 0, 0)) {
#if !LOG_DISABLED
LOG(EME, "EME - CDMProxyClearKey - ERROR(gcry_cipher_decrypt): %s", gpg_strerror(cipherError));
#else
UNUSED_VARIABLE(cipherError);
#endif
return false;
}
encryptedBufferByteOffset += subsampleNumEncryptedBytes;
}
}
return true;
}
bool CDMProxyClearKey::cencDecrypt(CDMProxyClearKey::cencDecryptContext& input)
{
if (!cencSetCounterVector(input) || !cencSetDecryptionKey(input))
return false;
return input.isSubsampled() ? cencDecryptSubsampled(input) : cencDecryptFullSample(input);
}
void CDMProxyClearKey::closeGCryptHandle()
{
if (m_gCryptHandle) {
gcry_cipher_close(*m_gCryptHandle);
m_gCryptHandle.reset();
}
}
gcry_cipher_hd_t& CDMProxyClearKey::gCryptHandle()
{
if (!m_gCryptHandle) {
m_gCryptHandle.emplace();
if (gcry_error_t error = gcry_cipher_open(&m_gCryptHandle.value(), GCRY_CIPHER_AES128, GCRY_CIPHER_MODE_CTR, GCRY_CIPHER_SECURE)) {
#if !LOG_DISABLED
LOG(EME, "EME - CDMProxyClearKey - ERROR(gcry_cipher_open): %s", gpg_strerror(error));
#else
UNUSED_VARIABLE(error);
#endif
RELEASE_ASSERT(false && "Should not get this far with a functional GCrypt!");
}
}
return *m_gCryptHandle;
}
} // namespace WebCore
#endif // ENABLE(ENCRYPTED_MEDIA)