Search in sources :

Example 1 with PublicKey

use of com.github.dedis.popstellar.model.objects.security.PublicKey in project popstellar by dedis.

the class ElectionHandler method handleCastVote.

/**
 * Process a CastVote message.
 *
 * @param context the HandlerContext of the message
 * @param castVote the message that was received
 */
public static void handleCastVote(HandlerContext context, CastVote castVote) {
    LAORepository laoRepository = context.getLaoRepository();
    Channel channel = context.getChannel();
    MessageID messageId = context.getMessageId();
    PublicKey senderPk = context.getSenderPk();
    Log.d(TAG, "handleCastVote: channel " + channel);
    Lao lao = laoRepository.getLaoByChannel(channel);
    Election election = laoRepository.getElectionByChannel(channel);
    // Verify the vote was created before the end of the election or the election is not closed yet
    if (election.getEndTimestamp() >= castVote.getCreation() || election.getState() != CLOSED) {
        // Retrieve previous cast vote message stored for the given sender
        Optional<MessageID> previousMessageIdOption = election.getMessageMap().entrySet().stream().filter(entry -> senderPk.equals(entry.getValue())).map(Map.Entry::getKey).findFirst();
        // Retrieve the creation time of the previous cast vote, if doesn't exist replace with min
        // value
        long previousMessageCreation = previousMessageIdOption.map(s -> laoRepository.getMessageById().get(s)).map(MessageGeneral::getData).map(CastVote.class::cast).map(CastVote::getCreation).orElse(Long.MIN_VALUE);
        // Verify the current cast vote message is the last one received
        if (previousMessageCreation <= castVote.getCreation()) {
            election.putVotesBySender(senderPk, castVote.getVotes());
            election.putSenderByMessageId(senderPk, messageId);
            lao.updateElection(election.getId(), election);
        }
    }
}
Also used : PublicKey(com.github.dedis.popstellar.model.objects.security.PublicKey) Channel(com.github.dedis.popstellar.model.objects.Channel) LAORepository(com.github.dedis.popstellar.repository.LAORepository) CastVote(com.github.dedis.popstellar.model.network.method.message.data.election.CastVote) Lao(com.github.dedis.popstellar.model.objects.Lao) Election(com.github.dedis.popstellar.model.objects.Election) Map(java.util.Map) MessageID(com.github.dedis.popstellar.model.objects.security.MessageID)

Example 2 with PublicKey

use of com.github.dedis.popstellar.model.objects.security.PublicKey in project popstellar by dedis.

the class LaoHandler method handleStateLao.

/**
 * Process a StateLao message.
 *
 * @param context the HandlerContext of the message
 * @param stateLao the message that was received
 */
public static void handleStateLao(HandlerContext context, StateLao stateLao) throws DataHandlingException {
    LAORepository laoRepository = context.getLaoRepository();
    Channel channel = context.getChannel();
    Log.d(TAG, "Receive State Lao Broadcast msg=" + stateLao);
    Lao lao = laoRepository.getLaoByChannel(channel);
    Log.d(TAG, "Receive State Lao Broadcast " + stateLao.getName());
    if (!laoRepository.getMessageById().containsKey(stateLao.getModificationId())) {
        Log.d(TAG, "Can't find modification id : " + stateLao.getModificationId());
        // queue it if we haven't received the update message yet
        throw new InvalidMessageIdException(stateLao, stateLao.getModificationId());
    }
    Log.d(TAG, "Verifying signatures");
    for (PublicKeySignaturePair pair : stateLao.getModificationSignatures()) {
        if (!pair.getWitness().verify(pair.getSignature(), stateLao.getModificationId())) {
            throw new InvalidSignatureException(stateLao, pair.getSignature());
        }
    }
    Log.d(TAG, "Success to verify state lao signatures");
    // TODO: verify if lao/state_lao is consistent with the lao/update message
    lao.setId(stateLao.getId());
    lao.setWitnesses(stateLao.getWitnesses());
    lao.setName(stateLao.getName());
    lao.setLastModified(stateLao.getLastModified());
    lao.setModificationId(stateLao.getModificationId());
    PublicKey publicKey = context.getKeyManager().getMainPublicKey();
    if (lao.getOrganizer().equals(publicKey) || lao.getWitnesses().contains(publicKey)) {
        context.getMessageSender().subscribe(lao.getChannel().subChannel("consensus")).subscribe();
    }
    // Now we're going to remove all pending updates which came prior to this state lao
    long targetTime = stateLao.getLastModified();
    lao.getPendingUpdates().removeIf(pendingUpdate -> pendingUpdate.getModificationTime() <= targetTime);
    laoRepository.updateNodes(channel);
}
Also used : InvalidSignatureException(com.github.dedis.popstellar.utility.error.InvalidSignatureException) PublicKey(com.github.dedis.popstellar.model.objects.security.PublicKey) Channel(com.github.dedis.popstellar.model.objects.Channel) LAORepository(com.github.dedis.popstellar.repository.LAORepository) InvalidMessageIdException(com.github.dedis.popstellar.utility.error.InvalidMessageIdException) StateLao(com.github.dedis.popstellar.model.network.method.message.data.lao.StateLao) CreateLao(com.github.dedis.popstellar.model.network.method.message.data.lao.CreateLao) Lao(com.github.dedis.popstellar.model.objects.Lao) UpdateLao(com.github.dedis.popstellar.model.network.method.message.data.lao.UpdateLao) PublicKeySignaturePair(com.github.dedis.popstellar.model.network.method.message.PublicKeySignaturePair)

