Search in sources :

Example 11 with PasswordException

use of i2p.bote.fileencryption.PasswordException in project i2p.i2p-bote by i2p.

the class DebugSupport method getUndecryptableFiles.

/**
 * Tests all encrypted I2P-Bote files and returns a list containing those that
 * cannot be decrypted.
 * @return A list of problem files, or an empty list if no problems were found
 * @throws PasswordException
 * @throws IOException
 * @throws GeneralSecurityException
 */
public List<File> getUndecryptableFiles() throws PasswordException, IOException, GeneralSecurityException {
    // make sure the password is correct
    byte[] password = passwordHolder.getPassword();
    if (password == null)
        throw new PasswordException();
    File passwordFile = configuration.getPasswordFile();
    boolean correct = FileEncryptionUtil.isPasswordCorrect(password, passwordFile);
    if (!correct)
        throw new PasswordException();
    // make a list of all encrypted files
    List<File> files = new ArrayList<File>();
    files.add(configuration.getIdentitiesFile());
    files.add(configuration.getAddressBookFile());
    File[] emailFolders = new File[] { configuration.getInboxDir(), configuration.getOutboxDir(), configuration.getSentFolderDir(), configuration.getTrashFolderDir() };
    ;
    for (File dir : emailFolders) files.addAll(Arrays.asList(dir.listFiles()));
    for (Iterator<File> iter = files.iterator(); iter.hasNext(); ) {
        File file = iter.next();
        FileInputStream stream = new FileInputStream(file);
        try {
            Util.readBytes(new EncryptedInputStream(stream, password));
            // no PasswordException or other exception occurred, so the file is good
            iter.remove();
        } catch (Exception e) {
            // leave the file in the list and log
            log.debug("Can't decrypt file <" + file.getAbsolutePath() + ">", e);
        } finally {
            if (stream != null)
                stream.close();
        }
    }
    return files;
}
Also used : EncryptedInputStream(i2p.bote.fileencryption.EncryptedInputStream) PasswordException(i2p.bote.fileencryption.PasswordException) ArrayList(java.util.ArrayList) File(java.io.File) FileInputStream(java.io.FileInputStream) PasswordException(i2p.bote.fileencryption.PasswordException) IOException(java.io.IOException) GeneralSecurityException(java.security.GeneralSecurityException)

Example 12 with PasswordException

use of i2p.bote.fileencryption.PasswordException in project i2p.i2p-bote by i2p.

the class Migrator method migrateNonPasswordedDataIfNeeded.

/**
 * Migrates files that are not encrypted, or encrypted with the default password.
 */
public void migrateNonPasswordedDataIfNeeded() {
    if (VersionComparator.comp(lastMigrationVersion, currentVersion) >= 0) {
        log.debug("No plaintext migration needed.");
        return;
    }
    try {
        boolean migrationSucceeded = true;
        if (VersionComparator.comp(lastMigrationVersion, "0.2.6") < 0)
            new MigrateTo026().migrateIfNeeded(configuration);
        if (VersionComparator.comp(lastMigrationVersion, "0.2.7") < 0)
            new MigrateTo027().migrateIfNeeded(configuration);
        if (VersionComparator.comp(lastMigrationVersion, "0.2.8") < 0) {
            PasswordCache defaultPasswordHolder = new PasswordCache(configuration);
            try {
                new MigrateTo028().migrateIfNeeded(configuration, defaultPasswordHolder);
            } catch (PasswordException e) {
                log.debug("Non-default password in use, deferring migration of encrypted files to after password entry");
                migrationSucceeded = false;
            }
        }
        if (migrationSucceeded) {
            log.debug("Migration successful, setting last successful migration to <" + currentVersion + ">.");
            setLastSuccessfulMigration(currentVersion);
        }
    } catch (Exception e) {
        log.error("Error migrating to the latest version.", e);
    }
}
Also used : PasswordCache(i2p.bote.fileencryption.PasswordCache) PasswordException(i2p.bote.fileencryption.PasswordException) PasswordException(i2p.bote.fileencryption.PasswordException) IOException(java.io.IOException)

Example 13 with PasswordException

use of i2p.bote.fileencryption.PasswordException in project i2p.i2p-bote by i2p.

the class NewEmailFragment method extractPerson.

