~ubuntu-branches/ubuntu/maverick/libtorrent-rasterbar/maverick

« back to all changes in this revision

Viewing changes to include/libtorrent/asio/ssl/basic_context.hpp

  • Committer: Bazaar Package Importer
  • Author(s): Cristian Greco
  • Date: 2008-07-02 10:46:21 UTC
  • Revision ID: james.westby@ubuntu.com-20080702104621-jzx3pfke9lkcxfxn
Tags: upstream-0.13.1
ImportĀ upstreamĀ versionĀ 0.13.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// basic_context.hpp
 
3
// ~~~~~~~~~~~~~~~~~
 
4
//
 
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)
 
7
//
 
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)
 
10
//
 
11
 
 
12
#ifndef ASIO_SSL_BASIC_CONTEXT_HPP
 
13
#define ASIO_SSL_BASIC_CONTEXT_HPP
 
14
 
 
15
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
 
16
# pragma once
 
17
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
 
18
 
 
19
#include "asio/detail/push_options.hpp"
 
20
 
 
21
#include "asio/detail/push_options.hpp"
 
22
#include <string>
 
23
#include <boost/noncopyable.hpp>
 
24
#include "asio/detail/pop_options.hpp"
 
25
 
 
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"
 
30
 
 
31
namespace asio {
 
32
namespace ssl {
 
33
 
 
34
/// SSL context.
 
35
template <typename Service>
 
36
class basic_context
 
37
  : public context_base,
 
38
    private boost::noncopyable
 
39
{
 
40
public:
 
41
  /// The type of the service that will be used to provide context operations.
 
42
  typedef Service service_type;
 
43
 
 
44
  /// The native implementation type of the locking dispatcher.
 
45
  typedef typename service_type::impl_type impl_type;
 
46
 
 
47
  /// Constructor.
 
48
  basic_context(asio::io_service& io_service, method m)
 
49
    : service_(asio::use_service<Service>(io_service)),
 
50
      impl_(service_.null())
 
51
  {
 
52
    service_.create(impl_, m);
 
53
  }
 
54
 
 
55
  /// Destructor.
 
56
  ~basic_context()
 
57
  {
 
58
    service_.destroy(impl_);
 
59
  }
 
60
 
 
61
  /// Get the underlying implementation in the native type.
 
62
  /**
 
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.
 
66
   */
 
67
  impl_type impl()
 
68
  {
 
69
    return impl_;
 
70
  }
 
71
 
 
72
  /// Set options on the context.
 
73
  /**
 
74
   * This function may be used to configure the SSL options used by the context.
 
75
   *
 
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.
 
79
   *
 
80
   * @throws asio::system_error Thrown on failure.
 
81
   */
 
82
  void set_options(options o)
 
83
  {
 
84
    asio::error_code ec;
 
85
    service_.set_options(impl_, o, ec);
 
86
    asio::detail::throw_error(ec);
 
87
  }
 
88
 
 
89
  /// Set options on the context.
 
90
  /**
 
91
   * This function may be used to configure the SSL options used by the context.
 
92
   *
 
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.
 
96
   *
 
97
   * @param ec Set to indicate what error occurred, if any.
 
98
   */
 
99
  asio::error_code set_options(options o,
 
100
      asio::error_code& ec)
 
101
  {
 
102
    return service_.set_options(impl_, o, ec);
 
103
  }
 
104
 
 
105
  /// Set the peer verification mode.
 
106
  /**
 
107
   * This function may be used to configure the peer verification mode used by
 
108
   * the context.
 
109
   *
 
110
   * @param v A bitmask of peer verification modes. The available verify_mode
 
111
   * values are defined in the context_base class.
 
112
   *
 
113
   * @throws asio::system_error Thrown on failure.
 
114
   */
 
115
  void set_verify_mode(verify_mode v)
 
116
  {
 
117
    asio::error_code ec;
 
118
    service_.set_verify_mode(impl_, v, ec);
 
119
    asio::detail::throw_error(ec);
 
120
  }
 
121
 
 
122
  /// Set the peer verification mode.
 
123
  /**
 
124
   * This function may be used to configure the peer verification mode used by
 
125
   * the context.
 
126
   *
 
127
   * @param v A bitmask of peer verification modes. The available verify_mode
 
128
   * values are defined in the context_base class.
 
129
   *
 
130
   * @param ec Set to indicate what error occurred, if any.
 
131
   */
 
132
  asio::error_code set_verify_mode(verify_mode v,
 
133
      asio::error_code& ec)
 
134
  {
 
135
    return service_.set_verify_mode(impl_, v, ec);
 
136
  }
 
137
 
 
138
  /// Load a certification authority file for performing verification.
 
139
  /**
 
140
   * This function is used to load one or more trusted certification authorities
 
141
   * from a file.
 
142
   *
 
143
   * @param filename The name of a file containing certification authority
 
144
   * certificates in PEM format.
 
145
   *
 
146
   * @throws asio::system_error Thrown on failure.
 
147
   */
 
148
  void load_verify_file(const std::string& filename)
 
149
  {
 
150
    asio::error_code ec;
 
151
    service_.load_verify_file(impl_, filename, ec);
 
152
    asio::detail::throw_error(ec);
 
153
  }
 
154
 
 
155
  /// Load a certification authority file for performing verification.
 
156
  /**
 
157
   * This function is used to load the certificates for one or more trusted
 
158
   * certification authorities from a file.
 
159
   *
 
160
   * @param filename The name of a file containing certification authority
 
161
   * certificates in PEM format.
 
162
   *
 
163
   * @param ec Set to indicate what error occurred, if any.
 
164
   */
 
165
  asio::error_code load_verify_file(const std::string& filename,
 
166
      asio::error_code& ec)
 
167
  {
 
168
    return service_.load_verify_file(impl_, filename, ec);
 
169
  }
 
170
 
 
171
  /// Add a directory containing certificate authority files to be used for
 
172
  /// performing verification.
 
173
  /**
 
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".
 
178
   *
 
179
   * @param path The name of a directory containing the certificates.
 
180
   *
 
181
   * @throws asio::system_error Thrown on failure.
 
182
   */
 
183
  void add_verify_path(const std::string& path)
 
184
  {
 
185
    asio::error_code ec;
 
186
    service_.add_verify_path(impl_, path, ec);
 
187
    asio::detail::throw_error(ec);
 
188
  }
 
189
 
 
190
  /// Add a directory containing certificate authority files to be used for
 
191
  /// performing verification.
 
192
  /**
 
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".
 
197
   *
 
198
   * @param path The name of a directory containing the certificates.
 
199
   *
 
200
   * @param ec Set to indicate what error occurred, if any.
 
201
   */
 
202
  asio::error_code add_verify_path(const std::string& path,
 
203
      asio::error_code& ec)
 
204
  {
 
205
    return service_.add_verify_path(impl_, path, ec);
 
206
  }
 
207
 
 
208
  /// Use a certificate from a file.
 
209
  /**
 
210
   * This function is used to load a certificate into the context from a file.
 
211
   *
 
212
   * @param filename The name of the file containing the certificate.
 
213
   *
 
214
   * @param format The file format (ASN.1 or PEM).
 
215
   *
 
216
   * @throws asio::system_error Thrown on failure.
 
217
   */
 
218
  void use_certificate_file(const std::string& filename, file_format format)
 
219
  {
 
220
    asio::error_code ec;
 
221
    service_.use_certificate_file(impl_, filename, format, ec);
 
222
    asio::detail::throw_error(ec);
 
223
  }
 
224
 
 
225
  /// Use a certificate from a file.
 
226
  /**
 
227
   * This function is used to load a certificate into the context from a file.
 
228
   *
 
229
   * @param filename The name of the file containing the certificate.
 
230
   *
 
231
   * @param format The file format (ASN.1 or PEM).
 
232
   *
 
233
   * @param ec Set to indicate what error occurred, if any.
 
234
   */
 
235
  asio::error_code use_certificate_file(const std::string& filename,
 
236
      file_format format, asio::error_code& ec)
 
237
  {
 
238
    return service_.use_certificate_file(impl_, filename, format, ec);
 
239
  }
 
240
 
 
241
  /// Use a certificate chain from a file.
 
242
  /**
 
243
   * This function is used to load a certificate chain into the context from a
 
244
   * file.
 
245
   *
 
246
   * @param filename The name of the file containing the certificate. The file
 
247
   * must use the PEM format.
 
248
   *
 
249
   * @throws asio::system_error Thrown on failure.
 
250
   */
 
251
  void use_certificate_chain_file(const std::string& filename)
 
252
  {
 
253
    asio::error_code ec;
 
254
    service_.use_certificate_chain_file(impl_, filename, ec);
 
255
    asio::detail::throw_error(ec);
 
256
  }
 
257
 
 
258
  /// Use a certificate chain from a file.
 
259
  /**
 
260
   * This function is used to load a certificate chain into the context from a
 
261
   * file.
 
262
   *
 
263
   * @param filename The name of the file containing the certificate. The file
 
264
   * must use the PEM format.
 
265
   *
 
266
   * @param ec Set to indicate what error occurred, if any.
 
267
   */
 
268
  asio::error_code use_certificate_chain_file(
 
269
      const std::string& filename, asio::error_code& ec)
 
270
  {
 
271
    return service_.use_certificate_chain_file(impl_, filename, ec);
 
272
  }
 
273
 
 
274
  /// Use a private key from a file.
 
275
  /**
 
276
   * This function is used to load a private key into the context from a file.
 
277
   *
 
278
   * @param filename The name of the file containing the private key.
 
279
   *
 
280
   * @param format The file format (ASN.1 or PEM).
 
281
   *
 
282
   * @throws asio::system_error Thrown on failure.
 
283
   */
 
284
  void use_private_key_file(const std::string& filename, file_format format)
 
285
  {
 
286
    asio::error_code ec;
 
287
    service_.use_private_key_file(impl_, filename, format, ec);
 
288
    asio::detail::throw_error(ec);
 
289
  }
 
290
 
 
291
  /// Use a private key from a file.
 
292
  /**
 
293
   * This function is used to load a private key into the context from a file.
 
294
   *
 
295
   * @param filename The name of the file containing the private key.
 
296
   *
 
297
   * @param format The file format (ASN.1 or PEM).
 
298
   *
 
299
   * @param ec Set to indicate what error occurred, if any.
 
300
   */
 
301
  asio::error_code use_private_key_file(const std::string& filename,
 
302
      file_format format, asio::error_code& ec)
 
303
  {
 
304
    return service_.use_private_key_file(impl_, filename, format, ec);
 
305
  }
 
306
 
 
307
  /// Use an RSA private key from a file.
 
308
  /**
 
309
   * This function is used to load an RSA private key into the context from a
 
310
   * file.
 
311
   *
 
312
   * @param filename The name of the file containing the RSA private key.
 
313
   *
 
314
   * @param format The file format (ASN.1 or PEM).
 
315
   *
 
316
   * @throws asio::system_error Thrown on failure.
 
317
   */
 
318
  void use_rsa_private_key_file(const std::string& filename, file_format format)
 
319
  {
 
320
    asio::error_code ec;
 
321
    service_.use_rsa_private_key_file(impl_, filename, format, ec);
 
322
    asio::detail::throw_error(ec);
 
323
  }
 
324
 
 
325
  /// Use an RSA private key from a file.
 
326
  /**
 
327
   * This function is used to load an RSA private key into the context from a
 
328
   * file.
 
329
   *
 
330
   * @param filename The name of the file containing the RSA private key.
 
331
   *
 
332
   * @param format The file format (ASN.1 or PEM).
 
333
   *
 
334
   * @param ec Set to indicate what error occurred, if any.
 
335
   */
 
336
  asio::error_code use_rsa_private_key_file(
 
337
      const std::string& filename, file_format format,
 
338
      asio::error_code& ec)
 
339
  {
 
340
    return service_.use_rsa_private_key_file(impl_, filename, format, ec);
 
341
  }
 
342
 
 
343
  /// Use the specified file to obtain the temporary Diffie-Hellman parameters.
 
344
  /**
 
345
   * This function is used to load Diffie-Hellman parameters into the context
 
346
   * from a file.
 
347
   *
 
348
   * @param filename The name of the file containing the Diffie-Hellman
 
349
   * parameters. The file must use the PEM format.
 
350
   *
 
351
   * @throws asio::system_error Thrown on failure.
 
352
   */
 
353
  void use_tmp_dh_file(const std::string& filename)
 
354
  {
 
355
    asio::error_code ec;
 
356
    service_.use_tmp_dh_file(impl_, filename, ec);
 
357
    asio::detail::throw_error(ec);
 
358
  }
 
359
 
 
360
  /// Use the specified file to obtain the temporary Diffie-Hellman parameters.
 
361
  /**
 
362
   * This function is used to load Diffie-Hellman parameters into the context
 
363
   * from a file.
 
364
   *
 
365
   * @param filename The name of the file containing the Diffie-Hellman
 
366
   * parameters. The file must use the PEM format.
 
367
   *
 
368
   * @param ec Set to indicate what error occurred, if any.
 
369
   */
 
370
  asio::error_code use_tmp_dh_file(const std::string& filename,
 
371
      asio::error_code& ec)
 
372
  {
 
373
    return service_.use_tmp_dh_file(impl_, filename, ec);
 
374
  }
 
375
 
 
376
  /// Set the password callback.
 
377
  /**
 
378
   * This function is used to specify a callback function to obtain password
 
379
   * information about an encrypted key in PEM format.
 
380
   *
 
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.
 
386
   * ); @endcode
 
387
   * The return value of the callback is a string containing the password.
 
388
   *
 
389
   * @throws asio::system_error Thrown on failure.
 
390
   */
 
391
  template <typename PasswordCallback>
 
392
  void set_password_callback(PasswordCallback callback)
 
393
  {
 
394
    asio::error_code ec;
 
395
    service_.set_password_callback(impl_, callback, ec);
 
396
    asio::detail::throw_error(ec);
 
397
  }
 
398
 
 
399
  /// Set the password callback.
 
400
  /**
 
401
   * This function is used to specify a callback function to obtain password
 
402
   * information about an encrypted key in PEM format.
 
403
   *
 
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.
 
409
   * ); @endcode
 
410
   * The return value of the callback is a string containing the password.
 
411
   *
 
412
   * @param ec Set to indicate what error occurred, if any.
 
413
   */
 
414
  template <typename PasswordCallback>
 
415
  asio::error_code set_password_callback(PasswordCallback callback,
 
416
      asio::error_code& ec)
 
417
  {
 
418
    return service_.set_password_callback(impl_, callback, ec);
 
419
  }
 
420
 
 
421
private:
 
422
  /// The backend service implementation.
 
423
  service_type& service_;
 
424
 
 
425
  /// The underlying native implementation.
 
426
  impl_type impl_;
 
427
};
 
428
 
 
429
} // namespace ssl
 
430
} // namespace asio
 
431
 
 
432
#include "asio/detail/pop_options.hpp"
 
433
 
 
434
#endif // ASIO_SSL_BASIC_CONTEXT_HPP