early-access version 3088

This commit is contained in:
pineappleEA
2022-11-05 15:35:56 +01:00
parent 4e4fc25ce3
commit b601909c6d
35519 changed files with 5996896 additions and 860 deletions

View File

@@ -0,0 +1,766 @@
//
// ssl/context.hpp
// ~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2022 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef BOOST_ASIO_SSL_CONTEXT_HPP
#define BOOST_ASIO_SSL_CONTEXT_HPP
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
#include <boost/asio/detail/config.hpp>
#include <string>
#include <boost/asio/buffer.hpp>
#include <boost/asio/io_context.hpp>
#include <boost/asio/ssl/context_base.hpp>
#include <boost/asio/ssl/detail/openssl_types.hpp>
#include <boost/asio/ssl/detail/openssl_init.hpp>
#include <boost/asio/ssl/detail/password_callback.hpp>
#include <boost/asio/ssl/detail/verify_callback.hpp>
#include <boost/asio/ssl/verify_mode.hpp>
#include <boost/asio/detail/push_options.hpp>
namespace boost {
namespace asio {
namespace ssl {
class context
: public context_base,
private noncopyable
{
public:
/// The native handle type of the SSL context.
typedef SSL_CTX* native_handle_type;
/// Constructor.
BOOST_ASIO_DECL explicit context(method m);
/// Construct to take ownership of a native handle.
BOOST_ASIO_DECL explicit context(native_handle_type native_handle);
#if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
/// Move-construct a context from another.
/**
* This constructor moves an SSL context from one object to another.
*
* @param other The other context object from which the move will occur.
*
* @note Following the move, the following operations only are valid for the
* moved-from object:
* @li Destruction.
* @li As a target for move-assignment.
*/
BOOST_ASIO_DECL context(context&& other);
/// Move-assign a context from another.
/**
* This assignment operator moves an SSL context from one object to another.
*
* @param other The other context object from which the move will occur.
*
* @note Following the move, the following operations only are valid for the
* moved-from object:
* @li Destruction.
* @li As a target for move-assignment.
*/
BOOST_ASIO_DECL context& operator=(context&& other);
#endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
/// Destructor.
BOOST_ASIO_DECL ~context();
/// Get the underlying implementation in the native type.
/**
* This function may be used to obtain the underlying implementation of the
* context. This is intended to allow access to context functionality that is
* not otherwise provided.
*/
BOOST_ASIO_DECL native_handle_type native_handle();
/// Clear options on the context.
/**
* This function may be used to configure the SSL options used by the context.
*
* @param o A bitmask of options. The available option values are defined in
* the context_base class. The specified options, if currently enabled on the
* context, are cleared.
*
* @throws boost::system::system_error Thrown on failure.
*
* @note Calls @c SSL_CTX_clear_options.
*/
BOOST_ASIO_DECL void clear_options(options o);
/// Clear options on the context.
/**
* This function may be used to configure the SSL options used by the context.
*
* @param o A bitmask of options. The available option values are defined in
* the context_base class. The specified options, if currently enabled on the
* context, are cleared.
*
* @param ec Set to indicate what error occurred, if any.
*
* @note Calls @c SSL_CTX_clear_options.
*/
BOOST_ASIO_DECL BOOST_ASIO_SYNC_OP_VOID clear_options(options o,
boost::system::error_code& ec);
/// Set options on the context.
/**
* This function may be used to configure the SSL options used by the context.
*
* @param o A bitmask of options. The available option values are defined in
* the context_base class. The options are bitwise-ored with any existing
* value for the options.
*
* @throws boost::system::system_error Thrown on failure.
*
* @note Calls @c SSL_CTX_set_options.
*/
BOOST_ASIO_DECL void set_options(options o);
/// Set options on the context.
/**
* This function may be used to configure the SSL options used by the context.
*
* @param o A bitmask of options. The available option values are defined in
* the context_base class. The options are bitwise-ored with any existing
* value for the options.
*
* @param ec Set to indicate what error occurred, if any.
*
* @note Calls @c SSL_CTX_set_options.
*/
BOOST_ASIO_DECL BOOST_ASIO_SYNC_OP_VOID set_options(options o,
boost::system::error_code& ec);
/// Set the peer verification mode.
/**
* This function may be used to configure the peer verification mode used by
* the context.
*
* @param v A bitmask of peer verification modes. See @ref verify_mode for
* available values.
*
* @throws boost::system::system_error Thrown on failure.
*
* @note Calls @c SSL_CTX_set_verify.
*/
BOOST_ASIO_DECL void set_verify_mode(verify_mode v);
/// Set the peer verification mode.
/**
* This function may be used to configure the peer verification mode used by
* the context.
*
* @param v A bitmask of peer verification modes. See @ref verify_mode for
* available values.
*
* @param ec Set to indicate what error occurred, if any.
*
* @note Calls @c SSL_CTX_set_verify.
*/
BOOST_ASIO_DECL BOOST_ASIO_SYNC_OP_VOID set_verify_mode(
verify_mode v, boost::system::error_code& ec);
/// Set the peer verification depth.
/**
* This function may be used to configure the maximum verification depth
* allowed by the context.
*
* @param depth Maximum depth for the certificate chain verification that
* shall be allowed.
*
* @throws boost::system::system_error Thrown on failure.
*
* @note Calls @c SSL_CTX_set_verify_depth.
*/
BOOST_ASIO_DECL void set_verify_depth(int depth);
/// Set the peer verification depth.
/**
* This function may be used to configure the maximum verification depth
* allowed by the context.
*
* @param depth Maximum depth for the certificate chain verification that
* shall be allowed.
*
* @param ec Set to indicate what error occurred, if any.
*
* @note Calls @c SSL_CTX_set_verify_depth.
*/
BOOST_ASIO_DECL BOOST_ASIO_SYNC_OP_VOID set_verify_depth(
int depth, boost::system::error_code& ec);
/// Set the callback used to verify peer certificates.
/**
* This function is used to specify a callback function that will be called
* by the implementation when it needs to verify a peer certificate.
*
* @param callback The function object to be used for verifying a certificate.
* The function signature of the handler must be:
* @code bool verify_callback(
* bool preverified, // True if the certificate passed pre-verification.
* verify_context& ctx // The peer certificate and other context.
* ); @endcode
* The return value of the callback is true if the certificate has passed
* verification, false otherwise.
*
* @throws boost::system::system_error Thrown on failure.
*
* @note Calls @c SSL_CTX_set_verify.
*/
template <typename VerifyCallback>
void set_verify_callback(VerifyCallback callback);
/// Set the callback used to verify peer certificates.
/**
* This function is used to specify a callback function that will be called
* by the implementation when it needs to verify a peer certificate.
*
* @param callback The function object to be used for verifying a certificate.
* The function signature of the handler must be:
* @code bool verify_callback(
* bool preverified, // True if the certificate passed pre-verification.
* verify_context& ctx // The peer certificate and other context.
* ); @endcode
* The return value of the callback is true if the certificate has passed
* verification, false otherwise.
*
* @param ec Set to indicate what error occurred, if any.
*
* @note Calls @c SSL_CTX_set_verify.
*/
template <typename VerifyCallback>
BOOST_ASIO_SYNC_OP_VOID set_verify_callback(VerifyCallback callback,
boost::system::error_code& ec);
/// Load a certification authority file for performing verification.
/**
* This function is used to load one or more trusted certification authorities
* from a file.
*
* @param filename The name of a file containing certification authority
* certificates in PEM format.
*
* @throws boost::system::system_error Thrown on failure.
*
* @note Calls @c SSL_CTX_load_verify_locations.
*/
BOOST_ASIO_DECL void load_verify_file(const std::string& filename);
/// Load a certification authority file for performing verification.
/**
* This function is used to load the certificates for one or more trusted
* certification authorities from a file.
*
* @param filename The name of a file containing certification authority
* certificates in PEM format.
*
* @param ec Set to indicate what error occurred, if any.
*
* @note Calls @c SSL_CTX_load_verify_locations.
*/
BOOST_ASIO_DECL BOOST_ASIO_SYNC_OP_VOID load_verify_file(
const std::string& filename, boost::system::error_code& ec);
/// Add certification authority for performing verification.
/**
* This function is used to add one trusted certification authority
* from a memory buffer.
*
* @param ca The buffer containing the certification authority certificate.
* The certificate must use the PEM format.
*
* @throws boost::system::system_error Thrown on failure.
*
* @note Calls @c SSL_CTX_get_cert_store and @c X509_STORE_add_cert.
*/
BOOST_ASIO_DECL void add_certificate_authority(const const_buffer& ca);
/// Add certification authority for performing verification.
/**
* This function is used to add one trusted certification authority
* from a memory buffer.
*
* @param ca The buffer containing the certification authority certificate.
* The certificate must use the PEM format.
*
* @param ec Set to indicate what error occurred, if any.
*
* @note Calls @c SSL_CTX_get_cert_store and @c X509_STORE_add_cert.
*/
BOOST_ASIO_DECL BOOST_ASIO_SYNC_OP_VOID add_certificate_authority(
const const_buffer& ca, boost::system::error_code& ec);
/// Configures the context to use the default directories for finding
/// certification authority certificates.
/**
* This function specifies that the context should use the default,
* system-dependent directories for locating certification authority
* certificates.
*
* @throws boost::system::system_error Thrown on failure.
*
* @note Calls @c SSL_CTX_set_default_verify_paths.
*/
BOOST_ASIO_DECL void set_default_verify_paths();
/// Configures the context to use the default directories for finding
/// certification authority certificates.
/**
* This function specifies that the context should use the default,
* system-dependent directories for locating certification authority
* certificates.
*
* @param ec Set to indicate what error occurred, if any.
*
* @note Calls @c SSL_CTX_set_default_verify_paths.
*/
BOOST_ASIO_DECL BOOST_ASIO_SYNC_OP_VOID set_default_verify_paths(
boost::system::error_code& ec);
/// Add a directory containing certificate authority files to be used for
/// performing verification.
/**
* This function is used to specify the name of a directory containing
* certification authority certificates. Each file in the directory must
* contain a single certificate. The files must be named using the subject
* name's hash and an extension of ".0".
*
* @param path The name of a directory containing the certificates.
*
* @throws boost::system::system_error Thrown on failure.
*
* @note Calls @c SSL_CTX_load_verify_locations.
*/
BOOST_ASIO_DECL void add_verify_path(const std::string& path);
/// Add a directory containing certificate authority files to be used for
/// performing verification.
/**
* This function is used to specify the name of a directory containing
* certification authority certificates. Each file in the directory must
* contain a single certificate. The files must be named using the subject
* name's hash and an extension of ".0".
*
* @param path The name of a directory containing the certificates.
*
* @param ec Set to indicate what error occurred, if any.
*
* @note Calls @c SSL_CTX_load_verify_locations.
*/
BOOST_ASIO_DECL BOOST_ASIO_SYNC_OP_VOID add_verify_path(
const std::string& path, boost::system::error_code& ec);
/// Use a certificate from a memory buffer.
/**
* This function is used to load a certificate into the context from a buffer.
*
* @param certificate The buffer containing the certificate.
*
* @param format The certificate format (ASN.1 or PEM).
*
* @throws boost::system::system_error Thrown on failure.
*
* @note Calls @c SSL_CTX_use_certificate or SSL_CTX_use_certificate_ASN1.
*/
BOOST_ASIO_DECL void use_certificate(
const const_buffer& certificate, file_format format);
/// Use a certificate from a memory buffer.
/**
* This function is used to load a certificate into the context from a buffer.
*
* @param certificate The buffer containing the certificate.
*
* @param format The certificate format (ASN.1 or PEM).
*
* @param ec Set to indicate what error occurred, if any.
*
* @note Calls @c SSL_CTX_use_certificate or SSL_CTX_use_certificate_ASN1.
*/
BOOST_ASIO_DECL BOOST_ASIO_SYNC_OP_VOID use_certificate(
const const_buffer& certificate, file_format format,
boost::system::error_code& ec);
/// Use a certificate from a file.
/**
* This function is used to load a certificate into the context from a file.
*
* @param filename The name of the file containing the certificate.
*
* @param format The file format (ASN.1 or PEM).
*
* @throws boost::system::system_error Thrown on failure.
*
* @note Calls @c SSL_CTX_use_certificate_file.
*/
BOOST_ASIO_DECL void use_certificate_file(
const std::string& filename, file_format format);
/// Use a certificate from a file.
/**
* This function is used to load a certificate into the context from a file.
*
* @param filename The name of the file containing the certificate.
*
* @param format The file format (ASN.1 or PEM).
*
* @param ec Set to indicate what error occurred, if any.
*
* @note Calls @c SSL_CTX_use_certificate_file.
*/
BOOST_ASIO_DECL BOOST_ASIO_SYNC_OP_VOID use_certificate_file(
const std::string& filename, file_format format,
boost::system::error_code& ec);
/// Use a certificate chain from a memory buffer.
/**
* This function is used to load a certificate chain into the context from a
* buffer.
*
* @param chain The buffer containing the certificate chain. The certificate
* chain must use the PEM format.
*
* @throws boost::system::system_error Thrown on failure.
*
* @note Calls @c SSL_CTX_use_certificate and SSL_CTX_add_extra_chain_cert.
*/
BOOST_ASIO_DECL void use_certificate_chain(const const_buffer& chain);
/// Use a certificate chain from a memory buffer.
/**
* This function is used to load a certificate chain into the context from a
* buffer.
*
* @param chain The buffer containing the certificate chain. The certificate
* chain must use the PEM format.
*
* @param ec Set to indicate what error occurred, if any.
*
* @note Calls @c SSL_CTX_use_certificate and SSL_CTX_add_extra_chain_cert.
*/
BOOST_ASIO_DECL BOOST_ASIO_SYNC_OP_VOID use_certificate_chain(
const const_buffer& chain, boost::system::error_code& ec);
/// Use a certificate chain from a file.
/**
* This function is used to load a certificate chain into the context from a
* file.
*
* @param filename The name of the file containing the certificate. The file
* must use the PEM format.
*
* @throws boost::system::system_error Thrown on failure.
*
* @note Calls @c SSL_CTX_use_certificate_chain_file.
*/
BOOST_ASIO_DECL void use_certificate_chain_file(const std::string& filename);
/// Use a certificate chain from a file.
/**
* This function is used to load a certificate chain into the context from a
* file.
*
* @param filename The name of the file containing the certificate. The file
* must use the PEM format.
*
* @param ec Set to indicate what error occurred, if any.
*
* @note Calls @c SSL_CTX_use_certificate_chain_file.
*/
BOOST_ASIO_DECL BOOST_ASIO_SYNC_OP_VOID use_certificate_chain_file(
const std::string& filename, boost::system::error_code& ec);
/// Use a private key from a memory buffer.
/**
* This function is used to load a private key into the context from a buffer.
*
* @param private_key The buffer containing the private key.
*
* @param format The private key format (ASN.1 or PEM).
*
* @throws boost::system::system_error Thrown on failure.
*
* @note Calls @c SSL_CTX_use_PrivateKey or SSL_CTX_use_PrivateKey_ASN1.
*/
BOOST_ASIO_DECL void use_private_key(
const const_buffer& private_key, file_format format);
/// Use a private key from a memory buffer.
/**
* This function is used to load a private key into the context from a buffer.
*
* @param private_key The buffer containing the private key.
*
* @param format The private key format (ASN.1 or PEM).
*
* @param ec Set to indicate what error occurred, if any.
*
* @note Calls @c SSL_CTX_use_PrivateKey or SSL_CTX_use_PrivateKey_ASN1.
*/
BOOST_ASIO_DECL BOOST_ASIO_SYNC_OP_VOID use_private_key(
const const_buffer& private_key, file_format format,
boost::system::error_code& ec);
/// Use a private key from a file.
/**
* This function is used to load a private key into the context from a file.
*
* @param filename The name of the file containing the private key.
*
* @param format The file format (ASN.1 or PEM).
*
* @throws boost::system::system_error Thrown on failure.
*
* @note Calls @c SSL_CTX_use_PrivateKey_file.
*/
BOOST_ASIO_DECL void use_private_key_file(
const std::string& filename, file_format format);
/// Use a private key from a file.
/**
* This function is used to load a private key into the context from a file.
*
* @param filename The name of the file containing the private key.
*
* @param format The file format (ASN.1 or PEM).
*
* @param ec Set to indicate what error occurred, if any.
*
* @note Calls @c SSL_CTX_use_PrivateKey_file.
*/
BOOST_ASIO_DECL BOOST_ASIO_SYNC_OP_VOID use_private_key_file(
const std::string& filename, file_format format,
boost::system::error_code& ec);
/// Use an RSA private key from a memory buffer.
/**
* This function is used to load an RSA private key into the context from a
* buffer.
*
* @param private_key The buffer containing the RSA private key.
*
* @param format The private key format (ASN.1 or PEM).
*
* @throws boost::system::system_error Thrown on failure.
*
* @note Calls @c SSL_CTX_use_RSAPrivateKey or SSL_CTX_use_RSAPrivateKey_ASN1.
*/
BOOST_ASIO_DECL void use_rsa_private_key(
const const_buffer& private_key, file_format format);
/// Use an RSA private key from a memory buffer.
/**
* This function is used to load an RSA private key into the context from a
* buffer.
*
* @param private_key The buffer containing the RSA private key.
*
* @param format The private key format (ASN.1 or PEM).
*
* @param ec Set to indicate what error occurred, if any.
*
* @note Calls @c SSL_CTX_use_RSAPrivateKey or SSL_CTX_use_RSAPrivateKey_ASN1.
*/
BOOST_ASIO_DECL BOOST_ASIO_SYNC_OP_VOID use_rsa_private_key(
const const_buffer& private_key, file_format format,
boost::system::error_code& ec);
/// Use an RSA private key from a file.
/**
* This function is used to load an RSA private key into the context from a
* file.
*
* @param filename The name of the file containing the RSA private key.
*
* @param format The file format (ASN.1 or PEM).
*
* @throws boost::system::system_error Thrown on failure.
*
* @note Calls @c SSL_CTX_use_RSAPrivateKey_file.
*/
BOOST_ASIO_DECL void use_rsa_private_key_file(
const std::string& filename, file_format format);
/// Use an RSA private key from a file.
/**
* This function is used to load an RSA private key into the context from a
* file.
*
* @param filename The name of the file containing the RSA private key.
*
* @param format The file format (ASN.1 or PEM).
*
* @param ec Set to indicate what error occurred, if any.
*
* @note Calls @c SSL_CTX_use_RSAPrivateKey_file.
*/
BOOST_ASIO_DECL BOOST_ASIO_SYNC_OP_VOID use_rsa_private_key_file(
const std::string& filename, file_format format,
boost::system::error_code& ec);
/// Use the specified memory buffer to obtain the temporary Diffie-Hellman
/// parameters.
/**
* This function is used to load Diffie-Hellman parameters into the context
* from a buffer.
*
* @param dh The memory buffer containing the Diffie-Hellman parameters. The
* buffer must use the PEM format.
*
* @throws boost::system::system_error Thrown on failure.
*
* @note Calls @c SSL_CTX_set_tmp_dh.
*/
BOOST_ASIO_DECL void use_tmp_dh(const const_buffer& dh);
/// Use the specified memory buffer to obtain the temporary Diffie-Hellman
/// parameters.
/**
* This function is used to load Diffie-Hellman parameters into the context
* from a buffer.
*
* @param dh The memory buffer containing the Diffie-Hellman parameters. The
* buffer must use the PEM format.
*
* @param ec Set to indicate what error occurred, if any.
*
* @note Calls @c SSL_CTX_set_tmp_dh.
*/
BOOST_ASIO_DECL BOOST_ASIO_SYNC_OP_VOID use_tmp_dh(
const const_buffer& dh, boost::system::error_code& ec);
/// Use the specified file to obtain the temporary Diffie-Hellman parameters.
/**
* This function is used to load Diffie-Hellman parameters into the context
* from a file.
*
* @param filename The name of the file containing the Diffie-Hellman
* parameters. The file must use the PEM format.
*
* @throws boost::system::system_error Thrown on failure.
*
* @note Calls @c SSL_CTX_set_tmp_dh.
*/
BOOST_ASIO_DECL void use_tmp_dh_file(const std::string& filename);
/// Use the specified file to obtain the temporary Diffie-Hellman parameters.
/**
* This function is used to load Diffie-Hellman parameters into the context
* from a file.
*
* @param filename The name of the file containing the Diffie-Hellman
* parameters. The file must use the PEM format.
*
* @param ec Set to indicate what error occurred, if any.
*
* @note Calls @c SSL_CTX_set_tmp_dh.
*/
BOOST_ASIO_DECL BOOST_ASIO_SYNC_OP_VOID use_tmp_dh_file(
const std::string& filename, boost::system::error_code& ec);
/// Set the password callback.
/**
* This function is used to specify a callback function to obtain password
* information about an encrypted key in PEM format.
*
* @param callback The function object to be used for obtaining the password.
* The function signature of the handler must be:
* @code std::string password_callback(
* std::size_t max_length, // The maximum size for a password.
* password_purpose purpose // Whether password is for reading or writing.
* ); @endcode
* The return value of the callback is a string containing the password.
*
* @throws boost::system::system_error Thrown on failure.
*
* @note Calls @c SSL_CTX_set_default_passwd_cb.
*/
template <typename PasswordCallback>
void set_password_callback(PasswordCallback callback);
/// Set the password callback.
/**
* This function is used to specify a callback function to obtain password
* information about an encrypted key in PEM format.
*
* @param callback The function object to be used for obtaining the password.
* The function signature of the handler must be:
* @code std::string password_callback(
* std::size_t max_length, // The maximum size for a password.
* password_purpose purpose // Whether password is for reading or writing.
* ); @endcode
* The return value of the callback is a string containing the password.
*
* @param ec Set to indicate what error occurred, if any.
*
* @note Calls @c SSL_CTX_set_default_passwd_cb.
*/
template <typename PasswordCallback>
BOOST_ASIO_SYNC_OP_VOID set_password_callback(PasswordCallback callback,
boost::system::error_code& ec);
private:
struct bio_cleanup;
struct x509_cleanup;
struct evp_pkey_cleanup;
struct rsa_cleanup;
struct dh_cleanup;
// Helper function used to set a peer certificate verification callback.
BOOST_ASIO_DECL BOOST_ASIO_SYNC_OP_VOID do_set_verify_callback(
detail::verify_callback_base* callback, boost::system::error_code& ec);
// Callback used when the SSL implementation wants to verify a certificate.
BOOST_ASIO_DECL static int verify_callback_function(
int preverified, X509_STORE_CTX* ctx);
// Helper function used to set a password callback.
BOOST_ASIO_DECL BOOST_ASIO_SYNC_OP_VOID do_set_password_callback(
detail::password_callback_base* callback, boost::system::error_code& ec);
// Callback used when the SSL implementation wants a password.
BOOST_ASIO_DECL static int password_callback_function(
char* buf, int size, int purpose, void* data);
// Helper function to set the temporary Diffie-Hellman parameters from a BIO.
BOOST_ASIO_DECL BOOST_ASIO_SYNC_OP_VOID do_use_tmp_dh(
BIO* bio, boost::system::error_code& ec);
// Helper function to make a BIO from a memory buffer.
BOOST_ASIO_DECL BIO* make_buffer_bio(const const_buffer& b);
// Translate an SSL error into an error code.
BOOST_ASIO_DECL static boost::system::error_code translate_error(long error);
// The underlying native implementation.
native_handle_type handle_;
// Ensure openssl is initialised.
boost::asio::ssl::detail::openssl_init<> init_;
};
} // namespace ssl
} // namespace asio
} // namespace boost
#include <boost/asio/detail/pop_options.hpp>
#include <boost/asio/ssl/impl/context.hpp>
#if defined(BOOST_ASIO_HEADER_ONLY)
# include <boost/asio/ssl/impl/context.ipp>
#endif // defined(BOOST_ASIO_HEADER_ONLY)
#endif // BOOST_ASIO_SSL_CONTEXT_HPP

