Search in sources :

Example 26 with InvalidJidException

use of de.pixart.messenger.xmpp.jid.InvalidJidException in project Pix-Art-Messenger by kriztan.

the class AxolotlService method buildSessionFromPEP.

private void buildSessionFromPEP(final SignalProtocolAddress address) {
    Log.i(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Building new session for " + address.toString());
    if (address.equals(getOwnAxolotlAddress())) {
        throw new AssertionError("We should NEVER build a session with ourselves. What happened here?!");
    }
    try {
        IqPacket bundlesPacket = mXmppConnectionService.getIqGenerator().retrieveBundlesForDevice(Jid.fromString(address.getName()), address.getDeviceId());
        Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Retrieving bundle: " + bundlesPacket);
        mXmppConnectionService.sendIqPacket(account, bundlesPacket, new OnIqPacketReceived() {

            @Override
            public void onIqPacketReceived(Account account, IqPacket packet) {
                if (packet.getType() == IqPacket.TYPE.TIMEOUT) {
                    fetchStatusMap.put(address, FetchStatus.TIMEOUT);
                } else if (packet.getType() == IqPacket.TYPE.RESULT) {
                    Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Received preKey IQ packet, processing...");
                    final IqParser parser = mXmppConnectionService.getIqParser();
                    final List<PreKeyBundle> preKeyBundleList = parser.preKeys(packet);
                    final PreKeyBundle bundle = parser.bundle(packet);
                    if (preKeyBundleList.isEmpty() || bundle == null) {
                        Log.e(Config.LOGTAG, AxolotlService.getLogprefix(account) + "preKey IQ packet invalid: " + packet);
                        fetchStatusMap.put(address, FetchStatus.ERROR);
                        finishBuildingSessionsFromPEP(address);
                        return;
                    }
                    Random random = new Random();
                    final PreKeyBundle preKey = preKeyBundleList.get(random.nextInt(preKeyBundleList.size()));
                    if (preKey == null) {
                        // should never happen
                        fetchStatusMap.put(address, FetchStatus.ERROR);
                        finishBuildingSessionsFromPEP(address);
                        return;
                    }
                    final PreKeyBundle preKeyBundle = new PreKeyBundle(0, address.getDeviceId(), preKey.getPreKeyId(), preKey.getPreKey(), bundle.getSignedPreKeyId(), bundle.getSignedPreKey(), bundle.getSignedPreKeySignature(), bundle.getIdentityKey());
                    try {
                        SessionBuilder builder = new SessionBuilder(axolotlStore, address);
                        builder.process(preKeyBundle);
                        XmppAxolotlSession session = new XmppAxolotlSession(account, axolotlStore, address, bundle.getIdentityKey());
                        sessions.put(address, session);
                        if (Config.X509_VERIFICATION) {
                            verifySessionWithPEP(session);
                        } else {
                            FingerprintStatus status = getFingerprintTrust(CryptoHelper.bytesToHex(bundle.getIdentityKey().getPublicKey().serialize()));
                            FetchStatus fetchStatus;
                            if (status != null && status.isVerified()) {
                                fetchStatus = FetchStatus.SUCCESS_VERIFIED;
                            } else if (status != null && status.isTrusted()) {
                                fetchStatus = FetchStatus.SUCCESS_TRUSTED;
                            } else {
                                fetchStatus = FetchStatus.SUCCESS;
                            }
                            fetchStatusMap.put(address, fetchStatus);
                            finishBuildingSessionsFromPEP(address);
                        }
                    } catch (UntrustedIdentityException | InvalidKeyException e) {
                        Log.e(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Error building session for " + address + ": " + e.getClass().getName() + ", " + e.getMessage());
                        fetchStatusMap.put(address, FetchStatus.ERROR);
                        finishBuildingSessionsFromPEP(address);
                    }
                } else {
                    fetchStatusMap.put(address, FetchStatus.ERROR);
                    Log.d(Config.LOGTAG, getLogprefix(account) + "Error received while building session:" + packet.findChild("error"));
                    finishBuildingSessionsFromPEP(address);
                }
            }
        });
    } catch (InvalidJidException e) {
        Log.e(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Got address with invalid jid: " + address.getName());
    }
}
Also used : Account(de.pixart.messenger.entities.Account) IqParser(de.pixart.messenger.parser.IqParser) UntrustedIdentityException(org.whispersystems.libsignal.UntrustedIdentityException) OnIqPacketReceived(de.pixart.messenger.xmpp.OnIqPacketReceived) InvalidJidException(de.pixart.messenger.xmpp.jid.InvalidJidException) SessionBuilder(org.whispersystems.libsignal.SessionBuilder) InvalidKeyException(org.whispersystems.libsignal.InvalidKeyException) IqPacket(de.pixart.messenger.xmpp.stanzas.IqPacket) PreKeyBundle(org.whispersystems.libsignal.state.PreKeyBundle) Random(java.util.Random)

Example 27 with InvalidJidException

use of de.pixart.messenger.xmpp.jid.InvalidJidException in project Pix-Art-Messenger by kriztan.

the class AxolotlService method completeSession.

private void completeSession(XmppAxolotlSession session) {
    final XmppAxolotlMessage axolotlMessage = new XmppAxolotlMessage(account.getJid().toBareJid(), getOwnDeviceId());
    axolotlMessage.addDevice(session);
    try {
        Jid jid = Jid.fromString(session.getRemoteAddress().getName());
        MessagePacket packet = mXmppConnectionService.getMessageGenerator().generateKeyTransportMessage(jid, axolotlMessage);
        mXmppConnectionService.sendMessagePacket(account, packet);
    } catch (InvalidJidException e) {
        throw new Error("Remote addresses are created from jid and should convert back to jid", e);
    }
}
Also used : MessagePacket(de.pixart.messenger.xmpp.stanzas.MessagePacket) Jid(de.pixart.messenger.xmpp.jid.Jid) InvalidJidException(de.pixart.messenger.xmpp.jid.InvalidJidException)

Example 28 with InvalidJidException

use of de.pixart.messenger.xmpp.jid.InvalidJidException in project Pix-Art-Messenger by kriztan.

the class MagicCreateActivity method afterTextChanged.

@Override
public void afterTextChanged(Editable s) {
    if (s.toString().trim().length() > 0) {
        try {
            mFullJidDisplay.setVisibility(View.VISIBLE);
            Jid jid = Jid.fromParts(s.toString().toLowerCase(), Config.MAGIC_CREATE_DOMAIN, null);
            mFullJidDisplay.setText(getString(R.string.your_full_jid_will_be, jid.toString()));
        } catch (InvalidJidException e) {
            mFullJidDisplay.setVisibility(View.INVISIBLE);
        }
    } else {
        mFullJidDisplay.setVisibility(View.INVISIBLE);
    }
}
Also used : Jid(de.pixart.messenger.xmpp.jid.Jid) InvalidJidException(de.pixart.messenger.xmpp.jid.InvalidJidException)

Example 29 with InvalidJidException

use of de.pixart.messenger.xmpp.jid.InvalidJidException in project Pix-Art-Messenger by kriztan.

the class EditAccountActivity method onBackendConnected.

protected void onBackendConnected() {
    boolean init = true;
    if (mSavedInstanceAccount != null) {
        try {
            this.mAccount = xmppConnectionService.findAccountByJid(Jid.fromString(mSavedInstanceAccount));
            this.mInitMode = mSavedInstanceInit;
            init = false;
        } catch (InvalidJidException e) {
            this.mAccount = null;
        }
    } else if (this.jidToEdit != null) {
        this.mAccount = xmppConnectionService.findAccountByJid(jidToEdit);
    }
    if (mAccount != null) {
        this.mInitMode |= this.mAccount.isOptionSet(Account.OPTION_REGISTER);
        this.mUsernameMode |= mAccount.isOptionSet(Account.OPTION_MAGIC_CREATE) && mAccount.isOptionSet(Account.OPTION_REGISTER);
        if (this.mAccount.getPrivateKeyAlias() != null) {
            this.mPassword.setHint(R.string.authenticate_with_certificate);
            if (this.mInitMode) {
                this.mPassword.requestFocus();
            }
        }
        if (mPendingFingerprintVerificationUri != null) {
            processFingerprintVerification(mPendingFingerprintVerificationUri, false);
            mPendingFingerprintVerificationUri = null;
        }
        updateAccountInformation(init);
    }
    if (Config.MAGIC_CREATE_DOMAIN == null && this.xmppConnectionService.getAccounts().size() == 0) {
        this.mCancelButton.setEnabled(false);
    }
    if (mUsernameMode) {
        this.binding.accountJid.setHint(R.string.username_hint);
    } else {
        final KnownHostsAdapter mKnownHostsAdapter = new KnownHostsAdapter(this, R.layout.simple_list_item, xmppConnectionService.getKnownHosts());
        this.binding.accountJid.setAdapter(mKnownHostsAdapter);
    }
    if (pendingUri != null) {
        processFingerprintVerification(pendingUri, false);
        pendingUri = null;
    }
    updateSaveButton();
    invalidateOptionsMenu();
}
Also used : KnownHostsAdapter(de.pixart.messenger.ui.adapter.KnownHostsAdapter) InvalidJidException(de.pixart.messenger.xmpp.jid.InvalidJidException)

Example 30 with InvalidJidException

use of de.pixart.messenger.xmpp.jid.InvalidJidException in project Pix-Art-Messenger by kriztan.

the class MagicCreateActivity method onCreate.

@Override
protected void onCreate(final Bundle savedInstanceState) {
    if (getResources().getBoolean(R.bool.portrait_only)) {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }
    super.onCreate(savedInstanceState);
    setContentView(R.layout.magic_create);
    mFullJidDisplay = findViewById(R.id.full_jid);
    mUsername = findViewById(R.id.username);
    mRandom = new SecureRandom();
    Button next = findViewById(R.id.create_account);
    next.setOnClickListener(v -> {
        String username = mUsername.getText().toString();
        if (username.contains("@") || username.length() < 3) {
            mUsername.setError(getString(R.string.invalid_username));
            mUsername.requestFocus();
        } else {
            mUsername.setError(null);
            try {
                Jid jid = Jid.fromParts(username.toLowerCase(), Config.MAGIC_CREATE_DOMAIN, null);
                Account account = xmppConnectionService.findAccountByJid(jid);
                if (account == null) {
                    account = new Account(jid, createPassword());
                    account.setOption(Account.OPTION_REGISTER, true);
                    account.setOption(Account.OPTION_DISABLED, true);
                    account.setOption(Account.OPTION_MAGIC_CREATE, true);
                    xmppConnectionService.createAccount(account);
                }
                Intent intent = new Intent(MagicCreateActivity.this, EditAccountActivity.class);
                intent.putExtra("jid", account.getJid().toBareJid().toString());
                intent.putExtra("init", true);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                Toast.makeText(MagicCreateActivity.this, R.string.secure_password_generated, Toast.LENGTH_SHORT).show();
                WelcomeActivity.addInviteUri(intent, getIntent());
                startActivity(intent);
            } catch (InvalidJidException e) {
                mUsername.setError(getString(R.string.invalid_username));
                mUsername.requestFocus();
            }
        }
    });
    mUsername.addTextChangedListener(this);
}
Also used : Account(de.pixart.messenger.entities.Account) Jid(de.pixart.messenger.xmpp.jid.Jid) Button(android.widget.Button) InvalidJidException(de.pixart.messenger.xmpp.jid.InvalidJidException) SecureRandom(java.security.SecureRandom) Intent(android.content.Intent)

Aggregations

InvalidJidException (de.pixart.messenger.xmpp.jid.InvalidJidException)33 Jid (de.pixart.messenger.xmpp.jid.Jid)25 Account (de.pixart.messenger.entities.Account)14 Conversation (de.pixart.messenger.entities.Conversation)13 MessagePacket (de.pixart.messenger.xmpp.stanzas.MessagePacket)5 AlertDialog (android.support.v7.app.AlertDialog)4 Contact (de.pixart.messenger.entities.Contact)4 Message (de.pixart.messenger.entities.Message)4 IqPacket (de.pixart.messenger.xmpp.stanzas.IqPacket)4 View (android.view.View)3 AdapterView (android.widget.AdapterView)3 ListView (android.widget.ListView)3 OnIqPacketReceived (de.pixart.messenger.xmpp.OnIqPacketReceived)3 ArrayList (java.util.ArrayList)3 SuppressLint (android.annotation.SuppressLint)2 Bundle (android.os.Bundle)2 SpannableString (android.text.SpannableString)2 Pair (android.util.Pair)2 OnItemClickListener (android.widget.AdapterView.OnItemClickListener)2 XmppAxolotlMessage (de.pixart.messenger.crypto.axolotl.XmppAxolotlMessage)2