Initial Commit

This commit is contained in:
2023-06-21 12:46:23 -04:00
commit c70248a520
1352 changed files with 336780 additions and 0 deletions

View File

@@ -0,0 +1,47 @@
using System;
namespace Org.BouncyCastle.Bcpg.Sig
{
/**
* packet giving signature creation time.
*/
public class Exportable
: SignatureSubpacket
{
private static byte[] BooleanToByteArray(bool val)
{
byte[] data = new byte[1];
if (val)
{
data[0] = 1;
return data;
}
else
{
return data;
}
}
public Exportable(
bool critical,
byte[] data)
: base(SignatureSubpacketTag.Exportable, critical, data)
{
}
public Exportable(
bool critical,
bool isExportable)
: base(SignatureSubpacketTag.Exportable, critical, BooleanToByteArray(isExportable))
{
}
public bool IsExportable()
{
return data[0] != 0;
}
}
}

View File

@@ -0,0 +1,61 @@
using System;
namespace Org.BouncyCastle.Bcpg.Sig
{
/**
* packet giving signature creation time.
*/
public class IssuerKeyId
: SignatureSubpacket
{
protected static byte[] KeyIdToBytes(
long keyId)
{
byte[] data = new byte[8];
data[0] = (byte)(keyId >> 56);
data[1] = (byte)(keyId >> 48);
data[2] = (byte)(keyId >> 40);
data[3] = (byte)(keyId >> 32);
data[4] = (byte)(keyId >> 24);
data[5] = (byte)(keyId >> 16);
data[6] = (byte)(keyId >> 8);
data[7] = (byte)keyId;
return data;
}
public IssuerKeyId(
bool critical,
byte[] data)
: base(SignatureSubpacketTag.IssuerKeyId, critical, data)
{
}
public IssuerKeyId(
bool critical,
long keyId)
: base(SignatureSubpacketTag.IssuerKeyId, critical, KeyIdToBytes(keyId))
{
}
public long KeyId
{
get
{
long keyId = ((long)(data[0] & 0xff) << 56)
| ((long)(data[1] & 0xff) << 48)
| ((long)(data[2] & 0xff) << 40)
| ((long)(data[3] & 0xff) << 32)
| ((long)(data[4] & 0xff) << 24)
| ((long)(data[5] & 0xff) << 16)
| ((long)(data[6] & 0xff) << 8)
| ((long)data[7] & 0xff);
return keyId;
}
}
}
}

View File

@@ -0,0 +1,56 @@
using System;
namespace Org.BouncyCastle.Bcpg.Sig
{
/**
* packet giving time after creation at which the key expires.
*/
public class KeyExpirationTime
: SignatureSubpacket
{
protected static byte[] TimeToBytes(
long t)
{
byte[] data = new byte[4];
data[0] = (byte)(t >> 24);
data[1] = (byte)(t >> 16);
data[2] = (byte)(t >> 8);
data[3] = (byte)t;
return data;
}
public KeyExpirationTime(
bool critical,
byte[] data)
: base(SignatureSubpacketTag.KeyExpireTime, critical, data)
{
}
public KeyExpirationTime(
bool critical,
long seconds)
: base(SignatureSubpacketTag.KeyExpireTime, critical, TimeToBytes(seconds))
{
}
/**
* Return the number of seconds after creation time a key is valid for.
*
* @return second count for key validity.
*/
public long Time
{
get
{
long time = ((long)(data[0] & 0xff) << 24) | ((long)(data[1] & 0xff) << 16)
| ((long)(data[2] & 0xff) << 8) | ((long)data[3] & 0xff);
return time;
}
}
}
}

View File

@@ -0,0 +1,36 @@
using System;
namespace Org.BouncyCastle.Bcpg.Sig
{
/**
* Packet holding the key flag values.
*/
public class KeyFlags
: SignatureSubpacket
{
private static byte[] IntToByteArray(
int v)
{
return new byte[]{ (byte) v };
}
public KeyFlags(
bool critical,
byte[] data)
: base(SignatureSubpacketTag.KeyFlags, critical, data)
{
}
public KeyFlags(
bool critical,
int flags)
: base(SignatureSubpacketTag.KeyFlags, critical, IntToByteArray(flags))
{
}
public int Flags
{
get { return data[0] & 0xff; }
}
}
}

View File

