|
1 | 1 | //! Manage the OHTTP key configuration |
2 | 2 |
|
3 | | -use anyhow::Result; |
| 3 | +use std::fs; |
| 4 | +use std::path::{Path, PathBuf}; |
| 5 | + |
| 6 | +use anyhow::{anyhow, Result}; |
| 7 | +use ohttp::hpke::{Aead, Kdf, Kem}; |
| 8 | +use ohttp::SymmetricSuite; |
4 | 9 | use tracing::info; |
5 | 10 |
|
6 | | -pub fn init_ohttp() -> Result<ohttp::Server> { |
7 | | - use ohttp::hpke::{Aead, Kdf, Kem}; |
8 | | - use ohttp::{KeyId, SymmetricSuite}; |
| 11 | +const KEY_ID: u8 = 1; |
| 12 | +const KEM: Kem = Kem::K256Sha256; |
| 13 | +const SYMMETRIC: &[SymmetricSuite] = |
| 14 | + &[SymmetricSuite::new(Kdf::HkdfSha256, Aead::ChaCha20Poly1305)]; |
| 15 | + |
| 16 | +/// OHTTP server key configuration |
| 17 | +/// |
| 18 | +/// This is combined so that the test path and the prod path both use the same |
| 19 | +/// code. The ServerKeyConfig.ikm is persisted to the configured path, and the |
| 20 | +/// server is used to run the directory server. |
| 21 | +#[derive(Debug, Clone)] |
| 22 | +pub struct ServerKeyConfig { |
| 23 | + ikm: [u8; 32], |
| 24 | + server: ohttp::Server, |
| 25 | +} |
| 26 | + |
| 27 | +impl From<ServerKeyConfig> for ohttp::Server { |
| 28 | + fn from(value: ServerKeyConfig) -> Self { value.server } |
| 29 | +} |
| 30 | + |
| 31 | +/// Generate a new OHTTP server key configuration |
| 32 | +pub fn gen_ohttp_server_config() -> Result<ServerKeyConfig> { |
| 33 | + let ikm = bitcoin::key::rand::random::<[u8; 32]>(); |
| 34 | + let config = ohttp::KeyConfig::new(KEY_ID, KEM, Vec::from(SYMMETRIC))?; |
| 35 | + Ok(ServerKeyConfig { ikm, server: ohttp::Server::new(config)? }) |
| 36 | +} |
| 37 | + |
| 38 | +/// Persist an OHTTP Key Configuration to the default path |
| 39 | +pub fn persist_new_key_config(ohttp_config: ServerKeyConfig, dir: &Path) -> Result<PathBuf> { |
| 40 | + use std::fs::OpenOptions; |
| 41 | + use std::io::Write; |
| 42 | + |
| 43 | + let key_path = key_path(dir); |
| 44 | + |
| 45 | + let mut file = OpenOptions::new() |
| 46 | + .write(true) |
| 47 | + .create_new(true) |
| 48 | + .open(&key_path) |
| 49 | + .map_err(|e| anyhow!("Failed to create new OHTTP key file: {}", e))?; |
| 50 | + |
| 51 | + file.write_all(&ohttp_config.ikm) |
| 52 | + .map_err(|e| anyhow!("Failed to write OHTTP keys to file: {}", e))?; |
| 53 | + info!("Saved OHTTP Key Configuration to {}", &key_path.display()); |
| 54 | + |
| 55 | + Ok(key_path) |
| 56 | +} |
| 57 | + |
| 58 | +/// Read the configured server from the default path |
| 59 | +/// May panic if key exists but is the unexpected format. |
| 60 | +pub fn read_server_config(dir: &Path) -> Result<ServerKeyConfig> { |
| 61 | + let key_path = key_path(dir); |
| 62 | + let ikm: [u8; 32] = fs::read(&key_path) |
| 63 | + .map_err(|e| anyhow!("Failed to read OHTTP key file: {}", e))? |
| 64 | + .try_into() |
| 65 | + .expect("Key wrong size: expected 32 bytes"); |
| 66 | + |
| 67 | + let server_config = ohttp::KeyConfig::derive(KEY_ID, KEM, SYMMETRIC.to_vec(), &ikm) |
| 68 | + .expect("Failed to derive OHTTP keys from file"); |
| 69 | + |
| 70 | + info!("Loaded existing OHTTP Key Configuration from {}", key_path.display()); |
| 71 | + Ok(ServerKeyConfig { ikm, server: ohttp::Server::new(server_config)? }) |
| 72 | +} |
| 73 | + |
| 74 | +/// Get the path to the key configuration file |
| 75 | +/// For now, default to [KEY_ID].ikm. |
| 76 | +/// In the future this might be able to save multiple keys named by KeyId. |
| 77 | +fn key_path(dir: &Path) -> PathBuf { dir.join(format!("{}.ikm", KEY_ID)) } |
9 | 78 |
|
10 | | - const KEY_ID: KeyId = 1; |
11 | | - const KEM: Kem = Kem::K256Sha256; |
12 | | - const SYMMETRIC: &[SymmetricSuite] = |
13 | | - &[SymmetricSuite::new(Kdf::HkdfSha256, Aead::ChaCha20Poly1305)]; |
| 79 | +#[cfg(test)] |
| 80 | +mod tests { |
| 81 | + use super::*; |
14 | 82 |
|
15 | | - // create or read from file |
16 | | - let server_config = ohttp::KeyConfig::new(KEY_ID, KEM, Vec::from(SYMMETRIC))?; |
17 | | - info!("Initialized a new OHTTP Key Configuration. GET /ohttp-keys to fetch it."); |
18 | | - Ok(ohttp::Server::new(server_config)?) |
| 83 | + #[test] |
| 84 | + fn round_trip_server_config() { |
| 85 | + let temp_dir = tempfile::tempdir().expect("Failed to create temp dir"); |
| 86 | + let ohttp_config = gen_ohttp_server_config().expect("Failed to generate server config"); |
| 87 | + let _path = persist_new_key_config(ohttp_config.clone(), temp_dir.path()) |
| 88 | + .expect("Failed to persist server config"); |
| 89 | + let ohttp_config_again = |
| 90 | + read_server_config(temp_dir.path()).expect("Failed to read server config"); |
| 91 | + assert_eq!(ohttp_config.ikm, ohttp_config_again.ikm); |
| 92 | + } |
19 | 93 | } |
0 commit comments