summary refs log tree commit diff
path: root/crypto/src/asn1/DerBitString.cs
blob: d5cb872bc0daa433001d7b5a72d37b2b950b20c3 (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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
using System;
using System.Text;

using Org.BouncyCastle.Utilities;

namespace Org.BouncyCastle.Asn1
{
	public class DerBitString
		: DerStringBase
	{
		private static readonly char[] table
			= { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };

		private readonly byte[]	data;
		private readonly int	padBits;

		/**
		 * return the correct number of pad bits for a bit string defined in
		 * a 32 bit constant
		 */
		static internal int GetPadBits(
			int bitString)
		{
			int val = 0;
			for (int i = 3; i >= 0; i--)
			{
				//
				// this may look a little odd, but if it isn't done like this pre jdk1.2
				// JVM's break!
				//
				if (i != 0)
				{
					if ((bitString >> (i * 8)) != 0)
					{
						val = (bitString >> (i * 8)) & 0xFF;
						break;
					}
				}
				else
				{
					if (bitString != 0)
					{
						val = bitString & 0xFF;
						break;
					}
				}
			}

			if (val == 0)
			{
				return 7;
			}

			int bits = 1;

			while (((val <<= 1) & 0xFF) != 0)
			{
				bits++;
			}

			return 8 - bits;
		}

		/**
		 * return the correct number of bytes for a bit string defined in
		 * a 32 bit constant
		 */
		static internal byte[] GetBytes(
			int bitString)
		{
			int bytes = 4;
			for (int i = 3; i >= 1; i--)
			{
				if ((bitString & (0xFF << (i * 8))) != 0)
				{
					break;
				}
				bytes--;
			}

			byte[] result = new byte[bytes];
			for (int i = 0; i < bytes; i++)
			{
				result[i] = (byte) ((bitString >> (i * 8)) & 0xFF);
			}

			return result;
		}

		/**
		 * return a Bit string from the passed in object
		 *
		 * @exception ArgumentException if the object cannot be converted.
		 */
		public static DerBitString GetInstance(
			object obj)
		{
			if (obj == null || obj is DerBitString)
			{
				return (DerBitString) obj;
			}

			throw new ArgumentException("illegal object in GetInstance: " + obj.GetType().Name);
		}

		/**
		 * return a Bit string from a tagged object.
		 *
		 * @param obj the tagged object holding the object we want
		 * @param explicitly true if the object is meant to be explicitly
		 *              tagged false otherwise.
		 * @exception ArgumentException if the tagged object cannot
		 *               be converted.
		 */
		public static DerBitString GetInstance(
			Asn1TaggedObject	obj,
			bool				isExplicit)
		{
			Asn1Object o = obj.GetObject();

			if (isExplicit || o is DerBitString)
			{
				return GetInstance(o);
			}

			return FromAsn1Octets(((Asn1OctetString)o).GetOctets());
		}

		internal DerBitString(
			byte	data,
			int		padBits)
		{
			this.data = new byte[]{ data };
			this.padBits = padBits;
		}

		/**
		 * @param data the octets making up the bit string.
		 * @param padBits the number of extra bits at the end of the string.
		 */
		public DerBitString(
			byte[]	data,
			int		padBits)
		{
			// TODO Deep copy?
			this.data = data;
			this.padBits = padBits;
		}

		public DerBitString(
			byte[] data)
		{
			// TODO Deep copy?
			this.data = data;
		}

		public DerBitString(
			Asn1Encodable obj)
		{
			this.data = obj.GetDerEncoded();
			//this.padBits = 0;
		}

		public byte[] GetBytes()
		{
			return data;
		}

		public int PadBits
		{
			get { return padBits; }
		}

		/**
		 * @return the value of the bit string as an int (truncating if necessary)
		 */
		public int IntValue
		{
			get
			{
				int value = 0;

				for (int i = 0; i != data.Length && i != 4; i++)
				{
					value |= (data[i] & 0xff) << (8 * i);
				}

				return value;
			}
		}

		internal override void Encode(
			DerOutputStream derOut)
		{
			byte[] bytes = new byte[GetBytes().Length + 1];

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

			derOut.WriteEncoded(Asn1Tags.BitString, bytes);
		}

		protected override int Asn1GetHashCode()
		{
			return padBits.GetHashCode() ^ Arrays.GetHashCode(data);
		}

		protected override bool Asn1Equals(
			Asn1Object asn1Object)
		{
			DerBitString other = asn1Object as DerBitString;

			if (other == null)
				return false;

			return this.padBits == other.padBits
				&& Arrays.AreEqual(this.data, other.data);
		}

		public override string GetString()
		{
			StringBuilder buffer = new StringBuilder("#");

			byte[] str = GetDerEncoded();

			for (int i = 0; i != str.Length; i++)
			{
				uint ubyte = str[i];
				buffer.Append(table[(ubyte >> 4) & 0xf]);
				buffer.Append(table[str[i] & 0xf]);
			}

			return buffer.ToString();
		}

		internal static DerBitString FromAsn1Octets(byte[] octets)
		{
	        if (octets.Length < 1)
	            throw new ArgumentException("truncated BIT STRING detected");

			int padBits = octets[0];
			byte[] data = new byte[octets.Length - 1];
			Array.Copy(octets, 1, data, 0, data.Length);
			return new DerBitString(data, padBits);
		}
	}
}