forked from Craig-Macomber/election
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
306 lines (261 loc) · 7.64 KB
/
client.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
package main
import (
"bytes"
"code.google.com/p/goprotobuf/proto"
"crypto/rsa"
"crypto/rand"
"fmt"
"github.com/Craig-Macomber/election/config"
"github.com/Craig-Macomber/election/keys"
"github.com/Craig-Macomber/election/msg"
"github.com/Craig-Macomber/election/msg/msgs"
"github.com/Craig-Macomber/election/sign"
"net"
"encoding/base64"
)
var ballotKey *rsa.PublicKey
var voteKey *rsa.PublicKey
const maxLength = 4096
func LoadVoterData(path string) *msgs.VoterData {
data := keys.LoadBytes(path)
var voterData msgs.VoterData
err := proto.Unmarshal(data, &voterData)
if err != nil {
panic(err)
}
return &voterData
}
func load(name string) (*msgs.ElectionConfig, *msgs.VoterData) {
privateInfo := LoadVoterData("demoElection/voterPrivate/" + name)
configBytes := config.LoadBytes()
privateInfo.ElectionConfig = configBytes
config := config.Unpack(privateInfo.ElectionConfig)
return config,privateInfo
}
func ConfigHash(privateInfo *msgs.VoterData) []byte {
return sign.Hash(privateInfo.ElectionConfig)
}
type SignatureStatus uint8
const (
Missing SignatureStatus = iota
Invalid
Valid
)
func CheckKeySig(privateInfo *msgs.VoterData) SignatureStatus{
if privateInfo.KeySignature==nil {
return Missing
}
config := config.Unpack(privateInfo.ElectionConfig)
voterListKey := keys.UnpackKey(config.VoterListServer.Key)
publicKey:=PublicKey(privateInfo)
if sign.CheckSig(voterListKey,publicKey,privateInfo.KeySignature) {
return Valid
}
return Invalid
}
func fillInfo(privateInfo *msgs.VoterData) {
//config := config.Unpack(privateInfo.ElectionConfig)
for CheckKeySig(privateInfo)!=Valid {
// TODO: prompt user here? (Maybe have an interactive mode, only try N times if not interactive?
fmt.Println("Attempting to get valid keySignature from voter list server...")
var err error
privateInfo.KeySignature, err = GetKeySig(PublicKey(privateInfo))
if err != nil {
fmt.Println(err)
} else {
fmt.Println("Got keySignature from voter list server. Checking...")
}
}
fmt.Println("Got valid keySignature from voter list server.")
}
func PublicKey(privateInfo *msgs.VoterData) []byte {
return []byte(keys.StringKey(privateInfo.Key.PublicKey))
}
// Prefix ballot with random 64 bits to make ballot unique.
// Ballots get de-duplicated by the voteServer, so its important
// that clients make their ballot unique.
func PrefixBallot(payload []byte) []byte{
const randomLength=8
b:=make([]byte,len(payload)+randomLength)
_,err:=rand.Read(b[:randomLength])
if err!=nil {
panic(err)
}
copy(b[randomLength:],payload)
return b
}
func main() {
name := "TestVoter1"
config,privateInfo:=load(name)
configHash := ConfigHash(privateInfo)
fmt.Printf("Loaded election config with hash: %s\n",base64.StdEncoding.EncodeToString(configHash))
ballotKey = keys.UnpackKey(config.BallotServer.Key)
voteKey = keys.UnpackKey(config.VoteServer.Key)
voterKey := keys.UnpackPrivateKey(privateInfo.Key)
fillInfo(privateInfo)
//var err error
//privateInfo.KeySignature, err = GetKeySig(PublicKey(privateInfo))
//if err != nil {
// panic(err)
//}
// Construct ballot
// TODO: prompt user or read from file
ballot := PrefixBallot([]byte("ballot!!"))
ballotSig, err := GetBallotSig(voterKey, privateInfo.KeySignature, ballot)
if err != nil {
panic(err)
}
vote, err := SubmitBallot(ballot, ballotSig)
if err != nil {
panic(err)
}
fmt.Printf("Cast ballot '%s' as %s\n", ballot, vote)
}
// Get signature from Voter List Server to prove our public key is valid
func GetKeySig(key []byte) ([]byte, error) {
// connect
conn, err := net.Dial("tcp", "localhost"+msg.Service)
if err != nil {
return nil, err
}
// send request
err = msg.WriteBlock(conn, msg.KeySignatureRequest, key)
if err != nil {
return nil, err
}
// Read response
t, err := msg.ReadType(conn)
if err != nil {
return nil, err
}
if t != msg.KeySignatureResponse {
return nil, fmt.Errorf("KeySignatureResponse: invalid response type %d. Expected: %d", t, msg.KeySignatureResponse)
}
sig, err := msg.ReadBlock(conn, maxLength)
conn.Close()
// TODO check sig is valid
return sig, err
}
// Get signature for the ballot from ballot server
func GetBallotSig(voterKey *rsa.PrivateKey, keySig, ballot []byte) ([]byte, error) {
conn, err := net.Dial("tcp", "localhost"+msg.Service)
if err != nil {
panic(err)
}
var r msgs.SignatureRequest
// TODO real values
r.VoterPublicKey = keys.PackKey(&voterKey.PublicKey)
blindedBallot, unblinder := sign.Blind(ballotKey, ballot)
r.BlindedBallot = blindedBallot
r.KeySignature = keySig
voterSignature, err := sign.Sign(voterKey, blindedBallot)
if err != nil {
fmt.Println(err)
panic(err)
}
r.VoterSignature = voterSignature
data, err := proto.Marshal(&r)
if err != nil {
panic(err)
}
err = msg.WriteBlock(conn, msg.SignatureRequest, data)
if err != nil {
panic(err)
}
t, err := msg.ReadType(conn)
if err != nil {
panic(err)
}
if t != msg.SignatureResponse {
return nil, fmt.Errorf("SignatureResponse: invalid response type %d. Expected: %d", t, msg.SignatureResponse)
}
data, err = msg.ReadBlock(conn, maxLength)
conn.Close()
if err != nil {
panic(err)
}
var response msgs.SignatureResponse
err = proto.Unmarshal(data, &response)
if err != nil {
fmt.Println("error reading SignatureResponse:", err)
panic(err)
}
if !KeysEqual(r.VoterPublicKey, response.Request.VoterPublicKey) {
fmt.Println("illegal response from server. Voter keys must match:")
fmt.Println(r.VoterPublicKey)
fmt.Println(response.Request.VoterPublicKey)
panic(err)
}
err = msg.ValidateSignatureRequest(response.Request)
if err != nil {
panic(err)
}
newBlindedBallot := response.Request.BlindedBallot
if !bytes.Equal(r.BlindedBallot, newBlindedBallot) {
fmt.Println("Server has proof you voted before!")
// TODO should store ballots (and blinding factors?) from all attempts
// so can try and cast old ballot
panic(err)
}
if !sign.CheckBlindSig(ballotKey, newBlindedBallot, response.BlindedBallotSignature) {
fmt.Println("illegal response from server. Signature in response is invalid:", ballotKey, newBlindedBallot, response.BlindedBallotSignature)
panic(err)
}
sig := sign.Unblind(ballotKey, response.BlindedBallotSignature, unblinder)
return sig, nil
}
func KeysEqual(a, b *msgs.PublicKey) bool {
if *a.E != *b.E {
return false
}
return bytes.Equal(a.N, b.N)
}
func SubmitBallot(ballot, sig []byte) (*msgs.VoteResponse, error) {
fmt.Printf("Casting Ballot: %s\n", ballot)
conn, err := net.Dial("tcp", "localhost"+msg.Service)
if err != nil {
return nil, err
}
var vote msgs.Vote
vote.Ballot = ballot
vote.BallotSignature = sig
// redundant sanity check signature
err = msg.ValidateVote(ballotKey, &vote)
if err != nil {
return nil, err
}
data, err := proto.Marshal(&vote)
if err != nil {
return nil, err
}
err = msg.WriteBlock(conn, msg.Vote, data)
if err != nil {
return nil, err
}
t, err := msg.ReadType(conn)
if err != nil {
return nil, err
}
if t != msg.VoteResponse {
return nil, fmt.Errorf("invalid response type")
}
data, err = msg.ReadBlock(conn, maxLength)
conn.Close()
if err != nil {
return nil, err
}
var response msgs.VoteResponse
err = proto.Unmarshal(data, &response)
if err != nil {
fmt.Println("error reading VoteResponse:", err)
return nil, err
}
b := response.BallotEntry
s := response.BallotEntrySignature
if !sign.CheckSig(voteKey, b, s) {
err = fmt.Errorf("illegal vote response from server. Signature in BallotEntry is invalid")
return nil, err
}
fmt.Printf("Got signed BallotEntry for: %s\n", ballot)
return &response, nil
}