Monero
Loading...
Searching...
No Matches
net_ssl.h
Go to the documentation of this file.
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 <functional>
34#include <stdint.h>
35#include <string>
36#include <vector>
37#include <boost/utility/string_ref.hpp>
38#include <boost/asio/io_context.hpp>
39#include <boost/asio/ip/tcp.hpp>
40#include <boost/asio/ssl.hpp>
41#include <boost/filesystem/path.hpp>
42#include <boost/system/error_code.hpp>
43
44#define SSL_FINGERPRINT_SIZE 32
45
46namespace epee
47{
48namespace net_utils
49{
55
63
65 {
66 std::string private_key_path;
67 std::string certificate_path;
68
70 void use_ssl_certificate(boost::asio::ssl::context &ssl_context) const;
71 };
72
78 {
79 // force sorted behavior in private
80 std::vector<std::vector<std::uint8_t>> fingerprints_;
81
82 public:
83 std::string ca_path;
87
96
98 ssl_options_t(std::vector<std::vector<std::uint8_t>> fingerprints, std::string ca_path);
99
100 ssl_options_t(const ssl_options_t&) = default;
102
105
107 explicit operator bool() const noexcept { return support != ssl_support_t::e_ssl_support_disabled; }
108
110 bool has_strong_verification(boost::string_ref host) const noexcept;
111
113 bool has_fingerprint(boost::asio::ssl::verify_context &ctx) const;
114
116 void configure(
117 boost::asio::ssl::stream<boost::asio::ip::tcp::socket> &socket,
118 boost::asio::ssl::stream_base::handshake_type type,
119 const std::string& host = {}) const;
120 boost::asio::ssl::context create_context() const;
121
143 bool handshake(
144 boost::asio::io_context& io_context,
145 boost::asio::ssl::stream<boost::asio::ip::tcp::socket> &socket,
146 boost::asio::ssl::stream_base::handshake_type type,
147 boost::asio::const_buffer buffer = {},
148 const std::string& host = {},
149 std::chrono::milliseconds timeout = std::chrono::seconds(15),
150 const std::function<bool()>& abort_requested = nullptr) const;
151 };
152
153 // https://security.stackexchange.com/questions/34780/checking-client-hello-for-https-classification
154 constexpr size_t get_ssl_magic_size() { return 9; }
155 bool is_ssl(const unsigned char *data, size_t len);
156 bool ssl_support_from_string(ssl_support_t &ssl, boost::string_ref s);
157
158 bool create_ec_ssl_certificate(EVP_PKEY *&pkey, X509 *&cert);
159 bool create_rsa_ssl_certificate(EVP_PKEY *&pkey, X509 *&cert);
160
162 boost::system::error_code store_ssl_keys(boost::asio::ssl::context& ssl, const boost::filesystem::path& base);
163}
164}
165
166#endif //_NET_SSL_H
#define s(x, c)
Definition aesb.c:47
boost::asio::ssl::context create_context() const
Definition net_ssl.cpp:303
void configure(boost::asio::ssl::stream< boost::asio::ip::tcp::socket > &socket, boost::asio::ssl::stream_base::handshake_type type, const std::string &host={}) const
configure ssl_stream handshake verification
Definition net_ssl.cpp:500
ssl_verification_t verification
Definition net_ssl.h:86
std::string ca_path
Definition net_ssl.h:83
ssl_options_t(const ssl_options_t &)=default
ssl_options_t(ssl_support_t support)
Verification is set to system ca unless SSL is disabled.
Definition net_ssl.h:89
bool has_strong_verification(boost::string_ref host) const noexcept
Definition net_ssl.cpp:443
ssl_options_t & operator=(const ssl_options_t &)=default
std::vector< std::vector< std::uint8_t > > fingerprints_
Definition net_ssl.h:80
bool handshake(boost::asio::io_context &io_context, boost::asio::ssl::stream< boost::asio::ip::tcp::socket > &socket, boost::asio::ssl::stream_base::handshake_type type, boost::asio::const_buffer buffer={}, const std::string &host={}, std::chrono::milliseconds timeout=std::chrono::seconds(15), const std::function< bool()> &abort_requested=nullptr) const
Definition net_ssl.cpp:553
ssl_authentication_t auth
Definition net_ssl.h:84
ssl_options_t(ssl_options_t &&)=default
bool has_fingerprint(boost::asio::ssl::verify_context &ctx) const
Search against internal fingerprints. Always false if behavior() != user_certificate_check.
Definition net_ssl.cpp:462
ssl_options_t & operator=(ssl_options_t &&)=default
Definition base.py:1
int bool
Definition hash.h:36
ssl_verification_t
Definition net_ssl.h:57
@ user_certificates
Verify peer via specific (non-chain) certificate(s) only.
Definition net_ssl.h:60
@ none
Do not verify peer.
Definition net_ssl.h:58
@ system_ca
Verify peer via system ca only (do not inspect user certificates).
Definition net_ssl.h:59
@ user_ca
Verify peer via specific (possibly chain) certificate(s) only.
Definition net_ssl.h:61
boost::system::error_code store_ssl_keys(boost::asio::ssl::context &ssl, const boost::filesystem::path &base)
Store private key for ssl at base + ".key" unencrypted and certificate for ssl at base + "....
Definition net_ssl.cpp:694
bool is_ssl(const unsigned char *data, size_t len)
Definition net_ssl.cpp:423
constexpr size_t get_ssl_magic_size()
Definition net_ssl.h:154
bool ssl_support_from_string(ssl_support_t &ssl, boost::string_ref s)
Definition net_ssl.cpp:681
bool create_rsa_ssl_certificate(EVP_PKEY *&pkey, X509 *&cert)
Definition net_ssl.cpp:143
bool create_ec_ssl_certificate(EVP_PKEY *&pkey, X509 *&cert)
ssl_support_t
Definition net_ssl.h:50
@ e_ssl_support_disabled
Definition net_ssl.h:51
@ e_ssl_support_autodetect
Definition net_ssl.h:53
@ e_ssl_support_enabled
Definition net_ssl.h:52
TODO: (mj-xmr) This will be reduced in an another PR.
Definition byte_slice.h:40
Definition support.py:1
unsigned char uint8_t
Definition stdint.h:124
std::string certificate_path
Certificate used for authentication to peer.
Definition net_ssl.h:67
std::string private_key_path
Private key used for authentication.
Definition net_ssl.h:66
void use_ssl_certificate(boost::asio::ssl::context &ssl_context) const
Load private_key_path and certificate_path into ssl_context.
Definition net_ssl.cpp:412
std::string data
Definition base58.cpp:37