Example #1
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);
            }
        }
Example #2
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();
        }
Example #3
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());
            }
        }
Example #4
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);
        }
Example #5
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]);
            }
        }
Example #6
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);
            }
        }
Example #7
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);
            }
        }