| 1 |
/* Definitions for generic OpenSSL include stuff. |
| 2 |
* |
| 3 |
* Copyright 2023-2026 Andre Lorbach and Adiscon GmbH. |
| 4 |
* |
| 5 |
* This file is part of the rsyslog runtime library. |
| 6 |
* |
| 7 |
* The rsyslog runtime library is free software: you can redistribute it and/or modify |
| 8 |
* it under the terms of the GNU Lesser General Public License as published by |
| 9 |
* the Free Software Foundation, either version 3 of the License, or |
| 10 |
* (at your option) any later version. |
| 11 |
* |
| 12 |
* The rsyslog runtime library is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
* GNU Lesser General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU Lesser General Public License |
| 18 |
* along with the rsyslog runtime library. If not, see <http://www.gnu.org/licenses/>. |
| 19 |
* |
| 20 |
* A copy of the GPL can be found in the file "COPYING" in this distribution. |
| 21 |
* A copy of the LGPL can be found in the file "COPYING.LESSER" in this distribution. |
| 22 |
*/ |
| 23 |
|
| 24 |
#ifndef INCLUDED_NET_OSSL_H |
| 25 |
#define INCLUDED_NET_OSSL_H |
| 26 |
|
| 27 |
/* Needed OpenSSL Includes */ |
| 28 |
#ifdef ENABLE_WOLFSSL |
| 29 |
#include <wolfssl/options.h> |
| 30 |
#include <wolfssl/ssl.h> |
| 31 |
#include <wolfssl/openssl/ssl.h> |
| 32 |
#include <wolfssl/openssl/x509v3.h> |
| 33 |
#include <wolfssl/openssl/err.h> |
| 34 |
#include <wolfssl/openssl/rand.h> |
| 35 |
#include <wolfssl/openssl/evp.h> |
| 36 |
#if OPENSSL_VERSION_NUMBER >= 0x30000000L && !defined(LIBRESSL_VERSION_NUMBER) |
| 37 |
#include <wolfssl/openssl/bioerr.h> |
| 38 |
#endif |
| 39 |
#ifndef OPENSSL_NO_ENGINE |
| 40 |
#include <wolfssl/openssl/engine.h> |
| 41 |
#endif |
| 42 |
#else |
| 43 |
#include <openssl/ssl.h> |
| 44 |
#include <openssl/x509v3.h> |
| 45 |
#include <openssl/err.h> |
| 46 |
#include <openssl/ocsp.h> |
| 47 |
#if OPENSSL_VERSION_NUMBER >= 0x30000000L && !defined(LIBRESSL_VERSION_NUMBER) |
| 48 |
#include <openssl/bioerr.h> |
| 49 |
#endif |
| 50 |
#ifndef OPENSSL_NO_ENGINE |
| 51 |
#include <openssl/engine.h> |
| 52 |
#endif |
| 53 |
#include <openssl/rand.h> |
| 54 |
#include <openssl/evp.h> |
| 55 |
#endif |
| 56 |
|
| 57 |
/* Internal OpenSSL defined ENUMS */ |
| 58 |
typedef enum { |
| 59 |
OSSL_AUTH_CERTNAME = 0, |
| 60 |
OSSL_AUTH_CERTFINGERPRINT = 1, |
| 61 |
OSSL_AUTH_CERTVALID = 2, |
| 62 |
OSSL_AUTH_CERTANON = 3 |
| 63 |
} AuthMode; |
| 64 |
|
| 65 |
typedef enum { OSSL_EXPIRED_PERMIT = 0, OSSL_EXPIRED_DENY = 1, OSSL_EXPIRED_WARN = 2 } PermitExpiredCerts; |
| 66 |
|
| 67 |
typedef enum { |
| 68 |
osslServer = 0, /**< Server SSL Object */ |
| 69 |
osslClient = 1 /**< Client SSL Object */ |
| 70 |
} osslSslState_t; |
| 71 |
|
| 72 |
/* the net_ossl object */ |
| 73 |
struct net_ossl_s { |
| 74 |
BEGINobjInstance |
| 75 |
; /* Data to implement generic object - MUST be the first data element! */ |
| 76 |
/* Config Cert vars */ |
| 77 |
const uchar *pszCAFile; |
| 78 |
const uchar *pszCRLFile; |
| 79 |
const uchar *pszExtraCAFiles; |
| 80 |
const uchar *pszKeyFile; |
| 81 |
const uchar *pszCertFile; |
| 82 |
AuthMode authMode; |
| 83 |
permittedPeers_t *pPermPeers; /* permitted peers */ |
| 84 |
int bReportAuthErr; /* only the first auth error is to be reported, this var triggers it. Initially, it is |
| 85 |
* set to 1 and changed to 0 after the first report. It is changed back to 1 after |
| 86 |
* one successful authentication. */ |
| 87 |
int bSANpriority; /* if true, we do stricter checking (if any SAN present we do not check CN) */ |
| 88 |
/* Open SSL objects */ |
| 89 |
BIO *bio; /* OpenSSL main BIO obj */ |
| 90 |
int ctx_is_copy; |
| 91 |
SSL_CTX *ctx; /* credentials, ciphers, ... */ |
| 92 |
SSL *ssl; /* OpenSSL main SSL obj */ |
| 93 |
osslSslState_t sslState; /**< what must we retry? */ |
| 94 |
}; |
| 95 |
|
| 96 |
/* interface */ |
| 97 |
BEGINinterface(net_ossl) /* name must also be changed in ENDinterface macro! */ |
| 98 |
rsRetVal (*Construct)(net_ossl_t **ppThis); |
| 99 |
rsRetVal (*Destruct)(net_ossl_t **ppThis); |
| 100 |
rsRetVal (*osslCtxInit)(net_ossl_t *pThis, const SSL_METHOD *method); |
| 101 |
#if OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(ENABLE_WOLFSSL) |
| 102 |
rsRetVal (*osslCtxInitCookie)(net_ossl_t *pThis); |
| 103 |
#endif // OPENSSL_VERSION_NUMBER >= 0x10100000L |
| 104 |
rsRetVal (*osslInitEngine)(net_ossl_t *pThis); |
| 105 |
// OpenSSL Helper function exports |
| 106 |
rsRetVal (*osslChkpeername)(net_ossl_t *pThis, X509 *certpeer, uchar *fromHostIP); |
| 107 |
rsRetVal (*osslPeerfingerprint)(net_ossl_t *pThis, X509 *certpeer, uchar *fromHostIP); |
| 108 |
X509 *(*osslGetpeercert)(net_ossl_t *pThis, SSL *ssl, uchar *fromHostIP); |
| 109 |
rsRetVal (*osslChkpeercertvalidity)(net_ossl_t *pThis, SSL *ssl, uchar *fromHostIP); |
| 110 |
#if OPENSSL_VERSION_NUMBER >= 0x10002000L && !defined(LIBRESSL_VERSION_NUMBER) && !defined(ENABLE_WOLFSSL) |
| 111 |
rsRetVal (*osslApplyTlscgfcmd)(net_ossl_t *pThis, uchar *tlscfgcmd); |
| 112 |
#endif // OPENSSL_VERSION_NUMBER >= 0x10002000L |
| 113 |
void (*osslSetBioCallback)(BIO *conn); |
| 114 |
void (*osslSetCtxVerifyCallback)(SSL_CTX *pCtx, int flags); |
| 115 |
void (*osslSetSslVerifyCallback)(SSL *pSsl, int flags); |
| 116 |
void (*osslLastOpenSSLErrorMsg)(uchar *fromHost, const int ret, SSL *ssl, int severity, const char *pszCallSource, |
| 117 |
const char *pszOsslApi); |
| 118 |
ENDinterface(net_ossl) |
| 119 |
|
| 120 |
#define net_osslCURR_IF_VERSION 1 /* increment whenever you change the interface structure! */ |
| 121 |
// ------------------------------------------------------ |
| 122 |
|
| 123 |
/* OpenSSL API differences */ |
| 124 |
#ifdef ENABLE_WOLFSSL |
| 125 |
#define RSYSLOG_X509_NAME_oneline(X509CERT) X509_get_subject_name(X509CERT) |
| 126 |
#else |
| 127 |
#if OPENSSL_VERSION_NUMBER >= 0x10100000L |
| 128 |
#define RSYSLOG_X509_NAME_oneline(X509CERT) X509_get_subject_name(X509CERT) |
| 129 |
#define RSYSLOG_BIO_method_name(SSLBIO) BIO_method_name(SSLBIO) |
| 130 |
#define RSYSLOG_BIO_number_read(SSLBIO) BIO_number_read(SSLBIO) |
| 131 |
#define RSYSLOG_BIO_number_written(SSLBIO) BIO_number_written(SSLBIO) |
| 132 |
#else |
| 133 |
#define RSYSLOG_X509_NAME_oneline(X509CERT) (X509CERT != NULL ? X509CERT->cert_info->subject : NULL) |
| 134 |
#define RSYSLOG_BIO_method_name(SSLBIO) SSLBIO->method->name |
| 135 |
#define RSYSLOG_BIO_number_read(SSLBIO) SSLBIO->num |
| 136 |
#define RSYSLOG_BIO_number_written(SSLBIO) SSLBIO->num |
| 137 |
#endif |
| 138 |
#endif |
| 139 |
|
| 140 |
/*-----------------------------------------------------------------------------*/ |
| 141 |
/* OpenSSL Global Helper functions prototypes */ |
| 142 |
#define MUTEX_TYPE pthread_mutex_t |
| 143 |
#define MUTEX_SETUP(x) pthread_mutex_init(&(x), NULL) |
| 144 |
#define MUTEX_CLEANUP(x) pthread_mutex_destroy(&(x)) |
| 145 |
#define MUTEX_LOCK(x) pthread_mutex_lock(&(x)) |
| 146 |
#define MUTEX_UNLOCK(x) pthread_mutex_unlock(&(x)) |
| 147 |
#define THREAD_ID pthread_self() |
| 148 |
|
| 149 |
/* This array will store all of the mutexes available to OpenSSL. */ |
| 150 |
struct CRYPTO_dynlock_value { |
| 151 |
MUTEX_TYPE mutex; |
| 152 |
}; |
| 153 |
|
| 154 |
void dyn_destroy_function(struct CRYPTO_dynlock_value *l, |
| 155 |
__attribute__((unused)) const char *file, |
| 156 |
__attribute__((unused)) int line); |
| 157 |
void dyn_lock_function(int mode, |
| 158 |
struct CRYPTO_dynlock_value *l, |
| 159 |
__attribute__((unused)) const char *file, |
| 160 |
__attribute__((unused)) int line); |
| 161 |
struct CRYPTO_dynlock_value *dyn_create_function(__attribute__((unused)) const char *file, |
| 162 |
__attribute__((unused)) int line); |
| 163 |
unsigned long id_function(void); |
| 164 |
void locking_function(int mode, int n, __attribute__((unused)) const char *file, __attribute__((unused)) int line); |
| 165 |
|
| 166 |
int opensslh_THREAD_setup(void); |
| 167 |
int opensslh_THREAD_cleanup(void); |
| 168 |
|
| 169 |
void osslGlblInit(void); |
| 170 |
void osslGlblExit(void); |
| 171 |
/*-----------------------------------------------------------------------------*/ |
| 172 |
|
| 173 |
/* prototypes */ |
| 174 |
PROTOTYPEObjFull(net_ossl); |
| 175 |
|
| 176 |
/* the name of our library binary */ |
| 177 |
// #define LM_NET_OSSL_FILENAME "lmnet_ossl" |
| 178 |
#define LM_NET_OSSL_FILENAME "lmnsd_ossl" |
| 179 |
|
| 180 |
|
| 181 |
#endif /* #ifndef INCLUDED_NET_OSSL_H */ |