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?
Asked
Active
Viewed 907 times
1 Answers
2
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)
}

zeppelin
- 8,947
- 2
- 24
- 30
-
Thanks @zeppelin. I was unit-testing an edge case where I created a key (using GPG Tool) today and tried to set expiration date to 100 years from now but it didn't let me do it and it defaulted to today as expiration date and hence expired the key. So in this case the key.getValidSeconds() was 0 and the key was expired. I know this is not real world use case. Thanks for your answer. – Es Kay Nov 19 '16 at 20:56