From 703ef7118a82ba378ed2ebdf7ca7a061339af8f2 Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Fri, 27 Mar 2015 11:54:06 +0700 Subject: ValidDays only returns 0 if there is no expiry, and is Obsolete - Thanks to Severin Friede for reporting (see JIRA BMA-127) --- crypto/src/openpgp/PgpPublicKey.cs | 47 +++++++++++++++++++++----------------- 1 file changed, 26 insertions(+), 21 deletions(-) (limited to 'crypto/src') diff --git a/crypto/src/openpgp/PgpPublicKey.cs b/crypto/src/openpgp/PgpPublicKey.cs index c6b2e9e0e..249b94ea6 100644 --- a/crypto/src/openpgp/PgpPublicKey.cs +++ b/crypto/src/openpgp/PgpPublicKey.cs @@ -266,16 +266,23 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp } /// The number of valid days from creation time - zero means no expiry. + /// WARNING: This method will return 1 for keys with version > 3 that expire in less than 1 day + [Obsolete("Use 'GetValidSeconds' instead")] public int ValidDays { get { - if (publicPk.Version > 3) + if (publicPk.Version <= 3) { - return (int)(GetValidSeconds() / (24 * 60 * 60)); + return publicPk.ValidDays; } - return publicPk.ValidDays; + long expSecs = GetValidSeconds(); + if (expSecs <= 0) + return 0; + + int days = (int)(expSecs / (24 * 60 * 60)); + return System.Math.Max(1, days); } } @@ -294,34 +301,32 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp /// The number of valid seconds from creation time - zero means no expiry. public long GetValidSeconds() { - if (publicPk.Version > 3) + if (publicPk.Version <= 3) { - if (IsMasterKey) - { - for (int i = 0; i != MasterKeyCertificationTypes.Length; i++) - { - long seconds = GetExpirationTimeFromSig(true, MasterKeyCertificationTypes[i]); + return (long)publicPk.ValidDays * (24 * 60 * 60); + } - if (seconds >= 0) - { - return seconds; - } - } - } - else + if (IsMasterKey) + { + for (int i = 0; i != MasterKeyCertificationTypes.Length; i++) { - long seconds = GetExpirationTimeFromSig(false, PgpSignature.SubkeyBinding); - + long seconds = GetExpirationTimeFromSig(true, MasterKeyCertificationTypes[i]); if (seconds >= 0) { return seconds; } } - - return 0; + } + else + { + long seconds = GetExpirationTimeFromSig(false, PgpSignature.SubkeyBinding); + if (seconds >= 0) + { + return seconds; + } } - return (long) publicPk.ValidDays * 24 * 60 * 60; + return 0; } private long GetExpirationTimeFromSig( -- cgit 1.4.1