Search in sources :

Example 1 with PgpEngine

use of eu.siacs.conversations.crypto.PgpEngine in project Conversations by siacs.

the class XmppConnectionService method attachFileToConversation.

public void attachFileToConversation(final Conversation conversation, final Uri uri, final UiCallback<Message> callback) {
    if (FileBackend.weOwnFile(this, uri)) {
        Log.d(Config.LOGTAG, "trying to attach file that belonged to us");
        callback.error(R.string.security_error_invalid_file_access, null);
        return;
    }
    final Message message;
    if (conversation.getNextEncryption() == Message.ENCRYPTION_PGP) {
        message = new Message(conversation, "", Message.ENCRYPTION_DECRYPTED);
    } else {
        message = new Message(conversation, "", conversation.getNextEncryption());
    }
    message.setCounterpart(conversation.getNextCounterpart());
    message.setType(Message.TYPE_FILE);
    mFileAddingExecutor.execute(new Runnable() {

        private void processAsFile() {
            final String path = getFileBackend().getOriginalPath(uri);
            if (path != null) {
                message.setRelativeFilePath(path);
                getFileBackend().updateFileParams(message);
                if (message.getEncryption() == Message.ENCRYPTION_DECRYPTED) {
                    getPgpEngine().encrypt(message, callback);
                } else {
                    callback.success(message);
                }
            } else {
                try {
                    getFileBackend().copyFileToPrivateStorage(message, uri);
                    getFileBackend().updateFileParams(message);
                    if (message.getEncryption() == Message.ENCRYPTION_DECRYPTED) {
                        final PgpEngine pgpEngine = getPgpEngine();
                        if (pgpEngine != null) {
                            pgpEngine.encrypt(message, callback);
                        } else if (callback != null) {
                            callback.error(R.string.unable_to_connect_to_keychain, null);
                        }
                    } else {
                        callback.success(message);
                    }
                } catch (FileBackend.FileCopyException e) {
                    callback.error(e.getResId(), message);
                }
            }
        }

        private void processAsVideo() throws FileNotFoundException {
            Log.d(Config.LOGTAG, "processing file as video");
            message.setRelativeFilePath(message.getUuid() + ".mp4");
            final DownloadableFile file = getFileBackend().getFile(message);
            file.getParentFile().mkdirs();
            ParcelFileDescriptor parcelFileDescriptor = getContentResolver().openFileDescriptor(uri, "r");
            FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
            final ArrayList<Integer> progressTracker = new ArrayList<>();
            final UiInformableCallback<Message> informableCallback;
            if (callback instanceof UiInformableCallback) {
                informableCallback = (UiInformableCallback<Message>) callback;
            } else {
                informableCallback = null;
            }
            MediaTranscoder.Listener listener = new MediaTranscoder.Listener() {

                @Override
                public void onTranscodeProgress(double progress) {
                    int p = ((int) Math.round(progress * 100) / 20) * 20;
                    if (!progressTracker.contains(p) && p != 100 && p != 0) {
                        progressTracker.add(p);
                        if (informableCallback != null) {
                            informableCallback.inform(getString(R.string.transcoding_video_progress, p));
                        }
                    }
                }

                @Override
                public void onTranscodeCompleted() {
                    if (message.getEncryption() == Message.ENCRYPTION_DECRYPTED) {
                        getPgpEngine().encrypt(message, callback);
                    } else {
                        callback.success(message);
                    }
                }

                @Override
                public void onTranscodeCanceled() {
                    processAsFile();
                }

                @Override
                public void onTranscodeFailed(Exception e) {
                    Log.d(Config.LOGTAG, "video transcoding failed " + e.getMessage());
                    processAsFile();
                }
            };
            MediaTranscoder.getInstance().transcodeVideo(fileDescriptor, file.getAbsolutePath(), MediaFormatStrategyPresets.createAndroid720pStrategy(), listener);
        }

        @Override
        public void run() {
            final String mimeType = MimeUtils.guessMimeTypeFromUri(XmppConnectionService.this, uri);
            if (mimeType != null && mimeType.startsWith("video/") && Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
                try {
                    processAsVideo();
                } catch (Throwable e) {
                    processAsFile();
                }
            } else {
                processAsFile();
            }
        }
    });
}
Also used : OnPhoneContactsLoadedListener(eu.siacs.conversations.utils.OnPhoneContactsLoadedListener) OnBindListener(eu.siacs.conversations.xmpp.OnBindListener) OnRenameListener(eu.siacs.conversations.entities.MucOptions.OnRenameListener) XmppAxolotlMessage(eu.siacs.conversations.crypto.axolotl.XmppAxolotlMessage) Message(eu.siacs.conversations.entities.Message) FileNotFoundException(java.io.FileNotFoundException) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) PgpEngine(eu.siacs.conversations.crypto.PgpEngine) MediaTranscoder(net.ypresto.androidtranscoder.MediaTranscoder) ParcelFileDescriptor(android.os.ParcelFileDescriptor) FileDescriptor(java.io.FileDescriptor) UiInformableCallback(eu.siacs.conversations.ui.UiInformableCallback) OtrException(net.java.otr4j.OtrException) FileNotFoundException(java.io.FileNotFoundException) InvalidJidException(eu.siacs.conversations.xmpp.jid.InvalidJidException) CertificateException(java.security.cert.CertificateException) ParcelFileDescriptor(android.os.ParcelFileDescriptor) DownloadableFile(eu.siacs.conversations.entities.DownloadableFile)

