|
4 | 4 | // |
5 | 5 |
|
6 | 6 | use anyhow::*; |
| 7 | +use base64::Engine; |
7 | 8 | use hyper::body::HttpBody; |
8 | 9 | use hyper::{header, Body, Method, Request, Response, StatusCode}; |
9 | 10 | use serde::Serialize; |
10 | 11 | use std::collections::HashMap; |
11 | 12 | use std::net::SocketAddr; |
12 | 13 | use tracing::{debug, info}; |
13 | 14 |
|
| 15 | +fn decode_runtime_data(raw: &str, encoding: Option<&str>) -> Result<Vec<u8>> { |
| 16 | + match encoding { |
| 17 | + Some("hex") => { |
| 18 | + hex::decode(raw).map_err(|e| anyhow!("invalid hex in runtime_data: {e}")) |
| 19 | + } |
| 20 | + Some("base64") => base64::engine::general_purpose::STANDARD |
| 21 | + .decode(raw) |
| 22 | + .map_err(|e| anyhow!("invalid base64 in runtime_data: {e}")), |
| 23 | + Some(other) => bail!("unsupported encoding: {other} (expected hex, base64, or omit)"), |
| 24 | + None => Ok(raw.as_bytes().to_vec()), |
| 25 | + } |
| 26 | +} |
| 27 | + |
14 | 28 | use crate::client::{ |
15 | | - aa::{AAClient, AaelEvent, AA_AAEL_URL, AA_EVIDENCE_URL, AA_ROOT, AA_TOKEN_URL}, |
| 29 | + aa::{ |
| 30 | + AAClient, AaelEvent, AA_AAEL_URL, AA_ADDITIONAL_EVIDENCE_URL, AA_EVIDENCE_URL, AA_ROOT, |
| 31 | + AA_TOKEN_URL, |
| 32 | + }, |
16 | 33 | cdh::{CDHClient, CDH_RESOURCE_URL, CDH_ROOT}, |
17 | 34 | }; |
18 | 35 | use crate::utils::split_nth_slash; |
@@ -168,8 +185,36 @@ impl Router { |
168 | 185 | info!("Get evidence"); |
169 | 186 | match params.get("runtime_data") { |
170 | 187 | Some(runtime_data) => { |
| 188 | + let data = match decode_runtime_data( |
| 189 | + runtime_data, |
| 190 | + params.get("encoding").map(|s| s.as_str()), |
| 191 | + ) { |
| 192 | + std::result::Result::Ok(d) => d, |
| 193 | + Err(e) => return self.internal_error(e.to_string()), |
| 194 | + }; |
| 195 | + match client.get_evidence(&data).await { |
| 196 | + std::result::Result::Ok(results) => { |
| 197 | + return self.octet_stream_response(results) |
| 198 | + } |
| 199 | + Err(e) => return self.internal_error(e.to_string()), |
| 200 | + } |
| 201 | + } |
| 202 | + None => return self.bad_request(), |
| 203 | + } |
| 204 | + } |
| 205 | + (AA_ADDITIONAL_EVIDENCE_URL, &Method::GET) => { |
| 206 | + info!("Get additional evidence"); |
| 207 | + match params.get("runtime_data") { |
| 208 | + Some(runtime_data) => { |
| 209 | + let data = match decode_runtime_data( |
| 210 | + runtime_data, |
| 211 | + params.get("encoding").map(|s| s.as_str()), |
| 212 | + ) { |
| 213 | + std::result::Result::Ok(d) => d, |
| 214 | + Err(e) => return self.internal_error(e.to_string()), |
| 215 | + }; |
171 | 216 | match client |
172 | | - .get_evidence(&runtime_data.clone().into_bytes()) |
| 217 | + .get_additional_evidence(&data) |
173 | 218 | .await |
174 | 219 | { |
175 | 220 | std::result::Result::Ok(results) => { |
|
0 commit comments