#if !defined(__alffx509util_h) // Sentry, use file only if it's not already included.
#define __alffx509util_h
/*  Project Biblio-Tec

	 SUBSYSTEM:    TECSecurity.dll Library
	 FILE:         alffx509util.h
	 AUTHOR:       Alcindo Ferreira Filho

	 OVERVIEW:
	 Source file for definition of ALFFX509Util
	 (Container Class for X509 Certificates utility methods)
    Based on OpenSSL (www.openssl.org).

    COPYRIGHT:    © 2003,2005
                  Alcindo Ferreira Filho
                  <alcindo@inttec.com.br>

    LICENSE:      Biblio-Tec license (BiblTec.txt)
*/

//General Settings for TEC Libraries
#include "teclibs.h"

// ALFFLib Includes
#include "alffmsgs.h"

// ALFFSSLLib Includes
#include "alffusesecurity.h"
#include "alffx509object.h"

// OpenSSL includes
#include <openssl/x509.h>

class _TECSECCLASS ALFFX509Util : public ALFFX509Object
{
 public:

  ALFFX509Util(TWindow* parent, ALFFUseSecurity* pSec);
  ~ALFFX509Util();
  char far* GetClassName() {return "ALFFX509Util";}

  /* Private Keys Handling Methods */
  ULONG      LoadPrivateKey(X509* certToLoadKey, EVP_PKEY** pKey, bool removeKey = false);

  /* Certificates Stack Handling Methods */
  ULONG      LoadCertificatesStack(int certType, STACK_OF(X509)** x509Certs,
                                   STACK_OF(X509_NAME)* client_CA = NULL,
                                   STACK_OF(X509)* caCerts = NULL);
  ULONG      SaveCertificatesStack(int certType, STACK_OF(X509)** x509Certs);
  int        GetCertificateChain(X509 *cert, STACK_OF(X509) **chain, STACK_OF(X509) *caCertificates);


  /* Certificates Revoked List (CRLs) Handling Methods */
  ULONG      LoadCRLStack (const char* crlFileName,  X509_NAME* crlIssuer, STACK_OF(X509_CRL)** x509Crls);
  ULONG      SaveCRLStack (const char* crlFileName, STACK_OF(X509_CRL)** x509Crls);
  bool       LoadCertificateCRLs(X509_STORE_CTX* storeCTX);
  bool       DownloadNewCRL(X509* baseCert);

  /* CA Certificates Stack Handling Methods */
  ULONG		 LoadCACertificatesStack(STACK_OF(X509)** caCerts);

  /* Error Handling Methods */
  void       DumpErrors(unsigned long firstError, int errMsg, int errTitle);

 protected:
  bool         	 GetCRLFileName(string crlFileUri, string& crlFileName);

  TWindow* 			 myParent;
  ALFFUseSecurity* pSecurity;
  ALFFMsgs* 		 pMsgs;
};
#endif
