123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436 |
- //
- // basic_context.hpp
- // ~~~~~~~~~~~~~~~~~
- //
- // Copyright (c) 2005 Voipster / Indrek dot Juhani at voipster dot com
- // Copyright (c) 2005-2008 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_BASIC_CONTEXT_HPP
- #define BOOST_ASIO_SSL_BASIC_CONTEXT_HPP
- #if defined(_MSC_VER) && (_MSC_VER >= 1200)
- # pragma once
- #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
- #include <boost/asio/detail/push_options.hpp>
- #include <boost/asio/detail/push_options.hpp>
- #include <string>
- #include <boost/noncopyable.hpp>
- #include <boost/asio/detail/pop_options.hpp>
- #include <boost/asio/error.hpp>
- #include <boost/asio/io_service.hpp>
- #include <boost/asio/ssl/context_base.hpp>
- #include <boost/asio/detail/throw_error.hpp>
- namespace boost {
- namespace asio {
- namespace ssl {
- /// SSL context.
- template <typename Service>
- class basic_context
- : public context_base,
- private boost::noncopyable
- {
- public:
- /// The type of the service that will be used to provide context operations.
- typedef Service service_type;
- /// The native implementation type of the locking dispatcher.
- typedef typename service_type::impl_type impl_type;
- /// Constructor.
- basic_context(boost::asio::io_service& io_service, method m)
- : service_(boost::asio::use_service<Service>(io_service)),
- impl_(service_.null())
- {
- service_.create(impl_, m);
- }
- /// Destructor.
- ~basic_context()
- {
- service_.destroy(impl_);
- }
- /// 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.
- */
- impl_type impl()
- {
- return impl_;
- }
- /// 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.
- */
- void set_options(options o)
- {
- boost::system::error_code ec;
- service_.set_options(impl_, o, ec);
- boost::asio::detail::throw_error(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.
- *
- * @param ec Set to indicate what error occurred, if any.
- */
- boost::system::error_code set_options(options o,
- boost::system::error_code& ec)
- {
- return service_.set_options(impl_, o, 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. The available verify_mode
- * values are defined in the context_base class.
- *
- * @throws boost::system::system_error Thrown on failure.
- */
- void set_verify_mode(verify_mode v)
- {
- boost::system::error_code ec;
- service_.set_verify_mode(impl_, v, ec);
- boost::asio::detail::throw_error(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. The available verify_mode
- * values are defined in the context_base class.
- *
- * @param ec Set to indicate what error occurred, if any.
- */
- boost::system::error_code set_verify_mode(verify_mode v,
- boost::system::error_code& ec)
- {
- return service_.set_verify_mode(impl_, v, 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.
- */
- void load_verify_file(const std::string& filename)
- {
- boost::system::error_code ec;
- service_.load_verify_file(impl_, filename, ec);
- boost::asio::detail::throw_error(ec);
- }
- /// 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.
- */
- boost::system::error_code load_verify_file(const std::string& filename,
- boost::system::error_code& ec)
- {
- return service_.load_verify_file(impl_, filename, 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.
- */
- void add_verify_path(const std::string& path)
- {
- boost::system::error_code ec;
- service_.add_verify_path(impl_, path, ec);
- boost::asio::detail::throw_error(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.
- *
- * @param ec Set to indicate what error occurred, if any.
- */
- boost::system::error_code add_verify_path(const std::string& path,
- boost::system::error_code& ec)
- {
- return service_.add_verify_path(impl_, path, 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.
- */
- void use_certificate_file(const std::string& filename, file_format format)
- {
- boost::system::error_code ec;
- service_.use_certificate_file(impl_, filename, format, ec);
- boost::asio::detail::throw_error(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).
- *
- * @param ec Set to indicate what error occurred, if any.
- */
- boost::system::error_code use_certificate_file(const std::string& filename,
- file_format format, boost::system::error_code& ec)
- {
- return service_.use_certificate_file(impl_, filename, format, 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.
- */
- void use_certificate_chain_file(const std::string& filename)
- {
- boost::system::error_code ec;
- service_.use_certificate_chain_file(impl_, filename, ec);
- boost::asio::detail::throw_error(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.
- *
- * @param ec Set to indicate what error occurred, if any.
- */
- boost::system::error_code use_certificate_chain_file(
- const std::string& filename, boost::system::error_code& ec)
- {
- return service_.use_certificate_chain_file(impl_, filename, 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.
- */
- void use_private_key_file(const std::string& filename, file_format format)
- {
- boost::system::error_code ec;
- service_.use_private_key_file(impl_, filename, format, ec);
- boost::asio::detail::throw_error(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).
- *
- * @param ec Set to indicate what error occurred, if any.
- */
- boost::system::error_code use_private_key_file(const std::string& filename,
- file_format format, boost::system::error_code& ec)
- {
- return service_.use_private_key_file(impl_, filename, format, 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.
- */
- void use_rsa_private_key_file(const std::string& filename, file_format format)
- {
- boost::system::error_code ec;
- service_.use_rsa_private_key_file(impl_, filename, format, ec);
- boost::asio::detail::throw_error(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).
- *
- * @param ec Set to indicate what error occurred, if any.
- */
- boost::system::error_code use_rsa_private_key_file(
- const std::string& filename, file_format format,
- boost::system::error_code& ec)
- {
- return service_.use_rsa_private_key_file(impl_, filename, format, 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.
- */
- void use_tmp_dh_file(const std::string& filename)
- {
- boost::system::error_code ec;
- service_.use_tmp_dh_file(impl_, filename, ec);
- boost::asio::detail::throw_error(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.
- *
- * @param ec Set to indicate what error occurred, if any.
- */
- boost::system::error_code use_tmp_dh_file(const std::string& filename,
- boost::system::error_code& ec)
- {
- return service_.use_tmp_dh_file(impl_, filename, 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.
- */
- template <typename PasswordCallback>
- void set_password_callback(PasswordCallback callback)
- {
- boost::system::error_code ec;
- service_.set_password_callback(impl_, callback, ec);
- boost::asio::detail::throw_error(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.
- *
- * @param ec Set to indicate what error occurred, if any.
- */
- template <typename PasswordCallback>
- boost::system::error_code set_password_callback(PasswordCallback callback,
- boost::system::error_code& ec)
- {
- return service_.set_password_callback(impl_, callback, ec);
- }
- private:
- /// The backend service implementation.
- service_type& service_;
- /// The underlying native implementation.
- impl_type impl_;
- };
- } // namespace ssl
- } // namespace asio
- } // namespace boost
- #include <boost/asio/detail/pop_options.hpp>
- #endif // BOOST_ASIO_SSL_BASIC_CONTEXT_HPP
|