4
// $Id: //poco/1.2/NetSSL_OpenSSL/include/Poco/Net/SecureSocketImpl.h#4 $
6
// Library: NetSSL_OpenSSL
8
// Module: SecureSocketImpl
10
// Definition of the SecureSocketImpl class.
12
// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
15
// Permission is hereby granted, free of charge, to any person or organization
16
// obtaining a copy of the software and accompanying documentation covered by
17
// this license (the "Software") to use, reproduce, display, distribute,
18
// execute, and transmit the Software, and to prepare derivative works of the
19
// Software, and to permit third-parties to whom the Software is furnished to
20
// do so, all subject to the following:
22
// The copyright notices in the Software and this entire statement, including
23
// the above license grant, this restriction and the following disclaimer,
24
// must be included in all copies of the Software, in whole or in part, and
25
// all derivative works of the Software, unless such copies or derivative
26
// works are solely in the form of machine-executable object code generated by
27
// a source language processor.
29
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
31
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
32
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
33
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
34
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
35
// DEALINGS IN THE SOFTWARE.
39
#ifndef NetSSL_SecureSocketImpl_INCLUDED
40
#define NetSSL_SecureSocketImpl_INCLUDED
43
#include "Poco/Net/NetSSL.h"
44
#include "Poco/Net/SocketImpl.h"
45
#include <openssl/bio.h>
46
#include <openssl/ssl.h>
56
class NetSSL_API SecureSocketImpl
57
/// The SocketImpl for SecureStreamSocket.
61
/// Creates the SecureSocketImpl.
63
SecureSocketImpl(SSL* _pSSL);
64
/// Creates the SecureSocketImpl.
66
virtual ~SecureSocketImpl();
67
/// Destroys the SecureSocketImpl.
69
SocketImpl* acceptConnection(SocketAddress& clientAddr);
70
/// Get the next completed connection from the
71
/// socket's completed connection queue.
73
/// If the queue is empty, waits until a connection
74
/// request completes.
76
/// Returns a new TCP socket for the connection
79
/// The client socket's address is returned in clientAddr.
81
void connect(const SocketAddress& address);
82
/// Initializes the socket and establishes a connection to
83
/// the TCP server at the given address.
85
/// Can also be used for UDP sockets. In this case, no
86
/// connection is established. Instead, incoming and outgoing
87
/// packets are restricted to the specified address.
89
void connect(const SocketAddress& address, const Poco::Timespan& timeout);
90
/// Initializes the socket, sets the socket timeout and
91
/// establishes a connection to the TCP server at the given address.
93
void connectNB(const SocketAddress& address);
94
/// Initializes the socket and establishes a connection to
95
/// the TCP server at the given address. Prior to opening the
96
/// connection the socket is set to nonblocking mode.
98
void bind(const SocketAddress& address, bool reuseAddress = false);
99
/// Bind a local address to the socket.
101
/// This is usually only done when establishing a server
102
/// socket. TCP clients should not bind a socket to a
103
/// specific address.
105
/// If reuseAddress is true, sets the SO_REUSEADDR
108
void listen(int backlog = 64);
109
/// Puts the socket into listening state.
111
/// The socket becomes a passive socket that
112
/// can accept incoming connection requests.
114
/// The backlog argument specifies the maximum
115
/// number of connections that can be queued
119
/// Close the socket.
121
int sendBytes(const void* buffer, int length, int flags = 0);
122
/// Sends the contents of the given buffer through
123
/// the socket. Any specified flags are ignored.
125
/// Returns the number of bytes sent, which may be
126
/// less than the number of bytes specified.
128
int receiveBytes(void* buffer, int length, int flags = 0);
129
/// Receives data from the socket and stores it
130
/// in buffer. Up to length bytes are received.
132
/// Returns the number of bytes received.
134
int sendTo(const void* buffer, int length, const SocketAddress& address, int flags = 0);
135
/// Sends the contents of the given buffer through
136
/// the socket to the given address.
138
/// Returns the number of bytes sent, which may be
139
/// less than the number of bytes specified.
141
int receiveFrom(void* buffer, int length, SocketAddress& address, int flags = 0);
142
/// Receives data from the socket and stores it
143
/// in buffer. Up to length bytes are received.
144
/// Stores the address of the sender in address.
146
/// Returns the number of bytes received.
148
void sendUrgent(unsigned char data);
149
/// Sends one byte of urgent data through
152
/// The data is sent with the MSG_OOB flag.
154
/// The preferred way for a socket to receive urgent data
155
/// is by enabling the SO_OOBINLINE option.
157
poco_socket_t sockfd();
158
// Returns the socket.
160
void setTunnelEndPoint(const std::string& endHost, Poco::UInt16 endPort);
161
/// Due to the fact that SSLConnections that run over proxies require
162
/// a different connect phase (ie send an unencrypted HTTP CONNECT before
163
/// establishing, we must inform the socket that is only used as a proxy
164
/// that works as a tunnel to the given endPoint.
165
/// Only call this method on disconnected sockets.
168
void setSockfd(poco_socket_t sock);
169
/// Set a socket description iff no socket is already set.
172
/// Invalidate the current socket. Must only be called on closed sockets.
174
static long postConnectionCheck(bool onServer, SSL* pSSL, const std::string& host);
175
/// PostConnectionCheck to verify that a peer really presented a valid certificate.
176
/// if onserver is false, used by clients to verify that a server is really the one it claims.
177
/// if onserver is true, used by the server to verify that a client is really the one it claims.
179
void connectSSL(const SocketAddress& address);
180
/// Creates and connects an SSL connection. Set _pSSL on success or exception otherwise.
182
void establishTunnel();
183
/// Creates a socket to the proxy and sends CONNECT.
185
static bool containsWildcards(const std::string& commonName);
186
/// Checks if the commonName of a certificate contains wildcards
188
static bool matchByAlias(const std::string& alias, const HostEntry& heData);
189
/// Checks if the alias is contained in heData
192
SecureSocketImpl(const SecureSocketImpl&);
193
SecureSocketImpl& operator = (const SecureSocketImpl&);
199
std::string _endHost;
200
Poco::UInt16 _endPort;
207
inline poco_socket_t SecureSocketImpl::sockfd()
209
return _socket.sockfd();
213
inline void SecureSocketImpl::setSockfd(poco_socket_t sock)
215
_socket.setSockfd(sock);
219
inline void SecureSocketImpl::invalidate()
221
_socket.invalidate();
225
inline void SecureSocketImpl::setTunnelEndPoint(const std::string& endHost, Poco::UInt16 endPort)
227
poco_assert (endPort != 0 && !endHost.empty());
234
} } // namespace Poco::Net
237
#endif // NetSSL_SecureSocketImpl_INCLUDED