From aef1335e7647c94496b621a495c490f272aadef1 Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Sat, 10 May 2014 21:53:50 +0000 Subject: [PATCH] add overload to set certificate by buffer, not just by path to a file --- include/libtorrent/torrent.hpp | 3 +++ include/libtorrent/torrent_handle.hpp | 6 +++++ src/torrent.cpp | 35 +++++++++++++++++++++++++++ src/torrent_handle.cpp | 10 ++++++++ 4 files changed, 54 insertions(+) diff --git a/include/libtorrent/torrent.hpp b/include/libtorrent/torrent.hpp index aff747f38..e586fe10c 100644 --- a/include/libtorrent/torrent.hpp +++ b/include/libtorrent/torrent.hpp @@ -881,6 +881,9 @@ namespace libtorrent , std::string const& private_key , std::string const& dh_params , std::string const& passphrase); + void set_ssl_cert_buffer(std::string const& certificate + , std::string const& private_key + , std::string const& dh_params); boost::asio::ssl::context* ssl_ctx() const { return m_ssl_ctx.get(); } #endif diff --git a/include/libtorrent/torrent_handle.hpp b/include/libtorrent/torrent_handle.hpp index 565df3942..c9d9a8aa0 100644 --- a/include/libtorrent/torrent_handle.hpp +++ b/include/libtorrent/torrent_handle.hpp @@ -765,6 +765,9 @@ namespace libtorrent // For SSL torrents, use this to specify a path to a .pem file to use as // this client's certificate. The certificate must be signed by the // certificate in the .torrent file to be valid. + // + // The set_ssl_certificate_buffer() overload takes the actual certificate, + // private key and DH params as strings, rather than paths to files. // // ``cert`` is a path to the (signed) certificate in .pem format // corresponding to this torrent. @@ -792,6 +795,9 @@ namespace libtorrent , std::string const& private_key , std::string const& dh_params , std::string const& passphrase = ""); + void set_ssl_certificate_buffer(std::string const& certificate + , std::string const& private_key + , std::string const& dh_params); // Returns the storage implementation for this torrent. This depends on the // storage contructor function that was passed to add_torrent. diff --git a/src/torrent.cpp b/src/torrent.cpp index 459e61906..39b3e38e9 100644 --- a/src/torrent.cpp +++ b/src/torrent.cpp @@ -4394,6 +4394,41 @@ namespace libtorrent alerts().post_alert(torrent_error_alert(get_handle(), ec)); } } + + void torrent::set_ssl_cert_buffer(std::string const& certificate + , std::string const& private_key + , std::string const& dh_params) + { + if (!m_ssl_ctx) return; + + boost::asio::const_buffer certificate_buf(certificate.c_str(), certificate.size()); + + using boost::asio::ssl::context; + error_code ec; + m_ssl_ctx->use_certificate(certificate_buf, context::pem, ec); + if (ec) + { + if (alerts().should_post()) + alerts().post_alert(torrent_error_alert(get_handle(), ec)); + } + + boost::asio::const_buffer private_key_buf(private_key.c_str(), private_key.size()); + m_ssl_ctx->use_private_key(private_key_buf, context::pem, ec); + if (ec) + { + if (alerts().should_post()) + alerts().post_alert(torrent_error_alert(get_handle(), ec)); + } + + boost::asio::const_buffer dh_params_buf(dh_params.c_str(), dh_params.size()); + m_ssl_ctx->use_tmp_dh(dh_params_buf, ec); + if (ec) + { + if (alerts().should_post()) + alerts().post_alert(torrent_error_alert(get_handle(), ec)); + } + } + #endif void torrent::remove_peer(peer_connection* p) diff --git a/src/torrent_handle.cpp b/src/torrent_handle.cpp index 3295c4840..734b64303 100644 --- a/src/torrent_handle.cpp +++ b/src/torrent_handle.cpp @@ -403,6 +403,16 @@ namespace libtorrent #endif } + void torrent_handle::set_ssl_certificate_buffer( + std::string const& certificate + , std::string const& private_key + , std::string const& dh_params) + { +#ifdef TORRENT_USE_OPENSSL + TORRENT_ASYNC_CALL3(set_ssl_cert_buffer, certificate, private_key, dh_params); +#endif + } + void torrent_handle::save_resume_data(int f) const { TORRENT_ASYNC_CALL1(save_resume_data, f);