Search in sources :

Example 1 with PwDbOutputException

use of com.keepassdroid.database.exception.PwDbOutputException in project KeePassDX by Kunzisoft.

the class Database method SaveData.

public void SaveData(Context ctx, Uri uri) throws IOException, PwDbOutputException {
    if (uri.getScheme().equals("file")) {
        String filename = uri.getPath();
        File tempFile = new File(filename + ".tmp");
        FileOutputStream fos = new FileOutputStream(tempFile);
        // BufferedOutputStream bos = new BufferedOutputStream(fos);
        // PwDbV3Output pmo = new PwDbV3Output(pm, bos, App.getCalendar());
        PwDbOutput pmo = PwDbOutput.getInstance(pm, fos);
        pmo.output();
        // bos.flush();
        // bos.close();
        fos.close();
        // Force data to disk before continuing
        try {
            fos.getFD().sync();
        } catch (SyncFailedException e) {
        // Ignore if fsync fails. We tried.
        }
        File orig = new File(filename);
        if (!tempFile.renameTo(orig)) {
            throw new IOException("Failed to store database.");
        }
    } else {
        OutputStream os;
        try {
            os = ctx.getContentResolver().openOutputStream(uri);
        } catch (Exception e) {
            throw new IOException("Failed to store database.");
        }
        PwDbOutput pmo = PwDbOutput.getInstance(pm, os);
        pmo.output();
        os.close();
    }
    mUri = uri;
}
Also used : FileOutputStream(java.io.FileOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) SyncFailedException(java.io.SyncFailedException) IOException(java.io.IOException) File(java.io.File) PwDbOutput(com.keepassdroid.database.save.PwDbOutput) InvalidDBException(com.keepassdroid.database.exception.InvalidDBException) PwDbOutputException(com.keepassdroid.database.exception.PwDbOutputException) InvalidPasswordException(com.keepassdroid.database.exception.InvalidPasswordException) ContentFileNotFoundException(com.keepassdroid.database.exception.ContentFileNotFoundException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) SyncFailedException(java.io.SyncFailedException)

Example 2 with PwDbOutputException

use of com.keepassdroid.database.exception.PwDbOutputException in project KeePassDX by Kunzisoft.

the class PwDbOutput method setIVs.

protected SecureRandom setIVs(PwDbHeader header) throws PwDbOutputException {
    SecureRandom random;
    try {
        random = SecureRandom.getInstance("SHA1PRNG");
    } catch (NoSuchAlgorithmException e) {
        throw new PwDbOutputException("Does not support secure random number generation.");
    }
    random.nextBytes(header.encryptionIV);
    random.nextBytes(header.masterSeed);
    return random;
}
Also used : PwDbOutputException(com.keepassdroid.database.exception.PwDbOutputException) SecureRandom(java.security.SecureRandom) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 3 with PwDbOutputException

use of com.keepassdroid.database.exception.PwDbOutputException in project KeePassDX by Kunzisoft.

the class PwDbV3Output method outputHeader.

