Catch and handle all errors from libgcrypt.

This commit is contained in:
Felix Geyer 2015-05-09 19:47:53 +02:00
parent a7f4e2d0cd
commit a762cef0a9
29 changed files with 622 additions and 194 deletions

View file

@ -34,11 +34,6 @@ bool LayeredStream::isSequential() const
return true;
}
QString LayeredStream::errorString() const
{
return m_baseDevice->errorString();
}
bool LayeredStream::open(QIODevice::OpenMode mode)
{
if (isOpen()) {

View file

@ -31,7 +31,6 @@ public:
virtual ~LayeredStream();
bool isSequential() const Q_DECL_OVERRIDE;
virtual QString errorString() const;
bool open(QIODevice::OpenMode mode) Q_DECL_OVERRIDE;
protected:

View file

@ -41,6 +41,10 @@ QByteArray StoreDataStream::storedData() const
qint64 StoreDataStream::readData(char* data, qint64 maxSize)
{
qint64 bytesRead = LayeredStream::readData(data, maxSize);
if (bytesRead == -1) {
setErrorString(m_baseDevice->errorString());
return -1;
}
m_storedData.append(data, bytesRead);

View file

@ -18,10 +18,9 @@
#include "SymmetricCipherStream.h"
SymmetricCipherStream::SymmetricCipherStream(QIODevice* baseDevice, SymmetricCipher::Algorithm algo,
SymmetricCipher::Mode mode, SymmetricCipher::Direction direction,
const QByteArray& key, const QByteArray& iv)
SymmetricCipher::Mode mode, SymmetricCipher::Direction direction)
: LayeredStream(baseDevice)
, m_cipher(new SymmetricCipher(algo, mode, direction, key, iv))
, m_cipher(new SymmetricCipher(algo, mode, direction))
, m_bufferPos(0)
, m_bufferFilling(false)
, m_error(false)
@ -33,6 +32,25 @@ SymmetricCipherStream::~SymmetricCipherStream()
close();
}
bool SymmetricCipherStream::init(const QByteArray& key, const QByteArray& iv)
{
m_isInitalized = m_cipher->init(key, iv);
if (!m_isInitalized) {
setErrorString(m_cipher->errorString());
}
return m_isInitalized;
}
bool SymmetricCipherStream::open(QIODevice::OpenMode mode)
{
if (!m_isInitalized) {
return false;
}
return LayeredStream::open(mode);
}
bool SymmetricCipherStream::reset()
{
if (isWritable()) {
@ -108,7 +126,11 @@ bool SymmetricCipherStream::readBlock()
return false;
}
else {
m_cipher->processInPlace(m_buffer);
if (!m_cipher->processInPlace(m_buffer)) {
m_error = true;
setErrorString(m_cipher->errorString());
return false;
}
m_bufferPos = 0;
m_bufferFilling = false;
@ -187,7 +209,11 @@ bool SymmetricCipherStream::writeBlock(bool lastBlock)
return true;
}
m_cipher->processInPlace(m_buffer);
if (!m_cipher->processInPlace(m_buffer)) {
m_error = true;
setErrorString(m_cipher->errorString());
return false;
}
if (m_baseDevice->write(m_buffer) != m_buffer.size()) {
m_error = true;

View file

@ -29,11 +29,13 @@ class SymmetricCipherStream : public LayeredStream
Q_OBJECT
public:
SymmetricCipherStream(QIODevice* baseDevice, SymmetricCipher::Algorithm algo, SymmetricCipher::Mode mode,
SymmetricCipher::Direction direction, const QByteArray& key, const QByteArray& iv);
SymmetricCipherStream(QIODevice* baseDevice, SymmetricCipher::Algorithm algo,
SymmetricCipher::Mode mode, SymmetricCipher::Direction direction);
~SymmetricCipherStream();
bool reset();
void close();
bool init(const QByteArray& key, const QByteArray& iv);
bool open(QIODevice::OpenMode mode) Q_DECL_OVERRIDE;
bool reset() Q_DECL_OVERRIDE;
void close() Q_DECL_OVERRIDE;
protected:
qint64 readData(char* data, qint64 maxSize) Q_DECL_OVERRIDE;
@ -48,6 +50,7 @@ private:
int m_bufferPos;
bool m_bufferFilling;
bool m_error;
bool m_isInitalized;
};
#endif // KEEPASSX_SYMMETRICCIPHERSTREAM_H