View File

@@ -0,0 +1,211 @@
//
// ssl/context_base.hpp
// ~~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2022 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef BOOST_ASIO_SSL_CONTEXT_BASE_HPP
#define BOOST_ASIO_SSL_CONTEXT_BASE_HPP
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
#include <boost/asio/detail/config.hpp>
#include <boost/asio/ssl/detail/openssl_types.hpp>
#include <boost/asio/detail/push_options.hpp>
namespace boost {
namespace asio {
namespace ssl {
/// The context_base class is used as a base for the basic_context class
/// template so that we have a common place to define various enums.
class context_base
{
public:
/// Different methods supported by a context.
enum method
{
/// Generic SSL version 2.
sslv2,
/// SSL version 2 client.
sslv2_client,
/// SSL version 2 server.
sslv2_server,
/// Generic SSL version 3.
sslv3,
/// SSL version 3 client.
sslv3_client,
/// SSL version 3 server.
sslv3_server,
/// Generic TLS version 1.
tlsv1,
/// TLS version 1 client.
tlsv1_client,
/// TLS version 1 server.
tlsv1_server,
/// Generic SSL/TLS.
sslv23,
/// SSL/TLS client.
sslv23_client,
/// SSL/TLS server.
sslv23_server,
/// Generic TLS version 1.1.
tlsv11,
/// TLS version 1.1 client.
tlsv11_client,
/// TLS version 1.1 server.
tlsv11_server,
/// Generic TLS version 1.2.
tlsv12,
/// TLS version 1.2 client.
tlsv12_client,
/// TLS version 1.2 server.
tlsv12_server,
/// Generic TLS version 1.3.
tlsv13,
/// TLS version 1.3 client.
tlsv13_client,
/// TLS version 1.3 server.
tlsv13_server,
/// Generic TLS.
tls,
/// TLS client.
tls_client,
/// TLS server.
tls_server
};
/// Bitmask type for SSL options.
typedef long options;
#if defined(GENERATING_DOCUMENTATION)
/// Implement various bug workarounds.
static const long default_workarounds = implementation_defined;
/// Always create a new key when using tmp_dh parameters.
static const long single_dh_use = implementation_defined;
/// Disable SSL v2.
static const long no_sslv2 = implementation_defined;
/// Disable SSL v3.
static const long no_sslv3 = implementation_defined;
/// Disable TLS v1.
static const long no_tlsv1 = implementation_defined;
/// Disable TLS v1.1.
static const long no_tlsv1_1 = implementation_defined;
/// Disable TLS v1.2.
static const long no_tlsv1_2 = implementation_defined;
/// Disable TLS v1.3.
static const long no_tlsv1_3 = implementation_defined;
/// Disable compression. Compression is disabled by default.
static const long no_compression = implementation_defined;
#else
BOOST_ASIO_STATIC_CONSTANT(long, default_workarounds = SSL_OP_ALL);
BOOST_ASIO_STATIC_CONSTANT(long, single_dh_use = SSL_OP_SINGLE_DH_USE);
BOOST_ASIO_STATIC_CONSTANT(long, no_sslv2 = SSL_OP_NO_SSLv2);
BOOST_ASIO_STATIC_CONSTANT(long, no_sslv3 = SSL_OP_NO_SSLv3);
BOOST_ASIO_STATIC_CONSTANT(long, no_tlsv1 = SSL_OP_NO_TLSv1);
# if defined(SSL_OP_NO_TLSv1_1)
BOOST_ASIO_STATIC_CONSTANT(long, no_tlsv1_1 = SSL_OP_NO_TLSv1_1);
# else // defined(SSL_OP_NO_TLSv1_1)
BOOST_ASIO_STATIC_CONSTANT(long, no_tlsv1_1 = 0x10000000L);
# endif // defined(SSL_OP_NO_TLSv1_1)
# if defined(SSL_OP_NO_TLSv1_2)
BOOST_ASIO_STATIC_CONSTANT(long, no_tlsv1_2 = SSL_OP_NO_TLSv1_2);
# else // defined(SSL_OP_NO_TLSv1_2)
BOOST_ASIO_STATIC_CONSTANT(long, no_tlsv1_2 = 0x08000000L);
# endif // defined(SSL_OP_NO_TLSv1_2)
# if defined(SSL_OP_NO_TLSv1_3)
BOOST_ASIO_STATIC_CONSTANT(long, no_tlsv1_3 = SSL_OP_NO_TLSv1_3);
# else // defined(SSL_OP_NO_TLSv1_3)
BOOST_ASIO_STATIC_CONSTANT(long, no_tlsv1_3 = 0x20000000L);
# endif // defined(SSL_OP_NO_TLSv1_3)
# if defined(SSL_OP_NO_COMPRESSION)
BOOST_ASIO_STATIC_CONSTANT(long, no_compression = SSL_OP_NO_COMPRESSION);
# else // defined(SSL_OP_NO_COMPRESSION)
BOOST_ASIO_STATIC_CONSTANT(long, no_compression = 0x20000L);
# endif // defined(SSL_OP_NO_COMPRESSION)
#endif
/// File format types.
enum file_format
{
/// ASN.1 file.
asn1,
/// PEM file.
pem
};
#if !defined(GENERATING_DOCUMENTATION)
// The following types and constants are preserved for backward compatibility.
// New programs should use the equivalents of the same names that are defined
// in the boost::asio::ssl namespace.
typedef int verify_mode;
BOOST_ASIO_STATIC_CONSTANT(int, verify_none = SSL_VERIFY_NONE);
BOOST_ASIO_STATIC_CONSTANT(int, verify_peer = SSL_VERIFY_PEER);
BOOST_ASIO_STATIC_CONSTANT(int,
verify_fail_if_no_peer_cert = SSL_VERIFY_FAIL_IF_NO_PEER_CERT);
BOOST_ASIO_STATIC_CONSTANT(int, verify_client_once = SSL_VERIFY_CLIENT_ONCE);
#endif
/// Purpose of PEM password.
enum password_purpose
{
/// The password is needed for reading/decryption.
for_reading,
/// The password is needed for writing/encryption.
for_writing
};
protected:
/// Protected destructor to prevent deletion through this type.
~context_base()
{
}
};
} // namespace ssl
} // namespace asio
} // namespace boost
#include <boost/asio/detail/pop_options.hpp>
#endif // BOOST_ASIO_SSL_CONTEXT_BASE_HPP

