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
//! RPC protocol types.
use rand::{rngs::OsRng, Rng};

impl_bytes!(
    SessionID,
    32,
    "Session identifier for multiplexing multiple sessions over the \
     same transport"
);

impl SessionID {
    /// Generate a random session identifier.
    pub fn random() -> Self {
        let mut session_id = [0u8; 32];
        OsRng.fill(&mut session_id);

        SessionID(session_id)
    }
}

/// RPC call kind.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, cbor::Encode, cbor::Decode)]
#[cbor(with_default)]
#[repr(u8)]
pub enum Kind {
    /// A secure RPC call using an encrypted and authenticated Noise session.
    NoiseSession = 0,
    /// An insecure RPC call where messages are sent in plain text.
    InsecureQuery = 1,
    /// A local RPC call.
    LocalQuery = 2,
}

impl Default for Kind {
    fn default() -> Self {
        Self::NoiseSession
    }
}

/// Frame.
#[derive(Clone, Debug, Default, cbor::Encode, cbor::Decode)]
pub struct Frame {
    pub session: SessionID,
    // The `untrusted_plaintext` field is only a temporary workaround until
    // the snow library supports encrypting the payload with authenticated
    // data.
    // This field contains a plaintext copy of the Request's `method` field
    // and is verified inside the enclave.  It is unused in other cases.
    pub untrusted_plaintext: String,
    pub payload: Vec<u8>,
}

#[derive(Clone, Debug, cbor::Encode, cbor::Decode)]
#[cbor(no_default)]
pub struct Request {
    pub method: String,
    pub args: cbor::Value,
}

#[derive(Clone, Debug, Default, cbor::Encode, cbor::Decode)]
pub struct Error {
    pub message: String,
}

#[derive(Clone, Debug, cbor::Encode, cbor::Decode)]
pub enum Body {
    Success(cbor::Value),
    Error(String),
}

#[derive(Clone, Debug, cbor::Encode, cbor::Decode)]
#[cbor(no_default)]
pub struct Response {
    pub body: Body,
}

/// Protocol message.
#[derive(Clone, Debug, cbor::Encode, cbor::Decode)]
pub enum Message {
    Request(Request),
    Response(Response),
    Close,
}

/// Feedback on the peer that handled the last EnclaveRPC call.
#[derive(Copy, Clone, Debug, PartialEq, Eq, cbor::Encode, cbor::Decode)]
pub enum PeerFeedback {
    Success = 0,
    Failure = 1,
    BadPeer = 2,
}