//    OpenVPN -- An application to securely tunnel IP networks
//               over a single port, with support for SSL/TLS-based
//               session authentication and key exchange,
//               packet encryption, packet authentication, and
//               packet compression.
//
//    Copyright (C) 2012- OpenVPN Inc.
//
//    SPDX-License-Identifier: MPL-2.0 OR AGPL-3.0-only WITH openvpn3-openssl-exception
//


#include "test_common.hpp"

#include <openvpn/ssl/sslchoose.hpp>


#include <openvpn/ssl/sslapi.hpp>

using namespace openvpn;

const std::string pvt_key_txt = R"(-----BEGIN PRIVATE KEY-----
MIIJQQIBADANBgkqhkiG9w0BAQEFAASCCSswggknAgEAAoICAQDLPzbmnKK1j9MG
UNnL2HoSAVSbVNFTWpWWloUnYXJOEKYXcRheMo7PRSGJkkbs8oY6yVNDklrt6PFQ
VZT52OjwiBTaUXQPhT0CtEpF10HaKhe+1hk1/P3EQjkppiYohNHkNWzUOADmdIJ5
T2geyLt6h94KOfK6tYe91sfN5rl39ZwktvOq8hwSKSFHqApCP9gnR08YdxXY5Xb2
1mmw07iWxBnQxGtl5ovhssWe3IuVUHeAIGEJxNXiUSm7SVB6oDSKHfxiHI9UUXGX
lDJUTuSV4ellYlqcs/H/6+WyM5t7NMPoRLTIw7+aFvCXBPLkII/uvFWb+irsO1IC
XScIFEARN1W5vq2jG1C9h5SRfMhpALgrUftiUCYsnQFoac1XorH7oIYkQGLv+USv
bOKr+T+TEqN9aMdC4oF/fPbps3IXBqL19pmhp18pa3XXydSvrR+uCyZE0Ol8w3Jf
5+m2m8ZmDTC5ir/Py1lWF1n+quYMx/f0hZduJUq6eO/xNZqEbGzq7/YNm4/PUSCk
dUaO4rO6VoGWXVhmh7qP/u3/N2fe2yv7qqGEBXoBoDjwlVBiZwC8ZDxfCkZovsCk
AAAtUWM/vmNeFH1Gh0BPz8UYMPI9CWqBiuy0MmtiqErjrQZWQJzbJ2ac/Nd7Jq0M
N13R1U9PIyiGjZLlVOsRWfvgGsis/wIDAQABAoICAAL1AP368m0U0hup6nlKRYBk
j5AQ/FirnTiLKRXJF6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d2oOdRFGeg5O9aVJcxl
da0X3BFsTH3RvZEyQcx8hiUsBqP9vh1SmC7TJvdCjuAZVnaT/A==
-----END CERTIFICATE-----)";

const std::string dhparam_txt = R"(-----BEGIN DH PARAMETERS-----
MIIBDAKCAQEA9WXZvihl5pltdS4C+Dc+Ki3KOuH0aBfSzmth/B3O0+oTN3CZXAak
WyK6dCCtXeBZ3ih3eNVO69BgoNVdbxuPIUWxo3EWFt6LteASgkjCEyTmrNd/aKAE
pS06RgsZcWs/Ps9iwcVFyE6kMi9Cbf3D2wwJT50kJmkEov+4cOH3NQVpS231I58x
rHMEMtNDFcJYCAEIG3cKqfX9unAgZUsDoLtyvfgvHV29koZxnmMt0+5f0qnezcnP
I/+4kAlXuAKdhsXohHeBhC2ijg/kTOMDxEbEVv+SkCIUyM+dB8UtlPKOH9HEL5Xi
+BpDSqO6Bha5+NAVUU7OdDsnzRwSWaD6lwIBAgICAOE=
-----END DH PARAMETERS-----)";

TEST(sslctx_ut, create_config)
{
    SSLLib::SSLAPI::Config::Ptr config = new SSLLib::SSLAPI::Config;
    // Do not log extra data during unit test
    config->set_debug_level(0);
    EXPECT_TRUE(config);
}