public PwDbHeaderV3 outputHeader(OutputStream os) throws PwDbOutputException {
    // Build header
    PwDbHeaderV3 header = new PwDbHeaderV3();
    header.signature1 = PwDbHeader.PWM_DBSIG_1;
    header.signature2 = PwDbHeaderV3.DBSIG_2;
    header.flags = PwDbHeaderV3.FLAG_SHA2;
    if (mPM.getEncAlgorithm() == PwEncryptionAlgorithm.Rjindal) {
        header.flags |= PwDbHeaderV3.FLAG_RIJNDAEL;
    } else if (mPM.getEncAlgorithm() == PwEncryptionAlgorithm.Twofish) {
        header.flags |= PwDbHeaderV3.FLAG_TWOFISH;
    } else {
        throw new PwDbOutputException("Unsupported algorithm.");
    }
    header.version = PwDbHeaderV3.DBVER_DW;
    header.numGroups = mPM.getGroups().size();
    header.numEntries = mPM.entries.size();
    header.numKeyEncRounds = mPM.getNumKeyEncRecords();
    setIVs(header);
    // Content checksum
    MessageDigest md = null;
    try {
        md = MessageDigest.getInstance("SHA-256");
    } catch (NoSuchAlgorithmException e) {
        throw new PwDbOutputException("SHA-256 not implemented here.");
    }
    // Header checksum
    MessageDigest headerDigest;
    try {
        headerDigest = MessageDigest.getInstance("SHA-256");
    } catch (NoSuchAlgorithmException e) {
        throw new PwDbOutputException("SHA-256 not implemented here.");
    }
    NullOutputStream nos;
    nos = new NullOutputStream();
    DigestOutputStream headerDos = new DigestOutputStream(nos, headerDigest);
    // Output header for the purpose of calculating the header checksum
    PwDbHeaderOutputV3 pho = new PwDbHeaderOutputV3(header, headerDos);
    try {
        pho.outputStart();
        pho.outputEnd();
        headerDos.flush();
    } catch (IOException e) {
        throw new PwDbOutputException(e);
    }
    byte[] headerHash = headerDigest.digest();
    headerHashBlock = getHeaderHashBuffer(headerHash);
    // Output database for the purpose of calculating the content checksum
    nos = new NullOutputStream();
    DigestOutputStream dos = new DigestOutputStream(nos, md);
    BufferedOutputStream bos = new BufferedOutputStream(dos);
    try {
        outputPlanGroupAndEntries(bos);
        bos.flush();
        bos.close();
    } catch (IOException e) {
        throw new PwDbOutputException("Failed to generate checksum.");
    }
    header.contentsHash = md.digest();
    // Output header for real output, containing content hash
    pho = new PwDbHeaderOutputV3(header, os);
    try {
        pho.outputStart();
        dos.on(false);
        pho.outputContentHash();
        dos.on(true);
        pho.outputEnd();
        dos.flush();
    } catch (IOException e) {
        throw new PwDbOutputException(e);
    }
    return header;
}
Also used : PwDbOutputException(com.keepassdroid.database.exception.PwDbOutputException) DigestOutputStream(java.security.DigestOutputStream) PwDbHeaderV3(com.keepassdroid.database.PwDbHeaderV3) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) MessageDigest(java.security.MessageDigest) BufferedOutputStream(java.io.BufferedOutputStream) NullOutputStream(com.keepassdroid.stream.NullOutputStream)

Example 4 with PwDbOutputException

use of com.keepassdroid.database.exception.PwDbOutputException in project KeePassDX by Kunzisoft.

the class PwDbV3Output method outputPlanGroupAndEntries.

public void outputPlanGroupAndEntries(OutputStream os) throws PwDbOutputException {
    LEDataOutputStream los = new LEDataOutputStream(os);
    if (useHeaderHash() && headerHashBlock != null) {
        try {
            los.writeUShort(0x0000);
            los.writeInt(headerHashBlock.length);
            los.write(headerHashBlock);
        } catch (IOException e) {
            throw new PwDbOutputException("Failed to output header hash: " + e.getMessage());
        }
    }
    // Groups
    List<PwGroup> groups = mPM.getGroups();
    for (int i = 0; i < groups.size(); i++) {
        PwGroupV3 pg = (PwGroupV3) groups.get(i);
        PwGroupOutputV3 pgo = new PwGroupOutputV3(pg, os);
        try {
            pgo.output();
        } catch (IOException e) {
            throw new PwDbOutputException("Failed to output a tree: " + e.getMessage());
        }
    }
    // Entries
    for (int i = 0; i < mPM.entries.size(); i++) {
        PwEntryV3 pe = (PwEntryV3) mPM.entries.get(i);
        PwEntryOutputV3 peo = new PwEntryOutputV3(pe, os);
        try {
            peo.output();
        } catch (IOException e) {
            throw new PwDbOutputException("Failed to output an entry.");
        }
    }
}
Also used : LEDataOutputStream(com.keepassdroid.stream.LEDataOutputStream) PwGroupV3(com.keepassdroid.database.PwGroupV3) PwEntryV3(com.keepassdroid.database.PwEntryV3) PwDbOutputException(com.keepassdroid.database.exception.PwDbOutputException) PwGroup(com.keepassdroid.database.PwGroup) IOException(java.io.IOException)

