Handshake.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. if (sni_length)
  56. extension_length += sni_length + 9;
  57. builder.append((u16)extension_length);
  58. if (sni_length) {
  59. // SNI extension
  60. builder.append((u16)HandshakeExtension::ServerName);
  61. // extension length
  62. builder.append((u16)(sni_length + 5));
  63. // SNI length
  64. builder.append((u16)(sni_length + 3));
  65. // SNI type
  66. builder.append((u8)0);
  67. // SNI host length + value
  68. builder.append((u16)sni_length);
  69. builder.append((const u8*)m_context.extensions.SNI.characters(), sni_length);
  70. }
  71. if (alpn_length) {
  72. // TODO
  73. VERIFY_NOT_REACHED();
  74. }
  75. // set the "length" field of the packet
  76. size_t remaining = builder.length() - start_length;
  77. size_t payload_position = 6;
  78. builder.set(payload_position, remaining / 0x10000);
  79. remaining %= 0x10000;
  80. builder.set(payload_position + 1, remaining / 0x100);
  81. remaining %= 0x100;
  82. builder.set(payload_position + 2, remaining);
  83. auto packet = builder.build();
  84. update_packet(packet);
  85. return packet;
  86. }
  87. ByteBuffer TLSv12::build_alert(bool critical, u8 code)
  88. {
  89. PacketBuilder builder(MessageType::Alert, (u16)m_context.options.version);
  90. builder.append((u8)(critical ? AlertLevel::Critical : AlertLevel::Warning));
  91. builder.append(code);
  92. if (critical)
  93. m_context.critical_error = code;
  94. auto packet = builder.build();
  95. update_packet(packet);
  96. return packet;
  97. }
  98. ByteBuffer TLSv12::build_finished()
  99. {
  100. PacketBuilder builder { MessageType::Handshake, m_context.options.version, 12 + 64 };
  101. builder.append((u8)HandshakeType::Finished);
  102. u32 out_size = 12;
  103. builder.append_u24(out_size);
  104. u8 out[out_size];
  105. auto outbuffer = Bytes { out, out_size };
  106. auto dummy = ByteBuffer::create_zeroed(0);
  107. auto digest = m_context.handshake_hash.digest();
  108. auto hashbuf = ReadonlyBytes { digest.immutable_data(), m_context.handshake_hash.digest_size() };
  109. pseudorandom_function(outbuffer, m_context.master_key, (const u8*)"client finished", 15, hashbuf, dummy);
  110. builder.append(outbuffer);
  111. auto packet = builder.build();
  112. update_packet(packet);
  113. return packet;
  114. }
  115. void TLSv12::alert(AlertLevel level, AlertDescription code)
  116. {
  117. auto the_alert = build_alert(level == AlertLevel::Critical, (u8)code);
  118. write_packet(the_alert);
  119. flush();
  120. }
  121. }