v / vlib / net / openssl / openssl_compat.h
60 lines · 56 sloc · 2.14 KB · 8e3e67eff2703ee6a931953575b6c236e7349712
Raw
1// Match the init API to the OpenSSL headers that are actually available.
2#if defined(LIBRESSL_VERSION_NUMBER) || !defined(OPENSSL_VERSION_NUMBER) \
3 || OPENSSL_VERSION_NUMBER < 0x10100000L
4static int v_net_openssl_init_ssl(void) {
5 SSL_load_error_strings();
6 return SSL_library_init();
7}
8#else
9static int v_net_openssl_init_ssl(void) {
10 return OPENSSL_init_ssl(OPENSSL_INIT_LOAD_SSL_STRINGS, 0);
11}
12#endif
13
14// SSL_get1_peer_certificate is only available in OpenSSL 3.x.
15#if defined(LIBRESSL_VERSION_NUMBER) || !defined(OPENSSL_VERSION_NUMBER) \
16 || OPENSSL_VERSION_NUMBER < 0x30000000L
17static X509 *v_net_openssl_get1_peer_certificate(SSL *ssl) {
18 return SSL_get_peer_certificate(ssl);
19}
20#else
21static X509 *v_net_openssl_get1_peer_certificate(SSL *ssl) {
22 return SSL_get1_peer_certificate(ssl);
23}
24#endif
25
26// ALPN (SSL_set_alpn_protos / SSL_get0_alpn_selected) is only available in
27// OpenSSL 1.0.2 and later. On older OpenSSL-compatible headers, fall back to
28// no-op shims so the module still links; ALPN is simply unavailable there.
29// LibreSSL reports a high OPENSSL_VERSION_NUMBER and provides ALPN, so it uses
30// the native path below.
31#if !defined(OPENSSL_VERSION_NUMBER) || OPENSSL_VERSION_NUMBER < 0x10002000L
32static int v_net_openssl_set_alpn_protos(SSL *ssl, const unsigned char *protos, unsigned int protos_len) {
33 (void)ssl;
34 (void)protos;
35 (void)protos_len;
36 return -1; // ALPN unsupported on this OpenSSL version
37}
38static void v_net_openssl_get0_alpn_selected(SSL *ssl, const unsigned char **data, unsigned int *len) {
39 (void)ssl;
40 *data = NULL;
41 *len = 0;
42}
43#else
44static int v_net_openssl_set_alpn_protos(SSL *ssl, const unsigned char *protos, unsigned int protos_len) {
45 return SSL_set_alpn_protos(ssl, protos, protos_len);
46}
47static void v_net_openssl_get0_alpn_selected(SSL *ssl, const unsigned char **data, unsigned int *len) {
48 SSL_get0_alpn_selected(ssl, data, len);
49}
50#endif
51
52// LibreSSL and older OpenSSL-compatible headers may not expose the async
53// SSL_ERROR constants, but V's SSLError enum needs stable values for them.
54#ifndef SSL_ERROR_WANT_ASYNC
55#define SSL_ERROR_WANT_ASYNC 9
56#endif
57
58#ifndef SSL_ERROR_WANT_ASYNC_JOB
59#define SSL_ERROR_WANT_ASYNC_JOB 10
60#endif
61