~ubuntu-branches/ubuntu/jaunty/psi/jaunty

« back to all changes in this revision

Viewing changes to third-party/qca/qca/src/qca_publickey.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jan Niehusmann
  • Date: 2008-04-14 18:57:30 UTC
  • mfrom: (2.1.9 hardy)
  • Revision ID: james.westby@ubuntu.com-20080414185730-528re3zp0m2hdlhi
Tags: 0.11-8
* added CONFIG -= link_prl to .pro files and removed dependencies
  which are made unnecessary by this change
* Fix segfault when closing last chat tab with qt4.4
  (This is from upstream svn, rev. 1101) (Closes: Bug#476122)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2003-2007  Justin Karneges <justin@affinix.com>
 
3
 * Copyright (C) 2004,2005  Brad Hards <bradh@frogmouth.net>
 
4
 *
 
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.
 
9
 *
 
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.
 
14
 *
 
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
 
18
 *
 
19
 */
 
20
 
 
21
#include "qca_publickey.h"
 
22
 
 
23
#include "qcaprovider.h"
 
24
 
 
25
#include <QFile>
 
26
#include <QTextStream>
 
27
 
 
28
namespace QCA {
 
29
 
 
30
Provider::Context *getContext(const QString &type, const QString &provider);
 
31
Provider::Context *getContext(const QString &type, Provider *p);
 
32
 
 
33
bool stringToFile(const QString &fileName, const QString &content)
 
34
{
 
35
        QFile f(fileName);
 
36
        if(!f.open(QFile::WriteOnly))
 
37
                return false;
 
38
        QTextStream ts(&f);
 
39
        ts << content;
 
40
        return true;
 
41
}
 
42
 
 
43
bool stringFromFile(const QString &fileName, QString *s)
 
44
{
 
45
        QFile f(fileName);
 
46
        if(!f.open(QFile::ReadOnly))
 
47
                return false;
 
48
        QTextStream ts(&f);
 
49
        *s = ts.readAll();
 
50
        return true;
 
51
}
 
52
 
 
53
bool arrayToFile(const QString &fileName, const QByteArray &content)
 
54
{
 
55
        QFile f(fileName);
 
56
        if(!f.open(QFile::WriteOnly))
 
57
                return false;
 
58
        f.write(content.data(), content.size());
 
59
        return true;
 
60
}
 
61
 
 
62
bool arrayFromFile(const QString &fileName, QByteArray *a)
 
63
{
 
64
        QFile f(fileName);
 
65
        if(!f.open(QFile::ReadOnly))
 
66
                return false;
 
67
        *a = f.readAll();
 
68
        return true;
 
69
}
 
70
 
 
71
bool ask_passphrase(const QString &fname, void *ptr, SecureArray *answer)
 
72
{
 
73
        PasswordAsker asker;
 
74
        asker.ask(Event::StylePassphrase, fname, ptr);
 
75
        asker.waitForResponse();
 
76
        if(!asker.accepted())
 
77
                return false;
 
78
        *answer = asker.password();
 
79
        return true;
 
80
}
 
81
 
 
82
ProviderList allProviders()
 
83
{
 
84
        ProviderList pl = providers();
 
85
        pl += defaultProvider();
 
86
        return pl;
 
87
}
 
88
 
 
89
Provider *providerForName(const QString &name)
 
90
{
 
91
        ProviderList pl = allProviders();
 
92
        for(int n = 0; n < pl.count(); ++n)
 
93
        {
 
94
                if(pl[n]->name() == name)
 
95
                        return pl[n];
 
96
        }
 
97
        return 0;
 
98
}
 
99
 
 
100
bool use_asker_fallback(ConvertResult r)
 
101
{
 
102
        // FIXME: we should only do this if we get ErrorPassphrase?
 
103
        //if(r == ErrorPassphrase)
 
104
        if(r != ConvertGood)
 
105
                return true;
 
106
        return false;
 
107
}
 
108
 
 
109
class Getter_GroupSet
 
110
{
 
111
public:
 
112
        static QList<DLGroupSet> getList(Provider *p)
 
113
        {
 
114
                QList<DLGroupSet> list;
 
115
                const DLGroupContext *c = static_cast<const DLGroupContext *>(getContext("dlgroup", p));
 
116
                if(!c)
 
117
                        return list;
 
118
                list = c->supportedGroupSets();
 
119
                delete c;
 
120
                return list;
 
121
        }
 
122
};
 
123
 
 
124
class Getter_PBE
 
125
{
 
126
public:
 
127
        static QList<PBEAlgorithm> getList(Provider *p)
 
128
        {
 
129
                QList<PBEAlgorithm> list;
 
130
                const PKeyContext *c = static_cast<const PKeyContext *>(getContext("pkey", p));
 
131
                if(!c)
 
132
                        return list;
 
133
                list = c->supportedPBEAlgorithms();
 
134
                delete c;
 
135
                return list;
 
136
        }
 
137
};
 
138
 
 
139
class Getter_Type
 
140
{
 
141
public:
 
142
        static QList<PKey::Type> getList(Provider *p)
 
143
        {
 
144
                QList<PKey::Type> list;
 
145
                const PKeyContext *c = static_cast<const PKeyContext *>(getContext("pkey", p));
 
146
                if(!c)
 
147
                        return list;
 
148
                list = c->supportedTypes();
 
149
                delete c;
 
150
                return list;
 
151
        }
 
152
};
 
153
 
 
154
class Getter_IOType
 
155
{
 
156
public:
 
157
        static QList<PKey::Type> getList(Provider *p)
 
158
        {
 
159
                QList<PKey::Type> list;
 
160
                const PKeyContext *c = static_cast<const PKeyContext *>(getContext("pkey", p));
 
161
                if(!c)
 
162
                        return list;
 
163
                list = c->supportedIOTypes();
 
164
                delete c;
 
165
                return list;
 
166
        }
 
167
};
 
168
 
 
169
template <typename I>
 
170
class Getter_PublicKey
 
171
{
 
172
public:
 
173
        // DER
 
174
        static ConvertResult fromData(PKeyContext *c, const QByteArray &in)
 
175
        {
 
176
                return c->publicFromDER(in);
 
177
        }
 
178
 
 
179
        // PEM
 
180
        static ConvertResult fromData(PKeyContext *c, const QString &in)
 
181
        {
 
182
                return c->publicFromPEM(in);
 
183
        }
 
184
 
 
185
        static PublicKey getKey(Provider *p, const I &in, const SecureArray &, ConvertResult *result)
 
186
        {
 
187
                PublicKey k;
 
188
                PKeyContext *c = static_cast<PKeyContext *>(getContext("pkey", p));
 
189
                if(!c)
 
190
                {
 
191
                        if(result)
 
192
                                *result = ErrorDecode;
 
193
                        return k;
 
194
                }
 
195
                ConvertResult r = fromData(c, in);
 
196
                if(result)
 
197
                        *result = r;
 
198
                if(r == ConvertGood)
 
199
                        k.change(c);
 
200
                else
 
201
                        delete c;
 
202
                return k;
 
203
        }
 
204
};
 
205
 
 
206
template <typename I>
 
207
class Getter_PrivateKey
 
208
{
 
209
public:
 
210
        // DER
 
211
        static ConvertResult fromData(PKeyContext *c, const SecureArray &in, const SecureArray &passphrase)
 
212
        {
 
213
                return c->privateFromDER(in, passphrase);
 
214
        }
 
215
 
 
216
        // PEM
 
217
        static ConvertResult fromData(PKeyContext *c, const QString &in, const SecureArray &passphrase)
 
218
        {
 
219
                return c->privateFromPEM(in, passphrase);
 
220
        }
 
221
 
 
222
        static PrivateKey getKey(Provider *p, const I &in, const SecureArray &passphrase, ConvertResult *result)
 
223
        {
 
224
                PrivateKey k;
 
225
                PKeyContext *c = static_cast<PKeyContext *>(getContext("pkey", p));
 
226
                if(!c)
 
227
                {
 
228
                        if(result)
 
229
                                *result = ErrorDecode;
 
230
                        return k;
 
231
                }
 
232
                ConvertResult r = fromData(c, in, passphrase);
 
233
                if(result)
 
234
                        *result = r;
 
235
                if(r == ConvertGood)
 
236
                        k.change(c);
 
237
                else
 
238
                        delete c;
 
239
                return k;
 
240
        }
 
241
};
 
242
 
 
243
Provider *providerForGroupSet(DLGroupSet set)
 
244
{
 
245
        ProviderList pl = allProviders();
 
246
        for(int n = 0; n < pl.count(); ++n)
 
247
        {
 
248
                if(Getter_GroupSet::getList(pl[n]).contains(set))
 
249
                        return pl[n];
 
250
        }
 
251
        return 0;
 
252
}
 
253
 
 
254
Provider *providerForPBE(PBEAlgorithm alg, PKey::Type ioType, const PKeyContext *prefer = 0)
 
255
{
 
256
        Provider *preferProvider = 0;
 
257
        if(prefer)
 
258
        {
 
259
                preferProvider = prefer->provider();
 
260
                if(prefer->supportedPBEAlgorithms().contains(alg) && prefer->supportedIOTypes().contains(ioType))
 
261
                        return preferProvider;
 
262
        }
 
263
 
 
264
        ProviderList pl = allProviders();
 
265
        for(int n = 0; n < pl.count(); ++n)
 
266
        {
 
267
                if(preferProvider && pl[n] == preferProvider)
 
268
                        continue;
 
269
 
 
270
                if(Getter_PBE::getList(pl[n]).contains(alg) && Getter_IOType::getList(pl[n]).contains(ioType))
 
271
                        return pl[n];
 
272
        }
 
273
        return 0;
 
274
}
 
275
 
 
276
Provider *providerForIOType(PKey::Type type, const PKeyContext *prefer = 0)
 
277
{
 
278
        Provider *preferProvider = 0;
 
279
        if(prefer)
 
280
        {
 
281
                preferProvider = prefer->provider();
 
282
                if(prefer && prefer->supportedIOTypes().contains(type))
 
283
                        return preferProvider;
 
284
        }
 
285
 
 
286
        ProviderList pl = allProviders();
 
287
        for(int n = 0; n < pl.count(); ++n)
 
288
        {
 
289
                if(preferProvider && pl[n] == preferProvider)
 
290
                        continue;
 
291
 
 
292
                if(Getter_IOType::getList(pl[n]).contains(type))
 
293
                        return pl[n];
 
294
        }
 
295
        return 0;
 
296
}
 
297
 
 
298
template <typename T, typename G>
 
299
QList<T> getList(const QString &provider)
 
300
{
 
301
        QList<T> list;
 
302
 
 
303
        // single
 
304
        if(!provider.isEmpty())
 
305
        {
 
306
                Provider *p = providerForName(provider);
 
307
                if(p)
 
308
                        list = G::getList(p);
 
309
        }
 
310
        // all
 
311
        else
 
312
        {
 
313
                ProviderList pl = allProviders();
 
314
                for(int n = 0; n < pl.count(); ++n)
 
315
                {
 
316
                        QList<T> other = G::getList(pl[n]);
 
317
                        for(int k = 0; k < other.count(); ++k)
 
318
                        {
 
319
                                // only add what we don't have in the list
 
320
                                if(!list.contains(other[k]))
 
321
                                        list += other[k];
 
322
                        }
 
323
                }
 
324
        }
 
325
 
 
326
        return list;
 
327
}
 
328
 
 
329
template <typename T, typename G, typename I>
 
330
T getKey(const QString &provider, const I &in, const SecureArray &passphrase, ConvertResult *result)
 
331
{
 
332
        T k;
 
333
 
 
334
        // single
 
335
        if(!provider.isEmpty())
 
336
        {
 
337
                Provider *p = providerForName(provider);
 
338
                if(!p)
 
339
                        return k;
 
340
                k = G::getKey(p, in, passphrase, result);
 
341
        }
 
342
        // all
 
343
        else
 
344
        {
 
345
                ProviderList pl = allProviders();
 
346
                for(int n = 0; n < pl.count(); ++n)
 
347
                {
 
348
                        ConvertResult r;
 
349
                        k = G::getKey(pl[n], in, passphrase, &r);
 
350
                        if(result)
 
351
                                *result = r;
 
352
                        if(!k.isNull())
 
353
                                break;
 
354
                        if(r == ErrorPassphrase) // don't loop if we get this
 
355
                                break;
 
356
                }
 
357
        }
 
358
 
 
359
        return k;
 
360
}
 
361
 
 
362
PBEAlgorithm get_pbe_default()
 
363
{
 
364
        return PBES2_TripleDES_SHA1;
 
365
}
 
366
 
 
367
static PrivateKey get_privatekey_der(const SecureArray &der, const QString &fileName, void *ptr, const SecureArray &passphrase, ConvertResult *result, const QString &provider)
 
368
{
 
369
        PrivateKey out;
 
370
        ConvertResult r;
 
371
        out = getKey<PrivateKey, Getter_PrivateKey<SecureArray>, SecureArray>(provider, der, passphrase, &r);
 
372
 
 
373
        // error converting without passphrase?  maybe a passphrase is needed
 
374
        if(use_asker_fallback(r) && passphrase.isEmpty())
 
375
        {
 
376
                SecureArray pass;
 
377
                if(ask_passphrase(fileName, ptr, &pass))
 
378
                        out = getKey<PrivateKey, Getter_PrivateKey<SecureArray>, SecureArray>(provider, der, pass, &r);
 
379
        }
 
380
        if(result)
 
381
                *result = r;
 
382
        return out;
 
383
}
 
384
 
 
385
static PrivateKey get_privatekey_pem(const QString &pem, const QString &fileName, void *ptr, const SecureArray &passphrase, ConvertResult *result, const QString &provider)
 
386
{
 
387
        PrivateKey out;
 
388
        ConvertResult r;
 
389
        out = getKey<PrivateKey, Getter_PrivateKey<QString>, QString>(provider, pem, passphrase, &r);
 
390
 
 
391
        // error converting without passphrase?  maybe a passphrase is needed
 
392
        if(use_asker_fallback(r) && passphrase.isEmpty())
 
393
        {
 
394
                SecureArray pass;
 
395
                if(ask_passphrase(fileName, ptr, &pass))
 
396
                        out = getKey<PrivateKey, Getter_PrivateKey<QString>, QString>(provider, pem, pass, &r);
 
397
        }
 
398
        if(result)
 
399
                *result = r;
 
400
        return out;
 
401
}
 
402
 
 
403
//----------------------------------------------------------------------------
 
404
// Global
 
405
//----------------------------------------------------------------------------
 
406
 
 
407
// adapted from Botan
 
408
static const unsigned char pkcs_sha1[] =
 
409
{
 
410
        0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2B, 0x0E, 0x03, 0x02,
 
411
        0x1A, 0x05, 0x00, 0x04, 0x14
 
412
};
 
413
 
 
414
static const unsigned char pkcs_md5[] =
 
415
{
 
416
        0x30, 0x20, 0x30, 0x0C, 0x06, 0x08, 0x2A, 0x86, 0x48, 0x86,
 
417
        0xF7, 0x0D, 0x02, 0x05, 0x05, 0x00, 0x04, 0x10
 
418
};
 
419
 
 
420
static const unsigned char pkcs_md2[] =
 
421
{
 
422
        0x30, 0x20, 0x30, 0x0C, 0x06, 0x08, 0x2A, 0x86, 0x48, 0x86,
 
423
        0xF7, 0x0D, 0x02, 0x02, 0x05, 0x00, 0x04, 0x10
 
424
};
 
425
 
 
426
static const unsigned char pkcs_ripemd160[] =
 
427
{
 
428
        0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2B, 0x24, 0x03, 0x02,
 
429
        0x01, 0x05, 0x00, 0x04, 0x14
 
430
};
 
431
 
 
432
QByteArray get_hash_id(const QString &name)
 
433
{
 
434
        if(name == "sha1")
 
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));
 
442
        else
 
443
                return QByteArray();
 
444
}
 
