Handshake.cpp 5.4 KB

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