-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.go
391 lines (344 loc) · 11.4 KB
/
common.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
package dirsyn
import (
"encoding/asn1"
"encoding/base64"
"encoding/hex"
"errors"
"math/big"
"os"
"reflect"
"regexp"
"strconv"
"strings"
"time"
)
var (
fmtInt func(int64, int) string = strconv.FormatInt
fmtUint func(uint64, int) string = strconv.FormatUint
atoi func(string) (int, error) = strconv.Atoi
itoa func(int) string = strconv.Itoa
cntns func(string, string) bool = strings.Contains
mkerr func(string) error = errors.New
fields func(string) []string = strings.Fields
trimS func(string) string = strings.TrimSpace
trimL func(string, string) string = strings.TrimLeft
trimR func(string, string) string = strings.TrimRight
trimPfx func(string, string) string = strings.TrimPrefix
trimSfx func(string, string) string = strings.TrimSuffix
hasPfx func(string, string) bool = strings.HasPrefix
hasSfx func(string, string) bool = strings.HasSuffix
join func([]string, string) string = strings.Join
split func(string, string) []string = strings.Split
splitN func(string, string, int) []string = strings.SplitN
idxr func(string, rune) int = strings.IndexRune
repAll func(string, string, string) string = strings.ReplaceAll
puint func(string, int, int) (uint64, error) = strconv.ParseUint
fuint func(uint64, int) string = strconv.FormatUint
hexdec func(string) ([]byte, error) = hex.DecodeString
hexencs func([]byte) string = hex.EncodeToString
hexlen func(int) int = hex.EncodedLen
hexenc func([]byte, []byte) int = hex.Encode
asn1m func(any) ([]byte, error) = asn1.Marshal
asn1mp func(any, string) ([]byte, error) = asn1.MarshalWithParams
asn1um func([]byte, any) ([]byte, error) = asn1.Unmarshal
asn1ump func([]byte, any, string) ([]byte, error) = asn1.UnmarshalWithParams
streqf func(string, string) bool = strings.EqualFold
stridx func(string, string) int = strings.Index
strlidx func(string, string) int = strings.LastIndex
strcnt func(string, string) int = strings.Count
trim func(string, string) string = strings.Trim
uc func(string) string = strings.ToUpper
lc func(string) string = strings.ToLower
readFile func(string) ([]byte, error) = os.ReadFile
newBigInt func(int64) *big.Int = big.NewInt
valOf func(any) reflect.Value = reflect.ValueOf
typeOf func(any) reflect.Type = reflect.TypeOf
regexMatch func(string, string) (bool, error) = regexp.MatchString
now func() time.Time = time.Now
)
func streq(a, b string) bool {
return a == b
}
/*
for firstComponent matches
*/
func assertFirstStructField(x any) (first any) {
value := valOf(x)
if isStruct(x) {
typ := typeOf(x)
if typ.NumField() > 0 {
first = value.Field(0).Interface()
}
}
return
}
/*
isStruct returns a boolean value indicative of whether
kind reflection revealed the presence of a struct type.
*/
func isStruct(x any) (is bool) {
if x != nil {
is = typeOf(x).Kind() == reflect.Struct
}
return
}
func newStrBuilder() strings.Builder {
return strings.Builder{}
}
func escapeString(x string) (esc string) {
if len(x) > 0 {
bld := newStrBuilder()
for _, z := range x {
if z > maxASCII {
for _, c := range []byte(string(z)) {
bld.WriteString(`\`)
bld.WriteString(fuint(uint64(c), 16))
}
} else {
bld.WriteRune(z)
}
}
esc = bld.String()
}
return
}
func hexEncode(x any) string {
var r string
switch tv := x.(type) {
case string:
r = tv
case []byte:
r = string(tv)
default:
return ``
}
e := newStrBuilder()
for _, c := range r {
for _, b := range []byte(string(c)) {
e.WriteString("\\")
e.WriteString(fuint(uint64(b), 16))
}
}
return e.String()
}
func hexDecode(x any) string {
var r string
switch tv := x.(type) {
case string:
r = tv
case []byte:
r = string(tv)
default:
return ``
}
d := newStrBuilder()
length := len(r)
for i := 0; i < length; i++ {
if r[i] == '\\' && i+3 <= length {
b, err := hexdec(r[i+1 : i+3])
if err != nil || !(isHex(rune(r[i+1])) || isHex(rune(r[i+2]))) {
return ``
}
d.Write(b)
i += 2
} else {
d.WriteString(string(r[i]))
}
}
return d.String()
}
func isBase64(x any) (is bool) {
var raw string
switch tv := x.(type) {
case string:
raw = tv
case []byte:
raw = string(tv)
default:
return
}
_, err := base64.StdEncoding.DecodeString(raw)
is = err == nil
return
}
func b64dec(enc []byte) (dec []byte, err error) {
dec = make([]byte, base64.StdEncoding.DecodedLen(len(enc)))
_, err = base64.StdEncoding.Decode(dec, enc)
return
}
func splitUnescaped(str, sep, esc string) (slice []string) {
slice = split(str, sep)
for i := len(slice) - 2; i >= 0; i-- {
if hasSfx(slice[i], esc) {
slice[i] = slice[i][:len(slice[i])-len(esc)] + sep + slice[i+1]
slice = append(slice[:i+1], slice[i+2:]...)
}
}
return
}
func strInSlice(r string, slice []string, cEM ...bool) (match bool) {
var cem bool
if len(cEM) > 0 {
cem = cEM[0]
}
for i := 0; i < len(slice) && !match; i++ {
if cem {
match = r == slice[i]
} else {
match = streqf(r, slice[i])
}
}
return
}
func isUnsignedNumber(x string) bool {
return isNumber(x) && !hasPfx(x, `-`)
}
func isNumber(x string) bool {
x = trimL(x, `-`)
for _, c := range x {
if !('0' <= rune(c) && rune(c) <= '9') {
return false
}
}
return true
}
func assertString(x any, min int, name string) (str string, err error) {
switch tv := x.(type) {
case []byte:
str, err = assertString(string(tv), min, name)
case string:
if len(tv) < min && min != 0 {
err = errorBadLength(name, 0)
break
}
str = tv
default:
err = errorBadType(name)
}
return
}
func caseIgnoreMatch(a, b any) (result Boolean, err error) {
result, err = caseBasedMatch(a, b, false)
return
}
func caseExactMatch(a, b any) (result Boolean, err error) {
result, err = caseBasedMatch(a, b, true)
return
}
func caseBasedMatch(a, b any, caseExact bool) (result Boolean, err error) {
var str1, str2 string
str1, err = assertString(a, 1, "string")
if err != nil {
return
}
str2, err = assertString(b, 1, "string")
if err != nil {
return
}
if caseExact {
result.Set(streq(str1, str2))
} else {
result.Set(streqf(str1, str2))
}
return
}
func caseIgnoreOrderingMatch(a, b any) (Boolean, error) {
return caseBasedOrderingMatch(a, b, false)
}
func caseExactOrderingMatch(a, b any) (Boolean, error) {
return caseBasedOrderingMatch(a, b, true)
}
func caseBasedOrderingMatch(a, b any, caseExact bool) (result Boolean, err error) {
var str1, str2 string
if str1, str2, err = prepareNumericStringAssertion(a, b); err == nil {
if caseExact {
result.Set(str1 < str2)
} else {
result.Set(lc(str1) < lc(str2))
}
}
return
}
/*
SyntaxVerification implements a closure function signature meant to be
honored by functions or methods intended to verify the syntax of a value.
*/
type SyntaxVerification func(any) Boolean
var syntaxVerifiers map[string]SyntaxVerification = map[string]SyntaxVerification{
`1.3.6.1.4.1.1466.115.121.1.3`: attributeTypeDescription,
`1.3.6.1.4.1.1466.115.121.1.6`: bitString,
`1.3.6.1.4.1.1466.115.121.1.7`: boolean,
`1.3.6.1.4.1.1466.115.121.1.11`: countryString,
`1.3.6.1.4.1.1466.115.121.1.14`: deliveryMethod,
`1.3.6.1.4.1.1466.115.121.1.15`: directoryString,
`1.3.6.1.4.1.1466.115.121.1.16`: dITContentRuleDescription,
`1.3.6.1.4.1.1466.115.121.1.17`: dITStructureRuleDescription,
`1.3.6.1.4.1.1466.115.121.1.12`: dN,
`1.3.6.1.4.1.1466.115.121.1.21`: enhancedGuide,
`1.3.6.1.4.1.1466.115.121.1.22`: facsimileTelephoneNumber,
`1.3.6.1.4.1.1466.115.121.1.23`: fax,
`1.3.6.1.4.1.1466.115.121.1.24`: generalizedTime,
`1.3.6.1.4.1.1466.115.121.1.25`: guide,
`1.3.6.1.4.1.1466.115.121.1.26`: iA5String,
`1.3.6.1.4.1.1466.115.121.1.27`: integer,
`1.3.6.1.4.1.1466.115.121.1.28`: jPEG,
`1.3.6.1.4.1.1466.115.121.1.54`: lDAPSyntaxDescription,
`1.3.6.1.4.1.1466.115.121.1.30`: matchingRuleDescription,
`1.3.6.1.4.1.1466.115.121.1.31`: matchingRuleUseDescription,
`1.3.6.1.4.1.1466.115.121.1.34`: nameAndOptionalUID,
`1.3.6.1.4.1.1466.115.121.1.35`: nameFormDescription,
`1.3.6.1.4.1.1466.115.121.1.36`: numericString,
`1.3.6.1.4.1.1466.115.121.1.37`: objectClassDescription,
`1.3.6.1.4.1.1466.115.121.1.40`: octetString,
`1.3.6.1.4.1.1466.115.121.1.38`: oID,
`1.3.6.1.4.1.1466.115.121.1.39`: otherMailbox,
`1.3.6.1.4.1.1466.115.121.1.41`: postalAddress,
`1.3.6.1.4.1.1466.115.121.1.44`: printableString,
`1.3.6.1.4.1.1466.115.121.1.58`: substringAssertion,
`1.3.6.1.4.1.1466.115.121.1.50`: telephoneNumber,
`1.3.6.1.4.1.1466.115.121.1.51`: teletexTerminalIdentifier,
`1.3.6.1.4.1.1466.115.121.1.52`: telexNumber,
`1.3.6.1.4.1.1466.115.121.1.53`: uTCTime,
}
/*
MatchingRuleAssertion defines a closure signature held by qualifying
function instances intended to implement a particular Matching Rule.
The semantics of the MatchingRuleAssertion are discussed in [§ 4.1 of
RFC 4517].
[§ 4.1 of RFC 4517]: https://datatracker.ietf.org/doc/html/rfc4517#section-4.1
*/
type MatchingRuleAssertion func(any, any) (Boolean, error)
var matchingRuleAssertions map[string]MatchingRuleAssertion = map[string]MatchingRuleAssertion{
"2.5.13.0": objectIdentifierMatch,
"2.5.13.1": distinguishedNameMatch,
"2.5.13.2": caseIgnoreMatch,
"2.5.13.3": caseIgnoreOrderingMatch,
"2.5.13.4": caseIgnoreSubstringsMatch,
"2.5.13.5": caseExactMatch,
"2.5.13.6": caseExactOrderingMatch,
"2.5.13.7": caseExactSubstringsMatch,
"2.5.13.8": numericStringMatch,
"2.5.13.9": numericStringOrderingMatch,
"2.5.13.10": numericStringSubstringsMatch,
"2.5.13.11": caseIgnoreListMatch,
"2.5.13.12": caseIgnoreListSubstringsMatch,
"2.5.13.13": booleanMatch,
"2.5.13.14": integerMatch,
"2.5.13.15": integerOrderingMatch,
"2.5.13.16": bitStringMatch,
"2.5.13.17": octetStringMatch,
"2.5.13.18": octetStringOrderingMatch,
"2.5.13.20": telephoneNumberMatch,
"2.5.13.21": telephoneNumberSubstringsMatch,
"2.5.13.23": uniqueMemberMatch,
"2.5.13.27": generalizedTimeMatch,
"2.5.13.28": generalizedTimeOrderingMatch,
"2.5.13.29": integerFirstComponentMatch,
"2.5.13.30": objectIdentifierFirstComponentMatch,
"2.5.13.31": directoryStringFirstComponentMatch,
"2.5.13.32": wordMatch,
"2.5.13.33": keywordMatch,
"1.3.6.1.4.1.1466.109.114.1": caseExactIA5Match,
"1.3.6.1.4.1.1466.109.114.2": caseIgnoreIA5Match,
"1.3.6.1.4.1.1466.109.114.3": caseIgnoreIA5SubstringsMatch,
}