ssl.qbk 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. [/
  2. / Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  3. /
  4. / Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. / file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. /]
  7. [section:ssl SSL]
  8. Boost.Asio contains classes and class templates for basic SSL support. These classes
  9. allow encrypted communication to be layered on top of an existing stream, such
  10. as a TCP socket.
  11. Before creating an encrypted stream, an application must construct an SSL
  12. context object. This object is used to set SSL options such as verification
  13. mode, certificate files, and so on. As an illustration, client-side
  14. initialisation may look something like:
  15. ssl::context ctx(ssl::context::sslv23);
  16. ctx.set_verify_mode(ssl::verify_peer);
  17. ctx.load_verify_file("ca.pem");
  18. To use SSL with a TCP socket, one may write:
  19. ssl::stream<ip::tcp::socket> ssl_sock(my_io_context, ctx);
  20. To perform socket-specific operations, such as establishing an outbound
  21. connection or accepting an incoming one, the underlying socket must first be
  22. obtained using the `ssl::stream` template's [link
  23. boost_asio.reference.ssl__stream.lowest_layer `lowest_layer()`] member function:
  24. ip::tcp::socket::lowest_layer_type& sock = ssl_sock.lowest_layer();
  25. sock.connect(my_endpoint);
  26. In some use cases the underlying stream object will need to have a longer
  27. lifetime than the SSL stream, in which case the template parameter should be a
  28. reference to the stream type:
  29. ip::tcp::socket sock(my_io_context);
  30. ssl::stream<ip::tcp::socket&> ssl_sock(sock, ctx);
  31. SSL handshaking must be performed prior to transmitting or receiving data over
  32. an encrypted connection. This is accomplished using the `ssl::stream`
  33. template's [link boost_asio.reference.ssl__stream.handshake handshake()] or [link
  34. boost_asio.reference.ssl__stream.async_handshake async_handshake()] member functions.
  35. Once connected, SSL stream objects are used as synchronous or asynchronous read
  36. and write streams. This means the objects can be used with any of the [link
  37. boost_asio.reference.read read()], [link boost_asio.reference.async_read async_read()],
  38. [link boost_asio.reference.write write()], [link boost_asio.reference.async_write
  39. async_write()], [link boost_asio.reference.read_until read_until()] or [link
  40. boost_asio.reference.async_read_until async_read_until()] free functions.
  41. [heading Certificate Verification]
  42. Boost.Asio provides various methods for configuring the way SSL certificates are
  43. verified:
  44. * [link boost_asio.reference.ssl__context.set_default_verify_paths ssl::context::set_default_verify_paths()]
  45. * [link boost_asio.reference.ssl__context.set_verify_mode ssl::context::set_verify_mode()]
  46. * [link boost_asio.reference.ssl__context.set_verify_callback ssl::context::set_verify_callback()]
  47. * [link boost_asio.reference.ssl__context.load_verify_file ssl::context::load_verify_file()]
  48. * [link boost_asio.reference.ssl__stream.set_verify_mode ssl::stream::set_verify_mode()]
  49. * [link boost_asio.reference.ssl__stream.set_verify_callback ssl::stream::set_verify_callback()]
  50. To simplify use cases where certificates are verified according to the rules in
  51. RFC 2818 (certificate verification for HTTPS), Boost.Asio provides a reusable
  52. verification callback as a function object:
  53. * [link boost_asio.reference.ssl__rfc2818_verification ssl::rfc2818_verification]
  54. The following example shows verification of a remote host's certificate
  55. according to the rules used by HTTPS:
  56. using boost::asio::ip::tcp;
  57. namespace ssl = boost::asio::ssl;
  58. typedef ssl::stream<tcp::socket> ssl_socket;
  59. // Create a context that uses the default paths for
  60. // finding CA certificates.
  61. ssl::context ctx(ssl::context::sslv23);
  62. ctx.set_default_verify_paths();
  63. // Open a socket and connect it to the remote host.
  64. boost::asio::io_context io_context;
  65. ssl_socket sock(io_context, ctx);
  66. tcp::resolver resolver(io_context);
  67. tcp::resolver::query query("host.name", "https");
  68. boost::asio::connect(sock.lowest_layer(), resolver.resolve(query));
  69. sock.lowest_layer().set_option(tcp::no_delay(true));
  70. // Perform SSL handshake and verify the remote host's
  71. // certificate.
  72. sock.set_verify_mode(ssl::verify_peer);
  73. sock.set_verify_callback(ssl::rfc2818_verification("host.name"));
  74. sock.handshake(ssl_socket::client);
  75. // ... read and write as normal ...
  76. [heading SSL and Threads]
  77. SSL stream objects perform no locking of their own. Therefore, it is essential
  78. that all asynchronous SSL operations are performed in an implicit or explicit
  79. [link boost_asio.overview.core.strands strand]. Note that this means that no
  80. synchronisation is required (and so no locking overhead is incurred) in single
  81. threaded programs.
  82. [heading See Also]
  83. [link boost_asio.reference.ssl__context ssl::context],
  84. [link boost_asio.reference.ssl__rfc2818_verification ssl::rfc2818_verification],
  85. [link boost_asio.reference.ssl__stream ssl::stream],
  86. [link boost_asio.examples.cpp03_examples.ssl SSL example (C++03)],
  87. [link boost_asio.examples.cpp11_examples.ssl SSL example (C++11)].
  88. [heading Notes]
  89. [@http://www.openssl.org OpenSSL] is required to make use of Boost.Asio's SSL
  90. support. When an application needs to use OpenSSL functionality that is not
  91. wrapped by Boost.Asio, the underlying OpenSSL types may be obtained by calling [link
  92. boost_asio.reference.ssl__context.native_handle `ssl::context::native_handle()`] or
  93. [link boost_asio.reference.ssl__stream.native_handle `ssl::stream::native_handle()`].
  94. [endsect]