V18.4.0 CAS plugin

Note that this version does not have Widevine Provisioning 4.0 support.
It is only suitable for device upgrades. A new patch with provisioning
4.0 support will be made later.
This commit is contained in:
Lu Chen
2024-02-22 13:45:32 -08:00
parent ff9728aaa2
commit 5f209e6980
92 changed files with 25729 additions and 0 deletions

31
wvutil/Android.bp Normal file
View File

@@ -0,0 +1,31 @@
// Builds libcasutil.a
cc_library_static {
name: "libcasutil",
proprietary: true,
local_include_dirs: [
"include",
],
srcs: [
"src/clock.cpp",
"src/log.cpp",
"src/file_store.cpp",
"src/file_utils.cpp",
"src/rw_lock.cpp",
"src/string_conversions.cpp",
"src/android_properties.cpp",
"src/timer.cpp",
],
shared_libs: [
"liblog",
"libutils",
"libcrypto",
"libhidlbase",
],
export_include_dirs: ["include"],
}

View File

@@ -0,0 +1,46 @@
// Copyright 2019 Google LLC. All Rights Reserved. This file and proprietary
// source code may only be used and distributed under the Widevine License
// Agreement.
#ifndef WVCDM_UTIL_ADVANCE_IV_CTR_H_
#define WVCDM_UTIL_ADVANCE_IV_CTR_H_
#include <stdint.h>
#include <string.h>
#include "string_conversions.h"
namespace wvutil {
// Advance an IV according to ISO-CENC's CTR modes. The lower half of the IV is
// split off and treated as an unsigned 64-bit integer, then incremented by the
// number of complete crypto blocks decrypted. The resulting value is then
// copied back into the IV over the previous lower half.
inline void AdvanceIvCtr(uint8_t (*subsample_iv)[16], size_t bytes) {
constexpr size_t kAesBlockSize = 16;
constexpr size_t kIvSize = kAesBlockSize;
constexpr size_t kCounterIndex = kIvSize / 2;
constexpr size_t kCounterSize = kIvSize / 2;
uint64_t counter;
static_assert(
sizeof(*subsample_iv) == kIvSize,
"The subsample_iv field is no longer the length of an AES-128 IV.");
static_assert(sizeof(counter) == kCounterSize,
"A uint64_t failed to be half the size of an AES-128 IV.");
// Defensive copy because the elements of the array may not be properly
// aligned
memcpy(&counter, &(*subsample_iv)[kCounterIndex], kCounterSize);
const size_t increment =
bytes / kAesBlockSize; // The truncation here is intentional
counter = htonll64(ntohll64(counter) + increment);
memcpy(&(*subsample_iv)[kCounterIndex], &counter, kCounterSize);
}
} // namespace wvutil
#endif // WVCDM_UTIL_ADVANCE_IV_CTR_H_

View File

@@ -0,0 +1,20 @@
// Copyright 2019 Google LLC. All Rights Reserved. This file and proprietary
// source code may only be used and distributed under the Widevine License
// Agreement.
#ifndef WVCDM_UTIL_ARRAYSIZE_H_
#define WVCDM_UTIL_ARRAYSIZE_H_
#include <stdint.h>
namespace wvutil {
// Returns the size of a fixed-length array.
template <typename T, size_t N>
constexpr size_t ArraySize(const T (&)[N]) {
return N;
}
} // namespace wvutil
#endif // WVCDM_UTIL_ARRAYSIZE_H_

View File

@@ -0,0 +1,46 @@
// Copyright 2018 Google LLC. All Rights Reserved. This file and proprietary
// source code may only be used and distributed under the Widevine Master
// License Agreement.
#ifndef CAS_PROPERTIES_H
#define CAS_PROPERTIES_H
#include <string>
namespace wvcas {
// Properties methods must be implemented for a platform. The values returned
// describe the capabilities and configuration of a device using Widevine CAS.
class Properties {
private:
Properties(); // Not implemented
~Properties(); // NotImplemented
public:
// Sets the |company_name| field value to be populated in and EMM license
// request. Returns false if unable to set the value.
static bool GetCompanyName(std::string* company_name);
// Sets the |model_name| field value to be populated in and EMM license
// request. Returns false if unable to set the value.
static bool GetModelName(std::string* model_name);
// Sets the |product_name| field value to be populated in and EMM license
// request. Returns false if unable to set the value.
static bool GetProductName(std::string* product_name);
// Sets the |arch_name| field value to be populated in and EMM license
// request. Returns false if unable to set the value.
static bool GetArchitectureName(std::string* arch_name);
// Sets the |device_name| field value to be populated in and EMM license
// request. Returns false if unable to set the value.
static bool GetDeviceName(std::string* device_name);
// Returns a path to CAS oemcrypto library, either default,
// or overridden through system property.
// Returned path could be either absolute or relative.
// Returns false if unable to set the value.
static bool GetOEMCryptoPath(std::string* path);
// Sets |version| to Widevine CAS plugin version. Returns false if unable to
// set the value.
static bool GetWvCasPluginVersion(std::string& version);
};
} // namespace wvcas
#endif // CAS_PROPERTIES_H

117
wvutil/include/cdm_random.h Normal file
View File

@@ -0,0 +1,117 @@
// Copyright 2019 Google LLC. All Rights Reserved. This file and proprietary
// source code may only be used and distributed under the Widevine License
// Agreement.
#ifndef WVCDM_CORE_CDM_RANDOM_H_
#define WVCDM_CORE_CDM_RANDOM_H_
#include <mutex>
#include <random>
#include <string>
namespace wvutil {
// CdmRandomGenerator is a thread safe, pseudo-random number generator.
// It's purpose is to simplified interface for C++11's <random> library.
// Some of the methods use a "device specific" random seed, if the
// compiler/device does not support device specific randomizers, then the
// actual value supplied may not be random. The generator is designed to
// meet the C++ named requirement UniformRandomBitGenerator to allow it to
// be used with standard library functions / class which are designed to
// work with the standard library generators.
class CdmRandomGenerator {
public:
// Result type of operator().
using result_type = unsigned int;
// Inclusive boundaries of operator().
static constexpr unsigned int min() { return 0; }
static constexpr unsigned int max() { return RAND_MAX; }
// The maximum number of bytes that can be generated at once for
// `RandomData()`.
static constexpr size_t kMaxRandomDataLength = 8192; // 8 kB
// Initializes the pseudo-random generator with a value from a device
// specific random number generator.
CdmRandomGenerator();
// Initializes the pseudo-random generator with the specified seed value.
explicit CdmRandomGenerator(unsigned int seed) : generator_(seed) {}
// All of these methods are thread-safe.
// Seeds the pseudo-random generator with a value from a device specific
// random number generator.
void Seed();
// Seeds the pseudo-random generator with the specified seed value.
// This is somewhat similar to `srand()` from the C standard library;
// except that the sequence generated from successive calls to `Rand()`
// will not necessarily be the same as they would be from the
// standard library `rand()`. This is due to the underlying pseudo-random
// generator that is used.
void Seed(unsigned int seed);
// Returns a pseudo-random integer.
// This is similar to `rand()` from the C standard library. The integer
// returned is in the range of [min(), max()].
unsigned int Rand();
// Allows for RNG to be callable.
unsigned int operator()() { return Rand(); }
// Returns a pseudo-random integer within the provided inclusive range.
uint64_t RandomInRange(uint64_t lower, uint64_t upper);
uint64_t RandomInRange(uint64_t upper) { return RandomInRange(0, upper); }
// Returns a byte string containing randomized bytes of the specified
// length.
// If |length| is greater than |CdmRandomGenerator::kMaxRandomDataLength|,
// then an error is logged and an empty string is returned.
std::string RandomData(size_t length);
// Random true/false using Bernoulli distribution of equal probability.
bool RandomBool();
private:
// Mutex is used to lock the object, and allowing it to be used
// concurrently in different threads.
std::mutex generator_lock_;
// The `default_random_engine` depends on the compiler used and
// potentially its version. This is important to know if you need to
// create reproducible tests between platforms.
std::default_random_engine generator_;
};
// Provides a static interface to a process-wide instance of
// CdmRandomGenerator.
class CdmRandom {
public:
static unsigned int Rand() { return GetInstance()->Rand(); }
static uint64_t RandomInRange(uint64_t lower, uint64_t upper) {
return GetInstance()->RandomInRange(lower, upper);
}
static uint64_t RandomInRange(uint64_t upper) {
return GetInstance()->RandomInRange(upper);
}
static std::string RandomData(size_t length) {
return GetInstance()->RandomData(length);
}
static bool RandomBool() { return GetInstance()->RandomBool(); }
private:
// These are intended to be used by tests if needed.
static void Seed(unsigned int seed) { GetInstance()->Seed(seed); }
static void Seed() { GetInstance()->Seed(); }
// Returns the process-wide instance of CdmRandomGenerator.
// It the global instance has not yet been created, then a new instance
// is created using a device-specific random seed.
static CdmRandomGenerator* GetInstance();
};
} // namespace wvutil
#endif // WVCDM_CORE_CDM_RANDOM_H_

26
wvutil/include/clock.h Normal file
View File

