Search in sources :

Example 81 with Account

use of eu.siacs.conversations.entities.Account in project Conversations by siacs.

the class XmppConnectionService method createAccountFromKey.

public void createAccountFromKey(final String alias, final OnAccountCreated callback) {
    new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                X509Certificate[] chain = KeyChain.getCertificateChain(XmppConnectionService.this, alias);
                Pair<Jid, String> info = CryptoHelper.extractJidAndName(chain[0]);
                if (findAccountByJid(info.first) == null) {
                    Account account = new Account(info.first, "");
                    account.setPrivateKeyAlias(alias);
                    account.setOption(Account.OPTION_DISABLED, true);
                    account.setDisplayName(info.second);
                    createAccount(account);
                    callback.onAccountCreated(account);
                    if (Config.X509_VERIFICATION) {
                        try {
                            getMemorizingTrustManager().getNonInteractive(account.getJid().getDomainpart()).checkClientTrusted(chain, "RSA");
                        } catch (CertificateException e) {
                            callback.informUser(R.string.certificate_chain_is_not_trusted);
                        }
                    }
                } else {
                    callback.informUser(R.string.account_already_exists);
                }
            } catch (Exception e) {
                e.printStackTrace();
                callback.informUser(R.string.unable_to_parse_certificate);
            }
        }
    }).start();
}
Also used : Account(eu.siacs.conversations.entities.Account) Jid(eu.siacs.conversations.xmpp.jid.Jid) CertificateException(java.security.cert.CertificateException) X509Certificate(java.security.cert.X509Certificate) OtrException(net.java.otr4j.OtrException) FileNotFoundException(java.io.FileNotFoundException) InvalidJidException(eu.siacs.conversations.xmpp.jid.InvalidJidException) CertificateException(java.security.cert.CertificateException)

Example 82 with Account

use of eu.siacs.conversations.entities.Account in project Conversations by siacs.

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 AxolotlAddress 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(eu.siacs.conversations.entities.Account) IdentityKey(org.whispersystems.libaxolotl.IdentityKey) OnIqPacketReceived(eu.siacs.conversations.xmpp.OnIqPacketReceived) Jid(eu.siacs.conversations.xmpp.jid.Jid) Bundle(android.os.Bundle) PreKeyBundle(org.whispersystems.libaxolotl.state.PreKeyBundle) AxolotlAddress(org.whispersystems.libaxolotl.AxolotlAddress) InvalidJidException(eu.siacs.conversations.xmpp.jid.InvalidJidException) X509Certificate(java.security.cert.X509Certificate) InvalidJidException(eu.siacs.conversations.xmpp.jid.InvalidJidException) InvalidKeyException(org.whispersystems.libaxolotl.InvalidKeyException) InvalidKeyIdException(org.whispersystems.libaxolotl.InvalidKeyIdException) UntrustedIdentityException(org.whispersystems.libaxolotl.UntrustedIdentityException) IqPacket(eu.siacs.conversations.xmpp.stanzas.IqPacket) Signature(java.security.Signature) Pair(android.util.Pair) IdentityKeyPair(org.whispersystems.libaxolotl.IdentityKeyPair)

Example 83 with Account

use of eu.siacs.conversations.entities.Account in project Conversations by siacs.

the class MessageGenerator method generateChatState.

public MessagePacket generateChatState(Conversation conversation) {
    final Account account = conversation.getAccount();
    MessagePacket packet = new MessagePacket();
    packet.setType(conversation.getMode() == Conversation.MODE_MULTI ? MessagePacket.TYPE_GROUPCHAT : MessagePacket.TYPE_CHAT);
    packet.setTo(conversation.getJid().toBareJid());
    packet.setFrom(account.getJid());
    packet.addChild(ChatState.toElement(conversation.getOutgoingChatState()));
    packet.addChild("no-store", "urn:xmpp:hints");
    //wrong! don't copy this. Its *store*
    packet.addChild("no-storage", "urn:xmpp:hints");
    return packet;
}
Also used : MessagePacket(eu.siacs.conversations.xmpp.stanzas.MessagePacket) Account(eu.siacs.conversations.entities.Account)

Example 84 with Account

