Better exception types to SSLexception
[folly.git] / folly / io / async / ssl / SSLErrors.cpp
1 /*
2  * Copyright 2016 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 <openssl/err.h>
20 #include <openssl/ssl.h>
21
22 using namespace folly;
23
24 namespace {
25
26 std::string decodeOpenSSLError(
27     int sslError,
28     unsigned long errError,
29     int sslOperationReturnValue) {
30   if (sslError == SSL_ERROR_SYSCALL && errError == 0) {
31     if (sslOperationReturnValue == 0) {
32       return "Connection EOF";
33     } else {
34       // In this case errno is set, AsyncSocketException will add it.
35       return "Network error";
36     }
37   } else if (sslError == SSL_ERROR_ZERO_RETURN) {
38     // This signifies a TLS closure alert.
39     return "SSL connection closed normally";
40   } else {
41     std::array<char, 256> buf;
42     std::string msg(ERR_error_string(errError, buf.data()));
43     return msg;
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
73 namespace folly {
74
75 SSLException::SSLException(
76     int sslErr,
77     unsigned long errError,
78     int sslOperationReturnValue,
79     int errno_copy)
80     : AsyncSocketException(
81           AsyncSocketException::SSL_ERROR,
82           decodeOpenSSLError(sslErr, errError, sslOperationReturnValue),
83           sslErr == SSL_ERROR_SYSCALL ? errno_copy : 0) {
84   if (sslErr == SSL_ERROR_ZERO_RETURN) {
85     sslError = SSLError::EOF_ERROR;
86   } else if (sslErr == SSL_ERROR_SYSCALL) {
87     sslError = SSLError::NETWORK_ERROR;
88   } else {
89     // Conservatively assume that this is an SSL error
90     sslError = SSLError::SSL_ERROR;
91   }
92 }
93
94 SSLException::SSLException(SSLError error)
95     : AsyncSocketException(
96           AsyncSocketException::SSL_ERROR,
97           getSSLErrorString(error).str(),
98           0),
99       sslError(error) {}
100 }