Ejemplo n.º 1
0
        internal override void encode(
            DEROutputStream derOut)
        {
            OIDTokenizer    tok  = new OIDTokenizer(identifier);
            MemoryStream    bOut = new MemoryStream();
            DEROutputStream dOut = new DEROutputStream(bOut);

            string token = "";

            try
            {
                token = tok.nextToken();
                int first = System.Int32.Parse(token);

                token = tok.nextToken();
                int second = System.Int32.Parse(token);

                writeField(bOut, first * 40 + second);
            }
            catch (Exception)
            {
                throw new Exception("Problems parsing identifier: " + identifier + ", " + token);
            }

            while (tok.hasMoreTokens())
            {
                writeField(bOut, System.Int64.Parse(tok.nextToken()));
            }

            dOut.Close();

            byte[] bytes = bOut.ToArray();

            derOut.writeEncoded(ASN1Tags.OBJECT_IDENTIFIER, bytes);
        }
Ejemplo n.º 2
0
        internal override void encode(
            DEROutputStream derOut)
        {
            char[]       c    = str.ToCharArray();
            MemoryStream bOut = new MemoryStream();

            for (int i = 0; i != c.Length; i++)
            {
                char ch = c[i];

                if (ch < 0x0080)
                {
                    bOut.WriteByte((byte)ch);
                }
                else if (ch < 0x0800)
                {
                    bOut.WriteByte((byte)(0xc0 | (ch >> 6)));
                    bOut.WriteByte((byte)(0x80 | (ch & 0x3f)));
                }
                else
                {
                    bOut.WriteByte((byte)(0xe0 | (ch >> 12)));
                    bOut.WriteByte((byte)(0x80 | ((ch >> 6) & 0x3F)));
                    bOut.WriteByte((byte)(0x80 | (ch & 0x3F)));
                }
            }

            derOut.writeEncoded(ASN1Tags.UTF8_STRING, bOut.ToArray());
        }
Ejemplo n.º 3
0
        internal override void encode(
            DEROutputStream derOut)
        {
            byte[] bytes = new byte[1];

            bytes[0] = value;

            derOut.writeEncoded(ASN1Tags.BOOLEAN, bytes);
        }
Ejemplo n.º 4
0
        internal override void encode(
            DEROutputStream derOut)
        {
            byte[] bytes = new byte[getBytes().Length + 1];

            bytes[0] = (byte)getPadBits();
            Array.Copy(getBytes(), 0, bytes, 1, bytes.Length - 1);

            derOut.writeEncoded(ASN1Tags.BIT_STRING, bytes);
        }
Ejemplo n.º 5
0
        internal override void encode(
            DEROutputStream derOut)
        {
            if (derOut is ASN1OutputStream || derOut is BEROutputStream)
            {
                derOut.WriteByte((byte)(ASN1Tags.CONSTRUCTED | ASN1Tags.OCTET_STRING));

                derOut.WriteByte(0x80);

                //
                // write out the octet array
                //
                if (octs != null)
                {
                    for (int i = 0; i != octs.Count; i++)
                    {
                        derOut.writeObject(octs[i]);
                    }
                }
                else
                {
                    int start = 0;
                    int end   = 0;

                    while ((end + 1) < str.Length)
                    {
                        if (str[end] == 0 && str[end + 1] == 0)
                        {
                            byte[] nStr = new byte[end - start + 1];

                            Array.Copy(str, start, nStr, 0, nStr.Length);

                            derOut.writeObject(new DEROctetString(nStr));
                            start = end + 1;
                        }
                        end++;
                    }

                    byte[] nStr2 = new byte[str.Length - start];

                    Array.Copy(str, start, nStr2, 0, nStr2.Length);

                    derOut.writeObject(new DEROctetString(nStr2));
                }

                derOut.WriteByte(0x00);
                derOut.WriteByte(0x00);
            }
            else
            {
                base.encode(derOut);
            }
        }
Ejemplo n.º 6
0
        public DERApplicationSpecific(
            int tag,
            ASN1Encodable obj)
        {
            this.tag = tag | ASN1Tags.CONSTRUCTED;

            MemoryStream    baos = new MemoryStream();
            DEROutputStream dos  = new DEROutputStream(baos);

            dos.writeObject(obj);

            this.octets = baos.ToArray();
        }
Ejemplo n.º 7
0
        internal override void encode(
            DEROutputStream derOut)
        {
            char[] c = str.ToCharArray();
            byte[] b = new byte[c.Length * 2];

            for (int i = 0; i != c.Length; i++)
            {
                b[2 * i]     = (byte)(c[i] >> 8);
                b[2 * i + 1] = (byte)c[i];
            }

            derOut.writeEncoded(ASN1Tags.BMP_STRING, b);
        }
Ejemplo n.º 8
0
        protected ASN1OctetString(
            ASN1Encodable obj)
        {
            try
            {
                MemoryStream    bOut = new MemoryStream();
                DEROutputStream dOut = new DEROutputStream(bOut);

                dOut.writeObject(obj);
                dOut.Close();

                this.str = bOut.ToArray();
            }
            catch (IOException e)
            {
                throw new ArgumentException("Error processing object : " + e.ToString());
            }
        }