445
 
 
446
QByteArray emsa3Encode(const QString &hashName, const QByteArray &digest, int size)
 
447
{
 
448
        QByteArray hash_id = get_hash_id(hashName);
 
449
        if(hash_id.isEmpty())
 
450
                return QByteArray();
 
451
 
 
452
        // logic adapted from Botan
 
453
        int basesize = hash_id.size() + digest.size() + 2;
 
454
        if(size == -1)
 
455
                size = basesize + 1; // default to 1-byte pad
 
456
        int padlen = size - basesize;
 
457
        if(padlen < 1)
 
458
                return QByteArray();
 
459
 
 
460
        QByteArray out(size, (char)0xff); // pad with 0xff
 
461
        out[0] = 0x01;
 
462
        out[padlen + 1] = 0x00;
 
463
        int at = padlen + 2;
 
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());
 
467
        return out;
 
468
}
 
469
 
 
470
//----------------------------------------------------------------------------
 
471
// DLGroup
 
472
//----------------------------------------------------------------------------
 
473
class DLGroup::Private
 
474
{
 
475
public:
 
476
        BigInteger p, q, g;
 
477
 
 
478
        Private(const BigInteger &p1, const BigInteger &q1, const BigInteger &g1)
 
479
        :p(p1), q(q1), g(g1)
 
480
        {
 
481
        }
 
482
};
 
