diff --git a/crypto/src/bcpg/DsaPublicBcpgKey.cs b/crypto/src/bcpg/DsaPublicBcpgKey.cs
index 61159567c..11294cc22 100644
--- a/crypto/src/bcpg/DsaPublicBcpgKey.cs
+++ b/crypto/src/bcpg/DsaPublicBcpgKey.cs
@@ -4,46 +4,46 @@ using Org.BouncyCastle.Math;
namespace Org.BouncyCastle.Bcpg
{
- /// <remarks>Base class for a DSA public key.</remarks>
- public class DsaPublicBcpgKey
- : BcpgObject, IBcpgKey
+ /// <remarks>Base class for a DSA public key.</remarks>
+ public class DsaPublicBcpgKey
+ : BcpgObject, IBcpgKey
{
private readonly MPInteger p, q, g, y;
- /// <param name="bcpgIn">The stream to read the packet from.</param>
- public DsaPublicBcpgKey(
- BcpgInputStream bcpgIn)
- {
- this.p = new MPInteger(bcpgIn);
- this.q = new MPInteger(bcpgIn);
- this.g = new MPInteger(bcpgIn);
- this.y = new MPInteger(bcpgIn);
- }
+ /// <param name="bcpgIn">The stream to read the packet from.</param>
+ public DsaPublicBcpgKey(
+ BcpgInputStream bcpgIn)
+ {
+ this.p = new MPInteger(bcpgIn);
+ this.q = new MPInteger(bcpgIn);
+ this.g = new MPInteger(bcpgIn);
+ this.y = new MPInteger(bcpgIn);
+ }
- public DsaPublicBcpgKey(
- BigInteger p,
- BigInteger q,
- BigInteger g,
- BigInteger y)
- {
- this.p = new MPInteger(p);
- this.q = new MPInteger(q);
- this.g = new MPInteger(g);
- this.y = new MPInteger(y);
- }
+ public DsaPublicBcpgKey(
+ BigInteger p,
+ BigInteger q,
+ BigInteger g,
+ BigInteger y)
+ {
+ this.p = new MPInteger(p);
+ this.q = new MPInteger(q);
+ this.g = new MPInteger(g);
+ this.y = new MPInteger(y);
+ }
- /// <summary>The format, as a string, always "PGP".</summary>
- public string Format
- {
- get { return "PGP"; }
- }
+ /// <summary>The format, as a string, always "PGP".</summary>
+ public string Format
+ {
+ get { return "PGP"; }
+ }
- /// <summary>Return the standard PGP encoding of the key.</summary>
+ /// <summary>Return the standard PGP encoding of the key.</summary>
public override byte[] GetEncoded()
{
try
{
- return base.GetEncoded();
+ return base.GetEncoded();
}
catch (Exception)
{
@@ -51,30 +51,30 @@ namespace Org.BouncyCastle.Bcpg
}
}
- public override void Encode(
- BcpgOutputStream bcpgOut)
- {
- bcpgOut.WriteObjects(p, q, g, y);
- }
+ public override void Encode(
+ BcpgOutputStream bcpgOut)
+ {
+ bcpgOut.WriteObjects(p, q, g, y);
+ }
- public BigInteger G
- {
- get { return g.Value; }
- }
+ public BigInteger G
+ {
+ get { return g.Value; }
+ }
- public BigInteger P
- {
- get { return p.Value; }
- }
+ public BigInteger P
+ {
+ get { return p.Value; }
+ }
- public BigInteger Q
- {
- get { return q.Value; }
- }
+ public BigInteger Q
+ {
+ get { return q.Value; }
+ }
- public BigInteger Y
- {
- get { return y.Value; }
- }
+ public BigInteger Y
+ {
+ get { return y.Value; }
+ }
}
}
diff --git a/crypto/src/bcpg/PublicKeyPacket.cs b/crypto/src/bcpg/PublicKeyPacket.cs
index 32d43149b..a45aeb469 100644
--- a/crypto/src/bcpg/PublicKeyPacket.cs
+++ b/crypto/src/bcpg/PublicKeyPacket.cs
@@ -5,11 +5,11 @@ using Org.BouncyCastle.Utilities.Date;
namespace Org.BouncyCastle.Bcpg
{
- /// <remarks>Basic packet for a PGP public key.</remarks>
+ /// <remarks>Basic packet for a PGP public key.</remarks>
public class PublicKeyPacket
: ContainedPacket //, PublicKeyAlgorithmTag
{
- private int version;
+ private int version;
private long time;
private int validDays;
private PublicKeyAlgorithmTag algorithm;
@@ -49,44 +49,44 @@ namespace Org.BouncyCastle.Bcpg
}
}
- /// <summary>Construct a version 4 public key packet.</summary>
+ /// <summary>Construct a version 4 public key packet.</summary>
public PublicKeyPacket(
PublicKeyAlgorithmTag algorithm,
DateTime time,
IBcpgKey key)
{
- this.version = 4;
+ this.version = 4;
this.time = DateTimeUtilities.DateTimeToUnixMs(time) / 1000L;
this.algorithm = algorithm;
this.key = key;
}
- public int Version
+ public virtual int Version
{
- get { return version; }
+ get { return version; }
}
- public PublicKeyAlgorithmTag Algorithm
+ public virtual PublicKeyAlgorithmTag Algorithm
{
- get { return algorithm; }
+ get { return algorithm; }
}
- public int ValidDays
+ public virtual int ValidDays
{
- get { return validDays; }
+ get { return validDays; }
}
- public DateTime GetTime()
+ public virtual DateTime GetTime()
{
return DateTimeUtilities.UnixMsToDateTime(time * 1000L);
}
- public IBcpgKey Key
+ public virtual IBcpgKey Key
{
- get { return key; }
+ get { return key; }
}
- public byte[] GetEncodedContents()
+ public virtual byte[] GetEncodedContents()
{
MemoryStream bOut = new MemoryStream();
BcpgOutputStream pOut = new BcpgOutputStream(bOut);
@@ -94,22 +94,22 @@ namespace Org.BouncyCastle.Bcpg
pOut.WriteByte((byte) version);
pOut.WriteInt((int) time);
- if (version <= 3)
+ if (version <= 3)
{
pOut.WriteShort((short) validDays);
}
- pOut.WriteByte((byte) algorithm);
+ pOut.WriteByte((byte) algorithm);
- pOut.WriteObject((BcpgObject)key);
+ pOut.WriteObject((BcpgObject)key);
- return bOut.ToArray();
+ return bOut.ToArray();
}
- public override void Encode(
- BcpgOutputStream bcpgOut)
- {
- bcpgOut.WritePacket(PacketTag.PublicKey, GetEncodedContents(), true);
- }
- }
+ public override void Encode(
+ BcpgOutputStream bcpgOut)
+ {
+ bcpgOut.WritePacket(PacketTag.PublicKey, GetEncodedContents(), true);
+ }
+ }
}
diff --git a/crypto/src/bcpg/UserAttributeSubpacket.cs b/crypto/src/bcpg/UserAttributeSubpacket.cs
index bd49d2150..05f60ac17 100644
--- a/crypto/src/bcpg/UserAttributeSubpacket.cs
+++ b/crypto/src/bcpg/UserAttributeSubpacket.cs
@@ -10,40 +10,44 @@ namespace Org.BouncyCastle.Bcpg
*/
public class UserAttributeSubpacket
{
- private readonly UserAttributeSubpacketTag type;
- private readonly byte[] data;
+ internal readonly UserAttributeSubpacketTag type;
+ private readonly bool longLength; // we preserve this as not everyone encodes length properly.
+ protected readonly byte[] data;
- internal UserAttributeSubpacket(
- UserAttributeSubpacketTag type,
- byte[] data)
+ protected internal UserAttributeSubpacket(UserAttributeSubpacketTag type, byte[] data)
+ : this(type, false, data)
+ {
+ }
+
+ protected internal UserAttributeSubpacket(UserAttributeSubpacketTag type, bool forceLongLength, byte[] data)
{
this.type = type;
+ this.longLength = forceLongLength;
this.data = data;
}
- public UserAttributeSubpacketTag SubpacketType
+ public virtual UserAttributeSubpacketTag SubpacketType
{
get { return type; }
}
- /**
+ /**
* return the generic data making up the packet.
*/
- public byte[] GetData()
+ public virtual byte[] GetData()
{
return data;
}
- public void Encode(
- Stream os)
+ public virtual void Encode(Stream os)
{
int bodyLen = data.Length + 1;
- if (bodyLen < 192)
+ if (bodyLen < 192 && !longLength)
{
os.WriteByte((byte)bodyLen);
}
- else if (bodyLen <= 8383)
+ else if (bodyLen <= 8383 && !longLength)
{
bodyLen -= 192;
@@ -69,18 +73,18 @@ namespace Org.BouncyCastle.Bcpg
if (obj == this)
return true;
- UserAttributeSubpacket other = obj as UserAttributeSubpacket;
+ UserAttributeSubpacket other = obj as UserAttributeSubpacket;
- if (other == null)
- return false;
+ if (other == null)
+ return false;
- return type == other.type
- && Arrays.AreEqual(data, other.data);
+ return type == other.type
+ && Arrays.AreEqual(data, other.data);
}
- public override int GetHashCode()
+ public override int GetHashCode()
{
- return type.GetHashCode() ^ Arrays.GetHashCode(data);
+ return type.GetHashCode() ^ Arrays.GetHashCode(data);
}
}
}
diff --git a/crypto/src/bcpg/UserAttributeSubpacketsReader.cs b/crypto/src/bcpg/UserAttributeSubpacketsReader.cs
index 2e5ea0f3e..f0cc1b8e4 100644
--- a/crypto/src/bcpg/UserAttributeSubpacketsReader.cs
+++ b/crypto/src/bcpg/UserAttributeSubpacketsReader.cs
@@ -5,59 +5,61 @@ using Org.BouncyCastle.Utilities.IO;
namespace Org.BouncyCastle.Bcpg
{
- /**
- * reader for user attribute sub-packets
- */
- public class UserAttributeSubpacketsParser
- {
- private readonly Stream input;
+ /**
+ * reader for user attribute sub-packets
+ */
+ public class UserAttributeSubpacketsParser
+ {
+ private readonly Stream input;
- public UserAttributeSubpacketsParser(
- Stream input)
- {
- this.input = input;
- }
+ public UserAttributeSubpacketsParser(
+ Stream input)
+ {
+ this.input = input;
+ }
- public UserAttributeSubpacket ReadPacket()
- {
- int l = input.ReadByte();
- if (l < 0)
- return null;
+ public virtual UserAttributeSubpacket ReadPacket()
+ {
+ int l = input.ReadByte();
+ if (l < 0)
+ return null;
- int bodyLen = 0;
- if (l < 192)
- {
- bodyLen = l;
- }
- else if (l <= 223)
- {
- bodyLen = ((l - 192) << 8) + (input.ReadByte()) + 192;
- }
- else if (l == 255)
- {
- bodyLen = (input.ReadByte() << 24) | (input.ReadByte() << 16)
- | (input.ReadByte() << 8) | input.ReadByte();
- }
- else
- {
- // TODO Error?
- }
+ int bodyLen = 0;
+ bool longLength = false;
+ if (l < 192)
+ {
+ bodyLen = l;
+ }
+ else if (l <= 223)
+ {
+ bodyLen = ((l - 192) << 8) + (input.ReadByte()) + 192;
+ }
+ else if (l == 255)
+ {
+ bodyLen = (input.ReadByte() << 24) | (input.ReadByte() << 16)
+ | (input.ReadByte() << 8) | input.ReadByte();
+ longLength = true;
+ }
+ else
+ {
+ throw new IOException("unrecognised length reading user attribute sub packet");
+ }
- int tag = input.ReadByte();
- if (tag < 0)
- throw new EndOfStreamException("unexpected EOF reading user attribute sub packet");
+ int tag = input.ReadByte();
+ if (tag < 0)
+ throw new EndOfStreamException("unexpected EOF reading user attribute sub packet");
- byte[] data = new byte[bodyLen - 1];
- if (Streams.ReadFully(input, data) < data.Length)
- throw new EndOfStreamException();
+ byte[] data = new byte[bodyLen - 1];
+ if (Streams.ReadFully(input, data) < data.Length)
+ throw new EndOfStreamException();
- UserAttributeSubpacketTag type = (UserAttributeSubpacketTag) tag;
- switch (type)
- {
- case UserAttributeSubpacketTag.ImageAttribute:
- return new ImageAttrib(data);
- }
- return new UserAttributeSubpacket(type, data);
- }
- }
+ UserAttributeSubpacketTag type = (UserAttributeSubpacketTag) tag;
+ switch (type)
+ {
+ case UserAttributeSubpacketTag.ImageAttribute:
+ return new ImageAttrib(longLength, data);
+ }
+ return new UserAttributeSubpacket(type, longLength, data);
+ }
+ }
}
diff --git a/crypto/src/bcpg/attr/ImageAttrib.cs b/crypto/src/bcpg/attr/ImageAttrib.cs
index 73490791c..2d0fef8b8 100644
--- a/crypto/src/bcpg/attr/ImageAttrib.cs
+++ b/crypto/src/bcpg/attr/ImageAttrib.cs
@@ -3,25 +3,29 @@ using System.IO;
namespace Org.BouncyCastle.Bcpg.Attr
{
- /// <remarks>Basic type for a image attribute packet.</remarks>
+ /// <remarks>Basic type for a image attribute packet.</remarks>
public class ImageAttrib
- : UserAttributeSubpacket
+ : UserAttributeSubpacket
{
- public enum Format : byte
- {
- Jpeg = 1
- }
+ public enum Format : byte
+ {
+ Jpeg = 1
+ }
- private static readonly byte[] Zeroes = new byte[12];
+ private static readonly byte[] Zeroes = new byte[12];
- private int hdrLength;
+ private int hdrLength;
private int _version;
private int _encoding;
private byte[] imageData;
- public ImageAttrib(
- byte[] data)
- : base(UserAttributeSubpacketTag.ImageAttribute, data)
+ public ImageAttrib(byte[] data)
+ : this(false, data)
+ {
+ }
+
+ public ImageAttrib(bool forceLongLength, byte[] data)
+ : base(UserAttributeSubpacketTag.ImageAttribute, forceLongLength, data)
{
hdrLength = ((data[1] & 0xff) << 8) | (data[0] & 0xff);
_version = data[2] & 0xff;
@@ -31,36 +35,36 @@ namespace Org.BouncyCastle.Bcpg.Attr
Array.Copy(data, hdrLength, imageData, 0, imageData.Length);
}
- public ImageAttrib(
- Format imageType,
- byte[] imageData)
- : this(ToByteArray(imageType, imageData))
- {
- }
+ public ImageAttrib(
+ Format imageType,
+ byte[] imageData)
+ : this(ToByteArray(imageType, imageData))
+ {
+ }
- private static byte[] ToByteArray(
- Format imageType,
- byte[] imageData)
- {
- MemoryStream bOut = new MemoryStream();
- bOut.WriteByte(0x10); bOut.WriteByte(0x00); bOut.WriteByte(0x01);
- bOut.WriteByte((byte) imageType);
- bOut.Write(Zeroes, 0, Zeroes.Length);
- bOut.Write(imageData, 0, imageData.Length);
- return bOut.ToArray();
- }
+ private static byte[] ToByteArray(
+ Format imageType,
+ byte[] imageData)
+ {
+ MemoryStream bOut = new MemoryStream();
+ bOut.WriteByte(0x10); bOut.WriteByte(0x00); bOut.WriteByte(0x01);
+ bOut.WriteByte((byte) imageType);
+ bOut.Write(Zeroes, 0, Zeroes.Length);
+ bOut.Write(imageData, 0, imageData.Length);
+ return bOut.ToArray();
+ }
- public int Version
+ public virtual int Version
{
- get { return _version; }
+ get { return _version; }
}
- public int Encoding
+ public virtual int Encoding
{
- get { return _encoding; }
+ get { return _encoding; }
}
- public byte[] GetImageData()
+ public virtual byte[] GetImageData()
{
return imageData;
}
|