blob: 645c9b71558ed3c0799b44b48b2cd17904f0290b [file] [log] [blame]
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001#ifndef _PROTO_OPENSSL_COMPAT_H
2#define _PROTO_OPENSSL_COMPAT_H
3#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 Tarreau80ebacf2016-11-24 20:07:11 +010017#if (OPENSSL_VERSION_NUMBER < 0x0090800fL)
18/* Functions present in OpenSSL 0.9.8, older not tested */
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +020019static inline const unsigned char *SSL_SESSION_get_id(const SSL_SESSION *sess, unsigned int *sid_length)
20{
21 *sid_length = sess->session_id_length;
22 return sess->session_id;
23}
24
25static inline X509_NAME_ENTRY *X509_NAME_get_entry(const X509_NAME *name, int loc)
26{
27 return sk_X509_NAME_ENTRY_value(name->entries, loc);
28}
29
30static inline ASN1_OBJECT *X509_NAME_ENTRY_get_object(const X509_NAME_ENTRY *ne)
31{
32 return ne->object;
33}
34
35static inline ASN1_STRING *X509_NAME_ENTRY_get_data(const X509_NAME_ENTRY *ne)
36{
37 return ne->value;
38}
39
40static inline int ASN1_STRING_length(const ASN1_STRING *x)
41{
42 return x->length;
43}
44
45static inline int X509_NAME_entry_count(X509_NAME *name)
46{
47 return sk_X509_NAME_ENTRY_num(name->entries)
48}
49
Willy Tarreau80ebacf2016-11-24 20:07:11 +010050static inline void X509_ALGOR_get0(ASN1_OBJECT **paobj, int *pptype, const void **ppval, const X509_ALGOR *algor)
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +020051{
Willy Tarreau80ebacf2016-11-24 20:07:11 +010052 *paobj = algor->algorithm;
53}
54
55#endif // OpenSSL < 0.9.8
56
57
58#if (OPENSSL_VERSION_NUMBER < 0x1000000fL)
59/*
60 * Functions introduced in OpenSSL 1.0.1
61 */
62static inline int SSL_SESSION_set1_id_context(SSL_SESSION *s, const unsigned char *sid_ctx, unsigned int sid_ctx_len)
63{
64 s->sid_ctx_length = sid_ctx_len;
65 memcpy(s->sid_ctx, sid_ctx, sid_ctx_len);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +020066 return 1;
67}
68
Willy Tarreau80ebacf2016-11-24 20:07:11 +010069static inline int EVP_PKEY_base_id(const EVP_PKEY *pkey)
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +020070{
Willy Tarreau80ebacf2016-11-24 20:07:11 +010071 return EVP_PKEY_type(pkey->type);
72}
73
74/* minimal implementation based on the fact that the only known call place
75 * doesn't make use of other arguments.
76 */
77static inline int X509_PUBKEY_get0_param(ASN1_OBJECT **ppkalg, const unsigned char **pk, int *ppklen, X509_ALGOR **pa, X509_PUBKEY *pub)
78{
79 *ppkalg = pub->algor->algorithm;
80 return 1;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +020081}
82
83#ifndef X509_get_X509_PUBKEY
84#define X509_get_X509_PUBKEY(x) ((x)->cert_info->key
85#endif
86
87#endif
88
Luca Pizzamiglio578b1692016-12-12 10:56:56 +010089#if (OPENSSL_VERSION_NUMBER < 0x1010000fL) || defined(LIBRESSL_VERSION_NUMBER)
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +020090/*
Luca Pizzamiglio578b1692016-12-12 10:56:56 +010091 * Functions introduced in OpenSSL 1.1.0 and not yet present in LibreSSL
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +020092 */
93
94static inline const unsigned char *SSL_SESSION_get0_id_context(const SSL_SESSION *sess, unsigned int *sid_ctx_length)
95{
96 *sid_ctx_length = sess->sid_ctx_length;
97 return sess->sid_ctx;
98}
99
100static inline int SSL_SESSION_set1_id(SSL_SESSION *s, const unsigned char *sid, unsigned int sid_len)
101{
102 s->session_id_length = sid_len;
103 memcpy(s->session_id, sid, sid_len);
104 return 1;
105}
106
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +0100107#if (!defined OPENSSL_NO_OCSP)
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +0200108static inline const OCSP_CERTID *OCSP_SINGLERESP_get0_id(const OCSP_SINGLERESP *single)
109{
110 return single->certId;
111}
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +0100112#endif
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +0200113
114static inline pem_password_cb *SSL_CTX_get_default_passwd_cb(SSL_CTX *ctx)
115{
116 return ctx->default_passwd_callback;
117}
118
119static inline void *SSL_CTX_get_default_passwd_cb_userdata(SSL_CTX *ctx)
120{
121 return ctx->default_passwd_callback_userdata;
122}
123
124#ifndef OPENSSL_NO_DH
125static inline int DH_set0_pqg(DH *dh, BIGNUM *p, BIGNUM *q, BIGNUM *g)
126{
127 /* Implements only the bare necessities for HAProxy */
128 dh->p = p;
129 dh->g = g;
130 return 1;
131}
132#endif
133
134static inline const unsigned char *ASN1_STRING_get0_data(const ASN1_STRING *x)
135{
136 return x->data;
137}
138
139static inline X509_ALGOR *X509_get0_tbs_sigalg(const X509 *x)
140{
141 return x->cert_info->signature;
142}
143
144#endif
145
146#if (OPENSSL_VERSION_NUMBER >= 0x1010000fL)
147#define __OPENSSL_110_CONST__ const
148#else
149#define __OPENSSL_110_CONST__
150#endif
151
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +0100152#ifdef OPENSSL_IS_BORINGSSL
153#define SSL_NO_GENERATE_CERTIFICATES
154
155static inline int EVP_PKEY_base_id(EVP_PKEY *pkey)
156{
157 return EVP_PKEY_type(pkey->type);
158}
159#endif
160
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +0200161#endif /* _PROTO_OPENSSL_COMPAT_H */