コード例 #1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="DataElement"/> struct.
 /// </summary>
 /// <param name="class">The class.</param>
 /// <param name="pc">The PC.</param>
 /// <param name="tag">The tag.</param>
 /// <param name="content">The content.</param>
 public DataElement(BerClass @class, BerPC pc, BerTag tag, byte[] content)
     : this()
 {
     this.Class   = @class;
     this.PC      = pc;
     this.Tag     = tag;
     this.Content = content;
 }
コード例 #2
0
 public Ber(BerClass cl, BerTags tg, bool c)
 {
     allSet();
     if ((int)cl > 3)
     {
         throw new Exception("class id must be from 0 to 3");
     }
     tagClass   = (byte)((int)cl & 3);
     _container = c;
     _tag       = (ulong)tg;
 }
コード例 #3
0
 public Ber(BerClass cl, BerTags tg, bool c, byte[] pl)
 {
     allSet();
     if ((int)cl > 3)
     {
         throw new Exception("class id must be from 0 to 3");
     }
     tagClass       = (byte)((int)cl & 3);
     _container     = c;
     _tag           = (ulong)tg;
     _payload       = pl;
     _payloadLength = pl.Length;
 }
コード例 #4
0
        /// <summary>
        /// Reads a sequence of ASN.1 elements from a stream.
        /// </summary>
        /// <param name="stream">The stream.</param>
        /// <returns>A sequence of elements.</returns>
        /// <remarks>
        /// The stream may not contain exclusively ASN.1 data.
        /// This method will read the stream exactly one element at a time,
        /// and the caller should only enumerate as many elements as are expected
        /// to avoid reading into other data.
        /// If the end of the stream is reached, the sequence terminates.
        /// </remarks>
        internal static IEnumerable <DataElement> ReadAsn1Elements(this Stream stream)
        {
            Requires.NotNull(stream, "stream");

            do
            {
                int b = stream.ReadByte();
                if (b == -1)
                {
                    yield break;
                }

                BerClass clazz = (BerClass)b & BerClass.Mask;
                BerPC    pc    = (BerPC)b & BerPC.Mask;
                BerTag   tag   = (BerTag)b & BerTag.Mask;

                uint length = 0;
                b = stream.ReadByte();
                if ((b & 0x80) == 0x80)
                {
                    // long form
                    byte lengthOfLength = (byte)(b & 0x7F);
                    for (int i = 0; i < lengthOfLength; i++)
                    {
                        // big endian
                        b        = stream.ReadByte();
                        length <<= 8;
                        length  += (uint)b;
                    }
                }
                else
                {
                    // short form.
                    length = (uint)b;
                }

                if (length > 8 * 1024)
                {
                    throw new FormatException("Invalid format or length too large.");
                }

                byte[] content   = new byte[length];
                int    bytesRead = stream.Read(content, 0, (int)length);
                if (bytesRead != length)
                {
                    throw new ArgumentException("Unexpected end of stream.");
                }

                yield return(new DataElement(clazz, pc, tag, content));
            }while (true);
        }
コード例 #5
0
        /// <summary>
        /// Reverses the operation GetShortClassName(), returning true
        /// if <paramref name="name"/> could be parsed into a BerClass value.
        /// </summary>
        public static bool GetClassFromName(string name, bool ignoreCase, out BerClass tagClass)
        {
            var comparison = ignoreCase
                          ? StringComparison.CurrentCultureIgnoreCase
                          : StringComparison.CurrentCulture;

            for (int index = 0; index < ShortClassNames.Length; index++)
            {
                if (String.Compare(ShortClassNames[index], name, comparison) == 0)
                {
                    tagClass = (BerClass)(index << 6);
                    return(true);
                }
            }

            tagClass = BerClass.Universal;
            return(false);
        }
コード例 #6
0
 /// <summary>
 /// Constructs a new BerTag instance.
 /// </summary>
 /// <param name="tagClass">The tag class.</param>
 /// <param name="number">The tag number.</param>
 /// <param name="isContainer">If true, the container flag is set.</param>
 public BerTag(BerClass tagClass, uint number, bool isContainer)
     : this(tagClass, number)
 {
     IsContainer = isContainer;
 }
