Search in sources :

Example 46 with Account

use of de.pixart.messenger.entities.Account in project Pix-Art-Messenger by kriztan.

the class SettingsActivity method deleteOmemoIdentities.

private boolean deleteOmemoIdentities() {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle(R.string.pref_delete_omemo_identities);
    final List<CharSequence> accounts = new ArrayList<>();
    for (Account account : xmppConnectionService.getAccounts()) {
        if (account.isEnabled()) {
            accounts.add(account.getJid().toBareJid().toString());
        }
    }
    final boolean[] checkedItems = new boolean[accounts.size()];
    builder.setMultiChoiceItems(accounts.toArray(new CharSequence[accounts.size()]), checkedItems, (dialog, which, isChecked) -> {
        checkedItems[which] = isChecked;
        final AlertDialog alertDialog = (AlertDialog) dialog;
        for (boolean item : checkedItems) {
            if (item) {
                alertDialog.getButton(DialogInterface.BUTTON_POSITIVE).setEnabled(true);
                return;
            }
        }
        alertDialog.getButton(DialogInterface.BUTTON_POSITIVE).setEnabled(false);
    });
    builder.setNegativeButton(R.string.cancel, null);
    builder.setPositiveButton(R.string.delete_selected_keys, (dialog, which) -> {
        for (int i = 0; i < checkedItems.length; ++i) {
            if (checkedItems[i]) {
                try {
                    Jid jid = Jid.fromString(accounts.get(i).toString());
                    Account account = xmppConnectionService.findAccountByJid(jid);
                    if (account != null) {
                        account.getAxolotlService().regenerateKeys(true);
                    }
                } catch (InvalidJidException e) {
                // 
                }
            }
        }
    });
    AlertDialog dialog = builder.create();
    dialog.show();
    dialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
    return true;
}
Also used : AlertDialog(android.support.v7.app.AlertDialog) Account(de.pixart.messenger.entities.Account) Jid(de.pixart.messenger.xmpp.jid.Jid) InvalidJidException(de.pixart.messenger.xmpp.jid.InvalidJidException) ArrayList(java.util.ArrayList)

Example 47 with Account

use of de.pixart.messenger.entities.Account in project Pix-Art-Messenger by kriztan.

the class XmppConnection method sendRegistryRequest.

