Fix copyright lines
[folly.git] / folly / io / async / ssl / SSLErrors.cpp
1 /*
2  * Copyright 2016-present Facebook, Inc.
3  *
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
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 #include <folly/io/async/ssl/SSLErrors.h>
17
18 #include <folly/Range.h>
19 #include <folly/portability/OpenSSL.h>
20
21 using namespace folly;
22
23 namespace {
24
25 std::string decodeOpenSSLError(
26     int sslError,
27     unsigned long errError,
28     int sslOperationReturnValue) {
29   if (sslError == SSL_ERROR_SYSCALL && errError == 0) {
30     if (sslOperationReturnValue == 0) {
31       return "Connection EOF";
32     } else {
33       // In this case errno is set, AsyncSocketException will add it.
34       return "Network error";
35     }
36   } else if (sslError == SSL_ERROR_ZERO_RETURN) {
37     // This signifies a TLS closure alert.
38     return "SSL connection closed normally";
39   } else {
40     std::array<char, 256> buf;
41     ERR_error_string_n(errError, buf.data(), buf.size());
42     // OpenSSL will null terminate the string.
43     return std::string(buf.data());
44   }
45 }
46
47 const StringPiece getSSLErrorString(SSLError error) {
48   StringPiece ret;
49   switch (error) {
50     case SSLError::CLIENT_RENEGOTIATION:
51       ret = "Client tried to renegotiate with server";
52       break;
53     case SSLError::INVALID_RENEGOTIATION:
54       ret = "Attempt to start renegotiation, but unsupported";
55       break;
56     case SSLError::EARLY_WRITE:
57       ret = "Attempt to write before SSL connection established";
58       break;
59     case SSLError::SSL_ERROR:
60       ret = "SSL error";
61       break;
62     case SSLError::NETWORK_ERROR:
63       ret = "Network error";
64       break;
65     case SSLError::EOF_ERROR:
66       ret = "SSL connection closed normally";
67       break;
68   }
69   return ret;
70 }
71
72 AsyncSocketException::AsyncSocketExceptionType exTypefromSSLErrInfo(
73     int sslErr,
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;
81     } else {
82       return AsyncSocketException::NETWORK_ERROR;
83     }
84   } else {
85     // Assume an actual SSL error
86     return AsyncSocketException::SSL_ERROR;
87   }
88 }
89
90 AsyncSocketException::AsyncSocketExceptionType exTypefromSSLErr(SSLError err) {
91   switch (err) {
92     case SSLError::EOF_ERROR:
93       return AsyncSocketException::END_OF_FILE;
94     case SSLError::NETWORK_ERROR:
95       return AsyncSocketException::NETWORK_ERROR;
96     default:
97       // everything else is a SSL_ERROR
98       return AsyncSocketException::SSL_ERROR;
99   }
100 }
101 } // namespace
102
103 namespace folly {
104
105 SSLException::SSLException(
106     int sslErr,
107     unsigned long errError,
108     int sslOperationReturnValue,
109     int errno_copy)
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;
118   } else {
119     // Conservatively assume that this is an SSL error
120     sslError = SSLError::SSL_ERROR;
121   }
122 }
123
124 SSLException::SSLException(SSLError error)
125     : AsyncSocketException(
126           exTypefromSSLErr(error),
127           getSSLErrorString(error).str(),
128           0),
129       sslError(error) {}
130 } // namespace folly