summary refs log tree commit diff
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2015-03-27 11:54:06 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2015-03-27 11:54:06 +0700
commit703ef7118a82ba378ed2ebdf7ca7a061339af8f2 (patch)
tree3cde212ef490a34f3b02361393c81ccd32de7eb6
parentImproved docs and code cleanup (diff)
downloadBouncyCastle.NET-ed25519-703ef7118a82ba378ed2ebdf7ca7a061339af8f2.tar.xz
ValidDays only returns 0 if there is no expiry, and is Obsolete
- Thanks to Severin Friede for reporting (see JIRA BMA-127)
-rw-r--r--crypto/src/openpgp/PgpPublicKey.cs47
1 files changed, 26 insertions, 21 deletions
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
         }
 
         /// <summary>The number of valid days from creation time - zero means no expiry.</summary>
+        /// <remarks>WARNING: This method will return 1 for keys with version > 3 that expire in less than 1 day</remarks>
+        [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
         /// <summary>The number of valid seconds from creation time - zero means no expiry.</summary>
         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(