private void sendRegistryRequest() {
    final IqPacket register = new IqPacket(IqPacket.TYPE.GET);
    register.query("jabber:iq:register");
    register.setTo(account.getServer());
    sendUnmodifiedIqPacket(register, new OnIqPacketReceived() {

        @Override
        public void onIqPacketReceived(final Account account, final IqPacket packet) {
            if (packet.getType() == IqPacket.TYPE.TIMEOUT) {
                return;
            }
            if (packet.getType() == IqPacket.TYPE.ERROR) {
                throw new StateChangingError(Account.State.REGISTRATION_FAILED);
            }
            final Element query = packet.query("jabber:iq:register");
            if (query.hasChild("username") && (query.hasChild("password"))) {
                final IqPacket register = new IqPacket(IqPacket.TYPE.SET);
                final Element username = new Element("username").setContent(account.getUsername());
                final Element password = new Element("password").setContent(account.getPassword());
                register.query("jabber:iq:register").addChild(username);
                register.query().addChild(password);
                register.setFrom(account.getJid().toBareJid());
                sendUnmodifiedIqPacket(register, registrationResponseListener, true);
            } else if (query.hasChild("x", Namespace.DATA)) {
                final Data data = Data.parse(query.findChild("x", Namespace.DATA));
                final Element blob = query.findChild("data", "urn:xmpp:bob");
                final String id = packet.getId();
                InputStream is;
                if (blob != null) {
                    try {
                        final String base64Blob = blob.getContent();
                        final byte[] strBlob = Base64.decode(base64Blob, Base64.DEFAULT);
                        is = new ByteArrayInputStream(strBlob);
                    } catch (Exception e) {
                        is = null;
                    }
                } else {
                    try {
                        Field field = data.getFieldByName("url");
                        URL url = field != null && field.getValue() != null ? new URL(field.getValue()) : null;
                        is = url != null ? url.openStream() : null;
                    } catch (IOException e) {
                        is = null;
                    }
                }
                if (is != null) {
                    Bitmap captcha = BitmapFactory.decodeStream(is);
                    try {
                        if (mXmppConnectionService.displayCaptchaRequest(account, id, data, captcha)) {
                            return;
                        }
                    } catch (Exception e) {
                        throw new StateChangingError(Account.State.REGISTRATION_FAILED);
                    }
                }
                throw new StateChangingError(Account.State.REGISTRATION_FAILED);
            } else if (query.hasChild("instructions") || query.hasChild("x", Namespace.OOB)) {
                final String instructions = query.findChildContent("instructions");
                final Element oob = query.findChild("x", Namespace.OOB);
                final String url = oob == null ? null : oob.findChildContent("url");
                if (url != null) {
                    setAccountCreationFailed(url);
                } else if (instructions != null) {
                    Matcher matcher = Patterns.AUTOLINK_WEB_URL.matcher(instructions);
                    if (matcher.find()) {
                        setAccountCreationFailed(instructions.substring(matcher.start(), matcher.end()));
                    }
                }
                throw new StateChangingError(Account.State.REGISTRATION_FAILED);
            }
        }
    }, true);
}
Also used : Account(de.pixart.messenger.entities.Account) Matcher(java.util.regex.Matcher) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Element(de.pixart.messenger.xml.Element) Data(de.pixart.messenger.xmpp.forms.Data) IOException(java.io.IOException) KeyManagementException(java.security.KeyManagementException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidJidException(de.pixart.messenger.xmpp.jid.InvalidJidException) ConnectException(java.net.ConnectException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) URL(java.net.URL) IqPacket(de.pixart.messenger.xmpp.stanzas.IqPacket) Field(de.pixart.messenger.xmpp.forms.Field) Bitmap(android.graphics.Bitmap) ByteArrayInputStream(java.io.ByteArrayInputStream)

Example 48 with Account

use of de.pixart.messenger.entities.Account in project Pix-Art-Messenger by kriztan.

the class AxolotlService method publishDeviceIdsAndRefineAccessModel.

private void publishDeviceIdsAndRefineAccessModel(final Set<Integer> ids, final boolean firstAttempt) {
    final Bundle publishOptions = account.getXmppConnection().getFeatures().pepPublishOptions() ? PublishOptions.openAccess() : null;
    IqPacket publish = mXmppConnectionService.getIqGenerator().publishDeviceIds(ids, publishOptions);
    mXmppConnectionService.sendIqPacket(account, publish, new OnIqPacketReceived() {

        @Override
        public void onIqPacketReceived(Account account, IqPacket packet) {
            Element error = packet.getType() == IqPacket.TYPE.ERROR ? packet.findChild("error") : null;
            if (firstAttempt && error != null && error.hasChild("precondition-not-met", Namespace.PUBSUB_ERROR)) {
                Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": precondition wasn't met for device list. pushing node configuration");
                mXmppConnectionService.pushNodeConfiguration(account, AxolotlService.PEP_DEVICE_LIST, publishOptions, new XmppConnectionService.OnConfigurationPushed() {

                    @Override
                    public void onPushSucceeded() {
                        publishDeviceIdsAndRefineAccessModel(ids, false);
                    }

                    @Override
                    public void onPushFailed() {
                        publishDeviceIdsAndRefineAccessModel(ids, false);
                    }
                });
            } else {
                if (AxolotlService.this.changeAccessMode.compareAndSet(true, false)) {
                    Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": done changing access mode");
                    account.setOption(Account.OPTION_REQUIRES_ACCESS_MODE_CHANGE, false);
                    mXmppConnectionService.databaseBackend.updateAccount(account);
                }
                if (packet.getType() == IqPacket.TYPE.ERROR) {
                    pepBroken = true;
                    Log.d(Config.LOGTAG, getLogprefix(account) + "Error received while publishing own device id" + packet.findChild("error"));
                }
            }
        }
    });
}
Also used : Account(de.pixart.messenger.entities.Account) OnIqPacketReceived(de.pixart.messenger.xmpp.OnIqPacketReceived) Bundle(android.os.Bundle) PreKeyBundle(org.whispersystems.libsignal.state.PreKeyBundle) Element(de.pixart.messenger.xml.Element) IqPacket(de.pixart.messenger.xmpp.stanzas.IqPacket)

