How to get passphrase cache duration left in gpg-agent?

Kalib Zen

I know there is a gpg-agent config to set how long we can cache a password into gpg-agent. The setting is called --max-cache-ttl n

But when a passphrase is cached in gpg-agent for example for 10 seconds, how do I obtain the current cache duration like how many seconds left until it will be expired? Is there a query option for this where I can obtain directly from gpg-agent?

MaXi32

Not sure about the built-in feature that gpg-agent has. I don't think it is possible but I'm showing a trick how you can get cache duration left:

First rule: When you cache a passphrase in gpg-agent, you first store the date in unix timestamp as a variable inside a config file:

GPG_MY_CONFIG="~/.gnupg/my-gpg.conf"
function set_config() {

    sudo sed -i "s/^\($1\s*=\s*\).*\$/\1$2/" $GPG_MY_CONFIG
}

echo "date_cached=$(date +%s)" | sudo tee --append $GPG_MY_CONFIG
# Now you got the following date (with unix timestamp) inside my-gpg.conf like below:
# date_cached=1599710839
# When you cached a new password, then run this code to update new date in unix timestamp:
# set_config date_cached "$(date +%s)"

It's best to have the current --max-cache-ttl n value from gpg-agent.conf file, so we can query this:

# ~/.gnupg/gpg-agent.conf
allow-preset-passphrase
default-cache-ttl 10
max-cache-ttl 10

First, read the setting max-cache-ttl value and save it in a variable expired_in_second like this:

# location of gpg config file
GPG_CONFIG_FILE="~/.gnupg/gpg-agent.conf"
# read the config file for value max-cache-ttl
expired_in_second=$(grep -oP 'max-cache-ttl\s*\K\d+' $GPG_CONFIG_FILE)

So now you got 2 important variables, you can get expired date by using this 2 variables:

# First source the config file:
source $GPG_MY_CONFIG
# expired_date = date_cached_previously + expired_duration (from max-cache-ttl)
expired_date=$(date -d "$(date -d @${date_cached}) + $expired_in_second seconds")

and to get the duration left you can use this (compare the expired date with the current time):

# second_left = expired_date - current_date
second_left="$(( $(date -d "$expired_date" "+%s") - $(date +%s) ))"

echo "$second_left seconds remaining before password is going to be expired"

Output:

10 seconds remaining before password is going to be expired

I believe the above code can be simplified more. Hope this help :)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to get gpg-agent to ask for passphrase when generating keys with GPGME?

From Dev

How can I get GPG Agent to cache my password?

From Dev

How can I get GPG Agent to cache my password?

From Dev

How can I adjust the default passphrase caching duration for GPG/PGP/SSH keys?

From Dev

How to get more gpg-agent info?

From Dev

Pass, how to cache passphrase

From Dev

How to make gpg find gpg-agent

From Dev

How does GPG agent work?

From Dev

gpg-agent asks for a passphrase, but ssh private key doesn’t have one

From Dev

GPG fails to retrieve passphrase

From Dev

How to generate a gpg4win keypair without passphrase?

From Dev

How to configure gpg to enter passphrase only once per session

From Dev

How to correctly change the passphrase of GPG's secret key?

From Dev

How can I restart gpg-agent?

From Dev

gpg - gpg-preset-passphrase installation

From Dev

How can I remove the passphrase from a gpg2 private key?

From Dev

How can I automate gpg decryption which uses a passphrase while keeping it secret?

From Dev

How can I forward a gpg key via ssh-agent?

From Dev

How to let gpg-agent confirm each key usage

From Dev

How to properly start gpg-agent on Ubuntu 16.04

From Dev

How to set up SSH key forwarding with gpg-agent?

From Dev

Is there a gpg-agent for cygwin?

From Dev

gpg: public key decryption failed: Bad passphrase

From Dev

GPG Passphrase + Secret Key tied encryption

From Dev

GPG says that it needs a passphrase but it actually doesn't

From Dev

GPG passphrase vs public key encryption

From Dev

GPG on Mac freezes when asking for my passphrase

From Dev

Use gpg to pass the passphrase file to decrypt the files

From Dev

ssh-agent: How to set it up so my CentOS server will only ask for passphrase once?

Related Related

  1. 1

    How to get gpg-agent to ask for passphrase when generating keys with GPGME?

  2. 2

    How can I get GPG Agent to cache my password?

  3. 3

    How can I get GPG Agent to cache my password?

  4. 4

    How can I adjust the default passphrase caching duration for GPG/PGP/SSH keys?

  5. 5

    How to get more gpg-agent info?

  6. 6

    Pass, how to cache passphrase

  7. 7

    How to make gpg find gpg-agent

  8. 8

    How does GPG agent work?

  9. 9

    gpg-agent asks for a passphrase, but ssh private key doesn’t have one

  10. 10

    GPG fails to retrieve passphrase

  11. 11

    How to generate a gpg4win keypair without passphrase?

  12. 12

    How to configure gpg to enter passphrase only once per session

  13. 13

    How to correctly change the passphrase of GPG's secret key?

  14. 14

    How can I restart gpg-agent?

  15. 15

    gpg - gpg-preset-passphrase installation

  16. 16

    How can I remove the passphrase from a gpg2 private key?

  17. 17

    How can I automate gpg decryption which uses a passphrase while keeping it secret?

  18. 18

    How can I forward a gpg key via ssh-agent?

  19. 19

    How to let gpg-agent confirm each key usage

  20. 20

    How to properly start gpg-agent on Ubuntu 16.04

  21. 21

    How to set up SSH key forwarding with gpg-agent?

  22. 22

    Is there a gpg-agent for cygwin?

  23. 23

    gpg: public key decryption failed: Bad passphrase

  24. 24

    GPG Passphrase + Secret Key tied encryption

  25. 25

    GPG says that it needs a passphrase but it actually doesn't

  26. 26

    GPG passphrase vs public key encryption

  27. 27

    GPG on Mac freezes when asking for my passphrase

  28. 28

    Use gpg to pass the passphrase file to decrypt the files

  29. 29

    ssh-agent: How to set it up so my CentOS server will only ask for passphrase once?

HotTag

Archive