Remove added length byte for keys

This commit is contained in:
Bastian Gruber 2025-07-24 13:09:17 +02:00
parent 9fa095873c
commit d14ce407fa
No known key found for this signature in database
GPG key ID: D2DF996A188CFBA2
6 changed files with 47 additions and 15 deletions

11
Cargo.lock generated
View file

@ -360,9 +360,9 @@ checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6"
[[package]]
name = "bhttp"
version = "0.5.4"
version = "0.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2bc657efe5aa3821f1cacfb47665c32849e09820844bff9f5066227312829fa3"
checksum = "16fc24bc615b9fd63148f59b218ea58a444b55762f8845da910e23aca686398b"
dependencies = [
"thiserror 1.0.69",
]
@ -1806,9 +1806,9 @@ dependencies = [
[[package]]
name = "ohttp"
version = "0.5.4"
version = "0.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "10a20082b908632960d0aa59af61e2771502b40249d55986e8bdbcd06d723ea5"
checksum = "622b8959bde5da6c70b0a49e8aa359e0c79c5e8ffd23eb3781c0cc575903d862"
dependencies = [
"aead 0.4.3",
"aes-gcm 0.9.2",
@ -1817,7 +1817,6 @@ dependencies = [
"hex",
"hkdf 0.11.0",
"hpke",
"lazy_static",
"log",
"rand",
"serde",
@ -1829,7 +1828,7 @@ dependencies = [
[[package]]
name = "ohttp-gateway"
version = "0.2.0"
version = "0.2.5"
dependencies = [
"anyhow",
"axum",

View file

@ -1,6 +1,6 @@
[package]
authors = ["Bastian Gruber <foreach@me.com>"]
version = "0.2.0"
version = "0.2.5"
edition = "2024"
name = "ohttp-gateway"
categories = ["web-programming", "web-programming::http-server"]
@ -22,8 +22,8 @@ hyper-util = { version = "0.1", features = ["full"] }
reqwest = { version = "0.12", features = ["json", "stream"] }
# OHTTP implementation - Using the martinthomson/ohttp crate
ohttp = { version = "0.5", features = ["rust-hpke"] }
bhttp = "0.5"
ohttp = { version = "0.6", features = ["rust-hpke"] }
bhttp = "0.6"
# Middleware and utilities
tower = "0.4"

View file

@ -90,6 +90,8 @@ async fn handle_ohttp_request_inner(
GatewayError::DecryptionError(format!("Failed to decapsulate: {e}"))
})?;
debug!("Request: {:#?}", bhttp_request);
debug!(
"Successfully decapsulated request, {} bytes",
bhttp_request.len()
@ -178,6 +180,8 @@ fn validate_ohttp_request(
/// Parse binary HTTP message with error handling
fn parse_bhttp_message(data: &[u8]) -> Result<Message, GatewayError> {
let mut cursor = std::io::Cursor::new(data);
debug!("Cursor: std::io::Cursor::new(data): {:?}", cursor);
Message::read_bhttp(&mut cursor)
.map_err(|e| GatewayError::InvalidRequest(format!("Failed to parse binary HTTP: {e}")))
}

View file

@ -216,8 +216,7 @@ impl KeyManager {
.config
.encode()?;
let mut out = Vec::with_capacity(cfg_bytes.len() + 2);
out.extend_from_slice(&(cfg_bytes.len() as u16).to_be_bytes()); // 2-byte length
let mut out = Vec::with_capacity(cfg_bytes.len());
out.extend_from_slice(&cfg_bytes);
Ok(out)
}

View file

@ -163,14 +163,14 @@ async fn test_config_serialization_format() {
let encoded_config = manager.get_encoded_config().await.unwrap();
// Verify basic structure: length prefix + config data
assert!(encoded_config.len() >= 4);
assert!(encoded_config.len() >= 2);
let length = u16::from_be_bytes([encoded_config[0], encoded_config[1]]);
assert_eq!(length as usize, encoded_config.len() - 2);
assert_eq!(length as usize, encoded_config.len());
// Verify it contains expected OHTTP key configuration elements
// The exact format would depend on your implementation
let config_data = &encoded_config[2..];
let config_data = &encoded_config[..];
assert!(!config_data.is_empty());
}

View file

@ -1,6 +1,9 @@
use bhttp::Message;
use ohttp_gateway::GatewayError;
use std::io::Cursor;
use std::time::Duration;
use tokio;
use tracing::debug;
// Your key manager module - adjust the import path as needed
use ohttp_gateway::key_manager::{CipherSuiteConfig, KeyManager, KeyManagerConfig};
@ -170,3 +173,30 @@ async fn test_cleanup_expired_keys() {
// Should have cleaned up the expired key
assert!(final_stats.total_keys <= 2);
}
#[tokio::test]
async fn test_bhttp_parsing() {
// let data = &[
// 2, 3, 71, 69, 84, 5, 104, 116, 116, 112, 115, 9, 108, 111, 99, 97, 108, 104, 111, 115, 116,
// 4, 47, 103, 101, 116, 10, 117, 115, 101, 114, 45, 97, 103, 101, 110, 116, 21, 79, 72, 84,
// 84, 80, 45, 84, 101, 115, 116, 45, 67, 108, 105, 101, 110, 116, 47, 49, 46, 48, 6, 97, 99,
// 99, 101, 112, 116, 16, 97, 112, 112, 108, 105, 99, 97, 116, 105, 111, 110, 47, 106, 115,
// 111, 110, 0, 0,
// ];
// let mut cursor = std::io::Cursor::new(data);
//
// let m = Message::read_bhttp(&mut cursor).unwrap();
//
// println!("TEST {:?}", m);
const REQUEST: &[u8] = &[
2, 3, 71, 69, 84, 5, 104, 116, 116, 112, 115, 9, 108, 111, 99, 97, 108, 104, 111, 115, 116,
4, 47, 103, 101, 116, 10, 117, 115, 101, 114, 45, 97, 103, 101, 110, 116, 21, 79, 72, 84,
84, 80, 45, 84, 101, 115, 116, 45, 67, 108, 105, 101, 110, 116, 47, 49, 46, 48, 6, 97, 99,
99, 101, 112, 116, 16, 97, 112, 112, 108, 105, 99, 97, 116, 105, 111, 110, 47, 106, 115,
111, 110, 0, 0,
];
let m = Message::read_bhttp(&mut Cursor::new(REQUEST)).unwrap();
println!("TEST {:?}", m);
assert!(m.header().get(b"accept").is_some());
}