libzmq  master
ZeroMQ C++ Core Engine (LIBZMQ)
curve_server.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_SERVER_HPP_INCLUDED__
31 #define __ZMQ_CURVE_SERVER_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 || crypto_secretbox_NONCEBYTES != 24 \
49 || crypto_secretbox_ZEROBYTES != 32 \
50 || crypto_secretbox_BOXZEROBYTES != 16
51 # error "CURVE library not built properly"
52 #endif
53 
54 #include "mechanism.hpp"
55 #include "options.hpp"
56 
57 namespace zmq
58 {
59 
60  class msg_t;
61  class session_base_t;
62 
63  class curve_server_t : public mechanism_t
64  {
65  public:
66 
67  curve_server_t (session_base_t *session_,
68  const std::string &peer_address_,
69  const options_t &options_);
70  virtual ~curve_server_t ();
71 
72  // mechanism implementation
73  virtual int next_handshake_command (msg_t *msg_);
74  virtual int process_handshake_command (msg_t *msg_);
75  virtual int encode (msg_t *msg_);
76  virtual int decode (msg_t *msg_);
77  virtual int zap_msg_available ();
78  virtual status_t status () const;
79 
80  private:
81 
82  enum state_t {
83  expect_hello,
84  send_welcome,
85  expect_initiate,
86  expect_zap_reply,
87  send_ready,
88  send_error,
89  error_sent,
90  connected
91  };
92 
93  session_base_t * const session;
94 
95  const std::string peer_address;
96 
97  // Current FSM state
98  state_t state;
99 
100  // Status code as received from ZAP handler
101  std::string status_code;
102 
103  uint64_t cn_nonce;
104  uint64_t cn_peer_nonce;
105 
106  // Our secret key (s)
107  uint8_t secret_key [crypto_box_SECRETKEYBYTES];
108 
109  // Our short-term public key (S')
110  uint8_t cn_public [crypto_box_PUBLICKEYBYTES];
111 
112  // Our short-term secret key (s')
113  uint8_t cn_secret [crypto_box_SECRETKEYBYTES];
114 
115  // Client's short-term public key (C')
116  uint8_t cn_client [crypto_box_PUBLICKEYBYTES];
117 
118  // Key used to produce cookie
119  uint8_t cookie_key [crypto_secretbox_KEYBYTES];
120 
121  // Intermediary buffer used to speed up boxing and unboxing.
122  uint8_t cn_precom [crypto_box_BEFORENMBYTES];
123 
124  int process_hello (msg_t *msg_);
125  int produce_welcome (msg_t *msg_);
126  int process_initiate (msg_t *msg_);
127  int produce_ready (msg_t *msg_);
128  int produce_error (msg_t *msg_) const;
129 
130  void send_zap_request (const uint8_t *key);
131  int receive_and_process_zap_reply ();
132  };
133 
134 }
135 
136 #endif
137 
138 #endif
Definition: address.hpp:35