Example 2 with PgpEngine

use of eu.siacs.conversations.crypto.PgpEngine in project Conversations by siacs.

the class PresenceParser method parseContactPresence.

public void parseContactPresence(final PresencePacket packet, final Account account) {
    final PresenceGenerator mPresenceGenerator = mXmppConnectionService.getPresenceGenerator();
    final Jid from = packet.getFrom();
    if (from == null || from.equals(account.getJid())) {
        return;
    }
    final String type = packet.getAttribute("type");
    final Contact contact = account.getRoster().getContact(from);
    if (type == null) {
        final String resource = from.isBareJid() ? "" : from.getResourcepart();
        contact.setPresenceName(packet.findChildContent("nick", "http://jabber.org/protocol/nick"));
        Avatar avatar = Avatar.parsePresence(packet.findChild("x", "vcard-temp:x:update"));
        if (avatar != null && (!contact.isSelf() || account.getAvatar() == null)) {
            avatar.owner = from.toBareJid();
            if (mXmppConnectionService.getFileBackend().isAvatarCached(avatar)) {
                if (avatar.owner.equals(account.getJid().toBareJid())) {
                    account.setAvatar(avatar.getFilename());
                    mXmppConnectionService.databaseBackend.updateAccount(account);
                    mXmppConnectionService.getAvatarService().clear(account);
                    mXmppConnectionService.updateConversationUi();
                    mXmppConnectionService.updateAccountUi();
                } else if (contact.setAvatar(avatar)) {
                    mXmppConnectionService.getAvatarService().clear(contact);
                    mXmppConnectionService.updateConversationUi();
                    mXmppConnectionService.updateRosterUi();
                }
            } else if (mXmppConnectionService.isDataSaverDisabled()) {
                mXmppConnectionService.fetchAvatar(account, avatar);
            }
        }
        int sizeBefore = contact.getPresences().size();
        final String show = packet.findChildContent("show");
        final Element caps = packet.findChild("c", "http://jabber.org/protocol/caps");
        final String message = packet.findChildContent("status");
        final Presence presence = Presence.parse(show, caps, message);
        contact.updatePresence(resource, presence);
        if (presence.hasCaps()) {
            mXmppConnectionService.fetchCaps(account, from, presence);
        }
        final Element idle = packet.findChild("idle", "urn:xmpp:idle:1");
        if (idle != null) {
            contact.flagInactive();
            String since = idle.getAttribute("since");
            try {
                contact.setLastseen(AbstractParser.parseTimestamp(since));
            } catch (NullPointerException | ParseException e) {
                contact.setLastseen(System.currentTimeMillis());
            }
        } else {
            contact.flagActive();
            contact.setLastseen(AbstractParser.parseTimestamp(packet));
        }
        PgpEngine pgp = mXmppConnectionService.getPgpEngine();
        Element x = packet.findChild("x", "jabber:x:signed");
        if (pgp != null && x != null) {
            Element status = packet.findChild("status");
            String msg = status != null ? status.getContent() : "";
            contact.setPgpKeyId(pgp.fetchKeyId(account, msg, x.getContent()));
        }
        boolean online = sizeBefore < contact.getPresences().size();
        mXmppConnectionService.onContactStatusChanged.onContactStatusChanged(contact, online);
    } else if (type.equals("unavailable")) {
        if (from.isBareJid()) {
            contact.clearPresences();
        } else {
            contact.removePresence(from.getResourcepart());
        }
        mXmppConnectionService.onContactStatusChanged.onContactStatusChanged(contact, false);
    } else if (type.equals("subscribe")) {
        if (contact.getOption(Contact.Options.PREEMPTIVE_GRANT)) {
            mXmppConnectionService.sendPresencePacket(account, mPresenceGenerator.sendPresenceUpdatesTo(contact));
        } else {
            contact.setOption(Contact.Options.PENDING_SUBSCRIPTION_REQUEST);
            final Conversation conversation = mXmppConnectionService.findOrCreateConversation(account, contact.getJid().toBareJid(), false);
            final String statusMessage = packet.findChildContent("status");
            if (statusMessage != null && !statusMessage.isEmpty() && conversation.countMessages() == 0) {
                conversation.add(new Message(conversation, statusMessage, Message.ENCRYPTION_NONE, Message.STATUS_RECEIVED));
            }
        }
    }
    mXmppConnectionService.updateRosterUi();
}
Also used : PresenceGenerator(eu.siacs.conversations.generator.PresenceGenerator) Jid(eu.siacs.conversations.xmpp.jid.Jid) Message(eu.siacs.conversations.entities.Message) Element(eu.siacs.conversations.xml.Element) PgpEngine(eu.siacs.conversations.crypto.PgpEngine) Conversation(eu.siacs.conversations.entities.Conversation) Avatar(eu.siacs.conversations.xmpp.pep.Avatar) Contact(eu.siacs.conversations.entities.Contact) Presence(eu.siacs.conversations.entities.Presence) ParseException(java.text.ParseException)