@@ -0,0 +1,26 @@
// Copyright 2018 Google LLC. All Rights Reserved. This file and proprietary
// source code may only be used and distributed under the Widevine License
// Agreement.
//
// Clock - Platform independent interface for a time library
//
#ifndef WVCDM_UTIL_CLOCK_H_
#define WVCDM_UTIL_CLOCK_H_
#include <stdint.h>
namespace wvutil {
// Provides time related information. The implementation is platform dependent.
class Clock {
public:
Clock() {}
virtual ~Clock() {}
// Provides the number of seconds since an epoch - 01/01/1970 00:00 UTC
virtual int64_t GetCurrentTime();
};
} // namespace wvutil
#endif // WVCDM_UTIL_CLOCK_H_

View File

@@ -0,0 +1,16 @@
// Copyright 2018 Google LLC. All Rights Reserved. This file and proprietary
// source code may only be used and distributed under the Widevine License
// Agreement.
#ifndef WVCDM_UTIL_DISALLOW_COPY_AND_ASSIGN_H_
#define WVCDM_UTIL_DISALLOW_COPY_AND_ASSIGN_H_
namespace wvutil {
#define CORE_DISALLOW_COPY_AND_ASSIGN(TypeName) \
TypeName(const TypeName&); \
void operator=(const TypeName&)
} // namespace wvutil
#endif // WVCDM_UTIL_DISALLOW_COPY_AND_ASSIGN_H_

View File

@@ -0,0 +1,87 @@
// Copyright 2018 Google LLC. All Rights Reserved. This file and proprietary
// source code may only be used and distributed under the Widevine License
// Agreement.
//
// File - Platform independent interface for a File class
//
#ifndef WVCDM_UTIL_FILE_STORE_H_
#define WVCDM_UTIL_FILE_STORE_H_
#include <cstdint>
#include <memory>
#include <string>
#include <vector>
#include "disallow_copy_and_assign.h"
#include "platform.h"
#include "util_common.h"
namespace wvutil {
static const std::string kAtscCertificateFileName = "atsccert.bin";
static const std::string kCertificateFileName = "cert1.bin";
static const std::string kCertificateFileNameExt = ".bin";
static const std::string kCertificateFileNamePrefix = "cert1_";
static const std::string kLegacyCertificateFileName = "cert.bin";
static const std::string kLegacyCertificateFileNamePrefix = "cert";
static const std::string kOemCertificateFileName = "oemcert.bin";
static const std::string kOemCertificateFileNamePrefix = "oemcert_";
// File class. The implementation is platform dependent.
class File {
public:
File() {}
virtual ~File() {}
virtual ssize_t Read(char* buffer, size_t bytes) = 0;
virtual ssize_t Write(const char* buffer, size_t bytes) = 0;
friend class FileSystem;
CORE_DISALLOW_COPY_AND_ASSIGN(File);
};
class FileSystem {
public:
FileSystem();
FileSystem(const std::string& origin, void* extra_data);
virtual ~FileSystem();
class Impl;
// defines as bit flag
enum OpenFlags {
kNoFlags = 0,
kCreate = 1,
kReadOnly = 2, // defaults to read and write access
kTruncate = 4
};
virtual std::unique_ptr<File> Open(const std::string& file_path, int flags);
virtual bool Exists(const std::string& file_path);
virtual bool Exists(const std::string& file_path, int* errno_value);
virtual bool Remove(const std::string& file_path);
virtual ssize_t FileSize(const std::string& file_path);
// Return the filenames stored at dir_path.
// dir_path will be stripped from the returned names.
virtual bool List(const std::string& dir_path,
std::vector<std::string>* names);
const std::string& origin() const { return origin_; }
void set_origin(const std::string& origin);
const std::string& identifier() const { return identifier_; }
void set_identifier(const std::string& identifier);
bool IsGlobal() const { return identifier_.empty(); }
private:
std::unique_ptr<FileSystem::Impl> impl_;
std::string origin_;
std::string identifier_;
CORE_DISALLOW_COPY_AND_ASSIGN(FileSystem);
};
} // namespace wvutil
#endif // WVCDM_UTIL_FILE_STORE_H_

View File

@@ -0,0 +1,29 @@
// Copyright 2018 Google LLC. All Rights Reserved. This file and proprietary
// source code may only be used and distributed under the Widevine License
// Agreement.
#include <string>
#include <vector>
namespace wvutil {
const char kCurrentDirectory[] = ".";
const char kParentDirectory[] = "..";
const char kDirectoryDelimiter = '/';
const char kWildcard[] = "*";
bool IsCurrentOrParentDirectory(const char* dir);
class FileUtils {
public:
static bool Exists(const std::string& src);
static bool Exists(const std::string& src, int* errno_value);
// The caller may only specifying a single wildcard
static bool Remove(const std::string& src);
static bool Copy(const std::string& src, const std::string& dest);
static bool List(const std::string& path, std::vector<std::string>* files);
static bool IsRegularFile(const std::string& path);
static bool IsDirectory(const std::string& path);
static bool CreateDirectory(const std::string& path);
};
} // namespace wvutil

111
wvutil/include/log.h Normal file
View File

@@ -0,0 +1,111 @@
// Copyright 2018 Google LLC. All Rights Reserved. This file and proprietary
// source code may only be used and distributed under the Widevine License
// Agreement.
//
// Log - Platform independent interface for a Logging class
//
#ifndef WVCDM_UTIL_LOG_H_
#define WVCDM_UTIL_LOG_H_
#include <cstdint>
#include <deque>
#include <mutex>
#include <string>
#include <vector>
#include "util_common.h"
namespace wvutil {
// Simple logging class. The implementation is platform dependent.
typedef enum {
// This log level should only be used for |g_cutoff|, in order to silence all
// logging. It should never be passed to |Log()| as a log level.
CDM_LOG_SILENT = -1,
CDM_LOG_ERROR = 0,
CDM_LOG_WARN = 1,
CDM_LOG_INFO = 2,
CDM_LOG_DEBUG = 3,
CDM_LOG_VERBOSE = 4,
} LogPriority;
extern LogPriority g_cutoff;
struct LogMessage {
uint32_t uid_;
int64_t time_ms_;
LogPriority priority_;
std::string message_;
};
class LogBuffer {
public:
static const int MAX_CAPACITY = 100;
void addLog(const LogMessage& log);
std::vector<LogMessage> getLogs();
private:
std::deque<LogMessage> buffer_;
std::mutex mutex_;
};
extern LogBuffer g_logbuf;
static const uint32_t UNKNOWN_UID = std::numeric_limits<uint32_t>::max();
#ifdef __ANDROID__
void SetLoggingUid(const uint32_t);
void ClearLoggingUid();
uint32_t GetLoggingUid();
uint32_t GetIpcCallingUid();
#else
static inline void SetLoggingUid(const uint32_t) {}
static inline void ClearLoggingUid() {}
static inline uint32_t GetLoggingUid() { return UNKNOWN_UID; }
static inline uint32_t GetIpcCallingUid() { return UNKNOWN_UID; }
#endif
struct LoggingUidSetter {
LoggingUidSetter() {}
LoggingUidSetter(uint32_t uid) { SetLoggingUid(uid); }
virtual ~LoggingUidSetter() { ClearLoggingUid(); }
};
// Enable/disable verbose logging (LOGV).
// This function is supplied for cases where the system layer does not
// initialize logging. This is also needed to initialize logging in
// unit tests.
void InitLogging();
#ifdef __GNUC__
[[gnu::format(printf, 5, 6)]]
#endif
void Log(const char* file, const char* function, int line,
LogPriority level, const char* fmt, ...);
// Log APIs
#ifdef CDM_DISABLE_LOGGING
# define LOGE(...) (void)0
# define LOGW(...) (void)0
# define LOGI(...) (void)0
# define LOGD(...) (void)0
# define LOGV(...) (void)0
#else
# ifndef LOGE
# define LOGE(...) \
Log(__FILE__, __func__, __LINE__, wvutil::CDM_LOG_ERROR, __VA_ARGS__)
# define LOGW(...) \
Log(__FILE__, __func__, __LINE__, wvutil::CDM_LOG_WARN, __VA_ARGS__)
# define LOGI(...) \
Log(__FILE__, __func__, __LINE__, wvutil::CDM_LOG_INFO, __VA_ARGS__)
# define LOGD(...) \
Log(__FILE__, __func__, __LINE__, wvutil::CDM_LOG_DEBUG, __VA_ARGS__)
# define LOGV(...) \
Log(__FILE__, __func__, __LINE__, wvutil::CDM_LOG_VERBOSE, __VA_ARGS__)
# endif
#endif
} // namespace wvutil
#endif // WVCDM_UTIL_LOG_H_

31
wvutil/include/platform.h Normal file
View File

@@ -0,0 +1,31 @@
// Copyright 2018 Google LLC. All Rights Reserved. This file and proprietary
// source code may only be used and distributed under the Widevine License
// Agreement.
//
// Platform - Abstracts some utilities between platforms.
//
#ifndef WVCDM_UTIL_PLATFORM_H_
#define WVCDM_UTIL_PLATFORM_H_
#include "util_common.h"
#ifdef _WIN32
# include <BaseTsd.h>
# include <winsock2.h> // For htonl and ntohl.
# include <wtypes.h>
# define __PRETTY_FUNCTION__ __FUNCTION__
# undef NO_ERROR
# undef GetCurrentTime
# undef DeleteFile
using ssize_t = SSIZE_T;
inline void sleep(int seconds) { Sleep(seconds * 1000); }
int setenv(const char* key, const char* value, int overwrite);
#else
# include <arpa/inet.h>
# include <sys/types.h>
# include <unistd.h>
#endif
#endif // WVCDM_UTIL_PLATFORM_H_

