aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/gnunet/voting/GroupCert.java
blob: c363674952664b0ba0778bb9aa0fd36aa1201f37 (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
/*
 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.Optional;
import com.google.common.base.Preconditions;
import org.gnunet.util.AbsoluteTime;
import org.gnunet.util.Configuration;
import org.gnunet.util.crypto.EcdsaPrivateKey;
import org.gnunet.util.crypto.EcdsaPublicKey;
import org.gnunet.util.crypto.EcdsaSignature;

/**
 * Group certificate.  Attests to the fact that a voter (identified
 * by a ECDSA public key) belongs to a group (given as string identifier) until a
 * the an expiration date (given as timestamp).
 */
public class GroupCert {
    private String group;
    private EcdsaPublicKey member;
    private EcdsaPublicKey signerPublicKey;
    private AbsoluteTime expiration;
    private EcdsaSignature signature;

    private GroupCert() {
        // do nothing, just make constructor private
    }
    public static GroupCert create(EcdsaPublicKey member, String group, EcdsaPrivateKey signerPrivateKey,
                                   EcdsaPublicKey signerPublicKey, AbsoluteTime expiration) {
        Preconditions.checkNotNull(member);
        Preconditions.checkNotNull(group);
        Preconditions.checkNotNull(signerPrivateKey);
        Preconditions.checkNotNull(signerPublicKey);
        Preconditions.checkNotNull(expiration);
        GroupCert groupCert = new GroupCert();
        groupCert.member = member;
        groupCert.expiration = expiration;
        groupCert.group = group;
        groupCert.signerPublicKey = signerPublicKey;
        String signData = "";
        signData += member.toString();
        signData += expiration.getSeconds();
        signData += group;
        signData += signerPublicKey;
        groupCert.signature = signerPrivateKey.sign(signData.getBytes(), 0);
        return groupCert;

    }

    public static GroupCert fromBallotConfig(Ballot ballot, Configuration cfg) {
        GroupCert groupCert = new GroupCert();
        Optional<String> optGroupSig = cfg.getValueString("vote", "GROUP_SIG");
        Optional<Long> optGroupExp = cfg.getValueNumber("vote", "GROUP_EXPIRATION");
        groupCert.member = ballot.voterPub;
        groupCert.group = ballot.group;
        if (!optGroupSig.isPresent()) {
            throw new InvalidBallotException("missing group cert signature in ballot");
        }
        groupCert.signature = EcdsaSignature.fromString(optGroupSig.get());
        if (groupCert.signature == null) {
            throw new InvalidBallotException("invalid group cert signature in ballot");
        }
        if (!optGroupExp.isPresent()) {
            throw new InvalidBallotException("missing or invalid group cert expiration in ballot");
        }
        groupCert.expiration = AbsoluteTime.fromSeconds(optGroupExp.get());
        return groupCert;
    }

    public static GroupCert fromGroupCertConfig(Configuration cfg) {
        GroupCert groupCert = new GroupCert();
        Optional<String> optMember = cfg.getValueString("groupcert", "MEMBER");
        Optional<String> optCa = cfg.getValueString("groupcert", "CA");
        Optional<String> optSig = cfg.getValueString("groupcert", "SIG");
        Optional<Long> optExpiration = cfg.getValueNumber("groupcert", "EXPIRATION");
        Optional<String> optGroup = cfg.getValueString("groupcert", "GROUP");
        if (!optMember.isPresent()) {
            throw new InvalidGroupCertException("missing member key in group cert");
        }
        if (!optCa.isPresent()) {
            throw new InvalidGroupCertException("missing CA in group cert");
        }
        if (!optSig.isPresent()) {
            throw new InvalidGroupCertException("missing signature in group cert");
        }
        if (!optExpiration.isPresent()) {
            throw new InvalidGroupCertException("missing expiration in group cert");
        }
        if (!optGroup.isPresent()) {
            throw new InvalidGroupCertException("missing group identifier in group cert");
        }
        groupCert.group = optGroup.get();
        groupCert.signature = EcdsaSignature.fromString(optSig.get());
        if (null == groupCert.signature) {
            throw new InvalidGroupCertException("invalid signature in group cert");
        }
        groupCert.expiration = AbsoluteTime.fromSeconds(optExpiration.get());
        groupCert.signerPublicKey = EcdsaPublicKey.fromString(optCa.get());
        if (null == groupCert.signerPublicKey) {
            throw new InvalidGroupCertException("invalid CA in group cert");
        }
        groupCert.member = EcdsaPublicKey.fromString(optMember.get());
        if (null == groupCert.member) {
            throw new InvalidGroupCertException("invalid member in group cert");
        }
        return groupCert;
    }

    /**
     * Encode the group certificate in a ballot configuration.
     *
     * @param cfg ballot configuration
     */
    public void writeBallotConfig(Configuration cfg) {
        cfg.setValueString("vote", "GROUP_SIG", signature.toString());
        cfg.setValueNumber("vote", "GROUP_EXPIRATION", expiration.getSeconds());
    }

    /**
     * Encode the group certificate in a group certificate config file.
     *
     * @param cfg group cert config file
     */
    public void writeGroupCertConfig(Configuration cfg) {
        cfg.setValueString("groupcert", "MEMBER", member.toString());
        cfg.setValueString("groupcert", "CA", signerPublicKey.toString());
        cfg.setValueString("groupcert", "SIG", signature.toString());
        cfg.setValueNumber("groupcert", "EXPIRATION", expiration.getSeconds());
        cfg.setValueString("groupcert", "GROUP", group);
    }

    public EcdsaPublicKey getMemberPublicKey() {
        return member;
    }

    public AbsoluteTime getExpiration() {
        return expiration;
    }

    public EcdsaSignature getSignature() {
        return signature;
    }
}