AppleTLSSession.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2013 Nils Maier
  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 APPLE_TLS_SESSION_H
  36. #define APPLE_TLS_SESSION_H
  37. #include "common.h"
  38. #include "TLSSession.h"
  39. #include "AppleTLSContext.h"
  40. namespace aria2 {
  41. class AppleTLSSession : public TLSSession
  42. {
  43. enum state_t {
  44. st_constructed,
  45. st_initialized,
  46. st_connected,
  47. st_closed,
  48. st_error
  49. };
  50. public:
  51. AppleTLSSession(AppleTLSContext* ctx);
  52. // MUST deallocate all resources
  53. virtual ~AppleTLSSession();
  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) CXX11_OVERRIDE;
  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) CXX11_OVERRIDE;
  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() CXX11_OVERRIDE;
  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, return value is
  70. // undefined.
  71. virtual int checkDirection() CXX11_OVERRIDE;
  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) CXX11_OVERRIDE;
  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) CXX11_OVERRIDE;
  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,
  87. TLSVersion& version,
  88. std::string& handshakeErr) CXX11_OVERRIDE;
  89. // Performs server side handshake. This function returns TLS_ERR_OK
  90. // if it succeeds, or TLS_ERR_WOULDBLOCK if the underlying transport
  91. // blocks, or TLS_ERR_ERROR.
  92. virtual int tlsAccept(TLSVersion& version) CXX11_OVERRIDE;
  93. // Returns last error string
  94. virtual std::string getLastErrorString() CXX11_OVERRIDE;
  95. private:
  96. static OSStatus
  97. SocketWrite(SSLConnectionRef conn, const void* data, size_t* len)
  98. {
  99. return ((AppleTLSSession*)conn)->sockWrite(data, len);
  100. }
  101. static OSStatus SocketRead(SSLConnectionRef conn, void* data, size_t* len)
  102. {
  103. return ((AppleTLSSession*)conn)->sockRead(data, len);
  104. }
  105. SSLContextRef sslCtx_;
  106. sock_t sockfd_;
  107. state_t state_;
  108. OSStatus lastError_;
  109. size_t writeBuffered_;
  110. OSStatus sockWrite(const void* data, size_t* len);
  111. OSStatus sockRead(void* data, size_t* len);
  112. };
  113. }
  114. #endif // TLS_SESSION_H