483
 
 
484
DLGroup::DLGroup()
 
485
{
 
486
        d = 0;
 
487
}
 
488
 
 
489
DLGroup::DLGroup(const BigInteger &p, const BigInteger &q, const BigInteger &g)
 
490
{
 
491
        d = new Private(p, q, g);
 
492
}
 
493
 
 
494
DLGroup::DLGroup(const BigInteger &p, const BigInteger &g)
 
495
{
 
496
        d = new Private(p, 0, g);
 
497
}
 
498
 
 
499
DLGroup::DLGroup(const DLGroup &from)
 
500
{
 
501
        d = 0;
 
502
        *this = from;
 
503
}
 
504
 
 
505
DLGroup::~DLGroup()
 
506
{
 
507
        delete d;
 
508
}
 
509
 
 
510
DLGroup & DLGroup::operator=(const DLGroup &from)
 
511
{
 
512
        delete d;
 
513
        d = 0;
 
514
 
 
515
        if(from.d)
 
516
                d = new Private(*from.d);
 
517
 
 
518
        return *this;
 
519
}
 
520
 
 
521
QList<DLGroupSet> DLGroup::supportedGroupSets(const QString &provider)
 
522
{
 
523
        return getList<DLGroupSet, Getter_GroupSet>(provider);
 
524
}
 
525
 
 
526
bool DLGroup::isNull() const
 
527
{
 
528
        return (d ? false: true);
 
529
}
 
530
 
 
531
BigInteger DLGroup::p() const
 
532
{
 
533
        return d->p;
 
534
}
 
535
 
 
536
BigInteger DLGroup::q() const
 
537
{
 
538
        return d->q;
 
539
}
 
540
 
 
541
BigInteger DLGroup::g() const
 
542
{
 
543
        return d->g;
 
544
}
 
545
 
 
546
//----------------------------------------------------------------------------
 
547
// PKey
 
548
//----------------------------------------------------------------------------
 
549
class PKey::Private
 
550
{
 
551
public:
 
552
};
 
553
 
 
554
PKey::PKey()
 
555
{
 
556
        d = new Private;
 
557
}
 
558
 
 
559
PKey::PKey(const QString &type, const QString &provider)
 
560
:Algorithm(type, provider)
 
561
{
 
562
        d = new Private;
 
563
}
 
564
 
 
565
PKey::PKey(const PKey &from)
 
566
:Algorithm(from)
 
567
{
 
568
        d = new Private;
 
569
        *this = from;
 
570
}
 
571
 
 
572
PKey::~PKey()
 
