File indexing completed on 2024-05-26 16:15:28

0001 /* This file is part of the KDE project
0002    Copyright (C) 2006 Thomas Schaap <thomas.schaap@kdemail.net>
0003 
0004    This library is free software; you can redistribute it and/or
0005    modify it under the terms of the GNU Library General Public
0006    License as published by the Free Software Foundation; either
0007    version 2 of the License, or (at your option) any later version.
0008 
0009    This library is distributed in the hope that it will be useful,
0010    but WITHOUT ANY WARRANTY; without even the implied warranty of
0011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012    Library General Public License for more details.
0013 
0014    You should have received a copy of the GNU Library General Public License
0015    along with this library; see the file COPYING.LIB.  If not, write to
0016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017    Boston, MA 02110-1301, USA.
0018 */
0019 
0020 #ifndef KoEncryptedStore_h
0021 #define KoEncryptedStore_h
0022 #ifdef QCA2
0023 #include "KoStore.h"
0024 #include <QHash>
0025 // QCA headers have "slots" and "signals", which QT_NO_SIGNALS_SLOTS_KEYWORDS does not like
0026 #define slots Q_SLOTS
0027 #define signals Q_SIGNALS
0028 #include <QtCrypto>
0029 #undef slots
0030 #undef signals
0031 
0032 class QString;
0033 class QByteArray;
0034 class QIODevice;
0035 class QWidget;
0036 class QUrl;
0037 class KZip;
0038 class KArchiveDirectory;
0039 class QTemporaryFile;
0040 struct KoEncryptedStore_EncryptionData;
0041 
0042 class KoEncryptedStore : public KoStore
0043 {
0044 public:
0045     KoEncryptedStore(const QString &filename, Mode mode, const QByteArray &appIdentification,
0046                      bool writeMimetype);
0047     KoEncryptedStore(QIODevice *dev, Mode mode, const QByteArray &appIdentification,
0048                      bool writeMimetype);
0049     KoEncryptedStore(QWidget *window, const QUrl &url, const QString &filename, Mode mode,
0050                      const QByteArray &appIdentification, bool writeMimetype);
0051     ~KoEncryptedStore();
0052 
0053     /*
0054      * Sets the password to be used for decryption or encryption of the file.
0055      *
0056      * This method only works if no password has been set or found yet,
0057      * i.e. when no file has been opened yet and this method hasn't been used yet.
0058      *
0059      * @param   password    A non-empty password.
0060      *
0061      * @return  True if the password was set
0062      */
0063     bool setPassword(const QString &password) override;
0064 
0065     /*
0066      * Returns whether a store opened for reading is actually encrypted.
0067      * This function will always return true in Write-mode.
0068      *
0069      * @return  True if the store is encrypted.
0070      */
0071     bool isEncrypted() override;
0072 
0073     QStringList directoryList() const override;
0074 
0075 protected:
0076 
0077     void init(const QByteArray &appIdentification);
0078     bool doFinalize() override;
0079     bool openWrite(const QString &name) override;
0080     bool openRead(const QString &name) override;
0081     bool closeWrite() override;
0082     bool closeRead() override;
0083     bool enterRelativeDirectory(const QString &dirName) override;
0084     bool enterAbsoluteDirectory(const QString &path) override;
0085     bool fileExists(const QString &absPath) const override;
0086 
0087     /**
0088      * Tries and find a password for this document in KWallet.
0089      * Uses m_filename as base for finding the password and stores it in m_password if found.
0090      */
0091     void findPasswordInKWallet();
0092 
0093     /*
0094      * Retrieves the password used to encrypt or decrypt the store. Note that
0095      * QString() will returned if no password has been given or the store is
0096      * not encrypted.
0097      *
0098      * @return  The password this store is encrypted with.
0099      */
0100     QString password() override;
0101 
0102     /**
0103      * Stores the password for this document in KWallet.
0104      * Uses m_filename as base for storing the password and stores the value in m_password.
0105      */
0106     void savePasswordInKWallet();
0107 
0108 private:
0109     QCA::SecureArray decryptFile(QCA::SecureArray & encryptedFile, KoEncryptedStore_EncryptionData & encData, QCA::SecureArray & password);
0110 
0111     /** returns true if the file should be encrypted, false otherwise **/
0112     bool isToBeEncrypted(const QString &fullpath);
0113 
0114 protected:
0115     QCA::Initializer m_qcaInit;
0116     QHash<QString, KoEncryptedStore_EncryptionData> m_encryptionData;
0117     QCA::SecureArray m_password;
0118     QString m_filename;
0119     QByteArray m_manifestBuffer;
0120     KZip *m_pZip;
0121     QTemporaryFile *m_tempFile;
0122     bool m_bPasswordUsed;
0123     bool m_bPasswordDeclined;
0124 
0125     /** In "Read" mode this pointer is pointing to the
0126     current directory in the archive to speed up the verification process */
0127     const KArchiveDirectory *m_currentDir;
0128 private:
0129     Q_DECLARE_PRIVATE(KoStore)
0130 };
0131 #endif
0132 #endif