TEST(sslctx_ut, config_new_factory_server)
{
    SSLLib::SSLAPI::Config::Ptr config = new SSLLib::SSLAPI::Config;
    EXPECT_TRUE(config);

    StrongRandomAPI::Ptr rng(new SSLLib::RandomAPI());
    config->set_rng(rng);

    config->set_mode(Mode(Mode::SERVER));
    config->load_cert(cert_txt);
    config->load_private_key(pvt_key_txt);
    config->load_ca(cert_txt, false);

    // Do not log extra data during unit test
    config->set_debug_level(0);

    auto factory_server = config->new_factory();
    EXPECT_TRUE(factory_server);

    auto server = factory_server->ssl();
    EXPECT_TRUE(server);
}

TEST(sslctx_ut, config_new_factory_client)
{
    SSLLib::SSLAPI::Config::Ptr config = new SSLLib::SSLAPI::Config;
    EXPECT_TRUE(config);

    StrongRandomAPI::Ptr rng(new SSLLib::RandomAPI());
    config->set_rng(rng);

    config->set_mode(Mode(Mode::CLIENT));
    config->load_cert(cert_txt);
    config->load_private_key(pvt_key_txt);
    config->load_ca(cert_txt, false);

    // Do not log extra data during unit test
    config->set_debug_level(0);

    auto factory_client = config->new_factory();
    EXPECT_TRUE(factory_client);

    auto client = factory_client->ssl();
    EXPECT_TRUE(client);
}

#define ITER 1000
#define ITER_OUT_LIMIT 1000

// From ovpn3/common/test/osx/ssl.cpp
template <typename T>
inline void do_write(T &obj, const bool server, char *msg, const size_t len, long &count, const int iter)
{
    const ssize_t status = obj.write_cleartext_unbuffered(msg, len);
    if (status > 0)
        count += status;
#if ITER <= ITER_OUT_LIMIT
    std::cout << (server ? "SERVER" : "CLIENT") << " WRITE #" << iter << " status=" << status << std::endl;
#endif
}

template <typename T>
inline void do_read(T &obj, const bool server, char *msg, const size_t len, long &count, const int iter)
{
    const ssize_t status = obj.read_cleartext(msg, len);
    if (status > 0)
        count += status;

#if ITER <= ITER_OUT_LIMIT
    const ssize_t trunc = 64;
    std::cout << (server ? "SERVER" : "CLIENT") << " READ #" << iter << " status=" << status << std::endl;
    if (status >= trunc)
    {
        msg[trunc] = 0;
        std::cout << "GOT IT: " << msg << std::endl;
    }
#endif
}

static inline bool xfer_oneway(SSLAPI &sender, SSLAPI &recv, std::string out)
{
    if (sender.read_ciphertext_ready())
    {
        BufferPtr buf = sender.read_ciphertext();
        recv.write_ciphertext(buf);
        std::cout << out << buf->size() << " bytes" << std::endl;
        return true;
    }

    if (sender.read_cleartext_ready())
    {
        /* this can also indicate an error */
        uint8_t cleartext[1024];

        std::cout << out << " read ready?" << std::endl;
        auto ctsize = sender.read_cleartext(cleartext, sizeof(cleartext));
        std::cout << ctsize << std::endl;

        EXPECT_FALSE(ctsize > 0);
        /* TODO: capture output for tests with data */
        return true;
    }
    return false;
}

static inline void xfer(SSLAPI &cli, SSLAPI &serv)
{
    while (xfer_oneway(cli, serv, "CLIENT -> SERVER ") || xfer_oneway(serv, cli, "SERVER -> CLIENT"))
    {
        /* while we have done work, keep working */
    }
}

static inline auto MakeClient(Frame::Ptr frame,
                              const std::string &pvt_key,
                              const std::string &cert,
                              const std::string &ca = "")
{
    SSLLib::SSLAPI::Config::Ptr config = new SSLLib::SSLAPI::Config;
    EXPECT_TRUE(config);

    StrongRandomAPI::Ptr rng(new SSLLib::RandomAPI());
    config->set_rng(rng);

    config->set_mode(Mode(Mode::CLIENT));
    config->load_cert(cert_txt);
    config->load_private_key(pvt_key_txt);
    config->set_frame(frame);
    if (ca.empty())
    {
        std::cout << "No CA for client\n";
        config->set_flags(SSLConfigAPI::LF_ALLOW_CLIENT_CERT_NOT_REQUIRED);
    }
    else
        config->load_ca(ca, false);

    auto factory_client = config->new_factory();
    EXPECT_TRUE(factory_client);

    auto client = factory_client->ssl();
    EXPECT_TRUE(client);

    return std::make_tuple(config, factory_client, client);
}

