~ubuntu-branches/ubuntu/wily/zeromq3/wily-proposed

« back to all changes in this revision

Viewing changes to .pc/protocol-downgrade-attack.patch/tests/test_security_curve.cpp

  • Committer: Package Import Robot
  • Author(s): Laszlo Boszormenyi (GCS)
  • Date: 2015-05-05 21:06:02 UTC
  • Revision ID: package-import@ubuntu.com-20150505210602-7nmw5z31bei3puj6
Tags: 4.0.5+dfsg-3
V3 protocol handler vulnerable to downgrade attacks, use upstream
backported fix for this issue (closes: #784366).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    Copyright (c) 2007-2014 Contributors as noted in the AUTHORS file
 
3
 
 
4
    This file is part of 0MQ.
 
5
 
 
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.
 
10
 
 
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.
 
15
 
 
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/>.
 
18
*/
 
19
 
 
20
#include "testutil.hpp"
 
21
 
 
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];
 
27
 
 
28
//  --------------------------------------------------------------------------
 
29
//  This methods receives and validates ZAP requestes (allowing or denying
 
30
//  each client connection).
 
31
 
 
32
static void zap_handler (void *handler)
 
33
{
 
34
    //  Process ZAP requests forever
 
35
    while (true) {
 
36
        char *version = s_recv (handler);
 
37
        if (!version)
 
38
            break;          //  Terminating
 
39
 
 
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);
 
47
        assert (size == 32);
 
48
 
 
49
        char client_key_text [40];
 
50
        zmq_z85_encode (client_key_text, client_key, 32);
 
51
 
 
52
        assert (streq (version, "1.0"));
 
53
        assert (streq (mechanism, "CURVE"));
 
54
        assert (streq (identity, "IDENT"));
 
55
 
 
56
        s_sendmore (handler, version);
 
57
        s_sendmore (handler, sequence);
 
58
 
 
59
        if (streq (client_key_text, client_public)) {
 
60
            s_sendmore (handler, "200");
 
61
            s_sendmore (handler, "OK");
 
62
            s_sendmore (handler, "anonymous");
 
63
            s_send     (handler, "");
 
64
        }
 
65
        else {
 
66
            s_sendmore (handler, "400");
 
67
            s_sendmore (handler, "Invalid client public key");
 
68
            s_sendmore (handler, "");
 
69
            s_send     (handler, "");
 
70
        }
 
71
        free (version);
 
72
        free (sequence);
 
73
        free (domain);
 
74
        free (address);
 
75
        free (identity);
 
76
        free (mechanism);
 
77
    }
 
78
    zmq_close (handler);
 
79
}
 
80
 
 
81
 
 
82
int main (void)
 
83
{
 
84
#ifndef HAVE_LIBSODIUM
 
85
    printf ("libsodium not installed, skipping CURVE test\n");
 
86
    return 0;
 
87
#endif
 
88
 
 
89
    //  Generate new keypairs for this test
 
90
    int rc = zmq_curve_keypair (client_public, client_secret);
 
91
    assert (rc == 0);
 
92
    rc = zmq_curve_keypair (server_public, server_secret);
 
93
    assert (rc == 0);
 
94
 
 
95
    setup_test_environment ();
 
96
    void *ctx = zmq_ctx_new ();
 
97
    assert (ctx);
 
98
 
 
99
    //  Spawn ZAP handler
 
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);
 
103
    assert (handler);
 
104
    rc = zmq_bind (handler, "inproc://zeromq.zap.01");
 
105
    assert (rc == 0);
 
106
    void *zap_thread = zmq_threadstart (&zap_handler, handler);
 
107
 
 
108
    //  Server socket will accept connections
 
109
    void *server = zmq_socket (ctx, ZMQ_DEALER);
 
110
    assert (server);
 
111
    int as_server = 1;
 
112
    rc = zmq_setsockopt (server, ZMQ_CURVE_SERVER, &as_server, sizeof (int));
 
113
    assert (rc == 0);
 
114
    rc = zmq_setsockopt (server, ZMQ_CURVE_SECRETKEY, server_secret, 40);
 
115
    assert (rc == 0);
 
116
    rc = zmq_setsockopt (server, ZMQ_IDENTITY, "IDENT", 6);
 
117
    assert (rc == 0);
 
118
    rc = zmq_bind (server, "tcp://127.0.0.1:9998");
 
119
    assert (rc == 0);
 
120
 
 
121
    //  Check CURVE security with valid credentials
 
122
    void *client = zmq_socket (ctx, ZMQ_DEALER);
 
123
    assert (client);
 
124
    rc = zmq_setsockopt (client, ZMQ_CURVE_SERVERKEY, server_public, 40);
 
125
    assert (rc == 0);
 
