xref: /aosp_15_r20/system/keymint/tests/src/bin/encrypted-keyblob-parse.rs (revision 9860b7637a5f185913c70aa0caabe3ecb78441e4)
1 // Copyright 2022, The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 //! Utility program to parse a legacy encrypted keyblob (but not decrypt it).
16 
17 use kmr_common::keyblob::legacy::EncryptedKeyBlob;
18 
main()19 fn main() {
20     let mut hex = false;
21     let args: Vec<String> = std::env::args().collect();
22     for arg in &args[1..] {
23         if arg == "--hex" {
24             hex = !hex;
25         } else {
26             process(arg, hex);
27         }
28     }
29 }
30 
process(filename: &str, hex: bool)31 fn process(filename: &str, hex: bool) {
32     let _ = env_logger::builder().is_test(true).try_init();
33 
34     println!("File: {}", filename);
35     let mut data: Vec<u8> = std::fs::read(filename).unwrap();
36     if hex {
37         let hexdata = std::str::from_utf8(&data).unwrap().trim();
38         data = match hex::decode(hexdata) {
39             Ok(v) => v,
40             Err(e) => {
41                 eprintln!(
42                     "{}: Failed to parse hex ({:?}): len={} {}",
43                     filename,
44                     e,
45                     hexdata.len(),
46                     hexdata
47                 );
48                 return;
49             }
50         };
51     }
52     let keyblob = match EncryptedKeyBlob::deserialize(&data) {
53         Ok(k) => k,
54         Err(e) => {
55             eprintln!("{}: Failed to parse: {:?}", filename, e);
56             return;
57         }
58     };
59     println!(
60         "{}, KeyBlob  {{\n  format={:?}\n  nonce={},\n  ciphertext=...(len {}),\n  tag={},",
61         filename,
62         keyblob.format,
63         hex::encode(&keyblob.nonce),
64         keyblob.ciphertext.len(),
65         hex::encode(&keyblob.tag)
66     );
67     if let Some(kdf_version) = keyblob.kdf_version {
68         println!("  kdf_version={}", kdf_version);
69     }
70     if let Some(addl_info) = keyblob.addl_info {
71         println!("  addl_info={}", addl_info);
72     }
73     println!("  hw_enforced={:?},\n  sw_enforced={:?},", keyblob.hw_enforced, keyblob.sw_enforced);
74     if let Some(key_slot) = keyblob.key_slot {
75         println!("  key_slot={}", key_slot);
76     }
77     println!("}}");
78 
79     // Also round-trip the keyblob to binary.
80     let regenerated_data = keyblob.serialize().unwrap();
81     assert_eq!(regenerated_data, data);
82 }
83