Socket.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * Copyright (c) 2020, Ali Mohammad Pur <ali.mpfard@gmail.com>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <LibCore/Timer.h>
  27. #include <LibCrypto/ASN1/DER.h>
  28. #include <LibCrypto/PK/Code/EMSA_PSS.h>
  29. #include <LibTLS/TLSv12.h>
  30. namespace TLS {
  31. Optional<ByteBuffer> TLSv12::read()
  32. {
  33. if (m_context.application_buffer.size()) {
  34. auto buf = m_context.application_buffer.slice(0, m_context.application_buffer.size());
  35. m_context.application_buffer.clear();
  36. return buf;
  37. }
  38. return {};
  39. }
  40. ByteBuffer TLSv12::read(size_t max_size)
  41. {
  42. if (m_context.application_buffer.size()) {
  43. auto length = min(m_context.application_buffer.size(), max_size);
  44. auto buf = m_context.application_buffer.slice(0, length);
  45. m_context.application_buffer = m_context.application_buffer.slice(length, m_context.application_buffer.size() - length);
  46. return buf;
  47. }
  48. return {};
  49. }
  50. ByteBuffer TLSv12::read_line(size_t max_size)
  51. {
  52. if (!can_read_line())
  53. return {};
  54. auto* start = m_context.application_buffer.data();
  55. auto* newline = (u8*)memchr(m_context.application_buffer.data(), '\n', m_context.application_buffer.size());
  56. ASSERT(newline);
  57. size_t offset = newline - start;
  58. if (offset > max_size)
  59. return {};
  60. auto buffer = ByteBuffer::copy(start, offset);
  61. m_context.application_buffer = m_context.application_buffer.slice(offset + 1, m_context.application_buffer.size() - offset - 1);
  62. return buffer;
  63. }
  64. bool TLSv12::write(const ByteBuffer& buffer)
  65. {
  66. if (m_context.connection_status != ConnectionStatus::Established) {
  67. dbg() << "write request while not connected";
  68. return false;
  69. }
  70. PacketBuilder builder { MessageType::ApplicationData, m_context.version, buffer.size() };
  71. builder.append(buffer);
  72. auto packet = builder.build();
  73. update_packet(packet);
  74. write_packet(packet);
  75. return true;
  76. }
  77. bool TLSv12::connect(const String& hostname, int port)
  78. {
  79. set_sni(hostname);
  80. return Core::Socket::connect(hostname, port);
  81. }
  82. bool TLSv12::common_connect(const struct sockaddr* saddr, socklen_t length)
  83. {
  84. if (m_context.critical_error)
  85. return false;
  86. if (Core::Socket::is_connected()) {
  87. if (is_established()) {
  88. ASSERT_NOT_REACHED();
  89. } else {
  90. Core::Socket::close(); // reuse?
  91. }
  92. }
  93. auto packet = build_hello();
  94. write_packet(packet);
  95. Core::Socket::on_connected = [this] {
  96. Core::Socket::on_ready_to_read = [this] {
  97. if (!Core::Socket::is_open() || !Core::Socket::is_connected() || Core::Socket::eof()) {
  98. // an abrupt closure (the server is a jerk)
  99. dbg() << "Socket not open, assuming abrupt closure";
  100. m_context.connection_finished = true;
  101. }
  102. if (m_context.critical_error) {
  103. dbg() << "READ CRITICAL ERROR " << m_context.critical_error << " :(";
  104. if (on_tls_error)
  105. on_tls_error((AlertDescription)m_context.critical_error);
  106. return;
  107. }
  108. if (m_context.application_buffer.size() == 0 && m_context.connection_finished) {
  109. if (on_tls_finished)
  110. on_tls_finished();
  111. if (m_context.tls_buffer.size()) {
  112. dbg() << "connection closed without finishing data transfer, " << m_context.tls_buffer.size() << " bytes still in buffer";
  113. } else {
  114. m_context.connection_finished = false;
  115. }
  116. if (!m_context.application_buffer.size())
  117. m_context.connection_status = ConnectionStatus::Disconnected;
  118. return;
  119. }
  120. flush();
  121. consume(Core::Socket::read(4096)); // FIXME: how much is proper?
  122. if (is_established() && m_context.application_buffer.size())
  123. if (on_tls_ready_to_read)
  124. on_tls_ready_to_read(*this);
  125. };
  126. m_write_notifier = Core::Notifier::construct(fd(), Core::Notifier::Event::Write);
  127. m_write_notifier->on_ready_to_write = [this] {
  128. if (!Core::Socket::is_open() || !Core::Socket::is_connected() || Core::Socket::eof()) {
  129. // an abrupt closure (the server is a jerk)
  130. dbg() << "Socket not open, assuming abrupt closure";
  131. m_context.connection_finished = true;
  132. }
  133. if (m_context.critical_error) {
  134. dbg() << "WRITE CRITICAL ERROR " << m_context.critical_error << " :(";
  135. if (on_tls_error)
  136. on_tls_error((AlertDescription)m_context.critical_error);
  137. return;
  138. }
  139. if (m_context.connection_finished) {
  140. if (on_tls_finished)
  141. on_tls_finished();
  142. if (m_context.tls_buffer.size()) {
  143. dbg() << "connection closed without finishing data transfer, " << m_context.tls_buffer.size() << " bytes still in buffer";
  144. } else {
  145. m_context.connection_finished = false;
  146. dbg() << "FINISHED";
  147. }
  148. if (!m_context.application_buffer.size()) {
  149. m_context.connection_status = ConnectionStatus::Disconnected;
  150. return;
  151. }
  152. }
  153. flush();
  154. if (is_established() && !m_context.application_buffer.size()) // hey client, you still have stuff to read...
  155. if (on_tls_ready_to_write)
  156. on_tls_ready_to_write(*this);
  157. };
  158. if (on_tls_connected)
  159. on_tls_connected();
  160. };
  161. bool success = Core::Socket::common_connect(saddr, length);
  162. if (!success)
  163. return false;
  164. return true;
  165. }
  166. bool TLSv12::flush()
  167. {
  168. auto out_buffer = write_buffer().data();
  169. size_t out_buffer_index { 0 };
  170. size_t out_buffer_length = write_buffer().size();
  171. if (out_buffer_length == 0)
  172. return true;
  173. #ifdef TLS_DEBUG
  174. dbg() << "SENDING...";
  175. print_buffer(out_buffer, out_buffer_length);
  176. #endif
  177. if (Core::Socket::write(&out_buffer[out_buffer_index], out_buffer_length)) {
  178. write_buffer().clear();
  179. return true;
  180. }
  181. if (m_context.send_retries++ == 10) {
  182. // drop the records, we can't send
  183. dbg() << "Dropping " << write_buffer().size() << " bytes worth of TLS records as max retries has been reached";
  184. write_buffer().clear();
  185. m_context.send_retries = 0;
  186. }
  187. return false;
  188. }
  189. }