~ubuntu-branches/ubuntu/wily/psi/wily-proposed

« back to all changes in this revision

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

  • Committer: Package Import Robot
  • Author(s): Jan Niehusmann
  • Date: 2014-07-01 21:49:34 UTC
  • mfrom: (6.1.7 sid)
  • Revision ID: package-import@ubuntu.com-20140701214934-gt4dkgm94byi4vnn
Tags: 0.15-1
* New upstream version
* set debhelper compat level to 9
* set Standards-Version to 3.9.5 (no further changes)
* add lintian override regarding license-problem-non-free-RFC
* use qconf to regenerate configure script
* implement hardening using buildflags instead of hardening-wrapper

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