Ejemplo n.º 9
0
        /*
         * A note on the implementation:
         * <p>
         * As DER requires the constructed, definite-length model to
         * be used for structured types, this varies slightly from the
         * ASN.1 descriptions given. Rather than just outputing SET,
         * we also have to specify CONSTRUCTED, and the objects length.
         */
        internal override void encode(
            DEROutputStream derOut)
        {
            MemoryStream    bOut = new MemoryStream();
            DEROutputStream dOut = new DEROutputStream(bOut);
            IEnumerator     e    = this.getObjects();

            while (e.MoveNext())
            {
                object obj = e.Current;

                dOut.writeObject(obj);
            }

            dOut.Close();

            byte[] bytes = bOut.ToArray();

            derOut.writeEncoded(ASN1Tags.SET | ASN1Tags.CONSTRUCTED, bytes);
        }
Ejemplo n.º 10
0
        /*
         */
        internal override void encode(
            DEROutputStream derOut)
        {
            if (derOut is ASN1OutputStream || derOut is BEROutputStream)
            {
                derOut.WriteByte((byte)(ASN1Tags.SEQUENCE | ASN1Tags.CONSTRUCTED));
                derOut.WriteByte(0x80);

                IEnumerator e = getObjects();
                while (e.MoveNext())
                {
                    derOut.writeObject(e.Current);
                }

                derOut.WriteByte(0x00);
                derOut.WriteByte(0x00);
            }
            else
            {
                base.encode(derOut);
            }
        }
Ejemplo n.º 11
0
        internal override void encode(
            DEROutputStream derOut)
        {
            if (!empty)
            {
                MemoryStream    bOut = new MemoryStream();
                DEROutputStream dOut = new DEROutputStream(bOut);

                dOut.writeObject(obj);
                dOut.Close();

                byte[] bytes = bOut.ToArray();

                if (explicitly)
                {
                    derOut.writeEncoded((int)(ASN1Tags.CONSTRUCTED | ASN1Tags.TAGGED) | tagNo, bytes);
                }
                else
                {
                    //
                    // need to mark constructed types...
                    //
                    if ((bytes[0] & (byte)ASN1Tags.CONSTRUCTED) != 0)
                    {
                        bytes[0] = (byte)((int)(ASN1Tags.CONSTRUCTED | ASN1Tags.TAGGED) | tagNo);
                    }
                    else
                    {
                        bytes[0] = (byte)((int)(ASN1Tags.TAGGED) | tagNo);
                    }

                    derOut.Write(bytes, 0, bytes.Length);
                }
            }
            else
            {
                derOut.writeEncoded((int)(ASN1Tags.CONSTRUCTED | ASN1Tags.TAGGED) | tagNo, new byte[0]);
            }
        }
Ejemplo n.º 12
0
 internal override void encode(
     DEROutputStream derOut)
 {
     derOut.writeEncoded(ASN1Tags.T61_STRING, this.getOctets());
 }
Ejemplo n.º 13
0
 internal override void encode(DEROutputStream derOut)
 {
     derOut.writeEncoded(ASN1Tags.APPLICATION | tag, octets);
 }
Ejemplo n.º 14
0
 internal override void encode(
     DEROutputStream derOut)
 {
     derOut.writeEncoded(ASN1Tags.GENERALIZED_TIME, this.getOctets());
 }
Ejemplo n.º 15
0
 internal override void encode(
     DEROutputStream derOut)
 {
     derOut.writeEncoded(ASN1Tags.INTEGER, bytes);
 }
Ejemplo n.º 16
0
 internal abstract void encode(DEROutputStream derOut);
Ejemplo n.º 17
0
 internal override void encode(
     DEROutputStream derOut)
 {
     derOut.writeEncoded(tag, data);
 }
Ejemplo n.º 18
0
 internal override void encode(
     DEROutputStream derOut)
 {
     derOut.writeEncoded(ASN1Tags.ENUMERATED, bytes);
 }
Ejemplo n.º 19
0
 internal override void encode(
     DEROutputStream derOut)
 {
     derOut.writeEncoded(ASN1Tags.NULL, zeroBytes);
 }
Ejemplo n.º 20
0
        internal override void encode(
            DEROutputStream derOut)
        {
            if (derOut is ASN1OutputStream || derOut is BEROutputStream)
            {
                derOut.WriteByte((byte)(((int)(ASN1Tags.CONSTRUCTED | ASN1Tags.TAGGED)) | tagNo));
                derOut.WriteByte(0x80);

                if (!empty)
                {
                    if (!explicitly)
                    {
                        if (obj is ASN1OctetString)
                        {
                            IEnumerator e;

                            if (obj is BEROctetString)
                            {
                                e = ((BEROctetString)obj).getObjects();
                            }
                            else
                            {
                                ASN1OctetString octs = (ASN1OctetString)obj;
                                BEROctetString  berO = new BEROctetString(octs.getOctets());

                                e = berO.getObjects();
                            }

                            while (e.MoveNext())
                            {
                                derOut.writeObject(e.Current);
                            }
                        }
                        else if (obj is ASN1Sequence)
                        {
                            IEnumerator e = ((ASN1Sequence)obj).getObjects();

                            while (e.MoveNext())
                            {
                                derOut.writeObject(e.Current);
                            }
                        }
                        else if (obj is ASN1Set)
                        {
                            IEnumerator e = ((ASN1Set)obj).getObjects();

                            while (e.MoveNext())
                            {
                                derOut.writeObject(e.Current);
                            }
                        }
                        else
                        {
                            throw new Exception("not implemented: " + obj.GetType().Name);
                        }
                    }
                    else
                    {
                        derOut.writeObject(obj);
                    }
                }

                derOut.WriteByte(0x00);
                derOut.WriteByte(0x00);
            }
            else
            {
                base.encode(derOut);
            }
        }
Ejemplo n.º 21
0
 internal override void encode(DEROutputStream derOut)
 {
     throw new IOException("Eeek!");
 }