#ifndef FIX_UTILITY_SSL_H
#define FIX_UTILITY_SSL_H
#if (HAVE_SSL > 0)
#include "Log.h"
#include "SessionSettings.h"
#include "Utility.h"
#ifndef _MSC_VER
#include <dirent.h>
#else
#ifndef HAVE_ACE_DIRENT
#include "dirent_windows.h"
#else
#include "ace/OS_NS_dirent.h"
#endif
#endif
#include "openssl/err.h"
#include "openssl/ssl.h"
namespace FIX {
#if defined(_MSC_VER)
static const char *WSAErrString(int code)
{
#define expand(x) \
{ x, #x }
static struct {
int code;
const char *s;
} tab[] = {expand(WSAEINTR), expand(WSAEBADF), expand(WSAEACCES), expand(WSAEFAULT),
expand(WSAEINVAL), expand(WSAEMFILE), expand(WSAEWOULDBLOCK), expand(WSAEINPROGRESS),
expand(WSAEALREADY), expand(WSAENOTSOCK), expand(WSAEDESTADDRREQ), expand(WSAEMSGSIZE),
expand(WSAEPROTOTYPE), expand(WSAENOPROTOOPT), expand(WSAEPROTONOSUPPORT), expand(WSAESOCKTNOSUPPORT),
expand(WSAEOPNOTSUPP), expand(WSAEPFNOSUPPORT), expand(WSAEAFNOSUPPORT), expand(WSAEADDRINUSE),
expand(WSAEADDRNOTAVAIL), expand(WSAENETDOWN), expand(WSAENETUNREACH), expand(WSAENETRESET),
expand(WSAECONNABORTED), expand(WSAECONNRESET), expand(WSAENOBUFS), expand(WSAEISCONN),
expand(WSAENOTCONN), expand(WSAESHUTDOWN), expand(WSAETOOMANYREFS), expand(WSAETIMEDOUT),
expand(WSAECONNREFUSED), expand(WSAELOOP), expand(WSAENAMETOOLONG), expand(WSAEHOSTDOWN),
expand(WSAEHOSTUNREACH), expand(WSAENOTEMPTY), expand(WSAEPROCLIM), expand(WSAEUSERS),
expand(WSAEDQUOT), expand(WSAESTALE), expand(WSAEREMOTE), {-1, ""}};
int i;
for (i = 0; tab[i].code > 0; i++) {
if (code == tab[i].code) {
return tab[i].s;
}
}
return "Unknown error code";
}
#undef expand
#define SLASH "\\"
#define SUFFIX "*"
#else
#define SLASH "/"
#endif
extern "C" {
typedef int (*passPhraseHandleCallbackType)(char *, int, int, void *);
int caListX509NameCmp(const X509_NAME *const *a, const X509_NAME *const *b);
STACK_OF(X509_NAME) * findCAList(const char *cpCAfile, const char *cpCApath);
int lookupX509Store(X509_STORE *pStore, int nType, X509_NAME *pName, X509_OBJECT *pObj);
int callbackVerify(int ok, X509_STORE_CTX *ctx);
int callbackVerifyCRL(int ok, X509_STORE_CTX *ctx, X509_STORE *revStore);
X509_STORE *createX509Store(const char *cpFile, const char *cpPath);
X509 *readX509(FILE *fp, X509 **x509, passPhraseHandleCallbackType cb, void *passwordCallbackParam);
EVP_PKEY *readPrivateKey(FILE *fp, EVP_PKEY **key, passPhraseHandleCallbackType cb, void *passwordCallbackParam);
}
int setSocketNonBlocking(socket_handle pSocket);
#define SSL_ALGO_UNKNOWN 0
#define SSL_ALGO_RSA 1
#define SSL_ALGO_DSA 2
#define SSL_ALGO_EC 8
#define SSL_ALGO_ALL (SSL_ALGO_RSA | SSL_ALGO_DSA | SSL_ALGO_EC)
#define SSL_PROTOCOL_NONE (0)
#define SSL_PROTOCOL_SSLV2 (1 << 0)
#define SSL_PROTOCOL_SSLV3 (1 << 1)
#define SSL_PROTOCOL_TLSV1 (1 << 2)
#define SSL_PROTOCOL_TLSV1_1 (1 << 3)
#define SSL_PROTOCOL_TLSV1_2 (1 << 4)
#if (OPENSSL_VERSION_NUMBER >= 0x1010100FL)
#define SSL_PROTOCOL_TLSV1_3 (1 << 5)
#define SSL_PROTOCOL_ALL \
(SSL_PROTOCOL_SSLV2 | SSL_PROTOCOL_SSLV3 | SSL_PROTOCOL_TLSV1 | SSL_PROTOCOL_TLSV1_1 | SSL_PROTOCOL_TLSV1_2 \
| SSL_PROTOCOL_TLSV1_3)
#else
#define SSL_PROTOCOL_ALL \
(SSL_PROTOCOL_SSLV2 | SSL_PROTOCOL_SSLV3 | SSL_PROTOCOL_TLSV1 | SSL_PROTOCOL_TLSV1_1 | SSL_PROTOCOL_TLSV1_2)
#endif
typedef enum {
SSL_CLIENT_VERIFY_NONE = 0,
SSL_CLIENT_VERIFY_REQUIRE = 1,
SSL_CLIENT_VERIFY_OPTIONAL = 2,
SSL_CLIENT_VERIFY_NOTSET = 3
} SSLVerifyClient;
void ssl_init();
void ssl_term();
void ssl_socket_close(socket_handle socket, SSL *ssl);
const char *socket_error(char *tempbuf, int buflen);
int typeofSSLAlgo(X509 *pCert, EVP_PKEY *pKey);
long protocolOptions(const char *opt);
void setCtxOptions(SSL_CTX *ctx, long options);
int enable_DH_ECDH(SSL_CTX *ctx, const char *certFile);
SSL_CTX *createSSLContext(bool server, const SessionSettings &settings, std::string &errStr);
bool loadSSLCert(
SSL_CTX *ctx,
bool server,
const SessionSettings &settings,
Log *log,
passPhraseHandleCallbackType cb,
void *passwordCallbackParam,
std::string &errStr);
bool loadCAInfo(
SSL_CTX *ctx,
bool server,
const SessionSettings &settings,
Log *log,
std::string &errStr,
int &verifyLevel);
X509_STORE *loadCRLInfo(SSL_CTX *ctx, const SessionSettings &settings, Log *log, std::string &errStr);
int acceptSSLConnection(socket_handle socket, SSL *ssl, Log *log, int verify);
bool is_ip_address(const std::string &address);
bool ssl_set_sni_hostname(SSL *ssl, const std::string &hostname, Log *log = nullptr);
}
#endif
#endif