Changeset View
Changeset View
Standalone View
Standalone View
src/qca_securemessage.cpp
Show All 19 Lines | |||||
20 | */ | 20 | */ | ||
21 | 21 | | |||
22 | #include "qca_securemessage.h" | 22 | #include "qca_securemessage.h" | ||
23 | 23 | | |||
24 | #include "qcaprovider.h" | 24 | #include "qcaprovider.h" | ||
25 | #include "qca_safeobj.h" | 25 | #include "qca_safeobj.h" | ||
26 | #include "qca_safetimer.h" | 26 | #include "qca_safetimer.h" | ||
27 | 27 | | |||
28 | namespace QCA { | 28 | namespace QCA | ||
29 | { | ||||
29 | 30 | | |||
30 | Provider::Context *getContext(const QString &type, const QString &provider); | 31 | Provider::Context *getContext(const QString &type, const QString &provider); | ||
31 | 32 | | |||
32 | //---------------------------------------------------------------------------- | 33 | //---------------------------------------------------------------------------- | ||
33 | // SecureMessageKey | 34 | // SecureMessageKey | ||
34 | //---------------------------------------------------------------------------- | 35 | //---------------------------------------------------------------------------- | ||
35 | class SecureMessageKey::Private : public QSharedData | 36 | class SecureMessageKey::Private : public QSharedData | ||
36 | { | 37 | { | ||
37 | public: | 38 | public: | ||
38 | SecureMessageKey::Type type; | 39 | SecureMessageKey::Type type; | ||
39 | PGPKey pgp_pub, pgp_sec; | 40 | PGPKey pgp_pub, pgp_sec; | ||
40 | CertificateChain cert_pub; | 41 | CertificateChain cert_pub; | ||
41 | PrivateKey cert_sec; | 42 | PrivateKey cert_sec; | ||
42 | 43 | | |||
43 | Private() | 44 | Private() | ||
44 | { | 45 | { | ||
45 | type = SecureMessageKey::None; | 46 | type = SecureMessageKey::None; | ||
46 | } | 47 | } | ||
47 | 48 | | |||
48 | // set the proper type, and reset the opposite data structures if needed | 49 | // set the proper type, and reset the opposite data structures if needed | ||
49 | void ensureType(SecureMessageKey::Type t) | 50 | void ensureType(SecureMessageKey::Type t) | ||
50 | { | 51 | { | ||
51 | // if we were non-null and changed, we may need to reset some things | 52 | // if we were non-null and changed, we may need to reset some things | ||
52 | if(type != SecureMessageKey::None && t != type) | 53 | if (type != SecureMessageKey::None && t != type) { | ||
53 | { | 54 | if (type == SecureMessageKey::X509) { | ||
54 | if(type == SecureMessageKey::X509) | | |||
55 | { | | |||
56 | cert_pub = CertificateChain(); | 55 | cert_pub = CertificateChain(); | ||
57 | cert_sec = PrivateKey(); | 56 | cert_sec = PrivateKey(); | ||
58 | } | 57 | } else if (type == SecureMessageKey::PGP) { | ||
59 | else if(type == SecureMessageKey::PGP) | | |||
60 | { | | |||
61 | pgp_pub = PGPKey(); | 58 | pgp_pub = PGPKey(); | ||
62 | pgp_sec = PGPKey(); | 59 | pgp_sec = PGPKey(); | ||
63 | } | 60 | } | ||
64 | } | 61 | } | ||
65 | type = t; | 62 | type = t; | ||
66 | } | 63 | } | ||
67 | }; | 64 | }; | ||
68 | 65 | | |||
69 | SecureMessageKey::SecureMessageKey() | 66 | SecureMessageKey::SecureMessageKey() | ||
70 | :d(new Private) | 67 | : d(new Private) | ||
71 | { | 68 | { | ||
72 | } | 69 | } | ||
73 | 70 | | |||
74 | SecureMessageKey::SecureMessageKey(const SecureMessageKey &from) | 71 | SecureMessageKey::SecureMessageKey(const SecureMessageKey &from) | ||
75 | :d(from.d) | 72 | : d(from.d) | ||
76 | { | 73 | { | ||
77 | } | 74 | } | ||
78 | 75 | | |||
79 | SecureMessageKey::~SecureMessageKey() | 76 | SecureMessageKey::~SecureMessageKey() | ||
80 | { | 77 | { | ||
81 | } | 78 | } | ||
82 | 79 | | |||
83 | SecureMessageKey & SecureMessageKey::operator=(const SecureMessageKey &from) | 80 | SecureMessageKey &SecureMessageKey::operator=(const SecureMessageKey &from) | ||
84 | { | 81 | { | ||
85 | d = from.d; | 82 | d = from.d; | ||
86 | return *this; | 83 | return *this; | ||
87 | } | 84 | } | ||
88 | 85 | | |||
89 | bool SecureMessageKey::isNull() const | 86 | bool SecureMessageKey::isNull() const | ||
90 | { | 87 | { | ||
91 | return (d->type == None); | 88 | return (d->type == None); | ||
▲ Show 20 Lines • Show All 52 Lines • ▼ Show 20 Line(s) | |||||
144 | void SecureMessageKey::setX509KeyBundle(const KeyBundle &kb) | 141 | void SecureMessageKey::setX509KeyBundle(const KeyBundle &kb) | ||
145 | { | 142 | { | ||
146 | setX509CertificateChain(kb.certificateChain()); | 143 | setX509CertificateChain(kb.certificateChain()); | ||
147 | setX509PrivateKey(kb.privateKey()); | 144 | setX509PrivateKey(kb.privateKey()); | ||
148 | } | 145 | } | ||
149 | 146 | | |||
150 | bool SecureMessageKey::havePrivate() const | 147 | bool SecureMessageKey::havePrivate() const | ||
151 | { | 148 | { | ||
152 | if(d->type == SecureMessageKey::PGP && !d->pgp_sec.isNull()) | 149 | if (d->type == SecureMessageKey::PGP && !d->pgp_sec.isNull()) { | ||
153 | return true; | 150 | return true; | ||
154 | else if(d->type == SecureMessageKey::X509 && !d->cert_sec.isNull()) | 151 | } else if (d->type == SecureMessageKey::X509 && !d->cert_sec.isNull()) { | ||
155 | return true; | 152 | return true; | ||
153 | } | ||||
156 | return false; | 154 | return false; | ||
157 | } | 155 | } | ||
158 | 156 | | |||
159 | QString SecureMessageKey::name() const | 157 | QString SecureMessageKey::name() const | ||
160 | { | 158 | { | ||
161 | if(d->type == SecureMessageKey::PGP && !d->pgp_pub.isNull()) | 159 | if (d->type == SecureMessageKey::PGP && !d->pgp_pub.isNull()) { | ||
162 | return d->pgp_pub.primaryUserId(); | 160 | return d->pgp_pub.primaryUserId(); | ||
163 | else if(d->type == SecureMessageKey::X509 && !d->cert_pub.isEmpty()) | 161 | } else if (d->type == SecureMessageKey::X509 && !d->cert_pub.isEmpty()) { | ||
164 | return d->cert_pub.primary().commonName(); | 162 | return d->cert_pub.primary().commonName(); | ||
165 | else | 163 | } else { | ||
166 | return QString(); | 164 | return QString(); | ||
167 | } | 165 | } | ||
166 | } | ||||
168 | 167 | | |||
169 | //---------------------------------------------------------------------------- | 168 | //---------------------------------------------------------------------------- | ||
170 | // SecureMessageSignature | 169 | // SecureMessageSignature | ||
171 | //---------------------------------------------------------------------------- | 170 | //---------------------------------------------------------------------------- | ||
172 | class SecureMessageSignature::Private : public QSharedData | 171 | class SecureMessageSignature::Private : public QSharedData | ||
173 | { | 172 | { | ||
174 | public: | 173 | public: | ||
175 | SecureMessageSignature::IdentityResult r; | 174 | SecureMessageSignature::IdentityResult r; | ||
176 | Validity v; | 175 | Validity v; | ||
177 | SecureMessageKey key; | 176 | SecureMessageKey key; | ||
178 | QDateTime ts; | 177 | QDateTime ts; | ||
179 | 178 | | |||
180 | Private() | 179 | Private() | ||
181 | { | 180 | { | ||
182 | r = SecureMessageSignature::NoKey; | 181 | r = SecureMessageSignature::NoKey; | ||
183 | v = ErrorValidityUnknown; | 182 | v = ErrorValidityUnknown; | ||
184 | } | 183 | } | ||
185 | }; | 184 | }; | ||
186 | 185 | | |||
187 | SecureMessageSignature::SecureMessageSignature() | 186 | SecureMessageSignature::SecureMessageSignature() | ||
188 | :d(new Private) | 187 | : d(new Private) | ||
189 | { | 188 | { | ||
190 | } | 189 | } | ||
191 | 190 | | |||
192 | SecureMessageSignature::SecureMessageSignature(IdentityResult r, Validity v, const SecureMessageKey &key, const QDateTime &ts) | 191 | SecureMessageSignature::SecureMessageSignature(IdentityResult r, Validity v, const SecureMessageKey &key, const QDateTime &ts) | ||
193 | :d(new Private) | 192 | : d(new Private) | ||
194 | { | 193 | { | ||
195 | d->r = r; | 194 | d->r = r; | ||
196 | d->v = v; | 195 | d->v = v; | ||
197 | d->key = key; | 196 | d->key = key; | ||
198 | d->ts = ts; | 197 | d->ts = ts; | ||
199 | } | 198 | } | ||
200 | 199 | | |||
201 | SecureMessageSignature::SecureMessageSignature(const SecureMessageSignature &from) | 200 | SecureMessageSignature::SecureMessageSignature(const SecureMessageSignature &from) | ||
202 | :d(from.d) | 201 | : d(from.d) | ||
203 | { | 202 | { | ||
204 | } | 203 | } | ||
205 | 204 | | |||
206 | SecureMessageSignature::~SecureMessageSignature() | 205 | SecureMessageSignature::~SecureMessageSignature() | ||
207 | { | 206 | { | ||
208 | } | 207 | } | ||
209 | 208 | | |||
210 | SecureMessageSignature & SecureMessageSignature::operator=(const SecureMessageSignature &from) | 209 | SecureMessageSignature &SecureMessageSignature::operator=(const SecureMessageSignature &from) | ||
211 | { | 210 | { | ||
212 | d = from.d; | 211 | d = from.d; | ||
213 | return *this; | 212 | return *this; | ||
214 | } | 213 | } | ||
215 | 214 | | |||
216 | SecureMessageSignature::IdentityResult SecureMessageSignature::identityResult() const | 215 | SecureMessageSignature::IdentityResult SecureMessageSignature::identityResult() const | ||
217 | { | 216 | { | ||
218 | return d->r; | 217 | return d->r; | ||
Show All 12 Lines | |||||
231 | QDateTime SecureMessageSignature::timestamp() const | 230 | QDateTime SecureMessageSignature::timestamp() const | ||
232 | { | 231 | { | ||
233 | return d->ts; | 232 | return d->ts; | ||
234 | } | 233 | } | ||
235 | 234 | | |||
236 | //---------------------------------------------------------------------------- | 235 | //---------------------------------------------------------------------------- | ||
237 | // SecureMessage | 236 | // SecureMessage | ||
238 | //---------------------------------------------------------------------------- | 237 | //---------------------------------------------------------------------------- | ||
239 | enum ResetMode | 238 | enum ResetMode { | ||
240 | { | | |||
241 | ResetSession = 0, | 239 | ResetSession = 0, | ||
242 | ResetSessionAndData = 1, | 240 | ResetSessionAndData = 1, | ||
243 | ResetAll = 2 | 241 | ResetAll = 2 | ||
244 | }; | 242 | }; | ||
245 | 243 | | |||
246 | class SecureMessage::Private : public QObject | 244 | class SecureMessage::Private : public QObject | ||
247 | { | 245 | { | ||
248 | Q_OBJECT | 246 | Q_OBJECT | ||
Show All 36 Lines | 247 | public: | |||
285 | 283 | | |||
286 | void init() | 284 | void init() | ||
287 | { | 285 | { | ||
288 | connect(c, SIGNAL(updated()), SLOT(updated())); | 286 | connect(c, SIGNAL(updated()), SLOT(updated())); | ||
289 | } | 287 | } | ||
290 | 288 | | |||
291 | void reset(ResetMode mode) | 289 | void reset(ResetMode mode) | ||
292 | { | 290 | { | ||
293 | if(c) | 291 | if (c) { | ||
294 | c->reset(); | 292 | c->reset(); | ||
293 | } | ||||
295 | 294 | | |||
296 | bytesWrittenArgs.clear(); | 295 | bytesWrittenArgs.clear(); | ||
297 | readyReadTrigger.stop(); | 296 | readyReadTrigger.stop(); | ||
298 | bytesWrittenTrigger.stop(); | 297 | bytesWrittenTrigger.stop(); | ||
299 | finishedTrigger.stop(); | 298 | finishedTrigger.stop(); | ||
300 | 299 | | |||
301 | if(mode >= ResetSessionAndData) | 300 | if (mode >= ResetSessionAndData) { | ||
302 | { | | |||
303 | in.clear(); | 301 | in.clear(); | ||
304 | success = false; | 302 | success = false; | ||
305 | errorCode = SecureMessage::ErrorUnknown; | 303 | errorCode = SecureMessage::ErrorUnknown; | ||
306 | detachedSig.clear(); | 304 | detachedSig.clear(); | ||
307 | hashName = QString(); | 305 | hashName = QString(); | ||
308 | signers.clear(); | 306 | signers.clear(); | ||
309 | } | 307 | } | ||
310 | 308 | | |||
311 | if(mode >= ResetAll) | 309 | if (mode >= ResetAll) { | ||
312 | { | | |||
313 | bundleSigner = true; | 310 | bundleSigner = true; | ||
314 | format = SecureMessage::Binary; | 311 | format = SecureMessage::Binary; | ||
315 | to.clear(); | 312 | to.clear(); | ||
316 | from.clear(); | 313 | from.clear(); | ||
317 | } | 314 | } | ||
318 | } | 315 | } | ||
319 | 316 | | |||
320 | public slots: | 317 | public slots: | ||
321 | void updated() | 318 | void updated() | ||
322 | { | 319 | { | ||
323 | bool sig_read = false; | 320 | bool sig_read = false; | ||
324 | bool sig_written = false; | 321 | bool sig_written = false; | ||
325 | bool sig_done = false; | 322 | bool sig_done = false; | ||
326 | int written = 0; | 323 | int written = 0; | ||
327 | { | 324 | { | ||
328 | QByteArray a = c->read(); | 325 | QByteArray a = c->read(); | ||
329 | if(!a.isEmpty()) | 326 | if (!a.isEmpty()) { | ||
330 | { | | |||
331 | sig_read = true; | 327 | sig_read = true; | ||
332 | in.append(a); | 328 | in.append(a); | ||
333 | } | 329 | } | ||
334 | 330 | | |||
335 | int x = c->written(); | 331 | int x = c->written(); | ||
336 | if(x > 0) | 332 | if (x > 0) { | ||
337 | { | | |||
338 | sig_written = true; | 333 | sig_written = true; | ||
339 | written = x; | 334 | written = x; | ||
340 | } | 335 | } | ||
341 | } | 336 | } | ||
342 | 337 | | |||
343 | if(c->finished()) | 338 | if (c->finished()) { | ||
344 | { | | |||
345 | sig_done = true; | 339 | sig_done = true; | ||
346 | 340 | | |||
347 | success = c->success(); | 341 | success = c->success(); | ||
348 | errorCode = c->errorCode(); | 342 | errorCode = c->errorCode(); | ||
349 | dtext = c->diagnosticText(); | 343 | dtext = c->diagnosticText(); | ||
350 | if(success) | 344 | if (success) { | ||
351 | { | | |||
352 | detachedSig = c->signature(); | 345 | detachedSig = c->signature(); | ||
353 | hashName = c->hashName(); | 346 | hashName = c->hashName(); | ||
354 | signers = c->signers(); | 347 | signers = c->signers(); | ||
355 | } | 348 | } | ||
356 | reset(ResetSession); | 349 | reset(ResetSession); | ||
357 | } | 350 | } | ||
358 | 351 | | |||
359 | if(sig_read) | 352 | if (sig_read) { | ||
360 | readyReadTrigger.start(); | 353 | readyReadTrigger.start(); | ||
361 | if(sig_written) | 354 | } | ||
362 | { | 355 | if (sig_written) { | ||
363 | bytesWrittenArgs += written; | 356 | bytesWrittenArgs += written; | ||
364 | bytesWrittenTrigger.start(); | 357 | bytesWrittenTrigger.start(); | ||
365 | } | 358 | } | ||
366 | if(sig_done) | 359 | if (sig_done) { | ||
367 | finishedTrigger.start(); | 360 | finishedTrigger.start(); | ||
368 | } | 361 | } | ||
362 | } | ||||
369 | 363 | | |||
370 | void t_readyRead() | 364 | void t_readyRead() | ||
371 | { | 365 | { | ||
372 | emit q->readyRead(); | 366 | emit q->readyRead(); | ||
373 | } | 367 | } | ||
374 | 368 | | |||
375 | void t_bytesWritten() | 369 | void t_bytesWritten() | ||
376 | { | 370 | { | ||
▲ Show 20 Lines • Show All 123 Lines • ▼ Show 20 Line(s) | 493 | { | |||
500 | d->reset(ResetSessionAndData); | 494 | d->reset(ResetSessionAndData); | ||
501 | d->c->setupSign(d->from, m, d->bundleSigner, d->smime); | 495 | d->c->setupSign(d->from, m, d->bundleSigner, d->smime); | ||
502 | d->c->start(d->format, MessageContext::Sign); | 496 | d->c->start(d->format, MessageContext::Sign); | ||
503 | } | 497 | } | ||
504 | 498 | | |||
505 | void SecureMessage::startVerify(const QByteArray &sig) | 499 | void SecureMessage::startVerify(const QByteArray &sig) | ||
506 | { | 500 | { | ||
507 | d->reset(ResetSessionAndData); | 501 | d->reset(ResetSessionAndData); | ||
508 | if(!sig.isEmpty()) | 502 | if (!sig.isEmpty()) { | ||
509 | d->c->setupVerify(sig); | 503 | d->c->setupVerify(sig); | ||
504 | } | ||||
510 | d->c->start(d->format, MessageContext::Verify); | 505 | d->c->start(d->format, MessageContext::Verify); | ||
511 | } | 506 | } | ||
512 | 507 | | |||
513 | void SecureMessage::startSignAndEncrypt() | 508 | void SecureMessage::startSignAndEncrypt() | ||
514 | { | 509 | { | ||
515 | d->reset(ResetSessionAndData); | 510 | d->reset(ResetSessionAndData); | ||
516 | d->c->setupEncrypt(d->to); | 511 | d->c->setupEncrypt(d->to); | ||
517 | d->c->setupSign(d->from, Message, d->bundleSigner, d->smime); | 512 | d->c->setupSign(d->from, Message, d->bundleSigner, d->smime); | ||
▲ Show 20 Lines • Show All 52 Lines • ▼ Show 20 Line(s) | |||||
570 | bool SecureMessage::wasSigned() const | 565 | bool SecureMessage::wasSigned() const | ||
571 | { | 566 | { | ||
572 | return !d->signers.isEmpty(); | 567 | return !d->signers.isEmpty(); | ||
573 | } | 568 | } | ||
574 | 569 | | |||
575 | bool SecureMessage::verifySuccess() const | 570 | bool SecureMessage::verifySuccess() const | ||
576 | { | 571 | { | ||
577 | // if we're not done or there were no signers, then return false | 572 | // if we're not done or there were no signers, then return false | ||
578 | if(!d->success || d->signers.isEmpty()) | 573 | if (!d->success || d->signers.isEmpty()) { | ||
579 | return false; | 574 | return false; | ||
575 | } | ||||
580 | 576 | | |||
581 | // make sure all signers have a valid signature | 577 | // make sure all signers have a valid signature | ||
582 | for(int n = 0; n < d->signers.count(); ++n) | 578 | for (int n = 0; n < d->signers.count(); ++n) { | ||
583 | { | 579 | if (d->signers[n].identityResult() != SecureMessageSignature::Valid) { | ||
584 | if(d->signers[n].identityResult() != SecureMessageSignature::Valid) | | |||
585 | return false; | 580 | return false; | ||
586 | } | 581 | } | ||
582 | } | ||||
587 | return true; | 583 | return true; | ||
588 | } | 584 | } | ||
589 | 585 | | |||
590 | SecureMessageSignature SecureMessage::signer() const | 586 | SecureMessageSignature SecureMessage::signer() const | ||
591 | { | 587 | { | ||
592 | if(d->signers.isEmpty()) | 588 | if (d->signers.isEmpty()) { | ||
593 | return SecureMessageSignature(); | 589 | return SecureMessageSignature(); | ||
590 | } | ||||
594 | 591 | | |||
595 | return d->signers.first(); | 592 | return d->signers.first(); | ||
596 | } | 593 | } | ||
597 | 594 | | |||
598 | SecureMessageSignatureList SecureMessage::signers() const | 595 | SecureMessageSignatureList SecureMessage::signers() const | ||
599 | { | 596 | { | ||
600 | return d->signers; | 597 | return d->signers; | ||
601 | } | 598 | } | ||
602 | 599 | | |||
603 | QString SecureMessage::diagnosticText() const | 600 | QString SecureMessage::diagnosticText() const | ||
604 | { | 601 | { | ||
605 | return d->dtext; | 602 | return d->dtext; | ||
606 | } | 603 | } | ||
607 | 604 | | |||
608 | //---------------------------------------------------------------------------- | 605 | //---------------------------------------------------------------------------- | ||
609 | // SecureMessageSystem | 606 | // SecureMessageSystem | ||
610 | //---------------------------------------------------------------------------- | 607 | //---------------------------------------------------------------------------- | ||
611 | SecureMessageSystem::SecureMessageSystem(QObject *parent, const QString &type, const QString &provider) | 608 | SecureMessageSystem::SecureMessageSystem(QObject *parent, const QString &type, const QString &provider) | ||
612 | :QObject(parent), Algorithm(type, provider) | 609 | : QObject(parent), Algorithm(type, provider) | ||
613 | { | 610 | { | ||
614 | } | 611 | } | ||
615 | 612 | | |||
616 | SecureMessageSystem::~SecureMessageSystem() | 613 | SecureMessageSystem::~SecureMessageSystem() | ||
617 | { | 614 | { | ||
618 | } | 615 | } | ||
619 | 616 | | |||
620 | //---------------------------------------------------------------------------- | 617 | //---------------------------------------------------------------------------- | ||
621 | // OpenPGP | 618 | // OpenPGP | ||
622 | //---------------------------------------------------------------------------- | 619 | //---------------------------------------------------------------------------- | ||
623 | OpenPGP::OpenPGP(QObject *parent, const QString &provider) | 620 | OpenPGP::OpenPGP(QObject *parent, const QString &provider) | ||
624 | :SecureMessageSystem(parent, "openpgp", provider) | 621 | : SecureMessageSystem(parent, "openpgp", provider) | ||
625 | { | 622 | { | ||
626 | } | 623 | } | ||
627 | 624 | | |||
628 | OpenPGP::~OpenPGP() | 625 | OpenPGP::~OpenPGP() | ||
629 | { | 626 | { | ||
630 | } | 627 | } | ||
631 | 628 | | |||
632 | //---------------------------------------------------------------------------- | 629 | //---------------------------------------------------------------------------- | ||
633 | // CMS | 630 | // CMS | ||
634 | //---------------------------------------------------------------------------- | 631 | //---------------------------------------------------------------------------- | ||
635 | class CMS::Private | 632 | class CMS::Private | ||
636 | { | 633 | { | ||
637 | public: | 634 | public: | ||
638 | CertificateCollection trusted, untrusted; | 635 | CertificateCollection trusted, untrusted; | ||
639 | SecureMessageKeyList privateKeys; | 636 | SecureMessageKeyList privateKeys; | ||
640 | }; | 637 | }; | ||
641 | 638 | | |||
642 | CMS::CMS(QObject *parent, const QString &provider) | 639 | CMS::CMS(QObject *parent, const QString &provider) | ||
643 | :SecureMessageSystem(parent, "cms", provider) | 640 | : SecureMessageSystem(parent, "cms", provider) | ||
644 | { | 641 | { | ||
645 | d = new Private; | 642 | d = new Private; | ||
646 | } | 643 | } | ||
647 | 644 | | |||
648 | CMS::~CMS() | 645 | CMS::~CMS() | ||
649 | { | 646 | { | ||
650 | delete d; | 647 | delete d; | ||
651 | } | 648 | } | ||
Show All 37 Lines |