/
valid.go
215 lines (200 loc) · 5.41 KB
/
valid.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
// Copyright (c) 2019 Faye Amacker. All rights reserved.
// Use of this source code is governed by a MIT license found in the LICENSE file.
package cbor
import (
"encoding/binary"
"errors"
"io"
"strconv"
)
// SyntaxError is a description of a CBOR syntax error.
type SyntaxError struct {
msg string
}
func (e *SyntaxError) Error() string { return e.msg }
// SemanticError is a description of a CBOR semantic error.
type SemanticError struct {
msg string
}
func (e *SemanticError) Error() string { return e.msg }
// Valid checks whether CBOR data is complete and well-formed.
func Valid(data []byte) (rest []byte, err error) {
if len(data) == 0 {
return nil, io.EOF
}
offset, _, err := valid(data, 0, 1)
if err != nil {
return nil, err
}
return data[offset:], nil
}
const (
maxNestingLevel = 32
)
func valid(data []byte, off int, depth int) (int, int, error) {
if depth > maxNestingLevel {
return 0, 0, errors.New("cbor: reached max depth " + strconv.Itoa(maxNestingLevel))
}
off, t, ai, val, err := validHead(data, off)
if err != nil {
return 0, 0, err
}
if ai == 31 {
if t == cborTypeByteString || t == cborTypeTextString {
return validIndefiniteString(data, off, t, depth)
}
return validIndefiniteArrOrMap(data, off, t, depth)
}
switch t {
case cborTypeByteString, cborTypeTextString:
valInt := int(val)
if valInt < 0 {
// Detect integer overflow
return 0, 0, errors.New("cbor: " + t.String() + " length " + strconv.FormatUint(val, 10) + " is too large, causing integer overflow")
}
if len(data)-off < valInt { // valInt+off may overflow integer
return 0, 0, io.ErrUnexpectedEOF
}
off += valInt
case cborTypeArray, cborTypeMap:
valInt := int(val)
if valInt < 0 {
// Detect integer overflow
return 0, 0, errors.New("cbor: " + t.String() + " length " + strconv.FormatUint(val, 10) + " is too large, causing integer overflow")
}
count := 1
if t == cborTypeMap {
count = 2
}
maxDepth := depth
for j := 0; j < count; j++ {
for i := 0; i < valInt; i++ {
var d int
if off, d, err = valid(data, off, depth+1); err != nil {
return 0, 0, err
}
if d > maxDepth {
maxDepth = d // Save max depth
}
}
}
depth = maxDepth
case cborTypeTag:
// Scan nested tag numbers to avoid recursion.
for true {
if len(data)-off < 1 { // Tag number must be followed by tag content.
return 0, 0, io.ErrUnexpectedEOF
}
if cborType(data[off]&0xE0) != cborTypeTag {
break
}
if off, _, _, _, err = validHead(data, off); err != nil {
return 0, 0, err
}
depth++
}
// Check tag content.
if off, depth, err = valid(data, off, depth); err != nil {
return 0, 0, err
}
}
return off, depth, nil
}
func validIndefiniteString(data []byte, off int, t cborType, depth int) (int, int, error) {
var err error
for true {
if len(data)-off < 1 {
return 0, 0, io.ErrUnexpectedEOF
}
if data[off] == 0xFF {
off++
break
}
// Peek ahead to get next type and indefinite length status.
nt := cborType(data[off] & 0xE0)
if t != nt {
return 0, 0, &SyntaxError{"cbor: wrong element type " + nt.String() + " for indefinite-length " + t.String()}
}
if (data[off] & 0x1F) == 31 {
return 0, 0, &SyntaxError{"cbor: indefinite-length " + t.String() + " chunk is not definite-length"}
}
if off, depth, err = valid(data, off, depth); err != nil {
return 0, 0, err
}
}
return off, depth, nil
}
func validIndefiniteArrOrMap(data []byte, off int, t cborType, depth int) (int, int, error) {
var err error
maxDepth := depth
i := 0
for ; true; i++ {
if len(data)-off < 1 {
return 0, 0, io.ErrUnexpectedEOF
}
if data[off] == 0xFF {
off++
break
}
var d int
if off, d, err = valid(data, off, depth+1); err != nil {
return 0, 0, err
}
if d > maxDepth {
maxDepth = d
}
}
if t == cborTypeMap && i%2 == 1 {
return 0, 0, &SyntaxError{"cbor: unexpected \"break\" code"}
}
return off, maxDepth, nil
}
func validHead(data []byte, off int) (_ int, t cborType, ai byte, val uint64, err error) {
dataLen := len(data) - off
if dataLen < 1 {
return 0, 0, 0, 0, io.ErrUnexpectedEOF
}
t = cborType(data[off] & 0xE0)
ai = data[off] & 0x1F
val = uint64(ai)
off++
switch ai {
case 24:
if dataLen < 2 {
return 0, 0, 0, 0, io.ErrUnexpectedEOF
}
val = uint64(data[off])
off++
case 25:
if dataLen < 3 {
return 0, 0, 0, 0, io.ErrUnexpectedEOF
}
val = uint64(binary.BigEndian.Uint16(data[off : off+2]))
off += 2
case 26:
if dataLen < 5 {
return 0, 0, 0, 0, io.ErrUnexpectedEOF
}
val = uint64(binary.BigEndian.Uint32(data[off : off+4]))
off += 4
case 27:
if dataLen < 9 {
return 0, 0, 0, 0, io.ErrUnexpectedEOF
}
val = binary.BigEndian.Uint64(data[off : off+8])
off += 8
case 28, 29, 30:
return 0, 0, 0, 0, &SyntaxError{"cbor: invalid additional information " + strconv.Itoa(int(ai)) + " for type " + t.String()}
case 31:
switch t {
case cborTypePositiveInt, cborTypeNegativeInt, cborTypeTag:
return 0, 0, 0, 0, &SyntaxError{"cbor: invalid additional information " + strconv.Itoa(int(ai)) + " for type " + t.String()}
case cborTypePrimitives: // 0xFF (break code) should not be outside validIndefinite().
return 0, 0, 0, 0, &SyntaxError{"cbor: unexpected \"break\" code"}
}
}
if t == cborTypePrimitives && ai == 24 && val < 32 {
return 0, 0, 0, 0, &SyntaxError{"cbor: invalid simple value " + strconv.Itoa(int(val)) + " for type " + t.String()}
}
return off, t, ai, val, nil
}