1) Change return type to WvCasStatus for functions in wv_cas_types.cc.

2) Add a binary wv_cas_types_example.
3) Surface wv_cas_key_fetcher *source code* to partner to serve as an example of how they would make a HTTP request to acquire an entitlement key from license server.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=229953562
This commit is contained in:
Fang Yu
2019-01-18 10:38:34 -08:00
parent 6e1f377329
commit e7173c82cd
25 changed files with 1213 additions and 173 deletions

View File

@@ -18,7 +18,9 @@ filegroup(
srcs = [
"test_ecmg_messages.h",
"wv_cas_ecm_example.cc",
"wv_cas_types_example.cc",
":wv_cas_ecm_example",
":wv_cas_types_example",
],
)
@@ -32,10 +34,16 @@ cc_library(
hdrs = ["test_ecmg_messages.h"],
)
cc_library(
name = "test_emmg_messages",
hdrs = ["test_emmg_messages.h"],
)
cc_binary(
name = "wv_cas_ecm_example",
srcs = ["wv_cas_ecm_example.cc"],
deps = [
"//base",
"@abseil_repo//absl/base:core_headers",
"//media_cas_packager_sdk/public:wv_cas_ecm",
"//media_cas_packager_sdk/public:wv_cas_types",
@@ -52,3 +60,13 @@ cc_binary(
"//protos/public:media_cas_encryption_proto",
],
)
cc_binary(
name = "wv_cas_types_example",
srcs = ["wv_cas_types_example.cc"],
deps = [
"//base",
"@abseil_repo//absl/base:core_headers",
"//media_cas_packager_sdk/public:wv_cas_types",
],
)

View File

@@ -14,7 +14,7 @@
namespace widevine {
namespace cas {
const char kTestChannelSetup[] = {
const char kTestEcmgChannelSetup[] = {
'\x03', // protocol_version
'\x00', '\x01', // message_type - Channel_setup
'\x00', '\x0e', // message_length
@@ -26,7 +26,7 @@ const char kTestChannelSetup[] = {
'\x4a', '\xd4', '\x00', '\x00' // parameter_value
};
const char kTestChannelStatus[] = {
const char kTestEcmgChannelStatus[] = {
'\x03', // protocol_version
'\x00', '\x03', // message_type - Channel_status
'\x00', '\x39', // message_length
@@ -62,7 +62,7 @@ const char kTestChannelStatus[] = {
'\x00', '\x64' // parameter_value
};
const char kTestStreamSetup[] = {
const char kTestEcmgStreamSetup[] = {
'\x03', // protocol_version
'\x01', '\x01', // message_type - Stream_setup
'\x00', '\x18', // message_length
@@ -80,7 +80,7 @@ const char kTestStreamSetup[] = {
'\x00', '\x64' // parameter_value
};
const char kTestStreamStatus[] = {
const char kTestEcmgStreamStatus[] = {
'\x03', // protocol_version
'\x01', '\x03', // message_type - Stream_status
'\x00', '\x17', // message_length
@@ -98,7 +98,7 @@ const char kTestStreamStatus[] = {
'\x01' // parameter_value
};
const char kTestCwProvision[] = {
const char kTestEcmgCwProvision[] = {
'\x03', // protocol_version
'\x02', '\x01', // message_type - CW_provision
'\x00', '\x44', // message_length
@@ -126,7 +126,7 @@ const char kTestCwProvision[] = {
'\x18', '\x19', '\x1a', '\x1b', '\x1c', '\x1d', '\x1e', '\x1f'};
// CW is encrypted using hardcoded fixed entitlement key.
const char kTestEcmResponse[] = {
const char kTestEcmgEcmResponse[] = {
'\x03', // protocol_version
'\x02', '\x02', // message_type - ECM_response
'\x00', '\xd2', // message_length
@@ -164,7 +164,7 @@ const char kTestEcmResponse[] = {
'\xff', '\xff', '\xff', '\xff', '\xff', '\xff', '\xff', '\xff', '\xff',
'\xff', '\xff', '\xff', '\xff', '\xff', '\xff', '\xff', '\xff'};
const char kTestStreamCloseRequest[] = {
const char kTestEcmgStreamCloseRequest[] = {
'\x03', // protocol_version
'\x01', '\x04', // message_type - Stream_close_request
'\x00', '\x0c', // message_length
@@ -176,7 +176,7 @@ const char kTestStreamCloseRequest[] = {
'\x00', '\x01' // parameter_value
};
const char kTestStreamCloseResponse[] = {
const char kTestEcmgStreamCloseResponse[] = {
'\x03', // protocol_version
'\x01', '\x05', // message_type - Stream_close_response
'\x00', '\x0c', // message_length
@@ -188,7 +188,7 @@ const char kTestStreamCloseResponse[] = {
'\x00', '\x01' // parameter_value
};
const char kTestChannelClose[] = {
const char kTestEcmgChannelClose[] = {
'\x03', // protocol_version
'\x00', '\x04', // message_type - Channel_close
'\x00', '\x06', // message_length

View File

@@ -0,0 +1,146 @@
////////////////////////////////////////////////////////////////////////////////
// Copyright 2019 Google LLC.
//
// This software is licensed under the terms defined in the Widevine Master
// License Agreement. For a copy of this agreement, please contact
// widevine-licensing@google.com.
////////////////////////////////////////////////////////////////////////////////
// Example EMMG messages used in unit tests.
#ifndef MEDIA_CAS_PACKAGER_SDK_EXAMPLE_TEST_EMMG_MESSAGES_H_
#define MEDIA_CAS_PACKAGER_SDK_EXAMPLE_TEST_EMMG_MESSAGES_H_
namespace widevine {
namespace cas {
const char kTestEmmgTsPacket[] = {
'\x47', '\x5F', '\xFF', '\x10', '\x00', '\x82', '\x70', '\x61', '\x00',
'\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00',
'\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00',
'\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00',
'\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00',
'\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00',
'\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00',
'\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00',
'\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00',
'\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00',
'\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00',
'\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x83', '\x70', '\x61',
'\x01', '\x01', '\x01', '\x01', '\x01', '\x01', '\x01', '\x01', '\x01',
'\x01', '\x01', '\x01', '\x01', '\x01', '\x01', '\x01', '\x01', '\x01',
'\x01', '\x01', '\x01', '\x01', '\x01', '\x01', '\x01', '\x01', '\x01',
'\x01', '\x01', '\x01', '\x01', '\x01', '\x01', '\x01', '\x01', '\x01',
'\x01', '\x01', '\x01', '\x01', '\x01', '\x01', '\x01', '\x01', '\x01',
'\x01', '\x01', '\x01', '\x01', '\x01', '\x01', '\x01', '\x01', '\x01',
'\x01', '\x01', '\x01', '\x01', '\x01', '\x01', '\x01', '\x01', '\x01',
'\x01', '\x01', '\x01', '\x01', '\x01', '\x01', '\x01', '\x01', '\x01',
'\x01', '\x01', '\x01', '\x01', '\x01', '\x01', '\x01', '\x01'};
const char kTestEmmgChannelSetup[] = {
'\x02', // protocol_version
'\x00', '\x11', // message_type - Channel_setup
'\x00', '\x13', // message_length
'\x00', '\x01', // parameter_type - client_id
'\x00', '\x04', // parameter_length
'\x4a', '\xd4', '\x00', '\x00', // parameter_value
'\x00', '\x03', // parameter_type - data_channel_id
'\x00', '\x02', // parameter_length
'\x00', '\x01', // parameter_value
'\x00', '\x02', // parameter_type - section_TSpkt_flag
'\x00', '\x01', // parameter_length
'\x01' // parameter_value
};
const char kTestEmmgStreamSetup[] = {
'\x02', // protocol_version
'\x01', '\x11', // message_type - Stream_setup
'\x00', '\x1f', // message_length
'\x00', '\x01', // parameter_type - client_id
'\x00', '\x04', // parameter_length
'\x4a', '\xd4', '\x00', '\x00', // parameter_value
'\x00', '\x03', // parameter_type - data_channel_id
'\x00', '\x02', // parameter_length
'\x00', '\x01', // parameter_value
'\x00', '\x04', // parameter_type - data_stream_id
'\x00', '\x02', // parameter_length
'\x00', '\x01', // parameter_value
'\x00', '\x08', // parameter_type - data_id
'\x00', '\x02', // parameter_length
'\x00', '\x01', // parameter_value
'\x00', '\x07', // parameter_type - data_type
'\x00', '\x01', // parameter_length
'\x01' // parameter_value - private data
};
const char kTestEmmgDataProvision[] = {
'\x02', // protocol_version
'\x02', '\x11', // message_type - Data_provision
'\x00', '\xda', // message_length
'\x00', '\x01', // parameter_type - client_id
'\x00', '\x04', // parameter_length
'\x4a', '\xd4', '\x00', '\x00', // parameter_value
'\x00', '\x03', // parameter_type - data_channel_id
'\x00', '\x02', // parameter_length
'\x00', '\x01', // parameter_value
'\x00', '\x04', // parameter_type - data_stream_id
'\x00', '\x02', // parameter_length
'\x00', '\x01', // parameter_value
'\x00', '\x08', // parameter_type - data_id
'\x00', '\x02', // parameter_length
'\x00', '\x01', // parameter_value
'\x00', '\x05', // parameter_type - datagram
'\x00', '\xbc', // parameter_length
'\x47', '\x5f', '\xff', '\x10', '\x00', '\x82', '\x70', '\x61', '\x00',
'\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00',
'\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00',
'\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00',
'\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00',
'\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00',
'\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00',
'\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00',
'\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00',
'\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00',
'\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00',
'\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x83', '\x70', '\x61',
'\x01', '\x01', '\x01', '\x01', '\x01', '\x01', '\x01', '\x01', '\x01',
'\x01', '\x01', '\x01', '\x01', '\x01', '\x01', '\x01', '\x01', '\x01',
'\x01', '\x01', '\x01', '\x01', '\x01', '\x01', '\x01', '\x01', '\x01',
'\x01', '\x01', '\x01', '\x01', '\x01', '\x01', '\x01', '\x01', '\x01',
'\x01', '\x01', '\x01', '\x01', '\x01', '\x01', '\x01', '\x01', '\x01',
'\x01', '\x01', '\x01', '\x01', '\x01', '\x01', '\x01', '\x01', '\x01',
'\x01', '\x01', '\x01', '\x01', '\x01', '\x01', '\x01', '\x01', '\x01',
'\x01', '\x01', '\x01', '\x01', '\x01', '\x01', '\x01', '\x01', '\x01',
'\x01', '\x01', '\x01', '\x01', '\x01', '\x01', '\x01', '\x01'};
const char kTestEmmgStreamCloseRequest[] = {
'\x02', // protocol_version
'\x01', '\x14', // message_type - Stream_close_request
'\x00', '\x14', // message_length
'\x00', '\x01', // parameter_type - client_id
'\x00', '\x04', // parameter_length
'\x4a', '\xd4', '\x00', '\x00', // parameter_value
'\x00', '\x03', // parameter_type - data_channel_id
'\x00', '\x02', // parameter_length
'\x00', '\x01', // parameter_value
'\x00', '\x04', // parameter_type - data_stream_id
'\x00', '\x02', // parameter_length
'\x00', '\x01' // parameter_value
};
const char kTestEmmgChannelClose[] = {
'\x02', // protocol_version
'\x00', '\x14', // message_type - Channel_close
'\x00', '\x0e', // message_length
'\x00', '\x01', // parameter_type - client_id
'\x00', '\x04', // parameter_length
'\x4a', '\xd4', '\x00', '\x00', // parameter_value
'\x00', '\x03', // parameter_type - data_channel_id
'\x00', '\x02', // parameter_length
'\x00', '\x01' // parameter_value
};
} // namespace cas
} // namespace widevine
#endif // MEDIA_CAS_PACKAGER_SDK_EXAMPLE_TEST_EMMG_MESSAGES_H_

View File

@@ -8,12 +8,23 @@
// Example of how to use the wv_cas_ecm library.
#include <cassert>
#include <fstream>
#include <iostream>
#include <string>
#include "gflags/gflags.h"
#include "media_cas_packager_sdk/public/wv_cas_ecm.h"
#include "media_cas_packager_sdk/public/wv_cas_types.h"
DEFINE_int32(content_iv_size, 8, "Content IV size");
DEFINE_bool(key_rotation, true, "Whether key rotation is enabled");
DEFINE_string(crypto_mode, "CSA2", "Only CBC, CTR, or CSA2 is allowed");
DEFINE_int32(ecm_pid, 149, "PID for the ECM packet");
DEFINE_string(output_file, "",
"If specified, generated ECM TS packet will be written to the "
"specified output file path");
const char kCsaEvenKey[] = "even_key"; // 8 bytes
const char kEvenContentIv8Bytes[] = "even_iv."; // 8 bytes
const char kEvenEntitlementKeyId[] = "fake_key_id1...."; // 16 bytes
@@ -24,26 +35,45 @@ const char kOddContentIv8Bytes[] = "odd_iv.."; // 8 bytes
const char kOddEntitlementKeyId[] = "fake_key_id2...."; // 16 bytes
const char kOddEntitlementKey[] =
"fakefakefakefakefakefakefake2..."; // 32 bytes
const size_t kTsPacketSize = 188;
widevine::cas::CryptoMode GetCryptoMode() {
if (FLAGS_crypto_mode.compare("CBC") == 0) {
return widevine::cas::CryptoMode::kAesCbc;
}
if (FLAGS_crypto_mode.compare("CTR") == 0) {
return widevine::cas::CryptoMode::kAesCtr;
}
return widevine::cas::CryptoMode::kDvbCsa2;
}
int main(int argc, char **argv) {
gflags::ParseCommandLineFlags(&argc, &argv, true);
// Generate ECM.
widevine::cas::WvCasEcm wv_cas_ecm;
widevine::cas::WvCasStatus status = wv_cas_ecm.Initialize(
/* content_iv_size= */ 8, /* key_rotation_enabled= */ true,
widevine::cas::CryptoMode::kDvbCsa2);
FLAGS_content_iv_size, FLAGS_key_rotation, GetCryptoMode());
if (status != widevine::cas::OK) {
std::cerr << "Failed to initialize WV CAS ECM, error: "
<< widevine::cas::GetWvCasStatusMessage(status)
<< std::endl;
}
std::string ecm;
status = wv_cas_ecm.GenerateEcm(
kCsaEvenKey, kEvenContentIv8Bytes, kEvenEntitlementKeyId,
kEvenEntitlementKey, kCsaOddKey, kOddContentIv8Bytes,
kOddEntitlementKeyId, kOddEntitlementKey, &ecm);
if (FLAGS_key_rotation) {
status = wv_cas_ecm.GenerateEcm(
kCsaEvenKey, kEvenContentIv8Bytes, kEvenEntitlementKeyId,
kEvenEntitlementKey, kCsaOddKey, kOddContentIv8Bytes,
kOddEntitlementKeyId, kOddEntitlementKey, &ecm);
} else {
status = wv_cas_ecm.GenerateSingleKeyEcm(kCsaEvenKey, kEvenContentIv8Bytes,
kEvenEntitlementKeyId,
kEvenEntitlementKey, &ecm);
}
if (status != widevine::cas::OK) {
std::cerr << "Failed to generate WV CAS ECM, error: "
<< widevine::cas::GetWvCasStatusMessage(status)
<< std::endl;
return -1;
} else {
std::cout << "ECM size: " << ecm.size() << std::endl;
std::cout << "ECM bytes: ";
@@ -52,6 +82,30 @@ int main(int argc, char **argv) {
}
std::cout << std::endl;
}
// Generate ECM TS Packet.
uint8_t packet[kTsPacketSize];
uint8_t continuity_counter; // not used.
status = wv_cas_ecm.GenerateTsPacket(ecm, FLAGS_ecm_pid,
/* table_id= */ 0x80,
&continuity_counter, packet);
if (status != widevine::cas::OK) {
std::cerr << "Failed to create ECM TS packet" << std::endl;
return -1;
} else {
std::cout << "TS packet bytes: ";
for (size_t i = 0; i < kTsPacketSize; i++) {
printf("'\\x%02x', ", static_cast<uint16_t>(packet[i]));
}
std::cout << std::endl;
}
// Write ECM TS Packet to a file.
if (!FLAGS_output_file.empty()) {
std::ofstream file;
file.open(FLAGS_output_file.c_str(), std::ios_base::binary);
assert(file.is_open());
file.write(reinterpret_cast<char *>(packet), kTsPacketSize);
file.close();
}
return 0;
}

View File

@@ -0,0 +1,37 @@
////////////////////////////////////////////////////////////////////////////////
// Copyright 2019 Google LLC.
//
// This software is licensed under the terms defined in the Widevine Master
// License Agreement. For a copy of this agreement, please contact
// widevine-licensing@google.com.
////////////////////////////////////////////////////////////////////////////////
// Example of how to use types/functions in wv_cas_types.
#include <string>
#include "gflags/gflags.h"
#include "glog/logging.h"
#include "media_cas_packager_sdk/public/wv_cas_types.h"
DEFINE_string(function_to_call, "CreateWvCasEncryptionRequestJson",
"Function in wv_cas_types to exercise");
void CallCreateWvCasEncryptionRequestJson() {
widevine::cas::WvCasEncryptionRequest request;
request.content_id = "cont_id cont_id ";
request.provider = "widevine_test";
request.track_types = {"SD"};
request.key_rotation = true;
std::string request_json;
widevine::cas::CreateWvCasEncryptionRequestJson(request, &request_json);
LOG(INFO) << FLAGS_function_to_call << " returns " << request_json;
}
int main(int argc, char **argv) {
gflags::ParseCommandLineFlags(&argc, &argv, true);
if (FLAGS_function_to_call.compare("CreateWvCasEncryptionRequestJson") == 0) {
CallCreateWvCasEncryptionRequestJson();
}
return 0;
}