| 1 |
/* |
| 2 |
* |
| 3 |
* Licensed to the Apache Software Foundation (ASF) under one |
| 4 |
* or more contributor license agreements. See the NOTICE file |
| 5 |
* distributed with this work for additional information |
| 6 |
* regarding copyright ownership. The ASF licenses this file |
| 7 |
* to you under the Apache License, Version 2.0 (the |
| 8 |
* "License"); you may not use this file except in compliance |
| 9 |
* with the License. You may obtain a copy of the License at |
| 10 |
* |
| 11 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 12 |
* |
| 13 |
* Unless required by applicable law or agreed to in writing, |
| 14 |
* software distributed under the License is distributed on an |
| 15 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 16 |
* KIND, either express or implied. See the License for the |
| 17 |
* specific language governing permissions and limitations |
| 18 |
* under the License. |
| 19 |
* |
| 20 |
*/ |
| 21 |
|
| 22 |
#include "options.hpp" |
| 23 |
|
| 24 |
#include <proton/connection_options.hpp> |
| 25 |
#include <proton/connection.hpp> |
| 26 |
#include <proton/container.hpp> |
| 27 |
#include <proton/error_condition.hpp> |
| 28 |
#include <proton/listen_handler.hpp> |
| 29 |
#include <proton/listener.hpp> |
| 30 |
#include <proton/message.hpp> |
| 31 |
#include <proton/messaging_handler.hpp> |
| 32 |
#include <proton/ssl.hpp> |
| 33 |
#include <proton/tracker.hpp> |
| 34 |
#include <proton/transport.hpp> |
| 35 |
|
| 36 |
#include <iostream> |
| 37 |
|
| 38 |
|
| 39 |
using proton::connection_options; |
| 40 |
using proton::ssl_client_options; |
| 41 |
using proton::ssl_server_options; |
| 42 |
using proton::ssl_certificate; |
| 43 |
|
| 44 |
// Helper functions defined below. |
| 45 |
bool using_OpenSSL(); |
| 46 |
std::string platform_CA(const std::string &base_name); |
| 47 |
ssl_certificate platform_certificate(const std::string &base_name, const std::string &passwd); |
| 48 |
std::string find_CN(const std::string &); |
| 49 |
|
| 50 |
namespace { |
| 51 |
const std::string verify_full("full"); // Normal verification |
| 52 |
const std::string verify_noname("noname"); // Skip matching host name against the certificate |
| 53 |
const std::string verify_fail("fail"); // Force name mismatch failure |
| 54 |
std::string verify(verify_full); // Default for example |
| 55 |
std::string cert_directory; |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
struct server_handler : public proton::messaging_handler { |
| 60 |
proton::listener listener; |
| 61 |
|
| 62 |
void on_connection_open(proton::connection &c) override { |
| 63 |
std::cout << "Inbound server connection connected via SSL. Protocol: " << |
| 64 |
c.transport().ssl().protocol() << std::endl; |
| 65 |
listener.stop(); // Just expecting the one connection. |
| 66 |
|
| 67 |
// Go and do default inbound open stuff too |
| 68 |
messaging_handler::on_connection_open(c); |
| 69 |
} |
| 70 |
|
| 71 |
void on_transport_error(proton::transport &t) override { |
| 72 |
listener.stop(); |
| 73 |
} |
| 74 |
|
| 75 |
void on_message(proton::delivery &, proton::message &m) override { |
| 76 |
std::cout << m.body() << std::endl; |
| 77 |
} |
| 78 |
}; |
| 79 |
|
| 80 |
|
| 81 |
class hello_world_direct : public proton::messaging_handler { |
| 82 |
private: |
| 83 |
class listener_open_handler : public proton::listen_handler { |
| 84 |
void on_open(proton::listener& l) override { |
| 85 |
std::ostringstream url; |
| 86 |
url << "//:" << l.port() << "/example"; // Connect to the actual port |
| 87 |
l.container().open_sender(url.str()); |
| 88 |
} |
| 89 |
}; |
| 90 |
|
| 91 |
listener_open_handler listen_handler; |
| 92 |
server_handler s_handler; |
| 93 |
|
| 94 |
public: |
| 95 |
void on_container_start(proton::container &c) override { |
| 96 |
// Configure listener. Details vary by platform. |
| 97 |
ssl_certificate server_cert = platform_certificate("tserver", "tserverpw"); |
| 98 |
ssl_server_options ssl_srv(server_cert); |
| 99 |
connection_options server_opts; |
| 100 |
server_opts.ssl_server_options(ssl_srv).handler(s_handler); |
| 101 |
c.server_connection_options(server_opts); |
| 102 |
|
| 103 |
// Configure client with a Certificate Authority database |
| 104 |
// populated with the server's self signed certificate. |
| 105 |
connection_options client_opts; |
| 106 |
if (verify == verify_full) { |
| 107 |
ssl_client_options ssl_cli(platform_CA("tserver")); |
| 108 |
client_opts.ssl_client_options(ssl_cli); |
| 109 |
// The next line is optional in normal use. Since the |
| 110 |
// example uses IP addresses in the connection string, use |
| 111 |
// the virtual_host option to set the server host name |
| 112 |
// used for certificate verification: |
| 113 |
client_opts.virtual_host("test_server"); |
| 114 |
} else if (verify == verify_noname) { |
| 115 |
// Downgrade the verification from VERIFY_PEER_NAME to VERIFY_PEER. |
| 116 |
ssl_client_options ssl_cli(platform_CA("tserver"), proton::ssl::VERIFY_PEER); |
| 117 |
client_opts.ssl_client_options(ssl_cli); |
| 118 |
} else if (verify == verify_fail) { |
| 119 |
ssl_client_options ssl_cli(platform_CA("tserver")); |
| 120 |
client_opts.ssl_client_options(ssl_cli); |
| 121 |
client_opts.virtual_host("wrong_name_for_server"); // Pick any name that doesn't match. |
| 122 |
} else throw std::logic_error("bad verify mode: " + verify); |
| 123 |
|
| 124 |
c.client_connection_options(client_opts); |
| 125 |
s_handler.listener = c.listen("//:0", listen_handler); // Listen on port 0 for a dynamic port |
| 126 |
} |
| 127 |
|
| 128 |
void on_connection_open(proton::connection &c) override { |
| 129 |
std::string subject = c.transport().ssl().remote_subject(); |
| 130 |
std::cout << "Outgoing client connection connected via SSL. Server certificate identity " << |
| 131 |
find_CN(subject) << std::endl; |
| 132 |
} |
| 133 |
|
| 134 |
void on_transport_error(proton::transport &t) override { |
| 135 |
std::string err = t.error().what(); |
| 136 |
if (verify == verify_fail && err.find("certificate") != std::string::npos) { |
| 137 |
std::cout << "Expected failure of connection with wrong peer name: " << err |
| 138 |
<< std::endl; |
| 139 |
} else { |
| 140 |
std::cout << "Unexpected transport error: " << err << std::endl; |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
void on_sendable(proton::sender &s) override { |
| 145 |
proton::message m; |
| 146 |
m.body("Hello World!"); |
| 147 |
s.send(m); |
| 148 |
s.close(); |
| 149 |
} |
| 150 |
|
| 151 |
void on_tracker_accept(proton::tracker &t) override { |
| 152 |
// All done. |
| 153 |
t.connection().close(); |
| 154 |
} |
| 155 |
}; |
| 156 |
|
| 157 |
int main(int argc, char **argv) { |
| 158 |
example::options opts(argc, argv); |
| 159 |
opts.add_value(cert_directory, 'c', "cert_directory", |
| 160 |
"directory containing SSL certificates and private key information", "CERTDIR"); |
| 161 |
opts.add_value(verify, 'v', "verify", "verify type: \"minimum\", \"full\", \"fail\"", "VERIFY"); |
| 162 |
|
| 163 |
try { |
| 164 |
opts.parse(); |
| 165 |
|
| 166 |
size_t sz = cert_directory.size(); |
| 167 |
if (sz) { |
| 168 |
if (cert_directory[sz -1] != '/') { |
| 169 |
cert_directory.append("/"); |
| 170 |
} |
| 171 |
} else { |
| 172 |
cert_directory = "ssl-certs/"; |
| 173 |
} |
| 174 |
|
| 175 |
if (verify != verify_noname && verify != verify_full && verify != verify_fail) { |
| 176 |
throw std::runtime_error("bad verify argument: " + verify); |
| 177 |
} |
| 178 |
|
| 179 |
hello_world_direct hwd; |
| 180 |
proton::container(hwd).run(); |
| 181 |
return 0; |
| 182 |
} catch (const example::bad_option& e) { |
| 183 |
std::cout << opts << std::endl << e.what() << std::endl; |
| 184 |
} catch (const std::exception& e) { |
| 185 |
std::cerr << e.what() << std::endl; |
| 186 |
} |
| 187 |
return 1; |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
bool using_OpenSSL() { |
| 192 |
// Current defaults. |
| 193 |
#if defined(_WIN32) |
| 194 |
return false; |
| 195 |
#else |
| 196 |
return true; |
| 197 |
#endif |
| 198 |
} |
| 199 |
|
| 200 |
ssl_certificate platform_certificate(const std::string &base_name, const std::string &passwd) { |
| 201 |
if (using_OpenSSL()) { |
| 202 |
// The first argument will be the name of the file containing the public certificate, the |
| 203 |
// second argument will be the name of the file containing the private key. |
| 204 |
return ssl_certificate(cert_directory + base_name + "-certificate.pem", |
| 205 |
cert_directory + base_name + "-private-key.pem", passwd); |
| 206 |
} |
| 207 |
else { |
| 208 |
// Windows SChannel |
| 209 |
// The first argument will be the database or store that contains one or more complete certificates |
| 210 |
// (public and private data). The second will be an optional name of the certificate in the store |
| 211 |
// (not used in this example with one certificate per store). |
| 212 |
return ssl_certificate(cert_directory + base_name + "-full.p12", "", passwd); |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
std::string platform_CA(const std::string &base_name) { |
| 217 |
if (using_OpenSSL()) { |
| 218 |
// In this simple example with self-signed certificates, the peer's certificate is the CA database. |
| 219 |
return cert_directory + base_name + "-certificate.pem"; |
| 220 |
} |
| 221 |
else { |
| 222 |
// Windows SChannel. Use a pkcs#12 file with just the peer's public certificate information. |
| 223 |
return cert_directory + base_name + "-certificate.p12"; |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
std::string find_CN(const std::string &subject) { |
| 228 |
// The subject string is returned with different whitespace and component ordering between platforms. |
| 229 |
// Here we just return the common name by searching for "CN=...." in the subject, knowing that |
| 230 |
// the test certificates do not contain any escaped characters. |
| 231 |
size_t pos = subject.find("CN="); |
| 232 |
if (pos == std::string::npos) throw std::runtime_error("No common name in certificate subject"); |
| 233 |
std::string cn = subject.substr(pos); |
| 234 |
pos = cn.find(','); |
| 235 |
return pos == std::string::npos ? cn : cn.substr(0, pos); |
| 236 |
} |