keepassxc

Форк
0
/
KeePass2Reader.cpp 
135 строк · 3.9 Кб
1
/*
2
 *  Copyright (C) 2017 KeePassXC Team <team@keepassxc.org>
3
 *
4
 *  This program is free software: you can redistribute it and/or modify
5
 *  it under the terms of the GNU General Public License as published by
6
 *  the Free Software Foundation, either version 2 or (at your option)
7
 *  version 3 of the License.
8
 *
9
 *  This program is distributed in the hope that it will be useful,
10
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 *  GNU General Public License for more details.
13
 *
14
 *  You should have received a copy of the GNU General Public License
15
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
 */
17

18
#include "format/KeePass2Reader.h"
19
#include "format/Kdbx3Reader.h"
20
#include "format/Kdbx4Reader.h"
21
#include "format/KeePass1.h"
22
#include "keys/CompositeKey.h"
23

24
#include <QFile>
25

26
/**
27
 * Read database from file and detect correct file format.
28
 *
29
 * @param filename input file
30
 * @param key database encryption composite key
31
 * @param db Database to read into
32
 * @return true on success
33
 */
34
bool KeePass2Reader::readDatabase(const QString& filename, QSharedPointer<const CompositeKey> key, Database* db)
35
{
36
    QFile file(filename);
37
    if (!file.open(QFile::ReadOnly)) {
38
        raiseError(file.errorString());
39
        return false;
40
    }
41

42
    bool ok = readDatabase(&file, std::move(key), db);
43

44
    if (file.error() != QFile::NoError) {
45
        raiseError(file.errorString());
46
        return false;
47
    }
48

49
    return ok;
50
}
51

52
/**
53
 * Read database from device and detect correct file format.
54
 *
55
 * @param device input device
56
 * @param key database encryption composite key
57
 * @param db Database to read into
58
 * @return true on success
59
 */
60
bool KeePass2Reader::readDatabase(QIODevice* device, QSharedPointer<const CompositeKey> key, Database* db)
61
{
62
    m_error = false;
63
    m_errorStr.clear();
64

65
    quint32 signature1, signature2;
66
    bool ok = KdbxReader::readMagicNumbers(device, signature1, signature2, m_version);
67

68
    if (!ok) {
69
        raiseError(tr("Failed to read database file."));
70
        return false;
71
    }
72

73
    if (signature1 == KeePass1::SIGNATURE_1 && signature2 == KeePass1::SIGNATURE_2) {
74
        raiseError(tr("The selected file is an old KeePass 1 database (.kdb).\n\n"
75
                      "You can import it by clicking on Database > 'Import KeePass 1 database…'.\n"
76
                      "This is a one-way migration. You won't be able to open the imported "
77
                      "database with the old KeePassX 0.4 version."));
78
        return false;
79
    } else if (!(signature1 == KeePass2::SIGNATURE_1 && signature2 == KeePass2::SIGNATURE_2)) {
80
        raiseError(tr("Not a KeePass database."));
81
        return false;
82
    }
83

84
    if (m_version < KeePass2::FILE_VERSION_MIN
85
        || (m_version & KeePass2::FILE_VERSION_CRITICAL_MASK) > KeePass2::FILE_VERSION_MAX) {
86
        raiseError(tr("Unsupported KeePass 2 database version."));
87
        return false;
88
    }
89

90
    // determine file format (KDBX 2/3 or 4)
91
    if (m_version < KeePass2::FILE_VERSION_4) {
92
        m_reader.reset(new Kdbx3Reader());
93
    } else {
94
        m_reader.reset(new Kdbx4Reader());
95
    }
96

97
    return m_reader->readDatabase(device, std::move(key), db);
98
}
99

100
bool KeePass2Reader::hasError() const
101
{
102
    return m_error || (!m_reader.isNull() && m_reader->hasError());
103
}
104

105
QString KeePass2Reader::errorString() const
106
{
107
    return !m_reader.isNull() ? m_reader->errorString() : m_errorStr;
108
}
109

110
/**
111
 * @return detected KDBX version
112
 */
113
quint32 KeePass2Reader::version() const
114
{
115
    return m_version;
116
}
117

118
/**
119
 * @return KDBX reader used for reading the input file
120
 */
121
QSharedPointer<KdbxReader> KeePass2Reader::reader() const
122
{
123
    return m_reader;
124
}
125

126
/**
127
 * Raise an error. Use in case of an unexpected read error.
128
 *
129
 * @param errorMessage error message
130
 */
131
void KeePass2Reader::raiseError(const QString& errorMessage)
132
{
133
    m_error = true;
134
    m_errorStr = errorMessage;
135
}
136

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.