rippled
WorkSSL.h
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2016 Ripple Labs Inc.
5 
6  Permission to use, copy, modify, and/or distribute this software for any
7  purpose with or without fee is hereby granted, provided that the above
8  copyright notice and this permission notice appear in all copies.
9 
10  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 //==============================================================================
19 
20 #ifndef RIPPLE_APP_MISC_DETAIL_WORKSSL_H_INCLUDED
21 #define RIPPLE_APP_MISC_DETAIL_WORKSSL_H_INCLUDED
22 
23 #include <ripple/app/misc/detail/WorkBase.h>
24 #include <ripple/basics/contract.h>
25 #include <ripple/core/Config.h>
26 #include <ripple/net/HTTPClientSSLContext.h>
27 #include <boost/asio/ssl.hpp>
28 #include <boost/bind.hpp>
29 #include <boost/format.hpp>
30 
31 namespace ripple {
32 
33 namespace detail {
34 
35 // Work over SSL
36 class WorkSSL : public WorkBase<WorkSSL>,
37  public std::enable_shared_from_this<WorkSSL>
38 {
39  friend class WorkBase<WorkSSL>;
40 
41 private:
42  using stream_type = boost::asio::ssl::stream<socket_type&>;
43 
46 
47 public:
48  WorkSSL(
49  std::string const& host,
50  std::string const& path,
51  std::string const& port,
52  boost::asio::io_service& ios,
54  Config const& config,
55  callback_type cb);
56  ~WorkSSL() = default;
57 
58 private:
61  {
62  return stream_;
63  }
64 
65  void
66  onConnect(error_code const& ec);
67 
68  void
69  onHandshake(error_code const& ec);
70 };
71 
72 //------------------------------------------------------------------------------
73 
75  std::string const& host,
76  std::string const& path,
77  std::string const& port,
78  boost::asio::io_service& ios,
80  Config const& config,
81  callback_type cb)
82  : WorkBase(host, path, port, ios, cb)
83  , context_(config, j, boost::asio::ssl::context::tlsv12_client)
84  , stream_(socket_, context_.context())
85 {
87  if (ec)
88  Throw<std::runtime_error>(
89  boost::str(boost::format("preConnectVerify: %s") % ec.message()));
90 }
91 
92 void
93 WorkSSL::onConnect(error_code const& ec)
94 {
95  auto err = ec ? ec : context_.postConnectVerify(stream_, host_);
96  if (err)
97  return fail(err);
98 
99  stream_.async_handshake(
100  boost::asio::ssl::stream_base::client,
101  strand_.wrap(boost::bind(
104  boost::asio::placeholders::error)));
105 }
106 
107 void
108 WorkSSL::onHandshake(error_code const& ec)
109 {
110  if (ec)
111  return fail(ec);
112 
113  onStart();
114 }
115 
116 } // namespace detail
117 
118 } // namespace ripple
119 
120 #endif
ripple::detail::WorkSSL::stream_type
boost::asio::ssl::stream< socket_type & > stream_type
Definition: WorkSSL.h:42
ripple::detail::WorkSSL::~WorkSSL
~WorkSSL()=default
std::string
STL class.
ripple::detail::WorkBase< WorkSSL >::fail
void fail(error_code const &ec)
Definition: WorkBase.h:164
ripple::detail::WorkSSL
Definition: WorkSSL.h:36
ripple::HTTPClientSSLContext::preConnectVerify
boost::system::error_code preConnectVerify(T &strm, std::string const &host)
invoked before connect/async_connect on an ssl stream to setup name verification.
Definition: HTTPClientSSLContext.h:107
ripple::detail::WorkBase< WorkSSL >::host_
std::string host_
Definition: WorkBase.h:53
ripple::detail::WorkSSL::onHandshake
void onHandshake(error_code const &ec)
Definition: WorkSSL.h:108
boost
Definition: IPAddress.h:117
ripple::detail::WorkSSL::stream
stream_type & stream()
Definition: WorkSSL.h:60
std::enable_shared_from_this< WorkSSL >::shared_from_this
T shared_from_this(T... args)
ripple::detail::WorkSSL::stream_
stream_type stream_
Definition: WorkSSL.h:45
ripple::detail::WorkSSL::context_
HTTPClientSSLContext context_
Definition: WorkSSL.h:44
ripple::Config
Definition: Config.h:66
ripple::detail::WorkBase< WorkSSL >::onStart
void onStart()
Definition: WorkBase.h:190
std::enable_shared_from_this
ripple::HTTPClientSSLContext::postConnectVerify
boost::system::error_code postConnectVerify(T &strm, std::string const &host)
invoked after connect/async_connect but before sending data on an ssl stream - to setup name verifica...
Definition: HTTPClientSSLContext.h:142
beast::Journal
A generic endpoint for log messages.
Definition: Journal.h:58
ripple::detail::WorkSSL::WorkSSL
WorkSSL(std::string const &host, std::string const &path, std::string const &port, boost::asio::io_service &ios, beast::Journal j, Config const &config, callback_type cb)
Definition: WorkSSL.h:74
ripple::detail::WorkSSL::onConnect
void onConnect(error_code const &ec)
Definition: WorkSSL.h:93
ripple::HTTPClientSSLContext
Definition: HTTPClientSSLContext.h:34
ripple::detail::WorkBase
Definition: WorkBase.h:36
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::detail::WorkBase< WorkSSL >::callback_type
std::function< void(error_code const &, response_type &&)> callback_type
Definition: WorkBase.h:43
ripple::detail::WorkBase< WorkSSL >::error_code
boost::system::error_code error_code
Definition: WorkBase.h:39
ripple::detail::WorkBase< WorkSSL >::strand_
boost::asio::io_service::strand strand_
Definition: WorkBase.h:58