2
Copyright (c) 2007-2014 Contributors as noted in the AUTHORS file
4
This file is part of 0MQ.
6
0MQ is free software; you can redistribute it and/or modify it under
7
the terms of the GNU Lesser General Public License as published by
8
the Free Software Foundation; either version 3 of the License, or
9
(at your option) any later version.
11
0MQ is distributed in the hope that it will be useful,
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
GNU Lesser General Public License for more details.
16
You should have received a copy of the GNU Lesser General Public License
17
along with this program. If not, see <http://www.gnu.org/licenses/>.
20
#include "testutil.hpp"
22
// We'll generate random test keys at startup
23
static char client_public [40];
24
static char client_secret [40];
25
static char server_public [40];
26
static char server_secret [40];
28
// --------------------------------------------------------------------------
29
// This methods receives and validates ZAP requestes (allowing or denying
30
// each client connection).
32
static void zap_handler (void *handler)
34
// Process ZAP requests forever
36
char *version = s_recv (handler);
40
char *sequence = s_recv (handler);
41
char *domain = s_recv (handler);
42
char *address = s_recv (handler);
43
char *identity = s_recv (handler);
44
char *mechanism = s_recv (handler);
45
uint8_t client_key [32];
46
int size = zmq_recv (handler, client_key, 32, 0);
49
char client_key_text [40];
50
zmq_z85_encode (client_key_text, client_key, 32);
52
assert (streq (version, "1.0"));
53
assert (streq (mechanism, "CURVE"));
54
assert (streq (identity, "IDENT"));
56
s_sendmore (handler, version);
57
s_sendmore (handler, sequence);
59
if (streq (client_key_text, client_public)) {
60
s_sendmore (handler, "200");
61
s_sendmore (handler, "OK");
62
s_sendmore (handler, "anonymous");
66
s_sendmore (handler, "400");
67
s_sendmore (handler, "Invalid client public key");
68
s_sendmore (handler, "");
84
#ifndef HAVE_LIBSODIUM
85
printf ("libsodium not installed, skipping CURVE test\n");
89
// Generate new keypairs for this test
90
int rc = zmq_curve_keypair (client_public, client_secret);
92
rc = zmq_curve_keypair (server_public, server_secret);
95
setup_test_environment ();
96
void *ctx = zmq_ctx_new ();
100
// We create and bind ZAP socket in main thread to avoid case
101
// where child thread does not start up fast enough.
102
void *handler = zmq_socket (ctx, ZMQ_REP);
104
rc = zmq_bind (handler, "inproc://zeromq.zap.01");
106
void *zap_thread = zmq_threadstart (&zap_handler, handler);
108
// Server socket will accept connections
109
void *server = zmq_socket (ctx, ZMQ_DEALER);
112
rc = zmq_setsockopt (server, ZMQ_CURVE_SERVER, &as_server, sizeof (int));
114
rc = zmq_setsockopt (server, ZMQ_CURVE_SECRETKEY, server_secret, 40);
116
rc = zmq_setsockopt (server, ZMQ_IDENTITY, "IDENT", 6);
118
rc = zmq_bind (server, "tcp://127.0.0.1:9998");
121
// Check CURVE security with valid credentials
122
void *client = zmq_socket (ctx, ZMQ_DEALER);
124
rc = zmq_setsockopt (client, ZMQ_CURVE_SERVERKEY, server_public, 40);
126
rc = zmq_setsockopt (client, ZMQ_CURVE_PUBLICKEY, client_public, 40);
128
rc = zmq_setsockopt (client, ZMQ_CURVE_SECRETKEY, client_secret, 40);
130
rc = zmq_connect (client, "tcp://localhost:9998");
132
bounce (server, client);
133
rc = zmq_close (client);
136
// Check CURVE security with a garbage server key
137
// This will be caught by the curve_server class, not passed to ZAP
138
char garbage_key [] = "0000111122223333444455556666777788889999";
139
client = zmq_socket (ctx, ZMQ_DEALER);
141
rc = zmq_setsockopt (client, ZMQ_CURVE_SERVERKEY, garbage_key, 40);
143
rc = zmq_setsockopt (client, ZMQ_CURVE_PUBLICKEY, client_public, 40);
145
rc = zmq_setsockopt (client, ZMQ_CURVE_SECRETKEY, client_secret, 40);
147
rc = zmq_connect (client, "tcp://localhost:9998");
149
expect_bounce_fail (server, client);
150
close_zero_linger (client);
152
// Check CURVE security with a garbage client public key
153
// This will be caught by the curve_server class, not passed to ZAP
154
client = zmq_socket (ctx, ZMQ_DEALER);
156
rc = zmq_setsockopt (client, ZMQ_CURVE_SERVERKEY, server_public, 40);
158
rc = zmq_setsockopt (client, ZMQ_CURVE_PUBLICKEY, garbage_key, 40);
160
rc = zmq_setsockopt (client, ZMQ_CURVE_SECRETKEY, client_secret, 40);
162
rc = zmq_connect (client, "tcp://localhost:9998");
164
expect_bounce_fail (server, client);
165
close_zero_linger (client);
167
// Check CURVE security with a garbage client secret key
168
// This will be caught by the curve_server class, not passed to ZAP
169
client = zmq_socket (ctx, ZMQ_DEALER);
171
rc = zmq_setsockopt (client, ZMQ_CURVE_SERVERKEY, server_public, 40);
173
rc = zmq_setsockopt (client, ZMQ_CURVE_PUBLICKEY, client_public, 40);
175
rc = zmq_setsockopt (client, ZMQ_CURVE_SECRETKEY, garbage_key, 40);
177
rc = zmq_connect (client, "tcp://localhost:9998");
179
expect_bounce_fail (server, client);
180
close_zero_linger (client);
182
// Check CURVE security with bogus client credentials
183
// This must be caught by the ZAP handler
184
char bogus_public [40];
185
char bogus_secret [40];
186
zmq_curve_keypair (bogus_public, bogus_secret);
188
client = zmq_socket (ctx, ZMQ_DEALER);
190
rc = zmq_setsockopt (client, ZMQ_CURVE_SERVERKEY, server_public, 40);
192
rc = zmq_setsockopt (client, ZMQ_CURVE_PUBLICKEY, bogus_public, 40);
194
rc = zmq_setsockopt (client, ZMQ_CURVE_SECRETKEY, bogus_secret, 40);
196
rc = zmq_connect (client, "tcp://localhost:9998");
198
expect_bounce_fail (server, client);
199
close_zero_linger (client);
201
// Check CURVE security with NULL client credentials
202
// This must be caught by the curve_server class, not passed to ZAP
203
client = zmq_socket (ctx, ZMQ_DEALER);
205
rc = zmq_connect (client, "tcp://localhost:9998");
207
expect_bounce_fail (server, client);
208
close_zero_linger (client);
210
// Check CURVE security with PLAIN client credentials
211
// This must be caught by the curve_server class, not passed to ZAP
212
client = zmq_socket (ctx, ZMQ_DEALER);
214
rc = zmq_setsockopt (client, ZMQ_PLAIN_USERNAME, "admin", 5);
216
rc = zmq_setsockopt (client, ZMQ_PLAIN_PASSWORD, "password", 8);
218
expect_bounce_fail (server, client);
219
close_zero_linger (client);
222
rc = zmq_close (server);
224
rc = zmq_ctx_term (ctx);
227
// Wait until ZAP handler terminates
228
zmq_threadclose (zap_thread);