-
Notifications
You must be signed in to change notification settings - Fork 0
/
asn.go
270 lines (232 loc) · 5.13 KB
/
asn.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
package objectid
/*
ASN1Notation contains an ordered sequence of NameAndNumberForm
instances.
*/
type ASN1Notation []NameAndNumberForm
/*
String is a stringer method that returns a properly formatted
ASN.1 string value.
*/
func (a ASN1Notation) String() string {
var x []string
for i := 0; i < len(a); i++ {
x = append(x, a[i].String())
}
return `{` + join(x, ` `) + `}`
}
/*
Root returns the root node (0) string value from the receiver.
*/
func (a ASN1Notation) Root() NameAndNumberForm {
x, _ := a.Index(0)
return x
}
/*
Leaf returns the leaf node (-1) string value from the receiver.
*/
func (a ASN1Notation) Leaf() NameAndNumberForm {
x, _ := a.Index(-1)
return x
}
/*
Parent returns the leaf node's parent (-2) string value from
the receiver.
*/
func (a ASN1Notation) Parent() NameAndNumberForm {
x, _ := a.Index(-2)
return x
}
/*
Len returns the integer length of the receiver.
*/
func (a ASN1Notation) Len() int { return len(a) }
/*
IsZero returns a boolean indicative of whether the receiver
is unset.
*/
func (a ASN1Notation) IsZero() bool {
if &a == nil {
return true
}
return a.Len() == 0
}
/*
Index returns the Nth index from the receiver, alongside a boolean
value indicative of success. This method supports the use of negative
indices.
*/
func (a ASN1Notation) Index(idx int) (nanf NameAndNumberForm, ok bool) {
L := a.Len()
// Bail if receiver is empty.
if L == 0 {
return
}
if idx < 0 {
var x int = L + idx
if x < 0 {
nanf = a[0]
} else {
nanf = a[x]
}
} else if idx > L {
nanf = a[L-1]
} else {
nanf = a[idx]
}
// Make sure the instance was produced
// via recommended procedure.
ok = nanf.parsed
return
}
/*
NewASN1Notation returns an instance of *ASN1Notation alongside an error.
Valid input forms for ASN.1 values are string (e.g.: "{iso(1)}") and string
slices (e.g.: []string{"iso(1)", "identified-organization(3)" ...}).
NumberForm values CANNOT be negative, and CANNOT overflow NumberForm (uint128).
*/
func NewASN1Notation(x any) (a *ASN1Notation, err error) {
// prepare temporary instance
t := new(ASN1Notation)
switch tv := x.(type) {
case string:
f := fields(condenseWHSP(trimR(trimL(tv, `{`), `}`)))
for i := 0; i < len(f); i++ {
var nanf *NameAndNumberForm
if nanf, err = NewNameAndNumberForm(f[i]); err != nil {
return
}
*t = append(*t, *nanf)
}
case []string:
for i := 0; i < len(tv); i++ {
var nanf *NameAndNumberForm
if nanf, err = NewNameAndNumberForm(condenseWHSP(tv[i])); err != nil {
return
}
*t = append(*t, *nanf)
}
default:
err = errorf("Unsupported %T input type: %#v\n", x, x)
return
}
// verify content is valid
if !t.Valid() {
err = errorf("%T instance did not pass validity checks: %#v", t, *t)
return
}
// transfer temporary content
// to return value instance.
a = new(ASN1Notation)
*a = *t
return
}
/*
Valid returns a boolean value indicative of whether the receiver's
length is greater than or equal to one (1) slice member.
*/
func (a ASN1Notation) Valid() bool {
// Don't waste time on
// zero instances.
if a.Len() == 0 {
return false
}
// bail out if any of the slice
// values are unparsed.
for i := 0; i < a.Len(); i++ {
if !a[i].parsed {
return false
}
}
root, ok := a.Index(0)
if !ok {
return false
}
// root cannot be greater than 2
return root.NumberForm().Lt(3)
}
/*
Ancestry returns slices of DotNotation values ordered from leaf node
(first) to root node (last).
Empty slices of DotNotation are returned if the dotNotation value
within the receiver is less than two (2) NumberForm values in length.
*/
func (a ASN1Notation) Ancestry() (anc []ASN1Notation) {
if a.Len() < 2 {
return
}
for i := a.Len(); i > 0; i-- {
anc = append(anc, a[:i])
}
return
}
/*
NewSubordinate returns a new instance of ASN1Notation based upon the
contents of the receiver as well as the input NameAndNumberForm
subordinate value. This creates a fully-qualified child ASN1Notation
value of the receiver.
*/
func (a ASN1Notation) NewSubordinate(nanf any) *ASN1Notation {
// Don't bother processing
if a.Len() == 0 {
return nil
}
// Prepare the new leaf numberForm,
// or die trying.
n, err := NewNameAndNumberForm(nanf)
if err != nil {
return nil
}
A := make(ASN1Notation, a.Len()+1, a.Len()+1)
for i := 0; i < a.Len(); i++ {
A[i] = a[i]
}
A[A.Len()-1] = *n
return &A
}
/*
AncestorOf returns a boolean value indicative of whether the receiver
is an ancestor of the input value, which can be string or ASN1Notation.
*/
func (a ASN1Notation) AncestorOf(asn any) bool {
if a.IsZero() {
return false
}
var A *ASN1Notation
switch tv := asn.(type) {
case string:
var err error
if A, err = NewASN1Notation(tv); err != nil {
return false
}
case *ASN1Notation:
if tv == nil {
return false
}
A = tv
case ASN1Notation:
if tv.Len() == 0 {
return false
}
A = &tv
default:
return false
}
if A.Len() < a.Len() {
return false
}
return a.matchASN1(A)
}
func (a ASN1Notation) matchASN1(asn *ASN1Notation) bool {
for i := 0; i < a.Len(); i++ {
x, _ := a.Index(i)
y, ok := asn.Index(i)
if !ok {
return false
}
if !x.Equal(y) {
return false
}
}
return true
}