5 // Copyright (c) 2005 Voipster / Indrek dot Juhani at voipster dot com
6 // Copyright (c) 2005-2008 Christopher M. Kohlhoff (chris at kohlhoff dot com)
8 // Distributed under the Boost Software License, Version 1.0. (See accompanying
9 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
12 #ifndef BOOST_ASIO_SSL_BASIC_CONTEXT_HPP
13 #define BOOST_ASIO_SSL_BASIC_CONTEXT_HPP
15 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
17 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
19 #include <boost/asio/detail/push_options.hpp>
21 #include <boost/asio/detail/push_options.hpp>
23 #include <boost/noncopyable.hpp>
24 #include <boost/asio/detail/pop_options.hpp>
26 #include <boost/asio/error.hpp>
27 #include <boost/asio/io_service.hpp>
28 #include <boost/asio/ssl/context_base.hpp>
29 #include <boost/asio/detail/throw_error.hpp>
36 template <typename Service
>
38 : public context_base
,
39 private boost::noncopyable
42 /// The type of the service that will be used to provide context operations.
43 typedef Service service_type
;
45 /// The native implementation type of the locking dispatcher.
46 typedef typename
service_type::impl_type impl_type
;
49 basic_context(boost::asio::io_service
& io_service
, method m
)
50 : service_(boost::asio::use_service
<Service
>(io_service
)),
51 impl_(service_
.null())
53 service_
.create(impl_
, m
);
59 service_
.destroy(impl_
);
62 /// Get the underlying implementation in the native type.
64 * This function may be used to obtain the underlying implementation of the
65 * context. This is intended to allow access to context functionality that is
66 * not otherwise provided.
73 /// Set options on the context.
75 * This function may be used to configure the SSL options used by the context.
77 * @param o A bitmask of options. The available option values are defined in
78 * the context_base class. The options are bitwise-ored with any existing
79 * value for the options.
81 * @throws boost::system::system_error Thrown on failure.
83 void set_options(options o
)
85 boost::system::error_code ec
;
86 service_
.set_options(impl_
, o
, ec
);
87 boost::asio::detail::throw_error(ec
);
90 /// Set options on the context.
92 * This function may be used to configure the SSL options used by the context.
94 * @param o A bitmask of options. The available option values are defined in
95 * the context_base class. The options are bitwise-ored with any existing
96 * value for the options.
98 * @param ec Set to indicate what error occurred, if any.
100 boost::system::error_code
set_options(options o
,
101 boost::system::error_code
& ec
)
103 return service_
.set_options(impl_
, o
, ec
);
106 /// Set the peer verification mode.
108 * This function may be used to configure the peer verification mode used by
111 * @param v A bitmask of peer verification modes. The available verify_mode
112 * values are defined in the context_base class.
114 * @throws boost::system::system_error Thrown on failure.
116 void set_verify_mode(verify_mode v
)
118 boost::system::error_code ec
;
119 service_
.set_verify_mode(impl_
, v
, ec
);
120 boost::asio::detail::throw_error(ec
);
123 /// Set the peer verification mode.
125 * This function may be used to configure the peer verification mode used by
128 * @param v A bitmask of peer verification modes. The available verify_mode
129 * values are defined in the context_base class.
131 * @param ec Set to indicate what error occurred, if any.
133 boost::system::error_code
set_verify_mode(verify_mode v
,
134 boost::system::error_code
& ec
)
136 return service_
.set_verify_mode(impl_
, v
, ec
);
139 /// Load a certification authority file for performing verification.
141 * This function is used to load one or more trusted certification authorities
144 * @param filename The name of a file containing certification authority
145 * certificates in PEM format.
147 * @throws boost::system::system_error Thrown on failure.
149 void load_verify_file(const std::string
& filename
)
151 boost::system::error_code ec
;
152 service_
.load_verify_file(impl_
, filename
, ec
);
153 boost::asio::detail::throw_error(ec
);
156 /// Load a certification authority file for performing verification.
158 * This function is used to load the certificates for one or more trusted
159 * certification authorities from a file.
161 * @param filename The name of a file containing certification authority
162 * certificates in PEM format.
164 * @param ec Set to indicate what error occurred, if any.
166 boost::system::error_code
load_verify_file(const std::string
& filename
,
167 boost::system::error_code
& ec
)
169 return service_
.load_verify_file(impl_
, filename
, ec
);
172 /// Add a directory containing certificate authority files to be used for
173 /// performing verification.
175 * This function is used to specify the name of a directory containing
176 * certification authority certificates. Each file in the directory must
177 * contain a single certificate. The files must be named using the subject
178 * name's hash and an extension of ".0".
180 * @param path The name of a directory containing the certificates.
182 * @throws boost::system::system_error Thrown on failure.
184 void add_verify_path(const std::string
& path
)
186 boost::system::error_code ec
;
187 service_
.add_verify_path(impl_
, path
, ec
);
188 boost::asio::detail::throw_error(ec
);
191 /// Add a directory containing certificate authority files to be used for
192 /// performing verification.
194 * This function is used to specify the name of a directory containing
195 * certification authority certificates. Each file in the directory must
196 * contain a single certificate. The files must be named using the subject
197 * name's hash and an extension of ".0".
199 * @param path The name of a directory containing the certificates.
201 * @param ec Set to indicate what error occurred, if any.
203 boost::system::error_code
add_verify_path(const std::string
& path
,
204 boost::system::error_code
& ec
)
206 return service_
.add_verify_path(impl_
, path
, ec
);
209 /// Use a certificate from a file.
211 * This function is used to load a certificate into the context from a file.
213 * @param filename The name of the file containing the certificate.
215 * @param format The file format (ASN.1 or PEM).
217 * @throws boost::system::system_error Thrown on failure.
219 void use_certificate_file(const std::string
& filename
, file_format format
)
221 boost::system::error_code ec
;
222 service_
.use_certificate_file(impl_
, filename
, format
, ec
);
223 boost::asio::detail::throw_error(ec
);
226 /// Use a certificate from a file.
228 * This function is used to load a certificate into the context from a file.
230 * @param filename The name of the file containing the certificate.
232 * @param format The file format (ASN.1 or PEM).
234 * @param ec Set to indicate what error occurred, if any.
236 boost::system::error_code
use_certificate_file(const std::string
& filename
,
237 file_format format
, boost::system::error_code
& ec
)
239 return service_
.use_certificate_file(impl_
, filename
, format
, ec
);
242 /// Use a certificate chain from a file.
244 * This function is used to load a certificate chain into the context from a
247 * @param filename The name of the file containing the certificate. The file
248 * must use the PEM format.
250 * @throws boost::system::system_error Thrown on failure.
252 void use_certificate_chain_file(const std::string
& filename
)
254 boost::system::error_code ec
;
255 service_
.use_certificate_chain_file(impl_
, filename
, ec
);
256 boost::asio::detail::throw_error(ec
);
259 /// Use a certificate chain from a file.
261 * This function is used to load a certificate chain into the context from a
264 * @param filename The name of the file containing the certificate. The file
265 * must use the PEM format.
267 * @param ec Set to indicate what error occurred, if any.
269 boost::system::error_code
use_certificate_chain_file(
270 const std::string
& filename
, boost::system::error_code
& ec
)
272 return service_
.use_certificate_chain_file(impl_
, filename
, ec
);
275 /// Use a private key from a file.
277 * This function is used to load a private key into the context from a file.
279 * @param filename The name of the file containing the private key.
281 * @param format The file format (ASN.1 or PEM).
283 * @throws boost::system::system_error Thrown on failure.
285 void use_private_key_file(const std::string
& filename
, file_format format
)
287 boost::system::error_code ec
;
288 service_
.use_private_key_file(impl_
, filename
, format
, ec
);
289 boost::asio::detail::throw_error(ec
);
292 /// Use a private key from a file.
294 * This function is used to load a private key into the context from a file.
296 * @param filename The name of the file containing the private key.
298 * @param format The file format (ASN.1 or PEM).
300 * @param ec Set to indicate what error occurred, if any.
302 boost::system::error_code
use_private_key_file(const std::string
& filename
,
303 file_format format
, boost::system::error_code
& ec
)
305 return service_
.use_private_key_file(impl_
, filename
, format
, ec
);
308 /// Use an RSA private key from a file.
310 * This function is used to load an RSA private key into the context from a
313 * @param filename The name of the file containing the RSA private key.
315 * @param format The file format (ASN.1 or PEM).
317 * @throws boost::system::system_error Thrown on failure.
319 void use_rsa_private_key_file(const std::string
& filename
, file_format format
)
321 boost::system::error_code ec
;
322 service_
.use_rsa_private_key_file(impl_
, filename
, format
, ec
);
323 boost::asio::detail::throw_error(ec
);
326 /// Use an RSA private key from a file.
328 * This function is used to load an RSA private key into the context from a
331 * @param filename The name of the file containing the RSA private key.
333 * @param format The file format (ASN.1 or PEM).
335 * @param ec Set to indicate what error occurred, if any.
337 boost::system::error_code
use_rsa_private_key_file(
338 const std::string
& filename
, file_format format
,
339 boost::system::error_code
& ec
)
341 return service_
.use_rsa_private_key_file(impl_
, filename
, format
, ec
);
344 /// Use the specified file to obtain the temporary Diffie-Hellman parameters.
346 * This function is used to load Diffie-Hellman parameters into the context
349 * @param filename The name of the file containing the Diffie-Hellman
350 * parameters. The file must use the PEM format.
352 * @throws boost::system::system_error Thrown on failure.
354 void use_tmp_dh_file(const std::string
& filename
)
356 boost::system::error_code ec
;
357 service_
.use_tmp_dh_file(impl_
, filename
, ec
);
358 boost::asio::detail::throw_error(ec
);
361 /// Use the specified file to obtain the temporary Diffie-Hellman parameters.
363 * This function is used to load Diffie-Hellman parameters into the context
366 * @param filename The name of the file containing the Diffie-Hellman
367 * parameters. The file must use the PEM format.
369 * @param ec Set to indicate what error occurred, if any.
371 boost::system::error_code
use_tmp_dh_file(const std::string
& filename
,
372 boost::system::error_code
& ec
)
374 return service_
.use_tmp_dh_file(impl_
, filename
, ec
);
377 /// Set the password callback.
379 * This function is used to specify a callback function to obtain password
380 * information about an encrypted key in PEM format.
382 * @param callback The function object to be used for obtaining the password.
383 * The function signature of the handler must be:
384 * @code std::string password_callback(
385 * std::size_t max_length, // The maximum size for a password.
386 * password_purpose purpose // Whether password is for reading or writing.
388 * The return value of the callback is a string containing the password.
390 * @throws boost::system::system_error Thrown on failure.
392 template <typename PasswordCallback
>
393 void set_password_callback(PasswordCallback callback
)
395 boost::system::error_code ec
;
396 service_
.set_password_callback(impl_
, callback
, ec
);
397 boost::asio::detail::throw_error(ec
);
400 /// Set the password callback.
402 * This function is used to specify a callback function to obtain password
403 * information about an encrypted key in PEM format.
405 * @param callback The function object to be used for obtaining the password.
406 * The function signature of the handler must be:
407 * @code std::string password_callback(
408 * std::size_t max_length, // The maximum size for a password.
409 * password_purpose purpose // Whether password is for reading or writing.
411 * The return value of the callback is a string containing the password.
413 * @param ec Set to indicate what error occurred, if any.
415 template <typename PasswordCallback
>
416 boost::system::error_code
set_password_callback(PasswordCallback callback
,
417 boost::system::error_code
& ec
)
419 return service_
.set_password_callback(impl_
, callback
, ec
);
423 /// The backend service implementation.
424 service_type
& service_
;
426 /// The underlying native implementation.
434 #include <boost/asio/detail/pop_options.hpp>
436 #endif // BOOST_ASIO_SSL_BASIC_CONTEXT_HPP