Support for outputing the database header.

This commit is contained in:
Brian Pellin committed 2009-04-30 21:32:54 -05:00
1 parent 0287413643
commit 76b68ef2c4
12 files changed
+592 -254

No files matched your search

@@ -0,0 +1,51 @@
/*
* Copyright 2009 Brian Pellin.
*
* This file is part of KeePassDroid.
*
* KeePassDroid is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* KeePassDroid is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with KeePassDroid. If not, see <http://www.gnu.org/licenses/>.
*
*/
package com.android.keepass.keepasslib;
import java.io.IOException;
import java.io.OutputStream;
import org.phoneid.keepassj2me.PwDbHeader;
import org.phoneid.keepassj2me.Types;
public class PwDbHeaderOutput {
private PwDbHeader mHeader;
private OutputStream mOS;
public PwDbHeaderOutput(PwDbHeader header, OutputStream os) {
mHeader = header;
mOS = os;
}
public void output() throws IOException {
mOS.write(Types.writeInt(mHeader.signature1));
mOS.write(Types.writeInt(mHeader.signature2));
mOS.write(Types.writeInt(mHeader.flags));
mOS.write(Types.writeInt(mHeader.version));
mOS.write(mHeader.masterSeed);
mOS.write(mHeader.encryptionIV);
mOS.write(Types.writeInt(mHeader.numGroups));
mOS.write(Types.writeInt(mHeader.numEntries));
mOS.write(mHeader.contentsHash);
mOS.write(mHeader.masterSeed2);
mOS.write(Types.writeInt(mHeader.numKeyEncRounds));
}
}
@@ -26,95 +26,7 @@ import org.phoneid.keepassj2me.PwEntry;
import org.phoneid.keepassj2me.Types;
public class PwEntryOutput {
private OutputStream mOS;
private PwEntry mPE;
/** Output the PwGroup to the stream
* @param pe
* @param os
*/
public PwEntryOutput(PwEntry pe, OutputStream os) {
mPE = pe;
mOS = os;
}
//NOTE: Need be to careful about using ints. The actual type written to file is a unsigned int
public void output() throws IOException {
// UUID
mOS.write(UUID_FIELD_TYPE);
mOS.write(UUID_FIELD_SIZE);
mOS.write(mPE.uuid);
// Group ID
mOS.write(GROUPID_FIELD_TYPE);
mOS.write(LONG_FOUR);
mOS.write(Types.writeInt(mPE.groupId));
// Image ID
mOS.write(IMAGEID_FIELD_TYPE);
mOS.write(LONG_FOUR);
mOS.write(Types.writeInt(mPE.imageId));
// Title
//byte[] title = mPE.title.getBytes("UTF-8");
mOS.write(TITLE_FIELD_TYPE);
Types.writeCString(mPE.title, mOS);
// URL
mOS.write(URL_FIELD_TYPE);
Types.writeCString(mPE.url, mOS);
// Username
mOS.write(USERNAME_FIELD_TYPE);
Types.writeCString(mPE.username, mOS);
// Password
byte[] password = mPE.getPassword();
mOS.write(PASSWORD_FIELD_TYPE);
mOS.write(Types.writeInt(password.length+1));
mOS.write(password);
mOS.write(0);
// Additional
mOS.write(ADDITIONAL_FIELD_TYPE);
Types.writeCString(mPE.additional, mOS);
// Create date
mOS.write(CREATE_FIELD_TYPE);
mOS.write(DATE_FIELD_SIZE);
mOS.write(Types.writeTime(mPE.tCreation));
// Modification date
mOS.write(MOD_FIELD_TYPE);
mOS.write(DATE_FIELD_SIZE);
mOS.write(Types.writeTime(mPE.tLastMod));
// Access date
mOS.write(ACCESS_FIELD_TYPE);
mOS.write(DATE_FIELD_SIZE);
mOS.write(Types.writeTime(mPE.tLastAccess));
// Expiration date
mOS.write(EXPIRE_FIELD_TYPE);
mOS.write(DATE_FIELD_SIZE);
mOS.write(Types.writeTime(mPE.tExpire));
// Binary desc
mOS.write(BINARY_DESC_FIELD_TYPE);
Types.writeCString(mPE.binaryDesc, mOS);
// Binary data
byte[] data = mPE.getBinaryData();
mOS.write(BINARY_DATA_FIELD_TYPE);
mOS.write(Types.writeInt(data.length));
mOS.write(data);
// End
mOS.write(END_FIELD_TYPE);
mOS.write(ZERO_FIELD_SIZE);
}
// Constants
public static final byte[] UUID_FIELD_TYPE = Types.writeShort(1);
public static final byte[] GROUPID_FIELD_TYPE = Types.writeShort(2);
public static final byte[] IMAGEID_FIELD_TYPE = Types.writeShort(3);
@@ -139,5 +51,109 @@ public class PwEntryOutput {
public static final byte[] ZERO_FIELD_SIZE = Types.writeInt(0);
public static final byte[] TEST = {0x33, 0x33, 0x33, 0x33};
private OutputStream mOS;
private PwEntry mPE;
private long outputBytes = 0;
/** Output the PwGroup to the stream
* @param pe
* @param os
*/
public PwEntryOutput(PwEntry pe, OutputStream os) {
mPE = pe;
mOS = os;
}
//NOTE: Need be to careful about using ints. The actual type written to file is a unsigned int
public void output() throws IOException {
outputBytes += 134; // Length of fixed size fields
// UUID
mOS.write(UUID_FIELD_TYPE);
mOS.write(UUID_FIELD_SIZE);
mOS.write(mPE.uuid);
// Group ID
mOS.write(GROUPID_FIELD_TYPE);
mOS.write(LONG_FOUR);
mOS.write(Types.writeInt(mPE.groupId));
// Image ID
mOS.write(IMAGEID_FIELD_TYPE);
mOS.write(LONG_FOUR);
mOS.write(Types.writeInt(mPE.imageId));
// Title
//byte[] title = mPE.title.getBytes("UTF-8");
mOS.write(TITLE_FIELD_TYPE);
int titleLen = Types.writeCString(mPE.title, mOS);
outputBytes += titleLen;
// URL
mOS.write(URL_FIELD_TYPE);
int urlLen = Types.writeCString(mPE.url, mOS);
outputBytes += urlLen;
// Username
mOS.write(USERNAME_FIELD_TYPE);
int userLen = Types.writeCString(mPE.username, mOS);
outputBytes += userLen;
// Password
byte[] password = mPE.getPassword();
mOS.write(PASSWORD_FIELD_TYPE);
mOS.write(Types.writeInt(password.length+1));
mOS.write(password);
mOS.write(0);
outputBytes += password.length + 1;
// Additional
mOS.write(ADDITIONAL_FIELD_TYPE);
int addlLen = Types.writeCString(mPE.additional, mOS);
outputBytes += addlLen;
// Create date
mOS.write(CREATE_FIELD_TYPE);
mOS.write(DATE_FIELD_SIZE);
mOS.write(Types.writeTime(mPE.tCreation));
// Modification date
mOS.write(MOD_FIELD_TYPE);
mOS.write(DATE_FIELD_SIZE);
mOS.write(Types.writeTime(mPE.tLastMod));
// Access date
mOS.write(ACCESS_FIELD_TYPE);
mOS.write(DATE_FIELD_SIZE);
mOS.write(Types.writeTime(mPE.tLastAccess));
// Expiration date
mOS.write(EXPIRE_FIELD_TYPE);
mOS.write(DATE_FIELD_SIZE);
mOS.write(Types.writeTime(mPE.tExpire));
// Binary desc
mOS.write(BINARY_DESC_FIELD_TYPE);
int descLen = Types.writeCString(mPE.binaryDesc, mOS);
outputBytes += descLen;
// Binary data
byte[] data = mPE.getBinaryData();
mOS.write(BINARY_DATA_FIELD_TYPE);
mOS.write(Types.writeInt(data.length));
mOS.write(data);
outputBytes += data.length;
// End
mOS.write(END_FIELD_TYPE);
mOS.write(ZERO_FIELD_SIZE);
}
/** Returns the number of bytes written by the stream
* @return Number of bytes written
*/
public long getLength() {
return outputBytes;
}
}
@@ -26,8 +26,28 @@ import org.phoneid.keepassj2me.PwGroup;
import org.phoneid.keepassj2me.Types;
public class PwGroupOutput {
// Constants
public static final byte[] GROUPID_FIELD_TYPE = Types.writeShort(1);
public static final byte[] NAME_FIELD_TYPE = Types.writeShort(2);
public static final byte[] CREATE_FIELD_TYPE = Types.writeShort(3);
public static final byte[] MOD_FIELD_TYPE = Types.writeShort(4);
public static final byte[] ACCESS_FIELD_TYPE = Types.writeShort(5);
public static final byte[] EXPIRE_FIELD_TYPE = Types.writeShort(6);
public static final byte[] IMAGEID_FIELD_TYPE = Types.writeShort(7);
public static final byte[] LEVEL_FIELD_TYPE = Types.writeShort(8);
public static final byte[] FLAGS_FIELD_TYPE = Types.writeShort(9);
public static final byte[] END_FIELD_TYPE = Types.writeShort(0xFFFF);
public static final byte[] LONG_FOUR = Types.writeInt(4);
public static final byte[] GROUPID_FIELD_SIZE = LONG_FOUR;
public static final byte[] DATE_FIELD_SIZE = Types.writeInt(5);
public static final byte[] IMAGEID_FIELD_SIZE = LONG_FOUR;
public static final byte[] LEVEL_FIELD_SIZE = Types.writeInt(2);
public static final byte[] FLAGS_FIELD_SIZE = LONG_FOUR;
public static final byte[] ZERO_FIELD_SIZE = Types.writeInt(0);
private OutputStream mOS;
private PwGroup mPG;
private long outputBytes = 0;
/** Output the PwGroup to the stream
* @param pg
@@ -38,9 +58,11 @@ public class PwGroupOutput {
mOS = os;
}
//NOTE: Need be to careful about using ints. The actual type written to file is a unsigned int
public void output() throws IOException {
//NOTE: Need be to careful about using ints. The actual type written to file is a unsigned int, but most values can't be greater than 2^31, so it probably doesn't matter.
outputBytes += 94; // Length of fixed size fields
// Group ID
mOS.write(GROUPID_FIELD_TYPE);
mOS.write(GROUPID_FIELD_SIZE);
@@ -48,8 +70,9 @@ public class PwGroupOutput {
// Name
mOS.write(NAME_FIELD_TYPE);
Types.writeCString(mPG.name, mOS);
int nameLen = Types.writeCString(mPG.name, mOS);
outputBytes += nameLen;
// Create date
mOS.write(CREATE_FIELD_TYPE);
mOS.write(DATE_FIELD_SIZE);
@@ -90,21 +113,11 @@ public class PwGroupOutput {
mOS.write(ZERO_FIELD_SIZE);
}
public static final byte[] GROUPID_FIELD_TYPE = Types.writeShort(1);
public static final byte[] NAME_FIELD_TYPE = Types.writeShort(2);
public static final byte[] CREATE_FIELD_TYPE = Types.writeShort(3);
public static final byte[] MOD_FIELD_TYPE = Types.writeShort(4);
public static final byte[] ACCESS_FIELD_TYPE = Types.writeShort(5);
public static final byte[] EXPIRE_FIELD_TYPE = Types.writeShort(6);
public static final byte[] IMAGEID_FIELD_TYPE = Types.writeShort(7);
public static final byte[] LEVEL_FIELD_TYPE = Types.writeShort(8);
public static final byte[] FLAGS_FIELD_TYPE = Types.writeShort(9);
public static final byte[] END_FIELD_TYPE = Types.writeShort(0xFFFF);
public static final byte[] LONG_FOUR = Types.writeInt(4);
public static final byte[] GROUPID_FIELD_SIZE = LONG_FOUR;
public static final byte[] DATE_FIELD_SIZE = Types.writeInt(5);
public static final byte[] IMAGEID_FIELD_SIZE = LONG_FOUR;
public static final byte[] LEVEL_FIELD_SIZE = Types.writeInt(2);
public static final byte[] FLAGS_FIELD_SIZE = LONG_FOUR;
public static final byte[] ZERO_FIELD_SIZE = Types.writeInt(0);
/** Returns the number of bytes written by the stream
* @return Number of bytes written
*/
public long getLength() {
return outputBytes;
}
}
@@ -0,0 +1,179 @@
/*
* Copyright 2009 Brian Pellin.
*
* This file is part of KeePassDroid.
*
* KeePassDroid is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* KeePassDroid is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with KeePassDroid. If not, see <http://www.gnu.org/licenses/>.
*
*/
package com.android.keepass.keepasslib;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.security.DigestOutputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import org.phoneid.keepassj2me.PwDbHeader;
import org.phoneid.keepassj2me.PwEntry;
import org.phoneid.keepassj2me.PwGroup;
import org.phoneid.keepassj2me.PwManager;
public class PwManagerOutput {
private PwManager mPM;
private OutputStream mOS;
private final boolean mDebug;
public static final boolean DEBUG = true;
public PwManagerOutput(PwManager pm, OutputStream os) {
mPM = pm;
mOS = os;
mDebug = false;
}
public PwManagerOutput(PwManager pm, OutputStream os, boolean debug) {
mPM = pm;
mOS = os;
mDebug = debug;
}
public void output() throws IOException, PwManagerOutputException {
/*
int filePadding = (int)(16 - (fileSize % 16)); // Pad file to 16-byte boundary
if ( filePadding > 0 ) {
byte[] padding = new byte[filePadding];
fos.write(padding);
}
*/
}
public void outputHeader(OutputStream os) throws PwManagerOutputException {
// Build header
PwDbHeader header = new PwDbHeader();
header.signature1 = PwDbHeader.PWM_DBSIG_1;
header.signature2 = PwDbHeader.PWM_DBSIG_2;
header.flags = PwDbHeader.PWM_FLAG_SHA2;
if ( mPM.getAlgorithm() == PwDbHeader.ALGO_AES ) {
header.flags |= PwDbHeader.PWM_FLAG_RIJNDAEL;
} else if ( mPM.getAlgorithm() == PwDbHeader.ALGO_TWOFISH ) {
header.flags |= PwDbHeader.PWM_FLAG_TWOFISH;
throw new PwManagerOutputException("Unsupported algorithm.");
} else {
throw new PwManagerOutputException("Unsupported algorithm.");
}
header.version = PwDbHeader.PWM_DBVER_DW;
header.numGroups = mPM.groups.size();
header.numEntries = mPM.entries.size();
header.numKeyEncRounds = mPM.getNumKeyEncRecords();
// Reuse random values to test equivalence in debug mode
if ( mDebug ) {
System.arraycopy(mPM.dbHeader.encryptionIV, 0, header.encryptionIV, 0, header.encryptionIV.length);
System.arraycopy(mPM.dbHeader.masterSeed, 0, header.masterSeed, 0, header.masterSeed.length);
System.arraycopy(mPM.dbHeader.masterSeed2, 0, header.masterSeed2, 0, header.masterSeed2.length);
} else {
SecureRandom random;
try {
random = SecureRandom.getInstance("SHA1PRNG");
} catch (NoSuchAlgorithmException e) {
throw new PwManagerOutputException("Does not support secure random number generation.");
}
random.nextBytes(header.encryptionIV);
random.nextBytes(header.masterSeed);
random.nextBytes(header.masterSeed2);
}
// Write checksum Checksum
MessageDigest md = null;
try {
md = MessageDigest.getInstance("SHA-256");
} catch (NoSuchAlgorithmException e) {
assert true;
throw new PwManagerOutputException("SHA-256 not implemented here.");
}
FileOutputStream fos;
try {
fos = new FileOutputStream("/dev/null");
} catch (FileNotFoundException e1) {
throw new PwManagerOutputException("Could not open /dev/null");
}
DigestOutputStream dos = new DigestOutputStream(fos, md);
try {
outputPlanGroupAndEntries(dos);
dos.close();
} catch (IOException e) {
throw new PwManagerOutputException("Failed to generate checksum.");
}
header.contentsHash = md.digest();
// Output header
PwDbHeaderOutput pho = new PwDbHeaderOutput(header, os);
try {
pho.output();
} catch (IOException e) {
throw new PwManagerOutputException("Failed to output the header.");
}
}
public void outputPlanGroupAndEntries(OutputStream os) throws IOException {
//long size = 0;
// Groups
for (int i = 0; i < mPM.groups.size(); i++ ) {
PwGroup pg = mPM.groups.get(i);
PwGroupOutput pgo = new PwGroupOutput(pg, os);
pgo.output();
//size += pgo.getLength();
}
// Entries
for (int i = 0; i < mPM.entries.size(); i++ ) {
PwEntry pe = mPM.entries.get(i);
PwEntryOutput peo = new PwEntryOutput(pe, os);
peo.output();
//size += peo.getLength();
}
//return size;
}
class PwManagerOutputException extends Exception {
public PwManagerOutputException(String string) {
super(string);
}
/**
*
*/
private static final long serialVersionUID = 3321212743159473368L;
}
}
@@ -0,0 +1,64 @@
/*
* Copyright 2009 Brian Pellin.
*
* This file is part of KeePassDroid.
*
* KeePassDroid is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* KeePassDroid is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with KeePassDroid. If not, see <http://www.gnu.org/licenses/>.
*
*/
package com.android.keepass.keepasslib;
import java.io.IOException;
import java.io.OutputStream;
import java.io.RandomAccessFile;
public class RandomFileOutputStream extends OutputStream {
RandomAccessFile mFile;
RandomFileOutputStream(RandomAccessFile file) {
mFile = file;
}
@Override
public void close() throws IOException {
super.close();
mFile.close();
}
@Override
public void write(byte[] buffer, int offset, int count) throws IOException {
super.write(buffer, offset, count);
mFile.write(buffer, offset, count);
}
@Override
public void write(byte[] buffer) throws IOException {
super.write(buffer);
mFile.write(buffer);
}
@Override
public void write(int oneByte) throws IOException {
mFile.write(oneByte);
}
public void seek(long pos) throws IOException {
mFile.seek(pos);
}
}