| 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 "./pn_test_proactor.hpp" |
| 23 |
|
| 24 |
#include <proton/connection.h> |
| 25 |
#include <proton/condition.h> |
| 26 |
#include <proton/delivery.h> |
| 27 |
#include <proton/link.h> |
| 28 |
#include <proton/listener.h> |
| 29 |
#include <proton/netaddr.h> |
| 30 |
#include <proton/proactor.h> |
| 31 |
#include <proton/session.h> |
| 32 |
#include <proton/sasl.h> |
| 33 |
#include <proton/ssl.h> |
| 34 |
#include <proton/transport.h> |
| 35 |
|
| 36 |
#include <stdio.h> |
| 37 |
#include <stdlib.h> |
| 38 |
#include <string.h> |
| 39 |
|
| 40 |
typedef struct app_data_t { |
| 41 |
const char *amqp_address; |
| 42 |
const char *container_id; |
| 43 |
|
| 44 |
pn_ssl_domain_t *server_ssl_domain; |
| 45 |
|
| 46 |
bool connection_succeeded; |
| 47 |
bool transport_error; |
| 48 |
} app_data_t; |
| 49 |
|
| 50 |
/* Note must be run in the current directory to find certificate files */ |
| 51 |
#define SSL_FILE(NAME) "ssl-certs/" NAME |
| 52 |
#define SSL_PW(NAME) NAME "pw" |
| 53 |
/* Windows vs. OpenSSL certificates */ |
| 54 |
#if defined(_WIN32) |
| 55 |
# define CERTIFICATE(NAME) SSL_FILE(NAME "-certificate.p12") |
| 56 |
# define SET_CREDENTIALS(DOMAIN, NAME) \ |
| 57 |
pn_ssl_domain_set_credentials(DOMAIN, SSL_FILE(NAME "-full.p12"), "", SSL_PW(NAME)) |
| 58 |
#else |
| 59 |
# define CERTIFICATE(NAME) SSL_FILE(NAME "-certificate.pem") |
| 60 |
# define SET_CREDENTIALS(DOMAIN, NAME) \ |
| 61 |
pn_ssl_domain_set_credentials(DOMAIN, CERTIFICATE(NAME), SSL_FILE(NAME "-private-key.pem"), SSL_PW(NAME)) |
| 62 |
#endif |
| 63 |
|
| 64 |
|
| 65 |
/* Returns true to continue, false if finished */ |
| 66 |
static bool server_handler(app_data_t* app, pn_event_t* event) { |
| 67 |
pn_listener_t *l = pn_event_listener(event); |
| 68 |
switch (pn_event_type(event)) { |
| 69 |
|
| 70 |
// Server side |
| 71 |
case PN_LISTENER_ACCEPT: { |
| 72 |
/* Configure a transport to allow SSL and SASL connections. See ssl_domain setup in main() */ |
| 73 |
pn_transport_t *t = pn_transport(); |
| 74 |
pn_transport_set_server(t); /* Must call before pn_sasl() */ |
| 75 |
pn_sasl_allowed_mechs(pn_sasl(t), "ANONYMOUS"); |
| 76 |
pn_ssl_init(pn_ssl(t), app->server_ssl_domain, NULL); |
| 77 |
pn_listener_accept2(l, NULL, t); |
| 78 |
|
| 79 |
/* Accept only one connection */ |
| 80 |
pn_listener_close(l); |
| 81 |
break; |
| 82 |
} |
| 83 |
|
| 84 |
case PN_TRANSPORT_CLOSED: |
| 85 |
break; |
| 86 |
|
| 87 |
default: break; |
| 88 |
} |
| 89 |
return true; |
| 90 |
} |
| 91 |
|
| 92 |
static bool client_handler(app_data_t* app, pn_event_t* event) { |
| 93 |
switch (pn_event_type(event)) { |
| 94 |
// Client side |
| 95 |
case PN_CONNECTION_INIT: { |
| 96 |
pn_connection_t* c = pn_event_connection(event); |
| 97 |
pn_session_t* s = pn_session(pn_event_connection(event)); |
| 98 |
pn_connection_set_container(c, app->container_id); |
| 99 |
pn_connection_open(c); |
| 100 |
pn_session_open(s); |
| 101 |
{ |
| 102 |
pn_link_t* l = pn_sender(s, "my_sender"); |
| 103 |
pn_terminus_set_address(pn_link_target(l), app->amqp_address); |
| 104 |
pn_link_open(l); |
| 105 |
break; |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
case PN_CONNECTION_BOUND: { |
| 110 |
break; |
| 111 |
} |
| 112 |
|
| 113 |
case PN_CONNECTION_REMOTE_OPEN: |
| 114 |
app->connection_succeeded = true; |
| 115 |
pn_connection_close(pn_event_connection(event)); |
| 116 |
break; |
| 117 |
|
| 118 |
case PN_TRANSPORT_ERROR: |
| 119 |
app->transport_error = true; |
| 120 |
break; |
| 121 |
|
| 122 |
case PN_CONNECTION_REMOTE_CLOSE: |
| 123 |
pn_connection_close(pn_event_connection(event)); |
| 124 |
break; |
| 125 |
|
| 126 |
case PN_SESSION_REMOTE_CLOSE: |
| 127 |
pn_connection_close(pn_event_connection(event)); |
| 128 |
break; |
| 129 |
|
| 130 |
case PN_LINK_REMOTE_CLOSE: |
| 131 |
case PN_LINK_REMOTE_DETACH: |
| 132 |
pn_connection_close(pn_event_connection(event)); |
| 133 |
break; |
| 134 |
|
| 135 |
default: break; |
| 136 |
} |
| 137 |
return true; |
| 138 |
} |
| 139 |
|
| 140 |
typedef bool handler_t(app_data_t* app, pn_event_t* event); |
| 141 |
void run(pn_proactor_t *p, app_data_t *app, handler_t *shandler, handler_t *chandler) { |
| 142 |
/* Loop and handle server/client events */ |
| 143 |
do { |
| 144 |
pn_event_batch_t *events = pn_proactor_wait(p); |
| 145 |
pn_event_t *e; |
| 146 |
for (e = pn_event_batch_next(events); e; e = pn_event_batch_next(events)) { |
| 147 |
if (pn_event_type(e)==PN_PROACTOR_INACTIVE) { |
| 148 |
return; |
| 149 |
} |
| 150 |
|
| 151 |
if (pn_event_listener(e)) { |
| 152 |
if (!shandler(app, e)) { |
| 153 |
return; |
| 154 |
} |
| 155 |
} else { |
| 156 |
if (!chandler(app, e)) { |
| 157 |
return; |
| 158 |
} |
| 159 |
} |
| 160 |
} |
| 161 |
pn_proactor_done(p, events); |
| 162 |
} while(true); |
| 163 |
} |
| 164 |
|
| 165 |
static void run_until_listener_open(pn_proactor_t* p) { |
| 166 |
do { |
| 167 |
pn_event_batch_t *events = pn_proactor_wait(p); |
| 168 |
pn_event_t *e; |
| 169 |
for (e = pn_event_batch_next(events); e; e = pn_event_batch_next(events)) { |
| 170 |
if (pn_event_type(e)==PN_LISTENER_OPEN) { |
| 171 |
pn_proactor_done(p, events); |
| 172 |
return; |
| 173 |
} |
| 174 |
} |
| 175 |
pn_proactor_done(p, events); |
| 176 |
} while (true); |
| 177 |
} |
| 178 |
|
| 179 |
static void setup_connection(pn_proactor_t* proactor, pn_transport_t* t) { |
| 180 |
pn_listener_t* l = pn_listener(); |
| 181 |
pn_proactor_listen(proactor, l, ":0", 16); |
| 182 |
// Don't know port until the listener is open |
| 183 |
run_until_listener_open(proactor); |
| 184 |
|
| 185 |
// Construct connect address (from listener port) |
| 186 |
const pn_netaddr_t* a = pn_listener_addr(l); |
| 187 |
char port[32]; |
| 188 |
port[0] = ':'; |
| 189 |
pn_netaddr_host_port(a, NULL, 0, port+1, 31); |
| 190 |
|
| 191 |
INFO("Connecting to " << port); |
| 192 |
pn_proactor_connect2(proactor, NULL, t, port); |
| 193 |
} |
| 194 |
|
| 195 |
TEST_CASE("ssl certificate verification tests") { |
| 196 |
struct app_data_t app = {0}; |
| 197 |
|
| 198 |
app.container_id = "ssl-test"; |
| 199 |
app.amqp_address = "fubar"; |
| 200 |
|
| 201 |
pn_test::auto_free<pn_proactor_t, pn_proactor_free> proactor(pn_proactor()); |
| 202 |
|
| 203 |
/* Configure server for default SSL */ |
| 204 |
pn_test::auto_free<pn_ssl_domain_t, pn_ssl_domain_free> |
| 205 |
sd(pn_ssl_domain(PN_SSL_MODE_SERVER)); |
| 206 |
app.server_ssl_domain = sd; |
| 207 |
|
| 208 |
/* Configure a client for SSL */ |
| 209 |
pn_transport_t *t = pn_transport(); |
| 210 |
pn_test::auto_free<pn_ssl_domain_t, pn_ssl_domain_free> |
| 211 |
cd(pn_ssl_domain(PN_SSL_MODE_CLIENT)); |
| 212 |
|
| 213 |
SECTION("Default connections don't verify to self signed server (even with correct name)") { |
| 214 |
REQUIRE(SET_CREDENTIALS(sd, "tserver") == 0); |
| 215 |
REQUIRE(pn_ssl_init(pn_ssl(t), NULL, NULL) == 0); |
| 216 |
REQUIRE(pn_ssl_set_peer_hostname(pn_ssl(t), "test_server") == 0); |
| 217 |
|
| 218 |
setup_connection(proactor, t); |
| 219 |
|
| 220 |
run(proactor, &app, server_handler, client_handler); |
| 221 |
CHECK(app.connection_succeeded==false); |
| 222 |
CHECK(app.transport_error==true); |
| 223 |
} |
| 224 |
|
| 225 |
SECTION("Connections noname verify to self signed cert if cert allowed (even with no name)") { |
| 226 |
REQUIRE(SET_CREDENTIALS(sd, "tserver") == 0); |
| 227 |
REQUIRE(pn_ssl_domain_set_trusted_ca_db(cd, CERTIFICATE("tserver")) == 0); |
| 228 |
REQUIRE(pn_ssl_domain_set_peer_authentication(cd, PN_SSL_VERIFY_PEER, NULL) == 0); |
| 229 |
REQUIRE(pn_ssl_init(pn_ssl(t), cd, NULL) == 0); |
| 230 |
|
| 231 |
setup_connection(proactor, t); |
| 232 |
|
| 233 |
run(proactor, &app, server_handler, client_handler); |
| 234 |
CHECK(app.connection_succeeded==true); |
| 235 |
CHECK(app.transport_error==false); |
| 236 |
} |
| 237 |
|
| 238 |
SECTION("Connections don't noname verify to self signed cert without cert allowed") { |
| 239 |
REQUIRE(SET_CREDENTIALS(sd, "tserver") == 0); |
| 240 |
REQUIRE(pn_ssl_domain_set_peer_authentication(cd, PN_SSL_VERIFY_PEER, NULL) == 0); |
| 241 |
REQUIRE(pn_ssl_init(pn_ssl(t), cd, NULL) == 0); |
| 242 |
|
| 243 |
setup_connection(proactor, t); |
| 244 |
|
| 245 |
run(proactor, &app, server_handler, client_handler); |
| 246 |
CHECK(app.connection_succeeded==false); |
| 247 |
CHECK(app.transport_error==true); |
| 248 |
} |
| 249 |
|
| 250 |
SECTION("Connections verify with self signed server if cert allowed") { |
| 251 |
REQUIRE(SET_CREDENTIALS(sd, "tserver") == 0); |
| 252 |
REQUIRE(pn_ssl_domain_set_trusted_ca_db(cd, CERTIFICATE("tserver")) == 0); |
| 253 |
REQUIRE(pn_ssl_domain_set_peer_authentication(cd, PN_SSL_VERIFY_PEER_NAME, NULL) == 0); |
| 254 |
REQUIRE(pn_ssl_init(pn_ssl(t), cd, NULL) == 0); |
| 255 |
REQUIRE(pn_ssl_set_peer_hostname(pn_ssl(t), "test_server") == 0); |
| 256 |
|
| 257 |
setup_connection(proactor, t); |
| 258 |
|
| 259 |
run(proactor, &app, server_handler, client_handler); |
| 260 |
CHECK(app.connection_succeeded==true); |
| 261 |
CHECK(app.transport_error==false); |
| 262 |
} |
| 263 |
|
| 264 |
SECTION("Anonymous server connections don't verify") { |
| 265 |
REQUIRE(pn_ssl_domain_set_trusted_ca_db(cd, CERTIFICATE("tserver")) == 0); |
| 266 |
REQUIRE(pn_ssl_domain_set_peer_authentication(cd, PN_SSL_VERIFY_PEER_NAME, NULL) == 0); |
| 267 |
REQUIRE(pn_ssl_init(pn_ssl(t), cd, NULL) == 0); |
| 268 |
|
| 269 |
setup_connection(proactor, t); |
| 270 |
|
| 271 |
run(proactor, &app, server_handler, client_handler); |
| 272 |
CHECK(app.connection_succeeded==false); |
| 273 |
CHECK(app.transport_error==true); |
| 274 |
} |
| 275 |
|
| 276 |
SECTION("Anonymous connections connect if anonymous allowed") { |
| 277 |
#ifndef _WIN32 |
| 278 |
REQUIRE(pn_ssl_domain_set_peer_authentication(cd, PN_SSL_ANONYMOUS_PEER, NULL) == 0); |
| 279 |
REQUIRE(pn_ssl_init(pn_ssl(t), cd, NULL) == 0); |
| 280 |
|
| 281 |
setup_connection(proactor, t); |
| 282 |
|
| 283 |
run(proactor, &app, server_handler, client_handler); |
| 284 |
CHECK(app.connection_succeeded==true); |
| 285 |
CHECK(app.transport_error==false); |
| 286 |
#else |
| 287 |
SUCCEED("Skipped: Windows schannel does not support anonymous connections"); |
| 288 |
#endif |
| 289 |
} |
| 290 |
|
| 291 |
SECTION("Default server (anonymous) doesn't verify against default client (verify peername)") { |
| 292 |
app.server_ssl_domain = 0; |
| 293 |
REQUIRE(pn_ssl_init(pn_ssl(t), NULL, NULL) == 0); |
| 294 |
|
| 295 |
setup_connection(proactor, t); |
| 296 |
|
| 297 |
run(proactor, &app, server_handler, client_handler); |
| 298 |
CHECK(app.connection_succeeded==false); |
| 299 |
CHECK(app.transport_error==true); |
| 300 |
} |
| 301 |
|
| 302 |
SECTION("Default server (anonymous) connects if anonymous allowed") { |
| 303 |
#ifndef _WIN32 |
| 304 |
app.server_ssl_domain = 0; |
| 305 |
REQUIRE(pn_ssl_domain_set_peer_authentication(cd, PN_SSL_ANONYMOUS_PEER, NULL) == 0); |
| 306 |
REQUIRE(pn_ssl_init(pn_ssl(t), cd, NULL) == 0); |
| 307 |
|
| 308 |
setup_connection(proactor, t); |
| 309 |
|
| 310 |
run(proactor, &app, server_handler, client_handler); |
| 311 |
CHECK(app.connection_succeeded==true); |
| 312 |
CHECK(app.transport_error==false); |
| 313 |
#else |
| 314 |
SUCCEED("Skipped: Windows schannel does not support anonymous connections"); |
| 315 |
#endif |
| 316 |
} |
| 317 |
} |