126
    rc = zmq_setsockopt (client, ZMQ_CURVE_PUBLICKEY, client_public, 40);
 
127
    assert (rc == 0);
 
128
    rc = zmq_setsockopt (client, ZMQ_CURVE_SECRETKEY, client_secret, 40);
 
129
    assert (rc == 0);
 
130
    rc = zmq_connect (client, "tcp://localhost:9998");
 
131
    assert (rc == 0);
 
132
    bounce (server, client);
 
133
    rc = zmq_close (client);
 
134
    assert (rc == 0);
 
135
 
 
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);
 
140
    assert (client);
 
141
    rc = zmq_setsockopt (client, ZMQ_CURVE_SERVERKEY, garbage_key, 40);
 
142
    assert (rc == 0);
 
143
    rc = zmq_setsockopt (client, ZMQ_CURVE_PUBLICKEY, client_public, 40);
 
144
    assert (rc == 0);
 
145
    rc = zmq_setsockopt (client, ZMQ_CURVE_SECRETKEY, client_secret, 40);
 
146
    assert (rc == 0);
 
147
    rc = zmq_connect (client, "tcp://localhost:9998");
 
148
    assert (rc == 0);
 
149
    expect_bounce_fail (server, client);
 
150
    close_zero_linger (client);
 
151
 
 
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);
 
155
    assert (client);
 
156
    rc = zmq_setsockopt (client, ZMQ_CURVE_SERVERKEY, server_public, 40);
 
157
    assert (rc == 0);
 
158
    rc = zmq_setsockopt (client, ZMQ_CURVE_PUBLICKEY, garbage_key, 40);
 
159
    assert (rc == 0);
 
160
    rc = zmq_setsockopt (client, ZMQ_CURVE_SECRETKEY, client_secret, 40);
 
161
    assert (rc == 0);
 
162
    rc = zmq_connect (client, "tcp://localhost:9998");
 
163
    assert (rc == 0);
 
164
    expect_bounce_fail (server, client);
 
165
    close_zero_linger (client);
 
166
 
 
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);
 
170
    assert (client);
 
171
    rc = zmq_setsockopt (client, ZMQ_CURVE_SERVERKEY, server_public, 40);
 
172
    assert (rc == 0);
 
173
    rc = zmq_setsockopt (client, ZMQ_CURVE_PUBLICKEY, client_public, 40);
 
174
    assert (rc == 0);
 
175
    rc = zmq_setsockopt (client, ZMQ_CURVE_SECRETKEY, garbage_key, 40);
 
176
    assert (rc == 0);
 
177
    rc = zmq_connect (client, "tcp://localhost:9998");
 
178
    assert (rc == 0);
 
179
    expect_bounce_fail (server, client);
 
180
    close_zero_linger (client);
 
181
 
 
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);
 
187
 
 
188
    client = zmq_socket (ctx, ZMQ_DEALER);
 
189
    assert (client);
 
190
    rc = zmq_setsockopt (client, ZMQ_CURVE_SERVERKEY, server_public, 40);
 
191
    assert (rc == 0);
 
192
    rc = zmq_setsockopt (client, ZMQ_CURVE_PUBLICKEY, bogus_public, 40);
 
193
    assert (rc == 0);
 
194
    rc = zmq_setsockopt (client, ZMQ_CURVE_SECRETKEY, bogus_secret, 40);
 
195
    assert (rc == 0);
 
196
    rc = zmq_connect (client, "tcp://localhost:9998");
 
197
    assert (rc == 0);
 
198
    expect_bounce_fail (server, client);
 
199
    close_zero_linger (client);
 
200
 
 
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);
 
204
    assert (client);
 
205
    rc = zmq_connect (client, "tcp://localhost:9998");
 
206
    assert (rc == 0);
 
207
    expect_bounce_fail (server, client);
 
208
    close_zero_linger (client);
 
209
 
 
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);
 
213
    assert (client);
 
214
    rc = zmq_setsockopt (client, ZMQ_PLAIN_USERNAME, "admin", 5);
 
215
    assert (rc == 0);
 
216
    rc = zmq_setsockopt (client, ZMQ_PLAIN_PASSWORD, "password", 8);
 
217
    assert (rc == 0);
 
218
    expect_bounce_fail (server, client);
 
219
    close_zero_linger (client);
 
220
    
 
221
    //  Shutdown
 
222
    rc = zmq_close (server);
 
223
    assert (rc == 0);
 
224
    rc = zmq_ctx_term (ctx);
 
225
    assert (rc == 0);
 
226
 
 
227
    //  Wait until ZAP handler terminates
 
228
    zmq_threadclose (zap_thread);
 
229
 
 
230
    return 0;
 
231
}