int zmq::gssapi_mechanism_base_t::produce_ready (msg_t *msg_) { unsigned char * const command_buffer = (unsigned char *) malloc (512); alloc_assert (command_buffer); unsigned char *ptr = command_buffer; // Add command name memcpy (ptr, "\x05READY", 6); ptr += 6; // 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 command_size = ptr - command_buffer; const int rc = msg_->init_size (command_size); errno_assert (rc == 0); memcpy (msg_->data (), command_buffer, command_size); free (command_buffer); if (do_encryption) return encode_message (msg_); return 0; }
int zmq::plain_client_t::produce_initiate (msg_t *msg_) const { unsigned char * const command_buffer = (unsigned char *) malloc (512); alloc_assert (command_buffer); unsigned char *ptr = command_buffer; // Add mechanism string memcpy (ptr, "\x08INITIATE", 9); ptr += 9; // 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 command_size = ptr - command_buffer; const int rc = msg_->init_size (command_size); errno_assert (rc == 0); memcpy (msg_->data (), command_buffer, command_size); free (command_buffer); return 0; }
int zmq::plain_mechanism_t::ready_command (msg_t *msg_) const { unsigned char * const command_buffer = (unsigned char *) malloc (512); alloc_assert (command_buffer); unsigned char *ptr = command_buffer; // Add command name memcpy (ptr, "READY\0", 6); ptr += 6; // 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 command_size = ptr - command_buffer; const int rc = msg_->init_size (command_size); errno_assert (rc == 0); memcpy (msg_->data (), command_buffer, command_size); free (command_buffer); return 0; }
int zmq::null_mechanism_t::next_handshake_command (msg_t *msg_) { if (ready_command_sent) { errno = EAGAIN; return -1; } if (zap_connected && !zap_reply_received) { if (zap_request_sent) { errno = EAGAIN; return -1; } send_zap_request (); zap_request_sent = true; const int rc = receive_and_process_zap_reply (); if (rc != 0) return -1; zap_reply_received = true; } unsigned char * const command_buffer = (unsigned char *) malloc (512); alloc_assert (command_buffer); unsigned char *ptr = command_buffer; // Add mechanism string memcpy (ptr, "\5READY", 6); ptr += 6; // 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); } // Add keepalive property, with 16 bit value in tenths of a second unsigned char keepalive_ivl_nbo[2]; put_uint16 (keepalive_ivl_nbo, static_cast <uint16_t> (options.keepalive_ivl / 100)); ptr += add_property (ptr, "Keepalive", keepalive_ivl_nbo, sizeof(keepalive_ivl_nbo)); const size_t command_size = ptr - command_buffer; const int rc = msg_->init_size (command_size); errno_assert (rc == 0); memcpy (msg_->data (), command_buffer, command_size); free (command_buffer); ready_command_sent = true; return 0; }
size_t zmq::mechanism_t::basic_properties_len() const { const char *socket_type = socket_type_string (options.type); return property_len (ZMTP_PROPERTY_SOCKET_TYPE, strlen (socket_type)) + ((options.type == ZMQ_REQ || options.type == ZMQ_DEALER || options.type == ZMQ_ROUTER) ? property_len (ZMTP_PROPERTY_IDENTITY, options.routing_id_size) : 0); }
int zmq::curve_server_t::ready_msg (msg_t *msg_) { 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); memcpy (ready_nonce + 16, &cn_nonce, 8); int rc = crypto_box_afternm (ready_box, ready_plaintext, mlen, ready_nonce, cn_precom); zmq_assert (rc == 0); rc = msg_->init_size (14 + mlen - crypto_box_BOXZEROBYTES); errno_assert (rc == 0); uint8_t *ready = static_cast <uint8_t *> (msg_->data ()); memcpy (ready, "READY\0", 6); // Short nonce, prefixed by "CurveZMQREADY---" memcpy (ready + 6, &cn_nonce, 8); // Box [metadata](S'->C') memcpy (ready + 14, ready_box + crypto_box_BOXZEROBYTES, mlen - crypto_box_BOXZEROBYTES); cn_nonce++; return 0; }
size_t zmq::mechanism_t::add_basic_properties (unsigned char *buf, size_t buf_capacity) const { unsigned char *ptr = buf; // Add socket type property const char *socket_type = socket_type_string (options.type); ptr += add_property (ptr, buf_capacity, ZMTP_PROPERTY_SOCKET_TYPE, socket_type, strlen (socket_type)); // Add identity (aka routing id) property if (options.type == ZMQ_REQ || options.type == ZMQ_DEALER || options.type == ZMQ_ROUTER) ptr += add_property (ptr, buf_capacity - (ptr - buf), ZMTP_PROPERTY_IDENTITY, options.routing_id, options.routing_id_size); return ptr - buf; }
int zmq::null_mechanism_t::next_handshake_message (msg_t *msg_) { if (ready_command_sent) { errno = EAGAIN; return -1; } unsigned char * const command_buffer = (unsigned char *) malloc (512); alloc_assert (command_buffer); unsigned char *ptr = command_buffer; // Add mechanism string memcpy (ptr, "READY\0", 6); ptr += 6; // 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 command_size = ptr - command_buffer; const int rc = msg_->init_size (command_size); errno_assert (rc == 0); memcpy (msg_->data (), command_buffer, command_size); free (command_buffer); ready_command_sent = true; return 0; }
int zmq::curve_client_t::produce_initiate (msg_t *msg_) { uint8_t vouch_nonce [crypto_box_NONCEBYTES]; uint8_t vouch_plaintext [crypto_box_ZEROBYTES + 64]; uint8_t vouch_box [crypto_box_BOXZEROBYTES + 80]; // Create vouch = Box [C',S](C->S') memset (vouch_plaintext, 0, crypto_box_ZEROBYTES); memcpy (vouch_plaintext + crypto_box_ZEROBYTES, cn_public, 32); memcpy (vouch_plaintext + crypto_box_ZEROBYTES + 32, server_key, 32); memcpy (vouch_nonce, "VOUCH---", 8); randombytes (vouch_nonce + 8, 16); int rc = crypto_box (vouch_box, vouch_plaintext, sizeof vouch_plaintext, vouch_nonce, cn_server, secret_key); zmq_assert (rc == 0); // Assume here that metadata is limited to 256 bytes uint8_t initiate_nonce [crypto_box_NONCEBYTES]; uint8_t initiate_plaintext [crypto_box_ZEROBYTES + 128 + 256]; uint8_t initiate_box [crypto_box_BOXZEROBYTES + 144 + 256]; // Create Box [C + vouch + metadata](C'->S') memset (initiate_plaintext, 0, crypto_box_ZEROBYTES); memcpy (initiate_plaintext + crypto_box_ZEROBYTES, public_key, 32); memcpy (initiate_plaintext + crypto_box_ZEROBYTES + 32, vouch_nonce + 8, 16); memcpy (initiate_plaintext + crypto_box_ZEROBYTES + 48, vouch_box + crypto_box_BOXZEROBYTES, 80); // Metadata starts after vouch uint8_t *ptr = initiate_plaintext + crypto_box_ZEROBYTES + 128; // 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 - initiate_plaintext; memcpy (initiate_nonce, "CurveZMQINITIATE", 16); put_uint64 (initiate_nonce + 16, cn_nonce); rc = crypto_box (initiate_box, initiate_plaintext, mlen, initiate_nonce, cn_server, cn_secret); zmq_assert (rc == 0); rc = msg_->init_size (113 + mlen - crypto_box_BOXZEROBYTES); errno_assert (rc == 0); uint8_t *initiate = static_cast <uint8_t *> (msg_->data ()); memcpy (initiate, "\x08INITIATE", 9); // Cookie provided by the server in the WELCOME command memcpy (initiate + 9, cn_cookie, 96); // Short nonce, prefixed by "CurveZMQINITIATE" memcpy (initiate + 105, initiate_nonce + 16, 8); // Box [C + vouch + metadata](C'->S') memcpy (initiate + 113, initiate_box + crypto_box_BOXZEROBYTES, mlen - crypto_box_BOXZEROBYTES); cn_nonce++; return 0; }
int zmq::null_mechanism_t::next_handshake_command (msg_t *msg_) { if (ready_command_sent || error_command_sent) { errno = EAGAIN; return -1; } if (zap_connected && !zap_reply_received) { if (zap_request_sent) { errno = EAGAIN; return -1; } send_zap_request (); zap_request_sent = true; const int rc = receive_and_process_zap_reply (); if (rc != 0) return -1; zap_reply_received = true; } if (zap_reply_received && strncmp (status_code, "200", sizeof status_code) != 0) { const int rc = msg_->init_size (6 + 1 + sizeof status_code); zmq_assert (rc == 0); unsigned char *msg_data = static_cast <unsigned char *> (msg_->data ()); memcpy (msg_data, "\5ERROR", 6); msg_data [6] = sizeof status_code; memcpy (msg_data + 7, status_code, sizeof status_code); error_command_sent = true; return 0; } unsigned char *const command_buffer = (unsigned char *) malloc (512); alloc_assert (command_buffer); unsigned char *ptr = command_buffer; // Add mechanism string memcpy (ptr, "\5READY", 6); ptr += 6; // 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 command_size = ptr - command_buffer; const int rc = msg_->init_size (command_size); errno_assert (rc == 0); memcpy (msg_->data (), command_buffer, command_size); free (command_buffer); ready_command_sent = true; return 0; }