Ticket #7610: boost-psk-patch.diff

File boost-psk-patch.diff, 11.6 KB (added by Roman Bovsunovskiy <a2k0001@…>, 10 years ago)

PSK support for boost::asio::ssl

  • asio/ssl/context.hpp

    diff -rupN '-x*DS*' /home/a2k/tmp/boost_1_52_0_beta1/boost/asio/ssl/context.hpp asio/ssl/context.hpp
    old new  
    2828# include <boost/asio/ssl/detail/openssl_init.hpp>
    2929# include <boost/asio/ssl/detail/password_callback.hpp>
    3030# include <boost/asio/ssl/detail/verify_callback.hpp>
     31# include <boost/asio/ssl/detail/psk_callback.hpp>
    3132# include <boost/asio/ssl/verify_mode.hpp>
    3233#endif // defined(BOOST_ASIO_ENABLE_OLD_SSL)
    3334
    public:  
    167168  BOOST_ASIO_DECL boost::system::error_code set_verify_mode(
    168169      verify_mode v, boost::system::error_code& ec);
    169170
     171  BOOST_ASIO_DECL void set_psk_identity(const char* psk);
     172
     173  template <typename PSKCallback>
     174  void set_psk_server_callback(PSKCallback callback);
     175
     176  template <typename PSKCallback>
     177  boost::system::error_code set_psk_server_callback(PSKCallback callback,
     178      boost::system::error_code& ec);
     179
    170180  /// Set the callback used to verify peer certificates.
    171181  /**
    172182   * This function is used to specify a callback function that will be called
    public:  
    494504      boost::system::error_code& ec);
    495505
    496506private:
     507  // Helper function used to set a peer identity and PSK callback.
     508  BOOST_ASIO_DECL boost::system::error_code do_set_psk_server_callback(
     509      detail::psk_callback_base* callback, boost::system::error_code& ec);
     510
     511  // Callback used when the SSL implementation wants to verify a PSK.
     512  BOOST_ASIO_DECL static unsigned int psk_server_callback_function(SSL *ssl,
     513      const char* identity, unsigned char* psk, unsigned int max_psk_len);
     514
    497515  // Helper function used to set a peer certificate verification callback.
    498516  BOOST_ASIO_DECL boost::system::error_code do_set_verify_callback(
    499517      detail::verify_callback_base* callback, boost::system::error_code& ec);
  • asio/ssl/detail/engine.hpp

    diff -rupN '-x*DS*' /home/a2k/tmp/boost_1_52_0_beta1/boost/asio/ssl/detail/engine.hpp asio/ssl/detail/engine.hpp
    old new public:  
    7777  BOOST_ASIO_DECL boost::system::error_code set_verify_callback(
    7878      verify_callback_base* callback, boost::system::error_code& ec);
    7979
     80  // Set a peer PSK verification callback
     81  BOOST_ASIO_DECL boost::system::error_code set_psk_server_callback(
     82      psk_callback_base* callback, boost::system::error_code& ec);
     83
    8084  // Perform an SSL handshake using either SSL_connect (client-side) or
    8185  // SSL_accept (server-side).
    8286  BOOST_ASIO_DECL want handshake(
    private:  
    116120  BOOST_ASIO_DECL static int verify_callback_function(
    117121      int preverified, X509_STORE_CTX* ctx);
    118122
     123  BOOST_ASIO_DECL static unsigned int psk_server_callback_function(
     124      SSL *ssl, const char* identity, unsigned char* psk, unsigned int max_psk_len);
     125
    119126  // The SSL_accept function may not be thread safe. This mutex is used to
    120127  // protect all calls to the SSL_accept function.
    121128  BOOST_ASIO_DECL static boost::asio::detail::static_mutex& accept_mutex();
  • asio/ssl/detail/impl/engine.ipp

    diff -rupN '-x*DS*' /home/a2k/tmp/boost_1_52_0_beta1/boost/asio/ssl/detail/impl/engine.ipp asio/ssl/detail/impl/engine.ipp
    old new boost::system::error_code engine::set_ve  
    9888  return ec;
    9989}
    10090
     91boost::system::error_code engine::set_psk_server_callback(
     92    psk_callback_base* callback, boost::system::error_code& ec)
     93{
     94  if (SSL_get_app_data(ssl_))
     95    delete static_cast<psk_callback_base*>(SSL_get_app_data(ssl_));
     96
     97  SSL_set_app_data(ssl_, callback);
     98
     99  ::SSL_set_psk_server_callback(ssl_, &engine::psk_server_callback_function);
     100
     101  ec = boost::system::error_code();
     102  return ec;
     103}
     104
    101105int engine::verify_callback_function(int preverified, X509_STORE_CTX* ctx)
    102106{
    103107  if (ctx)
    int engine::verify_callback_function(int  
    119123  }
    120124
    121125  return 0;
     126}
     127
     128unsigned int engine::psk_server_callback_function(SSL *ssl,
     129    const char* identity, unsigned char* psk, unsigned int max_psk_len)
     130{
     131  if (SSL_get_app_data(ssl))
     132  {
     133    psk_callback_base* callback =
     134      static_cast<psk_callback_base*>(
     135          SSL_get_app_data(ssl));
     136
     137    psk_context psk_ctx(ssl, identity, psk, max_psk_len);
     138    return callback->call(psk_ctx);
     139  }
     140
     141  return 0;
    122142}
    123143
    124144engine::want engine::handshake(
  • asio/ssl/detail/psk_callback.hpp

    diff -rupN '-x*DS*' /home/a2k/tmp/boost_1_52_0_beta1/boost/asio/ssl/detail/psk_callback.hpp asio/ssl/detail/psk_callback.hpp
    old new  
     1//
     2// ssl/detail/psk_callback.hpp
     3// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     4//
     5// Copyright (c) 2012 Roman Bovsunovskiy (a2k0001 at gmail dot com)
     6//
     7// Distributed under the Boost Software License, Version 1.0. (See accompanying
     8// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
     9//
     10
     11#ifndef BOOST_ASIO_SSL_DETAIL_PSK_CALLBACK_HPP
     12#define BOOST_ASIO_SSL_DETAIL_PSK_CALLBACK_HPP
     13
     14#if defined(_MSC_VER) && (_MSC_VER >= 1200)
     15# pragma once
     16#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
     17
     18#include <boost/asio/detail/config.hpp>
     19
     20#if !defined(BOOST_ASIO_ENABLE_OLD_SSL)
     21# include <boost/asio/ssl/psk_context.hpp>
     22#endif // !defined(BOOST_ASIO_ENABLE_OLD_SSL)
     23
     24#include <boost/asio/detail/push_options.hpp>
     25
     26namespace boost {
     27namespace asio {
     28namespace ssl {
     29namespace detail {
     30
     31#if !defined(BOOST_ASIO_ENABLE_OLD_SSL)
     32
     33class psk_callback_base
     34{
     35public:
     36  virtual ~psk_callback_base()
     37  {
     38  }
     39
     40  virtual unsigned int call(psk_context& ctx) = 0;
     41};
     42
     43template <typename PSKCallback>
     44class psk_callback : public psk_callback_base
     45{
     46public:
     47  explicit psk_callback(PSKCallback callback)
     48    : callback_(callback)
     49  {
     50  }
     51
     52  virtual unsigned int call(psk_context& ctx)
     53  {
     54    return callback_(ctx);
     55  }
     56
     57private:
     58  PSKCallback callback_;
     59};
     60
     61#endif // !defined(BOOST_ASIO_ENABLE_OLD_SSL)
     62
     63} // namespace detail
     64} // namespace ssl
     65} // namespace asio
     66} // namespace boost
     67
     68#include <boost/asio/detail/pop_options.hpp>
     69
     70#endif // BOOST_ASIO_SSL_DETAIL_PSK_CALLBACK_HPP
  • asio/ssl/impl/context.hpp

    diff -rupN '-x*DS*' /home/a2k/tmp/boost_1_52_0_beta1/boost/asio/ssl/impl/context.hpp asio/ssl/impl/context.hpp
    old new namespace ssl {  
    3030
    3131#if !defined(BOOST_ASIO_ENABLE_OLD_SSL)
    3232
     33template <typename PSKCallback>
     34void context::set_psk_server_callback(PSKCallback callback)
     35{
     36  boost::system::error_code ec;
     37  this->set_psk_server_callback(callback, ec);
     38  boost::asio::detail::throw_error(ec, "set_psk_callback");
     39}
     40
     41template <typename PSKCallback>
     42boost::system::error_code context::set_psk_server_callback(PSKCallback callback,
     43    boost::system::error_code& ec)
     44{
     45  return do_set_psk_server_callback(
     46      new detail::psk_callback<PSKCallback>(callback), ec);
     47}
     48
    3349template <typename VerifyCallback>
    3450void context::set_verify_callback(VerifyCallback callback)
    3551{
  • asio/ssl/impl/context.ipp

    diff -rupN '-x*DS*' /home/a2k/tmp/boost_1_52_0_beta1/boost/asio/ssl/impl/context.ipp asio/ssl/impl/context.ipp
    old new int context::verify_callback_function(in  
    475487  return 0;
    476488}
    477489
     490BOOST_ASIO_DECL void context::set_psk_identity(const char* identity)
     491{
     492  SSL_CTX_use_psk_identity_hint(handle_, identity);
     493}
     494
     495boost::system::error_code context::do_set_psk_server_callback(
     496      detail::psk_callback_base* callback, boost::system::error_code& ec)
     497{
     498  SSL_CTX_set_app_data(handle_, callback);
     499
     500  ::SSL_CTX_set_psk_server_callback(handle_,
     501      &context::psk_server_callback_function);
     502
     503  ec = boost::system::error_code();
     504  return ec;
     505}
     506
     507unsigned int context::psk_server_callback_function(SSL *ssl,
     508      const char* identity, unsigned char* psk, unsigned int max_psk_len)
     509{
     510  SSL_CTX* handle = ::SSL_get_SSL_CTX(ssl);
     511
     512  detail::psk_callback_base* callback =
     513    static_cast<detail::psk_callback_base*>(
     514        SSL_CTX_get_app_data(handle));
     515
     516  psk_context psk_ctx(ssl, identity, psk, max_psk_len);
     517  return callback->call(psk_ctx);
     518}
     519
    478520boost::system::error_code context::do_set_password_callback(
    479521    detail::password_callback_base* callback, boost::system::error_code& ec)
    480522{
  • asio/ssl/psk_context.hpp

    diff -rupN '-x*DS*' /home/a2k/tmp/boost_1_52_0_beta1/boost/asio/ssl/psk_context.hpp asio/ssl/psk_context.hpp
    old new  
     1//
     2// ssl/psk_context.hpp
     3// ~~~~~~~~~~~~~~~~~~~~~~
     4//
     5// Copyright (c) 2012 Roman Bovsunovskiy (a2k0001 at gmail dot com)
     6//
     7// Distributed under the Boost Software License, Version 1.0. (See accompanying
     8// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
     9//
     10
     11#ifndef BOOST_ASIO_SSL_PSK_CONTEXT_HPP
     12#define BOOST_ASIO_SSL_PSK_CONTEXT_HPP
     13
     14#if defined(_MSC_VER) && (_MSC_VER >= 1200)
     15# pragma once
     16#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
     17
     18#include <boost/asio/detail/config.hpp>
     19
     20#if !defined(BOOST_ASIO_ENABLE_OLD_SSL)
     21# include <boost/asio/detail/noncopyable.hpp>
     22# include <boost/asio/ssl/detail/openssl_types.hpp>
     23#endif // !defined(BOOST_ASIO_ENABLE_OLD_SSL)
     24
     25#include <boost/asio/detail/push_options.hpp>
     26
     27namespace boost {
     28namespace asio {
     29namespace ssl {
     30
     31#if !defined(BOOST_ASIO_ENABLE_OLD_SSL)
     32
     33/**
     34 * @note The psk_context does not own the underlying PSK string.
     35 */
     36class psk_context
     37  : private noncopyable
     38{
     39public:
     40  /// The native handle type of the PSK context.
     41  typedef SSL* native_handle_ssl_type;
     42  typedef const char* native_handle_identity_type;
     43  typedef unsigned char* native_handle_psk_type;
     44  typedef unsigned int native_handle_psk_maxlen_type;
     45
     46  /// Constructor.
     47  explicit psk_context(native_handle_ssl_type ssl_handle,
     48                       native_handle_identity_type identity_handle,
     49                       native_handle_psk_type psk_handle,
     50                       native_handle_psk_maxlen_type psk_maxlen_handle)
     51    : ssl_handle_(ssl_handle_),
     52    identity_handle_(identity_handle),
     53    psk_handle_(psk_handle),
     54    psk_maxlen_handle_(psk_maxlen_handle)
     55  {
     56  }
     57
     58  /// Get the underlying implementation in the native type.
     59  /**
     60   * This function may be used to obtain the underlying implementation of the
     61   * context. This is intended to allow access to context functionality that is
     62   * not otherwise provided.
     63   */
     64  native_handle_ssl_type native_ssl_handle()
     65  {
     66    return ssl_handle_;
     67  }
     68
     69  native_handle_identity_type native_identity_handle()
     70  {
     71    return identity_handle_;
     72  }
     73
     74  native_handle_psk_type native_psk_handle()
     75  {
     76    return psk_handle_;
     77  }
     78  native_handle_psk_maxlen_type native_psk_maxlen_handle()
     79  {
     80    return psk_maxlen_handle_;
     81  }
     82
     83private:
     84  // The underlying native implementation.
     85  native_handle_ssl_type ssl_handle_;
     86  native_handle_identity_type identity_handle_;
     87  native_handle_psk_type psk_handle_;
     88  native_handle_psk_maxlen_type psk_maxlen_handle_;
     89};
     90
     91#endif // defined(BOOST_ASIO_ENABLE_OLD_SSL)
     92
     93} // namespace ssl
     94} // namespace asio
     95} // namespace boost
     96
     97#include <boost/asio/detail/pop_options.hpp>
     98
     99#endif // BOOST_ASIO_SSL_PSK_CONTEXT_HPP