Example 3 with PublicKey

use of com.github.dedis.popstellar.model.objects.security.PublicKey in project popstellar by dedis.

the class KeyManager method getPublicKey.

private PublicKey getPublicKey(KeysetHandle keysetHandle) throws GeneralSecurityException, IOException {
    // Retrieve the public key from the keyset. This is not an easy task and thanks to this post :
    // https://stackoverflow.com/questions/53228475/google-tink-how-use-public-key-to-verify-signature
    // A solution was found
    ByteArrayOutputStream publicKeysetStream = new ByteArrayOutputStream();
    CleartextKeysetHandle.write(keysetHandle.getPublicKeysetHandle(), JsonKeysetWriter.withOutputStream(publicKeysetStream));
    // The "publickey" is still a json data. We need to extract the actual key from it
    JsonElement publicKeyJson = JsonParser.parseString(publicKeysetStream.toString());
    JsonObject root = publicKeyJson.getAsJsonObject();
    JsonArray keyArray = root.get("key").getAsJsonArray();
    JsonObject keyObject = keyArray.get(0).getAsJsonObject();
    JsonObject keyData = keyObject.get("keyData").getAsJsonObject();
    byte[] buffer = Base64.getDecoder().decode(keyData.get("value").getAsString());
    // Remove the first two bytes of the buffer as they are not part of the key
    byte[] publicKey = Arrays.copyOfRange(buffer, 2, buffer.length);
    return new PublicKey(publicKey);
}
Also used : JsonArray(com.google.gson.JsonArray) JsonElement(com.google.gson.JsonElement) PublicKey(com.github.dedis.popstellar.model.objects.security.PublicKey) JsonObject(com.google.gson.JsonObject) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 4 with PublicKey

use of com.github.dedis.popstellar.model.objects.security.PublicKey in project popstellar by dedis.

the class ConsensusHandler method handleElectAccept.

public static void handleElectAccept(HandlerContext context, ConsensusElectAccept consensusElectAccept) throws DataHandlingException {
    LAORepository laoRepository = context.getLaoRepository();
    Channel channel = context.getChannel();
    MessageID messageId = context.getMessageId();
    PublicKey senderPk = context.getSenderPk();
    Lao lao = laoRepository.getLaoByChannel(channel);
    Optional<ElectInstance> electInstanceOpt = lao.getElectInstance(consensusElectAccept.getMessageId());
    if (!electInstanceOpt.isPresent()) {
        Log.w(TAG, "elect_accept for invalid messageId : " + consensusElectAccept.getMessageId());
        throw new InvalidMessageIdException(consensusElectAccept, consensusElectAccept.getMessageId());
    }
    ElectInstance electInstance = electInstanceOpt.get();
    electInstance.addElectAccept(senderPk, messageId, consensusElectAccept);
    lao.updateElectInstance(electInstance);
    laoRepository.updateNodes(lao.getChannel());
}
Also used : ElectInstance(com.github.dedis.popstellar.model.objects.ElectInstance) PublicKey(com.github.dedis.popstellar.model.objects.security.PublicKey) Channel(com.github.dedis.popstellar.model.objects.Channel) LAORepository(com.github.dedis.popstellar.repository.LAORepository) InvalidMessageIdException(com.github.dedis.popstellar.utility.error.InvalidMessageIdException) Lao(com.github.dedis.popstellar.model.objects.Lao) MessageID(com.github.dedis.popstellar.model.objects.security.MessageID)

