peass-ng/PEASS-ng · error · ArgumentException

invalid date string:

Error message

invalid date string: 

What it means

The DerGeneralizedTime(string) constructor validates the date string immediately by calling ToDateTime(); if that raises FormatException, the constructor rethrows it as ArgumentException('invalid date string: ...'). Only properly formatted GeneralizedTime strings (yyyyMMddHHmmss... with optional fraction/timezone) are accepted.

Source

Thrown at winPEAS/winPEASexe/winPEAS/3rdParty/BouncyCastle/asn1/DerGeneralizedTime.cs:76

         * 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)
        {
#if PORTABLE
            this.time = time.ToUniversalTime().ToString(@"yyyyMMddHHmmss\Z");
#else
            this.time = time.ToString(@"yyyyMMddHHmmss\Z");
#endif
        }

        internal DerGeneralizedTime(
            byte[] bytes)

View on GitHub (pinned to 53fb989abc)

Solutions

  1. Ensure the string is DER GeneralizedTime format, e.g. '20260901123000Z' or '20260901123000.123+0200'
  2. Normalize the value first: parse to DateTime yourself (DateTime.Parse/DateTimeOffset) and use the DerGeneralizedTime(DateTime) constructor
  3. Trim garbage: verify with a regex like ^\d{14}(\.\d+)?(Z|[+-]\d{4})?$ before constructing

Example fix

// before
var gt = new DerGeneralizedTime(userString); // may throw
// after
if (!DateTime.TryParse(userString, CultureInfo.InvariantCulture, DateTimeStyles.None, out var dt))
    throw new FormatException("Not a valid date: " + userString);
var gt = new DerGeneralizedTime(dt);
Defensive patterns

Strategy: validation

Validate before calling

if (Regex.IsMatch(s ?? "", @"^\d{14}(\.\d+)?(Z|[+-]\d{4})?$"))
{ var gt = new DerGeneralizedTime(s); }

Try / catch

try { var gt = new DerGeneralizedTime(s); }
catch (ArgumentException ex) when (ex.Message.StartsWith("invalid date string")) { /* normalize or reject */ }

Prevention

When it happens

Trigger: Calling new DerGeneralizedTime(s) with a string that is not a valid DER GeneralizedTime: missing date parts, bad separators, invalid timezone suffix, non-digit characters, or an impossible date.

Common situations: Strings from certificates, logs, or user input parsed as GeneralizedTime; locale-dependent DateTime strings (e.g. '01/02/2026 3pm') fed in directly; truncated timestamp fields from corrupt data.

Related errors


AI-assisted analysis of peass-ng/PEASS-ng@53fb989abc (2026-09-02). Data as JSON: /api/errors/a55bd7d00468e98c. Report an issue: GitHub.