573
{
 
574
        delete d;
 
575
}
 
576
 
 
577
PKey & PKey::operator=(const PKey &from)
 
578
{
 
579
        Algorithm::operator=(from);
 
580
        *d = *from.d;
 
581
        return *this;
 
582
}
 
583
 
 
584
void PKey::set(const PKey &k)
 
585
{
 
586
        *this = k;
 
587
}
 
588
 
 
589
void PKey::assignToPublic(PKey *dest) const
 
590
{
 
591
        dest->set(*this);
 
592
 
 
593
        // converting private to public
 
594
        if(dest->isPrivate())
 
595
                static_cast<PKeyContext *>(dest->context())->key()->convertToPublic();
 
596
}
 
597
 
 
598
void PKey::assignToPrivate(PKey *dest) const
 
599
{
 
600
        dest->set(*this);
 
601
}
 
602
 
 
603
QList<PKey::Type> PKey::supportedTypes(const QString &provider)
 
604
{
 
605
        return getList<Type, Getter_Type>(provider);
 
606
}
 
607
 
 
608
QList<PKey::Type> PKey::supportedIOTypes(const QString &provider)
 
609
{
 
610
        return getList<Type, Getter_IOType>(provider);
 
611
}
 
612
 
 
613
bool PKey::isNull() const
 
614
{
 
615
        return (!context() ? true : false);
 
616
}
 
617
 
 
618
PKey::Type PKey::type() const
 
619
{
 
620
        if(isNull())
 
621
                return RSA; // some default so we don't explode
 
622
        return static_cast<const PKeyContext *>(context())->key()->type();
 
623
}
 
624
 
 
625
int PKey::bitSize() const
 
626
{
 
627
        return static_cast<const PKeyContext *>(context())->key()->bits();
 
628
}
 
629
 
 
630
bool PKey::isRSA() const
 
631
{
 
632
        return (type() == RSA);
 
633
}
 
634
 
 
635
bool PKey::isDSA() const
 
636
{
 
637
        return (type() == DSA);
 
638
}
 
639
 
 
640
bool PKey::isDH() const
 
641
{
 
642
        return (type() == DH);
 
643
}
 
644
 
 
645
bool PKey::isPublic() const
 
646
{
 
647
        if(isNull())
 
648
                return false;
 
649
        return !isPrivate();
 
650
}
 
651
 
 
652
bool PKey::isPrivate() const
 
653
{
 
654
        if(isNull())
 
655
                return false;
 
656
        return static_cast<const PKeyContext *>(context())->key()->isPrivate();
 
657
}
 
658
 
 
659
bool PKey::canExport() const
 
660
{
 
661
        return static_cast<const PKeyContext *>(context())->key()->canExport();
 
662
}
 
663
 
 
664
bool PKey::canKeyAgree() const
 
665
{
 
666
        return isDH();
 
667
}
 
668
 
 
669
PublicKey PKey::toPublicKey() const
 
670
{
 
671
        PublicKey k;
 
672
        if(!isNull())
 
673
                assignToPublic(&k);
 
674
        return k;
 
675
}
 
676
 
 
677
PrivateKey PKey::toPrivateKey() const
 
678
{
 
679
        PrivateKey k;
 
680
        if(!isNull() && isPrivate())
 
681
                assignToPrivate(&k);
 
682
        return k;
 
683
}
 
684
 
 
685
RSAPublicKey PKey::toRSAPublicKey() const
 
686
{
 
687
        RSAPublicKey k;
 
688
        if(!isNull() && isRSA())
 
689
                assignToPublic(&k);
 
690
        return k;
 
691
}
 
692
 
 
693
RSAPrivateKey PKey::toRSAPrivateKey() const
 
694
{
 
695
        RSAPrivateKey k;
 
696
        if(!isNull() && isRSA() && isPrivate())
 
697
                assignToPrivate(&k);
 
698
        return k;
 
699
}
 
700
 
 
701
DSAPublicKey PKey::toDSAPublicKey() const
 
702
{
 
703
        DSAPublicKey k;
 
704
        if(!isNull() && isDSA())
 
705
                assignToPublic(&k);
 
706
        return k;
 
707
}
 
708
 
 
709
DSAPrivateKey PKey::toDSAPrivateKey() const
 
710
{
 
711
        DSAPrivateKey k;
 
712
        if(!isNull() && isDSA() && isPrivate())
 
713
                assignToPrivate(&k);
 
714
        return k;
 
715
}
 
716
 
 
717
DHPublicKey PKey::toDHPublicKey() const
 
718
{
 
719
        DHPublicKey k;
 
720
        if(!isNull() && isDH())
 
721
                assignToPublic(&k);
 
722
        return k;
 
723
}
 
724
 
 
725
DHPrivateKey PKey::toDHPrivateKey() const
 
726
{
 
727
        DHPrivateKey k;
 
728
        if(!isNull() && isDH() && isPrivate())
 
729
                assignToPrivate(&k);
 
730
        return k;
 
731
}
 
732
 
 
733
bool PKey::operator==(const PKey &a) const
 
734
{
 
735
        if(isNull() || a.isNull() || type() != a.type())
 
736
                return false;
 
737
 
 
738
        if(a.isPrivate())
 
739
                return (toPrivateKey().toDER() == a.toPrivateKey().toDER());
 
740
        else
 
741
                return (toPublicKey().toDER() == a.toPublicKey().toDER());
 
742
}
 
743
 
 
744
bool PKey::operator!=(const PKey &a) const
 
745
{
 
746
        return !(*this == a);
 
747
}
 
748
 
 
749
//----------------------------------------------------------------------------
 
750
// PublicKey
 
751
//----------------------------------------------------------------------------
 
752
PublicKey::PublicKey()
 
753
{
 
754
}
 
755
 
 
756
PublicKey::PublicKey(const QString &type, const QString &provider)
 
757
:PKey(type, provider)
 
758
{
 
759
}
 
760
 
 
761
PublicKey::PublicKey(const PrivateKey &k)
 
762
{
 
763
        set(k.toPublicKey());
 
764
}
 
765
 
 
766
PublicKey::PublicKey(const QString &fileName)
 
767
{
 
768
        *this = fromPEMFile(fileName, 0, QString());
 
769
}
 
770
 
 
771
PublicKey::PublicKey(const PublicKey &from)
 
772
:PKey(from)
 
773
{
 
774
}
 
775
 
 
776
PublicKey::~PublicKey()
 
777
{
 
778
}
 
779
 
 
780
PublicKey & PublicKey::operator=(const PublicKey &from)
 
781
{
 
782
        PKey::operator=(from);
 
783
        return *this;
 
784
}
 
785
 
 
786
RSAPublicKey PublicKey::toRSA() const
 