Example 3 with PgpEngine

use of eu.siacs.conversations.crypto.PgpEngine in project Conversations by siacs.

the class ContactDetailsActivity method populateView.

private void populateView() {
    if (contact == null) {
        return;
    }
    invalidateOptionsMenu();
    setTitle(contact.getDisplayName());
    if (contact.showInRoster()) {
        send.setVisibility(View.VISIBLE);
        receive.setVisibility(View.VISIBLE);
        addContactButton.setVisibility(View.GONE);
        send.setOnCheckedChangeListener(null);
        receive.setOnCheckedChangeListener(null);
        List<String> statusMessages = contact.getPresences().getStatusMessages();
        if (statusMessages.size() == 0) {
            statusMessage.setVisibility(View.GONE);
        } else {
            StringBuilder builder = new StringBuilder();
            statusMessage.setVisibility(View.VISIBLE);
            int s = statusMessages.size();
            for (int i = 0; i < s; ++i) {
                if (s > 1) {
                    builder.append("• ");
                }
                builder.append(statusMessages.get(i));
                if (i < s - 1) {
                    builder.append("\n");
                }
            }
            statusMessage.setText(builder);
        }
        if (contact.getOption(Contact.Options.FROM)) {
            send.setText(R.string.send_presence_updates);
            send.setChecked(true);
        } else if (contact.getOption(Contact.Options.PENDING_SUBSCRIPTION_REQUEST)) {
            send.setChecked(false);
            send.setText(R.string.send_presence_updates);
        } else {
            send.setText(R.string.preemptively_grant);
            if (contact.getOption(Contact.Options.PREEMPTIVE_GRANT)) {
                send.setChecked(true);
            } else {
                send.setChecked(false);
            }
        }
        if (contact.getOption(Contact.Options.TO)) {
            receive.setText(R.string.receive_presence_updates);
            receive.setChecked(true);
        } else {
            receive.setText(R.string.ask_for_presence_updates);
            if (contact.getOption(Contact.Options.ASKING)) {
                receive.setChecked(true);
            } else {
                receive.setChecked(false);
            }
        }
        if (contact.getAccount().isOnlineAndConnected()) {
            receive.setEnabled(true);
            send.setEnabled(true);
        } else {
            receive.setEnabled(false);
            send.setEnabled(false);
        }
        send.setOnCheckedChangeListener(this.mOnSendCheckedChange);
        receive.setOnCheckedChangeListener(this.mOnReceiveCheckedChange);
    } else {
        addContactButton.setVisibility(View.VISIBLE);
        send.setVisibility(View.GONE);
        receive.setVisibility(View.GONE);
        statusMessage.setVisibility(View.GONE);
    }
    if (contact.isBlocked() && !this.showDynamicTags) {
        lastseen.setVisibility(View.VISIBLE);
        lastseen.setText(R.string.contact_blocked);
    } else {
        if (showLastSeen && contact.getLastseen() > 0) {
            lastseen.setVisibility(View.VISIBLE);
            lastseen.setText(UIHelper.lastseen(getApplicationContext(), contact.isActive(), contact.getLastseen()));
        } else {
            lastseen.setVisibility(View.GONE);
        }
    }
    if (contact.getPresences().size() > 1) {
        contactJidTv.setText(contact.getDisplayJid() + " (" + contact.getPresences().size() + ")");
    } else {
        contactJidTv.setText(contact.getDisplayJid());
    }
    String account;
    if (Config.DOMAIN_LOCK != null) {
        account = contact.getAccount().getJid().getLocalpart();
    } else {
        account = contact.getAccount().getJid().toBareJid().toString();
    }
    accountJidTv.setText(getString(R.string.using_account, account));
    badge.setImageBitmap(avatarService().get(contact, getPixel(72)));
    badge.setOnClickListener(this.onBadgeClick);
    keys.removeAllViews();
    boolean hasKeys = false;
    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if (Config.supportOtr()) {
        for (final String otrFingerprint : contact.getOtrFingerprints()) {
            hasKeys = true;
            View view = inflater.inflate(R.layout.contact_key, keys, false);
            TextView key = (TextView) view.findViewById(R.id.key);
            TextView keyType = (TextView) view.findViewById(R.id.key_type);
            ImageButton removeButton = (ImageButton) view.findViewById(R.id.button_remove);
            removeButton.setVisibility(View.VISIBLE);
            key.setText(CryptoHelper.prettifyFingerprint(otrFingerprint));
            if (otrFingerprint != null && otrFingerprint.equals(messageFingerprint)) {
                keyType.setText(R.string.otr_fingerprint_selected_message);
                keyType.setTextColor(getResources().getColor(R.color.accent));
            } else {
                keyType.setText(R.string.otr_fingerprint);
            }
            keys.addView(view);
            removeButton.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    confirmToDeleteFingerprint(otrFingerprint);
                }
            });
        }
    }
    if (Config.supportOmemo()) {
        boolean skippedInactive = false;
        boolean showsInactive = false;
        for (final XmppAxolotlSession session : contact.getAccount().getAxolotlService().findSessionsForContact(contact)) {
            final FingerprintStatus trust = session.getTrust();
            hasKeys |= !trust.isCompromised();
            if (!trust.isActive()) {
                if (showInactiveOmemo) {
                    showsInactive = true;
                } else {
                    skippedInactive = true;
                    continue;
                }
            }
            if (!trust.isCompromised()) {
                boolean highlight = session.getFingerprint().equals(messageFingerprint);
                addFingerprintRow(keys, session, highlight);
            }
        }
        if (showsInactive || skippedInactive) {
            mShowInactiveDevicesButton.setText(showsInactive ? R.string.hide_inactive_devices : R.string.show_inactive_devices);
            mShowInactiveDevicesButton.setVisibility(View.VISIBLE);
        } else {
            mShowInactiveDevicesButton.setVisibility(View.GONE);
        }
    } else {
        mShowInactiveDevicesButton.setVisibility(View.GONE);
    }
    if (Config.supportOpenPgp() && contact.getPgpKeyId() != 0) {
        hasKeys = true;
        View view = inflater.inflate(R.layout.contact_key, keys, false);
        TextView key = (TextView) view.findViewById(R.id.key);
        TextView keyType = (TextView) view.findViewById(R.id.key_type);
        keyType.setText(R.string.openpgp_key_id);
        if ("pgp".equals(messageFingerprint)) {
            keyType.setTextColor(getResources().getColor(R.color.accent));
        }
        key.setText(OpenPgpUtils.convertKeyIdToHex(contact.getPgpKeyId()));
        view.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                PgpEngine pgp = ContactDetailsActivity.this.xmppConnectionService.getPgpEngine();
                if (pgp != null) {
                    PendingIntent intent = pgp.getIntentForKey(contact);
                    if (intent != null) {
                        try {
                            startIntentSenderForResult(intent.getIntentSender(), 0, null, 0, 0, 0);
                        } catch (SendIntentException e) {
                        }
                    }
                }
            }
        });
        keys.addView(view);
    }
    keysWrapper.setVisibility(hasKeys ? View.VISIBLE : View.GONE);
    List<ListItem.Tag> tagList = contact.getTags(this);
    if (tagList.size() == 0 || !this.showDynamicTags) {
        tags.setVisibility(View.GONE);
    } else {
        tags.setVisibility(View.VISIBLE);
        tags.removeAllViewsInLayout();
        for (final ListItem.Tag tag : tagList) {
            final TextView tv = (TextView) inflater.inflate(R.layout.list_item_tag, tags, false);
            tv.setText(tag.getName());
            tv.setBackgroundColor(tag.getColor());
            tags.addView(tv);
        }
    }
}
Also used : FingerprintStatus(eu.siacs.conversations.crypto.axolotl.FingerprintStatus) PgpEngine(eu.siacs.conversations.crypto.PgpEngine) View(android.view.View) TextView(android.widget.TextView) SendIntentException(android.content.IntentSender.SendIntentException) XmppAxolotlSession(eu.siacs.conversations.crypto.axolotl.XmppAxolotlSession) ImageButton(android.widget.ImageButton) LayoutInflater(android.view.LayoutInflater) OnClickListener(android.view.View.OnClickListener) TextView(android.widget.TextView) PendingIntent(android.app.PendingIntent) ListItem(eu.siacs.conversations.entities.ListItem)

