blob: fad807f870acecf829ad9e10bce08fdeb1aed1a2 [file] [log] [blame]
David Drysdale30196cf2023-12-02 19:24:15 +00001/*
2 * Copyright (C) 2023 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17//! KeyMint helper functions that are only suitable for non-secure environments
18//! such as Cuttlefish.
19
20use kmr_hal::env::get_property;
21use log::error;
22
23/// Populate attestation ID information based on properties (where available).
24/// Retrieving the serial number requires SELinux permission.
25pub fn attestation_id_info() -> kmr_wire::AttestationIdInfo {
26 let prop = |name| {
27 get_property(name)
28 .unwrap_or_else(|_| format!("{} unavailable", name))
29 .as_bytes()
30 .to_vec()
31 };
32 kmr_wire::AttestationIdInfo {
33 brand: prop("ro.product.brand"),
34 device: prop("ro.product.device"),
35 product: prop("ro.product.name"),
36 serial: prop("ro.serialno"),
37 manufacturer: prop("ro.product.manufacturer"),
38 model: prop("ro.product.model"),
39 // Currently modem_simulator always returns one fixed value. See `handleGetIMEI` in
40 // device/google/cuttlefish/host/commands/modem_simulator/misc_service.cpp for more details.
41 // TODO(b/263188546): Use device-specific IMEI values when available.
42 imei: b"867400022047199".to_vec(),
43 imei2: b"867400022047199".to_vec(),
44 meid: vec![],
45 }
46}
47
48/// Get boot information based on system properties.
49pub fn get_boot_info() -> kmr_wire::SetBootInfoRequest {
David Drysdale30196cf2023-12-02 19:24:15 +000050 let vbmeta_digest = get_property("ro.boot.vbmeta.digest").unwrap_or_else(|_| "00".repeat(32));
51 let verified_boot_hash = hex::decode(&vbmeta_digest).unwrap_or_else(|_e| {
Catherine Vlasovc1bf5f82024-11-18 10:56:18 +000052 error!("failed to parse VBMeta digest hex data in '{vbmeta_digest}': {_e:?}");
David Drysdale30196cf2023-12-02 19:24:15 +000053 vec![0; 32]
54 });
55 let device_boot_locked = match get_property("ro.boot.vbmeta.device_state")
56 .unwrap_or_else(|_| "no-prop".to_string())
57 .as_str()
58 {
59 "locked" => true,
60 "unlocked" => false,
61 v => {
62 error!("Unknown device_state '{}', treating as unlocked", v);
63 false
64 }
65 };
Catherine Vlasovc1bf5f82024-11-18 10:56:18 +000066 let verified_boot_key_digest =
67 get_property("ro.boot.vbmeta.public_key_digest").unwrap_or_else(|_| "00".repeat(32));
68 let verified_boot_key = match device_boot_locked {
69 true => hex::decode(&verified_boot_key_digest).unwrap_or_else(|_e| {
70 error!("Failed to parse Verified Boot key hex data in '{verified_boot_key_digest}': {_e:?}");
71 vec![0; 32]
72 }),
73 // VTS-16+ requires the attested Verified Boot key to be 32 bytes of zeroes when the
74 // bootloader is unlocked, so we ignore the property's value in that case. Behaviour
75 // prior to VTS-16 is unspecified, so it's fine to return the same.
76 false => vec![0; 32],
77 };
David Drysdale30196cf2023-12-02 19:24:15 +000078 let verified_boot_state = match get_property("ro.boot.verifiedbootstate")
79 .unwrap_or_else(|_| "no-prop".to_string())
80 .as_str()
81 {
82 "green" => 0, // Verified
83 "yellow" => 1, // SelfSigned
84 "orange" => 2, // Unverified,
85 "red" => 3, // Failed,
86 v => {
87 error!("Unknown boot state '{}', treating as Unverified", v);
88 2
89 }
90 };
91
92 // Attempt to get the boot patchlevel from a system property. This requires an SELinux
93 // permission, so fall back to re-using the OS patchlevel if this can't be done.
94 let boot_patchlevel_prop = get_property("ro.vendor.boot_security_patch").unwrap_or_else(|e| {
95 error!("Failed to retrieve boot patchlevel: {:?}", e);
96 get_property(kmr_hal::env::OS_PATCHLEVEL_PROPERTY)
97 .unwrap_or_else(|_| "1970-09-19".to_string())
98 });
99 let boot_patchlevel =
100 kmr_hal::env::extract_patchlevel(&boot_patchlevel_prop).unwrap_or(19700919);
101
102 kmr_wire::SetBootInfoRequest {
103 verified_boot_key,
104 device_boot_locked,
105 verified_boot_state,
106 verified_boot_hash,
107 boot_patchlevel,
108 }
109}