xref: /aosp_15_r20/tools/security/remote_provisioning/hwtrust/src/session.rs (revision d9ecfb0f4d734c9ce41cde8ac4d585b094fd4222)
1*d9ecfb0fSAndroid Build Coastguard Worker //! Defines the context type for a session handling hwtrust data structures.
2*d9ecfb0fSAndroid Build Coastguard Worker 
3*d9ecfb0fSAndroid Build Coastguard Worker use crate::dice::ProfileVersion;
4*d9ecfb0fSAndroid Build Coastguard Worker use anyhow::bail;
5*d9ecfb0fSAndroid Build Coastguard Worker use clap::ValueEnum;
6*d9ecfb0fSAndroid Build Coastguard Worker use std::ops::RangeInclusive;
7*d9ecfb0fSAndroid Build Coastguard Worker use std::str::FromStr;
8*d9ecfb0fSAndroid Build Coastguard Worker 
9*d9ecfb0fSAndroid Build Coastguard Worker /// The context for a session handling hwtrust data structures.
10*d9ecfb0fSAndroid Build Coastguard Worker #[derive(Clone, Default, Debug)]
11*d9ecfb0fSAndroid Build Coastguard Worker pub struct Session {
12*d9ecfb0fSAndroid Build Coastguard Worker     /// Options that control the behaviour during this session.
13*d9ecfb0fSAndroid Build Coastguard Worker     pub options: Options,
14*d9ecfb0fSAndroid Build Coastguard Worker }
15*d9ecfb0fSAndroid Build Coastguard Worker 
16*d9ecfb0fSAndroid Build Coastguard Worker /// Options that control the behaviour of a session.
17*d9ecfb0fSAndroid Build Coastguard Worker #[derive(Clone, Default, Debug)]
18*d9ecfb0fSAndroid Build Coastguard Worker pub struct Options {
19*d9ecfb0fSAndroid Build Coastguard Worker     /// The range of supported Android Profile for DICE versions.
20*d9ecfb0fSAndroid Build Coastguard Worker     pub dice_profile_range: DiceProfileRange,
21*d9ecfb0fSAndroid Build Coastguard Worker     /// Allows DICE chains to have non-normal mode values.
22*d9ecfb0fSAndroid Build Coastguard Worker     pub allow_any_mode: bool,
23*d9ecfb0fSAndroid Build Coastguard Worker     /// The RKP instance associated to the session.
24*d9ecfb0fSAndroid Build Coastguard Worker     pub rkp_instance: RkpInstance,
25*d9ecfb0fSAndroid Build Coastguard Worker }
26*d9ecfb0fSAndroid Build Coastguard Worker 
27*d9ecfb0fSAndroid Build Coastguard Worker /// The set of RKP instances associated to the session.
28*d9ecfb0fSAndroid Build Coastguard Worker #[derive(Clone, Copy, Default, Debug, ValueEnum)]
29*d9ecfb0fSAndroid Build Coastguard Worker pub enum RkpInstance {
30*d9ecfb0fSAndroid Build Coastguard Worker     /// The DICE chain is associated to the default instance.
31*d9ecfb0fSAndroid Build Coastguard Worker     #[default]
32*d9ecfb0fSAndroid Build Coastguard Worker     Default,
33*d9ecfb0fSAndroid Build Coastguard Worker     /// The DICE chain is associated to the strongbox instance.
34*d9ecfb0fSAndroid Build Coastguard Worker     Strongbox,
35*d9ecfb0fSAndroid Build Coastguard Worker     /// The DICE chain is associated to the avf instance.
36*d9ecfb0fSAndroid Build Coastguard Worker     /// This option performs additional checks to ensure the chain conforms to the requirements
37*d9ecfb0fSAndroid Build Coastguard Worker     /// for an RKP VM chain. For detailed information, refer to the RKP VM specification:
38*d9ecfb0fSAndroid Build Coastguard Worker     /// https://android.googlesource.com/platform/packages/modules/Virtualization/+/main/docs/vm_remote_attestation.md#rkp-vm-marker
39*d9ecfb0fSAndroid Build Coastguard Worker     Avf,
40*d9ecfb0fSAndroid Build Coastguard Worker     /// The DICE chain is associated to the Widevine instance.
41*d9ecfb0fSAndroid Build Coastguard Worker     Widevine,
42*d9ecfb0fSAndroid Build Coastguard Worker }
43*d9ecfb0fSAndroid Build Coastguard Worker 
44*d9ecfb0fSAndroid Build Coastguard Worker impl FromStr for RkpInstance {
45*d9ecfb0fSAndroid Build Coastguard Worker     type Err = anyhow::Error;
46*d9ecfb0fSAndroid Build Coastguard Worker 
from_str(s: &str) -> Result<Self, Self::Err>47*d9ecfb0fSAndroid Build Coastguard Worker     fn from_str(s: &str) -> Result<Self, Self::Err> {
48*d9ecfb0fSAndroid Build Coastguard Worker         match s {
49*d9ecfb0fSAndroid Build Coastguard Worker             "default" => Ok(RkpInstance::Default),
50*d9ecfb0fSAndroid Build Coastguard Worker             "strongbox" => Ok(RkpInstance::Strongbox),
51*d9ecfb0fSAndroid Build Coastguard Worker             "avf" => Ok(RkpInstance::Avf),
52*d9ecfb0fSAndroid Build Coastguard Worker             "widevine" => Ok(RkpInstance::Widevine),
53*d9ecfb0fSAndroid Build Coastguard Worker             _ => bail!("invalid RKP instance: {}", s),
54*d9ecfb0fSAndroid Build Coastguard Worker         }
55*d9ecfb0fSAndroid Build Coastguard Worker     }
56*d9ecfb0fSAndroid Build Coastguard Worker }
57*d9ecfb0fSAndroid Build Coastguard Worker 
58*d9ecfb0fSAndroid Build Coastguard Worker impl Session {
59*d9ecfb0fSAndroid Build Coastguard Worker     /// Set allow_any_mode.
set_allow_any_mode(&mut self, allow_any_mode: bool)60*d9ecfb0fSAndroid Build Coastguard Worker     pub fn set_allow_any_mode(&mut self, allow_any_mode: bool) {
61*d9ecfb0fSAndroid Build Coastguard Worker         self.options.allow_any_mode = allow_any_mode
62*d9ecfb0fSAndroid Build Coastguard Worker     }
63*d9ecfb0fSAndroid Build Coastguard Worker 
64*d9ecfb0fSAndroid Build Coastguard Worker     /// Sets the RKP instance associated to the session.
set_rkp_instance(&mut self, rkp_instance: RkpInstance)65*d9ecfb0fSAndroid Build Coastguard Worker     pub fn set_rkp_instance(&mut self, rkp_instance: RkpInstance) {
66*d9ecfb0fSAndroid Build Coastguard Worker         self.options.rkp_instance = rkp_instance
67*d9ecfb0fSAndroid Build Coastguard Worker     }
68*d9ecfb0fSAndroid Build Coastguard Worker }
69*d9ecfb0fSAndroid Build Coastguard Worker 
70*d9ecfb0fSAndroid Build Coastguard Worker /// An inclusive range of Android Profile for DICE versions.
71*d9ecfb0fSAndroid Build Coastguard Worker #[derive(Clone, Debug, PartialEq, Eq)]
72*d9ecfb0fSAndroid Build Coastguard Worker pub struct DiceProfileRange(RangeInclusive<ProfileVersion>);
73*d9ecfb0fSAndroid Build Coastguard Worker 
74*d9ecfb0fSAndroid Build Coastguard Worker impl DiceProfileRange {
75*d9ecfb0fSAndroid Build Coastguard Worker     /// Creates a new inclusive range of Android Profile for DICE versions.
new(start: ProfileVersion, end: ProfileVersion) -> Self76*d9ecfb0fSAndroid Build Coastguard Worker     pub fn new(start: ProfileVersion, end: ProfileVersion) -> Self {
77*d9ecfb0fSAndroid Build Coastguard Worker         Self(RangeInclusive::new(start, end))
78*d9ecfb0fSAndroid Build Coastguard Worker     }
79*d9ecfb0fSAndroid Build Coastguard Worker 
80*d9ecfb0fSAndroid Build Coastguard Worker     /// Returns `true` if `version` is contained in the range.
contains(&self, version: ProfileVersion) -> bool81*d9ecfb0fSAndroid Build Coastguard Worker     pub fn contains(&self, version: ProfileVersion) -> bool {
82*d9ecfb0fSAndroid Build Coastguard Worker         self.0.contains(&version)
83*d9ecfb0fSAndroid Build Coastguard Worker     }
84*d9ecfb0fSAndroid Build Coastguard Worker 
85*d9ecfb0fSAndroid Build Coastguard Worker     /// Returns the lower bound of the range.
start(&self) -> ProfileVersion86*d9ecfb0fSAndroid Build Coastguard Worker     pub fn start(&self) -> ProfileVersion {
87*d9ecfb0fSAndroid Build Coastguard Worker         *self.0.start()
88*d9ecfb0fSAndroid Build Coastguard Worker     }
89*d9ecfb0fSAndroid Build Coastguard Worker 
90*d9ecfb0fSAndroid Build Coastguard Worker     /// Returns the upper bound of the range.
end(&self) -> ProfileVersion91*d9ecfb0fSAndroid Build Coastguard Worker     pub fn end(&self) -> ProfileVersion {
92*d9ecfb0fSAndroid Build Coastguard Worker         *self.0.end()
93*d9ecfb0fSAndroid Build Coastguard Worker     }
94*d9ecfb0fSAndroid Build Coastguard Worker }
95*d9ecfb0fSAndroid Build Coastguard Worker 
96*d9ecfb0fSAndroid Build Coastguard Worker impl Default for DiceProfileRange {
default() -> Self97*d9ecfb0fSAndroid Build Coastguard Worker     fn default() -> Self {
98*d9ecfb0fSAndroid Build Coastguard Worker         Self::new(ProfileVersion::Android14, ProfileVersion::Android16)
99*d9ecfb0fSAndroid Build Coastguard Worker     }
100*d9ecfb0fSAndroid Build Coastguard Worker }
101*d9ecfb0fSAndroid Build Coastguard Worker 
102*d9ecfb0fSAndroid Build Coastguard Worker impl Options {
103*d9ecfb0fSAndroid Build Coastguard Worker     /// The options use by VSR 13.
vsr13() -> Self104*d9ecfb0fSAndroid Build Coastguard Worker     pub fn vsr13() -> Self {
105*d9ecfb0fSAndroid Build Coastguard Worker         Self {
106*d9ecfb0fSAndroid Build Coastguard Worker             dice_profile_range: DiceProfileRange::new(
107*d9ecfb0fSAndroid Build Coastguard Worker                 ProfileVersion::Android13,
108*d9ecfb0fSAndroid Build Coastguard Worker                 ProfileVersion::Android15,
109*d9ecfb0fSAndroid Build Coastguard Worker             ),
110*d9ecfb0fSAndroid Build Coastguard Worker             ..Default::default()
111*d9ecfb0fSAndroid Build Coastguard Worker         }
112*d9ecfb0fSAndroid Build Coastguard Worker     }
113*d9ecfb0fSAndroid Build Coastguard Worker 
114*d9ecfb0fSAndroid Build Coastguard Worker     /// The options use by VSR 14.
vsr14() -> Self115*d9ecfb0fSAndroid Build Coastguard Worker     pub fn vsr14() -> Self {
116*d9ecfb0fSAndroid Build Coastguard Worker         Self {
117*d9ecfb0fSAndroid Build Coastguard Worker             dice_profile_range: DiceProfileRange::new(
118*d9ecfb0fSAndroid Build Coastguard Worker                 ProfileVersion::Android14,
119*d9ecfb0fSAndroid Build Coastguard Worker                 ProfileVersion::Android15,
120*d9ecfb0fSAndroid Build Coastguard Worker             ),
121*d9ecfb0fSAndroid Build Coastguard Worker             ..Default::default()
122*d9ecfb0fSAndroid Build Coastguard Worker         }
123*d9ecfb0fSAndroid Build Coastguard Worker     }
124*d9ecfb0fSAndroid Build Coastguard Worker 
125*d9ecfb0fSAndroid Build Coastguard Worker     /// The options use by VSR 15.
vsr15() -> Self126*d9ecfb0fSAndroid Build Coastguard Worker     pub fn vsr15() -> Self {
127*d9ecfb0fSAndroid Build Coastguard Worker         Self {
128*d9ecfb0fSAndroid Build Coastguard Worker             dice_profile_range: DiceProfileRange::new(
129*d9ecfb0fSAndroid Build Coastguard Worker                 ProfileVersion::Android14,
130*d9ecfb0fSAndroid Build Coastguard Worker                 ProfileVersion::Android15,
131*d9ecfb0fSAndroid Build Coastguard Worker             ),
132*d9ecfb0fSAndroid Build Coastguard Worker             ..Default::default()
133*d9ecfb0fSAndroid Build Coastguard Worker         }
134*d9ecfb0fSAndroid Build Coastguard Worker     }
135*d9ecfb0fSAndroid Build Coastguard Worker 
136*d9ecfb0fSAndroid Build Coastguard Worker     /// The options use by VSR 16.
vsr16() -> Self137*d9ecfb0fSAndroid Build Coastguard Worker     pub fn vsr16() -> Self {
138*d9ecfb0fSAndroid Build Coastguard Worker         Self {
139*d9ecfb0fSAndroid Build Coastguard Worker             dice_profile_range: DiceProfileRange::new(
140*d9ecfb0fSAndroid Build Coastguard Worker                 ProfileVersion::Android14,
141*d9ecfb0fSAndroid Build Coastguard Worker                 ProfileVersion::Android16,
142*d9ecfb0fSAndroid Build Coastguard Worker             ),
143*d9ecfb0fSAndroid Build Coastguard Worker             ..Default::default()
144*d9ecfb0fSAndroid Build Coastguard Worker         }
145*d9ecfb0fSAndroid Build Coastguard Worker     }
146*d9ecfb0fSAndroid Build Coastguard Worker }
147