aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/gnunet/voting/BallotTool.java
blob: 619afa5f6c4a59968d860d0ffb943395c8376d6e (plain) (blame)
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/*
 This file is part of GNUnet.
  (C) 2012, 2013 Christian Grothoff (and other contributing authors)

  GNUnet is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published
  by the Free Software Foundation; either version 3, or (at your
  option) any later version.

  GNUnet is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with GNUnet; see the file COPYING.  If not, write to the
  Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  Boston, MA 02111-1307, USA.
 */

package org.gnunet.voting;


import com.google.common.base.Charsets;
import com.google.common.io.ByteStreams;
import com.google.common.io.Files;
import com.google.common.io.OutputSupplier;
import org.gnunet.mesh.Mesh;
import org.gnunet.mesh.MeshRunabout;
import org.gnunet.mesh.TunnelEndHandler;
import org.gnunet.util.Configuration;
import org.gnunet.util.CryptoECC;
import org.gnunet.util.PeerIdentity;
import org.gnunet.util.Program;
import org.gnunet.util.getopt.Argument;
import org.gnunet.util.getopt.ArgumentAction;
import org.gnunet.voting.messages.BallotRegisterRequestMessage;
import org.gnunet.voting.messages.SubmitMessage;
import org.gnunet.voting.messages.SubmitResponseMessage;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.List;
import java.util.regex.Pattern;

/**
 * Tool for creating, manipulating and submitting ballot files.
 */
