Initial commit
[keepassx] / src / lib / SecString.h
1 /***************************************************************************
2  *   Copyright (C) 2005 by Tarek Saidi                                     *
3  *   tarek@linux                                                           *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; version 2 of the License.               *
8
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20 #ifndef _SECSTRING_H_
21 #define _SECSTRING_H_
22
23 #include "crypto/arcfour.h"
24
25 class SecData;
26
27 //! QString based class with in-memory encryption of its content.
28 /*!
29 This class can hold a QString object in an encrypted buffer. To get access to the string it is neccassary to unlock the SecString object.
30  */
31 class SecString{
32
33 friend class SecData;
34
35 public:
36         SecString();
37         ~SecString();
38         /*! Sets the content of the object.
39                 The SecString is locked after this operation.
40                 \param Source The string which should be set as content of the SecString.
41                 \param DelSrc Set this parameter TRUE if you want that SecString overwrites an deletes the source string.*/
42         void setString(QString& Source, bool DelSrc=false);
43         /*! Locks the string.
44                 That means that the unencrypted string will be overwritten and deleted and only the encrypted buffer remains.
45                 It is forbidden to call the function string() when the SecString is locked.*/
46         void lock();
47         void unlock();
48         const QString& string();
49         operator QString();
50         int length();
51         
52         static void overwrite(unsigned char* str,int len);
53         static void overwrite(QString& str);
54         static void generateSessionKey();
55         static void deleteSessionKey();
56         
57 private:
58         static CArcFour RC4;
59         static quint8* sessionkey;
60         bool locked;
61         QByteArray crypt;
62         QString plain;
63
64 };
65
66 class SecData{
67         public:
68                 SecData(int len);
69                 ~SecData();
70                 void lock();
71                 void unlock();
72                 void copyData(quint8* src);
73                 void copyData(SecData& secData);
74                 quint8* operator*();
75         
76         private:
77                 quint8* data;
78                 int length;
79                 bool locked;
80 };
81
82
83 #endif