curve_server.cpp 19.6 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

    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
#include <sodium.h>

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

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

zmq::curve_server_t::curve_server_t (session_base_t *session_,
36
                                     const std::string &peer_address_,
37 38 39
                                     const options_t &options_) :
    mechanism_t (options_),
    session (session_),
40
    peer_address (peer_address_),
41
    state (expect_hello),
42
    expecting_zap_reply (false),
43
    cn_nonce (1)
44 45
{
    //  Fetch our secret key from socket options
46
    memcpy (secret_key, options_.curve_secret_key, crypto_box_SECRETKEYBYTES);
47

48 49
    //  Generate short-term key pair
    const int rc = crypto_box_keypair (cn_public, cn_secret);
50 51 52 53 54 55 56
    zmq_assert (rc == 0);
}

zmq::curve_server_t::~curve_server_t ()
{
}

57
int zmq::curve_server_t::next_handshake_command (msg_t *msg_)
58 59 60 61 62
{
    int rc = 0;

    switch (state) {
        case send_welcome:
63
            rc = produce_welcome (msg_);
64 65 66 67
            if (rc == 0)
                state = expect_initiate;
            break;
        case send_ready:
68
            rc = produce_ready (msg_);
69 70 71 72 73 74 75 76 77 78 79
            if (rc == 0)
                state = connected;
            break;
        default:
            errno = EAGAIN;
            rc = -1;
            break;
    }
    return rc;
}

80
int zmq::curve_server_t::process_handshake_command (msg_t *msg_)
81 82 83 84 85 86 87 88
{
    int rc = 0;

    switch (state) {
        case expect_hello:
            rc = process_hello (msg_);
            if (rc == 0)
                state = send_welcome;
89 90
            else
                state = errored;
91 92 93
            break;
        case expect_initiate:
            rc = process_initiate (msg_);
94 95
            if (rc == 0)
                state = expecting_zap_reply? expect_zap_reply: send_ready;
96 97
            else
                state = errored;
98 99
            break;
        default:
100
            state = errored;
101
            errno = EPROTO;
102 103 104 105 106 107 108 109 110 111 112 113
            rc = -1;
            break;
    }
    if (rc == 0) {
        rc = msg_->close ();
        errno_assert (rc == 0);
        rc = msg_->init ();
        errno_assert (rc == 0);
    }
    return rc;
}

114 115 116 117 118 119 120 121
int zmq::curve_server_t::encode (msg_t *msg_)
{
    zmq_assert (state == connected);

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

    uint8_t message_nonce [crypto_box_NONCEBYTES];
    memcpy (message_nonce, "CurveZMQMESSAGES", 16);
122
    memcpy (message_nonce + 16, &cn_nonce, 8);
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139

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

    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,
140
                                 mlen, message_nonce, cn_precom);
141 142 143 144 145 146 147 148 149 150
    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 ());

151
    memcpy (message, "\x07MESSAGE", 8);
152
    memcpy (message + 8, &cn_nonce, 8);
153 154 155 156 157 158
    memcpy (message + 16, message_box + crypto_box_BOXZEROBYTES,
            mlen - crypto_box_BOXZEROBYTES);

    free (message_plaintext);
    free (message_box);

159
    cn_nonce++;
160 161 162 163 164 165 166 167 168 169 170 171 172 173

    return 0;
}

int zmq::curve_server_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 ());
174
    if (memcmp (message, "\x07MESSAGE", 8)) {
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
        errno = EPROTO;
        return -1;
    }

    uint8_t message_nonce [crypto_box_NONCEBYTES];
    memcpy (message_nonce, "CurveZMQMESSAGEC", 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,
196
                                      clen, message_nonce, cn_precom);
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
    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;
}

221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
int zmq::curve_server_t::zap_msg_available ()
{
    if (state != expect_zap_reply) {
        errno = EFSM;
        return -1;
    }
    const int rc = receive_and_process_zap_reply ();
    if (rc == 0)
        state = send_ready;
    return rc;
}

bool zmq::curve_server_t::is_handshake_complete () const
{
    return state == connected;
}

