libzmq  master
ZeroMQ C++ Core Engine (LIBZMQ)
curve_client.hpp
Go to the documentation of this file.
1 /*
2  Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file
3 
4  This file is part of libzmq, the ZeroMQ core engine in C++.
5 
6  libzmq is free software; you can redistribute it and/or modify it under
7  the terms of the GNU Lesser General Public License (LGPL) as published
8  by the Free Software Foundation; either version 3 of the License, or
9  (at your option) any later version.
10 
11  As a special exception, the Contributors give you permission to link
12  this library with independent modules to produce an executable,
13  regardless of the license terms of these independent modules, and to
14  copy and distribute the resulting executable under terms of your choice,
15  provided that you also meet, for each linked independent module, the
16  terms and conditions of the license of that module. An independent
17  module is a module which is not derived from or based on this library.
18  If you modify this library, you must extend this exception to your
19  version of the library.
20 
21  libzmq is distributed in the hope that it will be useful, but WITHOUT
22  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
23  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
24  License for more details.
25 
26  You should have received a copy of the GNU Lesser General Public License
27  along with this program. If not, see <http://www.gnu.org/licenses/>.
28 */
29 
30 #ifndef __ZMQ_CURVE_CLIENT_HPP_INCLUDED__
31 #define __ZMQ_CURVE_CLIENT_HPP_INCLUDED__
32 
33 #ifdef ZMQ_HAVE_CURVE
34 
35 #include "platform.hpp"
36 
37 #if defined (ZMQ_USE_TWEETNACL)
38 # include "tweetnacl.h"
39 #elif defined (ZMQ_USE_LIBSODIUM)
40 # include "sodium.h"
41 #endif
42 
43 #if crypto_box_NONCEBYTES != 24 \
44 || crypto_box_PUBLICKEYBYTES != 32 \
45 || crypto_box_SECRETKEYBYTES != 32 \
46 || crypto_box_ZEROBYTES != 32 \
47 || crypto_box_BOXZEROBYTES != 16
48 # error "CURVE library not built properly"
49 #endif
50 
51 #include "mechanism.hpp"
52 #include "options.hpp"
53 
54 namespace zmq
55 {
56 
57  class msg_t;
58  class session_base_t;
59 
60  class curve_client_t : public mechanism_t
61  {
62  public:
63 
64  curve_client_t (const options_t &options_);
65  virtual ~curve_client_t ();
66 
67  // mechanism implementation
68  virtual int next_handshake_command (msg_t *msg_);
69  virtual int process_handshake_command (msg_t *msg_);
70  virtual int encode (msg_t *msg_);
71  virtual int decode (msg_t *msg_);
72  virtual status_t status () const;
73 
74  private:
75 
76  enum state_t {
77  send_hello,
78  expect_welcome,
79  send_initiate,
80  expect_ready,
81  error_received,
82  connected
83  };
84 
85  // Current FSM state
86  state_t state;
87 
88  // Our public key (C)
89  uint8_t public_key [crypto_box_PUBLICKEYBYTES];
90 
91  // Our secret key (c)
92  uint8_t secret_key [crypto_box_SECRETKEYBYTES];
93 
94  // Our short-term public key (C')
95  uint8_t cn_public [crypto_box_PUBLICKEYBYTES];
96 
97  // Our short-term secret key (c')
98  uint8_t cn_secret [crypto_box_SECRETKEYBYTES];
99 
100  // Server's public key (S)
101  uint8_t server_key [crypto_box_PUBLICKEYBYTES];
102 
103  // Server's short-term public key (S')
104  uint8_t cn_server [crypto_box_PUBLICKEYBYTES];
105 
106  // Cookie received from server
107  uint8_t cn_cookie [16 + 80];
108 
109  // Intermediary buffer used to speed up boxing and unboxing.
110  uint8_t cn_precom [crypto_box_BEFORENMBYTES];
111 
112  // Nonce
113  uint64_t cn_nonce;
114  uint64_t cn_peer_nonce;
115 
116  int produce_hello (msg_t *msg_);
117  int process_welcome (const uint8_t *cmd_data, size_t data_size);
118  int produce_initiate (msg_t *msg_);
119  int process_ready (const uint8_t *cmd_data, size_t data_size);
120  int process_error (const uint8_t *cmd_data, size_t data_size);
121  };
122 
123 }
124 
125 #endif
126 
127 #endif
Definition: address.hpp:35