summary refs log tree commit diff
path: root/crypto/src/asn1/BerOctetString.cs
blob: fb42916124dfd040d68f2fb74f4f6dfa1ebef976 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
using System;
using System.Collections;
using System.IO;

using Org.BouncyCastle.Utilities;

namespace Org.BouncyCastle.Asn1
{
    public class BerOctetString
        : DerOctetString, IEnumerable
    {
        private static readonly int DefaultChunkSize = 1000;

        public static BerOctetString FromSequence(Asn1Sequence seq)
        {
            int count = seq.Count;
            Asn1OctetString[] v = new Asn1OctetString[count];
            for (int i = 0; i < count; ++i)
            {
                v[i] = Asn1OctetString.GetInstance(seq[i]);
            }
            return new BerOctetString(v);
        }

        private static byte[] ToBytes(Asn1OctetString[] octs)
        {
            MemoryStream bOut = new MemoryStream();
            foreach (Asn1OctetString o in octs)
            {
                byte[] octets = o.GetOctets();
                bOut.Write(octets, 0, octets.Length);
            }
            return bOut.ToArray();
        }

        private static Asn1OctetString[] ToOctetStringArray(IEnumerable e)
        {
            IList list = Platform.CreateArrayList(e);

            int count = list.Count;
            Asn1OctetString[] v = new Asn1OctetString[count];
            for (int i = 0; i < count; ++i)
            {
                v[i] = Asn1OctetString.GetInstance(list[i]);
            }
            return v;
        }

        private readonly int chunkSize;
        private readonly Asn1OctetString[] octs;

        [Obsolete("Will be removed")]
        public BerOctetString(IEnumerable e)
            : this(ToOctetStringArray(e))
        {
        }

        public BerOctetString(byte[] str)
			: this(str, DefaultChunkSize)
		{
		}

        public BerOctetString(Asn1OctetString[] octs)
            : this(octs, DefaultChunkSize)
        {
        }

        public BerOctetString(byte[] str, int chunkSize)
            : this(str, null, chunkSize)
        {
        }

        public BerOctetString(Asn1OctetString[] octs, int chunkSize)
            : this(ToBytes(octs), octs, chunkSize)
        {
        }

        private BerOctetString(byte[] str, Asn1OctetString[] octs, int chunkSize)
            : base(str)
        {
            this.octs = octs;
            this.chunkSize = chunkSize;
        }

        /**
         * return the DER octets that make up this string.
         */
		public IEnumerator GetEnumerator()
		{
			if (octs == null)
			{
				return GenerateOcts().GetEnumerator();
			}

			return octs.GetEnumerator();
		}

		[Obsolete("Use GetEnumerator() instead")]
        public IEnumerator GetObjects()
        {
			return GetEnumerator();
		}

		private IList GenerateOcts()
        {
            IList vec = Platform.CreateArrayList();
            for (int i = 0; i < str.Length; i += chunkSize)
            { 
				int end = System.Math.Min(str.Length, i + chunkSize);

                byte[] nStr = new byte[end - i]; 

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

                vec.Add(new DerOctetString(nStr));
             } 
             return vec; 
        }

        internal override void Encode(
            DerOutputStream derOut)
        {
            if (derOut is Asn1OutputStream || derOut is BerOutputStream)
            {
                derOut.WriteByte(Asn1Tags.Constructed | Asn1Tags.OctetString);

                derOut.WriteByte(0x80);

                //
                // write out the octet array
                //
                foreach (Asn1OctetString oct in this)
                {
                    derOut.WriteObject(oct);
                }

				derOut.WriteByte(0x00);
                derOut.WriteByte(0x00);
            }
            else
            {
                base.Encode(derOut);
            }
        }
    }
}