View File

@@ -0,0 +1,121 @@
//
// ssl/detail/buffered_handshake_op.hpp
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2022 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef BOOST_ASIO_SSL_DETAIL_BUFFERED_HANDSHAKE_OP_HPP
#define BOOST_ASIO_SSL_DETAIL_BUFFERED_HANDSHAKE_OP_HPP
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
#include <boost/asio/detail/config.hpp>
#include <boost/asio/ssl/detail/engine.hpp>
#include <boost/asio/detail/push_options.hpp>
namespace boost {
namespace asio {
namespace ssl {
namespace detail {
template <typename ConstBufferSequence>
class buffered_handshake_op
{
public:
static BOOST_ASIO_CONSTEXPR const char* tracking_name()
{
return "ssl::stream<>::async_buffered_handshake";
}
buffered_handshake_op(stream_base::handshake_type type,
const ConstBufferSequence& buffers)
: type_(type),
buffers_(buffers),
total_buffer_size_(boost::asio::buffer_size(buffers_))
{
}
engine::want operator()(engine& eng,
boost::system::error_code& ec,
std::size_t& bytes_transferred) const
{
return this->process(eng, ec, bytes_transferred,
boost::asio::buffer_sequence_begin(buffers_),
boost::asio::buffer_sequence_end(buffers_));
}
template <typename Handler>
void call_handler(Handler& handler,
const boost::system::error_code& ec,
const std::size_t& bytes_transferred) const
{
BOOST_ASIO_MOVE_OR_LVALUE(Handler)(handler)(ec, bytes_transferred);
}
private:
template <typename Iterator>
engine::want process(engine& eng,
boost::system::error_code& ec,
std::size_t& bytes_transferred,
Iterator begin, Iterator end) const
{
Iterator iter = begin;
std::size_t accumulated_size = 0;
for (;;)
{
engine::want want = eng.handshake(type_, ec);
if (want != engine::want_input_and_retry
|| bytes_transferred == total_buffer_size_)
return want;
// Find the next buffer piece to be fed to the engine.
while (iter != end)
{
const_buffer buffer(*iter);
// Skip over any buffers which have already been consumed by the engine.
if (bytes_transferred >= accumulated_size + buffer.size())
{
accumulated_size += buffer.size();
++iter;
continue;
}
// The current buffer may have been partially consumed by the engine on
// a previous iteration. If so, adjust the buffer to point to the
// unused portion.
if (bytes_transferred > accumulated_size)
buffer = buffer + (bytes_transferred - accumulated_size);
// Pass the buffer to the engine, and update the bytes transferred to
// reflect the total number of bytes consumed so far.
bytes_transferred += buffer.size();
buffer = eng.put_input(buffer);
bytes_transferred -= buffer.size();
break;
}
}
}
stream_base::handshake_type type_;
ConstBufferSequence buffers_;
std::size_t total_buffer_size_;
};
} // namespace detail
} // namespace ssl
} // namespace asio
} // namespace boost
#include <boost/asio/detail/pop_options.hpp>
#endif // BOOST_ASIO_SSL_DETAIL_BUFFERED_HANDSHAKE_OP_HPP

View File

@@ -0,0 +1,175 @@
//
// ssl/detail/engine.hpp
// ~~~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2022 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef BOOST_ASIO_SSL_DETAIL_ENGINE_HPP
#define BOOST_ASIO_SSL_DETAIL_ENGINE_HPP
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
#include <boost/asio/detail/config.hpp>
#include <boost/asio/buffer.hpp>
#include <boost/asio/detail/static_mutex.hpp>
#include <boost/asio/ssl/detail/openssl_types.hpp>
#include <boost/asio/ssl/detail/verify_callback.hpp>
#include <boost/asio/ssl/stream_base.hpp>
#include <boost/asio/ssl/verify_mode.hpp>
#include <boost/asio/detail/push_options.hpp>
namespace boost {
namespace asio {
namespace ssl {
namespace detail {
class engine
{
public:
enum want
{
// Returned by functions to indicate that the engine wants input. The input
// buffer should be updated to point to the data. The engine then needs to
// be called again to retry the operation.
want_input_and_retry = -2,
// Returned by functions to indicate that the engine wants to write output.
// The output buffer points to the data to be written. The engine then
// needs to be called again to retry the operation.
want_output_and_retry = -1,
// Returned by functions to indicate that the engine doesn't need input or
// output.
want_nothing = 0,
// Returned by functions to indicate that the engine wants to write output.
// The output buffer points to the data to be written. After that the
// operation is complete, and the engine does not need to be called again.
want_output = 1
};
// Construct a new engine for the specified context.
BOOST_ASIO_DECL explicit engine(SSL_CTX* context);
// Construct a new engine for an existing native SSL implementation.
BOOST_ASIO_DECL explicit engine(SSL* ssl_impl);
#if defined(BOOST_ASIO_HAS_MOVE)
// Move construct from another engine.
BOOST_ASIO_DECL engine(engine&& other) BOOST_ASIO_NOEXCEPT;
#endif // defined(BOOST_ASIO_HAS_MOVE)
// Destructor.
BOOST_ASIO_DECL ~engine();
#if defined(BOOST_ASIO_HAS_MOVE)
// Move assign from another engine.
BOOST_ASIO_DECL engine& operator=(engine&& other) BOOST_ASIO_NOEXCEPT;
#endif // defined(BOOST_ASIO_HAS_MOVE)
// Get the underlying implementation in the native type.
BOOST_ASIO_DECL SSL* native_handle();
// Set the peer verification mode.
BOOST_ASIO_DECL boost::system::error_code set_verify_mode(
verify_mode v, boost::system::error_code& ec);
// Set the peer verification depth.
BOOST_ASIO_DECL boost::system::error_code set_verify_depth(
int depth, boost::system::error_code& ec);
// Set a peer certificate verification callback.
BOOST_ASIO_DECL boost::system::error_code set_verify_callback(
verify_callback_base* callback, boost::system::error_code& ec);
// Perform an SSL handshake using either SSL_connect (client-side) or
// SSL_accept (server-side).
BOOST_ASIO_DECL want handshake(
stream_base::handshake_type type, boost::system::error_code& ec);
// Perform a graceful shutdown of the SSL session.
BOOST_ASIO_DECL want shutdown(boost::system::error_code& ec);
// Write bytes to the SSL session.
BOOST_ASIO_DECL want write(const boost::asio::const_buffer& data,
boost::system::error_code& ec, std::size_t& bytes_transferred);
// Read bytes from the SSL session.
BOOST_ASIO_DECL want read(const boost::asio::mutable_buffer& data,
boost::system::error_code& ec, std::size_t& bytes_transferred);
// Get output data to be written to the transport.
BOOST_ASIO_DECL boost::asio::mutable_buffer get_output(
const boost::asio::mutable_buffer& data);
// Put input data that was read from the transport.
BOOST_ASIO_DECL boost::asio::const_buffer put_input(
const boost::asio::const_buffer& data);
// Map an error::eof code returned by the underlying transport according to
// the type and state of the SSL session. Returns a const reference to the
// error code object, suitable for passing to a completion handler.
BOOST_ASIO_DECL const boost::system::error_code& map_error_code(
boost::system::error_code& ec) const;
private:
// Disallow copying and assignment.
engine(const engine&);
engine& operator=(const engine&);
// Callback used when the SSL implementation wants to verify a certificate.
BOOST_ASIO_DECL static int verify_callback_function(
int preverified, X509_STORE_CTX* ctx);
#if (OPENSSL_VERSION_NUMBER < 0x10000000L)
// The SSL_accept function may not be thread safe. This mutex is used to
// protect all calls to the SSL_accept function.
BOOST_ASIO_DECL static boost::asio::detail::static_mutex& accept_mutex();
#endif // (OPENSSL_VERSION_NUMBER < 0x10000000L)
// Perform one operation. Returns >= 0 on success or error, want_read if the
// operation needs more input, or want_write if it needs to write some output
// before the operation can complete.
BOOST_ASIO_DECL want perform(int (engine::* op)(void*, std::size_t),
void* data, std::size_t length, boost::system::error_code& ec,
std::size_t* bytes_transferred);
// Adapt the SSL_accept function to the signature needed for perform().
BOOST_ASIO_DECL int do_accept(void*, std::size_t);
// Adapt the SSL_connect function to the signature needed for perform().
BOOST_ASIO_DECL int do_connect(void*, std::size_t);
// Adapt the SSL_shutdown function to the signature needed for perform().
BOOST_ASIO_DECL int do_shutdown(void*, std::size_t);
// Adapt the SSL_read function to the signature needed for perform().
BOOST_ASIO_DECL int do_read(void* data, std::size_t length);
// Adapt the SSL_write function to the signature needed for perform().
BOOST_ASIO_DECL int do_write(void* data, std::size_t length);
SSL* ssl_;
BIO* ext_bio_;
};
} // namespace detail
} // namespace ssl
} // namespace asio
} // namespace boost
#include <boost/asio/detail/pop_options.hpp>
#if defined(BOOST_ASIO_HEADER_ONLY)
# include <boost/asio/ssl/detail/impl/engine.ipp>
#endif // defined(BOOST_ASIO_HEADER_ONLY)
#endif // BOOST_ASIO_SSL_DETAIL_ENGINE_HPP

View File

@@ -0,0 +1,69 @@
//
// ssl/detail/handshake_op.hpp
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2022 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef BOOST_ASIO_SSL_DETAIL_HANDSHAKE_OP_HPP
#define BOOST_ASIO_SSL_DETAIL_HANDSHAKE_OP_HPP
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
#include <boost/asio/detail/config.hpp>
#include <boost/asio/ssl/detail/engine.hpp>
#include <boost/asio/detail/push_options.hpp>
namespace boost {
namespace asio {
namespace ssl {
namespace detail {
class handshake_op
{
public:
static BOOST_ASIO_CONSTEXPR const char* tracking_name()
{
return "ssl::stream<>::async_handshake";
}
handshake_op(stream_base::handshake_type type)
: type_(type)
{
}
engine::want operator()(engine& eng,
boost::system::error_code& ec,
std::size_t& bytes_transferred) const
{
bytes_transferred = 0;
return eng.handshake(type_, ec);
}
template <typename Handler>
void call_handler(Handler& handler,
const boost::system::error_code& ec,
const std::size_t&) const
{
BOOST_ASIO_MOVE_OR_LVALUE(Handler)(handler)(ec);
}
private:
stream_base::handshake_type type_;
};
} // namespace detail
} // namespace ssl
} // namespace asio
} // namespace boost
#include <boost/asio/detail/pop_options.hpp>
#endif // BOOST_ASIO_SSL_DETAIL_HANDSHAKE_OP_HPP

View File

