-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathberobjectidentifier.go
154 lines (131 loc) · 3.54 KB
/
berobjectidentifier.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
package asn1ber
import (
"bytes"
"errors"
"fmt"
"io"
"math"
)
type BerObjectIdentifier struct {
value []int
}
var objectIdentifierTag = NewBerTag(UNIVERSAL_CLASS, PRIMITIVE, OBJECT_IDENTIFIER_TAG)
func NewBerObjectIdentifier(value []int) *BerObjectIdentifier {
return &BerObjectIdentifier{value: value} // Do not check for now...
}
func (b *BerObjectIdentifier) Encode(reversedWriter io.Writer, withTagList ...bool) (int, error) {
var withTag bool
if len(withTagList) > 0 {
withTag = withTagList[0]
} else {
withTag = true
}
firstSubidentifier := 40*b.value[0] + b.value[1]
var subidentifier int
codeLength := 0
for i := len(b.value) - 1; i > 0; i-- {
if i == 1 {
subidentifier = firstSubidentifier
} else {
subidentifier = b.value[i]
}
// get length of subidentifier
subIDLength := 1
for subidentifier > int(math.Pow(2, float64(7*subIDLength)))-1 {
subIDLength++
}
_, _ = WriteByte(reversedWriter, subidentifier&0x7f)
for j := 1; j <= (subIDLength - 1); j++ {
_, _ = WriteByte(reversedWriter, ((subidentifier>>(7*j))&0xff)|0x80)
}
codeLength += subIDLength
}
n, _ := EncodeLength(codeLength, reversedWriter)
codeLength += n
if withTag {
n, _ = objectIdentifierTag.Encode(reversedWriter)
codeLength += n
}
return codeLength, nil
}
func (b *BerObjectIdentifier) Decode(input io.Reader, withTagList ...bool) (int, error) {
var withTag bool
if len(withTagList) > 0 {
withTag = withTagList[0]
} else {
withTag = true
}
codeLength := 0
if withTag {
n, err := objectIdentifierTag.DecodeAndCheck(input)
codeLength += n
if err != nil {
return codeLength, err
}
}
berLength := &BerLength{}
n, err := berLength.Decode(input)
codeLength += n
if err != nil {
return codeLength, err
}
if berLength.Length == 0 {
b.value = []int{0}
return codeLength, err
}
byteCode := make([]byte, berLength.Length)
n, err = io.ReadFull(input, byteCode)
if err != nil {
return 0, err
}
codeLength += n
objectIdentifierComponentsList := make([]int, 0)
subIDEndIndex := 0
for (byteCode[subIDEndIndex] & 0x80) == 0x80 {
if subIDEndIndex >= (berLength.Length - 1) {
return codeLength, errors.New("invalid Object Identifier")
}
subIDEndIndex++
}
subIdentifier := 0
for i := 0; i <= subIDEndIndex; i++ {
subIdentifier |= int(byteCode[i]&0x7f) << ((subIDEndIndex - i) * 7)
}
if subIdentifier < 40 {
objectIdentifierComponentsList = append(objectIdentifierComponentsList, 0, subIdentifier)
} else if subIdentifier < 80 {
objectIdentifierComponentsList = append(objectIdentifierComponentsList, 1, subIdentifier-40)
} else {
objectIdentifierComponentsList = append(objectIdentifierComponentsList, 2, subIdentifier-80)
}
subIDEndIndex++
for subIDEndIndex < berLength.Length {
subIDStartIndex := subIDEndIndex
for (byteCode[subIDEndIndex] & 0x80) == 0x80 {
if subIDEndIndex == (berLength.Length - 1) {
return codeLength, errors.New("invalid Object Identifier")
}
subIDEndIndex++
}
subIdentifier = 0
for j := subIDStartIndex; j <= subIDEndIndex; j++ {
subIdentifier |= int(byteCode[j]&0x7f) << ((subIDEndIndex - j) * 7)
}
objectIdentifierComponentsList = append(objectIdentifierComponentsList, subIdentifier)
subIDEndIndex++
}
b.value = objectIdentifierComponentsList
return codeLength, err
}
func (b *BerObjectIdentifier) S() string {
var buffer bytes.Buffer
sep := ""
for _, i := range b.value {
buffer.WriteString(fmt.Sprintf("%s%d", sep, i))
sep = "."
}
return buffer.String()
}
func (b *BerObjectIdentifier) GetTag() *BerTag {
return objectIdentifierTag
}