Search in sources :

Example 1 with CipherInputStream

use of org.bouncycastle.crypto.io.CipherInputStream in project Conversations by siacs.

the class AbstractConnectionManager method upgrade.

public static InputStream upgrade(DownloadableFile file, InputStream is) {
    if (file.getKey() != null && file.getIv() != null) {
        AEADBlockCipher cipher = new GCMBlockCipher(new AESEngine());
        cipher.init(true, new AEADParameters(new KeyParameter(file.getKey()), 128, file.getIv()));
        return new CipherInputStream(is, cipher);
    } else {
        return is;
    }
}
Also used : AESEngine(org.bouncycastle.crypto.engines.AESEngine) AEADParameters(org.bouncycastle.crypto.params.AEADParameters) CipherInputStream(org.bouncycastle.crypto.io.CipherInputStream) KeyParameter(org.bouncycastle.crypto.params.KeyParameter) GCMBlockCipher(org.bouncycastle.crypto.modes.GCMBlockCipher) AEADBlockCipher(org.bouncycastle.crypto.modes.AEADBlockCipher)

Example 2 with CipherInputStream

use of org.bouncycastle.crypto.io.CipherInputStream in project Conversations by siacs.

the class ImportBackupService method importBackup.

private boolean importBackup(final Uri uri, final String password) {
    Log.d(Config.LOGTAG, "importing backup from " + uri);
    final Stopwatch stopwatch = Stopwatch.createStarted();
    try {
        final SQLiteDatabase db = mDatabaseBackend.getWritableDatabase();
        final InputStream inputStream;
        final String path = uri.getPath();
        final long fileSize;
        if ("file".equals(uri.getScheme()) && path != null) {
            final File file = new File(path);
            inputStream = new FileInputStream(file);
            fileSize = file.length();
        } else {
            final Cursor returnCursor = getContentResolver().query(uri, null, null, null, null);
            if (returnCursor == null) {
                fileSize = 0;
            } else {
                returnCursor.moveToFirst();
                fileSize = returnCursor.getLong(returnCursor.getColumnIndex(OpenableColumns.SIZE));
                returnCursor.close();
            }
            inputStream = getContentResolver().openInputStream(uri);
        }
        if (inputStream == null) {
            synchronized (mOnBackupProcessedListeners) {
                for (final OnBackupProcessed l : mOnBackupProcessedListeners) {
                    l.onBackupRestoreFailed();
                }
            }
            return false;
        }
        final CountingInputStream countingInputStream = new CountingInputStream(inputStream);
        final DataInputStream dataInputStream = new DataInputStream(countingInputStream);
        final BackupFileHeader backupFileHeader = BackupFileHeader.read(dataInputStream);
        Log.d(Config.LOGTAG, backupFileHeader.toString());
        if (mDatabaseBackend.getAccountJids(false).contains(backupFileHeader.getJid())) {
            synchronized (mOnBackupProcessedListeners) {
                for (OnBackupProcessed l : mOnBackupProcessedListeners) {
                    l.onAccountAlreadySetup();
                }
            }
            return false;
        }
        final byte[] key = ExportBackupService.getKey(password, backupFileHeader.getSalt());
        final AEADBlockCipher cipher = new GCMBlockCipher(new AESEngine());
        cipher.init(false, new AEADParameters(new KeyParameter(key), 128, backupFileHeader.getIv()));
        final CipherInputStream cipherInputStream = new CipherInputStream(countingInputStream, cipher);
        final GZIPInputStream gzipInputStream = new GZIPInputStream(cipherInputStream);
        final BufferedReader reader = new BufferedReader(new InputStreamReader(gzipInputStream, Charsets.UTF_8));
        db.beginTransaction();
        String line;
        StringBuilder multiLineQuery = null;
        while ((line = reader.readLine()) != null) {
            int count = count(line, '\'');
            if (multiLineQuery != null) {
                multiLineQuery.append('\n');
                multiLineQuery.append(line);
                if (count % 2 == 1) {
                    db.execSQL(multiLineQuery.toString());
                    multiLineQuery = null;
                    updateImportBackupNotification(fileSize, countingInputStream.getCount());
                }
            } else {
                if (count % 2 == 0) {
                    db.execSQL(line);
                    updateImportBackupNotification(fileSize, countingInputStream.getCount());
                } else {
                    multiLineQuery = new StringBuilder(line);
                }
            }
        }
        db.setTransactionSuccessful();
        db.endTransaction();
        final Jid jid = backupFileHeader.getJid();
        final Cursor countCursor = db.rawQuery("select count(messages.uuid) from messages join conversations on conversations.uuid=messages.conversationUuid join accounts on conversations.accountUuid=accounts.uuid where accounts.username=? and accounts.server=?", new String[] { jid.getEscapedLocal(), jid.getDomain().toEscapedString() });
        countCursor.moveToFirst();
        final int count = countCursor.getInt(0);
        Log.d(Config.LOGTAG, String.format("restored %d messages in %s", count, stopwatch.stop().toString()));
        countCursor.close();
        stopBackgroundService();
        synchronized (mOnBackupProcessedListeners) {
            for (OnBackupProcessed l : mOnBackupProcessedListeners) {
                l.onBackupRestored();
            }
        }
        return true;
    } catch (final Exception e) {
        final Throwable throwable = e.getCause();
        final boolean reasonWasCrypto = throwable instanceof BadPaddingException || e instanceof ZipException;
        synchronized (mOnBackupProcessedListeners) {
            for (OnBackupProcessed l : mOnBackupProcessedListeners) {
                if (reasonWasCrypto) {
                    l.onBackupDecryptionFailed();
                } else {
                    l.onBackupRestoreFailed();
                }
            }
        }
        Log.d(Config.LOGTAG, "error restoring backup " + uri, e);
        return false;
    }
}
Also used : KeyParameter(org.bouncycastle.crypto.params.KeyParameter) Stopwatch(com.google.common.base.Stopwatch) BadPaddingException(javax.crypto.BadPaddingException) Cursor(android.database.Cursor) GZIPInputStream(java.util.zip.GZIPInputStream) AEADBlockCipher(org.bouncycastle.crypto.modes.AEADBlockCipher) AESEngine(org.bouncycastle.crypto.engines.AESEngine) CipherInputStream(org.bouncycastle.crypto.io.CipherInputStream) InputStreamReader(java.io.InputStreamReader) Jid(eu.siacs.conversations.xmpp.Jid) GZIPInputStream(java.util.zip.GZIPInputStream) CountingInputStream(com.google.common.io.CountingInputStream) CipherInputStream(org.bouncycastle.crypto.io.CipherInputStream) DataInputStream(java.io.DataInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) CountingInputStream(com.google.common.io.CountingInputStream) ZipException(java.util.zip.ZipException) DataInputStream(java.io.DataInputStream) FileInputStream(java.io.FileInputStream) ZipException(java.util.zip.ZipException) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) BadPaddingException(javax.crypto.BadPaddingException) BackupFileHeader(eu.siacs.conversations.utils.BackupFileHeader) AEADParameters(org.bouncycastle.crypto.params.AEADParameters) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) BufferedReader(java.io.BufferedReader) File(java.io.File) GCMBlockCipher(org.bouncycastle.crypto.modes.GCMBlockCipher)

