curve_client.cpp 12 KB
Newer Older
1
/*
2
    Copyright (c) 2007-2014 Contributors as noted in the AUTHORS file
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37

    This file is part of 0MQ.

    0MQ is free software; you can redistribute it and/or modify it under
    the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.

    0MQ is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "platform.hpp"

#ifdef HAVE_LIBSODIUM

#ifdef ZMQ_HAVE_WINDOWS
#include "windows.hpp"
#endif

#include "msg.hpp"
#include "session_base.hpp"
#include "err.hpp"
#include "curve_client.hpp"
#include "wire.hpp"

zmq::curve_client_t::curve_client_t (const options_t &options_) :
    mechanism_t (options_),
    state (send_hello)
{
38 39 40
    memcpy (public_key, options_.curve_public_key, crypto_box_PUBLICKEYBYTES);
    memcpy (secret_key, options_.curve_secret_key, crypto_box_SECRETKEYBYTES);
    memcpy (server_key, options_.curve_server_key, crypto_box_PUBLICKEYBYTES);
41

42 43
    //  Generate short-term key pair
    const int rc = crypto_box_keypair (cn_public, cn_secret);
44 45 46 47 48 49 50
    zmq_assert (rc == 0);
}

zmq::curve_client_t::~curve_client_t ()
{
}

51
int zmq::curve_client_t::next_handshake_command (msg_t *msg_)
52 53 54 55 56
{
    int rc = 0;

    switch (state) {
        case send_hello:
57
            rc = produce_hello (msg_);
58 59 60 61
            if (rc == 0)
                state = expect_welcome;
            break;
        case send_initiate:
62
            rc = produce_initiate (msg_);
63 64 65 66 67 68 69 70 71 72
            if (rc == 0)
                state = expect_ready;
            break;
        default:
            errno = EAGAIN;
            rc = -1;
    }
    return rc;
}

73
int zmq::curve_client_t::process_handshake_command (msg_t *msg_)
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
{
    int rc = 0;

    switch (state) {
        case expect_welcome:
            rc = process_welcome (msg_);
            if (rc == 0)
                state = send_initiate;
            break;
        case expect_ready:
            rc = process_ready (msg_);
            if (rc == 0)
                state = connected;
            break;
        default:
89
            errno = EPROTO;
90
            rc = -1;
91
            break;
92 93 94 95 96 97 98 99 100 101
    }
    if (rc == 0) {
        rc = msg_->close ();
        errno_assert (rc == 0);
        rc = msg_->init ();
        errno_assert (rc == 0);
    }
    return rc;
}

102 103 104 105 106 107 108 109 110 111
int zmq::curve_client_t::encode (msg_t *msg_)
{
    zmq_assert (state == connected);

    uint8_t flags = 0;
    if (msg_->flags () & msg_t::more)
        flags |= 0x01;

    uint8_t message_nonce [crypto_box_NONCEBYTES];
    memcpy (message_nonce, "CurveZMQMESSAGEC", 16);
112
    memcpy (message_nonce + 16, &cn_nonce, 8);
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127

    const size_t mlen = crypto_box_ZEROBYTES + 1 + msg_->size ();

    uint8_t *message_plaintext = static_cast <uint8_t *> (malloc (mlen));
    alloc_assert (message_plaintext);

    memset (message_plaintext, 0, crypto_box_ZEROBYTES);
    message_plaintext [crypto_box_ZEROBYTES] = flags;
    memcpy (message_plaintext + crypto_box_ZEROBYTES + 1,
            msg_->data (), msg_->size ());

    uint8_t *message_box = static_cast <uint8_t *> (malloc (mlen));
    alloc_assert (message_box);

    int rc = crypto_box_afternm (message_box, message_plaintext,
128
                                 mlen, message_nonce, cn_precom);
129 130 131 132 133 134 135 136 137 138
    zmq_assert (rc == 0);

    rc = msg_->close ();
    zmq_assert (rc == 0);

    rc = msg_->init_size (16 + mlen - crypto_box_BOXZEROBYTES);
    zmq_assert (rc == 0);

    uint8_t *message = static_cast <uint8_t *> (msg_->data ());

139
    memcpy (message, "\x07MESSAGE", 8);
140
    memcpy (message + 8, &cn_nonce, 8);
141 142 143 144 145 146
    memcpy (message + 16, message_box + crypto_box_BOXZEROBYTES,
            mlen - crypto_box_BOXZEROBYTES);

    free (message_plaintext);
    free (message_box);

147
    cn_nonce++;
148 149 150 151 152 153 154 155 156 157 158 159 160 161

    return 0;
}

int zmq::curve_client_t::decode (msg_t *msg_)
{
    zmq_assert (state == connected);

    if (msg_->size () < 33) {
        errno = EPROTO;
        return -1;
    }

    const uint8_t *message = static_cast <uint8_t *> (msg_->data ());
162
    if (memcmp (message, "\x07MESSAGE", 8)) {
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
        errno = EPROTO;
        return -1;
    }

    uint8_t message_nonce [crypto_box_NONCEBYTES];
    memcpy (message_nonce, "CurveZMQMESSAGES", 16);
    memcpy (message_nonce + 16, message + 8, 8);

    const size_t clen = crypto_box_BOXZEROBYTES + (msg_->size () - 16);

    uint8_t *message_plaintext = static_cast <uint8_t *> (malloc (clen));
    alloc_assert (message_plaintext);

    uint8_t *message_box = static_cast <uint8_t *> (malloc (clen));
    alloc_assert (message_box);

    memset (message_box, 0, crypto_box_BOXZEROBYTES);
    memcpy (message_box + crypto_box_BOXZEROBYTES,
            message + 16, msg_->size () - 16);

    int rc = crypto_box_open_afternm (message_plaintext, message_box,
184
                                      clen, message_nonce, cn_precom);
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
    if (rc == 0) {
        rc = msg_->close ();
        zmq_assert (rc == 0);

        rc = msg_->init_size (clen - 1 - crypto_box_ZEROBYTES);
        zmq_assert (rc == 0);

        const uint8_t flags = message_plaintext [crypto_box_ZEROBYTES];
        if (flags & 0x01)
            msg_->set_flags (msg_t::more);

        memcpy (msg_->data (),
                message_plaintext + crypto_box_ZEROBYTES + 1,
                msg_->size ());
    }
    else
        errno = EPROTO;

    free (message_plaintext);
    free (message_box);

    return rc;
}

209 210 211 212 213
bool zmq::curve_client_t::is_handshake_complete () const
{
    return state == connected;
}

214
int zmq::curve_client_t::produce_hello (msg_t *msg_)
215 216 217 218 219 220 221
{
    uint8_t hello_nonce [crypto_box_NONCEBYTES];
    uint8_t hello_plaintext [crypto_box_ZEROBYTES + 64];
    uint8_t hello_box [crypto_box_BOXZEROBYTES + 80];

    //  Prepare the full nonce
    memcpy (hello_nonce, "CurveZMQHELLO---", 16);
222
    memcpy (hello_nonce + 16, &cn_nonce, 8);
223 224 225 226 227 228

    //  Create Box [64 * %x0](C'->S)
    memset (hello_plaintext, 0, sizeof hello_plaintext);

    int rc = crypto_box (hello_box, hello_plaintext,
                         sizeof hello_plaintext,
229
                         hello_nonce, server_key, cn_secret);
230 231 232 233 234 235
    zmq_assert (rc == 0);

    rc = msg_->init_size (200);
    errno_assert (rc == 0);
    uint8_t *hello = static_cast <uint8_t *> (msg_->data ());

236
    memcpy (hello, "\x05HELLO", 6);
237
    //  CurveZMQ major and minor version numbers
238
    memcpy (hello + 6, "\1\0", 2);
239
    //  Anti-amplification padding
240
    memset (hello + 8, 0, 72);
241
    //  Client public connection key
242
    memcpy (hello + 80, cn_public, crypto_box_PUBLICKEYBYTES);
243 244 245 246 247
    //  Short nonce, prefixed by "CurveZMQHELLO---"
    memcpy (hello + 112, hello_nonce + 16, 8);
    //  Signature, Box [64 * %x0](C'->S)
    memcpy (hello + 120, hello_box + crypto_box_BOXZEROBYTES, 80);

248
    cn_nonce++;
249 250 251 252 253 254 255 256 257 258 259 260

    return 0;
}

int zmq::curve_client_t::process_welcome (msg_t *msg_)
{
    if (msg_->size () != 168) {
        errno = EPROTO;
        return -1;
    }

    const uint8_t * welcome = static_cast <uint8_t *> (msg_->data ());
261
    if (memcmp (welcome, "\x07WELCOME", 8)) {
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
        errno = EPROTO;
        return -1;
    }

    uint8_t welcome_nonce [crypto_box_NONCEBYTES];
    uint8_t welcome_plaintext [crypto_box_ZEROBYTES + 128];
    uint8_t welcome_box [crypto_box_BOXZEROBYTES + 144];

    //  Open Box [S' + cookie](C'->S)
    memset (welcome_box, 0, crypto_box_BOXZEROBYTES);
    memcpy (welcome_box + crypto_box_BOXZEROBYTES, welcome + 24, 144);

    memcpy (welcome_nonce, "WELCOME-", 8);
    memcpy (welcome_nonce + 8, welcome + 8, 16);

    int rc = crypto_box_open (welcome_plaintext, welcome_box,
                              sizeof welcome_box,
279
                              welcome_nonce, server_key, cn_secret);
280 281 282 283 284
    if (rc != 0) {
        errno = EPROTO;
        return -1;
    }

285 286
    memcpy (cn_server, welcome_plaintext + crypto_box_ZEROBYTES, 32);
    memcpy (cn_cookie, welcome_plaintext + crypto_box_ZEROBYTES + 32, 16 + 80);
287 288

    //  Message independent precomputation
289
    rc = crypto_box_beforenm (cn_precom, cn_server, cn_secret);
290 291 292 293 294
    zmq_assert (rc == 0);

    return 0;
}

295
int zmq::curve_client_t::produce_initiate (msg_t *msg_)
296 297
{
    uint8_t vouch_nonce [crypto_box_NONCEBYTES];
298 299
    uint8_t vouch_plaintext [crypto_box_ZEROBYTES + 64];
    uint8_t vouch_box [crypto_box_BOXZEROBYTES + 80];
300

301
    //  Create vouch = Box [C',S](C->S')
302
    memset (vouch_plaintext, 0, crypto_box_ZEROBYTES);
303
    memcpy (vouch_plaintext + crypto_box_ZEROBYTES, cn_public, 32);
304
    memcpy (vouch_plaintext + crypto_box_ZEROBYTES + 32, server_key, 32);
305 306 307 308 309 310

    memcpy (vouch_nonce, "VOUCH---", 8);
    randombytes (vouch_nonce + 8, 16);

    int rc = crypto_box (vouch_box, vouch_plaintext,
                         sizeof vouch_plaintext,
311
                         vouch_nonce, cn_server, secret_key);
312 313
    zmq_assert (rc == 0);

314
    //  Assume here that metadata is limited to 256 bytes
315
    uint8_t initiate_nonce [crypto_box_NONCEBYTES];
316 317
    uint8_t initiate_plaintext [crypto_box_ZEROBYTES + 128 + 256];
    uint8_t initiate_box [crypto_box_BOXZEROBYTES + 144 + 256];
318 319 320

    //  Create Box [C + vouch + metadata](C'->S')
    memset (initiate_plaintext, 0, crypto_box_ZEROBYTES);
321 322
    memcpy (initiate_plaintext + crypto_box_ZEROBYTES, 
            public_key, 32);
323 324 325
    memcpy (initiate_plaintext + crypto_box_ZEROBYTES + 32,
            vouch_nonce + 8, 16);
    memcpy (initiate_plaintext + crypto_box_ZEROBYTES + 48,
326
            vouch_box + crypto_box_BOXZEROBYTES, 80);
327

328 329
    //  Metadata starts after vouch
    uint8_t *ptr = initiate_plaintext + crypto_box_ZEROBYTES + 128;
330 331 332 333 334 335 336 337 338

    //  Add socket type property
    const char *socket_type = socket_type_string (options.type);
    ptr += add_property (ptr, "Socket-Type", socket_type, strlen (socket_type));

    //  Add identity property
    if (options.type == ZMQ_REQ
    ||  options.type == ZMQ_DEALER
    ||  options.type == ZMQ_ROUTER)
Pieter Hintjens's avatar
Pieter Hintjens committed
339
        ptr += add_property (ptr, "Identity", options.identity, options.identity_size);
340 341 342 343

    const size_t mlen = ptr - initiate_plaintext;

    memcpy (initiate_nonce, "CurveZMQINITIATE", 16);
344
    memcpy (initiate_nonce + 16, &cn_nonce, 8);
345 346

    rc = crypto_box (initiate_box, initiate_plaintext,
347
                     mlen, initiate_nonce, cn_server, cn_secret);
348 349
    zmq_assert (rc == 0);

350
    rc = msg_->init_size (113 + mlen - crypto_box_BOXZEROBYTES);
351 352 353 354
    errno_assert (rc == 0);

    uint8_t *initiate = static_cast <uint8_t *> (msg_->data ());

355
    memcpy (initiate, "\x08INITIATE", 9);
356
    //  Cookie provided by the server in the WELCOME command
357
    memcpy (initiate + 9, cn_cookie, 96);
358
    //  Short nonce, prefixed by "CurveZMQINITIATE"
359
    memcpy (initiate + 105, &cn_nonce, 8);
360
    //  Box [C + vouch + metadata](C'->S')
361
    memcpy (initiate + 113, initiate_box + crypto_box_BOXZEROBYTES,
362
            mlen - crypto_box_BOXZEROBYTES);
363
    cn_nonce++;
364 365 366 367 368 369

    return 0;
}

int zmq::curve_client_t::process_ready (msg_t *msg_)
{
370
    if (msg_->size () < 30) {
371 372 373 374 375
        errno = EPROTO;
        return -1;
    }

    const uint8_t *ready = static_cast <uint8_t *> (msg_->data ());
376
    if (memcmp (ready, "\x05READY", 6)) {
377 378 379 380
        errno = EPROTO;
        return -1;
    }

381
    const size_t clen = (msg_->size () - 14) + crypto_box_BOXZEROBYTES;
382 383 384 385 386 387 388

    uint8_t ready_nonce [crypto_box_NONCEBYTES];
    uint8_t ready_plaintext [crypto_box_ZEROBYTES + 256];
    uint8_t ready_box [crypto_box_BOXZEROBYTES + 16 + 256];

    memset (ready_box, 0, crypto_box_BOXZEROBYTES);
    memcpy (ready_box + crypto_box_BOXZEROBYTES,
389
            ready + 14, clen - crypto_box_BOXZEROBYTES);
390 391

    memcpy (ready_nonce, "CurveZMQREADY---", 16);
392
    memcpy (ready_nonce + 16, ready + 6, 8);
393 394

    int rc = crypto_box_open_afternm (ready_plaintext, ready_box,
395
                                      clen, ready_nonce, cn_precom);
396 397 398 399 400 401

    if (rc != 0) {
        errno = EPROTO;
        return -1;
    }

402 403
    rc = parse_metadata (ready_plaintext + crypto_box_ZEROBYTES,
                         clen - crypto_box_ZEROBYTES);
404 405 406 407
    return rc;
}

#endif