From 70f62ed75e1facef2c117ec6a6cd290dcf28994f Mon Sep 17 00:00:00 2001 From: Fang Yu Date: Fri, 26 Oct 2018 11:20:39 -0700 Subject: [PATCH] (1) Return a boolean instead of util::Status for methods in wv_cas_ecm.h (2) Update wv_cas_ecm_example to NOT depend on ABSL ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=218884631 --- example/BUILD | 2 - example/wv_cas_ecm_example.cc | 46 +++---- media_cas_packager_sdk/public/wv_cas_ecm.cc | 99 +++++++------- media_cas_packager_sdk/public/wv_cas_ecm.h | 32 +++-- .../public/wv_cas_ecm_test.cc | 127 ++++++++---------- 5 files changed, 142 insertions(+), 164 deletions(-) diff --git a/example/BUILD b/example/BUILD index 32e54bf..f0514df 100644 --- a/example/BUILD +++ b/example/BUILD @@ -48,8 +48,6 @@ cc_binary( srcs = ["wv_cas_ecm_example.cc"], deps = [ "//base", - "@abseil_repo//absl/strings", - "//util:status", "//media_cas_packager_sdk/public:wv_cas_ecm", ], ) diff --git a/example/wv_cas_ecm_example.cc b/example/wv_cas_ecm_example.cc index 3b5a1f3..2d94154 100644 --- a/example/wv_cas_ecm_example.cc +++ b/example/wv_cas_ecm_example.cc @@ -8,47 +8,43 @@ // Example of how to use the wv_cas_ecm library. +#include #include -#include "gflags/gflags.h" -#include "glog/logging.h" -#include "absl/strings/escaping.h" -#include "absl/strings/str_cat.h" -#include "util/status.h" #include "media_cas_packager_sdk/public/wv_cas_ecm.h" +const int kContentIvSize = 16; +const bool kKeyRotationEnabled = true; +const int kCryptoMode = 1; // CTR const char kEvenKey[] = "even_content_key"; // 16 bytes const char kEvenContentIv8Bytes[] = "evencont"; // 8 bytes +const char kEvenContentIv16Bytes[] = "evencontevencont"; // 16 bytes const char kOddKey[] = "odd_content_key."; // 16 bytes const char kOddContentIv8Bytes[] = "oddcont."; // 8 bytes +const char kOddContentIv16Bytes[] = "oddcont.oddcont."; // 16 bytes const char kEntitlementKeyId[] = "ent_key_id......"; // 16 bytes const char kEntitlementKey[] = "entitlement_key................."; // 32 bytes int main(int argc, char **argv) { - gflags::ParseCommandLineFlags(&argc, &argv, true); - widevine::cas::WvCasEcm wv_cas_ecm; - auto status = wv_cas_ecm.Initialize(/* content_iv_size= */ 16, - /* key_rotation_enabled= */ true, - /* crypto_mode= */ 1); - if (!status.ok()) { - LOG(FATAL) << "Failed to initialize WV CAS ECM, error: " << status; + bool status = + wv_cas_ecm.Initialize(kContentIvSize, kKeyRotationEnabled, kCryptoMode); + if (!status) { + std::cerr << "Failed to initialize WV CAS ECM, error." << std::endl; } std::string ecm; - status = wv_cas_ecm.GenerateEcm( - /* even_key= */ kEvenKey, - /* even_content_iv= */ - absl::StrCat(kEvenContentIv8Bytes, kEvenContentIv8Bytes), - /* odd_key= */ kOddKey, - /* odd_content_iv= */ - absl::StrCat(kOddContentIv8Bytes, kOddContentIv8Bytes), - /* entitlement_key_id= */ kEntitlementKeyId, - /* entitlement_key= */ kEntitlementKey, &ecm); - if (!status.ok()) { - LOG(FATAL) << "Failed to generate WV CAS ECM, error: " << status; + status = wv_cas_ecm.GenerateEcm(kEvenKey, kEvenContentIv16Bytes, kOddKey, + kOddContentIv16Bytes, kEntitlementKeyId, + kEntitlementKey, &ecm); + if (!status) { + std::cerr << "Failed to generate WV CAS ECM, error" << std::endl; } else { - LOG(INFO) << "Generated WV CAS ECM (in hex): " - << absl::BytesToHexString(ecm); + std::cout << "ECM size: " << ecm.size() << std::endl; + std::cout << "ECM bytes: "; + for (size_t i = 0; i < ecm.size(); i++) { + printf("'\\x%x', ", static_cast(ecm.at(i))); + } + std::cout << std::endl; } return 0; diff --git a/media_cas_packager_sdk/public/wv_cas_ecm.cc b/media_cas_packager_sdk/public/wv_cas_ecm.cc index a642ba6..eb4606a 100644 --- a/media_cas_packager_sdk/public/wv_cas_ecm.cc +++ b/media_cas_packager_sdk/public/wv_cas_ecm.cc @@ -52,53 +52,52 @@ EcmInitParameters CreateEcmInitParameters(int content_iv_size, } // namespace -util::Status WvCasEcm::Initialize(int content_iv_size, - bool key_rotation_enabled, int crypto_mode) { +// TODO(user): Return a status class like +// instead of boolean. + +bool WvCasEcm::Initialize(int content_iv_size, bool key_rotation_enabled, + int crypto_mode) { if (initialized_) { - return util::Status( - util::error::INTERNAL, - "Cannot initialize an instance of WvCasEcm more than once"); + LOG(ERROR) << "Cannot initialize an instance of WvCasEcm more than once"; + return false; } if (content_iv_size != 8 && content_iv_size != 16) { - return util::Status(util::error::INVALID_ARGUMENT, - "Only support content_iv_size being 8 now"); + LOG(ERROR) << "Only support content_iv_size being 8 now"; + return false; } content_iv_size_ = content_iv_size; key_rotation_enabled_ = key_rotation_enabled; if (crypto_mode != kCryptoModeCtr) { - return util::Status(util::error::INVALID_ARGUMENT, - "Only CTR crypto mode is supported by Widevine plugin " - "for content encryption"); + LOG(ERROR) << "Only CTR crypto mode is supported by Widevine plugin for " + "content encryption"; + return false; } crypto_mode_ = crypto_mode; initialized_ = true; - return util::OkStatus(); + return true; } -util::Status WvCasEcm::GenerateEcm(const std::string& even_key, - - const std::string& even_content_iv, - const std::string& odd_key, - const std::string& odd_content_iv, - const std::string& entitlement_key_id, - const std::string& entitlement_key, - std::string* ecm) const { +bool WvCasEcm::GenerateEcm(const std::string& even_key, + const std::string& even_content_iv, const std::string& odd_key, + const std::string& odd_content_iv, + const std::string& entitlement_key_id, + const std::string& entitlement_key, std::string* ecm) const { DCHECK(ecm); if (!initialized_) { - return util::Status(util::error::INTERNAL, - "WvCasEcm has not been properly initialized"); + LOG(ERROR) << "WvCasEcm has not been properly initialized"; + return false; } if (!key_rotation_enabled_) { - return util::Status(util::error::INTERNAL, - "Please call GenerateSingleKeyEcm() instead when key " - "rotation is disabled"); + LOG(ERROR) << "Please call GenerateSingleKeyEcm() instead when key " + "rotation is disabled"; + return false; } if (even_content_iv.size() != content_iv_size_ || odd_content_iv.size() != content_iv_size_) { - return util::Status(util::error::INVALID_ARGUMENT, - "Size of content IV is incorrect"); + LOG(ERROR) << "Size of content IV is incorrect"; + return false; } // Create an instance of CasEcm in order to set the entitlement keys. @@ -117,7 +116,8 @@ util::Status WvCasEcm::GenerateEcm(const std::string& even_key, if (!(status = cas_ecm->Initialize(kDefaultContentId, kDefaultProvider, ecm_init_params, &entitlement_request)) .ok()) { - return status; + LOG(ERROR) << "Failed to initialize ECM class."; + return false; } FixedKeyFetcher fixed_key_fetcher( /* even_entitlement_key_id= */ entitlement_key_id, @@ -127,11 +127,13 @@ util::Status WvCasEcm::GenerateEcm(const std::string& even_key, if (!(status = fixed_key_fetcher.RequestEntitlementKey(entitlement_request, &entitlement_response)) .ok()) { - return status; + LOG(ERROR) << "Failed to get entitlement key."; + return false; } if (!(status = cas_ecm->ProcessCasEncryptionResponse(entitlement_response)) .ok()) { - return status; + LOG(ERROR) << "Failed to process entitlement key."; + return false; } // Generate ECM. @@ -153,27 +155,27 @@ util::Status WvCasEcm::GenerateEcm(const std::string& even_key, ecm_param.key_params[1].content_ivs.push_back(odd_content_iv); *ecm = ecm_generator.GenerateEcm(ecm_param); - return util::OkStatus(); + return true; } -util::Status WvCasEcm::GenerateSingleKeyEcm(const std::string& even_key, - const std::string& even_content_iv, - const std::string& entitlement_key_id, - const std::string& entitlement_key, - std::string* ecm) const { +bool WvCasEcm::GenerateSingleKeyEcm(const std::string& even_key, + const std::string& even_content_iv, + const std::string& entitlement_key_id, + const std::string& entitlement_key, + std::string* ecm) const { + DCHECK(ecm); if (!initialized_) { - DCHECK(ecm); - return util::Status(util::error::INTERNAL, - "WvCasEcm has not been properly initialized"); + LOG(ERROR) << "WvCasEcm has not been properly initialized"; + return false; } if (key_rotation_enabled_) { - return util::Status( - util::error::INTERNAL, - "Please call GenerateEcm() instead when key rotation is enabled"); + LOG(ERROR) + << "Please call GenerateEcm() instead when key rotation is enabled"; + return false; } if (even_content_iv.size() != content_iv_size_) { - return util::Status(util::error::INVALID_ARGUMENT, - "Size of content IV is incorrect"); + LOG(ERROR) << "Size of content IV is incorrect"; + return false; } // Create an instance of CasEcm in order to set the entitlement keys. @@ -192,7 +194,8 @@ util::Status WvCasEcm::GenerateSingleKeyEcm(const std::string& even_key, if (!(status = cas_ecm->Initialize(kDefaultContentId, kDefaultProvider, ecm_init_params, &entitlement_request)) .ok()) { - return status; + LOG(ERROR) << "Failed to get initialize ECM class."; + return false; } FixedKeyFetcher fixed_key_fetcher( /* even_entitlement_key_id= */ entitlement_key_id, @@ -202,11 +205,13 @@ util::Status WvCasEcm::GenerateSingleKeyEcm(const std::string& even_key, if (!(status = fixed_key_fetcher.RequestEntitlementKey(entitlement_request, &entitlement_response)) .ok()) { - return status; + LOG(ERROR) << "Failed to get entitlement key."; + return false; } if (!(status = cas_ecm->ProcessCasEncryptionResponse(entitlement_response)) .ok()) { - return status; + LOG(ERROR) << "Failed to process entitlement key."; + return false; } // Generate ECM. @@ -222,7 +227,7 @@ util::Status WvCasEcm::GenerateSingleKeyEcm(const std::string& even_key, ecm_param.key_params[0].content_ivs.push_back(even_content_iv); *ecm = ecm_generator.GenerateEcm(ecm_param); - return util::OkStatus(); + return true; } } // namespace cas diff --git a/media_cas_packager_sdk/public/wv_cas_ecm.h b/media_cas_packager_sdk/public/wv_cas_ecm.h index b59f785..020bffd 100644 --- a/media_cas_packager_sdk/public/wv_cas_ecm.h +++ b/media_cas_packager_sdk/public/wv_cas_ecm.h @@ -47,12 +47,12 @@ class WvCasEcm { // only CTR is supported by Widevine CAS plugin for now // // Returns: - // - A status indicating whether there was any error during initialization + // - A boolean indicating whether there was any error during initialization // // Note: // - 'even'/'odd' key in the ECM will be be encrypted using AEC_CBC - virtual util::Status Initialize(int content_iv_size, - bool key_rotation_enabled, int crypto_mode); + virtual bool Initialize(int content_iv_size, bool key_rotation_enabled, + int crypto_mode); // Generate an ECM containing two keys (even and odd). Can be called when // |key_rotation_enabled| is initialized to 'true'. @@ -67,20 +67,18 @@ class WvCasEcm { // - |ecm| for returning the generated ECM, must not be nullptr // // Returns: - // - A status indicating whether there was any error during processing + // - A boolean indicating whether there was any error during processing // // Note: // - The same |entitlement_key| will be used to encrypt both |even_key| // and |odd_key| in the ECM // - Size of |even_content_iv| and |odd_content_iv| must match // |content_iv_size| set during initialization - virtual util::Status GenerateEcm(const std::string& even_key, - const std::string& even_content_iv, - const std::string& odd_key, - const std::string& odd_content_iv, - const std::string& entitlement_key_id, - const std::string& entitlement_key, - std::string* ecm) const; + virtual bool GenerateEcm(const std::string& even_key, + const std::string& even_content_iv, const std::string& odd_key, + const std::string& odd_content_iv, + const std::string& entitlement_key_id, + const std::string& entitlement_key, std::string* ecm) const; // Generate an ECM containing only a singe even key. Can be called when // |key_rotation_enabled| is initialized to 'false'. @@ -93,16 +91,16 @@ class WvCasEcm { // - |ecm| for returning the generated ECM, must not be nullptr // // Returns: - // - A status indicating whether there was any error during processing + // - A boolean indicating whether there was any error during processing // // Note: // - Size of |even_content_iv| and |odd_content_iv| must match // |content_iv_size| set during initialization - virtual util::Status GenerateSingleKeyEcm(const std::string& even_key, - const std::string& even_content_iv, - const std::string& entitlement_key_id, - const std::string& entitlement_key, - std::string* ecm) const; + virtual bool GenerateSingleKeyEcm(const std::string& even_key, + const std::string& even_content_iv, + const std::string& entitlement_key_id, + const std::string& entitlement_key, + std::string* ecm) const; private: bool initialized_ = false; diff --git a/media_cas_packager_sdk/public/wv_cas_ecm_test.cc b/media_cas_packager_sdk/public/wv_cas_ecm_test.cc index 96da3ff..3d7b8fe 100644 --- a/media_cas_packager_sdk/public/wv_cas_ecm_test.cc +++ b/media_cas_packager_sdk/public/wv_cas_ecm_test.cc @@ -30,94 +30,75 @@ class WvCasEcmTest : public Test { }; TEST_F(WvCasEcmTest, InitializeTwice) { - EXPECT_OK(wv_cas_ecm_.Initialize(/* content_iv_size= */ 8, - /* key_rotation_enabled= */ true, - /* crypto_mode= */ 1)); - EXPECT_EQ(util::error::INTERNAL, - wv_cas_ecm_ - .Initialize(/* content_iv_size= */ 8, - /* key_rotation_enabled= */ true, - /* crypto_mode= */ 1) - .error_code()); + EXPECT_TRUE(wv_cas_ecm_.Initialize(/* content_iv_size= */ 8, + /* key_rotation_enabled= */ true, + /* crypto_mode= */ 1)); + EXPECT_FALSE(wv_cas_ecm_.Initialize(/* content_iv_size= */ 8, + /* key_rotation_enabled= */ true, + /* crypto_mode= */ 1)); } TEST_F(WvCasEcmTest, InitializeContentIvSize) { - EXPECT_EQ( - util::error::INVALID_ARGUMENT, - wv_cas_ecm_ - .Initialize(/* content_iv_size= */ 4, - /* key_rotation_enabled= */ true, /* crypto_mode= */ 1) - .error_code()); + EXPECT_FALSE(wv_cas_ecm_.Initialize(/* content_iv_size= */ 4, + /* key_rotation_enabled= */ true, + /* crypto_mode= */ 1)); } TEST_F(WvCasEcmTest, InitializeCryptoMode) { - EXPECT_EQ( - util::error::INVALID_ARGUMENT, - wv_cas_ecm_ - .Initialize(/* content_iv_size= */ 8, - /* key_rotation_enabled= */ true, /* crypto_mode= */ 0) - .error_code()); + EXPECT_FALSE(wv_cas_ecm_.Initialize(/* content_iv_size= */ 8, + /* key_rotation_enabled= */ true, + /* crypto_mode= */ 0)); } TEST_F(WvCasEcmTest, InitializeKeyRotationEnabled) { - EXPECT_OK(wv_cas_ecm_.Initialize(/* content_iv_size= */ 8, - /* key_rotation_enabled= */ true, - /* crypto_mode= */ 1)); + EXPECT_TRUE(wv_cas_ecm_.Initialize(/* content_iv_size= */ 8, + /* key_rotation_enabled= */ true, + /* crypto_mode= */ 1)); std::string actual_ecm; - EXPECT_EQ(util::error::INTERNAL, - wv_cas_ecm_.GenerateSingleKeyEcm("", "", "", "", &actual_ecm) - .error_code()); + EXPECT_FALSE(wv_cas_ecm_.GenerateSingleKeyEcm("", "", "", "", &actual_ecm)); } TEST_F(WvCasEcmTest, InitializeKeyRotationDisabled) { - EXPECT_OK(wv_cas_ecm_.Initialize(/* content_iv_size= */ 8, - /* key_rotation_enabled= */ false, - /* crypto_mode= */ 1)); + EXPECT_TRUE(wv_cas_ecm_.Initialize(/* content_iv_size= */ 8, + /* key_rotation_enabled= */ false, + /* crypto_mode= */ 1)); std::string actual_ecm; - EXPECT_EQ(util::error::INTERNAL, - wv_cas_ecm_.GenerateEcm("", "", "", "", "", "", &actual_ecm) - .error_code()); + EXPECT_FALSE(wv_cas_ecm_.GenerateEcm("", "", "", "", "", "", &actual_ecm)); } TEST_F(WvCasEcmTest, GenerateEcmInvalidContentIv) { - EXPECT_OK(wv_cas_ecm_.Initialize(/* content_iv_size= */ 8, - /* key_rotation_enabled= */ true, - /* crypto_mode= */ 1)); + EXPECT_TRUE(wv_cas_ecm_.Initialize(/* content_iv_size= */ 8, + /* key_rotation_enabled= */ true, + /* crypto_mode= */ 1)); std::string actual_ecm; - EXPECT_EQ(util::error::INVALID_ARGUMENT, - wv_cas_ecm_ - .GenerateEcm( - /* even_key= */ kEvenKey, - /* even_content_iv= */ kEvenContentIv8Bytes, - /* odd_key= */ kOddKey, - /* odd_content_iv= */ "123456789", - /* entitlement_key_id= */ kEntitlementKeyId, - /* entitlement_key= */ kEntitlementKey, &actual_ecm) - .error_code()); + EXPECT_FALSE(wv_cas_ecm_.GenerateEcm( + /* even_key= */ kEvenKey, + /* even_content_iv= */ kEvenContentIv8Bytes, + /* odd_key= */ kOddKey, + /* odd_content_iv= */ "123456789", + /* entitlement_key_id= */ kEntitlementKeyId, + /* entitlement_key= */ kEntitlementKey, &actual_ecm)); } TEST_F(WvCasEcmTest, GenerateSingleKeyEcmInvalidContentIv) { - EXPECT_OK(wv_cas_ecm_.Initialize(/* content_iv_size= */ 8, - /* key_rotation_enabled= */ false, - /* crypto_mode= */ 1)); + EXPECT_TRUE(wv_cas_ecm_.Initialize(/* content_iv_size= */ 8, + /* key_rotation_enabled= */ false, + /* crypto_mode= */ 1)); std::string actual_ecm; - EXPECT_EQ(util::error::INVALID_ARGUMENT, - wv_cas_ecm_ - .GenerateSingleKeyEcm( - /* even_key= */ kEvenKey, - /* even_content_iv= */ "1234", - /* entitlement_key_id= */ kEntitlementKeyId, - /* entitlement_key= */ kEntitlementKey, &actual_ecm) - .error_code()); + EXPECT_FALSE(wv_cas_ecm_.GenerateSingleKeyEcm( + /* even_key= */ kEvenKey, + /* even_content_iv= */ "1234", + /* entitlement_key_id= */ kEntitlementKeyId, + /* entitlement_key= */ kEntitlementKey, &actual_ecm)); } TEST_F(WvCasEcmTest, GenerateEcm8BytesContentIvSuccess) { - EXPECT_OK(wv_cas_ecm_.Initialize(/* content_iv_size= */ 8, - /* key_rotation_enabled= */ true, - /* crypto_mode= */ 1)); + EXPECT_TRUE(wv_cas_ecm_.Initialize(/* content_iv_size= */ 8, + /* key_rotation_enabled= */ true, + /* crypto_mode= */ 1)); std::string actual_ecm; - EXPECT_OK(wv_cas_ecm_.GenerateEcm( + EXPECT_TRUE(wv_cas_ecm_.GenerateEcm( /* even_key= */ kEvenKey, /* even_content_iv= */ kEvenContentIv8Bytes, /* odd_key= */ kOddKey, @@ -135,12 +116,12 @@ TEST_F(WvCasEcmTest, GenerateEcm8BytesContentIvSuccess) { } TEST_F(WvCasEcmTest, GenerateSingleKeyEcm8BytesContentIvSuccess) { - EXPECT_OK(wv_cas_ecm_.Initialize(/* content_iv_size= */ 8, - /* key_rotation_enabled= */ false, - /* crypto_mode= */ 1)); + EXPECT_TRUE(wv_cas_ecm_.Initialize(/* content_iv_size= */ 8, + /* key_rotation_enabled= */ false, + /* crypto_mode= */ 1)); std::string actual_ecm; - EXPECT_OK(wv_cas_ecm_.GenerateSingleKeyEcm( + EXPECT_TRUE(wv_cas_ecm_.GenerateSingleKeyEcm( /* even_key= */ kEvenKey, /* even_content_iv= */ kEvenContentIv8Bytes, /* entitlement_key_id= */ kEntitlementKeyId, @@ -154,12 +135,12 @@ TEST_F(WvCasEcmTest, GenerateSingleKeyEcm8BytesContentIvSuccess) { } TEST_F(WvCasEcmTest, GenerateEcm16BytesContentIvSuccess) { - EXPECT_OK(wv_cas_ecm_.Initialize(/* content_iv_size= */ 16, - /* key_rotation_enabled= */ true, - /* crypto_mode= */ 1)); + EXPECT_TRUE(wv_cas_ecm_.Initialize(/* content_iv_size= */ 16, + /* key_rotation_enabled= */ true, + /* crypto_mode= */ 1)); std::string actual_ecm; - EXPECT_OK(wv_cas_ecm_.GenerateEcm( + EXPECT_TRUE(wv_cas_ecm_.GenerateEcm( /* even_key= */ kEvenKey, /* even_content_iv= */ absl::StrCat(kEvenContentIv8Bytes, kEvenContentIv8Bytes), @@ -179,12 +160,12 @@ TEST_F(WvCasEcmTest, GenerateEcm16BytesContentIvSuccess) { } TEST_F(WvCasEcmTest, GenerateSingleKeyEcm16BytesContentIvSuccess) { - EXPECT_OK(wv_cas_ecm_.Initialize(/* content_iv_size= */ 16, - /* key_rotation_enabled= */ false, - /* crypto_mode= */ 1)); + EXPECT_TRUE(wv_cas_ecm_.Initialize(/* content_iv_size= */ 16, + /* key_rotation_enabled= */ false, + /* crypto_mode= */ 1)); std::string actual_ecm; - EXPECT_OK(wv_cas_ecm_.GenerateSingleKeyEcm( + EXPECT_TRUE(wv_cas_ecm_.GenerateSingleKeyEcm( /* even_key= */ kEvenKey, /* even_content_iv= */ absl::StrCat(kEvenContentIv8Bytes, kEvenContentIv8Bytes),