diff options
| author | Edwin Mons <edwin.mons@isode.com> | 2019-01-08 16:43:31 (GMT) |
|---|---|---|
| committer | Edwin Mons <edwin.mons@isode.com> | 2019-01-14 15:22:30 (GMT) |
| commit | 6f6ad903d9e248f59bddedb3ab4cae41a7d8bec0 (patch) | |
| tree | 0d02c381e515856a501b7589afd9c98e6b713ce2 /Swiften/TLS | |
| parent | 24ddcdb0a82cbd33deb5b72ad9f86f1c46fc9d13 (diff) | |
| download | swift-6f6ad903d9e248f59bddedb3ab4cae41a7d8bec0.zip swift-6f6ad903d9e248f59bddedb3ab4cae41a7d8bec0.tar.bz2 | |
Add optional message to TLSError
TLSError now takes an optional error message. OpenSSLContext has been
updated to send out one, and calls to SWIFT_LOG have been removed from
it for anything but setCertificateChain.
OpenSSLContext::handleDataFromApplication misinterpreted the return code
of SSL_write, triggering an onError in cases where more network I/O was
required.
Test-Information:
Unit tests pass on Debian 9
Server test code no longer emits undesirable warnings to stderr on macOS
10.14.
Change-Id: If0f932693361ef9738ae50d5445bfb4d3ed9b28f
Diffstat (limited to 'Swiften/TLS')
| -rw-r--r-- | Swiften/TLS/OpenSSL/OpenSSLContext.cpp | 23 | ||||
| -rw-r--r-- | Swiften/TLS/TLSContext.h | 1 | ||||
| -rw-r--r-- | Swiften/TLS/TLSError.h | 18 |
3 files changed, 25 insertions, 17 deletions
diff --git a/Swiften/TLS/OpenSSL/OpenSSLContext.cpp b/Swiften/TLS/OpenSSL/OpenSSLContext.cpp index 89917ee..968ef8f 100644 --- a/Swiften/TLS/OpenSSL/OpenSSLContext.cpp +++ b/Swiften/TLS/OpenSSL/OpenSSLContext.cpp | |||
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | * Copyright (c) 2010-2018 Isode Limited. | 2 | * Copyright (c) 2010-2019 Isode Limited. |
| 3 | * All rights reserved. | 3 | * All rights reserved. |
| 4 | * See the COPYING file for more information. | 4 | * See the COPYING file for more information. |
| 5 | */ | 5 | */ |
| @@ -180,7 +180,7 @@ void OpenSSLContext::accept() { | |||
| 180 | handle_ = std::unique_ptr<SSL>(SSL_new(context_.get())); | 180 | handle_ = std::unique_ptr<SSL>(SSL_new(context_.get())); |
| 181 | if (!handle_) { | 181 | if (!handle_) { |
| 182 | state_ = State::Error; | 182 | state_ = State::Error; |
| 183 | onError(std::make_shared<TLSError>()); | 183 | onError(std::make_shared<TLSError>(TLSError::AcceptFailed, openSSLInternalErrorToString())); |
| 184 | return; | 184 | return; |
| 185 | } | 185 | } |
| 186 | 186 | ||
| @@ -199,13 +199,14 @@ void OpenSSLContext::connect(const std::string& requestedServerName) { | |||
| 199 | handle_ = std::unique_ptr<SSL>(SSL_new(context_.get())); | 199 | handle_ = std::unique_ptr<SSL>(SSL_new(context_.get())); |
| 200 | if (!handle_) { | 200 | if (!handle_) { |
| 201 | state_ = State::Error; | 201 | state_ = State::Error; |
| 202 | onError(std::make_shared<TLSError>()); | 202 | onError(std::make_shared<TLSError>(TLSError::ConnectFailed, openSSLInternalErrorToString())); |
| 203 | return; | 203 | return; |
| 204 | } | 204 | } |
| 205 | 205 | ||
| 206 | if (!requestedServerName.empty()) { | 206 | if (!requestedServerName.empty()) { |
| 207 | if (SSL_set_tlsext_host_name(handle_.get(), const_cast<char*>(requestedServerName.c_str())) != 1) { | 207 | if (SSL_set_tlsext_host_name(handle_.get(), const_cast<char*>(requestedServerName.c_str())) != 1) { |
| 208 | SWIFT_LOG(error) << "Failed on SSL_set_tlsext_host_name()." << std::endl; | 208 | onError(std::make_shared<TLSError>(TLSError::ConnectFailed, "Failed to set Server Name Indication: " + openSSLInternalErrorToString()));\ |
| 209 | return; | ||
| 209 | } | 210 | } |
| 210 | } | 211 | } |
| 211 | 212 | ||
| @@ -237,9 +238,8 @@ void OpenSSLContext::doAccept() { | |||
| 237 | sendPendingDataToNetwork(); | 238 | sendPendingDataToNetwork(); |
| 238 | break; | 239 | break; |
| 239 | default: | 240 | default: |
| 240 | SWIFT_LOG(warning) << openSSLInternalErrorToString() << std::endl; | ||
| 241 | state_ = State::Error; | 241 | state_ = State::Error; |
| 242 | onError(std::make_shared<TLSError>()); | 242 | onError(std::make_shared<TLSError>(TLSError::AcceptFailed, openSSLInternalErrorToString())); |
| 243 | sendPendingDataToNetwork(); | 243 | sendPendingDataToNetwork(); |
| 244 | } | 244 | } |
| 245 | } | 245 | } |
| @@ -260,9 +260,9 @@ void OpenSSLContext::doConnect() { | |||
| 260 | sendPendingDataToNetwork(); | 260 | sendPendingDataToNetwork(); |
| 261 | break; | 261 | break; |
| 262 | default: | 262 | default: |
| 263 | SWIFT_LOG(warning) << openSSLInternalErrorToString() << std::endl; | ||
| 264 | state_ = State::Error; | 263 | state_ = State::Error; |
| 265 | onError(std::make_shared<TLSError>()); | 264 | onError(std::make_shared<TLSError>()); |
| 265 | onError(std::make_shared<TLSError>(TLSError::ConnectFailed, openSSLInternalErrorToString())); | ||
| 266 | } | 266 | } |
| 267 | } | 267 | } |
| 268 | 268 | ||
| @@ -312,12 +312,13 @@ void OpenSSLContext::handleDataFromNetwork(const SafeByteArray& data) { | |||
| 312 | } | 312 | } |
| 313 | 313 | ||
| 314 | void OpenSSLContext::handleDataFromApplication(const SafeByteArray& data) { | 314 | void OpenSSLContext::handleDataFromApplication(const SafeByteArray& data) { |
| 315 | if (SSL_write(handle_.get(), vecptr(data), data.size()) >= 0) { | 315 | auto ret = SSL_write(handle_.get(), vecptr(data), data.size()); |
| 316 | sendPendingDataToNetwork(); | 316 | if (ret > 0 || SSL_get_error(handle_.get(), ret) == SSL_ERROR_WANT_READ) { |
| 317 | sendPendingDataToNetwork(); | ||
| 317 | } | 318 | } |
| 318 | else { | 319 | else { |
| 319 | state_ = State::Error; | 320 | state_ = State::Error; |
| 320 | onError(std::make_shared<TLSError>()); | 321 | onError(std::make_shared<TLSError>(TLSError::UnknownError, openSSLInternalErrorToString())); |
| 321 | } | 322 | } |
| 322 | } | 323 | } |
| 323 | 324 | ||
| @@ -333,7 +334,7 @@ void OpenSSLContext::sendPendingDataToApplication() { | |||
| 333 | } | 334 | } |
| 334 | if (ret < 0 && SSL_get_error(handle_.get(), ret) != SSL_ERROR_WANT_READ) { | 335 | if (ret < 0 && SSL_get_error(handle_.get(), ret) != SSL_ERROR_WANT_READ) { |
| 335 | state_ = State::Error; | 336 | state_ = State::Error; |
| 336 | onError(std::make_shared<TLSError>()); | 337 | onError(std::make_shared<TLSError>(TLSError::UnknownError, openSSLInternalErrorToString())); |
| 337 | } | 338 | } |
| 338 | } | 339 | } |
| 339 | 340 | ||
diff --git a/Swiften/TLS/TLSContext.h b/Swiften/TLS/TLSContext.h index 55a86cd..9b0a2eb 100644 --- a/Swiften/TLS/TLSContext.h +++ b/Swiften/TLS/TLSContext.h | |||
| @@ -50,7 +50,6 @@ namespace Swift { | |||
| 50 | virtual ByteArray getFinishMessage() const = 0; | 50 | virtual ByteArray getFinishMessage() const = 0; |
| 51 | virtual ByteArray getPeerFinishMessage() const; | 51 | virtual ByteArray getPeerFinishMessage() const; |
| 52 | 52 | ||
| 53 | |||
| 54 | public: | 53 | public: |
| 55 | enum class Mode { | 54 | enum class Mode { |
| 56 | Client, | 55 | Client, |
diff --git a/Swiften/TLS/TLSError.h b/Swiften/TLS/TLSError.h index ae775e6..9e4af2f 100644 --- a/Swiften/TLS/TLSError.h +++ b/Swiften/TLS/TLSError.h | |||
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | * Copyright (c) 2012-2016 Isode Limited. | 2 | * Copyright (c) 2012-2019 Isode Limited. |
| 3 | * All rights reserved. | 3 | * All rights reserved. |
| 4 | * See the COPYING file for more information. | 4 | * See the COPYING file for more information. |
| 5 | */ | 5 | */ |
| @@ -7,6 +7,7 @@ | |||
| 7 | #pragma once | 7 | #pragma once |
| 8 | 8 | ||
| 9 | #include <memory> | 9 | #include <memory> |
| 10 | #include <string> | ||
| 10 | 11 | ||
| 11 | #include <Swiften/Base/API.h> | 12 | #include <Swiften/Base/API.h> |
| 12 | #include <Swiften/Base/Error.h> | 13 | #include <Swiften/Base/Error.h> |
| @@ -18,16 +19,23 @@ namespace Swift { | |||
| 18 | 19 | ||
| 19 | enum Type { | 20 | enum Type { |
| 20 | UnknownError, | 21 | UnknownError, |
| 21 | CertificateCardRemoved | 22 | CertificateCardRemoved, |
| 23 | AcceptFailed, | ||
| 24 | ConnectFailed | ||
| 22 | }; | 25 | }; |
| 23 | 26 | ||
| 24 | TLSError(Type type = UnknownError) : type(type) {} | 27 | TLSError(Type type = UnknownError, std::string message = "") : type_(type), message_(std::move(message)) {} |
| 25 | 28 | ||
| 26 | Type getType() const { | 29 | Type getType() const { |
| 27 | return type; | 30 | return type_; |
| 31 | } | ||
| 32 | |||
| 33 | const std::string& getMessage() const { | ||
| 34 | return message_; | ||
| 28 | } | 35 | } |
| 29 | 36 | ||
| 30 | private: | 37 | private: |
| 31 | Type type; | 38 | Type type_; |
| 39 | std::string message_; | ||
| 32 | }; | 40 | }; |
| 33 | } | 41 | } |
Swift