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
|
using System;
using System.Diagnostics;
using Org.BouncyCastle.Utilities;
namespace Org.BouncyCastle.Asn1
{
public class BerBitString
: DerBitString
{
private const int DefaultSegmentLimit = 1000;
internal static byte[] FlattenBitStrings(DerBitString[] bitStrings)
{
int count = bitStrings.Length;
switch (count)
{
case 0:
// No bits
return new byte[]{ 0 };
case 1:
return bitStrings[0].contents;
default:
{
int last = count - 1, totalLength = 0;
for (int i = 0; i < last; ++i)
{
byte[] elementContents = bitStrings[i].contents;
if (elementContents[0] != 0)
throw new ArgumentException("only the last nested bitstring can have padding", "bitStrings");
totalLength += elementContents.Length - 1;
}
// Last one can have padding
byte[] lastElementContents = bitStrings[last].contents;
byte padBits = lastElementContents[0];
totalLength += lastElementContents.Length;
byte[] contents = new byte[totalLength];
contents[0] = padBits;
int pos = 1;
for (int i = 0; i < count; ++i)
{
byte[] elementContents = bitStrings[i].contents;
int length = elementContents.Length - 1;
Array.Copy(elementContents, 1, contents, pos, length);
pos += length;
}
Debug.Assert(pos == totalLength);
return contents;
}
}
}
private readonly int segmentLimit;
private readonly DerBitString[] elements;
public BerBitString(byte data, int padBits)
: base(data, padBits)
{
this.elements = null;
this.segmentLimit = DefaultSegmentLimit;
}
public BerBitString(byte[] data)
: this(data, 0)
{
}
public BerBitString(byte[] data, int padBits)
: this(data, padBits, DefaultSegmentLimit)
{
}
public BerBitString(byte[] data, int padBits, int segmentLimit)
: base(data, padBits)
{
this.elements = null;
this.segmentLimit = segmentLimit;
}
public BerBitString(int namedBits)
: base(namedBits)
{
this.elements = null;
this.segmentLimit = DefaultSegmentLimit;
}
public BerBitString(Asn1Encodable obj)
: this(obj.GetDerEncoded(), 0)
{
}
public BerBitString(DerBitString[] elements)
: this(elements, DefaultSegmentLimit)
{
}
public BerBitString(DerBitString[] elements, int segmentLimit)
: base(FlattenBitStrings(elements), false)
{
this.elements = elements;
this.segmentLimit = segmentLimit;
}
internal BerBitString(byte[] contents, bool check)
: base(contents, check)
{
this.elements = null;
this.segmentLimit = DefaultSegmentLimit;
}
private bool IsConstructed
{
get { return null != elements || contents.Length > segmentLimit; }
}
internal override int EncodedLength(bool withID)
{
throw Platform.CreateNotImplementedException("BerBitString.EncodedLength");
// TODO This depends on knowing it's not DER
//if (!IsConstructed)
// return EncodedLength(withID, contents.Length);
//int totalLength = withID ? 4 : 3;
//if (null != elements)
//{
// for (int i = 0; i < elements.Length; ++i)
// {
// totalLength += elements[i].EncodedLength(true);
// }
//}
//else if (contents.Length < 2)
//{
// // No bits
//}
//else
//{
// int extraSegments = (contents.Length - 2) / (segmentLimit - 1);
// totalLength += extraSegments * EncodedLength(true, segmentLimit);
// int lastSegmentLength = contents.Length - (extraSegments * (segmentLimit - 1));
// totalLength += EncodedLength(true, lastSegmentLength);
//}
//return totalLength;
}
internal override void Encode(Asn1OutputStream asn1Out, bool withID)
{
if (!asn1Out.IsBer)
{
base.Encode(asn1Out, withID);
return;
}
if (!IsConstructed)
{
Encode(asn1Out, withID, contents, 0, contents.Length);
return;
}
asn1Out.WriteIdentifier(withID, Asn1Tags.Constructed | Asn1Tags.BitString);
asn1Out.WriteByte(0x80);
if (null != elements)
{
asn1Out.WritePrimitives(elements);
}
else if (contents.Length < 2)
{
// No bits
}
else
{
byte pad = contents[0];
int length = contents.Length;
int remaining = length - 1;
int segmentLength = segmentLimit - 1;
while (remaining > segmentLength)
{
Encode(asn1Out, true, (byte)0, contents, length - remaining, segmentLength);
remaining -= segmentLength;
}
Encode(asn1Out, true, pad, contents, length - remaining, remaining);
}
asn1Out.WriteByte(0x00);
asn1Out.WriteByte(0x00);
}
}
}
|