diff options
Diffstat (limited to 'crypto/src/asn1')
34 files changed, 909 insertions, 773 deletions
diff --git a/crypto/src/asn1/Asn1GeneralizedTime.cs b/crypto/src/asn1/Asn1GeneralizedTime.cs new file mode 100644 index 000000000..e844c8ca2 --- /dev/null +++ b/crypto/src/asn1/Asn1GeneralizedTime.cs @@ -0,0 +1,439 @@ +using System; +using System.Globalization; +using System.IO; +using System.Text; + +using Org.BouncyCastle.Utilities; + +namespace Org.BouncyCastle.Asn1 +{ + /** + * Base class representing the ASN.1 GeneralizedTime type. + * <p> + * The main difference between these and UTC time is a 4 digit year. + * </p> + * <p> + * One second resolution date+time on UTC timezone (Z) + * with 4 digit year (valid from 0001 to 9999). + * </p><p> + * Timestamp format is: yyyymmddHHMMSS'Z' + * </p><p> + * <h2>X.690</h2> + * This is what is called "restricted string", + * and it uses ASCII characters to encode digits and supplemental data. + * + * <h3>11: Restrictions on BER employed by both CER and DER</h3> + * <h4>11.7 GeneralizedTime </h4> + * <p> + * <b>11.7.1</b> The encoding shall terminate with a "Z", + * as described in the ITU-T Rec. X.680 | ISO/IEC 8824-1 clause on + * GeneralizedTime. + * </p><p> + * <b>11.7.2</b> The seconds element shall always be present. + * </p> + * <p> + * <b>11.7.3</b> The fractional-seconds elements, if present, + * shall omit all trailing zeros; if the elements correspond to 0, + * they shall be wholly omitted, and the decimal point element also + * shall be omitted. + */ + public class Asn1GeneralizedTime + : Asn1Object + { + internal class Meta : Asn1UniversalType + { + internal static readonly Asn1UniversalType Instance = new Meta(); + + private Meta() : base(typeof(Asn1GeneralizedTime), Asn1Tags.GeneralizedTime) { } + + internal override Asn1Object FromImplicitPrimitive(DerOctetString octetString) + { + return CreatePrimitive(octetString.GetOctets()); + } + } + + public static Asn1GeneralizedTime GetInstance(object obj) + { + if (obj == null) + return null; + + if (obj is Asn1GeneralizedTime asn1GeneralizedTime) + return asn1GeneralizedTime; + + if (obj is IAsn1Convertible asn1Convertible) + { + Asn1Object asn1Object = asn1Convertible.ToAsn1Object(); + if (asn1Object is Asn1GeneralizedTime converted) + return converted; + } + else if (obj is byte[] bytes) + { + try + { + return (Asn1GeneralizedTime)Meta.Instance.FromByteArray(bytes); + } + catch (IOException e) + { + throw new ArgumentException("failed to construct generalized time from byte[]: " + e.Message); + } + } + + throw new ArgumentException("illegal object in GetInstance: " + Platform.GetTypeName(obj), nameof(obj)); + } + + public static Asn1GeneralizedTime GetInstance(Asn1TaggedObject taggedObject, bool declaredExplicit) + { + return (Asn1GeneralizedTime)Meta.Instance.GetContextInstance(taggedObject, declaredExplicit); + } + + internal readonly byte[] m_contents; + + public Asn1GeneralizedTime(string time) + { + m_contents = Strings.ToByteArray(time); + + try + { + ToDateTime(); + } + catch (FormatException e) + { + throw new ArgumentException("invalid date string: " + e.Message); + } + } + + public Asn1GeneralizedTime(DateTime time) + { + DateTime utc = time.ToUniversalTime(); + var formatStr = @"yyyyMMddHHmmss\Z"; + var formatProvider = DateTimeFormatInfo.InvariantInfo; + string utcString = utc.ToString(formatStr, formatProvider); + m_contents = Strings.ToByteArray(utcString); + } + + // TODO Custom locale constructor? + //public Asn1GeneralizedTime(DateTime time, Locale locale) + //{ + // SimpleDateFormat dateF = new SimpleDateFormat("yyyyMMddHHmmss\Z", locale); + + // dateF.setTimeZone(new SimpleTimeZone(0, "Z")); + + // this.contents = Strings.toByteArray(dateF.format(time)); + //} + + internal Asn1GeneralizedTime(byte[] bytes) + { + if (bytes == null) + throw new ArgumentNullException(nameof(bytes)); + if (bytes.Length < 4) + throw new ArgumentException("GeneralizedTime string too short", nameof(bytes)); + + m_contents = bytes; + + if (!(IsDigit(0) && IsDigit(1) && IsDigit(2) && IsDigit(3))) + throw new ArgumentException("illegal characters in GeneralizedTime string", nameof(bytes)); + } + + public string TimeString => Strings.FromByteArray(m_contents); + + public string GetTime() + { + string stime = Strings.FromByteArray(m_contents); + + // + // standardise the format. + // + if (stime[stime.Length - 1] == 'Z') + return stime.Substring(0, stime.Length - 1) + "GMT+00:00"; + + int signPos = stime.Length - 6; + char sign = stime[signPos]; + if ((sign == '-' || sign == '+') && stime.IndexOf("GMT") == signPos - 3) + { + // already a GMT string! + return stime; + } + + signPos = stime.Length - 5; + sign = stime[signPos]; + if (sign == '-' || sign == '+') + { + return stime.Substring(0, signPos) + + "GMT" + + stime.Substring(signPos, 3) + + ":" + + stime.Substring(signPos + 3); + } + + signPos = stime.Length - 3; + sign = stime[signPos]; + if (sign == '-' || sign == '+') + { + return stime.Substring(0, signPos) + + "GMT" + + stime.Substring(signPos) + + ":00"; + } + + return stime + CalculateGmtOffset(stime); + } + + private string CalculateGmtOffset(string stime) + { + TimeZoneInfo timeZone = TimeZoneInfo.Local; + TimeSpan offset = timeZone.BaseUtcOffset; + + string sign = "+"; + if (offset.CompareTo(TimeSpan.Zero) < 0) + { + sign = "-"; + offset = offset.Duration(); + } + + int hours = offset.Hours; + int minutes = offset.Minutes; + + try + { + if (timeZone.SupportsDaylightSavingTime) + { + string d = stime + "GMT" + sign + Convert(hours) + ":" + Convert(minutes); + string formatStr = CalculateGmtFormatString(d); + + DateTime dateTime = ParseDateString(d, formatStr, makeUniversal: true); + + if (timeZone.IsDaylightSavingTime(dateTime)) + { + hours += sign.Equals("+") ? 1 : -1; + } + } + } + catch (Exception) + { + // we'll do our best and ignore daylight savings + } + + return "GMT" + sign + Convert(hours) + ":" + Convert(minutes); + } + + private string CalculateGmtFormatString(string d) + { + if (HasFractionalSeconds()) + { + int fCount = Platform.IndexOf(d, "GMT") - 1 - d.IndexOf('.'); + return @"yyyyMMddHHmmss." + FString(fCount) + @"'GMT'zzz"; + } + + if (HasSeconds()) + return @"yyyyMMddHHmmss'GMT'zzz"; + + if (HasMinutes()) + return @"yyyyMMddHHmm'GMT'zzz"; + + return @"yyyyMMddHH'GMT'zzz"; + } + + private string Convert(int time) + { + if (time < 10) + return "0" + time; + + return time.ToString(); + } + + public DateTime ToDateTime() + { + string formatStr; + string stime = Strings.FromByteArray(m_contents); + string d = stime; + bool makeUniversal = false; + + if (Platform.EndsWith(stime, "Z")) + { + if (HasFractionalSeconds()) + { + int fCount = d.Length - d.IndexOf('.') - 2; + formatStr = @"yyyyMMddHHmmss." + FString(fCount) + @"\Z"; + } + else if (HasSeconds()) + { + formatStr = @"yyyyMMddHHmmss\Z"; + } + else if (HasMinutes()) + { + formatStr = @"yyyyMMddHHmm\Z"; + } + else + { + formatStr = @"yyyyMMddHH\Z"; + } + } + else if (stime.IndexOf('-') > 0 || stime.IndexOf('+') > 0) + { + d = GetTime(); + formatStr = CalculateGmtFormatString(d); + makeUniversal = true; + } + else + { + if (HasFractionalSeconds()) + { + int fCount = d.Length - 1 - d.IndexOf('.'); + formatStr = @"yyyyMMddHHmmss." + FString(fCount); + } + else if (HasSeconds()) + { + formatStr = @"yyyyMMddHHmmss"; + } + else if (HasMinutes()) + { + formatStr = @"yyyyMMddHHmm"; + } + else + { + formatStr = @"yyyyMMddHH"; + } + } + + // TODO Epoch adjustment? + //return DateUtil.epochAdjust(dateF.parse(d)); + return ParseDateString(d, formatStr, makeUniversal); + } + + protected bool HasFractionalSeconds() + { + return m_contents.Length > 14 && m_contents[14] == '.'; + } + + protected bool HasSeconds() + { + return IsDigit(12) && IsDigit(13); + } + + protected bool HasMinutes() + { + return IsDigit(10) && IsDigit(11); + } + + private bool IsDigit(int pos) + { + return m_contents.Length > pos && m_contents[pos] >= '0' && m_contents[pos] <= '9'; + } + + internal override IAsn1Encoding GetEncoding(int encoding) + { + if (Asn1OutputStream.EncodingDer == encoding) + return new PrimitiveEncoding(Asn1Tags.Universal, Asn1Tags.GeneralizedTime, GetDerTime()); + + return new PrimitiveEncoding(Asn1Tags.Universal, Asn1Tags.GeneralizedTime, m_contents); + } + + internal override IAsn1Encoding GetEncodingImplicit(int encoding, int tagClass, int tagNo) + { + if (Asn1OutputStream.EncodingDer == encoding) + return new PrimitiveEncoding(tagClass, tagNo, GetDerTime()); + + return new PrimitiveEncoding(tagClass, tagNo, m_contents); + } + + protected override bool Asn1Equals(Asn1Object asn1Object) + { + if (!(asn1Object is Asn1GeneralizedTime that)) + return false; + + return Arrays.AreEqual(m_contents, that.m_contents); + } + + protected override int Asn1GetHashCode() + { + return Arrays.GetHashCode(m_contents); + } + + internal static Asn1GeneralizedTime CreatePrimitive(byte[] contents) + { + return new Asn1GeneralizedTime(contents); + } + + internal byte[] GetDerTime() + { + if (m_contents[m_contents.Length - 1] != 'Z') + { + return m_contents; // TODO: is there a better way? + } + + if (!HasMinutes()) + { + byte[] derTime = new byte[m_contents.Length + 4]; + + Array.Copy(m_contents, 0, derTime, 0, m_contents.Length - 1); + Array.Copy(Strings.ToByteArray("0000Z"), 0, derTime, m_contents.Length - 1, 5); + + return derTime; + } + else if (!HasSeconds()) + { + byte[] derTime = new byte[m_contents.Length + 2]; + + Array.Copy(m_contents, 0, derTime, 0, m_contents.Length - 1); + Array.Copy(Strings.ToByteArray("00Z"), 0, derTime, m_contents.Length - 1, 3); + + return derTime; + } + else if (HasFractionalSeconds()) + { + int ind = m_contents.Length - 2; + while (ind > 0 && m_contents[ind] == '0') + { + ind--; + } + + if (m_contents[ind] == '.') + { + byte[] derTime = new byte[ind + 1]; + + Array.Copy(m_contents, 0, derTime, 0, ind); + derTime[ind] = (byte)'Z'; + + return derTime; + } + else + { + byte[] derTime = new byte[ind + 2]; + + Array.Copy(m_contents, 0, derTime, 0, ind + 1); + derTime[ind + 1] = (byte)'Z'; + + return derTime; + } + } + else + { + return m_contents; + } + } + + private static string FString(int count) + { + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < count; ++i) + { + sb.Append('f'); + } + return sb.ToString(); + } + + private static DateTime ParseDateString(string s, string format, bool makeUniversal) + { + DateTimeStyles dateTimeStyles = DateTimeStyles.None; + if (Platform.EndsWith(format, "Z")) + { + dateTimeStyles |= DateTimeStyles.AdjustToUniversal; + dateTimeStyles |= DateTimeStyles.AssumeUniversal; + } + + DateTime dt = DateTime.ParseExact(s, format, DateTimeFormatInfo.InvariantInfo, dateTimeStyles); + + return makeUniversal ? dt.ToUniversalTime() : dt; + } + } +} diff --git a/crypto/src/asn1/Asn1InputStream.cs b/crypto/src/asn1/Asn1InputStream.cs index aa91cdf62..0e772010f 100644 --- a/crypto/src/asn1/Asn1InputStream.cs +++ b/crypto/src/asn1/Asn1InputStream.cs @@ -426,7 +426,7 @@ namespace Org.BouncyCastle.Asn1 case Asn1Tags.BitString: return DerBitString.CreatePrimitive(bytes); case Asn1Tags.GeneralizedTime: - return DerGeneralizedTime.CreatePrimitive(bytes); + return Asn1GeneralizedTime.CreatePrimitive(bytes); case Asn1Tags.GeneralString: return DerGeneralString.CreatePrimitive(bytes); case Asn1Tags.GraphicString: @@ -452,7 +452,7 @@ namespace Org.BouncyCastle.Asn1 case Asn1Tags.UniversalString: return DerUniversalString.CreatePrimitive(bytes); case Asn1Tags.UtcTime: - return DerUtcTime.CreatePrimitive(bytes); + return Asn1UtcTime.CreatePrimitive(bytes); case Asn1Tags.Utf8String: return DerUtf8String.CreatePrimitive(bytes); case Asn1Tags.VideotexString: diff --git a/crypto/src/asn1/Asn1UniversalTypes.cs b/crypto/src/asn1/Asn1UniversalTypes.cs index 214918bcd..f5b5d0498 100644 --- a/crypto/src/asn1/Asn1UniversalTypes.cs +++ b/crypto/src/asn1/Asn1UniversalTypes.cs @@ -49,9 +49,9 @@ namespace Org.BouncyCastle.Asn1 case Asn1Tags.IA5String: // [UNIVERSAL 22] IMPLICIT OCTET STRING (encode as if) return DerIA5String.Meta.Instance; case Asn1Tags.UtcTime: // [UNIVERSAL 23] IMPLICIT VisibleString (restricted values) - return DerUtcTime.Meta.Instance; + return Asn1UtcTime.Meta.Instance; case Asn1Tags.GeneralizedTime: // [UNIVERSAL 24] IMPLICIT VisibleString (restricted values) - return DerGeneralizedTime.Meta.Instance; + return Asn1GeneralizedTime.Meta.Instance; case Asn1Tags.GraphicString: // [UNIVERSAL 25] IMPLICIT OCTET STRING (encode as if) return DerGraphicString.Meta.Instance; case Asn1Tags.VisibleString: // [UNIVERSAL 26] IMPLICIT OCTET STRING (encode as if) diff --git a/crypto/src/asn1/Asn1UtcTime.cs b/crypto/src/asn1/Asn1UtcTime.cs new file mode 100644 index 000000000..05de430c4 --- /dev/null +++ b/crypto/src/asn1/Asn1UtcTime.cs @@ -0,0 +1,264 @@ +using System; +using System.Globalization; +using System.IO; + +using Org.BouncyCastle.Utilities; + +namespace Org.BouncyCastle.Asn1 +{ + /** + * UTC time object. + */ + public class Asn1UtcTime + : Asn1Object + { + internal class Meta : Asn1UniversalType + { + internal static readonly Asn1UniversalType Instance = new Meta(); + + private Meta() : base(typeof(Asn1UtcTime), Asn1Tags.UtcTime) {} + + internal override Asn1Object FromImplicitPrimitive(DerOctetString octetString) + { + return CreatePrimitive(octetString.GetOctets()); + } + } + + /** + * return a UTC Time from the passed in object. + * + * @exception ArgumentException if the object cannot be converted. + */ + public static Asn1UtcTime GetInstance(object obj) + { + if (obj == null) + return null; + + if (obj is Asn1UtcTime asn1UtcTime) + return asn1UtcTime; + + if (obj is IAsn1Convertible asn1Convertible) + { + Asn1Object asn1Object = asn1Convertible.ToAsn1Object(); + if (asn1Object is Asn1UtcTime converted) + return converted; + } + else if (obj is byte[] bytes) + { + try + { + return (Asn1UtcTime)Meta.Instance.FromByteArray(bytes); + } + catch (IOException e) + { + throw new ArgumentException("failed to construct UTC time from byte[]: " + e.Message); + } + } + + throw new ArgumentException("illegal object in GetInstance: " + Platform.GetTypeName(obj)); + } + + /** + * return a UTC Time from a tagged object. + * + * @param taggedObject the tagged object holding the object we want + * @param declaredExplicit true if the object is meant to be explicitly tagged false otherwise. + * @exception ArgumentException if the tagged object cannot be converted. + */ + public static Asn1UtcTime GetInstance(Asn1TaggedObject taggedObject, bool declaredExplicit) + { + return (Asn1UtcTime)Meta.Instance.GetContextInstance(taggedObject, declaredExplicit); + } + + private readonly string time; + + /** + * The correct format for this is YYMMDDHHMMSSZ (it used to be that seconds were + * never encoded. When you're creating one of these objects from scratch, that's + * what you want to use, otherwise we'll try to deal with whatever Gets read from + * the input stream... (this is why the input format is different from the GetTime() + * method output). + * <p> + * @param time the time string.</p> + */ + public Asn1UtcTime(string time) + { + if (time == null) + throw new ArgumentNullException("time"); + + this.time = time; + + try + { + ToDateTime(); + } + catch (FormatException e) + { + throw new ArgumentException("invalid date string: " + e.Message); + } + } + + /** + * base constructor from a DateTime object + */ + public Asn1UtcTime(DateTime time) + { + this.time = time.ToUniversalTime().ToString("yyMMddHHmmss", CultureInfo.InvariantCulture) + "Z"; + } + + internal Asn1UtcTime(byte[] contents) + { + // + // explicitly convert to characters + // + this.time = Strings.FromAsciiByteArray(contents); + } + + /** + * return the time as a date based on whatever a 2 digit year will return. For + * standardised processing use ToAdjustedDateTime(). + * + * @return the resulting date + * @exception ParseException if the date string cannot be parsed. + */ + public DateTime ToDateTime() + { + return ParseDateString(TimeString, @"yyMMddHHmmss'GMT'zzz"); + } + + /** + * return the time as an adjusted date + * in the range of 1950 - 2049. + * + * @return a date in the range of 1950 to 2049. + * @exception ParseException if the date string cannot be parsed. + */ + public DateTime ToAdjustedDateTime() + { + return ParseDateString(AdjustedTimeString, @"yyyyMMddHHmmss'GMT'zzz"); + } + + private DateTime ParseDateString(string dateStr, string formatStr) + { + DateTime dt = DateTime.ParseExact( + dateStr, + formatStr, + DateTimeFormatInfo.InvariantInfo); + + return dt.ToUniversalTime(); + } + + /** + * return the time - always in the form of + * YYMMDDhhmmssGMT(+hh:mm|-hh:mm). + * <p> + * Normally in a certificate we would expect "Z" rather than "GMT", + * however adding the "GMT" means we can just use: + * <pre> + * dateF = new SimpleDateFormat("yyMMddHHmmssz"); + * </pre> + * To read in the time and Get a date which is compatible with our local + * time zone.</p> + * <p> + * <b>Note:</b> In some cases, due to the local date processing, this + * may lead to unexpected results. If you want to stick the normal + * convention of 1950 to 2049 use the GetAdjustedTime() method.</p> + */ + public string TimeString + { + get + { + // + // standardise the format. + // + if (time.IndexOf('-') < 0 && time.IndexOf('+') < 0) + { + if (time.Length == 11) + { + return time.Substring(0, 10) + "00GMT+00:00"; + } + else + { + return time.Substring(0, 12) + "GMT+00:00"; + } + } + else + { + int index = time.IndexOf('-'); + if (index < 0) + { + index = time.IndexOf('+'); + } + string d = time; + + if (index == time.Length - 3) + { + d += "00"; + } + + if (index == 10) + { + return d.Substring(0, 10) + "00GMT" + d.Substring(10, 3) + ":" + d.Substring(13, 2); + } + else + { + return d.Substring(0, 12) + "GMT" + d.Substring(12, 3) + ":" + d.Substring(15, 2); + } + } + } + } + + /// <summary> + /// Return a time string as an adjusted date with a 4 digit year. + /// This goes in the range of 1950 - 2049. + /// </summary> + public string AdjustedTimeString + { + get + { + string d = TimeString; + string c = d[0] < '5' ? "20" : "19"; + + return c + d; + } + } + + internal byte[] GetOctets() + { + return Strings.ToAsciiByteArray(time); + } + + internal override IAsn1Encoding GetEncoding(int encoding) + { + return new PrimitiveEncoding(Asn1Tags.Universal, Asn1Tags.UtcTime, GetOctets()); + } + + internal override IAsn1Encoding GetEncodingImplicit(int encoding, int tagClass, int tagNo) + { + return new PrimitiveEncoding(tagClass, tagNo, GetOctets()); + } + + protected override bool Asn1Equals(Asn1Object asn1Object) + { + if (!(asn1Object is Asn1UtcTime that)) + return false; + + return this.time == that.time; + } + + protected override int Asn1GetHashCode() + { + return time.GetHashCode(); + } + + public override string ToString() + { + return time; + } + + internal static Asn1UtcTime CreatePrimitive(byte[] contents) + { + return new Asn1UtcTime(contents); + } + } +} diff --git a/crypto/src/asn1/DerGeneralizedTime.cs b/crypto/src/asn1/DerGeneralizedTime.cs index 898a3d585..0386ecb02 100644 --- a/crypto/src/asn1/DerGeneralizedTime.cs +++ b/crypto/src/asn1/DerGeneralizedTime.cs @@ -1,367 +1,33 @@ using System; -using System.Globalization; -using System.IO; -using System.Text; - -using Org.BouncyCastle.Utilities; namespace Org.BouncyCastle.Asn1 { - /** - * Generalized time object. - */ public class DerGeneralizedTime - : Asn1Object + : Asn1GeneralizedTime { - internal class Meta : Asn1UniversalType - { - internal static readonly Asn1UniversalType Instance = new Meta(); - - private Meta() : base(typeof(DerGeneralizedTime), Asn1Tags.GeneralizedTime) {} - - internal override Asn1Object FromImplicitPrimitive(DerOctetString octetString) - { - return CreatePrimitive(octetString.GetOctets()); - } - } - - /** - * return a generalized time from the passed in object - * - * @exception ArgumentException if the object cannot be converted. - */ - public static DerGeneralizedTime GetInstance(object obj) - { - if (obj == null || obj is DerGeneralizedTime) - { - return (DerGeneralizedTime)obj; - } - else if (obj is IAsn1Convertible) - { - Asn1Object asn1Object = ((IAsn1Convertible)obj).ToAsn1Object(); - if (asn1Object is DerGeneralizedTime) - return (DerGeneralizedTime)asn1Object; - } - else if (obj is byte[]) - { - try - { - return (DerGeneralizedTime)Meta.Instance.FromByteArray((byte[])obj); - } - catch (IOException e) - { - throw new ArgumentException("failed to construct generalized time from byte[]: " + e.Message); - } - } - - throw new ArgumentException("illegal object in GetInstance: " + Platform.GetTypeName(obj), "obj"); - } - - /** - * return a generalized Time object from a tagged object. - * - * @param taggedObject the tagged object holding the object we want - * @param declaredExplicit true if the object is meant to be explicitly tagged false otherwise. - * @exception ArgumentException if the tagged object cannot be converted. - */ - public static DerGeneralizedTime GetInstance(Asn1TaggedObject taggedObject, bool declaredExplicit) + public DerGeneralizedTime(byte[] time) + : base(time) { - return (DerGeneralizedTime)Meta.Instance.GetContextInstance(taggedObject, declaredExplicit); } - private readonly string time; - - /** - * The correct format for this is YYYYMMDDHHMMSS[.f]Z, or without the Z - * for local time, or Z+-HHMM on the end, for difference between local - * time and UTC time. The fractional second amount f must consist of at - * least one number with trailing zeroes removed. - * - * @param time the time string. - * @exception ArgumentException if string is an illegal format. - */ - public DerGeneralizedTime( - string time) - { - this.time = time; - - try - { - ToDateTime(); - } - catch (FormatException e) - { - throw new ArgumentException("invalid date string: " + e.Message); - } - } - - /** - * base constructor from a local time object - */ public DerGeneralizedTime(DateTime time) + : base(time) { - this.time = time.ToUniversalTime().ToString(@"yyyyMMddHHmmss\Z"); - } - - internal DerGeneralizedTime( - byte[] bytes) - { - // - // explicitly convert to characters - // - this.time = Strings.FromAsciiByteArray(bytes); } - /** - * Return the time. - * @return The time string as it appeared in the encoded object. - */ - public string TimeString + public DerGeneralizedTime(string time) + : base(time) { - get { return time; } - } - - /** - * return the time - always in the form of - * YYYYMMDDhhmmssGMT(+hh:mm|-hh:mm). - * <p> - * Normally in a certificate we would expect "Z" rather than "GMT", - * however adding the "GMT" means we can just use: - * <pre> - * dateF = new SimpleDateFormat("yyyyMMddHHmmssz"); - * </pre> - * To read in the time and Get a date which is compatible with our local - * time zone.</p> - */ - public string GetTime() - { - // - // standardise the format. - // - if (time[time.Length - 1] == 'Z') - return time.Substring(0, time.Length - 1) + "GMT+00:00"; - - int signPos = time.Length - 5; - char sign = time[signPos]; - if (sign == '-' || sign == '+') - { - return time.Substring(0, signPos) - + "GMT" - + time.Substring(signPos, 3) - + ":" - + time.Substring(signPos + 3); - } - else - { - signPos = time.Length - 3; - sign = time[signPos]; - if (sign == '-' || sign == '+') - { - return time.Substring(0, signPos) - + "GMT" - + time.Substring(signPos) - + ":00"; - } - } - - return time + CalculateGmtOffset(); - } - - private string CalculateGmtOffset() - { - char sign = '+'; - DateTime time = ToDateTime(); - - TimeSpan offset = TimeZoneInfo.Local.GetUtcOffset(time); - if (offset.CompareTo(TimeSpan.Zero) < 0) - { - sign = '-'; - offset = offset.Duration(); - } - int hours = offset.Hours; - int minutes = offset.Minutes; - - return "GMT" + sign + Convert(hours) + ":" + Convert(minutes); - } - - private static string Convert( - int time) - { - if (time < 10) - { - return "0" + time; - } - - return time.ToString(); - } - - public DateTime ToDateTime() - { - string formatStr; - string d = time; - bool makeUniversal = false; - - if (Platform.EndsWith(d, "Z")) - { - if (HasFractionalSeconds) - { - int fCount = d.Length - d.IndexOf('.') - 2; - formatStr = @"yyyyMMddHHmmss." + FString(fCount) + @"\Z"; - } - else if (HasSeconds) - { - formatStr = @"yyyyMMddHHmmss\Z"; - } - else if (HasMinutes) - { - formatStr = @"yyyyMMddHHmm\Z"; - } - else - { - formatStr = @"yyyyMMddHH\Z"; - } - } - else if (time.IndexOf('-') > 0 || time.IndexOf('+') > 0) - { - d = GetTime(); - makeUniversal = true; - - if (HasFractionalSeconds) - { - int fCount = Platform.IndexOf(d, "GMT") - 1 - d.IndexOf('.'); - formatStr = @"yyyyMMddHHmmss." + FString(fCount) + @"'GMT'zzz"; - } - else - { - formatStr = @"yyyyMMddHHmmss'GMT'zzz"; - } - } - else - { - if (HasFractionalSeconds) - { - int fCount = d.Length - 1 - d.IndexOf('.'); - formatStr = @"yyyyMMddHHmmss." + FString(fCount); - } - else if (HasSeconds) - { - formatStr = @"yyyyMMddHHmmss"; - } - else if (HasMinutes) - { - formatStr = @"yyyyMMddHHmm"; - } - else - { - formatStr = @"yyyyMMddHH"; - } - - // TODO? -// dateF.setTimeZone(new SimpleTimeZone(0, TimeZone.getDefault().getID())); - } - - return ParseDateString(d, formatStr, makeUniversal); - } - - private string FString( - int count) - { - StringBuilder sb = new StringBuilder(); - for (int i = 0; i < count; ++i) - { - sb.Append('f'); - } - return sb.ToString(); - } - - private DateTime ParseDateString(string s, string format, bool makeUniversal) - { - /* - * NOTE: DateTime.Kind and DateTimeStyles.AssumeUniversal not available in .NET 1.1 - */ - DateTimeStyles style = DateTimeStyles.None; - if (Platform.EndsWith(format, "Z")) - { - style |= DateTimeStyles.AdjustToUniversal; - style |= DateTimeStyles.AssumeUniversal; - } - - DateTime dt = DateTime.ParseExact(s, format, DateTimeFormatInfo.InvariantInfo, style); - - return makeUniversal ? dt.ToUniversalTime() : dt; - } - - private bool HasFractionalSeconds - { - get { return time.IndexOf('.') == 14; } - } - - private bool HasSeconds => IsDigit(12) && IsDigit(13); - - private bool HasMinutes => IsDigit(10) && IsDigit(11); - - private bool IsDigit(int pos) - { - return time.Length > pos && char.IsDigit(time[pos]); - } - - private byte[] GetOctets(int encoding) - { - if (Asn1OutputStream.EncodingDer == encoding && time[time.Length - 1] == 'Z') - { - if (!HasMinutes) - return Strings.ToAsciiByteArray(time.Insert(time.Length - 1, "0000")); - if (!HasSeconds) - return Strings.ToAsciiByteArray(time.Insert(time.Length - 1, "00")); - - if (HasFractionalSeconds) - { - int ind = time.Length - 2; - while (ind > 0 && time[ind] == '0') - { - --ind; - } - - if (time[ind] != '.') - { - ++ind; - } - - if (ind != time.Length - 1) - { - return Strings.ToAsciiByteArray(time.Remove(ind, time.Length - 1 - ind)); - } - } - } - - return Strings.ToAsciiByteArray(time); } internal override IAsn1Encoding GetEncoding(int encoding) { - return new PrimitiveEncoding(Asn1Tags.Universal, Asn1Tags.GeneralizedTime, GetOctets(encoding)); + return new PrimitiveEncoding(Asn1Tags.Universal, Asn1Tags.GeneralizedTime, GetDerTime()); } internal override IAsn1Encoding GetEncodingImplicit(int encoding, int tagClass, int tagNo) { - return new PrimitiveEncoding(tagClass, tagNo, GetOctets(encoding)); - } - - protected override bool Asn1Equals(Asn1Object asn1Object) - { - DerGeneralizedTime that = asn1Object as DerGeneralizedTime; - return null != that - && this.time.Equals(that.time); - } - - protected override int Asn1GetHashCode() - { - return time.GetHashCode(); - } - - internal static DerGeneralizedTime CreatePrimitive(byte[] contents) - { - return new DerGeneralizedTime(contents); + return new PrimitiveEncoding(tagClass, tagNo, GetDerTime()); } } } diff --git a/crypto/src/asn1/DerUTCTime.cs b/crypto/src/asn1/DerUTCTime.cs index 7f7756d49..089285367 100644 --- a/crypto/src/asn1/DerUTCTime.cs +++ b/crypto/src/asn1/DerUTCTime.cs @@ -1,274 +1,25 @@ using System; -using System.Globalization; -using System.IO; - -using Org.BouncyCastle.Utilities; namespace Org.BouncyCastle.Asn1 { - /** - * UTC time object. - */ public class DerUtcTime - : Asn1Object + : Asn1UtcTime { - internal class Meta : Asn1UniversalType - { - internal static readonly Asn1UniversalType Instance = new Meta(); - - private Meta() : base(typeof(DerUtcTime), Asn1Tags.UtcTime) {} - - internal override Asn1Object FromImplicitPrimitive(DerOctetString octetString) - { - return CreatePrimitive(octetString.GetOctets()); - } - } - - /** - * return a UTC Time from the passed in object. - * - * @exception ArgumentException if the object cannot be converted. - */ - public static DerUtcTime GetInstance(object obj) - { - if (obj == null || obj is DerUtcTime) - { - return (DerUtcTime)obj; - } - else if (obj is IAsn1Convertible) - { - Asn1Object asn1Object = ((IAsn1Convertible)obj).ToAsn1Object(); - if (asn1Object is DerUtcTime) - return (DerUtcTime)asn1Object; - } - else if (obj is byte[]) - { - try - { - return (DerUtcTime)Meta.Instance.FromByteArray((byte[])obj); - } - catch (IOException e) - { - throw new ArgumentException("failed to construct UTC time from byte[]: " + e.Message); - } - } - - throw new ArgumentException("illegal object in GetInstance: " + Platform.GetTypeName(obj)); - } - - /** - * return a UTC Time from a tagged object. - * - * @param taggedObject the tagged object holding the object we want - * @param declaredExplicit true if the object is meant to be explicitly tagged false otherwise. - * @exception ArgumentException if the tagged object cannot be converted. - */ - public static DerUtcTime GetInstance(Asn1TaggedObject taggedObject, bool declaredExplicit) - { - return (DerUtcTime)Meta.Instance.GetContextInstance(taggedObject, declaredExplicit); - } - - private readonly string time; - - /** - * The correct format for this is YYMMDDHHMMSSZ (it used to be that seconds were - * never encoded. When you're creating one of these objects from scratch, that's - * what you want to use, otherwise we'll try to deal with whatever Gets read from - * the input stream... (this is why the input format is different from the GetTime() - * method output). - * <p> - * @param time the time string.</p> - */ public DerUtcTime(string time) + : base(time) { - if (time == null) - throw new ArgumentNullException("time"); - - this.time = time; - - try - { - ToDateTime(); - } - catch (FormatException e) - { - throw new ArgumentException("invalid date string: " + e.Message); - } } - /** - * base constructor from a DateTime object - */ public DerUtcTime(DateTime time) + : base(time) { - this.time = time.ToUniversalTime().ToString("yyMMddHHmmss", CultureInfo.InvariantCulture) + "Z"; } internal DerUtcTime(byte[] contents) + : base(contents) { - // - // explicitly convert to characters - // - this.time = Strings.FromAsciiByteArray(contents); - } - -// public DateTime ToDateTime() -// { -// string tm = this.AdjustedTimeString; -// -// return new DateTime( -// Int16.Parse(tm.Substring(0, 4)), -// Int16.Parse(tm.Substring(4, 2)), -// Int16.Parse(tm.Substring(6, 2)), -// Int16.Parse(tm.Substring(8, 2)), -// Int16.Parse(tm.Substring(10, 2)), -// Int16.Parse(tm.Substring(12, 2))); -// } - - /** - * return the time as a date based on whatever a 2 digit year will return. For - * standardised processing use ToAdjustedDateTime(). - * - * @return the resulting date - * @exception ParseException if the date string cannot be parsed. - */ - public DateTime ToDateTime() - { - return ParseDateString(TimeString, @"yyMMddHHmmss'GMT'zzz"); - } - - /** - * return the time as an adjusted date - * in the range of 1950 - 2049. - * - * @return a date in the range of 1950 to 2049. - * @exception ParseException if the date string cannot be parsed. - */ - public DateTime ToAdjustedDateTime() - { - return ParseDateString(AdjustedTimeString, @"yyyyMMddHHmmss'GMT'zzz"); - } - - private DateTime ParseDateString(string dateStr, string formatStr) - { - DateTime dt = DateTime.ParseExact( - dateStr, - formatStr, - DateTimeFormatInfo.InvariantInfo); - - return dt.ToUniversalTime(); - } - - /** - * return the time - always in the form of - * YYMMDDhhmmssGMT(+hh:mm|-hh:mm). - * <p> - * Normally in a certificate we would expect "Z" rather than "GMT", - * however adding the "GMT" means we can just use: - * <pre> - * dateF = new SimpleDateFormat("yyMMddHHmmssz"); - * </pre> - * To read in the time and Get a date which is compatible with our local - * time zone.</p> - * <p> - * <b>Note:</b> In some cases, due to the local date processing, this - * may lead to unexpected results. If you want to stick the normal - * convention of 1950 to 2049 use the GetAdjustedTime() method.</p> - */ - public string TimeString - { - get - { - // - // standardise the format. - // - if (time.IndexOf('-') < 0 && time.IndexOf('+') < 0) - { - if (time.Length == 11) - { - return time.Substring(0, 10) + "00GMT+00:00"; - } - else - { - return time.Substring(0, 12) + "GMT+00:00"; - } - } - else - { - int index = time.IndexOf('-'); - if (index < 0) - { - index = time.IndexOf('+'); - } - string d = time; - - if (index == time.Length - 3) - { - d += "00"; - } - - if (index == 10) - { - return d.Substring(0, 10) + "00GMT" + d.Substring(10, 3) + ":" + d.Substring(13, 2); - } - else - { - return d.Substring(0, 12) + "GMT" + d.Substring(12, 3) + ":" + d.Substring(15, 2); - } - } - } - } - - /// <summary> - /// Return a time string as an adjusted date with a 4 digit year. - /// This goes in the range of 1950 - 2049. - /// </summary> - public string AdjustedTimeString - { - get - { - string d = TimeString; - string c = d[0] < '5' ? "20" : "19"; - - return c + d; - } - } - - private byte[] GetOctets() - { - return Strings.ToAsciiByteArray(time); - } - - internal override IAsn1Encoding GetEncoding(int encoding) - { - return new PrimitiveEncoding(Asn1Tags.Universal, Asn1Tags.UtcTime, GetOctets()); - } - - internal override IAsn1Encoding GetEncodingImplicit(int encoding, int tagClass, int tagNo) - { - return new PrimitiveEncoding(tagClass, tagNo, GetOctets()); - } - - protected override bool Asn1Equals(Asn1Object asn1Object) - { - DerUtcTime that = asn1Object as DerUtcTime; - return null != that - && this.time.Equals(that.time); } - protected override int Asn1GetHashCode() - { - return time.GetHashCode(); - } - - public override string ToString() - { - return time; - } - - internal static DerUtcTime CreatePrimitive(byte[] contents) - { - return new DerUtcTime(contents); - } + // TODO: create proper DER encoding. } } diff --git a/crypto/src/asn1/bc/BCObjectIdentifiers.cs b/crypto/src/asn1/bc/BCObjectIdentifiers.cs index f3933af87..d526980e5 100644 --- a/crypto/src/asn1/bc/BCObjectIdentifiers.cs +++ b/crypto/src/asn1/bc/BCObjectIdentifiers.cs @@ -192,7 +192,16 @@ namespace Org.BouncyCastle.Asn1.BC public static readonly DerObjectIdentifier lightsaberkem256r3 = pqc_kem_saber.Branch("7"); public static readonly DerObjectIdentifier saberkem256r3 = pqc_kem_saber.Branch("8"); public static readonly DerObjectIdentifier firesaberkem256r3 = pqc_kem_saber.Branch("9"); - + public static readonly DerObjectIdentifier ulightsaberkemr3 = pqc_kem_saber.Branch("10"); + public static readonly DerObjectIdentifier usaberkemr3 = pqc_kem_saber.Branch("11"); + public static readonly DerObjectIdentifier ufiresaberkemr3 = pqc_kem_saber.Branch("12"); + public static readonly DerObjectIdentifier lightsaberkem90sr3 = pqc_kem_saber.Branch("13"); + public static readonly DerObjectIdentifier saberkem90sr3 = pqc_kem_saber.Branch("14"); + public static readonly DerObjectIdentifier firesaberkem90sr3 = pqc_kem_saber.Branch("15"); + public static readonly DerObjectIdentifier ulightsaberkem90sr3 = pqc_kem_saber.Branch("16"); + public static readonly DerObjectIdentifier usaberkem90sr3 = pqc_kem_saber.Branch("17"); + public static readonly DerObjectIdentifier ufiresaberkem90sr3 = pqc_kem_saber.Branch("18"); + /** * SIKE */ diff --git a/crypto/src/asn1/cmp/PKIHeader.cs b/crypto/src/asn1/cmp/PKIHeader.cs index 553a81bc0..7ed914e6a 100644 --- a/crypto/src/asn1/cmp/PKIHeader.cs +++ b/crypto/src/asn1/cmp/PKIHeader.cs @@ -19,7 +19,7 @@ namespace Org.BouncyCastle.Asn1.Cmp private readonly DerInteger pvno; private readonly GeneralName sender; private readonly GeneralName recipient; - private readonly DerGeneralizedTime messageTime; + private readonly Asn1GeneralizedTime messageTime; private readonly AlgorithmIdentifier protectionAlg; private readonly Asn1OctetString senderKID; // KeyIdentifier private readonly Asn1OctetString recipKID; // KeyIdentifier @@ -42,7 +42,7 @@ namespace Org.BouncyCastle.Asn1.Cmp switch (tObj.TagNo) { case 0: - messageTime = DerGeneralizedTime.GetInstance(tObj, true); + messageTime = Asn1GeneralizedTime.GetInstance(tObj, true); break; case 1: protectionAlg = AlgorithmIdentifier.GetInstance(tObj, true); @@ -118,7 +118,7 @@ namespace Org.BouncyCastle.Asn1.Cmp get { return recipient; } } - public virtual DerGeneralizedTime MessageTime + public virtual Asn1GeneralizedTime MessageTime { get { return messageTime; } } diff --git a/crypto/src/asn1/cmp/PKIHeaderBuilder.cs b/crypto/src/asn1/cmp/PKIHeaderBuilder.cs index d771dda4c..cbefc73b8 100644 --- a/crypto/src/asn1/cmp/PKIHeaderBuilder.cs +++ b/crypto/src/asn1/cmp/PKIHeaderBuilder.cs @@ -9,7 +9,7 @@ namespace Org.BouncyCastle.Asn1.Cmp private DerInteger pvno; private GeneralName sender; private GeneralName recipient; - private DerGeneralizedTime messageTime; + private Asn1GeneralizedTime messageTime; private AlgorithmIdentifier protectionAlg; private Asn1OctetString senderKID; // KeyIdentifier private Asn1OctetString recipKID; // KeyIdentifier @@ -37,7 +37,7 @@ namespace Org.BouncyCastle.Asn1.Cmp this.recipient = recipient; } - public virtual PkiHeaderBuilder SetMessageTime(DerGeneralizedTime time) + public virtual PkiHeaderBuilder SetMessageTime(Asn1GeneralizedTime time) { messageTime = time; return this; diff --git a/crypto/src/asn1/cmp/RevAnnContent.cs b/crypto/src/asn1/cmp/RevAnnContent.cs index 4ef6fdbf7..cdd26c39f 100644 --- a/crypto/src/asn1/cmp/RevAnnContent.cs +++ b/crypto/src/asn1/cmp/RevAnnContent.cs @@ -19,18 +19,18 @@ namespace Org.BouncyCastle.Asn1.Cmp private readonly PkiStatusEncodable m_status; private readonly CertId m_certID; - private readonly DerGeneralizedTime m_willBeRevokedAt; - private readonly DerGeneralizedTime m_badSinceDate; + private readonly Asn1GeneralizedTime m_willBeRevokedAt; + private readonly Asn1GeneralizedTime m_badSinceDate; private readonly X509Extensions m_crlDetails; - public RevAnnContent(PkiStatusEncodable status, CertId certID, DerGeneralizedTime willBeRevokedAt, - DerGeneralizedTime badSinceDate) + public RevAnnContent(PkiStatusEncodable status, CertId certID, Asn1GeneralizedTime willBeRevokedAt, + Asn1GeneralizedTime badSinceDate) : this(status, certID, willBeRevokedAt, badSinceDate, null) { } - public RevAnnContent(PkiStatusEncodable status, CertId certID, DerGeneralizedTime willBeRevokedAt, - DerGeneralizedTime badSinceDate, X509Extensions crlDetails) + public RevAnnContent(PkiStatusEncodable status, CertId certID, Asn1GeneralizedTime willBeRevokedAt, + Asn1GeneralizedTime badSinceDate, X509Extensions crlDetails) { m_status = status; m_certID = certID; @@ -43,8 +43,8 @@ namespace Org.BouncyCastle.Asn1.Cmp { m_status = PkiStatusEncodable.GetInstance(seq[0]); m_certID = CertId.GetInstance(seq[1]); - m_willBeRevokedAt = DerGeneralizedTime.GetInstance(seq[2]); - m_badSinceDate = DerGeneralizedTime.GetInstance(seq[3]); + m_willBeRevokedAt = Asn1GeneralizedTime.GetInstance(seq[2]); + m_badSinceDate = Asn1GeneralizedTime.GetInstance(seq[3]); if (seq.Count > 4) { @@ -56,9 +56,9 @@ namespace Org.BouncyCastle.Asn1.Cmp public virtual CertId CertID => m_certID; - public virtual DerGeneralizedTime WillBeRevokedAt => m_willBeRevokedAt; + public virtual Asn1GeneralizedTime WillBeRevokedAt => m_willBeRevokedAt; - public virtual DerGeneralizedTime BadSinceDate => m_badSinceDate; + public virtual Asn1GeneralizedTime BadSinceDate => m_badSinceDate; public virtual X509Extensions CrlDetails => m_crlDetails; diff --git a/crypto/src/asn1/cms/CMSObjectIdentifiers.cs b/crypto/src/asn1/cms/CMSObjectIdentifiers.cs index 2ad0a3c7c..6f4f8fb5e 100644 --- a/crypto/src/asn1/cms/CMSObjectIdentifiers.cs +++ b/crypto/src/asn1/cms/CMSObjectIdentifiers.cs @@ -13,7 +13,8 @@ namespace Org.BouncyCastle.Asn1.Cms public static readonly DerObjectIdentifier AuthenticatedData = PkcsObjectIdentifiers.IdCTAuthData; public static readonly DerObjectIdentifier CompressedData = PkcsObjectIdentifiers.IdCTCompressedData; public static readonly DerObjectIdentifier AuthEnvelopedData = PkcsObjectIdentifiers.IdCTAuthEnvelopedData; - public static readonly DerObjectIdentifier timestampedData = PkcsObjectIdentifiers.IdCTTimestampedData; + public static readonly DerObjectIdentifier TimestampedData = PkcsObjectIdentifiers.IdCTTimestampedData; + public static readonly DerObjectIdentifier ZlibCompress = PkcsObjectIdentifiers.IdAlgZlibCompress; /** * The other Revocation Info arc diff --git a/crypto/src/asn1/cms/KEKIdentifier.cs b/crypto/src/asn1/cms/KEKIdentifier.cs index a42217440..36ab94f52 100644 --- a/crypto/src/asn1/cms/KEKIdentifier.cs +++ b/crypto/src/asn1/cms/KEKIdentifier.cs @@ -8,12 +8,12 @@ namespace Org.BouncyCastle.Asn1.Cms : Asn1Encodable { private Asn1OctetString keyIdentifier; - private DerGeneralizedTime date; + private Asn1GeneralizedTime date; private OtherKeyAttribute other; public KekIdentifier( byte[] keyIdentifier, - DerGeneralizedTime date, + Asn1GeneralizedTime date, OtherKeyAttribute other) { this.keyIdentifier = new DerOctetString(keyIdentifier); @@ -31,9 +31,9 @@ namespace Org.BouncyCastle.Asn1.Cms case 1: break; case 2: - if (seq[1] is DerGeneralizedTime) + if (seq[1] is Asn1GeneralizedTime) { - date = (DerGeneralizedTime) seq[1]; + date = (Asn1GeneralizedTime) seq[1]; } else { @@ -41,7 +41,7 @@ namespace Org.BouncyCastle.Asn1.Cms } break; case 3: - date = (DerGeneralizedTime) seq[1]; + date = (Asn1GeneralizedTime) seq[1]; other = OtherKeyAttribute.GetInstance(seq[2]); break; default: @@ -88,7 +88,7 @@ namespace Org.BouncyCastle.Asn1.Cms get { return keyIdentifier; } } - public DerGeneralizedTime Date + public Asn1GeneralizedTime Date { get { return date; } } diff --git a/crypto/src/asn1/cms/RecipientKeyIdentifier.cs b/crypto/src/asn1/cms/RecipientKeyIdentifier.cs index 995ddab51..dea9ce09d 100644 --- a/crypto/src/asn1/cms/RecipientKeyIdentifier.cs +++ b/crypto/src/asn1/cms/RecipientKeyIdentifier.cs @@ -8,12 +8,12 @@ namespace Org.BouncyCastle.Asn1.Cms : Asn1Encodable { private Asn1OctetString subjectKeyIdentifier; - private DerGeneralizedTime date; + private Asn1GeneralizedTime date; private OtherKeyAttribute other; public RecipientKeyIdentifier( Asn1OctetString subjectKeyIdentifier, - DerGeneralizedTime date, + Asn1GeneralizedTime date, OtherKeyAttribute other) { this.subjectKeyIdentifier = subjectKeyIdentifier; @@ -29,7 +29,7 @@ namespace Org.BouncyCastle.Asn1.Cms public RecipientKeyIdentifier( byte[] subjectKeyIdentifier, - DerGeneralizedTime date, + Asn1GeneralizedTime date, OtherKeyAttribute other) { this.subjectKeyIdentifier = new DerOctetString(subjectKeyIdentifier); @@ -48,9 +48,9 @@ namespace Org.BouncyCastle.Asn1.Cms case 1: break; case 2: - if (seq[1] is DerGeneralizedTime) + if (seq[1] is Asn1GeneralizedTime) { - date = (DerGeneralizedTime) seq[1]; + date = (Asn1GeneralizedTime)seq[1]; } else { @@ -58,7 +58,7 @@ namespace Org.BouncyCastle.Asn1.Cms } break; case 3: - date = (DerGeneralizedTime) seq[1]; + date = (Asn1GeneralizedTime)seq[1]; other = OtherKeyAttribute.GetInstance(seq[2]); break; default: @@ -105,7 +105,7 @@ namespace Org.BouncyCastle.Asn1.Cms get { return subjectKeyIdentifier; } } - public DerGeneralizedTime Date + public Asn1GeneralizedTime Date { get { return date; } } diff --git a/crypto/src/asn1/cms/Time.cs b/crypto/src/asn1/cms/Time.cs index 52fb4f937..89f1e4dae 100644 --- a/crypto/src/asn1/cms/Time.cs +++ b/crypto/src/asn1/cms/Time.cs @@ -22,7 +22,7 @@ namespace Org.BouncyCastle.Asn1.Cms { if (time == null) throw new ArgumentNullException("time"); - if (!(time is DerUtcTime) && !(time is DerGeneralizedTime)) + if (!(time is Asn1UtcTime) && !(time is Asn1GeneralizedTime)) throw new ArgumentException("unknown object passed to Time"); this.time = time; @@ -33,8 +33,7 @@ namespace Org.BouncyCastle.Asn1.Cms * and 2049 a UTCTime object is Generated, otherwise a GeneralizedTime * is used. */ - public Time( - DateTime date) + public Time(DateTime date) { string d = date.ToString("yyyyMMddHHmmss", CultureInfo.InvariantCulture) + "Z"; @@ -50,15 +49,16 @@ namespace Org.BouncyCastle.Asn1.Cms } } - public static Time GetInstance( - object obj) + public static Time GetInstance(object obj) { - if (obj == null || obj is Time) - return (Time)obj; - if (obj is DerUtcTime) - return new Time((DerUtcTime)obj); - if (obj is DerGeneralizedTime) - return new Time((DerGeneralizedTime)obj); + if (obj == null) + return null; + if (obj is Time time) + return time; + if (obj is Asn1UtcTime utcTime) + return new Time(utcTime); + if (obj is Asn1GeneralizedTime generalizedTime) + return new Time(generalizedTime); throw new ArgumentException("unknown object in factory: " + Platform.GetTypeName(obj), "obj"); } @@ -67,14 +67,10 @@ namespace Org.BouncyCastle.Asn1.Cms { get { - if (time is DerUtcTime) - { - return ((DerUtcTime)time).AdjustedTimeString; - } - else - { - return ((DerGeneralizedTime)time).GetTime(); - } + if (time is Asn1UtcTime utcTime) + return utcTime.AdjustedTimeString; + + return ((Asn1GeneralizedTime)time).GetTime(); } } @@ -84,12 +80,10 @@ namespace Org.BouncyCastle.Asn1.Cms { try { - if (time is DerUtcTime) - { - return ((DerUtcTime)time).ToAdjustedDateTime(); - } + if (time is Asn1UtcTime utcTime) + return utcTime.ToAdjustedDateTime(); - return ((DerGeneralizedTime)time).ToDateTime(); + return ((Asn1GeneralizedTime)time).ToDateTime(); } catch (FormatException e) { diff --git a/crypto/src/asn1/esf/CrlIdentifier.cs b/crypto/src/asn1/esf/CrlIdentifier.cs index a8e40c870..44c99170c 100644 --- a/crypto/src/asn1/esf/CrlIdentifier.cs +++ b/crypto/src/asn1/esf/CrlIdentifier.cs @@ -1,5 +1,5 @@ using System; - +using System.Reflection; using Org.BouncyCastle.Asn1.X509; using Org.BouncyCastle.Math; using Org.BouncyCastle.Utilities; @@ -21,7 +21,7 @@ namespace Org.BouncyCastle.Asn1.Esf : Asn1Encodable { private readonly X509Name crlIssuer; - private readonly DerUtcTime crlIssuedTime; + private readonly Asn1UtcTime crlIssuedTime; private readonly DerInteger crlNumber; public static CrlIdentifier GetInstance( @@ -48,7 +48,7 @@ namespace Org.BouncyCastle.Asn1.Esf throw new ArgumentException("Bad sequence size: " + seq.Count, "seq"); this.crlIssuer = X509Name.GetInstance(seq[0]); - this.crlIssuedTime = DerUtcTime.GetInstance(seq[1]); + this.crlIssuedTime = Asn1UtcTime.GetInstance(seq[1]); if (seq.Count > 2) { @@ -56,31 +56,36 @@ namespace Org.BouncyCastle.Asn1.Esf } } - public CrlIdentifier( - X509Name crlIssuer, - DateTime crlIssuedTime) - : this(crlIssuer, crlIssuedTime, null) + public CrlIdentifier(X509Name crlIssuer, DateTime crlIssuedTime) + : this(crlIssuer, crlIssuedTime, null) { } - public CrlIdentifier( - X509Name crlIssuer, - DateTime crlIssuedTime, - BigInteger crlNumber) + public CrlIdentifier(X509Name crlIssuer, DateTime crlIssuedTime, BigInteger crlNumber) + : this(crlIssuer, new Asn1UtcTime(crlIssuedTime), crlNumber) { - if (crlIssuer == null) - throw new ArgumentNullException("crlIssuer"); + } - this.crlIssuer = crlIssuer; - this.crlIssuedTime = new DerUtcTime(crlIssuedTime); + public CrlIdentifier(X509Name crlIssuer, Asn1UtcTime crlIssuedTime) + : this(crlIssuer, crlIssuedTime, null) + { + } - if (crlNumber != null) - { - this.crlNumber = new DerInteger(crlNumber); - } - } + public CrlIdentifier(X509Name crlIssuer, Asn1UtcTime crlIssuedTime, BigInteger crlNumber) + { + if (crlIssuer == null) + throw new ArgumentNullException(nameof(crlIssuer)); + + this.crlIssuer = crlIssuer; + this.crlIssuedTime = crlIssuedTime; + + if (null != crlNumber) + { + this.crlNumber = new DerInteger(crlNumber); + } + } - public X509Name CrlIssuer + public X509Name CrlIssuer { get { return crlIssuer; } } diff --git a/crypto/src/asn1/esf/OcspIdentifier.cs b/crypto/src/asn1/esf/OcspIdentifier.cs index e65f1cfe7..fa7069aed 100644 --- a/crypto/src/asn1/esf/OcspIdentifier.cs +++ b/crypto/src/asn1/esf/OcspIdentifier.cs @@ -20,7 +20,7 @@ namespace Org.BouncyCastle.Asn1.Esf : Asn1Encodable { private readonly ResponderID ocspResponderID; - private readonly DerGeneralizedTime producedAt; + private readonly Asn1GeneralizedTime producedAt; public static OcspIdentifier GetInstance( object obj) @@ -46,21 +46,30 @@ namespace Org.BouncyCastle.Asn1.Esf throw new ArgumentException("Bad sequence size: " + seq.Count, "seq"); this.ocspResponderID = ResponderID.GetInstance(seq[0].ToAsn1Object()); - this.producedAt = (DerGeneralizedTime) seq[1].ToAsn1Object(); + this.producedAt = (Asn1GeneralizedTime)seq[1].ToAsn1Object(); } - public OcspIdentifier( - ResponderID ocspResponderID, - DateTime producedAt) + public OcspIdentifier(ResponderID ocspResponderID, DateTime producedAt) { if (ocspResponderID == null) - throw new ArgumentNullException(); + throw new ArgumentNullException(nameof(ocspResponderID)); this.ocspResponderID = ocspResponderID; - this.producedAt = new DerGeneralizedTime(producedAt); + this.producedAt = new Asn1GeneralizedTime(producedAt); } - public ResponderID OcspResponderID + public OcspIdentifier(ResponderID ocspResponderID, Asn1GeneralizedTime producedAt) + { + if (ocspResponderID == null) + throw new ArgumentNullException(nameof(ocspResponderID)); + if (producedAt == null) + throw new ArgumentNullException(nameof(producedAt)); + + this.ocspResponderID = ocspResponderID; + this.producedAt = producedAt; + } + + public ResponderID OcspResponderID { get { return ocspResponderID; } } diff --git a/crypto/src/asn1/isismtt/x509/DeclarationOfMajority.cs b/crypto/src/asn1/isismtt/x509/DeclarationOfMajority.cs index b82c9373d..c9c96cbda 100644 --- a/crypto/src/asn1/isismtt/x509/DeclarationOfMajority.cs +++ b/crypto/src/asn1/isismtt/x509/DeclarationOfMajority.cs @@ -64,7 +64,7 @@ namespace Org.BouncyCastle.Asn1.IsisMtt.X509 } public DeclarationOfMajority( - DerGeneralizedTime dateOfBirth) + Asn1GeneralizedTime dateOfBirth) { this.declaration = new DerTaggedObject(false, 2, dateOfBirth); } @@ -155,14 +155,14 @@ namespace Org.BouncyCastle.Asn1.IsisMtt.X509 } } - public virtual DerGeneralizedTime DateOfBirth + public virtual Asn1GeneralizedTime DateOfBirth { get { switch ((Choice) declaration.TagNo) { case Choice.DateOfBirth: - return DerGeneralizedTime.GetInstance(declaration, false); + return Asn1GeneralizedTime.GetInstance(declaration, false); default: return null; } diff --git a/crypto/src/asn1/ocsp/CrlID.cs b/crypto/src/asn1/ocsp/CrlID.cs index fc1e59d22..24dda4edf 100644 --- a/crypto/src/asn1/ocsp/CrlID.cs +++ b/crypto/src/asn1/ocsp/CrlID.cs @@ -7,7 +7,7 @@ namespace Org.BouncyCastle.Asn1.Ocsp { private readonly DerIA5String crlUrl; private readonly DerInteger crlNum; - private readonly DerGeneralizedTime crlTime; + private readonly Asn1GeneralizedTime crlTime; // TODO Add GetInstance method(s) and make this private? public CrlID(Asn1Sequence seq) @@ -23,7 +23,7 @@ namespace Org.BouncyCastle.Asn1.Ocsp crlNum = DerInteger.GetInstance(o, true); break; case 2: - crlTime = DerGeneralizedTime.GetInstance(o, true); + crlTime = Asn1GeneralizedTime.GetInstance(o, true); break; default: throw new ArgumentException("unknown tag number: " + o.TagNo); @@ -41,7 +41,7 @@ namespace Org.BouncyCastle.Asn1.Ocsp get { return crlNum; } } - public DerGeneralizedTime CrlTime + public Asn1GeneralizedTime CrlTime { get { return crlTime; } } diff --git a/crypto/src/asn1/ocsp/ResponseData.cs b/crypto/src/asn1/ocsp/ResponseData.cs index a5769c0fa..dfb234bc1 100644 --- a/crypto/src/asn1/ocsp/ResponseData.cs +++ b/crypto/src/asn1/ocsp/ResponseData.cs @@ -13,7 +13,7 @@ namespace Org.BouncyCastle.Asn1.Ocsp private readonly bool versionPresent; private readonly DerInteger version; private readonly ResponderID responderID; - private readonly DerGeneralizedTime producedAt; + private readonly Asn1GeneralizedTime producedAt; private readonly Asn1Sequence responses; private readonly X509Extensions responseExtensions; @@ -43,7 +43,7 @@ namespace Org.BouncyCastle.Asn1.Ocsp public ResponseData( DerInteger version, ResponderID responderID, - DerGeneralizedTime producedAt, + Asn1GeneralizedTime producedAt, Asn1Sequence responses, X509Extensions responseExtensions) { @@ -56,7 +56,7 @@ namespace Org.BouncyCastle.Asn1.Ocsp public ResponseData( ResponderID responderID, - DerGeneralizedTime producedAt, + Asn1GeneralizedTime producedAt, Asn1Sequence responses, X509Extensions responseExtensions) : this(V1, responderID, producedAt, responses, responseExtensions) @@ -90,7 +90,7 @@ namespace Org.BouncyCastle.Asn1.Ocsp } this.responderID = ResponderID.GetInstance(seq[index++]); - this.producedAt = (DerGeneralizedTime)seq[index++]; + this.producedAt = (Asn1GeneralizedTime)seq[index++]; this.responses = (Asn1Sequence)seq[index++]; if (seq.Count > index) @@ -110,7 +110,7 @@ namespace Org.BouncyCastle.Asn1.Ocsp get { return responderID; } } - public DerGeneralizedTime ProducedAt + public Asn1GeneralizedTime ProducedAt { get { return producedAt; } } diff --git a/crypto/src/asn1/ocsp/RevokedInfo.cs b/crypto/src/asn1/ocsp/RevokedInfo.cs index c67be0678..e6438dd08 100644 --- a/crypto/src/asn1/ocsp/RevokedInfo.cs +++ b/crypto/src/asn1/ocsp/RevokedInfo.cs @@ -8,7 +8,7 @@ namespace Org.BouncyCastle.Asn1.Ocsp public class RevokedInfo : Asn1Encodable { - private readonly DerGeneralizedTime revocationTime; + private readonly Asn1GeneralizedTime revocationTime; private readonly CrlReason revocationReason; public static RevokedInfo GetInstance( @@ -35,13 +35,13 @@ namespace Org.BouncyCastle.Asn1.Ocsp } public RevokedInfo( - DerGeneralizedTime revocationTime) + Asn1GeneralizedTime revocationTime) : this(revocationTime, null) { } public RevokedInfo( - DerGeneralizedTime revocationTime, + Asn1GeneralizedTime revocationTime, CrlReason revocationReason) { if (revocationTime == null) @@ -54,7 +54,7 @@ namespace Org.BouncyCastle.Asn1.Ocsp private RevokedInfo( Asn1Sequence seq) { - this.revocationTime = (DerGeneralizedTime) seq[0]; + this.revocationTime = (Asn1GeneralizedTime)seq[0]; if (seq.Count > 1) { @@ -63,7 +63,7 @@ namespace Org.BouncyCastle.Asn1.Ocsp } } - public DerGeneralizedTime RevocationTime + public Asn1GeneralizedTime RevocationTime { get { return revocationTime; } } diff --git a/crypto/src/asn1/ocsp/SingleResponse.cs b/crypto/src/asn1/ocsp/SingleResponse.cs index ecdf3dab0..42b451af7 100644 --- a/crypto/src/asn1/ocsp/SingleResponse.cs +++ b/crypto/src/asn1/ocsp/SingleResponse.cs @@ -10,15 +10,15 @@ namespace Org.BouncyCastle.Asn1.Ocsp { private readonly CertID certID; private readonly CertStatus certStatus; - private readonly DerGeneralizedTime thisUpdate; - private readonly DerGeneralizedTime nextUpdate; + private readonly Asn1GeneralizedTime thisUpdate; + private readonly Asn1GeneralizedTime nextUpdate; private readonly X509Extensions singleExtensions; public SingleResponse( CertID certID, CertStatus certStatus, - DerGeneralizedTime thisUpdate, - DerGeneralizedTime nextUpdate, + Asn1GeneralizedTime thisUpdate, + Asn1GeneralizedTime nextUpdate, X509Extensions singleExtensions) { this.certID = certID; @@ -33,11 +33,11 @@ namespace Org.BouncyCastle.Asn1.Ocsp { this.certID = CertID.GetInstance(seq[0]); this.certStatus = CertStatus.GetInstance(seq[1]); - this.thisUpdate = (DerGeneralizedTime)seq[2]; + this.thisUpdate = (Asn1GeneralizedTime)seq[2]; if (seq.Count > 4) { - this.nextUpdate = DerGeneralizedTime.GetInstance( + this.nextUpdate = Asn1GeneralizedTime.GetInstance( (Asn1TaggedObject) seq[3], true); this.singleExtensions = X509Extensions.GetInstance( (Asn1TaggedObject) seq[4], true); @@ -48,7 +48,7 @@ namespace Org.BouncyCastle.Asn1.Ocsp if (o.TagNo == 0) { - this.nextUpdate = DerGeneralizedTime.GetInstance(o, true); + this.nextUpdate = Asn1GeneralizedTime.GetInstance(o, true); } else { @@ -90,12 +90,12 @@ namespace Org.BouncyCastle.Asn1.Ocsp get { return certStatus; } } - public DerGeneralizedTime ThisUpdate + public Asn1GeneralizedTime ThisUpdate { get { return thisUpdate; } } - public DerGeneralizedTime NextUpdate + public Asn1GeneralizedTime NextUpdate { get { return nextUpdate; } } diff --git a/crypto/src/asn1/pkcs/PKCSObjectIdentifiers.cs b/crypto/src/asn1/pkcs/PKCSObjectIdentifiers.cs index 1a6a5417a..570e0ded7 100644 --- a/crypto/src/asn1/pkcs/PKCSObjectIdentifiers.cs +++ b/crypto/src/asn1/pkcs/PKCSObjectIdentifiers.cs @@ -138,11 +138,12 @@ namespace Org.BouncyCastle.Asn1.Pkcs public static readonly DerObjectIdentifier IdAlg = IdSmime.Branch("3"); - public static readonly DerObjectIdentifier IdAlgEsdh = IdAlg.Branch("5"); - public static readonly DerObjectIdentifier IdAlgCms3DesWrap = IdAlg.Branch("6"); - public static readonly DerObjectIdentifier IdAlgCmsRC2Wrap = IdAlg.Branch("7"); - public static readonly DerObjectIdentifier IdAlgPwriKek = IdAlg.Branch("9"); - public static readonly DerObjectIdentifier IdAlgSsdh = IdAlg.Branch("10"); + public static readonly DerObjectIdentifier IdAlgEsdh = IdAlg.Branch("5"); + public static readonly DerObjectIdentifier IdAlgCms3DesWrap = IdAlg.Branch("6"); + public static readonly DerObjectIdentifier IdAlgCmsRC2Wrap = IdAlg.Branch("7"); + public static readonly DerObjectIdentifier IdAlgZlibCompress = IdAlg.Branch("8"); + public static readonly DerObjectIdentifier IdAlgPwriKek = IdAlg.Branch("9"); + public static readonly DerObjectIdentifier IdAlgSsdh = IdAlg.Branch("10"); /* * <pre> diff --git a/crypto/src/asn1/tsp/TSTInfo.cs b/crypto/src/asn1/tsp/TSTInfo.cs index 28a840e77..dde11494c 100644 --- a/crypto/src/asn1/tsp/TSTInfo.cs +++ b/crypto/src/asn1/tsp/TSTInfo.cs @@ -11,7 +11,7 @@ namespace Org.BouncyCastle.Asn1.Tsp private readonly DerObjectIdentifier tsaPolicyId; private readonly MessageImprint messageImprint; private readonly DerInteger serialNumber; - private readonly DerGeneralizedTime genTime; + private readonly Asn1GeneralizedTime genTime; private readonly Accuracy accuracy; private readonly DerBoolean ordering; private readonly DerInteger nonce; @@ -49,7 +49,7 @@ namespace Org.BouncyCastle.Asn1.Tsp // genTime e.MoveNext(); - genTime = DerGeneralizedTime.GetInstance(e.Current); + genTime = Asn1GeneralizedTime.GetInstance(e.Current); // default for ordering ordering = DerBoolean.False; @@ -96,7 +96,7 @@ namespace Org.BouncyCastle.Asn1.Tsp DerObjectIdentifier tsaPolicyId, MessageImprint messageImprint, DerInteger serialNumber, - DerGeneralizedTime genTime, + Asn1GeneralizedTime genTime, Accuracy accuracy, DerBoolean ordering, DerInteger nonce, @@ -140,7 +140,7 @@ namespace Org.BouncyCastle.Asn1.Tsp get { return accuracy; } } - public DerGeneralizedTime GenTime + public Asn1GeneralizedTime GenTime { get { return genTime; } } diff --git a/crypto/src/asn1/util/Asn1Dump.cs b/crypto/src/asn1/util/Asn1Dump.cs index 7bae766c3..6c005f512 100644 --- a/crypto/src/asn1/util/Asn1Dump.cs +++ b/crypto/src/asn1/util/Asn1Dump.cs @@ -206,12 +206,12 @@ namespace Org.BouncyCastle.Asn1.Utilities buf.Append(indent); buf.AppendLine("VideotexString(" + videotexString.GetString() + ")"); } - else if (obj is DerUtcTime utcTime) + else if (obj is Asn1UtcTime utcTime) { buf.Append(indent); buf.AppendLine("UTCTime(" + utcTime.TimeString + ")"); } - else if (obj is DerGeneralizedTime generalizedTime) + else if (obj is Asn1GeneralizedTime generalizedTime) { buf.Append(indent); buf.AppendLine("GeneralizedTime(" + generalizedTime.GetTime() + ")"); diff --git a/crypto/src/asn1/x509/AttCertValidityPeriod.cs b/crypto/src/asn1/x509/AttCertValidityPeriod.cs index d31e07402..893bc0838 100644 --- a/crypto/src/asn1/x509/AttCertValidityPeriod.cs +++ b/crypto/src/asn1/x509/AttCertValidityPeriod.cs @@ -7,8 +7,8 @@ namespace Org.BouncyCastle.Asn1.X509 public class AttCertValidityPeriod : Asn1Encodable { - private readonly DerGeneralizedTime notBeforeTime; - private readonly DerGeneralizedTime notAfterTime; + private readonly Asn1GeneralizedTime notBeforeTime; + private readonly Asn1GeneralizedTime notAfterTime; public static AttCertValidityPeriod GetInstance( object obj) @@ -39,24 +39,24 @@ namespace Org.BouncyCastle.Asn1.X509 if (seq.Count != 2) throw new ArgumentException("Bad sequence size: " + seq.Count); - notBeforeTime = DerGeneralizedTime.GetInstance(seq[0]); - notAfterTime = DerGeneralizedTime.GetInstance(seq[1]); + notBeforeTime = Asn1GeneralizedTime.GetInstance(seq[0]); + notAfterTime = Asn1GeneralizedTime.GetInstance(seq[1]); } public AttCertValidityPeriod( - DerGeneralizedTime notBeforeTime, - DerGeneralizedTime notAfterTime) + Asn1GeneralizedTime notBeforeTime, + Asn1GeneralizedTime notAfterTime) { this.notBeforeTime = notBeforeTime; this.notAfterTime = notAfterTime; } - public DerGeneralizedTime NotBeforeTime + public Asn1GeneralizedTime NotBeforeTime { get { return notBeforeTime; } } - public DerGeneralizedTime NotAfterTime + public Asn1GeneralizedTime NotAfterTime { get { return notAfterTime; } } diff --git a/crypto/src/asn1/x509/PrivateKeyUsagePeriod.cs b/crypto/src/asn1/x509/PrivateKeyUsagePeriod.cs index 89e8de6cb..a87c2ee9e 100644 --- a/crypto/src/asn1/x509/PrivateKeyUsagePeriod.cs +++ b/crypto/src/asn1/x509/PrivateKeyUsagePeriod.cs @@ -36,7 +36,7 @@ namespace Org.BouncyCastle.Asn1.X509 throw new ArgumentException("unknown object in GetInstance: " + Platform.GetTypeName(obj), "obj"); } - private DerGeneralizedTime _notBefore, _notAfter; + private Asn1GeneralizedTime _notBefore, _notAfter; private PrivateKeyUsagePeriod( Asn1Sequence seq) @@ -45,21 +45,21 @@ namespace Org.BouncyCastle.Asn1.X509 { if (tObj.TagNo == 0) { - _notBefore = DerGeneralizedTime.GetInstance(tObj, false); + _notBefore = Asn1GeneralizedTime.GetInstance(tObj, false); } else if (tObj.TagNo == 1) { - _notAfter = DerGeneralizedTime.GetInstance(tObj, false); + _notAfter = Asn1GeneralizedTime.GetInstance(tObj, false); } } } - public DerGeneralizedTime NotBefore + public Asn1GeneralizedTime NotBefore { get { return _notBefore; } } - public DerGeneralizedTime NotAfter + public Asn1GeneralizedTime NotAfter { get { return _notAfter; } } diff --git a/crypto/src/asn1/x509/TBSCertList.cs b/crypto/src/asn1/x509/TBSCertList.cs index ab847d563..2b288850f 100644 --- a/crypto/src/asn1/x509/TBSCertList.cs +++ b/crypto/src/asn1/x509/TBSCertList.cs @@ -193,8 +193,8 @@ namespace Org.BouncyCastle.Asn1.X509 thisUpdate = Time.GetInstance(seq[seqPos++]); if (seqPos < seq.Count - && (seq[seqPos] is DerUtcTime - || seq[seqPos] is DerGeneralizedTime + && (seq[seqPos] is Asn1UtcTime + || seq[seqPos] is Asn1GeneralizedTime || seq[seqPos] is Time)) { nextUpdate = Time.GetInstance(seq[seqPos++]); diff --git a/crypto/src/asn1/x509/Time.cs b/crypto/src/asn1/x509/Time.cs index efdf63850..1a6ac15c0 100644 --- a/crypto/src/asn1/x509/Time.cs +++ b/crypto/src/asn1/x509/Time.cs @@ -22,7 +22,7 @@ namespace Org.BouncyCastle.Asn1.X509 { if (time == null) throw new ArgumentNullException("time"); - if (!(time is DerUtcTime) && !(time is DerGeneralizedTime)) + if (!(time is Asn1UtcTime) && !(time is Asn1GeneralizedTime)) throw new ArgumentException("unknown object passed to Time"); this.time = time; @@ -49,27 +49,26 @@ namespace Org.BouncyCastle.Asn1.X509 } } - public static Time GetInstance( - object obj) + public static Time GetInstance(object obj) { - if (obj == null || obj is Time) - return (Time)obj; - if (obj is DerUtcTime) - return new Time((DerUtcTime)obj); - if (obj is DerGeneralizedTime) - return new Time((DerGeneralizedTime)obj); + if (obj == null) + return null; + if (obj is Time time) + return time; + if (obj is Asn1UtcTime utcTime) + return new Time(utcTime); + if (obj is Asn1GeneralizedTime generalizedTime) + return new Time(generalizedTime); throw new ArgumentException("unknown object in factory: " + Platform.GetTypeName(obj), "obj"); } public string GetTime() { - if (time is DerUtcTime) - { - return ((DerUtcTime) time).AdjustedTimeString; - } + if (time is Asn1UtcTime utcTime) + return utcTime.AdjustedTimeString; - return ((DerGeneralizedTime) time).GetTime(); + return ((Asn1GeneralizedTime)time).GetTime(); } /// <summary> @@ -80,14 +79,10 @@ namespace Org.BouncyCastle.Asn1.X509 { try { - if (time is DerUtcTime) - { - return ((DerUtcTime)time).ToAdjustedDateTime(); - } - else - { - return ((DerGeneralizedTime)time).ToDateTime(); - } + if (time is Asn1UtcTime utcTime) + return utcTime.ToAdjustedDateTime(); + + return ((Asn1GeneralizedTime)time).ToDateTime(); } catch (FormatException e) { diff --git a/crypto/src/asn1/x509/V1TBSCertificateGenerator.cs b/crypto/src/asn1/x509/V1TBSCertificateGenerator.cs index 20b525a48..9cbff1ef0 100644 --- a/crypto/src/asn1/x509/V1TBSCertificateGenerator.cs +++ b/crypto/src/asn1/x509/V1TBSCertificateGenerator.cs @@ -56,7 +56,7 @@ namespace Org.BouncyCastle.Asn1.X509 } public void SetStartDate( - DerUtcTime startDate) + Asn1UtcTime startDate) { this.startDate = new Time(startDate); } @@ -68,7 +68,7 @@ namespace Org.BouncyCastle.Asn1.X509 } public void SetEndDate( - DerUtcTime endDate) + Asn1UtcTime endDate) { this.endDate = new Time(endDate); } diff --git a/crypto/src/asn1/x509/V2AttributeCertificateInfoGenerator.cs b/crypto/src/asn1/x509/V2AttributeCertificateInfoGenerator.cs index 02580b5b8..c78c966b0 100644 --- a/crypto/src/asn1/x509/V2AttributeCertificateInfoGenerator.cs +++ b/crypto/src/asn1/x509/V2AttributeCertificateInfoGenerator.cs @@ -26,11 +26,13 @@ namespace Org.BouncyCastle.Asn1.X509 internal AttCertIssuer issuer; internal AlgorithmIdentifier signature; internal DerInteger serialNumber; -// internal AttCertValidityPeriod attrCertValidityPeriod; internal Asn1EncodableVector attributes; internal DerBitString issuerUniqueID; internal X509Extensions extensions; - internal DerGeneralizedTime startDate, endDate; + + // Note: validity period start/end dates stored directly + //internal AttCertValidityPeriod attrCertValidityPeriod; + internal Asn1GeneralizedTime startDate, endDate; public V2AttributeCertificateInfoGenerator() { @@ -78,13 +80,13 @@ namespace Org.BouncyCastle.Asn1.X509 } public void SetStartDate( - DerGeneralizedTime startDate) + Asn1GeneralizedTime startDate) { this.startDate = startDate; } public void SetEndDate( - DerGeneralizedTime endDate) + Asn1GeneralizedTime endDate) { this.endDate = endDate; } diff --git a/crypto/src/asn1/x509/V2TBSCertListGenerator.cs b/crypto/src/asn1/x509/V2TBSCertListGenerator.cs index 1d58751fd..aa1a0b95d 100644 --- a/crypto/src/asn1/x509/V2TBSCertListGenerator.cs +++ b/crypto/src/asn1/x509/V2TBSCertListGenerator.cs @@ -55,13 +55,13 @@ namespace Org.BouncyCastle.Asn1.X509 } public void SetThisUpdate( - DerUtcTime thisUpdate) + Asn1UtcTime thisUpdate) { this.thisUpdate = new Time(thisUpdate); } public void SetNextUpdate( - DerUtcTime nextUpdate) + Asn1UtcTime nextUpdate) { this.nextUpdate = (nextUpdate != null) ? new Time(nextUpdate) @@ -90,7 +90,7 @@ namespace Org.BouncyCastle.Asn1.X509 crlEntries.Add(crlEntry); } - public void AddCrlEntry(DerInteger userCertificate, DerUtcTime revocationDate, int reason) + public void AddCrlEntry(DerInteger userCertificate, Asn1UtcTime revocationDate, int reason) { AddCrlEntry(userCertificate, new Time(revocationDate), reason); } @@ -101,7 +101,7 @@ namespace Org.BouncyCastle.Asn1.X509 } public void AddCrlEntry(DerInteger userCertificate, Time revocationDate, int reason, - DerGeneralizedTime invalidityDate) + Asn1GeneralizedTime invalidityDate) { var extOids = new List<DerObjectIdentifier>(); var extValues = new List<X509Extension>(); diff --git a/crypto/src/asn1/x509/V3TBSCertificateGenerator.cs b/crypto/src/asn1/x509/V3TBSCertificateGenerator.cs index beb469a0d..544582ddb 100644 --- a/crypto/src/asn1/x509/V3TBSCertificateGenerator.cs +++ b/crypto/src/asn1/x509/V3TBSCertificateGenerator.cs @@ -58,7 +58,7 @@ namespace Org.BouncyCastle.Asn1.X509 } public void SetStartDate( - DerUtcTime startDate) + Asn1UtcTime startDate) { this.startDate = new Time(startDate); } @@ -70,7 +70,7 @@ namespace Org.BouncyCastle.Asn1.X509 } public void SetEndDate( - DerUtcTime endDate) + Asn1UtcTime endDate) { this.endDate = new Time(endDate); } diff --git a/crypto/src/asn1/x509/X509DefaultEntryConverter.cs b/crypto/src/asn1/x509/X509DefaultEntryConverter.cs index 7282ead26..d155efc5a 100644 --- a/crypto/src/asn1/x509/X509DefaultEntryConverter.cs +++ b/crypto/src/asn1/x509/X509DefaultEntryConverter.cs @@ -46,7 +46,7 @@ namespace Org.BouncyCastle.Asn1.X509 if (oid.Equals(X509Name.DateOfBirth)) // accept time string as well as # (for compatibility) { - return new DerGeneralizedTime(value); + return new Asn1GeneralizedTime(value); } if (oid.Equals(X509Name.C) diff --git a/crypto/src/asn1/x509/sigi/PersonalData.cs b/crypto/src/asn1/x509/sigi/PersonalData.cs index 439039888..e8c75bf93 100644 --- a/crypto/src/asn1/x509/sigi/PersonalData.cs +++ b/crypto/src/asn1/x509/sigi/PersonalData.cs @@ -29,7 +29,7 @@ namespace Org.BouncyCastle.Asn1.X509.SigI { private readonly NameOrPseudonym nameOrPseudonym; private readonly BigInteger nameDistinguisher; - private readonly DerGeneralizedTime dateOfBirth; + private readonly Asn1GeneralizedTime dateOfBirth; private readonly DirectoryString placeOfBirth; private readonly string gender; private readonly DirectoryString postalAddress; @@ -88,7 +88,7 @@ namespace Org.BouncyCastle.Asn1.X509.SigI nameDistinguisher = DerInteger.GetInstance(o, false).Value; break; case 1: - dateOfBirth = DerGeneralizedTime.GetInstance(o, false); + dateOfBirth = Asn1GeneralizedTime.GetInstance(o, false); break; case 2: placeOfBirth = DirectoryString.GetInstance(o, true); @@ -118,7 +118,7 @@ namespace Org.BouncyCastle.Asn1.X509.SigI public PersonalData( NameOrPseudonym nameOrPseudonym, BigInteger nameDistinguisher, - DerGeneralizedTime dateOfBirth, + Asn1GeneralizedTime dateOfBirth, DirectoryString placeOfBirth, string gender, DirectoryString postalAddress) @@ -141,7 +141,7 @@ namespace Org.BouncyCastle.Asn1.X509.SigI get { return nameDistinguisher; } } - public DerGeneralizedTime DateOfBirth + public Asn1GeneralizedTime DateOfBirth { get { return dateOfBirth; } } |