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
use serde::{Deserialize, Serialize};
mod tee;
#[cfg(feature = "tee-sev")]
pub use tee::sev::{SevChallenge, SevRequest};
#[cfg(feature = "tee-snp")]
pub use tee::snp::{SnpAttestation, SnpRequest};
#[derive(Serialize, Clone, Deserialize, Debug, Eq, PartialEq)]
#[serde(rename_all = "lowercase")]
pub enum Tee {
Sev,
Sgx,
Snp,
Tdx,
Sample,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct Request {
pub version: String,
pub tee: Tee,
#[serde(rename = "extra-params")]
pub extra_params: String,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct Challenge {
pub nonce: String,
#[serde(rename = "extra-params")]
pub extra_params: String,
}
#[derive(Clone, Serialize, Deserialize, Debug)]
pub struct TeePubKey {
pub alg: String,
#[serde(rename = "k-mod")]
pub k_mod: String,
#[serde(rename = "k-exp")]
pub k_exp: String,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct Attestation {
#[serde(rename = "tee-pubkey")]
pub tee_pubkey: TeePubKey,
#[serde(rename = "tee-evidence")]
pub tee_evidence: String,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct Response {
pub protected: String,
pub encrypted_key: String,
pub iv: String,
pub ciphertext: String,
pub tag: String,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct ErrorInformation {
#[serde(rename = "type")]
pub error_type: String,
pub detail: String,
}
#[cfg(test)]
mod tests {
use crate::*;
#[test]
fn parse_request() {
let data = r#"
{
"version": "0.0.0",
"tee": "sev",
"extra-params": ""
}"#;
let request: Request = serde_json::from_str(data).unwrap();
assert_eq!(request.version, "0.0.0");
assert_eq!(request.tee, Tee::Sev);
assert_eq!(request.extra_params, "");
}
#[test]
fn parse_challenge() {
let data = r#"
{
"nonce": "42",
"extra-params": ""
}"#;
let challenge: Challenge = serde_json::from_str(data).unwrap();
assert_eq!(challenge.nonce, "42");
assert_eq!(challenge.extra_params, "");
}
#[test]
fn parse_response() {
let data = r#"
{
"protected": "fakejoseheader",
"encrypted_key": "fakekey",
"iv": "randomdata",
"ciphertext": "fakeencoutput",
"tag": "faketag"
}"#;
let response: Response = serde_json::from_str(data).unwrap();
assert_eq!(response.protected, "fakejoseheader");
assert_eq!(response.encrypted_key, "fakekey");
assert_eq!(response.iv, "randomdata");
assert_eq!(response.ciphertext, "fakeencoutput");
assert_eq!(response.tag, "faketag");
}
#[test]
fn parse_attesation() {
let data = r#"
{
"tee-pubkey": {
"alg": "fakealgorithm",
"k-mod": "fakemodulus",
"k-exp": "fakeexponent"
},
"tee-evidence": "fakeevidence"
}"#;
let attestation: Attestation = serde_json::from_str(data).unwrap();
assert_eq!(attestation.tee_pubkey.alg, "fakealgorithm");
assert_eq!(attestation.tee_pubkey.k_mod, "fakemodulus");
assert_eq!(attestation.tee_pubkey.k_exp, "fakeexponent");
assert_eq!(attestation.tee_evidence, "fakeevidence");
}
#[test]
fn parse_error_information() {
let data = r#"
{
"type": "problemtype",
"detail": "problemdetail"
}"#;
let info: ErrorInformation = serde_json::from_str(data).unwrap();
assert_eq!(info.error_type, "problemtype");
assert_eq!(info.detail, "problemdetail");
}
}