-
Notifications
You must be signed in to change notification settings - Fork 0
/
oid.go
153 lines (132 loc) · 3.03 KB
/
oid.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
package objectid
/*
OID contains an underlying ASN1Notation value, and extends convenient methods allowing
interrogation and verification.
*/
type OID struct {
nanf ASN1Notation
parsed bool
}
/*
IsZero checks the receiver for nilness and returns a boolean indicative of the result.
*/
func (id *OID) IsZero() (is bool) {
if id != nil {
is = len(id.nanf) == 0
}
return
}
/*
Dot returns a DotNotation instance based on the contents of the underlying ASN1Notation
instance found within the receiver.
*/
func (id OID) Dot() (d DotNotation) {
if !id.IsZero() {
d = make(DotNotation, len(id.nanf))
for i := 0; i < len(id.nanf); i++ {
d[i] = id.nanf[i].NumberForm()
}
}
return
}
/*
ASN returns the underlying ASN1Notation instance found within the receiver.
*/
func (id OID) ASN() (a ASN1Notation) {
if !id.IsZero() {
a = id.nanf
}
return
}
/*
Valid returns a boolean value indicative of whether the receiver's state is considered value.
*/
func (id OID) Valid() (ok bool) {
if !id.IsZero() {
var nanf NameAndNumberForm
if nanf, ok = id.nanf.Index(0); ok {
var found bool
for i := 0; i < 3; i++ {
if nanf.NumberForm().Equal(i) {
found = true
break
}
}
ok = found
}
}
return
}
/*
Len returns the integer length of all underlying NumberForm values present within the receiver.
*/
func (id OID) Len() (i int) {
if !id.IsZero() {
i = len(id.nanf)
}
return
}
/*
Leaf returns the leaf-node instance of NameAndNumberForm.
*/
func (id OID) Leaf() (nanf NameAndNumberForm) {
if !id.IsZero() {
nanf, _ = id.nanf.Index(-1)
}
return
}
/*
Parent returns the leaf-node's Parent instance of NameAndNumberForm.
*/
func (id OID) Parent() (nanf NameAndNumberForm) {
if !id.IsZero() {
nanf, _ = id.nanf.Index(-2)
}
return
}
/*
Root returns the root node instance of NameAndNumberForm.
*/
func (id OID) Root() (nanf NameAndNumberForm) {
if !id.IsZero() {
nanf, _ = id.nanf.Index(0)
}
return
}
/*
NewOID creates an instance of OID and returns it alongside an error.
The correct raw input syntax is the ASN.1 NameAndNumberForm sequence syntax, i.e.:
{iso(1) identified-organization(3) dod(6)}
Not all NameAndNumberForm values (arcs) require actual names; they can be numbers alone or in the so-called nameAndNumber syntax (name(Number)). For example:
{iso(1) identified-organization(3) 6}
... is perfectly valid, but generally NOT recommended when clarity or precision is desired.
*/
func NewOID(x any) (o *OID, err error) {
t := new(OID)
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\n", x, x)
return
}
for i := 0; i < len(nfs) && err == nil; i++ {
var nanf *NameAndNumberForm
if nanf, err = NewNameAndNumberForm(nfs[i]); nanf != nil {
t.nanf = append(t.nanf, *nanf)
}
}
if err == nil {
err = errorf("%T instance did not pass validity checks: %#v", t, *t)
if t.Valid() {
o = new(OID)
o.parsed = true
*o = *t
err = nil
}
}
return
}