-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathber.go
133 lines (115 loc) · 3.04 KB
/
ber.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
package main
import (
"bytes"
"fmt"
ber "github.com/go-asn1-ber/asn1-ber"
)
func CopyBerPacket(packet *ber.Packet) *ber.Packet {
newPacket := ber.Encode(packet.ClassType, packet.TagType, packet.Tag, packet.Value, packet.Description)
for _, child := range packet.Children {
if len(child.Children) == 0 {
newPacket.AppendChild(child)
} else {
newPacket.AppendChild(CopyBerPacket(child))
}
}
return newPacket
}
// TODO: Generalize this method to "non-leaves"
func UpdateBerChildLeaf(packet *ber.Packet, idx int, newChild *ber.Packet) error {
if idx >= len(packet.Children) {
return fmt.Errorf("Error updating BER packet: index out of bounds")
}
(*packet).Children[idx] = newChild
updatedData := new(bytes.Buffer)
for x := 0; x < idx; x++ {
updatedData.Write(packet.Children[x].Bytes())
}
updatedData.Write(newChild.Bytes())
for x := idx + 1; x < len(packet.Children); x++ {
updatedData.Write(packet.Children[x].Bytes())
}
(*packet).Data = updatedData
return nil
}
func BerChildrenToList(packet *ber.Packet) []string {
attrs := make([]string, 0)
for _, child := range packet.Children {
attrs = append(attrs, child.Data.String())
}
return attrs
}
func EncodeAttributeList(attrs []string) *ber.Packet {
seq := ber.NewSequence("Attribute List")
for _, attr := range attrs {
seq.AppendChild(ber.NewString(ber.ClassUniversal, ber.TypePrimitive, ber.TagOctetString, attr, "Attribute"))
}
return seq
}
func EncodeBaseDN(baseDN string) *ber.Packet {
return ber.NewString(ber.ClassUniversal, ber.TypePrimitive, ber.TagOctetString, baseDN, "Base DN")
}
/*
func reencodeBerDataRecursive(packet *ber.Packet) {
// Recursively process the children first
for _, child := range packet.Children {
reencodeBerData(child)
}
// Compute the new Data field based on the packet's type and value
if packet.Value != nil {
v := reflect.ValueOf(packet.Value)
if packet.ClassType == ber.ClassUniversal {
switch packet.Tag {
case ber.TagOctetString:
sv, ok := v.Interface().(string)
if ok {
packet.Data.Reset()
packet.Data.Write([]byte(sv))
}
case ber.TagEnumerated:
bv, ok := v.Interface().([]byte)
if ok {
packet.Data.Reset()
packet.Data.Write(bv)
}
case ber.TagEmbeddedPDV:
bv, ok := v.Interface().([]byte)
if ok {
packet.Data.Reset()
packet.Data.Write(bv)
}
}
} else if packet.ClassType == ber.ClassContext {
switch packet.Tag {
case ber.TagEnumerated:
bv, ok := v.Interface().([]byte)
if ok {
packet.Data.Reset()
packet.Data.Write(bv)
}
case ber.TagEmbeddedPDV:
bv, ok := v.Interface().([]byte)
if ok {
packet.Data.Reset()
packet.Data.Write(bv)
}
}
}
}
// Handle constructed types
if packet.ClassType == ber.ClassUniversal {
switch packet.Tag {
case ber.TagSequence:
packet.Data.Reset()
for _, child := range packet.Children {
packet.Data.Write(child.Bytes())
}
case ber.TagSet:
packet.Data.Reset()
for _, child := range packet.Children {
packet.Data.Write(child.Bytes())
}
}
}
}
*/