blob: 359890d1cbd4dd9c0d8ec16c655051bf708fca9d [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
Prashant Patil93cd95a2024-12-23 12:59:03 +000023/// Retrieve the most significant attestation property for `name`.
24fn attestation_property(name: &str) -> Vec<u8> {
25 let prop_val =
26 get_property(&format!("ro.product.{}_for_attestation", name)).unwrap_or_default();
27 if !prop_val.is_empty() {
28 prop_val
29 } else {
30 let prop_val = get_property(&format!("ro.product.vendor.{}", name)).unwrap_or_default();
31 if !prop_val.is_empty() {
32 prop_val
33 } else {
34 get_property(&format!("ro.product.{}", name))
35 .unwrap_or_else(|prop_name| format!("{} unavailable", prop_name))
36 }
37 }
38 .as_bytes()
39 .to_vec()
40}
41
David Drysdale30196cf2023-12-02 19:24:15 +000042/// Populate attestation ID information based on properties (where available).
43/// Retrieving the serial number requires SELinux permission.
44pub fn attestation_id_info() -> kmr_wire::AttestationIdInfo {
Prashant Patil93cd95a2024-12-23 12:59:03 +000045
David Drysdale30196cf2023-12-02 19:24:15 +000046 kmr_wire::AttestationIdInfo {
Prashant Patil93cd95a2024-12-23 12:59:03 +000047 brand: attestation_property("brand"),
48 device: attestation_property("device"),
49 product: attestation_property("name"),
50 serial: get_property("ro.serialno")
51 .unwrap_or_else(|_| format!("ro.serialno unavailable"))
52 .as_bytes()
53 .to_vec(),
54 manufacturer: attestation_property("manufacturer"),
55 model: attestation_property("model"),
David Drysdale30196cf2023-12-02 19:24:15 +000056 // Currently modem_simulator always returns one fixed value. See `handleGetIMEI` in
57 // device/google/cuttlefish/host/commands/modem_simulator/misc_service.cpp for more details.
58 // TODO(b/263188546): Use device-specific IMEI values when available.
59 imei: b"867400022047199".to_vec(),
60 imei2: b"867400022047199".to_vec(),
61 meid: vec![],
62 }
63}
64
65/// Get boot information based on system properties.
66pub fn get_boot_info() -> kmr_wire::SetBootInfoRequest {
David Drysdale30196cf2023-12-02 19:24:15 +000067 let vbmeta_digest = get_property("ro.boot.vbmeta.digest").unwrap_or_else(|_| "00".repeat(32));
68 let verified_boot_hash = hex::decode(&vbmeta_digest).unwrap_or_else(|_e| {
Catherine Vlasovc1bf5f82024-11-18 10:56:18 +000069 error!("failed to parse VBMeta digest hex data in '{vbmeta_digest}': {_e:?}");
David Drysdale30196cf2023-12-02 19:24:15 +000070 vec![0; 32]
71 });
72 let device_boot_locked = match get_property("ro.boot.vbmeta.device_state")
73 .unwrap_or_else(|_| "no-prop".to_string())
74 .as_str()
75 {
76 "locked" => true,
77 "unlocked" => false,
78 v => {
79 error!("Unknown device_state '{}', treating as unlocked", v);
80 false
81 }
82 };
Catherine Vlasovc1bf5f82024-11-18 10:56:18 +000083 let verified_boot_key_digest =
84 get_property("ro.boot.vbmeta.public_key_digest").unwrap_or_else(|_| "00".repeat(32));
85 let verified_boot_key = match device_boot_locked {
86 true => hex::decode(&verified_boot_key_digest).unwrap_or_else(|_e| {
87 error!("Failed to parse Verified Boot key hex data in '{verified_boot_key_digest}': {_e:?}");
88 vec![0; 32]
89 }),
90 // VTS-16+ requires the attested Verified Boot key to be 32 bytes of zeroes when the
91 // bootloader is unlocked, so we ignore the property's value in that case. Behaviour
92 // prior to VTS-16 is unspecified, so it's fine to return the same.
93 false => vec![0; 32],
94 };
David Drysdale30196cf2023-12-02 19:24:15 +000095 let verified_boot_state = match get_property("ro.boot.verifiedbootstate")
96 .unwrap_or_else(|_| "no-prop".to_string())
97 .as_str()
98 {
99 "green" => 0, // Verified
100 "yellow" => 1, // SelfSigned
101 "orange" => 2, // Unverified,
102 "red" => 3, // Failed,
103 v => {
104 error!("Unknown boot state '{}', treating as Unverified", v);
105 2
106 }
107 };
108
109 // Attempt to get the boot patchlevel from a system property. This requires an SELinux
110 // permission, so fall back to re-using the OS patchlevel if this can't be done.
111 let boot_patchlevel_prop = get_property("ro.vendor.boot_security_patch").unwrap_or_else(|e| {
112 error!("Failed to retrieve boot patchlevel: {:?}", e);
113 get_property(kmr_hal::env::OS_PATCHLEVEL_PROPERTY)
114 .unwrap_or_else(|_| "1970-09-19".to_string())
115 });
116 let boot_patchlevel =
117 kmr_hal::env::extract_patchlevel(&boot_patchlevel_prop).unwrap_or(19700919);
118
119 kmr_wire::SetBootInfoRequest {
120 verified_boot_key,
121 device_boot_locked,
122 verified_boot_state,
123 verified_boot_hash,
124 boot_patchlevel,
125 }
126}