Search in sources :

Example 6 with PoPToken

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

the class Wallet method generateKeyFromPath.

/**
 * Generate a PoPToken (i.e. a key pair) from a given path.
 *
 * @param path a String path of the form: m/i/j/k/... where i,j,k,.. are 31-bit integer.
 * @return the generated PoP Token
 * @throws KeyGenerationException if an error occurs
 * @throws UninitializedWalletException if the wallet is not initialized with a seed
 */
private PoPToken generateKeyFromPath(@NonNull String path) throws KeyGenerationException, UninitializedWalletException {
    if (!isSetUp()) {
        throw new UninitializedWalletException();
    }
    // convert the path string in an array of int
    int[] pathValueInt = Arrays.stream(path.split("/")).skip(// remove the first element ('m')
    1).mapToInt(Integer::parseInt).toArray();
    try {
        // derive private and public key
        byte[] privateKey = SLIP10.deriveEd25519PrivateKey(aead.decrypt(seed, new byte[0]), pathValueInt);
        Ed25519PrivateKeyParameters prK = new Ed25519PrivateKeyParameters(privateKey, 0);
        Ed25519PublicKeyParameters puK = prK.generatePublicKey();
        byte[] publicKey = puK.getEncoded();
        return new PoPToken(privateKey, publicKey);
    } catch (GeneralSecurityException e) {
        throw new KeyGenerationException(e);
    }
}
Also used : PoPToken(com.github.dedis.popstellar.model.objects.security.PoPToken) GeneralSecurityException(java.security.GeneralSecurityException) Ed25519PublicKeyParameters(org.bouncycastle.crypto.params.Ed25519PublicKeyParameters) KeyGenerationException(com.github.dedis.popstellar.utility.error.keys.KeyGenerationException) UninitializedWalletException(com.github.dedis.popstellar.utility.error.keys.UninitializedWalletException) Ed25519PrivateKeyParameters(org.bouncycastle.crypto.params.Ed25519PrivateKeyParameters)

Example 7 with PoPToken

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

the class RollCallHandler method handleCloseRollCall.

/**
 * Process a CloseRollCall message.
 *
 * @param context the HandlerContext of the message
 * @param closeRollCall the message that was received
 */
public static void handleCloseRollCall(HandlerContext context, CloseRollCall closeRollCall) throws DataHandlingException {
    LAORepository laoRepository = context.getLaoRepository();
    Channel channel = context.getChannel();
    MessageID messageId = context.getMessageId();
    Lao lao = laoRepository.getLaoByChannel(channel);
    Log.d(TAG, "handleCloseRollCall: " + channel);
    String updateId = closeRollCall.getUpdateId();
    String closes = closeRollCall.getCloses();
    Optional<RollCall> rollCallOptional = lao.getRollCall(closes);
    if (!rollCallOptional.isPresent()) {
        Log.w(TAG, "Cannot find roll call to close : " + closes);
        throw new InvalidDataException(closeRollCall, "close id", closes);
    }
    RollCall rollCall = rollCallOptional.get();
    rollCall.setEnd(closeRollCall.getClosedAt());
    rollCall.setId(updateId);
    rollCall.getAttendees().addAll(closeRollCall.getAttendees());
    rollCall.setState(EventState.CLOSED);
    lao.updateRollCall(closes, rollCall);
    lao.updateWitnessMessage(messageId, closeRollCallWitnessMessage(messageId, rollCall));
    // Subscribe to the social media channels
    try {
        PoPToken token = context.getKeyManager().getValidPoPToken(lao, rollCall);
        context.getMessageSender().subscribe(channel.subChannel("social").subChannel(token.getPublicKey().getEncoded())).subscribe();
    } catch (InvalidPoPTokenException e) {
        Log.i(TAG, "Received a close roll-call that you did not attend");
    } catch (KeyException e) {
        Log.e(TAG, "Could not retrieve your PoP Token to subscribe you to your social media channel", e);
    }
}
Also used : PoPToken(com.github.dedis.popstellar.model.objects.security.PoPToken) InvalidPoPTokenException(com.github.dedis.popstellar.utility.error.keys.InvalidPoPTokenException) Channel(com.github.dedis.popstellar.model.objects.Channel) LAORepository(com.github.dedis.popstellar.repository.LAORepository) InvalidDataException(com.github.dedis.popstellar.utility.error.InvalidDataException) CreateRollCall(com.github.dedis.popstellar.model.network.method.message.data.rollcall.CreateRollCall) OpenRollCall(com.github.dedis.popstellar.model.network.method.message.data.rollcall.OpenRollCall) RollCall(com.github.dedis.popstellar.model.objects.RollCall) CloseRollCall(com.github.dedis.popstellar.model.network.method.message.data.rollcall.CloseRollCall) Lao(com.github.dedis.popstellar.model.objects.Lao) KeyException(com.github.dedis.popstellar.utility.error.keys.KeyException) MessageID(com.github.dedis.popstellar.model.objects.security.MessageID)

Example 8 with PoPToken

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

the class SocialMediaViewModel method isOwner.

/**
 * Check whether the sender of a chirp is the current user
 *
 * @param sender String of the PoPToken PublicKey
 * @return true if the sender is the current user
 */
public boolean isOwner(String sender) {
    Log.d(TAG, "Testing if the sender is also the owner");
    Lao lao = getCurrentLao();
    if (lao == null) {
        Log.e(TAG, LAO_FAILURE_MESSAGE);
        return false;
    }
    try {
        PoPToken token = keyManager.getValidPoPToken(lao);
        return sender.equals(token.getPublicKey().getEncoded());
    } catch (KeyException e) {
        ErrorUtils.logAndShow(getApplication(), TAG, e, R.string.error_retrieve_own_token);
        return false;
    }
}
Also used : PoPToken(com.github.dedis.popstellar.model.objects.security.PoPToken) Lao(com.github.dedis.popstellar.model.objects.Lao) KeyException(com.github.dedis.popstellar.utility.error.keys.KeyException)

