blob: 5ac3abaf8a3d96e214b42b9b024c8bf4b9b48027 [file] [log] [blame]
Willy Tarreau55994562019-05-09 14:52:44 +02001#ifndef _COMMON_OPENSSL_COMPAT_H
2#define _COMMON_OPENSSL_COMPAT_H
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02003#include <openssl/crypto.h>
4#include <openssl/ssl.h>
5#include <openssl/x509.h>
6#include <openssl/x509v3.h>
7#include <openssl/x509.h>
8#include <openssl/err.h>
9#include <openssl/rand.h>
10#if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP)
11#include <openssl/ocsp.h>
12#endif
13#ifndef OPENSSL_NO_DH
14#include <openssl/dh.h>
15#endif
16
Willy Tarreau1d158ab2019-05-09 13:41:45 +020017#if defined(LIBRESSL_VERSION_NUMBER)
18/* LibreSSL is a fork of OpenSSL 1.0.1g but pretends to be 2.0.0, thus
19 * systematically breaking when some code is written for a specific version
20 * of OpenSSL. Let's make it appear like what it really is and deal with
21 * extra features with ORs and not with AND NOT.
22 */
23#define HA_OPENSSL_VERSION_NUMBER 0x1000107fL
24#else /* this is for a real OpenSSL or a truly compatible derivative */
Willy Tarreau9a1ab082019-05-09 13:26:41 +020025#define HA_OPENSSL_VERSION_NUMBER OPENSSL_VERSION_NUMBER
Willy Tarreau1d158ab2019-05-09 13:41:45 +020026#endif
Willy Tarreau9a1ab082019-05-09 13:26:41 +020027
Willy Tarreau9356dac2019-05-10 09:22:53 +020028#ifndef OPENSSL_VERSION
29#define OPENSSL_VERSION SSLEAY_VERSION
30#define OpenSSL_version(x) SSLeay_version(x)
31#define OpenSSL_version_num SSLeay
32#endif
33
Willy Tarreau9a1ab082019-05-09 13:26:41 +020034#if (HA_OPENSSL_VERSION_NUMBER < 0x0090800fL)
Willy Tarreau80ebacf2016-11-24 20:07:11 +010035/* Functions present in OpenSSL 0.9.8, older not tested */
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +020036static inline const unsigned char *SSL_SESSION_get_id(const SSL_SESSION *sess, unsigned int *sid_length)
37{
38 *sid_length = sess->session_id_length;
39 return sess->session_id;
40}
41
42static inline X509_NAME_ENTRY *X509_NAME_get_entry(const X509_NAME *name, int loc)
43{
44 return sk_X509_NAME_ENTRY_value(name->entries, loc);
45}
46
47static inline ASN1_OBJECT *X509_NAME_ENTRY_get_object(const X509_NAME_ENTRY *ne)
48{
49 return ne->object;
50}
51
52static inline ASN1_STRING *X509_NAME_ENTRY_get_data(const X509_NAME_ENTRY *ne)
53{
54 return ne->value;
55}
56
57static inline int ASN1_STRING_length(const ASN1_STRING *x)
58{
59 return x->length;
60}
61
62static inline int X509_NAME_entry_count(X509_NAME *name)
63{
64 return sk_X509_NAME_ENTRY_num(name->entries)
65}
66
Willy Tarreau80ebacf2016-11-24 20:07:11 +010067static inline void X509_ALGOR_get0(ASN1_OBJECT **paobj, int *pptype, const void **ppval, const X509_ALGOR *algor)
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +020068{
Willy Tarreau80ebacf2016-11-24 20:07:11 +010069 *paobj = algor->algorithm;
70}
71
72#endif // OpenSSL < 0.9.8
73
74
Willy Tarreau9a1ab082019-05-09 13:26:41 +020075#if (HA_OPENSSL_VERSION_NUMBER < 0x1000000fL)
Willy Tarreau2b3205b2017-01-19 17:04:02 +010076/* Functions introduced in OpenSSL 1.0.0 */
Willy Tarreau80ebacf2016-11-24 20:07:11 +010077static inline int EVP_PKEY_base_id(const EVP_PKEY *pkey)
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +020078{
Willy Tarreau80ebacf2016-11-24 20:07:11 +010079 return EVP_PKEY_type(pkey->type);
80}
81
82/* minimal implementation based on the fact that the only known call place
83 * doesn't make use of other arguments.
84 */
85static inline int X509_PUBKEY_get0_param(ASN1_OBJECT **ppkalg, const unsigned char **pk, int *ppklen, X509_ALGOR **pa, X509_PUBKEY *pub)
86{
87 *ppkalg = pub->algor->algorithm;
88 return 1;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +020089}
90
91#ifndef X509_get_X509_PUBKEY
92#define X509_get_X509_PUBKEY(x) ((x)->cert_info->key
93#endif
94
95#endif
96
Willy Tarreau9a1ab082019-05-09 13:26:41 +020097#if (HA_OPENSSL_VERSION_NUMBER < 0x1000100fL)
Willy Tarreau2b3205b2017-01-19 17:04:02 +010098/*
99 * Functions introduced in OpenSSL 1.0.1
100 */
101static inline int SSL_SESSION_set1_id_context(SSL_SESSION *s, const unsigned char *sid_ctx, unsigned int sid_ctx_len)
102{
103 s->sid_ctx_length = sid_ctx_len;
104 memcpy(s->sid_ctx, sid_ctx, sid_ctx_len);
105 return 1;
106}
107#endif
108
Willy Tarreau1d158ab2019-05-09 13:41:45 +0200109#if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL) && (LIBRESSL_VERSION_NUMBER < 0x2070000fL)
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +0200110/*
Ilya Shipitsin54832b92019-05-05 23:27:54 +0500111 * Functions introduced in OpenSSL 1.1.0 and in LibreSSL 2.7.0
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +0200112 */
113
114static inline const unsigned char *SSL_SESSION_get0_id_context(const SSL_SESSION *sess, unsigned int *sid_ctx_length)
115{
116 *sid_ctx_length = sess->sid_ctx_length;
117 return sess->sid_ctx;
118}
119
120static inline int SSL_SESSION_set1_id(SSL_SESSION *s, const unsigned char *sid, unsigned int sid_len)
121{
122 s->session_id_length = sid_len;
123 memcpy(s->session_id, sid, sid_len);
124 return 1;
125}
126
Emmanuel Hocdet019f9b12017-10-02 17:12:06 +0200127static inline X509_ALGOR *X509_get0_tbs_sigalg(const X509 *x)
128{
129 return x->cert_info->signature;
130}
131
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +0100132#if (!defined OPENSSL_NO_OCSP)
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +0200133static inline const OCSP_CERTID *OCSP_SINGLERESP_get0_id(const OCSP_SINGLERESP *single)
134{
135 return single->certId;
136}
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +0100137#endif
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +0200138
139static inline pem_password_cb *SSL_CTX_get_default_passwd_cb(SSL_CTX *ctx)
140{
141 return ctx->default_passwd_callback;
142}
143
144static inline void *SSL_CTX_get_default_passwd_cb_userdata(SSL_CTX *ctx)
145{
146 return ctx->default_passwd_callback_userdata;
147}
148
149#ifndef OPENSSL_NO_DH
150static inline int DH_set0_pqg(DH *dh, BIGNUM *p, BIGNUM *q, BIGNUM *g)
151{
152 /* Implements only the bare necessities for HAProxy */
153 dh->p = p;
154 dh->g = g;
155 return 1;
156}
157#endif
158
159static inline const unsigned char *ASN1_STRING_get0_data(const ASN1_STRING *x)
160{
161 return x->data;
162}
163
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +0200164#endif
165
Willy Tarreau1d158ab2019-05-09 13:41:45 +0200166#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) || (LIBRESSL_VERSION_NUMBER >= 0x2070200fL)
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +0200167#define __OPENSSL_110_CONST__ const
168#else
169#define __OPENSSL_110_CONST__
170#endif
171
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +0100172#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +0100173
174static inline int EVP_PKEY_base_id(EVP_PKEY *pkey)
175{
176 return EVP_PKEY_type(pkey->type);
177}
178#endif
179
Willy Tarreaua4fb8ed2017-01-19 16:50:25 +0100180/* ERR_remove_state() was deprecated in 1.0.0 in favor of
181 * ERR_remove_thread_state(), which was in turn deprecated in
182 * 1.1.0 and does nothing anymore. Let's simply silently kill
183 * it.
184 */
Willy Tarreau9a1ab082019-05-09 13:26:41 +0200185#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL)
Willy Tarreaua4fb8ed2017-01-19 16:50:25 +0100186#undef ERR_remove_state
187#define ERR_remove_state(x)
188#endif
189
Willy Tarreau77d88da2017-01-19 17:10:54 +0100190
191/* RAND_pseudo_bytes() is deprecated in 1.1.0 in favor of RAND_bytes(). Note
192 * that the return codes differ, but it happens that the only use case (ticket
193 * key update) was already wrong, considering a non-cryptographic random as a
194 * failure.
195 */
Willy Tarreau9a1ab082019-05-09 13:26:41 +0200196#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL)
Willy Tarreau77d88da2017-01-19 17:10:54 +0100197#undef RAND_pseudo_bytes
198#define RAND_pseudo_bytes(x,y) RAND_bytes(x,y)
199#endif
200
Emmanuel Hocdet8c2ddc22017-07-19 16:04:05 +0200201
202/* Signature from RFC 5246, missing in openssl < 1.0.1 */
203#ifndef TLSEXT_signature_anonymous
204#define TLSEXT_signature_anonymous 0
205#define TLSEXT_signature_rsa 1
206#define TLSEXT_signature_dsa 2
207#define TLSEXT_signature_ecdsa 3
208#endif
209
Willy Tarreau9356dac2019-05-10 09:22:53 +0200210#if (OPENSSL_VERSION_NUMBER < 0x10100000L) || (LIBRESSL_VERSION_NUMBER < 0x20700000L)
211#define X509_getm_notBefore X509_get_notBefore
212#define X509_getm_notAfter X509_get_notAfter
213#endif
214
215#if (OPENSSL_VERSION_NUMBER < 0x1010000fL || defined LIBRESSL_VERSION_NUMBER)
216#define EVP_CTRL_AEAD_SET_IVLEN EVP_CTRL_GCM_SET_IVLEN
217#define EVP_CTRL_AEAD_SET_TAG EVP_CTRL_GCM_SET_TAG
218#endif
219
220/* Supported hash function for TLS tickets */
221#ifdef OPENSSL_NO_SHA256
222#define TLS_TICKET_HASH_FUNCT EVP_sha1
223#else
224#define TLS_TICKET_HASH_FUNCT EVP_sha256
225#endif /* OPENSSL_NO_SHA256 */
226
Willy Tarreau55994562019-05-09 14:52:44 +0200227#endif /* _COMMON_OPENSSL_COMPAT_H */