proxygen
SSLAcceptorHandshakeHelper.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2017-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 <string>
21 
22 namespace wangle {
23 
25 
26 using namespace folly;
27 
31  socket_ = std::move(sock);
32  callback_ = callback;
33 
34  socket_->enableClientHelloParsing();
35  socket_->forceCacheAddrOnFailure(true);
36  socket_->sslAccept(this);
37 }
38 
40  AsyncSSLSocket* sock, TransportInfo& tinfo) {
41  tinfo.secure = true;
42  tinfo.securityType = sock->getSecurityProtocol();
43  tinfo.sslSetupBytesRead = sock->getRawBytesReceived();
45  tinfo.sslServerName = sock->getSSLServerName() ?
46  std::make_shared<std::string>(sock->getSSLServerName()) : nullptr;
47  tinfo.sslCipher = sock->getNegotiatedCipherName() ?
48  std::make_shared<std::string>(sock->getNegotiatedCipherName()) : nullptr;
49  tinfo.sslVersion = sock->getSSLVersion();
50  const char* sigAlgName = sock->getSSLCertSigAlgName();
51  tinfo.sslCertSigAlgName =
52  std::make_shared<std::string>(sigAlgName ? sigAlgName : "");
53  tinfo.sslCertSize = sock->getSSLCertSize();
54  tinfo.sslResume = SSLUtil::getResumeState(sock);
55  tinfo.sslClientCiphers = std::make_shared<std::string>();
57  tinfo.sslClientCiphersHex = std::make_shared<std::string>();
58  sock->getSSLClientCiphers(
59  *tinfo.sslClientCiphersHex, /* convertToString = */ false);
60  tinfo.sslServerCiphers = std::make_shared<std::string>();
62  tinfo.sslClientComprMethods =
63  std::make_shared<std::string>(sock->getSSLClientComprMethods());
64  tinfo.sslClientExts =
65  std::make_shared<std::string>(sock->getSSLClientExts());
66  tinfo.sslClientSigAlgs =
67  std::make_shared<std::string>(sock->getSSLClientSigAlgs());
69  std::make_shared<std::string>(sock->getSSLClientSupportedVersions());
70 }
71 
73  const unsigned char* nextProto = nullptr;
74  unsigned nextProtoLength = 0;
75  sock->getSelectedNextProtocolNoThrow(&nextProto, &nextProtoLength);
76  if (VLOG_IS_ON(3)) {
77  if (nextProto) {
78  VLOG(3) << "Client selected next protocol " <<
79  std::string((const char*)nextProto, nextProtoLength);
80  } else {
81  VLOG(3) << "Client did not select a next protocol";
82  }
83  }
84 
85  // fill in SSL-related fields from TransportInfo
86  // the other fields like RTT are filled in the Acceptor
87  tinfo_.acceptTime = acceptTime_;
88  tinfo_.sslSetupTime = std::chrono::duration_cast<std::chrono::milliseconds>(
89  std::chrono::steady_clock::now() - acceptTime_
90  );
91  fillSSLTransportInfoFields(sock, tinfo_);
92 
93  auto nextProtocol = nextProto ?
94  std::string((const char*)nextProto, nextProtoLength) : empty_string;
95 
96  // The callback will delete this.
97  callback_->connectionReady(
99  std::move(nextProtocol),
102 }
103 
105  AsyncSSLSocket* sock,
106  const AsyncSocketException& ex) noexcept {
107  auto elapsedTime =
108  std::chrono::duration_cast<std::chrono::milliseconds>(
109  std::chrono::steady_clock::now() - acceptTime_);
110  VLOG(3) << "SSL handshake error after " << elapsedTime.count() <<
111  " ms; " << sock->getRawBytesReceived() << " bytes received & " <<
112  sock->getRawBytesWritten() << " bytes sent: " <<
113  ex.what();
114 
115  auto sslEx = folly::make_exception_wrapper<SSLException>(
116  sslError_, elapsedTime, sock->getRawBytesReceived());
117 
118  // The callback will delete this.
119  callback_->connectionError(socket_.get(), sslEx, sslError_);
120 }
121 
122 }
void handshakeSuc(folly::AsyncSSLSocket *sock) noexceptoverride
size_t getRawBytesReceived() const override
static const std::string empty_string
Definition: Acceptor.cpp:51
std::shared_ptr< std::string > sslClientCiphers
const char * getSSLCertSigAlgName() const
size_t getRawBytesWritten() const override
std::string getSSLClientComprMethods() const
std::string getSSLClientSupportedVersions() const
std::chrono::steady_clock::time_point now()
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
std::string getSecurityProtocol() const override
std::shared_ptr< std::string > sslClientExts
std::string securityType
void start(folly::AsyncSSLSocket::UniquePtr sock, AcceptorHandshakeHelper::Callback *callback) noexceptoverride
const char * getSSLServerName() const
std::shared_ptr< std::string > sslClientSigAlgs
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
requires E e noexcept(noexcept(s.error(std::move(e))))
SSLResumeEnum sslResume
std::unique_ptr< AsyncSSLSocket, Destructor > UniquePtr
std::shared_ptr< std::string > sslServerCiphers
std::shared_ptr< std::string > sslServerName
static SSLResumeEnum getResumeState(folly::AsyncSSLSocket *sslSocket)
Definition: SSLUtil.cpp:46
void handshakeErr(folly::AsyncSSLSocket *sock, const folly::AsyncSocketException &ex) noexceptoverride
AsyncServerSocket::UniquePtr socket_
static void fillSSLTransportInfoFields(folly::AsyncSSLSocket *sock, TransportInfo &tinfo)
void getSSLClientCiphers(std::string &clientCiphers, bool convertToString=true) const
std::shared_ptr< std::string > sslCertSigAlgName
std::shared_ptr< std::string > sslCipher
std::string getSSLClientExts() const
void getSSLServerCiphers(std::string &serverCiphers) const
std::string getSSLClientSigAlgs() const
const char * string
Definition: Conv.cpp:212
std::shared_ptr< std::string > sslClientComprMethods
folly::Function< void()> callback_
virtual const char * getNegotiatedCipherName() const
std::shared_ptr< std::string > sslClientSupportedVersions
std::shared_ptr< std::string > sslClientCiphersHex