Ejemplo n.º 1
0
        public IEnumerable<byte> Serialize(IAsn1Element element, int depth = 0)
        {
            string tabs = "";
            for (int i = 0; i < depth; i++)
            {
                tabs += "\t";
            }
            Trace.WriteLine($"{tabs}{element.GetType().Name} {element.Tag:x} (Total Size: {element.Size} Bytes, Data: {element.Length} Bytes)");
            yield return element.Tag;            

            foreach (var b in element.LengthBytes) yield return b;

            if (element is BitString)
            {
                //yield return ((BitString)element).UnusedBits;
            }

            if (element is Asn1Primitive)
            {                
                foreach (var b in ((Asn1Primitive)element).Data) yield return b;
            }
            else if (element is Asn1Container)
            {
                foreach (var b in ((Asn1Container) element).Children.SelectMany(e => Serialize(e, depth+1)))
                {
                    yield return b;
                }
            }
            else
            {
                throw new Exception("Unknown Asn1 element type: " + element.GetType().FullName);
            }
        }                
Ejemplo n.º 2
0
 protected Asn1Container(byte tag, IAsn1Element[] children)
 {
     Tag = tag;
     Children = children;
     var childLength = children.Sum(c => c.Size);
     LengthBytes = childLength.ToEncodedLength().ToArray();
     Length = childLength;
     Size = Length + LengthBytes.Length + 1;
 }
Ejemplo n.º 3
0
        public static byte[] Encode(IAsn1Element element, int depth = 0)
        {
            using (var ms = new MemoryStream())
            {
                Encode(element, ms);

                return(ms.ToArray());
            }
        }
Ejemplo n.º 4
0
        public IEnumerable <byte> Serialize(IAsn1Element element, int depth = 0)
        {
            string tabs = "";

            for (int i = 0; i < depth; i++)
            {
                tabs += "\t";
            }
            Trace.WriteLine($"{tabs}{element.GetType().Name} {element.Tag:x} (Total Size: {element.Size} Bytes, Data: {element.Length} Bytes)");
            yield return(element.Tag);

            foreach (var b in element.LengthBytes)
            {
                yield return(b);
            }

            if (element is BitString bitString)
            {
                //yield return ((BitString)element).UnusedBits;
            }

            if (element is Asn1Primitive asn1Primitive)
            {
                foreach (var b in asn1Primitive.Data)
                {
                    yield return(b);
                }
            }
            else if (element is Asn1Container asn1Container)
            {
                foreach (var b in asn1Container.Children.SelectMany(e => Serialize(e, depth + 1)))
                {
                    yield return(b);
                }
            }
            else
            {
                throw new Exception("Unknown Asn1 element type: " + element.GetType().FullName);
            }
        }
Ejemplo n.º 5
0
        public static void Encode(IAsn1Element element, Stream stream, int depth = 0)
        {
            /*
             * string tabs = "";
             * for (int i = 0; i < depth; i++)
             * {
             *  tabs += "\t";
             * }
             *
             * Trace.WriteLine($"{tabs}{element.GetType().Name} {element.Tag:x} (Total Size: {element.Size} Bytes, Data: {element.Length} Bytes)");
             */

            // Tag
            stream.WriteByte(element.Tag);

            // Length bytes
            stream.Write(element.LengthBytes, 0, element.LengthBytes.Length);

            if (element is BitString bitString)
            {
                // yield return ((BitString)element).UnusedBits;
            }
            if (element is Asn1Object asn1Primitive)
            {
                stream.Write(asn1Primitive.Data, 0, asn1Primitive.Data.Length);
            }
            else if (element is Asn1Container asn1Container)
            {
                foreach (var b in asn1Container.Children)
                {
                    Encode(b, stream, depth + 1);
                }
            }
            else
            {
                throw new Exception("Unknown Asn1 element type: " + element.GetType().FullName);
            }
        }
Ejemplo n.º 6
0
 public CertBag(IAsn1Element content)
 {
     Content = content;
 }
Ejemplo n.º 7
0
 public SafeContentsBag(IAsn1Element content)
 {
     Content = content;
 }
Ejemplo n.º 8
0
 public CertBag(IAsn1Element content)
 {
     Content = content ?? throw new ArgumentNullException(nameof(Content));
 }
Ejemplo n.º 9
0
 public SafeContentsBag(IAsn1Element content)
 {
     Content = content ?? throw new ArgumentNullException(nameof(content));
 }
Ejemplo n.º 10
0
 public SafeContentsBag(IAsn1Element content) 
 {
     Content = content;
 }
 public ContentInfo(ObjectIdentifier type, IAsn1Element content)
     : base(type, content)
 {
 }
Ejemplo n.º 12
0
 public CertBag(IAsn1Element content)
 {
     Content = content;
 }