TLSSession.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 { TLS_WANT_READ = 1, TLS_WANT_WRITE };
  42. enum TLSErrorCode {
  43. TLS_ERR_OK = 0,
  44. TLS_ERR_ERROR = -1,
  45. TLS_ERR_WOULDBLOCK = -2
  46. };
  47. // To create another SSL/TLS backend, implement TLSSession class below.
  48. //
  49. class TLSSession {
  50. public:
  51. static TLSSession* make(TLSContext* ctx);
  52. // MUST deallocate all resources
  53. virtual ~TLSSession() {}
  54. // Initializes SSL/TLS session. The |sockfd| is the underlying
  55. // transport socket. This function returns TLS_ERR_OK if it
  56. // succeeds, or TLS_ERR_ERROR.
  57. virtual int init(sock_t sockfd) = 0;
  58. // Sets |hostname| for TLS SNI extension. This is only meaningful for
  59. // client side session. This function returns TLS_ERR_OK if it
  60. // succeeds, or TLS_ERR_ERROR.
  61. virtual int setSNIHostname(const std::string& hostname) = 0;
  62. // Closes the SSL/TLS session. Don't close underlying transport
  63. // socket. This function returns TLS_ERR_OK if it succeeds, or
  64. // TLS_ERR_ERROR.
  65. virtual int closeConnection() = 0;
  66. // Returns TLS_WANT_READ if SSL/TLS session needs more data from
  67. // remote endpoint to proceed, or TLS_WANT_WRITE if SSL/TLS session
  68. // needs to write more data to proceed. If SSL/TLS session needs
  69. // neither read nor write data at the moment, TLS_WANT_READ must be
  70. // returned.
  71. virtual int checkDirection() = 0;
  72. // Sends |data| with length |len|. This function returns the number
  73. // of bytes sent if it succeeds, or TLS_ERR_WOULDBLOCK if the
  74. // underlying transport blocks, or TLS_ERR_ERROR.
  75. virtual ssize_t writeData(const void* data, size_t len) = 0;
  76. // Receives data into |data| with length |len|. This function returns
  77. // the number of bytes received if it succeeds, or TLS_ERR_WOULDBLOCK
  78. // if the underlying transport blocks, or TLS_ERR_ERROR.
  79. virtual ssize_t readData(void* data, size_t len) = 0;
  80. // Performs client side handshake. The |hostname| is the hostname of
  81. // the remote endpoint and is used to verify its certificate. This
  82. // function returns TLS_ERR_OK if it succeeds, or TLS_ERR_WOULDBLOCK
  83. // if the underlying transport blocks, or TLS_ERR_ERROR.
  84. // When returning TLS_ERR_ERROR, provide certificate validation error
  85. // in |handshakeErr|.
  86. virtual int tlsConnect(const std::string& hostname, TLSVersion& version,
  87. std::string& handshakeErr) = 0;
  88. // Performs server side handshake. This function returns TLS_ERR_OK
  89. // if it succeeds, or TLS_ERR_WOULDBLOCK if the underlying transport
  90. // blocks, or TLS_ERR_ERROR.
  91. virtual int tlsAccept(TLSVersion& version) = 0;
  92. // Returns last error string
  93. virtual std::string getLastErrorString() = 0;
  94. // Returns buffered length, which can be read immediately without
  95. // contacting network.
  96. virtual size_t getRecvBufferedLength() = 0;
  97. protected:
  98. TLSSession() {}
  99. private:
  100. TLSSession(const TLSSession&);
  101. TLSSession& operator=(const TLSSession&);
  102. };
  103. }
  104. #endif // TLS_SESSION_H