| 1 | |
|
| 2 | |
|
| 3 | | use super::*; |
| 4 | |
|
| 5 | |
|
| 6 | | #[derive(Clone, Debug, PartialEq, Eq)] |
| 7 | | pub enum SignatureStatus { |
| 8 | | Unsigned, |
| 9 | | |
| 10 | | |
| 11 | | Valid { |
| 12 | | key: KeyId, |
| 13 | | name: String, |
| 14 | | email: String, |
| 15 | | sealed: bool, |
| 16 | | revoked: bool, |
| 17 | | }, |
| 18 | | Invalid(String), |
| 19 | | } |
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | 1× | pub fn create_user_identity( |
| 28 | 1× | name: Option<&str>, |
| 29 | 1× | email: Option<&str>, |
| 30 | 1× | expires: Option<u64>, |
| 31 | 1× | ) -> Result<(KeyId, Identity)> { |
| 32 | 1× | let user = default_user_config(); |
| 33 | 1× | let defaults = Config::load(Some(&user), Path::new(""))?; |
| 34 | 1× | let name = name.map(str::to_string).unwrap_or(defaults.author_name); |
| 35 | 1× | let email = email.map(str::to_string).unwrap_or(defaults.author_email); |
| 36 | 1× | let sk = SecretKey::generate(); |
| 37 | 1× | let mut id = sk.identity(&name, &email, Hlc::now().wall_ms); |
| 38 | 1× | id.expires = expires; |
| 39 | 1× | let key = id.hash(); |
| 40 | 1× | let keys = default_keys_dir(); |
| 41 | 1× | fs::create_dir_all(&keys)?; |
| 42 | 1× | crate::durable::write_atomic_private( |
| 43 | 1× | &keys.join(format!("{}.secret", key.to_hex())), |
| 44 | 1× | &sk.encode(&key), |
| 45 | 0× | )?; |
| 46 | 1× | write_atomic( |
| 47 | 1× | &keys.join(format!("{}.identity", key.to_hex())), |
| 48 | 1× | &id.encode(), |
| 49 | 0× | )?; |
| 50 | 1× | Config::set_user(&user, "author.name", &id.name)?; |
| 51 | 1× | Config::set_user(&user, "author.email", &id.email)?; |
| 52 | 1× | Config::set_user(&user, "author.key", &key.to_hex())?; |
| 53 | 1× | Ok((key, id)) |
| 54 | 1× | } |
| 55 | |
|
| 56 | |
|
| 57 | | #[derive(Clone, Debug)] |
| 58 | | pub struct Rotation { |
| 59 | | pub old: KeyId, |
| 60 | | pub new: KeyId, |
| 61 | | pub identity: Identity, |
| 62 | | |
| 63 | | |
| 64 | | pub policy: Option<PolicyUpdate>, |
| 65 | | } |
| 66 | |
|
| 67 | | impl Repo { |
| 68 | | |
| 69 | |
|
| 70 | | |
| 71 | | |
| 72 | 72× | pub fn create_identity( |
| 73 | 72× | &mut self, |
| 74 | 72× | name: &str, |
| 75 | 72× | email: &str, |
| 76 | 72× | scope: IdentityScope, |
| 77 | 72× | ) -> Result<(KeyId, Identity)> { |
| 78 | 72× | self.create_identity_with(name, email, scope, None) |
| 79 | 72× | } |
| 80 | |
|
| 81 | | |
| 82 | 75× | pub fn create_identity_with( |
| 83 | 75× | &mut self, |
| 84 | 75× | name: &str, |
| 85 | 75× | email: &str, |
| 86 | 75× | scope: IdentityScope, |
| 87 | 75× | expires: Option<u64>, |
| 88 | 75× | ) -> Result<(KeyId, Identity)> { |
| 89 | 75× | let sk = SecretKey::generate(); |
| 90 | 75× | let mut id = sk.identity(name, email, Hlc::now().wall_ms); |
| 91 | 75× | id.expires = expires; |
| 92 | 75× | let key = self.install_key(&id, &sk)?; |
| 93 | 75× | self.set_author(&id, &key, scope)?; |
| 94 | 75× | Ok((key, id)) |
| 95 | 75× | } |
| 96 | |
|
| 97 | | |
| 98 | | |
| 99 | 93× | fn install_key(&self, id: &Identity, sk: &SecretKey) -> Result<KeyId> { |
| 100 | 93× | let key = self.store.put_identity(id)?; |
| 101 | 93× | fs::create_dir_all(&self.keys_dir)?; |
| 102 | 93× | let path = self.keys_dir.join(format!("{}.secret", key.to_hex())); |
| 103 | 93× | crate::durable::write_atomic_private(&path, &sk.encode(&key))?; |
| 104 | 93× | write_atomic( |
| 105 | 93× | &self.keys_dir.join(format!("{}.identity", key.to_hex())), |
| 106 | 93× | &id.encode(), |
| 107 | 0× | )?; |
| 108 | 93× | Ok(key) |
| 109 | 93× | } |
| 110 | |
|
| 111 | 94× | fn set_author(&mut self, id: &Identity, key: &KeyId, scope: IdentityScope) -> Result<()> { |
| 112 | 94× | match scope { |
| 113 | | IdentityScope::Repo => { |
| 114 | 37× | self.config.set("author.name", &id.name)?; |
| 115 | 37× | self.config.set("author.email", &id.email)?; |
| 116 | 37× | self.config.set("author.key", &key.to_hex())?; |
| 117 | 37× | self.save_config()?; |
| 118 | | } |
| 119 | | IdentityScope::User => { |
| 120 | 57× | Config::set_user(&self.user_config, "author.name", &id.name)?; |
| 121 | 57× | Config::set_user(&self.user_config, "author.email", &id.email)?; |
| 122 | 57× | Config::set_user(&self.user_config, "author.key", &key.to_hex())?; |
| 123 | 57× | self.config = Config::load(Some(&self.user_config), &self.oxy.join("config"))?; |
| 124 | | } |
| 125 | | } |
| 126 | 94× | Ok(()) |
| 127 | 94× | } |
| 128 | |
|
| 129 | | |
| 130 | 0× | fn author_scope(&self) -> IdentityScope { |
| 131 | 0× | if self.config.repo_layer().contains_key("author.key") { |
| 132 | 0× | IdentityScope::Repo |
| 133 | | } else { |
| 134 | 0× | IdentityScope::User |
| 135 | | } |
| 136 | 0× | } |
| 137 | |
|
| 138 | | |
| 139 | | |
| 140 | | |
| 141 | | |
| 142 | | |
| 143 | 18× | fn record_successor(&mut self, id: &Identity, old: &KeyId, new_key: &KeyId) -> Result<()> { |
| 144 | 36× | let names = |v: Option<&String>| v.and_then(|k| KeyId::from_hex(k)) == Some(*old); |
| 145 | 18× | let repo_pins = names(self.config.repo_layer().get("author.key")); |
| 146 | 18× | let user_entries = super::config::read_config_file(&self.user_config).unwrap_or_default(); |
| 147 | 18× | let user_names = names(user_entries.get("author.key")); |
| 148 | 18× | if !repo_pins && !user_names { |
| 149 | 0× | return self.set_author(id, new_key, self.author_scope()); |
| 150 | 18× | } |
| 151 | 18× | if user_names { |
| 152 | 7× | self.set_author(id, new_key, IdentityScope::User)?; |
| 153 | 11× | } |
| 154 | 18× | if repo_pins { |
| 155 | 12× | self.set_author(id, new_key, IdentityScope::Repo)?; |
| 156 | 6× | } |
| 157 | 18× | Ok(()) |
| 158 | 18× | } |
| 159 | |
|
| 160 | | |
| 161 | | |
| 162 | | |
| 163 | 18× | fn replace_identity( |
| 164 | 18× | &mut self, |
| 165 | 18× | new: Identity, |
| 166 | 18× | sk: SecretKey, |
| 167 | 18× | command: &str, |
| 168 | 18× | ) -> Result<Rotation> { |
| 169 | 18× | let Some((old, old_sk)) = self.secret_key()? else { |
| 170 | 0× | return fail("no secret key: run `oxy identity create` first"); |
| 171 | | }; |
| 172 | 18× | if new.previous != Some(old) { |
| 173 | 0× | return fail("the successor identity must name the current key as previous"); |
| 174 | 18× | } |
| 175 | 18× | let new_key = self.install_key(&new, &sk)?; |
| 176 | 18× | let attest = Attest { |
| 177 | 18× | repo: self.id()?, |
| 178 | 18× | hlc: Hlc::now(), |
| 179 | 18× | body: AttestBody::Rotate { new: new_key }, |
| 180 | | }; |
| 181 | 18× | let r = self.store.put_attest(&attest)?; |
| 182 | 18× | self.store |
| 183 | 18× | .put_signature(&r, &old_sk.sign(Domain::Attest, &old, &r))?; |
| 184 | 18× | let policy = match self.current_policy()? { |
| 185 | 9× | Some((_, sp)) if sp.policy.mentions(&old) => { |
| 186 | 9× | let mut p = self.next_policy()?; |
| 187 | 9× | p.substitute_key(&old, new.clone()); |
| 188 | 9× | Some(self.sign_policy_with(p, &[(old, &old_sk), (new_key, &sk)], command)?) |
| 189 | | } |
| 190 | 9× | _ => None, |
| 191 | | }; |
| 192 | 18× | self.record_successor(&new, &old, &new_key)?; |
| 193 | 18× | Ok(Rotation { |
| 194 | 18× | old, |
| 195 | 18× | new: new_key, |
| 196 | 18× | identity: new, |
| 197 | 18× | policy, |
| 198 | 18× | }) |
| 199 | 18× | } |
| 200 | |
|
| 201 | | |
| 202 | | |
| 203 | 3× | pub fn rotate_identity(&mut self) -> Result<Rotation> { |
| 204 | 3× | let Some((old, _)) = self.secret_key()? else { |
| 205 | 0× | return fail("no secret key: run `oxy identity create` first"); |
| 206 | | }; |
| 207 | 3× | let cur = self.identity(&old)?; |
| 208 | 3× | let sk = SecretKey::generate(); |
| 209 | 3× | let mut id = sk.identity(&cur.name, &cur.email, Hlc::now().wall_ms); |
| 210 | 3× | id.previous = Some(old); |
| 211 | 3× | id.delegates = cur.delegates.clone(); |
| 212 | 3× | id.expires = cur.expires; |
| 213 | 3× | self.replace_identity(id, sk, "oxy identity rotate") |
| 214 | 3× | } |
| 215 | |
|
| 216 | | |
| 217 | 2× | pub fn renew_identity(&mut self, expires: Option<u64>) -> Result<Rotation> { |
| 218 | 2× | let Some((old, sk)) = self.secret_key()? else { |
| 219 | 0× | return fail("no secret key: run `oxy identity create` first"); |
| 220 | | }; |
| 221 | 2× | let cur = self.identity(&old)?; |
| 222 | 2× | let id = cur.successor(cur.delegates.clone(), expires, Hlc::now().wall_ms); |
| 223 | 2× | self.replace_identity(id, sk, "oxy identity renew") |
| 224 | 2× | } |
| 225 | |
|
| 226 | | |
| 227 | | |
| 228 | | |
| 229 | 12× | pub fn delegate_add( |
| 230 | 12× | &mut self, |
| 231 | 12× | delegate: Identity, |
| 232 | 12× | scope: Scope, |
| 233 | 12× | expires: Option<u64>, |
| 234 | 12× | ) -> Result<Rotation> { |
| 235 | 12× | self.delegate_add_many(vec![delegate], scope, expires) |
| 236 | 12× | } |
| 237 | |
|
| 238 | | |
| 239 | 13× | pub fn delegate_add_many( |
| 240 | 13× | &mut self, |
| 241 | 13× | new_delegates: Vec<Identity>, |
| 242 | 13× | scope: Scope, |
| 243 | 13× | expires: Option<u64>, |
| 244 | 13× | ) -> Result<Rotation> { |
| 245 | 13× | let Some((old, sk)) = self.secret_key()? else { |
| 246 | 0× | return fail("no secret key: run `oxy identity create` first"); |
| 247 | | }; |
| 248 | 13× | if scope.is_empty() { |
| 249 | 0× | return fail("a delegate needs at least one scope: changes, refs or seal"); |
| 250 | 13× | } |
| 251 | 13× | if new_delegates.is_empty() { |
| 252 | 0× | return fail("name at least one identity file"); |
| 253 | 13× | } |
| 254 | 13× | let cur = self.identity(&old)?; |
| 255 | 13× | let mut delegates = cur.delegates.clone(); |
| 256 | 13× | let mut added = Vec::new(); |
| 257 | 14× | for delegate in new_delegates { |
| 258 | 14× | let dk = self.store.put_identity(&delegate)?; |
| 259 | 14× | added.push(dk); |
| 260 | 14× | if dk == old { |
| 261 | 0× | return fail("an identity cannot delegate to itself"); |
| 262 | 14× | } |
| 263 | 14× | if cur.delegate(&dk).is_some() |
| 264 | 12× | || delegates |
| 265 | 12× | .iter() |
| 266 | 12× | .skip(cur.delegates.len()) |
| 267 | 12× | .any(|d| d.key == dk) |
| 268 | | { |
| 269 | 2× | return fail(format!( |
| 270 | | "{} is already a delegate; remove it first to change its scope or expiry", |
| 271 | 2× | dk.short(12) |
| 272 | | )); |
| 273 | 12× | } |
| 274 | 12× | delegates.push(Delegate { |
| 275 | 12× | key: dk, |
| 276 | 12× | scope, |
| 277 | 12× | expires, |
| 278 | 12× | }); |
| 279 | | } |
| 280 | 11× | let id = cur.successor(delegates, cur.expires, Hlc::now().wall_ms); |
| 281 | 11× | self.replace_identity( |
| 282 | 11× | id, |
| 283 | 11× | sk, |
| 284 | 11× | &format!( |
| 285 | | "oxy identity delegate add {} --scope {scope}", |
| 286 | 11× | added |
| 287 | 11× | .iter() |
| 288 | 12× | .map(|k| k.short(12)) |
| 289 | 11× | .collect::<Vec<_>>() |
| 290 | 11× | .join(" ") |
| 291 | | ), |
| 292 | | ) |
| 293 | 13× | } |
| 294 | |
|
| 295 | | |
| 296 | 3× | pub fn delegate_remove(&mut self, delegate: &KeyId) -> Result<Rotation> { |
| 297 | 3× | let Some((old, sk)) = self.secret_key()? else { |
| 298 | 0× | return fail("no secret key: run `oxy identity create` first"); |
| 299 | | }; |
| 300 | 3× | let cur = self.identity(&old)?; |
| 301 | 3× | if cur.delegate(delegate).is_none() { |
| 302 | 1× | return fail(format!("{} is not a delegate", delegate.short(12))); |
| 303 | 2× | } |
| 304 | 2× | let delegates = cur |
| 305 | 2× | .delegates |
| 306 | 2× | .iter() |
| 307 | 2× | .copied() |
| 308 | 2× | .filter(|d| d.key != *delegate) |
| 309 | 2× | .collect(); |
| 310 | 2× | let id = cur.successor(delegates, cur.expires, Hlc::now().wall_ms); |
| 311 | 2× | self.replace_identity( |
| 312 | 2× | id, |
| 313 | 2× | sk, |
| 314 | 2× | &format!("oxy identity delegate remove {}", delegate.short(12)), |
| 315 | | ) |
| 316 | 3× | } |
| 317 | |
|
| 318 | | |
| 319 | | |
| 320 | | |
| 321 | | |
| 322 | | |
| 323 | 5× | pub fn revoke_identity(&self, key: &KeyId, reason: &str) -> Result<Option<PolicyUpdate>> { |
| 324 | 5× | let Some((own, sk)) = self.secret_key()? else { |
| 325 | 0× | return fail("no secret key: run `oxy identity create` first"); |
| 326 | | }; |
| 327 | 5× | let policy = self.current_policy()?.map(|(_, sp)| sp.policy); |
| 328 | 5× | let is_root = policy.as_ref().is_some_and(|p| p.roots.contains(&own)); |
| 329 | 5× | let is_delegate = self.identity(&own)?.delegate(key).is_some(); |
| 330 | 5× | if *key != own && !is_root && !is_delegate { |
| 331 | 1× | return fail("only the key's holder, the identity that delegated to it, or a policy root can revoke it"); |
| 332 | 4× | } |
| 333 | 4× | let attest = Attest { |
| 334 | 4× | repo: self.id()?, |
| 335 | 4× | hlc: Hlc::now(), |
| 336 | 4× | body: AttestBody::Revoke { |
| 337 | 4× | key: *key, |
| 338 | 4× | reason: reason.into(), |
| 339 | 4× | }, |
| 340 | | }; |
| 341 | 4× | let r = self.store.put_attest(&attest)?; |
| 342 | 4× | self.store |
| 343 | 4× | .put_signature(&r, &sk.sign(Domain::Attest, &own, &r))?; |
| 344 | 4× | let dir = self.oxy.join("revoked"); |
| 345 | 4× | fs::create_dir_all(&dir)?; |
| 346 | 4× | write_atomic(&dir.join(key.to_hex()), r.to_hex().as_bytes())?; |
| 347 | 1× | match policy { |
| 348 | 1× | Some(p) if p.mentions(key) => Ok(Some(self.policy_revoke(key)?)), |
| 349 | 3× | _ => Ok(None), |
| 350 | | } |
| 351 | 5× | } |
| 352 | |
|
| 353 | | |
| 354 | 262× | pub fn is_revoked(&self, key: &KeyId) -> Result<bool> { |
| 355 | 262× | if self.oxy.join("revoked").join(key.to_hex()).exists() { |
| 356 | 5× | return Ok(true); |
| 357 | 257× | } |
| 358 | 257× | Ok(self |
| 359 | 257× | .current_policy()? |
| 360 | 257× | .is_some_and(|(_, sp)| sp.policy.is_revoked(key))) |
| 361 | 262× | } |
| 362 | |
|
| 363 | | |
| 364 | | |
| 365 | 1023× | pub fn signing_key(&self) -> Option<KeyId> { |
| 366 | 1023× | if self.config.signer_keyless { |
| 367 | | |
| 368 | 1× | return self.keyless_key.borrow().as_ref().map(|(k, _, _)| *k); |
| 369 | 1022× | } |
| 370 | 1022× | self.config.signer_key.or(self.config.author_key) |
| 371 | 1023× | } |
| 372 | |
|
| 373 | | |
| 374 | | |
| 375 | | |
| 376 | | |
| 377 | 800× | pub fn secret_key(&self) -> Result<Option<(KeyId, SecretKey)>> { |
| 378 | 800× | if self.config.signer_keyless { |
| 379 | 1× | return self.keyless_secret().map(Some); |
| 380 | 799× | } |
| 381 | 799× | let Some(key) = self.signing_key() else { |
| 382 | 224× | return Ok(None); |
| 383 | | }; |
| 384 | 575× | let path = self.keys_dir.join(format!("{}.secret", key.to_hex())); |
| 385 | 575× | match fs::read(&path) { |
| 386 | 574× | Ok(b) => { |
| 387 | 574× | let (k, sk) = SecretKey::decode(&b)?; |
| 388 | 574× | if k != key { |
| 389 | 0× | return fail(format!( |
| 390 | | "{} holds a different key than its name", |
| 391 | 0× | path.display() |
| 392 | | )); |
| 393 | 574× | } |
| 394 | 574× | if !self.store.has(&key) { |
| 395 | 2× | let id_path = self.keys_dir.join(format!("{}.identity", key.to_hex())); |
| 396 | 2× | if let Ok(bytes) = fs::read(&id_path) { |
| 397 | 2× | let id = Identity::decode(&bytes)?; |
| 398 | 2× | if id.hash() == key { |
| 399 | 2× | self.store.put_identity(&id)?; |
| 400 | 0× | } |
| 401 | 0× | } |
| 402 | 572× | } |
| 403 | 574× | Ok(Some((k, sk))) |
| 404 | | } |
| 405 | 1× | Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(None), |
| 406 | 0× | Err(e) => Err(e.into()), |
| 407 | | } |
| 408 | 800× | } |
| 409 | |
|
| 410 | | |
| 411 | | |
| 412 | | |
| 413 | 332× | pub fn identity(&self, key: &KeyId) -> Result<Identity> { |
| 414 | 332× | match self.store.get_identity(key) { |
| 415 | 328× | Ok(id) => Ok(id), |
| 416 | 4× | Err(e) => { |
| 417 | 4× | let path = self.keys_dir.join(format!("{}.identity", key.to_hex())); |
| 418 | 4× | match fs::read(&path) { |
| 419 | 2× | Ok(bytes) => { |
| 420 | 2× | let id = Identity::decode(&bytes)?; |
| 421 | 2× | if id.hash() != *key { |
| 422 | 1× | return fail(format!( |
| 423 | | "{} does not hold identity {}", |
| 424 | 1× | path.display(), |
| 425 | 1× | key.short(12) |
| 426 | | )); |
| 427 | 1× | } |
| 428 | 1× | Ok(id) |
| 429 | | } |
| 430 | | |
| 431 | | |
| 432 | | |
| 433 | 2× | Err(_) if self.config.author_key == Some(*key) => fail(format!( |
| 434 | | "this repository has no identity {} (author.key, set in {}): it is not in the repository, nor in {}.\n\ |
| 435 | | An agent home names its human's identity, and each delegation rotates that identity, so the key goes stale. \ |
| 436 | | Pin the identity this repository uses, as its owner sees it: `oxy identity show` in the owner's shell gives the key, \ |
| 437 | | then `oxy config author.key <KEY>` here; or ask the owner.", |
| 438 | 2× | key.short(12), |
| 439 | 2× | if self.config.is_repo_set("author.key") { |
| 440 | 1× | "this repository's config" |
| 441 | | } else { |
| 442 | 1× | "the user config" |
| 443 | | }, |
| 444 | 2× | self.keys_dir.display() |
| 445 | | )), |
| 446 | 0× | Err(_) => Err(e.into()), |
| 447 | | } |
| 448 | | } |
| 449 | | } |
| 450 | 332× | } |
| 451 | |
|
| 452 | | |
| 453 | 46× | pub fn sign_change(&self, hash: &ChangeHash) -> Result<Signature> { |
| 454 | 46× | let Some((key, sk)) = self.secret_key()? else { |
| 455 | 0× | return fail("no secret key: run `oxy identity create` first"); |
| 456 | | }; |
| 457 | 46× | let sig = sk.sign(Domain::Change, &key, hash); |
| 458 | 46× | self.store.put_signature(hash, &sig)?; |
| 459 | 46× | Ok(sig) |
| 460 | 46× | } |
| 461 | |
|
| 462 | | |
| 463 | 24× | pub fn verify_change(&self, hash: &ChangeHash) -> Result<SignatureStatus> { |
| 464 | 24× | self.verify_change_known(hash, None) |
| 465 | 24× | } |
| 466 | |
|
| 467 | | |
| 468 | | |
| 469 | | |
| 470 | 445× | fn verify_change_known( |
| 471 | 445× | &self, |
| 472 | 445× | hash: &ChangeHash, |
| 473 | 445× | known: Option<&crate::state::ChangeMeta>, |
| 474 | 445× | ) -> Result<SignatureStatus> { |
| 475 | 445× | let sigs = self.store.signatures(hash)?; |
| 476 | 445× | if sigs.is_empty() { |
| 477 | 350× | return Ok(SignatureStatus::Unsigned); |
| 478 | 95× | } |
| 479 | | let read; |
| 480 | 95× | let change = match known { |
| 481 | 17× | Some(m) => m, |
| 482 | | None => { |
| 483 | 78× | read = self.store.get_change_meta(hash)?; |
| 484 | 78× | &read |
| 485 | | } |
| 486 | | }; |
| 487 | 95× | let mut last_err = String::new(); |
| 488 | 95× | for sig in &sigs { |
| 489 | 95× | match self.check_signer(change, sig, Domain::Change, hash) { |
| 490 | 89× | Ok(id) => { |
| 491 | | return Ok(SignatureStatus::Valid { |
| 492 | 89× | key: sig.key, |
| 493 | 89× | name: self.certified_subject(&sig.key).unwrap_or(id.name), |
| 494 | 89× | email: id.email, |
| 495 | | sealed: false, |
| 496 | 89× | revoked: self.is_revoked(&sig.key)?, |
| 497 | | }) |
| 498 | | } |
| 499 | 6× | Err(e) => last_err = e, |
| 500 | | } |
| 501 | | } |
| 502 | 6× | Ok(SignatureStatus::Invalid(last_err)) |
| 503 | 445× | } |
| 504 | |
|
| 505 | | |
| 506 | | |
| 507 | | |
| 508 | | |
| 509 | | |
| 510 | | |
| 511 | | |
| 512 | 14× | fn granted( |
| 513 | 14× | &self, |
| 514 | 14× | author: &KeyId, |
| 515 | 14× | signer: &KeyId, |
| 516 | 14× | needed: Scope, |
| 517 | 14× | at_ms: u64, |
| 518 | 14× | ) -> std::result::Result<Option<String>, String> { |
| 519 | 14× | let neither = || { |
| 520 | 7× | Some(format!( |
| 521 | 7× | "key {} is neither the author nor a delegate", |
| 522 | 7× | signer.short(12) |
| 523 | 7× | )) |
| 524 | 7× | }; |
| 525 | 14× | let Ok(Some(c)) = self.store.certificate_of(signer) else { |
| 526 | 4× | return Ok(neither()); |
| 527 | | }; |
| 528 | 10× | let Ok(sc) = self.store.get_certificate(&c) else { |
| 529 | 0× | return Ok(neither()); |
| 530 | | }; |
| 531 | 10× | let Some(g) = &sc.cert.grant else { |
| 532 | 1× | return Ok(neither()); |
| 533 | | }; |
| 534 | 9× | let who = signer.short(12); |
| 535 | 9× | let issuer = self |
| 536 | 9× | .store |
| 537 | 9× | .get_identity(&sc.cert.issuer) |
| 538 | 9× | .map_err(|_| format!("key {who}'s certificate names an unknown issuer"))?; |
| 539 | 9× | sc.verify(&issuer) |
| 540 | 9× | .map_err(|e| format!("key {who}'s certificate: {e}"))?; |
| 541 | | |
| 542 | | |
| 543 | 9× | if g.grant.grantor != *author && !self.granted_before_rotation(author, &g.grant) { |
| 544 | 2× | return Ok(neither()); |
| 545 | 7× | } |
| 546 | 7× | let grantor = self |
| 547 | 7× | .identity(&g.grant.grantor) |
| 548 | 7× | .map_err(|_| format!("key {who}'s grant is by an unknown author"))?; |
| 549 | 7× | g.verify(&grantor) |
| 550 | 7× | .map_err(|e| format!("key {who}'s grant: {e}"))?; |
| 551 | 7× | if g.grant.subject != sc.cert.subject || g.grant.issuer != sc.cert.issuer { |
| 552 | 0× | return Err(format!( |
| 553 | 0× | "key {who}'s grant is for {} from issuer {}, not its certificate's", |
| 554 | 0× | g.grant.subject, |
| 555 | 0× | g.grant.issuer.short(12) |
| 556 | 0× | )); |
| 557 | 7× | } |
| 558 | 7× | if !sc.cert.scope.has(needed) || !g.grant.scope.has(needed) { |
| 559 | 0× | return Err(format!("key {who} is not granted {needed}")); |
| 560 | 7× | } |
| 561 | 7× | if !g.grant.covers(needed, at_ms) { |
| 562 | 2× | return Err(format!( |
| 563 | 2× | "key {who}'s grant did not cover the time the change was made" |
| 564 | 2× | )); |
| 565 | 5× | } |
| 566 | 5× | Ok(None) |
| 567 | 14× | } |
| 568 | |
|
| 569 | | |
| 570 | | |
| 571 | | |
| 572 | | |
| 573 | | |
| 574 | | |
| 575 | 3× | fn granted_before_rotation(&self, author: &KeyId, grant: &crate::cert::Grant) -> bool { |
| 576 | 3× | let mut cur = self.identity(author).ok(); |
| 577 | 3× | for _ in 0..32 { |
| 578 | 3× | let Some(id) = cur else { |
| 579 | 0× | return false; |
| 580 | | }; |
| 581 | 2× | match id.previous { |
| 582 | 2× | Some(prev) if prev == grant.grantor => return grant.not_before < id.created, |
| 583 | 0× | Some(prev) => cur = self.identity(&prev).ok(), |
| 584 | 1× | None => return false, |
| 585 | | } |
| 586 | | } |
| 587 | 0× | false |
| 588 | 3× | } |
| 589 | |
|
| 590 | | |
| 591 | | |
| 592 | | |
| 593 | 247× | pub fn certified_subject(&self, key: &KeyId) -> Option<String> { |
| 594 | 247× | let c = self.store.certificate_of(key).ok()??; |
| 595 | 9× | Some(self.store.get_certificate(&c).ok()?.cert.subject) |
| 596 | 247× | } |
| 597 | |
|
| 598 | | |
| 599 | 260× | fn check_signer( |
| 600 | 260× | &self, |
| 601 | 260× | change: &crate::state::ChangeMeta, |
| 602 | 260× | sig: &Signature, |
| 603 | 260× | domain: Domain, |
| 604 | 260× | target: &ObjectRef, |
| 605 | 260× | ) -> std::result::Result<Identity, String> { |
| 606 | 260× | let id = self |
| 607 | 260× | .store |
| 608 | 260× | .get_identity(&sig.key) |
| 609 | 260× | .map_err(|_| format!("unknown key {}", sig.key.short(12)))?; |
| 610 | | |
| 611 | | |
| 612 | | |
| 613 | | |
| 614 | | |
| 615 | | |
| 616 | 260× | if !change.author.is_zero() && sig.key != change.author { |
| 617 | 34× | let needed = match domain { |
| 618 | 22× | Domain::Change => Scope::CHANGES, |
| 619 | 12× | _ => Scope::SEAL, |
| 620 | | }; |
| 621 | 34× | let delegation = self |
| 622 | 34× | .store |
| 623 | 34× | .get_identity(&change.author) |
| 624 | 34× | .ok() |
| 625 | 34× | .and_then(|a| a.delegate(&sig.key).copied()); |
| 626 | 19× | match delegation { |
| 627 | | |
| 628 | | None => { |
| 629 | 7× | if let Some(why) = |
| 630 | 14× | self.granted(&change.author, &sig.key, needed, change.hlc.wall_ms)? |
| 631 | | { |
| 632 | 7× | return Err(why); |
| 633 | 5× | } |
| 634 | | } |
| 635 | 20× | Some(d) if !d.scope.has(needed) => { |
| 636 | 1× | return Err(format!( |
| 637 | | "key {} is a delegate for {} only and may not {}", |
| 638 | 1× | sig.key.short(12), |
| 639 | | d.scope, |
| 640 | 1× | if needed == Scope::SEAL { |
| 641 | 1× | "seal" |
| 642 | | } else { |
| 643 | 0× | "sign changes" |
| 644 | | } |
| 645 | | )) |
| 646 | | } |
| 647 | 19× | Some(d) if !d.allows(needed, change.hlc.wall_ms) => { |
| 648 | 1× | return Err(format!( |
| 649 | 1× | "key {}'s delegation had expired when the change was made", |
| 650 | 1× | sig.key.short(12) |
| 651 | 1× | )) |
| 652 | | } |
| 653 | 18× | Some(_) => {} |
| 654 | | } |
| 655 | 226× | } |
| 656 | 249× | if id.is_expired_at(change.hlc.wall_ms) { |
| 657 | 1× | return Err(format!( |
| 658 | 1× | "key {} had expired when the change was made", |
| 659 | 1× | sig.key.short(12) |
| 660 | 1× | )); |
| 661 | 248× | } |
| 662 | 248× | verify_signature(&id, sig, domain, target).map_err(|e| e.to_string())?; |
| 663 | 247× | Ok(id) |
| 664 | 260× | } |
| 665 | |
|
| 666 | | |
| 667 | | |
| 668 | 343× | pub fn authenticate_change(&self, hash: &ChangeHash) -> Result<SignatureStatus> { |
| 669 | 343× | self.authenticate_change_known(hash, None) |
| 670 | 343× | } |
| 671 | |
|
| 672 | | |
| 673 | | |
| 674 | 421× | pub fn authenticate_change_known( |
| 675 | 421× | &self, |
| 676 | 421× | hash: &ChangeHash, |
| 677 | 421× | known: Option<&crate::state::ChangeMeta>, |
| 678 | 421× | ) -> Result<SignatureStatus> { |
| 679 | 421× | let direct = self.verify_change_known(hash, known)?; |
| 680 | 421× | if matches!(direct, SignatureStatus::Valid { .. }) { |
| 681 | 74× | return Ok(direct); |
| 682 | 347× | } |
| 683 | 347× | let mut last_err = match &direct { |
| 684 | 2× | SignatureStatus::Invalid(e) => e.clone(), |
| 685 | 345× | _ => String::new(), |
| 686 | | }; |
| 687 | 347× | let seals = self.store.seals_for(hash)?; |
| 688 | 347× | if seals.is_empty() { |
| 689 | 182× | return Ok(match last_err.is_empty() { |
| 690 | 180× | true => SignatureStatus::Unsigned, |
| 691 | 2× | false => SignatureStatus::Invalid(last_err), |
| 692 | | }); |
| 693 | 165× | } |
| 694 | | let read; |
| 695 | 165× | let change = match known { |
| 696 | 0× | Some(m) => m, |
| 697 | | None => { |
| 698 | 165× | read = self.store.get_change_meta(hash)?; |
| 699 | 165× | &read |
| 700 | | } |
| 701 | | }; |
| 702 | 165× | for seal_ref in seals { |
| 703 | 165× | let attest = self.store.get_attest(&seal_ref)?; |
| 704 | 165× | let AttestBody::Seal { hashes } = &attest.body else { |
| 705 | 0× | continue; |
| 706 | | }; |
| 707 | 165× | if !hashes.contains(hash) { |
| 708 | 0× | continue; |
| 709 | 165× | } |
| 710 | 165× | for sig in self.store.signatures(&seal_ref)? { |
| 711 | 165× | match self.check_signer(change, &sig, Domain::Attest, &seal_ref) { |
| 712 | 158× | Ok(id) => { |
| 713 | | return Ok(SignatureStatus::Valid { |
| 714 | 158× | key: sig.key, |
| 715 | 158× | name: self.certified_subject(&sig.key).unwrap_or(id.name), |
| 716 | 158× | email: id.email, |
| 717 | | sealed: true, |
| 718 | 158× | revoked: self.is_revoked(&sig.key)?, |
| 719 | | }) |
| 720 | | } |
| 721 | 7× | Err(e) => last_err = e, |
| 722 | | } |
| 723 | | } |
| 724 | | } |
| 725 | 7× | if last_err.is_empty() { |
| 726 | 0× | Ok(SignatureStatus::Unsigned) |
| 727 | | } else { |
| 728 | 7× | Ok(SignatureStatus::Invalid(last_err)) |
| 729 | | } |
| 730 | 421× | } |
| 731 | |
|
| 732 | | |
| 733 | | |
| 734 | | |
| 735 | | |
| 736 | 3× | pub fn seal(&self, hashes: &[ChangeHash]) -> Result<ObjectRef> { |
| 737 | 3× | if hashes.is_empty() { |
| 738 | 0× | return fail("nothing to seal"); |
| 739 | 3× | } |
| 740 | 3× | if hashes.len() > crate::attest::MAX_SEAL_HASHES { |
| 741 | 0× | return fail(format!( |
| 742 | | "a seal covers at most {} changes", |
| 743 | | crate::attest::MAX_SEAL_HASHES |
| 744 | | )); |
| 745 | 3× | } |
| 746 | 3× | for h in hashes { |
| 747 | 3× | if !self.store.has(h) { |
| 748 | 0× | return fail(format!("unknown change {}", h.short(12))); |
| 749 | 3× | } |
| 750 | | } |
| 751 | 3× | let mut sorted = hashes.to_vec(); |
| 752 | 3× | sorted.sort(); |
| 753 | 3× | sorted.dedup(); |
| 754 | 3× | let sa = self.sign_attest(&Attest::seal(self.id()?, sorted.clone()))?; |
| 755 | 3× | let r = self.store.put_attest(&sa.attest)?; |
| 756 | 3× | for sig in &sa.sigs { |
| 757 | 3× | self.store.put_signature(&r, sig)?; |
| 758 | | } |
| 759 | 3× | for h in &sorted { |
| 760 | 3× | self.store.index_seal(h, &r)?; |
| 761 | | } |
| 762 | 3× | Ok(r) |
| 763 | 3× | } |
| 764 | |
|
| 765 | | |
| 766 | | |
| 767 | | |
| 768 | | |
| 769 | 82× | pub(crate) fn seals_for_author(&self, author: &KeyId, signer: &KeyId) -> bool { |
| 770 | 82× | author == signer |
| 771 | 8× | || author.is_zero() |
| 772 | 7× | || ((self.config.signer_key.is_some() || self.config.signer_keyless) |
| 773 | 5× | && self.config.author_key == Some(*author)) |
| 774 | 82× | } |
| 775 | |
|
| 776 | | |
| 777 | | |
| 778 | | |
| 779 | 0× | pub fn sealable(&self) -> Result<Vec<ChangeHash>> { |
| 780 | 0× | let Some((key, _)) = self.secret_key()? else { |
| 781 | 0× | return fail("no identity: run oxy identity create"); |
| 782 | | }; |
| 783 | 0× | let heads = self.bookmark_heads()?; |
| 784 | 0× | let state = self.load_state_history(&heads)?; |
| 785 | 0× | let mut out = Vec::new(); |
| 786 | 0× | for (h, meta) in state.changes() { |
| 787 | 0× | if self.seals_for_author(&meta.author, &key) |
| 788 | 0× | && !matches!(self.authenticate_change(h)?, SignatureStatus::Valid { .. }) |
| 789 | 0× | { |
| 790 | 0× | out.push(*h); |
| 791 | 0× | } |
| 792 | | } |
| 793 | 0× | out.sort(); |
| 794 | 0× | Ok(out) |
| 795 | 0× | } |
| 796 | |
|
| 797 | 185× | pub(crate) fn sign_attest(&self, attest: &Attest) -> Result<SignedAttest> { |
| 798 | 185× | let Some((key, sk)) = self.secret_key()? else { |
| 799 | 0× | return fail("no secret key: run `oxy identity create` first"); |
| 800 | | }; |
| 801 | 185× | let r = self.store.put_attest(attest)?; |
| 802 | 183× | let sig = sk.sign(Domain::Attest, &key, &r); |
| 803 | 183× | self.store.put_signature(&r, &sig)?; |
| 804 | 183× | if let AttestBody::Seal { hashes } = &attest.body { |
| 805 | 55× | for h in hashes { |
| 806 | 55× | self.store.index_seal(h, &r)?; |
| 807 | | } |
| 808 | 131× | } |
| 809 | 183× | Ok(SignedAttest { |
| 810 | 183× | attest: attest.clone(), |
| 811 | 183× | sigs: vec![sig], |
| 812 | 183× | }) |
| 813 | 185× | } |
| 814 | | } |