fuchsia_tee_manager_config/
lib.rs

1// Copyright 2024 The Fuchsia Authors
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use anyhow::{anyhow, Result};
6use serde::{Deserialize, Serialize};
7use std::path::PathBuf;
8
9#[derive(Serialize, Deserialize, Debug, Clone)]
10#[serde(rename_all = "camelCase", tag = "type")]
11enum Config {
12    GlobalPlatform(GlobalPlatformConfig),
13    BinderRpc(BinderRpcConfig),
14}
15
16// Configuration values specific to GlobalPlatform TAs.
17#[derive(Serialize, Deserialize, Debug, Clone)]
18#[serde(rename_all = "camelCase")]
19struct GlobalPlatformConfig {
20    /// Only create one instance of the trusted app and route all connections to it.
21    single_instance: bool,
22    /// Whether `single_instance` trusted apps support multiple separate sessions.
23    // TODO: Support multiSession functionality.
24    multi_session: bool,
25    /// The trusted app should continue running even in low power states and suspension.
26    // TODO: Support instanceKeepAlive functionality.
27    instance_keep_alive: bool,
28}
29
30// Configuration values specific to Binder RPC TAs.
31#[derive(Serialize, Deserialize, Debug, Clone)]
32#[serde(rename_all = "camelCase")]
33struct BinderRpcConfig {
34    startup_behavior: StartupBehavior,
35}
36
37#[derive(Serialize, Deserialize, Debug, Clone)]
38#[serde(rename_all = "camelCase")]
39enum StartupBehavior {
40    Lazy,
41    Eager,
42}
43
44/// Configuration for how to run a trusted application in Fuchsia.
45#[derive(Serialize, Deserialize, Debug, Clone)]
46#[serde(rename_all = "camelCase")]
47pub struct TAConfig {
48    /// The component url to run as the trusted application.
49    pub url: String,
50    /// Type-specific configuration.
51    config: Config,
52    /// Additional capabilities to pass to the component at `url`.
53    capabilities: Vec<()>,
54}
55
56impl TAConfig {
57    pub fn new(url: String) -> Self {
58        Self {
59            url,
60            config: Config::GlobalPlatform(GlobalPlatformConfig {
61                single_instance: false,
62                multi_session: false,
63                instance_keep_alive: false,
64            }),
65            capabilities: vec![],
66        }
67    }
68
69    pub fn parse_config(path: &PathBuf) -> Result<Self> {
70        let contents = std::fs::read_to_string(path)
71            .map_err(|e| anyhow!("Could not read config file at {path:?}: {e}"))?;
72        let parsed = serde_json::from_str(&contents)
73            .map_err(|e| anyhow!("Could not deserialize {path:?} from json: {e}"))?;
74        Ok(parsed)
75    }
76}