This repository was archived by the owner on Sep 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvote-server.go
138 lines (123 loc) · 2.99 KB
/
vote-server.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 (
"crypto"
"crypto/rand"
"crypto/rsa"
"crypto/sha256"
"crypto/x509"
"encoding/binary"
"encoding/pem"
"io"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"encoding/hex"
"fmt"
)
const LSAGS_PK_SIZE = 29
const MAX_GROUP_SIZE = 1000
var server_sk *rsa.PrivateKey
var election_tag []byte
func Exists(name string) bool {
if _, err := os.Stat(name); err != nil {
if os.IsNotExist(err) {
return false
}
}
return true
}
func statusHandler(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("VOTE"))
}
func groupVoteHandler(group string) func(http.ResponseWriter, *http.Request) {
var group_number uint64
if _, err := fmt.Sscan(group, &group_number); err != nil {
panic(err)
}
return func(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
return
}
r.Body = http.MaxBytesReader(w, r.Body, 256+(MAX_GROUP_SIZE+2)*(LSAGS_PK_SIZE))
postdata, err := ioutil.ReadAll(r.Body)
if err != nil {
return
}
if len(postdata) < 2+LSAGS_PK_SIZE {
return
}
vote_size := postdata[0] | postdata[1]<<8
filename := hex.EncodeToString(postdata[2+vote_size:2+vote_size+LSAGS_PK_SIZE]) + ".vote"
vote_path := filepath.Join("votes", group, filename)
if Exists(vote_path) {
return
} // no re-voting for now
h := sha256.New()
if _, err := h.Write([]byte("VOTEVOTE")); err != nil {
return
}
if _, err := h.Write(election_tag); err != nil {
return
}
if err := binary.Write(h, binary.LittleEndian, group_number); err != nil {
return
}
if _, err := h.Write(postdata); err != nil {
return
}
h_val := h.Sum(nil)
server_sig, err := rsa.SignPKCS1v15(rand.Reader, server_sk, crypto.SHA256, h_val)
if err != nil {
return
}
if err := ioutil.WriteFile(vote_path, postdata, os.FileMode(0600)); err != nil {
panic(err)
}
w.Write(server_sig)
fmt.Println("Received a vote ", vote_path)
}
}
func main() {
if len(os.Args) != 2 {
panic("usage: vote-server.go SECRETKEY_FILE")
}
election_tag = make([]byte, 32)
{
filetype := make([]byte, 8)
f, err := os.Open(filepath.Join("groups", "groups"))
defer f.Close()
if err != nil {
panic(err)
}
if _, err := io.ReadFull(f, filetype); err != nil {
panic(err)
}
if _, err := io.ReadFull(f, election_tag); err != nil {
panic(err)
}
}
server_sk_raw, err := ioutil.ReadFile(os.Args[1])
if err != nil {
panic(err)
}
server_sk_pem, _ := pem.Decode(server_sk_raw)
if server_sk_pem == nil {
panic("No PEM block in server secret key file?")
}
server_sk, err = x509.ParsePKCS1PrivateKey(server_sk_pem.Bytes)
if err != nil {
panic(err)
}
groups, err := filepath.Glob(filepath.Join("votes", "*"))
if err != nil {
panic(err)
}
for _, group := range groups {
http.HandleFunc("/"+filepath.ToSlash(group),
groupVoteHandler(filepath.Base(group)))
}
http.Handle("/groups/", http.StripPrefix("/groups", http.FileServer(http.Dir("groups/"))))
http.HandleFunc("/status", statusHandler)
panic(http.ListenAndServeTLS(":10443", "cert.pem", os.Args[1], nil))
}