787
{
 
788
        return toRSAPublicKey();
 
789
}
 
790
 
 
791
DSAPublicKey PublicKey::toDSA() const
 
792
{
 
793
        return toDSAPublicKey();
 
794
}
 
795
 
 
796
DHPublicKey PublicKey::toDH() const
 
797
{
 
798
        return toDHPublicKey();
 
799
}
 
800
 
 
801
bool PublicKey::canEncrypt() const
 
802
{
 
803
        return isRSA();
 
804
}
 
805
 
 
806
bool PublicKey::canVerify() const
 
807
{
 
808
        return (isRSA() || isDSA());
 
809
}
 
810
 
 
811
int PublicKey::maximumEncryptSize(EncryptionAlgorithm alg) const
 
812
{
 
813
        return static_cast<const PKeyContext *>(context())->key()->maximumEncryptSize(alg);
 
814
}
 
815
 
 
816
SecureArray PublicKey::encrypt(const SecureArray &a, EncryptionAlgorithm alg)
 
817
{
 
818
        return static_cast<PKeyContext *>(context())->key()->encrypt(a, alg);
 
819
}
 
820
 
 
821
void PublicKey::startVerify(SignatureAlgorithm alg, SignatureFormat format)
 
822
{
 
823
        if(isDSA() && format == DefaultFormat)
 
824
                format = IEEE_1363;
 
825
        static_cast<PKeyContext *>(context())->key()->startVerify(alg, format);
 
826
}
 
827
 
 
828
void PublicKey::update(const MemoryRegion &a)
 
829
{
 
830
        static_cast<PKeyContext *>(context())->key()->update(a);
 
831
}
 
832
 
 
833
bool PublicKey::validSignature(const QByteArray &sig)
 
834
{
 
835
        return static_cast<PKeyContext *>(context())->key()->endVerify(sig);
 
836
}
 
837
 
 
838
bool PublicKey::verifyMessage(const MemoryRegion &a, const QByteArray &sig, SignatureAlgorithm alg, SignatureFormat format)
 
839
{
 
840
        startVerify(alg, format);
 
841
        update(a);
 
842
        return validSignature(sig);
 
843
}
 
844
 
 
845
QByteArray PublicKey::toDER() const
 
846
{
 
847
        QByteArray out;
 
848
        const PKeyContext *cur = static_cast<const PKeyContext *>(context());
 
849
        Provider *p = providerForIOType(type(), cur);
 
850
        if(!p)
 
851
                return out;
 
852
        if(cur->provider() == p)
 
853
        {
 
854
                out = cur->publicToDER();
 
855
        }
 
856
        else
 
857
        {
 
858
                PKeyContext *pk = static_cast<PKeyContext *>(getContext("pkey", p));
 
859
                if(pk->importKey(cur->key()))
 
860
                        out = pk->publicToDER();
 
861
                delete pk;
 
862
        }
 
863
        return out;
 
864
}
 
865
 
 
866
QString PublicKey::toPEM() const
 
867
{
 
868
        QString out;
 
869
        const PKeyContext *cur = static_cast<const PKeyContext *>(context());
 
870
        Provider *p = providerForIOType(type(), cur);
 
871
        if(!p)
 
872
                return out;
 
873
        if(cur->provider() == p)
 
874
        {
 
875
                out = cur->publicToPEM();
 
876
        }
 
877
        else
 
878
        {
 
879
                PKeyContext *pk = static_cast<PKeyContext *>(getContext("pkey", p));
 
880
                if(pk->importKey(cur->key()))
 
881
                        out = pk->publicToPEM();
 
882
                delete pk;
 
883
        }
 
884
        return out;
 
885
}
 
886
 
 
887
bool PublicKey::toPEMFile(const QString &fileName) const
 
888
{
 
889
        return stringToFile(fileName, toPEM());
 
890
}
 
891
 
 
892
PublicKey PublicKey::fromDER(const QByteArray &a, ConvertResult *result, const QString &provider)
 
893
{
 
894
        return getKey<PublicKey, Getter_PublicKey<QByteArray>, QByteArray>(provider, a, SecureArray(), result);
 
895
}
 
896
 
 
897
PublicKey PublicKey::fromPEM(const QString &s, ConvertResult *result, const QString &provider)
 
898
{
 
899
        return getKey<PublicKey, Getter_PublicKey<QString>, QString>(provider, s, SecureArray(), result);
 
900
}
 
901
 
 
902
PublicKey PublicKey::fromPEMFile(const QString &fileName, ConvertResult *result, const QString &provider)
 
903
{
 
904
        QString pem;
 
905
        if(!stringFromFile(fileName, &pem))
 
906
        {
 
907
                if(result)
 
908
                        *result = ErrorFile;
 
909
                return PublicKey();
 
910
        }
 
911
        return fromPEM(pem, result, provider);
 
912
}
 
913
 
 
914
//----------------------------------------------------------------------------
 
915
// PrivateKey
 
916
//----------------------------------------------------------------------------
 
917
PrivateKey::PrivateKey()
 
918
{
 
919
}
 
920
 
 
921
PrivateKey::PrivateKey(const QString &type, const QString &provider)
 
922
:PKey(type, provider)
 
923
{
 
924
}
 
925
 
 
926
PrivateKey::PrivateKey(const QString &fileName, const SecureArray &passphrase)
 
927
{
 
928
        *this = fromPEMFile(fileName, passphrase, 0, QString());
 
929
}
 
930
 
 
931
PrivateKey::PrivateKey(const PrivateKey &from)
 
932
:PKey(from)
 
933
{
 
934
}
 
935
 
 
936
PrivateKey::~PrivateKey()
 
937
{
 
938
}
 
939
 
 
940
PrivateKey & PrivateKey::operator=(const PrivateKey &from)
 
941
{
 
942
        PKey::operator=(from);
 
943
        return *this;
 
944
}
 
945
 
 
946
RSAPrivateKey PrivateKey::toRSA() const
 
947
{
 
948
        return toRSAPrivateKey();
 
949
}
 
950
 
 
951
DSAPrivateKey PrivateKey::toDSA() const
 
952
{
 
953
        return toDSAPrivateKey();
 
954
}
 
955
 
 
956
DHPrivateKey PrivateKey::toDH() const
 
957
{
 
958
        return toDHPrivateKey();
 
959
}
 
960
 
 
961
bool PrivateKey::canDecrypt() const
 
962
{
 
963
        return isRSA();
 
964
}
 
965
 
 
966
bool PrivateKey::canSign() const
 
967
{
 
968
        return (isRSA() || isDSA());
 
969
}
 
970
 
 
971
bool PrivateKey::decrypt(const SecureArray &in, SecureArray *out, EncryptionAlgorithm alg)
 