static inline auto MakeServer(Frame::Ptr frame, const std::string &pvt_key, const std::string &cert)
{
    SSLLib::SSLAPI::Config::Ptr config = new SSLLib::SSLAPI::Config;
    config->enable_legacy_algorithms(false);
    EXPECT_TRUE(config);

    StrongRandomAPI::Ptr rng(new SSLLib::RandomAPI());
    config->set_rng(rng);

    config->set_mode(Mode(Mode::SERVER));
    config->load_cert(cert);
    config->load_private_key(pvt_key);
    config->load_ca(cert, false);
    config->set_frame(frame);
    config->load_dh(dhparam_txt);

    auto factory_server = config->new_factory();
    EXPECT_TRUE(factory_server);

    auto server = factory_server->ssl();
    EXPECT_TRUE(server);

    return std::make_tuple(config, factory_server, server);
}

TEST(sslctx_ut, handshake)
{
    Frame::Ptr frame(new Frame(Frame::Context(128, 4096, 4096 - 128, 0, 16, 0)));
    auto [serverconfig, serverfactory, server] = MakeServer(frame, pvt_key_txt, cert_txt);
    auto [clientconfig, clientfactory, client] = MakeClient(std::move(frame), pvt_key_txt, cert_txt);

    // Some internal state that's inside these objects seems required for ssl to work
    // serverconfig.reset();
    // serverfactory.reset();
    // clientconfig.reset();
    // clientfactory.reset();

    client->start_handshake();

    try
    {
        for (auto i = 0u; i < ITER; ++i)
        {
            xfer(*client, *server);
        }
    }
    catch (...)
    {
        FAIL();
    }
}

TEST(sslctx_ut, ca_handshake)
{
    Frame::Ptr frame(new Frame(Frame::Context(128, 4096, 4096 - 128, 0, 16, 0)));
    auto [serverconfig, serverfactory, server] = MakeServer(frame, pvt_key_txt, cert_txt);
    auto [clientconfig, clientfactory, client] = MakeClient(std::move(frame), pvt_key_txt, cert_txt, cert_txt);

    // Some internal state that's inside these objects seems required for ssl to work
    // serverconfig.reset();
    // serverfactory.reset();
    // clientconfig.reset();
    // clientfactory.reset();

    client->start_handshake();

    try
    {
        for (auto i = 0u; i < ITER; ++i)
        {
            xfer(*client, *server);
        }
    }
    catch (...)
    {
        FAIL();
    }
}

TEST(sslctx_ut, handshake_fail)
{
    Frame::Ptr frame(new Frame(Frame::Context(128, 4096, 4096 - 128, 0, 16, 0)));
    auto [serverconfig, serverfactory, server] = MakeServer(frame, fail_pvt_key_txt, fail_cert_txt);
    auto [clientconfig, clientfactory, client] = MakeClient(std::move(frame), pvt_key_txt, cert_txt);

    client->start_handshake();

    try
    {
        for (auto i = 0u; i < ITER; ++i)
        {
            xfer(*client, *server);
        }
    }
    catch (...)
    {
        // Oddly enough, these return true even though a full handshake did not happen
        // EXPECT_FALSE(client->did_full_handshake());
        // EXPECT_FALSE(server->did_full_handshake());
        return;
    }
    FAIL();
}

