-
Notifications
You must be signed in to change notification settings - Fork 4
/
cerificate.go
142 lines (124 loc) · 2.38 KB
/
cerificate.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
package agent
import (
"encoding/hex"
"github.com/fxamacker/cbor/v2"
)
const (
Empty uint64 = iota
Fork
Labeled
Leaf
Pruned
)
func LookUp(paths [][]byte, cert []byte)([]byte, error) {
var certificate interface{}
err := cbor.Unmarshal(cert, &certificate)
if err != nil{
return nil,err
}
certi_struct, ok := certificate.(map[interface{}]interface{})
if !ok{
return nil, nil
}
for k, v := range(certi_struct){
k_value, ok := k.(string)
if ok && k_value == "tree"{
tree_value, ok := v.([]interface{})
if !ok {
continue
}
return lookupPath(paths, tree_value)
}
}
return nil, nil
}
func lookupPath(paths [][]byte, tree []interface{})([]byte, error) {
if tree == nil{
return nil, nil
}
offset := 0
if len(paths) == 0{
if tree == nil{
return nil, nil
}
tree_0, ok := tree[0].(uint64)
if !ok{
return nil, nil
}
if tree_0 == Leaf{
tree_1, ok := tree[1].([]byte)
if !ok{
return nil, nil
}
return tree_1, nil
} else {
return nil, nil
}
}
label := paths[0]
t_flatten, _ := flattenForks(tree)
t, _ := findLabel(label, t_flatten)
offset += 1
return lookupPath(paths[offset:], t)
}
func flattenForks(tree []interface{}) ([][]interface{}, error){
tree_0, ok := tree[0].(uint64)
if !ok{
return nil, nil
}
if tree_0 == Empty{
return [][]interface{}{}, nil
} else if tree_0 == Fork{
t_1, ok := tree[1].([]interface{})
if !ok{
return nil, nil
}
t_2, ok := tree[2].([]interface{})
if !ok{
return nil, nil
}
left, _ := flattenForks(t_1)
right, _ := flattenForks(t_2)
return append(left, right...), nil
} else {
return [][]interface{}{tree}, nil
}
}
func findLabel(label []byte, trees [][]interface{}) ([]interface{}, error) {
if len(trees) == 0{
return nil, nil
}
for _, t := range(trees) {
t_0, ok := t[0].(uint64)
if !ok{
return nil, nil
}
if t_0 == Labeled{
t_1, ok := t[1].([]byte)
if !ok {
return nil, nil
}
if (hex.EncodeToString(t_1) != hex.EncodeToString(label)) {
//fmt.Println(label, " error")
continue
}
t_2, ok := t[2].([]interface{})
if !ok{
return nil, nil
}
return t_2, nil
}
}
return nil, nil
}
type Certificate struct {
Tree Tree `cbor:"tree"`
Signature []byte `cbor:"signature"`
Delegation []byte `cbor:"delegation"`
}
type Tree struct {
_ struct{} `cbor:",toarray"`
sym byte
a []byte
b []byte
}