972
{
 
973
        return static_cast<PKeyContext *>(context())->key()->decrypt(in, out, alg);
 
974
}
 
975
 
 
976
void PrivateKey::startSign(SignatureAlgorithm alg, SignatureFormat format)
 
977
{
 
978
        if(isDSA() && format == DefaultFormat)
 
979
                format = IEEE_1363;
 
980
        static_cast<PKeyContext *>(context())->key()->startSign(alg, format);
 
981
}
 
982
 
 
983
void PrivateKey::update(const MemoryRegion &a)
 
984
{
 
985
        static_cast<PKeyContext *>(context())->key()->update(a);
 
986
}
 
987
 
 
988
QByteArray PrivateKey::signature()
 
989
{
 
990
        return static_cast<PKeyContext *>(context())->key()->endSign();
 
991
}
 
992
 
 
993
QByteArray PrivateKey::signMessage(const MemoryRegion &a, SignatureAlgorithm alg, SignatureFormat format)
 
994
{
 
995
        startSign(alg, format);
 
996
        update(a);
 
997
        return signature();
 
998
}
 
999
 
 
1000
SymmetricKey PrivateKey::deriveKey(const PublicKey &theirs)
 
1001
{
 
1002
        const PKeyContext *theirContext = static_cast<const PKeyContext *>(theirs.context());
 
1003
        return static_cast<PKeyContext *>(context())->key()->deriveKey(*(theirContext->key()));
 
1004
}
 
1005
 
 
1006
QList<PBEAlgorithm> PrivateKey::supportedPBEAlgorithms(const QString &provider)
 
1007
{
 
1008
        return getList<PBEAlgorithm, Getter_PBE>(provider);
 
1009
}
 
1010
 
 
1011
SecureArray PrivateKey::toDER(const SecureArray &passphrase, PBEAlgorithm pbe) const
 
1012
{
 
1013
        SecureArray out;
 
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);
 
1018
        if(!p)
 
1019
                return out;
 
1020
        if(cur->provider() == p)
 
1021
        {
 
1022
                out = cur->privateToDER(passphrase, pbe);
 
1023
        }
 
1024
        else
 
1025
        {
 
1026
                PKeyContext *pk = static_cast<PKeyContext *>(getContext("pkey", p));
 
1027
                if(pk->importKey(cur->key()))
 
1028
                        out = pk->privateToDER(passphrase, pbe);
 
1029
                delete pk;
 
1030
        }
 
1031
        return out;
 
1032
}
 
1033
 
 
1034
QString PrivateKey::toPEM(const SecureArray &passphrase, PBEAlgorithm pbe) const
 
1035
{
 
1036
        QString out;
 
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);
 
1041
        if(!p)
 
1042
                return out;
 
1043
        if(cur->provider() == p)
 
1044
        {
 
1045
                out = cur->privateToPEM(passphrase, pbe);
 
1046
        }
 
1047
        else
 
1048
        {
 
1049
                PKeyContext *pk = static_cast<PKeyContext *>(getContext("pkey", p));
 
1050
                if(pk->importKey(cur->key()))
 
1051
                        out = pk->privateToPEM(passphrase, pbe);
 
1052
                delete pk;
 
1053
        }
 
1054
        return out;
 
1055
}
 
1056
 
 
1057
bool PrivateKey::toPEMFile(const QString &fileName, const SecureArray &passphrase, PBEAlgorithm pbe) const
 
1058
{
 
1059
        return stringToFile(fileName, toPEM(passphrase, pbe));
 
1060
}
 
1061
 
 
1062
PrivateKey PrivateKey::fromDER(const SecureArray &a, const SecureArray &passphrase, ConvertResult *result, const QString &provider)
 
1063
{
 
1064
        return get_privatekey_der(a, QString(), (void *)&a, passphrase, result, provider);
 
1065
}
 
1066
 
 
1067
PrivateKey PrivateKey::fromPEM(const QString &s, const SecureArray &passphrase, ConvertResult *result, const QString &provider)
 
1068
{
 
1069
        return get_privatekey_pem(s, QString(), (void *)&s, passphrase, result, provider);
 
1070
}
 
1071
 
 
1072
PrivateKey PrivateKey::fromPEMFile(const QString &fileName, const SecureArray &passphrase, ConvertResult *result, const QString &provider)
 
1073
{
 
1074
        QString pem;
 
1075
        if(!stringFromFile(fileName, &pem))
 
1076
        {
 
1077
                if(result)
 
1078
                        *result = ErrorFile;
 
1079
                return PrivateKey();
 
1080
        }
 
1081
        return get_privatekey_pem(pem, fileName, 0, passphrase, result, provider);
 
1082
}
 
1083
 
 
1084
//----------------------------------------------------------------------------
 
1085
// KeyGenerator
 
1086
//----------------------------------------------------------------------------
 
1087
class KeyGenerator::Private : public QObject
 
1088
{
 
1089
        Q_OBJECT
 
1090
public:
 
1091
        KeyGenerator *parent;
 
1092
        bool blocking, wasBlocking;
 
1093
        PrivateKey key;
 
1094
        DLGroup group;
 
1095
 
 
1096
        PKeyBase *k;
 
1097
        PKeyContext *dest;
 
1098
        DLGroupContext *dc;
 
1099
 
 
1100
        Private(KeyGenerator *_parent) : QObject(_parent), parent(_parent)
 
1101
        {
 
1102
                k = 0;
 
1103
                dest = 0;
 
1104
                dc = 0;
 
1105
        }
 
1106
 
 
1107
        ~Private()
 
1108
        {
 
1109
                delete k;
 
1110
                delete dest;
 
1111
                delete dc;
 
1112
        }
 
1113
 
 
1114
public slots:
 
1115
        void done()
 
1116
        {
 
1117
                if(!k->isNull())
 
1118
                {
 
1119
                        if(!wasBlocking)
 
1120
                        {
 
1121
                                k->setParent(0);
 
1122
                                k->moveToThread(0);
 
1123
                        }
 
1124
                        dest->setKey(k);
 
1125
                        k = 0;
 
1126
 
 
1127
                        key.change(dest);
 
1128
                        dest = 0;
 
1129
                }
 
1130
                else
 
1131
                {
 
1132
                        delete k;
 
1133
                        k = 0;
 
1134
                        delete dest;
 
1135
                        dest = 0;
 
1136
                }
 
1137
 
 
1138
                if(!wasBlocking)
 
1139
                        emit parent->finished();
 
1140
        }
 
1141
 
 
1142
        void done_group()
 
1143
        {
 
1144
                if(!dc->isNull())
 
1145
                {
 
1146
                        BigInteger p, q, g;
 
1147
                        dc->getResult(&p, &q, &g);
 
1148
                        group = DLGroup(p, q, g);
 
1149
                }
 
1150
                delete dc;
 
1151
                dc = 0;
 
1152
 
 
1153
                if(!wasBlocking)
 
1154
                        emit parent->finished();
 
1155
        }
 
1156
};
 
