Shaka Packager SDK
raw_key_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 raw key encryption/decryption.
8 
9 #include <packager/app/validate_flag.h>
10 #include <packager/utils/absl_flag_hexbytes.h>
11 
12 ABSL_FLAG(bool,
13  enable_fixed_key_encryption,
14  false,
15  "Same as --enable_raw_key_encryption. Will be deprecated.");
16 ABSL_FLAG(bool,
17  enable_fixed_key_decryption,
18  false,
19  "Same as --enable_raw_key_decryption. Will be deprecated.");
20 ABSL_FLAG(bool,
21  enable_raw_key_encryption,
22  false,
23  "Enable encryption with raw key (key provided in command line).");
24 ABSL_FLAG(bool,
25  enable_raw_key_decryption,
26  false,
27  "Enable decryption with raw key (key provided in command line).");
28 ABSL_FLAG(shaka::HexBytes,
29  key_id,
30  {},
31  "Key id in hex string format. Will be deprecated. Use --keys.");
32 ABSL_FLAG(shaka::HexBytes,
33  key,
34  {},
35  "Key in hex string format. Will be deprecated. Use --keys.");
36 ABSL_FLAG(std::string,
37  keys,
38  "",
39  "A list of key information in the form of label=<drm "
40  "label>:key_id=<32-digit key id in hex>:key=<32-digit key in "
41  "hex>,label=...");
42 ABSL_FLAG(shaka::HexBytes,
43  iv,
44  {},
45  "IV in hex string format. If not specified, a random IV will be "
46  "generated. This flag should only be used for testing.");
47 ABSL_FLAG(shaka::HexBytes,
48  pssh,
49  {},
50  "One or more PSSH boxes in hex string format. If not specified, "
51  "will generate a v1 common PSSH box as specified in "
52  "https://goo.gl/s8RIhr.");
53 
54 namespace shaka {
55 
57  bool success = true;
58 
59  if (absl::GetFlag(FLAGS_enable_fixed_key_encryption))
60  absl::SetFlag(&FLAGS_enable_raw_key_encryption, true);
61  if (absl::GetFlag(FLAGS_enable_fixed_key_decryption))
62  absl::SetFlag(&FLAGS_enable_raw_key_decryption, true);
63  if (absl::GetFlag(FLAGS_enable_fixed_key_encryption) ||
64  absl::GetFlag(FLAGS_enable_fixed_key_decryption)) {
66  "--enable_fixed_key_encryption and --enable_fixed_key_decryption are "
67  "going to be deprecated. Please switch to --enable_raw_key_encryption "
68  "and --enable_raw_key_decryption as soon as possible.");
69  }
70 
71  const bool raw_key_crypto = absl::GetFlag(FLAGS_enable_raw_key_encryption) ||
72  absl::GetFlag(FLAGS_enable_raw_key_decryption);
73  const char raw_key_crypto_label[] = "--enable_raw_key_encryption/decryption";
74  // --key_id and --key are associated with --enable_raw_key_encryption and
75  // --enable_raw_key_decryption.
76  if (absl::GetFlag(FLAGS_keys).empty()) {
77  if (!ValidateFlag("key_id", absl::GetFlag(FLAGS_key_id).bytes,
78  raw_key_crypto, false, raw_key_crypto_label)) {
79  success = false;
80  }
81  if (!ValidateFlag("key", absl::GetFlag(FLAGS_key).bytes, raw_key_crypto,
82  false, raw_key_crypto_label)) {
83  success = false;
84  }
85  if (success && (!absl::GetFlag(FLAGS_key_id).bytes.empty() ||
86  !absl::GetFlag(FLAGS_key).bytes.empty())) {
88  "--key_id and --key are going to be deprecated. Please switch to "
89  "--keys as soon as possible.");
90  }
91  } else {
92  if (!absl::GetFlag(FLAGS_key_id).bytes.empty() ||
93  !absl::GetFlag(FLAGS_key).bytes.empty()) {
94  PrintError("--key_id or --key cannot be used together with --keys.");
95  success = false;
96  }
97  }
98  if (!ValidateFlag("iv", absl::GetFlag(FLAGS_iv).bytes,
99  absl::GetFlag(FLAGS_enable_raw_key_encryption), true,
100  "--enable_raw_key_encryption")) {
101  success = false;
102  }
103  if (!absl::GetFlag(FLAGS_iv).bytes.empty()) {
104  if (absl::GetFlag(FLAGS_iv).bytes.size() != 8 &&
105  absl::GetFlag(FLAGS_iv).bytes.size() != 16) {
106  PrintError(
107  "--iv should be either 8 bytes (16 hex digits) or 16 bytes (32 hex "
108  "digits).");
109  success = false;
110  }
111  }
112 
113  // --pssh is associated with --enable_raw_key_encryption.
114  if (!ValidateFlag("pssh", absl::GetFlag(FLAGS_pssh).bytes,
115  absl::GetFlag(FLAGS_enable_raw_key_encryption), true,
116  "--enable_raw_key_encryption")) {
117  success = false;
118  }
119  return success;
120 }
121 
122 } // namespace shaka
All the methods that are virtual are virtual for mocking.
Definition: crypto_flags.cc:66
void PrintWarning(const std::string &warning_message)
void PrintError(const std::string &error_message)
bool ValidateFlag(const char *flag_name, const FlagType &flag_value, bool condition, bool optional, const char *label)
Definition: validate_flag.h:37
bool ValidateRawKeyCryptoFlags()