openssl_context_service.hpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. //
  2. // openssl_context_service.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. #ifndef BOOST_ASIO_SSL_DETAIL_OPENSSL_CONTEXT_SERVICE_HPP
  12. #define BOOST_ASIO_SSL_DETAIL_OPENSSL_CONTEXT_SERVICE_HPP
  13. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  14. # pragma once
  15. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  16. #include <boost/asio/detail/push_options.hpp>
  17. #include <boost/asio/detail/push_options.hpp>
  18. #include <cstring>
  19. #include <string>
  20. #include <boost/function.hpp>
  21. #include <boost/asio/detail/pop_options.hpp>
  22. #include <boost/asio/error.hpp>
  23. #include <boost/asio/io_service.hpp>
  24. #include <boost/asio/detail/service_base.hpp>
  25. #include <boost/asio/ssl/context_base.hpp>
  26. #include <boost/asio/ssl/detail/openssl_init.hpp>
  27. #include <boost/asio/ssl/detail/openssl_types.hpp>
  28. namespace boost {
  29. namespace asio {
  30. namespace ssl {
  31. namespace detail {
  32. class openssl_context_service
  33. : public boost::asio::detail::service_base<openssl_context_service>
  34. {
  35. public:
  36. // The native type of the context.
  37. typedef ::SSL_CTX* impl_type;
  38. // The type for the password callback function object.
  39. typedef boost::function<std::string(std::size_t,
  40. context_base::password_purpose)> password_callback_type;
  41. // Constructor.
  42. openssl_context_service(boost::asio::io_service& io_service)
  43. : boost::asio::detail::service_base<openssl_context_service>(io_service)
  44. {
  45. }
  46. // Destroy all user-defined handler objects owned by the service.
  47. void shutdown_service()
  48. {
  49. }
  50. // Return a null context implementation.
  51. static impl_type null()
  52. {
  53. return 0;
  54. }
  55. // Create a new context implementation.
  56. void create(impl_type& impl, context_base::method m)
  57. {
  58. switch (m)
  59. {
  60. case context_base::sslv2:
  61. impl = ::SSL_CTX_new(::SSLv2_method());
  62. break;
  63. case context_base::sslv2_client:
  64. impl = ::SSL_CTX_new(::SSLv2_client_method());
  65. break;
  66. case context_base::sslv2_server:
  67. impl = ::SSL_CTX_new(::SSLv2_server_method());
  68. break;
  69. case context_base::sslv3:
  70. impl = ::SSL_CTX_new(::SSLv3_method());
  71. break;
  72. case context_base::sslv3_client:
  73. impl = ::SSL_CTX_new(::SSLv3_client_method());
  74. break;
  75. case context_base::sslv3_server:
  76. impl = ::SSL_CTX_new(::SSLv3_server_method());
  77. break;
  78. case context_base::tlsv1:
  79. impl = ::SSL_CTX_new(::TLSv1_method());
  80. break;
  81. case context_base::tlsv1_client:
  82. impl = ::SSL_CTX_new(::TLSv1_client_method());
  83. break;
  84. case context_base::tlsv1_server:
  85. impl = ::SSL_CTX_new(::TLSv1_server_method());
  86. break;
  87. case context_base::sslv23:
  88. impl = ::SSL_CTX_new(::SSLv23_method());
  89. break;
  90. case context_base::sslv23_client:
  91. impl = ::SSL_CTX_new(::SSLv23_client_method());
  92. break;
  93. case context_base::sslv23_server:
  94. impl = ::SSL_CTX_new(::SSLv23_server_method());
  95. break;
  96. default:
  97. impl = ::SSL_CTX_new(0);
  98. break;
  99. }
  100. }
  101. // Destroy a context implementation.
  102. void destroy(impl_type& impl)
  103. {
  104. if (impl != null())
  105. {
  106. if (impl->default_passwd_callback_userdata)
  107. {
  108. password_callback_type* callback =
  109. static_cast<password_callback_type*>(
  110. impl->default_passwd_callback_userdata);
  111. delete callback;
  112. impl->default_passwd_callback_userdata = 0;
  113. }
  114. ::SSL_CTX_free(impl);
  115. impl = null();
  116. }
  117. }
  118. // Set options on the context.
  119. boost::system::error_code set_options(impl_type& impl,
  120. context_base::options o, boost::system::error_code& ec)
  121. {
  122. ::SSL_CTX_set_options(impl, o);
  123. ec = boost::system::error_code();
  124. return ec;
  125. }
  126. // Set peer verification mode.
  127. boost::system::error_code set_verify_mode(impl_type& impl,
  128. context_base::verify_mode v, boost::system::error_code& ec)
  129. {
  130. ::SSL_CTX_set_verify(impl, v, 0);
  131. ec = boost::system::error_code();
  132. return ec;
  133. }
  134. // Load a certification authority file for performing verification.
  135. boost::system::error_code load_verify_file(impl_type& impl,
  136. const std::string& filename, boost::system::error_code& ec)
  137. {
  138. if (::SSL_CTX_load_verify_locations(impl, filename.c_str(), 0) != 1)
  139. {
  140. ec = boost::asio::error::invalid_argument;
  141. return ec;
  142. }
  143. ec = boost::system::error_code();
  144. return ec;
  145. }
  146. // Add a directory containing certification authority files to be used for
  147. // performing verification.
  148. boost::system::error_code add_verify_path(impl_type& impl,
  149. const std::string& path, boost::system::error_code& ec)
  150. {
  151. if (::SSL_CTX_load_verify_locations(impl, 0, path.c_str()) != 1)
  152. {
  153. ec = boost::asio::error::invalid_argument;
  154. return ec;
  155. }
  156. ec = boost::system::error_code();
  157. return ec;
  158. }
  159. // Use a certificate from a file.
  160. boost::system::error_code use_certificate_file(impl_type& impl,
  161. const std::string& filename, context_base::file_format format,
  162. boost::system::error_code& ec)
  163. {
  164. int file_type;
  165. switch (format)
  166. {
  167. case context_base::asn1:
  168. file_type = SSL_FILETYPE_ASN1;
  169. break;
  170. case context_base::pem:
  171. file_type = SSL_FILETYPE_PEM;
  172. break;
  173. default:
  174. {
  175. ec = boost::asio::error::invalid_argument;
  176. return ec;
  177. }
  178. }
  179. if (::SSL_CTX_use_certificate_file(impl, filename.c_str(), file_type) != 1)
  180. {
  181. ec = boost::asio::error::invalid_argument;
  182. return ec;
  183. }
  184. ec = boost::system::error_code();
  185. return ec;
  186. }
  187. // Use a certificate chain from a file.
  188. boost::system::error_code use_certificate_chain_file(impl_type& impl,
  189. const std::string& filename, boost::system::error_code& ec)
  190. {
  191. if (::SSL_CTX_use_certificate_chain_file(impl, filename.c_str()) != 1)
  192. {
  193. ec = boost::asio::error::invalid_argument;
  194. return ec;
  195. }
  196. ec = boost::system::error_code();
  197. return ec;
  198. }
  199. // Use a private key from a file.
  200. boost::system::error_code use_private_key_file(impl_type& impl,
  201. const std::string& filename, context_base::file_format format,
  202. boost::system::error_code& ec)
  203. {
  204. int file_type;
  205. switch (format)
  206. {
  207. case context_base::asn1:
  208. file_type = SSL_FILETYPE_ASN1;
  209. break;
  210. case context_base::pem:
  211. file_type = SSL_FILETYPE_PEM;
  212. break;
  213. default:
  214. {
  215. ec = boost::asio::error::invalid_argument;
  216. return ec;
  217. }
  218. }
  219. if (::SSL_CTX_use_PrivateKey_file(impl, filename.c_str(), file_type) != 1)
  220. {
  221. ec = boost::asio::error::invalid_argument;
  222. return ec;
  223. }
  224. ec = boost::system::error_code();
  225. return ec;
  226. }
  227. // Use an RSA private key from a file.
  228. boost::system::error_code use_rsa_private_key_file(impl_type& impl,
  229. const std::string& filename, context_base::file_format format,
  230. boost::system::error_code& ec)
  231. {
  232. int file_type;
  233. switch (format)
  234. {
  235. case context_base::asn1:
  236. file_type = SSL_FILETYPE_ASN1;
  237. break;
  238. case context_base::pem:
  239. file_type = SSL_FILETYPE_PEM;
  240. break;
  241. default:
  242. {
  243. ec = boost::asio::error::invalid_argument;
  244. return ec;
  245. }
  246. }
  247. if (::SSL_CTX_use_RSAPrivateKey_file(
  248. impl, filename.c_str(), file_type) != 1)
  249. {
  250. ec = boost::asio::error::invalid_argument;
  251. return ec;
  252. }
  253. ec = boost::system::error_code();
  254. return ec;
  255. }
  256. // Use the specified file to obtain the temporary Diffie-Hellman parameters.
  257. boost::system::error_code use_tmp_dh_file(impl_type& impl,
  258. const std::string& filename, boost::system::error_code& ec)
  259. {
  260. ::BIO* bio = ::BIO_new_file(filename.c_str(), "r");
  261. if (!bio)
  262. {
  263. ec = boost::asio::error::invalid_argument;
  264. return ec;
  265. }
  266. ::DH* dh = ::PEM_read_bio_DHparams(bio, 0, 0, 0);
  267. if (!dh)
  268. {
  269. ::BIO_free(bio);
  270. ec = boost::asio::error::invalid_argument;
  271. return ec;
  272. }
  273. ::BIO_free(bio);
  274. int result = ::SSL_CTX_set_tmp_dh(impl, dh);
  275. ::DH_free(dh);
  276. if (result != 1)
  277. {
  278. ec = boost::asio::error::invalid_argument;
  279. return ec;
  280. }
  281. ec = boost::system::error_code();
  282. return ec;
  283. }
  284. static int password_callback(char* buf, int size, int purpose, void* data)
  285. {
  286. using namespace std; // For strncat and strlen.
  287. if (data)
  288. {
  289. password_callback_type* callback =
  290. static_cast<password_callback_type*>(data);
  291. std::string passwd = (*callback)(static_cast<std::size_t>(size),
  292. purpose ? context_base::for_writing : context_base::for_reading);
  293. *buf = '\0';
  294. strncat(buf, passwd.c_str(), size);
  295. return strlen(buf);
  296. }
  297. return 0;
  298. }
  299. // Set the password callback.
  300. template <typename Password_Callback>
  301. boost::system::error_code set_password_callback(impl_type& impl,
  302. Password_Callback callback, boost::system::error_code& ec)
  303. {
  304. // Allocate callback function object if not already present.
  305. if (impl->default_passwd_callback_userdata)
  306. {
  307. password_callback_type* callback_function =
  308. static_cast<password_callback_type*>(
  309. impl->default_passwd_callback_userdata);
  310. *callback_function = callback;
  311. }
  312. else
  313. {
  314. password_callback_type* callback_function =
  315. new password_callback_type(callback);
  316. impl->default_passwd_callback_userdata = callback_function;
  317. }
  318. // Set the password callback.
  319. SSL_CTX_set_default_passwd_cb(impl,
  320. &openssl_context_service::password_callback);
  321. ec = boost::system::error_code();
  322. return ec;
  323. }
  324. private:
  325. // Ensure openssl is initialised.
  326. openssl_init<> init_;
  327. };
  328. } // namespace detail
  329. } // namespace ssl
  330. } // namespace asio
  331. } // namespace boost
  332. #include <boost/asio/detail/pop_options.hpp>
  333. #endif // BOOST_ASIO_SSL_DETAIL_OPENSSL_CONTEXT_SERVICE_HPP