5
// Copyright (c) 2005 Voipster / Indrek dot Juhani at voipster dot com
6
// Copyright (c) 2005 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 ASIO_SSL_BASIC_CONTEXT_HPP
13
#define ASIO_SSL_BASIC_CONTEXT_HPP
15
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
17
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
19
#include "asio/detail/push_options.hpp"
21
#include "asio/detail/push_options.hpp"
23
#include <boost/noncopyable.hpp>
24
#include "asio/detail/pop_options.hpp"
26
#include "asio/error.hpp"
27
#include "asio/io_service.hpp"
28
#include "asio/ssl/context_base.hpp"
29
#include "asio/detail/throw_error.hpp"
35
template <typename Service>
37
: public context_base,
38
private boost::noncopyable
41
/// The type of the service that will be used to provide context operations.
42
typedef Service service_type;
44
/// The native implementation type of the locking dispatcher.
45
typedef typename service_type::impl_type impl_type;
48
basic_context(asio::io_service& io_service, method m)
49
: service_(asio::use_service<Service>(io_service)),
50
impl_(service_.null())
52
service_.create(impl_, m);
58
service_.destroy(impl_);
61
/// Get the underlying implementation in the native type.
63
* This function may be used to obtain the underlying implementation of the
64
* context. This is intended to allow access to context functionality that is
65
* not otherwise provided.
72
/// Set options on the context.
74
* This function may be used to configure the SSL options used by the context.
76
* @param o A bitmask of options. The available option values are defined in
77
* the context_base class. The options are bitwise-ored with any existing
78
* value for the options.
80
* @throws asio::system_error Thrown on failure.
82
void set_options(options o)
85
service_.set_options(impl_, o, ec);
86
asio::detail::throw_error(ec);
89
/// Set options on the context.
91
* This function may be used to configure the SSL options used by the context.
93
* @param o A bitmask of options. The available option values are defined in
94
* the context_base class. The options are bitwise-ored with any existing
95
* value for the options.
97
* @param ec Set to indicate what error occurred, if any.
99
asio::error_code set_options(options o,
100
asio::error_code& ec)
102
return service_.set_options(impl_, o, ec);
105
/// Set the peer verification mode.
107
* This function may be used to configure the peer verification mode used by
110
* @param v A bitmask of peer verification modes. The available verify_mode
111
* values are defined in the context_base class.
113
* @throws asio::system_error Thrown on failure.
115
void set_verify_mode(verify_mode v)
118
service_.set_verify_mode(impl_, v, ec);
119
asio::detail::throw_error(ec);
122
/// Set the peer verification mode.
124
* This function may be used to configure the peer verification mode used by
127
* @param v A bitmask of peer verification modes. The available verify_mode
128
* values are defined in the context_base class.
130
* @param ec Set to indicate what error occurred, if any.
132
asio::error_code set_verify_mode(verify_mode v,
133
asio::error_code& ec)
135
return service_.set_verify_mode(impl_, v, ec);
138
/// Load a certification authority file for performing verification.
140
* This function is used to load one or more trusted certification authorities
143
* @param filename The name of a file containing certification authority
144
* certificates in PEM format.
146
* @throws asio::system_error Thrown on failure.
148
void load_verify_file(const std::string& filename)
151
service_.load_verify_file(impl_, filename, ec);
152
asio::detail::throw_error(ec);
155
/// Load a certification authority file for performing verification.
157
* This function is used to load the certificates for one or more trusted
158
* certification authorities from a file.
160
* @param filename The name of a file containing certification authority
161
* certificates in PEM format.
163
* @param ec Set to indicate what error occurred, if any.
165
asio::error_code load_verify_file(const std::string& filename,
166
asio::error_code& ec)
168
return service_.load_verify_file(impl_, filename, ec);
171
/// Add a directory containing certificate authority files to be used for
172
/// performing verification.
174
* This function is used to specify the name of a directory containing
175
* certification authority certificates. Each file in the directory must
176
* contain a single certificate. The files must be named using the subject
177
* name's hash and an extension of ".0".
179
* @param path The name of a directory containing the certificates.
181
* @throws asio::system_error Thrown on failure.
183
void add_verify_path(const std::string& path)
186
service_.add_verify_path(impl_, path, ec);
187
asio::detail::throw_error(ec);
190
/// Add a directory containing certificate authority files to be used for
191
/// performing verification.
193
* This function is used to specify the name of a directory containing
194
* certification authority certificates. Each file in the directory must
195
* contain a single certificate. The files must be named using the subject
196
* name's hash and an extension of ".0".
198
* @param path The name of a directory containing the certificates.
200
* @param ec Set to indicate what error occurred, if any.
202
asio::error_code add_verify_path(const std::string& path,
203
asio::error_code& ec)
205
return service_.add_verify_path(impl_, path, ec);
208
/// Use a certificate from a file.
210
* This function is used to load a certificate into the context from a file.
212
* @param filename The name of the file containing the certificate.
214
* @param format The file format (ASN.1 or PEM).
216
* @throws asio::system_error Thrown on failure.
218
void use_certificate_file(const std::string& filename, file_format format)
221
service_.use_certificate_file(impl_, filename, format, ec);
222
asio::detail::throw_error(ec);
225
/// Use a certificate from a file.
227
* This function is used to load a certificate into the context from a file.
229
* @param filename The name of the file containing the certificate.
231
* @param format The file format (ASN.1 or PEM).
233
* @param ec Set to indicate what error occurred, if any.
235
asio::error_code use_certificate_file(const std::string& filename,
236
file_format format, asio::error_code& ec)
238
return service_.use_certificate_file(impl_, filename, format, ec);
241
/// Use a certificate chain from a file.
243
* This function is used to load a certificate chain into the context from a
246
* @param filename The name of the file containing the certificate. The file
247
* must use the PEM format.
249
* @throws asio::system_error Thrown on failure.
251
void use_certificate_chain_file(const std::string& filename)
254
service_.use_certificate_chain_file(impl_, filename, ec);
255
asio::detail::throw_error(ec);
258
/// Use a certificate chain from a file.
260
* This function is used to load a certificate chain into the context from a
263
* @param filename The name of the file containing the certificate. The file
264
* must use the PEM format.
266
* @param ec Set to indicate what error occurred, if any.
268
asio::error_code use_certificate_chain_file(
269
const std::string& filename, asio::error_code& ec)
271
return service_.use_certificate_chain_file(impl_, filename, ec);
274
/// Use a private key from a file.
276
* This function is used to load a private key into the context from a file.
278
* @param filename The name of the file containing the private key.
280
* @param format The file format (ASN.1 or PEM).
282
* @throws asio::system_error Thrown on failure.
284
void use_private_key_file(const std::string& filename, file_format format)
287
service_.use_private_key_file(impl_, filename, format, ec);
288
asio::detail::throw_error(ec);
291
/// Use a private key from a file.
293
* This function is used to load a private key into the context from a file.
295
* @param filename The name of the file containing the private key.
297
* @param format The file format (ASN.1 or PEM).
299
* @param ec Set to indicate what error occurred, if any.
301
asio::error_code use_private_key_file(const std::string& filename,
302
file_format format, asio::error_code& ec)
304
return service_.use_private_key_file(impl_, filename, format, ec);
307
/// Use an RSA private key from a file.
309
* This function is used to load an RSA private key into the context from a
312
* @param filename The name of the file containing the RSA private key.
314
* @param format The file format (ASN.1 or PEM).
316
* @throws asio::system_error Thrown on failure.
318
void use_rsa_private_key_file(const std::string& filename, file_format format)
321
service_.use_rsa_private_key_file(impl_, filename, format, ec);
322
asio::detail::throw_error(ec);
325
/// Use an RSA private key from a file.
327
* This function is used to load an RSA private key into the context from a
330
* @param filename The name of the file containing the RSA private key.
332
* @param format The file format (ASN.1 or PEM).
334
* @param ec Set to indicate what error occurred, if any.
336
asio::error_code use_rsa_private_key_file(
337
const std::string& filename, file_format format,
338
asio::error_code& ec)
340
return service_.use_rsa_private_key_file(impl_, filename, format, ec);
343
/// Use the specified file to obtain the temporary Diffie-Hellman parameters.
345
* This function is used to load Diffie-Hellman parameters into the context
348
* @param filename The name of the file containing the Diffie-Hellman
349
* parameters. The file must use the PEM format.
351
* @throws asio::system_error Thrown on failure.
353
void use_tmp_dh_file(const std::string& filename)
356
service_.use_tmp_dh_file(impl_, filename, ec);
357
asio::detail::throw_error(ec);
360
/// Use the specified file to obtain the temporary Diffie-Hellman parameters.
362
* This function is used to load Diffie-Hellman parameters into the context
365
* @param filename The name of the file containing the Diffie-Hellman
366
* parameters. The file must use the PEM format.
368
* @param ec Set to indicate what error occurred, if any.
370
asio::error_code use_tmp_dh_file(const std::string& filename,
371
asio::error_code& ec)
373
return service_.use_tmp_dh_file(impl_, filename, ec);
376
/// Set the password callback.
378
* This function is used to specify a callback function to obtain password
379
* information about an encrypted key in PEM format.
381
* @param callback The function object to be used for obtaining the password.
382
* The function signature of the handler must be:
383
* @code std::string password_callback(
384
* std::size_t max_length, // The maximum size for a password.
385
* password_purpose purpose // Whether password is for reading or writing.
387
* The return value of the callback is a string containing the password.
389
* @throws asio::system_error Thrown on failure.
391
template <typename PasswordCallback>
392
void set_password_callback(PasswordCallback callback)
395
service_.set_password_callback(impl_, callback, ec);
396
asio::detail::throw_error(ec);
399
/// Set the password callback.
401
* This function is used to specify a callback function to obtain password
402
* information about an encrypted key in PEM format.
404
* @param callback The function object to be used for obtaining the password.
405
* The function signature of the handler must be:
406
* @code std::string password_callback(
407
* std::size_t max_length, // The maximum size for a password.
408
* password_purpose purpose // Whether password is for reading or writing.
410
* The return value of the callback is a string containing the password.
412
* @param ec Set to indicate what error occurred, if any.
414
template <typename PasswordCallback>
415
asio::error_code set_password_callback(PasswordCallback callback,
416
asio::error_code& ec)
418
return service_.set_password_callback(impl_, callback, ec);
422
/// The backend service implementation.
423
service_type& service_;
425
/// The underlying native implementation.
432
#include "asio/detail/pop_options.hpp"
434
#endif // ASIO_SSL_BASIC_CONTEXT_HPP