コード例 #7
0
 /// <summary>
 /// Constructs a new BerTag instance, not setting
 /// the container flag.
 /// </summary>
 /// <param name="tagClass">The tag class.</param>
 /// <param name="number">The tag number.</param>
 public BerTag(BerClass tagClass, uint number)
     : this()
 {
     Preamble = (byte)(tagClass & BerClass.Mask);
     Number   = number;
 }
コード例 #8
0
 /// <summary>
 /// Initializes a new instance of the <see cref="DataElement"/> struct.
 /// </summary>
 /// <param name="class">The class.</param>
 /// <param name="pc">The PC.</param>
 /// <param name="tag">The tag.</param>
 /// <param name="nestedElements">The content.</param>
 public DataElement(BerClass @class, BerPC pc, BerTag tag, params DataElement[] nestedElements)
     : this(@class, pc, tag, Asn.WriteAsn1Elements(nestedElements))
 {
 }
コード例 #9
0
 /// <summary>
 /// Gets the name of the tag class identified by <paramref name="tagClass"/>:
 /// either 'C' (context-specific), 'A' (application), 'U' (universal) or
 /// 'P' (private).
 /// </summary>
 public static string GetShortClassName(BerClass tagClass)
 {
     return(ShortClassNames[(byte)tagClass >> 6]);
 }
コード例 #10
0
ファイル: Asn.cs プロジェクト: martijn00/PCLCrypto
 /// <summary>
 /// Initializes a new instance of the <see cref="DataElement"/> struct.
 /// </summary>
 /// <param name="class">The class.</param>
 /// <param name="pc">The PC.</param>
 /// <param name="tag">The tag.</param>
 /// <param name="nestedElements">The content.</param>
 public DataElement(BerClass @class, BerPC pc, BerTag tag, params DataElement[] nestedElements)
     : this(@class, pc, tag, Asn.WriteAsn1Elements(nestedElements))
 {
 }
コード例 #11
0
ファイル: Asn.cs プロジェクト: martijn00/PCLCrypto
 /// <summary>
 /// Initializes a new instance of the <see cref="DataElement"/> struct.
 /// </summary>
 /// <param name="class">The class.</param>
 /// <param name="pc">The PC.</param>
 /// <param name="tag">The tag.</param>
 /// <param name="content">The content.</param>
 public DataElement(BerClass @class, BerPC pc, BerTag tag, byte[] content)
     : this()
 {
     this.Class = @class;
     this.PC = pc;
     this.Tag = tag;
     this.Content = content;
 }
コード例 #12
0
ファイル: BerDefinitions.cs プロジェクト: jv42/ember-plus
 /// <summary>
 /// Gets the name of the tag class identified by <paramref name="tagClass"/>:
 /// either 'C' (context-specific), 'A' (application), 'U' (universal) or
 /// 'P' (private).
 /// </summary>
 public static string GetShortClassName(BerClass tagClass)
 {
     return ShortClassNames[(byte)tagClass >> 6];
 }
コード例 #13
0
ファイル: BerDefinitions.cs プロジェクト: jv42/ember-plus
        /// <summary>
        /// Reverses the operation GetShortClassName(), returning true
        /// if <paramref name="name"/> could be parsed into a BerClass value.
        /// </summary>
        public static bool GetClassFromName(string name, bool ignoreCase, out BerClass tagClass)
        {
            var comparison = ignoreCase
                          ? StringComparison.CurrentCultureIgnoreCase
                          : StringComparison.CurrentCulture;

             for(int index = 0; index < ShortClassNames.Length; index++)
             {
            if(String.Compare(ShortClassNames[index], name, comparison) == 0)
            {
               tagClass = (BerClass)(index << 6);
               return true;
            }
             }

             tagClass = BerClass.Universal;
             return false;
        }