65
wvutil/include/rw_lock.h Normal file
View File

@@ -0,0 +1,65 @@
// Copyright 2019 Google LLC. All Rights Reserved. This file and proprietary
// source code may only be used and distributed under the Widevine License
// Agreement.
#ifndef WVCDM_UTIL_RW_LOCK_H_
#define WVCDM_UTIL_RW_LOCK_H_
#include <stdint.h>
#include <condition_variable>
#include <mutex>
#include "disallow_copy_and_assign.h"
#include "util_common.h"
namespace wvutil {
// A simple reader-writer mutex implementation that mimics the one from C++17
class shared_mutex {
public:
shared_mutex() : reader_count_(0), has_writer_(false) {}
~shared_mutex();
// These methods take the mutex as a reader. They do not fulfill the
// SharedMutex requirement from the C++14 STL, but they fulfill enough of it
// to be used with |shared_lock| below.
void lock_shared();
void unlock_shared();
// These methods take the mutex as a writer. They fulfill the Mutex
// requirement from the C++11 STL so that this mutex can be used with
// |std::unique_lock|.
void lock() { lock_implementation(false); }
bool try_lock() { return lock_implementation(true); }
void unlock();
private:
bool lock_implementation(bool abort_if_unavailable);
uint32_t reader_count_;
bool has_writer_;
std::mutex mutex_;
std::condition_variable condition_variable_;
CORE_DISALLOW_COPY_AND_ASSIGN(shared_mutex);
};
// A simple reader lock implementation that mimics the one from C++14
template <typename Mutex>
class shared_lock {
public:
explicit shared_lock(Mutex& lock) : lock_(&lock) { lock_->lock_shared(); }
explicit shared_lock(Mutex* lock) : lock_(lock) { lock_->lock_shared(); }
~shared_lock() { lock_->unlock_shared(); }
private:
Mutex* lock_;
CORE_DISALLOW_COPY_AND_ASSIGN(shared_lock);
};
} // namespace wvutil
#endif // WVCDM_UTIL_RW_LOCK_H_

View File

@@ -0,0 +1,61 @@
// Copyright 2018 Google LLC. All Rights Reserved. This file and proprietary
// source code may only be used and distributed under the Widevine License
// Agreement.
#ifndef WVCDM_UTIL_STRING_CONVERSIONS_H_
#define WVCDM_UTIL_STRING_CONVERSIONS_H_
#include <stddef.h>
#include <stdint.h>
#include <string>
#include <vector>
#include "util_common.h"
namespace wvutil {
// ASCII hex to Binary conversion.
std::vector<uint8_t> a2b_hex(const std::string& b);
std::vector<uint8_t> a2b_hex(const std::string& label,
const std::string& b);
std::string a2bs_hex(const std::string& b);
// Binary to ASCII hex conversion. The default versions limit output to 2k to
// protect us from log spam. The unlimited version has no length limit.
std::string b2a_hex(const std::vector<uint8_t>& b);
std::string unlimited_b2a_hex(const std::vector<uint8_t>& b);
std::string b2a_hex(const std::string& b);
std::string unlimited_b2a_hex(const std::string& b);
std::string HexEncode(const uint8_t* bytes, size_t size);
std::string UnlimitedHexEncode(const uint8_t* bytes, size_t size);
// Base64 encoding/decoding.
// Converts binary data into the ASCII Base64 character set and vice
// versa using the encoding rules defined in RFC4648 section 4.
std::string Base64Encode(const std::vector<uint8_t>& bin_input);
std::string Base64Encode(const std::string& bin_input);
std::vector<uint8_t> Base64Decode(const std::string& bin_input);
// URL-Safe Base64 encoding/decoding.
// Converts binary data into the URL/Filename safe ASCII Base64
// character set and vice versa using the encoding rules defined in
// RFC4648 section 5.
std::string Base64SafeEncode(const std::vector<uint8_t>& bin_input);
std::string Base64SafeEncode(const std::string& bin_input);
std::vector<uint8_t> Base64SafeDecode(const std::string& bin_input);
// URL-Safe Base64 encoding without padding.
// Similar to Base64SafeEncode(), without any padding character '='
// at the end.
std::string Base64SafeEncodeNoPad(const std::vector<uint8_t>& bin_input);
std::string Base64SafeEncodeNoPad(const std::string& bin_input);
// Host to Network/Network to Host conversion.
int64_t htonll64(int64_t x);
inline int64_t ntohll64(int64_t x) { return htonll64(x); }
// Encode unsigned integer into a big endian formatted string.
std::string EncodeUint32(uint32_t u);
} // namespace wvutil
#endif // WVCDM_UTIL_STRING_CONVERSIONS_H_

View File

@@ -0,0 +1,23 @@
// Copyright 2022 Google LLC. All Rights Reserved. This file and proprietary
// source code may only be used and distributed under the Widevine License
// Agreement.
#ifndef WVCDM_UTIL_STRING_FORMAT_H_
#define WVCDM_UTIL_STRING_FORMAT_H_
#include <string>
namespace wvutil {
#ifdef __GNUC__
[[gnu::format(printf, 2, 3)]]
#endif
bool FormatString(std::string* out, const char* fmt, ...);
#ifdef __GNUC__
[[gnu::format(printf, 2, 0)]]
#endif
bool VFormatString(std::string* out, const char* fmt, va_list vlist);
} // namespace wvutil
#endif // WVCDM_UTIL_STRING_FORMAT_H_

55
wvutil/include/timer.h Normal file
View File

@@ -0,0 +1,55 @@
// Copyright 2018 Google LLC. All Rights Reserved. This file and proprietary
// source code may only be used and distributed under the Widevine Master
// License Agreement.
//
// Timer - Platform independent interface for a Timer class
//
#ifndef TIMER_H_
#define TIMER_H_
#include <stdint.h>
#include "disallow_copy_and_assign.h"
namespace wvutil {
// Timer Handler class.
//
// Derive from this class if you wish to receive events when the timer
// expires. Provide the handler when setting up a new Timer.
class TimerHandler {
public:
TimerHandler(){};
virtual ~TimerHandler(){};
virtual void OnTimerEvent() = 0;
};
// Timer class. The implementation is platform dependent.
//
// This class provides a simple recurring timer API. The class receiving
// timer expiry events should derive from TimerHandler.
// Specify the receiver class and the periodicty of timer events when
// the timer is initiated by calling Start.
class Timer {
public:
class Impl;
Timer();
~Timer();
bool Start(TimerHandler *handler, uint32_t time_in_secs);
void Stop();
bool IsRunning();
private:
Impl *impl_;
CORE_DISALLOW_COPY_AND_ASSIGN(Timer);
};
} // namespace wvutil
#endif // TIMER_H_

View File

@@ -0,0 +1,45 @@
// Copyright 2018 Google LLC. All Rights Reserved. This file and proprietary
// source code may only be used and distributed under the Widevine License
// Agreement.
#ifndef WVCDM_UTIL_UTIL_COMMON_H_
#define WVCDM_UTIL_UTIL_COMMON_H_
// This section deals with defines that are platform-specific.
#ifdef _WIN32
# define CORE_UTIL_IGNORE_DEPRECATED
# define CORE_UTIL_RESTORE_WARNINGS
#else
# ifdef __GNUC__
# define CORE_UTIL_IGNORE_DEPRECATED \
_Pragma("GCC diagnostic push") \
_Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"")
# define CORE_UTIL_RESTORE_WARNINGS _Pragma("GCC diagnostic pop")
# else
# define CORE_UTIL_IGNORE_DEPRECATED
# define CORE_UTIL_RESTORE_WARNINGS
# endif
#endif
// This section deals with attribute-detection and is platform-agnostic.
#if !defined(__has_cpp_attribute)
# define __has_cpp_attribute(x) 0
#endif
#if __has_cpp_attribute(fallthrough)
# define CORE_UTIL_FALLTHROUGH [[fallthrough]]
#elif __has_cpp_attribute(clang::fallthrough)
# define CORE_UTIL_FALLTHROUGH [[clang::fallthrough]]
#elif __has_cpp_attribute(gnu::fallthrough)
# define CORE_UTIL_FALLTHROUGH [[gnu::fallthrough]]
#else
# define CORE_UTIL_FALLTHROUGH
#endif
#endif // WVCDM_UTIL_UTIL_COMMON_H_

View File

@@ -0,0 +1,16 @@
// Copyright 2021 Google LLC. All rights reserved. This file and proprietary
// source code may only be used and distributed under the Widevine
// License Agreement.
#ifndef WVCDM_UTIL_WV_ATTRIBUTES_H_
#define WVCDM_UTIL_WV_ATTRIBUTES_H_
#ifndef UNUSED
# if defined(__GNUC__) || defined(__clang__)
# define UNUSED __attribute__((__unused__))
# else
# define UNUSED
# endif
#endif
#endif // WVCDM_UTIL_WV_ATTRIBUTES_H_

View File

