AppleTLSSession.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. enum state_t {
  43. st_constructed,
  44. st_initialized,
  45. st_connected,
  46. st_closed,
  47. st_error
  48. };
  49. public:
  50. AppleTLSSession(AppleTLSContext* ctx);
  51. // MUST deallocate all resources
  52. virtual ~AppleTLSSession();
  53. // Initializes SSL/TLS session. The |sockfd| is the underlying
  54. // tranport socket. This function returns TLS_ERR_OK if it
  55. // succeeds, or TLS_ERR_ERROR.
  56. virtual int init(sock_t sockfd) CXX11_OVERRIDE;
  57. // Sets |hostname| for TLS SNI extension. This is only meaningful for
  58. // client side session. This function returns TLS_ERR_OK if it
  59. // succeeds, or TLS_ERR_ERROR.
  60. virtual int setSNIHostname(const std::string& hostname) CXX11_OVERRIDE;
  61. // Closes the SSL/TLS session. Don't close underlying transport
  62. // socket. This function returns TLS_ERR_OK if it succeeds, or
  63. // TLS_ERR_ERROR.
  64. virtual int closeConnection() CXX11_OVERRIDE;
  65. // Returns TLS_WANT_READ if SSL/TLS session needs more data from
  66. // remote endpoint to proceed, or TLS_WANT_WRITE if SSL/TLS session
  67. // needs to write more data to proceed. If SSL/TLS session needs
  68. // neither read nor write data at the moment, return value is
  69. // undefined.
  70. virtual int checkDirection() CXX11_OVERRIDE;
  71. // Sends |data| with length |len|. This function returns the number
  72. // of bytes sent if it succeeds, or TLS_ERR_WOULDBLOCK if the
  73. // underlying tranport blocks, or TLS_ERR_ERROR.
  74. virtual ssize_t writeData(const void* data, size_t len) CXX11_OVERRIDE;
  75. // Receives data into |data| with length |len|. This function returns
  76. // the number of bytes received if it succeeds, or TLS_ERR_WOULDBLOCK
  77. // if the underlying tranport blocks, or TLS_ERR_ERROR.
  78. virtual ssize_t readData(void* data, size_t len) CXX11_OVERRIDE;
  79. // Performs client side handshake. The |hostname| is the hostname of
  80. // the remote endpoint and is used to verify its certificate. This
  81. // function returns TLS_ERR_OK if it succeeds, or TLS_ERR_WOULDBLOCK
  82. // if the underlying transport blocks, or TLS_ERR_ERROR.
  83. // When returning TLS_ERR_ERROR, provide certificate validation error
  84. // in |handshakeErr|.
  85. virtual int tlsConnect
  86. (const std::string& hostname, std::string& handshakeErr) CXX11_OVERRIDE;
  87. // Performs server side handshake. This function returns TLS_ERR_OK
  88. // if it succeeds, or TLS_ERR_WOULDBLOCK if the underlying transport
  89. // blocks, or TLS_ERR_ERROR.
  90. virtual int tlsAccept() CXX11_OVERRIDE;
  91. // Returns last error string
  92. virtual std::string getLastErrorString() CXX11_OVERRIDE;
  93. private:
  94. static OSStatus SocketWrite(SSLConnectionRef conn, const void* data, size_t* len) {
  95. return ((AppleTLSSession*)conn)->sockWrite(data, len);
  96. }
  97. static OSStatus SocketRead(SSLConnectionRef conn, void* data, size_t* len) {
  98. return ((AppleTLSSession*)conn)->sockRead(data, len);
  99. }
  100. AppleTLSContext *ctx_;
  101. SSLContextRef sslCtx_;
  102. sock_t sockfd_;
  103. state_t state_;
  104. OSStatus lastError_;
  105. size_t writeBuffered_;
  106. OSStatus sockWrite(const void* data, size_t* len);
  107. OSStatus sockRead(void* data, size_t* len);
  108. };
  109. }
  110. #endif // TLS_SESSION_H