private Person extractPerson(String recipient) {
    if (recipient.equals("Anonymous"))
        return null;
    String recipientName = BoteHelper.extractName(recipient);
    try {
        recipientName = BoteHelper.getName(recipient);
    } catch (PasswordException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (GeneralSecurityException e) {
        e.printStackTrace();
    }
    String recipientAddr = BoteHelper.extractEmailDestination(recipient);
    if (recipientAddr == null) {
        // Assume external address
        recipientAddr = recipient;
        if (recipientName.isEmpty())
            recipientName = recipientAddr;
        return new Person(recipientName, recipientAddr, null, true);
    } else {
        if (// Dest with no name
        recipientName.isEmpty())
            recipientName = recipientAddr.substring(0, 5);
        Bitmap recipientPic = null;
        try {
            recipientPic = BoteHelper.getPictureForDestination(recipientAddr);
        } catch (PasswordException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (GeneralSecurityException e) {
            e.printStackTrace();
        }
        return new Person(recipientName, recipientAddr, recipientPic);
    }
}
Also used : PasswordException(i2p.bote.fileencryption.PasswordException) Bitmap(android.graphics.Bitmap) GeneralSecurityException(java.security.GeneralSecurityException) IOException(java.io.IOException) Person(i2p.bote.android.util.Person)

Example 14 with PasswordException

use of i2p.bote.fileencryption.PasswordException in project i2p.i2p-bote by i2p.

the class ViewEmailActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_view_email);
    // Set the action bar
    Toolbar toolbar = (Toolbar) findViewById(R.id.main_toolbar);
    setSupportActionBar(toolbar);
    // Enable ActionBar app icon to behave as action to go back
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setDisplayShowTitleEnabled(false);
    Intent i = getIntent();
    String folderName = i.getStringExtra(FOLDER_NAME);
    mFolder = BoteHelper.getMailFolder(folderName == null ? "inbox" : folderName);
    mMessageId = i.getStringExtra(MESSAGE_ID);
    // Instantiate the ViewPager and PagerAdapter
    mPager = (ViewPager) findViewById(R.id.pager);
    mPagerAdapter = new ViewEmailPagerAdapter(getSupportFragmentManager());
    mPager.setAdapter(mPagerAdapter);
    mPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {

        @Override
        public void onPageSelected(int position) {
            mMessageId = mPagerAdapter.getMessageId(position);
            // Mark the visible email as not new
            if (mMessageId != null) {
                try {
                    if (!BoteHelper.isOutbox(mFolder))
                        mFolder.setNew(mMessageId, false);
                    mFolder.setRecent(mMessageId, false);
                } catch (PasswordException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (GeneralSecurityException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    });
    // Fire off a Loader to fetch the list of Emails
    getSupportLoaderManager().initLoader(MESSAGE_ID_LIST_LOADER, null, this);
}
Also used : PasswordException(i2p.bote.fileencryption.PasswordException) GeneralSecurityException(java.security.GeneralSecurityException) Intent(android.content.Intent) ViewPager(android.support.v4.view.ViewPager) Toolbar(android.support.v7.widget.Toolbar)

Example 15 with PasswordException

use of i2p.bote.fileencryption.PasswordException in project i2p.i2p-bote by i2p.

the class ViewEmailFragment method displayEmail.

private void displayEmail(Email email, View v) {
    View sigInvalid = v.findViewById(R.id.signature_invalid);
    TextView subject = (TextView) v.findViewById(R.id.email_subject);
    ImageView picture = (ImageView) v.findViewById(R.id.picture);
    TextView sender = (TextView) v.findViewById(R.id.email_sender);
    LinearLayout toRecipients = (LinearLayout) v.findViewById(R.id.email_to);
    TextView sent = (TextView) v.findViewById(R.id.email_sent);
    TextView received = (TextView) v.findViewById(R.id.email_received);
    TextView content = (TextView) v.findViewById(R.id.email_content);
    LinearLayout attachments = (LinearLayout) v.findViewById(R.id.attachments);
    try {
        String fromAddress = email.getOneFromAddress();
        subject.setText(email.getSubject());
        Bitmap pic = BoteHelper.getPictureForAddress(fromAddress);
        if (pic != null)
            picture.setImageBitmap(pic);
        else if (!email.isAnonymous()) {
            ViewGroup.LayoutParams lp = picture.getLayoutParams();
            picture.setImageBitmap(BoteHelper.getIdenticonForAddress(fromAddress, lp.width, lp.height));
        }
        final String senderDisplay = BoteHelper.getDisplayAddress(fromAddress);
        if (!email.isSignatureValid() && !email.isAnonymous()) {
            sigInvalid.setVisibility(View.VISIBLE);
            sigInvalid.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View view) {
                    Toast.makeText(getActivity(), getString(R.string.signature_invalid, senderDisplay), Toast.LENGTH_LONG).show();
                }
            });
        }
        sender.setText(senderDisplay);
        if (email.isAnonymous() && !BoteHelper.isSentEmail(email))
            sender.setTypeface(Typeface.DEFAULT, Typeface.ITALIC);
        Address[] emailToRecipients = email.getToAddresses();
        if (emailToRecipients != null) {
            for (Address recipient : emailToRecipients) {
                TextView tv = new TextView(getActivity());
                tv.setText(BoteHelper.getDisplayAddress(recipient.toString()));
                tv.setTextAppearance(getActivity(), R.style.TextAppearance_AppCompat_Secondary);
                toRecipients.addView(tv);
            }
        }
        Address[] emailCcRecipients = email.getCCAddresses();
        if (emailCcRecipients != null) {
            v.findViewById(R.id.email_cc_row).setVisibility(View.VISIBLE);
            LinearLayout ccRecipients = (LinearLayout) v.findViewById(R.id.email_cc);
            for (Address recipient : emailCcRecipients) {
                TextView tv = new TextView(getActivity());
                tv.setText(BoteHelper.getDisplayAddress(recipient.toString()));
                tv.setTextAppearance(getActivity(), R.style.TextAppearance_AppCompat_Secondary);
                ccRecipients.addView(tv);
            }
        }
        Address[] emailBccRecipients = email.getBCCAddresses();
        if (emailBccRecipients != null) {
            v.findViewById(R.id.email_bcc_row).setVisibility(View.VISIBLE);
            LinearLayout bccRecipients = (LinearLayout) v.findViewById(R.id.email_bcc);
            for (Address recipient : emailBccRecipients) {
                TextView tv = new TextView(getActivity());
                tv.setText(BoteHelper.getDisplayAddress(recipient.toString()));
                tv.setTextAppearance(getActivity(), R.style.TextAppearance_AppCompat_Secondary);
                bccRecipients.addView(tv);
            }
        }
        if (email.getSentDate() != null)
            sent.setText(DateFormat.getInstance().format(email.getSentDate()));
        if (email.getReceivedDate() != null)
            received.setText(DateFormat.getInstance().format(email.getReceivedDate()));
        content.setText(email.getText());
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
            content.setTextIsSelectable(true);
        List<Part> parts = email.getParts();
        for (int partIndex = 0; partIndex < parts.size(); partIndex++) {
            Part part = parts.get(partIndex);
            if (Part.ATTACHMENT.equalsIgnoreCase(part.getDisposition())) {
                final ContentAttachment attachment = new ContentAttachment(getActivity(), part);
                View a = getActivity().getLayoutInflater().inflate(R.layout.listitem_attachment, attachments, false);
                ((TextView) a.findViewById(R.id.filename)).setText(attachment.getFileName());
                ((TextView) a.findViewById(R.id.size)).setText(attachment.getHumanReadableSize());
                final ImageView action = (ImageView) a.findViewById(R.id.attachment_action);
                action.setImageDrawable(new IconicsDrawable(getActivity(), GoogleMaterial.Icon.gmd_more_vert).colorRes(R.color.md_grey_600).sizeDp(24).paddingDp(4));
                action.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View view) {
                        PopupMenu popup = new PopupMenu(getActivity(), action);
                        popup.inflate(R.menu.attachment);
                        popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {

                            @Override
                            public boolean onMenuItemClick(MenuItem menuItem) {
                                switch(menuItem.getItemId()) {
                                    case R.id.save_attachment:
                                        saveAttachment(attachment);
                                        return true;
                                    default:
                                        return false;
                                }
                            }
                        });
                        popup.show();
                    }
                });
                final Intent i = new Intent(Intent.ACTION_VIEW);
                i.setData(AttachmentProvider.getUriForAttachment(mFolderName, mMessageId, partIndex));
                i.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
                a.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View view) {
                        startActivity(i);
                    }
                });
                attachments.addView(a);
            }
        }
        // Prepare fields for replying
        mIsAnonymous = email.isAnonymous();
    } catch (MessagingException e) {
        // TODO Handle
        e.printStackTrace();
    } catch (PasswordException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (GeneralSecurityException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    if (BoteHelper.isOutbox(mFolderName)) {
        ((TextView) v.findViewById(R.id.email_status)).setText(BoteHelper.getEmailStatusText(getActivity(), email, true));
        v.findViewById(R.id.email_status_row).setVisibility(View.VISIBLE);
    }
}
Also used : Address(javax.mail.Address) MessagingException(javax.mail.MessagingException) GeneralSecurityException(java.security.GeneralSecurityException) MenuItem(android.view.MenuItem) Intent(android.content.Intent) IOException(java.io.IOException) ImageView(android.widget.ImageView) View(android.view.View) TextView(android.widget.TextView) ContentAttachment(i2p.bote.android.util.ContentAttachment) PasswordException(i2p.bote.fileencryption.PasswordException) Bitmap(android.graphics.Bitmap) Part(javax.mail.Part) TextView(android.widget.TextView) ImageView(android.widget.ImageView) IconicsDrawable(com.mikepenz.iconics.IconicsDrawable) LinearLayout(android.widget.LinearLayout) PopupMenu(android.support.v7.widget.PopupMenu)

Aggregations

PasswordException (i2p.bote.fileencryption.PasswordException)26 IOException (java.io.IOException)16 GeneralSecurityException (java.security.GeneralSecurityException)15 MessagingException (javax.mail.MessagingException)11 Email (i2p.bote.email.Email)9 Bitmap (android.graphics.Bitmap)4 View (android.view.View)4 ImageView (android.widget.ImageView)4 TextView (android.widget.TextView)4 Person (i2p.bote.android.util.Person)4 Contact (i2p.bote.packet.dht.Contact)4 Part (javax.mail.Part)4 Intent (android.content.Intent)3 File (java.io.File)3 ArrayList (java.util.ArrayList)3 Address (javax.mail.Address)3 InternetAddress (javax.mail.internet.InternetAddress)3 SuppressLint (android.annotation.SuppressLint)2 IconicsDrawable (com.mikepenz.iconics.IconicsDrawable)2 ContentAttachment (i2p.bote.android.util.ContentAttachment)2