Example #1
0
 static void AssertIsUniversal(uint type)
 {
     if (type >= BerType.LastUniversal ||
         BerType.IsApplicationDefined(type))
     {
         throw new ArgumentException("type is not universal!");
     }
 }
Example #2
0
        /// <summary>
        /// Decodes the value of the current TLV as an RelativeOid.
        /// Throws an exception in case of a format mismatch.
        /// </summary>
        /// <returns>An array of integers containing the subidentifiers of the RelativeOid value of the current TLV.</returns>
        public int[] GetRelativeOid()
        {
            if (IsContainer)
            {
                ThrowError(207, "Invalid RelativeOid encoding");
            }

            Debug.Assert(Value != null || Length == 0);
            Debug.Assert(Type == BerType.RelativeOid || BerType.IsApplicationDefined(Type));

            var input = new BerMemoryInput(Value);

            return(BerEncoding.DecodeRelativeOid(input, Length));
        }
Example #3
0
        /// <summary>
        /// Decodes the value of the current TLV as an GeneralizedTime represented
        /// as a DateTime structure.
        /// Throws an exception in case of a format mismatch.
        /// </summary>
        /// <returns>DateTime value containing the GeneralizedTime value of the current TLV.</returns>
        public DateTime GetGeneralizedTime()
        {
            if (IsContainer || Length == 0 || Value == null)
            {
                ThrowError(208, "Invalid GeneralizedTime encoding");
            }

            Debug.Assert(Value != null || Length == 0);
            Debug.Assert(Type == BerType.GeneralizedTime || BerType.IsApplicationDefined(Type));

            var input = new BerMemoryInput(Value);

            return(BerEncoding.DecodeGeneralizedTime(input, Length));
        }
Example #4
0
        /// <summary>
        /// Decodes the value of the current TLV as an OBJECT IDENTIFIER represented
        /// as an integer array, each integer containing one sub-identifier.
        /// Throws an exception in case of a format mismatch.
        /// </summary>
        /// <returns>Integer array containing the OBJECT IDENTIFIER value of the current TLV.</returns>
        public int[] GetObjectIdentifier()
        {
            if (IsContainer || Length == 0 || Value == null)
            {
                ThrowError(209, "Invalid ObjectIdentifier encoding");
            }

            Debug.Assert(Value != null || Length == 0);
            Debug.Assert(Type == BerType.ObjectIdentifier || BerType.IsApplicationDefined(Type));

            var input = new BerMemoryInput(Value);

            return(BerEncoding.DecodeObjectIdentifier(input, Length));
        }
Example #5
0
        /// <summary>
        /// Decodes the value of the current TLV as an octet string (byte array).
        /// Throws an exception in case of a format mismatch.
        /// </summary>
        /// <returns>Byte array containing the OCTET STRING value of the current TLV.</returns>
        public byte[] GetOctetString()
        {
            if (IsContainer)
            {
                ThrowError(206, "Invalid OctetString encoding");
            }

            Debug.Assert(Value != null || Length == 0);
            Debug.Assert(Type == BerType.OctetString || BerType.IsApplicationDefined(Type));

            var input = new BerMemoryInput(Value);

            return(BerEncoding.DecodeByteArray(input, Length));
        }
Example #6
0
        /// <summary>
        /// Decodes the value of the current TLV an UTF-8 string.
        /// Throws an exception in case of a format mismatch.
        /// </summary>
        /// <returns>The string value of the current TLV.</returns>
        public string GetString()
        {
            if (IsContainer)
            {
                ThrowError(205, "Invalid String encoding");
            }

            Debug.Assert(Value != null || Length == 0);
            Debug.Assert(Type == BerType.UTF8String || Type == BerType.Bitstring || BerType.IsApplicationDefined(Type));

            var input = new BerMemoryInput(Value);

            return(BerEncoding.DecodeUtf8String(input, Length));
        }
Example #7
0
        /// <summary>
        /// Decodes the value of the current TLV a 64bit integer.
        /// Throws an exception in case of a format mismatch.
        /// </summary>
        /// <returns>The integer value of the current TLV.</returns>
        public long GetLong()
        {
            if (IsContainer || Length == 0 || Value == null)
            {
                ThrowError(203, "Invalid Integer encoding");
            }

            Debug.Assert(Value != null || Length == 0);
            Debug.Assert(Type == BerType.Integer || BerType.IsApplicationDefined(Type));

            var input = new BerMemoryInput(Value);

            return(BerEncoding.DecodeLong(input, Length));
        }
Example #8
0
        /// <summary>
        /// Decodes the value of the current TLV as boolean.
        /// Throws an exception in case of a format mismatch.
        /// </summary>
        /// <returns>The boolean value of the current TLV.</returns>
        public bool GetBoolean()
        {
            if (IsContainer || Length == 0 || Value == null)
            {
                ThrowError(201, "Invalid Boolean encoding");
            }

            Debug.Assert(Value != null || Length == 0);
            Debug.Assert(Type == BerType.Boolean || BerType.IsApplicationDefined(Type));

            var input = new BerMemoryInput(Value);

            return(BerEncoding.DecodeBoolean(input));
        }
Example #9
0
        /// <summary>
        /// Gets the name of the type passed in <paramref name="type"/> in PascalCase
        /// (e.g. 'ObjectIdentifier' if type equals 6).
        /// If <paramref name="type"/> is an application-defined type, returns a name
        /// in the form 'A-1' (for type APPLICATION 1).
        /// </summary>
        /// <param name="type">Usually one of the BerType constants.</param>
        /// <returns></returns>
        public static string GetTypeName(uint type)
        {
            if (BerType.IsApplicationDefined(type))
            {
                return(String.Format("A-{0}", type & ~BerType.ApplicationFlag));
            }

            var typeName = null as string;

            if (type < TypeNames.Length)
            {
                typeName = TypeNames[type];
            }

            return(typeName ?? type.ToString());
        }