-
Notifications
You must be signed in to change notification settings - Fork 1
/
secure.go
138 lines (130 loc) · 3.45 KB
/
secure.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
package main
import (
"bufio"
"crypto/elliptic"
"encoding/base64"
"encoding/hex"
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"strings"
"vote"
)
type Ballot struct {
tea *vote.Mark
coffee *vote.Mark
}
var votes chan Ballot
func MainServer(w http.ResponseWriter, req *http.Request) {
http.ServeFile(w, req, "secure.html")
}
func VoteServer(w http.ResponseWriter, req *http.Request) {
teabase := req.FormValue("tea")
coffeebase := req.FormValue("coffee")
log.Println("Recieved only ", teabase, " and ", coffeebase)
c := elliptic.P256()
public, err := base64.StdEncoding.DecodeString("BFqcfyJcH+Bx7xA9YjSxYXyVR5FIQeIH+XbeIZot+jIMnYH8nX5aOY397xXUOZiwzYvWFELsMJeSMSIHkHyR5K0=")
if err != nil {
panic("Bad public key")
}
px, py := elliptic.Unmarshal(c, public)
var ballot Ballot
teabytes, err := base64.StdEncoding.DecodeString(teabase)
if err != nil {
log.Println("Bad base 64")
w.WriteHeader(http.StatusBadRequest)
return
}
coffeebytes, err := base64.StdEncoding.DecodeString(coffeebase)
if err != nil {
log.Println("Bad base 64")
w.WriteHeader(http.StatusBadRequest)
return
}
teavote := vote.UnmarshalCheckbox(c, teabytes)
coffeevote := vote.UnmarshalCheckbox(c, coffeebytes)
if !vote.IsValidBox(c, teavote, px, py) ||
!vote.IsValidBox(c, coffeevote, px, py) {
log.Println("Bad proof")
w.WriteHeader(http.StatusBadRequest)
return
}
ballot.tea = vote.ExtractMark(teavote)
ballot.coffee = vote.ExtractMark(coffeevote)
votes <- ballot
w.WriteHeader(http.StatusOK)
}
func collect() {
c := elliptic.P256()
var tottea *vote.Mark
var totcoffee *vote.Mark
valid := false
for ballot := range votes {
if valid {
tottea = vote.AddMarks(c, tottea, ballot.tea)
totcoffee = vote.AddMarks(c, totcoffee, ballot.coffee)
} else {
tottea = ballot.tea
totcoffee = ballot.coffee
valid = true
}
teabase := base64.StdEncoding.EncodeToString(
vote.MarshalMark(c, tottea))
coffeebase := base64.StdEncoding.EncodeToString(
vote.MarshalMark(c, totcoffee))
log.Printf("Sending %s, %s to third party.\n",
teabase, coffeebase)
file, err := os.Create("votes.tmp")
if err != nil {
log.Printf("Unable to transmit votes:%s\n", err.Error())
continue
}
file.WriteString(fmt.Sprintf("tea:%s\ncoffee:%s\n",
teabase, coffeebase))
file.Close()
os.Rename("votes.tmp", "votes")
}
}
func ResultsServer(w http.ResponseWriter, req *http.Request) {
c := elliptic.P256()
priv, err := hex.DecodeString("71f8d90c7900c1fb3b80a9bed8783f5527533be09fee785a70e41144b6b4a35f")
if err != nil {
panic("Bad private key")
}
file, err := os.Open("votes")
if err != nil {
return
}
scanner := bufio.NewScanner(file)
var line string
results := make(map[string]int)
for scanner.Scan() {
line = scanner.Text()
parts := strings.Split(line, ":")
name := parts[0]
t, _ := base64.StdEncoding.DecodeString(parts[1])
ballot := vote.UnmarshalMark(c, t)
result, _ := vote.DecryptMark(c, ballot, priv)
log.Printf("Third party saw %s", line)
results[name] = result
}
w.Header().Set("Content-Type","application/json")
res, _ := json.Marshal(results)
w.Write(res)
}
func main() {
votes = make(chan Ballot, 10)
go collect()
http.HandleFunc("/", MainServer)
http.Handle("/static/",
http.StripPrefix("/static/",
http.FileServer(http.Dir("static/"))))
http.HandleFunc("/vote/", VoteServer)
http.HandleFunc("/results/", ResultsServer)
err := http.ListenAndServe(":8080", nil)
if err != nil {
log.Fatal("Listen and Serve: ", err)
}
}