Example 5 with PublicKey

use of com.github.dedis.popstellar.model.objects.security.PublicKey in project popstellar by dedis.

the class StateLaoTest method isEqualTest.

@Test
public void isEqualTest() {
    assertEquals(stateLao, new StateLao(id, name, creation, lastModified, organizer, modificationId, witnesses, modificationSignatures));
    // The modification id isn't taken into account to know if they are equal
    assertEquals(stateLao, new StateLao(id, name, creation, lastModified, organizer, Base64DataUtils.generateMessageIDOtherThan(modificationId), witnesses, modificationSignatures));
    // same goes for modification signatures
    assertEquals(stateLao, new StateLao(id, name, creation, lastModified, organizer, modificationId, witnesses, null));
    String random = " random string";
    String newId = Lao.generateLaoId(organizer, creation, random);
    assertNotEquals(stateLao, new StateLao(newId, random, creation, lastModified, organizer, modificationId, witnesses, modificationSignatures));
    PublicKey newKey = Base64DataUtils.generatePublicKeyOtherThan(organizer);
    newId = Lao.generateLaoId(newKey, creation, name);
    assertNotEquals(stateLao, new StateLao(newId, name, creation, lastModified, newKey, modificationId, witnesses, modificationSignatures));
    newId = Lao.generateLaoId(organizer, 99, name);
    assertNotEquals(stateLao, new StateLao(newId, name, 99, lastModified, organizer, modificationId, witnesses, modificationSignatures));
    assertNotEquals(stateLao, new StateLao(id, name, creation, 1000, organizer, modificationId, witnesses, modificationSignatures));
    assertNotEquals(stateLao, new StateLao(id, name, creation, lastModified, organizer, modificationId, Sets.newSet(generatePublicKey()), modificationSignatures));
}
Also used : Base64DataUtils.generatePublicKey(com.github.dedis.popstellar.testutils.Base64DataUtils.generatePublicKey) PublicKey(com.github.dedis.popstellar.model.objects.security.PublicKey) Test(org.junit.Test)

Aggregations

PublicKey (com.github.dedis.popstellar.model.objects.security.PublicKey)21 MessageID (com.github.dedis.popstellar.model.objects.security.MessageID)8 Channel (com.github.dedis.popstellar.model.objects.Channel)6 Lao (com.github.dedis.popstellar.model.objects.Lao)6 LAORepository (com.github.dedis.popstellar.repository.LAORepository)6 Test (org.junit.Test)4 ElectInstance (com.github.dedis.popstellar.model.objects.ElectInstance)3 Base64DataUtils.generatePublicKey (com.github.dedis.popstellar.testutils.Base64DataUtils.generatePublicKey)3 ConsensusElectAccept (com.github.dedis.popstellar.model.network.method.message.data.consensus.ConsensusElectAccept)2 CreateLao (com.github.dedis.popstellar.model.network.method.message.data.lao.CreateLao)2 StateLao (com.github.dedis.popstellar.model.network.method.message.data.lao.StateLao)2 UpdateLao (com.github.dedis.popstellar.model.network.method.message.data.lao.UpdateLao)2 Chirp (com.github.dedis.popstellar.model.objects.Chirp)2 KeyPair (com.github.dedis.popstellar.model.objects.security.KeyPair)2 InvalidMessageIdException (com.github.dedis.popstellar.utility.error.InvalidMessageIdException)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 Map (java.util.Map)2 DateUtils.getRelativeTimeSpanString (android.text.format.DateUtils.getRelativeTimeSpanString)1 ImageButton (android.widget.ImageButton)1