@@ -0,0 +1,88 @@
// Copyright 2018 Google LLC. All Rights Reserved. This file and proprietary
// source code may only be used and distributed under the Widevine Master
// License Agreement.
#include <cutils/properties.h>
#include "cas_properties.h"
#include "log.h"
namespace {
// Version format: OEMCrypto_major.OEMCrypto_minor.Plugin_version
constexpr char kWvCasPluginVersion[] = "18.4.0";
bool GetAndroidProperty(const char* key, std::string* value) {
char val[PROPERTY_VALUE_MAX];
if (!key) {
LOGW("GetAndroidProperty: Invalid property key parameter");
return false;
}
if (!value) {
LOGW("GetAndroidProperty: Invalid property value parameter");
return false;
}
if (property_get(key, val, "Unknown") <= 0) return false;
*value = val;
return true;
}
} // namespace
namespace wvcas {
bool Properties::GetCompanyName(std::string* company_name) {
if (!company_name) {
LOGW("Properties::GetCompanyName: Invalid parameter");
return false;
}
return GetAndroidProperty("ro.product.manufacturer", company_name);
}
bool Properties::GetModelName(std::string* model_name) {
if (!model_name) {
LOGW("Properties::GetModelName: Invalid parameter");
return false;
}
return GetAndroidProperty("ro.product.model", model_name);
}
bool Properties::GetArchitectureName(std::string* arch_name) {
if (!arch_name) {
LOGW("Properties::GetArchitectureName: Invalid parameter");
return false;
}
return GetAndroidProperty("ro.product.cpu.abi", arch_name);
}
bool Properties::GetDeviceName(std::string* device_name) {
if (!device_name) {
LOGW("Properties::GetDeviceName: Invalid parameter");
return false;
}
return GetAndroidProperty("ro.product.device", device_name);
}
bool Properties::GetProductName(std::string* product_name) {
if (!product_name) {
LOGW("Properties::GetProductName: Invalid parameter");
return false;
}
return GetAndroidProperty("ro.product.name", product_name);
}
bool Properties::GetOEMCryptoPath(std::string* path) {
if (path == nullptr) {
LOGW("Properties::GetOEMCryptoPath: Invalid parameter");
return false;
}
*path = "liboemcrypto.so";
return true;
}
bool Properties::GetWvCasPluginVersion(std::string& version) {
version = kWvCasPluginVersion;
return true;
}
} // namespace wvcas

20
wvutil/src/clock.cpp Normal file
View File

@@ -0,0 +1,20 @@
// Copyright 2018 Google LLC. All Rights Reserved. This file and proprietary
// source code may only be used and distributed under the Widevine License
// Agreement.
//
// Clock - implemented using the standard linux time library
#include "clock.h"
#include <sys/time.h>
namespace wvutil {
int64_t Clock::GetCurrentTime() {
struct timeval tv;
tv.tv_sec = tv.tv_usec = 0;
gettimeofday(&tv, nullptr);
return tv.tv_sec;
}
} // namespace wvutil

537
wvutil/src/file_store.cpp Normal file
View File

