proxygen
SSLErrors.cpp
Go to the documentation of this file.
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  */
17 
18 #include <folly/Range.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) {
51  ret = "Client tried to renegotiate with server";
52  break;
54  ret = "Attempt to start renegotiation, but unsupported";
55  break;
57  ret = "Attempt to write before SSL connection established";
58  break;
60  ret = "SSL error";
61  break;
63  ret = "Network error";
64  break;
66  ret = "SSL connection closed normally";
67  break;
68  }
69  return ret;
70 }
71 
73  int sslErr,
74  unsigned long errError,
75  int sslOperationReturnValue) {
76  if (sslErr == SSL_ERROR_ZERO_RETURN) {
78  } else if (sslErr == SSL_ERROR_SYSCALL) {
79  if (errError == 0 && sslOperationReturnValue == 0) {
81  } else {
83  }
84  } else {
85  // Assume an actual SSL error
87  }
88 }
89 
91  switch (err) {
96  default:
97  // everything else is a SSL_ERROR
99  }
100 }
101 } // namespace
102 
103 namespace folly {
104 
106  int sslErr,
107  unsigned long errError,
108  int sslOperationReturnValue,
109  int errno_copy)
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 
126  exTypefromSSLErr(error),
127  getSSLErrorString(error).str(),
128  0),
129  sslError(error) {}
130 } // namespace folly
SSLException(int sslError, unsigned long errError, int sslOperationReturnValue, int errno_copy)
Definition: SSLErrors.cpp:105
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
requires And< SemiMovable< VN >... > &&SemiMovable< E > auto error(E e)
Definition: error.h:48
SSLError
Definition: SSLErrors.h:22
const char * string
Definition: Conv.cpp:212