Realistic 3D camera system
3D camera system components
openssl_context_service.hpp
Go to the documentation of this file.
1 //
2 // ssl/old/detail/openssl_context_service.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2005 Voipster / Indrek dot Juhani at voipster dot com
6 // Copyright (c) 2005-2015 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_OLD_DETAIL_OPENSSL_CONTEXT_SERVICE_HPP
13 #define ASIO_SSL_OLD_DETAIL_OPENSSL_CONTEXT_SERVICE_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/config.hpp"
20 #include <cstring>
21 #include <string>
22 #include <boost/function.hpp>
24 #include "asio/error.hpp"
25 #include "asio/io_service.hpp"
29 
31 
32 namespace asio {
33 namespace ssl {
34 namespace old {
35 namespace detail {
36 
38  : public asio::detail::service_base<openssl_context_service>
39 {
40 public:
41  // The native type of the context.
42  typedef ::SSL_CTX* impl_type;
43 
44  // The type for the password callback function object.
45  typedef boost::function<std::string(std::size_t,
47 
48  // Constructor.
50  : asio::detail::service_base<openssl_context_service>(io_service)
51  {
52  }
53 
54  // Destroy all user-defined handler objects owned by the service.
56  {
57  }
58 
59  // Return a null context implementation.
60  static impl_type null()
61  {
62  return 0;
63  }
64 
65  // Create a new context implementation.
66  void create(impl_type& impl, context_base::method m)
67  {
68  switch (m)
69  {
70 #if defined(OPENSSL_NO_SSL2)
75  break;
76 #else // defined(OPENSSL_NO_SSL2)
78  impl = ::SSL_CTX_new(::SSLv2_method());
79  break;
81  impl = ::SSL_CTX_new(::SSLv2_client_method());
82  break;
84  impl = ::SSL_CTX_new(::SSLv2_server_method());
85  break;
86 #endif // defined(OPENSSL_NO_SSL2)
88  impl = ::SSL_CTX_new(::SSLv3_method());
89  break;
91  impl = ::SSL_CTX_new(::SSLv3_client_method());
92  break;
94  impl = ::SSL_CTX_new(::SSLv3_server_method());
95  break;
97  impl = ::SSL_CTX_new(::TLSv1_method());
98  break;
100  impl = ::SSL_CTX_new(::TLSv1_client_method());
101  break;
103  impl = ::SSL_CTX_new(::TLSv1_server_method());
104  break;
106  impl = ::SSL_CTX_new(::SSLv23_method());
107  break;
109  impl = ::SSL_CTX_new(::SSLv23_client_method());
110  break;
112  impl = ::SSL_CTX_new(::SSLv23_server_method());
113  break;
114  default:
115  impl = ::SSL_CTX_new(0);
116  break;
117  }
118  }
119 
120  // Destroy a context implementation.
121  void destroy(impl_type& impl)
122  {
123  if (impl != null())
124  {
125  if (impl->default_passwd_callback_userdata)
126  {
127  password_callback_type* callback =
128  static_cast<password_callback_type*>(
129  impl->default_passwd_callback_userdata);
130  delete callback;
131  impl->default_passwd_callback_userdata = 0;
132  }
133 
134  ::SSL_CTX_free(impl);
135  impl = null();
136  }
137  }
138 
139  // Set options on the context.
140  asio::error_code set_options(impl_type& impl,
142  {
143  ::SSL_CTX_set_options(impl, o);
144 
145  ec = asio::error_code();
146  return ec;
147  }
148 
149  // Set peer verification mode.
152  {
153  ::SSL_CTX_set_verify(impl, v, 0);
154 
155  ec = asio::error_code();
156  return ec;
157  }
158 
159  // Load a certification authority file for performing verification.
161  const std::string& filename, asio::error_code& ec)
162  {
163  if (::SSL_CTX_load_verify_locations(impl, filename.c_str(), 0) != 1)
164  {
166  return ec;
167  }
168 
169  ec = asio::error_code();
170  return ec;
171  }
172 
173  // Add a directory containing certification authority files to be used for
174  // performing verification.
176  const std::string& path, asio::error_code& ec)
177  {
178  if (::SSL_CTX_load_verify_locations(impl, 0, path.c_str()) != 1)
179  {
181  return ec;
182  }
183 
184  ec = asio::error_code();
185  return ec;
186  }
187 
188  // Use a certificate from a file.
190  const std::string& filename, context_base::file_format format,
191  asio::error_code& ec)
192  {
193  int file_type;
194  switch (format)
195  {
196  case context_base::asn1:
197  file_type = SSL_FILETYPE_ASN1;
198  break;
199  case context_base::pem:
200  file_type = SSL_FILETYPE_PEM;
201  break;
202  default:
203  {
205  return ec;
206  }
207  }
208 
209  if (::SSL_CTX_use_certificate_file(impl, filename.c_str(), file_type) != 1)
210  {
212  return ec;
213  }
214 
215  ec = asio::error_code();
216  return ec;
217  }
218 
219  // Use a certificate chain from a file.
221  const std::string& filename, asio::error_code& ec)
222  {
223  if (::SSL_CTX_use_certificate_chain_file(impl, filename.c_str()) != 1)
224  {
226  return ec;
227  }
228 
229  ec = asio::error_code();
230  return ec;
231  }
232 
233  // Use a private key from a file.
235  const std::string& filename, context_base::file_format format,
236  asio::error_code& ec)
237  {
238  int file_type;
239  switch (format)
240  {
241  case context_base::asn1:
242  file_type = SSL_FILETYPE_ASN1;
243  break;
244  case context_base::pem:
245  file_type = SSL_FILETYPE_PEM;
246  break;
247  default:
248  {
250  return ec;
251  }
252  }
253 
254  if (::SSL_CTX_use_PrivateKey_file(impl, filename.c_str(), file_type) != 1)
255  {
257  return ec;
258  }
259 
260  ec = asio::error_code();
261  return ec;
262  }
263 
264  // Use an RSA private key from a file.
266  const std::string& filename, context_base::file_format format,
267  asio::error_code& ec)
268  {
269  int file_type;
270  switch (format)
271  {
272  case context_base::asn1:
273  file_type = SSL_FILETYPE_ASN1;
274  break;
275  case context_base::pem:
276  file_type = SSL_FILETYPE_PEM;
277  break;
278  default:
279  {
281  return ec;
282  }
283  }
284 
285  if (::SSL_CTX_use_RSAPrivateKey_file(
286  impl, filename.c_str(), file_type) != 1)
287  {
289  return ec;
290  }
291 
292  ec = asio::error_code();
293  return ec;
294  }
295 
296  // Use the specified file to obtain the temporary Diffie-Hellman parameters.
298  const std::string& filename, asio::error_code& ec)
299  {
300  ::BIO* bio = ::BIO_new_file(filename.c_str(), "r");
301  if (!bio)
302  {
304  return ec;
305  }
306 
307  ::DH* dh = ::PEM_read_bio_DHparams(bio, 0, 0, 0);
308  if (!dh)
309  {
310  ::BIO_free(bio);
312  return ec;
313  }
314 
315  ::BIO_free(bio);
316  int result = ::SSL_CTX_set_tmp_dh(impl, dh);
317  ::DH_free(dh);
318  if (result != 1)
319  {
321  return ec;
322  }
323 
324  ec = asio::error_code();
325  return ec;
326  }
327 
328  static int password_callback(char* buf, int size, int purpose, void* data)
329  {
330  using namespace std; // For strncat and strlen.
331 
332  if (data)
333  {
334  password_callback_type* callback =
335  static_cast<password_callback_type*>(data);
336  std::string passwd = (*callback)(static_cast<std::size_t>(size),
338  *buf = '\0';
339  strncat(buf, passwd.c_str(), size);
340  return strlen(buf);
341  }
342 
343  return 0;
344  }
345 
346  // Set the password callback.
347  template <typename Password_Callback>
349  Password_Callback callback, asio::error_code& ec)
350  {
351  // Allocate callback function object if not already present.
352  if (impl->default_passwd_callback_userdata)
353  {
354  password_callback_type* callback_function =
355  static_cast<password_callback_type*>(
356  impl->default_passwd_callback_userdata);
357  *callback_function = callback;
358  }
359  else
360  {
361  password_callback_type* callback_function =
362  new password_callback_type(callback);
363  impl->default_passwd_callback_userdata = callback_function;
364  }
365 
366  // Set the password callback.
367  SSL_CTX_set_default_passwd_cb(impl,
369 
370  ec = asio::error_code();
371  return ec;
372  }
373 
374 private:
375  // Ensure openssl is initialised.
377 };
378 
379 } // namespace detail
380 } // namespace old
381 } // namespace ssl
382 } // namespace asio
383 
385 
386 #endif // ASIO_SSL_OLD_DETAIL_OPENSSL_CONTEXT_SERVICE_HPP
The password is needed for reading/decryption.
asio::error_code set_verify_mode(impl_type &impl, context_base::verify_mode v, asio::error_code &ec)
asio::error_code load_verify_file(impl_type &impl, const std::string &filename, asio::error_code &ec)
void throw_error(const asio::error_code &err)
Definition: throw_error.hpp:31
boost::function< std::string(std::size_t, context_base::password_purpose)> password_callback_type
Provides core I/O functionality.
Definition: io_service.hpp:184
asio::error_code set_password_callback(impl_type &impl, Password_Callback callback, asio::error_code &ec)
STL namespace.
static int password_callback(char *buf, int size, int purpose, void *data)
Generic SSL version 2.
Invalid argument.
Definition: error.hpp:113
asio::error_code use_tmp_dh_file(impl_type &impl, const std::string &filename, asio::error_code &ec)
asio::error_code use_certificate_chain_file(impl_type &impl, const std::string &filename, asio::error_code &ec)
method
Different methods supported by a context.
asio::error_code use_private_key_file(impl_type &impl, const std::string &filename, context_base::file_format format, asio::error_code &ec)
asio::error_code use_rsa_private_key_file(impl_type &impl, const std::string &filename, context_base::file_format format, asio::error_code &ec)
asio::error_code set_options(impl_type &impl, context_base::options o, asio::error_code &ec)
asio::error_code use_certificate_file(impl_type &impl, const std::string &filename, context_base::file_format format, asio::error_code &ec)
Class to represent an error code value.
Definition: error_code.hpp:80
void create(impl_type &impl, context_base::method m)
password_purpose
Purpose of PEM password.
void shutdown_service()
Destroy all user-defined handler objects owned by the service.
asio::error_code add_verify_path(impl_type &impl, const std::string &path, asio::error_code &ec)
Generic TLS version 1.
The password is needed for writing/encryption.
long options
Bitmask type for SSL options.
Generic SSL version 3.
file_format
File format types.