Example 49 with Account

use of de.pixart.messenger.entities.Account in project Pix-Art-Messenger by kriztan.

the class AxolotlService method publishDeviceVerificationAndBundle.

public void publishDeviceVerificationAndBundle(final SignedPreKeyRecord signedPreKeyRecord, final Set<PreKeyRecord> preKeyRecords, final boolean announceAfter, final boolean wipe) {
    try {
        IdentityKey axolotlPublicKey = axolotlStore.getIdentityKeyPair().getPublicKey();
        PrivateKey x509PrivateKey = KeyChain.getPrivateKey(mXmppConnectionService, account.getPrivateKeyAlias());
        X509Certificate[] chain = KeyChain.getCertificateChain(mXmppConnectionService, account.getPrivateKeyAlias());
        Signature verifier = Signature.getInstance("sha256WithRSA");
        verifier.initSign(x509PrivateKey, mXmppConnectionService.getRNG());
        verifier.update(axolotlPublicKey.serialize());
        byte[] signature = verifier.sign();
        IqPacket packet = mXmppConnectionService.getIqGenerator().publishVerification(signature, chain, getOwnDeviceId());
        Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + ": publish verification for device " + getOwnDeviceId());
        mXmppConnectionService.sendIqPacket(account, packet, new OnIqPacketReceived() {

            @Override
            public void onIqPacketReceived(final Account account, IqPacket packet) {
                String node = AxolotlService.PEP_VERIFICATION + ":" + getOwnDeviceId();
                mXmppConnectionService.pushNodeConfiguration(account, node, PublishOptions.openAccess(), new XmppConnectionService.OnConfigurationPushed() {

                    @Override
                    public void onPushSucceeded() {
                        Log.d(Config.LOGTAG, getLogprefix(account) + "configured verification node to be world readable");
                        publishDeviceBundle(signedPreKeyRecord, preKeyRecords, announceAfter, wipe);
                    }

                    @Override
                    public void onPushFailed() {
                        Log.d(Config.LOGTAG, getLogprefix(account) + "unable to set access model on verification node");
                        publishDeviceBundle(signedPreKeyRecord, preKeyRecords, announceAfter, wipe);
                    }
                });
            }
        });
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : Account(de.pixart.messenger.entities.Account) IdentityKey(org.whispersystems.libsignal.IdentityKey) PrivateKey(java.security.PrivateKey) OnIqPacketReceived(de.pixart.messenger.xmpp.OnIqPacketReceived) X509Certificate(java.security.cert.X509Certificate) UntrustedIdentityException(org.whispersystems.libsignal.UntrustedIdentityException) InvalidKeyIdException(org.whispersystems.libsignal.InvalidKeyIdException) InvalidJidException(de.pixart.messenger.xmpp.jid.InvalidJidException) InvalidKeyException(org.whispersystems.libsignal.InvalidKeyException) IqPacket(de.pixart.messenger.xmpp.stanzas.IqPacket) Signature(java.security.Signature)

Example 50 with Account

use of de.pixart.messenger.entities.Account in project Pix-Art-Messenger by kriztan.

the class AxolotlService method verifySessionWithPEP.

private void verifySessionWithPEP(final XmppAxolotlSession session) {
    Log.d(Config.LOGTAG, "trying to verify fresh session (" + session.getRemoteAddress().getName() + ") with pep");
    final SignalProtocolAddress address = session.getRemoteAddress();
    final IdentityKey identityKey = session.getIdentityKey();
    try {
        IqPacket packet = mXmppConnectionService.getIqGenerator().retrieveVerificationForDevice(Jid.fromString(address.getName()), address.getDeviceId());
        mXmppConnectionService.sendIqPacket(account, packet, new OnIqPacketReceived() {

            @Override
            public void onIqPacketReceived(Account account, IqPacket packet) {
                Pair<X509Certificate[], byte[]> verification = mXmppConnectionService.getIqParser().verification(packet);
                if (verification != null) {
                    try {
                        Signature verifier = Signature.getInstance("sha256WithRSA");
                        verifier.initVerify(verification.first[0]);
                        verifier.update(identityKey.serialize());
                        if (verifier.verify(verification.second)) {
                            try {
                                mXmppConnectionService.getMemorizingTrustManager().getNonInteractive().checkClientTrusted(verification.first, "RSA");
                                String fingerprint = session.getFingerprint();
                                Log.d(Config.LOGTAG, "verified session with x.509 signature. fingerprint was: " + fingerprint);
                                setFingerprintTrust(fingerprint, FingerprintStatus.createActiveVerified(true));
                                axolotlStore.setFingerprintCertificate(fingerprint, verification.first[0]);
                                fetchStatusMap.put(address, FetchStatus.SUCCESS_VERIFIED);
                                Bundle information = CryptoHelper.extractCertificateInformation(verification.first[0]);
                                try {
                                    final String cn = information.getString("subject_cn");
                                    final Jid jid = Jid.fromString(address.getName());
                                    Log.d(Config.LOGTAG, "setting common name for " + jid + " to " + cn);
                                    account.getRoster().getContact(jid).setCommonName(cn);
                                } catch (final InvalidJidException ignored) {
                                // ignored
                                }
                                finishBuildingSessionsFromPEP(address);
                                return;
                            } catch (Exception e) {
                                Log.d(Config.LOGTAG, "could not verify certificate");
                            }
                        }
                    } catch (Exception e) {
                        Log.d(Config.LOGTAG, "error during verification " + e.getMessage());
                    }
                } else {
                    Log.d(Config.LOGTAG, "no verification found");
                }
                fetchStatusMap.put(address, FetchStatus.SUCCESS);
                finishBuildingSessionsFromPEP(address);
            }
        });
    } catch (InvalidJidException e) {
        fetchStatusMap.put(address, FetchStatus.SUCCESS);
        finishBuildingSessionsFromPEP(address);
    }
}
Also used : Account(de.pixart.messenger.entities.Account) IdentityKey(org.whispersystems.libsignal.IdentityKey) OnIqPacketReceived(de.pixart.messenger.xmpp.OnIqPacketReceived) Jid(de.pixart.messenger.xmpp.jid.Jid) Bundle(android.os.Bundle) PreKeyBundle(org.whispersystems.libsignal.state.PreKeyBundle) InvalidJidException(de.pixart.messenger.xmpp.jid.InvalidJidException) X509Certificate(java.security.cert.X509Certificate) UntrustedIdentityException(org.whispersystems.libsignal.UntrustedIdentityException) InvalidKeyIdException(org.whispersystems.libsignal.InvalidKeyIdException) InvalidJidException(de.pixart.messenger.xmpp.jid.InvalidJidException) InvalidKeyException(org.whispersystems.libsignal.InvalidKeyException) IqPacket(de.pixart.messenger.xmpp.stanzas.IqPacket) Signature(java.security.Signature) SignalProtocolAddress(org.whispersystems.libsignal.SignalProtocolAddress) IdentityKeyPair(org.whispersystems.libsignal.IdentityKeyPair) Pair(android.util.Pair)

Aggregations

Account (de.pixart.messenger.entities.Account)104 IqPacket (de.pixart.messenger.xmpp.stanzas.IqPacket)39 OnIqPacketReceived (de.pixart.messenger.xmpp.OnIqPacketReceived)31 Jid (de.pixart.messenger.xmpp.jid.Jid)26 Element (de.pixart.messenger.xml.Element)23 InvalidJidException (de.pixart.messenger.xmpp.jid.InvalidJidException)19 Conversation (de.pixart.messenger.entities.Conversation)18 ArrayList (java.util.ArrayList)14 Intent (android.content.Intent)12 Contact (de.pixart.messenger.entities.Contact)12 Bookmark (de.pixart.messenger.entities.Bookmark)10 PendingIntent (android.app.PendingIntent)9 Message (de.pixart.messenger.entities.Message)8 Bundle (android.os.Bundle)7 AlertDialog (android.support.v7.app.AlertDialog)7 MucOptions (de.pixart.messenger.entities.MucOptions)7 List (java.util.List)7 SuppressLint (android.annotation.SuppressLint)6 Bitmap (android.graphics.Bitmap)6 View (android.view.View)6