ssl/qsslsocket.cpp

Switch to Source codePreprocessed file
LineSource CodeCoverage
1 -
2 -
3 -
4 -
5 -
6 -
7 -
8 -
9 -
10static int qt_timeout_value(int msecs, int elapsed) -
11{ -
12 if (msecs == -1)
-
13 return -1;
-
14 -
15 int timeout = msecs - elapsed; -
16 return timeout < 0 ? 0 : timeout;
-
17} -
18 -
19class QSslSocketGlobalData -
20{ -
21public: -
22 QSslSocketGlobalData() : config(new QSslConfigurationPrivate) {}
-
23 -
24 QMutex mutex; -
25 QList<QSslCipher> supportedCiphers; -
26 QExplicitlySharedDataPointer<QSslConfigurationPrivate> config; -
27}; -
28static QSslSocketGlobalData *globalData() { static QGlobalStatic<QSslSocketGlobalData > thisGlobalStatic = { { (0) }, false }; if (!thisGlobalStatic.pointer.load() && !thisGlobalStatic.destroyed) { QSslSocketGlobalData *x = new QSslSocketGlobalData; if (!thisGlobalStatic.pointer.testAndSetOrdered(0, x)) delete x; else static QGlobalStaticDeleter<QSslSocketGlobalData > cleanup(thisGlobalStatic); } return thisGlobalStatic.pointer.load(); }
-
29 -
30 -
31 -
32 -
33 -
34 -
35QSslSocket::QSslSocket(QObject *parent) -
36 : QTcpSocket(*new QSslSocketBackendPrivate, parent) -
37{ -
38 QSslSocketPrivate * const d = d_func(); -
39 -
40 -
41 -
42 d->q_ptr = this; -
43 d->init(); -
44}
-
45 -
46 -
47 -
48 -
49QSslSocket::~QSslSocket() -
50{ -
51 QSslSocketPrivate * const d = d_func(); -
52 -
53 -
54 -
55 delete d->plainSocket; -
56 d->plainSocket = 0; -
57}
-
58void QSslSocket::resume() -
59{ -
60 -
61 QMetaObject::invokeMethod(this, "_q_resumeImplementation", Qt::QueuedConnection); -
62}
-
63void QSslSocket::connectToHostEncrypted(const QString &hostName, quint16 port, OpenMode mode, NetworkLayerProtocol protocol) -
64{ -
65 QSslSocketPrivate * const d = d_func(); -
66 if (d->state == ConnectedState || d->state == ConnectingState) {
-
67 QMessageLogger("ssl/qsslsocket.cpp", 421, __PRETTY_FUNCTION__).warning("QSslSocket::connectToHostEncrypted() called when already connecting/connected"); -
68 return;
-
69 } -
70 -
71 d->init(); -
72 d->autoStartHandshake = true; -
73 d->initialized = true; -
74 -
75 -
76 -
77 connectToHost(hostName, port, mode, protocol); -
78}
-
79void QSslSocket::connectToHostEncrypted(const QString &hostName, quint16 port, -
80 const QString &sslPeerName, OpenMode mode, -
81 NetworkLayerProtocol protocol) -
82{ -
83 QSslSocketPrivate * const d = d_func(); -
84 if (d->state == ConnectedState || d->state == ConnectingState) {
-
85 QMessageLogger("ssl/qsslsocket.cpp", 451, __PRETTY_FUNCTION__).warning("QSslSocket::connectToHostEncrypted() called when already connecting/connected"); -
86 return;
-
87 } -
88 -
89 d->init(); -
90 d->autoStartHandshake = true; -
91 d->initialized = true; -
92 d->verificationPeerName = sslPeerName; -
93 -
94 -
95 -
96 connectToHost(hostName, port, mode, protocol); -
97}
-
98bool QSslSocket::setSocketDescriptor(qintptr socketDescriptor, SocketState state, OpenMode openMode) -
99{ -
100 QSslSocketPrivate * const d = d_func(); -
101 -
102 -
103 -
104 -
105 if (!d->plainSocket)
-
106 d->createPlainSocket(openMode);
-
107 bool retVal = d->plainSocket->setSocketDescriptor(socketDescriptor, state, openMode); -
108 d->cachedSocketDescriptor = d->plainSocket->socketDescriptor(); -
109 setSocketError(d->plainSocket->error()); -
110 setSocketState(state); -
111 setOpenMode(openMode); -
112 setLocalPort(d->plainSocket->localPort()); -
113 setLocalAddress(d->plainSocket->localAddress()); -
114 setPeerPort(d->plainSocket->peerPort()); -
115 setPeerAddress(d->plainSocket->peerAddress()); -
116 setPeerName(d->plainSocket->peerName()); -
117 return retVal;
-
118} -
119 -
120 -
121 -
122 -
123 -
124 -
125 -
126void QSslSocket::setSocketOption(QAbstractSocket::SocketOption option, const QVariant &value) -
127{ -
128 QSslSocketPrivate * const d = d_func(); -
129 if (d->plainSocket)
-
130 d->plainSocket->setSocketOption(option, value);
-
131}
-
132 -
133 -
134 -
135 -
136 -
137 -
138 -
139QVariant QSslSocket::socketOption(QAbstractSocket::SocketOption option) -
140{ -
141 QSslSocketPrivate * const d = d_func(); -
142 if (d->plainSocket)
-
143 return d->plainSocket->socketOption(option);
-
144 else -
145 return QVariant();
-
146} -
147QSslSocket::SslMode QSslSocket::mode() const -
148{ -
149 const QSslSocketPrivate * const d = d_func(); -
150 return d->mode;
-
151} -
152bool QSslSocket::isEncrypted() const -
153{ -
154 const QSslSocketPrivate * const d = d_func(); -
155 return d->connectionEncrypted;
-
156} -
157 -
158 -
159 -
160 -
161 -
162 -
163QSsl::SslProtocol QSslSocket::protocol() const -
164{ -
165 const QSslSocketPrivate * const d = d_func(); -
166 return d->configuration.protocol;
-
167} -
168 -
169 -
170 -
171 -
172 -
173 -
174void QSslSocket::setProtocol(QSsl::SslProtocol protocol) -
175{ -
176 QSslSocketPrivate * const d = d_func(); -
177 d->configuration.protocol = protocol; -
178}
-
179QSslSocket::PeerVerifyMode QSslSocket::peerVerifyMode() const -
180{ -
181 const QSslSocketPrivate * const d = d_func(); -
182 return d->configuration.peerVerifyMode;
-
183} -
184void QSslSocket::setPeerVerifyMode(QSslSocket::PeerVerifyMode mode) -
185{ -
186 QSslSocketPrivate * const d = d_func(); -
187 d->configuration.peerVerifyMode = mode; -
188}
-
189int QSslSocket::peerVerifyDepth() const -
190{ -
191 const QSslSocketPrivate * const d = d_func(); -
192 return d->configuration.peerVerifyDepth;
-
193} -
194void QSslSocket::setPeerVerifyDepth(int depth) -
195{ -
196 QSslSocketPrivate * const d = d_func(); -
197 if (depth < 0) {
-
198 QMessageLogger("ssl/qsslsocket.cpp", 665, __PRETTY_FUNCTION__).warning("QSslSocket::setPeerVerifyDepth: cannot set negative depth of %d", depth); -
199 return;
-
200 } -
201 d->configuration.peerVerifyDepth = depth; -
202}
-
203QString QSslSocket::peerVerifyName() const -
204{ -
205 const QSslSocketPrivate * const d = d_func(); -
206 return d->verificationPeerName;
-
207} -
208void QSslSocket::setPeerVerifyName(const QString &hostName) -
209{ -
210 QSslSocketPrivate * const d = d_func(); -
211 d->verificationPeerName = hostName; -
212}
-
213 -
214 -
215 -
216 -
217 -
218 -
219 -
220qint64 QSslSocket::bytesAvailable() const -
221{ -
222 const QSslSocketPrivate * const d = d_func(); -
223 if (d->mode == UnencryptedMode)
-
224 return QIODevice::bytesAvailable() + (d->plainSocket ? d->plainSocket->bytesAvailable() : 0);
-
225 return QIODevice::bytesAvailable();
-
226} -
227 -
228 -
229 -
230 -
231 -
232 -
233 -
234qint64 QSslSocket::bytesToWrite() const -
235{ -
236 const QSslSocketPrivate * const d = d_func(); -
237 if (d->mode == UnencryptedMode)
-
238 return d->plainSocket ? d->plainSocket->bytesToWrite() : 0;
-
239 return d->writeBuffer.size();
-
240} -
241qint64 QSslSocket::encryptedBytesAvailable() const -
242{ -
243 const QSslSocketPrivate * const d = d_func(); -
244 if (d->mode == UnencryptedMode)
-
245 return 0;
-
246 return d->plainSocket->bytesAvailable();
-
247} -
248 -
249 -
250 -
251 -
252 -
253 -
254 -
255qint64 QSslSocket::encryptedBytesToWrite() const -
256{ -
257 const QSslSocketPrivate * const d = d_func(); -
258 if (d->mode == UnencryptedMode)
-
259 return 0;
-
260 return d->plainSocket->bytesToWrite();
-
261} -
262 -
263 -
264 -
265 -
266 -
267 -
268 -
269bool QSslSocket::canReadLine() const -
270{ -
271 const QSslSocketPrivate * const d = d_func(); -
272 if (d->mode == UnencryptedMode)
-
273 return QIODevice::canReadLine() || (d->plainSocket && d->plainSocket->canReadLine());
-
274 return QIODevice::canReadLine();
-
275} -
276 -
277 -
278 -
279 -
280void QSslSocket::close() -
281{ -
282 -
283 -
284 -
285 QSslSocketPrivate * const d = d_func(); -
286 if (d->plainSocket)
-
287 d->plainSocket->close();
-
288 QTcpSocket::close(); -
289 -
290 -
291 d->buffer.clear(); -
292 d->writeBuffer.clear(); -
293}
-
294 -
295 -
296 -
297 -
298bool QSslSocket::atEnd() const -
299{ -
300 const QSslSocketPrivate * const d = d_func(); -
301 if (d->mode == UnencryptedMode)
-
302 return QIODevice::atEnd() && (!d->plainSocket || d->plainSocket->atEnd());
-
303 return QIODevice::atEnd();
-
304} -
305bool QSslSocket::flush() -
306{ -
307 QSslSocketPrivate * const d = d_func(); -
308 -
309 -
310 -
311 if (d->mode != UnencryptedMode)
-
312 -
313 d->transmit();
-
314 -
315 return d->plainSocket ? d->plainSocket->flush() : false;
-
316} -
317 -
318 -
319 -
320 -
321 -
322 -
323void QSslSocket::setReadBufferSize(qint64 size) -
324{ -
325 QSslSocketPrivate * const d = d_func(); -
326 d->readBufferMaxSize = size; -
327 -
328 if (d->plainSocket)
-
329 d->plainSocket->setReadBufferSize(size);
-
330}
-
331void QSslSocket::abort() -
332{ -
333 QSslSocketPrivate * const d = d_func(); -
334 -
335 -
336 -
337 if (d->plainSocket)
-
338 d->plainSocket->abort();
-
339 close(); -
340}
-
341QSslConfiguration QSslSocket::sslConfiguration() const -
342{ -
343 const QSslSocketPrivate * const d = d_func(); -
344 -
345 -
346 QSslConfigurationPrivate *copy = new QSslConfigurationPrivate(d->configuration); -
347 copy->ref.store(0); -
348 copy->sessionCipher = d->sessionCipher(); -
349 -
350 return QSslConfiguration(copy);
-
351} -
352void QSslSocket::setSslConfiguration(const QSslConfiguration &configuration) -
353{ -
354 QSslSocketPrivate * const d = d_func(); -
355 d->configuration.localCertificate = configuration.localCertificate(); -
356 d->configuration.privateKey = configuration.privateKey(); -
357 d->configuration.ciphers = configuration.ciphers(); -
358 d->configuration.caCertificates = configuration.caCertificates(); -
359 d->configuration.peerVerifyDepth = configuration.peerVerifyDepth(); -
360 d->configuration.peerVerifyMode = configuration.peerVerifyMode(); -
361 d->configuration.protocol = configuration.protocol(); -
362 d->configuration.sslOptions = configuration.d->sslOptions; -
363 -
364 -
365 -
366 -
367 if (!configuration.d->allowRootCertOnDemandLoading)
partially evaluated: !configuration.d->allowRootCertOnDemandLoading
TRUEFALSE
yes
Evaluation Count:42
no
Evaluation Count:0
0-42
368 d->allowRootCertOnDemandLoading = false;
executed: d->allowRootCertOnDemandLoading = false;
Execution Count:42
42
369}
executed: }
Execution Count:42
42
370void QSslSocket::setLocalCertificate(const QSslCertificate &certificate) -
371{ -
372 QSslSocketPrivate * const d = d_func(); -
373 d->configuration.localCertificate = certificate; -
374}
-
375void QSslSocket::setLocalCertificate(const QString &path, -
376 QSsl::EncodingFormat format) -
377{ -
378 QSslSocketPrivate * const d = d_func(); -
379 QFile file(path); -
380 if (file.open(QIODevice::ReadOnly | QIODevice::Text))
-
381 d->configuration.localCertificate = QSslCertificate(file.readAll(), format);
-
382}
-
383 -
384 -
385 -
386 -
387 -
388 -
389 -
390QSslCertificate QSslSocket::localCertificate() const -
391{ -
392 const QSslSocketPrivate * const d = d_func(); -
393 return d->configuration.localCertificate;
-
394} -
395QSslCertificate QSslSocket::peerCertificate() const -
396{ -
397 const QSslSocketPrivate * const d = d_func(); -
398 return d->configuration.peerCertificate;
-
399} -
400QList<QSslCertificate> QSslSocket::peerCertificateChain() const -
401{ -
402 const QSslSocketPrivate * const d = d_func(); -
403 return d->configuration.peerCertificateChain;
-
404} -
405QSslCipher QSslSocket::sessionCipher() const -
406{ -
407 const QSslSocketPrivate * const d = d_func(); -
408 return d->sessionCipher();
-
409} -
410void QSslSocket::setPrivateKey(const QSslKey &key) -
411{ -
412 QSslSocketPrivate * const d = d_func(); -
413 d->configuration.privateKey = key; -
414}
-
415void QSslSocket::setPrivateKey(const QString &fileName, QSsl::KeyAlgorithm algorithm, -
416 QSsl::EncodingFormat format, const QByteArray &passPhrase) -
417{ -
418 QSslSocketPrivate * const d = d_func(); -
419 QFile file(fileName); -
420 if (file.open(QIODevice::ReadOnly)) {
-
421 d->configuration.privateKey = QSslKey(file.readAll(), algorithm, -
422 format, QSsl::PrivateKey, passPhrase); -
423 }
-
424}
-
425 -
426 -
427 -
428 -
429 -
430 -
431QSslKey QSslSocket::privateKey() const -
432{ -
433 const QSslSocketPrivate * const d = d_func(); -
434 return d->configuration.privateKey;
-
435} -
436QList<QSslCipher> QSslSocket::ciphers() const -
437{ -
438 const QSslSocketPrivate * const d = d_func(); -
439 return d->configuration.ciphers;
-
440} -
441void QSslSocket::setCiphers(const QList<QSslCipher> &ciphers) -
442{ -
443 QSslSocketPrivate * const d = d_func(); -
444 d->configuration.ciphers = ciphers; -
445}
-
446void QSslSocket::setCiphers(const QString &ciphers) -
447{ -
448 QSslSocketPrivate * const d = d_func(); -
449 d->configuration.ciphers.clear(); -
450 for (QForeachContainer<__typeof__(ciphers.split(QLatin1String(":"),QString::SkipEmptyParts))> _container_(ciphers.split(QLatin1String(":"),QString::SkipEmptyParts)); !_container_.brk && _container_.i != _container_.e; __extension__ ({ ++_container_.brk; ++_container_.i; })) for (const QString &cipherName = *_container_.i;; __extension__ ({--_container_.brk; break;})) { -
451 for (int i = 0; i < 3; ++i) {
-
452 -
453 QSslCipher cipher(cipherName, QSsl::SslProtocol(i)); -
454 if (!cipher.isNull())
-
455 d->configuration.ciphers << cipher;
-
456 }
-
457 }
-
458}
-
459void QSslSocket::setDefaultCiphers(const QList<QSslCipher> &ciphers) -
460{ -
461 QSslSocketPrivate::setDefaultCiphers(ciphers); -
462}
-
463QList<QSslCipher> QSslSocket::defaultCiphers() -
464{ -
465 return QSslSocketPrivate::defaultCiphers();
-
466} -
467QList<QSslCipher> QSslSocket::supportedCiphers() -
468{ -
469 return QSslSocketPrivate::supportedCiphers();
-
470} -
471bool QSslSocket::addCaCertificates(const QString &path, QSsl::EncodingFormat format, -
472 QRegExp::PatternSyntax syntax) -
473{ -
474 QSslSocketPrivate * const d = d_func(); -
475 QList<QSslCertificate> certs = QSslCertificate::fromPath(path, format, syntax); -
476 if (certs.isEmpty())
-
477 return false;
-
478 -
479 d->configuration.caCertificates += certs; -
480 return true;
-
481} -
482void QSslSocket::addCaCertificate(const QSslCertificate &certificate) -
483{ -
484 QSslSocketPrivate * const d = d_func(); -
485 d->configuration.caCertificates += certificate; -
486}
-
487void QSslSocket::addCaCertificates(const QList<QSslCertificate> &certificates) -
488{ -
489 QSslSocketPrivate * const d = d_func(); -
490 d->configuration.caCertificates += certificates; -
491}
-
492void QSslSocket::setCaCertificates(const QList<QSslCertificate> &certificates) -
493{ -
494 QSslSocketPrivate * const d = d_func(); -
495 d->configuration.caCertificates = certificates; -
496 d->allowRootCertOnDemandLoading = false; -
497}
-
498QList<QSslCertificate> QSslSocket::caCertificates() const -
499{ -
500 const QSslSocketPrivate * const d = d_func(); -
501 return d->configuration.caCertificates;
-
502} -
503bool QSslSocket::addDefaultCaCertificates(const QString &path, QSsl::EncodingFormat encoding, -
504 QRegExp::PatternSyntax syntax) -
505{ -
506 return QSslSocketPrivate::addDefaultCaCertificates(path, encoding, syntax);
-
507} -
508void QSslSocket::addDefaultCaCertificate(const QSslCertificate &certificate) -
509{ -
510 QSslSocketPrivate::addDefaultCaCertificate(certificate); -
511}
-
512void QSslSocket::addDefaultCaCertificates(const QList<QSslCertificate> &certificates) -
513{ -
514 QSslSocketPrivate::addDefaultCaCertificates(certificates); -
515}
-
516void QSslSocket::setDefaultCaCertificates(const QList<QSslCertificate> &certificates) -
517{ -
518 QSslSocketPrivate::setDefaultCaCertificates(certificates); -
519}
-
520QList<QSslCertificate> QSslSocket::defaultCaCertificates() -
521{ -
522 return QSslSocketPrivate::defaultCaCertificates();
-
523} -
524QList<QSslCertificate> QSslSocket::systemCaCertificates() -
525{ -
526 -
527 return QSslSocketPrivate::systemCaCertificates();
-
528} -
529bool QSslSocket::waitForConnected(int msecs) -
530{ -
531 QSslSocketPrivate * const d = d_func(); -
532 if (!d->plainSocket)
-
533 return false;
-
534 bool retVal = d->plainSocket->waitForConnected(msecs); -
535 if (!retVal) {
-
536 setSocketState(d->plainSocket->state()); -
537 setSocketError(d->plainSocket->error()); -
538 setErrorString(d->plainSocket->errorString()); -
539 }
-
540 return retVal;
-
541} -
542bool QSslSocket::waitForEncrypted(int msecs) -
543{ -
544 QSslSocketPrivate * const d = d_func(); -
545 if (!d->plainSocket || d->connectionEncrypted)
-
546 return false;
-
547 if (d->mode == UnencryptedMode && !d->autoStartHandshake)
-
548 return false;
-
549 -
550 QElapsedTimer stopWatch; -
551 stopWatch.start(); -
552 -
553 if (d->plainSocket->state() != QAbstractSocket::ConnectedState) {
-
554 -
555 if (!d->plainSocket->waitForConnected(msecs))
-
556 return false;
-
557 }
-
558 -
559 while (!d->connectionEncrypted) {
-
560 -
561 if (d->mode == UnencryptedMode)
-
562 startClientEncryption();
-
563 -
564 -
565 if (!d->plainSocket->waitForReadyRead(qt_timeout_value(msecs, stopWatch.elapsed())))
-
566 return false;
-
567 }
-
568 return d->connectionEncrypted;
-
569} -
570 -
571 -
572 -
573 -
574bool QSslSocket::waitForReadyRead(int msecs) -
575{ -
576 QSslSocketPrivate * const d = d_func(); -
577 if (!d->plainSocket)
-
578 return false;
-
579 if (d->mode == UnencryptedMode && !d->autoStartHandshake)
-
580 return d->plainSocket->waitForReadyRead(msecs);
-
581 -
582 -
583 -
584 -
585 -
586 bool readyReadEmitted = false; -
587 bool *previousReadyReadEmittedPointer = d->readyReadEmittedPointer; -
588 d->readyReadEmittedPointer = &readyReadEmitted; -
589 -
590 QElapsedTimer stopWatch; -
591 stopWatch.start(); -
592 -
593 if (!d->connectionEncrypted) {
-
594 -
595 if (!waitForEncrypted(msecs)) {
-
596 d->readyReadEmittedPointer = previousReadyReadEmittedPointer; -
597 return false;
-
598 } -
599 }
-
600 -
601 if (!d->writeBuffer.isEmpty()) {
-
602 -
603 d->transmit(); -
604 }
-
605 -
606 -
607 -
608 while (!readyReadEmitted &&
-
609 d->plainSocket->waitForReadyRead(qt_timeout_value(msecs, stopWatch.elapsed()))) {
-
610 }
-
611 -
612 d->readyReadEmittedPointer = previousReadyReadEmittedPointer; -
613 return readyReadEmitted;
-
614} -
615 -
616 -
617 -
618 -
619bool QSslSocket::waitForBytesWritten(int msecs) -
620{ -
621 QSslSocketPrivate * const d = d_func(); -
622 if (!d->plainSocket)
-
623 return false;
-
624 if (d->mode == UnencryptedMode)
-
625 return d->plainSocket->waitForBytesWritten(msecs);
-
626 -
627 QElapsedTimer stopWatch; -
628 stopWatch.start(); -
629 -
630 if (!d->connectionEncrypted) {
-
631 -
632 if (!waitForEncrypted(msecs))
-
633 return false;
-
634 }
-
635 if (!d->writeBuffer.isEmpty()) {
-
636 -
637 d->transmit(); -
638 }
-
639 -
640 return d->plainSocket->waitForBytesWritten(qt_timeout_value(msecs, stopWatch.elapsed()));
-
641} -
642bool QSslSocket::waitForDisconnected(int msecs) -
643{ -
644 QSslSocketPrivate * const d = d_func(); -
645 -
646 -
647 if (state() == UnconnectedState) {
-
648 QMessageLogger("ssl/qsslsocket.cpp", 15571562, __PRETTY_FUNCTION__).warning("QSslSocket::waitForDisconnected() is not allowed in UnconnectedState"); -
649 return false;
-
650 } -
651 -
652 if (!d->plainSocket)
-
653 return false;
-
654 if (d->mode == UnencryptedMode)
-
655 return d->plainSocket->waitForDisconnected(msecs);
-
656 -
657 QElapsedTimer stopWatch; -
658 stopWatch.start(); -
659 -
660 if (!d->connectionEncrypted) {
-
661 -
662 if (!waitForEncrypted(msecs))
-
663 return false;
-
664 }
-
665 bool retVal = d->plainSocket->waitForDisconnected(qt_timeout_value(msecs, stopWatch.elapsed())); -
666 if (!retVal) {
-
667 setSocketState(d->plainSocket->state()); -
668 setSocketError(d->plainSocket->error()); -
669 setErrorString(d->plainSocket->errorString()); -
670 }
-
671 return retVal;
-
672} -
673QList<QSslError> QSslSocket::sslErrors() const -
674{ -
675 const QSslSocketPrivate * const d = d_func(); -
676 return d->sslErrors;
-
677} -
678 -
679 -
680 -
681 -
682 -
683 -
684bool QSslSocket::supportsSsl() -
685{ -
686 return QSslSocketPrivate::supportsSsl();
-
687} -
688long QSslSocket::sslLibraryVersionNumber() -
689{ -
690 return QSslSocketPrivate::sslLibraryVersionNumber();
-
691} -
692 -
693 -
694 -
695 -
696 -
697 -
698 -
699QString QSslSocket::sslLibraryVersionString() -
700{ -
701 return QSslSocketPrivate::sslLibraryVersionString();
-
702} -
703void QSslSocket::startClientEncryption() -
704{ -
705 QSslSocketPrivate * const d = d_func(); -
706 if (d->mode != UnencryptedMode) {
-
707 QMessageLogger("ssl/qsslsocket.cpp", 16471652, __PRETTY_FUNCTION__).warning("QSslSocket::startClientEncryption: cannot start handshake on non-plain connection"); -
708 return;
-
709 } -
710 if (state() != ConnectedState) {
-
711 QMessageLogger("ssl/qsslsocket.cpp", 16511656, __PRETTY_FUNCTION__).warning("QSslSocket::startClientEncryption: cannot start handshake when not connected"); -
712 return;
-
713 } -
714 -
715 -
716 -
717 d->mode = SslClientMode; -
718 modeChanged(d->mode); -
719 d->startClientEncryption(); -
720}
-
721void QSslSocket::startServerEncryption() -
722{ -
723 QSslSocketPrivate * const d = d_func(); -
724 if (d->mode != UnencryptedMode) {
-
725 QMessageLogger("ssl/qsslsocket.cpp", 16861691, __PRETTY_FUNCTION__).warning("QSslSocket::startServerEncryption: cannot start handshake on non-plain connection"); -
726 return;
-
727 } -
728 -
729 -
730 -
731 d->mode = SslServerMode; -
732 modeChanged(d->mode); -
733 d->startServerEncryption(); -
734}
-
735void QSslSocket::ignoreSslErrors() -
736{ -
737 QSslSocketPrivate * const d = d_func(); -
738 d->ignoreAllSslErrors = true; -
739}
-
740void QSslSocket::ignoreSslErrors(const QList<QSslError> &errors) -
741{ -
742 QSslSocketPrivate * const d = d_func(); -
743 d->ignoreErrorsList = errors; -
744}
-
745 -
746 -
747 -
748 -
749void QSslSocket::connectToHost(const QString &hostName, quint16 port, OpenMode openMode, NetworkLayerProtocol protocol) -
750{ -
751 QSslSocketPrivate * const d = d_func(); -
752 d->preferredNetworkLayerProtocol = protocol; -
753 if (!d->initialized)
-
754 d->init();
-
755 d->initialized = false; -
756 -
757 -
758 -
759 -
760 -
761 if (!d->plainSocket) {
-
762 -
763 -
764 -
765 d->createPlainSocket(openMode); -
766 }
-
767 -
768 d->plainSocket->setProxy(proxy()); -
769 -
770 QIODevice::open(openMode); -
771 d->plainSocket->connectToHost(hostName, port, openMode, d->preferredNetworkLayerProtocol); -
772 d->cachedSocketDescriptor = d->plainSocket->socketDescriptor(); -
773}
-
774 -
775 -
776 -
777 -
778void QSslSocket::disconnectFromHost() -
779{ -
780 QSslSocketPrivate * const d = d_func(); -
781 -
782 -
783 -
784 if (!d->plainSocket)
-
785 return;
-
786 if (d->state == UnconnectedState)
-
787 return;
-
788 if (d->mode == UnencryptedMode && !d->autoStartHandshake) {
-
789 d->plainSocket->disconnectFromHost(); -
790 return;
-
791 } -
792 if (d->state <= ConnectingState) {
-
793 d->pendingClose = true; -
794 return;
-
795 } -
796 -
797 -
798 if (d->state != ClosingState) {
-
799 d->state = ClosingState; -
800 stateChanged(d->state); -
801 }
-
802 -
803 if (!d->writeBuffer.isEmpty())
-
804 return;
-
805 -
806 if (d->mode == UnencryptedMode) {
-
807 d->plainSocket->disconnectFromHost(); -
808 } else {
-
809 d->disconnectFromHost(); -
810 }
-
811} -
812 -
813 -
814 -
815 -
816qint64 QSslSocket::readData(char *data, qint64 maxlen) -
817{ -
818 QSslSocketPrivate * const d = d_func(); -
819 qint64 readBytes = 0; -
820 -
821 if (d->mode == UnencryptedMode && !d->autoStartHandshake) {
-
822 readBytes = d->plainSocket->read(data, maxlen); -
823 } else {
-
824 int bytesToRead = qMin<int>(maxlen, d->buffer.size()); -
825 readBytes = d->buffer.read(data, bytesToRead); -
826 }
-
827 -
828 -
829 -
830 -
831 -
832 -
833 if (d->buffer.isEmpty() && d->plainSocket->bytesAvailable()) {
-
834 QMetaObject::invokeMethod(this, "_q_flushReadBuffer", Qt::QueuedConnection); -
835 }
-
836 -
837 return readBytes;
-
838} -
839 -
840 -
841 -
842 -
843qint64 QSslSocket::writeData(const char *data, qint64 len) -
844{ -
845 QSslSocketPrivate * const d = d_func(); -
846 -
847 -
848 -
849 if (d->mode == UnencryptedMode && !d->autoStartHandshake)
-
850 return d->plainSocket->write(data, len);
-
851 -
852 char *writePtr = d->writeBuffer.reserve(len); -
853 ::memcpy(writePtr, data, len); -
854 -
855 -
856 QMetaObject::invokeMethod(this, "_q_flushWriteBuffer", Qt::QueuedConnection); -
857 -
858 return len;
-
859} -
860 -
861 -
862 -
863 -
864QSslSocketPrivate::QSslSocketPrivate() -
865 : initialized(false) -
866 , mode(QSslSocket::UnencryptedMode) -
867 , autoStartHandshake(false) -
868 , connectionEncrypted(false) -
869 , ignoreAllSslErrors(false) -
870 , readyReadEmittedPointer(0) -
871 , allowRootCertOnDemandLoading(true) -
872 , plainSocket(0) -
873 , paused(false) -
874{ -
875 QSslConfigurationPrivate::deepCopyDefaultConfiguration(&configuration); -
876}
-
877 -
878 -
879 -
880 -
881QSslSocketPrivate::~QSslSocketPrivate() -
882{ -
883} -
884 -
885 -
886 -
887 -
888void QSslSocketPrivate::init() -
889{ -
890 mode = QSslSocket::UnencryptedMode; -
891 autoStartHandshake = false; -
892 connectionEncrypted = false; -
893 ignoreAllSslErrors = false; -
894 -
895 -
896 -
897 -
898 -
899 buffer.clear(); -
900 writeBuffer.clear(); -
901 configuration.peerCertificate.clear(); -
902 configuration.peerCertificateChain.clear(); -
903}
-
904 -
905 -
906 -
907 -
908QList<QSslCipher> QSslSocketPrivate::defaultCiphers() -
909{ -
910 QMutexLocker locker(&globalData()->mutex); -
911 return globalData()->config->ciphers;
-
912} -
913 -
914 -
915 -
916 -
917QList<QSslCipher> QSslSocketPrivate::supportedCiphers() -
918{ -
919 QSslSocketPrivate::ensureInitialized(); -
920 QMutexLocker locker(&globalData()->mutex); -
921 return globalData()->supportedCiphers;
-
922} -
923 -
924 -
925 -
926 -
927void QSslSocketPrivate::setDefaultCiphers(const QList<QSslCipher> &ciphers) -
928{ -
929 QMutexLocker locker(&globalData()->mutex); -
930 globalData()->config.detach(); -
931 globalData()->config->ciphers = ciphers; -
932}
-
933 -
934 -
935 -
936 -
937void QSslSocketPrivate::setDefaultSupportedCiphers(const QList<QSslCipher> &ciphers) -
938{ -
939 QMutexLocker locker(&globalData()->mutex); -
940 globalData()->config.detach(); -
941 globalData()->supportedCiphers = ciphers; -
942}
-
943 -
944 -
945 -
946 -
947QList<QSslCertificate> QSslSocketPrivate::defaultCaCertificates() -
948{ -
949 QSslSocketPrivate::ensureInitialized(); -
950 QMutexLocker locker(&globalData()->mutex); -
951 return globalData()->config->caCertificates;
-
952} -
953 -
954 -
955 -
956 -
957void QSslSocketPrivate::setDefaultCaCertificates(const QList<QSslCertificate> &certs) -
958{ -
959 QSslSocketPrivate::ensureInitialized(); -
960 QMutexLocker locker(&globalData()->mutex); -
961 globalData()->config.detach(); -
962 globalData()->config->caCertificates = certs; -
963 -
964 -
965 s_loadRootCertsOnDemand = false; -
966}
-
967 -
968 -
969 -
970 -
971bool QSslSocketPrivate::addDefaultCaCertificates(const QString &path, QSsl::EncodingFormat format, -
972 QRegExp::PatternSyntax syntax) -
973{ -
974 QSslSocketPrivate::ensureInitialized(); -
975 QList<QSslCertificate> certs = QSslCertificate::fromPath(path, format, syntax); -
976 if (certs.isEmpty())
-
977 return false;
-
978 -
979 QMutexLocker locker(&globalData()->mutex); -
980 globalData()->config.detach(); -
981 globalData()->config->caCertificates += certs; -
982 return true;
-
983} -
984 -
985 -
986 -
987 -
988void QSslSocketPrivate::addDefaultCaCertificate(const QSslCertificate &cert) -
989{ -
990 QSslSocketPrivate::ensureInitialized(); -
991 QMutexLocker locker(&globalData()->mutex); -
992 globalData()->config.detach(); -
993 globalData()->config->caCertificates += cert; -
994}
-
995 -
996 -
997 -
998 -
999void QSslSocketPrivate::addDefaultCaCertificates(const QList<QSslCertificate> &certs) -
1000{ -
1001 QSslSocketPrivate::ensureInitialized(); -
1002 QMutexLocker locker(&globalData()->mutex); -
1003 globalData()->config.detach(); -
1004 globalData()->config->caCertificates += certs; -
1005}
-
1006 -
1007 -
1008 -
1009 -
1010QSslConfiguration QSslConfigurationPrivate::defaultConfiguration() -
1011{ -
1012 QSslSocketPrivate::ensureInitialized(); -
1013 QMutexLocker locker(&globalData()->mutex); -
1014 return QSslConfiguration(globalData()->config.data());
-
1015} -
1016 -
1017 -
1018 -
1019 -
1020void QSslConfigurationPrivate::setDefaultConfiguration(const QSslConfiguration &configuration) -
1021{ -
1022 QSslSocketPrivate::ensureInitialized(); -
1023 QMutexLocker locker(&globalData()->mutex); -
1024 if (globalData()->config == configuration.d)
-
1025 return;
-
1026 -
1027 globalData()->config = const_cast<QSslConfigurationPrivate*>(configuration.d.constData()); -
1028}
-
1029 -
1030 -
1031 -
1032 -
1033void QSslConfigurationPrivate::deepCopyDefaultConfiguration(QSslConfigurationPrivate *ptr) -
1034{ -
1035 QSslSocketPrivate::ensureInitialized(); -
1036 QMutexLocker locker(&globalData()->mutex); -
1037 const QSslConfigurationPrivate *global = globalData()->config.constData(); -
1038 -
1039 if (!global) {
-
1040 ptr = 0; -
1041 return;
-
1042 } -
1043 -
1044 ptr->ref.store(1); -
1045 ptr->peerCertificate = global->peerCertificate; -
1046 ptr->peerCertificateChain = global->peerCertificateChain; -
1047 ptr->localCertificate = global->localCertificate; -
1048 ptr->privateKey = global->privateKey; -
1049 ptr->sessionCipher = global->sessionCipher; -
1050 ptr->ciphers = global->ciphers; -
1051 ptr->caCertificates = global->caCertificates; -
1052 ptr->protocol = global->protocol; -
1053 ptr->peerVerifyMode = global->peerVerifyMode; -
1054 ptr->peerVerifyDepth = global->peerVerifyDepth; -
1055 ptr->sslOptions = global->sslOptions; -
1056}
-
1057 -
1058 -
1059 -
1060 -
1061void QSslSocketPrivate::createPlainSocket(QIODevice::OpenMode openMode) -
1062{ -
1063 QSslSocket * const q = q_func(); -
1064 q->setOpenMode(openMode); -
1065 q->setSocketState(QAbstractSocket::UnconnectedState); -
1066 q->setSocketError(QAbstractSocket::UnknownSocketError); -
1067 q->setLocalPort(0); -
1068 q->setLocalAddress(QHostAddress()); -
1069 q->setPeerPort(0); -
1070 q->setPeerAddress(QHostAddress()); -
1071 q->setPeerName(QString()); -
1072 -
1073 plainSocket = new QTcpSocket(q); -
1074 -
1075 -
1076 plainSocket->setProperty("_q_networksession", q->property("_q_networksession")); -
1077 -
1078 q->connect(plainSocket, "2""connected()", -
1079 q, "1""_q_connectedSlot()", -
1080 Qt::DirectConnection); -
1081 q->connect(plainSocket, "2""hostFound()", -
1082 q, "1""_q_hostFoundSlot()", -
1083 Qt::DirectConnection); -
1084 q->connect(plainSocket, "2""disconnected()", -
1085 q, "1""_q_disconnectedSlot()", -
1086 Qt::DirectConnection); -
1087 q->connect(plainSocket, "2""stateChanged(QAbstractSocket::SocketState)", -
1088 q, "1""_q_stateChangedSlot(QAbstractSocket::SocketState)", -
1089 Qt::DirectConnection); -
1090 q->connect(plainSocket, "2""error(QAbstractSocket::SocketError)", -
1091 q, "1""_q_errorSlot(QAbstractSocket::SocketError)", -
1092 Qt::DirectConnection); -
1093 q->connect(plainSocket, "2""readyRead()", -
1094 q, "1""_q_readyReadSlot()", -
1095 Qt::DirectConnection); -
1096 q->connect(plainSocket, "2""bytesWritten(qint64)", -
1097 q, "1""_q_bytesWrittenSlot(qint64)", -
1098 Qt::DirectConnection); -
1099 -
1100 q->connect(plainSocket, "2""proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)", -
1101 q, "2""proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)"); -
1102 -
1103 -
1104 buffer.clear(); -
1105 writeBuffer.clear(); -
1106 connectionEncrypted = false; -
1107 configuration.peerCertificate.clear(); -
1108 configuration.peerCertificateChain.clear(); -
1109 mode = QSslSocket::UnencryptedMode; -
1110 q->setReadBufferSize(readBufferMaxSize); -
1111}
-
1112 -
1113void QSslSocketPrivate::pauseSocketNotifiers(QSslSocket *socket) -
1114{ -
1115 if (!socket->d_func()->plainSocket)
-
1116 return;
-
1117 QAbstractSocketPrivate::pauseSocketNotifiers(socket->d_func()->plainSocket); -
1118}
-
1119 -
1120void QSslSocketPrivate::resumeSocketNotifiers(QSslSocket *socket) -
1121{ -
1122 if (!socket->d_func()->plainSocket)
-
1123 return;
-
1124 QAbstractSocketPrivate::resumeSocketNotifiers(socket->d_func()->plainSocket); -
1125}
-
1126 -
1127bool QSslSocketPrivate::isPaused() const -
1128{ -
1129 return paused;
-
1130} -
1131 -
1132 -
1133 -
1134 -
1135void QSslSocketPrivate::_q_connectedSlot() -
1136{ -
1137 QSslSocket * const q = q_func(); -
1138 q->setLocalPort(plainSocket->localPort()); -
1139 q->setLocalAddress(plainSocket->localAddress()); -
1140 q->setPeerPort(plainSocket->peerPort()); -
1141 q->setPeerAddress(plainSocket->peerAddress()); -
1142 q->setPeerName(plainSocket->peerName()); -
1143 cachedSocketDescriptor = plainSocket->socketDescriptor(); -
1144 if (autoStartHandshake)
-
1145 q->startClientEncryption();
-
1146 -
1147 q->connected(); -
1148 -
1149 if (pendingClose && !autoStartHandshake) {
-
1150 pendingClose = false; -
1151 q->disconnectFromHost(); -
1152 }
-
1153}
-
1154 -
1155 -
1156 -
1157 -
1158void QSslSocketPrivate::_q_hostFoundSlot() -
1159{ -
1160 QSslSocket * const q = q_func(); -
1161 -
1162 -
1163 -
1164 -
1165 q->hostFound(); -
1166}
-
1167 -
1168 -
1169 -
1170 -
1171void QSslSocketPrivate::_q_disconnectedSlot() -
1172{ -
1173 QSslSocket * const q = q_func(); -
1174 -
1175 -
1176 -
1177 -
1178 disconnected(); -
1179 q->disconnected(); -
1180}
-
1181 -
1182 -
1183 -
1184 -
1185void QSslSocketPrivate::_q_stateChangedSlot(QAbstractSocket::SocketState state) -
1186{ -
1187 QSslSocket * const q = q_func(); -
1188 -
1189 -
1190 -
1191 q->setSocketState(state); -
1192 q->stateChanged(state); -
1193}
-
1194 -
1195 -
1196 -
1197 -
1198void QSslSocketPrivate::_q_errorSlot(QAbstractSocket::SocketError error) -
1199{ -
1200 QSslSocket * const q = q_func(); -
1201 -
1202 -
1203 -
1204 -
1205 -
1206 q->setSocketError(plainSocket->error()); -
1207 q->setErrorString(plainSocket->errorString()); -
1208 q->error(error); -
1209}
-
1210 -
1211 -
1212 -
1213 -
1214void QSslSocketPrivate::_q_readyReadSlot() -
1215{ -
1216 QSslSocket * const q = q_func(); -
1217 -
1218 -
1219 -
1220 if (mode == QSslSocket::UnencryptedMode) {
-
1221 if (readyReadEmittedPointer)
-
1222 *readyReadEmittedPointer = true;
-
1223 q->readyRead(); -
1224 return;
-
1225 } -
1226 -
1227 transmit(); -
1228}
-
1229 -
1230 -
1231 -
1232 -
1233void QSslSocketPrivate::_q_bytesWrittenSlot(qint64 written) -
1234{ -
1235 QSslSocket * const q = q_func(); -
1236 -
1237 -
1238 -
1239 -
1240 if (mode == QSslSocket::UnencryptedMode)
-
1241 q->bytesWritten(written);
-
1242 else -
1243 q->encryptedBytesWritten(written);
-
1244 if (state == QAbstractSocket::ClosingState && writeBuffer.isEmpty())
-
1245 q->disconnectFromHost();
-
1246}
-
1247 -
1248 -
1249 -
1250 -
1251void QSslSocketPrivate::_q_flushWriteBuffer() -
1252{ -
1253 QSslSocket * const q = q_func(); -
1254 if (!writeBuffer.isEmpty())
-
1255 q->flush();
-
1256}
-
1257 -
1258 -
1259 -
1260 -
1261void QSslSocketPrivate::_q_flushReadBuffer() -
1262{ -
1263 -
1264 if (mode != QSslSocket::UnencryptedMode)
-
1265 transmit();
-
1266}
-
1267 -
1268 -
1269 -
1270 -
1271void QSslSocketPrivate::_q_resumeImplementation() -
1272{ -
1273 QSslSocket * const q = q_func(); -
1274 if (plainSocket)
-
1275 plainSocket->resume();
-
1276 paused = false; -
1277 if (!connectionEncrypted) {
-
1278 if (verifyErrorsHaveBeenIgnored()) {
-
1279 continueHandshake(); -
1280 } else {
-
1281 q->setErrorString(sslErrors.first().errorString()); -
1282 q->setSocketError(QAbstractSocket::SslHandshakeFailedError); -
1283 q->error(QAbstractSocket::SslHandshakeFailedError); -
1284 plainSocket->disconnectFromHost(); -
1285 return;
-
1286 } -
1287 } -
1288 transmit(); -
1289}
-
1290 -
1291 -
1292 -
1293 -
1294bool QSslSocketPrivate::verifyErrorsHaveBeenIgnored() -
1295{ -
1296 bool doEmitSslError; -
1297 if (!ignoreErrorsList.empty()) {
-
1298 -
1299 -
1300 -
1301 doEmitSslError = false; -
1302 for (int a = 0; a < sslErrors.count(); a++) {
-
1303 if (!ignoreErrorsList.contains(sslErrors.at(a))) {
-
1304 doEmitSslError = true; -
1305 break;
-
1306 } -
1307 }
-
1308 } else {
-
1309 -
1310 -
1311 -
1312 doEmitSslError = !ignoreAllSslErrors; -
1313 }
-
1314 return !doEmitSslError;
-
1315} -
1316 -
1317 -
1318 -
1319 -
1320qint64 QSslSocketPrivate::peek(char *data, qint64 maxSize) -
1321{ -
1322 if (mode == QSslSocket::UnencryptedMode && !autoStartHandshake) {
-
1323 -
1324 -
1325 qint64 r = buffer.peek(data, maxSize); -
1326 if (r == maxSize)
-
1327 return r;
-
1328 data += r; -
1329 -
1330 if (plainSocket) {
-
1331 qint64 r2 = plainSocket->peek(data, maxSize - r); -
1332 if (r2 < 0)
-
1333 return (r > 0 ? r : r2);
-
1334 return r + r2;
-
1335 } else { -
1336 return -1;
-
1337 } -
1338 } else { -
1339 -
1340 return QTcpSocketPrivate::peek(data, maxSize);
-
1341 } -
1342} -
1343 -
1344 -
1345 -
1346 -
1347QByteArray QSslSocketPrivate::peek(qint64 maxSize) -
1348{ -
1349 if (mode == QSslSocket::UnencryptedMode && !autoStartHandshake) {
-
1350 -
1351 -
1352 QByteArray ret; -
1353 ret.reserve(maxSize); -
1354 ret.resize(buffer.peek(ret.data(), maxSize)); -
1355 if (ret.length() == maxSize)
-
1356 return ret;
-
1357 -
1358 if (plainSocket)
-
1359 return ret + plainSocket->peek(maxSize - ret.length());
-
1360 else -
1361 return QByteArray();
-
1362 } else { -
1363 -
1364 return QTcpSocketPrivate::peek(maxSize);
-
1365 } -
1366} -
1367 -
1368 -
1369 -
1370 -
1371bool QSslSocketPrivate::rootCertOnDemandLoadingSupported() -
1372{ -
1373 return s_loadRootCertsOnDemand;
never executed: return s_loadRootCertsOnDemand;
0
1374} -
1375 -
1376 -
1377 -
1378 -
1379QList<QByteArray> QSslSocketPrivate::unixRootCertDirectories() -
1380{ -
1381 return QList<QByteArray>() << "/etc/ssl/certs/" 55
1382 << "/usr/lib/ssl/certs/" 55
1383 << "/usr/share/ssl/" 55
1384 << "/usr/local/ssl/" 55
1385 << "/var/ssl/certs/" 55
1386 << "/usr/local/ssl/certs/" 55
1387 << "/var/certmgretc/webopenssl/user_trustedcerts/" 55
1388 << "/opt/openssl/certs/";
executed: return QList<QByteArray>() << "/etc/ssl/certs/" << "/usr/lib/ssl/certs/" << "/usr/share/ssl/" << "/usr/local/ssl/" << "/var/ssl/certs/" << "/usr/local/ssl/certs/" << "/etc/openssl/certs/" << "/opt/openssl/certs/";
Execution Count:55
55
1389} -
1390 -
1391 -
1392 -
1393 -
Switch to Source codePreprocessed file

Generated by Squish Coco Non-Commercial