feat: DID rotation + federation peer notification (Part 3)
- node.rotate-did: generates new Ed25519 keypair, signs rotation proof with old key, overwrites identity files, requires password - federation.notify-did-change: broadcasts rotation proof to all trusted/observer peers over Tor - federation.peer-did-changed: receiving side verifies rotation proof against known pubkey before updating peer's DID - Rate-limited: 3/600s for rotation, 5/60s for peer notification - Signature verification uses ed25519_dalek (constant-time) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.6
parent
9f5cad7de0
commit
fb953e888a
@@ -1,8 +1,11 @@
|
||||
use super::RpcHandler;
|
||||
use crate::{backup, identity, nostr_discovery};
|
||||
use crate::container::docker_packages;
|
||||
use anyhow::Result;
|
||||
use anyhow::{Context, Result};
|
||||
use ed25519_dalek::SigningKey;
|
||||
use nostr_sdk::ToBech32;
|
||||
use rand::rngs::OsRng;
|
||||
use tokio::fs;
|
||||
|
||||
impl RpcHandler {
|
||||
pub(super) async fn handle_node_did(&self) -> Result<serde_json::Value> {
|
||||
@@ -145,4 +148,81 @@ impl RpcHandler {
|
||||
"error": status.error,
|
||||
}))
|
||||
}
|
||||
|
||||
/// Rotate the node's Ed25519 identity keypair.
|
||||
/// Requires password re-verification. Returns a signed proof that peers can
|
||||
/// use to verify the rotation was authorized by the holder of the old key.
|
||||
pub(super) async fn handle_node_rotate_did(
|
||||
&self,
|
||||
params: Option<serde_json::Value>,
|
||||
) -> Result<serde_json::Value> {
|
||||
let params = params.ok_or_else(|| anyhow::anyhow!("Missing params"))?;
|
||||
let password = params
|
||||
.get("password")
|
||||
.and_then(|v| v.as_str())
|
||||
.ok_or_else(|| anyhow::anyhow!("Missing 'password' parameter"))?;
|
||||
|
||||
// Re-verify password before allowing key rotation
|
||||
if !self.auth_manager.verify_password(password).await? {
|
||||
anyhow::bail!("Password verification failed");
|
||||
}
|
||||
|
||||
let identity_dir = self.config.data_dir.join("identity");
|
||||
|
||||
// Load the current identity to get old DID and signing key
|
||||
let old_identity = identity::NodeIdentity::load_or_create(&identity_dir).await?;
|
||||
let old_pubkey_hex = old_identity.pubkey_hex();
|
||||
let old_did = identity::did_key_from_pubkey_hex(&old_pubkey_hex)?;
|
||||
|
||||
// Generate a new Ed25519 keypair
|
||||
let new_signing_key = SigningKey::generate(&mut OsRng);
|
||||
let new_pubkey_hex = hex::encode(new_signing_key.verifying_key().as_bytes());
|
||||
let new_did = identity::did_key_from_pubkey_hex(&new_pubkey_hex)?;
|
||||
|
||||
// Create a rotation proof signed by the OLD key:
|
||||
// "did-rotate:{old_did}:{new_did}:{timestamp}"
|
||||
let timestamp = chrono::Utc::now().to_rfc3339();
|
||||
let proof_message = format!("did-rotate:{}:{}:{}", old_did, new_did, timestamp);
|
||||
let proof_signature = old_identity.sign(proof_message.as_bytes());
|
||||
|
||||
// Write the new key files, overwriting the old ones
|
||||
let key_path = identity_dir.join("node_key");
|
||||
let pub_path = identity_dir.join("node_key.pub");
|
||||
|
||||
fs::write(&key_path, new_signing_key.to_bytes())
|
||||
.await
|
||||
.context("Failed to write new node key")?;
|
||||
|
||||
#[cfg(unix)]
|
||||
{
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
fs::set_permissions(&key_path, std::fs::Permissions::from_mode(0o600))
|
||||
.await
|
||||
.context("Failed to set key permissions")?;
|
||||
}
|
||||
|
||||
fs::write(&pub_path, new_signing_key.verifying_key().as_bytes())
|
||||
.await
|
||||
.context("Failed to write new node public key")?;
|
||||
|
||||
// Update in-memory state so the new pubkey is reflected immediately
|
||||
let (mut data, _) = self.state_manager.get_snapshot().await;
|
||||
data.server_info.pubkey = new_pubkey_hex.clone();
|
||||
self.state_manager.update_data(data).await;
|
||||
|
||||
tracing::info!(
|
||||
old_did = %old_did,
|
||||
new_did = %new_did,
|
||||
"Node DID rotated successfully"
|
||||
);
|
||||
|
||||
Ok(serde_json::json!({
|
||||
"old_did": old_did,
|
||||
"new_did": new_did,
|
||||
"new_pubkey": new_pubkey_hex,
|
||||
"proof_signature": proof_signature,
|
||||
"proof_message": proof_message,
|
||||
"timestamp": timestamp,
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user