Chris@16: // Chris@16: // ssl/old/basic_context.hpp Chris@16: // ~~~~~~~~~~~~~~~~~~~~~~~~~ Chris@16: // Chris@16: // Copyright (c) 2005 Voipster / Indrek dot Juhani at voipster dot com Chris@101: // Copyright (c) 2005-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com) Chris@16: // Chris@16: // Distributed under the Boost Software License, Version 1.0. (See accompanying Chris@16: // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) Chris@16: // Chris@16: Chris@16: #ifndef BOOST_ASIO_SSL_OLD_BASIC_CONTEXT_HPP Chris@16: #define BOOST_ASIO_SSL_OLD_BASIC_CONTEXT_HPP Chris@16: Chris@16: #if defined(_MSC_VER) && (_MSC_VER >= 1200) Chris@16: # pragma once Chris@16: #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: #include Chris@16: Chris@16: namespace boost { Chris@16: namespace asio { Chris@16: namespace ssl { Chris@16: namespace old { Chris@16: Chris@16: /// SSL context. Chris@16: template Chris@16: class basic_context Chris@16: : public context_base, Chris@16: private boost::noncopyable Chris@16: { Chris@16: public: Chris@16: /// The type of the service that will be used to provide context operations. Chris@16: typedef Service service_type; Chris@16: Chris@16: /// The native implementation type of the SSL context. Chris@16: typedef typename service_type::impl_type impl_type; Chris@16: Chris@16: /// Constructor. Chris@16: basic_context(boost::asio::io_service& io_service, method m) Chris@16: : service_(boost::asio::use_service(io_service)), Chris@16: impl_(service_.null()) Chris@16: { Chris@16: service_.create(impl_, m); Chris@16: } Chris@16: Chris@16: /// Destructor. Chris@16: ~basic_context() Chris@16: { Chris@16: service_.destroy(impl_); Chris@16: } Chris@16: Chris@16: /// Get the underlying implementation in the native type. Chris@16: /** Chris@16: * This function may be used to obtain the underlying implementation of the Chris@16: * context. This is intended to allow access to context functionality that is Chris@16: * not otherwise provided. Chris@16: */ Chris@16: impl_type impl() Chris@16: { Chris@16: return impl_; Chris@16: } Chris@16: Chris@16: /// Set options on the context. Chris@16: /** Chris@16: * This function may be used to configure the SSL options used by the context. Chris@16: * Chris@16: * @param o A bitmask of options. The available option values are defined in Chris@16: * the context_base class. The options are bitwise-ored with any existing Chris@16: * value for the options. Chris@16: * Chris@16: * @throws boost::system::system_error Thrown on failure. Chris@16: */ Chris@16: void set_options(options o) Chris@16: { Chris@16: boost::system::error_code ec; Chris@16: service_.set_options(impl_, o, ec); Chris@16: boost::asio::detail::throw_error(ec); Chris@16: } Chris@16: Chris@16: /// Set options on the context. Chris@16: /** Chris@16: * This function may be used to configure the SSL options used by the context. Chris@16: * Chris@16: * @param o A bitmask of options. The available option values are defined in Chris@16: * the context_base class. The options are bitwise-ored with any existing Chris@16: * value for the options. Chris@16: * Chris@16: * @param ec Set to indicate what error occurred, if any. Chris@16: */ Chris@16: boost::system::error_code set_options(options o, Chris@16: boost::system::error_code& ec) Chris@16: { Chris@16: return service_.set_options(impl_, o, ec); Chris@16: } Chris@16: Chris@16: /// Set the peer verification mode. Chris@16: /** Chris@16: * This function may be used to configure the peer verification mode used by Chris@16: * the context. Chris@16: * Chris@16: * @param v A bitmask of peer verification modes. The available verify_mode Chris@16: * values are defined in the context_base class. Chris@16: * Chris@16: * @throws boost::system::system_error Thrown on failure. Chris@16: */ Chris@16: void set_verify_mode(verify_mode v) Chris@16: { Chris@16: boost::system::error_code ec; Chris@16: service_.set_verify_mode(impl_, v, ec); Chris@16: boost::asio::detail::throw_error(ec); Chris@16: } Chris@16: Chris@16: /// Set the peer verification mode. Chris@16: /** Chris@16: * This function may be used to configure the peer verification mode used by Chris@16: * the context. Chris@16: * Chris@16: * @param v A bitmask of peer verification modes. The available verify_mode Chris@16: * values are defined in the context_base class. Chris@16: * Chris@16: * @param ec Set to indicate what error occurred, if any. Chris@16: */ Chris@16: boost::system::error_code set_verify_mode(verify_mode v, Chris@16: boost::system::error_code& ec) Chris@16: { Chris@16: return service_.set_verify_mode(impl_, v, ec); Chris@16: } Chris@16: Chris@16: /// Load a certification authority file for performing verification. Chris@16: /** Chris@16: * This function is used to load one or more trusted certification authorities Chris@16: * from a file. Chris@16: * Chris@16: * @param filename The name of a file containing certification authority Chris@16: * certificates in PEM format. Chris@16: * Chris@16: * @throws boost::system::system_error Thrown on failure. Chris@16: */ Chris@16: void load_verify_file(const std::string& filename) Chris@16: { Chris@16: boost::system::error_code ec; Chris@16: service_.load_verify_file(impl_, filename, ec); Chris@16: boost::asio::detail::throw_error(ec); Chris@16: } Chris@16: Chris@16: /// Load a certification authority file for performing verification. Chris@16: /** Chris@16: * This function is used to load the certificates for one or more trusted Chris@16: * certification authorities from a file. Chris@16: * Chris@16: * @param filename The name of a file containing certification authority Chris@16: * certificates in PEM format. Chris@16: * Chris@16: * @param ec Set to indicate what error occurred, if any. Chris@16: */ Chris@16: boost::system::error_code load_verify_file(const std::string& filename, Chris@16: boost::system::error_code& ec) Chris@16: { Chris@16: return service_.load_verify_file(impl_, filename, ec); Chris@16: } Chris@16: Chris@16: /// Add a directory containing certificate authority files to be used for Chris@16: /// performing verification. Chris@16: /** Chris@16: * This function is used to specify the name of a directory containing Chris@16: * certification authority certificates. Each file in the directory must Chris@16: * contain a single certificate. The files must be named using the subject Chris@16: * name's hash and an extension of ".0". Chris@16: * Chris@16: * @param path The name of a directory containing the certificates. Chris@16: * Chris@16: * @throws boost::system::system_error Thrown on failure. Chris@16: */ Chris@16: void add_verify_path(const std::string& path) Chris@16: { Chris@16: boost::system::error_code ec; Chris@16: service_.add_verify_path(impl_, path, ec); Chris@16: boost::asio::detail::throw_error(ec); Chris@16: } Chris@16: Chris@16: /// Add a directory containing certificate authority files to be used for Chris@16: /// performing verification. Chris@16: /** Chris@16: * This function is used to specify the name of a directory containing Chris@16: * certification authority certificates. Each file in the directory must Chris@16: * contain a single certificate. The files must be named using the subject Chris@16: * name's hash and an extension of ".0". Chris@16: * Chris@16: * @param path The name of a directory containing the certificates. Chris@16: * Chris@16: * @param ec Set to indicate what error occurred, if any. Chris@16: */ Chris@16: boost::system::error_code add_verify_path(const std::string& path, Chris@16: boost::system::error_code& ec) Chris@16: { Chris@16: return service_.add_verify_path(impl_, path, ec); Chris@16: } Chris@16: Chris@16: /// Use a certificate from a file. Chris@16: /** Chris@16: * This function is used to load a certificate into the context from a file. Chris@16: * Chris@16: * @param filename The name of the file containing the certificate. Chris@16: * Chris@16: * @param format The file format (ASN.1 or PEM). Chris@16: * Chris@16: * @throws boost::system::system_error Thrown on failure. Chris@16: */ Chris@16: void use_certificate_file(const std::string& filename, file_format format) Chris@16: { Chris@16: boost::system::error_code ec; Chris@16: service_.use_certificate_file(impl_, filename, format, ec); Chris@16: boost::asio::detail::throw_error(ec); Chris@16: } Chris@16: Chris@16: /// Use a certificate from a file. Chris@16: /** Chris@16: * This function is used to load a certificate into the context from a file. Chris@16: * Chris@16: * @param filename The name of the file containing the certificate. Chris@16: * Chris@16: * @param format The file format (ASN.1 or PEM). Chris@16: * Chris@16: * @param ec Set to indicate what error occurred, if any. Chris@16: */ Chris@16: boost::system::error_code use_certificate_file(const std::string& filename, Chris@16: file_format format, boost::system::error_code& ec) Chris@16: { Chris@16: return service_.use_certificate_file(impl_, filename, format, ec); Chris@16: } Chris@16: Chris@16: /// Use a certificate chain from a file. Chris@16: /** Chris@16: * This function is used to load a certificate chain into the context from a Chris@16: * file. Chris@16: * Chris@16: * @param filename The name of the file containing the certificate. The file Chris@16: * must use the PEM format. Chris@16: * Chris@16: * @throws boost::system::system_error Thrown on failure. Chris@16: */ Chris@16: void use_certificate_chain_file(const std::string& filename) Chris@16: { Chris@16: boost::system::error_code ec; Chris@16: service_.use_certificate_chain_file(impl_, filename, ec); Chris@16: boost::asio::detail::throw_error(ec); Chris@16: } Chris@16: Chris@16: /// Use a certificate chain from a file. Chris@16: /** Chris@16: * This function is used to load a certificate chain into the context from a Chris@16: * file. Chris@16: * Chris@16: * @param filename The name of the file containing the certificate. The file Chris@16: * must use the PEM format. Chris@16: * Chris@16: * @param ec Set to indicate what error occurred, if any. Chris@16: */ Chris@16: boost::system::error_code use_certificate_chain_file( Chris@16: const std::string& filename, boost::system::error_code& ec) Chris@16: { Chris@16: return service_.use_certificate_chain_file(impl_, filename, ec); Chris@16: } Chris@16: Chris@16: /// Use a private key from a file. Chris@16: /** Chris@16: * This function is used to load a private key into the context from a file. Chris@16: * Chris@16: * @param filename The name of the file containing the private key. Chris@16: * Chris@16: * @param format The file format (ASN.1 or PEM). Chris@16: * Chris@16: * @throws boost::system::system_error Thrown on failure. Chris@16: */ Chris@16: void use_private_key_file(const std::string& filename, file_format format) Chris@16: { Chris@16: boost::system::error_code ec; Chris@16: service_.use_private_key_file(impl_, filename, format, ec); Chris@16: boost::asio::detail::throw_error(ec); Chris@16: } Chris@16: Chris@16: /// Use a private key from a file. Chris@16: /** Chris@16: * This function is used to load a private key into the context from a file. Chris@16: * Chris@16: * @param filename The name of the file containing the private key. Chris@16: * Chris@16: * @param format The file format (ASN.1 or PEM). Chris@16: * Chris@16: * @param ec Set to indicate what error occurred, if any. Chris@16: */ Chris@16: boost::system::error_code use_private_key_file(const std::string& filename, Chris@16: file_format format, boost::system::error_code& ec) Chris@16: { Chris@16: return service_.use_private_key_file(impl_, filename, format, ec); Chris@16: } Chris@16: Chris@16: /// Use an RSA private key from a file. Chris@16: /** Chris@16: * This function is used to load an RSA private key into the context from a Chris@16: * file. Chris@16: * Chris@16: * @param filename The name of the file containing the RSA private key. Chris@16: * Chris@16: * @param format The file format (ASN.1 or PEM). Chris@16: * Chris@16: * @throws boost::system::system_error Thrown on failure. Chris@16: */ Chris@16: void use_rsa_private_key_file(const std::string& filename, file_format format) Chris@16: { Chris@16: boost::system::error_code ec; Chris@16: service_.use_rsa_private_key_file(impl_, filename, format, ec); Chris@16: boost::asio::detail::throw_error(ec); Chris@16: } Chris@16: Chris@16: /// Use an RSA private key from a file. Chris@16: /** Chris@16: * This function is used to load an RSA private key into the context from a Chris@16: * file. Chris@16: * Chris@16: * @param filename The name of the file containing the RSA private key. Chris@16: * Chris@16: * @param format The file format (ASN.1 or PEM). Chris@16: * Chris@16: * @param ec Set to indicate what error occurred, if any. Chris@16: */ Chris@16: boost::system::error_code use_rsa_private_key_file( Chris@16: const std::string& filename, file_format format, Chris@16: boost::system::error_code& ec) Chris@16: { Chris@16: return service_.use_rsa_private_key_file(impl_, filename, format, ec); Chris@16: } Chris@16: Chris@16: /// Use the specified file to obtain the temporary Diffie-Hellman parameters. Chris@16: /** Chris@16: * This function is used to load Diffie-Hellman parameters into the context Chris@16: * from a file. Chris@16: * Chris@16: * @param filename The name of the file containing the Diffie-Hellman Chris@16: * parameters. The file must use the PEM format. Chris@16: * Chris@16: * @throws boost::system::system_error Thrown on failure. Chris@16: */ Chris@16: void use_tmp_dh_file(const std::string& filename) Chris@16: { Chris@16: boost::system::error_code ec; Chris@16: service_.use_tmp_dh_file(impl_, filename, ec); Chris@16: boost::asio::detail::throw_error(ec); Chris@16: } Chris@16: Chris@16: /// Use the specified file to obtain the temporary Diffie-Hellman parameters. Chris@16: /** Chris@16: * This function is used to load Diffie-Hellman parameters into the context Chris@16: * from a file. Chris@16: * Chris@16: * @param filename The name of the file containing the Diffie-Hellman Chris@16: * parameters. The file must use the PEM format. Chris@16: * Chris@16: * @param ec Set to indicate what error occurred, if any. Chris@16: */ Chris@16: boost::system::error_code use_tmp_dh_file(const std::string& filename, Chris@16: boost::system::error_code& ec) Chris@16: { Chris@16: return service_.use_tmp_dh_file(impl_, filename, ec); Chris@16: } Chris@16: Chris@16: /// Set the password callback. Chris@16: /** Chris@16: * This function is used to specify a callback function to obtain password Chris@16: * information about an encrypted key in PEM format. Chris@16: * Chris@16: * @param callback The function object to be used for obtaining the password. Chris@16: * The function signature of the handler must be: Chris@16: * @code std::string password_callback( Chris@16: * std::size_t max_length, // The maximum size for a password. Chris@16: * password_purpose purpose // Whether password is for reading or writing. Chris@16: * ); @endcode Chris@16: * The return value of the callback is a string containing the password. Chris@16: * Chris@16: * @throws boost::system::system_error Thrown on failure. Chris@16: */ Chris@16: template Chris@16: void set_password_callback(PasswordCallback callback) Chris@16: { Chris@16: boost::system::error_code ec; Chris@16: service_.set_password_callback(impl_, callback, ec); Chris@16: boost::asio::detail::throw_error(ec); Chris@16: } Chris@16: Chris@16: /// Set the password callback. Chris@16: /** Chris@16: * This function is used to specify a callback function to obtain password Chris@16: * information about an encrypted key in PEM format. Chris@16: * Chris@16: * @param callback The function object to be used for obtaining the password. Chris@16: * The function signature of the handler must be: Chris@16: * @code std::string password_callback( Chris@16: * std::size_t max_length, // The maximum size for a password. Chris@16: * password_purpose purpose // Whether password is for reading or writing. Chris@16: * ); @endcode Chris@16: * The return value of the callback is a string containing the password. Chris@16: * Chris@16: * @param ec Set to indicate what error occurred, if any. Chris@16: */ Chris@16: template Chris@16: boost::system::error_code set_password_callback(PasswordCallback callback, Chris@16: boost::system::error_code& ec) Chris@16: { Chris@16: return service_.set_password_callback(impl_, callback, ec); Chris@16: } Chris@16: Chris@16: private: Chris@16: /// The backend service implementation. Chris@16: service_type& service_; Chris@16: Chris@16: /// The underlying native implementation. Chris@16: impl_type impl_; Chris@16: }; Chris@16: Chris@16: } // namespace old Chris@16: } // namespace ssl Chris@16: } // namespace asio Chris@16: } // namespace boost Chris@16: Chris@16: #include Chris@16: Chris@16: #endif // BOOST_ASIO_SSL_OLD_BASIC_CONTEXT_HPP