/
reg_cred.go
176 lines (166 loc) · 3.98 KB
/
reg_cred.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
package reg
// xlReg_go/reg_cred.go
import (
"crypto/rsa"
"encoding/hex"
"fmt"
xc "github.com/jddixon/xlCrypto_go"
xi "github.com/jddixon/xlNodeID_go"
xt "github.com/jddixon/xlTransport_go"
xu "github.com/jddixon/xlUtil_go"
"strings"
)
var _ = fmt.Print
type RegCred struct {
Name string
ID *xi.NodeID
CommsPubKey *rsa.PublicKey // CommsKey in token
SigPubKey *rsa.PublicKey // SigKey in token
EndPoints []xt.EndPointI // MyEnds in token
Version xu.DecimalVersion // not in token at all
}
func (rc *RegCred) String() string {
// Ignore possible errors; the values in this data structure
// should have already been checked.
ck_, _ := xc.RSAPubKeyToDisk(rc.CommsPubKey)
sk_, _ := xc.RSAPubKeyToDisk(rc.SigPubKey)
ck := string(ck_)
sk := string(sk_)
// XXX Space after the colon is strictly required
ss := []string{"regCred {"}
ss = append(ss, " Name: "+rc.Name)
ss = append(ss, " ID: "+rc.ID.String())
ss = append(ss, " CommsPubKey: "+ck)
ss = append(ss, " SigPubKey: "+sk)
ss = append(ss, " EndPoints {")
for i := 0; i < len(rc.EndPoints); i++ {
ss = append(ss, " "+rc.EndPoints[i].String())
}
ss = append(ss, " }")
ss = append(ss, " Version: "+rc.Version.String())
ss = append(ss, "}")
return strings.Join(ss, "\r\n") + "\r\n"
}
func ParseRegCred(s string) (rc *RegCred, err error) {
var (
line string
parts []string
name string
nodeID *xi.NodeID
ck, sk *rsa.PublicKey
e []xt.EndPointI
version xu.DecimalVersion
)
ss := strings.Split(s, "\n")
line, err = xc.NextNBLine(&ss)
if (err == nil) && (line != "regCred {") {
err = IllFormedRegCred
}
if err == nil {
line, err = xc.NextNBLine(&ss)
if err == nil {
parts = strings.Split(line, ": ")
if len(parts) == 2 && parts[0] == "Name" {
name = strings.TrimLeft(parts[1], " \t")
} else {
err = IllFormedRegCred
}
}
}
if err == nil {
var id []byte
line, err = xc.NextNBLine(&ss)
if err == nil {
parts = strings.Split(line, ": ")
if len(parts) == 2 && parts[0] == "ID" {
id, err = hex.DecodeString(parts[1])
} else {
err = IllFormedRegCred
}
}
if err == nil {
nodeID, err = xi.New(id)
}
}
if err == nil {
line, err = xc.NextNBLine(&ss)
if err == nil {
parts = strings.Split(line, ": ")
if len(parts) == 2 && parts[0] == "CommsPubKey" {
ck, err = xc.RSAPubKeyFromDisk([]byte(parts[1]))
} else {
err = IllFormedRegCred
}
}
}
if err == nil {
line, err = xc.NextNBLine(&ss)
if err == nil {
parts = strings.Split(line, ": ")
if len(parts) == 2 && parts[0] == "SigPubKey" {
sk, err = xc.RSAPubKeyFromDisk([]byte(parts[1]))
} else {
err = IllFormedRegCred
}
}
}
if err == nil {
line, err = xc.NextNBLine(&ss)
if err == nil {
// collect EndPoints section; this should be turned into a
// utility function
if line == "EndPoints {" {
for err == nil {
line = strings.TrimSpace(ss[0]) // peek
if line == "}" {
break
}
line, err = xc.NextNBLine(&ss)
if err == nil {
line = strings.TrimSpace(line)
parts := strings.Split(line, ": ")
if len(parts) != 2 || parts[0] != "TcpEndPoint" {
err = IllFormedRegCred
} else {
var ep xt.EndPointI
ep, err = xt.NewTcpEndPoint(parts[1])
if err == nil {
e = append(e, ep)
}
}
}
}
if err == nil {
line, err = xc.NextNBLine(&ss)
if (err == nil) && (line != "}") {
err = MissingClosingBrace
}
}
} else {
err = MissingEndPointsSection
}
}
}
if err == nil {
line, err = xc.NextNBLine(&ss)
if err == nil {
parts = strings.Split(line, ": ")
if len(parts) == 2 && parts[0] == "Version" {
version, err = xu.ParseDecimalVersion(parts[1])
} else {
err = IllFormedRegCred
}
}
}
if err == nil {
rc = &RegCred{
Name: name,
ID: nodeID,
CommsPubKey: ck,
SigPubKey: sk,
EndPoints: e,
Version: version,
}
}
return
}