oasis_core_runtime/enclave_rpc/
types.rs

1//! RPC protocol types.
2use rand::{rngs::OsRng, Rng};
3
4impl_bytes!(
5    SessionID,
6    32,
7    "Session identifier for multiplexing multiple sessions over the \
8     same transport"
9);
10
11impl SessionID {
12    /// Generate a random session identifier.
13    pub fn random() -> Self {
14        let mut session_id = [0u8; 32];
15        OsRng.fill(&mut session_id);
16
17        SessionID(session_id)
18    }
19}
20
21/// RPC call kind.
22#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, cbor::Encode, cbor::Decode)]
23#[cbor(with_default)]
24#[repr(u8)]
25pub enum Kind {
26    /// A secure RPC call using an encrypted and authenticated Noise session.
27    NoiseSession = 0,
28    /// An insecure RPC call where messages are sent in plain text.
29    InsecureQuery = 1,
30    /// A local RPC call.
31    LocalQuery = 2,
32}
33
34impl Default for Kind {
35    fn default() -> Self {
36        Self::NoiseSession
37    }
38}
39
40/// Frame.
41#[derive(Clone, Debug, Default, cbor::Encode, cbor::Decode)]
42pub struct Frame {
43    pub session: SessionID,
44    // The `untrusted_plaintext` field is only a temporary workaround until
45    // the snow library supports encrypting the payload with authenticated
46    // data.
47    // This field contains a plaintext copy of the Request's `method` field
48    // and is verified inside the enclave.  It is unused in other cases.
49    pub untrusted_plaintext: String,
50    pub payload: Vec<u8>,
51}
52
53#[derive(Clone, Debug, cbor::Encode, cbor::Decode)]
54#[cbor(no_default)]
55pub struct Request {
56    pub method: String,
57    pub args: cbor::Value,
58}
59
60#[derive(Clone, Debug, Default, cbor::Encode, cbor::Decode)]
61pub struct Error {
62    pub message: String,
63}
64
65#[derive(Clone, Debug, cbor::Encode, cbor::Decode)]
66pub enum Body {
67    Success(cbor::Value),
68    Error(String),
69}
70
71#[derive(Clone, Debug, cbor::Encode, cbor::Decode)]
72#[cbor(no_default)]
73pub struct Response {
74    pub body: Body,
75}
76
77/// Protocol message.
78#[derive(Clone, Debug, cbor::Encode, cbor::Decode)]
79pub enum Message {
80    Request(Request),
81    Response(Response),
82    Close,
83}
84
85/// Feedback on the peer that handled the last EnclaveRPC call.
86#[derive(Copy, Clone, Debug, PartialEq, Eq, cbor::Encode, cbor::Decode)]
87pub enum PeerFeedback {
88    Success = 0,
89    Failure = 1,
90    BadPeer = 2,
91}