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 02110-1301, USA
21
#include "qca_publickey.h"
23
#include "qcaprovider.h"
26
#include <QTextStream>
30
Provider::Context *getContext(const QString &type, const QString &provider);
31
Provider::Context *getContext(const QString &type, Provider *p);
33
bool stringToFile(const QString &fileName, const QString &content)
36
if(!f.open(QFile::WriteOnly))
43
bool stringFromFile(const QString &fileName, QString *s)
46
if(!f.open(QFile::ReadOnly))
53
bool arrayToFile(const QString &fileName, const QByteArray &content)
56
if(!f.open(QFile::WriteOnly))
58
f.write(content.data(), content.size());
62
bool arrayFromFile(const QString &fileName, QByteArray *a)
65
if(!f.open(QFile::ReadOnly))
71
bool ask_passphrase(const QString &fname, void *ptr, SecureArray *answer)
74
asker.ask(Event::StylePassphrase, fname, ptr);
75
asker.waitForResponse();
78
*answer = asker.password();
82
ProviderList allProviders()
84
ProviderList pl = providers();
85
pl += defaultProvider();
89
Provider *providerForName(const QString &name)
91
ProviderList pl = allProviders();
92
for(int n = 0; n < pl.count(); ++n)
94
if(pl[n]->name() == name)
100
bool use_asker_fallback(ConvertResult r)
102
// FIXME: we should only do this if we get ErrorPassphrase?
103
//if(r == ErrorPassphrase)
109
class Getter_GroupSet
112
static QList<DLGroupSet> getList(Provider *p)
114
QList<DLGroupSet> list;
115
const DLGroupContext *c = static_cast<const DLGroupContext *>(getContext("dlgroup", p));
118
list = c->supportedGroupSets();
127
static QList<PBEAlgorithm> getList(Provider *p)
129
QList<PBEAlgorithm> list;
130
const PKeyContext *c = static_cast<const PKeyContext *>(getContext("pkey", p));
133
list = c->supportedPBEAlgorithms();
142
static QList<PKey::Type> getList(Provider *p)
144
QList<PKey::Type> list;
145
const PKeyContext *c = static_cast<const PKeyContext *>(getContext("pkey", p));
148
list = c->supportedTypes();
157
static QList<PKey::Type> getList(Provider *p)
159
QList<PKey::Type> list;
160
const PKeyContext *c = static_cast<const PKeyContext *>(getContext("pkey", p));
163
list = c->supportedIOTypes();
169
template <typename I>
170
class Getter_PublicKey
174
static ConvertResult fromData(PKeyContext *c, const QByteArray &in)
176
return c->publicFromDER(in);
180
static ConvertResult fromData(PKeyContext *c, const QString &in)
182
return c->publicFromPEM(in);
185
static PublicKey getKey(Provider *p, const I &in, const SecureArray &, ConvertResult *result)
188
PKeyContext *c = static_cast<PKeyContext *>(getContext("pkey", p));
192
*result = ErrorDecode;
195
ConvertResult r = fromData(c, in);
206
template <typename I>
207
class Getter_PrivateKey
211
static ConvertResult fromData(PKeyContext *c, const SecureArray &in, const SecureArray &passphrase)
213
return c->privateFromDER(in, passphrase);
217
static ConvertResult fromData(PKeyContext *c, const QString &in, const SecureArray &passphrase)
219
return c->privateFromPEM(in, passphrase);
222
static PrivateKey getKey(Provider *p, const I &in, const SecureArray &passphrase, ConvertResult *result)
225
PKeyContext *c = static_cast<PKeyContext *>(getContext("pkey", p));
229
*result = ErrorDecode;
232
ConvertResult r = fromData(c, in, passphrase);
243
Provider *providerForGroupSet(DLGroupSet set)
245
ProviderList pl = allProviders();
246
for(int n = 0; n < pl.count(); ++n)
248
if(Getter_GroupSet::getList(pl[n]).contains(set))
254
Provider *providerForPBE(PBEAlgorithm alg, PKey::Type ioType, const PKeyContext *prefer = 0)
256
Provider *preferProvider = 0;
259
preferProvider = prefer->provider();
260
if(prefer->supportedPBEAlgorithms().contains(alg) && prefer->supportedIOTypes().contains(ioType))
261
return preferProvider;
264
ProviderList pl = allProviders();
265
for(int n = 0; n < pl.count(); ++n)
267
if(preferProvider && pl[n] == preferProvider)
270
if(Getter_PBE::getList(pl[n]).contains(alg) && Getter_IOType::getList(pl[n]).contains(ioType))
276
Provider *providerForIOType(PKey::Type type, const PKeyContext *prefer = 0)
278
Provider *preferProvider = 0;
281
preferProvider = prefer->provider();
282
if(prefer && prefer->supportedIOTypes().contains(type))
283
return preferProvider;
286
ProviderList pl = allProviders();
287
for(int n = 0; n < pl.count(); ++n)
289
if(preferProvider && pl[n] == preferProvider)
292
if(Getter_IOType::getList(pl[n]).contains(type))
298
template <typename T, typename G>
299
QList<T> getList(const QString &provider)
304
if(!provider.isEmpty())
306
Provider *p = providerForName(provider);
308
list = G::getList(p);
313
ProviderList pl = allProviders();
314
for(int n = 0; n < pl.count(); ++n)
316
QList<T> other = G::getList(pl[n]);
317
for(int k = 0; k < other.count(); ++k)
319
// only add what we don't have in the list
320
if(!list.contains(other[k]))
329
template <typename T, typename G, typename I>
330
T getKey(const QString &provider, const I &in, const SecureArray &passphrase, ConvertResult *result)
335
if(!provider.isEmpty())
337
Provider *p = providerForName(provider);
340
k = G::getKey(p, in, passphrase, result);
345
ProviderList pl = allProviders();
346
for(int n = 0; n < pl.count(); ++n)
349
k = G::getKey(pl[n], in, passphrase, &r);
354
if(r == ErrorPassphrase) // don't loop if we get this
362
PBEAlgorithm get_pbe_default()
364
return PBES2_TripleDES_SHA1;
367
static PrivateKey get_privatekey_der(const SecureArray &der, const QString &fileName, void *ptr, const SecureArray &passphrase, ConvertResult *result, const QString &provider)
371
out = getKey<PrivateKey, Getter_PrivateKey<SecureArray>, SecureArray>(provider, der, passphrase, &r);
373
// error converting without passphrase? maybe a passphrase is needed
374
if(use_asker_fallback(r) && passphrase.isEmpty())
377
if(ask_passphrase(fileName, ptr, &pass))
378
out = getKey<PrivateKey, Getter_PrivateKey<SecureArray>, SecureArray>(provider, der, pass, &r);
385
static PrivateKey get_privatekey_pem(const QString &pem, const QString &fileName, void *ptr, const SecureArray &passphrase, ConvertResult *result, const QString &provider)
389
out = getKey<PrivateKey, Getter_PrivateKey<QString>, QString>(provider, pem, passphrase, &r);
391
// error converting without passphrase? maybe a passphrase is needed
392
if(use_asker_fallback(r) && passphrase.isEmpty())
395
if(ask_passphrase(fileName, ptr, &pass))
396
out = getKey<PrivateKey, Getter_PrivateKey<QString>, QString>(provider, pem, pass, &r);
403
//----------------------------------------------------------------------------
405
//----------------------------------------------------------------------------
407
// adapted from Botan
408
static const unsigned char pkcs_sha1[] =
410
0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2B, 0x0E, 0x03, 0x02,
411
0x1A, 0x05, 0x00, 0x04, 0x14
414
static const unsigned char pkcs_md5[] =
416
0x30, 0x20, 0x30, 0x0C, 0x06, 0x08, 0x2A, 0x86, 0x48, 0x86,
417
0xF7, 0x0D, 0x02, 0x05, 0x05, 0x00, 0x04, 0x10
420
static const unsigned char pkcs_md2[] =
422
0x30, 0x20, 0x30, 0x0C, 0x06, 0x08, 0x2A, 0x86, 0x48, 0x86,
423
0xF7, 0x0D, 0x02, 0x02, 0x05, 0x00, 0x04, 0x10
426
static const unsigned char pkcs_ripemd160[] =
428
0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2B, 0x24, 0x03, 0x02,
429
0x01, 0x05, 0x00, 0x04, 0x14
432
QByteArray get_hash_id(const QString &name)
435
return QByteArray::fromRawData((const char *)pkcs_sha1, sizeof(pkcs_sha1));
436
else if(name == "md5")
437
return QByteArray::fromRawData((const char *)pkcs_md5, sizeof(pkcs_md5));
438
else if(name == "md2")
439
return QByteArray::fromRawData((const char *)pkcs_md2, sizeof(pkcs_md2));
440
else if(name == "ripemd160")
441
return QByteArray::fromRawData((const char *)pkcs_ripemd160, sizeof(pkcs_ripemd160));
446
QByteArray emsa3Encode(const QString &hashName, const QByteArray &digest, int size)
448
QByteArray hash_id = get_hash_id(hashName);
449
if(hash_id.isEmpty())
452
// logic adapted from Botan
453
int basesize = hash_id.size() + digest.size() + 2;
455
size = basesize + 1; // default to 1-byte pad
456
int padlen = size - basesize;
460
QByteArray out(size, (char)0xff); // pad with 0xff
462
out[padlen + 1] = 0x00;
464
memcpy(out.data() + at, hash_id.data(), hash_id.size());
465
at += hash_id.size();
466
memcpy(out.data() + at, digest.data(), digest.size());
470
//----------------------------------------------------------------------------
472
//----------------------------------------------------------------------------
473
class DLGroup::Private
478
Private(const BigInteger &p1, const BigInteger &q1, const BigInteger &g1)
489
DLGroup::DLGroup(const BigInteger &p, const BigInteger &q, const BigInteger &g)
491
d = new Private(p, q, g);
494
DLGroup::DLGroup(const BigInteger &p, const BigInteger &g)
496
d = new Private(p, 0, g);
499
DLGroup::DLGroup(const DLGroup &from)
510
DLGroup & DLGroup::operator=(const DLGroup &from)
516
d = new Private(*from.d);
521
QList<DLGroupSet> DLGroup::supportedGroupSets(const QString &provider)
523
return getList<DLGroupSet, Getter_GroupSet>(provider);
526
bool DLGroup::isNull() const
528
return (d ? false: true);
531
BigInteger DLGroup::p() const
536
BigInteger DLGroup::q() const
541
BigInteger DLGroup::g() const
546
//----------------------------------------------------------------------------
548
//----------------------------------------------------------------------------
559
PKey::PKey(const QString &type, const QString &provider)
560
:Algorithm(type, provider)
565
PKey::PKey(const PKey &from)
577
PKey & PKey::operator=(const PKey &from)
579
Algorithm::operator=(from);
584
void PKey::set(const PKey &k)
589
void PKey::assignToPublic(PKey *dest) const
593
// converting private to public
594
if(dest->isPrivate())
595
static_cast<PKeyContext *>(dest->context())->key()->convertToPublic();
598
void PKey::assignToPrivate(PKey *dest) const
603
QList<PKey::Type> PKey::supportedTypes(const QString &provider)
605
return getList<Type, Getter_Type>(provider);
608
QList<PKey::Type> PKey::supportedIOTypes(const QString &provider)
610
return getList<Type, Getter_IOType>(provider);
613
bool PKey::isNull() const
615
return (!context() ? true : false);
618
PKey::Type PKey::type() const
621
return RSA; // some default so we don't explode
622
return static_cast<const PKeyContext *>(context())->key()->type();
625
int PKey::bitSize() const
627
return static_cast<const PKeyContext *>(context())->key()->bits();
630
bool PKey::isRSA() const
632
return (type() == RSA);
635
bool PKey::isDSA() const
637
return (type() == DSA);
640
bool PKey::isDH() const
642
return (type() == DH);
645
bool PKey::isPublic() const
652
bool PKey::isPrivate() const
656
return static_cast<const PKeyContext *>(context())->key()->isPrivate();
659
bool PKey::canExport() const
661
return static_cast<const PKeyContext *>(context())->key()->canExport();
664
bool PKey::canKeyAgree() const
669
PublicKey PKey::toPublicKey() const
677
PrivateKey PKey::toPrivateKey() const
680
if(!isNull() && isPrivate())
685
RSAPublicKey PKey::toRSAPublicKey() const
688
if(!isNull() && isRSA())
693
RSAPrivateKey PKey::toRSAPrivateKey() const
696
if(!isNull() && isRSA() && isPrivate())
701
DSAPublicKey PKey::toDSAPublicKey() const
704
if(!isNull() && isDSA())
709
DSAPrivateKey PKey::toDSAPrivateKey() const
712
if(!isNull() && isDSA() && isPrivate())
717
DHPublicKey PKey::toDHPublicKey() const
720
if(!isNull() && isDH())
725
DHPrivateKey PKey::toDHPrivateKey() const
728
if(!isNull() && isDH() && isPrivate())
733
bool PKey::operator==(const PKey &a) const
735
if(isNull() || a.isNull() || type() != a.type())
739
return (toPrivateKey().toDER() == a.toPrivateKey().toDER());
741
return (toPublicKey().toDER() == a.toPublicKey().toDER());
744
bool PKey::operator!=(const PKey &a) const
746
return !(*this == a);
749
//----------------------------------------------------------------------------
751
//----------------------------------------------------------------------------
752
PublicKey::PublicKey()
756
PublicKey::PublicKey(const QString &type, const QString &provider)
757
:PKey(type, provider)
761
PublicKey::PublicKey(const PrivateKey &k)
763
set(k.toPublicKey());
766
PublicKey::PublicKey(const QString &fileName)
768
*this = fromPEMFile(fileName, 0, QString());
771
PublicKey::PublicKey(const PublicKey &from)
776
PublicKey::~PublicKey()
780
PublicKey & PublicKey::operator=(const PublicKey &from)
782
PKey::operator=(from);
786
RSAPublicKey PublicKey::toRSA() const
788
return toRSAPublicKey();
791
DSAPublicKey PublicKey::toDSA() const
793
return toDSAPublicKey();
796
DHPublicKey PublicKey::toDH() const
798
return toDHPublicKey();
801
bool PublicKey::canEncrypt() const
806
bool PublicKey::canVerify() const
808
return (isRSA() || isDSA());
811
int PublicKey::maximumEncryptSize(EncryptionAlgorithm alg) const
813
return static_cast<const PKeyContext *>(context())->key()->maximumEncryptSize(alg);
816
SecureArray PublicKey::encrypt(const SecureArray &a, EncryptionAlgorithm alg)
818
return static_cast<PKeyContext *>(context())->key()->encrypt(a, alg);
821
void PublicKey::startVerify(SignatureAlgorithm alg, SignatureFormat format)
823
if(isDSA() && format == DefaultFormat)
825
static_cast<PKeyContext *>(context())->key()->startVerify(alg, format);
828
void PublicKey::update(const MemoryRegion &a)
830
static_cast<PKeyContext *>(context())->key()->update(a);
833
bool PublicKey::validSignature(const QByteArray &sig)
835
return static_cast<PKeyContext *>(context())->key()->endVerify(sig);
838
bool PublicKey::verifyMessage(const MemoryRegion &a, const QByteArray &sig, SignatureAlgorithm alg, SignatureFormat format)
840
startVerify(alg, format);
842
return validSignature(sig);
845
QByteArray PublicKey::toDER() const
848
const PKeyContext *cur = static_cast<const PKeyContext *>(context());
849
Provider *p = providerForIOType(type(), cur);
852
if(cur->provider() == p)
854
out = cur->publicToDER();
858
PKeyContext *pk = static_cast<PKeyContext *>(getContext("pkey", p));
859
if(pk->importKey(cur->key()))
860
out = pk->publicToDER();
866
QString PublicKey::toPEM() const
869
const PKeyContext *cur = static_cast<const PKeyContext *>(context());
870
Provider *p = providerForIOType(type(), cur);
873
if(cur->provider() == p)
875
out = cur->publicToPEM();
879
PKeyContext *pk = static_cast<PKeyContext *>(getContext("pkey", p));
880
if(pk->importKey(cur->key()))
881
out = pk->publicToPEM();
887
bool PublicKey::toPEMFile(const QString &fileName) const
889
return stringToFile(fileName, toPEM());
892
PublicKey PublicKey::fromDER(const QByteArray &a, ConvertResult *result, const QString &provider)
894
return getKey<PublicKey, Getter_PublicKey<QByteArray>, QByteArray>(provider, a, SecureArray(), result);
897
PublicKey PublicKey::fromPEM(const QString &s, ConvertResult *result, const QString &provider)
899
return getKey<PublicKey, Getter_PublicKey<QString>, QString>(provider, s, SecureArray(), result);
902
PublicKey PublicKey::fromPEMFile(const QString &fileName, ConvertResult *result, const QString &provider)
905
if(!stringFromFile(fileName, &pem))
911
return fromPEM(pem, result, provider);
914
//----------------------------------------------------------------------------
916
//----------------------------------------------------------------------------
917
PrivateKey::PrivateKey()
921
PrivateKey::PrivateKey(const QString &type, const QString &provider)
922
:PKey(type, provider)
926
PrivateKey::PrivateKey(const QString &fileName, const SecureArray &passphrase)
928
*this = fromPEMFile(fileName, passphrase, 0, QString());
931
PrivateKey::PrivateKey(const PrivateKey &from)
936
PrivateKey::~PrivateKey()
940
PrivateKey & PrivateKey::operator=(const PrivateKey &from)
942
PKey::operator=(from);
946
RSAPrivateKey PrivateKey::toRSA() const
948
return toRSAPrivateKey();
951
DSAPrivateKey PrivateKey::toDSA() const
953
return toDSAPrivateKey();
956
DHPrivateKey PrivateKey::toDH() const
958
return toDHPrivateKey();
961
bool PrivateKey::canDecrypt() const
966
bool PrivateKey::canSign() const
968
return (isRSA() || isDSA());
971
bool PrivateKey::decrypt(const SecureArray &in, SecureArray *out, EncryptionAlgorithm alg)
973
return static_cast<PKeyContext *>(context())->key()->decrypt(in, out, alg);
976
void PrivateKey::startSign(SignatureAlgorithm alg, SignatureFormat format)
978
if(isDSA() && format == DefaultFormat)
980
static_cast<PKeyContext *>(context())->key()->startSign(alg, format);
983
void PrivateKey::update(const MemoryRegion &a)
985
static_cast<PKeyContext *>(context())->key()->update(a);
988
QByteArray PrivateKey::signature()
990
return static_cast<PKeyContext *>(context())->key()->endSign();
993
QByteArray PrivateKey::signMessage(const MemoryRegion &a, SignatureAlgorithm alg, SignatureFormat format)
995
startSign(alg, format);
1000
SymmetricKey PrivateKey::deriveKey(const PublicKey &theirs)
1002
const PKeyContext *theirContext = static_cast<const PKeyContext *>(theirs.context());
1003
return static_cast<PKeyContext *>(context())->key()->deriveKey(*(theirContext->key()));
1006
QList<PBEAlgorithm> PrivateKey::supportedPBEAlgorithms(const QString &provider)
1008
return getList<PBEAlgorithm, Getter_PBE>(provider);
1011
SecureArray PrivateKey::toDER(const SecureArray &passphrase, PBEAlgorithm pbe) const
1014
if(pbe == PBEDefault)
1015
pbe = get_pbe_default();
1016
const PKeyContext *cur = static_cast<const PKeyContext *>(context());
1017
Provider *p = providerForPBE(pbe, type(), cur);
1020
if(cur->provider() == p)
1022
out = cur->privateToDER(passphrase, pbe);
1026
PKeyContext *pk = static_cast<PKeyContext *>(getContext("pkey", p));
1027
if(pk->importKey(cur->key()))
1028
out = pk->privateToDER(passphrase, pbe);
1034
QString PrivateKey::toPEM(const SecureArray &passphrase, PBEAlgorithm pbe) const
1037
if(pbe == PBEDefault)
1038
pbe = get_pbe_default();
1039
const PKeyContext *cur = static_cast<const PKeyContext *>(context());
1040
Provider *p = providerForPBE(pbe, type(), cur);
1043
if(cur->provider() == p)
1045
out = cur->privateToPEM(passphrase, pbe);
1049
PKeyContext *pk = static_cast<PKeyContext *>(getContext("pkey", p));
1050
if(pk->importKey(cur->key()))
1051
out = pk->privateToPEM(passphrase, pbe);
1057
bool PrivateKey::toPEMFile(const QString &fileName, const SecureArray &passphrase, PBEAlgorithm pbe) const
1059
return stringToFile(fileName, toPEM(passphrase, pbe));
1062
PrivateKey PrivateKey::fromDER(const SecureArray &a, const SecureArray &passphrase, ConvertResult *result, const QString &provider)
1064
return get_privatekey_der(a, QString(), (void *)&a, passphrase, result, provider);
1067
PrivateKey PrivateKey::fromPEM(const QString &s, const SecureArray &passphrase, ConvertResult *result, const QString &provider)
1069
return get_privatekey_pem(s, QString(), (void *)&s, passphrase, result, provider);
1072
PrivateKey PrivateKey::fromPEMFile(const QString &fileName, const SecureArray &passphrase, ConvertResult *result, const QString &provider)
1075
if(!stringFromFile(fileName, &pem))
1078
*result = ErrorFile;
1079
return PrivateKey();
1081
return get_privatekey_pem(pem, fileName, 0, passphrase, result, provider);
1084
//----------------------------------------------------------------------------
1086
//----------------------------------------------------------------------------
1087
class KeyGenerator::Private : public QObject
1091
KeyGenerator *parent;
1092
bool blocking, wasBlocking;
1100
Private(KeyGenerator *_parent) : QObject(_parent), parent(_parent)
1139
emit parent->finished();
1147
dc->getResult(&p, &q, &g);
1148
group = DLGroup(p, q, g);
1154
emit parent->finished();
1158
KeyGenerator::KeyGenerator(QObject *parent)
1161
d = new Private(this);
1165
KeyGenerator::~KeyGenerator()
1170
bool KeyGenerator::blockingEnabled() const
1175
void KeyGenerator::setBlockingEnabled(bool b)
1180
bool KeyGenerator::isBusy() const
1182
return (d->k ? true: false);
1185
PrivateKey KeyGenerator::createRSA(int bits, int exp, const QString &provider)
1188
return PrivateKey();
1190
d->key = PrivateKey();
1191
d->wasBlocking = d->blocking;
1192
d->k = static_cast<RSAContext *>(getContext("rsa", provider));
1193
d->dest = static_cast<PKeyContext *>(getContext("pkey", d->k->provider()));
1197
d->k->moveToThread(thread());
1199
connect(d->k, SIGNAL(finished()), d, SLOT(done()));
1200
static_cast<RSAContext *>(d->k)->createPrivate(bits, exp, false);
1204
static_cast<RSAContext *>(d->k)->createPrivate(bits, exp, true);
1211
PrivateKey KeyGenerator::createDSA(const DLGroup &domain, const QString &provider)
1214
return PrivateKey();
1216
d->key = PrivateKey();
1217
d->wasBlocking = d->blocking;
1218
d->k = static_cast<DSAContext *>(getContext("dsa", provider));
1219
d->dest = static_cast<PKeyContext *>(getContext("pkey", d->k->provider()));
1223
d->k->moveToThread(thread());
1225
connect(d->k, SIGNAL(finished()), d, SLOT(done()));
1226
static_cast<DSAContext *>(d->k)->createPrivate(domain, false);
1230
static_cast<DSAContext *>(d->k)->createPrivate(domain, true);
1237
PrivateKey KeyGenerator::createDH(const DLGroup &domain, const QString &provider)
1240
return PrivateKey();
1242
d->key = PrivateKey();
1243
d->wasBlocking = d->blocking;
1244
d->k = static_cast<DHContext *>(getContext("dh", provider));
1245
d->dest = static_cast<PKeyContext *>(getContext("pkey", d->k->provider()));
1249
d->k->moveToThread(thread());
1251
connect(d->k, SIGNAL(finished()), d, SLOT(done()));
1252
static_cast<DHContext *>(d->k)->createPrivate(domain, false);
1256
static_cast<DHContext *>(d->k)->createPrivate(domain, true);
1263
PrivateKey KeyGenerator::key() const
1268
DLGroup KeyGenerator::createDLGroup(QCA::DLGroupSet set, const QString &provider)
1274
if(!provider.isEmpty())
1275
p = providerForName(provider);
1277
p = providerForGroupSet(set);
1279
d->group = DLGroup();
1280
d->wasBlocking = d->blocking;
1281
d->dc = static_cast<DLGroupContext *>(getContext("dlgroup", p));
1285
connect(d->dc, SIGNAL(finished()), d, SLOT(done_group()));
1286
d->dc->fetchGroup(set, false);
1290
d->dc->fetchGroup(set, true);
1297
DLGroup KeyGenerator::dlGroup() const
1302
//----------------------------------------------------------------------------
1304
//----------------------------------------------------------------------------
1305
RSAPublicKey::RSAPublicKey()
1309
RSAPublicKey::RSAPublicKey(const BigInteger &n, const BigInteger &e, const QString &provider)
1311
RSAContext *k = static_cast<RSAContext *>(getContext("rsa", provider));
1312
k->createPublic(n, e);
1313
PKeyContext *c = static_cast<PKeyContext *>(getContext("pkey", k->provider()));
1318
RSAPublicKey::RSAPublicKey(const RSAPrivateKey &k)
1323
BigInteger RSAPublicKey::n() const
1325
return static_cast<const RSAContext *>(static_cast<const PKeyContext *>(context())->key())->n();
1328
BigInteger RSAPublicKey::e() const
1330
return static_cast<const RSAContext *>(static_cast<const PKeyContext *>(context())->key())->e();
1333
//----------------------------------------------------------------------------
1335
//----------------------------------------------------------------------------
1336
RSAPrivateKey::RSAPrivateKey()
1340
RSAPrivateKey::RSAPrivateKey(const BigInteger &n, const BigInteger &e, const BigInteger &p, const BigInteger &q, const BigInteger &d, const QString &provider)
1342
RSAContext *k = static_cast<RSAContext *>(getContext("rsa", provider));
1343
k->createPrivate(n, e, p, q, d);
1344
PKeyContext *c = static_cast<PKeyContext *>(getContext("pkey", k->provider()));
1349
BigInteger RSAPrivateKey::n() const
1351
return static_cast<const RSAContext *>(static_cast<const PKeyContext *>(context())->key())->n();
1354
BigInteger RSAPrivateKey::e() const
1356
return static_cast<const RSAContext *>(static_cast<const PKeyContext *>(context())->key())->e();
1359
BigInteger RSAPrivateKey::p() const
1361
return static_cast<const RSAContext *>(static_cast<const PKeyContext *>(context())->key())->p();
1364
BigInteger RSAPrivateKey::q() const
1366
return static_cast<const RSAContext *>(static_cast<const PKeyContext *>(context())->key())->q();
1369
BigInteger RSAPrivateKey::d() const
1371
return static_cast<const RSAContext *>(static_cast<const PKeyContext *>(context())->key())->d();
1374
//----------------------------------------------------------------------------
1376
//----------------------------------------------------------------------------
1377
DSAPublicKey::DSAPublicKey()
1381
DSAPublicKey::DSAPublicKey(const DLGroup &domain, const BigInteger &y, const QString &provider)
1383
DSAContext *k = static_cast<DSAContext *>(getContext("dsa", provider));
1384
k->createPublic(domain, y);
1385
PKeyContext *c = static_cast<PKeyContext *>(getContext("pkey", k->provider()));
1390
DSAPublicKey::DSAPublicKey(const DSAPrivateKey &k)
1395
DLGroup DSAPublicKey::domain() const
1397
return static_cast<const DSAContext *>(static_cast<const PKeyContext *>(context())->key())->domain();
1400
BigInteger DSAPublicKey::y() const
1402
return static_cast<const DSAContext *>(static_cast<const PKeyContext *>(context())->key())->y();
1405
//----------------------------------------------------------------------------
1407
//----------------------------------------------------------------------------
1408
DSAPrivateKey::DSAPrivateKey()
1412
DSAPrivateKey::DSAPrivateKey(const DLGroup &domain, const BigInteger &y, const BigInteger &x, const QString &provider)
1414
DSAContext *k = static_cast<DSAContext *>(getContext("dsa", provider));
1415
k->createPrivate(domain, y, x);
1416
PKeyContext *c = static_cast<PKeyContext *>(getContext("pkey", k->provider()));
1421
DLGroup DSAPrivateKey::domain() const
1423
return static_cast<const DSAContext *>(static_cast<const PKeyContext *>(context())->key())->domain();
1426
BigInteger DSAPrivateKey::y() const
1428
return static_cast<const DSAContext *>(static_cast<const PKeyContext *>(context())->key())->y();
1431
BigInteger DSAPrivateKey::x() const
1433
return static_cast<const DSAContext *>(static_cast<const PKeyContext *>(context())->key())->x();
1436
//----------------------------------------------------------------------------
1438
//----------------------------------------------------------------------------
1439
DHPublicKey::DHPublicKey()
1443
DHPublicKey::DHPublicKey(const DLGroup &domain, const BigInteger &y, const QString &provider)
1445
DHContext *k = static_cast<DHContext *>(getContext("dh", provider));
1446
k->createPublic(domain, y);
1447
PKeyContext *c = static_cast<PKeyContext *>(getContext("pkey", k->provider()));
1452
DHPublicKey::DHPublicKey(const DHPrivateKey &k)
1457
DLGroup DHPublicKey::domain() const
1459
return static_cast<const DHContext *>(static_cast<const PKeyContext *>(context())->key())->domain();
1462
BigInteger DHPublicKey::y() const
1464
return static_cast<const DHContext *>(static_cast<const PKeyContext *>(context())->key())->y();
1467
//----------------------------------------------------------------------------
1469
//----------------------------------------------------------------------------
1470
DHPrivateKey::DHPrivateKey()
1474
DHPrivateKey::DHPrivateKey(const DLGroup &domain, const BigInteger &y, const BigInteger &x, const QString &provider)
1476
DHContext *k = static_cast<DHContext *>(getContext("dh", provider));
1477
k->createPrivate(domain, y, x);
1478
PKeyContext *c = static_cast<PKeyContext *>(getContext("pkey", k->provider()));
1483
DLGroup DHPrivateKey::domain() const
1485
return static_cast<const DHContext *>(static_cast<const PKeyContext *>(context())->key())->domain();
1488
BigInteger DHPrivateKey::y() const
1490
return static_cast<const DHContext *>(static_cast<const PKeyContext *>(context())->key())->y();
1493
BigInteger DHPrivateKey::x() const
1495
return static_cast<const DHContext *>(static_cast<const PKeyContext *>(context())->key())->x();
1500
#include "qca_publickey.moc"