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