@@ -0,0 +1,537 @@
// Copyright 2018 Google LLC. All Rights Reserved. This file and proprietary
// source code may only be used and distributed under the Widevine License
// Agreement.
//
// File class - provides a simple android specific file implementation
#include "file_store.h"
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <threads.h>
#include <time.h>
#include <unistd.h>
#include <algorithm>
#include "file_utils.h"
#include "log.h"
#include "string_conversions.h"
#include <openssl/md5.h>
// Size of the thread-local error string buffer. Used for calls
// to strerror_r(3).
#define ERRORSTR_BUF_SIZE 1024
namespace wvutil {
namespace {
// Maximum number of attempts to read or write on a file.
constexpr size_t kMaxIoAttempts = 5;
// Stand in constant for a closed file descriptor.
constexpr int kClosedFd = -1;
// A reset value of |errno|. Although unlikely, it is possible that a
// system call could fail and not set |errno| to a new value. This
// would technically be a bug with glibc, but it could cause our error
// handling code to enter a bad state.
constexpr int kNoError = 0;
constexpr char kEmptyOrigin[] = "";
// Reads from file specified by |fd| into the provided |buffer| up to
// the number of bytes specified by |count|.
// This is an internal function and assumes that all parameters are
// valid.
//
// Returns:
// 0 to |count| - Number of bytes successfully read from file.
// -1 - Error occurred, check |errno| for read(2).
ssize_t SafeRead(int fd, char* buffer, size_t count) {
size_t attempts = 0;
size_t total_bytes_read = 0;
while (total_bytes_read < count && attempts < kMaxIoAttempts) {
const size_t to_read = count - total_bytes_read;
errno = kNoError;
const ssize_t res = read(fd, buffer, to_read);
if (res > 0) {
attempts = 0; // Clearing |attempts| on success.
// It is possible that fewer bytes than |to_read| were read.
// In this case, try reading again. Non-critical errors will
// likely result in success or |errno| being set to EAGAIN on
// the second call. Critical errors will result in a different
// error that the caller will need to handle.
total_bytes_read += static_cast<size_t>(res);
continue;
}
if (res == 0) return total_bytes_read; // EOF.
attempts++;
if ((errno != EINTR && errno != EAGAIN) || attempts >= kMaxIoAttempts) {
// Caller must handle all other errors, or if max attempts
// have been reached.
return -1;
}
// read() was interrupted by signal, safe to try again.
}
return total_bytes_read;
}
// Writes to the file specified by |fd| from the provided |buffer|.
// Function will attempt to write all bytes specified by |count|.
// This is an internal function and assumes that all parameters are
// valid.
//
// Returns:
// |count| - Successfully wrote all bytes to file.
// -1 - Error occurred, check |errno| for write(2).
ssize_t SafeWrite(int fd, const char* buffer, size_t count) {
size_t attempts = 0;
size_t total_bytes_written = 0;
while (total_bytes_written < count && attempts < kMaxIoAttempts) {
const size_t to_write = count - total_bytes_written;
errno = kNoError;
const ssize_t res = write(fd, &buffer[total_bytes_written], to_write);
if (res > 0) {
attempts = 0; // Clearing |attempts| on success.
// It is possible that fewer bytes than |to_write| were written.
// In this case, try writing again. Non-critical errors will
// likely result in success or |errno| being set to EAGAIN on
// the second call. Critical errors will result in a different
// error that the caller will need to handle.
total_bytes_written += static_cast<size_t>(res);
continue;
}
if (res == 0) return total_bytes_written; // Possible EOF.
attempts++;
if ((errno != EINTR && errno != EAGAIN) || attempts >= kMaxIoAttempts) {
// Caller must handle all other errors, or if max attempts
// have been reached.
return -1;
}
// write() was interrupted by signal, safe to try again.
}
return total_bytes_written;
}
// Converts the provided error number to its string representation.
// Supports a subset of error numbers expected from the system calls
// used in this module.
// TODO(b/183653374): Replace this with strerrorname_np().
const char* ErrnoToString(int num) {
switch (num) {
case kNoError:
return "ZERO";
case EACCES:
return "EACCES";
case EAGAIN:
return "EAGAIN";
case EBADF:
return "EBADF";
case EBUSY:
return "EBUSY";
case EDESTADDRREQ:
return "EDESTADDRREQ";
case EDQUOT:
return "EDQUOT";
case EEXIST:
return "EEXIST";
case EFAULT:
return "EFAULT";
case EFBIG:
return "EFBIG";
case EINTR:
return "EINTR";
case EINVAL:
return "EINVAL";
case EIO:
return "EIO";
case EISDIR:
return "EISDIR";
case ELOOP:
return "ELOOP";
case EMFILE:
return "EMFILE";
case ENAMETOOLONG:
return "ENAMETOOLONG";
case ENFILE:
return "ENFILE";
case ENODEV:
return "ENODEV";
case ENOENT:
return "ENOENT";
case ENOMEM:
return "ENOMEM";
case ENOSPC:
return "ENOSPC";
case ENOTDIR:
return "ENOTDIR";
case ENXIO:
return "ENXIO";
case EOPNOTSUPP:
return "EOPNOTSUPP";
case EOVERFLOW:
return "EOVERFLOW";
case EPERM:
return "EPERM";
case EPIPE:
return "EPIPE";
case EROFS:
return "EROFS";
case ETXTBSY:
return "ETXTBSY";
#if EWOULDBLOCK != EAGAIN
case EWOULDBLOCK:
return "EWOULDBLOCK";
#endif
}
return "UNKNOWN";
}
// Safely converts the provided error number to its standard
// description string as provided by strerror_r().
// This function is guaranteed to return null-terminated string,
// and is thread safe.
const char* ErrnoToDescription(int num) {
static thread_local char error_buf[ERRORSTR_BUF_SIZE];
if (num == kNoError) {
return "Unspecified error";
}
// Always ensure there is a null term.
error_buf[sizeof(error_buf) - 1] = 0;
// See strerror_l(3) manual page for details.
#if (_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !_GNU_SOURCE
// Portable version:
// int strerror_r(int num, char* buf, size_t buflen)
// Returns:
// 0 on success
// -1 or a positive value on error (depends on glibc version)
const int res = strerror_r(num, error_buf, sizeof(error_buf));
return res != 0 ? "Unknown" : error_buf;
#else
// GNU specific version:
// char* strerror_r(int num, char* buf, size_t buflen)
// Returns:
// Pointer to |buf| or internal string on success
// Null on error
const char* res = strerror_r(num, error_buf, sizeof(error_buf));
return res == nullptr ? "Unknown" : res;
#endif
}
// Converts the provided file |mode| to a "ls" style file mode.
std::string StatModeToString(unsigned int mode) {
std::string mode_rep;
mode_rep.reserve(11); // 1 file type + 9 permissions + 1 term.
// File type.
if (S_ISREG(mode)) {
mode_rep.append("-");
} else if (S_ISDIR(mode)) {
mode_rep.append("d");
} else if (S_ISLNK(mode)) {
mode_rep.append("l");
} else if (S_ISCHR(mode)) {
mode_rep.append("c");
} else if (S_ISBLK(mode)) {
mode_rep.append("b");
} else if (S_ISSOCK(mode)) {
mode_rep.append("s");
} else {
mode_rep.append("?");
}
// User owner permission.
mode_rep.append((mode & S_IRUSR) ? "r" : "-");
mode_rep.append((mode & S_IWUSR) ? "w" : "-");
mode_rep.append((mode & S_IXUSR) ? "x" : "-");
// Group owner permission.
mode_rep.append((mode & S_IRGRP) ? "r" : "-");
mode_rep.append((mode & S_IWGRP) ? "w" : "-");
mode_rep.append((mode & S_IXGRP) ? "x" : "-");
// Others permission.
mode_rep.append((mode & S_IROTH) ? "r" : "-");
mode_rep.append((mode & S_IWOTH) ? "w" : "-");
mode_rep.append((mode & S_IXOTH) ? "x" : "-");
return mode_rep;
}
// Coverts the provided system time in seconds to a string representation
// in the system's local time.
// Time format is a modified ISO8601.
// Example: 2021-03-01 12:32:53
// If the time cannot be converted into this format, then the decimal
// representation of seconds are returned.
std::string PosixTimeToString(time_t seconds) {
struct tm timestamp;
if (localtime_r(&seconds, &timestamp) == nullptr) {
// Only possible failure is an overflow.
return std::to_string(seconds);
}
char buffer[32];
memset(buffer, 0, sizeof(buffer));
const size_t length = strftime(buffer, sizeof(buffer), "%F %T", &timestamp);
if (length == 0) {
// Unexpected error. Just return seconds.
return std::to_string(seconds);
}
return std::string(buffer, length);
}
std::string GetFileNameSafeHash(const std::string& input) {
std::vector<uint8_t> hash(MD5_DIGEST_LENGTH);
MD5(reinterpret_cast<const uint8_t*>(input.data()), input.size(),
hash.data());
return wvutil::Base64SafeEncode(hash);
}
std::string GetFileNameForIdentifier(const std::string path,
const std::string identifier) {
std::string file_name = path;
std::string dir_path;
const size_t delimiter_pos = path.rfind(kDirectoryDelimiter);
if (delimiter_pos != std::string::npos) {
dir_path = file_name.substr(0, delimiter_pos);
file_name = path.substr(delimiter_pos + 1);
}
if (file_name == kCertificateFileName && !identifier.empty()) {
const std::string hash = GetFileNameSafeHash(identifier);
file_name = kCertificateFileNamePrefix + hash + kCertificateFileNameExt;
} else if (file_name == kLegacyCertificateFileName && !identifier.empty()) {
const std::string hash = GetFileNameSafeHash(identifier);
file_name =
kLegacyCertificateFileNamePrefix + hash + kCertificateFileNameExt;
}
if (dir_path.empty())
return file_name;
return dir_path + kDirectoryDelimiter + file_name;
}
} // namespace
class AndroidFile : public File {
public:
// Parameters:
// |fd| - Open file descriptor for a regular file.
// |flags| - Bit field of flags originally passed to FileSystem::Open()
// |file_path| - Path used to open file.
AndroidFile(int fd, int flags, const std::string& file_path)
: fd_(fd), flags_(flags), file_path_(file_path) {}
~AndroidFile() { Close(); }
bool IsOpen() const { return fd_ != kClosedFd; }
bool CanWrite() const { return !(flags_ & FileSystem::kReadOnly); }
// Used for logging.
const char* file_path() const { return file_path_.c_str(); }
ssize_t Read(char* buffer, size_t bytes) override {
if (!buffer) {
LOGE("Output |buffer| is null");
return -1;
}
if (!IsOpen()) {
LOGE("File not open: path = %s", file_path());
return -1;
}
const ssize_t res = SafeRead(fd_, buffer, bytes);
if (res < 0) {
const int saved_errno = errno;
LOGE("Read failed: errno = %s (%d), desc = %s",
ErrnoToString(saved_errno), saved_errno,
ErrnoToDescription(saved_errno));
return -1;
} else if (res < bytes) {
LOGD("Read output truncated: expected = %zu, actual = %zd", bytes, res);
}
return res;
}
ssize_t Write(const char* buffer, size_t bytes) override {
if (!buffer) {
LOGE("Input |buffer| is null");
return -1;
}
if (!IsOpen()) {
LOGE("File not open: path = %s", file_path());
return -1;
}
if (!CanWrite()) {
LOGE("File is read only: path = %s", file_path());
return -1;
}
const ssize_t res = SafeWrite(fd_, buffer, bytes);
if (res < 0) {
const int saved_errno = errno;
LOGE("Write failed: errno = %s (%d), desc = %s",
ErrnoToString(saved_errno), saved_errno,
ErrnoToDescription(saved_errno));
return -1;
}
FlushFile();
return res;
}
private:
void FlushFile() { fsync(fd_); }
void Close() {
if (IsOpen()) {
FlushFile();
close(fd_);
fd_ = kClosedFd;
}
}
// Logs the contents of the file's stat info.
void LogStat() const {
if (!IsOpen()) {
LOGD("No stat info available");
return;
}
struct stat st;
errno = kNoError;
if (fstat(fd_, &st) != 0) {
const int saved_errno = errno;
if (errno != EBADF) {
// No logs if an issue with FD, caller would have indicated the problem.
LOGE("Stat failed: errno = %s (%d), desc = %s",
ErrnoToString(saved_errno), saved_errno,
ErrnoToDescription(saved_errno));
}
return;
}
LOGD(
"Stat: path = %s, st_dev = %lu, st_ino = %lu, st_mode = 0%o (%s), "
"st_uid = %u, st_gid = %u, st_size = %ld, st_atime = %s, "
"st_mtime = %s, st_ctime = %s",
file_path(), st.st_dev, st.st_ino, st.st_mode,
StatModeToString(st.st_mode).c_str(), st.st_uid, st.st_gid, st.st_size,
PosixTimeToString(st.st_atime).c_str(),
PosixTimeToString(st.st_mtime).c_str(),
PosixTimeToString(st.st_ctime).c_str());
}
// File descriptor of the opened file. Set to -1 when closed.
int fd_ = kClosedFd;
// Bit field of OpenFlags.
int flags_ = 0;
// Path used to open the file descriptor.
std::string file_path_;
};
class FileSystem::Impl {};
FileSystem::FileSystem() : FileSystem(kEmptyOrigin, nullptr) {}
FileSystem::FileSystem(const std::string& origin, void* /* extra_data */)
: origin_(origin) {}
FileSystem::~FileSystem() {}
std::unique_ptr<File> FileSystem::Open(const std::string& file_name,
int flags) {
const std::string file_path =
GetFileNameForIdentifier(file_name, identifier_);
// Verify flags.
if ((flags & kReadOnly) && (flags & kTruncate)) {
LOGE(
"Cannot be both truncated and be read-only: "
"file = %s, identifier = %s",
file_name.c_str(), identifier_.c_str());
return nullptr;
}
// Create the enclosing directory if it does not exist.
const size_t delimiter_pos = file_path.rfind(kDirectoryDelimiter);
if (delimiter_pos != std::string::npos) {
const std::string dir_path = file_path.substr(0, delimiter_pos);
if ((flags & FileSystem::kCreate) && !Exists(dir_path))
FileUtils::CreateDirectory(dir_path);
}
const bool exists = Exists(file_path);
if (!(flags & kCreate) && !exists) {
LOGD("File does not exist: file = %s, identifier = %s", file_name.c_str(),
identifier_.c_str());
return nullptr;
}
const int open_flags = ((flags & kCreate) ? O_CREAT : 0) |
((flags & kReadOnly) ? O_RDONLY : O_RDWR) |
((flags & kTruncate) ? O_TRUNC : 0) |
O_CLOEXEC; // Never share on calls to exec().
constexpr mode_t kDefaultMode = S_IRUSR | S_IWUSR;
errno = kNoError;
const int fd = open(file_path.c_str(), open_flags, kDefaultMode);
if (fd < 0) {
const int saved_errno = errno;
LOGE("%s failed: errno = %s (%d), desc = %s, path = %s",
exists ? "Open" : "Create", ErrnoToString(saved_errno), saved_errno,
ErrnoToDescription(saved_errno), file_path.c_str());
return nullptr;
}
// Check that the opened file is a regular file.
struct stat st;
errno = kNoError;
if (fstat(fd, &st) != 0) {
const int saved_errno = errno;
LOGE("Stat failed: errno = %s (%d), desc = %s", ErrnoToString(saved_errno),
saved_errno, ErrnoToDescription(saved_errno));
close(fd);
return nullptr;
}
if (!S_ISREG(st.st_mode)) {
LOGE("Not a file: path = %s, st_mode = 0%o (%s)", file_path.c_str(),
st.st_mode, StatModeToString(st.st_mode).c_str());
close(fd);
return nullptr;
}
return std::unique_ptr<File>(new AndroidFile(fd, flags, file_path));
}
bool FileSystem::Exists(const std::string& path) {
return FileUtils::Exists(GetFileNameForIdentifier(path, identifier_));
}
bool FileSystem::Exists(const std::string& path, int* errno_value) {
return FileUtils::Exists(GetFileNameForIdentifier(path, identifier_),
errno_value);
}
bool FileSystem::Remove(const std::string& path) {
return FileUtils::Remove(GetFileNameForIdentifier(path, identifier_));
}
ssize_t FileSystem::FileSize(const std::string& file_name) {
const std::string file_path =
GetFileNameForIdentifier(file_name, identifier_);
struct stat st;
errno = kNoError;
if (stat(file_path.c_str(), &st) == 0) {
if (st.st_size == 0) {
LOGW("File is empty: name = %s", file_name.c_str());
}
return st.st_size;
}
// Else, error occurred.
const int saved_errno = errno;
LOGE("Stat failed: errno = %s (%d), desc = %s", ErrnoToString(saved_errno),
saved_errno, ErrnoToDescription(saved_errno));
return -1;
}
bool FileSystem::List(const std::string& path,
std::vector<std::string>* filenames) {
return FileUtils::List(GetFileNameForIdentifier(path, origin_), filenames);
}
void FileSystem::set_origin(const std::string& origin) { origin_ = origin; }
void FileSystem::set_identifier(const std::string& identifier) {
identifier_ = identifier;
}
} // namespace wvutil

