NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.3: NDN, CCN, CCNx, content centric networks
API Documentation
echo_server_tls.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015, Peter Thorson. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  * * Redistributions of source code must retain the above copyright
7  * notice, this list of conditions and the following disclaimer.
8  * * Redistributions in binary form must reproduce the above copyright
9  * notice, this list of conditions and the following disclaimer in the
10  * documentation and/or other materials provided with the distribution.
11  * * Neither the name of the WebSocket++ Project nor the
12  * names of its contributors may be used to endorse or promote products
13  * derived from this software without specific prior written permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL PETER THORSON BE LIABLE FOR ANY
19  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  */
27 
38 
39 #include <websocketpp/server.hpp>
40 
41 #include <iostream>
42 
44 
45 using websocketpp::lib::placeholders::_1;
46 using websocketpp::lib::placeholders::_2;
47 using websocketpp::lib::bind;
48 
49 // pull out the type of messages sent by our config
51 typedef websocketpp::lib::shared_ptr<websocketpp::lib::asio::ssl::context> context_ptr;
52 
54  std::cout << "on_message called with hdl: " << hdl.lock().get()
55  << " and message: " << msg->get_payload()
56  << std::endl;
57 
58  try {
59  s->send(hdl, msg->get_payload(), msg->get_opcode());
60  } catch (const websocketpp::lib::error_code& e) {
61  std::cout << "Echo failed because: " << e
62  << "(" << e.message() << ")" << std::endl;
63  }
64 }
65 
68 
69  con->set_body("Hello World!");
70  con->set_status(websocketpp::http::status_code::ok);
71 }
72 
73 std::string get_password() {
74  return "test";
75 }
76 
77 // See https://wiki.mozilla.org/Security/Server_Side_TLS for more details about
78 // the TLS modes. The code below demonstrates how to implement both the modern
79 enum tls_mode {
82 };
83 
85  namespace asio = websocketpp::lib::asio;
86 
87  std::cout << "on_tls_init called with hdl: " << hdl.lock().get() << std::endl;
88  std::cout << "using TLS mode: " << (mode == MOZILLA_MODERN ? "Mozilla Modern" : "Mozilla Intermediate") << std::endl;
89 
90  context_ptr ctx = websocketpp::lib::make_shared<asio::ssl::context>(asio::ssl::context::sslv23);
91 
92  try {
93  if (mode == MOZILLA_MODERN) {
94  // Modern disables TLSv1
95  ctx->set_options(asio::ssl::context::default_workarounds |
96  asio::ssl::context::no_sslv2 |
97  asio::ssl::context::no_sslv3 |
98  asio::ssl::context::no_tlsv1 |
99  asio::ssl::context::single_dh_use);
100  } else {
101  ctx->set_options(asio::ssl::context::default_workarounds |
102  asio::ssl::context::no_sslv2 |
103  asio::ssl::context::no_sslv3 |
104  asio::ssl::context::single_dh_use);
105  }
106  ctx->set_password_callback(bind(&get_password));
107  ctx->use_certificate_chain_file("server.pem");
108  ctx->use_private_key_file("server.pem", asio::ssl::context::pem);
109 
110  // Example method of generating this file:
111  // `openssl dhparam -out dh.pem 2048`
112  // Mozilla Intermediate suggests 1024 as the minimum size to use
113  // Mozilla Modern suggests 2048 as the minimum size to use.
114  ctx->use_tmp_dh_file("dh.pem");
115 
116  std::string ciphers;
117 
118  if (mode == MOZILLA_MODERN) {
119  ciphers = "ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!3DES:!MD5:!PSK";
120  } else {
121  ciphers = "ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:AES:CAMELLIA:DES-CBC3-SHA:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!aECDH:!EDH-DSS-DES-CBC3-SHA:!EDH-RSA-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA";
122  }
123 
124  if (SSL_CTX_set_cipher_list(ctx->native_handle() , ciphers.c_str()) != 1) {
125  std::cout << "Error setting cipher list" << std::endl;
126  }
127  } catch (std::exception& e) {
128  std::cout << "Exception: " << e.what() << std::endl;
129  }
130  return ctx;
131 }
132 
133 int main() {
134  // Create a server endpoint
135  server echo_server;
136 
137  // Initialize ASIO
138  echo_server.init_asio();
139 
140  // Register our message handler
141  echo_server.set_message_handler(bind(&on_message,&echo_server,::_1,::_2));
142  echo_server.set_http_handler(bind(&on_http,&echo_server,::_1));
143  echo_server.set_tls_init_handler(bind(&on_tls_init,MOZILLA_INTERMEDIATE,::_1));
144 
145  // Listen on port 9002
146  echo_server.listen(9002);
147 
148  // Start the server accept loop
149  echo_server.start_accept();
150 
151  // Start the ASIO io_service run loop
152  echo_server.run();
153 
154 }
websocketpp::config::asio_tls_client::message_type::ptr message_ptr
websocketpp::lib::shared_ptr< websocketpp::lib::asio::ssl::context > context_ptr
int main()
std::string get_password()
websocketpp::config::asio::message_type::ptr message_ptr
void on_message(server *s, websocketpp::connection_hdl hdl, message_ptr msg)
tls_mode
void on_http(server *s, websocketpp::connection_hdl hdl)
lib::weak_ptr< void > connection_hdl
A handle to uniquely identify a connection.
connection_ptr get_con_from_hdl(connection_hdl hdl, lib::error_code &ec)
Retrieves a connection_ptr from a connection_hdl (exception free)
Definition: endpoint.hpp:643
context_ptr on_tls_init(tls_mode mode, websocketpp::connection_hdl hdl)
void init_asio(io_service_ptr ptr, lib::error_code &ec)
initialize asio transport with external io_service (exception free)
Definition: endpoint.hpp:181
void set_tls_init_handler(tls_init_handler h)
Set TLS init handler.
Definition: tls.hpp:455
lib::shared_ptr< message > ptr
Definition: message.hpp:86
void start_accept(lib::error_code &ec)
Starts the server&#39;s async connection acceptance loop (exception free)
Server endpoint role based on the given config.
void set_http_handler(http_handler h)
Definition: endpoint.hpp:312
websocketpp::server< websocketpp::config::asio_tls > server
NOTES.
connection_type::ptr connection_ptr
Type of a shared pointer to the connections this server will create.
std::size_t run()
wraps the run method of the internal io_service object
Definition: endpoint.hpp:613
void listen(lib::asio::ip::tcp::endpoint const &ep, lib::error_code &ec)
Set up endpoint for listening manually (exception free)
Definition: endpoint.hpp:391
websocketpp::lib::shared_ptr< boost::asio::ssl::context > context_ptr
void set_message_handler(message_handler h)
Definition: endpoint.hpp:322
void send(connection_hdl hdl, std::string const &payload, frame::opcode::value op, lib::error_code &ec)
Create a message and add it to the outgoing send queue (exception free)