Example 3 with CipherInputStream

use of org.bouncycastle.crypto.io.CipherInputStream in project Zom-Android by zom.

the class Downloader method setupInputStream.

public static InputStream setupInputStream(InputStream is, byte[] keyAndIv) {
    if (keyAndIv != null && keyAndIv.length == 48) {
        byte[] key = new byte[32];
        byte[] iv = new byte[16];
        System.arraycopy(keyAndIv, 0, iv, 0, 16);
        System.arraycopy(keyAndIv, 16, key, 0, 32);
        AEADBlockCipher cipher = new GCMBlockCipher(new AESEngine());
        cipher.init(true, new AEADParameters(new KeyParameter(key), 128, iv));
        return new CipherInputStream(is, cipher);
    } else {
        return is;
    }
}
Also used : AESEngine(org.bouncycastle.crypto.engines.AESEngine) AEADParameters(org.bouncycastle.crypto.params.AEADParameters) CipherInputStream(org.bouncycastle.crypto.io.CipherInputStream) KeyParameter(org.bouncycastle.crypto.params.KeyParameter) GCMBlockCipher(org.bouncycastle.crypto.modes.GCMBlockCipher) AEADBlockCipher(org.bouncycastle.crypto.modes.AEADBlockCipher)

Aggregations

AESEngine (org.bouncycastle.crypto.engines.AESEngine)3 CipherInputStream (org.bouncycastle.crypto.io.CipherInputStream)3 AEADBlockCipher (org.bouncycastle.crypto.modes.AEADBlockCipher)3 GCMBlockCipher (org.bouncycastle.crypto.modes.GCMBlockCipher)3 AEADParameters (org.bouncycastle.crypto.params.AEADParameters)3 KeyParameter (org.bouncycastle.crypto.params.KeyParameter)3 Cursor (android.database.Cursor)1 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)1 Stopwatch (com.google.common.base.Stopwatch)1 CountingInputStream (com.google.common.io.CountingInputStream)1 BackupFileHeader (eu.siacs.conversations.utils.BackupFileHeader)1 Jid (eu.siacs.conversations.xmpp.Jid)1 BufferedReader (java.io.BufferedReader)1 DataInputStream (java.io.DataInputStream)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1