2 * Copyright 2016 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 #include <folly/io/async/ssl/SSLErrors.h>
18 #include <folly/Range.h>
19 #include <openssl/err.h>
20 #include <openssl/ssl.h>
22 using namespace folly;
26 std::string decodeOpenSSLError(
28 unsigned long errError,
29 int sslOperationReturnValue) {
30 if (sslError == SSL_ERROR_SYSCALL && errError == 0) {
31 if (sslOperationReturnValue == 0) {
32 return "Connection EOF";
34 // In this case errno is set, AsyncSocketException will add it.
35 return "Network error";
37 } else if (sslError == SSL_ERROR_ZERO_RETURN) {
38 // This signifies a TLS closure alert.
39 return "SSL connection closed normally";
41 std::array<char, 256> buf;
42 std::string msg(ERR_error_string(errError, buf.data()));
47 const StringPiece getSSLErrorString(SSLError error) {
50 case SSLError::CLIENT_RENEGOTIATION:
51 ret = "Client tried to renegotiate with server";
53 case SSLError::INVALID_RENEGOTIATION:
54 ret = "Attempt to start renegotiation, but unsupported";
56 case SSLError::EARLY_WRITE:
57 ret = "Attempt to write before SSL connection established";
59 case SSLError::SSL_ERROR:
62 case SSLError::NETWORK_ERROR:
63 ret = "Network error";
65 case SSLError::EOF_ERROR:
66 ret = "SSL connection closed normally";
72 AsyncSocketException::AsyncSocketExceptionType exTypefromSSLErrInfo(
74 unsigned long errError,
75 int sslOperationReturnValue) {
76 if (sslErr == SSL_ERROR_ZERO_RETURN) {
77 return AsyncSocketException::END_OF_FILE;
78 } else if (sslErr == SSL_ERROR_SYSCALL) {
79 if (errError == 0 && sslOperationReturnValue == 0) {
80 return AsyncSocketException::END_OF_FILE;
82 return AsyncSocketException::NETWORK_ERROR;
85 // Assume an actual SSL error
86 return AsyncSocketException::SSL_ERROR;
90 AsyncSocketException::AsyncSocketExceptionType exTypefromSSLErr(SSLError err) {
92 case SSLError::EOF_ERROR:
93 return AsyncSocketException::END_OF_FILE;
94 case SSLError::NETWORK_ERROR:
95 return AsyncSocketException::NETWORK_ERROR;
97 // everything else is a SSL_ERROR
98 return AsyncSocketException::SSL_ERROR;
105 SSLException::SSLException(
107 unsigned long errError,
108 int sslOperationReturnValue,
110 : AsyncSocketException(
111 exTypefromSSLErrInfo(sslErr, errError, sslOperationReturnValue),
112 decodeOpenSSLError(sslErr, errError, sslOperationReturnValue),
113 sslErr == SSL_ERROR_SYSCALL ? errno_copy : 0) {
114 if (sslErr == SSL_ERROR_ZERO_RETURN) {
115 sslError = SSLError::EOF_ERROR;
116 } else if (sslErr == SSL_ERROR_SYSCALL) {
117 sslError = SSLError::NETWORK_ERROR;
119 // Conservatively assume that this is an SSL error
120 sslError = SSLError::SSL_ERROR;
124 SSLException::SSLException(SSLError error)
125 : AsyncSocketException(
126 exTypefromSSLErr(error),
127 getSSLErrorString(error).str(),