Example 4 with PgpEngine

use of eu.siacs.conversations.crypto.PgpEngine in project Conversations by siacs.

the class XmppConnectionService method attachImageToConversation.

public void attachImageToConversation(final Conversation conversation, final Uri uri, final UiCallback<Message> callback) {
    if (FileBackend.weOwnFile(this, uri)) {
        Log.d(Config.LOGTAG, "trying to attach file that belonged to us");
        callback.error(R.string.security_error_invalid_file_access, null);
        return;
    }
    final String mimeType = MimeUtils.guessMimeTypeFromUri(this, uri);
    final String compressPictures = getCompressPicturesPreference();
    if ("never".equals(compressPictures) || ("auto".equals(compressPictures) && getFileBackend().useImageAsIs(uri)) || (mimeType != null && mimeType.endsWith("/gif"))) {
        Log.d(Config.LOGTAG, conversation.getAccount().getJid().toBareJid() + ": not compressing picture. sending as file");
        attachFileToConversation(conversation, uri, callback);
        return;
    }
    final Message message;
    if (conversation.getNextEncryption() == Message.ENCRYPTION_PGP) {
        message = new Message(conversation, "", Message.ENCRYPTION_DECRYPTED);
    } else {
        message = new Message(conversation, "", conversation.getNextEncryption());
    }
    message.setCounterpart(conversation.getNextCounterpart());
    message.setType(Message.TYPE_IMAGE);
    mFileAddingExecutor.execute(new Runnable() {

        @Override
        public void run() {
            try {
                getFileBackend().copyImageToPrivateStorage(message, uri);
                if (conversation.getNextEncryption() == Message.ENCRYPTION_PGP) {
                    final PgpEngine pgpEngine = getPgpEngine();
                    if (pgpEngine != null) {
                        pgpEngine.encrypt(message, callback);
                    } else if (callback != null) {
                        callback.error(R.string.unable_to_connect_to_keychain, null);
                    }
                } else {
                    callback.success(message);
                }
            } catch (final FileBackend.FileCopyException e) {
                callback.error(e.getResId(), message);
            }
        }
    });
}
Also used : XmppAxolotlMessage(eu.siacs.conversations.crypto.axolotl.XmppAxolotlMessage) Message(eu.siacs.conversations.entities.Message) PgpEngine(eu.siacs.conversations.crypto.PgpEngine)

