dodo  0.0.1
A C++ library to create containerized Linux services
tlssocket.hpp
Go to the documentation of this file.
1 /*
2  * This file is part of the dodo library (https://github.com/jmspit/dodo).
3  * Copyright (c) 2019 Jan-Marten Spit.
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, version 3.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 /**
19  * @file tlssocket.hpp
20  * Defines the dodo::network::TLSSocket class.
21  */
22 
23 #ifndef network_tlssocket_hpp
24 #define network_tlssocket_hpp
25 
26 #include <openssl/ssl.h>
27 
28 #include "network/socket.hpp"
29 #include "network/socketparams.hpp"
30 #include "network/tlscontext.hpp"
31 #include "network/x509cert.hpp"
32 
33 namespace dodo::network {
34 
35  /**
36  * Socket for TLS encrypted traffic between trusted endpoints.
37  *
38  * @ref developer_networking
39  */
40  class TLSSocket : public BaseSocket {
41  public:
42 
43  /**
44  * Construct from existing socket file descriptor.
45  * @param socket The socket file descriptor.
46  * @param tlscontext The TLSContext to apply.
47  * @param peer_name The peer name to validate against the peer certificate CN and SubjectAltNames. Only used
48  * when the tlscontext has pvVerifyFQDN set (and possibly when pvCustom is set).
49  */
50  TLSSocket( int socket, TLSContext& tlscontext, const X509Common::SAN &peer_name );
51 
52  /**
53  * Construct from scratch.
54  * @param blocking If true, operate in blocking mode.
55  * @param params The SocketParams to apply.
56  * @param tlscontext The TLSContext to apply.
57  * @param peer_name The peer name to validate against the peer certificate CN and SubjectAltNames. Only used
58  * when the tlscontext has pvVerifyFQDN set (and possibly when pvCustom is set).
59  */
60  TLSSocket( bool blocking,
61  SocketParams params,
62  TLSContext& tlscontext,
63  const X509Common::SAN& peer_name );
64 
65  /**
66  * Destructor.
67  */
68  virtual ~TLSSocket();
69 
70  /**
71  * Send data
72  * @param buf Data to send
73  * @param len Size of data in buf
74  * @param more If true, do not force a send buffer flush, more data will follow
75  * @return The SystemError code.
76  */
77  virtual common::SystemError send( const void* buf, ssize_t len, bool more = false );
78 
79  /**
80  * Receive data
81  * @param buf Buffer to receive in
82  * @param request Max bytes to receive in buf
83  * @param received Actual received bytes
84  * @return The SystemError code.
85  */
86  virtual common::SystemError receive( void* buf, ssize_t request, ssize_t &received );
87 
88  /**
89  * Accept a connection.
90  * @return The SystemError.
91  */
92  virtual TLSSocket* accept();
93 
94 
95  /**
96  * Connect to the Address.
97  * @param address The address to connect to.
98  * @return The SystemError code.
99  */
100  virtual common::SystemError connect( const Address &address );
101 
102  /**
103  * Get the peer's certificate.
104  * @return A pointer to the peer certificate (must not be freed).
105  */
106  X509* getPeerCertificate() const;
107 
108  /**
109  * Identity
110  * @param socket The socket to compare to.
111  * @return True if both sockets are equal and have the same value in socket_.
112  */
113  bool operator==(const TLSSocket& socket ) const { return this->socket_ == socket.socket_; };
114 
115  /**
116  * Ordering
117  * @param socket The socket to compare to.
118  * @return True if this Socket has a smaller socket descriptor than socket.
119  */
120  bool operator<(const TLSSocket& socket ) const { return this->socket_ < socket.socket_; };
121 
122  /**
123  * return the negotiated TLS (SSL) protocol version. This is only meaningfull after a connect returned
124  * SystemError::ecOK.
125  * @return the TLS (SSL) protocol version.
126  */
127  int getTLSProtocolVersion() const {
128  if ( ssl_ ) return SSL_version( ssl_ ); else return 0;
129  }
130 
131  /**
132  * Return the negotiated TLS (SSL) protocol version. This is only meaningful after a connect returned
133  * SystemError::ecOK.
134  * @return the TLS (SSL) protocol version.
135  */
136  std::string getTLSProtocolVersionString() const {
137  if ( ssl_ ) return SSL_get_version( ssl_ ); else return "?";
138  }
139 
140  /**
141  * Return the negotiated cipher name. This is only meaningful after a connect returned
142  * SystemError::ecOK.
143  * @return the TLS (SSL) protocol version.
144  */
145  std::string getTLSCurrentCipherName() const {
146  if ( ssl_ ) return SSL_get_cipher_name( ssl_ ); else return "?";
147  }
148 
149  protected:
150 
151  /**
152  * The SSL object.
153  */
154  SSL* ssl_;
155 
156  /**
157  * The TLSContext
158  */
160 
161  /**
162  * The peer name connected to, for TLS CN and SubjectAltName matching.
163  */
165 
166  };
167 
168 };
169 
170 #endif
dodo::network::TLSSocket::ssl_
SSL * ssl_
The SSL object.
Definition: tlssocket.hpp:154
dodo::network::TLSSocket::operator==
bool operator==(const TLSSocket &socket) const
Identity.
Definition: tlssocket.hpp:113
dodo::network::X509Common::SAN
Subject AltName record.
Definition: x509cert.hpp:70
tlscontext.hpp
socketparams.hpp
dodo::network::TLSSocket::peer_name_
X509Common::SAN peer_name_
The peer name connected to, for TLS CN and SubjectAltName matching.
Definition: tlssocket.hpp:164
dodo::network::TLSContext
TLS security context.
Definition: tlscontext.hpp:50
dodo::network::TLSSocket::getTLSProtocolVersionString
std::string getTLSProtocolVersionString() const
Return the negotiated TLS (SSL) protocol version.
Definition: tlssocket.hpp:136
dodo::network::Address
Generic network Address, supporting ipv4 and ipv6 transparently.
Definition: address.hpp:90
dodo::network::TLSSocket::send
virtual common::SystemError send(const void *buf, ssize_t len, bool more=false)
Send data.
Definition: tlssocket.cpp:105
dodo::network::TLSSocket::tlscontext_
TLSContext & tlscontext_
The TLSContext.
Definition: tlssocket.hpp:159
dodo::network::TLSSocket::connect
virtual common::SystemError connect(const Address &address)
Connect to the Address.
Definition: tlssocket.cpp:57
dodo::network::TLSSocket::receive
virtual common::SystemError receive(void *buf, ssize_t request, ssize_t &received)
Receive data.
Definition: tlssocket.cpp:123
dodo::network::TLSSocket
Socket for TLS encrypted traffic between trusted endpoints.
Definition: tlssocket.hpp:40
dodo::network::SocketParams
Socket parameters - the family (domain), socket type and protocol triplet.
Definition: socketparams.hpp:35
dodo::network::TLSSocket::getTLSCurrentCipherName
std::string getTLSCurrentCipherName() const
Return the negotiated cipher name.
Definition: tlssocket.hpp:145
dodo::network::BaseSocket::socket_
int socket_
The socket file decsriptor.
Definition: basesocket.hpp:503
socket.hpp
dodo::network
Interface for network communication.
Definition: address.hpp:37
dodo::network::TLSSocket::TLSSocket
TLSSocket(int socket, TLSContext &tlscontext, const X509Common::SAN &peer_name)
Construct from existing socket file descriptor.
Definition: tlssocket.cpp:32
dodo::network::TLSSocket::getTLSProtocolVersion
int getTLSProtocolVersion() const
return the negotiated TLS (SSL) protocol version.
Definition: tlssocket.hpp:127
dodo::network::TLSSocket::~TLSSocket
virtual ~TLSSocket()
Destructor.
Definition: tlssocket.cpp:53
dodo::network::TLSSocket::accept
virtual TLSSocket * accept()
Accept a connection.
Definition: tlssocket.cpp:88
dodo::common::SystemError
Linux system error primitive to provide a consistent interface to Linux error codes.
Definition: systemerror.hpp:53
x509cert.hpp
dodo::network::TLSSocket::operator<
bool operator<(const TLSSocket &socket) const
Ordering.
Definition: tlssocket.hpp:120
dodo::network::TLSSocket::getPeerCertificate
X509 * getPeerCertificate() const
Get the peer's certificate.
Definition: tlssocket.cpp:101
dodo::network::BaseSocket
Interface to and common implementation of concrete sockets (Socket, TLSSocket).
Definition: basesocket.hpp:36