int zmq::curve_server_t::process_hello (msg_t *msg_)
{
    if (msg_->size () != 200) {
241 242
        //  Temporary support for CURVE debugging
        puts ("CURVE I: client HELLO is not correct size");
243 244 245 246 247
        errno = EPROTO;
        return -1;
    }

    const uint8_t * const hello = static_cast <uint8_t *> (msg_->data ());
248
    if (memcmp (hello, "\x05HELLO", 6)) {
249 250
        //  Temporary support for CURVE debugging
        puts ("CURVE I: client HELLO has invalid command name");
251 252 253 254
        errno = EPROTO;
        return -1;
    }

255 256
    const uint8_t major = hello [6];
    const uint8_t minor = hello [7];
257 258

    if (major != 1 || minor != 0) {
259 260
        //  Temporary support for CURVE debugging
        puts ("CURVE I: client HELLO has unknown version number");
261 262 263 264
        errno = EPROTO;
        return -1;
    }

265 266
    //  Save client's short-term public key (C')
    memcpy (cn_client, hello + 80, 32);
267 268 269 270 271 272 273 274 275 276 277 278 279 280

    uint8_t hello_nonce [crypto_box_NONCEBYTES];
    uint8_t hello_plaintext [crypto_box_ZEROBYTES + 64];
    uint8_t hello_box [crypto_box_BOXZEROBYTES + 80];

    memcpy (hello_nonce, "CurveZMQHELLO---", 16);
    memcpy (hello_nonce + 16, hello + 112, 8);

    memset (hello_box, 0, crypto_box_BOXZEROBYTES);
    memcpy (hello_box + crypto_box_BOXZEROBYTES, hello + 120, 80);

    //  Open Box [64 * %x0](C'->S)
    int rc = crypto_box_open (hello_plaintext, hello_box,
                              sizeof hello_box,
281
                              hello_nonce, cn_client, secret_key);
282
    if (rc != 0) {
283 284
        //  Temporary support for CURVE debugging
        puts ("CURVE I: cannot open client HELLO -- wrong server key?");
285 286 287 288 289 290 291
        errno = EPROTO;
        return -1;
    }

    return rc;
}

292
int zmq::curve_server_t::produce_welcome (msg_t *msg_)
293 294 295 296 297 298 299 300 301 302 303 304 305
{
    uint8_t cookie_nonce [crypto_secretbox_NONCEBYTES];
    uint8_t cookie_plaintext [crypto_secretbox_ZEROBYTES + 64];
    uint8_t cookie_ciphertext [crypto_secretbox_BOXZEROBYTES + 80];

    //  Create full nonce for encryption
    //  8-byte prefix plus 16-byte random nonce
    memcpy (cookie_nonce, "COOKIE--", 8);
    randombytes (cookie_nonce + 8, 16);

    //  Generate cookie = Box [C' + s'](t)
    memset (cookie_plaintext, 0, crypto_secretbox_ZEROBYTES);
    memcpy (cookie_plaintext + crypto_secretbox_ZEROBYTES,
306
            cn_client, 32);
307
    memcpy (cookie_plaintext + crypto_secretbox_ZEROBYTES + 32,
308
            cn_secret, 32);
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329

    //  Generate fresh cookie key
    randombytes (cookie_key, crypto_secretbox_KEYBYTES);

    //  Encrypt using symmetric cookie key
    int rc = crypto_secretbox (cookie_ciphertext, cookie_plaintext,
                               sizeof cookie_plaintext,
                               cookie_nonce, cookie_key);
    zmq_assert (rc == 0);

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

    //  Create full nonce for encryption
    //  8-byte prefix plus 16-byte random nonce
    memcpy (welcome_nonce, "WELCOME-", 8);
    randombytes (welcome_nonce + 8, crypto_box_NONCEBYTES - 8);

    //  Create 144-byte Box [S' + cookie](S->C')
    memset (welcome_plaintext, 0, crypto_box_ZEROBYTES);
330
    memcpy (welcome_plaintext + crypto_box_ZEROBYTES, cn_public, 32);
331 332 333 334 335 336 337
    memcpy (welcome_plaintext + crypto_box_ZEROBYTES + 32,
            cookie_nonce + 8, 16);
    memcpy (welcome_plaintext + crypto_box_ZEROBYTES + 48,
            cookie_ciphertext + crypto_secretbox_BOXZEROBYTES, 80);

    rc = crypto_box (welcome_ciphertext, welcome_plaintext,
                     sizeof welcome_plaintext,
338
                     welcome_nonce, cn_client, secret_key);
339 340 341 342 343 344
    zmq_assert (rc == 0);

    rc = msg_->init_size (168);
    errno_assert (rc == 0);

    uint8_t * const welcome = static_cast <uint8_t *> (msg_->data ());
345
    memcpy (welcome, "\x07WELCOME", 8);
346 347 348 349 350 351 352 353
    memcpy (welcome + 8, welcome_nonce + 8, 16);
    memcpy (welcome + 24, welcome_ciphertext + crypto_box_BOXZEROBYTES, 144);

    return 0;
}

