libDwmCredence-0.1.32
DwmCredencePeer.hh
Go to the documentation of this file.
1//===========================================================================
2// @(#) $DwmPath$
3//===========================================================================
4// Copyright (c) Daniel W. McRobb 2022
5// All rights reserved.
6//
7// Redistribution and use in source and binary forms, with or without
8// modification, are permitted provided that the following conditions
9// are met:
10//
11// 1. Redistributions of source code must retain the above copyright
12// notice, this list of conditions and the following disclaimer.
13// 2. Redistributions in binary form must reproduce the above copyright
14// notice, this list of conditions and the following disclaimer in the
15// documentation and/or other materials provided with the distribution.
16// 3. The names of the authors and copyright holders may not be used to
17// endorse or promote products derived from this software without
18// specific prior written permission.
19//
20// IN NO EVENT SHALL DANIEL W. MCROBB BE LIABLE TO ANY PARTY FOR
21// DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES,
22// INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE,
23// EVEN IF DANIEL W. MCROBB HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
24// DAMAGE.
25//
26// THE SOFTWARE PROVIDED HEREIN IS ON AN "AS IS" BASIS, AND
27// DANIEL W. MCROBB HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT,
28// UPDATES, ENHANCEMENTS, OR MODIFICATIONS. DANIEL W. MCROBB MAKES NO
29// REPRESENTATIONS AND EXTENDS NO WARRANTIES OF ANY KIND, EITHER
30// IMPLIED OR EXPRESS, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
31// WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE,
32// OR THAT THE USE OF THIS SOFTWARE WILL NOT INFRINGE ANY PATENT,
33// TRADEMARK OR OTHER RIGHTS.
34//===========================================================================
35
36//---------------------------------------------------------------------------
40//---------------------------------------------------------------------------
41
42#ifndef _DWMCREDENCEPEER_HH_
43#define _DWMCREDENCEPEER_HH_
44
45#include <chrono>
46#include <boost/asio.hpp>
47
48#include "DwmStreamIO.hh"
49#include "DwmSysLogger.hh"
54
55namespace Dwm {
56
57 namespace Credence {
58
59 //------------------------------------------------------------------------
66 //------------------------------------------------------------------------
67 class Peer
68 {
69 public:
70 //----------------------------------------------------------------------
72 //----------------------------------------------------------------------
74
75 //----------------------------------------------------------------------
79 //----------------------------------------------------------------------
80 void SetKeyExchangeTimeout(std::chrono::milliseconds ms);
81
82 //----------------------------------------------------------------------
87 //----------------------------------------------------------------------
88 bool Accept(boost::asio::ip::tcp::socket && s);
89
90 //----------------------------------------------------------------------
95 //----------------------------------------------------------------------
96 bool Accept(boost::asio::local::stream_protocol::socket && s);
97
98 //----------------------------------------------------------------------
102 //----------------------------------------------------------------------
103 bool Connect(const std::string & host, uint16_t port,
104 std::chrono::milliseconds timeOut = std::chrono::milliseconds(5000));
105
106 //----------------------------------------------------------------------
110 //----------------------------------------------------------------------
111 bool Connect(const std::string & path,
112 std::chrono::milliseconds timeOut = std::chrono::milliseconds(5000));
113
114 //----------------------------------------------------------------------
118 //----------------------------------------------------------------------
119 void SetIdExchangeTimeout(std::chrono::milliseconds ms);
120
121 //----------------------------------------------------------------------
129 //----------------------------------------------------------------------
130 bool Authenticate(const KeyStash & keyStash,
131 const KnownKeys & knownKeys);
132
133 //----------------------------------------------------------------------
135 //----------------------------------------------------------------------
136 const std::string & Id() const { return _theirId; }
137
138 //----------------------------------------------------------------------
144 //----------------------------------------------------------------------
145 template <typename T>
146 bool Send(const T & msg)
147 {
148 bool rc = false;
149 if (_xos) {
150 if (StreamIO::Write(*_xos, msg)) {
151 if (_xos->flush()) {
152 rc = true;
153 }
154 else {
155 Syslog(LOG_ERR, "Failed to flush encrypted stream to %s",
156 EndPointString().c_str());
157 }
158 }
159 else {
160 Syslog(LOG_ERR, "Failed to send message to %s",
161 EndPointString().c_str());
162 }
163 }
164 else {
165 Syslog(LOG_ERR, "Invalid encrypted output stream");
166 }
167 return rc;
168 }
169
170 //----------------------------------------------------------------------
176 //----------------------------------------------------------------------
177 template <typename T>
178 bool Receive(T & msg)
179 {
180 bool rc = false;
181 if (_xis) {
182 if (StreamIO::Read(*_xis, msg)) {
183 rc = true;
184 }
185 else {
186 Syslog(LOG_DEBUG, "Failed to receive message from %s",
187 EndPointString().c_str());
188 }
189 }
190 else {
191 Syslog(LOG_ERR, "Invalid encrypted input stream");
192 }
193 return rc;
194 }
195
196 //----------------------------------------------------------------------
198 //----------------------------------------------------------------------
199 bool ReceiveWouldBlock(size_t numBytes);
200
201 //----------------------------------------------------------------------
203 //----------------------------------------------------------------------
205
206 //----------------------------------------------------------------------
209 //----------------------------------------------------------------------
210 std::string EndPointString() const;
211
212 private:
213 std::chrono::milliseconds _keyExchangeTimeout;
214 std::chrono::milliseconds _idExchangeTimeout;
215 boost::asio::ip::tcp::endpoint _endPoint;
216 boost::asio::local::stream_protocol::endpoint _lendPoint;
217 std::string _theirId;
218 std::string _agreedKey;
219 std::unique_ptr<boost::asio::ip::tcp::iostream> _ios;
220 std::unique_ptr<boost::asio::local::stream_protocol::iostream> _lios;
221 std::unique_ptr<XChaCha20Poly1305::Istream> _xis;
222 std::unique_ptr<XChaCha20Poly1305::Ostream> _xos;
223 };
224
225 } // namespace Credence
226
227} // namespace Dwm
228
229#endif // _DWMCREDENCEPEER_HH_
Dwm::Credence::KeyStash class declaration.
Dwm::Credence::KnownKeys class declaration.
Dwm::Credence::XChaCha20Poly1305::Istream class declaration.
Dwm::Credence::XChaCha20Poly1305::Ostream class declaration.
#define Syslog(...)
Encapsulates storage of an Ed25519KeyPair in a filesystem.
Definition DwmCredenceKeyStash.hh:56
Encapsulates the storage of a set of known public keys.
Definition DwmCredenceKnownKeys.hh:60
Encapsulate a network peer.
Definition DwmCredencePeer.hh:68
bool Send(const T &msg)
Sends the given msg to the peer.
Definition DwmCredencePeer.hh:146
bool Connect(const std::string &host, uint16_t port, std::chrono::milliseconds timeOut=std::chrono::milliseconds(5000))
Used by a client to connect to host at the given port, waiting timeOut for success.
void SetIdExchangeTimeout(std::chrono::milliseconds ms)
Sets the time we'll wait for the peer to send its ID during authentication.
void SetKeyExchangeTimeout(std::chrono::milliseconds ms)
Sets the time we'll wait for the peer to send its public key.
Peer()
Default constructor.
bool Accept(boost::asio::ip::tcp::socket &&s)
Used by a server to accept a new connection on the given TCP socket s.
bool Receive(T &msg)
Receives the given msg from the peer.
Definition DwmCredencePeer.hh:178
const std::string & Id() const
If Authenticate() was used, returns the peer's identifier.
Definition DwmCredencePeer.hh:136
bool Accept(boost::asio::local::stream_protocol::socket &&s)
Used by a server to accept a new connection on the given UNIX domain socket s.
std::string EndPointString() const
Returns a string representation of the peer endpoint, in 'addr:port' form.
bool Connect(const std::string &path, std::chrono::milliseconds timeOut=std::chrono::milliseconds(5000))
Used by a client to connect to a UNIX domain socket at the given path, waiting timeOut for success.
bool Authenticate(const KeyStash &keyStash, const KnownKeys &knownKeys)
Using the given keyStash and knownKeys, authenticate our identity to the peer and verify the peer's i...
bool ReceiveWouldBlock(size_t numBytes)
Returns true if a Receive() of numBytes or greater would block.
void Disconnect()
Disconnects the peer.
static std::istream & Read(std::istream &is, char &c)
static std::ostream & Write(std::ostream &os, char c)