encrypteddevice.h

00001 /*
00002  * This file is part of signon
00003  *
00004  * Copyright (C) 2009-2011 Nokia Corporation.
00005  *
00006  * Contact: Rauli Ikonen <rauli.ikonen@nixuopen.org>
00007  * Contact: Alberto Mardegan <alberto.mardegan@nokia.com>
00008  *
00009  * This library is free software; you can redistribute it and/or
00010  * modify it under the terms of the GNU Lesser General Public License
00011  * version 2.1 as published by the Free Software Foundation.
00012  *
00013  * This library is distributed in the hope that it will be useful, but
00014  * WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00016  * Lesser General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU Lesser General Public
00019  * License along with this library; if not, write to the Free Software
00020  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
00021  * 02110-1301 USA
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