1157
 
 
1158
KeyGenerator::KeyGenerator(QObject *parent)
 
1159
:QObject(parent)
 
1160
{
 
1161
        d = new Private(this);
 
1162
        d->blocking = true;
 
1163
}
 
1164
 
 
1165
KeyGenerator::~KeyGenerator()
 
1166
{
 
1167
        delete d;
 
1168
}
 
1169
 
 
1170
bool KeyGenerator::blockingEnabled() const
 
1171
{
 
1172
        return d->blocking;
 
1173
}
 
1174
 
 
1175
void KeyGenerator::setBlockingEnabled(bool b)
 
1176
{
 
1177
        d->blocking = b;
 
1178
}
 
1179
 
 
1180
bool KeyGenerator::isBusy() const
 
1181
{
 
1182
        return (d->k ? true: false);
 
1183
}
 
1184
 
 
1185
PrivateKey KeyGenerator::createRSA(int bits, int exp, const QString &provider)
 
1186
{
 
1187
        if(isBusy())
 
1188
                return PrivateKey();
 
1189
 
 
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()));
 
1194
 
 
1195
        if(!d->blocking)
 
1196
        {
 
1197
                d->k->moveToThread(thread());
 
1198
                d->k->setParent(d);
 
1199
                connect(d->k, SIGNAL(finished()), d, SLOT(done()));
 
1200
                static_cast<RSAContext *>(d->k)->createPrivate(bits, exp, false);
 
1201
        }
 
1202
        else
 
1203
        {
 
1204
                static_cast<RSAContext *>(d->k)->createPrivate(bits, exp, true);
 
1205
                d->done();
 
1206
        }
 
1207
 
 
1208
        return d->key;
 
1209
}
 
1210
 
 
1211
PrivateKey KeyGenerator::createDSA(const DLGroup &domain, const QString &provider)
 
1212
{
 
1213
        if(isBusy())
 
1214
                return PrivateKey();
 
1215
 
 
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()));
 
1220
 
 
1221
        if(!d->blocking)
 
1222
        {
 
1223
                d->k->moveToThread(thread());
 
1224
                d->k->setParent(d);
 
1225
                connect(d->k, SIGNAL(finished()), d, SLOT(done()));
 
1226
                static_cast<DSAContext *>(d->k)->createPrivate(domain, false);
 
1227
        }
 
1228
        else
 
1229
        {
 
1230
                static_cast<DSAContext *>(d->k)->createPrivate(domain, true);
 
1231
                d->done();
 
1232
        }
 
1233
 
 
1234
        return d->key;
 
1235
}
 
1236
 
 
1237
PrivateKey KeyGenerator::createDH(const DLGroup &domain, const QString &provider)
 
1238
{
 
1239
        if(isBusy())
 
1240
                return PrivateKey();
 
1241
 
 
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()));
 
1246
 
 
1247
        if(!d->blocking)
 
1248
        {
 
1249
                d->k->moveToThread(thread());
 
1250
                d->k->setParent(d);
 
1251
                connect(d->k, SIGNAL(finished()), d, SLOT(done()));
 
1252
                static_cast<DHContext *>(d->k)->createPrivate(domain, false);
 
1253
        }
 
1254
        else
 
1255
        {
 
1256
                static_cast<DHContext *>(d->k)->createPrivate(domain, true);
 
1257
                d->done();
 
1258
        }
 
1259
 
 
1260
        return d->key;
 
1261
}
 
1262
 
 
1263
PrivateKey KeyGenerator::key() const
 
1264
{
 
1265
        return d->key;
 
1266
}
 
1267
 
 
1268
DLGroup KeyGenerator::createDLGroup(QCA::DLGroupSet set, const QString &provider)
 
1269
{
 
1270
        if(isBusy())
 
1271
                return DLGroup();
 
1272
 
 
1273
        Provider *p;
 
1274
        if(!provider.isEmpty())
 
1275
                p = providerForName(provider);
 
1276
        else
 
1277
                p = providerForGroupSet(set);
 
1278
 
 
1279
        d->group = DLGroup();
 
1280
        d->wasBlocking = d->blocking;
 
1281
        d->dc = static_cast<DLGroupContext *>(getContext("dlgroup", p));
 
1282
 
 
1283
        if(!d->blocking)
 
1284
        {
 
1285
                connect(d->dc, SIGNAL(finished()), d, SLOT(done_group()));
 
1286
                d->dc->fetchGroup(set, false);
 
1287
        }
 
1288
        else
 
1289
        {
 
1290
                d->dc->fetchGroup(set, true);
 
1291
                d->done_group();
 
1292
        }
 
1293
 
 
1294
        return d->group;
 
1295
}
 
1296
 
 
1297
DLGroup KeyGenerator::dlGroup() const
 
1298
{
 
1299
        return d->group;
 
1300
}
 
1301
 
 
1302
//----------------------------------------------------------------------------
 
1303
// RSAPublicKey
 
1304
//----------------------------------------------------------------------------
 
1305
RSAPublicKey::RSAPublicKey()
 
1306
{
 
1307
}
 
1308
 
 
1309
RSAPublicKey::RSAPublicKey(const BigInteger &n, const BigInteger &e, const QString &provider)
 
1310
{
 
1311
        RSAContext *k = static_cast<RSAContext *>(getContext("rsa", provider));
 
1312
        k->createPublic(n, e);
 
1313
        PKeyContext *c = static_cast<PKeyContext *>(getContext("pkey", k->provider()));
 
1314
        c->setKey(k);
 
1315
        change(c);
 
1316
}
 
1317
 
 
1318
RSAPublicKey::RSAPublicKey(const RSAPrivateKey &k)
 
1319
:PublicKey(k)
 
1320
{
 
1321
}
 
1322
 
 
1323
BigInteger RSAPublicKey::n() const
 
1324
{
 
1325
        return static_cast<const RSAContext *>(static_cast<const PKeyContext *>(context())->key())->n();
 
1326
}
 
1327
 
 
1328
BigInteger RSAPublicKey::e() const
 
1329
{
 
1330
        return static_cast<const RSAContext *>(static_cast<const PKeyContext *>(context())->key())->e();
 
1331
}
 
1332
 
 
1333
//----------------------------------------------------------------------------
 
1334
// RSAPrivateKey
 
1335
//----------------------------------------------------------------------------
 
1336
RSAPrivateKey::RSAPrivateKey()
 
