Fix License in spec
[platform/upstream/libwebsockets.git] / lib / tls / private-lib-tls.h
1 /*
2  * libwebsockets - small server side websockets and web server implementation
3  *
4  * Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com>
5  *
6  *  This library is free software; you can redistribute it and/or
7  *  modify it under the terms of the GNU Lesser General Public
8  *  License as published by the Free Software Foundation:
9  *  version 2.1 of the License.
10  *
11  *  This library is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  *  Lesser General Public License for more details.
15  *
16  *  You should have received a copy of the GNU Lesser General Public
17  *  License along with this library; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19  *  MA  02110-1301  USA
20  *
21  *  This is included from private-lib-core.h if LWS_WITH_TLS
22  */
23
24 #if !defined(__LWS_TLS_PRIVATE_H__)
25 #define __LWS_TLS_PRIVATE_H__
26
27
28 #if defined(LWS_WITH_TLS)
29
30 #if defined(USE_WOLFSSL)
31  #if defined(USE_OLD_CYASSL)
32   #if defined(_WIN32)
33    #include <IDE/WIN/user_settings.h>
34    #include <cyassl/ctaocrypt/settings.h>
35   #else
36    #include <cyassl/options.h>
37   #endif
38   #include <cyassl/openssl/ssl.h>
39   #include <cyassl/error-ssl.h>
40  #else
41   #if defined(_WIN32)
42    #include <IDE/WIN/user_settings.h>
43    #include <wolfssl/wolfcrypt/settings.h>
44   #else
45    #include <wolfssl/options.h>
46   #endif
47   #include <wolfssl/openssl/ssl.h>
48   #include <wolfssl/error-ssl.h>
49   #define OPENSSL_NO_TLSEXT
50  #endif /* not USE_OLD_CYASSL */
51 #else /* WOLFSSL */
52  #if defined(LWS_WITH_ESP32)
53   #define OPENSSL_NO_TLSEXT
54   #if !defined(LWS_AMAZON_RTOS)
55    /* AMAZON RTOS has its own setting via MTK_MBEDTLS_CONFIG_FILE */
56    #undef MBEDTLS_CONFIG_FILE
57    #define MBEDTLS_CONFIG_FILE <mbedtls/esp_config.h>
58   #endif
59   #include <mbedtls/ssl.h>
60   #include <mbedtls/aes.h>
61   #include <mbedtls/gcm.h>
62   #include <mbedtls/x509_crt.h>
63   #include "openssl/ssl.h" /* wrapper !!!! */
64  #else /* not esp32 */
65   #if defined(LWS_WITH_MBEDTLS)
66    #include <mbedtls/ssl.h>
67    #include <mbedtls/aes.h>
68    #include <mbedtls/gcm.h>
69    #include <mbedtls/x509_crt.h>
70    #include <mbedtls/x509_csr.h>
71    #include <mbedtls/ecp.h>
72    #include <mbedtls/ecdsa.h>
73    #include "openssl/ssl.h" /* wrapper !!!! */
74   #else
75    #include <openssl/ssl.h>
76    #include <openssl/evp.h>
77    #include <openssl/err.h>
78    #include <openssl/md5.h>
79    #include <openssl/sha.h>
80    #include <openssl/rsa.h>
81    #include <openssl/bn.h>
82    #include <openssl/aes.h>
83    #ifdef LWS_HAVE_OPENSSL_ECDH_H
84     #include <openssl/ecdh.h>
85    #endif
86    #if !defined(LWS_HAVE_EVP_MD_CTX_free)
87     #define EVP_MD_CTX_free EVP_MD_CTX_destroy
88    #endif
89    #include <openssl/x509v3.h>
90   #endif /* not mbedtls */
91   #if defined(OPENSSL_VERSION_NUMBER)
92    #if (OPENSSL_VERSION_NUMBER < 0x0009080afL)
93 /*
94  * later openssl defines this to negate the presence of tlsext... but it was
95  * only introduced at 0.9.8j.  Earlier versions don't know it exists so don't
96  * define it... making it look like the feature exists...
97  */
98     #define OPENSSL_NO_TLSEXT
99    #endif
100   #endif
101  #endif /* not ESP32 */
102 #endif /* not USE_WOLFSSL */
103
104 #endif /* LWS_WITH_TLS */
105
106 enum lws_tls_extant {
107         LWS_TLS_EXTANT_NO,
108         LWS_TLS_EXTANT_YES,
109         LWS_TLS_EXTANT_ALTERNATIVE
110 };
111
112
113 #if defined(LWS_WITH_TLS)
114
115 typedef SSL lws_tls_conn;
116 typedef SSL_CTX lws_tls_ctx;
117 typedef BIO lws_tls_bio;
118 typedef X509 lws_tls_x509;
119
120 #if defined(LWS_WITH_NETWORK)
121 #include "private-network.h"
122 #endif
123
124 LWS_EXTERN int
125 lws_context_init_ssl_library(const struct lws_context_creation_info *info);
126 LWS_EXTERN void
127 lws_context_deinit_ssl_library(struct lws_context *context);
128 #define LWS_SSL_ENABLED(vh) (vh && vh->tls.use_ssl)
129
130 extern const struct lws_tls_ops tls_ops_openssl, tls_ops_mbedtls;
131
132 struct lws_ec_valid_curves {
133         int id;
134         const char *jwa_name; /* list terminates with NULL jwa_name */
135 };
136
137 LWS_EXTERN enum lws_tls_extant
138 lws_tls_use_any_upgrade_check_extant(const char *name);
139 LWS_EXTERN int openssl_websocket_private_data_index;
140
141
142 LWS_EXTERN void
143 lws_tls_err_describe_clear(void);
144
145 LWS_EXTERN int
146 lws_tls_openssl_cert_info(X509 *x509, enum lws_tls_cert_info type,
147                           union lws_tls_cert_info_results *buf, size_t len);
148 LWS_EXTERN int
149 lws_tls_check_all_cert_lifetimes(struct lws_context *context);
150
151 LWS_EXTERN int
152 lws_tls_alloc_pem_to_der_file(struct lws_context *context, const char *filename,
153                               const char *inbuf, lws_filepos_t inlen,
154                               uint8_t **buf, lws_filepos_t *amount);
155 LWS_EXTERN char *
156 lws_ssl_get_error_string(int status, int ret, char *buf, size_t len);
157
158 int
159 lws_gencrypto_bits_to_bytes(int bits);
160
161 void
162 lws_gencrypto_destroy_elements(struct lws_gencrypto_keyelem *el, int m);
163
164 /* genec */
165
166 struct lws_gencrypto_keyelem;
167 struct lws_ec_curves;
168
169 LWS_EXTERN const struct lws_ec_curves lws_ec_curves[4];
170 const struct lws_ec_curves *
171 lws_genec_curve(const struct lws_ec_curves *table, const char *name);
172 LWS_VISIBLE void
173 lws_genec_destroy_elements(struct lws_gencrypto_keyelem *el);
174 int
175 lws_gencrypto_mbedtls_rngf(void *context, unsigned char *buf, size_t len);
176
177 int
178 lws_genec_confirm_curve_allowed_by_tls_id(const char *allowed, int id,
179                                           struct lws_jwk *jwk);
180
181 #endif
182 #endif