21 #include <boost/asio/ssl.hpp>
27 inline boost::asio::ssl::stream_base::handshake_type roleToImpl(
TlsRole role) {
29 return (boost::asio::ssl::stream_base::server);
31 return (boost::asio::ssl::stream_base::client);
36 class TlsContext :
public TlsContextBase {
40 virtual ~TlsContext() { }
45 explicit TlsContext(
TlsRole role);
48 boost::asio::ssl::context& getContext();
54 ::SSL_CTX* getNativeContext();
60 virtual bool getCertRequired()
const;
67 virtual void setCertRequired(
bool cert_required);
72 virtual void loadCaFile(
const std::string& ca_file);
77 virtual void loadCaPath(
const std::string& ca_path);
82 virtual void loadCertFile(
const std::string& cert_file);
87 virtual void loadKeyFile(
const std::string& key_file);
93 boost::asio::ssl::context context_;
96 friend class TlsContextBase;
100 typedef boost::asio::ssl::stream<boost::asio::ip::tcp::socket> TlsStreamImpl;
109 template <
typename Callback,
typename TlsStreamImpl>
112 : TlsStreamImpl(service.get_io_service(), context->getContext()),
113 role_(context->getRole()) {
119 template <
typename Callback>
120 class TlsStream :
public TlsStreamBase<Callback, TlsStreamImpl> {
124 typedef TlsStreamBase<Callback, TlsStreamImpl> Base;
132 : Base(service, context) {
136 virtual ~TlsStream() { }
141 virtual void handshake(Callback& callback) {
142 Base::async_handshake(roleToImpl(Base::getRole()), callback);
148 virtual void shutdown(Callback& callback) {
149 Base::async_shutdown(callback);
162 virtual std::string getSubject() {
163 ::X509* cert = ::SSL_get_peer_certificate(this->native_handle());
167 ::X509_NAME *name = ::X509_get_subject_name(cert);
168 int loc = ::X509_NAME_get_index_by_NID(name, NID_commonName, -1);
169 ::X509_NAME_ENTRY* ne = ::X509_NAME_get_entry(name, loc);
174 unsigned char* buf = 0;
175 int len = ::ASN1_STRING_to_UTF8(&buf, ::X509_NAME_ENTRY_get_data(ne));
180 std::string ret(reinterpret_cast<char*>(buf), static_cast<size_t>(len));
196 virtual std::string getIssuer() {
197 ::X509* cert = ::SSL_get_peer_certificate(this->native_handle());
201 ::X509_NAME *name = ::X509_get_issuer_name(cert);
202 int loc = ::X509_NAME_get_index_by_NID(name, NID_commonName, -1);
203 ::X509_NAME_ENTRY* ne = ::X509_NAME_get_entry(name, loc);
208 unsigned char* buf = 0;
209 int len = ::ASN1_STRING_to_UTF8(&buf, ::X509_NAME_ENTRY_get_data(ne));
214 std::string ret(reinterpret_cast<char*>(buf), static_cast<size_t>(len));
222 #ifdef HAVE_STREAM_TRUNCATED_ERROR
223 const int STREAM_TRUNCATED = boost::asio::ssl::error::stream_truncated;
225 const int STREAM_TRUNCATED = ERR_PACK(ERR_LIB_SSL, 0, SSL_R_SHORT_READ);
231 #endif // WITH_OPENSSL
233 #endif // OPENSSL_TLS_H
boost::shared_ptr< TlsContext > TlsContextPtr
The type of shared pointers to TlsContext objects.
Defines the logger used by the top-level component of kea-dhcp-ddns.
TlsStreamBase(IOService &service, TlsContextPtr context)
Constructor.
A wrapper interface for the ASIO library.
TlsRole
Client and server roles.