2015-04-02 23:08:27 +03:00
|
|
|
/*
|
|
|
|
* Copyright 2015, Mozilla Foundation and contributors
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
2014-09-24 02:04:49 +04:00
|
|
|
|
2015-01-16 00:37:54 +03:00
|
|
|
#include <assert.h>
|
2014-09-24 02:04:49 +04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2017-07-27 03:50:46 +03:00
|
|
|
#include <vector>
|
2019-02-15 11:27:01 +03:00
|
|
|
#include <string>
|
2014-09-24 02:04:49 +04:00
|
|
|
|
2017-01-11 23:52:05 +03:00
|
|
|
#include "ClearKeyCDM.h"
|
2015-01-12 10:15:55 +03:00
|
|
|
#include "ClearKeySessionManager.h"
|
2017-01-11 23:52:05 +03:00
|
|
|
// This include is required in order for content_decryption_module to work
|
|
|
|
// on Unix systems.
|
|
|
|
#include "stddef.h"
|
|
|
|
#include "content_decryption_module.h"
|
2017-07-27 03:50:46 +03:00
|
|
|
#include "content_decryption_module_ext.h"
|
|
|
|
|
2017-07-28 14:48:56 +03:00
|
|
|
#ifndef XP_WIN
|
2017-07-27 03:50:46 +03:00
|
|
|
# include <sys/types.h>
|
|
|
|
# include <sys/stat.h>
|
|
|
|
# include <unistd.h>
|
|
|
|
#endif
|
2014-09-24 02:04:49 +04:00
|
|
|
|
2017-01-11 23:52:05 +03:00
|
|
|
#ifdef ENABLE_WMF
|
2015-02-28 06:15:18 +03:00
|
|
|
# include "WMFUtils.h"
|
2017-01-11 23:52:05 +03:00
|
|
|
#endif // ENABLE_WMF
|
2014-09-24 02:04:49 +04:00
|
|
|
|
2017-01-14 00:23:31 +03:00
|
|
|
extern "C" {
|
|
|
|
|
2017-05-29 08:14:26 +03:00
|
|
|
CDM_API
|
2017-01-11 23:52:05 +03:00
|
|
|
void INITIALIZE_CDM_MODULE() {}
|
2014-09-24 02:04:49 +04:00
|
|
|
|
2017-07-27 03:50:46 +03:00
|
|
|
static bool sCanReadHostVerificationFiles = false;
|
|
|
|
|
2017-05-29 08:14:26 +03:00
|
|
|
CDM_API
|
2017-01-11 23:52:05 +03:00
|
|
|
void* CreateCdmInstance(int cdm_interface_version, const char* key_system,
|
|
|
|
uint32_t key_system_size,
|
|
|
|
GetCdmHostFunc get_cdm_host_func, void* user_data) {
|
|
|
|
CK_LOGE("ClearKey CreateCDMInstance");
|
|
|
|
|
2019-01-14 19:40:04 +03:00
|
|
|
if (cdm_interface_version != cdm::ContentDecryptionModule_10::kVersion) {
|
2017-10-06 19:06:06 +03:00
|
|
|
CK_LOGE(
|
|
|
|
"ClearKey CreateCDMInstance failed due to requesting unsupported "
|
|
|
|
"version %d.",
|
|
|
|
cdm_interface_version);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2017-01-11 23:52:05 +03:00
|
|
|
#ifdef ENABLE_WMF
|
|
|
|
if (!wmf::EnsureLibs()) {
|
|
|
|
CK_LOGE("Required libraries were not found");
|
|
|
|
return nullptr;
|
2015-01-16 00:37:54 +03:00
|
|
|
}
|
|
|
|
#endif
|
2014-09-24 02:04:49 +04:00
|
|
|
|
2017-08-17 21:41:54 +03:00
|
|
|
#ifdef MOZILLA_OFFICIAL
|
2017-07-27 03:50:46 +03:00
|
|
|
// Test that we're able to read the host files.
|
|
|
|
if (!sCanReadHostVerificationFiles) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2017-08-17 21:41:54 +03:00
|
|
|
#endif
|
2017-07-27 03:50:46 +03:00
|
|
|
|
2019-01-14 19:40:04 +03:00
|
|
|
cdm::Host_10* host = static_cast<cdm::Host_10*>(
|
2017-01-11 23:52:05 +03:00
|
|
|
get_cdm_host_func(cdm_interface_version, user_data));
|
|
|
|
ClearKeyCDM* clearKey = new ClearKeyCDM(host);
|
2014-09-24 02:04:49 +04:00
|
|
|
|
2017-01-11 23:52:05 +03:00
|
|
|
CK_LOGE("Created ClearKeyCDM instance!");
|
2017-01-14 00:23:31 +03:00
|
|
|
|
2017-01-11 23:52:05 +03:00
|
|
|
return clearKey;
|
|
|
|
}
|
2017-07-27 03:50:46 +03:00
|
|
|
|
|
|
|
const size_t TEST_READ_SIZE = 16 * 1024;
|
|
|
|
|
|
|
|
bool CanReadSome(cdm::PlatformFile aFile) {
|
|
|
|
vector<uint8_t> data;
|
|
|
|
data.resize(TEST_READ_SIZE);
|
|
|
|
#ifdef XP_WIN
|
|
|
|
DWORD bytesRead = 0;
|
|
|
|
return ReadFile(aFile, &data.front(), TEST_READ_SIZE, &bytesRead, nullptr) &&
|
|
|
|
bytesRead > 0;
|
|
|
|
#else
|
2017-07-28 14:48:56 +03:00
|
|
|
return read(aFile, &data.front(), TEST_READ_SIZE) > 0;
|
2017-07-27 03:50:46 +03:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClosePlatformFile(cdm::PlatformFile aFile) {
|
|
|
|
#ifdef XP_WIN
|
|
|
|
CloseHandle(aFile);
|
2017-07-28 14:48:56 +03:00
|
|
|
#else
|
2017-07-27 03:50:46 +03:00
|
|
|
close(aFile);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2019-02-15 11:27:01 +03:00
|
|
|
static uint32_t NumExpectedHostFiles(const cdm::HostFile* aHostFiles,
|
|
|
|
uint32_t aNumFiles) {
|
|
|
|
#if !defined(XP_WIN)
|
|
|
|
// We expect 4 binaries: clearkey, libxul, plugin-container, and Firefox.
|
|
|
|
return 4;
|
|
|
|
#else
|
|
|
|
// Windows running x64 or x86 natively should also have 4 as above.
|
|
|
|
// For Windows on ARM64, we run an x86 plugin-contianer process under
|
|
|
|
// emulation, and so we expect one additional binary; the x86
|
|
|
|
// xul.dll used by plugin-container.exe.
|
|
|
|
bool i686underAArch64 = false;
|
|
|
|
// Assume that we're running under x86 emulation on an aarch64 host if
|
|
|
|
// one of the paths ends with the x86 plugin-container path we'd expect.
|
|
|
|
const std::wstring plugincontainer = L"i686\\plugin-container.exe";
|
|
|
|
for (uint32_t i = 0; i < aNumFiles; i++) {
|
|
|
|
const cdm::HostFile& hostFile = aHostFiles[i];
|
|
|
|
if (hostFile.file != cdm::kInvalidPlatformFile) {
|
|
|
|
std::wstring path = hostFile.file_path;
|
|
|
|
auto offset = path.find(plugincontainer);
|
|
|
|
if (offset != std::string::npos &&
|
|
|
|
offset == path.size() - plugincontainer.size()) {
|
|
|
|
i686underAArch64 = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return i686underAArch64 ? 5 : 4;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-07-27 03:50:46 +03:00
|
|
|
CDM_API
|
|
|
|
bool VerifyCdmHost_0(const cdm::HostFile* aHostFiles, uint32_t aNumFiles) {
|
2019-02-15 11:27:01 +03:00
|
|
|
// Check that we've received the expected number of host files.
|
|
|
|
bool rv = (aNumFiles == NumExpectedHostFiles(aHostFiles, aNumFiles));
|
2017-07-27 03:50:46 +03:00
|
|
|
// Verify that each binary is readable inside the sandbox,
|
|
|
|
// and close the handle.
|
|
|
|
for (uint32_t i = 0; i < aNumFiles; i++) {
|
|
|
|
const cdm::HostFile& hostFile = aHostFiles[i];
|
|
|
|
if (hostFile.file != cdm::kInvalidPlatformFile) {
|
|
|
|
if (!CanReadSome(hostFile.file)) {
|
|
|
|
rv = false;
|
|
|
|
}
|
|
|
|
ClosePlatformFile(hostFile.file);
|
|
|
|
}
|
|
|
|
if (hostFile.sig_file != cdm::kInvalidPlatformFile) {
|
|
|
|
ClosePlatformFile(hostFile.sig_file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sCanReadHostVerificationFiles = rv;
|
|
|
|
return rv;
|
2014-09-24 02:04:49 +04:00
|
|
|
}
|
2017-07-27 03:50:46 +03:00
|
|
|
|
|
|
|
} // extern "C".
|