34 typedef websocketpp::lib::shared_ptr<websocketpp::lib::asio::ssl::context>
context_ptr;
36 using websocketpp::lib::placeholders::_1;
37 using websocketpp::lib::placeholders::_2;
38 using websocketpp::lib::bind;
41 std::cout << msg->get_payload() << std::endl;
46 STACK_OF(GENERAL_NAME) * san_names = NULL;
48 san_names = (STACK_OF(GENERAL_NAME) *) X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL);
49 if (san_names == NULL) {
53 int san_names_count = sk_GENERAL_NAME_num(san_names);
57 for (
int i = 0; i < san_names_count; i++) {
58 const GENERAL_NAME * current_name = sk_GENERAL_NAME_value(san_names, i);
60 if (current_name->type != GEN_DNS) {
64 char * dns_name = (
char *) ASN1_STRING_data(current_name->d.dNSName);
67 if (ASN1_STRING_length(current_name->d.dNSName) != strlen(dns_name)) {
71 result = (strcasecmp(hostname, dns_name) == 0);
73 sk_GENERAL_NAME_pop_free(san_names, GENERAL_NAME_free);
81 int common_name_loc = X509_NAME_get_index_by_NID(X509_get_subject_name(cert), NID_commonName, -1);
82 if (common_name_loc < 0) {
87 X509_NAME_ENTRY * common_name_entry = X509_NAME_get_entry(X509_get_subject_name(cert), common_name_loc);
88 if (common_name_entry == NULL) {
93 ASN1_STRING * common_name_asn1 = X509_NAME_ENTRY_get_data(common_name_entry);
94 if (common_name_asn1 == NULL) {
98 char * common_name_str = (
char *) ASN1_STRING_data(common_name_asn1);
101 if (ASN1_STRING_length(common_name_asn1) != strlen(common_name_str)) {
106 return (strcasecmp(hostname, common_name_str) == 0);
115 bool verify_certificate(
const char * hostname,
bool preverified, boost::asio::ssl::verify_context& ctx) {
129 int depth = X509_STORE_CTX_get_error_depth(ctx.native_handle());
133 if (depth == 0 && preverified) {
134 X509* cert = X509_STORE_CTX_get_current_cert(ctx.native_handle());
179 context_ptr ctx = websocketpp::lib::make_shared<boost::asio::ssl::context>(boost::asio::ssl::context::sslv23);
182 ctx->set_options(boost::asio::ssl::context::default_workarounds |
183 boost::asio::ssl::context::no_sslv2 |
184 boost::asio::ssl::context::no_sslv3 |
185 boost::asio::ssl::context::single_dh_use);
188 ctx->set_verify_mode(boost::asio::ssl::verify_peer);
192 ctx->load_verify_file(
"ca-chain.cert.pem");
193 }
catch (std::exception& e) {
194 std::cout << e.what() << std::endl;
199 int main(
int argc,
char* argv[]) {
202 std::string hostname =
"localhost";
203 std::string port =
"9002";
210 std::cout <<
"Usage: print_server_tls <hostname> <port>" << std::endl;
214 std::string uri =
"wss://" + hostname +
":" + port;
227 c.set_tls_init_handler(bind(&
on_tls_init, hostname.c_str(), ::_1));
229 websocketpp::lib::error_code ec;
232 std::cout <<
"could not create connection because: " << ec.message() << std::endl;
247 std::cout << e.
what() << std::endl;
static level const all
Special aggregate value representing "all levels".
connection_type::ptr connection_ptr
Type of a shared pointer to the connections this server will create.
bool verify_certificate(const char *hostname, bool preverified, boost::asio::ssl::verify_context &ctx)
This code is derived from examples and documentation found ato00po http://www.boost.org/doc/libs/1_61_0/doc/html/boost_asio/example/cpp03/ssl/client.cpp and https://github.com/iSECPartners/ssl-conservatory.
static level const app
Special channel for application specific logs. Not used by the library.
context_ptr on_tls_init(const char *hostname, websocketpp::connection_hdl)
TLS Initialization handler.
Client endpoint role based on the given config.
connection_type::message_ptr message_ptr
Type of message pointers that this endpoint uses.
static level const frame_payload
One line per frame, includes the full message payload (warning: chatty)
lib::weak_ptr< void > connection_hdl
A handle to uniquely identify a connection.
alog_type & get_alog()
Get reference to access logger.
bool verify_common_name(const char *hostname, X509 *cert)
Verify that the certificate common name matches the given hostname.
websocketpp::client< websocketpp::config::asio_tls_client > client
void on_message(websocketpp::connection_hdl, client::message_ptr msg)
connection_ptr connect(connection_ptr con)
Begin the connection process for the given connection.
int main(int argc, char *argv[])
void set_access_channels(log::level channels)
Set Access logging channel.
websocketpp::lib::shared_ptr< websocketpp::lib::asio::ssl::context > context_ptr
void clear_access_channels(log::level channels)
Clear Access logging channels.
bool verify_subject_alternative_name(const char *hostname, X509 *cert)
Verify that one of the subject alternative names matches the given hostname.
void set_error_channels(log::level channels)
Set Error logging channel.
websocketpp::lib::shared_ptr< boost::asio::ssl::context > context_ptr
void set_message_handler(message_handler h)
virtual char const * what() const
static level const all
Special aggregate value representing "all levels".
connection_ptr get_connection(uri_ptr location, lib::error_code &ec)
Get a new connection.