]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#1661] Checkpoint: did asiolink (but need more UTs)
authorFrancis Dupont <fdupont@isc.org>
Sun, 14 Feb 2021 17:45:26 +0000 (18:45 +0100)
committerFrancis Dupont <fdupont@isc.org>
Wed, 24 Mar 2021 08:09:02 +0000 (09:09 +0100)
src/lib/asiolink/openssl_tls.cc
src/lib/asiolink/openssl_tls.h

index 0ee5e8bcc748b09f921588508f37c14905d75115..407b3a7d3622ee87eb4ba0112eacf215d375d45f 100644 (file)
@@ -45,6 +45,7 @@ TlsContext::TlsContext(TlsRole role)
 
 boost::asio::ssl::context&
 TlsContext::getContext() {
+    ::SSL_CTX_up_ref(context_.native_handle());
     return (context_);
 }
 
index 9c224afcfa468247e62df624212e14e6c367561d..7171dbe8c0a19f89d6d7b029b1ddf5e15cf4c6a5 100644 (file)
@@ -230,6 +230,58 @@ const int STREAM_TRUNCATED = boost::asio::ssl::error::stream_truncated;
 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 OpenSSL TLS stream.
+///
+/// @param callback The callback.
+template <typename Callback>
+class TlsStream : public TlsStreamImpl {
+public:
+
+    /// @brief Constructor.
+    ///
+    /// @param service I/O Service object used to manage the stream.
+    /// @param context 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 Handshake.
+    ///
+    virtual void handshake(Callback& callback) {
+        using namespace boost::asio::ssl;
+        if (role_ == SERVER) {
+            async_handshake(stream_base::server, callback);
+        } else {
+            async_handshake(stream_base::client, 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.
+    virtual TlsCertificate* getPeerCert() {
+        return (::SSL_get_peer_certificate(this->native_handle()));
+    }
+
+    /// @brief The role i.e. client or server.
+    TlsRole role_;
+};
+
 } // namespace asiolink
 } // namespace isc