sgxs/einittoken.rs
1/* Copyright (c) Fortanix, Inc.
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7use std::fmt;
8use std::io::{Read, Result as IoResult};
9
10pub use abi::Einittoken;
11use abi::{Attributes, Sigstruct};
12
13pub trait EinittokenProvider: fmt::Debug {
14 /// Obtain an `EINITTOKEN` for the enclave specified by this `SIGSTRUCT`
15 /// and `ATTRIBUTES`.
16 ///
17 /// The provider may maintain a cache and may return results from the cache
18 /// if `retry` is `false`. A caching provider should recompute the token if
19 /// `retry` is `true`.
20 fn token(
21 &mut self,
22 sigstruct: &Sigstruct,
23 attributes: Attributes,
24 retry: bool,
25 ) -> Result<Einittoken, anyhow::Error>;
26
27 /// Will this provider exhibit different behavior if `retry` is `true`?
28 fn can_retry(&self) -> bool;
29}
30
31impl<P: EinittokenProvider + 'static> From<P> for Box<dyn EinittokenProvider> {
32 fn from(p: P) -> Self {
33 Box::new(p)
34 }
35}
36
37pub fn read<R: Read>(reader: &mut R) -> IoResult<Einittoken> {
38 let mut buf = [0u8; 304];
39 reader.read_exact(&mut buf)?;
40 Einittoken::try_copy_from(&buf).ok_or_else(|| unreachable!())
41}