@@ -0,0 +1,101 @@
using System;
using System.IO;
using System.Text;
namespace Org.BouncyCastle.Bcpg.Sig
{
/**
* Class provided a NotationData object according to
* RFC2440, Chapter 5.2.3.15. Notation Data
*/
public class NotationData
: SignatureSubpacket
{
public const int HeaderFlagLength = 4;
public const int HeaderNameLength = 2;
public const int HeaderValueLength = 2;
public NotationData(
bool critical,
byte[] data)
: base(SignatureSubpacketTag.NotationData, critical, data)
{
}
public NotationData(
bool critical,
bool humanReadable,
string notationName,
string notationValue)
: base(SignatureSubpacketTag.NotationData, critical,
createData(humanReadable, notationName, notationValue))
{
}
private static byte[] createData(
bool humanReadable,
string notationName,
string notationValue)
{
MemoryStream os = new MemoryStream();
// (4 octets of flags, 2 octets of name length (M),
// 2 octets of value length (N),
// M octets of name data,
// N octets of value data)
// flags
os.WriteByte(humanReadable ? (byte)0x80 : (byte)0x00);
os.WriteByte(0x0);
os.WriteByte(0x0);
os.WriteByte(0x0);
byte[] nameData, valueData = null;
int nameLength, valueLength;
nameData = Encoding.UTF8.GetBytes(notationName);
nameLength = System.Math.Min(nameData.Length, 0xFF);
valueData = Encoding.UTF8.GetBytes(notationValue);
valueLength = System.Math.Min(valueData.Length, 0xFF);
// name length
os.WriteByte((byte)(nameLength >> 8));
os.WriteByte((byte)(nameLength >> 0));
// value length
os.WriteByte((byte)(valueLength >> 8));
os.WriteByte((byte)(valueLength >> 0));
// name
os.Write(nameData, 0, nameLength);
// value
os.Write(valueData, 0, valueLength);
return os.ToArray();
}
public bool IsHumanReadable
{
get { return data[0] == (byte)0x80; }
}
public string GetNotationName()
{
int nameLength = ((data[HeaderFlagLength] << 8) + (data[HeaderFlagLength + 1] << 0));
int namePos = HeaderFlagLength + HeaderNameLength + HeaderValueLength;
return Encoding.UTF8.GetString(data, namePos, nameLength);
}
public string GetNotationValue()
{
int nameLength = ((data[HeaderFlagLength] << 8) + (data[HeaderFlagLength + 1] << 0));
int valueLength = ((data[HeaderFlagLength + HeaderNameLength] << 8) + (data[HeaderFlagLength + HeaderNameLength + 1] << 0));
int valuePos = HeaderFlagLength + HeaderNameLength + HeaderValueLength + nameLength;
return Encoding.UTF8.GetString(data, valuePos, valueLength);
}
}
}

View File

@@ -0,0 +1,54 @@
using System;
namespace Org.BouncyCastle.Bcpg.Sig
{
/**
* packet giving signature creation time.
*/
public class PreferredAlgorithms
: SignatureSubpacket
{
private static byte[] IntToByteArray(
int[] v)
{
byte[] data = new byte[v.Length];
for (int i = 0; i != v.Length; i++)
{
data[i] = (byte)v[i];
}
return data;
}
public PreferredAlgorithms(
SignatureSubpacketTag type,
bool critical,
byte[] data)
: base(type, critical, data)
{
}
public PreferredAlgorithms(
SignatureSubpacketTag type,
bool critical,
int[] preferences)
: base(type, critical, IntToByteArray(preferences))
{
}
public int[] GetPreferences()
{
int[] v = new int[data.Length];
for (int i = 0; i != v.Length; i++)
{
v[i] = data[i] & 0xff;
}
return v;
}
}
}

View File

@@ -0,0 +1,48 @@
using System;
namespace Org.BouncyCastle.Bcpg.Sig
{
/**
* packet giving whether or not the signature is signed using the primary user ID for the key.
*/
public class PrimaryUserId
: SignatureSubpacket
{
private static byte[] BooleanToByteArray(
bool val)
{
byte[] data = new byte[1];
if (val)
{
data[0] = 1;
return data;
}
else
{
return data;
}
}
public PrimaryUserId(
bool critical,
byte[] data)
: base(SignatureSubpacketTag.PrimaryUserId, critical, data)
{
}
public PrimaryUserId(
bool critical,
bool isPrimaryUserId)
: base(SignatureSubpacketTag.PrimaryUserId, critical, BooleanToByteArray(isPrimaryUserId))
{
}
public bool IsPrimaryUserId()
{
return data[0] != 0;
}
}
}

View File

