This repository has been archived by the owner on Mar 13, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
/
raw.go
334 lines (283 loc) · 7 KB
/
raw.go
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
package asn1
import (
"bytes"
"io"
"io/ioutil"
"strconv"
)
// ASN.1 class tags.
const (
classUniversal = 0x00
classApplication = 0x01
classContextSpecific = 0x02
classPrivate = 0x03
)
// ASN.1 universal tag numbers.
const (
tagEoc = 0x00
tagBoolean = 0x01
tagInteger = 0x02
tagBitString = 0x03
tagOctetString = 0x04
tagNull = 0x05
tagOid = 0x06
tagSequence = 0x10
tagSet = 0x11
tagPrintableString = 0x13
tagT61String = 0x14
tagIA5String = 0x16
tagUtcTime = 0x17
)
// Internal consts
const (
intBits = strconv.IntSize
intBytes = intBits / 8
)
type rawValue struct {
Class uint
Tag uint
Constructed bool
Indefinite bool
Content []byte
}
func (raw *rawValue) encode() ([]byte, error) {
if raw == nil {
return []byte{}, nil
}
buf, err := encodeIdentifier(raw)
if err != nil {
return nil, err
}
// Add length information and raw data
if !raw.Indefinite {
length := uint(len(raw.Content))
buf = append(buf, encodeLength(length)...)
buf = append(buf, raw.Content...)
} else {
// Indefinite length uses 0x80, data..., 0x00, 0x00
if !raw.Constructed {
return nil, syntaxError("indefinite length is only allowed to constructed types")
}
buf = append(buf, 0x80)
buf = append(buf, raw.Content...)
buf = append(buf, 0x00, 0x00)
}
return buf, nil
}
func encodeIdentifier(node *rawValue) ([]byte, error) {
if node.Class > 0x03 {
return nil, syntaxError("invalid class value: %d", node.Class)
}
identifier := []byte{0x00}
// Class (bits 7 and 6) + primitive/constructed (1 bit) + tag (5 bits)
identifier[0] += byte((node.Class & 0x03) << 6)
// Primitive/constructed (bit 5)
if node.Constructed {
identifier[0] += byte(1 << 5)
}
// Tag (bits 4 to 0)
if node.Tag <= 30 {
identifier[0] += byte(0x1f & node.Tag)
} else {
identifier[0] += 0x1f
identifier = append(identifier, encodeMultiByteTag(node.Tag)...)
}
return identifier, nil
}
func encodeMultiByteTag(tag uint) []byte {
// A tag is encoded in a big endian sequence of octets each one holding a 7 bit value.
// The most significant bit of each octet must be 1, with the exception of the last octet that must be zero.
// Example: 1xxxxxxx 1xxxxxxx ... 0xxxxxxx
// An int32 needs 5 octets and an int64 needs 10:
bufLen := (intBits-1)/7 + 1
buf := make([]byte, bufLen)
for i := range buf {
shift := uint(7 * (len(buf) - i - 1))
mask := uint(0x7f << shift)
buf[i] = byte((tag & mask) >> shift)
// Only the last byte is not marked with 0x80
if i != len(buf)-1 {
buf[i] |= 0x80
}
}
// Discard leading zero values
return removeLeadingBytes(buf, 0x80)
}
func encodeLength(length uint) []byte {
// The first bit indicates if length is encoded in a single byte
if length < 0x80 {
return []byte{byte(length)}
}
// Multi byte length follow the rules:
// - First byte: 0x80 + N (number of following bytes where length is encoded).
// - N bytes
// A byte slice length is an int. So we just need at most 4 bytes
buf := make([]byte, intBytes)
for i := range buf {
shift := uint((intBytes - i - 1) * 8)
mask := uint(0xff << shift)
buf[i] = byte((mask & length) >> shift)
}
// Ignore leading zeros
buf = removeLeadingBytes(buf, 0x00)
// Add leading byte with the number of following bytes
buf = append([]byte{0x80 + byte(len(buf))}, buf...)
return buf
}
func removeLeadingBytes(buf []byte, target byte) []byte {
start := 0
for start < len(buf)-1 && buf[start] == target {
start++
}
return buf[start:]
}
func decodeRawValue(reader io.Reader) (*rawValue, error) {
class, tag, constructed, err := decodeIdentifier(reader)
if err != nil {
return nil, err
}
length, indefinite, err := decodeLength(reader)
if err != nil {
return nil, err
}
if indefinite && !constructed {
return nil, parseError("primitive node with indefinite length")
}
// Indefinite form
var content []byte
if !indefinite {
content = make([]byte, length)
_, err = io.ReadFull(reader, content)
if err != nil {
return nil, err
}
} else {
buffer := bytes.NewBuffer([]byte{})
childrenReader := io.TeeReader(reader, buffer)
err := readEoc(childrenReader)
if err != nil {
return nil, err
}
// At this point, buffer also contains the EoC bytes
content = buffer.Bytes()
content = content[:len(content)-2]
}
raw := rawValue{class, tag, constructed, indefinite, content}
return &raw, nil
}
func readEoc(reader io.Reader) error {
for {
class, tag, constructed, err := decodeIdentifier(reader)
if err != nil {
return err
}
length, indefinite, err := decodeLength(reader)
if err != nil {
return err
}
if indefinite && !constructed {
return parseError("primitive node with indefinite length")
}
if class == 0 && tag == 0 && indefinite == false && length == 0 {
break
}
if indefinite {
err = readEoc(reader)
} else {
err = skipBytes(reader, int64(length))
}
if err != nil {
return err
}
}
return nil
}
func decodeMultiByteTag(reader io.Reader) (uint, error) {
// Tag is encoded in one or more following bytes
tag := uint(0)
for {
// Read a byte
b, err := readByte(reader)
if err != nil {
return 0, err
}
// if we need to shift out non zeros bits, so the tag is too big for an uint
msb := uint64(0xfe) << (intBits - 8) // 7 most significant bits
if uint64(tag)&msb != 0 {
return 0, parseError("multi byte tag too big")
}
// Shift the previous value and add the new 7 bits
tag = (tag << 7) | uint(b&0x7f)
// The last byte is indicated by the most significant bit equals to zero
if b&0x80 == 0 {
break
}
}
return tag, nil
}
func decodeIdentifier(reader io.Reader) (class uint, tag uint, constructed bool, err error) {
b, err := readByte(reader)
if err != nil {
return
}
// Read class and constructed flag
class = uint((b & 0xc0) >> 6)
if b&0x20 != 0 {
constructed = true
}
// Read the tag number
tag = uint(b & 0x1f)
if tag == 0x1f {
// Tag is encoded in one or more following bytes
tag, err = decodeMultiByteTag(reader)
if err != nil {
return
}
}
return
}
func decodeLength(reader io.Reader) (length uint, indefinite bool, err error) {
// Read length
b, err := readByte(reader)
if err != nil {
return
}
// Short form
if b&0x80 == 0 {
length = uint(b)
return
}
// Indefinite form
if b == 0x80 {
indefinite = true
return
}
// Long form
if b == 0xff {
err = parseError("invalid number of length octets: %x", b)
return
}
octets := make([]byte, int(b&0x7f))
_, err = io.ReadFull(reader, octets)
if err != nil {
return
}
for _, b = range octets {
msb := uint64(0xff) << (intBits - 8)
if uint64(length)&msb != 0 {
err = parseError("multi byte length too big")
return
}
length = (length << 8) | uint(b)
}
return
}
func readByte(reader io.Reader) (byte, error) {
buf := []byte{0x00}
_, err := io.ReadFull(reader, buf)
return buf[0], err
}
func skipBytes(reader io.Reader, count int64) error {
_, err := io.CopyN(ioutil.Discard, reader, count)
return err
}