/*
 * Copyright (C) 2020-2021 Aloysius Indrayanto
 *
 * This file is part of the eCxx library, see LICENSE file for the license details.
 */


#ifndef ECXX_PLATFORM_NODEMCU_WEB_SERVER_SSL_H
#define ECXX_PLATFORM_NODEMCU_WEB_SERVER_SSL_H


#include "WebServer.h"


//
// Forward declaration of secure web server class and its support classes
//
namespace BearSSL {

    class PrivateKey;
    class X509List;

    class WiFiServerSecure;

    using ESP8266WebServerSecure = esp8266webserver::ESP8266WebServerTemplate<WiFiServerSecure>;

} // namespace BearSSL


namespace eCxx {


//
// A simple and thin wrapper class for NodeMCU's ESP8266WebServerSecure class so that the public API
// will appear more consistent with other eCxx library APIs
//
class WebServerSSL : public BasicWebServer<BearSSL::ESP8266WebServerSecure> {
    public:
        WebServerSSL() = delete;

    public:
        enum class CertIssuerKeyType : uint8_t {
            RSA = 1,
            EC  = 2
        };

    public:
        WebServerSSL(uint16_t port);
        ~WebServerSSL();

        bool setRSACertificate(PGMCharPointer chain, PGMCharPointer privateKey);
        bool setEllipticCurveCertificate(PGMCharPointer chain, PGMCharPointer privateKey, CertIssuerKeyType certIssuerKeyType);

        bool setClientTrustAnchor(PGMCharPointer clientTA);

    private:
         BearSSL::X509List*   _chain;
         BearSSL::PrivateKey* _privateKey;

         BearSSL::X509List*   _clientTA;

         void clearCertificate();
         void clearClientTA();
};


} // namespace eCxx


#endif