int zmq::curve_server_t::process_initiate (msg_t *msg_)
{
354
    if (msg_->size () < 257) {
355 356
        //  Temporary support for CURVE debugging
        puts ("CURVE I: client INITIATE is not correct size");
357 358 359 360 361
        errno = EPROTO;
        return -1;
    }

    const uint8_t *initiate = static_cast <uint8_t *> (msg_->data ());
362
    if (memcmp (initiate, "\x08INITIATE", 9)) {
363 364
        //  Temporary support for CURVE debugging
        puts ("CURVE I: client INITIATE has invalid command name");
365 366 367 368 369 370 371 372 373 374
        errno = EPROTO;
        return -1;
    }

    uint8_t cookie_nonce [crypto_secretbox_NONCEBYTES];
    uint8_t cookie_plaintext [crypto_secretbox_ZEROBYTES + 64];
    uint8_t cookie_box [crypto_secretbox_BOXZEROBYTES + 80];

    //  Open Box [C' + s'](t)
    memset (cookie_box, 0, crypto_secretbox_BOXZEROBYTES);
375
    memcpy (cookie_box + crypto_secretbox_BOXZEROBYTES, initiate + 25, 80);
376 377

    memcpy (cookie_nonce, "COOKIE--", 8);
378
    memcpy (cookie_nonce + 8, initiate + 9, 16);
379 380 381 382 383

    int rc = crypto_secretbox_open (cookie_plaintext, cookie_box,
                                    sizeof cookie_box,
                                    cookie_nonce, cookie_key);
    if (rc != 0) {
384 385
        //  Temporary support for CURVE debugging
        puts ("CURVE I: cannot open client INITIATE cookie");
386 387 388 389 390
        errno = EPROTO;
        return -1;
    }

    //  Check cookie plain text is as expected [C' + s']
391 392
    if (memcmp (cookie_plaintext + crypto_secretbox_ZEROBYTES, cn_client, 32)
    ||  memcmp (cookie_plaintext + crypto_secretbox_ZEROBYTES + 32, cn_secret, 32)) {
393 394
        //  Temporary support for CURVE debugging
        puts ("CURVE I: client INITIATE cookie is not valid");
395
        errno = EPROTO;
396 397 398
        return -1;
    }

399
    const size_t clen = (msg_->size () - 113) + crypto_box_BOXZEROBYTES;
400 401

    uint8_t initiate_nonce [crypto_box_NONCEBYTES];
402 403
    uint8_t initiate_plaintext [crypto_box_ZEROBYTES + 128 + 256];
    uint8_t initiate_box [crypto_box_BOXZEROBYTES + 144 + 256];
404 405 406 407

    //  Open Box [C + vouch + metadata](C'->S')
    memset (initiate_box, 0, crypto_box_BOXZEROBYTES);
    memcpy (initiate_box + crypto_box_BOXZEROBYTES,
408
            initiate + 113, clen - crypto_box_BOXZEROBYTES);
409 410

    memcpy (initiate_nonce, "CurveZMQINITIATE", 16);
411
    memcpy (initiate_nonce + 16, initiate + 105, 8);
412 413

    rc = crypto_box_open (initiate_plaintext, initiate_box,
414
                          clen, initiate_nonce, cn_client, cn_secret);
415
    if (rc != 0) {
416 417
        //  Temporary support for CURVE debugging
        puts ("CURVE I: cannot open client INITIATE");
418 419 420 421 422 423 424
        errno = EPROTO;
        return -1;
    }

    const uint8_t *client_key = initiate_plaintext + crypto_box_ZEROBYTES;

    uint8_t vouch_nonce [crypto_box_NONCEBYTES];
425 426
    uint8_t vouch_plaintext [crypto_box_ZEROBYTES + 64];
    uint8_t vouch_box [crypto_box_BOXZEROBYTES + 80];
427

428
    //  Open Box Box [C',S](C->S') and check contents
429 430
    memset (vouch_box, 0, crypto_box_BOXZEROBYTES);
    memcpy (vouch_box + crypto_box_BOXZEROBYTES,
431
            initiate_plaintext + crypto_box_ZEROBYTES + 48, 80);
432 433 434 435 436 437 438

    memcpy (vouch_nonce, "VOUCH---", 8);
    memcpy (vouch_nonce + 8,
            initiate_plaintext + crypto_box_ZEROBYTES + 32, 16);

    rc = crypto_box_open (vouch_plaintext, vouch_box,
                          sizeof vouch_box,
439
                          vouch_nonce, client_key, cn_secret);
440
    if (rc != 0) {
441 442
        //  Temporary support for CURVE debugging
        puts ("CURVE I: cannot open client INITIATE vouch");
443 444 445 446
        errno = EPROTO;
        return -1;
    }

447 448
    //  What we decrypted must be the client's short-term public key
    if (memcmp (vouch_plaintext + crypto_box_ZEROBYTES, cn_client, 32)) {
449 450 451 452 453
        errno = EPROTO;
        return -1;
    }

    //  Precompute connection secret from client key
454
    rc = crypto_box_beforenm (cn_precom, cn_client, cn_secret);
455 456
    zmq_assert (rc == 0);

457 458 459 460 461 462 463 464 465 466 467 468
    //  Use ZAP protocol (RFC 27) to authenticate the user.
    rc = session->zap_connect ();
    if (rc == 0) {
        send_zap_request (client_key);
        rc = receive_and_process_zap_reply ();
        if (rc != 0) {
            if (errno != EAGAIN)
                return -1;
            expecting_zap_reply = true;
        }
    }

469 470
    return parse_metadata (initiate_plaintext + crypto_box_ZEROBYTES + 128,
                           clen - crypto_box_ZEROBYTES - 128);
471 472
}

