Handshake.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * Copyright (c) 2020, Ali Mohammad Pur <mpfard@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Random.h>
  7. #include <LibCrypto/ASN1/DER.h>
  8. #include <LibCrypto/PK/Code/EMSA_PSS.h>
  9. #include <LibTLS/TLSv12.h>
  10. namespace TLS {
  11. ByteBuffer TLSv12::build_hello()
  12. {
  13. fill_with_random(&m_context.local_random, 32);
  14. auto packet_version = (u16)m_context.options.version;
  15. auto version = (u16)m_context.options.version;
  16. PacketBuilder builder { MessageType::Handshake, packet_version };
  17. builder.append((u8)ClientHello);
  18. // hello length (for later)
  19. u8 dummy[3] = {};
  20. builder.append(dummy, 3);
  21. auto start_length = builder.length();
  22. builder.append(version);
  23. builder.append(m_context.local_random, sizeof(m_context.local_random));
  24. builder.append(m_context.session_id_size);
  25. if (m_context.session_id_size)
  26. builder.append(m_context.session_id, m_context.session_id_size);
  27. size_t extension_length = 0;
  28. size_t alpn_length = 0;
  29. size_t alpn_negotiated_length = 0;
  30. // ALPN
  31. if (!m_context.negotiated_alpn.is_null()) {
  32. alpn_negotiated_length = m_context.negotiated_alpn.length();
  33. alpn_length = alpn_negotiated_length + 1;
  34. extension_length += alpn_length + 6;
  35. } else if (m_context.alpn.size()) {
  36. for (auto& alpn : m_context.alpn) {
  37. size_t length = alpn.length();
  38. alpn_length += length + 1;
  39. }
  40. if (alpn_length)
  41. extension_length += alpn_length + 6;
  42. }
  43. // Ciphers
  44. builder.append((u16)(m_context.options.usable_cipher_suites.size() * sizeof(u16)));
  45. for (auto suite : m_context.options.usable_cipher_suites)
  46. builder.append((u16)suite);
  47. // we don't like compression
  48. VERIFY(!m_context.options.use_compression);
  49. builder.append((u8)1);
  50. builder.append((u8)m_context.options.use_compression);
  51. // set SNI if we have one, and the user hasn't explicitly asked us to omit it.
  52. auto sni_length = 0;
  53. if (!m_context.extensions.SNI.is_null() && m_context.options.use_sni)
  54. sni_length = m_context.extensions.SNI.length();
  55. // signature_algorithms: 2b extension ID, 2b extension length, 2b vector length, 2xN signatures and hashes
  56. extension_length += 2 + 2 + 2 + 2 * m_context.options.supported_signature_algorithms.size();
  57. if (sni_length)
  58. extension_length += sni_length + 9;
  59. builder.append((u16)extension_length);
  60. if (sni_length) {
  61. // SNI extension
  62. builder.append((u16)HandshakeExtension::ServerName);
  63. // extension length
  64. builder.append((u16)(sni_length + 5));
  65. // SNI length
  66. builder.append((u16)(sni_length + 3));
  67. // SNI type
  68. builder.append((u8)0);
  69. // SNI host length + value
  70. builder.append((u16)sni_length);
  71. builder.append((const u8*)m_context.extensions.SNI.characters(), sni_length);
  72. }
  73. // signature_algorithms extension
  74. builder.append((u16)HandshakeExtension::SignatureAlgorithms);
  75. // Extension length
  76. builder.append((u16)(2 + 2 * m_context.options.supported_signature_algorithms.size()));
  77. // Vector count
  78. builder.append((u16)(m_context.options.supported_signature_algorithms.size() * 2));
  79. // Entries
  80. for (auto& entry : m_context.options.supported_signature_algorithms) {
  81. builder.append((u8)entry.hash);
  82. builder.append((u8)entry.signature);
  83. }
  84. if (alpn_length) {
  85. // TODO
  86. VERIFY_NOT_REACHED();
  87. }
  88. // set the "length" field of the packet
  89. size_t remaining = builder.length() - start_length;
  90. size_t payload_position = 6;
  91. builder.set(payload_position, remaining / 0x10000);
  92. remaining %= 0x10000;
  93. builder.set(payload_position + 1, remaining / 0x100);
  94. remaining %= 0x100;
  95. builder.set(payload_position + 2, remaining);
  96. auto packet = builder.build();
  97. update_packet(packet);
  98. return packet;
  99. }
  100. ByteBuffer TLSv12::build_alert(bool critical, u8 code)
  101. {
  102. PacketBuilder builder(MessageType::Alert, (u16)m_context.options.version);
  103. builder.append((u8)(critical ? AlertLevel::Critical : AlertLevel::Warning));
  104. builder.append(code);
  105. if (critical)
  106. m_context.critical_error = code;
  107. auto packet = builder.build();
  108. update_packet(packet);
  109. return packet;
  110. }
  111. ByteBuffer TLSv12::build_finished()
  112. {
  113. PacketBuilder builder { MessageType::Handshake, m_context.options.version, 12 + 64 };
  114. builder.append((u8)HandshakeType::Finished);
  115. u32 out_size = 12;
  116. builder.append_u24(out_size);
  117. u8 out[out_size];
  118. auto outbuffer = Bytes { out, out_size };
  119. auto dummy = ByteBuffer::create_zeroed(0);
  120. auto digest = m_context.handshake_hash.digest();
  121. auto hashbuf = ReadonlyBytes { digest.immutable_data(), m_context.handshake_hash.digest_size() };
  122. pseudorandom_function(outbuffer, m_context.master_key, (const u8*)"client finished", 15, hashbuf, dummy);
  123. builder.append(outbuffer);
  124. auto packet = builder.build();
  125. update_packet(packet);
  126. return packet;
  127. }
  128. void TLSv12::alert(AlertLevel level, AlertDescription code)
  129. {
  130. auto the_alert = build_alert(level == AlertLevel::Critical, (u8)code);
  131. write_packet(the_alert);
  132. flush();
  133. }
  134. }