1//
2// SecureStreamSocketImpl.cpp
3//
4// Library: NetSSL_OpenSSL
5// Package: SSLSockets
6// Module: SecureStreamSocketImpl
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/SecureStreamSocketImpl.h"
16#include "Poco/Net/SSLException.h"
17#include "Poco/Thread.h"
18
19
20namespace Poco {
21namespace Net {
22
23
24SecureStreamSocketImpl::SecureStreamSocketImpl(Context::Ptr pContext):
25 _impl(new StreamSocketImpl, pContext),
26 _lazyHandshake(false)
27{
28}
29
30
31SecureStreamSocketImpl::SecureStreamSocketImpl(StreamSocketImpl* pStreamSocket, Context::Ptr pContext):
32 _impl(pStreamSocket, pContext),
33 _lazyHandshake(false)
34{
35 pStreamSocket->duplicate();
36 reset(_impl.sockfd());
37}
38
39
40SecureStreamSocketImpl::~SecureStreamSocketImpl()
41{
42 try
43 {
44 reset();
45 }
46 catch (...)
47 {
48 poco_unexpected();
49 }
50}
51
52
53SocketImpl* SecureStreamSocketImpl::acceptConnection(SocketAddress& /*clientAddr*/)
54{
55 throw Poco::InvalidAccessException("Cannot acceptConnection() on a SecureStreamSocketImpl");
56}
57
58
59void SecureStreamSocketImpl::acceptSSL()
60{
61 _impl.acceptSSL();
62}
63
64
65void SecureStreamSocketImpl::connect(const SocketAddress& address)
66{
67 _impl.connect(address, !_lazyHandshake);
68 reset(_impl.sockfd());
69}
70
71
72void SecureStreamSocketImpl::connect(const SocketAddress& address, const Poco::Timespan& timeout)
73{
74 _impl.connect(address, timeout, !_lazyHandshake);
75 reset(_impl.sockfd());
76}
77
78
79void SecureStreamSocketImpl::connectNB(const SocketAddress& address)
80{
81 _impl.connectNB(address);
82 reset(_impl.sockfd());
83}
84
85
86void SecureStreamSocketImpl::connectSSL()
87{
88 _impl.connectSSL(!_lazyHandshake);
89}
90
91
92void SecureStreamSocketImpl::bind(const SocketAddress& address, bool reuseAddress, bool reusePort)
93{
94 _impl.bind(address, reuseAddress, reusePort);
95 reset(_impl.sockfd());
96}
97
98
99void SecureStreamSocketImpl::listen(int /*backlog*/)
100{
101 throw Poco::InvalidAccessException("Cannot listen() on a SecureStreamSocketImpl");
102}
103
104
105void SecureStreamSocketImpl::close()
106{
107 reset();
108 _impl.close();
109}
110
111
112void SecureStreamSocketImpl::abort()
113{
114 reset();
115 _impl.abort();
116}
117
118
119int SecureStreamSocketImpl::sendBytes(const void* buffer, int length, int flags)
120{
121 return _impl.sendBytes(buffer, length, flags);
122}
123
124
125int SecureStreamSocketImpl::receiveBytes(void* buffer, int length, int flags)
126{
127 return _impl.receiveBytes(buffer, length, flags);
128}
129
130
131int SecureStreamSocketImpl::sendTo(const void* /*buffer*/, int /*length*/, const SocketAddress& /*address*/, int /*flags*/)
132{
133 throw Poco::InvalidAccessException("Cannot sendTo() on a SecureStreamSocketImpl");
134}
135
136
137int SecureStreamSocketImpl::receiveFrom(void* /*buffer*/, int /*length*/, SocketAddress& /*address*/, int /*flags*/)
138{
139 throw Poco::InvalidAccessException("Cannot receiveFrom() on a SecureStreamSocketImpl");
140}
141
142
143void SecureStreamSocketImpl::sendUrgent(unsigned char /*data*/)
144{
145 throw Poco::InvalidAccessException("Cannot sendUrgent() on a SecureStreamSocketImpl");
146}
147
148
149int SecureStreamSocketImpl::available()
150{
151 return _impl.available();
152}
153
154
155void SecureStreamSocketImpl::shutdownReceive()
156{
157}
158
159
160void SecureStreamSocketImpl::shutdownSend()
161{
162}
163
164
165void SecureStreamSocketImpl::shutdown()
166{
167 _impl.shutdown();
168}
169
170
171bool SecureStreamSocketImpl::secure() const
172{
173 return true;
174}
175
176
177bool SecureStreamSocketImpl::havePeerCertificate() const
178{
179 X509* pCert = _impl.peerCertificate();
180 if (pCert)
181 {
182 X509_free(pCert);
183 return true;
184 }
185 else return false;
186}
187
188
189X509Certificate SecureStreamSocketImpl::peerCertificate() const
190{
191 X509* pCert = _impl.peerCertificate();
192 if (pCert)
193 return X509Certificate(pCert);
194 else
195 throw SSLException("No certificate available");
196}
197
198
199void SecureStreamSocketImpl::setLazyHandshake(bool flag)
200{
201 _lazyHandshake = flag;
202}
203
204
205bool SecureStreamSocketImpl::getLazyHandshake() const
206{
207 return _lazyHandshake;
208}
209
210
211void SecureStreamSocketImpl::verifyPeerCertificate()
212{
213 _impl.verifyPeerCertificate();
214}
215
216
217void SecureStreamSocketImpl::verifyPeerCertificate(const std::string& hostName)
218{
219 _impl.verifyPeerCertificate(hostName);
220}
221
222
223int SecureStreamSocketImpl::completeHandshake()
224{
225 return _impl.completeHandshake();
226}
227
228
229} } // namespace Poco::Net
230