00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifndef SIGNON_ENCRYPTEDDEVICE_H
00025 #define SIGNON_ENCRYPTEDDEVICE_H
00026
00027 #include <QIODevice>
00028 #include <QByteArray>
00029 #include <openssl/aes.h>
00030
00031 namespace SignOn {
00032
00040 class EncryptedDevice : public QIODevice
00041 {
00042 public:
00056 EncryptedDevice(QIODevice *actualDevice,
00057 const unsigned char *encryptionKey, unsigned int keySize,
00058 const unsigned char *ivOn, const unsigned char *ivOut);
00059
00060 virtual bool isSequential () const { return true; }
00061
00062 virtual bool open(OpenMode mode);
00063 virtual void close();
00064
00065 virtual qint64 bytesAvailable() const;
00066 virtual qint64 bytesToWrite() const;
00067
00072 void setTemporaryDataSource(QByteArray *tmp) { m_tempByteArray = tmp; m_tempByteArrayPos = 0; }
00073 void clearTemporaryDataSource() { m_tempByteArray = NULL; }
00074
00075 protected:
00076 virtual qint64 readData(char *data, qint64 maxLen);
00077 virtual qint64 writeData(const char *data, qint64 len);
00078
00079 private:
00080 Q_DISABLE_COPY(EncryptedDevice);
00081
00082 QIODevice *m_actualDevice;
00083 unsigned char m_keyStreamOut[AES_BLOCK_SIZE];
00084 unsigned int m_currentPosOut;
00085 unsigned char m_keyStreamIn[AES_BLOCK_SIZE];
00086 unsigned int m_currentPosIn;
00087 AES_KEY m_encryptionKey;
00088 QByteArray *m_tempByteArray;
00089 int m_tempByteArrayPos;
00090 bool m_valid;
00091 };
00092
00093 class TemporaryEncryptedDataSourceSetter
00094 {
00095 public:
00096 TemporaryEncryptedDataSourceSetter(EncryptedDevice *dev, QByteArray *arr) : m_dev(dev) {
00097 m_dev->setTemporaryDataSource(arr);
00098 }
00099 ~TemporaryEncryptedDataSourceSetter() {
00100 m_dev->clearTemporaryDataSource();
00101 }
00102 private:
00103 EncryptedDevice *m_dev;
00104 };
00105
00106 }
00107
00108 #endif // SIGNON_ENCRYPTEDDEVICE_H