240
wvutil/src/file_utils.cpp Normal file
View File

@@ -0,0 +1,240 @@
// Copyright 2018 Google LLC. All Rights Reserved. This file and proprietary
// source code may only be used and distributed under the Widevine License
// Agreement.
#include "file_utils.h"
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/sendfile.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <cstring>
#include "log.h"
namespace wvutil {
bool IsCurrentOrParentDirectory(const char* dir) {
return strcmp(dir, kCurrentDirectory) == 0 ||
strcmp(dir, kParentDirectory) == 0;
}
bool FileUtils::Exists(const std::string& path) {
return Exists(path, nullptr);
}
bool FileUtils::Exists(const std::string& path, int* errno_value) {
struct stat buf;
int error = 0;
int res = stat(path.c_str(), &buf) == 0;
if (!res) {
error = errno;
if (error == ENOENT) {
LOGI("stat failed: ENOENT");
} else {
LOGE("stat failed: %d, %s", error, strerror(error));
}
}
if (errno_value != nullptr) *errno_value = error;
return res;
}
bool FileUtils::Remove(const std::string& path) {
if (FileUtils::IsDirectory(path)) {
// Handle directory deletion
DIR* dir;
if ((dir = opendir(path.c_str())) != nullptr) {
// first remove files and dir within it
struct dirent* entry;
while ((entry = readdir(dir)) != nullptr) {
if (!IsCurrentOrParentDirectory(entry->d_name)) {
std::string path_to_remove = path + kDirectoryDelimiter;
path_to_remove += entry->d_name;
if (!Remove(path_to_remove)) {
closedir(dir);
LOGW("Failed to remove directory entry: dir_path = %s, entry = %s",
path.c_str(), entry->d_name);
return false;
}
}
}
closedir(dir);
}
if (rmdir(path.c_str())) {
LOGW("File::Remove: rmdir failed: %d, %s", errno, strerror(errno));
return false;
}
return true;
} else {
size_t wildcard_pos = path.find(kWildcard);
if (wildcard_pos == std::string::npos) {
// Handle file deletion
if (unlink(path.c_str()) && (errno != ENOENT)) {
LOGW("File::Remove: unlink failed: %d, %s", errno, strerror(errno));
return false;
}
} else {
// Handle wildcard specified file deletion
size_t delimiter_pos = path.rfind(kDirectoryDelimiter, wildcard_pos);
if (delimiter_pos == std::string::npos) {
LOGW("File::Remove: unable to find path delimiter before wildcard");
return false;
}
DIR* dir;
std::string dir_path = path.substr(0, delimiter_pos);
std::string prepend =
path.substr(delimiter_pos + 1, wildcard_pos - delimiter_pos - 1);
if ((dir = opendir(dir_path.c_str())) == nullptr) {
LOGW("File::Remove: directory open failed for wildcard: %d, %s", errno,
strerror(errno));
return false;
}
struct dirent* entry;
std::string ext = path.substr(wildcard_pos + 1);
while ((entry = readdir(dir)) != nullptr) {
size_t filename_len = strlen(entry->d_name);
if (filename_len > ext.size()) {
if (strcmp(entry->d_name + filename_len - ext.size(), ext.c_str()) ==
0 &&
!IsCurrentOrParentDirectory(entry->d_name) &&
strncmp(entry->d_name, prepend.c_str(), prepend.size()) == 0) {
std::string file_path_to_remove =
dir_path + kDirectoryDelimiter + entry->d_name;
if (!Remove(file_path_to_remove)) {
closedir(dir);
return false;
}
}
}
}
closedir(dir);
}
return true;
}
}
bool FileUtils::Copy(const std::string& src, const std::string& dest) {
struct stat stat_buf;
if (stat(src.c_str(), &stat_buf)) {
LOGV("File::Copy: file %s stat error: %d, %s", src.c_str(), errno,
strerror(errno));
return false;
}
int fd_src = open(src.c_str(), O_RDONLY);
if (fd_src < 0) {
LOGW("File::Copy: unable to open file %s: %d, %s", src.c_str(), errno,
strerror(errno));
return false;
}
int fd_dest = open(dest.c_str(), O_WRONLY | O_CREAT, stat_buf.st_mode);
if (fd_dest < 0) {
LOGW("File::Copy: unable to open file %s: %d, %s", dest.c_str(), errno,
strerror(errno));
close(fd_src);
return false;
}
off_t offset = 0;
bool status = true;
if (sendfile(fd_dest, fd_src, &offset, stat_buf.st_size) < 0) {
LOGV("File::Copy: unable to copy %s to %s: %d, %s", src.c_str(),
dest.c_str(), errno, strerror(errno));
status = false;
}
close(fd_src);
close(fd_dest);
return status;
}
bool FileUtils::List(const std::string& path, std::vector<std::string>* files) {
if (nullptr == files) {
LOGV("File::List: files destination not provided");
return false;
}
if (!FileUtils::Exists(path)) {
LOGV("File::List: path %s does not exist: %d, %s", path.c_str(), errno,
strerror(errno));
return false;
}
DIR* dir = opendir(path.c_str());
if (dir == nullptr) {
LOGW("File::List: unable to open directory %s: %d, %s", path.c_str(), errno,
strerror(errno));
return false;
}
files->clear();
struct dirent* entry;
while ((entry = readdir(dir)) != nullptr) {
if (!IsCurrentOrParentDirectory(entry->d_name)) {
files->push_back(entry->d_name);
}
}
closedir(dir);
return true;
}
bool FileUtils::IsRegularFile(const std::string& path) {
struct stat buf;
if (stat(path.c_str(), &buf) == 0)
return buf.st_mode & S_IFREG;
else
return false;
}
bool FileUtils::IsDirectory(const std::string& path) {
struct stat buf;
if (stat(path.c_str(), &buf) == 0)
return buf.st_mode & S_IFDIR;
else
return false;
}
bool FileUtils::CreateDirectory(const std::string& path_in) {
std::string path = path_in;
size_t size = path.size();
if ((size == 1) && (path[0] == kDirectoryDelimiter)) return true;
if (size <= 1) return false;
size_t pos = path.find(kDirectoryDelimiter, 1);
while (pos < size) {
path[pos] = '\0';
if (mkdir(path.c_str(), 0700) != 0) {
if (errno != EEXIST) {
LOGW("File::CreateDirectory: mkdir failed: %d, %s\n", errno,
strerror(errno));
return false;
}
}
path[pos] = kDirectoryDelimiter;
pos = path.find(kDirectoryDelimiter, pos + 1);
}
if (path[size - 1] != kDirectoryDelimiter) {
if (mkdir(path.c_str(), 0700) != 0) {
if (errno != EEXIST) {
LOGW("File::CreateDirectory: mkdir failed: %d, %s\n", errno,
strerror(errno));
return false;
}
}
}
return true;
}
} // namespace wvutil

144
wvutil/src/log.cpp Normal file
View File

