1//
2// SecureStreamSocket.cpp
3//
4// Library: NetSSL_OpenSSL
5// Package: SSLSockets
6// Module: SecureStreamSocket
7//
8// Copyright (c) 2006-2010, Applied Informatics Software Engineering GmbH.
9// and Contributors.
10//
11// SPDX-License-Identifier: BSL-1.0
12//
13
14
15#include "Poco/Net/SecureStreamSocket.h"
16#include "Poco/Net/SecureStreamSocketImpl.h"
17#include "Poco/Net/SocketImpl.h"
18#include "Poco/Net/SSLManager.h"
19#include "Poco/Exception.h"
20
21
22using Poco::InvalidArgumentException;
23
24
25namespace Poco {
26namespace Net {
27
28
29SecureStreamSocket::SecureStreamSocket():
30 StreamSocket(new SecureStreamSocketImpl(SSLManager::instance().defaultClientContext()))
31{
32}
33
34
35SecureStreamSocket::SecureStreamSocket(Context::Ptr pContext):
36 StreamSocket(new SecureStreamSocketImpl(pContext))
37{
38}
39
40
41SecureStreamSocket::SecureStreamSocket(Context::Ptr pContext, Session::Ptr pSession):
42 StreamSocket(new SecureStreamSocketImpl(pContext))
43{
44 useSession(pSession);
45}
46
47
48SecureStreamSocket::SecureStreamSocket(const SocketAddress& address):
49 StreamSocket(new SecureStreamSocketImpl(SSLManager::instance().defaultClientContext()))
50{
51 connect(address);
52}
53
54
55SecureStreamSocket::SecureStreamSocket(const SocketAddress& address, const std::string& hostName):
56 StreamSocket(new SecureStreamSocketImpl(SSLManager::instance().defaultClientContext()))
57{
58 static_cast<SecureStreamSocketImpl*>(impl())->setPeerHostName(hostName);
59 connect(address);
60}
61
62
63SecureStreamSocket::SecureStreamSocket(const SocketAddress& address, Context::Ptr pContext):
64 StreamSocket(new SecureStreamSocketImpl(pContext))
65{
66 connect(address);
67}
68
69
70SecureStreamSocket::SecureStreamSocket(const SocketAddress& address, Context::Ptr pContext, Session::Ptr pSession):
71 StreamSocket(new SecureStreamSocketImpl(pContext))
72{
73 useSession(pSession);
74 connect(address);
75}
76
77
78SecureStreamSocket::SecureStreamSocket(const SocketAddress& address, const std::string& hostName, Context::Ptr pContext):
79 StreamSocket(new SecureStreamSocketImpl(pContext))
80{
81 static_cast<SecureStreamSocketImpl*>(impl())->setPeerHostName(hostName);
82 connect(address);
83}
84
85
86SecureStreamSocket::SecureStreamSocket(const SocketAddress& address, const std::string& hostName, Context::Ptr pContext, Session::Ptr pSession):
87 StreamSocket(new SecureStreamSocketImpl(pContext))
88{
89 static_cast<SecureStreamSocketImpl*>(impl())->setPeerHostName(hostName);
90 useSession(pSession);
91 connect(address);
92}
93
94
95SecureStreamSocket::SecureStreamSocket(const Socket& socket):
96 StreamSocket(socket)
97{
98 if (!dynamic_cast<SecureStreamSocketImpl*>(impl()))
99 throw InvalidArgumentException("Cannot assign incompatible socket");
100}
101
102
103SecureStreamSocket::SecureStreamSocket(SocketImpl* pImpl):
104 StreamSocket(pImpl)
105{
106 if (!dynamic_cast<SecureStreamSocketImpl*>(impl()))
107 throw InvalidArgumentException("Cannot assign incompatible socket");
108}
109
110
111SecureStreamSocket::~SecureStreamSocket()
112{
113}
114
115
116SecureStreamSocket& SecureStreamSocket::operator = (const Socket& socket)
117{
118 if (dynamic_cast<SecureStreamSocketImpl*>(socket.impl()))
119 StreamSocket::operator = (socket);
120 else
121 throw InvalidArgumentException("Cannot assign incompatible socket");
122 return *this;
123}
124
125
126bool SecureStreamSocket::havePeerCertificate() const
127{
128 return static_cast<SecureStreamSocketImpl*>(impl())->havePeerCertificate();
129}
130
131
132X509Certificate SecureStreamSocket::peerCertificate() const
133{
134 return static_cast<SecureStreamSocketImpl*>(impl())->peerCertificate();
135}
136
137
138void SecureStreamSocket::setPeerHostName(const std::string& hostName)
139{
140 static_cast<SecureStreamSocketImpl*>(impl())->setPeerHostName(hostName);
141}
142
143
144const std::string& SecureStreamSocket::getPeerHostName() const
145{
146 return static_cast<SecureStreamSocketImpl*>(impl())->getPeerHostName();
147}
148
149
150SecureStreamSocket SecureStreamSocket::attach(const StreamSocket& streamSocket)
151{
152 SecureStreamSocketImpl* pImpl = new SecureStreamSocketImpl(static_cast<StreamSocketImpl*>(streamSocket.impl()), SSLManager::instance().defaultClientContext());
153 SecureStreamSocket result(pImpl);
154 if (pImpl->context()->isForServerUse())
155 pImpl->acceptSSL();
156 else
157 pImpl->connectSSL();
158 return result;
159}
160
161
162SecureStreamSocket SecureStreamSocket::attach(const StreamSocket& streamSocket, Context::Ptr pContext)
163{
164 SecureStreamSocketImpl* pImpl = new SecureStreamSocketImpl(static_cast<StreamSocketImpl*>(streamSocket.impl()), pContext);
165 SecureStreamSocket result(pImpl);
166 if (pImpl->context()->isForServerUse())
167 pImpl->acceptSSL();
168 else
169 pImpl->connectSSL();
170 return result;
171}
172
173
174SecureStreamSocket SecureStreamSocket::attach(const StreamSocket& streamSocket, Context::Ptr pContext, Session::Ptr pSession)
175{
176 SecureStreamSocketImpl* pImpl = new SecureStreamSocketImpl(static_cast<StreamSocketImpl*>(streamSocket.impl()), pContext);
177 SecureStreamSocket result(pImpl);
178 result.useSession(pSession);
179 if (pImpl->context()->isForServerUse())
180 pImpl->acceptSSL();
181 else
182 pImpl->connectSSL();
183 return result;
184}
185
186
187SecureStreamSocket SecureStreamSocket::attach(const StreamSocket& streamSocket, const std::string& peerHostName)
188{
189 SecureStreamSocketImpl* pImpl = new SecureStreamSocketImpl(static_cast<StreamSocketImpl*>(streamSocket.impl()), SSLManager::instance().defaultClientContext());
190 SecureStreamSocket result(pImpl);
191 result.setPeerHostName(peerHostName);
192 if (pImpl->context()->isForServerUse())
193 pImpl->acceptSSL();
194 else
195 pImpl->connectSSL();
196 return result;
197}
198
199
200SecureStreamSocket SecureStreamSocket::attach(const StreamSocket& streamSocket, const std::string& peerHostName, Context::Ptr pContext)
201{
202 SecureStreamSocketImpl* pImpl = new SecureStreamSocketImpl(static_cast<StreamSocketImpl*>(streamSocket.impl()), pContext);
203 SecureStreamSocket result(pImpl);
204 result.setPeerHostName(peerHostName);
205 if (pImpl->context()->isForServerUse())
206 pImpl->acceptSSL();
207 else
208 pImpl->connectSSL();
209 return result;
210}
211
212
213SecureStreamSocket SecureStreamSocket::attach(const StreamSocket& streamSocket, const std::string& peerHostName, Context::Ptr pContext, Session::Ptr pSession)
214{
215 SecureStreamSocketImpl* pImpl = new SecureStreamSocketImpl(static_cast<StreamSocketImpl*>(streamSocket.impl()), pContext);
216 SecureStreamSocket result(pImpl);
217 result.setPeerHostName(peerHostName);
218 result.useSession(pSession);
219 if (pImpl->context()->isForServerUse())
220 pImpl->acceptSSL();
221 else
222 pImpl->connectSSL();
223 return result;
224}
225
226
227Context::Ptr SecureStreamSocket::context() const
228{
229 return static_cast<SecureStreamSocketImpl*>(impl())->context();
230}
231
232
233void SecureStreamSocket::setLazyHandshake(bool flag)
234{
235 static_cast<SecureStreamSocketImpl*>(impl())->setLazyHandshake(flag);
236}
237
238
239bool SecureStreamSocket::getLazyHandshake() const
240{
241 return static_cast<SecureStreamSocketImpl*>(impl())->getLazyHandshake();
242}
243
244
245void SecureStreamSocket::verifyPeerCertificate()
246{
247 static_cast<SecureStreamSocketImpl*>(impl())->verifyPeerCertificate();
248}
249
250
251void SecureStreamSocket::verifyPeerCertificate(const std::string& hostName)
252{
253 static_cast<SecureStreamSocketImpl*>(impl())->verifyPeerCertificate(hostName);
254}
255
256
257int SecureStreamSocket::completeHandshake()
258{
259 return static_cast<SecureStreamSocketImpl*>(impl())->completeHandshake();
260}
261
262
263Session::Ptr SecureStreamSocket::currentSession()
264{
265 return static_cast<SecureStreamSocketImpl*>(impl())->currentSession();
266}
267
268
269void SecureStreamSocket::useSession(Session::Ptr pSession)
270{
271 static_cast<SecureStreamSocketImpl*>(impl())->useSession(pSession);
272}
273
274
275bool SecureStreamSocket::sessionWasReused()
276{
277 return static_cast<SecureStreamSocketImpl*>(impl())->sessionWasReused();
278}
279
280
281void SecureStreamSocket::abort()
282{
283 static_cast<SecureStreamSocketImpl*>(impl())->abort();
284}
285
286
287} } // namespace Poco::Net
288