473
int zmq::curve_server_t::produce_ready (msg_t *msg_)
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
{
    uint8_t ready_nonce [crypto_box_NONCEBYTES];
    uint8_t ready_plaintext [crypto_box_ZEROBYTES + 256];
    uint8_t ready_box [crypto_box_BOXZEROBYTES + 16 + 256];

    //  Create Box [metadata](S'->C')
    memset (ready_plaintext, 0, crypto_box_ZEROBYTES);
    uint8_t *ptr = ready_plaintext + crypto_box_ZEROBYTES;

    //  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)
        ptr += add_property (ptr, "Identity",
            options.identity, options.identity_size);

    const size_t mlen = ptr - ready_plaintext;

    memcpy (ready_nonce, "CurveZMQREADY---", 16);
497
    memcpy (ready_nonce + 16, &cn_nonce, 8);
498 499

    int rc = crypto_box_afternm (ready_box, ready_plaintext,
500
                                 mlen, ready_nonce, cn_precom);
501 502
    zmq_assert (rc == 0);

503
    rc = msg_->init_size (14 + mlen - crypto_box_BOXZEROBYTES);
504 505 506 507
    errno_assert (rc == 0);

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

508
    memcpy (ready, "\x05READY", 6);
509
    //  Short nonce, prefixed by "CurveZMQREADY---"
510
    memcpy (ready + 6, &cn_nonce, 8);
511
    //  Box [metadata](S'->C')
512
    memcpy (ready + 14, ready_box + crypto_box_BOXZEROBYTES,
513 514
            mlen - crypto_box_BOXZEROBYTES);

515
    cn_nonce++;
516 517 518 519

    return 0;
}