use of eu.siacs.conversations.entities.Account in project Conversations by siacs.

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();
                Bundle pubsubOptions = new Bundle();
                pubsubOptions.putString("pubsub#access_model", "open");
                mXmppConnectionService.pushNodeConfiguration(account, account.getJid().toBareJid(), node, pubsubOptions, 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(eu.siacs.conversations.entities.Account) IdentityKey(org.whispersystems.libaxolotl.IdentityKey) PrivateKey(java.security.PrivateKey) OnIqPacketReceived(eu.siacs.conversations.xmpp.OnIqPacketReceived) Bundle(android.os.Bundle) PreKeyBundle(org.whispersystems.libaxolotl.state.PreKeyBundle) X509Certificate(java.security.cert.X509Certificate) InvalidJidException(eu.siacs.conversations.xmpp.jid.InvalidJidException) InvalidKeyException(org.whispersystems.libaxolotl.InvalidKeyException) InvalidKeyIdException(org.whispersystems.libaxolotl.InvalidKeyIdException) UntrustedIdentityException(org.whispersystems.libaxolotl.UntrustedIdentityException) IqPacket(eu.siacs.conversations.xmpp.stanzas.IqPacket) Signature(java.security.Signature)

Example 85 with Account

use of eu.siacs.conversations.entities.Account in project Conversations by siacs.

the class AxolotlService method publishDeviceBundle.

private void publishDeviceBundle(SignedPreKeyRecord signedPreKeyRecord, Set<PreKeyRecord> preKeyRecords, final boolean announceAfter, final boolean wipe) {
    IqPacket publish = mXmppConnectionService.getIqGenerator().publishBundles(signedPreKeyRecord, axolotlStore.getIdentityKeyPair().getPublicKey(), preKeyRecords, getOwnDeviceId());
    Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + ": Bundle " + getOwnDeviceId() + " in PEP not current. Publishing...");
    mXmppConnectionService.sendIqPacket(account, publish, new OnIqPacketReceived() {

        @Override
        public void onIqPacketReceived(Account account, IqPacket packet) {
            if (packet.getType() == IqPacket.TYPE.RESULT) {
                Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Successfully published bundle. ");
                if (wipe) {
                    wipeOtherPepDevices();
                } else if (announceAfter) {
                    Log.d(Config.LOGTAG, getLogprefix(account) + "Announcing device " + getOwnDeviceId());
                    publishOwnDeviceIdIfNeeded();
                }
            } else if (packet.getType() == IqPacket.TYPE.ERROR) {
                pepBroken = true;
                Log.d(Config.LOGTAG, getLogprefix(account) + "Error received while publishing bundle: " + packet.findChild("error"));
            }
        }
    });
}
Also used : Account(eu.siacs.conversations.entities.Account) OnIqPacketReceived(eu.siacs.conversations.xmpp.OnIqPacketReceived) IqPacket(eu.siacs.conversations.xmpp.stanzas.IqPacket)

Aggregations

Account (eu.siacs.conversations.entities.Account)100 IqPacket (eu.siacs.conversations.xmpp.stanzas.IqPacket)41 OnIqPacketReceived (eu.siacs.conversations.xmpp.OnIqPacketReceived)33 Jid (eu.siacs.conversations.xmpp.jid.Jid)22 Element (eu.siacs.conversations.xml.Element)21 InvalidJidException (eu.siacs.conversations.xmpp.jid.InvalidJidException)17 Conversation (eu.siacs.conversations.entities.Conversation)16 Contact (eu.siacs.conversations.entities.Contact)9 Message (eu.siacs.conversations.entities.Message)9 ArrayList (java.util.ArrayList)8 PendingIntent (android.app.PendingIntent)7 Intent (android.content.Intent)7 Bookmark (eu.siacs.conversations.entities.Bookmark)7 SuppressLint (android.annotation.SuppressLint)6 AlertDialog (android.app.AlertDialog)6 TextView (android.widget.TextView)6 MessagePacket (eu.siacs.conversations.xmpp.stanzas.MessagePacket)6 FileNotFoundException (java.io.FileNotFoundException)6 DialogInterface (android.content.DialogInterface)5 View (android.view.View)5