32 #include "platform.hpp" 36 #ifdef ZMQ_HAVE_WINDOWS 46 zmq::curve_client_t::curve_client_t (
const options_t &options_) :
47 mechanism_t (options_),
53 memcpy (public_key, options_.curve_public_key, crypto_box_PUBLICKEYBYTES);
54 memcpy (secret_key, options_.curve_secret_key, crypto_box_SECRETKEYBYTES);
55 memcpy (server_key, options_.curve_server_key, crypto_box_PUBLICKEYBYTES);
58 rc = crypto_box_keypair (cn_public, cn_secret);
62 zmq::curve_client_t::~curve_client_t ()
66 int zmq::curve_client_t::next_handshake_command (msg_t *msg_)
72 rc = produce_hello (msg_);
74 state = expect_welcome;
77 rc = produce_initiate (msg_);
88 int zmq::curve_client_t::process_handshake_command (msg_t *msg_)
90 const unsigned char *msg_data =
91 static_cast <
unsigned char *> (msg_->data ());
92 const size_t msg_size = msg_->size ();
95 if (msg_size >= 8 && !memcmp (msg_data,
"\7WELCOME", 8))
96 rc = process_welcome (msg_data, msg_size);
98 if (msg_size >= 6 && !memcmp (msg_data,
"\5READY", 6))
99 rc = process_ready (msg_data, msg_size);
101 if (msg_size >= 6 && !memcmp (msg_data,
"\5ERROR", 6))
102 rc = process_error (msg_data, msg_size);
118 int zmq::curve_client_t::encode (msg_t *msg_)
128 uint8_t message_nonce [crypto_box_NONCEBYTES];
129 memcpy (message_nonce,
"CurveZMQMESSAGEC", 16);
132 const size_t mlen = crypto_box_ZEROBYTES + 1 + msg_->size ();
134 uint8_t *message_plaintext = static_cast <uint8_t *> (malloc (mlen));
137 memset (message_plaintext, 0, crypto_box_ZEROBYTES);
138 message_plaintext [crypto_box_ZEROBYTES] = flags;
139 memcpy (message_plaintext + crypto_box_ZEROBYTES + 1,
140 msg_->data (), msg_->size ());
142 uint8_t *message_box = static_cast <uint8_t *> (malloc (mlen));
145 int rc = crypto_box_afternm (message_box, message_plaintext,
146 mlen, message_nonce, cn_precom);
152 rc = msg_->init_size (16 + mlen - crypto_box_BOXZEROBYTES);
155 uint8_t *message = static_cast <uint8_t *> (msg_->data ());
157 memcpy (message,
"\x07MESSAGE", 8);
158 memcpy (message + 8, message_nonce + 16, 8);
159 memcpy (message + 16, message_box + crypto_box_BOXZEROBYTES,
160 mlen - crypto_box_BOXZEROBYTES);
162 free (message_plaintext);
170 int zmq::curve_client_t::decode (msg_t *msg_)
174 if (msg_->size () < 33) {
179 const uint8_t *message = static_cast <uint8_t *> (msg_->data ());
180 if (memcmp (message,
"\x07MESSAGE", 8)) {
185 uint8_t message_nonce [crypto_box_NONCEBYTES];
186 memcpy (message_nonce,
"CurveZMQMESSAGES", 16);
187 memcpy (message_nonce + 16, message + 8, 8);
189 if (nonce <= cn_peer_nonce) {
193 cn_peer_nonce = nonce;
195 const size_t clen = crypto_box_BOXZEROBYTES + (msg_->size () - 16);
197 uint8_t *message_plaintext = static_cast <uint8_t *> (malloc (clen));
200 uint8_t *message_box = static_cast <uint8_t *> (malloc (clen));
203 memset (message_box, 0, crypto_box_BOXZEROBYTES);
204 memcpy (message_box + crypto_box_BOXZEROBYTES,
205 message + 16, msg_->size () - 16);
207 int rc = crypto_box_open_afternm (message_plaintext, message_box,
208 clen, message_nonce, cn_precom);
213 rc = msg_->init_size (clen - 1 - crypto_box_ZEROBYTES);
216 const uint8_t flags = message_plaintext [crypto_box_ZEROBYTES];
222 memcpy (msg_->data (),
223 message_plaintext + crypto_box_ZEROBYTES + 1,
229 free (message_plaintext);
237 if (state == connected)
240 if (state == error_received)
246 int zmq::curve_client_t::produce_hello (msg_t *msg_)
248 uint8_t hello_nonce [crypto_box_NONCEBYTES];
249 uint8_t hello_plaintext [crypto_box_ZEROBYTES + 64];
250 uint8_t hello_box [crypto_box_BOXZEROBYTES + 80];
253 memcpy (hello_nonce,
"CurveZMQHELLO---", 16);
257 memset (hello_plaintext, 0,
sizeof hello_plaintext);
259 int rc = crypto_box (hello_box, hello_plaintext,
260 sizeof hello_plaintext,
261 hello_nonce, server_key, cn_secret);
265 rc = msg_->init_size (200);
267 uint8_t *hello = static_cast <uint8_t *> (msg_->data ());
269 memcpy (hello,
"\x05HELLO", 6);
271 memcpy (hello + 6,
"\1\0", 2);
273 memset (hello + 8, 0, 72);
275 memcpy (hello + 80, cn_public, crypto_box_PUBLICKEYBYTES);
277 memcpy (hello + 112, hello_nonce + 16, 8);
279 memcpy (hello + 120, hello_box + crypto_box_BOXZEROBYTES, 80);
286 int zmq::curve_client_t::process_welcome (
287 const uint8_t *msg_data,
size_t msg_size)
289 if (msg_size != 168) {
294 uint8_t welcome_nonce [crypto_box_NONCEBYTES];
295 uint8_t welcome_plaintext [crypto_box_ZEROBYTES + 128];
296 uint8_t welcome_box [crypto_box_BOXZEROBYTES + 144];
299 memset (welcome_box, 0, crypto_box_BOXZEROBYTES);
300 memcpy (welcome_box + crypto_box_BOXZEROBYTES, msg_data + 24, 144);
302 memcpy (welcome_nonce,
"WELCOME-", 8);
303 memcpy (welcome_nonce + 8, msg_data + 8, 16);
305 int rc = crypto_box_open (welcome_plaintext, welcome_box,
307 welcome_nonce, server_key, cn_secret);
313 memcpy (cn_server, welcome_plaintext + crypto_box_ZEROBYTES, 32);
314 memcpy (cn_cookie, welcome_plaintext + crypto_box_ZEROBYTES + 32, 16 + 80);
317 rc = crypto_box_beforenm (cn_precom, cn_server, cn_secret);
320 state = send_initiate;
325 int zmq::curve_client_t::produce_initiate (msg_t *msg_)
327 uint8_t vouch_nonce [crypto_box_NONCEBYTES];
328 uint8_t vouch_plaintext [crypto_box_ZEROBYTES + 64];
329 uint8_t vouch_box [crypto_box_BOXZEROBYTES + 80];
332 memset (vouch_plaintext, 0, crypto_box_ZEROBYTES);
333 memcpy (vouch_plaintext + crypto_box_ZEROBYTES, cn_public, 32);
334 memcpy (vouch_plaintext + crypto_box_ZEROBYTES + 32, server_key, 32);
336 memcpy (vouch_nonce,
"VOUCH---", 8);
337 randombytes (vouch_nonce + 8, 16);
339 int rc = crypto_box (vouch_box, vouch_plaintext,
340 sizeof vouch_plaintext,
341 vouch_nonce, cn_server, secret_key);
346 uint8_t initiate_nonce [crypto_box_NONCEBYTES];
347 uint8_t initiate_plaintext [crypto_box_ZEROBYTES + 128 + 256];
348 uint8_t initiate_box [crypto_box_BOXZEROBYTES + 144 + 256];
351 memset (initiate_plaintext, 0, crypto_box_ZEROBYTES);
352 memcpy (initiate_plaintext + crypto_box_ZEROBYTES,
354 memcpy (initiate_plaintext + crypto_box_ZEROBYTES + 32,
355 vouch_nonce + 8, 16);
356 memcpy (initiate_plaintext + crypto_box_ZEROBYTES + 48,
357 vouch_box + crypto_box_BOXZEROBYTES, 80);
360 uint8_t *ptr = initiate_plaintext + crypto_box_ZEROBYTES + 128;
363 const char *socket_type = socket_type_string (options.type);
364 ptr += add_property (ptr,
"Socket-Type", socket_type, strlen (socket_type));
370 ptr += add_property (ptr,
"Identity", options.identity, options.identity_size);
372 const size_t mlen = ptr - initiate_plaintext;
374 memcpy (initiate_nonce,
"CurveZMQINITIATE", 16);
377 rc = crypto_box (initiate_box, initiate_plaintext,
378 mlen, initiate_nonce, cn_server, cn_secret);
382 rc = msg_->init_size (113 + mlen - crypto_box_BOXZEROBYTES);
385 uint8_t *initiate = static_cast <uint8_t *> (msg_->data ());
387 memcpy (initiate,
"\x08INITIATE", 9);
389 memcpy (initiate + 9, cn_cookie, 96);
391 memcpy (initiate + 105, initiate_nonce + 16, 8);
393 memcpy (initiate + 113, initiate_box + crypto_box_BOXZEROBYTES,
394 mlen - crypto_box_BOXZEROBYTES);
400 int zmq::curve_client_t::process_ready (
401 const uint8_t *msg_data,
size_t msg_size)
408 const size_t clen = (msg_size - 14) + crypto_box_BOXZEROBYTES;
410 uint8_t ready_nonce [crypto_box_NONCEBYTES];
411 uint8_t ready_plaintext [crypto_box_ZEROBYTES + 256];
412 uint8_t ready_box [crypto_box_BOXZEROBYTES + 16 + 256];
414 memset (ready_box, 0, crypto_box_BOXZEROBYTES);
415 memcpy (ready_box + crypto_box_BOXZEROBYTES,
416 msg_data + 14, clen - crypto_box_BOXZEROBYTES);
418 memcpy (ready_nonce,
"CurveZMQREADY---", 16);
419 memcpy (ready_nonce + 16, msg_data + 6, 8);
422 int rc = crypto_box_open_afternm (ready_plaintext, ready_box,
423 clen, ready_nonce, cn_precom);
430 rc = parse_metadata (ready_plaintext + crypto_box_ZEROBYTES,
431 clen - crypto_box_ZEROBYTES);
438 int zmq::curve_client_t::process_error (
439 const uint8_t *msg_data,
size_t msg_size)
441 if (state != expect_welcome && state != expect_ready) {
449 const size_t error_reason_len = static_cast <
size_t> (msg_data [6]);
450 if (error_reason_len > msg_size - 7) {
454 state = error_received;
uint64_t get_uint64(const unsigned char *buffer_)
void put_uint64(unsigned char *buffer_, uint64_t value)