Example #1
0
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
}
Example #2
0
func HandelSignatureRequest(data []byte, c net.Conn) {
	var v msgs.Vote
	err := proto.Unmarshal(data, &v)
	if err != nil {
		fmt.Println("server error reading Vote:", err)
		server.ConnectionError(c)
		return
	}

	err = msg.ValidateVote(ballotKey, &v)
	if err != nil {
		fmt.Println(err)
		return
	}

	keyString := stringKey(&v)
	// TODO: needs a lock
	id, ok := ballotSet[keyString]
	if !ok {
		id = len(votes)
		votes = append(votes, v)
		ballotSet[keyString] = id
		fmt.Printf("Got Vote %d: %s\n", id, v.Ballot)
	}

	var response msgs.VoteResponse
	var ballotEntry msgs.BallotEntry
	tmp := uint64(id)
	ballotEntry.Id = &tmp
	ballotEntry.Ballot = votes[id].Ballot

	ballotBytes, err := proto.Marshal(&ballotEntry)

	response.BallotEntry = ballotBytes
	response.BallotEntrySignature, err = sign.Sign(votePrivateKey, ballotBytes)

	responseData, err := proto.Marshal(&response)
	server.SendBlock(msg.VoteResponse, responseData, c)
}