~ubuntu-branches/ubuntu/gutsy/poco/gutsy

« back to all changes in this revision

Viewing changes to NetSSL_OpenSSL/include/Poco/Net/SecureSocketImpl.h

  • Committer: Bazaar Package Importer
  • Author(s): Krzysztof Burghardt
  • Date: 2007-04-27 18:33:48 UTC
  • Revision ID: james.westby@ubuntu.com-20070427183348-xgnpct0qd6a2ip34
Tags: upstream-1.2.9
ImportĀ upstreamĀ versionĀ 1.2.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// SecureSocketImpl.h
 
3
//
 
4
// $Id: //poco/1.2/NetSSL_OpenSSL/include/Poco/Net/SecureSocketImpl.h#4 $
 
5
//
 
6
// Library: NetSSL_OpenSSL
 
7
// Package: SSLSockets
 
8
// Module:  SecureSocketImpl
 
9
//
 
10
// Definition of the SecureSocketImpl class.
 
11
//
 
12
// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
 
13
// and Contributors.
 
14
//
 
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:
 
21
// 
 
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.
 
28
// 
 
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.
 
36
//
 
37
 
 
38
 
 
39
#ifndef NetSSL_SecureSocketImpl_INCLUDED
 
40
#define NetSSL_SecureSocketImpl_INCLUDED
 
41
 
 
42
 
 
43
#include "Poco/Net/NetSSL.h"
 
44
#include "Poco/Net/SocketImpl.h"
 
45
#include <openssl/bio.h>
 
46
#include <openssl/ssl.h>
 
47
 
 
48
 
 
49
namespace Poco {
 
50
namespace Net {
 
51
 
 
52
 
 
53
class HostEntry;
 
54
 
 
55
 
 
56
class NetSSL_API SecureSocketImpl
 
57
        /// The SocketImpl for SecureStreamSocket.
 
58
{
 
59
public:
 
60
        SecureSocketImpl();
 
61
                /// Creates the SecureSocketImpl.
 
62
 
 
63
        SecureSocketImpl(SSL* _pSSL);
 
64
                /// Creates the SecureSocketImpl.
 
65
 
 
66
        virtual ~SecureSocketImpl();
 
67
                /// Destroys the SecureSocketImpl.
 
68
 
 
69
        SocketImpl* acceptConnection(SocketAddress& clientAddr);
 
70
                /// Get the next completed connection from the
 
71
                /// socket's completed connection queue.
 
72
                ///
 
73
                /// If the queue is empty, waits until a connection
 
74
                /// request completes.
 
75
                ///
 
76
                /// Returns a new TCP socket for the connection
 
77
                /// with the client.
 
78
                ///
 
79
                /// The client socket's address is returned in clientAddr.
 
80
        
 
81
        void connect(const SocketAddress& address);
 
82
                /// Initializes the socket and establishes a connection to 
 
83
                /// the TCP server at the given address.
 
84
                ///
 
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.
 
88
 
 
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.
 
92
 
 
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.
 
97
        
 
98
        void bind(const SocketAddress& address, bool reuseAddress = false);
 
99
                /// Bind a local address to the socket.
 
100
                ///
 
101
                /// This is usually only done when establishing a server
 
102
                /// socket. TCP clients should not bind a socket to a
 
103
                /// specific address.
 
104
                ///
 
105
                /// If reuseAddress is true, sets the SO_REUSEADDR
 
106
                /// socket option.
 
107
                
 
108
        void listen(int backlog = 64);
 
109
                /// Puts the socket into listening state.
 
110
                ///
 
111
                /// The socket becomes a passive socket that
 
112
                /// can accept incoming connection requests.
 
113
                ///
 
114
                /// The backlog argument specifies the maximum
 
115
                /// number of connections that can be queued
 
116
                /// for this socket.
 
117
 
 
118
        void close();
 
119
                /// Close the socket.
 
120
        
 
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.
 
124
                ///
 
125
                /// Returns the number of bytes sent, which may be
 
126
                /// less than the number of bytes specified.
 
127
        
 
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.
 
131
                ///
 
132
                /// Returns the number of bytes received.
 
133
        
 
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.
 
137
                ///
 
138
                /// Returns the number of bytes sent, which may be
 
139
                /// less than the number of bytes specified.
 
140
        
 
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.
 
145
                ///
 
146
                /// Returns the number of bytes received.
 
147
        
 
148
        void sendUrgent(unsigned char data);
 
149
                /// Sends one byte of urgent data through
 
150
                /// the socket.
 
151
                ///
 
152
                /// The data is sent with the MSG_OOB flag.
 
153
                ///
 
154
                /// The preferred way for a socket to receive urgent data
 
155
                /// is by enabling the SO_OOBINLINE option.
 
156
 
 
157
        poco_socket_t sockfd();
 
158
                // Returns the socket.
 
159
 
 
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.
 
166
 
 
167
protected:
 
168
        void setSockfd(poco_socket_t sock);
 
169
                /// Set a socket description iff no socket is already set.
 
170
 
 
171
        void invalidate();
 
172
                /// Invalidate the current socket. Must only be called on closed sockets.
 
173
 
 
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.
 
178
 
 
179
        void connectSSL(const SocketAddress& address);
 
180
                /// Creates and connects an SSL connection. Set _pSSL on success or exception otherwise.
 
181
 
 
182
        void establishTunnel();
 
183
                /// Creates a socket to the proxy and sends CONNECT.
 
184
 
 
185
        static bool containsWildcards(const std::string& commonName);
 
186
                /// Checks if the commonName of a certificate contains wildcards
 
187
 
 
188
        static bool matchByAlias(const std::string& alias, const HostEntry& heData);
 
189
                /// Checks if the alias is contained in heData
 
190
 
 
191
private:
 
192
        SecureSocketImpl(const SecureSocketImpl&);
 
193
        SecureSocketImpl& operator = (const SecureSocketImpl&);
 
194
 
 
195
private:
 
196
        BIO* _pBIO;
 
197
        SSL* _pSSL;
 
198
        SocketImpl   _socket;
 
199
        std::string  _endHost;
 
200
        Poco::UInt16 _endPort;
 
201
};
 
202
 
 
203
 
 
204
//
 
205
// inlines
 
206
//
 
207
inline poco_socket_t SecureSocketImpl::sockfd()
 
208
{
 
209
        return _socket.sockfd();
 
210
}
 
211
 
 
212
 
 
213
inline void SecureSocketImpl::setSockfd(poco_socket_t sock)
 
214
{
 
215
        _socket.setSockfd(sock);
 
216
}
 
217
 
 
218
 
 
219
inline void SecureSocketImpl::invalidate()
 
220
{
 
221
        _socket.invalidate();
 
222
}
 
223
 
 
224
 
 
225
inline void SecureSocketImpl::setTunnelEndPoint(const std::string& endHost, Poco::UInt16 endPort)
 
226
{
 
227
        poco_assert (endPort != 0 && !endHost.empty());
 
228
 
 
229
        _endHost = endHost;
 
230
        _endPort = endPort;
 
231
}
 
232
 
 
233
 
 
234
} } // namespace Poco::Net
 
235
 
 
236
 
 
237
#endif // NetSSL_SecureSocketImpl_INCLUDED