DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator
widevine_encryption_flags.cc
1 // Copyright 2014 Google Inc. 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 "packager/app/validate_flag.h"
12 #include "packager/base/logging.h"
13 #include "packager/base/strings/string_piece.h"
14 #include "packager/base/strings/string_util.h"
15 
16 DEFINE_bool(enable_widevine_encryption,
17  false,
18  "Enable encryption with Widevine license server/proxy. User should "
19  "provide either AES signing key (--aes_signing_key, "
20  "--aes_signing_iv) or RSA signing key (--rsa_signing_key_path).");
21 DEFINE_bool(enable_widevine_decryption,
22  false,
23  "Enable decryption with Widevine license server/proxy. User should "
24  "provide either AES signing key (--aes_signing_key, "
25  "--aes_signing_iv) or RSA signing key (--rsa_signing_key_path).");
26 DEFINE_bool(include_common_pssh,
27  false,
28  "When using Widevine encryption, include an additional v1 PSSH box "
29  "for the common system ID that includes the key IDs. See: "
30  "https://goo.gl/507mKp");
31 DEFINE_string(key_server_url, "", "Key server url. Required for encryption and "
32  "decryption");
33 DEFINE_string(content_id, "", "Content Id (hex).");
34 DEFINE_string(policy,
35  "",
36  "The name of a stored policy, which specifies DRM content "
37  "rights.");
38 DEFINE_int32(max_sd_pixels,
39  768 * 576,
40  "If the video track has more pixels per frame than max_sd_pixels, "
41  "it is considered as HD, SD otherwise. Default: 768 * 576.");
42 DEFINE_string(signer, "", "The name of the signer.");
43 DEFINE_string(aes_signing_key,
44  "",
45  "AES signing key in hex string. --aes_signing_iv is required. "
46  "Exclusive with --rsa_signing_key_path.");
47 DEFINE_string(aes_signing_iv,
48  "",
49  "AES signing iv in hex string.");
50 DEFINE_string(rsa_signing_key_path,
51  "",
52  "Stores PKCS#1 RSA private key for request signing. Exclusive "
53  "with --aes_signing_key.");
54 DEFINE_int32(crypto_period_duration,
55  0,
56  "Crypto period duration in seconds. If it is non-zero, key "
57  "rotation is enabled.");
58 DEFINE_string(protection_scheme,
59  "cenc",
60  "Choose protection scheme, 'cenc' or 'cbc1' or pattern-based "
61  "protection schemes 'cens' or 'cbcs'. Note that if a "
62  "pattern-based protection scheme only applies to video stream; "
63  "audio stream will be encrypted using the corresponding "
64  "non-pattern-based protection schemes, i.e. 'cenc' for 'cens', "
65  "'cbc1' for 'cbcs'.");
66 
67 namespace edash_packager {
68 
70  bool success = true;
71 
72  const bool widevine_crypto =
73  FLAGS_enable_widevine_encryption || FLAGS_enable_widevine_decryption;
74  const char widevine_crypto_label[] =
75  "--enable_widevine_encryption/decryption";
76  // key_server_url and signer (optional) are associated with
77  // enable_widevine_encryption and enable_widevine_descryption.
78  if (!ValidateFlag("key_server_url",
79  FLAGS_key_server_url,
80  widevine_crypto,
81  false,
82  widevine_crypto_label)) {
83  success = false;
84  }
85  if (!ValidateFlag("signer",
86  FLAGS_signer,
87  widevine_crypto,
88  true,
89  widevine_crypto_label)) {
90  success = false;
91  }
92  if (widevine_crypto && FLAGS_signer.empty() &&
93  base::StartsWith(base::StringPiece(FLAGS_key_server_url), "http",
94  base::CompareCase::INSENSITIVE_ASCII)) {
95  LOG(WARNING) << "--signer is likely required with "
96  "--enable_widevine_encryption/decryption.";
97  }
98 
99  const char widevine_encryption_label[] = "--enable_widevine_encryption";
100  // content_id and policy (optional) are associated with
101  // enable_widevine_encryption.
102  if (!ValidateFlag("content_id",
103  FLAGS_content_id,
104  FLAGS_enable_widevine_encryption,
105  false,
106  widevine_encryption_label)) {
107  success = false;
108  }
109  if (!ValidateFlag("policy",
110  FLAGS_policy,
111  FLAGS_enable_widevine_encryption,
112  true,
113  widevine_encryption_label)) {
114  success = false;
115  }
116  if (FLAGS_include_common_pssh && !FLAGS_enable_widevine_encryption) {
117  PrintError("--include_common_pssh is only valid with "
118  "--enable_widevine_encryption");
119  success = false;
120  }
121 
122  if (FLAGS_max_sd_pixels <= 0) {
123  PrintError("--max_sd_pixels must be positive.");
124  success = false;
125  }
126 
127  const bool aes = !FLAGS_signer.empty() && FLAGS_rsa_signing_key_path.empty();
128  const char aes_label[] =
129  "--signer is specified and exclusive with --rsa_signing_key_path";
130  // aes_signer_key and aes_signing_iv are associated with aes signing.
131  if (!ValidateFlag(
132  "aes_signing_key", FLAGS_aes_signing_key, aes, true, aes_label)) {
133  success = false;
134  }
135  if (!ValidateFlag(
136  "aes_signing_iv", FLAGS_aes_signing_iv, aes, true, aes_label)) {
137  success = false;
138  }
139 
140  const bool rsa = !FLAGS_signer.empty() && FLAGS_aes_signing_key.empty() &&
141  FLAGS_aes_signing_iv.empty();
142  const char rsa_label[] =
143  "--signer is specified and exclusive with --aes_signing_key/iv";
144  // rsa_signing_key_path is associated with rsa_signing.
145  if (!ValidateFlag("rsa_signing_key_path",
146  FLAGS_rsa_signing_key_path,
147  rsa,
148  true,
149  rsa_label)) {
150  success = false;
151  }
152 
153  if (!FLAGS_signer.empty() &&
154  (FLAGS_aes_signing_key.empty() || FLAGS_aes_signing_iv.empty()) &&
155  FLAGS_rsa_signing_key_path.empty()) {
156  PrintError(
157  "--aes_signing_key/iv or --rsa_signing_key_path is required with "
158  "--signer.");
159  success = false;
160  }
161 
162  if (FLAGS_crypto_period_duration < 0) {
163  PrintError("--crypto_period_duration should not be negative.");
164  success = false;
165  }
166  return success;
167 }
168 
169 } // namespace edash_packager
bool ValidateFlag(const char *flag_name, const std::string &flag_value, bool condition, bool optional, const char *label)
void PrintError(const std::string &error_message)