@@ -0,0 +1,383 @@
//
// ssl/detail/impl/engine.ipp
// ~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2022 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef BOOST_ASIO_SSL_DETAIL_IMPL_ENGINE_IPP
#define BOOST_ASIO_SSL_DETAIL_IMPL_ENGINE_IPP
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
#include <boost/asio/detail/config.hpp>
#include <boost/asio/detail/throw_error.hpp>
#include <boost/asio/error.hpp>
#include <boost/asio/ssl/detail/engine.hpp>
#include <boost/asio/ssl/error.hpp>
#include <boost/asio/ssl/verify_context.hpp>
#include <boost/asio/detail/push_options.hpp>
namespace boost {
namespace asio {
namespace ssl {
namespace detail {
engine::engine(SSL_CTX* context)
: ssl_(::SSL_new(context))
{
if (!ssl_)
{
boost::system::error_code ec(
static_cast<int>(::ERR_get_error()),
boost::asio::error::get_ssl_category());
boost::asio::detail::throw_error(ec, "engine");
}
#if (OPENSSL_VERSION_NUMBER < 0x10000000L)
accept_mutex().init();
#endif // (OPENSSL_VERSION_NUMBER < 0x10000000L)
::SSL_set_mode(ssl_, SSL_MODE_ENABLE_PARTIAL_WRITE);
::SSL_set_mode(ssl_, SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER);
#if defined(SSL_MODE_RELEASE_BUFFERS)
::SSL_set_mode(ssl_, SSL_MODE_RELEASE_BUFFERS);
#endif // defined(SSL_MODE_RELEASE_BUFFERS)
::BIO* int_bio = 0;
::BIO_new_bio_pair(&int_bio, 0, &ext_bio_, 0);
::SSL_set_bio(ssl_, int_bio, int_bio);
}
engine::engine(SSL* ssl_impl)
: ssl_(ssl_impl)
{
#if (OPENSSL_VERSION_NUMBER < 0x10000000L)
accept_mutex().init();
#endif // (OPENSSL_VERSION_NUMBER < 0x10000000L)
::SSL_set_mode(ssl_, SSL_MODE_ENABLE_PARTIAL_WRITE);
::SSL_set_mode(ssl_, SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER);
#if defined(SSL_MODE_RELEASE_BUFFERS)
::SSL_set_mode(ssl_, SSL_MODE_RELEASE_BUFFERS);
#endif // defined(SSL_MODE_RELEASE_BUFFERS)
::BIO* int_bio = 0;
::BIO_new_bio_pair(&int_bio, 0, &ext_bio_, 0);
::SSL_set_bio(ssl_, int_bio, int_bio);
}
#if defined(BOOST_ASIO_HAS_MOVE)
engine::engine(engine&& other) BOOST_ASIO_NOEXCEPT
: ssl_(other.ssl_),
ext_bio_(other.ext_bio_)
{
other.ssl_ = 0;
other.ext_bio_ = 0;
}
#endif // defined(BOOST_ASIO_HAS_MOVE)
engine::~engine()
{
if (ssl_ && SSL_get_app_data(ssl_))
{
delete static_cast<verify_callback_base*>(SSL_get_app_data(ssl_));
SSL_set_app_data(ssl_, 0);
}
if (ext_bio_)
::BIO_free(ext_bio_);
if (ssl_)
::SSL_free(ssl_);
}
#if defined(BOOST_ASIO_HAS_MOVE)
engine& engine::operator=(engine&& other) BOOST_ASIO_NOEXCEPT
{
if (this != &other)
{
ssl_ = other.ssl_;
ext_bio_ = other.ext_bio_;
other.ssl_ = 0;
other.ext_bio_ = 0;
}
return *this;
}
#endif // defined(BOOST_ASIO_HAS_MOVE)
SSL* engine::native_handle()
{
return ssl_;
}
boost::system::error_code engine::set_verify_mode(
verify_mode v, boost::system::error_code& ec)
{
::SSL_set_verify(ssl_, v, ::SSL_get_verify_callback(ssl_));
ec = boost::system::error_code();
return ec;
}
boost::system::error_code engine::set_verify_depth(
int depth, boost::system::error_code& ec)
{
::SSL_set_verify_depth(ssl_, depth);
ec = boost::system::error_code();
return ec;
}
boost::system::error_code engine::set_verify_callback(
verify_callback_base* callback, boost::system::error_code& ec)
{
if (SSL_get_app_data(ssl_))
delete static_cast<verify_callback_base*>(SSL_get_app_data(ssl_));
SSL_set_app_data(ssl_, callback);
::SSL_set_verify(ssl_, ::SSL_get_verify_mode(ssl_),
&engine::verify_callback_function);
ec = boost::system::error_code();
return ec;
}
int engine::verify_callback_function(int preverified, X509_STORE_CTX* ctx)
{
if (ctx)
{
if (SSL* ssl = static_cast<SSL*>(
::X509_STORE_CTX_get_ex_data(
ctx, ::SSL_get_ex_data_X509_STORE_CTX_idx())))
{
if (SSL_get_app_data(ssl))
{
verify_callback_base* callback =
static_cast<verify_callback_base*>(
SSL_get_app_data(ssl));
verify_context verify_ctx(ctx);
return callback->call(preverified != 0, verify_ctx) ? 1 : 0;
}
}
}
return 0;
}
engine::want engine::handshake(
stream_base::handshake_type type, boost::system::error_code& ec)
{
return perform((type == boost::asio::ssl::stream_base::client)
? &engine::do_connect : &engine::do_accept, 0, 0, ec, 0);
}
engine::want engine::shutdown(boost::system::error_code& ec)
{
return perform(&engine::do_shutdown, 0, 0, ec, 0);
}
engine::want engine::write(const boost::asio::const_buffer& data,
boost::system::error_code& ec, std::size_t& bytes_transferred)
{
if (data.size() == 0)
{
ec = boost::system::error_code();
return engine::want_nothing;
}
return perform(&engine::do_write,
const_cast<void*>(data.data()),
data.size(), ec, &bytes_transferred);
}
engine::want engine::read(const boost::asio::mutable_buffer& data,
boost::system::error_code& ec, std::size_t& bytes_transferred)
{
if (data.size() == 0)
{
ec = boost::system::error_code();
return engine::want_nothing;
}
return perform(&engine::do_read, data.data(),
data.size(), ec, &bytes_transferred);
}
boost::asio::mutable_buffer engine::get_output(
const boost::asio::mutable_buffer& data)
{
int length = ::BIO_read(ext_bio_,
data.data(), static_cast<int>(data.size()));
return boost::asio::buffer(data,
length > 0 ? static_cast<std::size_t>(length) : 0);
}
boost::asio::const_buffer engine::put_input(
const boost::asio::const_buffer& data)
{
int length = ::BIO_write(ext_bio_,
data.data(), static_cast<int>(data.size()));
return boost::asio::buffer(data +
(length > 0 ? static_cast<std::size_t>(length) : 0));
}
const boost::system::error_code& engine::map_error_code(
boost::system::error_code& ec) const
{
// We only want to map the error::eof code.
if (ec != boost::asio::error::eof)
return ec;
// If there's data yet to be read, it's an error.
if (BIO_wpending(ext_bio_))
{
ec = boost::asio::ssl::error::stream_truncated;
return ec;
}
// SSL v2 doesn't provide a protocol-level shutdown, so an eof on the
// underlying transport is passed through.
#if (OPENSSL_VERSION_NUMBER < 0x10100000L)
if (SSL_version(ssl_) == SSL2_VERSION)
return ec;
#endif // (OPENSSL_VERSION_NUMBER < 0x10100000L)
// Otherwise, the peer should have negotiated a proper shutdown.
if ((::SSL_get_shutdown(ssl_) & SSL_RECEIVED_SHUTDOWN) == 0)
{
ec = boost::asio::ssl::error::stream_truncated;
}
return ec;
}
#if (OPENSSL_VERSION_NUMBER < 0x10000000L)
boost::asio::detail::static_mutex& engine::accept_mutex()
{
static boost::asio::detail::static_mutex mutex = BOOST_ASIO_STATIC_MUTEX_INIT;
return mutex;
}
#endif // (OPENSSL_VERSION_NUMBER < 0x10000000L)
engine::want engine::perform(int (engine::* op)(void*, std::size_t),
void* data, std::size_t length, boost::system::error_code& ec,
std::size_t* bytes_transferred)
{
std::size_t pending_output_before = ::BIO_ctrl_pending(ext_bio_);
::ERR_clear_error();
int result = (this->*op)(data, length);
int ssl_error = ::SSL_get_error(ssl_, result);
int sys_error = static_cast<int>(::ERR_get_error());
std::size_t pending_output_after = ::BIO_ctrl_pending(ext_bio_);
if (ssl_error == SSL_ERROR_SSL)
{
ec = boost::system::error_code(sys_error,
boost::asio::error::get_ssl_category());
return pending_output_after > pending_output_before
? want_output : want_nothing;
}
if (ssl_error == SSL_ERROR_SYSCALL)
{
if (sys_error == 0)
{
ec = boost::asio::ssl::error::unspecified_system_error;
}
else
{
ec = boost::system::error_code(sys_error,
boost::asio::error::get_ssl_category());
}
return pending_output_after > pending_output_before
? want_output : want_nothing;
}
if (result > 0 && bytes_transferred)
*bytes_transferred = static_cast<std::size_t>(result);
if (ssl_error == SSL_ERROR_WANT_WRITE)
{
ec = boost::system::error_code();
return want_output_and_retry;
}
else if (pending_output_after > pending_output_before)
{
ec = boost::system::error_code();
return result > 0 ? want_output : want_output_and_retry;
}
else if (ssl_error == SSL_ERROR_WANT_READ)
{
ec = boost::system::error_code();
return want_input_and_retry;
}
else if (ssl_error == SSL_ERROR_ZERO_RETURN)
{
ec = boost::asio::error::eof;
return want_nothing;
}
else if (ssl_error == SSL_ERROR_NONE)
{
ec = boost::system::error_code();
return want_nothing;
}
else
{
ec = boost::asio::ssl::error::unexpected_result;
return want_nothing;
}
}
int engine::do_accept(void*, std::size_t)
{
#if (OPENSSL_VERSION_NUMBER < 0x10000000L)
boost::asio::detail::static_mutex::scoped_lock lock(accept_mutex());
#endif // (OPENSSL_VERSION_NUMBER < 0x10000000L)
return ::SSL_accept(ssl_);
}
int engine::do_connect(void*, std::size_t)
{
return ::SSL_connect(ssl_);
}
int engine::do_shutdown(void*, std::size_t)
{
int result = ::SSL_shutdown(ssl_);
if (result == 0)
result = ::SSL_shutdown(ssl_);
return result;
}
int engine::do_read(void* data, std::size_t length)
{
return ::SSL_read(ssl_, data,
length < INT_MAX ? static_cast<int>(length) : INT_MAX);
}
int engine::do_write(void* data, std::size_t length)
{
return ::SSL_write(ssl_, data,
length < INT_MAX ? static_cast<int>(length) : INT_MAX);
}
} // namespace detail
} // namespace ssl
} // namespace asio
} // namespace boost
#include <boost/asio/detail/pop_options.hpp>
#endif // BOOST_ASIO_SSL_DETAIL_IMPL_ENGINE_IPP

View File

@@ -0,0 +1,167 @@
//
// ssl/detail/impl/openssl_init.ipp
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2005 Voipster / Indrek dot Juhani at voipster dot com
// Copyright (c) 2005-2022 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef BOOST_ASIO_SSL_DETAIL_IMPL_OPENSSL_INIT_IPP
#define BOOST_ASIO_SSL_DETAIL_IMPL_OPENSSL_INIT_IPP
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
#include <boost/asio/detail/config.hpp>
#include <vector>
#include <boost/asio/detail/assert.hpp>
#include <boost/asio/detail/mutex.hpp>
#include <boost/asio/detail/tss_ptr.hpp>
#include <boost/asio/ssl/detail/openssl_init.hpp>
#include <boost/asio/ssl/detail/openssl_types.hpp>
#include <boost/asio/detail/push_options.hpp>
namespace boost {
namespace asio {
namespace ssl {
namespace detail {
class openssl_init_base::do_init
{
public:
do_init()
{
#if (OPENSSL_VERSION_NUMBER < 0x10100000L)
::SSL_library_init();
::SSL_load_error_strings();
::OpenSSL_add_all_algorithms();
mutexes_.resize(::CRYPTO_num_locks());
for (size_t i = 0; i < mutexes_.size(); ++i)
mutexes_[i].reset(new boost::asio::detail::mutex);
::CRYPTO_set_locking_callback(&do_init::openssl_locking_func);
#endif // (OPENSSL_VERSION_NUMBER < 0x10100000L)
#if (OPENSSL_VERSION_NUMBER < 0x10000000L)
::CRYPTO_set_id_callback(&do_init::openssl_id_func);
#endif // (OPENSSL_VERSION_NUMBER < 0x10000000L)
#if !defined(SSL_OP_NO_COMPRESSION) \
&& (OPENSSL_VERSION_NUMBER >= 0x00908000L)
null_compression_methods_ = sk_SSL_COMP_new_null();
#endif // !defined(SSL_OP_NO_COMPRESSION)
// && (OPENSSL_VERSION_NUMBER >= 0x00908000L)
}
~do_init()
{
#if !defined(SSL_OP_NO_COMPRESSION) \
&& (OPENSSL_VERSION_NUMBER >= 0x00908000L)
sk_SSL_COMP_free(null_compression_methods_);
#endif // !defined(SSL_OP_NO_COMPRESSION)
// && (OPENSSL_VERSION_NUMBER >= 0x00908000L)
#if (OPENSSL_VERSION_NUMBER < 0x10000000L)
::CRYPTO_set_id_callback(0);
#endif // (OPENSSL_VERSION_NUMBER < 0x10000000L)
#if (OPENSSL_VERSION_NUMBER < 0x10100000L)
::CRYPTO_set_locking_callback(0);
::ERR_free_strings();
::EVP_cleanup();
::CRYPTO_cleanup_all_ex_data();
#endif // (OPENSSL_VERSION_NUMBER < 0x10100000L)
#if (OPENSSL_VERSION_NUMBER < 0x10000000L)
::ERR_remove_state(0);
#elif (OPENSSL_VERSION_NUMBER < 0x10100000L)
::ERR_remove_thread_state(NULL);
#endif // (OPENSSL_VERSION_NUMBER < 0x10000000L)
#if (OPENSSL_VERSION_NUMBER >= 0x10002000L) \
&& (OPENSSL_VERSION_NUMBER < 0x10100000L) \
&& !defined(SSL_OP_NO_COMPRESSION)
::SSL_COMP_free_compression_methods();
#endif // (OPENSSL_VERSION_NUMBER >= 0x10002000L)
// && (OPENSSL_VERSION_NUMBER < 0x10100000L)
// && !defined(SSL_OP_NO_COMPRESSION)
#if !defined(OPENSSL_IS_BORINGSSL) && !defined(BOOST_ASIO_USE_WOLFSSL)
::CONF_modules_unload(1);
#endif // !defined(OPENSSL_IS_BORINGSSL) && !defined(BOOST_ASIO_USE_WOLFSSL)
#if !defined(OPENSSL_NO_ENGINE) \
&& (OPENSSL_VERSION_NUMBER < 0x10100000L)
::ENGINE_cleanup();
#endif // !defined(OPENSSL_NO_ENGINE)
// && (OPENSSL_VERSION_NUMBER < 0x10100000L)
}
#if !defined(SSL_OP_NO_COMPRESSION) \
&& (OPENSSL_VERSION_NUMBER >= 0x00908000L)
STACK_OF(SSL_COMP)* get_null_compression_methods() const
{
return null_compression_methods_;
}
#endif // !defined(SSL_OP_NO_COMPRESSION)
// && (OPENSSL_VERSION_NUMBER >= 0x00908000L)
private:
#if (OPENSSL_VERSION_NUMBER < 0x10000000L)
static unsigned long openssl_id_func()
{
#if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
return ::GetCurrentThreadId();
#else // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
void* id = &errno;
BOOST_ASIO_ASSERT(sizeof(unsigned long) >= sizeof(void*));
return reinterpret_cast<unsigned long>(id);
#endif // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
}
#endif // (OPENSSL_VERSION_NUMBER < 0x10000000L)
#if (OPENSSL_VERSION_NUMBER < 0x10100000L)
static void openssl_locking_func(int mode, int n,
const char* /*file*/, int /*line*/)
{
if (mode & CRYPTO_LOCK)
instance()->mutexes_[n]->lock();
else
instance()->mutexes_[n]->unlock();
}
// Mutexes to be used in locking callbacks.
std::vector<boost::asio::detail::shared_ptr<
boost::asio::detail::mutex> > mutexes_;
#endif // (OPENSSL_VERSION_NUMBER < 0x10100000L)
#if !defined(SSL_OP_NO_COMPRESSION) \
&& (OPENSSL_VERSION_NUMBER >= 0x00908000L)
STACK_OF(SSL_COMP)* null_compression_methods_;
#endif // !defined(SSL_OP_NO_COMPRESSION)
// && (OPENSSL_VERSION_NUMBER >= 0x00908000L)
};
boost::asio::detail::shared_ptr<openssl_init_base::do_init>
openssl_init_base::instance()
{
static boost::asio::detail::shared_ptr<do_init> init(new do_init);
return init;
}
#if !defined(SSL_OP_NO_COMPRESSION) \
&& (OPENSSL_VERSION_NUMBER >= 0x00908000L)
STACK_OF(SSL_COMP)* openssl_init_base::get_null_compression_methods()
{
return instance()->get_null_compression_methods();
}
#endif // !defined(SSL_OP_NO_COMPRESSION)
// && (OPENSSL_VERSION_NUMBER >= 0x00908000L)
} // namespace detail
} // namespace ssl
} // namespace asio
} // namespace boost
#include <boost/asio/detail/pop_options.hpp>
#endif // BOOST_ASIO_SSL_DETAIL_IMPL_OPENSSL_INIT_IPP

