Shaka Packager SDK
Loading...
Searching...
No Matches
widevine_encryption_flags.cc
1// Copyright 2014 Google LLC. All rights reserved.
2//
3// Use of this source code is governed by a BSD-style
4// license that can be found in the LICENSE file or at
5// https://developers.google.com/open-source/licenses/bsd
6//
7// Defines command line flags for widevine_encryption.
8
9#include <packager/app/widevine_encryption_flags.h>
10
11#include <cstdint>
12#include <string>
13
14#include <absl/flags/flag.h>
15#include <absl/log/log.h>
16#include <absl/strings/ascii.h>
17#include <absl/strings/match.h>
18
19#include <packager/app/validate_flag.h>
20#include <packager/utils/absl_flag_hexbytes.h>
21
22ABSL_FLAG(bool,
23 enable_widevine_encryption,
24 false,
25 "Enable encryption with Widevine key server. User should provide "
26 "either AES signing key (--aes_signing_key, --aes_signing_iv) or "
27 "RSA signing key (--rsa_signing_key_path).");
28ABSL_FLAG(bool,
29 enable_widevine_decryption,
30 false,
31 "Enable decryption with Widevine license server/proxy. User should "
32 "provide either AES signing key (--aes_signing_key, "
33 "--aes_signing_iv) or RSA signing key (--rsa_signing_key_path).");
34ABSL_FLAG(std::string,
35 key_server_url,
36 "",
37 "Key server url. Required for encryption and "
38 "decryption");
39ABSL_FLAG(shaka::HexBytes, content_id, {}, "Content Id (hex).");
40ABSL_FLAG(std::string,
41 policy,
42 "",
43 "The name of a stored policy, which specifies DRM content "
44 "rights.");
45ABSL_FLAG(int32_t,
46 max_sd_pixels,
47 768 * 576,
48 "The video track is considered SD if its max pixels per frame is "
49 "no higher than max_sd_pixels. Default: 442368 (768 x 576).");
50ABSL_FLAG(int32_t,
51 max_hd_pixels,
52 1920 * 1080,
53 "The video track is considered HD if its max pixels per frame is "
54 "higher than max_sd_pixels, but no higher than max_hd_pixels. "
55 "Default: 2073600 (1920 x 1080).");
56ABSL_FLAG(int32_t,
57 max_uhd1_pixels,
58 4096 * 2160,
59 "The video track is considered UHD1 if its max pixels per frame "
60 "is higher than max_hd_pixels, but no higher than max_uhd1_pixels."
61 " Otherwise it is UHD2. Default: 8847360 (4096 x 2160).");
62ABSL_FLAG(std::string, signer, "", "The name of the signer.");
63ABSL_FLAG(shaka::HexBytes,
64 aes_signing_key,
65 {},
66 "AES signing key in hex string. --aes_signing_iv is required. "
67 "Exclusive with --rsa_signing_key_path.");
68ABSL_FLAG(shaka::HexBytes, aes_signing_iv, {}, "AES signing iv in hex string.");
69ABSL_FLAG(std::string,
70 rsa_signing_key_path,
71 "",
72 "Stores PKCS#1 RSA private key for request signing. Exclusive "
73 "with --aes_signing_key.");
74ABSL_FLAG(int32_t,
75 crypto_period_duration,
76 0,
77 "Crypto period duration in seconds. If it is non-zero, key "
78 "rotation is enabled.");
79ABSL_FLAG(shaka::HexBytes,
80 group_id,
81 {},
82 "Identifier for a group of licenses (hex).");
83ABSL_FLAG(bool,
84 enable_entitlement_license,
85 false,
86 "Enable entitlement license when using Widevine key server.");
87
88namespace shaka {
89namespace {
90const bool kOptional = true;
91} // namespace
92
94 bool success = true;
95
96 const bool widevine_crypto =
97 absl::GetFlag(FLAGS_enable_widevine_encryption) ||
98 absl::GetFlag(FLAGS_enable_widevine_decryption);
99 const char widevine_crypto_label[] =
100 "--enable_widevine_encryption/decryption";
101 // key_server_url and signer (optional) are associated with
102 // enable_widevine_encryption and enable_widevine_descryption.
103 if (!ValidateFlag("key_server_url", absl::GetFlag(FLAGS_key_server_url),
104 widevine_crypto, !kOptional, widevine_crypto_label)) {
105 success = false;
106 }
107 if (!ValidateFlag("signer", absl::GetFlag(FLAGS_signer), widevine_crypto,
108 kOptional, widevine_crypto_label)) {
109 success = false;
110 }
111 if (widevine_crypto && absl::GetFlag(FLAGS_signer).empty() &&
112 absl::StartsWith(
113 absl::AsciiStrToLower(absl::GetFlag(FLAGS_key_server_url)), "http")) {
114 LOG(WARNING) << "--signer is likely required with "
115 "--enable_widevine_encryption/decryption.";
116 }
117
118 const char widevine_encryption_label[] = "--enable_widevine_encryption";
119 // content_id and policy (optional) are associated with
120 // enable_widevine_encryption.
121 if (!ValidateFlag("content_id", absl::GetFlag(FLAGS_content_id).bytes,
122 absl::GetFlag(FLAGS_enable_widevine_encryption), !kOptional,
123 widevine_encryption_label)) {
124 success = false;
125 }
126 if (!ValidateFlag("policy", absl::GetFlag(FLAGS_policy),
127 absl::GetFlag(FLAGS_enable_widevine_encryption), kOptional,
128 widevine_encryption_label)) {
129 success = false;
130 }
131
132 if (absl::GetFlag(FLAGS_max_sd_pixels) <= 0) {
133 PrintError("--max_sd_pixels must be positive.");
134 success = false;
135 }
136 if (absl::GetFlag(FLAGS_max_hd_pixels) <= 0) {
137 PrintError("--max_hd_pixels must be positive.");
138 success = false;
139 }
140 if (absl::GetFlag(FLAGS_max_uhd1_pixels) <= 0) {
141 PrintError("--max_uhd1_pixels must be positive.");
142 success = false;
143 }
144 if (absl::GetFlag(FLAGS_max_hd_pixels) <=
145 absl::GetFlag(FLAGS_max_sd_pixels)) {
146 PrintError("--max_hd_pixels must be greater than --max_sd_pixels.");
147 success = false;
148 }
149 if (absl::GetFlag(FLAGS_max_uhd1_pixels) <=
150 absl::GetFlag(FLAGS_max_hd_pixels)) {
151 PrintError("--max_uhd1_pixels must be greater than --max_hd_pixels.");
152 success = false;
153 }
154
155 const bool aes = !absl::GetFlag(FLAGS_aes_signing_key).bytes.empty() ||
156 !absl::GetFlag(FLAGS_aes_signing_iv).bytes.empty();
157 if (aes && (absl::GetFlag(FLAGS_aes_signing_key).bytes.empty() ||
158 absl::GetFlag(FLAGS_aes_signing_iv).bytes.empty())) {
159 PrintError("--aes_signing_key/iv is required if using aes signing.");
160 success = false;
161 }
162
163 const bool rsa = !absl::GetFlag(FLAGS_rsa_signing_key_path).empty();
164
165 if (absl::GetFlag(FLAGS_signer).empty() && (aes || rsa)) {
166 PrintError("--signer is required if using aes/rsa signing.");
167 success = false;
168 }
169 if (!absl::GetFlag(FLAGS_signer).empty() && !aes && !rsa) {
171 "--aes_signing_key/iv or --rsa_signing_key_path is required with "
172 "--signer.");
173 success = false;
174 }
175 if (aes && rsa) {
177 "Only one of --aes_signing_key/iv and --rsa_signing_key_path should be "
178 "specified.");
179 success = false;
180 }
181
182 if (absl::GetFlag(FLAGS_crypto_period_duration) < 0) {
183 PrintError("--crypto_period_duration should not be negative.");
184 success = false;
185 }
186 return success;
187}
188
189} // namespace shaka
All the methods that are virtual are virtual for mocking.
void PrintError(const std::string &error_message)
bool ValidateFlag(const char *flag_name, const FlagType &flag_value, bool condition, bool optional, const char *label)
bool ValidateWidevineCryptoFlags()