520
void zmq::curve_server_t::send_zap_request (const uint8_t *key)
521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539
{
    int rc;
    msg_t msg;

    //  Address delimiter frame
    rc = msg.init ();
    errno_assert (rc == 0);
    msg.set_flags (msg_t::more);
    rc = session->write_zap_msg (&msg);
    errno_assert (rc == 0);

    //  Version frame
    rc = msg.init_size (3);
    errno_assert (rc == 0);
    memcpy (msg.data (), "1.0", 3);
    msg.set_flags (msg_t::more);
    rc = session->write_zap_msg (&msg);
    errno_assert (rc == 0);

540
    //  Request ID frame
541 542 543 544 545 546 547 548
    rc = msg.init_size (1);
    errno_assert (rc == 0);
    memcpy (msg.data (), "1", 1);
    msg.set_flags (msg_t::more);
    rc = session->write_zap_msg (&msg);
    errno_assert (rc == 0);

    //  Domain frame
549
    rc = msg.init_size (options.zap_domain.length ());
550
    errno_assert (rc == 0);
551
    memcpy (msg.data (), options.zap_domain.c_str (), options.zap_domain.length ());
552 553 554 555
    msg.set_flags (msg_t::more);
    rc = session->write_zap_msg (&msg);
    errno_assert (rc == 0);

556 557 558 559 560 561 562 563
    //  Address frame
    rc = msg.init_size (peer_address.length ());
    errno_assert (rc == 0);
    memcpy (msg.data (), peer_address.c_str (), peer_address.length ());
    msg.set_flags (msg_t::more);
    rc = session->write_zap_msg (&msg);
    errno_assert (rc == 0);

564
    //  Identity frame
565
    rc = msg.init_size (options.identity_size);
566
    errno_assert (rc == 0);
567 568 569 570 571
    memcpy (msg.data (), options.identity, options.identity_size);
    msg.set_flags (msg_t::more);
    rc = session->write_zap_msg (&msg);
    errno_assert (rc == 0);

572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590
    //  Mechanism frame
    rc = msg.init_size (5);
    errno_assert (rc == 0);
    memcpy (msg.data (), "CURVE", 5);
    msg.set_flags (msg_t::more);
    rc = session->write_zap_msg (&msg);
    errno_assert (rc == 0);

    //  Credentials frame
    rc = msg.init_size (crypto_box_PUBLICKEYBYTES);
    errno_assert (rc == 0);
    memcpy (msg.data (), key, crypto_box_PUBLICKEYBYTES);
    rc = session->write_zap_msg (&msg);
    errno_assert (rc == 0);
}

int zmq::curve_server_t::receive_and_process_zap_reply ()
{
    int rc = 0;
591
    msg_t msg [7];  //  ZAP reply consists of 7 frames
592

593 594
    //  Initialize all reply frames
    for (int i = 0; i < 7; i++) {
595 596 597 598
        rc = msg [i].init ();
        errno_assert (rc == 0);
    }

599
    for (int i = 0; i < 7; i++) {
600 601 602
        rc = session->read_zap_msg (&msg [i]);
        if (rc == -1)
            break;
603
        if ((msg [i].flags () & msg_t::more) == (i < 6? 0: msg_t::more)) {
604 605 606 607 608 609 610 611 612 613 614
            errno = EPROTO;
            rc = -1;
            break;
        }
    }

    if (rc != 0)
        goto error;

    //  Address delimiter frame
    if (msg [0].size () > 0) {
615
        rc = -1;
616 617 618 619 620 621
        errno = EPROTO;
        goto error;
    }

    //  Version frame
    if (msg [1].size () != 3 || memcmp (msg [1].data (), "1.0", 3)) {
622
        rc = -1;
623 624 625 626
        errno = EPROTO;
        goto error;
    }

627
    //  Request id frame
628
    if (msg [2].size () != 1 || memcmp (msg [2].data (), "1", 1)) {
629
        rc = -1;
630 631 632 633 634 635
        errno = EPROTO;
        goto error;
    }

    //  Status code frame
    if (msg [3].size () != 3 || memcmp (msg [3].data (), "200", 3)) {
636
        rc = -1;
637 638
        //  Temporary support for CURVE debugging
        puts ("CURVE I: ZAP handler rejected client authentication");
639 640 641 642
        errno = EACCES;
        goto error;
    }

643 644 645
    //  Save user id
    set_user_id (msg [5].data (), msg [5].size ());

646 647 648 649
    //  Process metadata frame
    rc = parse_metadata (static_cast <const unsigned char*> (msg [6].data ()),
                         msg [6].size ());

650
error:
651
    for (int i = 0; i < 7; i++) {
652 653 654 655 656 657 658 659
        const int rc2 = msg [i].close ();
        errno_assert (rc2 == 0);
    }

    return rc;
}

#endif