keepassxc/src/keys/CompositeKey.cpp

124 lines
2.9 KiB
C++
Raw Normal View History

/*
* Copyright (C) 2010 Felix Geyer <debfx@fobos.de>
2017-06-09 23:40:36 +02:00
* Copyright (C) 2017 KeePassXC Team <team@keepassxc.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 or (at your option)
* version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "CompositeKey.h"
2017-01-14 13:25:30 -05:00
#include <QFile>
#include <QtConcurrent>
2012-04-18 22:08:22 +02:00
#include "core/Global.h"
#include "crypto/CryptoHash.h"
CompositeKey::CompositeKey()
{
}
CompositeKey::CompositeKey(const CompositeKey& key)
{
*this = key;
}
CompositeKey::~CompositeKey()
{
clear();
}
2012-01-11 23:59:50 +01:00
void CompositeKey::clear()
{
qDeleteAll(m_keys);
m_keys.clear();
m_challengeResponseKeys.clear();
2012-01-11 23:59:50 +01:00
}
2012-10-12 12:10:13 +02:00
bool CompositeKey::isEmpty() const
{
return m_keys.isEmpty() && m_challengeResponseKeys.isEmpty();
2012-10-12 12:10:13 +02:00
}
CompositeKey* CompositeKey::clone() const
{
return new CompositeKey(*this);
}
CompositeKey& CompositeKey::operator=(const CompositeKey& key)
{
2012-07-17 10:47:56 +02:00
// handle self assignment as that would break when calling clear()
if (this == &key) {
return *this;
}
clear();
for (const Key* subKey : asConst(key.m_keys)) {
addKey(*subKey);
}
for (const auto subKey : asConst(key.m_challengeResponseKeys)) {
addChallengeResponseKey(subKey);
}
return *this;
}
QByteArray CompositeKey::rawKey() const
{
CryptoHash cryptoHash(CryptoHash::Sha256);
for (const Key* key : m_keys) {
cryptoHash.addData(key->rawKey());
}
return cryptoHash.result();
}
bool CompositeKey::transform(const Kdf& kdf, QByteArray& result) const
{
return kdf.transform(rawKey(), result);
}
bool CompositeKey::challenge(const QByteArray& seed, QByteArray& result) const
{
// if no challenge response was requested, return nothing to
// maintain backwards compatibility with regular databases.
if (m_challengeResponseKeys.length() == 0) {
result.clear();
return true;
}
CryptoHash cryptoHash(CryptoHash::Sha256);
for (const auto key : m_challengeResponseKeys) {
// if the device isn't present or fails, return an error
if (!key->challenge(seed)) {
return false;
}
cryptoHash.addData(key->rawKey());
}
result = cryptoHash.result();
return true;
}
void CompositeKey::addKey(const Key& key)
{
m_keys.append(key.clone());
}
void CompositeKey::addChallengeResponseKey(QSharedPointer<ChallengeResponseKey> key)
{
m_challengeResponseKeys.append(key);
}