1
0
mirror of https://github.com/aziascreations/BakaShrot.git synced 2026-11-23 20:58:46 +01:00

Made bakashrot-cert write a certificate and its key both or neither, Refused a clock before 1970 at start, Noted the replication limits in the example config

- bakashrot-cert ca and issue write nothing when either file of the
  pair is there already, and remove the certificate again when its key
  can't be written.
- The server refuses to start with a clock before 1970 (pushed_at 0);
  set back that far while running isn't checked, noted in
  known-issues.md.
- The example config says to keep sync_interval under 5 min (idle links
  are closed) and what a replicated attachment can be resumed past
  (read back and hashed in 30 s). Both left as they are, why in
  known-issues.md.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-16 11:02:08 +01:00
parent 39ba7fce6c
commit 764bc3652a
7 changed files with 104 additions and 21 deletions
@@ -99,8 +99,12 @@ fn run(command: Command) -> Result<(), String> {
match command {
Command::Ca { name, days, out } => {
let ca = Ca::create(&name, days).map_err(|e| e.to_string())?;
write_new(&out.join("ca.crt"), &ca.certificate, false)?;
write_new(&out.join("ca.key"), &ca.key, true)?;
write_pair(
&out.join("ca.crt"),
&ca.certificate,
&out.join("ca.key"),
&ca.key,
)?;
}
Command::Issue {
name,
@@ -118,8 +122,12 @@ fn run(command: Command) -> Result<(), String> {
.issue(&identity, usage, days)
.map_err(|e| e.to_string())?;
let file = file.unwrap_or(name);
write_new(&out.join(format!("{file}.crt")), &issued.certificate, false)?;
write_new(&out.join(format!("{file}.key")), &issued.key, true)?;
write_pair(
&out.join(format!("{file}.crt")),
&issued.certificate,
&out.join(format!("{file}.key")),
&issued.key,
)?;
}
Command::Revoke {
certificates,
@@ -159,8 +167,28 @@ fn write_new(path: &Path, text: &str, private: bool) -> Result<(), String> {
.open(path)
.map_err(|error| format!("{}: {error}", path.display()))?;
file.write_all(text.as_bytes())
.map_err(|error| format!("{}: {error}", path.display()))?;
println!("Wrote {}", path.display());
.map_err(|error| format!("{}: {error}", path.display()))
}
/// Writes a certificate and its key, both or neither: nothing is written
/// when either is there already, the certificate is removed again when
/// its key can't be written.
fn write_pair(certificate: &Path, pem: &str, key: &Path, key_pem: &str) -> Result<(), String> {
for path in [certificate, key] {
if path.exists() {
return Err(format!(
"{}: already there, nothing written",
path.display()
));
}
}
write_new(certificate, pem, false)?;
if let Err(error) = write_new(key, key_pem, true) {
let _ = fs::remove_file(certificate);
return Err(format!("{error}, nothing written"));
}
println!("Wrote {}", certificate.display());
println!("Wrote {}", key.display());
Ok(())
}
+14
View File
@@ -121,6 +121,20 @@ fn nothing_is_overwritten_or_mixed_up() {
assert!(!ok, "{text}");
assert_eq!(read(&folder, "ca.key"), key, "the CA's key is still there");
// Half of a pair there already: refused, the other half not written.
let half = folder.join("half");
fs::create_dir_all(&half).unwrap();
fs::write(half.join("ca.key"), "kept").unwrap();
let (ok, text) = cert(&half, &["ca", "--name", "Half"]);
assert!(!ok && text.contains("nothing written"), "{text}");
assert!(!half.join("ca.crt").exists());
assert_eq!(read(&half, "ca.key"), "kept");
fs::write(folder.join("porch.key"), "kept").unwrap();
let (ok, text) = cert(&folder, &["issue", "--name", "porch"]);
assert!(!ok && text.contains("nothing written"), "{text}");
assert!(!folder.join("porch.crt").exists());
assert_eq!(read(&folder, "porch.key"), "kept");
assert!(cert(&folder, &["issue", "--name", "kitchen"]).0);
assert!(
!cert(&folder, &["issue", "--name", "kitchen"]).0,