View File

@@ -0,0 +1,427 @@
//
// ssl/detail/io.hpp
// ~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2022 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef BOOST_ASIO_SSL_DETAIL_IO_HPP
#define BOOST_ASIO_SSL_DETAIL_IO_HPP
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
#include <boost/asio/detail/config.hpp>
#include <boost/asio/detail/base_from_cancellation_state.hpp>
#include <boost/asio/detail/handler_tracking.hpp>
#include <boost/asio/ssl/detail/engine.hpp>
#include <boost/asio/ssl/detail/stream_core.hpp>
#include <boost/asio/write.hpp>
#include <boost/asio/detail/push_options.hpp>
namespace boost {
namespace asio {
namespace ssl {
namespace detail {
template <typename Stream, typename Operation>
std::size_t io(Stream& next_layer, stream_core& core,
const Operation& op, boost::system::error_code& ec)
{
boost::system::error_code io_ec;
std::size_t bytes_transferred = 0;
do switch (op(core.engine_, ec, bytes_transferred))
{
case engine::want_input_and_retry:
// If the input buffer is empty then we need to read some more data from
// the underlying transport.
if (core.input_.size() == 0)
{
core.input_ = boost::asio::buffer(core.input_buffer_,
next_layer.read_some(core.input_buffer_, io_ec));
if (!ec)
ec = io_ec;
}
// Pass the new input data to the engine.
core.input_ = core.engine_.put_input(core.input_);
// Try the operation again.
continue;
case engine::want_output_and_retry:
// Get output data from the engine and write it to the underlying
// transport.
boost::asio::write(next_layer,
core.engine_.get_output(core.output_buffer_), io_ec);
if (!ec)
ec = io_ec;
// Try the operation again.
continue;
case engine::want_output:
// Get output data from the engine and write it to the underlying
// transport.
boost::asio::write(next_layer,
core.engine_.get_output(core.output_buffer_), io_ec);
if (!ec)
ec = io_ec;
// Operation is complete. Return result to caller.
core.engine_.map_error_code(ec);
return bytes_transferred;
default:
// Operation is complete. Return result to caller.
core.engine_.map_error_code(ec);
return bytes_transferred;
} while (!ec);
// Operation failed. Return result to caller.
core.engine_.map_error_code(ec);
return 0;
}
template <typename Stream, typename Operation, typename Handler>
class io_op
: public boost::asio::detail::base_from_cancellation_state<Handler>
{
public:
io_op(Stream& next_layer, stream_core& core,
const Operation& op, Handler& handler)
: boost::asio::detail::base_from_cancellation_state<Handler>(handler),
next_layer_(next_layer),
core_(core),
op_(op),
start_(0),
want_(engine::want_nothing),
bytes_transferred_(0),
handler_(BOOST_ASIO_MOVE_CAST(Handler)(handler))
{
}
#if defined(BOOST_ASIO_HAS_MOVE)
io_op(const io_op& other)
: boost::asio::detail::base_from_cancellation_state<Handler>(other),
next_layer_(other.next_layer_),
core_(other.core_),
op_(other.op_),
start_(other.start_),
want_(other.want_),
ec_(other.ec_),
bytes_transferred_(other.bytes_transferred_),
handler_(other.handler_)
{
}
io_op(io_op&& other)
: boost::asio::detail::base_from_cancellation_state<Handler>(
BOOST_ASIO_MOVE_CAST(
boost::asio::detail::base_from_cancellation_state<Handler>)(
other)),
next_layer_(other.next_layer_),
core_(other.core_),
op_(BOOST_ASIO_MOVE_CAST(Operation)(other.op_)),
start_(other.start_),
want_(other.want_),
ec_(other.ec_),
bytes_transferred_(other.bytes_transferred_),
handler_(BOOST_ASIO_MOVE_CAST(Handler)(other.handler_))
{
}
#endif // defined(BOOST_ASIO_HAS_MOVE)
void operator()(boost::system::error_code ec,
std::size_t bytes_transferred = ~std::size_t(0), int start = 0)
{
switch (start_ = start)
{
case 1: // Called after at least one async operation.
do
{
switch (want_ = op_(core_.engine_, ec_, bytes_transferred_))
{
case engine::want_input_and_retry:
// If the input buffer already has data in it we can pass it to the
// engine and then retry the operation immediately.
if (core_.input_.size() != 0)
{
core_.input_ = core_.engine_.put_input(core_.input_);
continue;
}
// The engine wants more data to be read from input. However, we
// cannot allow more than one read operation at a time on the
// underlying transport. The pending_read_ timer's expiry is set to
// pos_infin if a read is in progress, and neg_infin otherwise.
if (core_.expiry(core_.pending_read_) == core_.neg_infin())
{
// Prevent other read operations from being started.
core_.pending_read_.expires_at(core_.pos_infin());
BOOST_ASIO_HANDLER_LOCATION((
__FILE__, __LINE__, Operation::tracking_name()));
// Start reading some data from the underlying transport.
next_layer_.async_read_some(
boost::asio::buffer(core_.input_buffer_),
BOOST_ASIO_MOVE_CAST(io_op)(*this));
}
else
{
BOOST_ASIO_HANDLER_LOCATION((
__FILE__, __LINE__, Operation::tracking_name()));
// Wait until the current read operation completes.
core_.pending_read_.async_wait(BOOST_ASIO_MOVE_CAST(io_op)(*this));
}
// Yield control until asynchronous operation completes. Control
// resumes at the "default:" label below.
return;
case engine::want_output_and_retry:
case engine::want_output:
// The engine wants some data to be written to the output. However, we
// cannot allow more than one write operation at a time on the
// underlying transport. The pending_write_ timer's expiry is set to
// pos_infin if a write is in progress, and neg_infin otherwise.
if (core_.expiry(core_.pending_write_) == core_.neg_infin())
{
// Prevent other write operations from being started.
core_.pending_write_.expires_at(core_.pos_infin());
BOOST_ASIO_HANDLER_LOCATION((
__FILE__, __LINE__, Operation::tracking_name()));
// Start writing all the data to the underlying transport.
boost::asio::async_write(next_layer_,
core_.engine_.get_output(core_.output_buffer_),
BOOST_ASIO_MOVE_CAST(io_op)(*this));
}
else
{
BOOST_ASIO_HANDLER_LOCATION((
__FILE__, __LINE__, Operation::tracking_name()));
// Wait until the current write operation completes.
core_.pending_write_.async_wait(BOOST_ASIO_MOVE_CAST(io_op)(*this));
}
// Yield control until asynchronous operation completes. Control
// resumes at the "default:" label below.
return;
default:
// The SSL operation is done and we can invoke the handler, but we
// have to keep in mind that this function might be being called from
// the async operation's initiating function. In this case we're not
// allowed to call the handler directly. Instead, issue a zero-sized
// read so the handler runs "as-if" posted using io_context::post().
if (start)
{
BOOST_ASIO_HANDLER_LOCATION((
__FILE__, __LINE__, Operation::tracking_name()));
next_layer_.async_read_some(
boost::asio::buffer(core_.input_buffer_, 0),
BOOST_ASIO_MOVE_CAST(io_op)(*this));
// Yield control until asynchronous operation completes. Control
// resumes at the "default:" label below.
return;
}
else
{
// Continue on to run handler directly.
break;
}
}
default:
if (bytes_transferred == ~std::size_t(0))
bytes_transferred = 0; // Timer cancellation, no data transferred.
else if (!ec_)
ec_ = ec;
switch (want_)
{
case engine::want_input_and_retry:
// Add received data to the engine's input.
core_.input_ = boost::asio::buffer(
core_.input_buffer_, bytes_transferred);
core_.input_ = core_.engine_.put_input(core_.input_);
// Release any waiting read operations.
core_.pending_read_.expires_at(core_.neg_infin());
// Check for cancellation before continuing.
if (this->cancelled() != cancellation_type::none)
{
ec_ = boost::asio::error::operation_aborted;
break;
}
// Try the operation again.
continue;
case engine::want_output_and_retry:
// Release any waiting write operations.
core_.pending_write_.expires_at(core_.neg_infin());
// Check for cancellation before continuing.
if (this->cancelled() != cancellation_type::none)
{
ec_ = boost::asio::error::operation_aborted;
break;
}
// Try the operation again.
continue;
case engine::want_output:
// Release any waiting write operations.
core_.pending_write_.expires_at(core_.neg_infin());
// Fall through to call handler.
default:
// Pass the result to the handler.
op_.call_handler(handler_,
core_.engine_.map_error_code(ec_),
ec_ ? 0 : bytes_transferred_);
// Our work here is done.
return;
}
} while (!ec_);
// Operation failed. Pass the result to the handler.
op_.call_handler(handler_, core_.engine_.map_error_code(ec_), 0);
}
}
//private:
Stream& next_layer_;
stream_core& core_;
Operation op_;
int start_;
engine::want want_;
boost::system::error_code ec_;
std::size_t bytes_transferred_;
Handler handler_;
};
template <typename Stream, typename Operation, typename Handler>
inline asio_handler_allocate_is_deprecated
asio_handler_allocate(std::size_t size,
io_op<Stream, Operation, Handler>* this_handler)
{
#if defined(BOOST_ASIO_NO_DEPRECATED)
boost_asio_handler_alloc_helpers::allocate(size, this_handler->handler_);
return asio_handler_allocate_is_no_longer_used();
#else // defined(BOOST_ASIO_NO_DEPRECATED)
return boost_asio_handler_alloc_helpers::allocate(
size, this_handler->handler_);
#endif // defined(BOOST_ASIO_NO_DEPRECATED)
}
template <typename Stream, typename Operation, typename Handler>
inline asio_handler_deallocate_is_deprecated
asio_handler_deallocate(void* pointer, std::size_t size,
io_op<Stream, Operation, Handler>* this_handler)
{
boost_asio_handler_alloc_helpers::deallocate(
pointer, size, this_handler->handler_);
#if defined(BOOST_ASIO_NO_DEPRECATED)
return asio_handler_deallocate_is_no_longer_used();
#endif // defined(BOOST_ASIO_NO_DEPRECATED)
}
template <typename Stream, typename Operation, typename Handler>
inline bool asio_handler_is_continuation(
io_op<Stream, Operation, Handler>* this_handler)
{
return this_handler->start_ == 0 ? true
: boost_asio_handler_cont_helpers::is_continuation(this_handler->handler_);
}
template <typename Function, typename Stream,
typename Operation, typename Handler>
inline asio_handler_invoke_is_deprecated
asio_handler_invoke(Function& function,
io_op<Stream, Operation, Handler>* this_handler)
{
boost_asio_handler_invoke_helpers::invoke(
function, this_handler->handler_);
#if defined(BOOST_ASIO_NO_DEPRECATED)
return asio_handler_invoke_is_no_longer_used();
#endif // defined(BOOST_ASIO_NO_DEPRECATED)
}
template <typename Function, typename Stream,
typename Operation, typename Handler>
inline asio_handler_invoke_is_deprecated
asio_handler_invoke(const Function& function,
io_op<Stream, Operation, Handler>* this_handler)
{
boost_asio_handler_invoke_helpers::invoke(
function, this_handler->handler_);
#if defined(BOOST_ASIO_NO_DEPRECATED)
return asio_handler_invoke_is_no_longer_used();
#endif // defined(BOOST_ASIO_NO_DEPRECATED)
}
template <typename Stream, typename Operation, typename Handler>
inline void async_io(Stream& next_layer, stream_core& core,
const Operation& op, Handler& handler)
{
io_op<Stream, Operation, Handler>(
next_layer, core, op, handler)(
boost::system::error_code(), 0, 1);
}
} // namespace detail
} // namespace ssl
template <template <typename, typename> class Associator,
typename Stream, typename Operation,
typename Handler, typename DefaultCandidate>
struct associator<Associator,
ssl::detail::io_op<Stream, Operation, Handler>,
DefaultCandidate>
: Associator<Handler, DefaultCandidate>
{
static typename Associator<Handler, DefaultCandidate>::type get(
const ssl::detail::io_op<Stream, Operation, Handler>& h,
const DefaultCandidate& c = DefaultCandidate()) BOOST_ASIO_NOEXCEPT
{
return Associator<Handler, DefaultCandidate>::get(h.handler_, c);
}
};
} // namespace asio
} // namespace boost
#include <boost/asio/detail/pop_options.hpp>
#endif // BOOST_ASIO_SSL_DETAIL_IO_HPP

View File

