/
asn.go
236 lines (203 loc) · 4.77 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
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() (is bool) {
if is = &a == nil; !is {
is = a.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 (a ASN1Notation) Index(idx int) (nanf NameAndNumberForm, ok bool) {
L := a.Len()
// Bail if receiver is empty.
if L > 0 {
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)
var nfs []string
switch tv := x.(type) {
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) && err == nil; i++ {
var nanf *NameAndNumberForm
if nanf, err = NewNameAndNumberForm(nfs[i]); nanf != nil {
*t = append(*t, *nanf)
}
}
if err != nil {
return
}
// verify content is valid
err = errorf("%T instance did not pass validity checks: %#v", t, *t)
if t.Valid() {
// transfer temporary content
// to return value instance.
a = new(ASN1Notation)
*a = *t
err = nil
}
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() (is bool) {
// Don't waste time on
// zero instances.
if L := a.Len(); L > 0 {
if root, ok := a.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 (a ASN1Notation) Ancestry() (anc []ASN1Notation) {
if a.Len() >= 2 {
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 {
var A ASN1Notation
if a.Len() > 0 {
// Prepare the new leaf numberForm,
// or die trying.
if n, err := NewNameAndNumberForm(nanf); err == 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) (anc bool) {
if !a.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() > a.Len() {
anc = a.matchASN1(A)
}
}
return
}
func (a ASN1Notation) matchASN1(asn *ASN1Notation) (matched bool) {
L := a.Len()
ct := 0
for i := 0; i < L; i++ {
x, _ := a.Index(i)
if y, ok := asn.Index(i); ok {
if x.Equal(y) {
ct++
}
}
}
return ct == L
}