Handshake.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 <AK/Random.h>
  27. #include <LibCore/Timer.h>
  28. #include <LibCrypto/ASN1/DER.h>
  29. #include <LibCrypto/PK/Code/EMSA_PSS.h>
  30. #include <LibTLS/TLSv12.h>
  31. namespace TLS {
  32. ByteBuffer TLSv12::build_hello()
  33. {
  34. AK::fill_with_random(&m_context.local_random, 32);
  35. auto packet_version = (u16)m_context.version;
  36. auto version = (u16)m_context.version;
  37. PacketBuilder builder { MessageType::Handshake, packet_version };
  38. builder.append((u8)ClientHello);
  39. // hello length (for later)
  40. u8 dummy[3] = {};
  41. builder.append(dummy, 3);
  42. auto start_length = builder.length();
  43. builder.append(version);
  44. builder.append(m_context.local_random, sizeof(m_context.local_random));
  45. builder.append(m_context.session_id_size);
  46. if (m_context.session_id_size)
  47. builder.append(m_context.session_id, m_context.session_id_size);
  48. size_t extension_length = 0;
  49. size_t alpn_length = 0;
  50. size_t alpn_negotiated_length = 0;
  51. // ALPN
  52. if (!m_context.negotiated_alpn.is_null()) {
  53. alpn_negotiated_length = m_context.negotiated_alpn.length();
  54. alpn_length = alpn_negotiated_length + 1;
  55. extension_length += alpn_length + 6;
  56. } else if (m_context.alpn.size()) {
  57. for (auto& alpn : m_context.alpn) {
  58. size_t length = alpn.length();
  59. alpn_length += length + 1;
  60. }
  61. if (alpn_length)
  62. extension_length += alpn_length + 6;
  63. }
  64. // Ciphers
  65. builder.append((u16)(4 * sizeof(u16)));
  66. builder.append((u16)CipherSuite::RSA_WITH_AES_128_CBC_SHA256);
  67. builder.append((u16)CipherSuite::RSA_WITH_AES_256_CBC_SHA256);
  68. builder.append((u16)CipherSuite::RSA_WITH_AES_128_CBC_SHA);
  69. builder.append((u16)CipherSuite::RSA_WITH_AES_256_CBC_SHA);
  70. // we don't like compression
  71. builder.append((u8)1);
  72. builder.append((u8)0);
  73. // set SNI if we have one
  74. auto sni_length = 0;
  75. if (!m_context.SNI.is_null())
  76. sni_length = m_context.SNI.length();
  77. if (sni_length)
  78. extension_length += sni_length + 9;
  79. builder.append((u16)extension_length);
  80. if (sni_length) {
  81. // SNI extension
  82. builder.append((u16)HandshakeExtension::ServerName);
  83. // extension length
  84. builder.append((u16)(sni_length + 5));
  85. // SNI length
  86. builder.append((u16)(sni_length + 3));
  87. // SNI type
  88. builder.append((u8)0);
  89. // SNI host length + value
  90. builder.append((u16)sni_length);
  91. builder.append((const u8*)m_context.SNI.characters(), sni_length);
  92. }
  93. if (alpn_length) {
  94. // TODO
  95. ASSERT_NOT_REACHED();
  96. }
  97. // set the "length" field of the packet
  98. size_t remaining = builder.length() - start_length;
  99. size_t payload_position = 6;
  100. builder.set(payload_position, remaining / 0x10000);
  101. remaining %= 0x10000;
  102. builder.set(payload_position + 1, remaining / 0x100);
  103. remaining %= 0x100;
  104. builder.set(payload_position + 2, remaining);
  105. auto packet = builder.build();
  106. update_packet(packet);
  107. return packet;
  108. }
  109. ByteBuffer TLSv12::build_alert(bool critical, u8 code)
  110. {
  111. PacketBuilder builder(MessageType::Alert, (u16)m_context.version);
  112. builder.append((u8)(critical ? AlertLevel::Critical : AlertLevel::Warning));
  113. builder.append(code);
  114. if (critical)
  115. m_context.critical_error = code;
  116. auto packet = builder.build();
  117. update_packet(packet);
  118. return packet;
  119. }
  120. ByteBuffer TLSv12::build_finished()
  121. {
  122. PacketBuilder builder { MessageType::Handshake, m_context.version, 12 + 64 };
  123. builder.append((u8)HandshakeType::Finished);
  124. u32 out_size = 12;
  125. builder.append_u24(out_size);
  126. u8 out[out_size];
  127. auto outbuffer = ByteBuffer::wrap(out, out_size);
  128. auto dummy = ByteBuffer::create_zeroed(0);
  129. auto digest = m_context.handshake_hash.digest();
  130. auto hashbuf = ByteBuffer::wrap(digest.immutable_data(), m_context.handshake_hash.digest_size());
  131. pseudorandom_function(outbuffer, m_context.master_key, (const u8*)"client finished", 15, hashbuf, dummy);
  132. builder.append(outbuffer);
  133. auto packet = builder.build();
  134. update_packet(packet);
  135. return packet;
  136. }
  137. }