@@ -0,0 +1,103 @@
//
// ssl/detail/openssl_init.hpp
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2022 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef BOOST_ASIO_SSL_DETAIL_OPENSSL_INIT_HPP
#define BOOST_ASIO_SSL_DETAIL_OPENSSL_INIT_HPP
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
#include <boost/asio/detail/config.hpp>
#include <cstring>
#include <boost/asio/detail/memory.hpp>
#include <boost/asio/detail/noncopyable.hpp>
#include <boost/asio/ssl/detail/openssl_types.hpp>
#include <boost/asio/detail/push_options.hpp>
namespace boost {
namespace asio {
namespace ssl {
namespace detail {
class openssl_init_base
: private noncopyable
{
protected:
// Class that performs the actual initialisation.
class do_init;
// Helper function to manage a do_init singleton. The static instance of the
// openssl_init object ensures that this function is always called before
// main, and therefore before any other threads can get started. The do_init
// instance must be static in this function to ensure that it gets
// initialised before any other global objects try to use it.
BOOST_ASIO_DECL static boost::asio::detail::shared_ptr<do_init> instance();
#if !defined(SSL_OP_NO_COMPRESSION) \
&& (OPENSSL_VERSION_NUMBER >= 0x00908000L)
// Get an empty stack of compression methods, to be used when disabling
// compression.
BOOST_ASIO_DECL static STACK_OF(SSL_COMP)* get_null_compression_methods();
#endif // !defined(SSL_OP_NO_COMPRESSION)
// && (OPENSSL_VERSION_NUMBER >= 0x00908000L)
};
template <bool Do_Init = true>
class openssl_init : private openssl_init_base
{
public:
// Constructor.
openssl_init()
: ref_(instance())
{
using namespace std; // For memmove.
// Ensure openssl_init::instance_ is linked in.
openssl_init* tmp = &instance_;
memmove(&tmp, &tmp, sizeof(openssl_init*));
}
// Destructor.
~openssl_init()
{
}
#if !defined(SSL_OP_NO_COMPRESSION) \
&& (OPENSSL_VERSION_NUMBER >= 0x00908000L)
using openssl_init_base::get_null_compression_methods;
#endif // !defined(SSL_OP_NO_COMPRESSION)
// && (OPENSSL_VERSION_NUMBER >= 0x00908000L)
private:
// Instance to force initialisation of openssl at global scope.
static openssl_init instance_;
// Reference to singleton do_init object to ensure that openssl does not get
// cleaned up until the last user has finished with it.
boost::asio::detail::shared_ptr<do_init> ref_;
};
template <bool Do_Init>
openssl_init<Do_Init> openssl_init<Do_Init>::instance_;
} // namespace detail
} // namespace ssl
} // namespace asio
} // namespace boost
#include <boost/asio/detail/pop_options.hpp>
#if defined(BOOST_ASIO_HEADER_ONLY)
# include <boost/asio/ssl/detail/impl/openssl_init.ipp>
#endif // defined(BOOST_ASIO_HEADER_ONLY)
#endif // BOOST_ASIO_SSL_DETAIL_OPENSSL_INIT_HPP

View File

@@ -0,0 +1,34 @@
//
// ssl/detail/openssl_types.hpp
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2022 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef BOOST_ASIO_SSL_DETAIL_OPENSSL_TYPES_HPP
#define BOOST_ASIO_SSL_DETAIL_OPENSSL_TYPES_HPP
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
#include <boost/asio/detail/config.hpp>
#include <boost/asio/detail/socket_types.hpp>
#if defined(BOOST_ASIO_USE_WOLFSSL)
# include <wolfssl/options.h>
#endif // defined(BOOST_ASIO_USE_WOLFSSL)
#include <openssl/conf.h>
#include <openssl/ssl.h>
#if !defined(OPENSSL_NO_ENGINE)
# include <openssl/engine.h>
#endif // !defined(OPENSSL_NO_ENGINE)
#include <openssl/dh.h>
#include <openssl/err.h>
#include <openssl/rsa.h>
#include <openssl/x509.h>
#include <openssl/x509v3.h>
#endif // BOOST_ASIO_SSL_DETAIL_OPENSSL_TYPES_HPP

View File

@@ -0,0 +1,68 @@
//
// ssl/detail/password_callback.hpp
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2022 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef BOOST_ASIO_SSL_DETAIL_PASSWORD_CALLBACK_HPP
#define BOOST_ASIO_SSL_DETAIL_PASSWORD_CALLBACK_HPP
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
#include <boost/asio/detail/config.hpp>
#include <cstddef>
#include <string>
#include <boost/asio/ssl/context_base.hpp>
#include <boost/asio/detail/push_options.hpp>
namespace boost {
namespace asio {
namespace ssl {
namespace detail {
class password_callback_base
{
public:
virtual ~password_callback_base()
{
}
virtual std::string call(std::size_t size,
context_base::password_purpose purpose) = 0;
};
template <typename PasswordCallback>
class password_callback : public password_callback_base
{
public:
explicit password_callback(PasswordCallback callback)
: callback_(callback)
{
}
virtual std::string call(std::size_t size,
context_base::password_purpose purpose)
{
return callback_(size, purpose);
}
private:
PasswordCallback callback_;
};
} // namespace detail
} // namespace ssl
} // namespace asio
} // namespace boost
#include <boost/asio/detail/pop_options.hpp>
#endif // BOOST_ASIO_SSL_DETAIL_PASSWORD_CALLBACK_HPP

View File

@@ -0,0 +1,74 @@
//
// ssl/detail/read_op.hpp
// ~~~~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2022 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef BOOST_ASIO_SSL_DETAIL_READ_OP_HPP
#define BOOST_ASIO_SSL_DETAIL_READ_OP_HPP
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
#include <boost/asio/detail/config.hpp>
#include <boost/asio/detail/buffer_sequence_adapter.hpp>
#include <boost/asio/ssl/detail/engine.hpp>
#include <boost/asio/detail/push_options.hpp>
namespace boost {
namespace asio {
namespace ssl {
namespace detail {
template <typename MutableBufferSequence>
class read_op
{
public:
static BOOST_ASIO_CONSTEXPR const char* tracking_name()
{
return "ssl::stream<>::async_read_some";
}
read_op(const MutableBufferSequence& buffers)
: buffers_(buffers)
{
}
engine::want operator()(engine& eng,
boost::system::error_code& ec,
std::size_t& bytes_transferred) const
{
boost::asio::mutable_buffer buffer =
boost::asio::detail::buffer_sequence_adapter<boost::asio::mutable_buffer,
MutableBufferSequence>::first(buffers_);
return eng.read(buffer, ec, bytes_transferred);
}
template <typename Handler>
void call_handler(Handler& handler,
const boost::system::error_code& ec,
const std::size_t& bytes_transferred) const
{
BOOST_ASIO_MOVE_OR_LVALUE(Handler)(handler)(ec, bytes_transferred);
}
private:
MutableBufferSequence buffers_;
};
} // namespace detail
} // namespace ssl
} // namespace asio
} // namespace boost
#include <boost/asio/detail/pop_options.hpp>
#endif // BOOST_ASIO_SSL_DETAIL_READ_OP_HPP

View File

@@ -0,0 +1,71 @@
//
// ssl/detail/shutdown_op.hpp
// ~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2022 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef BOOST_ASIO_SSL_DETAIL_SHUTDOWN_OP_HPP
#define BOOST_ASIO_SSL_DETAIL_SHUTDOWN_OP_HPP
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
#include <boost/asio/detail/config.hpp>
#include <boost/asio/ssl/detail/engine.hpp>
#include <boost/asio/detail/push_options.hpp>
namespace boost {
namespace asio {
namespace ssl {
namespace detail {
class shutdown_op
{
public:
static BOOST_ASIO_CONSTEXPR const char* tracking_name()
{
return "ssl::stream<>::async_shutdown";
}
engine::want operator()(engine& eng,
boost::system::error_code& ec,
std::size_t& bytes_transferred) const
{
bytes_transferred = 0;
return eng.shutdown(ec);
}
template <typename Handler>
void call_handler(Handler& handler,
const boost::system::error_code& ec,
const std::size_t&) const
{
if (ec == boost::asio::error::eof)
{
// The engine only generates an eof when the shutdown notification has
// been received from the peer. This indicates that the shutdown has
// completed successfully, and thus need not be passed on to the handler.
BOOST_ASIO_MOVE_OR_LVALUE(Handler)(handler)(boost::system::error_code());
}
else
{
BOOST_ASIO_MOVE_OR_LVALUE(Handler)(handler)(ec);
}
}
};
} // namespace detail
} // namespace ssl
} // namespace asio
} // namespace boost
#include <boost/asio/detail/pop_options.hpp>
#endif // BOOST_ASIO_SSL_DETAIL_SHUTDOWN_OP_HPP

View File

@@ -0,0 +1,223 @@
//
// ssl/detail/stream_core.hpp
// ~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2022 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef BOOST_ASIO_SSL_DETAIL_STREAM_CORE_HPP
#define BOOST_ASIO_SSL_DETAIL_STREAM_CORE_HPP
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
#include <boost/asio/detail/config.hpp>
#if defined(BOOST_ASIO_HAS_BOOST_DATE_TIME)
# include <boost/asio/deadline_timer.hpp>
#else // defined(BOOST_ASIO_HAS_BOOST_DATE_TIME)
# include <boost/asio/steady_timer.hpp>
#endif // defined(BOOST_ASIO_HAS_BOOST_DATE_TIME)
#include <boost/asio/ssl/detail/engine.hpp>
#include <boost/asio/buffer.hpp>
#include <boost/asio/detail/push_options.hpp>
namespace boost {
namespace asio {
namespace ssl {
namespace detail {
struct stream_core
{
// According to the OpenSSL documentation, this is the buffer size that is
// sufficient to hold the largest possible TLS record.
enum { max_tls_record_size = 17 * 1024 };
template <typename Executor>
stream_core(SSL_CTX* context, const Executor& ex)
: engine_(context),
pending_read_(ex),
pending_write_(ex),
output_buffer_space_(max_tls_record_size),
output_buffer_(boost::asio::buffer(output_buffer_space_)),
input_buffer_space_(max_tls_record_size),
input_buffer_(boost::asio::buffer(input_buffer_space_))
{
pending_read_.expires_at(neg_infin());
pending_write_.expires_at(neg_infin());
}
template <typename Executor>
stream_core(SSL* ssl_impl, const Executor& ex)
: engine_(ssl_impl),
pending_read_(ex),
pending_write_(ex),
output_buffer_space_(max_tls_record_size),
output_buffer_(boost::asio::buffer(output_buffer_space_)),
input_buffer_space_(max_tls_record_size),
input_buffer_(boost::asio::buffer(input_buffer_space_))
{
pending_read_.expires_at(neg_infin());
pending_write_.expires_at(neg_infin());
}
#if defined(BOOST_ASIO_HAS_MOVE)
stream_core(stream_core&& other)
: engine_(BOOST_ASIO_MOVE_CAST(engine)(other.engine_)),
#if defined(BOOST_ASIO_HAS_BOOST_DATE_TIME)
pending_read_(
BOOST_ASIO_MOVE_CAST(boost::asio::deadline_timer)(
other.pending_read_)),
pending_write_(
BOOST_ASIO_MOVE_CAST(boost::asio::deadline_timer)(
other.pending_write_)),
#else // defined(BOOST_ASIO_HAS_BOOST_DATE_TIME)
pending_read_(
BOOST_ASIO_MOVE_CAST(boost::asio::steady_timer)(
other.pending_read_)),
pending_write_(
BOOST_ASIO_MOVE_CAST(boost::asio::steady_timer)(
other.pending_write_)),
#endif // defined(BOOST_ASIO_HAS_BOOST_DATE_TIME)
output_buffer_space_(
BOOST_ASIO_MOVE_CAST(std::vector<unsigned char>)(
other.output_buffer_space_)),
output_buffer_(other.output_buffer_),
input_buffer_space_(
BOOST_ASIO_MOVE_CAST(std::vector<unsigned char>)(
other.input_buffer_space_)),
input_buffer_(other.input_buffer_),
input_(other.input_)
{
other.output_buffer_ = boost::asio::mutable_buffer(0, 0);
other.input_buffer_ = boost::asio::mutable_buffer(0, 0);
other.input_ = boost::asio::const_buffer(0, 0);
}
#endif // defined(BOOST_ASIO_HAS_MOVE)
~stream_core()
{
}
#if defined(BOOST_ASIO_HAS_MOVE)
stream_core& operator=(stream_core&& other)
{
if (this != &other)
{
engine_ = BOOST_ASIO_MOVE_CAST(engine)(other.engine_);
#if defined(BOOST_ASIO_HAS_BOOST_DATE_TIME)
pending_read_ =
BOOST_ASIO_MOVE_CAST(boost::asio::deadline_timer)(
other.pending_read_);
pending_write_ =
BOOST_ASIO_MOVE_CAST(boost::asio::deadline_timer)(
other.pending_write_);
#else // defined(BOOST_ASIO_HAS_BOOST_DATE_TIME)
pending_read_ =
BOOST_ASIO_MOVE_CAST(boost::asio::steady_timer)(
other.pending_read_);
pending_write_ =
BOOST_ASIO_MOVE_CAST(boost::asio::steady_timer)(
other.pending_write_);
#endif // defined(BOOST_ASIO_HAS_BOOST_DATE_TIME)
output_buffer_space_ =
BOOST_ASIO_MOVE_CAST(std::vector<unsigned char>)(
other.output_buffer_space_);
output_buffer_ = other.output_buffer_;
input_buffer_space_ =
BOOST_ASIO_MOVE_CAST(std::vector<unsigned char>)(
other.input_buffer_space_);
input_buffer_ = other.input_buffer_;
input_ = other.input_;
other.output_buffer_ = boost::asio::mutable_buffer(0, 0);
other.input_buffer_ = boost::asio::mutable_buffer(0, 0);
other.input_ = boost::asio::const_buffer(0, 0);
}
return *this;
}
#endif // defined(BOOST_ASIO_HAS_MOVE)
// The SSL engine.
engine engine_;
#if defined(BOOST_ASIO_HAS_BOOST_DATE_TIME)
// Timer used for storing queued read operations.
boost::asio::deadline_timer pending_read_;
// Timer used for storing queued write operations.
boost::asio::deadline_timer pending_write_;
// Helper function for obtaining a time value that always fires.
static boost::asio::deadline_timer::time_type neg_infin()
{
return boost::posix_time::neg_infin;
}
// Helper function for obtaining a time value that never fires.
static boost::asio::deadline_timer::time_type pos_infin()
{
return boost::posix_time::pos_infin;
}
// Helper function to get a timer's expiry time.
static boost::asio::deadline_timer::time_type expiry(
const boost::asio::deadline_timer& timer)
{
return timer.expires_at();
}
#else // defined(BOOST_ASIO_HAS_BOOST_DATE_TIME)
// Timer used for storing queued read operations.
boost::asio::steady_timer pending_read_;
// Timer used for storing queued write operations.
boost::asio::steady_timer pending_write_;
// Helper function for obtaining a time value that always fires.
static boost::asio::steady_timer::time_point neg_infin()
{
return (boost::asio::steady_timer::time_point::min)();
}
// Helper function for obtaining a time value that never fires.
static boost::asio::steady_timer::time_point pos_infin()
{
return (boost::asio::steady_timer::time_point::max)();
}
// Helper function to get a timer's expiry time.
static boost::asio::steady_timer::time_point expiry(
const boost::asio::steady_timer& timer)
{
return timer.expiry();
}
#endif // defined(BOOST_ASIO_HAS_BOOST_DATE_TIME)
// Buffer space used to prepare output intended for the transport.
std::vector<unsigned char> output_buffer_space_;
// A buffer that may be used to prepare output intended for the transport.
boost::asio::mutable_buffer output_buffer_;
// Buffer space used to read input intended for the engine.
std::vector<unsigned char> input_buffer_space_;
// A buffer that may be used to read input intended for the engine.
boost::asio::mutable_buffer input_buffer_;
// The buffer pointing to the engine's unconsumed input.
boost::asio::const_buffer input_;
};
} // namespace detail
} // namespace ssl
} // namespace asio
} // namespace boost
#include <boost/asio/detail/pop_options.hpp>
#endif // BOOST_ASIO_SSL_DETAIL_STREAM_CORE_HPP

