TestTLSHandshake.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Copyright (c) 2021, Peter Bocan <me@pbocan.net>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibCore/ConfigFile.h>
  7. #include <LibCore/EventLoop.h>
  8. #include <LibCore/File.h>
  9. #include <LibCrypto/ASN1/ASN1.h>
  10. #include <LibTLS/TLSv12.h>
  11. #include <LibTest/TestCase.h>
  12. static const char* ca_certs_file = "./ca_certs.ini";
  13. static int port = 443;
  14. constexpr const char* DEFAULT_SERVER { "www.google.com" };
  15. static ByteBuffer operator""_b(const char* string, size_t length)
  16. {
  17. return ByteBuffer::copy(string, length).release_value();
  18. }
  19. Vector<Certificate> load_certificates();
  20. String locate_ca_certs_file();
  21. String locate_ca_certs_file()
  22. {
  23. if (Core::File::exists(ca_certs_file)) {
  24. return ca_certs_file;
  25. }
  26. auto on_target_path = String("/etc/ca_certs.ini");
  27. if (Core::File::exists(on_target_path)) {
  28. return on_target_path;
  29. }
  30. return "";
  31. }
  32. Vector<Certificate> load_certificates()
  33. {
  34. Vector<Certificate> certificates;
  35. auto ca_certs_filepath = locate_ca_certs_file();
  36. if (ca_certs_filepath == "") {
  37. warnln("Could not locate ca_certs.ini file.");
  38. return certificates;
  39. }
  40. auto config = Core::ConfigFile::open(ca_certs_filepath);
  41. auto now = Core::DateTime::now();
  42. auto last_year = Core::DateTime::create(now.year() - 1);
  43. auto next_year = Core::DateTime::create(now.year() + 1);
  44. for (auto& entity : config->groups()) {
  45. Certificate cert;
  46. cert.subject.subject = entity;
  47. cert.issuer.subject = config->read_entry(entity, "issuer_subject", entity);
  48. cert.subject.country = config->read_entry(entity, "country");
  49. cert.not_before = Crypto::ASN1::parse_generalized_time(config->read_entry(entity, "not_before", "")).value_or(last_year);
  50. cert.not_after = Crypto::ASN1::parse_generalized_time(config->read_entry(entity, "not_after", "")).value_or(next_year);
  51. certificates.append(move(cert));
  52. }
  53. return certificates;
  54. }
  55. static Vector<Certificate> s_root_ca_certificates = load_certificates();
  56. TEST_CASE(test_TLS_hello_handshake)
  57. {
  58. Core::EventLoop loop;
  59. RefPtr<TLS::TLSv12> tls = TLS::TLSv12::construct(nullptr);
  60. tls->set_root_certificates(s_root_ca_certificates);
  61. bool sent_request = false;
  62. ByteBuffer contents;
  63. tls->set_on_tls_ready_to_write([&](TLS::TLSv12& tls) {
  64. if (sent_request)
  65. return;
  66. sent_request = true;
  67. Core::deferred_invoke([&tls] { tls.set_on_tls_ready_to_write(nullptr); });
  68. if (!tls.write("GET / HTTP/1.1\r\nHost: "_b)) {
  69. FAIL("write(0) failed");
  70. loop.quit(0);
  71. }
  72. auto* the_server = DEFAULT_SERVER;
  73. if (!tls.write(StringView(the_server).bytes())) {
  74. FAIL("write(1) failed");
  75. loop.quit(0);
  76. }
  77. if (!tls.write("\r\nConnection : close\r\n\r\n"_b)) {
  78. FAIL("write(2) failed");
  79. loop.quit(0);
  80. }
  81. });
  82. tls->on_tls_ready_to_read = [&](TLS::TLSv12& tls) {
  83. auto data = tls.read();
  84. if (!data.has_value()) {
  85. FAIL("No data received");
  86. loop.quit(1);
  87. } else {
  88. // print_buffer(data.value(), 16);
  89. if (contents.try_append(data.value().data(), data.value().size()).is_error()) {
  90. FAIL("Allocation failure");
  91. loop.quit(1);
  92. }
  93. }
  94. };
  95. tls->on_tls_finished = [&] {
  96. loop.quit(0);
  97. };
  98. tls->on_tls_error = [&](TLS::AlertDescription) {
  99. FAIL("Connection failure");
  100. loop.quit(1);
  101. };
  102. if (!tls->connect(DEFAULT_SERVER, port)) {
  103. FAIL("connect() failed");
  104. return;
  105. }
  106. loop.exec();
  107. }