Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aien-sovereign-core-mv/blob/main/crates/rad-id-sync/src/keys.rs # aien-sovereign-core-mv AienOS host runtime: inference scheduling, paged KV state, the aien CLI and Spark verification tooling. Its inference engine is in alpha testing (DGX Spark GB10); Omega is the implementation destination. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aien-sovereign-core-mv / crates / rad-id-sync / src / keys.rs` 216 lines ยท 7326 bytes Raw Blame History 1 // SPDX-License-Identifier: Apache-2.0 2 // Copyright 2026 Drake Stapleton (AIEN / aien-dev) 3 // 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at 7 // 8 // http://www.apache.org/licenses/LICENSE-2.0 9 // 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 16 use crate::identity::{IdentityAnchor, KeyRecord}; 17 use sha2::{Digest, Sha256}; 18 use std::fs; 19 use std::path::PathBuf; 20 21 #[derive(Debug, serde::Serialize, serde::Deserialize)] 22 pub struct SyncReport { 23 pub anchor_did: String, 24 pub entity_name: String, 25 pub radicle_key_detected: Option, 26 pub ssh_key_detected: Option, 27 pub in_sync: bool, 28 pub rotated: bool, 29 pub message: String, 30 } 31 32 pub fn get_sha256_fingerprint(key_str: &str) -> String { 33 let mut hasher = Sha256::new(); 34 hasher.update(key_str.trim().as_bytes()); 35 hex::encode(hasher.finalize()) 36 } 37 38 /// Validate format of detected public keys. 39 pub fn validate_public_key_format(key_type: &str, key_str: &str) -> Result<(), String> { 40 let trimmed = key_str.trim(); 41 if trimmed.is_empty() { 42 return Err("Public key is empty".to_string()); 43 } 44 45 match key_type { 46 "ssh-`ed25519`" => { 47 if !trimmed.starts_with("ssh-`ed25519` ") { 48 return Err( 49 "Invalid SSH Ed25519 key format: expected prefix ssh-`ed25519`".to_string(), 50 ); 51 } 52 let parts: Vec<&str> = trimmed.split_whitespace().collect(); 53 if parts.len() < 2 { 54 return Err("Malformed SSH key: missing base64 token payload".to_string()); 55 } 56 if parts[1].len() < 32 { 57 return Err("SSH key payload too short for Ed25519 public key".to_string()); 58 } 59 Ok(()) 60 } 61 "radicle-`ed25519`" => { 62 if trimmed.starts_with("ssh-`ed25519` ") 63 || trimmed.starts_with("rad:") 64 || trimmed.len() >= 32 65 { 66 Ok(()) 67 } else { 68 Err("Invalid Radicle public key length or format".to_string()) 69 } 70 } 71 _ => Ok(()), 72 } 73 } 74 75 pub fn discover_local_keys() -> (Option, Option) { 76 let home = std::env::var("HOME") 77 .or_else(|_| std::env::var("USERPROFILE")) 78 .unwrap_or_else(|_| ".".to_string()); 79 80 // 1. Radicle Key 81 let rad_pub_path = PathBuf::from(&home) 82 .join(".radicle") 83 .join("keys") 84 .join("radicle.pub"); 85 let rad_key = if rad_pub_path.exists() { 86 fs::read_to_string(rad_pub_path) 87 .ok() 88 .map(|s| s.trim().to_string()) 89 } else { 90 None 91 }; 92 93 // 2. SSH Key 94 let ssh_pub_path = PathBuf::from(&home).join(".ssh").join("id_`ed25519`.pub"); 95 let ssh_key = if ssh_pub_path.exists() { 96 fs::read_to_string(ssh_pub_path) 97 .ok() 98 .map(|s| s.trim().to_string()) 99 } else { 100 None 101 }; 102 103 (rad_key, ssh_key) 104 } 105 106 pub fn sync_identity_keys( 107 mut anchor: IdentityAnchor, 108 ) -> Result<(IdentityAnchor, SyncReport), String> { 109 let (rad_key, ssh_key) = discover_local_keys(); 110 let mut rotated = false; 111 let mut message = "Keys are already synchronized with Identity Anchor.".to_string(); 112 113 let now = chrono::Utc::now().to_rfc3339(); 114 115 // Check Radicle Key sync 116 if let Some(ref r_key) = rad_key { 117 validate_public_key_format("radicle-`ed25519`", r_key)?; 118 if anchor.primary_radicle_key.as_ref() != Some(r_key) { 119 if anchor.primary_radicle_key.is_some() { 120 rotated = true; 121 message = 122 "Detected rotated Radicle public key; updated Identity Anchor.".to_string(); 123 } 124 anchor.primary_radicle_key = Some(r_key.clone()); 125 anchor.key_history.push(KeyRecord { 126 key_type: "radicle-`ed25519`".to_string(), 127 public_key: r_key.clone(), 128 finger_print: get_sha256_fingerprint(r_key), 129 added_at: now.clone(), 130 status: "active".to_string(), 131 }); 132 } 133 } 134 135 // Check SSH Key sync 136 if let Some(ref s_key) = ssh_key { 137 validate_public_key_format("ssh-`ed25519`", s_key)?; 138 if anchor.primary_ssh_key.as_ref() != Some(s_key) { 139 if anchor.primary_ssh_key.is_some() { 140 rotated = true; 141 message = "Detected updated SSH public key; updated Identity Anchor.".to_string(); 142 } 143 anchor.primary_ssh_key = Some(s_key.clone()); 144 anchor.key_history.push(KeyRecord { 145 key_type: "ssh-`ed25519`".to_string(), 146 public_key: s_key.clone(), 147 finger_print: get_sha256_fingerprint(s_key), 148 added_at: now.clone(), 149 status: "active".to_string(), 150 }); 151 } 152 } 153 154 anchor.last_synced_at = now; 155 anchor.save()?; 156 157 let in_sync = anchor.primary_ssh_key.is_some() || anchor.primary_radicle_key.is_some(); 158 159 let report = SyncReport { 160 anchor_did: anchor.did.clone(), 161 entity_name: anchor.name.clone(), 162 radicle_key_detected: rad_key, 163 ssh_key_detected: ssh_key, 164 in_sync, 165 rotated, 166 message, 167 }; 168 169 Ok((anchor, report)) 170 } 171 172 #[cfg(test)] 173 mod tests { 174 use super::*; 175 176 #[test] 177 fn test_sha256_fingerprint_deterministic() { 178 let sample = "ssh-`ed25519` AAAAC3NzaC1lZDI1NTE5AAAAIExamplePublicKey user@host"; 179 let fp1 = get_sha256_fingerprint(sample); 180 let fp2 = get_sha256_fingerprint(sample); 181 assert_eq!(fp1, fp2); 182 assert_eq!(fp1.len(), 64); 183 } 184 185 #[test] 186 fn test_sync_report_serialization() { 187 let report = SyncReport { 188 anchor_did: "did:rad:aien:spark-01".to_string(), 189 entity_name: "AIEN".to_string(), 190 radicle_key_detected: Some("ssh-`ed25519` AAAAC...".to_string()), 191 ssh_key_detected: Some("ssh-`ed25519` AAAAC...".to_string()), 192 in_sync: true, 193 rotated: false, 194 message: "All keys synchronized.".to_string(), 195 }; 196 let json = serde_json::to_string(&report).expect("serializable"); 197 assert!(json.contains("did:rad:aien:spark-01")); 198 assert!(json.contains("All keys synchronized.")); 199 } 200 201 #[test] 202 fn test_validate_public_key_format() { 203 assert!(validate_public_key_format( 204 "ssh-`ed25519`", 205 "ssh-`ed25519` AAAAC3NzaC1lZDI1NTE5AAAAIExampleValidPayloadKey user@host" 206 ) 207 .is_ok()); 208 assert!(validate_public_key_format("ssh-`ed25519`", "rsa-bad AAAAC3").is_err()); 209 assert!(validate_public_key_format("ssh-`ed25519`", "ssh-`ed25519` short").is_err()); 210 assert!( 211 validate_public_key_format("radicle-`ed25519`", "rad:z6MkuTf9Vd6F4sM6eS2y5fV9tF3p") 212 .is_ok() 213 ); 214 assert!(validate_public_key_format("radicle-`ed25519`", "").is_err()); 215 } 216 }