Search in sources :

Example 11 with PwDbOutputException

use of com.keepassdroid.database.exception.PwDbOutputException in project keepass2android by PhilippC.

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);
    random.nextBytes(header.transformSeed);
    return random;
}
Also used : PwDbOutputException(com.keepassdroid.database.exception.PwDbOutputException) SecureRandom(java.security.SecureRandom) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 12 with PwDbOutputException

use of com.keepassdroid.database.exception.PwDbOutputException in project keepass2android by PhilippC.

the class PwDbV3Output method output.

@Override
public void output() throws PwDbOutputException {
    prepForOutput();
    PwDbHeader header = outputHeader(mOS);
    byte[] finalKey = getFinalKey(header);
    Cipher cipher;
    try {
        if (mPM.algorithm == PwEncryptionAlgorithm.Rjindal) {
            cipher = CipherFactory.getInstance("AES/CBC/PKCS5Padding");
        } else if (mPM.algorithm == PwEncryptionAlgorithm.Twofish) {
            cipher = CipherFactory.getInstance("TWOFISH/CBC/PKCS7PADDING");
        } else {
            throw new Exception();
        }
    } catch (Exception e) {
        throw new PwDbOutputException("Algorithm not supported.");
    }
    try {
        cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(finalKey, "AES"), new IvParameterSpec(header.encryptionIV));
        CipherOutputStream cos = new CipherOutputStream(mOS, cipher);
        BufferedOutputStream bos = new BufferedOutputStream(cos);
        outputPlanGroupAndEntries(bos);
        bos.flush();
        bos.close();
    } catch (InvalidKeyException e) {
        throw new PwDbOutputException("Invalid key");
    } catch (InvalidAlgorithmParameterException e) {
        throw new PwDbOutputException("Invalid algorithm parameter.");
    } catch (IOException e) {
        throw new PwDbOutputException("Failed to output final encrypted part.");
    }
}
Also used : CipherOutputStream(javax.crypto.CipherOutputStream) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) PwDbOutputException(com.keepassdroid.database.exception.PwDbOutputException) SecretKeySpec(javax.crypto.spec.SecretKeySpec) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) IOException(java.io.IOException) InvalidKeyException(java.security.InvalidKeyException) PwDbHeader(com.keepassdroid.database.PwDbHeader) BufferedOutputStream(java.io.BufferedOutputStream) PwDbOutputException(com.keepassdroid.database.exception.PwDbOutputException) IOException(java.io.IOException) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException)

Example 13 with PwDbOutputException

use of com.keepassdroid.database.exception.PwDbOutputException in project keepass2android by PhilippC.

the class PwDbV3Output method outputHeader.

public PwDbHeader 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);
    // Write checksum Checksum
    MessageDigest md = null;
    try {
        md = MessageDigest.getInstance("SHA-256");
    } catch (NoSuchAlgorithmException e) {
        throw new PwDbOutputException("SHA-256 not implemented here.");
    }
    NullOutputStream nos;
    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
    PwDbHeaderOutputV3 pho = new PwDbHeaderOutputV3(header, os);
    try {
        pho.output();
    } catch (IOException e) {
        throw new PwDbOutputException("Failed to output the header.");
    }
    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 14 with PwDbOutputException

use of com.keepassdroid.database.exception.PwDbOutputException in project keepass2android by PhilippC.

the class PwDbV3Output method outputPlanGroupAndEntries.

public void outputPlanGroupAndEntries(OutputStream os) throws PwDbOutputException {
    // long size = 0;
    // Groups
    List<PwGroupV3> 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 group: " + 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 : PwGroupV3(com.keepassdroid.database.PwGroupV3) PwEntryV3(com.keepassdroid.database.PwEntryV3) PwDbOutputException(com.keepassdroid.database.exception.PwDbOutputException) IOException(java.io.IOException)

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