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 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523
|
/*
* Copyright (C) 2006 Brad Hards <bradh@frogmouth.net>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*
*/
#include "hasht.h"
#include "nss.h"
#include "pk11func.h"
#include <QtCrypto>
#include <QDebug>
#include <QStringList>
#include <QtPlugin>
//-----------------------------------------------------------
class nssHashContext : public QCA::HashContext
{
Q_OBJECT
public:
nssHashContext(QCA::Provider *p, const QString &type)
: QCA::HashContext(p, type)
{
SECStatus s;
NSS_NoDB_Init(".");
m_status = 0;
/* Get a slot to use for the crypto operations */
m_slot = PK11_GetInternalKeySlot();
if (!m_slot) {
qDebug() << "GetInternalKeySlot failed";
m_status = 1;
return;
}
if (QLatin1String("md2") == type) {
m_hashAlgo = SEC_OID_MD2;
} else if (QLatin1String("md5") == type) {
m_hashAlgo = SEC_OID_MD5;
} else if (QLatin1String("sha1") == type) {
m_hashAlgo = SEC_OID_SHA1;
} else if (QLatin1String("sha256") == type) {
m_hashAlgo = SEC_OID_SHA256;
} else if (QLatin1String("sha384") == type) {
m_hashAlgo = SEC_OID_SHA384;
} else if (QLatin1String("sha512") == type) {
m_hashAlgo = SEC_OID_SHA512;
} else {
qDebug() << "Unknown provider type: " << type;
return; /* this will probably cause a segfault... */
}
m_context = PK11_CreateDigestContext(m_hashAlgo);
if (!m_context) {
qDebug() << "CreateDigestContext failed";
return;
}
s = PK11_DigestBegin(m_context);
if (s != SECSuccess) {
qDebug() << "DigestBegin failed";
return;
}
}
~nssHashContext() override
{
PK11_DestroyContext(m_context, PR_TRUE);
if (m_slot)
PK11_FreeSlot(m_slot);
}
Context *clone() const override
{
return new nssHashContext(provider(), type());
}
void clear() override
{
SECStatus s;
PK11_DestroyContext(m_context, PR_TRUE);
m_context = PK11_CreateDigestContext(m_hashAlgo);
if (!m_context) {
qDebug() << "CreateDigestContext failed";
return;
}
s = PK11_DigestBegin(m_context);
if (s != SECSuccess) {
qDebug() << "DigestBegin failed";
return;
}
}
void update(const QCA::MemoryRegion &a) override
{
PK11_DigestOp(m_context, (const unsigned char *)a.data(), a.size());
}
QCA::MemoryRegion final() override
{
unsigned int len = 0;
QCA::SecureArray a(64);
PK11_DigestFinal(m_context, (unsigned char *)a.data(), &len, a.size());
a.resize(len);
return a;
}
private:
PK11SlotInfo *m_slot;
int m_status;
PK11Context *m_context;
SECOidTag m_hashAlgo;
};
//-----------------------------------------------------------
class nssHmacContext : public QCA::MACContext
{
Q_OBJECT
public:
nssHmacContext(QCA::Provider *p, const QString &type)
: QCA::MACContext(p, type)
{
NSS_NoDB_Init(".");
m_context = nullptr;
m_status = 0;
/* Get a slot to use for the crypto operations */
m_slot = PK11_GetInternalKeySlot();
if (!m_slot) {
qDebug() << "GetInternalKeySlot failed";
m_status = 1;
return;
}
if (QLatin1String("hmac(md5)") == type) {
m_macAlgo = CKM_MD5_HMAC;
} else if (QLatin1String("hmac(sha1)") == type) {
m_macAlgo = CKM_SHA_1_HMAC;
} else if (QLatin1String("hmac(sha256)") == type) {
m_macAlgo = CKM_SHA256_HMAC;
} else if (QLatin1String("hmac(sha384)") == type) {
m_macAlgo = CKM_SHA384_HMAC;
} else if (QLatin1String("hmac(sha512)") == type) {
m_macAlgo = CKM_SHA512_HMAC;
} else if (QLatin1String("hmac(ripemd160)") == type) {
m_macAlgo = CKM_RIPEMD160_HMAC;
} else {
qDebug() << "Unknown provider type: " << type;
return; /* this will probably cause a segfault... */
}
}
~nssHmacContext() override
{
if (m_context)
PK11_DestroyContext(m_context, PR_TRUE);
if (m_slot)
PK11_FreeSlot(m_slot);
}
Context *clone() const override
{
return new nssHmacContext(provider(), type());
}
void clear()
{
PK11_DestroyContext(m_context, PR_TRUE);
SECItem noParams;
noParams.data = nullptr;
noParams.len = 0;
m_context = PK11_CreateContextBySymKey(m_macAlgo, CKA_SIGN, m_nssKey, &noParams);
if (!m_context) {
qDebug() << "CreateContextBySymKey failed";
return;
}
SECStatus s = PK11_DigestBegin(m_context);
if (s != SECSuccess) {
qDebug() << "DigestBegin failed";
return;
}
}
QCA::KeyLength keyLength() const override
{
return anyKeyLength();
}
void setup(const QCA::SymmetricKey &key) override
{
/* turn the raw key into a SECItem */
SECItem keyItem;
keyItem.data = (unsigned char *)key.data();
keyItem.len = key.size();
m_nssKey = PK11_ImportSymKey(m_slot, m_macAlgo, PK11_OriginUnwrap, CKA_SIGN, &keyItem, nullptr);
SECItem noParams;
noParams.data = nullptr;
noParams.len = 0;
m_context = PK11_CreateContextBySymKey(m_macAlgo, CKA_SIGN, m_nssKey, &noParams);
if (!m_context) {
qDebug() << "CreateContextBySymKey failed";
return;
}
SECStatus s = PK11_DigestBegin(m_context);
if (s != SECSuccess) {
qDebug() << "DigestBegin failed";
return;
}
}
void update(const QCA::MemoryRegion &a) override
{
PK11_DigestOp(m_context, (const unsigned char *)a.data(), a.size());
}
void final(QCA::MemoryRegion *out) override
{
// NSS doesn't appear to be able to tell us how big the digest will
// be for a given algorithm until after we finalise it, so we work
// around the problem a bit.
QCA::SecureArray sa(HASH_LENGTH_MAX, 0); // assume the biggest hash size we know
unsigned int len = 0;
PK11_DigestFinal(m_context, (unsigned char *)sa.data(), &len, sa.size());
sa.resize(len); // and fix it up later
*out = sa;
}
private:
PK11SlotInfo *m_slot;
int m_status;
PK11Context *m_context;
CK_MECHANISM_TYPE m_macAlgo;
PK11SymKey *m_nssKey;
};
//-----------------------------------------------------------
class nssCipherContext : public QCA::CipherContext
{
Q_OBJECT
public:
nssCipherContext(QCA::Provider *p, const QString &type)
: QCA::CipherContext(p, type)
{
NSS_NoDB_Init(".");
if (QLatin1String("aes128-ecb") == type) {
m_cipherMechanism = CKM_AES_ECB;
} else if (QLatin1String("aes128-cbc") == type) {
m_cipherMechanism = CKM_AES_CBC;
} else if (QLatin1String("des-ecb") == type) {
m_cipherMechanism = CKM_DES_ECB;
} else if (QLatin1String("des-cbc") == type) {
m_cipherMechanism = CKM_DES_CBC;
} else if (QLatin1String("des-cbc-pkcs7") == type) {
m_cipherMechanism = CKM_DES_CBC_PAD;
} else if (QLatin1String("tripledes-ecb") == type) {
m_cipherMechanism = CKM_DES3_ECB;
} else {
qDebug() << "Unknown provider type: " << type;
return; /* this will probably cause a segfault... */
}
}
~nssCipherContext() override
{
}
void setup(QCA::Direction dir,
const QCA::SymmetricKey &key,
const QCA::InitializationVector &iv,
const QCA::AuthTag &tag) override
{
Q_UNUSED(tag);
/* Get a slot to use for the crypto operations */
m_slot = PK11_GetBestSlot(m_cipherMechanism, nullptr);
if (!m_slot) {
qDebug() << "GetBestSlot failed";
return;
}
/* turn the raw key into a SECItem */
SECItem keyItem;
keyItem.data = (unsigned char *)key.data();
keyItem.len = key.size();
if (QCA::Encode == dir) {
m_nssKey = PK11_ImportSymKey(m_slot, m_cipherMechanism, PK11_OriginUnwrap, CKA_ENCRYPT, &keyItem, nullptr);
} else {
// decryption
m_nssKey = PK11_ImportSymKey(m_slot, m_cipherMechanism, PK11_OriginUnwrap, CKA_DECRYPT, &keyItem, nullptr);
}
SECItem ivItem;
ivItem.data = (unsigned char *)iv.data();
ivItem.len = iv.size();
m_params = PK11_ParamFromIV(m_cipherMechanism, &ivItem);
if (QCA::Encode == dir) {
m_context = PK11_CreateContextBySymKey(m_cipherMechanism, CKA_ENCRYPT, m_nssKey, m_params);
} else {
// decryption
m_context = PK11_CreateContextBySymKey(m_cipherMechanism, CKA_DECRYPT, m_nssKey, m_params);
}
if (!m_context) {
qDebug() << "CreateContextBySymKey failed";
return;
}
}
QCA::Provider::Context *clone() const override
{
return new nssCipherContext(*this);
}
int blockSize() const override
{
return PK11_GetBlockSize(m_cipherMechanism, m_params);
}
QCA::AuthTag tag() const override
{
// For future implementation
return QCA::AuthTag();
}
bool update(const QCA::SecureArray &in, QCA::SecureArray *out) override
{
out->resize(in.size() + blockSize());
int resultLength;
PK11_CipherOp(
m_context, (unsigned char *)out->data(), &resultLength, out->size(), (unsigned char *)in.data(), in.size());
out->resize(resultLength);
return true;
}
bool final(QCA::SecureArray *out) override
{
out->resize(blockSize());
unsigned int resultLength;
PK11_DigestFinal(m_context, (unsigned char *)out->data(), &resultLength, out->size());
out->resize(resultLength);
return true;
}
QCA::KeyLength keyLength() const override
{
int min = 0;
int max = 0;
int multiple = 0;
switch (m_cipherMechanism) {
case CKM_AES_ECB:
case CKM_AES_CBC:
min = max = 16;
multiple = 1;
break;
case CKM_DES_ECB:
case CKM_DES_CBC:
case CKM_DES_CBC_PAD:
min = max = 8;
multiple = 1;
break;
case CKM_DES3_ECB:
min = 16;
max = 24;
multiple = 1;
break;
}
return QCA::KeyLength(min, max, multiple);
}
private:
PK11SymKey *m_nssKey;
CK_MECHANISM_TYPE m_cipherMechanism;
PK11SlotInfo *m_slot;
PK11Context *m_context;
SECItem *m_params;
};
//==========================================================
class nssProvider : public QCA::Provider
{
public:
void init() override
{
}
~nssProvider() override
{
}
int qcaVersion() const override
{
return QCA_VERSION;
}
QString name() const override
{
return QStringLiteral("qca-nss");
}
QStringList features() const override
{
QStringList list;
list += QStringLiteral("md2");
list += QStringLiteral("md5");
list += QStringLiteral("sha1");
list += QStringLiteral("sha256");
list += QStringLiteral("sha384");
list += QStringLiteral("sha512");
list += QStringLiteral("hmac(md5)");
list += QStringLiteral("hmac(sha1)");
list += QStringLiteral("hmac(sha256)");
list += QStringLiteral("hmac(sha384)");
list += QStringLiteral("hmac(sha512)");
// appears to not be implemented in NSS yet
// list += QStringLiteral("hmac(ripemd160)");
list += QStringLiteral("aes128-ecb");
list += QStringLiteral("aes128-cbc");
list += QStringLiteral("des-ecb");
list += QStringLiteral("des-cbc");
list += QStringLiteral("des-cbc-pkcs7");
list += QStringLiteral("tripledes-ecb");
return list;
}
Context *createContext(const QString &type) override
{
if (type == QLatin1String("md2"))
return new nssHashContext(this, type);
if (type == QLatin1String("md5"))
return new nssHashContext(this, type);
if (type == QLatin1String("sha1"))
return new nssHashContext(this, type);
if (type == QLatin1String("sha256"))
return new nssHashContext(this, type);
if (type == QLatin1String("sha384"))
return new nssHashContext(this, type);
if (type == QLatin1String("sha512"))
return new nssHashContext(this, type);
if (type == QLatin1String("hmac(md5)"))
return new nssHmacContext(this, type);
if (type == QLatin1String("hmac(sha1)"))
return new nssHmacContext(this, type);
if (type == QLatin1String("hmac(sha256)"))
return new nssHmacContext(this, type);
if (type == QLatin1String("hmac(sha384)"))
return new nssHmacContext(this, type);
if (type == QLatin1String("hmac(sha512)"))
return new nssHmacContext(this, type);
if (type == QLatin1String("hmac(ripemd160)"))
return new nssHmacContext(this, type);
if (type == QLatin1String("aes128-ecb"))
return new nssCipherContext(this, type);
if (type == QLatin1String("aes128-cbc"))
return new nssCipherContext(this, type);
if (type == QLatin1String("des-ecb"))
return new nssCipherContext(this, type);
if (type == QLatin1String("des-cbc"))
return new nssCipherContext(this, type);
if (type == QLatin1String("des-cbc-pkcs7"))
return new nssCipherContext(this, type);
if (type == QLatin1String("tripledes-ecb"))
return new nssCipherContext(this, type);
else
return nullptr;
}
};
class nssPlugin : public QObject, public QCAPlugin
{
Q_OBJECT
Q_PLUGIN_METADATA(IID "com.affinix.qca.Plugin/1.0")
Q_INTERFACES(QCAPlugin)
public:
QCA::Provider *createProvider() override
{
return new nssProvider;
}
};
#include "qca-nss.moc"
|