Search in sources :

Example 1 with EncryptedInputStream

use of i2p.bote.fileencryption.EncryptedInputStream 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 2 with EncryptedInputStream

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

the class Identities method readIdentities.

/**
 * Reads <code>Identities</code> from the encrypted identities file. Each identity
 * is defined by one line that contains two to four tab-separated fields:<br/>
 * Email Identity key, Public Name, Description, and Email Address.
 * The first two are mandatory, the last two are optional.<br/>
 * <p/>
 * Additionally, the file can set a default Email Identity by including a
 * line that starts with "Default ", followed by an Email Destination key.<br/>
 * The destination key must match one of the Email Identities defined in
 * the file.
 * <p/>
 * An Email Identity key consists of two public keys and two private keys, whereas
 * an Email Destination consists only of two public keys.
 * @throws PasswordException
 * @throws IOException
 * @throws GeneralSecurityException
 */
private void readIdentities() throws PasswordException, IOException, GeneralSecurityException {
    log.debug("Reading identities file: <" + identitiesFile.getAbsolutePath() + ">");
    if (!identitiesFile.exists()) {
        log.debug("Identities file does not exist: <" + identitiesFile.getAbsolutePath() + ">");
        identities = new TreeSet<EmailIdentity>(new IdentityComparator());
        return;
    }
    BufferedReader input = null;
    try {
        InputStream encryptedStream = new EncryptedInputStream(new FileInputStream(identitiesFile), passwordHolder);
        input = new BufferedReader(new InputStreamReader(encryptedStream));
        // No PasswordException occurred, so read the input stream
        Properties properties = new Properties();
        properties.load(new InputStreamReader(encryptedStream));
        loadFromProperties(properties, false, false);
    } finally {
        if (input != null)
            try {
                input.close();
            } catch (IOException e) {
                log.error("Error closing input stream.", e);
            }
    }
}
Also used : EncryptedInputStream(i2p.bote.fileencryption.EncryptedInputStream) InputStreamReader(java.io.InputStreamReader) FileInputStream(java.io.FileInputStream) EncryptedInputStream(i2p.bote.fileencryption.EncryptedInputStream) InputStream(java.io.InputStream) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) Properties(java.util.Properties) SortedProperties(i2p.bote.util.SortedProperties) FileInputStream(java.io.FileInputStream)

Example 3 with EncryptedInputStream

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

the class MigrateTo028 method migrateAddressBookIfNeeded.

private void migrateAddressBookIfNeeded(Configuration configuration, PasswordHolder passwordHolder) throws FileNotFoundException, IOException, GeneralSecurityException, PasswordException {
    File addressBookFile = configuration.getAddressBookFile();
    if (!addressBookFile.exists())
        return;
    BufferedReader input = null;
    try {
        InputStream encryptedStream = new EncryptedInputStream(new FileInputStream(addressBookFile), passwordHolder);
        input = new BufferedReader(new InputStreamReader(encryptedStream));
        // No PasswordException occurred, so read the input stream and call migrateAddressBook() if needed
        List<String> lines = Util.readLines(encryptedStream);
        if (!isAddressBookFileMigrated(lines))
            migrateAddressBook(lines, configuration, passwordHolder);
    } finally {
        if (input != null)
            input.close();
    }
}
Also used : EncryptedInputStream(i2p.bote.fileencryption.EncryptedInputStream) InputStreamReader(java.io.InputStreamReader) FileInputStream(java.io.FileInputStream) EncryptedInputStream(i2p.bote.fileencryption.EncryptedInputStream) InputStream(java.io.InputStream) BufferedReader(java.io.BufferedReader) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 4 with EncryptedInputStream

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

the class AddressBook method readContacts.

/**
 * Reads an <code>AddressBook</code> from the encrypted address book file.
 * Each contact is defined by one line that contains an Email Destination
 * and a name, separated by a tab character.
 * @throws PasswordException
 */
private void readContacts() throws PasswordException {
    if (!addressFile.exists()) {
        log.debug("Address file does not exist: <" + addressFile.getAbsolutePath() + ">");
        contacts = new TreeSet<Contact>(new ContactComparator());
        return;
    }
    log.debug("Reading address book from <" + addressFile.getAbsolutePath() + ">");
    BufferedReader input = null;
    try {
        InputStream encryptedStream = new EncryptedInputStream(new FileInputStream(addressFile), passwordHolder);
        input = new BufferedReader(new InputStreamReader(encryptedStream));
        // No PasswordException occurred, so parse the input stream
        Properties properties = new Properties();
        properties.load(new InputStreamReader(encryptedStream));
        loadFromProperties(properties, false, false);
    } catch (PasswordException e) {
        throw e;
    } catch (Exception e) {
        log.error("Can't read address book.", e);
    } finally {
        if (input != null)
            try {
                input.close();
            } catch (IOException e) {
                log.error("Error closing input stream.", e);
            }
    }
}
Also used : EncryptedInputStream(i2p.bote.fileencryption.EncryptedInputStream) PasswordException(i2p.bote.fileencryption.PasswordException) InputStreamReader(java.io.InputStreamReader) FileInputStream(java.io.FileInputStream) EncryptedInputStream(i2p.bote.fileencryption.EncryptedInputStream) InputStream(java.io.InputStream) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) Properties(java.util.Properties) SortedProperties(i2p.bote.util.SortedProperties) FileInputStream(java.io.FileInputStream) PasswordException(i2p.bote.fileencryption.PasswordException) GeneralSecurityException(java.security.GeneralSecurityException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) Contact(i2p.bote.packet.dht.Contact)

Example 5 with EncryptedInputStream

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

the class EmailFolder method createFolderElement.

@Override
protected Email createFolderElement(File emailFile) throws Exception {
    InputStream emailStream = null;
    try {
        emailStream = new BufferedInputStream(new EncryptedInputStream(new FileInputStream(emailFile), passwordHolder));
        InputStream metadataStream = null;
        File metadataFile = getMetadataFile(emailFile);
        if (metadataFile.exists())
            metadataStream = new BufferedInputStream(new EncryptedInputStream(new FileInputStream(metadataFile), passwordHolder));
        Email email = new Email(emailStream, metadataStream, passwordHolder);
        String messageIdString = emailFile.getName().substring(0, 44);
        email.setMessageID(messageIdString);
        return email;
    } finally {
        if (emailStream != null)
            emailStream.close();
    }
}
Also used : EncryptedInputStream(i2p.bote.fileencryption.EncryptedInputStream) Email(i2p.bote.email.Email) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) EncryptedInputStream(i2p.bote.fileencryption.EncryptedInputStream) InputStream(java.io.InputStream) File(java.io.File) FileInputStream(java.io.FileInputStream)

Aggregations

EncryptedInputStream (i2p.bote.fileencryption.EncryptedInputStream)8 FileInputStream (java.io.FileInputStream)8 InputStream (java.io.InputStream)7 InputStreamReader (java.io.InputStreamReader)5 BufferedReader (java.io.BufferedReader)4 File (java.io.File)4 IOException (java.io.IOException)3 Properties (java.util.Properties)3 PasswordException (i2p.bote.fileencryption.PasswordException)2 SortedProperties (i2p.bote.util.SortedProperties)2 BufferedInputStream (java.io.BufferedInputStream)2 GeneralSecurityException (java.security.GeneralSecurityException)2 Email (i2p.bote.email.Email)1 EmailMetadata (i2p.bote.email.EmailMetadata)1 Contact (i2p.bote.packet.dht.Contact)1 FileNotFoundException (java.io.FileNotFoundException)1 ArrayList (java.util.ArrayList)1