Debian Codegraph @

cpp/examples/ssl_client_cert.cpp

raw
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 <proton/connection.hpp>
23 #include <proton/connection_options.hpp>
24 #include <proton/container.hpp>
25 #include <proton/listen_handler.hpp>
26 #include <proton/listener.hpp>
27 #include <proton/message.hpp>
28 #include <proton/messaging_handler.hpp>
29 #include <proton/sasl.hpp>
30 #include <proton/ssl.hpp>
31 #include <proton/tracker.hpp>
32 #include <proton/transport.hpp>
33
34 #include <iostream>
35 #include <sstream>
36
37
38 using proton::connection_options;
39 using proton::ssl_client_options;
40 using proton::ssl_server_options;
41 using proton::ssl_certificate;
42 using proton::sasl;
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 static std::string cert_directory;
49 static std::string find_CN(const std::string &);
50
51
52 struct server_handler : public proton::messaging_handler {
53 proton::listener listener;
54
55 void on_connection_open(proton::connection &c) override {
56 std::cout << "Inbound server connection connected via SSL. Protocol: " <<
57 c.transport().ssl().protocol() << std::endl;
58 if (c.transport().sasl().outcome() == sasl::OK) {
59 std::string subject = c.transport().ssl().remote_subject();
60 std::cout << "Inbound client certificate identity " << find_CN(subject) << std::endl;
61 }
62 else {
63 std::cout << "Inbound client authentication failed" <<std::endl;
64 c.close();
65 }
66 listener.stop();
67
68 // Go and do default inbound open stuff too
69 messaging_handler::on_connection_open(c);
70 }
71
72 void on_message(proton::delivery &, proton::message &m) override {
73 std::cout << m.body() << std::endl;
74 }
75 };
76
77
78 class hello_world_direct : public proton::messaging_handler {
79 private:
80 class listener_open_handler : public proton::listen_handler {
81 void on_open(proton::listener& l) override {
82 std::ostringstream url;
83 url << "//:" << l.port() << "/example"; // Connect to the actual port
84 l.container().open_sender(url.str());
85 }
86 };
87
88 listener_open_handler listen_handler;
89 server_handler s_handler;
90
91 public:
92
93 void on_container_start(proton::container &c) override {
94 // Configure listener. Details vary by platform.
95 ssl_certificate server_cert = platform_certificate("tserver", "tserverpw");
96 std::string client_CA = platform_CA("tclient");
97 // Specify an SSL domain with CA's for client certificate verification.
98 ssl_server_options srv_ssl(server_cert, client_CA);
99 connection_options server_opts;
100 server_opts.ssl_server_options(srv_ssl).handler(s_handler);
101 server_opts.sasl_allowed_mechs("EXTERNAL");
102 c.server_connection_options(server_opts);
103
104 // Configure client.
105 ssl_certificate client_cert = platform_certificate("tclient", "tclientpw");
106 std::string server_CA = platform_CA("tserver");
107 // Since the test certificate's credentials are unlikely to match this host's name, downgrade the verification
108 // from VERIFY_PEER_NAME to VERIFY_PEER.
109 ssl_client_options ssl_cli(client_cert, server_CA, proton::ssl::VERIFY_PEER);
110 connection_options client_opts;
111 client_opts.ssl_client_options(ssl_cli).sasl_allowed_mechs("EXTERNAL");
112 c.client_connection_options(client_opts);
113
114 s_handler.listener = c.listen("//:0", listen_handler);
115 }
116
117 void on_connection_open(proton::connection &c) override {
118 std::string subject = c.transport().ssl().remote_subject();
119 std::cout << "Outgoing client connection connected via SSL. Server certificate identity " <<
120 find_CN(subject) << std::endl;
121 }
122
123 void on_sendable(proton::sender &s) override {
124 proton::message m;
125 m.body("Hello World!");
126 s.send(m);
127 s.close();
128 }
129
130 void on_tracker_accept(proton::tracker &t) override {
131 // All done.
132 t.connection().close();
133 }
134 };
135
136 int main(int argc, char **argv) {
137 try {
138 // Location of certificates and private key information:
139 if (argc > 1) {
140 cert_directory = argv[1];
141 size_t sz = cert_directory.size();
142 if (sz && cert_directory[sz -1] != '/')
143 cert_directory.append("/");
144 } else {
145 cert_directory = "ssl-certs/";
146 }
147 hello_world_direct hwd;
148 proton::container(hwd).run();
149 return 0;
150 } catch (const std::exception& e) {
151 std::cerr << e.what() << std::endl;
152 }
153 return 1;
154 }
155
156
157 bool using_OpenSSL() {
158 // Current defaults.
159 #if defined(_WIN32)
160 return false;
161 #else
162 return true;
163 #endif
164 }
165
166 ssl_certificate platform_certificate(const std::string &base_name, const std::string &passwd) {
167 if (using_OpenSSL()) {
168 // The first argument will be the name of the file containing the public certificate, the
169 // second argument will be the name of the file containing the private key.
170 return ssl_certificate(cert_directory + base_name + "-certificate.pem",
171 cert_directory + base_name + "-private-key.pem", passwd);
172 }
173 else {
174 // Windows SChannel
175 // The first argument will be the database or store that contains one or more complete certificates
176 // (public and private data). The second will be an optional name of the certificate in the store
177 // (not used in this example with one certificate per store).
178 return ssl_certificate(cert_directory + base_name + "-full.p12", "", passwd);
179 }
180 }
181
182 std::string platform_CA(const std::string &base_name) {
183 if (using_OpenSSL()) {
184 // In this simple example with self-signed certificates, the peer's certificate is the CA database.
185 return cert_directory + base_name + "-certificate.pem";
186 }
187 else {
188 // Windows SChannel. Use a pkcs#12 file with just the peer's public certificate information.
189 return cert_directory + base_name + "-certificate.p12";
190 }
191 }
192
193 std::string find_CN(const std::string &subject) {
194 // The subject string is returned with different whitespace and component ordering between platforms.
195 // Here we just return the common name by searching for "CN=...." in the subject, knowing that
196 // the test certificates do not contain any escaped characters.
197 size_t pos = subject.find("CN=");
198 if (pos == std::string::npos) throw std::runtime_error("No common name in certificate subject");
199 std::string cn = subject.substr(pos);
200 pos = cn.find(',');
201 return pos == std::string::npos ? cn : cn.substr(0, pos);
202 }