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