@@ -327,8 +327,12 @@ int zmq::curve_server_t::process_initiate (msg_t *msg_)
327327 const size_t clen = (size - 113 ) + crypto_box_BOXZEROBYTES;
328328
329329 uint8_t initiate_nonce[crypto_box_NONCEBYTES];
330- uint8_t initiate_plaintext[crypto_box_ZEROBYTES + 128 + 256 ];
331- uint8_t initiate_box[crypto_box_BOXZEROBYTES + 144 + 256 ];
330+ uint8_t *initiate_plaintext =
331+ static_cast <uint8_t *> (malloc (crypto_box_ZEROBYTES + clen));
332+ alloc_assert (initiate_plaintext);
333+ uint8_t *initiate_box =
334+ static_cast <uint8_t *> (malloc (crypto_box_BOXZEROBYTES + clen));
335+ alloc_assert (initiate_box);
332336
333337 // Open Box [C + vouch + metadata](C'->S')
334338 memset (initiate_box, 0 , crypto_box_BOXZEROBYTES);
@@ -339,18 +343,19 @@ int zmq::curve_server_t::process_initiate (msg_t *msg_)
339343 memcpy (initiate_nonce + 16 , initiate + 105 , 8 );
340344 cn_peer_nonce = get_uint64 (initiate + 105 );
341345
346+ const uint8_t *client_key = initiate_plaintext + crypto_box_ZEROBYTES;
347+
342348 rc = crypto_box_open (initiate_plaintext, initiate_box, clen,
343349 initiate_nonce, _cn_client, _cn_secret);
344350 if (rc != 0 ) {
345351 // CURVE I: cannot open client INITIATE
346352 session->get_socket ()->event_handshake_failed_protocol (
347353 session->get_endpoint (), ZMQ_PROTOCOL_ERROR_ZMTP_CRYPTOGRAPHIC);
348354 errno = EPROTO;
349- return -1 ;
355+ rc = -1 ;
356+ goto exit;
350357 }
351358
352- const uint8_t *client_key = initiate_plaintext + crypto_box_ZEROBYTES;
353-
354359 uint8_t vouch_nonce[crypto_box_NONCEBYTES];
355360 uint8_t vouch_plaintext[crypto_box_ZEROBYTES + 64 ];
356361 uint8_t vouch_box[crypto_box_BOXZEROBYTES + 80 ];
@@ -371,7 +376,8 @@ int zmq::curve_server_t::process_initiate (msg_t *msg_)
371376 session->get_socket ()->event_handshake_failed_protocol (
372377 session->get_endpoint (), ZMQ_PROTOCOL_ERROR_ZMTP_CRYPTOGRAPHIC);
373378 errno = EPROTO;
374- return -1 ;
379+ rc = -1 ;
380+ goto exit;
375381 }
376382
377383 // What we decrypted must be the client's short-term public key
@@ -383,7 +389,8 @@ int zmq::curve_server_t::process_initiate (msg_t *msg_)
383389 session->get_socket ()->event_handshake_failed_protocol (
384390 session->get_endpoint (), ZMQ_PROTOCOL_ERROR_ZMTP_KEY_EXCHANGE);
385391 errno = EPROTO;
386- return -1 ;
392+ rc = -1 ;
393+ goto exit;
387394 }
388395
389396 // Precompute connection secret from client key
@@ -405,23 +412,29 @@ int zmq::curve_server_t::process_initiate (msg_t *msg_)
405412 // is attempted)
406413 rc = receive_and_process_zap_reply ();
407414 if (rc == -1 )
408- return - 1 ;
415+ goto exit ;
409416 } else if (!options.zap_enforce_domain ) {
410417 // This supports the Stonehouse pattern (encryption without
411418 // authentication) in legacy mode (domain set but no handler).
412419 state = sending_ready;
413420 } else {
414421 session->get_socket ()->event_handshake_failed_no_detail (
415422 session->get_endpoint (), EFAULT);
416- return -1 ;
423+ rc = -1 ;
424+ goto exit;
417425 }
418426 } else {
419427 // This supports the Stonehouse pattern (encryption without authentication).
420428 state = sending_ready;
421429 }
422430
423- return parse_metadata (initiate_plaintext + crypto_box_ZEROBYTES + 128 ,
424- clen - crypto_box_ZEROBYTES - 128 );
431+ rc = parse_metadata (initiate_plaintext + crypto_box_ZEROBYTES + 128 ,
432+ clen - crypto_box_ZEROBYTES - 128 );
433+
434+ exit:
435+ free (initiate_plaintext);
436+ free (initiate_box);
437+ return rc;
425438}
426439
427440int zmq::curve_server_t::produce_ready (msg_t *msg_)
0 commit comments