@@ -0,0 +1,144 @@
// Copyright 2018 Google LLC. All Rights Reserved. This file and proprietary
// source code may only be used and distributed under the Widevine License
// Agreement.
//
// Log - implemented using the standard Android logging mechanism
/*
* Qutoing from system/core/include/log/log.h:
* Normally we strip ALOGV (VERBOSE messages) from release builds.
* You can modify this (for example with "#define LOG_NDEBUG 0"
* at the top of your source file) to change that behavior.
*/
#ifndef LOG_NDEBUG
# ifdef NDEBUG
# define LOG_NDEBUG 1
# else
# define LOG_NDEBUG 0
# endif
#endif
#define LOG_TAG "WVCas"
#define LOG_BUF_SIZE 5120
#include "log.h"
#include <hwbinder/IPCThreadState.h>
#include <utils/Log.h>
#include <stdarg.h>
#include <stdio.h>
#include <sys/time.h>
#include <mutex>
#include <string>
/*
* Uncomment the line below if you want to have the LOGV messages to print
* IMPORTANT : this will affect all of CDM
*/
// #define LOG_NDEBUG 0
namespace wvutil {
namespace {
int64_t GetCurrentTimeMs() {
struct timeval tv {};
gettimeofday(&tv, NULL);
auto msec1 = static_cast<int64_t>(tv.tv_sec) * 1000;
auto msec2 = static_cast<int64_t>(tv.tv_usec) / 1000;
return msec1 + msec2;
}
} // namespace
LogPriority g_cutoff = CDM_LOG_INFO;
LogBuffer g_logbuf;
thread_local bool tl_logging_uid_set_ = false;
thread_local uint32_t tl_logging_uid_ = UNKNOWN_UID;
void SetLoggingUid(const uint32_t uid) {
tl_logging_uid_set_ = true;
tl_logging_uid_ = uid;
}
void ClearLoggingUid() {
tl_logging_uid_set_ = false;
tl_logging_uid_ = UNKNOWN_UID;
}
uint32_t GetLoggingUid() { return tl_logging_uid_; }
uint32_t GetIpcCallingUid() {
const auto self = android::hardware::IPCThreadState::selfOrNull();
return self ? self->getCallingUid() : UNKNOWN_UID;
}
void InitLogging() {}
void Log(const char* file, const char* function, int line, LogPriority level,
const char* format, ...) {
const char* filename = strrchr(file, '/');
filename = filename == nullptr ? file : filename + 1;
static thread_local char buf[LOG_BUF_SIZE];
int len =
snprintf(buf, LOG_BUF_SIZE, "[%s(%d):%s] ", filename, line, function);
if (len < 0) len = 0;
if (static_cast<unsigned int>(len) < sizeof(buf)) {
va_list ap;
va_start(ap, format);
vsnprintf(buf + len, LOG_BUF_SIZE - len, format, ap);
va_end(ap);
}
android_LogPriority prio = ANDROID_LOG_VERBOSE;
switch (level) {
case CDM_LOG_SILENT:
return; // It is nonsensical to pass LOG_SILENT.
case CDM_LOG_ERROR:
prio = ANDROID_LOG_ERROR;
break;
case CDM_LOG_WARN:
prio = ANDROID_LOG_WARN;
break;
case CDM_LOG_INFO:
prio = ANDROID_LOG_INFO;
break;
case CDM_LOG_DEBUG:
prio = ANDROID_LOG_DEBUG;
break;
#if LOG_NDEBUG
case CDM_LOG_VERBOSE:
return;
#else
case CDM_LOG_VERBOSE:
prio = ANDROID_LOG_VERBOSE;
break;
#endif
}
__android_log_write(prio, LOG_TAG, buf);
if (level <= CDM_LOG_INFO) {
uint32_t uid = tl_logging_uid_set_ ? tl_logging_uid_ : GetIpcCallingUid();
g_logbuf.addLog({uid, GetCurrentTimeMs(), level, buf});
}
}
void LogBuffer::addLog(const LogMessage& log) {
std::unique_lock<std::mutex> lock(mutex_);
buffer_.push_back(log);
while (buffer_.size() > MAX_CAPACITY) {
buffer_.pop_front();
}
}
std::vector<LogMessage> LogBuffer::getLogs() {
std::unique_lock<std::mutex> lock(mutex_);
return {buffer_.begin(), buffer_.end()};
}
} // namespace wvutil

60
wvutil/src/rw_lock.cpp Normal file
View File

@@ -0,0 +1,60 @@
// Copyright 2019 Google LLC. All Rights Reserved. This file and proprietary
// source code may only be used and distributed under the Widevine License
// Agreement.
#include "rw_lock.h"
#include "log.h"
namespace wvutil {
shared_mutex::~shared_mutex() {
if (reader_count_ > 0) {
LOGE("shared_mutex destroyed with active readers!");
}
if (has_writer_) {
LOGE("shared_mutex destroyed with an active writer!");
}
}
void shared_mutex::lock_shared() {
std::unique_lock<std::mutex> lock(mutex_);
while (has_writer_) {
condition_variable_.wait(lock);
}
++reader_count_;
}
void shared_mutex::unlock_shared() {
std::unique_lock<std::mutex> lock(mutex_);
--reader_count_;
if (reader_count_ == 0) {
condition_variable_.notify_all();
}
}
bool shared_mutex::lock_implementation(bool abort_if_unavailable) {
std::unique_lock<std::mutex> lock(mutex_);
while (reader_count_ > 0 || has_writer_) {
if (abort_if_unavailable) return false;
condition_variable_.wait(lock);
}
has_writer_ = true;
return true;
}
void shared_mutex::unlock() {
std::unique_lock<std::mutex> lock(mutex_);
has_writer_ = false;
condition_variable_.notify_all();
}
} // namespace wvutil

View File

