-
Notifications
You must be signed in to change notification settings - Fork 0
/
asn.go
280 lines (239 loc) · 5.83 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
271
272
273
274
275
276
277
278
279
280
package objectid
/*
asn.go handles ASN1Notation operations. For ASN.1 encoding/decoding, see codec.go.
*/
/*
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 (r ASN1Notation) String() string {
var x []string
for i := 0; i < len(r); i++ {
x = append(x, r[i].String())
}
return `{` + join(x, ` `) + `}`
}
/*
Dot returns a [DotNotation] instance based on the contents of the receiver instance.
Note that at a receiver length of two (2) or more is required for successful output.
*/
func (r ASN1Notation) Dot() (d DotNotation) {
if r.Len() < 2 {
return
}
if !r.IsZero() {
L := r.Len()
d = make(DotNotation, L)
for i := 0; i < L; i++ {
d[i] = r[i].NumberForm()
}
}
return
}
/*
Root returns the root node (0) string value from the receiver.
*/
func (r ASN1Notation) Root() NameAndNumberForm {
x, _ := r.Index(0)
return x
}
/*
Leaf returns the leaf node (-1) string value from the receiver.
*/
func (r ASN1Notation) Leaf() NameAndNumberForm {
x, _ := r.Index(-1)
return x
}
/*
Parent returns the leaf node's parent (-2) string value from the receiver.
*/
func (r ASN1Notation) Parent() NameAndNumberForm {
x, _ := r.Index(-2)
return x
}
/*
Len returns the integer length of the receiver.
*/
func (r ASN1Notation) Len() int { return len(r) }
/*
IsZero returns a Boolean indicative of whether the receiver is unset.
*/
func (r ASN1Notation) IsZero() (is bool) {
if is = &r == nil; !is {
is = r.Len() == 0
}
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 ASN1Notation) Index(idx int) (nanf NameAndNumberForm, ok bool) {
L := r.Len()
// Bail if receiver is empty.
if L > 0 {
if idx < 0 {
var x int = L + idx
if x < 0 {
nanf = r[0]
} else {
nanf = r[x]
}
} else if idx > L {
nanf = r[L-1]
} else {
nanf = r[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) ... }")
- string slices (e.g.: []string{"iso(1)", "identified-organization(3)" ...})
- [NameAndNumberForm] slices ([][NameAndNumberForm]{...})
Note that the following root node abbreviations are supported:
- `itu-t` resolves to itu-t(0)
- `iso` resolves to iso(1)
- `joint-iso-itu-t` resolves to joint-iso-itu-t(2)
Case is significant during processing of the above abbreviations. Note that it is
inappropriate to utilize these abbreviations for any portion of an [ASN1Notation]
instance other than as the respective root node.
[NumberForm] values CANNOT be negative, but are unbounded in their magnitude.
*/
func NewASN1Notation(x any) (r *ASN1Notation, err error) {
// prepare temporary instance
t := make(ASN1Notation, 0)
r = new(ASN1Notation)
var nfs []string
switch tv := x.(type) {
case []NameAndNumberForm:
t = ASN1Notation(tv)
if !t.Valid() {
err = errorf("%T instance did not pass validity checks: %#v", t, t)
break
}
*r = t
return
case string:
nfs = fields(condenseWHSP(trimR(trimL(tv, `{`), `}`)))
case []string:
nfs = tv
default:
err = errorf("Unsupported %T input type: %#v", x, x)
return
}
for i := 0; i < len(nfs); i++ {
var nanf *NameAndNumberForm
if nanf, err = NewNameAndNumberForm(nfs[i]); err != nil {
break
}
t = append(t, *nanf)
}
if err == nil {
// 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.
*r = 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 (r ASN1Notation) Valid() (is bool) {
// Don't waste time on
// zero instances.
if L := r.Len(); L > 0 {
if root, ok := r.Index(0); ok {
// root cannot be greater than 2
is = root.NumberForm().Lt(3)
}
}
return
}
/*
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 (r ASN1Notation) Ancestry() (anc []ASN1Notation) {
if r.Len() >= 2 {
for i := r.Len(); i > 0; i-- {
anc = append(anc, r[: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 (r ASN1Notation) NewSubordinate(nanf any) *ASN1Notation {
var A ASN1Notation
if r.Len() > 0 {
// Prepare the new leaf numberForm, or die trying.
if n, err := NewNameAndNumberForm(nanf); err == nil {
A = make(ASN1Notation, r.Len()+1, r.Len()+1)
for i := 0; i < r.Len(); i++ {
A[i] = r[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 (r ASN1Notation) AncestorOf(asn any) (anc bool) {
if !r.IsZero() {
var A *ASN1Notation
switch tv := asn.(type) {
case string:
A, _ = NewASN1Notation(tv)
case *ASN1Notation:
if tv != nil {
A = tv
}
case ASN1Notation:
if tv.Len() >= 0 {
A = &tv
}
}
if A.Len() > r.Len() {
anc = r.matchASN1(A)
}
}
return
}
func (r ASN1Notation) matchASN1(asn *ASN1Notation) (matched bool) {
L := r.Len()
ct := 0
for i := 0; i < L; i++ {
x, _ := r.Index(i)
if y, ok := asn.Index(i); ok {
if x.Equal(y) {
ct++
}
}
}
return ct == L
}