FreeRDP
Loading...
Searching...
No Matches
opensslcompat.h
1
20#ifndef FREERDP_LIB_CRYPTO_OPENSSLCOMPAT_H
21#define FREERDP_LIB_CRYPTO_OPENSSLCOMPAT_H
22
23#include <winpr/winpr.h>
24#include <winpr/assert.h>
25#include <freerdp/config.h>
26
27#include <freerdp/api.h>
28
29#include <openssl/x509.h>
30
31#ifdef WITH_OPENSSL
32
33#include <openssl/opensslv.h>
34
35#if OPENSSL_VERSION_NUMBER < 0x10100000L || \
36 (defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x2070000fL)
37
38#include <openssl/bio.h>
39#include <openssl/rsa.h>
40#include <openssl/bn.h>
41
42#define BIO_get_data(b) (b)->ptr
43#define BIO_set_data(b, v) (b)->ptr = v
44#define BIO_get_init(b) (b)->init
45#define BIO_set_init(b, v) (b)->init = v
46#define BIO_get_next(b, v) (b)->next_bio
47#define BIO_set_next(b, v) (b)->next_bio = v
48#define BIO_get_shutdown(b) (b)->shutdown
49#define BIO_set_shutdown(b, v) (b)->shutdown = v
50#define BIO_get_retry_reason(b) (b)->retry_reason
51#define BIO_set_retry_reason(b, v) (b)->retry_reason = v
52
53#define BIO_meth_set_write(b, f) (b)->bwrite = (f)
54#define BIO_meth_set_read(b, f) (b)->bread = (f)
55#define BIO_meth_set_puts(b, f) (b)->bputs = (f)
56#define BIO_meth_set_gets(b, f) (b)->bgets = (f)
57#define BIO_meth_set_ctrl(b, f) (b)->ctrl = (f)
58#define BIO_meth_set_create(b, f) (b)->create = (f)
59#define BIO_meth_set_destroy(b, f) (b)->destroy = (f)
60#define BIO_meth_set_callback_ctrl(b, f) (b)->callback_ctrl = (f)
61
62WINPR_ATTR_NODISCARD
63FREERDP_LOCAL
64BIO_METHOD* BIO_meth_new(int type, const char* name);
65
66FREERDP_LOCAL void RSA_get0_key(const RSA* r, const BIGNUM** n, const BIGNUM** e, const BIGNUM** d);
67
68#endif /* OPENSSL < 1.1.0 || LIBRESSL */
69#endif /* WITH_OPENSSL */
70
71/* Drop in replacement for older OpenSSL and LibRESSL */
72#if defined(LIBRESSL_VERSION_NUMBER) || \
73 (defined(OPENSSL_VERSION_NUMBER) && (OPENSSL_VERSION_NUMBER < 0x1010000fL))
74
75WINPR_ATTR_NODISCARD
76static inline STACK_OF(X509) * sk_X509_deep_copy(const STACK_OF(X509) * sk,
77 X509* (*copyfunc)(const X509*),
78 void (*freefunc)(X509*))
79{
80 WINPR_ASSERT(copyfunc);
81 WINPR_ASSERT(freefunc);
82
83 STACK_OF(X509)* stack = sk_X509_new_null();
84 if (!stack)
85 return nullptr;
86
87 if (sk)
88 {
89 for (int i = 0; i < sk_X509_num(sk); i++)
90 {
91 X509* cert = sk_X509_value(sk, i);
92 X509* copy = copyfunc(cert);
93 if (!copy)
94 goto fail;
95 const int rc = sk_X509_push(stack, copy);
96 if (rc <= 0)
97 goto fail;
98 }
99 }
100
101 return stack;
102
103fail:
104 sk_X509_pop_free(stack, freefunc);
105 return nullptr;
106}
107#endif
108
109/* OpenSSL API is not const consistent.
110 * While the TYPE_dup function take non const arguments
111 * the TYPE_sk versions require the arguments to be const...
112 */
113WINPR_ATTR_NODISCARD
114static inline X509* X509_const_dup(const X509* x509)
115{
116 X509* ptr = WINPR_CAST_CONST_PTR_AWAY(x509, X509*);
117 return X509_dup(ptr);
118}
119#endif /* FREERDP_LIB_CRYPTO_OPENSSLCOMPAT_H */