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