TestTLSHandshake.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * Copyright (c) 2021, Peter Bocan <me@pbocan.net>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Base64.h>
  7. #include <LibCore/ConfigFile.h>
  8. #include <LibCore/DeprecatedFile.h>
  9. #include <LibCore/EventLoop.h>
  10. #include <LibCrypto/ASN1/ASN1.h>
  11. #include <LibTLS/TLSv12.h>
  12. #include <LibTest/TestCase.h>
  13. static StringView ca_certs_file = "./ca_certs.ini"sv;
  14. static int port = 443;
  15. constexpr auto DEFAULT_SERVER = "www.google.com"sv;
  16. static ByteBuffer operator""_b(char const* string, size_t length)
  17. {
  18. return ByteBuffer::copy(string, length).release_value();
  19. }
  20. Vector<Certificate> load_certificates();
  21. DeprecatedString locate_ca_certs_file();
  22. DeprecatedString locate_ca_certs_file()
  23. {
  24. if (Core::DeprecatedFile::exists(ca_certs_file)) {
  25. return ca_certs_file;
  26. }
  27. auto on_target_path = DeprecatedString("/etc/ca_certs.ini");
  28. if (Core::DeprecatedFile::exists(on_target_path)) {
  29. return on_target_path;
  30. }
  31. return "";
  32. }
  33. Vector<Certificate> load_certificates()
  34. {
  35. Vector<Certificate> certificates;
  36. auto ca_certs_filepath = locate_ca_certs_file();
  37. if (ca_certs_filepath == "") {
  38. warnln("Could not locate ca_certs.ini file.");
  39. return certificates;
  40. }
  41. auto config = Core::ConfigFile::open(ca_certs_filepath).release_value_but_fixme_should_propagate_errors();
  42. for (auto& entity : config->groups()) {
  43. for (auto& subject : config->keys(entity)) {
  44. auto certificate_base64 = config->read_entry(entity, subject);
  45. auto certificate_data_result = decode_base64(certificate_base64);
  46. if (certificate_data_result.is_error()) {
  47. dbgln("Skipping CA Certificate {} {}: out of memory", entity, subject);
  48. continue;
  49. }
  50. auto certificate_data = certificate_data_result.release_value();
  51. auto certificate_result = Certificate::parse_asn1(certificate_data.bytes());
  52. // If the certificate does not parse it is likely using elliptic curve keys/signatures, which are not
  53. // supported right now. Currently, ca_certs.ini should only contain certificates with RSA keys/signatures.
  54. if (!certificate_result.has_value()) {
  55. dbgln("Skipping CA Certificate {} {}: unable to parse", entity, subject);
  56. continue;
  57. }
  58. auto certificate = certificate_result.release_value();
  59. certificates.append(move(certificate));
  60. }
  61. }
  62. return certificates;
  63. }
  64. static Vector<Certificate> s_root_ca_certificates = load_certificates();
  65. TEST_CASE(test_TLS_hello_handshake)
  66. {
  67. Core::EventLoop loop;
  68. TLS::Options options;
  69. options.set_root_certificates(s_root_ca_certificates);
  70. options.set_alert_handler([&](TLS::AlertDescription) {
  71. FAIL("Connection failure");
  72. loop.quit(1);
  73. });
  74. options.set_finish_callback([&] {
  75. loop.quit(0);
  76. });
  77. auto tls = MUST(TLS::TLSv12::connect(DEFAULT_SERVER, port, move(options)));
  78. ByteBuffer contents;
  79. tls->on_ready_to_read = [&] {
  80. auto read_bytes = MUST(tls->read_some(contents.must_get_bytes_for_writing(4 * KiB)));
  81. if (read_bytes.is_empty()) {
  82. FAIL("No data received");
  83. loop.quit(1);
  84. }
  85. loop.quit(0);
  86. };
  87. if (tls->write_until_depleted("GET / HTTP/1.1\r\nHost: "_b).is_error()) {
  88. FAIL("write(0) failed");
  89. return;
  90. }
  91. auto the_server = DEFAULT_SERVER;
  92. if (tls->write_until_depleted(the_server.bytes()).is_error()) {
  93. FAIL("write(1) failed");
  94. return;
  95. }
  96. if (tls->write_until_depleted("\r\nConnection : close\r\n\r\n"_b).is_error()) {
  97. FAIL("write(2) failed");
  98. return;
  99. }
  100. loop.exec();
  101. }