View File

@@ -0,0 +1,64 @@
//
// ssl/detail/verify_callback.hpp
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2022 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef BOOST_ASIO_SSL_DETAIL_VERIFY_CALLBACK_HPP
#define BOOST_ASIO_SSL_DETAIL_VERIFY_CALLBACK_HPP
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
#include <boost/asio/detail/config.hpp>
#include <boost/asio/ssl/verify_context.hpp>
#include <boost/asio/detail/push_options.hpp>
namespace boost {
namespace asio {
namespace ssl {
namespace detail {
class verify_callback_base
{
public:
virtual ~verify_callback_base()
{
}
virtual bool call(bool preverified, verify_context& ctx) = 0;
};
template <typename VerifyCallback>
class verify_callback : public verify_callback_base
{
public:
explicit verify_callback(VerifyCallback callback)
: callback_(callback)
{
}
virtual bool call(bool preverified, verify_context& ctx)
{
return callback_(preverified, ctx);
}
private:
VerifyCallback callback_;
};
} // namespace detail
} // namespace ssl
} // namespace asio
} // namespace boost
#include <boost/asio/detail/pop_options.hpp>
#endif // BOOST_ASIO_SSL_DETAIL_VERIFY_CALLBACK_HPP

View File

@@ -0,0 +1,78 @@
//
// ssl/detail/write_op.hpp
// ~~~~~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2022 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef BOOST_ASIO_SSL_DETAIL_WRITE_OP_HPP
#define BOOST_ASIO_SSL_DETAIL_WRITE_OP_HPP
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
#include <boost/asio/detail/config.hpp>
#include <boost/asio/detail/buffer_sequence_adapter.hpp>
#include <boost/asio/ssl/detail/engine.hpp>
#include <boost/asio/detail/push_options.hpp>
namespace boost {
namespace asio {
namespace ssl {
namespace detail {
template <typename ConstBufferSequence>
class write_op
{
public:
static BOOST_ASIO_CONSTEXPR const char* tracking_name()
{
return "ssl::stream<>::async_write_some";
}
write_op(const ConstBufferSequence& buffers)
: buffers_(buffers)
{
}
engine::want operator()(engine& eng,
boost::system::error_code& ec,
std::size_t& bytes_transferred) const
{
unsigned char storage[
boost::asio::detail::buffer_sequence_adapter<boost::asio::const_buffer,
ConstBufferSequence>::linearisation_storage_size];
boost::asio::const_buffer buffer =
boost::asio::detail::buffer_sequence_adapter<boost::asio::const_buffer,
ConstBufferSequence>::linearise(buffers_, boost::asio::buffer(storage));
return eng.write(buffer, ec, bytes_transferred);
}
template <typename Handler>
void call_handler(Handler& handler,
const boost::system::error_code& ec,
const std::size_t& bytes_transferred) const
{
BOOST_ASIO_MOVE_OR_LVALUE(Handler)(handler)(ec, bytes_transferred);
}
private:
ConstBufferSequence buffers_;
};
} // namespace detail
} // namespace ssl
} // namespace asio
} // namespace boost
#include <boost/asio/detail/pop_options.hpp>
#endif // BOOST_ASIO_SSL_DETAIL_WRITE_OP_HPP

View File

@@ -0,0 +1,129 @@
//
// ssl/error.hpp
// ~~~~~~~~~~~~~
//
// Copyright (c) 2003-2022 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef BOOST_ASIO_SSL_ERROR_HPP
#define BOOST_ASIO_SSL_ERROR_HPP
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
#include <boost/asio/detail/config.hpp>
#include <boost/system/error_code.hpp>
#include <boost/asio/ssl/detail/openssl_types.hpp>
#include <boost/asio/detail/push_options.hpp>
namespace boost {
namespace asio {
namespace error {
enum ssl_errors
{
// Error numbers are those produced by openssl.
};
extern BOOST_ASIO_DECL
const boost::system::error_category& get_ssl_category();
static const boost::system::error_category&
ssl_category BOOST_ASIO_UNUSED_VARIABLE
= boost::asio::error::get_ssl_category();
} // namespace error
namespace ssl {
namespace error {
enum stream_errors
{
#if defined(GENERATING_DOCUMENTATION)
/// The underlying stream closed before the ssl stream gracefully shut down.
stream_truncated,
/// The underlying SSL library returned a system error without providing
/// further information.
unspecified_system_error,
/// The underlying SSL library generated an unexpected result from a function
/// call.
unexpected_result
#else // defined(GENERATING_DOCUMENTATION)
# if (OPENSSL_VERSION_NUMBER < 0x10100000L) \
&& !defined(OPENSSL_IS_BORINGSSL) \
&& !defined(BOOST_ASIO_USE_WOLFSSL)
stream_truncated = ERR_PACK(ERR_LIB_SSL, 0, SSL_R_SHORT_READ),
# else
stream_truncated = 1,
# endif
unspecified_system_error = 2,
unexpected_result = 3
#endif // defined(GENERATING_DOCUMENTATION)
};
extern BOOST_ASIO_DECL
const boost::system::error_category& get_stream_category();
static const boost::system::error_category&
stream_category BOOST_ASIO_UNUSED_VARIABLE
= boost::asio::ssl::error::get_stream_category();
} // namespace error
} // namespace ssl
} // namespace asio
} // namespace boost
namespace boost {
namespace system {
template<> struct is_error_code_enum<boost::asio::error::ssl_errors>
{
static const bool value = true;
};
template<> struct is_error_code_enum<boost::asio::ssl::error::stream_errors>
{
static const bool value = true;
};
} // namespace system
} // namespace boost
namespace boost {
namespace asio {
namespace error {
inline boost::system::error_code make_error_code(ssl_errors e)
{
return boost::system::error_code(
static_cast<int>(e), get_ssl_category());
}
} // namespace error
namespace ssl {
namespace error {
inline boost::system::error_code make_error_code(stream_errors e)
{
return boost::system::error_code(
static_cast<int>(e), get_stream_category());
}
} // namespace error
} // namespace ssl
} // namespace asio
} // namespace boost
#include <boost/asio/detail/pop_options.hpp>
#if defined(BOOST_ASIO_HEADER_ONLY)
# include <boost/asio/ssl/impl/error.ipp>
#endif // defined(BOOST_ASIO_HEADER_ONLY)
#endif // BOOST_ASIO_SSL_ERROR_HPP

View File

@@ -0,0 +1,92 @@
//
// ssl/host_name_verification.hpp
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2022 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef BOOST_ASIO_SSL_HOST_NAME_VERIFICATION_HPP
#define BOOST_ASIO_SSL_HOST_NAME_VERIFICATION_HPP
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
#include <boost/asio/detail/config.hpp>
#include <string>
#include <boost/asio/ssl/detail/openssl_types.hpp>
#include <boost/asio/ssl/verify_context.hpp>
#include <boost/asio/detail/push_options.hpp>
namespace boost {
namespace asio {
namespace ssl {
/// Verifies a certificate against a host_name according to the rules described
/// in RFC 6125.
/**
* @par Example
* The following example shows how to synchronously open a secure connection to
* a given host name:
* @code
* using boost::asio::ip::tcp;
* namespace ssl = boost::asio::ssl;
* typedef ssl::stream<tcp::socket> ssl_socket;
*
* // Create a context that uses the default paths for finding CA certificates.
* ssl::context ctx(ssl::context::sslv23);
* ctx.set_default_verify_paths();
*
* // Open a socket and connect it to the remote host.
* boost::asio::io_context io_context;
* ssl_socket sock(io_context, ctx);
* tcp::resolver resolver(io_context);
* tcp::resolver::query query("host.name", "https");
* boost::asio::connect(sock.lowest_layer(), resolver.resolve(query));
* sock.lowest_layer().set_option(tcp::no_delay(true));
*
* // Perform SSL handshake and verify the remote host's certificate.
* sock.set_verify_mode(ssl::verify_peer);
* sock.set_verify_callback(ssl::host_name_verification("host.name"));
* sock.handshake(ssl_socket::client);
*
* // ... read and write as normal ...
* @endcode
*/
class host_name_verification
{
public:
/// The type of the function object's result.
typedef bool result_type;
/// Constructor.
explicit host_name_verification(const std::string& host)
: host_(host)
{
}
/// Perform certificate verification.
BOOST_ASIO_DECL bool operator()(bool preverified, verify_context& ctx) const;
private:
// Helper function to check a host name against an IPv4 address
// The host name to be checked.
std::string host_;
};
} // namespace ssl
} // namespace asio
} // namespace boost
#include <boost/asio/detail/pop_options.hpp>
#if defined(BOOST_ASIO_HEADER_ONLY)
# include <boost/asio/ssl/impl/host_name_verification.ipp>
#endif // defined(BOOST_ASIO_HEADER_ONLY)
#endif // BOOST_ASIO_SSL_HOST_NAME_VERIFICATION_HPP

View File

@@ -0,0 +1,69 @@
//
// ssl/impl/context.hpp
// ~~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2005 Voipster / Indrek dot Juhani at voipster dot com
// Copyright (c) 2005-2022 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef BOOST_ASIO_SSL_IMPL_CONTEXT_HPP
#define BOOST_ASIO_SSL_IMPL_CONTEXT_HPP
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
#include <boost/asio/detail/config.hpp>
#include <boost/asio/detail/throw_error.hpp>
#include <boost/asio/detail/push_options.hpp>
namespace boost {
namespace asio {
namespace ssl {
template <typename VerifyCallback>
void context::set_verify_callback(VerifyCallback callback)
{
boost::system::error_code ec;
this->set_verify_callback(callback, ec);
boost::asio::detail::throw_error(ec, "set_verify_callback");
}
template <typename VerifyCallback>
BOOST_ASIO_SYNC_OP_VOID context::set_verify_callback(
VerifyCallback callback, boost::system::error_code& ec)
{
do_set_verify_callback(
new detail::verify_callback<VerifyCallback>(callback), ec);
BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
}
template <typename PasswordCallback>
void context::set_password_callback(PasswordCallback callback)
{
boost::system::error_code ec;
this->set_password_callback(callback, ec);
boost::asio::detail::throw_error(ec, "set_password_callback");
}
template <typename PasswordCallback>
BOOST_ASIO_SYNC_OP_VOID context::set_password_callback(
PasswordCallback callback, boost::system::error_code& ec)
{
do_set_password_callback(
new detail::password_callback<PasswordCallback>(callback), ec);
BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
}
} // namespace ssl
} // namespace asio
} // namespace boost
#include <boost/asio/detail/pop_options.hpp>
#endif // BOOST_ASIO_SSL_IMPL_CONTEXT_HPP

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,126 @@
//
// ssl/impl/error.ipp
// ~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2022 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef BOOST_ASIO_SSL_IMPL_ERROR_IPP
#define BOOST_ASIO_SSL_IMPL_ERROR_IPP
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
#include <boost/asio/detail/config.hpp>
#include <boost/asio/ssl/error.hpp>
#include <boost/asio/ssl/detail/openssl_init.hpp>
#include <boost/asio/detail/push_options.hpp>
namespace boost {
namespace asio {
namespace error {
namespace detail {
class ssl_category : public boost::system::error_category
{
public:
const char* name() const BOOST_ASIO_ERROR_CATEGORY_NOEXCEPT
{
return "asio.ssl";
}
std::string message(int value) const
{
const char* reason = ::ERR_reason_error_string(value);
if (reason)
{
const char* lib = ::ERR_lib_error_string(value);
#if (OPENSSL_VERSION_NUMBER < 0x30000000L)
const char* func = ::ERR_func_error_string(value);
#else // (OPENSSL_VERSION_NUMBER < 0x30000000L)
const char* func = 0;
#endif // (OPENSSL_VERSION_NUMBER < 0x30000000L)
std::string result(reason);
if (lib || func)
{
result += " (";
if (lib)
result += lib;
if (lib && func)
result += ", ";
if (func)
result += func;
result += ")";
}
return result;
}
return "asio.ssl error";
}
};
} // namespace detail
const boost::system::error_category& get_ssl_category()
{
static detail::ssl_category instance;
return instance;
}
} // namespace error
namespace ssl {
namespace error {
#if (OPENSSL_VERSION_NUMBER < 0x10100000L) && !defined(OPENSSL_IS_BORINGSSL)
const boost::system::error_category& get_stream_category()
{
return boost::asio::error::get_ssl_category();
}
#else
namespace detail {
class stream_category : public boost::system::error_category
{
public:
const char* name() const BOOST_ASIO_ERROR_CATEGORY_NOEXCEPT
{
return "asio.ssl.stream";
}
std::string message(int value) const
{
switch (value)
{
case stream_truncated: return "stream truncated";
case unspecified_system_error: return "unspecified system error";
case unexpected_result: return "unexpected result";
default: return "asio.ssl.stream error";
}
}
};
} // namespace detail
const boost::system::error_category& get_stream_category()
{
static detail::stream_category instance;
return instance;
}
#endif
} // namespace error
} // namespace ssl
} // namespace asio
} // namespace boost
#include <boost/asio/detail/pop_options.hpp>
#endif // BOOST_ASIO_SSL_IMPL_ERROR_IPP