@@ -0,0 +1,48 @@
using System;
namespace Org.BouncyCastle.Bcpg.Sig
{
/**
* packet giving whether or not is revocable.
*/
public class Revocable
: SignatureSubpacket
{
private static byte[] BooleanToByteArray(
bool value)
{
byte[] data = new byte[1];
if (value)
{
data[0] = 1;
return data;
}
else
{
return data;
}
}
public Revocable(
bool critical,
byte[] data)
: base(SignatureSubpacketTag.Revocable, critical, data)
{
}
public Revocable(
bool critical,
bool isRevocable)
: base(SignatureSubpacketTag.Revocable, critical, BooleanToByteArray(isRevocable))
{
}
public bool IsRevocable()
{
return data[0] != 0;
}
}
}

View File

@@ -0,0 +1,47 @@
using System;
using Org.BouncyCastle.Utilities.Date;
namespace Org.BouncyCastle.Bcpg.Sig
{
/**
* packet giving signature creation time.
*/
public class SignatureCreationTime
: SignatureSubpacket
{
protected static byte[] TimeToBytes(
DateTime time)
{
long t = DateTimeUtilities.DateTimeToUnixMs(time) / 1000L;
byte[] data = new byte[4];
data[0] = (byte)(t >> 24);
data[1] = (byte)(t >> 16);
data[2] = (byte)(t >> 8);
data[3] = (byte)t;
return data;
}
public SignatureCreationTime(
bool critical,
byte[] data)
: base(SignatureSubpacketTag.CreationTime, critical, data)
{
}
public SignatureCreationTime(
bool critical,
DateTime date)
: base(SignatureSubpacketTag.CreationTime, critical, TimeToBytes(date))
{
}
public DateTime GetTime()
{
long time = (long)(
((uint)data[0] << 24)
| ((uint)data[1] << 16)
| ((uint)data[2] << 8)
| ((uint)data[3])
);
return DateTimeUtilities.UnixMsToDateTime(time * 1000L);
}
}
}

View File

@@ -0,0 +1,54 @@
using System;
namespace Org.BouncyCastle.Bcpg.Sig
{
/**
* packet giving signature expiration time.
*/
public class SignatureExpirationTime
: SignatureSubpacket
{
protected static byte[] TimeToBytes(
long t)
{
byte[] data = new byte[4];
data[0] = (byte)(t >> 24);
data[1] = (byte)(t >> 16);
data[2] = (byte)(t >> 8);
data[3] = (byte)t;
return data;
}
public SignatureExpirationTime(
bool critical,
byte[] data)
: base(SignatureSubpacketTag.ExpireTime, critical, data)
{
}
public SignatureExpirationTime(
bool critical,
long seconds)
: base(SignatureSubpacketTag.ExpireTime, critical, TimeToBytes(seconds))
{
}
/**
* return time in seconds before signature expires after creation time.
*/
public long Time
{
get
{
long time = ((long)(data[0] & 0xff) << 24) | ((long)(data[1] & 0xff) << 16)
| ((long)(data[2] & 0xff) << 8) | ((long)data[3] & 0xff);
return time;
}
}
}
}

View File

@@ -0,0 +1,52 @@
using System;
namespace Org.BouncyCastle.Bcpg.Sig
{
/**
* packet giving the User ID of the signer.
*/
public class SignerUserId
: SignatureSubpacket
{
private static byte[] UserIdToBytes(
string id)
{
byte[] idData = new byte[id.Length];
for (int i = 0; i != id.Length; i++)
{
idData[i] = (byte)id[i];
}
return idData;
}
public SignerUserId(
bool critical,
byte[] data)
: base(SignatureSubpacketTag.SignerUserId, critical, data)
{
}
public SignerUserId(
bool critical,
string userId)
: base(SignatureSubpacketTag.SignerUserId, critical, UserIdToBytes(userId))
{
}
public string GetId()
{
char[] chars = new char[data.Length];
for (int i = 0; i != chars.Length; i++)
{
chars[i] = (char)(data[i] & 0xff);
}
return new string(chars);
}
}
}

View File

@@ -0,0 +1,56 @@
using System;
namespace Org.BouncyCastle.Bcpg.Sig
{
/**
* packet giving signature creation time.
*/
public class TrustSignature
: SignatureSubpacket
{
private static byte[] IntToByteArray(
int v1,
int v2)
{
byte[] data = new byte[2];
data[0] = (byte)v1;
data[1] = (byte)v2;
return data;
}
public TrustSignature(
bool critical,
byte[] data)
: base(SignatureSubpacketTag.TrustSig, critical, data)
{
}
public TrustSignature(
bool critical,
int depth,
int trustAmount)
: base(SignatureSubpacketTag.TrustSig, critical, IntToByteArray(depth, trustAmount))
{
}
public int Depth
{
get
{
return data[0] & 0xff;
}
}
public int TrustAmount
{
get
{
return data[1] & 0xff;
}
}
}
}