2
* Copyright (C) 2003-2007 Justin Karneges <justin@affinix.com>
3
* Copyright (C) 2004,2005 Brad Hards <bradh@frogmouth.net>
5
* This library is free software; you can redistribute it and/or
6
* modify it under the terms of the GNU Lesser General Public
7
* License as published by the Free Software Foundation; either
8
* version 2.1 of the License, or (at your option) any later version.
10
* This library is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
* Lesser General Public License for more details.
15
* You should have received a copy of the GNU Lesser General Public
16
* License along with this library; if not, write to the Free Software
17
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22
#include "qca_publickey.h"
24
#include "qcaprovider.h"
27
#include <QTextStream>
31
Provider::Context *getContext(const QString &type, const QString &provider);
32
Provider::Context *getContext(const QString &type, Provider *p);
34
bool stringToFile(const QString &fileName, const QString &content)
37
if(!f.open(QFile::WriteOnly))
44
bool stringFromFile(const QString &fileName, QString *s)
47
if(!f.open(QFile::ReadOnly))
54
bool arrayToFile(const QString &fileName, const QByteArray &content)
57
if(!f.open(QFile::WriteOnly))
59
f.write(content.data(), content.size());
63
bool arrayFromFile(const QString &fileName, QByteArray *a)
66
if(!f.open(QFile::ReadOnly))
72
bool ask_passphrase(const QString &fname, void *ptr, SecureArray *answer)
75
asker.ask(Event::StylePassphrase, fname, ptr);
76
asker.waitForResponse();
79
*answer = asker.password();
83
ProviderList allProviders()
85
ProviderList pl = providers();
86
pl += defaultProvider();
90
Provider *providerForName(const QString &name)
92
ProviderList pl = allProviders();
93
for(int n = 0; n < pl.count(); ++n)
95
if(pl[n]->name() == name)
101
bool use_asker_fallback(ConvertResult r)
103
// FIXME: we should only do this if we get ErrorPassphrase?
104
//if(r == ErrorPassphrase)
110
class Getter_GroupSet
113
static QList<DLGroupSet> getList(Provider *p)
115
QList<DLGroupSet> list;
116
const DLGroupContext *c = static_cast<const DLGroupContext *>(getContext("dlgroup", p));
119
list = c->supportedGroupSets();
128
static QList<PBEAlgorithm> getList(Provider *p)
130
QList<PBEAlgorithm> list;
131
const PKeyContext *c = static_cast<const PKeyContext *>(getContext("pkey", p));
134
list = c->supportedPBEAlgorithms();
143
static QList<PKey::Type> getList(Provider *p)
145
QList<PKey::Type> list;
146
const PKeyContext *c = static_cast<const PKeyContext *>(getContext("pkey", p));
149
list = c->supportedTypes();
158
static QList<PKey::Type> getList(Provider *p)
160
QList<PKey::Type> list;
161
const PKeyContext *c = static_cast<const PKeyContext *>(getContext("pkey", p));
164
list = c->supportedIOTypes();
170
template <typename I>
171
class Getter_PublicKey
175
static ConvertResult fromData(PKeyContext *c, const QByteArray &in)
177
return c->publicFromDER(in);
181
static ConvertResult fromData(PKeyContext *c, const QString &in)
183
return c->publicFromPEM(in);
186
static PublicKey getKey(Provider *p, const I &in, const SecureArray &, ConvertResult *result)
189
PKeyContext *c = static_cast<PKeyContext *>(getContext("pkey", p));
193
*result = ErrorDecode;
196
ConvertResult r = fromData(c, in);
207
template <typename I>
208
class Getter_PrivateKey
212
static ConvertResult fromData(PKeyContext *c, const SecureArray &in, const SecureArray &passphrase)
214
return c->privateFromDER(in, passphrase);
218
static ConvertResult fromData(PKeyContext *c, const QString &in, const SecureArray &passphrase)
220
return c->privateFromPEM(in, passphrase);
223
static PrivateKey getKey(Provider *p, const I &in, const SecureArray &passphrase, ConvertResult *result)
226
PKeyContext *c = static_cast<PKeyContext *>(getContext("pkey", p));
230
*result = ErrorDecode;
233
ConvertResult r = fromData(c, in, passphrase);
244
Provider *providerForGroupSet(DLGroupSet set)
246
ProviderList pl = allProviders();
247
for(int n = 0; n < pl.count(); ++n)
249
if(Getter_GroupSet::getList(pl[n]).contains(set))
255
Provider *providerForPBE(PBEAlgorithm alg, PKey::Type ioType, const PKeyContext *prefer = 0)
257
Provider *preferProvider = 0;
260
preferProvider = prefer->provider();
261
if(prefer->supportedPBEAlgorithms().contains(alg) && prefer->supportedIOTypes().contains(ioType))
262
return preferProvider;
265
ProviderList pl = allProviders();
266
for(int n = 0; n < pl.count(); ++n)
268
if(preferProvider && pl[n] == preferProvider)
271
if(Getter_PBE::getList(pl[n]).contains(alg) && Getter_IOType::getList(pl[n]).contains(ioType))
277
Provider *providerForIOType(PKey::Type type, const PKeyContext *prefer = 0)
279
Provider *preferProvider = 0;
282
preferProvider = prefer->provider();
283
if(prefer && prefer->supportedIOTypes().contains(type))
284
return preferProvider;
287
ProviderList pl = allProviders();
288
for(int n = 0; n < pl.count(); ++n)
290
if(preferProvider && pl[n] == preferProvider)
293
if(Getter_IOType::getList(pl[n]).contains(type))
299
template <typename T, typename G>
300
QList<T> getList(const QString &provider)
305
if(!provider.isEmpty())
307
Provider *p = providerForName(provider);
309
list = G::getList(p);
314
ProviderList pl = allProviders();
315
for(int n = 0; n < pl.count(); ++n)
317
QList<T> other = G::getList(pl[n]);
318
for(int k = 0; k < other.count(); ++k)
320
// only add what we don't have in the list
321
if(!list.contains(other[k]))
330
template <typename T, typename G, typename I>
331
T getKey(const QString &provider, const I &in, const SecureArray &passphrase, ConvertResult *result)
336
if(!provider.isEmpty())
338
Provider *p = providerForName(provider);
341
k = G::getKey(p, in, passphrase, result);
346
ProviderList pl = allProviders();
347
for(int n = 0; n < pl.count(); ++n)
350
k = G::getKey(pl[n], in, passphrase, &r);
355
if(r == ErrorPassphrase) // don't loop if we get this
363
PBEAlgorithm get_pbe_default()
365
return PBES2_TripleDES_SHA1;
368
static PrivateKey get_privatekey_der(const SecureArray &der, const QString &fileName, void *ptr, const SecureArray &passphrase, ConvertResult *result, const QString &provider)
372
out = getKey<PrivateKey, Getter_PrivateKey<SecureArray>, SecureArray>(provider, der, passphrase, &r);
374
// error converting without passphrase? maybe a passphrase is needed
375
if(use_asker_fallback(r) && passphrase.isEmpty())
378
if(ask_passphrase(fileName, ptr, &pass))
379
out = getKey<PrivateKey, Getter_PrivateKey<SecureArray>, SecureArray>(provider, der, pass, &r);
386
static PrivateKey get_privatekey_pem(const QString &pem, const QString &fileName, void *ptr, const SecureArray &passphrase, ConvertResult *result, const QString &provider)
390
out = getKey<PrivateKey, Getter_PrivateKey<QString>, QString>(provider, pem, passphrase, &r);
392
// error converting without passphrase? maybe a passphrase is needed
393
if(use_asker_fallback(r) && passphrase.isEmpty())
396
if(ask_passphrase(fileName, ptr, &pass))
397
out = getKey<PrivateKey, Getter_PrivateKey<QString>, QString>(provider, pem, pass, &r);
404
//----------------------------------------------------------------------------
406
//----------------------------------------------------------------------------
408
// adapted from Botan
409
static const unsigned char pkcs_sha1[] =
411
0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2B, 0x0E, 0x03, 0x02,
412
0x1A, 0x05, 0x00, 0x04, 0x14
415
static const unsigned char pkcs_md5[] =
417
0x30, 0x20, 0x30, 0x0C, 0x06, 0x08, 0x2A, 0x86, 0x48, 0x86,
418
0xF7, 0x0D, 0x02, 0x05, 0x05, 0x00, 0x04, 0x10
421
static const unsigned char pkcs_md2[] =
423
0x30, 0x20, 0x30, 0x0C, 0x06, 0x08, 0x2A, 0x86, 0x48, 0x86,
424
0xF7, 0x0D, 0x02, 0x02, 0x05, 0x00, 0x04, 0x10
427
static const unsigned char pkcs_ripemd160[] =
429
0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2B, 0x24, 0x03, 0x02,
430
0x01, 0x05, 0x00, 0x04, 0x14
433
QByteArray get_hash_id(const QString &name)
436
return QByteArray::fromRawData((const char *)pkcs_sha1, sizeof(pkcs_sha1));
437
else if(name == "md5")
438
return QByteArray::fromRawData((const char *)pkcs_md5, sizeof(pkcs_md5));
439
else if(name == "md2")
440
return QByteArray::fromRawData((const char *)pkcs_md2, sizeof(pkcs_md2));
441
else if(name == "ripemd160")
442
return QByteArray::fromRawData((const char *)pkcs_ripemd160, sizeof(pkcs_ripemd160));
447
QByteArray emsa3Encode(const QString &hashName, const QByteArray &digest, int size)
449
QByteArray hash_id = get_hash_id(hashName);
450
if(hash_id.isEmpty())
453
// logic adapted from Botan
454
int basesize = hash_id.size() + digest.size() + 2;
456
size = basesize + 1; // default to 1-byte pad
457
int padlen = size - basesize;
461
QByteArray out(size, (char)0xff); // pad with 0xff
463
out[padlen + 1] = 0x00;
465
memcpy(out.data() + at, hash_id.data(), hash_id.size());
466
at += hash_id.size();
467
memcpy(out.data() + at, digest.data(), digest.size());
471
//----------------------------------------------------------------------------
473
//----------------------------------------------------------------------------
474
class DLGroup::Private
479
Private(const BigInteger &p1, const BigInteger &q1, const BigInteger &g1)
490
DLGroup::DLGroup(const BigInteger &p, const BigInteger &q, const BigInteger &g)
492
d = new Private(p, q, g);
495
DLGroup::DLGroup(const BigInteger &p, const BigInteger &g)
497
d = new Private(p, 0, g);
500
DLGroup::DLGroup(const DLGroup &from)
511
DLGroup & DLGroup::operator=(const DLGroup &from)
517
d = new Private(*from.d);
522
QList<DLGroupSet> DLGroup::supportedGroupSets(const QString &provider)
524
return getList<DLGroupSet, Getter_GroupSet>(provider);
527
bool DLGroup::isNull() const
529
return (d ? false: true);
532
BigInteger DLGroup::p() const
537
BigInteger DLGroup::q() const
542
BigInteger DLGroup::g() const
547
//----------------------------------------------------------------------------
549
//----------------------------------------------------------------------------
560
PKey::PKey(const QString &type, const QString &provider)
561
:Algorithm(type, provider)
566
PKey::PKey(const PKey &from)
578
PKey & PKey::operator=(const PKey &from)
580
Algorithm::operator=(from);
585
void PKey::set(const PKey &k)
590
void PKey::assignToPublic(PKey *dest) const
594
// converting private to public
595
if(dest->isPrivate())
596
static_cast<PKeyContext *>(dest->context())->key()->convertToPublic();
599
void PKey::assignToPrivate(PKey *dest) const
604
QList<PKey::Type> PKey::supportedTypes(const QString &provider)
606
return getList<Type, Getter_Type>(provider);
609
QList<PKey::Type> PKey::supportedIOTypes(const QString &provider)
611
return getList<Type, Getter_IOType>(provider);
614
bool PKey::isNull() const
616
return (!context() ? true : false);
619
PKey::Type PKey::type() const
622
return RSA; // some default so we don't explode
623
return static_cast<const PKeyContext *>(context())->key()->type();
626
int PKey::bitSize() const
628
return static_cast<const PKeyContext *>(context())->key()->bits();
631
bool PKey::isRSA() const
633
return (type() == RSA);
636
bool PKey::isDSA() const
638
return (type() == DSA);
641
bool PKey::isDH() const
643
return (type() == DH);
646
bool PKey::isPublic() const
653
bool PKey::isPrivate() const
657
return static_cast<const PKeyContext *>(context())->key()->isPrivate();
660
bool PKey::canExport() const
662
return static_cast<const PKeyContext *>(context())->key()->canExport();
665
bool PKey::canKeyAgree() const
670
PublicKey PKey::toPublicKey() const
678
PrivateKey PKey::toPrivateKey() const
681
if(!isNull() && isPrivate())
686
RSAPublicKey PKey::toRSAPublicKey() const
689
if(!isNull() && isRSA())
694
RSAPrivateKey PKey::toRSAPrivateKey() const
697
if(!isNull() && isRSA() && isPrivate())
702
DSAPublicKey PKey::toDSAPublicKey() const
705
if(!isNull() && isDSA())
710
DSAPrivateKey PKey::toDSAPrivateKey() const
713
if(!isNull() && isDSA() && isPrivate())
718
DHPublicKey PKey::toDHPublicKey() const
721
if(!isNull() && isDH())
726
DHPrivateKey PKey::toDHPrivateKey() const
729
if(!isNull() && isDH() && isPrivate())
734
bool PKey::operator==(const PKey &a) const
736
if(isNull() || a.isNull() || type() != a.type())
740
return (toPrivateKey().toDER() == a.toPrivateKey().toDER());
742
return (toPublicKey().toDER() == a.toPublicKey().toDER());
745
bool PKey::operator!=(const PKey &a) const
747
return !(*this == a);
750
//----------------------------------------------------------------------------
752
//----------------------------------------------------------------------------
753
PublicKey::PublicKey()
757
PublicKey::PublicKey(const QString &type, const QString &provider)
758
:PKey(type, provider)
762
PublicKey::PublicKey(const PrivateKey &k)
764
set(k.toPublicKey());
767
PublicKey::PublicKey(const QString &fileName)
769
*this = fromPEMFile(fileName, 0, QString());
772
PublicKey::PublicKey(const PublicKey &from)
777
PublicKey::~PublicKey()
781
PublicKey & PublicKey::operator=(const PublicKey &from)
783
PKey::operator=(from);
787
RSAPublicKey PublicKey::toRSA() const
789
return toRSAPublicKey();
792
DSAPublicKey PublicKey::toDSA() const
794
return toDSAPublicKey();
797
DHPublicKey PublicKey::toDH() const
799
return toDHPublicKey();
802
bool PublicKey::canEncrypt() const
807
bool PublicKey::canVerify() const
809
return (isRSA() || isDSA());
812
int PublicKey::maximumEncryptSize(EncryptionAlgorithm alg) const
814
return static_cast<const PKeyContext *>(context())->key()->maximumEncryptSize(alg);
817
SecureArray PublicKey::encrypt(const SecureArray &a, EncryptionAlgorithm alg)
819
return static_cast<PKeyContext *>(context())->key()->encrypt(a, alg);
822
void PublicKey::startVerify(SignatureAlgorithm alg, SignatureFormat format)
824
if(isDSA() && format == DefaultFormat)
826
static_cast<PKeyContext *>(context())->key()->startVerify(alg, format);
829
void PublicKey::update(const MemoryRegion &a)
831
static_cast<PKeyContext *>(context())->key()->update(a);
834
bool PublicKey::validSignature(const QByteArray &sig)
836
return static_cast<PKeyContext *>(context())->key()->endVerify(sig);
839
bool PublicKey::verifyMessage(const MemoryRegion &a, const QByteArray &sig, SignatureAlgorithm alg, SignatureFormat format)
841
startVerify(alg, format);
843
return validSignature(sig);
846
QByteArray PublicKey::toDER() const
849
const PKeyContext *cur = static_cast<const PKeyContext *>(context());
850
Provider *p = providerForIOType(type(), cur);
853
if(cur->provider() == p)
855
out = cur->publicToDER();
859
PKeyContext *pk = static_cast<PKeyContext *>(getContext("pkey", p));
860
if(pk->importKey(cur->key()))
861
out = pk->publicToDER();
867
QString PublicKey::toPEM() const
870
const PKeyContext *cur = static_cast<const PKeyContext *>(context());
871
Provider *p = providerForIOType(type(), cur);
874
if(cur->provider() == p)
876
out = cur->publicToPEM();
880
PKeyContext *pk = static_cast<PKeyContext *>(getContext("pkey", p));
881
if(pk->importKey(cur->key()))
882
out = pk->publicToPEM();
888
bool PublicKey::toPEMFile(const QString &fileName) const
890
return stringToFile(fileName, toPEM());
893
PublicKey PublicKey::fromDER(const QByteArray &a, ConvertResult *result, const QString &provider)
895
return getKey<PublicKey, Getter_PublicKey<QByteArray>, QByteArray>(provider, a, SecureArray(), result);
898
PublicKey PublicKey::fromPEM(const QString &s, ConvertResult *result, const QString &provider)
900
return getKey<PublicKey, Getter_PublicKey<QString>, QString>(provider, s, SecureArray(), result);
903
PublicKey PublicKey::fromPEMFile(const QString &fileName, ConvertResult *result, const QString &provider)
906
if(!stringFromFile(fileName, &pem))
912
return fromPEM(pem, result, provider);
915
//----------------------------------------------------------------------------
917
//----------------------------------------------------------------------------
918
PrivateKey::PrivateKey()
922
PrivateKey::PrivateKey(const QString &type, const QString &provider)
923
:PKey(type, provider)
927
PrivateKey::PrivateKey(const QString &fileName, const SecureArray &passphrase)
929
*this = fromPEMFile(fileName, passphrase, 0, QString());
932
PrivateKey::PrivateKey(const PrivateKey &from)
937
PrivateKey::~PrivateKey()
941
PrivateKey & PrivateKey::operator=(const PrivateKey &from)
943
PKey::operator=(from);
947
RSAPrivateKey PrivateKey::toRSA() const
949
return toRSAPrivateKey();
952
DSAPrivateKey PrivateKey::toDSA() const
954
return toDSAPrivateKey();
957
DHPrivateKey PrivateKey::toDH() const
959
return toDHPrivateKey();
962
bool PrivateKey::canDecrypt() const
967
bool PrivateKey::canSign() const
969
return (isRSA() || isDSA());
972
bool PrivateKey::decrypt(const SecureArray &in, SecureArray *out, EncryptionAlgorithm alg)
974
return static_cast<PKeyContext *>(context())->key()->decrypt(in, out, alg);
977
void PrivateKey::startSign(SignatureAlgorithm alg, SignatureFormat format)
979
if(isDSA() && format == DefaultFormat)
981
static_cast<PKeyContext *>(context())->key()->startSign(alg, format);
984
void PrivateKey::update(const MemoryRegion &a)
986
static_cast<PKeyContext *>(context())->key()->update(a);
989
QByteArray PrivateKey::signature()
991
return static_cast<PKeyContext *>(context())->key()->endSign();
994
QByteArray PrivateKey::signMessage(const MemoryRegion &a, SignatureAlgorithm alg, SignatureFormat format)
996
startSign(alg, format);
1001
SymmetricKey PrivateKey::deriveKey(const PublicKey &theirs)
1003
const PKeyContext *theirContext = static_cast<const PKeyContext *>(theirs.context());
1004
return static_cast<PKeyContext *>(context())->key()->deriveKey(*(theirContext->key()));
1007
QList<PBEAlgorithm> PrivateKey::supportedPBEAlgorithms(const QString &provider)
1009
return getList<PBEAlgorithm, Getter_PBE>(provider);
1012
SecureArray PrivateKey::toDER(const SecureArray &passphrase, PBEAlgorithm pbe) const
1015
if(pbe == PBEDefault)
1016
pbe = get_pbe_default();
1017
const PKeyContext *cur = static_cast<const PKeyContext *>(context());
1018
Provider *p = providerForPBE(pbe, type(), cur);
1021
if(cur->provider() == p)
1023
out = cur->privateToDER(passphrase, pbe);
1027
PKeyContext *pk = static_cast<PKeyContext *>(getContext("pkey", p));
1028
if(pk->importKey(cur->key()))
1029
out = pk->privateToDER(passphrase, pbe);
1035
QString PrivateKey::toPEM(const SecureArray &passphrase, PBEAlgorithm pbe) const
1038
if(pbe == PBEDefault)
1039
pbe = get_pbe_default();
1040
const PKeyContext *cur = static_cast<const PKeyContext *>(context());
1041
Provider *p = providerForPBE(pbe, type(), cur);
1044
if(cur->provider() == p)
1046
out = cur->privateToPEM(passphrase, pbe);
1050
PKeyContext *pk = static_cast<PKeyContext *>(getContext("pkey", p));
1051
if(pk->importKey(cur->key()))
1052
out = pk->privateToPEM(passphrase, pbe);
1058
bool PrivateKey::toPEMFile(const QString &fileName, const SecureArray &passphrase, PBEAlgorithm pbe) const
1060
return stringToFile(fileName, toPEM(passphrase, pbe));
1063
PrivateKey PrivateKey::fromDER(const SecureArray &a, const SecureArray &passphrase, ConvertResult *result, const QString &provider)
1065
return get_privatekey_der(a, QString(), (void *)&a, passphrase, result, provider);
1068
PrivateKey PrivateKey::fromPEM(const QString &s, const SecureArray &passphrase, ConvertResult *result, const QString &provider)
1070
return get_privatekey_pem(s, QString(), (void *)&s, passphrase, result, provider);
1073
PrivateKey PrivateKey::fromPEMFile(const QString &fileName, const SecureArray &passphrase, ConvertResult *result, const QString &provider)
1076
if(!stringFromFile(fileName, &pem))
1079
*result = ErrorFile;
1080
return PrivateKey();
1082
return get_privatekey_pem(pem, fileName, 0, passphrase, result, provider);
1085
//----------------------------------------------------------------------------
1087
//----------------------------------------------------------------------------
1088
class KeyGenerator::Private : public QObject
1092
KeyGenerator *parent;
1093
bool blocking, wasBlocking;
1101
Private(KeyGenerator *_parent) : QObject(_parent), parent(_parent)
1140
emit parent->finished();
1148
dc->getResult(&p, &q, &g);
1149
group = DLGroup(p, q, g);
1155
emit parent->finished();
1159
KeyGenerator::KeyGenerator(QObject *parent)
1162
d = new Private(this);
1166
KeyGenerator::~KeyGenerator()
1171
bool KeyGenerator::blockingEnabled() const
1176
void KeyGenerator::setBlockingEnabled(bool b)
1181
bool KeyGenerator::isBusy() const
1183
return (d->k ? true: false);
1186
PrivateKey KeyGenerator::createRSA(int bits, int exp, const QString &provider)
1189
return PrivateKey();
1191
d->key = PrivateKey();
1192
d->wasBlocking = d->blocking;
1193
d->k = static_cast<RSAContext *>(getContext("rsa", provider));
1194
d->dest = static_cast<PKeyContext *>(getContext("pkey", d->k->provider()));
1198
d->k->moveToThread(thread());
1200
connect(d->k, SIGNAL(finished()), d, SLOT(done()));
1201
static_cast<RSAContext *>(d->k)->createPrivate(bits, exp, false);
1205
static_cast<RSAContext *>(d->k)->createPrivate(bits, exp, true);
1212
PrivateKey KeyGenerator::createDSA(const DLGroup &domain, const QString &provider)
1215
return PrivateKey();
1217
d->key = PrivateKey();
1218
d->wasBlocking = d->blocking;
1219
d->k = static_cast<DSAContext *>(getContext("dsa", provider));
1220
d->dest = static_cast<PKeyContext *>(getContext("pkey", d->k->provider()));
1224
d->k->moveToThread(thread());
1226
connect(d->k, SIGNAL(finished()), d, SLOT(done()));
1227
static_cast<DSAContext *>(d->k)->createPrivate(domain, false);
1231
static_cast<DSAContext *>(d->k)->createPrivate(domain, true);
1238
PrivateKey KeyGenerator::createDH(const DLGroup &domain, const QString &provider)
1241
return PrivateKey();
1243
d->key = PrivateKey();
1244
d->wasBlocking = d->blocking;
1245
d->k = static_cast<DHContext *>(getContext("dh", provider));
1246
d->dest = static_cast<PKeyContext *>(getContext("pkey", d->k->provider()));
1250
d->k->moveToThread(thread());
1252
connect(d->k, SIGNAL(finished()), d, SLOT(done()));
1253
static_cast<DHContext *>(d->k)->createPrivate(domain, false);
1257
static_cast<DHContext *>(d->k)->createPrivate(domain, true);
1264
PrivateKey KeyGenerator::key() const
1269
DLGroup KeyGenerator::createDLGroup(QCA::DLGroupSet set, const QString &provider)
1275
if(!provider.isEmpty())
1276
p = providerForName(provider);
1278
p = providerForGroupSet(set);
1280
d->group = DLGroup();
1281
d->wasBlocking = d->blocking;
1282
d->dc = static_cast<DLGroupContext *>(getContext("dlgroup", p));
1286
connect(d->dc, SIGNAL(finished()), d, SLOT(done_group()));
1287
d->dc->fetchGroup(set, false);
1291
d->dc->fetchGroup(set, true);
1298
DLGroup KeyGenerator::dlGroup() const
1303
//----------------------------------------------------------------------------
1305
//----------------------------------------------------------------------------
1306
RSAPublicKey::RSAPublicKey()
1310
RSAPublicKey::RSAPublicKey(const BigInteger &n, const BigInteger &e, const QString &provider)
1312
RSAContext *k = static_cast<RSAContext *>(getContext("rsa", provider));
1313
k->createPublic(n, e);
1314
PKeyContext *c = static_cast<PKeyContext *>(getContext("pkey", k->provider()));
1319
RSAPublicKey::RSAPublicKey(const RSAPrivateKey &k)
1324
BigInteger RSAPublicKey::n() const
1326
return static_cast<const RSAContext *>(static_cast<const PKeyContext *>(context())->key())->n();
1329
BigInteger RSAPublicKey::e() const
1331
return static_cast<const RSAContext *>(static_cast<const PKeyContext *>(context())->key())->e();
1334
//----------------------------------------------------------------------------
1336
//----------------------------------------------------------------------------
1337
RSAPrivateKey::RSAPrivateKey()
1341
RSAPrivateKey::RSAPrivateKey(const BigInteger &n, const BigInteger &e, const BigInteger &p, const BigInteger &q, const BigInteger &d, const QString &provider)
1343
RSAContext *k = static_cast<RSAContext *>(getContext("rsa", provider));
1344
k->createPrivate(n, e, p, q, d);
1345
PKeyContext *c = static_cast<PKeyContext *>(getContext("pkey", k->provider()));
1350
BigInteger RSAPrivateKey::n() const
1352
return static_cast<const RSAContext *>(static_cast<const PKeyContext *>(context())->key())->n();
1355
BigInteger RSAPrivateKey::e() const
1357
return static_cast<const RSAContext *>(static_cast<const PKeyContext *>(context())->key())->e();
1360
BigInteger RSAPrivateKey::p() const
1362
return static_cast<const RSAContext *>(static_cast<const PKeyContext *>(context())->key())->p();
1365
BigInteger RSAPrivateKey::q() const
1367
return static_cast<const RSAContext *>(static_cast<const PKeyContext *>(context())->key())->q();
1370
BigInteger RSAPrivateKey::d() const
1372
return static_cast<const RSAContext *>(static_cast<const PKeyContext *>(context())->key())->d();
1375
//----------------------------------------------------------------------------
1377
//----------------------------------------------------------------------------
1378
DSAPublicKey::DSAPublicKey()
1382
DSAPublicKey::DSAPublicKey(const DLGroup &domain, const BigInteger &y, const QString &provider)
1384
DSAContext *k = static_cast<DSAContext *>(getContext("dsa", provider));
1385
k->createPublic(domain, y);
1386
PKeyContext *c = static_cast<PKeyContext *>(getContext("pkey", k->provider()));
1391
DSAPublicKey::DSAPublicKey(const DSAPrivateKey &k)
1396
DLGroup DSAPublicKey::domain() const
1398
return static_cast<const DSAContext *>(static_cast<const PKeyContext *>(context())->key())->domain();
1401
BigInteger DSAPublicKey::y() const
1403
return static_cast<const DSAContext *>(static_cast<const PKeyContext *>(context())->key())->y();
1406
//----------------------------------------------------------------------------
1408
//----------------------------------------------------------------------------
1409
DSAPrivateKey::DSAPrivateKey()
1413
DSAPrivateKey::DSAPrivateKey(const DLGroup &domain, const BigInteger &y, const BigInteger &x, const QString &provider)
1415
DSAContext *k = static_cast<DSAContext *>(getContext("dsa", provider));
1416
k->createPrivate(domain, y, x);
1417
PKeyContext *c = static_cast<PKeyContext *>(getContext("pkey", k->provider()));
1422
DLGroup DSAPrivateKey::domain() const
1424
return static_cast<const DSAContext *>(static_cast<const PKeyContext *>(context())->key())->domain();
1427
BigInteger DSAPrivateKey::y() const
1429
return static_cast<const DSAContext *>(static_cast<const PKeyContext *>(context())->key())->y();
1432
BigInteger DSAPrivateKey::x() const
1434
return static_cast<const DSAContext *>(static_cast<const PKeyContext *>(context())->key())->x();
1437
//----------------------------------------------------------------------------
1439
//----------------------------------------------------------------------------
1440
DHPublicKey::DHPublicKey()
1444
DHPublicKey::DHPublicKey(const DLGroup &domain, const BigInteger &y, const QString &provider)
1446
DHContext *k = static_cast<DHContext *>(getContext("dh", provider));
1447
k->createPublic(domain, y);
1448
PKeyContext *c = static_cast<PKeyContext *>(getContext("pkey", k->provider()));
1453
DHPublicKey::DHPublicKey(const DHPrivateKey &k)
1458
DLGroup DHPublicKey::domain() const
1460
return static_cast<const DHContext *>(static_cast<const PKeyContext *>(context())->key())->domain();
1463
BigInteger DHPublicKey::y() const
1465
return static_cast<const DHContext *>(static_cast<const PKeyContext *>(context())->key())->y();
1468
//----------------------------------------------------------------------------
1470
//----------------------------------------------------------------------------
1471
DHPrivateKey::DHPrivateKey()
1475
DHPrivateKey::DHPrivateKey(const DLGroup &domain, const BigInteger &y, const BigInteger &x, const QString &provider)
1477
DHContext *k = static_cast<DHContext *>(getContext("dh", provider));
1478
k->createPrivate(domain, y, x);
1479
PKeyContext *c = static_cast<PKeyContext *>(getContext("pkey", k->provider()));
1484
DLGroup DHPrivateKey::domain() const
1486
return static_cast<const DHContext *>(static_cast<const PKeyContext *>(context())->key())->domain();
1489
BigInteger DHPrivateKey::y() const
1491
return static_cast<const DHContext *>(static_cast<const PKeyContext *>(context())->key())->y();
1494
BigInteger DHPrivateKey::x() const
1496
return static_cast<const DHContext *>(static_cast<const PKeyContext *>(context())->key())->x();
1501
#include "qca_publickey.moc"