Example 9 with PoPToken

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

the class RollCallTokenFragment method onCreateView.

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    mRollCallTokenFragmentBinding = RollCallTokenFragmentBinding.inflate(inflater, container, false);
    mLaoDetailViewModel = LaoDetailActivity.obtainViewModel(requireActivity());
    String rollCallId = requireArguments().getString(EXTRA_ID);
    Optional<RollCall> optRollCall = mLaoDetailViewModel.getCurrentLao().getValue().getRollCall(rollCallId);
    if (!optRollCall.isPresent()) {
        Log.d(TAG, "failed to retrieve roll call with id " + rollCallId);
        mLaoDetailViewModel.openLaoWallet();
    } else {
        rollCall = optRollCall.get();
    }
    String firstLaoId = mLaoDetailViewModel.getCurrentLaoValue().getId();
    String pk = "";
    Log.d(TAG, "rollcall: " + rollCallId);
    try {
        PoPToken token = wallet.generatePoPToken(firstLaoId, rollCall.getPersistentId());
        pk = token.getPublicKey().getEncoded();
    } catch (KeyException e) {
        Log.d(TAG, "failed to retrieve token from wallet", e);
        mLaoDetailViewModel.openLaoWallet();
    }
    mRollCallTokenFragmentBinding.rollcallName.setText("Roll Call: " + rollCall.getName());
    mRollCallTokenFragmentBinding.publicKey.setText("Public key:\n" + pk);
    Bitmap myBitmap = QRCode.from(pk).bitmap();
    mRollCallTokenFragmentBinding.pkQrCode.setImageBitmap(myBitmap);
    mRollCallTokenFragmentBinding.setLifecycleOwner(getActivity());
    return mRollCallTokenFragmentBinding.getRoot();
}
Also used : PoPToken(com.github.dedis.popstellar.model.objects.security.PoPToken) Bitmap(android.graphics.Bitmap) RollCall(com.github.dedis.popstellar.model.objects.RollCall) KeyException(com.github.dedis.popstellar.utility.error.keys.KeyException) Nullable(androidx.annotation.Nullable)

Example 10 with PoPToken

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

the class KeyManagerTest method popTokenRetrievingWorks.

@Test
public void popTokenRetrievingWorks() throws KeyException {
    PoPToken token = Base64DataUtils.generatePoPToken();
    when(wallet.recoverKey(any(), any(), any())).thenReturn(token);
    // create LAO and RollCalls
    Lao lao = new Lao("lao", Base64DataUtils.generatePublicKey(), 54213424);
    RollCall rollCall1 = new RollCall(lao.getId(), 5421364, "rollcall1");
    RollCall rollCall2 = new RollCall(lao.getId(), 5421363, "rollcall2");
    lao.updateRollCall(rollCall1.getId(), rollCall1);
    lao.updateRollCall(rollCall2.getId(), rollCall2);
    KeyManager manager = new KeyManager(androidKeysetManager, wallet);
    assertEquals(token, manager.getValidPoPToken(lao));
    assertEquals(token, manager.getValidPoPToken(lao, rollCall1));
    // make sure that rollcall1 was taken and not rollcall2 as the oldest is rollcall 1
    verify(wallet, atLeast(1)).recoverKey(eq(lao.getId()), eq(rollCall1.getId()), any());
}
Also used : PoPToken(com.github.dedis.popstellar.model.objects.security.PoPToken) RollCall(com.github.dedis.popstellar.model.objects.RollCall) Lao(com.github.dedis.popstellar.model.objects.Lao) Test(org.junit.Test) HiltAndroidTest(dagger.hilt.android.testing.HiltAndroidTest)

Aggregations

PoPToken (com.github.dedis.popstellar.model.objects.security.PoPToken)12 Lao (com.github.dedis.popstellar.model.objects.Lao)7 KeyException (com.github.dedis.popstellar.utility.error.keys.KeyException)6 Channel (com.github.dedis.popstellar.model.objects.Channel)4 RollCall (com.github.dedis.popstellar.model.objects.RollCall)4 HiltAndroidTest (dagger.hilt.android.testing.HiltAndroidTest)4 Test (org.junit.Test)4 CompositeDisposable (io.reactivex.disposables.CompositeDisposable)3 Disposable (io.reactivex.disposables.Disposable)3 MessageGeneral (com.github.dedis.popstellar.model.network.method.message.MessageGeneral)2 Wallet (com.github.dedis.popstellar.model.objects.Wallet)2 InvalidPoPTokenException (com.github.dedis.popstellar.utility.error.keys.InvalidPoPTokenException)2 KeyGenerationException (com.github.dedis.popstellar.utility.error.keys.KeyGenerationException)2 UninitializedWalletException (com.github.dedis.popstellar.utility.error.keys.UninitializedWalletException)2 GeneralSecurityException (java.security.GeneralSecurityException)2 Ed25519PrivateKeyParameters (org.bouncycastle.crypto.params.Ed25519PrivateKeyParameters)2 Ed25519PublicKeyParameters (org.bouncycastle.crypto.params.Ed25519PublicKeyParameters)2 Bitmap (android.graphics.Bitmap)1 Nullable (androidx.annotation.Nullable)1 CastVote (com.github.dedis.popstellar.model.network.method.message.data.election.CastVote)1