Aggregations

PgpEngine (eu.siacs.conversations.crypto.PgpEngine)4 Message (eu.siacs.conversations.entities.Message)3 XmppAxolotlMessage (eu.siacs.conversations.crypto.axolotl.XmppAxolotlMessage)2 PendingIntent (android.app.PendingIntent)1 SendIntentException (android.content.IntentSender.SendIntentException)1 ParcelFileDescriptor (android.os.ParcelFileDescriptor)1 LayoutInflater (android.view.LayoutInflater)1 View (android.view.View)1 OnClickListener (android.view.View.OnClickListener)1 ImageButton (android.widget.ImageButton)1 TextView (android.widget.TextView)1 FingerprintStatus (eu.siacs.conversations.crypto.axolotl.FingerprintStatus)1 XmppAxolotlSession (eu.siacs.conversations.crypto.axolotl.XmppAxolotlSession)1 Contact (eu.siacs.conversations.entities.Contact)1 Conversation (eu.siacs.conversations.entities.Conversation)1 DownloadableFile (eu.siacs.conversations.entities.DownloadableFile)1 ListItem (eu.siacs.conversations.entities.ListItem)1 OnRenameListener (eu.siacs.conversations.entities.MucOptions.OnRenameListener)1 Presence (eu.siacs.conversations.entities.Presence)1 PresenceGenerator (eu.siacs.conversations.generator.PresenceGenerator)1