| 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 | throw Poco::InvalidAccessException("Cannot bind() a SecureStreamSocketImpl" ); |
| 95 | } |
| 96 | |
| 97 | |
| 98 | void SecureStreamSocketImpl::listen(int backlog) |
| 99 | { |
| 100 | throw Poco::InvalidAccessException("Cannot listen() on a SecureStreamSocketImpl" ); |
| 101 | } |
| 102 | |
| 103 | |
| 104 | void SecureStreamSocketImpl::close() |
| 105 | { |
| 106 | reset(); |
| 107 | _impl.close(); |
| 108 | } |
| 109 | |
| 110 | |
| 111 | void SecureStreamSocketImpl::abort() |
| 112 | { |
| 113 | reset(); |
| 114 | _impl.abort(); |
| 115 | } |
| 116 | |
| 117 | |
| 118 | int SecureStreamSocketImpl::sendBytes(const void* buffer, int length, int flags) |
| 119 | { |
| 120 | return _impl.sendBytes(buffer, length, flags); |
| 121 | } |
| 122 | |
| 123 | |
| 124 | int SecureStreamSocketImpl::receiveBytes(void* buffer, int length, int flags) |
| 125 | { |
| 126 | return _impl.receiveBytes(buffer, length, flags); |
| 127 | } |
| 128 | |
| 129 | |
| 130 | int SecureStreamSocketImpl::sendTo(const void* buffer, int length, const SocketAddress& address, int flags) |
| 131 | { |
| 132 | throw Poco::InvalidAccessException("Cannot sendTo() on a SecureStreamSocketImpl" ); |
| 133 | } |
| 134 | |
| 135 | |
| 136 | int SecureStreamSocketImpl::receiveFrom(void* buffer, int length, SocketAddress& address, int flags) |
| 137 | { |
| 138 | throw Poco::InvalidAccessException("Cannot receiveFrom() on a SecureStreamSocketImpl" ); |
| 139 | } |
| 140 | |
| 141 | |
| 142 | void SecureStreamSocketImpl::sendUrgent(unsigned char data) |
| 143 | { |
| 144 | throw Poco::InvalidAccessException("Cannot sendUrgent() on a SecureStreamSocketImpl" ); |
| 145 | } |
| 146 | |
| 147 | |
| 148 | int SecureStreamSocketImpl::available() |
| 149 | { |
| 150 | return _impl.available(); |
| 151 | } |
| 152 | |
| 153 | |
| 154 | void SecureStreamSocketImpl::shutdownReceive() |
| 155 | { |
| 156 | } |
| 157 | |
| 158 | |
| 159 | void SecureStreamSocketImpl::shutdownSend() |
| 160 | { |
| 161 | } |
| 162 | |
| 163 | |
| 164 | void SecureStreamSocketImpl::shutdown() |
| 165 | { |
| 166 | _impl.shutdown(); |
| 167 | } |
| 168 | |
| 169 | |
| 170 | bool SecureStreamSocketImpl::secure() const |
| 171 | { |
| 172 | return true; |
| 173 | } |
| 174 | |
| 175 | |
| 176 | bool SecureStreamSocketImpl::havePeerCertificate() const |
| 177 | { |
| 178 | X509* pCert = _impl.peerCertificate(); |
| 179 | if (pCert) |
| 180 | { |
| 181 | X509_free(pCert); |
| 182 | return true; |
| 183 | } |
| 184 | else return false; |
| 185 | } |
| 186 | |
| 187 | |
| 188 | X509Certificate SecureStreamSocketImpl::peerCertificate() const |
| 189 | { |
| 190 | X509* pCert = _impl.peerCertificate(); |
| 191 | if (pCert) |
| 192 | return X509Certificate(pCert); |
| 193 | else |
| 194 | throw SSLException("No certificate available" ); |
| 195 | } |
| 196 | |
| 197 | |
| 198 | void SecureStreamSocketImpl::setLazyHandshake(bool flag) |
| 199 | { |
| 200 | _lazyHandshake = flag; |
| 201 | } |
| 202 | |
| 203 | |
| 204 | bool SecureStreamSocketImpl::getLazyHandshake() const |
| 205 | { |
| 206 | return _lazyHandshake; |
| 207 | } |
| 208 | |
| 209 | |
| 210 | void SecureStreamSocketImpl::verifyPeerCertificate() |
| 211 | { |
| 212 | _impl.verifyPeerCertificate(); |
| 213 | } |
| 214 | |
| 215 | |
| 216 | void SecureStreamSocketImpl::verifyPeerCertificate(const std::string& hostName) |
| 217 | { |
| 218 | _impl.verifyPeerCertificate(hostName); |
| 219 | } |
| 220 | |
| 221 | |
| 222 | int SecureStreamSocketImpl::completeHandshake() |
| 223 | { |
| 224 | return _impl.completeHandshake(); |
| 225 | } |
| 226 | |
| 227 | |
| 228 | } } // namespace Poco::Net |
| 229 | |