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 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150
|
/*
* Copyright (C) 2007 Alon Bar-Lev <alon.barlev@gmail.com>
*
* 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 WITHANY 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 <QFile>
#include <QHash>
#include <QtCrypto>
#include <QtPlugin>
using namespace QCA;
// qPrintable is ASCII only!!!
#define myPrintable(s) (s).toUtf8().constData()
namespace softstoreQCAPlugin {
class softstoreKeyStoreListContext;
static softstoreKeyStoreListContext *s_keyStoreList = nullptr;
enum KeyType
{
keyTypeInvalid,
keyTypePKCS12,
keyTypePKCS8Inline,
keyTypePKCS8FilePEM,
keyTypePKCS8FileDER
};
enum PublicType
{
publicTypeInvalid,
publicTypeX509Chain
};
struct SoftStoreEntry
{
QString name;
CertificateChain chain;
KeyType keyReferenceType;
QString keyReference;
bool noPassphrase;
int unlockTimeout;
};
class softstorePKeyBase : public PKeyBase
{
Q_OBJECT
private:
bool _has_privateKeyRole;
SoftStoreEntry _entry;
QString _serialized;
PrivateKey _privkey;
PrivateKey _privkeySign;
PublicKey _pubkey;
QDateTime dueTime;
public:
static inline QString typeToString(PKey::Type t)
{
switch (t) {
case PKey::RSA:
return QStringLiteral("rsa");
case PKey::DSA:
return QStringLiteral("dsa");
case PKey::DH:
return QStringLiteral("dh");
default:
return QLatin1String("");
}
}
softstorePKeyBase(const SoftStoreEntry &entry, const QString &serialized, Provider *p)
: PKeyBase(p, QStringLiteral("rsa") /*typeToString (entry.chain.primary ().subjectPublicKey ().type ())*/)
{
QCA_logTextMessage(QStringLiteral("softstorePKeyBase::softstorePKeyBase1 - entry"), Logger::Debug);
_has_privateKeyRole = true;
_entry = entry;
_serialized = serialized;
_pubkey = _entry.chain.primary().subjectPublicKey();
QCA_logTextMessage(QStringLiteral("softstorePKeyBase::softstorePKeyBase1 - return"), Logger::Debug);
}
softstorePKeyBase(const softstorePKeyBase &from)
: PKeyBase(from.provider(), QStringLiteral("rsa") /*typeToString (from._pubkey.type ())*/)
{
QCA_logTextMessage(QStringLiteral("softstorePKeyBase::softstorePKeyBaseC - entry"), Logger::Debug);
_has_privateKeyRole = from._has_privateKeyRole;
_entry = from._entry;
_serialized = from._serialized;
_pubkey = from._pubkey;
_privkey = from._privkey;
QCA_logTextMessage(QStringLiteral("softstorePKeyBase::softstorePKeyBaseC - return"), Logger::Debug);
}
~softstorePKeyBase() override
{
QCA_logTextMessage(QStringLiteral("softstorePKeyBase::~softstorePKeyBase - entry"), Logger::Debug);
QCA_logTextMessage(QStringLiteral("softstorePKeyBase::~softstorePKeyBase - return"), Logger::Debug);
}
Provider::Context *clone() const override
{
return new softstorePKeyBase(*this);
}
public:
bool isNull() const override
{
return _pubkey.isNull();
}
PKey::Type type() const override
{
return _pubkey.type();
}
bool isPrivate() const override
{
return _has_privateKeyRole;
}
bool canExport() const override
{
return !_has_privateKeyRole;
}
void convertToPublic() override
{
QCA_logTextMessage(QStringLiteral("softstorePKeyBase::convertToPublic - entry"), Logger::Debug);
if (_has_privateKeyRole) {
_has_privateKeyRole = false;
}
QCA_logTextMessage(QStringLiteral("softstorePKeyBase::convertToPublic - return"), Logger::Debug);
}
int bits() const override
{
return _pubkey.bitSize();
}
int maximumEncryptSize(EncryptionAlgorithm alg) const override
{
return _pubkey.maximumEncryptSize(alg);
}
SecureArray encrypt(const SecureArray &in, EncryptionAlgorithm alg) override
{
return _pubkey.encrypt(in, alg);
}
bool decrypt(const SecureArray &in, SecureArray *out, EncryptionAlgorithm alg) override
{
if (_ensureAccess()) {
return _privkey.decrypt(in, out, alg);
} else {
return false;
}
}
void startSign(SignatureAlgorithm alg, SignatureFormat format) override
{
if (_ensureAccess()) {
/*
* We must use one object thought
* signing, so it won't expire by
* it-self or during passphrase.
*/
_privkeySign = _privkey;
_privkeySign.startSign(alg, format);
}
}
void startVerify(SignatureAlgorithm alg, SignatureFormat sf) override
{
_pubkey.startVerify(alg, sf);
}
void update(const MemoryRegion &in) override
{
if (_has_privateKeyRole) {
_privkeySign.update(in);
} else {
_pubkey.update(in);
}
}
QByteArray endSign() override
{
const QByteArray r = _privkeySign.signature();
_privkeySign = PrivateKey();
return r;
}
virtual bool validSignature(const QByteArray &sig)
{
return _pubkey.validSignature(sig);
}
virtual void createPrivate(int bits, int exp, bool block)
{
Q_UNUSED(bits);
Q_UNUSED(exp);
Q_UNUSED(block);
}
virtual void createPrivate(const BigInteger &n,
const BigInteger &e,
const BigInteger &p,
const BigInteger &q,
const BigInteger &d)
{
Q_UNUSED(n);
Q_UNUSED(e);
Q_UNUSED(p);
Q_UNUSED(q);
Q_UNUSED(d);
}
virtual void createPublic(const BigInteger &n, const BigInteger &e)
{
Q_UNUSED(n);
Q_UNUSED(e);
}
public:
PublicKey _publicKey() const
{
return _pubkey;
}
bool _ensureAccess()
{
bool ret = false;
QCA_logTextMessage(QStringLiteral("softstorePKeyBase::_ensureAccess - entry"), Logger::Debug);
if (_entry.unlockTimeout != -1) {
if (dueTime >= QDateTime::currentDateTime()) {
QCA_logTextMessage(QStringLiteral("softstorePKeyBase::_ensureAccess - dueTime reached, clearing"),
Logger::Debug);
_privkey = PrivateKey();
}
}
if (!_privkey.isNull()) {
ret = true;
} else {
KeyStoreEntry entry;
KeyStoreEntryContext *context = nullptr;
QString storeId, storeName;
ConvertResult cresult;
QCA_logTextMessage(QStringLiteral("softstorePKeyBase::_ensureAccess - no current key, creating"),
Logger::Debug);
// too lazy to create scope
context = reinterpret_cast<KeyStoreListContext *>(s_keyStoreList)->entryPassive(_serialized);
if (context != nullptr) {
storeId = context->storeId();
storeName = context->storeName();
entry.change(context);
}
while (!ret) {
SecureArray passphrase;
switch (_entry.keyReferenceType) {
case keyTypeInvalid:
case keyTypePKCS8Inline:
break;
case keyTypePKCS12:
case keyTypePKCS8FilePEM:
case keyTypePKCS8FileDER:
{
QFile file(_entry.keyReference);
while (!file.open(QIODevice::ReadOnly)) {
TokenAsker asker;
asker.ask(KeyStoreInfo(KeyStore::SmartCard, storeId, storeName), entry, context);
asker.waitForResponse();
if (!asker.accepted()) {
goto cleanup1;
}
}
} break;
}
if (!_entry.noPassphrase) {
PasswordAsker asker;
asker.ask(Event::StylePassphrase, KeyStoreInfo(KeyStore::User, storeId, storeName), entry, context);
asker.waitForResponse();
passphrase = asker.password();
if (!asker.accepted()) {
goto cleanup1;
}
}
switch (_entry.keyReferenceType) {
case keyTypeInvalid:
break;
case keyTypePKCS12:
{
KeyBundle bundle = KeyBundle::fromFile(_entry.keyReference, passphrase, &cresult);
if (cresult == ConvertGood) {
_privkey = bundle.privateKey();
ret = true;
}
} break;
case keyTypePKCS8Inline:
{
PrivateKey k =
PrivateKey::fromDER(Base64().stringToArray(_entry.keyReference), passphrase, &cresult);
if (cresult == ConvertGood) {
_privkey = k;
ret = true;
}
} break;
case keyTypePKCS8FilePEM:
{
PrivateKey k = PrivateKey::fromPEMFile(_entry.keyReference, passphrase, &cresult);
if (cresult == ConvertGood) {
_privkey = k;
ret = true;
}
} break;
case keyTypePKCS8FileDER:
{
QFile file(_entry.keyReference);
if (file.open(QIODevice::ReadOnly)) {
const QByteArray contents = file.readAll();
PrivateKey k = PrivateKey::fromDER(contents, passphrase, &cresult);
if (cresult == ConvertGood) {
_privkey = k;
ret = true;
}
}
} break;
}
}
if (_entry.unlockTimeout != -1) {
dueTime = QDateTime::currentDateTime().addSecs(_entry.unlockTimeout);
}
cleanup1:;
}
QCA_logTextMessage(QString::asprintf("softstorePKeyBase::_ensureAccess - return ret=%d", ret ? 1 : 0),
Logger::Debug);
return ret;
}
};
class softstorePKeyContext : public PKeyContext
{
Q_OBJECT
private:
PKeyBase *_k;
public:
softstorePKeyContext(Provider *p)
: PKeyContext(p)
{
_k = nullptr;
}
~softstorePKeyContext() override
{
delete _k;
_k = nullptr;
}
Provider::Context *clone() const override
{
softstorePKeyContext *c = new softstorePKeyContext(*this);
c->_k = (PKeyBase *)_k->clone();
return c;
}
public:
QList<PKey::Type> supportedTypes() const override
{
QList<PKey::Type> list;
list += static_cast<softstorePKeyBase *>(_k)->_publicKey().type();
return list;
}
QList<PKey::Type> supportedIOTypes() const override
{
QList<PKey::Type> list;
list += static_cast<softstorePKeyBase *>(_k)->_publicKey().type();
return list;
}
QList<PBEAlgorithm> supportedPBEAlgorithms() const override
{
QList<PBEAlgorithm> list;
return list;
}
PKeyBase *key() override
{
return _k;
}
const PKeyBase *key() const override
{
return _k;
}
void setKey(PKeyBase *key) override
{
delete _k;
_k = key;
}
bool importKey(const PKeyBase *key) override
{
Q_UNUSED(key);
return false;
}
static int passphrase_cb(char *buf, int size, int rwflag, void *u)
{
Q_UNUSED(buf);
Q_UNUSED(size);
Q_UNUSED(rwflag);
Q_UNUSED(u);
return 0;
}
QByteArray publicToDER() const override
{
return static_cast<softstorePKeyBase *>(_k)->_publicKey().toDER();
}
QString publicToPEM() const override
{
return static_cast<softstorePKeyBase *>(_k)->_publicKey().toPEM();
}
ConvertResult publicFromDER(const QByteArray &in) override
{
Q_UNUSED(in);
return ErrorDecode;
}
ConvertResult publicFromPEM(const QString &s) override
{
Q_UNUSED(s);
return ErrorDecode;
}
SecureArray privateToDER(const SecureArray &passphrase, PBEAlgorithm pbe) const override
{
Q_UNUSED(passphrase);
Q_UNUSED(pbe);
return SecureArray();
}
QString privateToPEM(const SecureArray &passphrase, PBEAlgorithm pbe) const override
{
Q_UNUSED(passphrase);
Q_UNUSED(pbe);
return QString();
}
ConvertResult privateFromDER(const SecureArray &in, const SecureArray &passphrase) override
{
Q_UNUSED(in);
Q_UNUSED(passphrase);
return ErrorDecode;
}
ConvertResult privateFromPEM(const QString &s, const SecureArray &passphrase) override
{
Q_UNUSED(s);
Q_UNUSED(passphrase);
return ErrorDecode;
}
};
class softstoreKeyStoreEntryContext : public KeyStoreEntryContext
{
Q_OBJECT
private:
KeyStoreEntry::Type _item_type;
KeyBundle _key;
SoftStoreEntry _entry;
QString _serialized;
public:
softstoreKeyStoreEntryContext(const KeyBundle &key,
const SoftStoreEntry &entry,
const QString &serialized,
Provider *p)
: KeyStoreEntryContext(p)
{
_item_type = KeyStoreEntry::TypeKeyBundle;
_key = key;
_entry = entry;
_serialized = serialized;
}
softstoreKeyStoreEntryContext(const softstoreKeyStoreEntryContext &from)
: KeyStoreEntryContext(from)
{
_item_type = from._item_type;
_key = from._key;
_entry = from._entry;
_serialized = from._serialized;
}
Provider::Context *clone() const override
{
return new softstoreKeyStoreEntryContext(*this);
}
public:
KeyStoreEntry::Type type() const override
{
return KeyStoreEntry::TypeKeyBundle;
}
QString name() const override
{
return _entry.name;
}
QString id() const override
{
return _entry.name;
}
KeyBundle keyBundle() const override
{
return _key;
}
Certificate certificate() const override
{
return _entry.chain.primary();
}
QString storeId() const override
{
return QString::asprintf("%s/%s", "qca-softstore", myPrintable(_entry.name));
}
QString storeName() const override
{
return _entry.name;
}
bool ensureAccess() override
{
return static_cast<softstorePKeyBase *>(static_cast<PKeyContext *>(_key.privateKey().context())->key())
->_ensureAccess();
}
QString serialize() const override
{
return _serialized;
}
};
class softstoreKeyStoreListContext : public KeyStoreListContext
{
Q_OBJECT
private:
int _last_id;
QList<SoftStoreEntry> _entries;
public:
softstoreKeyStoreListContext(Provider *p)
: KeyStoreListContext(p)
{
QCA_logTextMessage(
QString::asprintf("softstoreKeyStoreListContext::softstoreKeyStoreListContext - entry Provider=%p",
(void *)p),
Logger::Debug);
_last_id = 0;
QCA_logTextMessage(QStringLiteral("softstoreKeyStoreListContext::softstoreKeyStoreListContext - return"),
Logger::Debug);
}
~softstoreKeyStoreListContext() override
{
QCA_logTextMessage(QStringLiteral("softstoreKeyStoreListContext::~softstoreKeyStoreListContext - entry"),
Logger::Debug);
s_keyStoreList = nullptr;
QCA_logTextMessage(QStringLiteral("softstoreKeyStoreListContext::~softstoreKeyStoreListContext - return"),
Logger::Debug);
}
Provider::Context *clone() const override
{
QCA_logTextMessage(QStringLiteral("softstoreKeyStoreListContext::clone - entry/return"), Logger::Debug);
return nullptr;
}
public:
void start() override
{
QCA_logTextMessage(QStringLiteral("softstoreKeyStoreListContext::start - entry"), Logger::Debug);
QMetaObject::invokeMethod(this, "doReady", Qt::QueuedConnection);
QCA_logTextMessage(QStringLiteral("softstoreKeyStoreListContext::start - return"), Logger::Debug);
}
void setUpdatesEnabled(bool enabled) override
{
QCA_logTextMessage(
QString::asprintf("softstoreKeyStoreListContext::setUpdatesEnabled - entry/return enabled=%d",
enabled ? 1 : 0),
Logger::Debug);
}
KeyStoreEntryContext *entry(int id, const QString &entryId) override
{
QCA_logTextMessage(QString::asprintf("softstoreKeyStoreListContext::entry - entry/return id=%d entryId='%s'",
id,
myPrintable(entryId)),
Logger::Debug);
Q_UNUSED(id);
Q_UNUSED(entryId);
return nullptr;
}
KeyStoreEntryContext *entryPassive(const QString &serialized) override
{
KeyStoreEntryContext *entry = nullptr;
QCA_logTextMessage(QString::asprintf("softstoreKeyStoreListContext::entryPassive - entry serialized='%s'",
myPrintable(serialized)),
Logger::Debug);
if (serialized.startsWith(QLatin1String("qca-softstore/"))) {
SoftStoreEntry sentry;
if (_deserializeSoftStoreEntry(serialized, sentry)) {
entry = _keyStoreEntryBySoftStoreEntry(sentry);
}
}
QCA_logTextMessage(
QString::asprintf("softstoreKeyStoreListContext::entryPassive - return entry=%p", (void *)entry),
Logger::Debug);
return entry;
}
KeyStore::Type type(int id) const override
{
Q_UNUSED(id);
QCA_logTextMessage(QString::asprintf("softstoreKeyStoreListContext::type - entry/return id=%d", id),
Logger::Debug);
return KeyStore::User;
}
QString storeId(int id) const override
{
QString ret;
QCA_logTextMessage(QString::asprintf("softstoreKeyStoreListContext::storeId - entry id=%d", id), Logger::Debug);
ret = QStringLiteral("qca-softstore");
QCA_logTextMessage(QString::asprintf("softstoreKeyStoreListContext::storeId - return ret=%s", myPrintable(ret)),
Logger::Debug);
return ret;
}
QString name(int id) const override
{
QString ret;
QCA_logTextMessage(QString::asprintf("softstoreKeyStoreListContext::name - entry id=%d", id), Logger::Debug);
ret = QStringLiteral("User Software Store");
QCA_logTextMessage(QString::asprintf("softstoreKeyStoreListContext::name - return ret=%s", myPrintable(ret)),
Logger::Debug);
return ret;
}
QList<KeyStoreEntry::Type> entryTypes(int id) const override
{
Q_UNUSED(id);
QCA_logTextMessage(QString::asprintf("softstoreKeyStoreListContext::entryTypes - entry/return id=%d", id),
Logger::Debug);
QList<KeyStoreEntry::Type> list;
list += KeyStoreEntry::TypeKeyBundle;
list += KeyStoreEntry::TypeCertificate;
return list;
}
QList<int> keyStores() override
{
QList<int> list;
QCA_logTextMessage(QStringLiteral("softstoreKeyStoreListContext::keyStores - entry"), Logger::Debug);
list += _last_id;
QCA_logTextMessage(
QString::asprintf("softstoreKeyStoreListContext::keyStores - return out.size()=%d", int(list.size())),
Logger::Debug);
return list;
}
QList<KeyStoreEntryContext *> entryList(int id) override
{
QList<KeyStoreEntryContext *> list;
QCA_logTextMessage(QString::asprintf("softstoreKeyStoreListContext::entryList - entry id=%d", id),
Logger::Debug);
foreach (const SoftStoreEntry &e, _entries) {
list += _keyStoreEntryBySoftStoreEntry(e);
}
QCA_logTextMessage(
QString::asprintf("softstoreKeyStoreListContext::entryList - return out.size()=%d", int(list.size())),
Logger::Debug);
return list;
}
void _emit_diagnosticText(const QString &t)
{
QCA_logTextMessage(
QString::asprintf("softstoreKeyStoreListContext::_emit_diagnosticText - entry t='%s'", myPrintable(t)),
Logger::Debug);
QCA_logTextMessage(t, Logger::Warning);
emit diagnosticText(t);
QCA_logTextMessage(QStringLiteral("softstoreKeyStoreListContext::_emit_diagnosticText - return"),
Logger::Debug);
}
private Q_SLOTS:
void doReady()
{
QCA_logTextMessage(QStringLiteral("softstoreKeyStoreListContext::doReady - entry"), Logger::Debug);
emit busyEnd();
QCA_logTextMessage(QStringLiteral("softstoreKeyStoreListContext::doReady - return"), Logger::Debug);
}
void doUpdated()
{
QCA_logTextMessage(QStringLiteral("softstoreKeyStoreListContext::doUpdated - entry"), Logger::Debug);
emit updated();
QCA_logTextMessage(QStringLiteral("softstoreKeyStoreListContext::doUpdated - return"), Logger::Debug);
}
public:
void _updateConfig(const QVariantMap &config, const int maxEntries)
{
QCA_logTextMessage(QStringLiteral("softstoreKeyStoreListContext::_updateConfig - entry"), Logger::Debug);
QMap<QString, KeyType> keyTypeMap;
keyTypeMap[QStringLiteral("pkcs12")] = keyTypePKCS12;
keyTypeMap[QStringLiteral("pkcs8")] = keyTypePKCS8Inline;
keyTypeMap[QStringLiteral("pkcs8-file-pem")] = keyTypePKCS8FilePEM;
keyTypeMap[QStringLiteral("pkcs8-file-der")] = keyTypePKCS8FileDER;
QMap<QString, PublicType> publicTypeMap;
publicTypeMap[QStringLiteral("x509chain")] = publicTypeX509Chain;
_last_id++;
_entries.clear();
for (int i = 0; i < maxEntries; i++) {
if (config[QString::asprintf("entry_%02d_enabled", i)].toBool()) {
ConvertResult cresult;
SoftStoreEntry entry;
PublicType publicType = publicTypeInvalid;
entry.name = config[QString::asprintf("entry_%02d_name", i)].toString();
const QString stringReferenceType = config[QString::asprintf("entry_%02d_private_type", i)].toString();
const QString stringPublicType = config[QString::asprintf("entry_%02d_public_type", i)].toString();
entry.noPassphrase = config[QString::asprintf("entry_%02d_no_passphrase", i)].toBool();
entry.unlockTimeout = config[QString::asprintf("entry_%02d_unlock_timeout", i)].toInt();
if (publicTypeMap.contains(stringPublicType)) {
publicType = publicTypeMap[stringPublicType];
} else {
_emit_diagnosticText(QString::asprintf("Software Store: Bad public key type of '%s' entry.\n",
myPrintable(entry.name)));
goto cleanup1;
}
if (keyTypeMap.contains(stringReferenceType)) {
entry.keyReferenceType = keyTypeMap[stringReferenceType];
} else {
_emit_diagnosticText(QString::asprintf("Software Store: Bad private key type of '%s' entry.\n",
myPrintable(entry.name)));
goto cleanup1;
}
entry.keyReference = config[QString::asprintf("entry_%02d_private", i)].toString();
switch (publicType) {
case publicTypeInvalid:
goto cleanup1;
break;
case publicTypeX509Chain:
const QStringList base64certs =
config[QString::asprintf("entry_%02d_public", i)].toString().split(QStringLiteral("!"));
foreach (const QString &s, base64certs) {
entry.chain += Certificate::fromDER(Base64().stringToArray(s).toByteArray(), &cresult);
}
if (cresult != ConvertGood) {
_emit_diagnosticText(QString::asprintf(
"Software Store: Cannot load certificate of '%s' entry.\n", myPrintable(entry.name)));
goto cleanup1;
}
break;
}
_entries += entry;
cleanup1:; // nothing to do for this entry.
}
}
QMetaObject::invokeMethod(s_keyStoreList, "doUpdated", Qt::QueuedConnection);
QCA_logTextMessage(QStringLiteral("softstoreKeyStoreListContext::_updateConfig - return"), Logger::Debug);
}
private:
QString _serializeSoftStoreEntry(const SoftStoreEntry &entry) const
{
QString serialized;
QCA_logTextMessage(QString::asprintf("softstoreKeyStoreListContext::_serializeSoftStoreEntry - entry name=%s",
myPrintable(entry.name)),
Logger::Debug);
serialized = QString::asprintf("qca-softstore/0/%s/%d/%s/%d/%d/x509chain/",
myPrintable(_escapeString(entry.name)),
entry.keyReferenceType,
myPrintable(_escapeString(entry.keyReference)),
entry.noPassphrase ? 1 : 0,
entry.unlockTimeout);
QStringList list;
foreach (const Certificate &i, entry.chain) {
list += _escapeString(Base64().arrayToString(i.toDER()));
}
serialized.append(list.join(QStringLiteral("/")));
QCA_logTextMessage(
QString::asprintf("softstoreKeyStoreListContext::_serializeSoftStoreEntry - return serialized='%s'",
myPrintable(serialized)),
Logger::Debug);
return serialized;
}
bool _deserializeSoftStoreEntry(const QString &serialized, SoftStoreEntry &entry) const
{
bool ret = false;
QCA_logTextMessage(
QString::asprintf("softstoreKeyStoreListContext::_deserializeSoftStoreEntry - entry from='%s'",
myPrintable(serialized)),
Logger::Debug);
entry = SoftStoreEntry();
const QStringList list = serialized.split(QStringLiteral("/"));
int n = 0;
if (list.size() < 8) {
goto cleanup;
}
if (list[n++] != QLatin1String("qca-softstore")) {
goto cleanup;
}
if (list[n++].toInt() != 0) {
goto cleanup;
}
entry.name = _unescapeString(list[n++]);
entry.keyReferenceType = (KeyType)list[n++].toInt();
entry.keyReference = _unescapeString(list[n++]);
entry.noPassphrase = list[n++].toInt() != 0;
entry.unlockTimeout = list[n++].toInt();
n++; // skip public key for now.
while (n < list.size()) {
Certificate cert = Certificate::fromDER(Base64().stringToArray(_unescapeString(list[n++])).toByteArray());
if (cert.isNull()) {
goto cleanup;
}
entry.chain += cert;
}
ret = true;
cleanup:
QCA_logTextMessage(
QString::asprintf(
"softstoreKeyStoreListContext::_deserializeSoftStoreEntry - return ret=%d chain.size()=%d",
ret ? 1 : 0,
int(entry.chain.size())),
Logger::Debug);
return ret;
}
softstoreKeyStoreEntryContext *_keyStoreEntryBySoftStoreEntry(const SoftStoreEntry &sentry) const
{
softstoreKeyStoreEntryContext *entry = nullptr;
QCA_logTextMessage(
QString::asprintf("softstoreKeyStoreListContext::_keyStoreEntryBySoftStoreEntry - entry name=%s",
myPrintable(sentry.name)),
Logger::Debug);
QString serialized = _serializeSoftStoreEntry(sentry);
softstorePKeyBase *pkey = new softstorePKeyBase(sentry, serialized, provider());
softstorePKeyContext *pkc = new softstorePKeyContext(provider());
pkc->setKey(pkey);
PrivateKey privkey;
privkey.change(pkc);
KeyBundle key;
key.setCertificateChainAndKey(sentry.chain, privkey);
entry = new softstoreKeyStoreEntryContext(key, sentry, serialized, provider());
QCA_logTextMessage(
QString::asprintf("softstoreKeyStoreListContext::_keyStoreEntryBySoftStoreEntry - return entry=%p",
(void *)entry),
Logger::Debug);
return entry;
}
QString _escapeString(const QString &from) const
{
QString to;
foreach (const QChar &c, from) {
if (c == QLatin1Char('/') || c == QLatin1Char('\\')) {
to += QString::asprintf("\\x%04x", c.unicode());
} else {
to += c;
}
}
return to;
}
QString _unescapeString(const QString &from) const
{
QString to;
for (int i = 0; i < from.size(); i++) {
QChar c = from[i];
if (c == QLatin1Char('\\')) {
#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 2)
to += QChar((ushort)QStringView(from).mid(i + 2, 4).toInt(nullptr, 16));
#else
to += QChar((ushort)from.midRef(i + 2, 4).toInt(nullptr, 16));
#endif
i += 5;
} else {
to += c;
}
}
return to;
}
};
}
using namespace softstoreQCAPlugin;
class softstoreProvider : public Provider
{
private:
static const int _CONFIG_MAX_ENTRIES;
QVariantMap _config;
public:
softstoreProvider()
{
}
~softstoreProvider() override
{
}
public:
int qcaVersion() const override
{
return QCA_VERSION;
}
void init() override
{
}
QString name() const override
{
return QStringLiteral("qca-softstore");
}
QStringList features() const override
{
QCA_logTextMessage(QStringLiteral("softstoreProvider::features - entry/return"), Logger::Debug);
QStringList list;
list += QStringLiteral("pkey");
list += QStringLiteral("keystorelist");
return list;
}
Context *createContext(const QString &type) override
{
Provider::Context *context = nullptr;
QCA_logTextMessage(QString::asprintf("softstoreProvider::createContext - entry type='%s'", myPrintable(type)),
Logger::Debug);
if (type == QLatin1String("keystorelist")) {
if (s_keyStoreList == nullptr) {
s_keyStoreList = new softstoreKeyStoreListContext(this);
s_keyStoreList->_updateConfig(_config, _CONFIG_MAX_ENTRIES);
}
context = s_keyStoreList;
}
QCA_logTextMessage(QString::asprintf("softstoreProvider::createContext - return context=%p", (void *)context),
Logger::Debug);
return context;
}
QVariantMap defaultConfig() const override
{
QVariantMap mytemplate;
QCA_logTextMessage(QStringLiteral("softstoreProvider::defaultConfig - entry/return"), Logger::Debug);
mytemplate[QStringLiteral("formtype")] = QStringLiteral("http://affinix.com/qca/forms/qca-softstore#1.0");
for (int i = 0; i < _CONFIG_MAX_ENTRIES; i++) {
mytemplate[QString::asprintf("entry_%02d_enabled", i)] = false;
mytemplate[QString::asprintf("entry_%02d_name", i)] = QLatin1String("");
mytemplate[QString::asprintf("entry_%02d_public_type", i)] = QLatin1String("");
mytemplate[QString::asprintf("entry_%02d_private_type", i)] = QLatin1String("");
mytemplate[QString::asprintf("entry_%02d_public", i)] = QLatin1String("");
mytemplate[QString::asprintf("entry_%02d_private", i)] = QLatin1String("");
mytemplate[QString::asprintf("entry_%02d_unlock_timeout", i)] = -1;
mytemplate[QString::asprintf("entry_%02d_no_passphrase", i)] = false;
}
return mytemplate;
}
void configChanged(const QVariantMap &config) override
{
QCA_logTextMessage(QStringLiteral("softstoreProvider::configChanged - entry"), Logger::Debug);
_config = config;
if (s_keyStoreList != nullptr) {
s_keyStoreList->_updateConfig(_config, _CONFIG_MAX_ENTRIES);
}
QCA_logTextMessage(QStringLiteral("softstoreProvider::configChanged - return"), Logger::Debug);
}
};
const int softstoreProvider::_CONFIG_MAX_ENTRIES = 50;
class softstorePlugin : public QObject, public QCAPlugin
{
Q_OBJECT
Q_PLUGIN_METADATA(IID "com.affinix.qca.Plugin/1.0")
Q_INTERFACES(QCAPlugin)
public:
Provider *createProvider() override
{
return new softstoreProvider;
}
};
#include "qca-softstore.moc"
|