const int STREAM_TRUNCATED = ERR_PACK(ERR_LIB_SSL, 0, SSL_R_SHORT_READ);
#endif
-/// @brief The type of underlying TLS streams.
-typedef boost::asio::ssl::stream<boost::asio::ip::tcp::socket> TlsStreamImpl;
-
-/// @brief The type of X509 certificates.
-typedef ::X509 TlsCertificate;
-
-/// @brief TlsStreamBase constructor.
-///
-/// @param Callback The type of callbacks.
-/// @param TlsStreamImpl The type of underlying TLS streams.
-/// @param TlsCertificate The type of X509 certificates.
-template <typename Callback, typename TlsStreamImpl, typename TlsCertificate>
-TlsStreamBase<Callback, TlsStreamImpl, TlsCertificate>::
-TlsStreamBase(IOService& service, TlsContextPtr context)
- : TlsStreamImpl(service.get_io_service(), context->getContext()),
- role_(context->getRole()) {
-}
-
-/// @brief OpenSSL TLS stream.
-///
-/// @param callback The callback.
-template <typename Callback>
-class TlsStream : public TlsStreamBase<Callback, TlsStreamImpl, TlsCertificate> {
-public:
-
- /// @brief Type of the base.
- typedef TlsStreamBase<Callback, TlsStreamImpl, TlsCertificate> Base;
-
- /// @brief Constructor.
- ///
- /// @param service I/O Service object used to manage the stream.
- /// @param context Pointer to the TLS context.
- /// @note The caller must not provide a null pointer to the TLS context.
- TlsStream(IOService& service, TlsContextPtr context)
- : TlsStreamImpl(service.get_io_service(), context->getContext()),
- role_(context->role_) {
- }
-
- /// @brief Destructor.
- virtual ~TlsStream() { }
-
- /// @brief TLS Handshake.
- ///
- /// @param callback Callback object.
- virtual void handshake(Callback& callback) {
- Base::async_handshake(roleToImpl(Base::getRole()), callback);
- }
-
- /// @brief TLS shutdown.
- ///
- /// @param callback Callback object.
- virtual void shutdown(Callback& callback) {
- Base::async_shutdown(callback);
- }
-
- /// @brief Clear the SSL object.
- virtual void clear() {
- static_cast<void>(::SSL_clear(this->native_handle()));
- }
-
- /// @brief Return the peer certificate.
- ///
- /// @note The native_handle() method is used so it can't be made const.
- /// @note Do not forget to free it when no longer used.
- virtual TlsCertificate* getPeerCert() {
- return (::SSL_get_peer_certificate(this->native_handle()));
- }
-
- /// @break Return the commonName part of the subjectName of
- /// the peer certificate.
- ///
- /// First commonName when there are more than one, in UTF-8.
- ///
- /// @return The commonName part of the subjectName or the empty string.
- virtual std::string getSubject() {
- TlsCertificate* cert = getPeerCert();
- if (!cert) {
- return ("");
- }
- ::X509_NAME *name = ::X509_get_subject_name(cert);
- int loc = ::X509_NAME_get_index_by_NID(name, NID_commonName, -1);
- ::X509_NAME_ENTRY* ne = ::X509_NAME_get_entry(name, loc);
- if (!ne) {
- ::X509_free(cert);
- return ("");
- }
- unsigned char* buf = 0;
- int len = ::ASN1_STRING_to_UTF8(&buf, ::X509_NAME_ENTRY_get_data(ne));
- if (len < 0) {
- ::X509_free(cert);
- return ("");
- }
- std::string ret(reinterpret_cast<char*>(buf), static_cast<size_t>(len));
- ::OPENSSL_free(buf);
- ::X509_free(cert);
- return (ret);
- }
-
- /// @break Return the commonName part of the issuerName of
- /// the peer certificate.
- ///
- /// First commonName when there are more than one, in UTF-8.
- ///
- /// @return The commonName part of the issuerName or the empty string.
- virtual std::string getIssuer() {
- TlsCertificate* cert = getPeerCert();
- if (!cert) {
- return ("");
- }
- ::X509_NAME *name = ::X509_get_issuer_name(cert);
- int loc = ::X509_NAME_get_index_by_NID(name, NID_commonName, -1);
- ::X509_NAME_ENTRY* ne = ::X509_NAME_get_entry(name, loc);
- if (!ne) {
- ::X509_free(cert);
- return ("");
- }
- unsigned char* buf = 0;
- int len = ::ASN1_STRING_to_UTF8(&buf, ::X509_NAME_ENTRY_get_data(ne));
- if (len < 0) {
- ::X509_free(cert);
- return ("");
- }
- std::string ret(reinterpret_cast<char*>(buf), static_cast<size_t>(len));
- ::OPENSSL_free(buf);
- ::X509_free(cert);
- return (ret);
- }
-};
-
-// Stream truncated error code.
-const int STREAM_TRUNCATED = boost::asio::ssl::error::stream_truncated;
-
} // namespace asiolink
} // namespace isc