1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
//! Consensus SGX and quote policy handling.

use std::sync::Arc;

use anyhow::{bail, Result};
use slog::{debug, Logger};
use thiserror::Error;

use crate::{
    common::{logger::get_logger, namespace::Namespace, sgx::QuotePolicy, version::Version},
    consensus::{
        keymanager::SignedPolicySGX,
        registry::{SGXConstraints, TEEHardware},
        state::{
            beacon::ImmutableState as BeaconState,
            keymanager::{ImmutableState as KeyManagerState, Status},
            registry::ImmutableState as RegistryState,
        },
        verifier::Verifier,
    },
    future::block_on,
};

/// Policy verifier error.
#[derive(Error, Debug)]
pub enum PolicyVerifierError {
    #[error("missing runtime descriptor")]
    MissingRuntimeDescriptor,
    #[error("no corresponding runtime deployment")]
    NoDeployment,
    #[error("bad TEE constraints")]
    BadTEEConstraints,
    #[error("policy mismatch")]
    PolicyMismatch,
    #[error("policy hasn't been published")]
    PolicyNotPublished,
    #[error("status mismatch")]
    StatusMismatch,
    #[error("status hasn't been published")]
    StatusNotPublished,
    #[error("configured runtime hardware mismatch")]
    HardwareMismatch,
    #[error("runtime doesn't use key manager")]
    NoKeyManager,
}

/// Consensus policy verifier.
pub struct PolicyVerifier {
    consensus_verifier: Arc<dyn Verifier>,
    logger: Logger,
}

impl PolicyVerifier {
    /// Create a new consensus policy verifier.
    pub fn new(consensus_verifier: Arc<dyn Verifier>) -> Self {
        let logger = get_logger("runtime/policy_verifier");
        Self {
            consensus_verifier,
            logger,
        }
    }

    /// Fetch runtime's quote policy from the latest verified consensus layer state.
    ///
    /// If the runtime version is not provided, the policy for the active deployment is returned.
    pub fn quote_policy(
        &self,
        runtime_id: &Namespace,
        version: Option<Version>,
    ) -> Result<QuotePolicy> {
        // Fetch quote policy from the consensus layer using the given or the active version.
        // TODO: Make this async.
        let consensus_state = block_on(self.consensus_verifier.latest_state())?;
        let registry_state = RegistryState::new(&consensus_state);
        let runtime = registry_state
            .runtime(runtime_id)?
            .ok_or(PolicyVerifierError::MissingRuntimeDescriptor)?;

        let ad = match version {
            Some(version) => runtime
                .deployment_for_version(version)
                .ok_or(PolicyVerifierError::NoDeployment)?,
            None => {
                let beacon_state = BeaconState::new(&consensus_state);
                let epoch = beacon_state.epoch()?;

                runtime
                    .active_deployment(epoch)
                    .ok_or(PolicyVerifierError::NoDeployment)?
            }
        };

        let policy = match runtime.tee_hardware {
            TEEHardware::TEEHardwareIntelSGX => {
                let sc: SGXConstraints = ad
                    .try_decode_tee()
                    .map_err(|_| PolicyVerifierError::BadTEEConstraints)?;
                sc.policy()
            }
            _ => bail!(PolicyVerifierError::HardwareMismatch),
        };

        Ok(policy)
    }

    /// Verify that runtime's quote policy has been published in the consensus layer.
    pub fn verify_quote_policy(
        &self,
        policy: QuotePolicy,
        runtime_id: &Namespace,
        version: Option<Version>,
    ) -> Result<QuotePolicy> {
        let published_policy = self.quote_policy(runtime_id, version)?;

        if policy != published_policy {
            debug!(
                self.logger,
                "quote policy mismatch";
                "untrusted" => ?policy,
                "published" => ?published_policy,
            );
            return Err(PolicyVerifierError::PolicyMismatch.into());
        }

        Ok(published_policy)
    }

    /// Fetch key manager's status from the latest verified consensus layer state.
    pub fn key_manager_status(&self, key_manager: Namespace) -> Result<Status> {
        // TODO: Make this async.
        let consensus_state = block_on(self.consensus_verifier.latest_state())?;
        let km_state = KeyManagerState::new(&consensus_state);
        km_state
            .status(key_manager)?
            .ok_or_else(|| PolicyVerifierError::StatusNotPublished.into())
    }

    /// Verify that key manager's status has been published in the consensus layer.
    pub fn verify_key_manager_status(
        &self,
        status: Status,
        key_manager: Namespace,
    ) -> Result<Status> {
        let published_status = self.key_manager_status(key_manager)?;

        if status != published_status {
            debug!(
                self.logger,
                "key manager status mismatch";
                "untrusted" => ?status,
                "published" => ?published_status,
            );
            return Err(PolicyVerifierError::StatusMismatch.into());
        }

        Ok(published_status)
    }

    /// Fetch key manager's policy from the latest verified consensus layer state.
    pub fn key_manager_policy(&self, key_manager: Namespace) -> Result<SignedPolicySGX> {
        self.key_manager_status(key_manager)?
            .policy
            .ok_or_else(|| PolicyVerifierError::PolicyNotPublished.into())
    }

    /// Verify that key manager's policy has been published in the consensus layer.
    pub fn verify_key_manager_policy(
        &self,
        policy: SignedPolicySGX,
        key_manager: Namespace,
    ) -> Result<SignedPolicySGX> {
        let published_policy = self.key_manager_policy(key_manager)?;

        if policy != published_policy {
            debug!(
                self.logger,
                "key manager policy mismatch";
                "untrusted" => ?policy,
                "published" => ?published_policy,
            );
            return Err(PolicyVerifierError::PolicyMismatch.into());
        }

        Ok(published_policy)
    }

    /// Fetch runtime's key manager.
    pub fn key_manager(&self, runtime_id: &Namespace) -> Result<Namespace> {
        // TODO: Make this async.
        let consensus_state = block_on(self.consensus_verifier.latest_state())?;
        let registry_state = RegistryState::new(&consensus_state);
        let runtime = registry_state
            .runtime(runtime_id)?
            .ok_or(PolicyVerifierError::MissingRuntimeDescriptor)?;
        let key_manager = runtime
            .key_manager
            .ok_or(PolicyVerifierError::NoKeyManager)?;

        Ok(key_manager)
    }
}