Example 5 with PwDbOutputException

use of com.keepassdroid.database.exception.PwDbOutputException in project KeePassDX by Kunzisoft.

the class PwDbV4Output method output.

@Override
public void output() throws PwDbOutputException {
    try {
        try {
            engine = CipherFactory.getInstance(mPM.dataCipher);
        } catch (NoSuchAlgorithmException e) {
            throw new PwDbOutputException("No such cipher", e);
        }
        header = (PwDbHeaderV4) outputHeader(mOS);
        OutputStream osPlain;
        if (header.version < PwDbHeaderV4.FILE_VERSION_32_4) {
            CipherOutputStream cos = attachStreamEncryptor(header, mOS);
            cos.write(header.streamStartBytes);
            HashedBlockOutputStream hashed = new HashedBlockOutputStream(cos);
            osPlain = hashed;
        } else {
            mOS.write(hashOfHeader);
            mOS.write(headerHmac);
            HmacBlockOutputStream hbos = new HmacBlockOutputStream(mOS, mPM.hmacKey);
            osPlain = attachStreamEncryptor(header, hbos);
        }
        OutputStream osXml;
        try {
            if (mPM.compressionAlgorithm == PwCompressionAlgorithm.Gzip) {
                osXml = new GZIPOutputStream(osPlain);
            } else {
                osXml = osPlain;
            }
            if (header.version >= PwDbHeaderV4.FILE_VERSION_32_4) {
                PwDbInnerHeaderOutputV4 ihOut = new PwDbInnerHeaderOutputV4((PwDatabaseV4) mPM, header, osXml);
                ihOut.output();
            }
            outputDatabase(osXml);
            osXml.close();
        } catch (IllegalArgumentException e) {
            throw new PwDbOutputException(e);
        } catch (IllegalStateException e) {
            throw new PwDbOutputException(e);
        }
    } catch (IOException e) {
        throw new PwDbOutputException(e);
    }
}
Also used : CipherOutputStream(javax.crypto.CipherOutputStream) PwDbOutputException(com.keepassdroid.database.exception.PwDbOutputException) GZIPOutputStream(java.util.zip.GZIPOutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) CipherOutputStream(javax.crypto.CipherOutputStream) LEDataOutputStream(com.keepassdroid.stream.LEDataOutputStream) HmacBlockOutputStream(com.keepassdroid.stream.HmacBlockOutputStream) HashedBlockOutputStream(com.keepassdroid.stream.HashedBlockOutputStream) OutputStream(java.io.OutputStream) HmacBlockOutputStream(com.keepassdroid.stream.HmacBlockOutputStream) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) HashedBlockOutputStream(com.keepassdroid.stream.HashedBlockOutputStream)

Aggregations

PwDbOutputException (com.keepassdroid.database.exception.PwDbOutputException)14 IOException (java.io.IOException)11 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)8 BufferedOutputStream (java.io.BufferedOutputStream)4 CipherOutputStream (javax.crypto.CipherOutputStream)4 PwDbHeaderV3 (com.keepassdroid.database.PwDbHeaderV3)3 SecureRandom (java.security.SecureRandom)3 Cipher (javax.crypto.Cipher)3 PwDbHeader (com.keepassdroid.database.PwDbHeader)2 PwDbHeaderV4 (com.keepassdroid.database.PwDbHeaderV4)2 PwEntryV3 (com.keepassdroid.database.PwEntryV3)2 PwGroupV3 (com.keepassdroid.database.PwGroupV3)2 LEDataOutputStream (com.keepassdroid.stream.LEDataOutputStream)2 NullOutputStream (com.keepassdroid.stream.NullOutputStream)2 OutputStream (java.io.OutputStream)2 DigestOutputStream (java.security.DigestOutputStream)2 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)2 InvalidKeyException (java.security.InvalidKeyException)2 MessageDigest (java.security.MessageDigest)2 IvParameterSpec (javax.crypto.spec.IvParameterSpec)2