|
| 1 | +/* |
| 2 | + * Copyright (C) 2019 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 <QApplication> |
| 19 | +#include <QString> |
| 20 | + |
| 21 | +#include "Database.h" |
| 22 | +#include "Entry.h" |
| 23 | +#include "Group.h" |
| 24 | +#include "PasswordHealth.h" |
| 25 | +#include "zxcvbn.h" |
| 26 | + |
| 27 | +PasswordHealth::PasswordHealth(double entropy) |
| 28 | + : m_entropy(entropy) |
| 29 | + , m_score(entropy) |
| 30 | +{ |
| 31 | + switch (quality()) { |
| 32 | + case Quality::bad: |
| 33 | + case Quality::poor: |
| 34 | + m_reason = QApplication::tr("Very weak password"); |
| 35 | + m_details = QApplication::tr("Password entropy is %1 bit").arg(QString::number(m_entropy, 'f', 2)); |
| 36 | + break; |
| 37 | + |
| 38 | + case Quality::weak: |
| 39 | + m_reason = QApplication::tr("Weak password"); |
| 40 | + m_details = QApplication::tr("Password entropy is %1 bit").arg(QString::number(m_entropy, 'f', 2)); |
| 41 | + break; |
| 42 | + |
| 43 | + case Quality::good: |
| 44 | + case Quality::excellent: |
| 45 | + // Reasons are essentially error messages; if there's nothing |
| 46 | + // to complain, leave it empty. |
| 47 | + break; |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +PasswordHealth::PasswordHealth(QString pwd) |
| 52 | + : PasswordHealth(ZxcvbnMatch(pwd.toLatin1(), nullptr, nullptr)) |
| 53 | +{ |
| 54 | +} |
| 55 | + |
| 56 | +PasswordHealth::PasswordHealth(QSharedPointer<Database> db, QString pwd, Cache* cache) |
| 57 | + : PasswordHealth(ZxcvbnMatch(pwd.toLatin1(), nullptr, nullptr)) |
| 58 | +{ |
| 59 | + // Whenever the password is re-used, reduce score by |
| 60 | + // this many points: |
| 61 | + constexpr auto penalty = 15; |
| 62 | + |
| 63 | + if (!db || !db->rootGroup()) { |
| 64 | + return; |
| 65 | + } |
| 66 | + |
| 67 | + // Set up the cache if not yet done (and use our own cache if |
| 68 | + // the caller didn't give us one) |
| 69 | + Cache mycache; |
| 70 | + if (!cache) { |
| 71 | + cache = &mycache; |
| 72 | + } |
| 73 | + if (cache->isEmpty()) { |
| 74 | + const auto groups = db->rootGroup()->groupsRecursive(true); |
| 75 | + for (const auto* group : groups) { |
| 76 | + if (!group->isRecycled()) { |
| 77 | + for (const auto* entry : group->entries()) { |
| 78 | + if (!entry->isRecycled()) { |
| 79 | + (*cache)[entry->password()] |
| 80 | + << QApplication::tr("Used in %1/%2").arg(group->hierarchy().join('/'), entry->title()); |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + // If the password is in the database more than once, |
| 88 | + // reduce the score accordingly |
| 89 | + const auto& used = (*cache)[pwd]; |
| 90 | + const auto count = used.size(); |
| 91 | + if (count > 1) { |
| 92 | + m_score -= penalty * (count - 1); |
| 93 | + addTo(m_reason, QApplication::tr("Password is used %1 times").arg(QString::number(count))); |
| 94 | + addTo(m_details, used.join('\n')); |
| 95 | + |
| 96 | + // Don't allow re-used passwords to be considered "good" |
| 97 | + // no matter how great their entropy is. |
| 98 | + if (m_score > 64) { |
| 99 | + m_score = 64; |
| 100 | + } |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +PasswordHealth::PasswordHealth(QSharedPointer<Database> db, const Entry& entry, Cache* cache) |
| 105 | + : PasswordHealth(db, entry.password(), cache) |
| 106 | +{ |
| 107 | + // If the password has already expired, reduce score to 0. |
| 108 | + // Else, if the password is going to expire in the next |
| 109 | + // 30 days, reduce score by 2 points per day. |
| 110 | + if (entry.isExpired()) { |
| 111 | + m_score = 0; |
| 112 | + addTo(m_reason, QApplication::tr("Password has expired")); |
| 113 | + addTo(m_details, |
| 114 | + QApplication::tr("Password expiry was %1") |
| 115 | + .arg(entry.timeInfo().expiryTime().toString(Qt::DefaultLocaleShortDate))); |
| 116 | + } else if (entry.timeInfo().expires()) { |
| 117 | + const auto days = QDateTime::currentDateTime().daysTo(entry.timeInfo().expiryTime()); |
| 118 | + if (days <= 30) { |
| 119 | + // First bring the score down into the "weak" range |
| 120 | + // so that the entry appears in Health Check. Then |
| 121 | + // reduce the score by 2 points for every day that |
| 122 | + // we get closer to expiry. days<=0 has already |
| 123 | + // been handled above ("isExpired()"). |
| 124 | + if (m_score > 60) { |
| 125 | + m_score = 60; |
| 126 | + } |
| 127 | + m_score -= (30 - days) * 2; |
| 128 | + addTo(m_reason, |
| 129 | + days <= 2 ? QApplication::tr("Password is about to expire") |
| 130 | + : days <= 10 ? QApplication::tr("Password expires in %1 days").arg(days) |
| 131 | + : QApplication::tr("Password will expire soon")); |
| 132 | + addTo(m_details, |
| 133 | + QApplication::tr("Password expires on %1") |
| 134 | + .arg(entry.timeInfo().expiryTime().toString(Qt::DefaultLocaleShortDate))); |
| 135 | + } |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +void PasswordHealth::addTo(QString& to, QString newText) |
| 140 | +{ |
| 141 | + if (!to.isEmpty()) { |
| 142 | + to += '\n'; |
| 143 | + } |
| 144 | + to += newText; |
| 145 | +} |
| 146 | + |
| 147 | +PasswordHealth::Quality PasswordHealth::quality() const |
| 148 | +{ |
| 149 | + const auto s = score(); |
| 150 | + return s <= 0 ? Quality::bad |
| 151 | + : s < 40 ? Quality::poor : s < 65 ? Quality::weak : s < 100 ? Quality::good : Quality::excellent; |
| 152 | +} |
| 153 | + |
| 154 | +QColor PasswordHealth::color() const |
| 155 | +{ |
| 156 | + switch (quality()) { |
| 157 | + case Quality::bad: |
| 158 | + return QColor("red"); |
| 159 | + |
| 160 | + case Quality::poor: |
| 161 | + return QColor("orange"); |
| 162 | + |
| 163 | + case Quality::weak: |
| 164 | + return QColor("yellow"); |
| 165 | + |
| 166 | + case Quality::good: |
| 167 | + return QColor("green"); |
| 168 | + |
| 169 | + case Quality::excellent: |
| 170 | + return QColor("green"); |
| 171 | + } |
| 172 | + |
| 173 | + // Unreachable. Using "return" here instead of "default" |
| 174 | + // above because we want a compiler warning when someone |
| 175 | + // adds new enum values. |
| 176 | + return QColor(); |
| 177 | +} |
0 commit comments