summary refs log tree commit diff
diff options
context:
space:
mode:
authorDavid Hook <dgh@bouncycastle.org>2021-05-23 13:28:45 +1000
committerDavid Hook <dgh@bouncycastle.org>2021-05-23 13:28:45 +1000
commitf7d837b990cf19d3b324c7238eace45eb8c5d6bf (patch)
treec1b71240a688b4dcd3c237c4d2add9f091b33a97
parentgithub #296 TSP fix (diff)
downloadBouncyCastle.NET-ed25519-f7d837b990cf19d3b324c7238eace45eb8c5d6bf.tar.xz
github #283 added extra features fields, corrected parsing for new draft
-rw-r--r--crypto/src/bcpg/sig/Features.cs62
-rw-r--r--crypto/test/src/openpgp/test/PgpFeaturesTest.cs55
2 files changed, 81 insertions, 36 deletions
diff --git a/crypto/src/bcpg/sig/Features.cs b/crypto/src/bcpg/sig/Features.cs
index 29584239a..135d7f54e 100644
--- a/crypto/src/bcpg/sig/Features.cs
+++ b/crypto/src/bcpg/sig/Features.cs
@@ -10,25 +10,40 @@ namespace Org.BouncyCastle.Bcpg.Sig
     public class Features
         : SignatureSubpacket
     {
-        /** Identifier for the modification detection feature */
-        public static readonly byte FEATURE_MODIFICATION_DETECTION = 1;
+        /** Identifier for the Modification Detection (packets 18 and 19) */
+        public static readonly byte FEATURE_MODIFICATION_DETECTION = 0x01;
+        /** Identifier for the AEAD Encrypted Data Packet (packet 20) and version 5
+         Symmetric-Key Encrypted Session Key Packets (packet 3) */
+        public static readonly byte FEATURE_AEAD_ENCRYPTED_DATA = 0x02;
+        /** Identifier for the Version 5 Public-Key Packet format and corresponding new
+           fingerprint format */
+        public static readonly byte FEATURE_VERSION_5_PUBLIC_KEY = 0x04;
 
-        private static byte[] FeatureToByteArray(byte feature)
+        private static byte[] featureToByteArray(byte feature)
         {
-            return new byte[]{ feature };
+            byte[] data = new byte[1];
+            data[0] = feature;
+            return data;
         }
 
         public Features(
-            bool    critical,
-            bool    isLongLength,
-            byte[]  data)
-            : base(SignatureSubpacketTag.Features, critical, isLongLength, data)
+            bool critical,
+            bool isLongLength,
+            byte[] data): base(SignatureSubpacketTag.Features, critical, isLongLength, data)
         {
+
+        }
+      
+
+        public Features(bool critical, byte features): this(critical, false, featureToByteArray(features))
+        {
+
         }
+   
 
-        public Features(bool critical, byte feature)
-            : base(SignatureSubpacketTag.Features, critical, false, FeatureToByteArray(feature))
+        public Features(bool critical, int features):  this(critical, false, featureToByteArray((byte)features))
         {
+           
         }
 
         /**
@@ -44,32 +59,7 @@ namespace Org.BouncyCastle.Bcpg.Sig
          */
         public bool SupportsFeature(byte feature)
         {
-            return Array.IndexOf(data, feature) >= 0;
-        }
-
-        /**
-         * Sets support for a particular feature.
-         */
-        private void SetSupportsFeature(byte feature, bool support)
-        {
-            if (feature == 0)
-                throw new ArgumentException("cannot be 0", "feature");
-
-            int i = Array.IndexOf(data, feature);
-            if ((i >= 0) == support)
-                return;
-
-            if (support)
-            {
-                data = Arrays.Append(data, feature);
-            }
-            else
-            {
-                byte[] temp = new byte[data.Length - 1];
-                Array.Copy(data, 0, temp, 0, i);
-                Array.Copy(data, i + 1, temp, i, temp.Length - i);
-                data = temp;
-            }
+            return (data[0] & feature) != 0;
         }
     }
 }
diff --git a/crypto/test/src/openpgp/test/PgpFeaturesTest.cs b/crypto/test/src/openpgp/test/PgpFeaturesTest.cs
new file mode 100644
index 000000000..2969d8982
--- /dev/null
+++ b/crypto/test/src/openpgp/test/PgpFeaturesTest.cs
@@ -0,0 +1,55 @@
+
+using NUnit.Core;
+using NUnit.Framework;
+using Org.BouncyCastle.Bcpg.Sig;
+
+namespace Org.BouncyCastle.Bcpg.OpenPgp.Tests
+{
+    [TestFixture]
+    public class PgpFeaturesTest
+    {
+        [Test]
+        public void PerformTest()
+        {
+            Features f = new Features(true, Features.FEATURE_MODIFICATION_DETECTION);
+            Assert.IsTrue(f.SupportsFeature(Features.FEATURE_MODIFICATION_DETECTION));
+            Assert.IsTrue(f.SupportsModificationDetection);
+            Assert.IsTrue(!f.SupportsFeature(Features.FEATURE_VERSION_5_PUBLIC_KEY));
+
+            f = new Features(true, Features.FEATURE_VERSION_5_PUBLIC_KEY);
+            Assert.IsTrue(!f.SupportsModificationDetection);
+            Assert.IsTrue(f.SupportsFeature(Features.FEATURE_VERSION_5_PUBLIC_KEY));
+
+            f = new Features(true, Features.FEATURE_AEAD_ENCRYPTED_DATA);
+            Assert.IsTrue(f.SupportsFeature(Features.FEATURE_AEAD_ENCRYPTED_DATA));
+            Assert.IsTrue(!f.SupportsModificationDetection);
+            Assert.IsTrue(!f.SupportsFeature(Features.FEATURE_VERSION_5_PUBLIC_KEY));
+
+            f = new Features(true, Features.FEATURE_AEAD_ENCRYPTED_DATA | Features.FEATURE_MODIFICATION_DETECTION);
+            Assert.IsTrue(f.SupportsFeature(Features.FEATURE_AEAD_ENCRYPTED_DATA));
+            Assert.IsTrue(f.SupportsModificationDetection);
+            Assert.IsTrue(!f.SupportsFeature(Features.FEATURE_VERSION_5_PUBLIC_KEY));
+
+            f = new Features(true, Features.FEATURE_VERSION_5_PUBLIC_KEY | Features.FEATURE_MODIFICATION_DETECTION);
+            Assert.IsTrue(!f.SupportsFeature(Features.FEATURE_AEAD_ENCRYPTED_DATA));
+            Assert.IsTrue(f.SupportsModificationDetection);
+            Assert.IsTrue(f.SupportsFeature(Features.FEATURE_VERSION_5_PUBLIC_KEY));
+        }
+
+        public static void Main(string[] args)
+        {
+            Suite.Run(new NullListener(), NUnit.Core.TestFilter.Empty);
+        }
+
+        [Suite]
+        public static TestSuite Suite
+        {
+            get
+            {
+                TestSuite suite = new TestSuite("PGP Features Tests");
+                suite.Add(new PgpFeaturesTest());
+                return suite;
+            }
+        }
+    }
+}