| 1 | // Copyright (c) 2006-2013, Andrey N. Sabelnikov, www.sabelnikov.net |
| 2 | // All rights reserved. |
| 3 | // |
| 4 | // Redistribution and use in source and binary forms, with or without |
| 5 | // modification, are permitted provided that the following conditions are met: |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above copyright |
| 9 | // notice, this list of conditions and the following disclaimer in the |
| 10 | // documentation and/or other materials provided with the distribution. |
| 11 | // * Neither the name of the Andrey N. Sabelnikov nor the |
| 12 | // names of its contributors may be used to endorse or promote products |
| 13 | // derived from this software without specific prior written permission. |
| 14 | // |
| 15 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 16 | // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 17 | // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 18 | // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER BE LIABLE FOR ANY |
| 19 | // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 20 | // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 21 | // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 22 | // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 23 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 24 | // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 | // |
| 26 | |
| 27 | |
| 28 | |
| 29 | #ifndef _NET_SSL_H |
| 30 | #define _NET_SSL_H |
| 31 | |
| 32 | #include <chrono> |
| 33 | #include <stdint.h> |
| 34 | #include <string> |
| 35 | #include <vector> |
| 36 | #include <boost/utility/string_ref.hpp> |
| 37 | #include <boost/asio/io_context.hpp> |
| 38 | #include <boost/asio/ip/tcp.hpp> |
| 39 | #include <boost/asio/ssl.hpp> |
| 40 | #include <boost/filesystem/path.hpp> |
| 41 | #include <boost/system/error_code.hpp> |
| 42 | |
| 43 | #define SSL_FINGERPRINT_SIZE 32 |
| 44 | |
| 45 | namespace epee |
| 46 | { |
| 47 | namespace net_utils |
| 48 | { |
| 49 | enum class ssl_support_t: uint8_t { |
| 50 | e_ssl_support_disabled, |
| 51 | e_ssl_support_enabled, |
| 52 | e_ssl_support_autodetect, |
| 53 | }; |
| 54 | |
| 55 | enum class ssl_verification_t : uint8_t |
| 56 | { |
| 57 | none = 0, //!< Do not verify peer. |
| 58 | system_ca, //!< Verify peer via system ca only (do not inspect user certificates) |
| 59 | user_certificates,//!< Verify peer via specific (non-chain) certificate(s) only. |
| 60 | user_ca //!< Verify peer via specific (possibly chain) certificate(s) only. |
| 61 | }; |
| 62 | |
| 63 | struct ssl_authentication_t |
| 64 | { |
| 65 | std::string private_key_path; //!< Private key used for authentication |
| 66 | std::string certificate_path; //!< Certificate used for authentication to peer. |
| 67 | |
| 68 | //! Load `private_key_path` and `certificate_path` into `ssl_context`. |
| 69 | void use_ssl_certificate(boost::asio::ssl::context &ssl_context) const; |
| 70 | }; |
| 71 | |
| 72 | /*! |
| 73 | \note `verification != disabled && support == disabled` is currently |
| 74 | "allowed" via public interface but obviously invalid configuation. |
| 75 | */ |
| 76 | class ssl_options_t |
| 77 | { |
| 78 | // force sorted behavior in private |
| 79 | std::vector<std::vector<std::uint8_t>> fingerprints_; |
| 80 | |
| 81 | public: |
| 82 | std::string ca_path; |
| 83 | ssl_authentication_t auth; |
| 84 | ssl_support_t support; |
| 85 | ssl_verification_t verification; |
| 86 | |
| 87 | //! Verification is set to system ca unless SSL is disabled. |
| 88 | ssl_options_t(ssl_support_t support) |
| 89 | : fingerprints_(), |
| 90 | ca_path(), |
| 91 | auth(), |
| 92 | support(support), |
| 93 | verification(support == ssl_support_t::e_ssl_support_disabled ? ssl_verification_t::none : ssl_verification_t::system_ca) |
| 94 | {} |
| 95 | |
| 96 | //! Provide user fingerprints and/or ca path. Enables SSL and user_certificate verification |
| 97 | ssl_options_t(std::vector<std::vector<std::uint8_t>> fingerprints, std::string ca_path); |
| 98 | |
| 99 | ssl_options_t(const ssl_options_t&) = default; |
| 100 | ssl_options_t(ssl_options_t&&) = default; |
| 101 | |
| 102 | ssl_options_t& operator=(const ssl_options_t&) = default; |
| 103 | ssl_options_t& operator=(ssl_options_t&&) = default; |
| 104 | |
| 105 | //! \return False iff ssl is disabled, otherwise true. |
| 106 | explicit operator bool() const noexcept { return support != ssl_support_t::e_ssl_support_disabled; } |
| 107 | |
| 108 | //! \return True if `host` can be verified using `this` configuration WITHOUT system "root" CAs. |
| 109 | bool has_strong_verification(boost::string_ref host) const noexcept; |
| 110 | |
| 111 | //! Search against internal fingerprints. Always false if `behavior() != user_certificate_check`. |
| 112 | bool has_fingerprint(boost::asio::ssl::verify_context &ctx) const; |
| 113 | |
| 114 | //! configure ssl_stream handshake verification |
| 115 | void configure( |
| 116 | boost::asio::ssl::stream<boost::asio::ip::tcp::socket> &socket, |
| 117 | boost::asio::ssl::stream_base::handshake_type type, |
| 118 | const std::string& host = {}) const; |
| 119 | boost::asio::ssl::context create_context() const; |
| 120 | |
| 121 | /*! \note If `this->support == autodetect && this->verification != none`, |
| 122 | then the handshake will not fail when peer verification fails. The |
| 123 | assumption is that a re-connect will be attempted, so a warning is |
| 124 | logged instead of failure. |
| 125 | |
| 126 | \note It is strongly encouraged that clients using `system_ca` |
| 127 | verification provide a non-empty `host` for rfc2818 verification. |
| 128 | |
| 129 | \param io_context associated with `socket`. |
| 130 | \param socket Used in SSL handshake and verification |
| 131 | \param type Client or server |
| 132 | \param host This parameter is only used when |
| 133 | `type == client && !host.empty()`. The value is sent to the server for |
| 134 | situations where multiple hostnames are being handled by a server. If |
| 135 | `verification == system_ca` the client also does a rfc2818 check to |
| 136 | ensure that the server certificate is to the provided hostname. |
| 137 | |
| 138 | \return True if the SSL handshake completes with peer verification |
| 139 | settings. */ |
| 140 | bool handshake( |
| 141 | boost::asio::io_context& io_context, |
| 142 | boost::asio::ssl::stream<boost::asio::ip::tcp::socket> &socket, |
| 143 | boost::asio::ssl::stream_base::handshake_type type, |
| 144 | boost::asio::const_buffer buffer = {}, |
| 145 | const std::string& host = {}, |
| 146 | std::chrono::milliseconds timeout = std::chrono::seconds(15)) const; |
| 147 | }; |
| 148 | |
| 149 | // https://security.stackexchange.com/questions/34780/checking-client-hello-for-https-classification |
| 150 | constexpr size_t get_ssl_magic_size() { return 9; } |
| 151 | bool is_ssl(const unsigned char *data, size_t len); |
| 152 | bool ssl_support_from_string(ssl_support_t &ssl, boost::string_ref s); |
| 153 | |
| 154 | bool create_ec_ssl_certificate(EVP_PKEY *&pkey, X509 *&cert); |
| 155 | bool create_rsa_ssl_certificate(EVP_PKEY *&pkey, X509 *&cert); |
| 156 | |
| 157 | //! Store private key for `ssl` at `base + ".key"` unencrypted and certificate for `ssl` at `base + ".crt"`. |
| 158 | boost::system::error_code store_ssl_keys(boost::asio::ssl::context& ssl, const boost::filesystem::path& base); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | #endif //_NET_SSL_H |