Search in sources :

Example 1 with Signature

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

the class LaoDetailViewModel method signMessage.

public void signMessage(WitnessMessage witnessMessage) {
    Log.d(TAG, "signing message with ID " + witnessMessage.getMessageId());
    Lao lao = getCurrentLaoValue();
    if (lao == null) {
        Log.d(TAG, LAO_FAILURE_MESSAGE);
        return;
    }
    Channel channel = lao.getChannel();
    try {
        KeyPair mainKey = keyManager.getMainKeyPair();
        // generate the signature of the message
        Signature signature = mainKey.sign(witnessMessage.getMessageId());
        Log.d(TAG, PUBLISH_MESSAGE);
        WitnessMessageSignature signatureMessage = new WitnessMessageSignature(witnessMessage.getMessageId(), signature);
        disposables.add(networkManager.getMessageSender().publish(keyManager.getMainKeyPair(), channel, signatureMessage).subscribe(() -> Log.d(TAG, "Verifying the signature of  message  with id: " + witnessMessage.getMessageId()), error -> ErrorUtils.logAndShow(getApplication(), TAG, error, R.string.error_sign_message)));
    } catch (GeneralSecurityException e) {
        Log.d(TAG, PK_FAILURE_MESSAGE, e);
    }
}
Also used : PackageManager(android.content.pm.PackageManager) HomeViewModel(com.github.dedis.popstellar.ui.home.HomeViewModel) NonNull(androidx.annotation.NonNull) ElectionEnd(com.github.dedis.popstellar.model.network.method.message.data.election.ElectionEnd) StateLao(com.github.dedis.popstellar.model.network.method.message.data.lao.StateLao) LiveDataReactiveStreams(androidx.lifecycle.LiveDataReactiveStreams) ConsensusNode(com.github.dedis.popstellar.model.objects.ConsensusNode) AndroidSchedulers(io.reactivex.android.schedulers.AndroidSchedulers) PublicKey(com.github.dedis.popstellar.model.objects.security.PublicKey) Manifest(android.Manifest) GeneralSecurityException(java.security.GeneralSecurityException) Lao(com.github.dedis.popstellar.model.objects.Lao) WitnessMessage(com.github.dedis.popstellar.model.objects.WitnessMessage) Gson(com.google.gson.Gson) Schedulers(io.reactivex.schedulers.Schedulers) Transformations(androidx.lifecycle.Transformations) ContextCompat(androidx.core.content.ContextCompat) Log(android.util.Log) CloseRollCall(com.github.dedis.popstellar.model.network.method.message.data.rollcall.CloseRollCall) ConsensusElect(com.github.dedis.popstellar.model.network.method.message.data.consensus.ConsensusElect) KeyManager(com.github.dedis.popstellar.utility.security.KeyManager) MessageGeneral(com.github.dedis.popstellar.model.network.method.message.MessageGeneral) Set(java.util.Set) ElectionVote(com.github.dedis.popstellar.model.network.method.message.data.election.ElectionVote) KeyGenerationException(com.github.dedis.popstellar.utility.error.keys.KeyGenerationException) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) ElectionSetup(com.github.dedis.popstellar.model.network.method.message.data.election.ElectionSetup) HiltViewModel(dagger.hilt.android.lifecycle.HiltViewModel) Objects(java.util.Objects) ScanningAction(com.github.dedis.popstellar.ui.qrcode.ScanningAction) List(java.util.List) CompositeDisposable(io.reactivex.disposables.CompositeDisposable) Disposable(io.reactivex.disposables.Disposable) Stream(java.util.stream.Stream) KeyException(com.github.dedis.popstellar.utility.error.keys.KeyException) Application(android.app.Application) RollCall(com.github.dedis.popstellar.model.objects.RollCall) Optional(java.util.Optional) SingleEvent(com.github.dedis.popstellar.SingleEvent) Wallet(com.github.dedis.popstellar.model.objects.Wallet) CameraPermissionViewModel(com.github.dedis.popstellar.ui.qrcode.CameraPermissionViewModel) MutableLiveData(androidx.lifecycle.MutableLiveData) R(com.github.dedis.popstellar.R) CreateRollCall(com.github.dedis.popstellar.model.network.method.message.data.rollcall.CreateRollCall) EventType(com.github.dedis.popstellar.model.objects.event.EventType) EventState(com.github.dedis.popstellar.model.objects.event.EventState) ErrorUtils(com.github.dedis.popstellar.utility.error.ErrorUtils) Signature(com.github.dedis.popstellar.model.objects.security.Signature) ElectInstance(com.github.dedis.popstellar.model.objects.ElectInstance) LAORepository(com.github.dedis.popstellar.repository.LAORepository) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Inject(javax.inject.Inject) PoPToken(com.github.dedis.popstellar.model.objects.security.PoPToken) UninitializedWalletException(com.github.dedis.popstellar.utility.error.keys.UninitializedWalletException) KeyPair(com.github.dedis.popstellar.model.objects.security.KeyPair) Toast(android.widget.Toast) AndroidViewModel(androidx.lifecycle.AndroidViewModel) UpdateLao(com.github.dedis.popstellar.model.network.method.message.data.lao.UpdateLao) WitnessMessageSignature(com.github.dedis.popstellar.model.network.method.message.data.message.WitnessMessageSignature) LiveData(androidx.lifecycle.LiveData) Channel(com.github.dedis.popstellar.model.objects.Channel) BackpressureStrategy(io.reactivex.BackpressureStrategy) Election(com.github.dedis.popstellar.model.objects.Election) CastVote(com.github.dedis.popstellar.model.network.method.message.data.election.CastVote) OpenRollCall(com.github.dedis.popstellar.model.network.method.message.data.rollcall.OpenRollCall) ConsensusElectAccept(com.github.dedis.popstellar.model.network.method.message.data.consensus.ConsensusElectAccept) Barcode(com.google.android.gms.vision.barcode.Barcode) QRCodeScanningViewModel(com.github.dedis.popstellar.ui.qrcode.QRCodeScanningViewModel) MessageID(com.github.dedis.popstellar.model.objects.security.MessageID) VisibleForTesting(androidx.annotation.VisibleForTesting) GlobalNetworkManager(com.github.dedis.popstellar.repository.remote.GlobalNetworkManager) KeyPair(com.github.dedis.popstellar.model.objects.security.KeyPair) WitnessMessageSignature(com.github.dedis.popstellar.model.network.method.message.data.message.WitnessMessageSignature) Channel(com.github.dedis.popstellar.model.objects.Channel) Signature(com.github.dedis.popstellar.model.objects.security.Signature) WitnessMessageSignature(com.github.dedis.popstellar.model.network.method.message.data.message.WitnessMessageSignature) GeneralSecurityException(java.security.GeneralSecurityException) StateLao(com.github.dedis.popstellar.model.network.method.message.data.lao.StateLao) Lao(com.github.dedis.popstellar.model.objects.Lao) UpdateLao(com.github.dedis.popstellar.model.network.method.message.data.lao.UpdateLao)

