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
|
/*
* Copyright (C) 2007 Justin Karneges <justin@affinix.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 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 <QtCore>
#include <QtCrypto>
using namespace QCA;
static char cert_pem[] =
"-----BEGIN CERTIFICATE-----\n"
"MIIBsTCCAVugAwIBAgIBADANBgkqhkiG9w0BAQUFADA4MRQwEgYDVQQDEwtUZXN0\n"
"IENlcnQgMTELMAkGA1UEBhMCVVMxEzARBgNVBAoTClRlc3QgT3JnIDEwHhcNMDcw\n"
"NjE5MjAzOTI4WhcNMTIwNjE5MjAzOTI4WjA4MRQwEgYDVQQDEwtUZXN0IENlcnQg\n"
"MTELMAkGA1UEBhMCVVMxEzARBgNVBAoTClRlc3QgT3JnIDEwXDANBgkqhkiG9w0B\n"
"AQEFAANLADBIAkEA3645RS/xBlWnjju6moaRYQuIDo7fwM+GxhE91HECLAg3Hnkr\n"
"I+qx96VXd006olOn8MrkbjSqcTJ4LcDaCGI1YwIDAQABo1AwTjAdBgNVHQ4EFgQU\n"
"nm5lNkkblHdoB0gLeh8mB6Ed+TMwDwYDVR0TAQH/BAUwAwIBADAcBgNVHREEFTAT\n"
"gRF0ZXN0MUBleGFtcGxlLmNvbTANBgkqhkiG9w0BAQUFAANBAFTtXtwfYcJZBsXJ\n"
"+Ckm9qbg7qR/XRERDzeR0yhHZE7F/jU5YQv7+iJL4l95iH9PkZNOk15Tu/Kzzekx\n"
"6CTXzKA=\n"
"-----END CERTIFICATE-----";
static char key_n_dec[] =
"1171510158037441543813157379806833168225785177834459013412026750"
"9262193808059395366696241600386200064326196137137376912654785051"
"560621331316573341676090723";
static char key_e_dec[] = "65537";
//----------------------------------------------------------------------------
// TestProvider
//----------------------------------------------------------------------------
class TestProvider : public Provider
{
public:
TestProvider()
{
appendPluginDiagnosticText("TestProvider constructed\n");
}
void init()
{
appendPluginDiagnosticText("TestProvider initialized\n");
}
void deinit()
{
appendPluginDiagnosticText("TestProvider deinitialized\n");
}
~TestProvider()
{
appendPluginDiagnosticText("TestProvider destructed\n");
}
int version() const
{
return 0x010203; // 1.2.3
}
int qcaVersion() const
{
return QCA_VERSION;
}
QString name() const
{
return "qca-test";
}
QStringList features() const
{
QStringList list;
list += "keystorelist";
return list;
}
Context *createContext(const QString &type);
};
//----------------------------------------------------------------------------
// TestData
//----------------------------------------------------------------------------
class TestKeyStore
{
public:
int contextId;
KeyStore::Type type;
QString storeId;
QString name;
bool readOnly;
bool avail; // for simplicity, all items share this global toggle
QList<KeyBundle> certs;
TestKeyStore()
: contextId(-1)
, type(KeyStore::SmartCard)
, readOnly(true)
, avail(true)
{
}
};
class TestData
{
public:
int context_at;
QList<TestKeyStore> stores;
TestData()
: context_at(0)
{
}
};
//----------------------------------------------------------------------------
// TestRSAContext
//----------------------------------------------------------------------------
static KeyStoreEntry make_entry(Provider *p, TestKeyStore *store);
class TestRSAContext : public RSAContext
{
Q_OBJECT
public:
bool priv;
TestKeyStore *store;
TestRSAContext(Provider *p)
: RSAContext(p)
, priv(true)
, store(0)
{
}
TestRSAContext(const TestRSAContext &from)
: RSAContext(from)
, priv(from.priv)
, store(from.store)
{
}
Context *clone() const
{
return new TestRSAContext(*this);
}
virtual bool isNull() const
{
return false;
}
virtual PKey::Type type() const
{
return PKey::RSA;
}
virtual bool isPrivate() const
{
return priv;
}
virtual bool canExport() const
{
return false;
}
virtual void convertToPublic()
{
priv = false;
}
virtual int bits() const
{
return 2048;
}
virtual void startSign(SignatureAlgorithm alg, SignatureFormat format)
{
Q_UNUSED(alg);
Q_UNUSED(format);
}
virtual void update(const MemoryRegion &in)
{
Q_UNUSED(in);
}
virtual QByteArray endSign()
{
if (!store)
return QByteArray();
while (store->contextId == -1 || !store->avail) {
KeyStoreInfo info(store->type, store->storeId, store->name);
KeyStoreEntry entry = make_entry(provider(), store);
TokenAsker asker;
asker.ask(info, entry, 0);
asker.waitForResponse();
if (!asker.accepted())
return QByteArray();
}
return "foobar";
}
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);
}
virtual BigInteger n() const
{
return BigInteger(QString(key_n_dec));
}
virtual BigInteger e() const
{
return BigInteger(QString(key_e_dec));
}
virtual BigInteger p() const
{
return BigInteger();
}
virtual BigInteger q() const
{
return BigInteger();
}
virtual BigInteger d() const
{
return BigInteger();
}
};
//----------------------------------------------------------------------------
// TestPKeyContext
//----------------------------------------------------------------------------
class TestPKeyContext : public PKeyContext
{
Q_OBJECT
public:
TestRSAContext *_key;
TestPKeyContext(Provider *p)
: PKeyContext(p)
, _key(0)
{
}
TestPKeyContext(const TestPKeyContext &from)
: PKeyContext(from)
, _key(0)
{
if (from._key)
_key = (TestRSAContext *)from._key->clone();
}
~TestPKeyContext()
{
delete _key;
}
Context *clone() const
{
return new TestPKeyContext(*this);
}
virtual QList<PKey::Type> supportedTypes() const
{
QList<PKey::Type> list;
list += PKey::RSA;
return list;
}
virtual QList<PKey::Type> supportedIOTypes() const
{
return QList<PKey::Type>();
}
virtual QList<PBEAlgorithm> supportedPBEAlgorithms() const
{
return QList<PBEAlgorithm>();
}
virtual PKeyBase *key()
{
return _key;
}
virtual const PKeyBase *key() const
{
return _key;
}
virtual void setKey(PKeyBase *key)
{
delete _key;
_key = (TestRSAContext *)key;
}
virtual bool importKey(const PKeyBase *key)
{
Q_UNUSED(key);
return false;
}
};
//----------------------------------------------------------------------------
// TestCertContext
//----------------------------------------------------------------------------
class TestCertContext : public CertContext
{
Q_OBJECT
public:
CertContextProps _props;
TestCertContext(Provider *p)
: CertContext(p)
{
}
Context *clone() const
{
return new TestCertContext(*this);
}
virtual QByteArray toDER() const
{
QStringList lines = toPEM().split('\n');
lines.removeFirst();
lines.removeLast();
QString enc = lines.join("");
return Base64().stringToArray(enc).toByteArray();
}
virtual QString toPEM() const
{
return QString(cert_pem);
}
virtual ConvertResult fromDER(const QByteArray &a)
{
Q_UNUSED(a);
return ErrorDecode;
}
virtual ConvertResult fromPEM(const QString &s)
{
Q_UNUSED(s);
return ErrorDecode;
}
virtual bool createSelfSigned(const CertificateOptions &opts, const PKeyContext &priv)
{
Q_UNUSED(opts);
Q_UNUSED(priv);
return false;
}
virtual const CertContextProps *props() const
{
return &_props;
}
virtual bool compare(const CertContext *other) const
{
Q_UNUSED(other);
return false;
}
virtual PKeyContext *subjectPublicKey() const
{
TestRSAContext *rsa1 = new TestRSAContext(provider());
rsa1->priv = false;
TestPKeyContext *kc1 = new TestPKeyContext(provider());
kc1->setKey(rsa1);
return kc1;
}
virtual bool isIssuerOf(const CertContext *other) const
{
Q_UNUSED(other);
return false;
}
virtual Validity validate(const QList<CertContext *> &trusted,
const QList<CertContext *> &untrusted,
const QList<CRLContext *> &crls,
UsageMode u,
ValidateFlags vf) const
{
Q_UNUSED(trusted);
Q_UNUSED(untrusted);
Q_UNUSED(crls);
Q_UNUSED(u);
Q_UNUSED(vf);
return ErrorValidityUnknown;
}
virtual Validity validate_chain(const QList<CertContext *> &chain,
const QList<CertContext *> &trusted,
const QList<CRLContext *> &crls,
UsageMode u,
ValidateFlags vf) const
{
Q_UNUSED(chain);
Q_UNUSED(trusted);
Q_UNUSED(crls);
Q_UNUSED(u);
Q_UNUSED(vf);
return ErrorValidityUnknown;
}
};
//----------------------------------------------------------------------------
// TestKeyStoreEntryContext
//----------------------------------------------------------------------------
class TestKeyStoreEntryContext : public KeyStoreEntryContext
{
Q_OBJECT
public:
QString _id, _name, _storeId, _storeName;
KeyBundle kb;
TestKeyStore *store;
TestKeyStoreEntryContext(Provider *p)
: KeyStoreEntryContext(p)
{
}
virtual Context *clone() const
{
return new TestKeyStoreEntryContext(*this);
}
virtual KeyStoreEntry::Type type() const
{
return KeyStoreEntry::TypeKeyBundle;
}
virtual QString id() const
{
return _id;
}
virtual QString name() const
{
return _name;
}
virtual QString storeId() const
{
return _storeId;
}
virtual QString storeName() const
{
return _storeName;
}
virtual bool isAvailable() const
{
return store->avail;
}
virtual QString serialize() const
{
return QString("qca-test-1/fake_serialized");
}
virtual KeyBundle keyBundle() const
{
return kb;
}
virtual bool ensureAccess()
{
return true;
}
};
KeyStoreEntry make_entry(Provider *p, TestKeyStore *store)
{
KeyStoreEntry entry;
TestKeyStoreEntryContext *kse = new TestKeyStoreEntryContext(p);
kse->_id = QString::number(0);
kse->_name = store->certs[0].certificateChain().primary().commonName();
kse->_storeId = store->storeId;
kse->_storeName = store->name;
kse->kb = store->certs[0];
kse->store = store;
entry.change(kse);
return entry;
}
//----------------------------------------------------------------------------
// TestKeyStoreListContext
//----------------------------------------------------------------------------
class TestKeyStoreListContext : public KeyStoreListContext
{
Q_OBJECT
public:
TestData data;
int step;
QTimer t;
bool first;
int next_id;
TestKeyStoreListContext(Provider *p)
: KeyStoreListContext(p)
, t(this)
{
step = 0;
next_id = 1;
KeyBundle cert1;
Certificate pub1;
TestCertContext *cc1 = new TestCertContext(provider());
cc1->_props.subject += CertificateInfoPair(CertificateInfoType(CommonName), "Test Cert 1");
pub1.change(cc1);
PrivateKey sec1;
TestRSAContext *rsa1 = new TestRSAContext(provider());
TestPKeyContext *kc1 = new TestPKeyContext(provider());
kc1->setKey(rsa1);
sec1.change(kc1);
cert1.setCertificateChainAndKey(pub1, sec1);
TestKeyStore ks1;
ks1.storeId = "store1";
ks1.name = "Test Store 1";
ks1.certs += cert1;
ks1.avail = false;
data.stores += ks1;
TestKeyStore ks2;
ks2.storeId = "store2";
ks2.name = "Test Store 2";
ks2.readOnly = false;
data.stores += ks2;
rsa1->store = &data.stores[0];
connect(&t, SIGNAL(timeout()), SLOT(do_step()));
}
int findStore(int contextId) const
{
for (int n = 0; n < data.stores.count(); ++n) {
if (data.stores[n].contextId == contextId)
return n;
}
return -1;
}
virtual Context *clone() const
{
return 0;
}
virtual void start()
{
first = true;
emit diagnosticText("qca-test: TestKeyStoreListContext started\n");
t.start(2000);
}
virtual void setUpdatesEnabled(bool enabled)
{
Q_UNUSED(enabled);
}
virtual QList<int> keyStores()
{
QList<int> list;
for (int n = 0; n < data.stores.count(); ++n) {
int id = data.stores[n].contextId;
if (id != -1)
list += id;
}
return list;
}
virtual KeyStore::Type type(int id) const
{
int at = findStore(id);
if (at == -1)
return KeyStore::SmartCard;
return data.stores[at].type;
}
virtual QString storeId(int id) const
{
int at = findStore(id);
if (at == -1)
return QString();
return data.stores[at].storeId;
}
virtual QString name(int id) const
{
int at = findStore(id);
if (at == -1)
return QString();
return data.stores[at].name;
}
virtual bool isReadOnly(int id) const
{
int at = findStore(id);
if (at == -1)
return true;
return data.stores[at].readOnly;
}
virtual QList<KeyStoreEntry::Type> entryTypes(int id) const
{
Q_UNUSED(id);
QList<KeyStoreEntry::Type> list;
list += KeyStoreEntry::TypeKeyBundle;
return list;
}
virtual QList<KeyStoreEntryContext *> entryList(int id)
{
QList<KeyStoreEntryContext *> out;
int at = findStore(id);
if (at == -1)
return out;
TestKeyStore &store = data.stores[at];
for (int n = 0; n < store.certs.count(); ++n) {
TestKeyStoreEntryContext *kse = new TestKeyStoreEntryContext(provider());
kse->_id = QString::number(n);
kse->_name = store.certs[n].certificateChain().primary().commonName();
kse->_storeId = store.storeId;
kse->_storeName = store.name;
kse->kb = store.certs[n];
kse->store = &store;
out += kse;
}
return out;
}
virtual KeyStoreEntryContext *entryPassive(const QString &serialized)
{
if (serialized == "qca-test-1/fake_serialized") {
TestKeyStore &store = data.stores[0];
TestKeyStoreEntryContext *kse = new TestKeyStoreEntryContext(provider());
kse->_id = QString::number(0);
kse->_name = store.certs[0].certificateChain().primary().commonName();
kse->_storeId = store.storeId;
kse->_storeName = store.name;
kse->kb = store.certs[0];
kse->store = &store;
return kse;
} else
return 0;
}
virtual QString writeEntry(int id, const KeyBundle &kb)
{
int at = findStore(id);
if (at == -1)
return QString();
if (data.stores[at].readOnly)
return QString();
data.stores[at].certs += kb;
return QString::number(data.stores[at].certs.count() - 1);
}
virtual bool removeEntry(int id, const QString &entryId)
{
int at = findStore(id);
if (at == -1)
return false;
if (data.stores[at].readOnly)
return false;
int index = entryId.toInt();
if (index < 0 || index >= data.stores[at].certs.count())
return false;
data.stores[at].certs.removeAt(index);
return true;
}
private Q_SLOTS:
void do_step()
{
emit diagnosticText(QString("qca-test: TestKeyStoreListContext do_step %1\n").arg(step));
if (step == 0) {
// make first store available
data.stores[0].contextId = next_id++;
if (first) {
first = false;
emit busyEnd();
} else
emit updated();
} else if (step == 1) {
// make second store available
data.stores[1].contextId = next_id++;
emit updated();
} else if (step == 2) {
// make items in the first store available
data.stores[0].avail = true;
emit storeUpdated(data.stores[0].contextId);
} else if (step == 3) {
// make the first store unavailable
data.stores[0].contextId = -1;
emit updated();
} else if (step == 4) {
// make the first store available
data.stores[0].contextId = next_id++;
emit updated();
} else if (step == 5) {
// make the second store unavailable
data.stores[1].contextId = -1;
emit updated();
} else if (step == 6) {
// make the first store unavailable
data.stores[0].contextId = -1;
emit updated();
} else if (step == 7) {
// do it all over again in 10 seconds
// (2 seconds before, 6 seconds here, 2 seconds after)
t.start(6000);
} else {
step = 0;
data.stores[0].avail = false;
// set interval to 2 seconds
t.start(2000);
return;
}
++step;
}
};
//----------------------------------------------------------------------------
// TestProvider
//----------------------------------------------------------------------------
Provider::Context *TestProvider::createContext(const QString &type)
{
if (type == "keystorelist")
return new TestKeyStoreListContext(this);
else
return 0;
}
//----------------------------------------------------------------------------
// TestPlugin
//----------------------------------------------------------------------------
class TestPlugin : public QObject, public QCAPlugin
{
Q_OBJECT
Q_PLUGIN_METADATA(IID "com.affinix.qca.Plugin/1.0")
Q_INTERFACES(QCAPlugin)
public:
virtual Provider *createProvider()
{
return new TestProvider;
}
};
#include "qca-test.moc"
|