Skip to content

Commit 4817259

Browse files
committed
AA: get_token API add runtime_data paramter
Signed-off-by: Jiale Zhang <zhangjiale@linux.alibaba.com>
1 parent d56a2cf commit 4817259

File tree

13 files changed

+146
-39
lines changed

13 files changed

+146
-39
lines changed

api-server-rest/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ $ curl http://127.0.0.1:8006/cdh/resource/default/key/1
1313
$ curl http://127.0.0.1:8006/aa/evidence\?runtime_data\=xxxx
1414
{"svn":"1","report_data":"eHh4eA=="}
1515

16-
$ curl http://127.0.0.1:8006/aa/token\?token_type\=kbs
16+
$ curl http://127.0.0.1:8006/aa/token\?token_type\=kbs\?structured_runtime_data=xxx
1717
{"token":"eyJhbGciOiJFi...","tee_keypair":"-----BEGIN... "}
1818
```

api-server-rest/build.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ use utoipa::OpenApi;
1212
get,
1313
path = "/aa/token",
1414
params(
15-
("token_type" = String, Query, description = "Token Type")
15+
("token_type" = String, Query, description = "Token Type"),
16+
("structured_runtime_data" = Option<String>, Query, description = "Structured data in JSON format, which will be hashed as runtime data")
1617
),
1718
responses(
1819
(status = 200, description = "success response",

api-server-rest/openapi/api.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,16 @@
8080
"schema": {
8181
"type": "string"
8282
}
83+
},
84+
{
85+
"name": "structured_runtime_data",
86+
"in": "query",
87+
"description": "Structured data in JSON format, which will be hashed as runtime data",
88+
"required": false,
89+
"schema": {
90+
"type": "string",
91+
"nullable": true
92+
}
8393
}
8494
],
8595
"responses": {

api-server-rest/protos/attestation_agent.proto

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ message GetEvidenceResponse {
1212

1313
message GetTokenRequest {
1414
string TokenType = 1;
15+
optional string StructuredRuntimeData = 2;
1516
}
1617

1718
message GetTokenResponse {

api-server-rest/src/aa.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,12 @@ impl ApiHandler for AAClient {
5757
match url_path {
5858
AA_TOKEN_URL => match params.get("token_type") {
5959
Some(token_type) => {
60+
let default_structed_runtime_data = String::from("{}");
61+
let structured_runtime_data = params
62+
.get("structured_runtime_data")
63+
.unwrap_or(&default_structed_runtime_data);
6064
let results = self
61-
.get_token(token_type)
65+
.get_token(token_type, structured_runtime_data)
6266
.await
6367
.unwrap_or_else(|e| e.to_string().into());
6468
return self.octet_stream_response(results);
@@ -95,9 +99,14 @@ impl AAClient {
9599
})
96100
}
97101

98-
pub async fn get_token(&self, token_type: &str) -> Result<Vec<u8>> {
102+
pub async fn get_token(
103+
&self,
104+
token_type: &str,
105+
structured_runtime_data: &str,
106+
) -> Result<Vec<u8>> {
99107
let req = GetTokenRequest {
100108
TokenType: token_type.to_string(),
109+
StructuredRuntimeData: Some(structured_runtime_data.to_string()),
101110
..Default::default()
102111
};
103112
let res = self

api-server-rest/src/ttrpc_proto/attestation_agent.rs

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,8 @@ pub struct GetTokenRequest {
275275
// message fields
276276
// @@protoc_insertion_point(field:attestation_agent.GetTokenRequest.TokenType)
277277
pub TokenType: ::std::string::String,
278+
// @@protoc_insertion_point(field:attestation_agent.GetTokenRequest.StructuredRuntimeData)
279+
pub StructuredRuntimeData: ::std::option::Option<::std::string::String>,
278280
// special fields
279281
// @@protoc_insertion_point(special_field:attestation_agent.GetTokenRequest.special_fields)
280282
pub special_fields: ::protobuf::SpecialFields,
@@ -292,13 +294,18 @@ impl GetTokenRequest {
292294
}
293295

294296
fn generated_message_descriptor_data() -> ::protobuf::reflect::GeneratedMessageDescriptorData {
295-
let mut fields = ::std::vec::Vec::with_capacity(1);
297+
let mut fields = ::std::vec::Vec::with_capacity(2);
296298
let mut oneofs = ::std::vec::Vec::with_capacity(0);
297299
fields.push(::protobuf::reflect::rt::v2::make_simpler_field_accessor::<_, _>(
298300
"TokenType",
299301
|m: &GetTokenRequest| { &m.TokenType },
300302
|m: &mut GetTokenRequest| { &mut m.TokenType },
301303
));
304+
fields.push(::protobuf::reflect::rt::v2::make_option_accessor::<_, _>(
305+
"StructuredRuntimeData",
306+
|m: &GetTokenRequest| { &m.StructuredRuntimeData },
307+
|m: &mut GetTokenRequest| { &mut m.StructuredRuntimeData },
308+
));
302309
::protobuf::reflect::GeneratedMessageDescriptorData::new_2::<GetTokenRequest>(
303310
"GetTokenRequest",
304311
fields,
@@ -320,6 +327,9 @@ impl ::protobuf::Message for GetTokenRequest {
320327
10 => {
321328
self.TokenType = is.read_string()?;
322329
},
330+
18 => {
331+
self.StructuredRuntimeData = ::std::option::Option::Some(is.read_string()?);
332+
},
323333
tag => {
324334
::protobuf::rt::read_unknown_or_skip_group(tag, is, self.special_fields.mut_unknown_fields())?;
325335
},
@@ -335,6 +345,9 @@ impl ::protobuf::Message for GetTokenRequest {
335345
if !self.TokenType.is_empty() {
336346
my_size += ::protobuf::rt::string_size(1, &self.TokenType);
337347
}
348+
if let Some(v) = self.StructuredRuntimeData.as_ref() {
349+
my_size += ::protobuf::rt::string_size(2, &v);
350+
}
338351
my_size += ::protobuf::rt::unknown_fields_size(self.special_fields.unknown_fields());
339352
self.special_fields.cached_size().set(my_size as u32);
340353
my_size
@@ -344,6 +357,9 @@ impl ::protobuf::Message for GetTokenRequest {
344357
if !self.TokenType.is_empty() {
345358
os.write_string(1, &self.TokenType)?;
346359
}
360+
if let Some(v) = self.StructuredRuntimeData.as_ref() {
361+
os.write_string(2, v)?;
362+
}
347363
os.write_unknown_fields(self.special_fields.unknown_fields())?;
348364
::std::result::Result::Ok(())
349365
}
@@ -362,12 +378,14 @@ impl ::protobuf::Message for GetTokenRequest {
362378

363379
fn clear(&mut self) {
364380
self.TokenType.clear();
381+
self.StructuredRuntimeData = ::std::option::Option::None;
365382
self.special_fields.clear();
366383
}
367384

368385
fn default_instance() -> &'static GetTokenRequest {
369386
static instance: GetTokenRequest = GetTokenRequest {
370387
TokenType: ::std::string::String::new(),
388+
StructuredRuntimeData: ::std::option::Option::None,
371389
special_fields: ::protobuf::SpecialFields::new(),
372390
};
373391
&instance
@@ -517,12 +535,14 @@ static file_descriptor_proto_data: &'static [u8] = b"\
517535
\n\x17attestation_agent.proto\x12\x11attestation_agent\"6\n\x12GetEviden\
518536
ceRequest\x12\x20\n\x0bRuntimeData\x18\x01\x20\x01(\x0cR\x0bRuntimeData\
519537
\"1\n\x13GetEvidenceResponse\x12\x1a\n\x08Evidence\x18\x01\x20\x01(\x0cR\
520-
\x08Evidence\"/\n\x0fGetTokenRequest\x12\x1c\n\tTokenType\x18\x01\x20\
521-
\x01(\tR\tTokenType\"(\n\x10GetTokenResponse\x12\x14\n\x05Token\x18\x01\
522-
\x20\x01(\x0cR\x05Token2\xcc\x01\n\x17AttestationAgentService\x12\\\n\
523-
\x0bGetEvidence\x12%.attestation_agent.GetEvidenceRequest\x1a&.attestati\
524-
on_agent.GetEvidenceResponse\x12S\n\x08GetToken\x12\".attestation_agent.\
525-
GetTokenRequest\x1a#.attestation_agent.GetTokenResponseb\x06proto3\
538+
\x08Evidence\"\x84\x01\n\x0fGetTokenRequest\x12\x1c\n\tTokenType\x18\x01\
539+
\x20\x01(\tR\tTokenType\x129\n\x15StructuredRuntimeData\x18\x02\x20\x01(\
540+
\tH\0R\x15StructuredRuntimeData\x88\x01\x01B\x18\n\x16_StructuredRuntime\
541+
Data\"(\n\x10GetTokenResponse\x12\x14\n\x05Token\x18\x01\x20\x01(\x0cR\
542+
\x05Token2\xcc\x01\n\x17AttestationAgentService\x12\\\n\x0bGetEvidence\
543+
\x12%.attestation_agent.GetEvidenceRequest\x1a&.attestation_agent.GetEvi\
544+
denceResponse\x12S\n\x08GetToken\x12\".attestation_agent.GetTokenRequest\
545+
\x1a#.attestation_agent.GetTokenResponseb\x06proto3\
526546
";
527547

528548
/// `FileDescriptorProto` object which was a source for this generated file

attestation-agent/app/src/rpc/attestation/mod.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,12 @@ pub mod grpc {
4646
debug!("Call AA to get token ...");
4747

4848
let token = attestation_agent
49-
.get_token(&request.token_type)
49+
.get_token(
50+
&request.token_type,
51+
&request
52+
.structured_runtime_data
53+
.unwrap_or_else(|| String::from("{}")),
54+
)
5055
.await
5156
.map_err(|e| {
5257
error!("Call AA to get token failed: {}", e);
@@ -186,7 +191,11 @@ pub mod ttrpc {
186191
let mut attestation_agent = attestation_agent_mutex_clone.lock().await;
187192

188193
let token = attestation_agent
189-
.get_token(&req.TokenType)
194+
.get_token(
195+
&req.TokenType,
196+
&req.StructuredRuntimeData
197+
.unwrap_or_else(|| String::from("{}")),
198+
)
190199
.await
191200
.map_err(|e| {
192201
error!("Call AA-KBC to get token failed: {}", e);

attestation-agent/kbs_protocol/src/token_provider/aa/attestation_agent.rs

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,8 @@ pub struct GetTokenRequest {
275275
// message fields
276276
// @@protoc_insertion_point(field:attestation_agent.GetTokenRequest.TokenType)
277277
pub TokenType: ::std::string::String,
278+
// @@protoc_insertion_point(field:attestation_agent.GetTokenRequest.StructuredRuntimeData)
279+
pub StructuredRuntimeData: ::std::option::Option<::std::string::String>,
278280
// special fields
279281
// @@protoc_insertion_point(special_field:attestation_agent.GetTokenRequest.special_fields)
280282
pub special_fields: ::protobuf::SpecialFields,
@@ -292,13 +294,18 @@ impl GetTokenRequest {
292294
}
293295

294296
fn generated_message_descriptor_data() -> ::protobuf::reflect::GeneratedMessageDescriptorData {
295-
let mut fields = ::std::vec::Vec::with_capacity(1);
297+
let mut fields = ::std::vec::Vec::with_capacity(2);
296298
let mut oneofs = ::std::vec::Vec::with_capacity(0);
297299
fields.push(::protobuf::reflect::rt::v2::make_simpler_field_accessor::<_, _>(
298300
"TokenType",
299301
|m: &GetTokenRequest| { &m.TokenType },
300302
|m: &mut GetTokenRequest| { &mut m.TokenType },
301303
));
304+
fields.push(::protobuf::reflect::rt::v2::make_option_accessor::<_, _>(
305+
"StructuredRuntimeData",
306+
|m: &GetTokenRequest| { &m.StructuredRuntimeData },
307+
|m: &mut GetTokenRequest| { &mut m.StructuredRuntimeData },
308+
));
302309
::protobuf::reflect::GeneratedMessageDescriptorData::new_2::<GetTokenRequest>(
303310
"GetTokenRequest",
304311
fields,
@@ -320,6 +327,9 @@ impl ::protobuf::Message for GetTokenRequest {
320327
10 => {
321328
self.TokenType = is.read_string()?;
322329
},
330+
18 => {
331+
self.StructuredRuntimeData = ::std::option::Option::Some(is.read_string()?);
332+
},
323333
tag => {
324334
::protobuf::rt::read_unknown_or_skip_group(tag, is, self.special_fields.mut_unknown_fields())?;
325335
},
@@ -335,6 +345,9 @@ impl ::protobuf::Message for GetTokenRequest {
335345
if !self.TokenType.is_empty() {
336346
my_size += ::protobuf::rt::string_size(1, &self.TokenType);
337347
}
348+
if let Some(v) = self.StructuredRuntimeData.as_ref() {
349+
my_size += ::protobuf::rt::string_size(2, &v);
350+
}
338351
my_size += ::protobuf::rt::unknown_fields_size(self.special_fields.unknown_fields());
339352
self.special_fields.cached_size().set(my_size as u32);
340353
my_size
@@ -344,6 +357,9 @@ impl ::protobuf::Message for GetTokenRequest {
344357
if !self.TokenType.is_empty() {
345358
os.write_string(1, &self.TokenType)?;
346359
}
360+
if let Some(v) = self.StructuredRuntimeData.as_ref() {
361+
os.write_string(2, v)?;
362+
}
347363
os.write_unknown_fields(self.special_fields.unknown_fields())?;
348364
::std::result::Result::Ok(())
349365
}
@@ -362,12 +378,14 @@ impl ::protobuf::Message for GetTokenRequest {
362378

363379
fn clear(&mut self) {
364380
self.TokenType.clear();
381+
self.StructuredRuntimeData = ::std::option::Option::None;
365382
self.special_fields.clear();
366383
}
367384

368385
fn default_instance() -> &'static GetTokenRequest {
369386
static instance: GetTokenRequest = GetTokenRequest {
370387
TokenType: ::std::string::String::new(),
388+
StructuredRuntimeData: ::std::option::Option::None,
371389
special_fields: ::protobuf::SpecialFields::new(),
372390
};
373391
&instance
@@ -1125,23 +1143,25 @@ static file_descriptor_proto_data: &'static [u8] = b"\
11251143
\n\x17attestation-agent.proto\x12\x11attestation_agent\"6\n\x12GetEviden\
11261144
ceRequest\x12\x20\n\x0bRuntimeData\x18\x01\x20\x01(\x0cR\x0bRuntimeData\
11271145
\"1\n\x13GetEvidenceResponse\x12\x1a\n\x08Evidence\x18\x01\x20\x01(\x0cR\
1128-
\x08Evidence\"/\n\x0fGetTokenRequest\x12\x1c\n\tTokenType\x18\x01\x20\
1129-
\x01(\tR\tTokenType\"(\n\x10GetTokenResponse\x12\x14\n\x05Token\x18\x01\
1130-
\x20\x01(\x0cR\x05Token\"v\n\x1fExtendRuntimeMeasurementRequest\x12\x16\
1131-
\n\x06Events\x18\x01\x20\x03(\x0cR\x06Events\x12)\n\rRegisterIndex\x18\
1132-
\x02\x20\x01(\x04H\0R\rRegisterIndex\x88\x01\x01B\x10\n\x0e_RegisterInde\
1133-
x\"\"\n\x20ExtendRuntimeMeasurementResponse\"K\n\x11InitDataPlaintext\
1134-
\x12\x18\n\x07Content\x18\x01\x20\x01(\x0cR\x07Content\x12\x1c\n\tAlgori\
1135-
thm\x18\x02\x20\x01(\tR\tAlgorithm\".\n\x14CheckInitDataRequest\x12\x16\
1136-
\n\x06Digest\x18\x01\x20\x01(\x0cR\x06Digest\"\x17\n\x15CheckInitDataRes\
1137-
ponse2\xb6\x03\n\x17AttestationAgentService\x12\\\n\x0bGetEvidence\x12%.\
1138-
attestation_agent.GetEvidenceRequest\x1a&.attestation_agent.GetEvidenceR\
1139-
esponse\x12S\n\x08GetToken\x12\".attestation_agent.GetTokenRequest\x1a#.\
1140-
attestation_agent.GetTokenResponse\x12\x83\x01\n\x18ExtendRuntimeMeasure\
1141-
ment\x122.attestation_agent.ExtendRuntimeMeasurementRequest\x1a3.attesta\
1142-
tion_agent.ExtendRuntimeMeasurementResponse\x12b\n\rCheckInitData\x12'.a\
1143-
ttestation_agent.CheckInitDataRequest\x1a(.attestation_agent.CheckInitDa\
1144-
taResponseb\x06proto3\
1146+
\x08Evidence\"\x84\x01\n\x0fGetTokenRequest\x12\x1c\n\tTokenType\x18\x01\
1147+
\x20\x01(\tR\tTokenType\x129\n\x15StructuredRuntimeData\x18\x02\x20\x01(\
1148+
\tH\0R\x15StructuredRuntimeData\x88\x01\x01B\x18\n\x16_StructuredRuntime\
1149+
Data\"(\n\x10GetTokenResponse\x12\x14\n\x05Token\x18\x01\x20\x01(\x0cR\
1150+
\x05Token\"v\n\x1fExtendRuntimeMeasurementRequest\x12\x16\n\x06Events\
1151+
\x18\x01\x20\x03(\x0cR\x06Events\x12)\n\rRegisterIndex\x18\x02\x20\x01(\
1152+
\x04H\0R\rRegisterIndex\x88\x01\x01B\x10\n\x0e_RegisterIndex\"\"\n\x20Ex\
1153+
tendRuntimeMeasurementResponse\"K\n\x11InitDataPlaintext\x12\x18\n\x07Co\
1154+
ntent\x18\x01\x20\x01(\x0cR\x07Content\x12\x1c\n\tAlgorithm\x18\x02\x20\
1155+
\x01(\tR\tAlgorithm\".\n\x14CheckInitDataRequest\x12\x16\n\x06Digest\x18\
1156+
\x01\x20\x01(\x0cR\x06Digest\"\x17\n\x15CheckInitDataResponse2\xb6\x03\n\
1157+
\x17AttestationAgentService\x12\\\n\x0bGetEvidence\x12%.attestation_agen\
1158+
t.GetEvidenceRequest\x1a&.attestation_agent.GetEvidenceResponse\x12S\n\
1159+
\x08GetToken\x12\".attestation_agent.GetTokenRequest\x1a#.attestation_ag\
1160+
ent.GetTokenResponse\x12\x83\x01\n\x18ExtendRuntimeMeasurement\x122.atte\
1161+
station_agent.ExtendRuntimeMeasurementRequest\x1a3.attestation_agent.Ext\
1162+
endRuntimeMeasurementResponse\x12b\n\rCheckInitData\x12'.attestation_age\
1163+
nt.CheckInitDataRequest\x1a(.attestation_agent.CheckInitDataResponseb\
1164+
\x06proto3\
11451165
";
11461166

11471167
/// `FileDescriptorProto` object which was a source for this generated file

attestation-agent/lib/src/lib.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,11 @@ pub trait AttestationAPIs {
7878
) -> Result<Vec<u8>>;
7979

8080
/// Get attestation Token
81-
async fn get_token(&mut self, token_type: &str) -> Result<Vec<u8>>;
81+
async fn get_token(
82+
&mut self,
83+
token_type: &str,
84+
structured_runtime_data: &str,
85+
) -> Result<Vec<u8>>;
8286

8387
/// Get TEE hardware signed evidence that includes the runtime data.
8488
async fn get_evidence(&mut self, runtime_data: &[u8]) -> Result<Vec<u8>>;
@@ -190,7 +194,11 @@ impl AttestationAPIs for AttestationAgent {
190194
}
191195

192196
#[allow(unreachable_code)]
193-
async fn get_token(&mut self, _token_type: &str) -> Result<Vec<u8>> {
197+
async fn get_token(
198+
&mut self,
199+
_token_type: &str,
200+
_structured_runtime_data: &str,
201+
) -> Result<Vec<u8>> {
194202
let _uri = match self.config.as_ref() {
195203
Some(c) => c.as_uri.clone(),
196204
None => {
@@ -207,13 +215,13 @@ impl AttestationAPIs for AttestationAgent {
207215
#[cfg(feature = "kbs")]
208216
TokenType::Kbs => {
209217
token::kbs::KbsTokenGetter::default()
210-
.get_token(_uri)
218+
.get_token(_uri, _structured_runtime_data)
211219
.await?
212220
}
213221
#[cfg(feature = "coco_as")]
214222
TokenType::CoCoAS => {
215223
token::coco_as::CoCoASTokenGetter::default()
216-
.get_token(_uri)
224+
.get_token(_uri, _structured_runtime_data)
217225
.await?
218226
}
219227
};

attestation-agent/lib/src/token/coco_as.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,37 @@ use anyhow::*;
88
use async_trait::async_trait;
99
use base64::engine::general_purpose::URL_SAFE_NO_PAD;
1010
use base64::Engine;
11+
use sha2::{Digest, Sha384};
1112

1213
#[derive(Default)]
1314
pub struct CoCoASTokenGetter {}
1415

1516
#[async_trait]
1617
impl GetToken for CoCoASTokenGetter {
17-
async fn get_token(&self, as_uri: String) -> Result<Vec<u8>> {
18+
async fn get_token(&self, as_uri: String, structured_runtime_data: &str) -> Result<Vec<u8>> {
19+
let structured_value: serde_json::Value = serde_json::from_str(structured_runtime_data)
20+
.context("Get Token Failed: Structured Runtime Data must be a JSON Map")?;
21+
22+
// TODO: Request AS to get Nonce and insert the Nonce into structured runtime data JSON Map.
23+
24+
let hash_materials =
25+
serde_json::to_vec(&structured_value).context("parse JSON structured data")?;
26+
let mut hasher = Sha384::new();
27+
hasher.update(hash_materials);
28+
let structured_data_digest = hasher.finalize().to_vec();
29+
1830
let tee_type = attester::detect_tee_type();
1931
let attester = attester::BoxedAttester::try_from(tee_type)?;
20-
let evidence = attester.get_evidence(vec![]).await?;
32+
let evidence = attester
33+
.get_evidence(structured_data_digest.clone())
34+
.await?;
2135

2236
let request_body = serde_json::json!({
2337
"tee": serde_json::to_string(&tee_type)?,
38+
"runtime_data": {
39+
"structured": structured_value
40+
},
41+
"runtime_data_hash_algorithm": "sha384",
2442
"evidence": URL_SAFE_NO_PAD.encode(evidence.as_bytes()),
2543
});
2644

0 commit comments

Comments
 (0)