From fe195599646f29c738a78f9df85039c3743582a5 Mon Sep 17 00:00:00 2001 From: "Dustin C. Hatch" Date: Sat, 9 Nov 2024 13:40:03 -0600 Subject: [PATCH] server: Add /user/ca route The _GET /user/ca_ operation returns the public key of the user CA. This can be used by hosts to "bootstrap" their trusted signing keys for user authentication. --- src/server/mod.rs | 1 + src/server/user.rs | 24 +++++++++++++++++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/server/mod.rs b/src/server/mod.rs index edd7bf2..6971f65 100644 --- a/src/server/mod.rs +++ b/src/server/mod.rs @@ -44,6 +44,7 @@ pub fn make_app(config: Configuration) -> Router { .route("/host/sign", post(host::sign_host_cert)) .route("/user/oidc-config", get(user::get_oidc_config)) .route("/user/sign", post(user::sign_user_cert)) + .route("/user/ca", get(user::get_ca_pubkey)) .with_state(ctx) } diff --git a/src/server/user.rs b/src/server/user.rs index 058bde1..bdabc81 100644 --- a/src/server/user.rs +++ b/src/server/user.rs @@ -22,8 +22,8 @@ use ssh_key::Algorithm; use tracing::{debug, error, info, trace, warn}; use super::error::SignKeyError; -use super::{AuthError, Context}; use super::oidc; +use super::{AuthError, Context}; use crate::ca; /// Response type for GET /user/openid-config @@ -219,6 +219,28 @@ pub(super) async fn sign_user_cert( Ok(cert.to_openssh().map_err(ca::CertError::from)?) } +/// Get the public key of the user CA +/// +/// Returns a string representation of the CA public key. This can be +/// used by hosts to find the current public key to trust for +/// authenticating users. +pub(super) async fn get_ca_pubkey( + State(ctx): State, +) -> Result { + let config = &ctx.config; + let privkey = ca::load_private_key( + &config.ca.user.private_key_file, + config.ca.user.private_key_passphrase_file.as_ref(), + ) + .await + .map_err(SignKeyError::LoadPrivateKey)?; + let pubkey = privkey.public_key() + .to_openssh() + .map_err(ca::LoadKeyError::SshKey) + .map_err(SignKeyError::LoadPrivateKey)?; + Ok(format!("{}\n", pubkey)) +} + /// Get OIDC provider metadata (possibly from cache) /// /// This function will return metadata for the configured OIDC identity