-
Notifications
You must be signed in to change notification settings - Fork 1
/
dot.go
557 lines (475 loc) · 11.1 KB
/
dot.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
package objectid
import "math/big"
/*
DotNotation contains an ordered sequence of [NumberForm] instances.
*/
type DotNotation []NumberForm
/*
String is a stringer method that returns the dot notation form of the
receiver (e.g.: "1.3.6.1").
*/
func (r DotNotation) String() (s string) {
if !r.IsZero() {
var x []string
for i := 0; i < len(r); i++ {
x = append(x, r[i].String())
}
s = join(x, `.`)
}
return
}
/*
Root returns the root node (0) [NumberForm] instance.
*/
func (r DotNotation) Root() NumberForm {
x, _ := r.Index(0)
return x
}
func (r DotNotation) Len() int {
return len(r)
}
/*
Leaf returns the leaf-node (-1) [NumberForm] instance.
*/
func (r DotNotation) Leaf() NumberForm {
x, _ := r.Index(-1)
return x
}
/*
Parent returns the leaf-node's parent (-2) [NumberForm] instance.
*/
func (r DotNotation) Parent() NumberForm {
x, _ := r.Index(-2)
return x
}
/*
IsZero returns a Boolean indicative of whether the receiver is unset.
*/
func (r *DotNotation) IsZero() (is bool) {
if r != nil {
is = r.Len() == 0
}
return
}
/*
NewDotNotation returns an instance of *[DotNotation] alongside a Boolean
value indicative of success.
Variadic input allows for slice mixtures of all of the following types,
each treated as an individual [NumberForm] instance:
- *[math/big.Int]
- [NumberForm]
- string
- uint64
- uint
- int
If a string primitive is the only input option, it will be treated as a
complete [DotNotation] (e.g.: "1.3.6").
*/
func NewDotNotation(x ...any) (r *DotNotation, err error) {
var _d DotNotation = make(DotNotation, 0)
if len(x) == 1 {
if slice, ok := x[0].(string); ok {
r, err = newDotNotationStr(slice)
return
}
}
for i := 0; i < len(x) && err == nil; i++ {
var nf NumberForm
switch tv := x[i].(type) {
case NumberForm:
if !tv.Valid() {
err = errorf("Unsupported %T value", tv)
break
}
nf = tv
case *big.Int, string, uint64, uint, int:
nf, err = NewNumberForm(tv)
default:
err = errorf("Unsupported slice type '%T' for OID", tv)
}
_d = append(_d, nf)
}
if err == nil {
r = new(DotNotation)
*r = _d
}
return
}
func newDotNotationStr(dot string) (r *DotNotation, err error) {
if !isNumericOID(dot) {
err = errorf("Invalid OID '%s' cannot be processed", dot)
return
}
z := split(dot, `.`)
_d := make(DotNotation, 0)
for j := 0; j < len(z) && err == nil; j++ {
var nf NumberForm
if nf, err = NewNumberForm(z[j]); err == nil {
_d = append(_d, nf)
}
}
if err == nil {
r = new(DotNotation)
*r = _d
}
return
}
/*
Encode returns the ASN.1 encoding of the receiver instance alongside an error.
*/
func (r DotNotation) Encode() (b []byte, err error) {
if r.Len() < 2 {
err = errorf("Length below encoding minimum")
return
}
var start int
firstArc := r[0].cast()
forty := big.NewInt(40)
firstArc.Mul(firstArc, forty)
if r[1].cast().Cmp(forty) < 1 {
// this is meant for second level arcs <= 39
firstArc.Add(firstArc, r[1].cast()) // first + arc2
if firstBytes := firstArc.Bytes(); len(firstBytes) == 0 {
// We need the explicit zero byte, not an
// empty []byte{} instance. This effort
// is really needed for OID "0.0".
b = append([]byte{0x00}, b...)
} else {
b = append(firstBytes, b...)
}
start = 2
} else {
if r[0].cast().Uint64() != 2 {
err = errorf("Only joint-iso-itu-t(2) OIDs allow second-level arcs > 39")
return
}
// Multi-Byte encoding for second-level arcs
// below joint-iso-itu-t(2), such as "999" for
// "2.999", that are larger than 39. Instead,
// skip the Addition operation we'd normally
// perform, and just begin VLQ encoding each
// subsequent byte.
start = 1
}
if len(r) > start {
for i := start; i < len(r); i++ {
b = append(b, encodeVLQ(r[i].cast().Bytes())...)
}
}
b = append([]byte{byte(len(b))}, b...) // byte representation of int length of byte slice b
b = append([]byte{0x06}, b...) // ASN.1 Object Identifier Tag (0x06)
return
}
/*
Decode returns an error following an attempt to parse b, which must be
the ASN.1 encoding of an OID, into the receiver instance. The receiver
instance is reinitialized at runtime.
*/
func (r *DotNotation) Decode(b []byte) (err error) {
if len(b) < 3 {
err = errorf("Truncated OID encoding")
return
}
if b[0] != 0x06 {
err = errorf("Invalid ASN.1 Tag; want: 0x06")
return
}
length := int(b[1])
b = b[2:]
if length != len(b) {
err = errorf("Length of bytes does not match with the indicated length")
return
}
var (
i int
subidentifier *big.Int = big.NewInt(0)
)
*r = make(DotNotation, 0)
for i < len(b) {
for {
subidentifier.Lsh(subidentifier, 7)
subidentifier.Add(subidentifier, big.NewInt(int64(b[i]&0x7F)))
if b[i]&0x80 == 0 {
break
}
i++
}
i++
*r = append(*r, NumberForm(*subidentifier))
subidentifier = big.NewInt(0)
}
if len(*r) > 0 {
r.decodeFirstArcs(b[0])
}
return
}
func (r *DotNotation) decodeFirstArcs(b byte) {
var firstArc *big.Int
var secondArc *big.Int
var forty *big.Int = big.NewInt(40)
var eighty *big.Int = big.NewInt(80)
if (*r)[0].cast().Cmp(big.NewInt(80)) < 0 {
firstArc = big.NewInt(0).Div((*r)[0].cast(), forty)
secondArc = big.NewInt(0).Mod((*r)[0].cast(), forty)
} else {
firstArc = big.NewInt(2)
if b >= 0x80 {
// Handle large second-level arcs
secondArc = big.NewInt(0).Sub((*r)[0].cast(), firstArc)
secondArc.Add(secondArc, firstArc)
} else {
secondArc = big.NewInt(0).Sub((*r)[0].cast(), eighty)
}
}
(*r)[0] = NumberForm(*secondArc)
*r = append(DotNotation{NumberForm(*firstArc)}, *r...)
}
/*
IntSlice returns slices of integer values and an error. The integer values are based
upon the contents of the receiver. Note that if any single arc number overflows int,
a zero slice is returned.
Successful output can be cast as an instance of [encoding/asn1.ObjectIdentifier], if desired.
*/
func (r DotNotation) IntSlice() (slice []int, err error) {
if r.IsZero() {
return
}
var t []int
for i := 0; i < len(r); i++ {
var n int
if n, err = atoi(r[i].String()); err != nil {
return
}
t = append(t, n)
}
if len(t) > 0 {
slice = t[:]
}
return
}
/*
Uint64Slice returns slices of uint64 values and an error. The uint64
values are based upon the contents of the receiver.
Note that if any single arc number overflows uint64, a zero slice is
returned alongside an error.
Successful output can be cast as an instance of [crypto/x509.OID], if
desired.
*/
func (r DotNotation) Uint64Slice() (slice []uint64, err error) {
if r.IsZero() {
return
}
var t []uint64
for i := 0; i < len(r); i++ {
var n uint64
if n, err = puint64(r[i].String(), 10, 64); err != nil {
return
}
t = append(t, n)
}
if len(t) > 0 {
slice = t[:]
}
return
}
/*
Index returns the Nth index from the receiver, alongside a Boolean
value indicative of success. This method supports the use of negative
indices.
*/
func (r DotNotation) Index(idx int) (a NumberForm, ok bool) {
if L := len(r); L > 0 {
if idx < 0 {
a = r[0]
if x := L + idx; x >= 0 {
a = r[x]
}
} else if idx > L {
a = r[L-1]
} else if idx < L {
a = r[idx]
}
ok = !a.IsZero()
}
return
}
/*
Ancestry returns slices of [DotNotation] values ordered from leaf node
(first) to root node (last).
Empty slices of [DotNotation] are returned if the dot notation value
within the receiver is less than two (2) [NumberForm] values in length.
*/
func (r DotNotation) Ancestry() (anc []DotNotation) {
if r.Len() > 0 {
for i := r.Len(); i > 0; i-- {
anc = append(anc, r[:i])
}
}
return
}
/*
AncestorOf returns a Boolean value indicative of whether the receiver
is an ancestor of the input value, which can be string or [DotNotation].
*/
func (r DotNotation) AncestorOf(dot any) (is bool) {
if !r.IsZero() {
if D := assertDotNot(dot); !D.IsZero() {
if D.Len() > r.Len() {
is = r.matchDotNot(D, 0)
}
}
}
return
}
/*
ChildOf returns a Boolean value indicative of whether the receiver is
a direct superior (parent) of the input value, which can be string or
[ASN1Notation].
*/
func (r DotNotation) ChildOf(asn any) (cof bool) {
if !r.IsZero() {
if D := assertDotNot(asn); !D.IsZero() {
if D.Len()-1 == r.Len() {
cof = r.matchDotNot(D, 0)
}
}
}
return
}
/*
SiblingOf returns a Boolean value indicative of whether the receiver is
a sibling of the input value, which can be string or [ASN1Notation].
*/
func (r DotNotation) SiblingOf(dot any) (sof bool) {
if !r.IsZero() {
if D := assertDotNot(dot); !D.IsZero() {
if D.Len() == r.Len() && !D.Leaf().Equal(r.Leaf()) {
sof = r.matchDotNot(D, -1)
}
}
}
return
}
func assertDotNot(dot any) (D *DotNotation) {
switch tv := dot.(type) {
case string:
D, _ = NewDotNotation(tv)
case *DotNotation:
if tv != nil {
D = tv
}
case DotNotation:
if tv.Len() >= 0 {
D = &tv
}
}
return
}
func (r DotNotation) matchDotNot(dot *DotNotation, off int) bool {
L := r.Len()
ct := 0
for i := 0; i < L; i++ {
x, _ := r.Index(i)
if y, ok := dot.Index(i); ok {
if x.Equal(y) {
ct++
} else if off == -1 && L-1 == i {
// sibling check should end in
// a FAILED match for the final
// arcs.
ct++
}
}
}
return ct == L
}
/*
NewSubordinate returns a new instance of [DotNotation] based upon the
contents of the receiver as well as the input [NumberForm] subordinate
value. This creates a fully-qualified child [DotNotation] value of the
receiver.
*/
func (r DotNotation) NewSubordinate(nf any) (dot *DotNotation) {
if r.Len() > 0 {
// Prepare the new leaf numberForm, or die trying.
if a, err := NewNumberForm(nf); err == nil {
D := make(DotNotation, r.Len()+1, r.Len()+1)
for i := 0; i < r.Len(); i++ {
D[i] = r[i]
}
D[D.Len()-1] = a
dot = &D
}
}
return
}
/*
Valid returns a Boolean value indicative of the following:
- Receiver's length is greater than or equal to two (2) slice members, AND ...
- The first slice in the receiver contains an unsigned decimal value that is less than three (3)
*/
func (r DotNotation) Valid() (is bool) {
if !r.IsZero() {
is = r.Root().Lt(3) && r.Len() >= 2
}
return
}
/*
encodeVLQ returns the VLQ -- or Variable Length Quantity -- encoding of
the raw input value.
*/
func encodeVLQ(b []byte) []byte {
var oid []byte
n := big.NewInt(0).SetBytes(b)
for n.Cmp(big.NewInt(0)) > 0 {
temp := new(big.Int)
temp.Mod(n, big.NewInt(128))
if len(oid) > 0 {
temp.Add(temp, big.NewInt(128))
}
oid = append([]byte{byte(temp.Uint64())}, oid...)
n.Div(n, big.NewInt(128))
}
return oid
}
func isNumericOID(id string) bool {
if !isValidOIDPrefix(id) {
return false
}
var last rune
for i, c := range id {
switch {
case c == '.':
if last == c {
return false
} else if i == len(id)-1 {
return false
}
last = '.'
case ('0' <= c && c <= '9'):
last = c
continue
}
}
return true
}
func isValidOIDPrefix(id string) bool {
slices := split(id, `.`)
if len(slices) < 2 {
return false
}
root, err := atoi(slices[0])
if err != nil {
return false
}
if !(0 <= root && root <= 2) {
return false
}
var sub int
if sub, err = atoi(slices[1]); err != nil {
return false
} else if !(0 <= sub && sub <= 39) && root != 2 {
return false
}
return true
}