TLSSession.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2013 Tatsuhiro Tsujikawa
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * In addition, as a special exception, the copyright holders give
  22. * permission to link the code of portions of this program with the
  23. * OpenSSL library under certain conditions as described in each
  24. * individual source file, and distribute linked combinations
  25. * including the two.
  26. * You must obey the GNU General Public License in all respects
  27. * for all of the code used other than OpenSSL. If you modify
  28. * file(s) with this exception, you may extend this exception to your
  29. * version of the file(s), but you are not obligated to do so. If you
  30. * do not wish to do so, delete this exception statement from your
  31. * version. If you delete this exception statement from all source
  32. * files in the program, then also delete it here.
  33. */
  34. /* copyright --> */
  35. #ifndef TLS_SESSION_H
  36. #define TLS_SESSION_H
  37. #include "common.h"
  38. #include "a2netcompat.h"
  39. #include "TLSContext.h"
  40. namespace aria2 {
  41. enum TLSDirection {
  42. TLS_WANT_READ = 1,
  43. TLS_WANT_WRITE
  44. };
  45. enum TLSErrorCode {
  46. TLS_ERR_OK = 0,
  47. TLS_ERR_ERROR = -1,
  48. TLS_ERR_WOULDBLOCK = -2
  49. };
  50. // To create another SSL/TLS backend, implement TLSSession class below.
  51. //
  52. class TLSSession {
  53. public:
  54. static TLSSession* make(TLSContext* ctx);
  55. // MUST deallocate all resources
  56. virtual ~TLSSession() {}
  57. // Initializes SSL/TLS session. The |sockfd| is the underlying
  58. // tranport socket. This function returns TLS_ERR_OK if it
  59. // succeeds, or TLS_ERR_ERROR.
  60. virtual int init(sock_t sockfd) = 0;
  61. // Sets |hostname| for TLS SNI extension. This is only meaningful for
  62. // client side session. This function returns TLS_ERR_OK if it
  63. // succeeds, or TLS_ERR_ERROR.
  64. virtual int setSNIHostname(const std::string& hostname) = 0;
  65. // Closes the SSL/TLS session. Don't close underlying transport
  66. // socket. This function returns TLS_ERR_OK if it succeeds, or
  67. // TLS_ERR_ERROR.
  68. virtual int closeConnection() = 0;
  69. // Returns TLS_WANT_READ if SSL/TLS session needs more data from
  70. // remote endpoint to proceed, or TLS_WANT_WRITE if SSL/TLS session
  71. // needs to write more data to proceed. If SSL/TLS session needs
  72. // neither read nor write data at the moment, TLS_WANT_READ must be
  73. // returned.
  74. virtual int checkDirection() = 0;
  75. // Sends |data| with length |len|. This function returns the number
  76. // of bytes sent if it succeeds, or TLS_ERR_WOULDBLOCK if the
  77. // underlying tranport blocks, or TLS_ERR_ERROR.
  78. virtual ssize_t writeData(const void* data, size_t len) = 0;
  79. // Receives data into |data| with length |len|. This function returns
  80. // the number of bytes received if it succeeds, or TLS_ERR_WOULDBLOCK
  81. // if the underlying tranport blocks, or TLS_ERR_ERROR.
  82. virtual ssize_t readData(void* data, size_t len) = 0;
  83. // Performs client side handshake. The |hostname| is the hostname of
  84. // the remote endpoint and is used to verify its certificate. This
  85. // function returns TLS_ERR_OK if it succeeds, or TLS_ERR_WOULDBLOCK
  86. // if the underlying transport blocks, or TLS_ERR_ERROR.
  87. // When returning TLS_ERR_ERROR, provide certificate validation error
  88. // in |handshakeErr|.
  89. virtual int tlsConnect(const std::string& hostname, std::string& handshakeErr) = 0;
  90. // Performs server side handshake. This function returns TLS_ERR_OK
  91. // if it succeeds, or TLS_ERR_WOULDBLOCK if the underlying transport
  92. // blocks, or TLS_ERR_ERROR.
  93. virtual int tlsAccept() = 0;
  94. // Returns last error string
  95. virtual std::string getLastErrorString() = 0;
  96. protected:
  97. TLSSession() {}
  98. private:
  99. TLSSession(const TLSSession&);
  100. TLSSession& operator=(const TLSSession&);
  101. };
  102. }
  103. #endif // TLS_SESSION_H