1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
|
#include "encryptionextension.hh"
#include "logger.hh"
/* ********************************************************************************************* *
* Implementation of EncryptionKey
* ********************************************************************************************* */
EncryptionKey::EncryptionKey(QObject *parent)
: ConfigObject(parent)
{
// pass...
}
void
EncryptionKey::clear() {
_key.clear();
}
bool
EncryptionKey::fromHex(const QString &hex, const ErrorStack &err) {
return setKey(QByteArray::fromHex(hex.toLocal8Bit()), err);
}
QString
EncryptionKey::toHex() const {
return _key.toHex();
}
const QByteArray &
EncryptionKey::key() const {
return _key;
}
bool
EncryptionKey::setKey(const QByteArray &key, const ErrorStack &err) {
if (key.isEmpty()) {
errMsg(err) << "Cannot set empty encryption key.";
return false;
}
if (_key == key)
return true;
_key = key;
emit modified(this);
return true;
}
/* ********************************************************************************************* *
* Implementation of BasicEncryptionKey
* ********************************************************************************************* */
BasicEncryptionKey::BasicEncryptionKey(QObject *parent)
: EncryptionKey(parent)
{
// pass...
}
ConfigItem *
BasicEncryptionKey::clone() const {
BasicEncryptionKey *key = new BasicEncryptionKey();
if (! key->copy(*this)) {
key->deleteLater();
return nullptr;
}
return key;
}
YAML::Node
BasicEncryptionKey::serialize(const Context &context, const ErrorStack &err) {
YAML::Node node = EncryptionKey::serialize(context, err);
if (node.IsNull())
return node;
YAML::Node type;
type["dmr"] = node;
return type;
}
bool
BasicEncryptionKey::parse(const YAML::Node &node, Context &ctx, const ErrorStack &err) {
if (! node)
return false;
if ((! node.IsMap()) || (1 != node.size())) {
errMsg(err) << node.Mark().line << ":" << node.Mark().column
<< ": Cannot parse basic encryption key: Expected object with one child.";
return false;
}
YAML::Node key = node.begin()->second;
return EncryptionKey::parse(key, ctx, err);
}
/* ********************************************************************************************* *
* Implementation of ARC4EncryptionKey
* ********************************************************************************************* */
ARC4EncryptionKey::ARC4EncryptionKey(QObject *parent)
: EncryptionKey(parent)
{
// pass...
}
ConfigItem *
ARC4EncryptionKey::clone() const {
ARC4EncryptionKey *key = new ARC4EncryptionKey();
if (! key->copy(*this)) {
key->deleteLater();
return nullptr;
}
return key;
}
bool
ARC4EncryptionKey::fromHex(const QString &hex, const ErrorStack &err) {
if (10 != hex.size()) {
errMsg(err) << "Cannot set RC4 (enhanced) ecryption key to '" << hex << "': Not a 40bit key.";
return false;
}
return EncryptionKey::fromHex(hex);
}
bool
ARC4EncryptionKey::setKey(const QByteArray &key, const ErrorStack &err) {
if (5 != key.size()) {
errMsg(err) << "Cannot set RC4 (enhanced) ecryption key: Not a 40bit key.";
return false;
}
return EncryptionKey::setKey(key, err);
}
YAML::Node
ARC4EncryptionKey::serialize(const Context &context, const ErrorStack &err) {
YAML::Node node = EncryptionKey::serialize(context, err);
if (node.IsNull())
return node;
YAML::Node type;
type["rc4"] = node;
return type;
}
bool
ARC4EncryptionKey::parse(const YAML::Node &node, Context &ctx, const ErrorStack &err) {
if (! node)
return false;
if ((! node.IsMap()) || (1 != node.size())) {
errMsg(err) << node.Mark().line << ":" << node.Mark().column
<< ": Cannot parse basic encryption key: Expected object with one child.";
return false;
}
YAML::Node key = node.begin()->second;
return EncryptionKey::parse(key, ctx, err);
}
/* ********************************************************************************************* *
* Implementation of AESEncryptionKey
* ********************************************************************************************* */
AESEncryptionKey::AESEncryptionKey(QObject *parent)
: EncryptionKey(parent)
{
// pass...
}
ConfigItem *
AESEncryptionKey::clone() const {
AESEncryptionKey *key = new AESEncryptionKey();
if (! key->copy(*this)) {
key->deleteLater();
return nullptr;
}
return key;
}
bool
AESEncryptionKey::setKey(const QByteArray &key, const ErrorStack &err) {
if (16 > key.size()) {
errMsg(err) << "Cannot set AES ecryption key to '" << key.toHex() << "': Key smaller than 128bit.";
return false;
}
return EncryptionKey::setKey(key, err);
}
YAML::Node
AESEncryptionKey::serialize(const Context &context, const ErrorStack &err) {
YAML::Node node = EncryptionKey::serialize(context, err);
if (node.IsNull())
return node;
YAML::Node type;
type["aes"] = node;
return type;
}
bool
AESEncryptionKey::parse(const YAML::Node &node, Context &ctx, const ErrorStack &err) {
if (! node)
return false;
if ((! node.IsMap()) || (1 != node.size())) {
errMsg(err) << node.Mark().line << ":" << node.Mark().column
<< ": Cannot parse enhanced encryption key: Expected object with one child.";
return false;
}
YAML::Node key = node.begin()->second;
return EncryptionKey::parse(key, ctx, err);
}
/* ********************************************************************************************* *
* Implementation of EncryptionKeys
* ********************************************************************************************* */
EncryptionKeys::EncryptionKeys(QObject *parent)
: ConfigObjectList({BasicEncryptionKey::staticMetaObject, ARC4EncryptionKey::staticMetaObject, AESEncryptionKey::staticMetaObject}, parent)
{
// pass...
}
int
EncryptionKeys::add(ConfigObject *obj, int row, bool unique) {
if ((nullptr == obj) || (! obj->is<EncryptionKey>())) {
logError() << "Cannot add nullptr or non-encryption key objects to key list.";
return -1;
}
return ConfigObjectList::add(obj, row, unique);
}
EncryptionKey *
EncryptionKeys::key(int index) const {
if (index >= count())
return nullptr;
return get(index)->as<EncryptionKey>();
}
ConfigItem *
EncryptionKeys::allocateChild(const YAML::Node &node, ConfigItem::Context &ctx, const ErrorStack &err) {
Q_UNUSED(ctx)
if (! node)
return nullptr;
if ((! node.IsMap()) || (1 != node.size())) {
errMsg(err) << node.Mark().line << ":" << node.Mark().column
<< ": Cannot create encryption key: Expected object with one child.";
return nullptr;
}
QString type = QString::fromStdString(node.begin()->first.as<std::string>());
if (("basic" == type) || ("dmr" == type)) {
return new BasicEncryptionKey();
} else if ("rc4" == type) {
return new ARC4EncryptionKey();
} else if ("aes" == type) {
return new AESEncryptionKey();
}
errMsg(err) << node.Mark().line << ":" << node.Mark().column
<< ": Cannot create encryption key: Unknown type '" << type << "'.";
return nullptr;
}
|