2023-06-05 01:16:05 +02:00
|
|
|
/* SPDX-License-Identifier: MPL-2.0 */
|
2017-08-17 11:45:18 +02:00
|
|
|
|
|
|
|
#ifndef __TESTUTIL_SECURITY_HPP_INCLUDED__
|
|
|
|
#define __TESTUTIL_SECURITY_HPP_INCLUDED__
|
|
|
|
|
2019-03-20 16:03:06 +01:00
|
|
|
#include "testutil_unity.hpp"
|
2019-02-02 11:32:27 +01:00
|
|
|
#include "testutil_monitoring.hpp"
|
2017-08-17 11:45:18 +02:00
|
|
|
|
|
|
|
// security test utils
|
|
|
|
|
2021-09-21 20:11:11 +02:00
|
|
|
typedef void (socket_config_fn) (void *, void *);
|
2017-08-17 11:45:18 +02:00
|
|
|
|
2017-08-17 12:11:14 +02:00
|
|
|
// NULL specific functions
|
2019-03-23 11:27:12 +01:00
|
|
|
void socket_config_null_client (void *server_, void *server_secret_);
|
2017-08-17 12:11:14 +02:00
|
|
|
|
2019-03-23 11:27:12 +01:00
|
|
|
void socket_config_null_server (void *server_, void *server_secret_);
|
2017-08-17 12:11:14 +02:00
|
|
|
|
2017-08-17 12:22:26 +02:00
|
|
|
// PLAIN specific functions
|
2019-03-23 11:27:12 +01:00
|
|
|
void socket_config_plain_client (void *server_, void *server_secret_);
|
2017-08-19 13:18:51 +02:00
|
|
|
|
2019-03-23 11:27:12 +01:00
|
|
|
void socket_config_plain_server (void *server_, void *server_secret_);
|
2017-08-17 12:22:26 +02:00
|
|
|
|
2017-08-17 11:45:18 +02:00
|
|
|
// CURVE specific functions
|
|
|
|
|
|
|
|
// We'll generate random test keys at startup
|
2019-03-23 11:27:12 +01:00
|
|
|
extern char valid_client_public[41];
|
|
|
|
extern char valid_client_secret[41];
|
|
|
|
extern char valid_server_public[41];
|
|
|
|
extern char valid_server_secret[41];
|
2017-08-17 11:45:18 +02:00
|
|
|
|
2019-03-23 11:27:12 +01:00
|
|
|
void setup_testutil_security_curve ();
|
|
|
|
|
|
|
|
void socket_config_curve_server (void *server_, void *server_secret_);
|
2017-08-17 11:45:18 +02:00
|
|
|
|
|
|
|
struct curve_client_data_t
|
|
|
|
{
|
|
|
|
const char *server_public;
|
|
|
|
const char *client_public;
|
|
|
|
const char *client_secret;
|
|
|
|
};
|
|
|
|
|
2019-03-23 11:27:12 +01:00
|
|
|
void socket_config_curve_client (void *client_, void *data_);
|
2017-08-17 11:45:18 +02:00
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
// This methods receives and validates ZAP requests (allowing or denying
|
|
|
|
// each client connection).
|
|
|
|
|
|
|
|
enum zap_protocol_t
|
|
|
|
{
|
2017-09-18 11:48:14 +02:00
|
|
|
zap_ok,
|
|
|
|
// ZAP-compliant non-standard cases
|
|
|
|
zap_status_temporary_failure,
|
|
|
|
zap_status_internal_error,
|
|
|
|
// ZAP protocol errors
|
|
|
|
zap_wrong_version,
|
|
|
|
zap_wrong_request_id,
|
|
|
|
zap_status_invalid,
|
2017-09-18 15:11:51 +02:00
|
|
|
zap_too_many_parts,
|
2017-09-18 17:19:36 +02:00
|
|
|
zap_disconnect,
|
|
|
|
zap_do_not_recv,
|
|
|
|
zap_do_not_send
|
2017-08-17 11:45:18 +02:00
|
|
|
};
|
|
|
|
|
2019-03-23 11:27:12 +01:00
|
|
|
extern void *zap_requests_handled;
|
2017-08-17 11:45:18 +02:00
|
|
|
|
2019-03-20 16:03:06 +01:00
|
|
|
void zap_handler_generic (zap_protocol_t zap_protocol_,
|
2019-03-23 11:27:12 +01:00
|
|
|
const char *expected_routing_id_ = "IDENT");
|
2017-08-17 11:45:18 +02:00
|
|
|
|
2019-03-23 11:27:12 +01:00
|
|
|
void zap_handler (void * /*unused_*/);
|
2017-08-17 11:45:18 +02:00
|
|
|
|
2019-02-02 11:32:27 +01:00
|
|
|
// Security-specific monitor event utilities
|
2017-08-18 13:19:07 +02:00
|
|
|
|
|
|
|
// assert_* are macros rather than functions, to allow assertion failures be
|
|
|
|
// attributed to the causing source code line
|
2019-03-20 17:03:26 +01:00
|
|
|
#define assert_no_more_monitor_events_with_timeout(monitor, timeout) \
|
|
|
|
{ \
|
|
|
|
int event_count = 0; \
|
|
|
|
int event, err; \
|
|
|
|
while ((event = get_monitor_event_with_timeout ((monitor), &err, NULL, \
|
|
|
|
(timeout))) \
|
|
|
|
!= -1) { \
|
|
|
|
if (event == ZMQ_EVENT_HANDSHAKE_FAILED_NO_DETAIL \
|
|
|
|
&& (err == EPIPE || err == ECONNRESET \
|
|
|
|
|| err == ECONNABORTED)) { \
|
|
|
|
fprintf (stderr, \
|
|
|
|
"Ignored event (skipping any further events): %x " \
|
|
|
|
"(err = %i == %s)\n", \
|
|
|
|
event, err, zmq_strerror (err)); \
|
|
|
|
continue; \
|
|
|
|
} \
|
|
|
|
++event_count; \
|
|
|
|
/* TODO write this into a buffer and attach to the assertion msg below */ \
|
|
|
|
print_unexpected_event_stderr (event, err, 0, 0); \
|
|
|
|
} \
|
|
|
|
TEST_ASSERT_EQUAL_INT (0, event_count); \
|
2017-08-18 13:19:07 +02:00
|
|
|
}
|
|
|
|
|
2017-09-19 11:05:46 +02:00
|
|
|
void setup_context_and_server_side (
|
2018-05-27 13:01:36 +02:00
|
|
|
void **zap_control_,
|
|
|
|
void **zap_thread_,
|
|
|
|
void **server_,
|
|
|
|
void **server_mon_,
|
|
|
|
char *my_endpoint_,
|
2017-09-19 11:05:46 +02:00
|
|
|
zmq_thread_fn zap_handler_ = &zap_handler,
|
|
|
|
socket_config_fn socket_config_ = &socket_config_curve_server,
|
|
|
|
void *socket_config_data_ = valid_server_secret,
|
2019-03-23 11:27:12 +01:00
|
|
|
const char *routing_id_ = "IDENT");
|
2017-09-19 11:05:46 +02:00
|
|
|
|
2019-03-20 16:03:06 +01:00
|
|
|
void shutdown_context_and_server_side (void *zap_thread_,
|
2018-05-27 13:01:36 +02:00
|
|
|
void *server_,
|
|
|
|
void *server_mon_,
|
|
|
|
void *zap_control_,
|
2019-03-23 11:27:12 +01:00
|
|
|
bool zap_handler_stopped_ = false);
|
2017-09-19 11:05:46 +02:00
|
|
|
|
2019-03-20 16:03:06 +01:00
|
|
|
void *create_and_connect_client (char *my_endpoint_,
|
2017-09-19 11:05:46 +02:00
|
|
|
socket_config_fn socket_config_,
|
|
|
|
void *socket_config_data_,
|
2019-03-23 11:27:12 +01:00
|
|
|
void **client_mon_ = NULL);
|
2017-09-19 11:05:46 +02:00
|
|
|
|
2019-03-20 16:03:06 +01:00
|
|
|
void expect_new_client_bounce_fail (char *my_endpoint_,
|
2018-05-27 13:01:36 +02:00
|
|
|
void *server_,
|
2017-09-19 11:05:46 +02:00
|
|
|
socket_config_fn socket_config_,
|
|
|
|
void *socket_config_data_,
|
2018-05-27 13:01:36 +02:00
|
|
|
void **client_mon_ = NULL,
|
|
|
|
int expected_client_event_ = 0,
|
2019-03-23 11:27:12 +01:00
|
|
|
int expected_client_value_ = 0);
|
2017-09-19 11:05:46 +02:00
|
|
|
|
2017-08-17 11:45:18 +02:00
|
|
|
#endif
|