public class BallotTool {
    public static void main(String[] args) {
        new Program(args) {
            @Argument(
                    shortname = "q",
                    longname = "query",
                    action = ArgumentAction.SET,
                    description = "query election result")
            boolean query = false;

            @Argument(
                    shortname = "s",
                    longname = "submit",
                    action = ArgumentAction.SET,
                    description = "submit the vote to the authorities")
            boolean submit = false;

            @Argument(
                    shortname = "r",
                    longname = "register",
                    action = ArgumentAction.SET,
                    description = "register an election with the authorities")
            boolean register = false;

            @Argument(
                    shortname = "i",
                    longname = "issue",
                    action = ArgumentAction.SET,
                    description = "sign the ballot as issuer")
            boolean issue = false;

            @Argument(
                    shortname = "x",
                    longname = "select",
                    action = ArgumentAction.STORE_STRING,
                    argumentName = "CHOICE",
                    description = "select and encrypt a vote option")
            String select = null;

            @Argument(
                    shortname = "p",
                    longname = "permission",
                    action = ArgumentAction.SET,
                    description = "request permission to vote from the certificate authority")
             boolean permission = false;

            @Argument(
                    shortname = "V",
                    longname = "verify",
                    action = ArgumentAction.SET,
                    description = "verify signatures in the ballot and show information")
            boolean verify = false;

            @Argument(
                    shortname = "t",
                    longname = "template",
                    action = ArgumentAction.SET,
                    description = "write a template ballot to the give ballot file")
            boolean template = false;

            @Argument(
                    shortname = "g",
                    longname = "gen-key",
                    action = ArgumentAction.SET,
                    description = "generate a private key")
            boolean genKey = false;

            @Argument(
                    shortname = "P",
                    longname = "print-key",
                    action = ArgumentAction.SET,
                    description = "print the public key for the private key")
            boolean printKey = false;


            @Override
            protected String makeHelpText() {
                return "gnunet-ballot [OPTIONS]... BALLOT [PRIVKEYFILE]\n" +
                        "Create, modify and execute operation on ballots.";
            }

            private void runTemplate(String filename) {
                File f = new File(filename);
                if (f.exists()) {
                    System.err.println("file already exists, not overwriting");
                    return;
                }
                InputStream is = getClass().getResourceAsStream("template.espec");
                OutputSupplier<FileOutputStream> oss = Files.newOutputStreamSupplier(f);
                try {
                    ByteStreams.copy(is, oss);
                } catch (IOException e) {
                    System.err.println("could not copy template file: " + e.getMessage());
                }
            }

            private void runIssue(String ballotFilename, String privKeyFilename) {
                File bf = new File(ballotFilename);
                if (!bf.exists()) {
                    System.err.println("ballot file does not exist");
                    return;
                }
                File kf = new File(privKeyFilename);
                if (!kf.exists()) {
                    System.err.println("private-key file does not exist");
                    return;
                }
                Ballot b = new Ballot(ballotFilename);
                CryptoECC.PrivateKey privateKey = CryptoECC.PrivateKey.fromFile(privKeyFilename);
                if (privateKey == null) {
                    System.err.println("keyfile invalid");
                    return;
                }
                b.issue(privateKey);
                try {
                    Files.write(b.serialize(), bf, Charsets.UTF_8);
                } catch (IOException e) {
                    System.err.println("could not write ballot file: " + e.getMessage());
                }
            }

            public void runSelect(String ballotFilename, String privKeyFilename, String choice) {
                File bf = new File(ballotFilename);
                if (!bf.exists()) {
                    System.err.println("ballot file does not exist");
                    return;
                }
                File kf = new File(privKeyFilename);
                if (!kf.exists()) {
                    System.err.println("private-key file does not exist");
                    return;
                }
                CryptoECC.PrivateKey privateKey = CryptoECC.PrivateKey.fromFile(privKeyFilename);
                if (privateKey == null) {
                    System.err.println("keyfile invalid");
                    return;
                }
                Ballot ballot = new Ballot(ballotFilename);
                ballot.encodeChoice(choice, privateKey);
                try {
                    Files.write(ballot.serialize(), bf, Charsets.UTF_8);
                } catch (IOException e) {
                    System.err.println("can not write to ballot file");
                    return;
                }
                System.out.println("vote written to ballot file");
            }

            public void runPermission(String ballotFilename, String privKeyFilename) {

            }

            public void runGenKey(String privKeyFilename) {
                File kf = new File(privKeyFilename);
                if (kf.exists()) {
                    System.err.println("private key file already exists, not overwriting");
                    return;
                }
                CryptoECC.PrivateKey privateKey = CryptoECC.PrivateKey.createRandom();
                try {
                    privateKey.write(privKeyFilename);
                } catch (IOException e) {
                    System.err.println("can't write to key file: " + e.getMessage());
                }
            }

            public void runPrintKey(String privKeyFilename) {
                File kf = new File(privKeyFilename);
                if (!kf.exists()) {
                    System.err.println("key file does not exist");
                    return;
                }
                CryptoECC.PrivateKey privateKey = CryptoECC.PrivateKey.fromFile(privKeyFilename);
                if (privateKey == null) {
                    System.err.println("key invalid");
                    return;
                }
                CryptoECC.PublicKey publicKey = CryptoECC.computePublicKey(privateKey);
                System.out.println(publicKey.toString());
            }

            public void runVerify(String ballotFilename) {
                File bf = new File(ballotFilename);
                if (!bf.exists()) {
                    System.err.println("ballot file does not exist");
                    return;
                }
                Ballot ballot = new Ballot(ballotFilename);
                System.out.print(ballot.describe());
            }

            @Override
            public void run() {
                if (template) {
                    if (this.unprocessedArgs.length != 1) {
                        System.err.println("-t/--template requires exactly one positional argument");
                        return;
                    }
                    runTemplate(unprocessedArgs[0]);
                    return;
                }

                if (register) {
                    if (this.unprocessedArgs.length != 1) {
                        System.err.println("-r/--register requires exactly one positional argument");
                        return;
                    }
                    RegisterCommand c = new RegisterCommand(getConfiguration(), unprocessedArgs[0]);
                    c.run();
                    return;
                }
                if (issue) {
                    if (this.unprocessedArgs.length != 2) {
                        System.err.println("-i/--issue requires exactly two positional arguments");
                        return;
                    }
                    runIssue(unprocessedArgs[0], unprocessedArgs[1]);
                    return;
                }
                if (select != null) {
                    if (this.unprocessedArgs.length != 2) {
                        System.err.println("-x/--select requires exactly two positional argument");
                        return;
                    }
                    runSelect(unprocessedArgs[0], unprocessedArgs[1], select);
                    return;
                }
                if (submit) {
                    if (this.unprocessedArgs.length != 1) {
                        System.err.println("-s/--submit requires exactly one positional argument");
                        return;
                    }
                    SubmitCommand c = new SubmitCommand(getConfiguration(), unprocessedArgs[0]);
                    c.run();
                    return;
                }
                if (verify) {
                    if (this.unprocessedArgs.length != 1) {
                        System.err.println("-V/--verify requires exactly one positional argument");
                        return;
                    }
                    runVerify(unprocessedArgs[0]);
                    return;
                }
                if (query) {
                    if (this.unprocessedArgs.length != 1) {
                        System.err.println("-q/--query requires exactly one positional argument");
                        return;
                    }
                    QueryCommand c = new QueryCommand(getConfiguration(), unprocessedArgs[0]);
                    c.run();
                    return;
                }
                if (permission) {
                    if (this.unprocessedArgs.length != 2) {
                        System.err.println("-p/--permission requires exactly two positional arguments");
                        return;
                    }
                    PermissionCommand c = new PermissionCommand(getConfiguration(),
                            unprocessedArgs[0], unprocessedArgs[1]);
                    c.run();
                    return;
                }
                if (genKey) {
                    if (this.unprocessedArgs.length != 1) {
                        System.err.println("-g/--gen-key requires exactly one positional argument");
                        return;
                    }
                    runGenKey(unprocessedArgs[0]);
                }
                if (printKey) {
                    if (this.unprocessedArgs.length != 1) {
                        System.err.println("-P/--print-key requires exactly one positional argument");
                        return;
                    }
                    runPrintKey(unprocessedArgs[0]);
                }
            }
        }.start();

    }

}