1 // 2 // ssl/old/basic_context.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 BOOST_ASIO_SSL_OLD_BASIC_CONTEXT_HPP 13 #define BOOST_ASIO_SSL_OLD_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 <boost/asio/detail/config.hpp> 20 #include <string> 21 #include <boost/noncopyable.hpp> 22 #include <boost/asio/detail/throw_error.hpp> 23 #include <boost/asio/error.hpp> 24 #include <boost/asio/io_service.hpp> 25 #include <boost/asio/ssl/context_base.hpp> 26 27 #include <boost/asio/detail/push_options.hpp> 28 29 namespace boost { 30 namespace asio { 31 namespace ssl { 32 namespace old { 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 SSL context. 45 typedef typename service_type::impl_type impl_type; 46 47 /// Constructor. basic_context(boost::asio::io_service & io_service,method m)48 basic_context(boost::asio::io_service& io_service, method m) 49 : service_(boost::asio::use_service<Service>(io_service)), 50 impl_(service_.null()) 51 { 52 service_.create(impl_, m); 53 } 54 55 /// Destructor. ~basic_context()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 */ impl()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 boost::system::system_error Thrown on failure. 81 */ set_options(options o)82 void set_options(options o) 83 { 84 boost::system::error_code ec; 85 service_.set_options(impl_, o, ec); 86 boost::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 */ set_options(options o,boost::system::error_code & ec)99 boost::system::error_code set_options(options o, 100 boost::system::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 boost::system::system_error Thrown on failure. 114 */ set_verify_mode(verify_mode v)115 void set_verify_mode(verify_mode v) 116 { 117 boost::system::error_code ec; 118 service_.set_verify_mode(impl_, v, ec); 119 boost::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 */ set_verify_mode(verify_mode v,boost::system::error_code & ec)132 boost::system::error_code set_verify_mode(verify_mode v, 133 boost::system::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 boost::system::system_error Thrown on failure. 147 */ load_verify_file(const std::string & filename)148 void load_verify_file(const std::string& filename) 149 { 150 boost::system::error_code ec; 151 service_.load_verify_file(impl_, filename, ec); 152 boost::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 */ load_verify_file(const std::string & filename,boost::system::error_code & ec)165 boost::system::error_code load_verify_file(const std::string& filename, 166 boost::system::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 boost::system::system_error Thrown on failure. 182 */ add_verify_path(const std::string & path)183 void add_verify_path(const std::string& path) 184 { 185 boost::system::error_code ec; 186 service_.add_verify_path(impl_, path, ec); 187 boost::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 */ add_verify_path(const std::string & path,boost::system::error_code & ec)202 boost::system::error_code add_verify_path(const std::string& path, 203 boost::system::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 boost::system::system_error Thrown on failure. 217 */ use_certificate_file(const std::string & filename,file_format format)218 void use_certificate_file(const std::string& filename, file_format format) 219 { 220 boost::system::error_code ec; 221 service_.use_certificate_file(impl_, filename, format, ec); 222 boost::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 */ use_certificate_file(const std::string & filename,file_format format,boost::system::error_code & ec)235 boost::system::error_code use_certificate_file(const std::string& filename, 236 file_format format, boost::system::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 boost::system::system_error Thrown on failure. 250 */ use_certificate_chain_file(const std::string & filename)251 void use_certificate_chain_file(const std::string& filename) 252 { 253 boost::system::error_code ec; 254 service_.use_certificate_chain_file(impl_, filename, ec); 255 boost::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 */ use_certificate_chain_file(const std::string & filename,boost::system::error_code & ec)268 boost::system::error_code use_certificate_chain_file( 269 const std::string& filename, boost::system::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 boost::system::system_error Thrown on failure. 283 */ use_private_key_file(const std::string & filename,file_format format)284 void use_private_key_file(const std::string& filename, file_format format) 285 { 286 boost::system::error_code ec; 287 service_.use_private_key_file(impl_, filename, format, ec); 288 boost::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 */ use_private_key_file(const std::string & filename,file_format format,boost::system::error_code & ec)301 boost::system::error_code use_private_key_file(const std::string& filename, 302 file_format format, boost::system::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 boost::system::system_error Thrown on failure. 317 */ use_rsa_private_key_file(const std::string & filename,file_format format)318 void use_rsa_private_key_file(const std::string& filename, file_format format) 319 { 320 boost::system::error_code ec; 321 service_.use_rsa_private_key_file(impl_, filename, format, ec); 322 boost::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 */ use_rsa_private_key_file(const std::string & filename,file_format format,boost::system::error_code & ec)336 boost::system::error_code use_rsa_private_key_file( 337 const std::string& filename, file_format format, 338 boost::system::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 boost::system::system_error Thrown on failure. 352 */ use_tmp_dh_file(const std::string & filename)353 void use_tmp_dh_file(const std::string& filename) 354 { 355 boost::system::error_code ec; 356 service_.use_tmp_dh_file(impl_, filename, ec); 357 boost::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 */ use_tmp_dh_file(const std::string & filename,boost::system::error_code & ec)370 boost::system::error_code use_tmp_dh_file(const std::string& filename, 371 boost::system::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 boost::system::system_error Thrown on failure. 390 */ 391 template <typename PasswordCallback> set_password_callback(PasswordCallback callback)392 void set_password_callback(PasswordCallback callback) 393 { 394 boost::system::error_code ec; 395 service_.set_password_callback(impl_, callback, ec); 396 boost::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> set_password_callback(PasswordCallback callback,boost::system::error_code & ec)415 boost::system::error_code set_password_callback(PasswordCallback callback, 416 boost::system::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 old 430 } // namespace ssl 431 } // namespace asio 432 } // namespace boost 433 434 #include <boost/asio/detail/pop_options.hpp> 435 436 #endif // BOOST_ASIO_SSL_OLD_BASIC_CONTEXT_HPP 437