nixio: Improve number handling with non-double Lua setups
[project/luci.git] / libs / nixio / src / axtls-compat.c
1 /*
2 * Copyright (c) 2007, Cameron Rich
3 *
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * * Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 * * Neither the name of the axTLS project nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
22 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 /*
32 * Enable a subset of openssl compatible functions. We don't aim to be 100%
33 * compatible - just to be able to do basic ports etc.
34 *
35 * Only really tested on mini_httpd, so I'm not too sure how extensive this
36 * port is.
37 */
38
39 #include "config.h"
40
41 #include <stdlib.h>
42 #include <string.h>
43 #include <stdarg.h>
44 #include "ssl.h"
45
46 static char *key_password = NULL;
47
48 void *SSLv23_server_method(void) { return NULL; }
49 void *SSLv3_server_method(void) { return NULL; }
50 void *TLSv1_server_method(void) { return NULL; }
51 void *SSLv23_client_method(void) { return NULL; }
52 void *SSLv3_client_method(void) { return NULL; }
53 void *TLSv1_client_method(void) { return NULL; }
54 void *SSLv23_method(void) { return NULL; }
55 void *TLSv1_method(void) { return NULL; }
56
57 SSL_CTX * SSL_CTX_new(void *meth)
58 {
59 SSL_CTX *ssl_ctx = ssl_ctx_new(SSL_SERVER_VERIFY_LATER, 5);
60 return ssl_ctx;
61 }
62
63 void SSL_CTX_free(SSL_CTX * ssl_ctx)
64 {
65 ssl_ctx_free(ssl_ctx);
66 }
67
68 SSL * SSL_new(SSL_CTX *ssl_ctx)
69 {
70 SSL *ssl;
71
72 ssl = ssl_new(ssl_ctx, -1); /* fd is set later */
73
74 return ssl;
75 }
76
77 int SSL_set_fd(SSL *s, int fd)
78 {
79 s->client_fd = fd;
80 return 1; /* always succeeds */
81 }
82
83 int SSL_accept(SSL *ssl)
84 {
85 ssl->next_state = HS_CLIENT_HELLO;
86 while (ssl_read(ssl, NULL) == SSL_OK)
87 {
88 if (ssl->next_state == HS_CLIENT_HELLO)
89 return 1; /* we're done */
90 }
91
92 return -1;
93 }
94
95 int SSL_connect(SSL *ssl)
96 {
97 SET_SSL_FLAG(SSL_IS_CLIENT);
98 int stat = do_client_connect(ssl);
99 ssl_display_error(stat);
100 return (stat == SSL_OK) ? 1 : -1;
101 }
102
103 void SSL_free(SSL *ssl)
104 {
105 ssl_free(ssl);
106 }
107
108 int SSL_read(SSL *ssl, void *buf, int num)
109 {
110 uint8_t *read_buf;
111 int ret;
112
113 while ((ret = ssl_read(ssl, &read_buf)) == SSL_OK);
114
115 if (ret > SSL_OK)
116 {
117 memcpy(buf, read_buf, ret > num ? num : ret);
118 }
119
120 return ret;
121 }
122
123 int SSL_write(SSL *ssl, const void *buf, int num)
124 {
125 return ssl_write(ssl, buf, num);
126 }
127
128 int SSL_CTX_use_certificate_file(SSL_CTX *ssl_ctx, const char *file, int type)
129 {
130 return (ssl_obj_load(ssl_ctx, SSL_OBJ_X509_CERT, file, NULL) == SSL_OK);
131 }
132
133 int SSL_CTX_use_PrivateKey_file(SSL_CTX *ssl_ctx, const char *file, int type)
134 {
135 return (ssl_obj_load(ssl_ctx, SSL_OBJ_RSA_KEY, file, key_password) == SSL_OK);
136 }
137
138 int SSL_CTX_use_certificate_ASN1(SSL_CTX *ssl_ctx, int len, const uint8_t *d)
139 {
140 return (ssl_obj_memory_load(ssl_ctx,
141 SSL_OBJ_X509_CERT, d, len, NULL) == SSL_OK);
142 }
143
144 int SSL_CTX_set_session_id_context(SSL_CTX *ctx, const unsigned char *sid_ctx,
145 unsigned int sid_ctx_len)
146 {
147 return 1;
148 }
149
150 int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx)
151 {
152 return 1;
153 }
154
155 int SSL_CTX_use_certificate_chain_file(SSL_CTX *ssl_ctx, const char *file)
156 {
157 return (ssl_obj_load(ssl_ctx,
158 SSL_OBJ_X509_CERT, file, NULL) == SSL_OK);
159 }
160
161 int SSL_shutdown(SSL *ssl)
162 {
163 return 1;
164 }
165
166 /*** get/set session ***/
167 SSL_SESSION *SSL_get1_session(SSL *ssl)
168 {
169 return (SSL_SESSION *)ssl_get_session_id(ssl); /* note: wrong cast */
170 }
171
172 int SSL_set_session(SSL *ssl, SSL_SESSION *session)
173 {
174 memcpy(ssl->session_id, (uint8_t *)session, SSL_SESSION_ID_SIZE);
175 return 1;
176 }
177
178 void SSL_SESSION_free(SSL_SESSION *session) { }
179 /*** end get/set session ***/
180
181 long SSL_CTX_ctrl(SSL_CTX *ctx, int cmd, long larg, void *parg)
182 {
183 return 0;
184 }
185
186 void SSL_CTX_set_verify(SSL_CTX *ctx, int mode,
187 int (*verify_callback)(int, void *)) {
188 if (mode & SSL_VERIFY_PEER) {
189 ctx->options &= ~SSL_SERVER_VERIFY_LATER;
190 ctx->options |= SSL_CLIENT_AUTHENTICATION;
191 } else {
192 ctx->options |= SSL_SERVER_VERIFY_LATER;
193 ctx->options &= ~SSL_CLIENT_AUTHENTICATION;
194 }
195 }
196
197 void SSL_CTX_set_verify_depth(SSL_CTX *ctx,int depth) { }
198
199 int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile,
200 const char *CApath)
201 {
202 return 1;
203 }
204
205 void *SSL_load_client_CA_file(const char *file)
206 {
207 return (void *)file;
208 }
209
210 void SSL_CTX_set_client_CA_list(SSL_CTX *ssl_ctx, void *file)
211 {
212
213 ssl_obj_load(ssl_ctx, SSL_OBJ_X509_CERT, (const char *)file, NULL);
214 }
215
216 void SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, void *cb) { }
217
218 void SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx, void *u)
219 {
220 key_password = (char *)u;
221 }
222
223 int SSL_peek(SSL *ssl, void *buf, int num)
224 {
225 memcpy(buf, ssl->bm_data, num);
226 return num;
227 }
228
229 void SSL_set_bio(SSL *ssl, void *rbio, void *wbio) { }
230
231 long SSL_get_verify_result(const SSL *ssl)
232 {
233 return ssl_handshake_status(ssl);
234 }
235
236 int SSL_state(SSL *ssl)
237 {
238 return 0x03; // ok state
239 }
240
241 /** end of could do better list */
242
243 void *SSL_get_peer_certificate(const SSL *ssl)
244 {
245 return &ssl->ssl_ctx->certs[0];
246 }
247
248 int SSL_clear(SSL *ssl)
249 {
250 return 1;
251 }
252
253
254 int SSL_CTX_check_private_key(const SSL_CTX *ctx)
255 {
256 return 1;
257 }
258
259 int SSL_CTX_set_cipher_list(SSL_CTX *s, const char *str)
260 {
261 return 1;
262 }
263
264 int SSL_get_error(const SSL *ssl, int ret)
265 {
266 ssl_display_error(ret);
267 return ret; /* TODO: return proper return code */
268 }
269
270 void SSL_CTX_set_options(SSL_CTX *ssl_ctx, int option) {}
271 int SSL_library_init(void ) { return 1; }
272 void SSL_load_error_strings(void ) {}
273 void ERR_print_errors_fp(FILE *fp) {}
274
275 long SSL_CTX_get_timeout(const SSL_CTX *ssl_ctx) {
276 return CONFIG_SSL_EXPIRY_TIME*3600; }
277 long SSL_CTX_set_timeout(SSL_CTX *ssl_ctx, long t) {
278 return SSL_CTX_get_timeout(ssl_ctx); }
279 void BIO_printf(FILE *f, const char *format, ...)
280 {
281 va_list(ap);
282 va_start(ap, format);
283 vfprintf(f, format, ap);
284 va_end(ap);
285 }
286
287 void* BIO_s_null(void) { return NULL; }
288 FILE *BIO_new(bio_func_type_t func)
289 {
290 if (func == BIO_s_null)
291 return fopen("/dev/null", "r");
292 else
293 return NULL;
294 }
295
296 FILE *BIO_new_fp(FILE *stream, int close_flag) { return stream; }
297 int BIO_free(FILE *a) { if (a != stdout && a != stderr) fclose(a); return 1; }