Search code examples
javabouncycastleopenpgp

Differentiate between PGP subkey that is expired vs one that never expires - Java bouncycastle


I am using bouncycastle.openpgp library to get the validity dates of a PGP key. A PGP key can be assumed to be never expiring if the key.getValidDays() == 0. How to differentiate between a once valid and now expired key vs a never expiring one?


Solution

  • An expired key, will have a non-zero getValidDays, as they are specified relative to the creation date, not the current one.

    getValidDays
    
    public int getValidDays()
    Returns:
    number of valid days from creation time - zero means no expiry.
    

    I.e. you should be able to differentiate between the never expiring and valid/expired keys, with the code like this:

    if(key.getValidSeconds() == 0) {
        //Never Expiring Key
    } else if(Instant.now().isAfter(key.getCreationTime().toInstant().plusSeconds(key.getValidSeconds()))) {
        //Expired Key
    } else {
        //Valid Key (has not expired yet)
    }