TestTLSHandshake.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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->on_tls_ready_to_write = [&](TLS::TLSv12& tls) {
  64. if (sent_request)
  65. return;
  66. sent_request = true;
  67. if (!tls.write("GET / HTTP/1.1\r\nHost: "_b)) {
  68. FAIL("write(0) failed");
  69. loop.quit(0);
  70. }
  71. auto* the_server = DEFAULT_SERVER;
  72. if (!tls.write(StringView(the_server).bytes())) {
  73. FAIL("write(1) failed");
  74. loop.quit(0);
  75. }
  76. if (!tls.write("\r\nConnection : close\r\n\r\n"_b)) {
  77. FAIL("write(2) failed");
  78. loop.quit(0);
  79. }
  80. };
  81. tls->on_tls_ready_to_read = [&](TLS::TLSv12& tls) {
  82. auto data = tls.read();
  83. if (!data.has_value()) {
  84. FAIL("No data received");
  85. loop.quit(1);
  86. } else {
  87. // print_buffer(data.value(), 16);
  88. if (!contents.try_append(data.value().data(), data.value().size())) {
  89. FAIL("Allocation failure");
  90. loop.quit(1);
  91. }
  92. }
  93. };
  94. tls->on_tls_finished = [&] {
  95. loop.quit(0);
  96. };
  97. tls->on_tls_error = [&](TLS::AlertDescription) {
  98. FAIL("Connection failure");
  99. loop.quit(1);
  100. };
  101. if (!tls->connect(DEFAULT_SERVER, port)) {
  102. FAIL("connect() failed");
  103. return;
  104. }
  105. loop.exec();
  106. }