TEST(sslctx_ut, clienthello)
{
    /* Checks that a server context correctly responds to a TLS 1.3 client hello */
    uint8_t clienthello[] = {
        0x16, 0x03, 0x01, 0x01, 0x10, 0x01, 0x00, 0x01, 0x0c, 0x03, 0x03, 0x0a, 0xef, 0xce, 0xc6, 0xe3, 0xcc, 0xb7, 0x7c, 0x38, 0xb2, 0x0e, 0xac, 0x12, 0xb5, 0xfa, 0x6d, 0x98, 0xc7, 0x76, 0xd1, 0x96, 0x22, 0x97, 0xf5, 0x57, 0xc5, 0x12, 0xf7, 0x01, 0xfc, 0xbe, 0x7d, 0x20, 0x64, 0xbd, 0xb5, 0x68, 0x06, 0x55, 0xb1, 0xf3, 0xf3, 0xc7, 0xb4, 0x1b, 0xbe, 0x83, 0x89, 0xab, 0xa5, 0xa0, 0x55, 0xa5, 0x6d, 0xca, 0xb1, 0x21, 0x5f, 0x2c, 0x71, 0xf5, 0x13, 0x4d, 0xeb, 0x68, 0x00, 0x32, 0x13, 0x02, 0x13, 0x03, 0x13, 0x01, 0xc0, 0x2c, 0xc0, 0x30, 0x00, 0x9f, 0xcc, 0xa9, 0xcc, 0xa8, 0xcc, 0xaa, 0xc0, 0x2b, 0xc0, 0x2f, 0x00, 0x9e, 0xc0, 0x24, 0xc0, 0x28, 0x00, 0x6b, 0xc0, 0x23, 0xc0, 0x27, 0x00, 0x67, 0xc0, 0x0a, 0xc0, 0x14, 0x00, 0x39, 0xc0, 0x09, 0xc0, 0x13, 0x00, 0x33, 0x00, 0xff, 0x01, 0x00, 0x00, 0x91, 0x00, 0x0b, 0x00, 0x04, 0x03, 0x00, 0x01, 0x02, 0x00, 0x0a, 0x00, 0x16, 0x00, 0x14, 0x00, 0x1d, 0x00, 0x17, 0x00, 0x1e, 0x00, 0x19, 0x00, 0x18, 0x01, 0x00, 0x01, 0x01, 0x01, 0x02, 0x01, 0x03, 0x01, 0x04, 0x00, 0x16, 0x00, 0x00, 0x00, 0x17, 0x00, 0x00, 0x00, 0x0d, 0x00, 0x2a, 0x00, 0x28, 0x04, 0x03, 0x05, 0x03, 0x06, 0x03, 0x08, 0x07, 0x08, 0x08, 0x08, 0x09, 0x08, 0x0a, 0x08, 0x0b, 0x08, 0x04, 0x08, 0x05, 0x08, 0x06, 0x04, 0x01, 0x05, 0x01, 0x06, 0x01, 0x03, 0x03, 0x03, 0x01, 0x03, 0x02, 0x04, 0x02, 0x05, 0x02, 0x06, 0x02, 0x00, 0x2b, 0x00, 0x05, 0x04, 0x03, 0x04, 0x03, 0x03, 0x00, 0x2d, 0x00, 0x02, 0x01, 0x01, 0x00, 0x33, 0x00, 0x26, 0x00, 0x24, 0x00, 0x1d, 0x00, 0x20, 0x6e, 0x11, 0xc2, 0x45, 0x19, 0x7c, 0x68, 0x45, 0xf8, 0x4f, 0xa4, 0x26, 0xa2, 0x39, 0xce, 0xf1, 0x38, 0x8d, 0x83, 0x35, 0xee, 0x45, 0x74, 0x08, 0x83, 0x08, 0x44, 0x15, 0xf7, 0xd4, 0x38, 0x13};

    Frame::Ptr frame(new Frame(Frame::Context(128, 4096, 4096 - 128, 0, 16, 0)));
    auto [serverconfig, serverfactory, server] = MakeServer(std::move(frame), pvt_key_txt, cert_txt);

    server->start_handshake();

    server->write_ciphertext_unbuffered(clienthello, sizeof(clienthello));

    server->read_cleartext_ready();

    uint8_t cleartext[1024];

    auto ctsize = server->read_cleartext(cleartext, sizeof(cleartext));
    std::cout << ctsize << std::endl;

    EXPECT_TRUE(server->read_ciphertext_ready());
    auto buf = server->read_ciphertext();
    ASSERT_TRUE(buf->length() > 1);
}