Skip to content

Commit f7d837b

Browse files
committed
github #283 added extra features fields, corrected parsing for new draft
1 parent 33c1a42 commit f7d837b

File tree

2 files changed

+81
-36
lines changed

2 files changed

+81
-36
lines changed

crypto/src/bcpg/sig/Features.cs

Lines changed: 26 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,40 @@ namespace Org.BouncyCastle.Bcpg.Sig
1010
public class Features
1111
: SignatureSubpacket
1212
{
13-
/** Identifier for the modification detection feature */
14-
public static readonly byte FEATURE_MODIFICATION_DETECTION = 1;
13+
/** Identifier for the Modification Detection (packets 18 and 19) */
14+
public static readonly byte FEATURE_MODIFICATION_DETECTION = 0x01;
15+
/** Identifier for the AEAD Encrypted Data Packet (packet 20) and version 5
16+
Symmetric-Key Encrypted Session Key Packets (packet 3) */
17+
public static readonly byte FEATURE_AEAD_ENCRYPTED_DATA = 0x02;
18+
/** Identifier for the Version 5 Public-Key Packet format and corresponding new
19+
fingerprint format */
20+
public static readonly byte FEATURE_VERSION_5_PUBLIC_KEY = 0x04;
1521

16-
private static byte[] FeatureToByteArray(byte feature)
22+
private static byte[] featureToByteArray(byte feature)
1723
{
18-
return new byte[]{ feature };
24+
byte[] data = new byte[1];
25+
data[0] = feature;
26+
return data;
1927
}
2028

2129
public Features(
22-
bool critical,
23-
bool isLongLength,
24-
byte[] data)
25-
: base(SignatureSubpacketTag.Features, critical, isLongLength, data)
30+
bool critical,
31+
bool isLongLength,
32+
byte[] data): base(SignatureSubpacketTag.Features, critical, isLongLength, data)
2633
{
34+
35+
}
36+
37+
38+
public Features(bool critical, byte features): this(critical, false, featureToByteArray(features))
39+
{
40+
2741
}
42+
2843

29-
public Features(bool critical, byte feature)
30-
: base(SignatureSubpacketTag.Features, critical, false, FeatureToByteArray(feature))
44+
public Features(bool critical, int features): this(critical, false, featureToByteArray((byte)features))
3145
{
46+
3247
}
3348

3449
/**
@@ -44,32 +59,7 @@ public bool SupportsModificationDetection
4459
*/
4560
public bool SupportsFeature(byte feature)
4661
{
47-
return Array.IndexOf(data, feature) >= 0;
48-
}
49-
50-
/**
51-
* Sets support for a particular feature.
52-
*/
53-
private void SetSupportsFeature(byte feature, bool support)
54-
{
55-
if (feature == 0)
56-
throw new ArgumentException("cannot be 0", "feature");
57-
58-
int i = Array.IndexOf(data, feature);
59-
if ((i >= 0) == support)
60-
return;
61-
62-
if (support)
63-
{
64-
data = Arrays.Append(data, feature);
65-
}
66-
else
67-
{
68-
byte[] temp = new byte[data.Length - 1];
69-
Array.Copy(data, 0, temp, 0, i);
70-
Array.Copy(data, i + 1, temp, i, temp.Length - i);
71-
data = temp;
72-
}
62+
return (data[0] & feature) != 0;
7363
}
7464
}
7565
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+

2+
using NUnit.Core;
3+
using NUnit.Framework;
4+
using Org.BouncyCastle.Bcpg.Sig;
5+
6+
namespace Org.BouncyCastle.Bcpg.OpenPgp.Tests
7+
{
8+
[TestFixture]
9+
public class PgpFeaturesTest
10+
{
11+
[Test]
12+
public void PerformTest()
13+
{
14+
Features f = new Features(true, Features.FEATURE_MODIFICATION_DETECTION);
15+
Assert.IsTrue(f.SupportsFeature(Features.FEATURE_MODIFICATION_DETECTION));
16+
Assert.IsTrue(f.SupportsModificationDetection);
17+
Assert.IsTrue(!f.SupportsFeature(Features.FEATURE_VERSION_5_PUBLIC_KEY));
18+
19+
f = new Features(true, Features.FEATURE_VERSION_5_PUBLIC_KEY);
20+
Assert.IsTrue(!f.SupportsModificationDetection);
21+
Assert.IsTrue(f.SupportsFeature(Features.FEATURE_VERSION_5_PUBLIC_KEY));
22+
23+
f = new Features(true, Features.FEATURE_AEAD_ENCRYPTED_DATA);
24+
Assert.IsTrue(f.SupportsFeature(Features.FEATURE_AEAD_ENCRYPTED_DATA));
25+
Assert.IsTrue(!f.SupportsModificationDetection);
26+
Assert.IsTrue(!f.SupportsFeature(Features.FEATURE_VERSION_5_PUBLIC_KEY));
27+
28+
f = new Features(true, Features.FEATURE_AEAD_ENCRYPTED_DATA | Features.FEATURE_MODIFICATION_DETECTION);
29+
Assert.IsTrue(f.SupportsFeature(Features.FEATURE_AEAD_ENCRYPTED_DATA));
30+
Assert.IsTrue(f.SupportsModificationDetection);
31+
Assert.IsTrue(!f.SupportsFeature(Features.FEATURE_VERSION_5_PUBLIC_KEY));
32+
33+
f = new Features(true, Features.FEATURE_VERSION_5_PUBLIC_KEY | Features.FEATURE_MODIFICATION_DETECTION);
34+
Assert.IsTrue(!f.SupportsFeature(Features.FEATURE_AEAD_ENCRYPTED_DATA));
35+
Assert.IsTrue(f.SupportsModificationDetection);
36+
Assert.IsTrue(f.SupportsFeature(Features.FEATURE_VERSION_5_PUBLIC_KEY));
37+
}
38+
39+
public static void Main(string[] args)
40+
{
41+
Suite.Run(new NullListener(), NUnit.Core.TestFilter.Empty);
42+
}
43+
44+
[Suite]
45+
public static TestSuite Suite
46+
{
47+
get
48+
{
49+
TestSuite suite = new TestSuite("PGP Features Tests");
50+
suite.Add(new PgpFeaturesTest());
51+
return suite;
52+
}
53+
}
54+
}
55+
}

0 commit comments

Comments
 (0)