diff --git a/Cargo.lock b/Cargo.lock index 5055c004e..aefb7a3b3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3075,9 +3075,9 @@ dependencies = [ [[package]] name = "kbs-types" -version = "0.6.0" +version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "febd73b2b1df274ea454d81ddf76f596af9754410b7ed6f988f2e1782a175da3" +checksum = "9b6441ed73b0faa50707d4de41c6b45c76654b661b96aaf7b26a41331eedc0a5" dependencies = [ "serde", "serde_json", diff --git a/Cargo.toml b/Cargo.toml index d01757083..e96b53e42 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -38,7 +38,7 @@ env_logger = "0.11.3" hex = "0.4.3" hmac = "0.12.1" jwt-simple = { version = "0.12", default-features = false, features = ["pure-rust"] } -kbs-types = "0.6.0" +kbs-types = "0.7.0" lazy_static = "1.4.0" log = "0.4.22" nix = "0.28" diff --git a/attestation-agent/kbs_protocol/src/client/mod.rs b/attestation-agent/kbs_protocol/src/client/mod.rs index c9077f6c9..5febb6911 100644 --- a/attestation-agent/kbs_protocol/src/client/mod.rs +++ b/attestation-agent/kbs_protocol/src/client/mod.rs @@ -48,7 +48,7 @@ pub struct KbsClient { pub(crate) token: Option, } -pub const KBS_PROTOCOL_VERSION: &str = "0.1.0"; +pub const KBS_PROTOCOL_VERSION: &str = "0.1.1"; pub const KBS_GET_RESOURCE_MAX_ATTEMPT: u64 = 3; diff --git a/attestation-agent/kbs_protocol/src/client/rcar_client.rs b/attestation-agent/kbs_protocol/src/client/rcar_client.rs index 23a2c8b4e..7c4b453d3 100644 --- a/attestation-agent/kbs_protocol/src/client/rcar_client.rs +++ b/attestation-agent/kbs_protocol/src/client/rcar_client.rs @@ -104,21 +104,39 @@ impl KbsClient> { let request = Request { version: String::from(KBS_PROTOCOL_VERSION), tee, - extra_params: String::new(), + extra_params: serde_json::Value::String(String::new()), }; debug!("send auth request to {auth_endpoint}"); - let challenge = self + let resp = self .http_client .post(auth_endpoint) .header("Content-Type", "application/json") .json(&request) .send() - .await? - .json::() .await?; + match resp.status() { + reqwest::StatusCode::OK => { + debug!("KBS request OK"); + } + reqwest::StatusCode::UNAUTHORIZED => { + let error_info = resp.json::().await?; + bail!( + "KBS request unauthorized, ErrorInformation: {:?}", + error_info + ); + } + _ => { + bail!( + "KBS Server Internal Failed, Response: {:?}", + resp.text().await? + ); + } + } + + let challenge = resp.json::().await?; debug!("get challenge: {challenge:#?}"); let tee_pubkey = self.tee_key.export_pubkey()?; let runtime_data = json!({ @@ -135,7 +153,7 @@ impl KbsClient> { let attest_endpoint = format!("{}/{KBS_PREFIX}/attest", self.kbs_host_url); let attest = Attestation { tee_pubkey, - tee_evidence: evidence, + tee_evidence: serde_json::from_str(&evidence)?, // TODO: change attesters to return Value? }; debug!("send attest request."); @@ -345,10 +363,19 @@ mod test { .try_into() .expect("resource uri"); - let resource = client - .get_resource(resource_uri) - .await - .expect("get resource"); + let resource = match client.get_resource(resource_uri).await { + Ok(resource) => resource, + Err(e) => { + // Skip the test if the kbs server returned ProtocolVersion error. Any other + // error is treated as a failure. + assert!(e + .to_string() + .contains("KBS Client Protocol Version Mismatch")); + println!("NOTE: the test is skipped due to KBS protocol incompatibility."); + return (); + } + }; + assert_eq!(resource, CONTENT); let (token, key) = client.get_token().await.expect("get token"); diff --git a/attestation-agent/kbs_protocol/src/error.rs b/attestation-agent/kbs_protocol/src/error.rs index d46d42889..930dc3244 100644 --- a/attestation-agent/kbs_protocol/src/error.rs +++ b/attestation-agent/kbs_protocol/src/error.rs @@ -42,6 +42,6 @@ pub enum Error { #[error("KBS resource not found: {0}")] ResourceNotFound(String), - #[error("request unautorized")] + #[error("request unauthorized")] UnAuthorized, } diff --git a/attestation-agent/kbs_protocol/src/keypair.rs b/attestation-agent/kbs_protocol/src/keypair.rs index 12670a83e..897724e3e 100644 --- a/attestation-agent/kbs_protocol/src/keypair.rs +++ b/attestation-agent/kbs_protocol/src/keypair.rs @@ -7,7 +7,7 @@ use anyhow::{Context, Result}; use base64::{engine::general_purpose::URL_SAFE_NO_PAD, Engine}; use crypto::{ - rsa::{PaddingMode, RSAKeyPair, RSA_KTY}, + rsa::{PaddingMode, RSAKeyPair}, WrapType, }; use kbs_types::{Response, TeePubKey}; @@ -31,11 +31,10 @@ impl TeeKeyPair { let k_mod = URL_SAFE_NO_PAD.encode(self.keypair.n()); let k_exp = URL_SAFE_NO_PAD.encode(self.keypair.e()); - Ok(TeePubKey { + Ok(TeePubKey::RSA { alg: PaddingMode::PKCS1v15.as_ref().to_string(), k_mod, k_exp, - kty: RSA_KTY.to_string(), }) }