1 // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef _SSL_TYPES_H_
16 #define _SSL_TYPES_H_
17 
18 #ifdef __cplusplus
19  extern "C" {
20 #endif
21 
22 //#include "private-lib-core.h"
23 #include <lws_config.h>
24 #if defined(LWS_PLAT_FREERTOS)
25  /* AMAZON RTOS has its own setting via MTK_MBEDTLS_CONFIG_FILE */
26  #if !defined(LWS_AMAZON_RTOS)
27   #undef MBEDTLS_CONFIG_FILE
28   #define MBEDTLS_CONFIG_FILE <mbedtls/esp_config.h>
29  #endif
30 #endif
31 
32 #include "ssl_code.h"
33 
34 typedef void SSL_CIPHER;
35 
36 typedef void X509_STORE_CTX;
37 typedef void X509_STORE;
38 
39 typedef void RSA;
40 
41 typedef void STACK;
42 typedef void BIO;
43 
44 #if defined(WIN32) || defined(_WIN32)
45 #define ossl_inline __inline
46 #else
47 #define ossl_inline inline
48 #endif
49 
50 #define SSL_METHOD_CALL(f, s, ...)        s->method->func->ssl_##f(s, ##__VA_ARGS__)
51 #define X509_METHOD_CALL(f, x, ...)       x->method->x509_##f(x, ##__VA_ARGS__)
52 #define EVP_PKEY_METHOD_CALL(f, k, ...)   k->method->pkey_##f(k, ##__VA_ARGS__)
53 
54 typedef int (*OPENSSL_sk_compfunc)(const void *, const void *);
55 
56 struct stack_st;
57 typedef struct stack_st OPENSSL_STACK;
58 
59 struct ssl_method_st;
60 typedef struct ssl_method_st SSL_METHOD;
61 
62 struct ssl_method_func_st;
63 typedef struct ssl_method_func_st SSL_METHOD_FUNC;
64 
65 struct record_layer_st;
66 typedef struct record_layer_st RECORD_LAYER;
67 
68 struct ossl_statem_st;
69 typedef struct ossl_statem_st OSSL_STATEM;
70 
71 struct ssl_session_st;
72 typedef struct ssl_session_st SSL_SESSION;
73 
74 struct ssl_ctx_st;
75 typedef struct ssl_ctx_st SSL_CTX;
76 
77 struct ssl_st;
78 typedef struct ssl_st SSL;
79 
80 struct cert_st;
81 typedef struct cert_st CERT;
82 
83 struct x509_st;
84 typedef struct x509_st X509;
85 
86 struct X509_VERIFY_PARAM_st;
87 typedef struct X509_VERIFY_PARAM_st X509_VERIFY_PARAM;
88 
89 struct evp_pkey_st;
90 typedef struct evp_pkey_st EVP_PKEY;
91 
92 struct x509_method_st;
93 typedef struct x509_method_st X509_METHOD;
94 
95 struct pkey_method_st;
96 typedef struct pkey_method_st PKEY_METHOD;
97 
98 struct stack_st {
99 
100     char **data;
101 
102     int num_alloc;
103 
104     OPENSSL_sk_compfunc c;
105 };
106 
107 struct evp_pkey_st {
108 
109     void *pkey_pm;
110 
111     const PKEY_METHOD *method;
112 };
113 
114 struct x509_st {
115 
116     /* X509 certification platform private point */
117     void *x509_pm;
118 
119     const X509_METHOD *method;
120 };
121 
122 struct cert_st {
123 
124     int sec_level;
125 
126     X509 *x509;
127 
128     EVP_PKEY *pkey;
129 
130 };
131 
132 struct ossl_statem_st {
133 
134     MSG_FLOW_STATE state;
135 
136     int hand_state;
137 };
138 
139 struct record_layer_st {
140 
141     int rstate;
142 
143     int read_ahead;
144 };
145 
146 struct ssl_session_st {
147 
148     long timeout;
149 
150     long time;
151 
152     X509 *peer;
153 };
154 
155 struct X509_VERIFY_PARAM_st {
156 
157     int depth;
158 
159 };
160 
161 typedef int (*next_proto_cb)(SSL *ssl, const unsigned char **out,
162                              unsigned char *outlen, const unsigned char *in,
163                              unsigned int inlen, void *arg);
164 
165 
166 struct ssl_ctx_st
167 {
168     int version;
169 
170     int references;
171 
172     unsigned long options;
173 
174     const SSL_METHOD *method;
175 
176     CERT *cert;
177 
178     X509 *client_CA;
179 
180     const char **alpn_protos;
181 
182     next_proto_cb alpn_cb;
183 
184     int verify_mode;
185 
186     int (*default_verify_callback) (int ok, X509_STORE_CTX *ctx);
187 
188     long session_timeout;
189 
190     int read_ahead;
191 
192     int read_buffer_len;
193 
194     X509_VERIFY_PARAM param;
195 };
196 
197 struct ssl_st
198 {
199     /* protocol version(one of SSL3.0, TLS1.0, etc.) */
200     int version;
201 
202     unsigned long options;
203 
204     /* shut things down(0x01 : sent, 0x02 : received) */
205     int shutdown;
206 
207     CERT *cert;
208 
209     X509 *client_CA;
210 
211     SSL_CTX  *ctx;
212 
213     const SSL_METHOD *method;
214 
215     const char **alpn_protos;
216 
217     RECORD_LAYER rlayer;
218 
219     /* where we are */
220     OSSL_STATEM statem;
221 
222     SSL_SESSION *session;
223 
224     int verify_mode;
225 
226     int (*verify_callback) (int ok, X509_STORE_CTX *ctx);
227 
228     int rwstate;
229     int interrupted_remaining_write;
230 
231     long verify_result;
232 
233     X509_VERIFY_PARAM param;
234 
235     int err;
236 
237     void (*info_callback) (const SSL *ssl, int type, int val);
238 
239     /* SSL low-level system arch point */
240     void *ssl_pm;
241 };
242 
243 struct ssl_method_st {
244     /* protocol version(one of SSL3.0, TLS1.0, etc.) */
245     int version;
246 
247     /* SSL mode(client(0) , server(1), not known(-1)) */
248     int endpoint;
249 
250     const SSL_METHOD_FUNC *func;
251 };
252 
253 struct ssl_method_func_st {
254 
255     int (*ssl_new)(SSL *ssl);
256 
257     void (*ssl_free)(SSL *ssl);
258 
259     int (*ssl_handshake)(SSL *ssl);
260 
261     int (*ssl_shutdown)(SSL *ssl);
262 
263     int (*ssl_clear)(SSL *ssl);
264 
265     int (*ssl_read)(SSL *ssl, void *buffer, int len);
266 
267     int (*ssl_send)(SSL *ssl, const void *buffer, int len);
268 
269     int (*ssl_pending)(const SSL *ssl);
270 
271     void (*ssl_set_fd)(SSL *ssl, int fd, int mode);
272 
273     int (*ssl_get_fd)(const SSL *ssl, int mode);
274 
275     void (*ssl_set_bufflen)(SSL *ssl, int len);
276 
277     long (*ssl_get_verify_result)(const SSL *ssl);
278 
279     OSSL_HANDSHAKE_STATE (*ssl_get_state)(const SSL *ssl);
280 };
281 
282 struct x509_method_st {
283 
284     int (*x509_new)(X509 *x, X509 *m_x);
285 
286     void (*x509_free)(X509 *x);
287 
288     int (*x509_load)(X509 *x, const unsigned char *buf, int len);
289 
290     int (*x509_show_info)(X509 *x);
291 };
292 
293 struct pkey_method_st {
294 
295     int (*pkey_new)(EVP_PKEY *pkey, EVP_PKEY *m_pkey);
296 
297     void (*pkey_free)(EVP_PKEY *pkey);
298 
299     int (*pkey_load)(EVP_PKEY *pkey, const unsigned char *buf, int len);
300 };
301 
302 #define OPENSSL_NPN_NEGOTIATED 1
303 
304 int X509_STORE_CTX_get_error(X509_STORE_CTX *ctx);
305 int X509_STORE_CTX_get_error_depth(X509_STORE_CTX *ctx);
306 
307 #ifdef __cplusplus
308 }
309 #endif
310 
311 #endif
312