@@ -0,0 +1,343 @@
// Copyright 2018 Google LLC. All Rights Reserved. This file and proprietary
// source code may only be used and distributed under the Widevine License
// Agreement.
#include "string_conversions.h"
#include <ctype.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <iostream>
#include "log.h"
#include "platform.h"
namespace wvutil {
namespace {
// Base64 character set, indexed for their 6-bit mapping, plus '='.
const char kBase64Codes[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
// URL safe Base64 character set.
const char kBase64SafeCodes[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_=";
// Gets the low |n| bits of |in|.
#define GET_LOW_BITS(in, n) ((in) & ((1 << (n)) - 1))
// Gets the given (zero-indexed) bits [a, b) of |in|.
#define GET_BITS(in, a, b) GET_LOW_BITS((in) >> (a), (b) - (a))
// Calculates a/b using round-up division (only works for positive numbers).
#define CEIL_DIVIDE(a, b) ((((a)-1) / (b)) + 1)
// Decodes a single Base64 encoded character into its 6-bit value.
// The provided |codes| must be a Base64 character map.
int DecodeBase64Char(char c, const char* codes) {
const char* c_in_codes = strchr(codes, c);
if (c_in_codes == nullptr) return -1;
const uintptr_t c_in_codes_int = reinterpret_cast<uintptr_t>(c_in_codes);
const uintptr_t codes_int = reinterpret_cast<uintptr_t>(codes);
return static_cast<int>(c_in_codes_int - codes_int);
}
bool DecodeHexChar(char ch, uint8_t* digit) {
if (ch >= '0' && ch <= '9') {
*digit = ch - '0';
return true;
}
ch = tolower(ch);
if ((ch >= 'a') && (ch <= 'f')) {
*digit = ch - 'a' + 10;
return true;
}
return false;
}
// Encode for standard base64 encoding (RFC4648).
// https://en.wikipedia.org/wiki/Base64
// Text | M | a | n |
// ASCI | 77 (0x4d) | 97 (0x61) | 110 (0x6e) |
// Bits | 0 1 0 0 1 1 0 1 0 1 1 0 0 0 0 1 0 1 1 0 1 1 1 0 |
// Index | 19 | 22 | 5 | 46 |
// Base64 | T | W | F | u |
// | <----------------- 24-bits -----------------> |
// The provided |codes| must be a Base64 character map.
std::string Base64EncodeInternal(const uint8_t* data, size_t length,
const char* codes) {
// |temp| stores a 24-bit block that is treated as an array where insertions
// occur from high to low.
uint32_t temp = 0;
size_t out_index = 0;
const size_t out_size = CEIL_DIVIDE(length, 3) * 4;
std::string result(out_size, '\0');
for (size_t i = 0; i < length; i++) {
// "insert" 8-bits of data
temp |= (data[i] << ((2 - (i % 3)) * 8));
if (i % 3 == 2) {
result[out_index++] = codes[GET_BITS(temp, 18, 24)];
result[out_index++] = codes[GET_BITS(temp, 12, 18)];
result[out_index++] = codes[GET_BITS(temp, 6, 12)];
result[out_index++] = codes[GET_BITS(temp, 0, 6)];
temp = 0;
}
}
if (length % 3 == 1) {
result[out_index++] = codes[GET_BITS(temp, 18, 24)];
result[out_index++] = codes[GET_BITS(temp, 12, 18)];
result[out_index++] = '=';
result[out_index++] = '=';
} else if (length % 3 == 2) {
result[out_index++] = codes[GET_BITS(temp, 18, 24)];
result[out_index++] = codes[GET_BITS(temp, 12, 18)];
result[out_index++] = codes[GET_BITS(temp, 6, 12)];
result[out_index++] = '=';
}
return result;
}
std::vector<uint8_t> Base64DecodeInternal(const char* encoded, size_t length,
const char* codes) {
const size_t out_size_max = CEIL_DIVIDE(length * 3, 4);
std::vector<uint8_t> result(out_size_max, '\0');
// |temp| stores 24-bits of data that is treated as an array where insertions
// occur from high to low.
uint32_t temp = 0;
size_t out_index = 0;
size_t i;
for (i = 0; i < length; i++) {
if (encoded[i] == '=') {
// Verify an '=' only appears at the end. We want i to remain at the
// first '=', so we need an inner loop.
for (size_t j = i; j < length; j++) {
if (encoded[j] != '=') {
LOGE("base64Decode failed");
return std::vector<uint8_t>();
}
}
if (length % 4 != 0) {
// If padded, then the length must be a multiple of 4.
// Unpadded messages are OK.
LOGE("base64Decode failed");
return std::vector<uint8_t>();
}
break;
}
const int decoded = DecodeBase64Char(encoded[i], codes);
if (decoded < 0) {
LOGE("base64Decode failed");
return std::vector<uint8_t>();
}
// "insert" 6-bits of data
temp |= (decoded << ((3 - (i % 4)) * 6));
if (i % 4 == 3) {
result[out_index++] = GET_BITS(temp, 16, 24);
result[out_index++] = GET_BITS(temp, 8, 16);
result[out_index++] = GET_BITS(temp, 0, 8);
temp = 0;
}
}
switch (i % 4) {
case 1:
LOGE("base64Decode failed");
return std::vector<uint8_t>();
case 2:
result[out_index++] = GET_BITS(temp, 16, 24);
break;
case 3:
result[out_index++] = GET_BITS(temp, 16, 24);
result[out_index++] = GET_BITS(temp, 8, 16);
break;
}
result.resize(out_index);
return result;
}
} // namespace
// converts an ascii hex string(2 bytes per digit) into a decimal byte string
std::vector<uint8_t> a2b_hex(const std::string& byte) {
std::vector<uint8_t> array;
size_t count = byte.size();
if (count == 0 || (count % 2) != 0) {
LOGE("Invalid input size %zu for string %s", count, byte.c_str());
return array;
}
for (size_t i = 0; i < count / 2; ++i) {
unsigned char msb = 0; // most significant 4 bits
unsigned char lsb = 0; // least significant 4 bits
if (!DecodeHexChar(byte[i * 2], &msb) ||
!DecodeHexChar(byte[i * 2 + 1], &lsb)) {
LOGE("Invalid hex value %c%c at index %zu", byte[i * 2], byte[i * 2 + 1],
i);
return array;
}
array.push_back((msb << 4) | lsb);
}
return array;
}
// converts an ascii hex string(2 bytes per digit) into a decimal byte string
// dump the string with the label.
std::vector<uint8_t> a2b_hex(const std::string& label,
const std::string& byte) {
std::cout << std::endl
<< "[[DUMP: " << label << " ]= \"" << byte << "\"]" << std::endl
<< std::endl;
return a2b_hex(byte);
}
std::string a2bs_hex(const std::string& byte) {
std::vector<uint8_t> array = a2b_hex(byte);
return std::string(array.begin(), array.end());
}
std::string b2a_hex(const std::vector<uint8_t>& byte) {
if (byte.empty()) return "";
return HexEncode(byte.data(), byte.size());
}
std::string unlimited_b2a_hex(const std::vector<uint8_t>& byte) {
if (byte.empty()) return "";
return UnlimitedHexEncode(byte.data(), byte.size());
}
std::string b2a_hex(const std::string& byte) {
if (byte.empty()) return "";
return HexEncode(reinterpret_cast<const uint8_t*>(byte.data()),
byte.length());
}
std::string unlimited_b2a_hex(const std::string& byte) {
if (byte.empty()) return "";
return UnlimitedHexEncode(reinterpret_cast<const uint8_t*>(byte.data()),
byte.length());
}
std::string HexEncode(const uint8_t* in_buffer, size_t size) {
constexpr unsigned int kMaxSafeSize = 2048;
if (size > kMaxSafeSize) size = kMaxSafeSize;
return UnlimitedHexEncode(in_buffer, size);
}
std::string UnlimitedHexEncode(const uint8_t* in_buffer, size_t size) {
static const char kHexChars[] = "0123456789ABCDEF";
if (size == 0) return "";
// Each input byte creates two output hex characters.
std::string out_buffer(size * 2, '\0');
for (unsigned int i = 0; i < size; ++i) {
char byte = in_buffer[i];
out_buffer[(i << 1)] = kHexChars[(byte >> 4) & 0xf];
out_buffer[(i << 1) + 1] = kHexChars[byte & 0xf];
}
return out_buffer;
}
// Standard Base64 encoding and decoding.
std::string Base64Encode(const std::vector<uint8_t>& bin_input) {
if (bin_input.empty()) {
return std::string();
}
return Base64EncodeInternal(bin_input.data(), bin_input.size(), kBase64Codes);
}
std::string Base64Encode(const std::string& bin_input) {
if (bin_input.empty()) {
return std::string();
}
return Base64EncodeInternal(
reinterpret_cast<const uint8_t*>(bin_input.data()), bin_input.size(),
kBase64Codes);
}
// Decode for standard base64 encoding (RFC4648).
std::vector<uint8_t> Base64Decode(const std::string& b64_input) {
if (b64_input.empty()) {
return std::vector<uint8_t>();
}
return Base64DecodeInternal(b64_input.data(), b64_input.size(), kBase64Codes);
}
// URL/Filename Safe Base64 encoding and decoding.
// This is the encoding required to interface with the provisioning server, as
// well as for certain license server transactions. It is also used for logging
// certain strings. The difference between web safe encoding vs regular encoding
// is that the web safe version replaces '+' with '-' and '/' with '_'.
std::string Base64SafeEncode(const std::vector<uint8_t>& bin_input) {
if (bin_input.empty()) {
return std::string();
}
return Base64EncodeInternal(bin_input.data(), bin_input.size(),
kBase64SafeCodes);
}
std::string Base64SafeEncode(const std::string& bin_input) {
if (bin_input.empty()) {
return std::string();
}
return Base64EncodeInternal(
reinterpret_cast<const uint8_t*>(bin_input.data()), bin_input.size(),
kBase64SafeCodes);
}
std::vector<uint8_t> Base64SafeDecode(const std::string& b64_input) {
if (b64_input.empty()) {
return std::vector<uint8_t>();
}
return Base64DecodeInternal(b64_input.data(), b64_input.size(),
kBase64SafeCodes);
}
// URL/Filename Safe Base64 encoding without padding.
std::string Base64SafeEncodeNoPad(const std::vector<uint8_t>& bin_input) {
std::string b64_output = Base64SafeEncode(bin_input);
// Output size: ceiling [ bin_input.size() * 4 / 3 ].
b64_output.resize((bin_input.size() * 4 + 2) / 3);
return b64_output;
}
std::string Base64SafeEncodeNoPad(const std::string& bin_input) {
std::string b64_output = Base64SafeEncode(bin_input);
// Output size: ceiling [ bin_input.size() * 4 / 3 ].
b64_output.resize((bin_input.size() * 4 + 2) / 3);
return b64_output;
}
// Host to Network/Network to Host conversion.
// Convert to big endian (network-byte-order)
int64_t htonll64(int64_t x) {
union {
uint32_t array[2];
int64_t number;
} mixed;
mixed.number = 1;
if (mixed.array[0] == 1) { // Little Endian.
mixed.number = x;
uint32_t temp = mixed.array[0];
mixed.array[0] = htonl(mixed.array[1]);
mixed.array[1] = htonl(temp);
return mixed.number;
} else { // Big Endian.
return x;
}
}
// Encode unsigned integer into a big endian formatted string
std::string EncodeUint32(unsigned int u) {
std::string s;
s.push_back((u >> 24) & 0xFF);
s.push_back((u >> 16) & 0xFF);
s.push_back((u >> 8) & 0xFF);
s.push_back(u & 0xFF);
return s;
}
} // namespace wvutil

104
wvutil/src/timer.cpp Normal file
View File

@@ -0,0 +1,104 @@
// Copyright 2018 Google LLC. All Rights Reserved. This file and proprietary
// source code may only be used and distributed under the Widevine Master
// License Agreement.
//
// Timer class - provides a simple Android specific timer implementation
#include "timer.h"
#include <unistd.h>
#include <utils/Mutex.h>
#include <utils/RefBase.h>
#include <utils/StrongPointer.h>
#include <utils/Thread.h>
namespace wvutil {
class Timer::Impl : virtual public android::RefBase {
private:
class ImplThread : public android::Thread {
public:
ImplThread() : Thread(false), handler_(NULL), period_ns_(0) {}
virtual ~ImplThread() {};
bool Start(TimerHandler *handler, uint32_t time_in_secs) {
handler_ = handler;
period_ns_ = time_in_secs * 1000000000ll;
return run("wvutil::Timer::Impl") == android::NO_ERROR;
}
void Stop() {
{
android::Mutex::Autolock autoLock(lock_);
stop_condition_.signal();
}
requestExitAndWait();
}
private:
virtual bool threadLoop() {
android::Mutex::Autolock autoLock(lock_);
stop_condition_.waitRelative(lock_, period_ns_);
handler_->OnTimerEvent();
return true;
}
TimerHandler *handler_;
uint64_t period_ns_;
android::Mutex lock_;
android::Condition stop_condition_;
CORE_DISALLOW_COPY_AND_ASSIGN(ImplThread);
};
android::sp<ImplThread> impl_thread_;
public:
Impl() {}
virtual ~Impl() {};
bool Start(TimerHandler *handler, uint32_t time_in_secs) {
impl_thread_ = new ImplThread();
return impl_thread_->Start(handler, time_in_secs);
}
void Stop() {
impl_thread_->Stop();
impl_thread_.clear();
}
bool IsRunning() {
return (impl_thread_ != NULL) && (impl_thread_->isRunning());
}
CORE_DISALLOW_COPY_AND_ASSIGN(Impl);
};
Timer::Timer() : impl_(new Timer::Impl()) {
}
Timer::~Timer() {
if (IsRunning())
Stop();
delete impl_;
impl_ = NULL;
}
bool Timer::Start(TimerHandler *handler, uint32_t time_in_secs) {
if (!handler || time_in_secs == 0)
return false;
return impl_->Start(handler, time_in_secs);
}
void Timer::Stop() {
impl_->Stop();
}
bool Timer::IsRunning() {
return impl_->IsRunning();
}
} // namespace wvutil