#include <file.h>
Classes | |
struct | DWORD |
A 32 bits double word. More... | |
union | ENDIAN |
Used for little-big endian conversion. More... | |
struct | WORD |
A 16 bits word. More... | |
Public Member Functions | |
File (const File &f) throw (YAPETException) | |
File (const std::string &fn, const Key &key, bool create=false) throw (YAPETException) | |
Constructor. | |
std::string | getFilename () const |
Returns the file name of the current file. | |
time_t | getMasterPWSet (const Key &key) const throw (YAPETException,YAPETInvalidPasswordException) |
const File & | operator= (const File &f) throw (YAPETException) |
Returns the time the master password was set. | |
std::list< PartDec > | read (const Key &key) const throw (YAPETException) |
Reads the stored password records from the file. | |
void | save (std::list< PartDec > &records) throw (YAPETException) |
Saves a password record list. | |
void | setNewKey (const Key &oldkey, const Key &newkey) throw (YAPETException) |
Sets a new encryption key for the current file. | |
~File () | |
Protected Member Functions | |
void | initFile (const Key &key) throw (YAPETException) |
Initializes an empty file. | |
bool | isempty () const throw (YAPETException) |
Indicates whether or not the file is empty. | |
BDBuffer * | read () const throw (YAPETException) |
Reads from the current offset in the file. | |
BDBuffer * | readHeader () const throw (YAPETException) |
Reads the encrypted header. | |
void | seekDataSection () const throw (YAPETException) |
Seeks to the first password record length indicator in the file. | |
uint32_t | uint32_from_disk (uint32_t i) const |
The given integer will be converted to the endianess of the host. | |
uint32_t | uint32_from_disk (uint32_t i) const |
The given integer will be converted to the endianess of the host. | |
uint32_t | uint32_to_disk (uint32_t i) const |
The given integer will be converted to big endian format. | |
uint32_t | uint32_to_disk (uint32_t i) const |
The given integer will be converted to big endian format. | |
void | validateKey (const Key &key) throw (YAPETException,YAPETInvalidPasswordException) |
Validates the given key. | |
void | write (const BDBuffer &buff, bool forceappend=false, bool forcewrite=false) throw (YAPETException, YAPETRetryException) |
Writes at the current offset in the file. | |
void | writeHeader (const BDBuffer &enc_header) throw (YAPETException) |
Writes the given encrypted header to the file. | |
void | writeHeader (const Record< FileHeader > &header, const Key &key) throw (YAPETException) |
Writes the given header encrypted to the file. | |
Private Member Functions | |
time_t | lastModified () const throw (YAPETException) |
Returns the last modification time of the open file. | |
void | openCreate () throw (YAPETException) |
Creates and opens a new file. | |
void | openNoCreate () throw (YAPETException) |
Opens an existing file. | |
void | preparePWSave () throw (YAPETException) |
Prepare the file for saving the password records. | |
void | seekAbs (off_t offset) const throw (YAPETException) |
Seek to an absolute offset. | |
void | seekCurr (off_t offset) const throw (YAPETException) |
Seek to a position relative to the current offset. | |
Private Attributes | |
int | fd |
The file descriptor of the password file. | |
std::string | filename |
The file name of the file. | |
time_t | mtime |
The modification time of the file. |
Each file created by this class starts with a unencrypted recognition string which currently consists of the 8 bytes "YAPET1.0" as depicted below.
+--------+--------+--------+--------+--------+--------+--------+--------+ | Y | A | P | E | T | 1 | . | 0 | | 1 byte | 1 byte | 1 byte | 1 byte | 1 byte | 1 byte | 1 byte | 1 byte | +--------+--------+--------+--------+--------+--------+--------+--------+
After the recognition string a 4 byte unsigned integer which is stored in big-endian order follows. This indicator is read to determine how many bytes to read in order to get the encrypted header.
+--------+--------+--------+--------+ | Length indicator in big-endian | | order (4 bytes) | +--------+--------+--------+--------+--...---+ | Encrypted header exactly as many bytes | | indicated by the prefix | +--------+--------+--------+--------+--...---+
The decrypted header is 25 bytes in size. The first byte indicates the version of the file. The next 20 bytes are used as control string. After decryption, the control string is compared to the predefined clear text control string, in order to find out whether or not the key used to decrypt was the same used to encrypt.
+--------+ |Version | | 1 byte | +--------+--------+--------+--...---+ | Control String | | 20 bytes | +--------+--------+--------+--...---+ | Time when the Password | | was set (4 bytes) | +--------+--------+--------+
Each encrypted password record is prefixed by a 4 byte unsigned integer which is stored in big-endian order. The methods take care returning them in the appropriate order of the host system. That integer is used to indicate the length of the following encrypted data chunk.
+--------+--------+--------+--------+ | Length indicator in big-endian | | order (4 bytes) | +--------+--------+--------+--------+--...---+ | Encrypted password record of exactly as | | many bytes as indicated by the prefix | +--------+--------+--------+--------+--...---+ | Length indicator in big-endian | | order (4 bytes) | +--------+--------+--------+--------+--...---+ | Encrypted password record of exactly as | | many bytes as indicated by the prefix | +--------+--------+--------+--------+--...---+ [ . . . ]
Putting this together, an encrypted file created by this class looks like this
+--------+--------+--------+--------+--------+--------+--------+--------+ | Y | A | P | E | T | 1 | . | 0 | | 1 byte | 1 byte | 1 byte | 1 byte | 1 byte | 1 byte | 1 byte | 1 byte | +--------+--------+--------+--------+--------+--------+--------+--------+ | Length indicator in big-endian | | order (4 bytes) | +--------+--------+--------+--------+--...---+ | Encrypted header exactly as many bytes | | indicated by the prefix | +--------+--------+--------+--------+--...---+ | Length indicator in big-endian | | order (4 bytes) | +--------+--------+--------+--------+--...---+ | Encrypted password record of exactly as | | many bytes as indicated by the prefix | +--------+--------+--------+--------+--...---+ | Length indicator in big-endian | | order (4 bytes) | +--------+--------+--------+--------+--...---+ | Encrypted password record of exactly as | | many bytes as indicated by the prefix | +--------+--------+--------+--------+--...---+ [ . . . ]
Instances of this class keeps the file open for the lifetime of the instance.
When saving a password record list, the file is reopened with the O_TRUNC
specified. The recognition string and header are copied over from the former version of the file.
Definition at line 170 of file file.h.
File::File | ( | const std::string & | fn, | |
const Key & | key, | |||
bool | create = false | |||
) | throw (YAPETException) |
Opens the file specified. Optionally creates the file if it does not exist and create
is set true
.
When opening an existing file, the key provided is validated. When creating a new file, the key is used to encrypt the header.
The file opened or created will stay open as long as the instance of this class exists. There is no method for closing the file. Only the destructor closes the file.
fn | string holding the file name | |
key | the key used for verification or encrypting the file header | |
create | flag indicating whether the file should be created (true ) or just opened (false ). Be aware that passing true to this flag always causes the file to be created. Even if it already exists. Existing files will be truncated and the data stored will be lost. |
File::File | ( | const File & | f | ) | throw (YAPETException) |
std::string YAPET::File::getFilename | ( | ) | const [inline] |
Definition at line 324 of file file.h.
References filename.
Referenced by MainWindow::changePassword(), MainWindow::lockScreen(), MainWindow::printTitle(), MainWindow::run(), and MainWindow::saveFile().
time_t File::getMasterPWSet | ( | const Key & | key | ) | const throw (YAPETException,YAPETInvalidPasswordException) |
Returns the time as a time_t
when the master password was set.
key | the key used to decrypt the header. |
time_t
representing the time the master password was set. Definition at line 742 of file file.cc.
References YAPET::Crypt::decrypt(), and YAPET::FileHeader::pwset.
Referenced by MainWindow::bottomRightWinContent().
void File::initFile | ( | const Key & | key | ) | throw (YAPETException) [protected] |
Creates a header struct FileHeader
and calls writeHeader
. It then reads and decrypts the header in order to verify.
key | reference to the key used to encrypt the header. |
Definition at line 393 of file file.cc.
References YAPET::FileHeader::control, CONTROL_STR, YAPET::Crypt::decrypt(), YAPET::HEADER_CONTROL_SIZE, YAPET::FileHeader::pwset, and YAPET::FileHeader::version.
bool File::isempty | ( | ) | const throw (YAPETException) [protected] |
time_t File::lastModified | ( | ) | const throw (YAPETException) [private] |
Returns the time of the last modification of the file specified by fd
.
time_t
holdign the last modification date. Definition at line 143 of file file.cc.
References fd.
Referenced by preparePWSave().
void File::openCreate | ( | ) | throw (YAPETException) [private] |
Creates a file with name specified in filename
and sets fd
to the obtained file descriptor. The file is opened for read-write. It is created with only the user having read-write access.
Definition at line 118 of file file.cc.
Referenced by preparePWSave().
void File::openNoCreate | ( | ) | throw (YAPETException) [private] |
const File & File::operator= | ( | const File & | f | ) | throw (YAPETException) |
void File::preparePWSave | ( | ) | throw (YAPETException) [private] |
Truncates the file up to the header by creating a new empty file copying over the existing header.
Definition at line 186 of file file.cc.
References fd, lastModified(), mtime, openCreate(), readHeader(), and writeHeader().
std::list< PartDec > File::read | ( | const Key & | key | ) | const throw (YAPETException) |
Reads the stored records from the file using the key provided and returns a list holding the partially decrypted records. If the file has no records stored, it returns an empty list.
key | the key used to decrypt the records. It has to be same key that was used to encrypt the records, of course. |
BDBuffer * File::read | ( | ) | const throw (YAPETException) [protected] |
Reads from the current position in the file. The method expects the file pointer to point a record length indicator. In other words, it first reads four bytes from the current file position, which will give it a clue about the length of the encrypted record to read.
It then reads as many bytes as indicated from the file and returns the data read in a BDBuffer
. The memory allocated by the BDBuffer
has to be freed by the caller.
The record length indicator needs to be stored in big-endian order.
BDBuffer
holding the encrypted password record. The memory occupied by the buffer has to be freed by the caller of the method. It returns NULL
when the end of file has been reached. Definition at line 281 of file file.cc.
References fd, filename, and uint32_from_disk().
Referenced by MainWindow::changePassword(), readHeader(), seekDataSection(), and setNewKey().
BDBuffer * File::readHeader | ( | ) | const throw (YAPETException) [protected] |
Reads the encrypted header from the file. It also checks whether or not the file is of the expected type by reading the recognition string. If the string read does not match the one predefined, it throws an YAPETException
.
The memory occupied by the BDBuffer
returned has to be freed by the caller.
BDBuffer
holding the encrypted file header. The memory occupied has to be freed by the caller. Definition at line 491 of file file.cc.
References alloca(), fd, read(), RECOG_STR, and seekAbs().
Referenced by preparePWSave().
void File::save | ( | std::list< PartDec > & | records | ) | throw (YAPETException) |
void File::seekAbs | ( | off_t | offset | ) | const throw (YAPETException) [private] |
Seeks offset
bytes from the beginning of the file.
offset | the offset in bytes from the beginning. |
Definition at line 172 of file file.cc.
Referenced by readHeader(), and seekDataSection().
void File::seekCurr | ( | off_t | offset | ) | const throw (YAPETException) [private] |
Seeks offset
bytes from the current position in the file specified by fd
.
offset | the offset in bytes from the current position in the file. |
Definition at line 160 of file file.cc.
Referenced by seekDataSection().
void File::seekDataSection | ( | ) | const throw (YAPETException) [protected] |
Seeks to the first password record in the file. The file pointer is set to point to the record length indicator, so that a call to read()
will return the first password record.
Definition at line 209 of file file.cc.
References fd, read(), RECOG_STR, seekAbs(), seekCurr(), and uint32_from_disk().
void File::setNewKey | ( | const Key & | oldkey, | |
const Key & | newkey | |||
) | throw (YAPETException) |
Uses a new key to encrypt the records in the file. The records stored in the file are decrypted using the old key and then encrypted using the new key.
Before performing this operation, it renames the file encrypted with the old key to 'filename + ".bak"'. It then reads the data from this file and writes it to the newly created file named 'filename'.
oldkey | the old key used to encrypt the records | |
newkey | the new key used to encrypt the records |
Definition at line 678 of file file.cc.
References YAPET::Crypt::decrypt(), YAPET::Crypt::encrypt(), and read().
Referenced by MainWindow::changePassword().
uint32_t YAPET::File::uint32_from_disk | ( | uint32_t | i | ) | const [inline, protected] |
uint32_t File::uint32_from_disk | ( | uint32_t | i | ) | const [protected] |
Converts the length indicator read from the file to the host byte order. The length indicator is always stored in big endian order.
i | the length indicator as read from the file |
Definition at line 258 of file file.cc.
References uint32_to_disk().
Referenced by read(), and seekDataSection().
uint32_t YAPET::File::uint32_to_disk | ( | uint32_t | i | ) | const [inline, protected] |
uint32_t File::uint32_to_disk | ( | uint32_t | i | ) | const [protected] |
Converts the length indicator provided to the big endian byte order, suitable for writing to disk.
i | the length indicator in host byte order |
Definition at line 234 of file file.cc.
References YAPET::File::File::WORD::a, YAPET::File::File::DWORD::a, YAPET::File::File::ENDIAN::abcd, YAPET::File::File::WORD::b, YAPET::File::File::DWORD::b, and YAPET::File::File::ENDIAN::dword.
Referenced by uint32_from_disk().
void File::validateKey | ( | const Key & | key | ) | throw (YAPETException,YAPETInvalidPasswordException) [protected] |
Validates the key provided by reading the file header, decrypting it and comparing the control string of the file header with the string RECOG_STR
. If those operations succeed, the key is considered valid. In case of an error, an exception is thrown.
Since this method relies on readHeader()
, the file type is checked automatically.
key | the key to validate against the file. |
Definition at line 524 of file file.cc.
References YAPET::FileHeader::control, CONTROL_STR, YAPET::Crypt::decrypt(), and YAPET::HEADER_CONTROL_SIZE.
void File::write | ( | const BDBuffer & | buff, | |
bool | forceappend = false , |
|||
bool | forcewrite = false | |||
) | throw (YAPETException, YAPETRetryException) [protected] |
Writes the supplied BDBuffer
to disk. It writes in front of the BDBuffer
its length. The length is stored in big-endian order.
buff | reference to a BDBuffer holding the encrypted data to write | |
forceappend | if this flag is set to true , the method first seeks to the end of the file, if set to false , it writes at the position the file pointer points to. | |
forcewrite | before writing any data, the method checks whether the last modification date stored in mtime matches the date returned by lastModified() . If they differ, and this flag is set to false , the write operation will fail and an exception is thrown. If the flag is set to true , it writes the data to the file regardless of the last modification date. |
YAPETRetryException | if the file has been externally modified (outside of this class), and forcewrite is not true , this exception is thrown. |
void File::writeHeader | ( | const BDBuffer & | enc_header | ) | throw (YAPETException) [protected] |
void File::writeHeader | ( | const Record< FileHeader > & | header, | |
const Key & | key | |||
) | throw (YAPETException) [protected] |
Encrypts and writes the file header provided to the file.
header | a reference to the header. It will be encrypted using the key provided. | |
key | the key used to encrypt the header provided. |
Definition at line 432 of file file.cc.
References YAPET::Crypt::encrypt().
Referenced by preparePWSave().
int YAPET::File::fd [private] |
The file descriptor of the password file.
Definition at line 177 of file file.h.
Referenced by isempty(), lastModified(), openCreate(), openNoCreate(), preparePWSave(), read(), readHeader(), seekDataSection(), and ~File().
std::string YAPET::File::filename [private] |
The file name of the file.
Definition at line 183 of file file.h.
Referenced by getFilename(), openCreate(), openNoCreate(), and read().
time_t YAPET::File::mtime [private] |
Holds the modification time of the file. It has to be updated each time a write occurs.
This is used to detect file modification made outside this class.
Definition at line 193 of file file.h.
Referenced by preparePWSave().