View File

@@ -0,0 +1,75 @@
//
// ssl/impl/host_name_verification.ipp
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2022 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef BOOST_ASIO_SSL_IMPL_HOST_NAME_VERIFICATION_IPP
#define BOOST_ASIO_SSL_IMPL_HOST_NAME_VERIFICATION_IPP
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
#include <boost/asio/detail/config.hpp>
#include <cctype>
#include <cstring>
#include <boost/asio/ip/address.hpp>
#include <boost/asio/ssl/host_name_verification.hpp>
#include <boost/asio/ssl/detail/openssl_types.hpp>
#include <boost/asio/detail/push_options.hpp>
namespace boost {
namespace asio {
namespace ssl {
bool host_name_verification::operator()(
bool preverified, verify_context& ctx) const
{
using namespace std; // For memcmp.
// Don't bother looking at certificates that have failed pre-verification.
if (!preverified)
return false;
// We're only interested in checking the certificate at the end of the chain.
int depth = X509_STORE_CTX_get_error_depth(ctx.native_handle());
if (depth > 0)
return true;
// Try converting the host name to an address. If it is an address then we
// need to look for an IP address in the certificate rather than a host name.
boost::system::error_code ec;
ip::address address = ip::make_address(host_, ec);
const bool is_address = !ec;
(void)address;
X509* cert = X509_STORE_CTX_get_current_cert(ctx.native_handle());
if (is_address)
{
return X509_check_ip_asc(cert, host_.c_str(), 0) == 1;
}
else
{
char* peername = 0;
const int result = X509_check_host(cert,
host_.c_str(), host_.size(), 0, &peername);
OPENSSL_free(peername);
return result == 1;
}
}
} // namespace ssl
} // namespace asio
} // namespace boost
#include <boost/asio/detail/pop_options.hpp>
#endif // BOOST_ASIO_SSL_IMPL_HOST_NAME_VERIFICATION_IPP

View File

@@ -0,0 +1,166 @@
//
// ssl/impl/rfc2818_verification.ipp
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2022 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef BOOST_ASIO_SSL_IMPL_RFC2818_VERIFICATION_IPP
#define BOOST_ASIO_SSL_IMPL_RFC2818_VERIFICATION_IPP
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
#include <boost/asio/detail/config.hpp>
#if !defined(BOOST_ASIO_NO_DEPRECATED)
#include <cctype>
#include <cstring>
#include <boost/asio/ip/address.hpp>
#include <boost/asio/ssl/rfc2818_verification.hpp>
#include <boost/asio/ssl/detail/openssl_types.hpp>
#include <boost/asio/detail/push_options.hpp>
namespace boost {
namespace asio {
namespace ssl {
bool rfc2818_verification::operator()(
bool preverified, verify_context& ctx) const
{
using namespace std; // For memcmp.
// Don't bother looking at certificates that have failed pre-verification.
if (!preverified)
return false;
// We're only interested in checking the certificate at the end of the chain.
int depth = X509_STORE_CTX_get_error_depth(ctx.native_handle());
if (depth > 0)
return true;
// Try converting the host name to an address. If it is an address then we
// need to look for an IP address in the certificate rather than a host name.
boost::system::error_code ec;
ip::address address = ip::make_address(host_, ec);
bool is_address = !ec;
X509* cert = X509_STORE_CTX_get_current_cert(ctx.native_handle());
// Go through the alternate names in the certificate looking for matching DNS
// or IP address entries.
GENERAL_NAMES* gens = static_cast<GENERAL_NAMES*>(
X509_get_ext_d2i(cert, NID_subject_alt_name, 0, 0));
for (int i = 0; i < sk_GENERAL_NAME_num(gens); ++i)
{
GENERAL_NAME* gen = sk_GENERAL_NAME_value(gens, i);
if (gen->type == GEN_DNS && !is_address)
{
ASN1_IA5STRING* domain = gen->d.dNSName;
if (domain->type == V_ASN1_IA5STRING && domain->data && domain->length)
{
const char* pattern = reinterpret_cast<const char*>(domain->data);
std::size_t pattern_length = domain->length;
if (match_pattern(pattern, pattern_length, host_.c_str()))
{
GENERAL_NAMES_free(gens);
return true;
}
}
}
else if (gen->type == GEN_IPADD && is_address)
{
ASN1_OCTET_STRING* ip_address = gen->d.iPAddress;
if (ip_address->type == V_ASN1_OCTET_STRING && ip_address->data)
{
if (address.is_v4() && ip_address->length == 4)
{
ip::address_v4::bytes_type bytes = address.to_v4().to_bytes();
if (memcmp(bytes.data(), ip_address->data, 4) == 0)
{
GENERAL_NAMES_free(gens);
return true;
}
}
else if (address.is_v6() && ip_address->length == 16)
{
ip::address_v6::bytes_type bytes = address.to_v6().to_bytes();
if (memcmp(bytes.data(), ip_address->data, 16) == 0)
{
GENERAL_NAMES_free(gens);
return true;
}
}
}
}
}
GENERAL_NAMES_free(gens);
// No match in the alternate names, so try the common names. We should only
// use the "most specific" common name, which is the last one in the list.
X509_NAME* name = X509_get_subject_name(cert);
int i = -1;
ASN1_STRING* common_name = 0;
while ((i = X509_NAME_get_index_by_NID(name, NID_commonName, i)) >= 0)
{
X509_NAME_ENTRY* name_entry = X509_NAME_get_entry(name, i);
common_name = X509_NAME_ENTRY_get_data(name_entry);
}
if (common_name && common_name->data && common_name->length)
{
const char* pattern = reinterpret_cast<const char*>(common_name->data);
std::size_t pattern_length = common_name->length;
if (match_pattern(pattern, pattern_length, host_.c_str()))
return true;
}
return false;
}
bool rfc2818_verification::match_pattern(const char* pattern,
std::size_t pattern_length, const char* host)
{
using namespace std; // For tolower.
const char* p = pattern;
const char* p_end = p + pattern_length;
const char* h = host;
while (p != p_end && *h)
{
if (*p == '*')
{
++p;
while (*h && *h != '.')
if (match_pattern(p, p_end - p, h++))
return true;
}
else if (tolower(*p) == tolower(*h))
{
++p;
++h;
}
else
{
return false;
}
}
return p == p_end && !*h;
}
} // namespace ssl
} // namespace asio
} // namespace boost
#include <boost/asio/detail/pop_options.hpp>
#endif // !defined(BOOST_ASIO_NO_DEPRECATED)
#endif // BOOST_ASIO_SSL_IMPL_RFC2818_VERIFICATION_IPP

View File

@@ -0,0 +1,29 @@
//
// impl/ssl/src.hpp
// ~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2022 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef BOOST_ASIO_SSL_IMPL_SRC_HPP
#define BOOST_ASIO_SSL_IMPL_SRC_HPP
#define BOOST_ASIO_SOURCE
#include <boost/asio/detail/config.hpp>
#if defined(BOOST_ASIO_HEADER_ONLY)
# error Do not compile Asio library source with BOOST_ASIO_HEADER_ONLY defined
#endif
#include <boost/asio/ssl/impl/context.ipp>
#include <boost/asio/ssl/impl/error.ipp>
#include <boost/asio/ssl/detail/impl/engine.ipp>
#include <boost/asio/ssl/detail/impl/openssl_init.ipp>
#include <boost/asio/ssl/impl/host_name_verification.ipp>
#include <boost/asio/ssl/impl/rfc2818_verification.ipp>
#endif // BOOST_ASIO_SSL_IMPL_SRC_HPP

View File

@@ -0,0 +1,100 @@
//
// ssl/rfc2818_verification.hpp
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2022 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef BOOST_ASIO_SSL_RFC2818_VERIFICATION_HPP
#define BOOST_ASIO_SSL_RFC2818_VERIFICATION_HPP
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
#include <boost/asio/detail/config.hpp>
#if !defined(BOOST_ASIO_NO_DEPRECATED)
#include <string>
#include <boost/asio/ssl/detail/openssl_types.hpp>
#include <boost/asio/ssl/verify_context.hpp>
#include <boost/asio/detail/push_options.hpp>
namespace boost {
namespace asio {
namespace ssl {
/// (Deprecated. Use ssl::host_name_verification.) Verifies a certificate
/// against a hostname according to the rules described in RFC 2818.
/**
* @par Example
* The following example shows how to synchronously open a secure connection to
* a given host name:
* @code
* using boost::asio::ip::tcp;
* namespace ssl = boost::asio::ssl;
* typedef ssl::stream<tcp::socket> ssl_socket;
*
* // Create a context that uses the default paths for finding CA certificates.
* ssl::context ctx(ssl::context::sslv23);
* ctx.set_default_verify_paths();
*
* // Open a socket and connect it to the remote host.
* boost::asio::io_context io_context;
* ssl_socket sock(io_context, ctx);
* tcp::resolver resolver(io_context);
* tcp::resolver::query query("host.name", "https");
* boost::asio::connect(sock.lowest_layer(), resolver.resolve(query));
* sock.lowest_layer().set_option(tcp::no_delay(true));
*
* // Perform SSL handshake and verify the remote host's certificate.
* sock.set_verify_mode(ssl::verify_peer);
* sock.set_verify_callback(ssl::rfc2818_verification("host.name"));
* sock.handshake(ssl_socket::client);
*
* // ... read and write as normal ...
* @endcode
*/
class rfc2818_verification
{
public:
/// The type of the function object's result.
typedef bool result_type;
/// Constructor.
explicit rfc2818_verification(const std::string& host)
: host_(host)
{
}
/// Perform certificate verification.
BOOST_ASIO_DECL bool operator()(bool preverified, verify_context& ctx) const;
private:
// Helper function to check a host name against a pattern.
BOOST_ASIO_DECL static bool match_pattern(const char* pattern,
std::size_t pattern_length, const char* host);
// Helper function to check a host name against an IPv4 address
// The host name to be checked.
std::string host_;
};
} // namespace ssl
} // namespace asio
} // namespace boost
#include <boost/asio/detail/pop_options.hpp>
#if defined(BOOST_ASIO_HEADER_ONLY)
# include <boost/asio/ssl/impl/rfc2818_verification.ipp>
#endif // defined(BOOST_ASIO_HEADER_ONLY)
#endif // !defined(BOOST_ASIO_NO_DEPRECATED)
#endif // BOOST_ASIO_SSL_RFC2818_VERIFICATION_HPP

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,54 @@
//
// ssl/stream_base.hpp
// ~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2022 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef BOOST_ASIO_SSL_STREAM_BASE_HPP
#define BOOST_ASIO_SSL_STREAM_BASE_HPP
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
#include <boost/asio/detail/config.hpp>
#include <boost/asio/detail/push_options.hpp>
namespace boost {
namespace asio {
namespace ssl {
/// The stream_base class is used as a base for the boost::asio::ssl::stream
/// class template so that we have a common place to define various enums.
class stream_base
{
public:
/// Different handshake types.
enum handshake_type
{
/// Perform handshaking as a client.
client,
/// Perform handshaking as a server.
server
};
protected:
/// Protected destructor to prevent deletion through this type.
~stream_base()
{
}
};
} // namespace ssl
} // namespace asio
} // namespace boost
#include <boost/asio/detail/pop_options.hpp>
#endif // BOOST_ASIO_SSL_STREAM_BASE_HPP

View File

@@ -0,0 +1,69 @@
//
// ssl/verify_context.hpp
// ~~~~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2022 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef BOOST_ASIO_SSL_VERIFY_CONTEXT_HPP
#define BOOST_ASIO_SSL_VERIFY_CONTEXT_HPP
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
#include <boost/asio/detail/config.hpp>
#include <boost/asio/detail/noncopyable.hpp>
#include <boost/asio/ssl/detail/openssl_types.hpp>
#include <boost/asio/detail/push_options.hpp>
namespace boost {
namespace asio {
namespace ssl {
/// A simple wrapper around the X509_STORE_CTX type, used during verification of
/// a peer certificate.
/**
* @note The verify_context does not own the underlying X509_STORE_CTX object.
*/
class verify_context
: private noncopyable
{
public:
/// The native handle type of the verification context.
typedef X509_STORE_CTX* native_handle_type;
/// Constructor.
explicit verify_context(native_handle_type handle)
: handle_(handle)
{
}
/// Get the underlying implementation in the native type.
/**
* This function may be used to obtain the underlying implementation of the
* context. This is intended to allow access to context functionality that is
* not otherwise provided.
*/
native_handle_type native_handle()
{
return handle_;
}
private:
// The underlying native implementation.
native_handle_type handle_;
};
} // namespace ssl
} // namespace asio
} // namespace boost
#include <boost/asio/detail/pop_options.hpp>
#endif // BOOST_ASIO_SSL_VERIFY_CONTEXT_HPP

View File

@@ -0,0 +1,65 @@
//
// ssl/verify_mode.hpp
// ~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2022 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef BOOST_ASIO_SSL_VERIFY_MODE_HPP
#define BOOST_ASIO_SSL_VERIFY_MODE_HPP
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
#include <boost/asio/detail/config.hpp>
#include <boost/asio/ssl/detail/openssl_types.hpp>
#include <boost/asio/detail/push_options.hpp>
namespace boost {
namespace asio {
namespace ssl {
/// Bitmask type for peer verification.
/**
* Possible values are:
*
* @li @ref verify_none
* @li @ref verify_peer
* @li @ref verify_fail_if_no_peer_cert
* @li @ref verify_client_once
*/
typedef int verify_mode;
#if defined(GENERATING_DOCUMENTATION)
/// No verification.
const int verify_none = implementation_defined;
/// Verify the peer.
const int verify_peer = implementation_defined;
/// Fail verification if the peer has no certificate. Ignored unless
/// @ref verify_peer is set.
const int verify_fail_if_no_peer_cert = implementation_defined;
/// Do not request client certificate on renegotiation. Ignored unless
/// @ref verify_peer is set.
const int verify_client_once = implementation_defined;
#else
const int verify_none = SSL_VERIFY_NONE;
const int verify_peer = SSL_VERIFY_PEER;
const int verify_fail_if_no_peer_cert = SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
const int verify_client_once = SSL_VERIFY_CLIENT_ONCE;
#endif
} // namespace ssl
} // namespace asio
} // namespace boost
#include <boost/asio/detail/pop_options.hpp>
#endif // BOOST_ASIO_SSL_VERIFY_MODE_HPP