-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathkeysign.rs
More file actions
44 lines (37 loc) · 1.09 KB
/
keysign.rs
File metadata and controls
44 lines (37 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
use std::error::Error;
use clap::Parser;
use gpgme::{Context, Protocol};
#[derive(Debug, Parser)]
struct Cli {
#[arg(long)]
/// Use the CMS protocol
cms: bool,
#[arg(long)]
/// Key to use for signing. Default key is used otherwise
key: Option<String>,
/// Key to sign
keyid: String,
}
fn main() -> Result<(), Box<dyn Error>> {
let args = Cli::parse();
let proto = if args.cms {
Protocol::Cms
} else {
Protocol::OpenPgp
};
let mut ctx = Context::from_protocol(proto)?;
let key_to_sign = ctx
.get_key(&args.keyid)
.map_err(|e| format!("no key matched given key-id: {e:?}"))?;
if let Some(key) = args.key {
let key = ctx
.get_secret_key(key)
.map_err(|e| format!("unable to find signing key: {e:?}"))?;
ctx.add_signer(&key)
.map_err(|e| format!("add_signer() failed: {e:?}"))?;
}
ctx.sign_key(&key_to_sign, None::<String>, Default::default())
.map_err(|e| format!("signing failed: {e:?}"))?;
println!("Signed key for {}", args.keyid);
Ok(())
}