1337
{
 
1338
}
 
1339
 
 
1340
RSAPrivateKey::RSAPrivateKey(const BigInteger &n, const BigInteger &e, const BigInteger &p, const BigInteger &q, const BigInteger &d, const QString &provider)
 
1341
{
 
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()));
 
1345
        c->setKey(k);
 
1346
        change(c);
 
1347
}
 
1348
 
 
1349
BigInteger RSAPrivateKey::n() const
 
1350
{
 
1351
        return static_cast<const RSAContext *>(static_cast<const PKeyContext *>(context())->key())->n();
 
1352
}
 
1353
 
 
1354
BigInteger RSAPrivateKey::e() const
 
1355
{
 
1356
        return static_cast<const RSAContext *>(static_cast<const PKeyContext *>(context())->key())->e();
 
1357
}
 
1358
 
 
1359
BigInteger RSAPrivateKey::p() const
 
1360
{
 
1361
        return static_cast<const RSAContext *>(static_cast<const PKeyContext *>(context())->key())->p();
 
1362
}
 
1363
 
 
1364
BigInteger RSAPrivateKey::q() const
 
1365
{
 
1366
        return static_cast<const RSAContext *>(static_cast<const PKeyContext *>(context())->key())->q();
 
1367
}
 
1368
 
 
1369
BigInteger RSAPrivateKey::d() const
 
1370
{
 
1371
        return static_cast<const RSAContext *>(static_cast<const PKeyContext *>(context())->key())->d();
 
1372
}
 
1373
 
 
1374
//----------------------------------------------------------------------------
 
1375
// DSAPublicKey
 
1376
//----------------------------------------------------------------------------
 
1377
DSAPublicKey::DSAPublicKey()
 
1378
{
 
1379
}
 
1380
 
 
1381
DSAPublicKey::DSAPublicKey(const DLGroup &domain, const BigInteger &y, const QString &provider)
 
1382
{
 
1383
        DSAContext *k = static_cast<DSAContext *>(getContext("dsa", provider));
 
1384
        k->createPublic(domain, y);
 
1385
        PKeyContext *c = static_cast<PKeyContext *>(getContext("pkey", k->provider()));
 
1386
        c->setKey(k);
 
1387
        change(c);
 
1388
}
 
1389
 
 
1390
DSAPublicKey::DSAPublicKey(const DSAPrivateKey &k)
 
1391
:PublicKey(k)
 
1392
{
 
1393
}
 
1394
 
 
1395
DLGroup DSAPublicKey::domain() const
 
1396
{
 
1397
        return static_cast<const DSAContext *>(static_cast<const PKeyContext *>(context())->key())->domain();
 
1398
}
 
1399
 
 
1400
BigInteger DSAPublicKey::y() const
 
1401
{
 
1402
        return static_cast<const DSAContext *>(static_cast<const PKeyContext *>(context())->key())->y();
 
1403
}
 
1404
 
 
1405
//----------------------------------------------------------------------------
 
1406
// DSAPrivateKey
 
1407
//----------------------------------------------------------------------------
 
1408
DSAPrivateKey::DSAPrivateKey()
 
1409
{
 
1410
}
 
1411
 
 
1412
DSAPrivateKey::DSAPrivateKey(const DLGroup &domain, const BigInteger &y, const BigInteger &x, const QString &provider)
 
1413
{
 
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()));
 
1417
        c->setKey(k);
 
1418
        change(c);
 
1419
}
 
1420
 
 
1421
DLGroup DSAPrivateKey::domain() const
 
1422
{
 
1423
        return static_cast<const DSAContext *>(static_cast<const PKeyContext *>(context())->key())->domain();
 
1424
}
 
1425
 
 
1426
BigInteger DSAPrivateKey::y() const
 
1427
{
 
1428
        return static_cast<const DSAContext *>(static_cast<const PKeyContext *>(context())->key())->y();
 
1429
}
 
1430
 
 
1431
BigInteger DSAPrivateKey::x() const
 
1432
{
 
1433
        return static_cast<const DSAContext *>(static_cast<const PKeyContext *>(context())->key())->x();
 
1434
}
 
1435
 
 
1436
//----------------------------------------------------------------------------
 
1437
// DHPublicKey
 
1438
//----------------------------------------------------------------------------
 
1439
DHPublicKey::DHPublicKey()
 
1440
{
 
1441
}
 
1442
 
 
1443
DHPublicKey::DHPublicKey(const DLGroup &domain, const BigInteger &y, const QString &provider)
 
1444
{
 
1445
        DHContext *k = static_cast<DHContext *>(getContext("dh", provider));
 
1446
        k->createPublic(domain, y);
 
1447
        PKeyContext *c = static_cast<PKeyContext *>(getContext("pkey", k->provider()));
 
1448
        c->setKey(k);
 
1449
        change(c);
 
1450
}
 
1451
 
 
1452
DHPublicKey::DHPublicKey(const DHPrivateKey &k)
 
1453
:PublicKey(k)
 
1454
{
 
1455
}
 
1456
 
 
1457
DLGroup DHPublicKey::domain() const
 
1458
{
 
1459
        return static_cast<const DHContext *>(static_cast<const PKeyContext *>(context())->key())->domain();
 
1460
}
 
1461
 
 
1462
BigInteger DHPublicKey::y() const
 
1463
{
 
1464
        return static_cast<const DHContext *>(static_cast<const PKeyContext *>(context())->key())->y();
 
1465
}
 
1466
 
 
1467
//----------------------------------------------------------------------------
 
1468
// DHPrivateKey
 
1469
//----------------------------------------------------------------------------
 
1470
DHPrivateKey::DHPrivateKey()
 
1471
{
 
1472
}
 
1473
 
 
1474
DHPrivateKey::DHPrivateKey(const DLGroup &domain, const BigInteger &y, const BigInteger &x, const QString &provider)
 
1475
{
 
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()));
 
1479
        c->setKey(k);
 
1480
        change(c);
 
1481
}
 
1482
 
 
1483
DLGroup DHPrivateKey::domain() const
 
1484
{
 
1485
        return static_cast<const DHContext *>(static_cast<const PKeyContext *>(context())->key())->domain();
 
1486
}
 
1487
 
 
1488
BigInteger DHPrivateKey::y() const
 
1489
{
 
1490
        return static_cast<const DHContext *>(static_cast<const PKeyContext *>(context())->key())->y();
 
1491
}
 
1492
 
 
1493
BigInteger DHPrivateKey::x() const
 
1494
{
 
1495
        return static_cast<const DHContext *>(static_cast<const PKeyContext *>(context())->key())->x();
 
1496
}
 
1497
 
 
1498
}
 
1499
 
 
1500
#include "qca_publickey.moc"