Example 2 with Signature

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

the class MessageGeneral method verify.

public boolean verify() {
    if (!this.sender.verify(this.signature, this.dataBuf))
        return false;
    if (this.data instanceof WitnessMessageSignature) {
        WitnessMessageSignature witness = (WitnessMessageSignature) this.data;
        Signature witnessSignature = witness.getSignature();
        MessageID messageID = witness.getMessageId();
        return this.sender.verify(witnessSignature, messageID);
    } else {
        return true;
    }
}
Also used : WitnessMessageSignature(com.github.dedis.popstellar.model.network.method.message.data.message.WitnessMessageSignature) Signature(com.github.dedis.popstellar.model.objects.security.Signature) WitnessMessageSignature(com.github.dedis.popstellar.model.network.method.message.data.message.WitnessMessageSignature) MessageID(com.github.dedis.popstellar.model.objects.security.MessageID)

Example 3 with Signature

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

the class MessageGeneralTest method verifyFailsOnInvalidData.

@Test
public void verifyFailsOnInvalidData() {
    MessageGeneral msg = new MessageGeneral(KEY_PAIR.getPublicKey(), DATA_ENCODED, DATA, new Signature("UB6xpjpUGN5VtmWAw1T3npHxiZfKaXzx3ny5PXl_qF4"), MESSAGE_ID, WITNESS_SIGNATURES);
    assertThat(msg.verify(), is(false));
}
Also used : Signature(com.github.dedis.popstellar.model.objects.security.Signature) Test(org.junit.Test)

Example 4 with Signature

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

the class KeyManagerTest method keyPairIsRight.

@Test
public void keyPairIsRight() throws GeneralSecurityException {
    KeyManager keyManager = new KeyManager(androidKeysetManager, wallet);
    KeyPair mainKeyPair = keyManager.getMainKeyPair();
    PublicKey mainKey = keyManager.getMainPublicKey();
    assertEquals(mainKey, mainKeyPair.getPublicKey());
    // We cannot extract the public key from the keyset handle
    // But we can make sure both signatures are equals and the key manager keypair can verify it
    PublicKeySign signer = androidKeysetManager.getKeysetHandle().getPrimitive(PublicKeySign.class);
    // Generate any data, here a message id
    Base64URLData data = Base64DataUtils.generateMessageID();
    Signature signature = mainKeyPair.sign(data);
    assertArrayEquals(signer.sign(data.getData()), signature.getData());
    assertTrue(mainKey.verify(signature, data));
}
Also used : KeyPair(com.github.dedis.popstellar.model.objects.security.KeyPair) Base64URLData(com.github.dedis.popstellar.model.objects.security.Base64URLData) PublicKey(com.github.dedis.popstellar.model.objects.security.PublicKey) Signature(com.github.dedis.popstellar.model.objects.security.Signature) PublicKeySign(com.google.crypto.tink.PublicKeySign) Test(org.junit.Test) HiltAndroidTest(dagger.hilt.android.testing.HiltAndroidTest)

Aggregations

Signature (com.github.dedis.popstellar.model.objects.security.Signature)3 WitnessMessageSignature (com.github.dedis.popstellar.model.network.method.message.data.message.WitnessMessageSignature)2 KeyPair (com.github.dedis.popstellar.model.objects.security.KeyPair)2 MessageID (com.github.dedis.popstellar.model.objects.security.MessageID)2 Test (org.junit.Test)2 Manifest (android.Manifest)1 Application (android.app.Application)1 PackageManager (android.content.pm.PackageManager)1 Log (android.util.Log)1 Toast (android.widget.Toast)1 NonNull (androidx.annotation.NonNull)1 VisibleForTesting (androidx.annotation.VisibleForTesting)1 ContextCompat (androidx.core.content.ContextCompat)1 AndroidViewModel (androidx.lifecycle.AndroidViewModel)1 LiveData (androidx.lifecycle.LiveData)1 LiveDataReactiveStreams (androidx.lifecycle.LiveDataReactiveStreams)1 MutableLiveData (androidx.lifecycle.MutableLiveData)1 Transformations (androidx.lifecycle.Transformations)1 R (com.github.dedis.popstellar.R)1 SingleEvent (com.github.dedis.popstellar.SingleEvent)1