curve_client_tools.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /*
  2. Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file
  3. This file is part of libzmq, the ZeroMQ core engine in C++.
  4. libzmq is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU Lesser General Public License (LGPL) as published
  6. by the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. As a special exception, the Contributors give you permission to link
  9. this library with independent modules to produce an executable,
  10. regardless of the license terms of these independent modules, and to
  11. copy and distribute the resulting executable under terms of your choice,
  12. provided that you also meet, for each linked independent module, the
  13. terms and conditions of the license of that module. An independent
  14. module is a module which is not derived from or based on this library.
  15. If you modify this library, you must extend this exception to your
  16. version of the library.
  17. libzmq is distributed in the hope that it will be useful, but WITHOUT
  18. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  19. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  20. License for more details.
  21. You should have received a copy of the GNU Lesser General Public License
  22. along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. */
  24. #ifndef __ZMQ_CURVE_CLIENT_TOOLS_HPP_INCLUDED__
  25. #define __ZMQ_CURVE_CLIENT_TOOLS_HPP_INCLUDED__
  26. #ifdef ZMQ_HAVE_CURVE
  27. #if defined(ZMQ_USE_TWEETNACL)
  28. #include "tweetnacl.h"
  29. #elif defined(ZMQ_USE_LIBSODIUM)
  30. #include "sodium.h"
  31. #endif
  32. #if crypto_box_NONCEBYTES != 24 || crypto_box_PUBLICKEYBYTES != 32 \
  33. || crypto_box_SECRETKEYBYTES != 32 || crypto_box_ZEROBYTES != 32 \
  34. || crypto_box_BOXZEROBYTES != 16
  35. #error "CURVE library not built properly"
  36. #endif
  37. #include "wire.hpp"
  38. #include "err.hpp"
  39. #include "secure_allocator.hpp"
  40. #include <vector>
  41. namespace zmq
  42. {
  43. struct curve_client_tools_t
  44. {
  45. static int produce_hello (void *data_,
  46. const uint8_t *server_key_,
  47. const uint64_t cn_nonce_,
  48. const uint8_t *cn_public_,
  49. const uint8_t *cn_secret_)
  50. {
  51. uint8_t hello_nonce[crypto_box_NONCEBYTES];
  52. std::vector<uint8_t, secure_allocator_t<uint8_t> > hello_plaintext (
  53. crypto_box_ZEROBYTES + 64, 0);
  54. uint8_t hello_box[crypto_box_BOXZEROBYTES + 80];
  55. // Prepare the full nonce
  56. memcpy (hello_nonce, "CurveZMQHELLO---", 16);
  57. put_uint64 (hello_nonce + 16, cn_nonce_);
  58. // Create Box [64 * %x0](C'->S)
  59. const int rc =
  60. crypto_box (hello_box, &hello_plaintext[0], hello_plaintext.size (),
  61. hello_nonce, server_key_, cn_secret_);
  62. if (rc == -1)
  63. return -1;
  64. uint8_t *hello = static_cast<uint8_t *> (data_);
  65. memcpy (hello, "\x05HELLO", 6);
  66. // CurveZMQ major and minor version numbers
  67. memcpy (hello + 6, "\1\0", 2);
  68. // Anti-amplification padding
  69. memset (hello + 8, 0, 72);
  70. // Client public connection key
  71. memcpy (hello + 80, cn_public_, crypto_box_PUBLICKEYBYTES);
  72. // Short nonce, prefixed by "CurveZMQHELLO---"
  73. memcpy (hello + 112, hello_nonce + 16, 8);
  74. // Signature, Box [64 * %x0](C'->S)
  75. memcpy (hello + 120, hello_box + crypto_box_BOXZEROBYTES, 80);
  76. return 0;
  77. }
  78. static int process_welcome (const uint8_t *msg_data_,
  79. size_t msg_size_,
  80. const uint8_t *server_key_,
  81. const uint8_t *cn_secret_,
  82. uint8_t *cn_server_,
  83. uint8_t *cn_cookie_,
  84. uint8_t *cn_precom_)
  85. {
  86. if (msg_size_ != 168) {
  87. errno = EPROTO;
  88. return -1;
  89. }
  90. uint8_t welcome_nonce[crypto_box_NONCEBYTES];
  91. std::vector<uint8_t, secure_allocator_t<uint8_t> > welcome_plaintext (
  92. crypto_box_ZEROBYTES + 128);
  93. uint8_t welcome_box[crypto_box_BOXZEROBYTES + 144];
  94. // Open Box [S' + cookie](C'->S)
  95. memset (welcome_box, 0, crypto_box_BOXZEROBYTES);
  96. memcpy (welcome_box + crypto_box_BOXZEROBYTES, msg_data_ + 24, 144);
  97. memcpy (welcome_nonce, "WELCOME-", 8);
  98. memcpy (welcome_nonce + 8, msg_data_ + 8, 16);
  99. int rc = crypto_box_open (&welcome_plaintext[0], welcome_box,
  100. sizeof welcome_box, welcome_nonce,
  101. server_key_, cn_secret_);
  102. if (rc != 0) {
  103. errno = EPROTO;
  104. return -1;
  105. }
  106. memcpy (cn_server_, &welcome_plaintext[crypto_box_ZEROBYTES], 32);
  107. memcpy (cn_cookie_, &welcome_plaintext[crypto_box_ZEROBYTES + 32],
  108. 16 + 80);
  109. // Message independent precomputation
  110. rc = crypto_box_beforenm (cn_precom_, cn_server_, cn_secret_);
  111. zmq_assert (rc == 0);
  112. return 0;
  113. }
  114. static int produce_initiate (void *data_,
  115. size_t size_,
  116. const uint64_t cn_nonce_,
  117. const uint8_t *server_key_,
  118. const uint8_t *public_key_,
  119. const uint8_t *secret_key_,
  120. const uint8_t *cn_public_,
  121. const uint8_t *cn_secret_,
  122. const uint8_t *cn_server_,
  123. const uint8_t *cn_cookie_,
  124. const uint8_t *metadata_plaintext_,
  125. const size_t metadata_length_)
  126. {
  127. uint8_t vouch_nonce[crypto_box_NONCEBYTES];
  128. std::vector<uint8_t, secure_allocator_t<uint8_t> > vouch_plaintext (
  129. crypto_box_ZEROBYTES + 64);
  130. uint8_t vouch_box[crypto_box_BOXZEROBYTES + 80];
  131. // Create vouch = Box [C',S](C->S')
  132. std::fill (vouch_plaintext.begin (),
  133. vouch_plaintext.begin () + crypto_box_ZEROBYTES, 0);
  134. memcpy (&vouch_plaintext[crypto_box_ZEROBYTES], cn_public_, 32);
  135. memcpy (&vouch_plaintext[crypto_box_ZEROBYTES + 32], server_key_, 32);
  136. memset (vouch_nonce, 0, crypto_box_NONCEBYTES);
  137. memcpy (vouch_nonce, "VOUCH---", 8);
  138. randombytes (vouch_nonce + 8, 16);
  139. int rc =
  140. crypto_box (vouch_box, &vouch_plaintext[0], vouch_plaintext.size (),
  141. vouch_nonce, cn_server_, secret_key_);
  142. if (rc == -1)
  143. return -1;
  144. uint8_t initiate_nonce[crypto_box_NONCEBYTES];
  145. std::vector<uint8_t> initiate_box (crypto_box_BOXZEROBYTES + 144
  146. + metadata_length_);
  147. std::vector<uint8_t, secure_allocator_t<uint8_t> > initiate_plaintext (
  148. crypto_box_ZEROBYTES + 128 + metadata_length_);
  149. // Create Box [C + vouch + metadata](C'->S')
  150. std::fill (initiate_plaintext.begin (),
  151. initiate_plaintext.begin () + crypto_box_ZEROBYTES, 0);
  152. // False positives due to https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99578
  153. #if __GNUC__ >= 11
  154. #pragma GCC diagnostic ignored "-Warray-bounds"
  155. #pragma GCC diagnostic ignored "-Wstringop-overflow="
  156. #endif
  157. memcpy (&initiate_plaintext[crypto_box_ZEROBYTES], public_key_, 32);
  158. memcpy (&initiate_plaintext[crypto_box_ZEROBYTES + 32], vouch_nonce + 8,
  159. 16);
  160. memcpy (&initiate_plaintext[crypto_box_ZEROBYTES + 48],
  161. vouch_box + crypto_box_BOXZEROBYTES, 80);
  162. if (metadata_length_) {
  163. memcpy (&initiate_plaintext[crypto_box_ZEROBYTES + 48 + 80],
  164. metadata_plaintext_, metadata_length_);
  165. }
  166. #if __GNUC__ >= 11
  167. #pragma GCC diagnostic pop
  168. #pragma GCC diagnostic pop
  169. #endif
  170. memcpy (initiate_nonce, "CurveZMQINITIATE", 16);
  171. put_uint64 (initiate_nonce + 16, cn_nonce_);
  172. rc = crypto_box (&initiate_box[0], &initiate_plaintext[0],
  173. crypto_box_ZEROBYTES + 128 + metadata_length_,
  174. initiate_nonce, cn_server_, cn_secret_);
  175. if (rc == -1)
  176. return -1;
  177. uint8_t *initiate = static_cast<uint8_t *> (data_);
  178. zmq_assert (size_
  179. == 113 + 128 + crypto_box_BOXZEROBYTES + metadata_length_);
  180. memcpy (initiate, "\x08INITIATE", 9);
  181. // Cookie provided by the server in the WELCOME command
  182. memcpy (initiate + 9, cn_cookie_, 96);
  183. // Short nonce, prefixed by "CurveZMQINITIATE"
  184. memcpy (initiate + 105, initiate_nonce + 16, 8);
  185. // Box [C + vouch + metadata](C'->S')
  186. memcpy (initiate + 113, &initiate_box[crypto_box_BOXZEROBYTES],
  187. 128 + metadata_length_ + crypto_box_BOXZEROBYTES);
  188. return 0;
  189. }
  190. static bool is_handshake_command_welcome (const uint8_t *msg_data_,
  191. const size_t msg_size_)
  192. {
  193. return is_handshake_command (msg_data_, msg_size_, "\7WELCOME");
  194. }
  195. static bool is_handshake_command_ready (const uint8_t *msg_data_,
  196. const size_t msg_size_)
  197. {
  198. return is_handshake_command (msg_data_, msg_size_, "\5READY");
  199. }
  200. static bool is_handshake_command_error (const uint8_t *msg_data_,
  201. const size_t msg_size_)
  202. {
  203. return is_handshake_command (msg_data_, msg_size_, "\5ERROR");
  204. }
  205. // non-static functions
  206. curve_client_tools_t (
  207. const uint8_t (&curve_public_key_)[crypto_box_PUBLICKEYBYTES],
  208. const uint8_t (&curve_secret_key_)[crypto_box_SECRETKEYBYTES],
  209. const uint8_t (&curve_server_key_)[crypto_box_PUBLICKEYBYTES])
  210. {
  211. int rc;
  212. memcpy (public_key, curve_public_key_, crypto_box_PUBLICKEYBYTES);
  213. memcpy (secret_key, curve_secret_key_, crypto_box_SECRETKEYBYTES);
  214. memcpy (server_key, curve_server_key_, crypto_box_PUBLICKEYBYTES);
  215. // Generate short-term key pair
  216. memset (cn_secret, 0, crypto_box_SECRETKEYBYTES);
  217. memset (cn_public, 0, crypto_box_PUBLICKEYBYTES);
  218. rc = crypto_box_keypair (cn_public, cn_secret);
  219. zmq_assert (rc == 0);
  220. }
  221. int produce_hello (void *data_, const uint64_t cn_nonce_) const
  222. {
  223. return produce_hello (data_, server_key, cn_nonce_, cn_public,
  224. cn_secret);
  225. }
  226. int process_welcome (const uint8_t *msg_data_,
  227. size_t msg_size_,
  228. uint8_t *cn_precom_)
  229. {
  230. return process_welcome (msg_data_, msg_size_, server_key, cn_secret,
  231. cn_server, cn_cookie, cn_precom_);
  232. }
  233. int produce_initiate (void *data_,
  234. size_t size_,
  235. const uint64_t cn_nonce_,
  236. const uint8_t *metadata_plaintext_,
  237. const size_t metadata_length_) const
  238. {
  239. return produce_initiate (data_, size_, cn_nonce_, server_key,
  240. public_key, secret_key, cn_public, cn_secret,
  241. cn_server, cn_cookie, metadata_plaintext_,
  242. metadata_length_);
  243. }
  244. // Our public key (C)
  245. uint8_t public_key[crypto_box_PUBLICKEYBYTES];
  246. // Our secret key (c)
  247. uint8_t secret_key[crypto_box_SECRETKEYBYTES];
  248. // Our short-term public key (C')
  249. uint8_t cn_public[crypto_box_PUBLICKEYBYTES];
  250. // Our short-term secret key (c')
  251. uint8_t cn_secret[crypto_box_SECRETKEYBYTES];
  252. // Server's public key (S)
  253. uint8_t server_key[crypto_box_PUBLICKEYBYTES];
  254. // Server's short-term public key (S')
  255. uint8_t cn_server[crypto_box_PUBLICKEYBYTES];
  256. // Cookie received from server
  257. uint8_t cn_cookie[16 + 80];
  258. private:
  259. template <size_t N>
  260. static bool is_handshake_command (const uint8_t *msg_data_,
  261. const size_t msg_size_,
  262. const char (&prefix_)[N])
  263. {
  264. return msg_size_ >= (N - 1) && !memcmp (msg_data_, prefix_, N - 1);
  265. }
  266. };
  267. }
  268. #endif
  269. #endif