gpg-rs / gpgme

GPGme bindings for Rust
GNU Lesser General Public License v2.1
83 stars 13 forks source link

Invalid expiration date #32

Closed DBLouis closed 4 years ago

DBLouis commented 4 years ago

Using the following code, the created keys should expires one hour later. In fact they expires after 60 years.

Side note: Why not ask for a duration instead of a absolute time? It would match the API of gpgme.

use gpgme::{Context, CreateKeyFlags, PassphraseRequest, PinentryMode, Protocol};
use std::{
    io::Write,
    time::{Duration, SystemTime},
};

fn main() {
    let validity = Duration::from_secs(3600);
    let expiry = SystemTime::now() + validity;

    let mut ctx = Context::from_protocol(Protocol::OpenPgp).unwrap();
    ctx.set_offline(true);
    ctx.set_armor(true);
    ctx.set_pinentry_mode(PinentryMode::Loopback).unwrap();
    ctx.set_engine_home_dir("/tmp").unwrap();

    ctx.with_passphrase_provider(
        |_: PassphraseRequest, out: &mut dyn Write| {
            out.write_all("password".as_bytes())?;
            Ok(())
        },
        |ctx| {
            let res = ctx
                .create_key_with_flags(
                    "john doe <john@doe.com>",
                    "ed25519",
                    Some(expiry),
                    CreateKeyFlags::CERT,
                )
                .unwrap();
            let fpr = res.fingerprint().unwrap();
            let key = ctx.get_key(fpr).unwrap();
            ctx.create_subkey_with_flags(&key, "ed25519", Some(expiry), CreateKeyFlags::AUTH)
                .unwrap();
            ctx.create_subkey_with_flags(&key, "cv25519", Some(expiry), CreateKeyFlags::ENCR)
                .unwrap();
            ctx.create_subkey_with_flags(&key, "ed25519", Some(expiry), CreateKeyFlags::SIGN)
                .unwrap();
        },
    );
}
johnschug commented 4 years ago

This should be fixed now. When the wrappers for those functions were originally written, the upstream documentation listed the expire parameter as a timestamp. The documentation was changed later.

The wrappers now take a duration to match the current upstream behaviour.

DBLouis commented 4 years ago

I just tested it and it works. Thank you