Merge pull request #278 from nmav/ocserv
[project/luci.git] / libs / nixio / axTLS / ssl / openssl.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 #ifdef CONFIG_OPENSSL_COMPATIBLE
42 #include <stdlib.h>
43 #include <string.h>
44 #include <stdarg.h>
45 #include "ssl.h"
46
47 #define OPENSSL_CTX_ATTR ((OPENSSL_CTX *)ssl_ctx->bonus_attr)
48
49 static char *key_password = NULL;
50
51 void *SSLv23_server_method(void) { return NULL; }
52 void *SSLv3_server_method(void) { return NULL; }
53 void *TLSv1_server_method(void) { return NULL; }
54 void *SSLv23_client_method(void) { return NULL; }
55 void *SSLv3_client_method(void) { return NULL; }
56 void *TLSv1_client_method(void) { return NULL; }
57
58 typedef void * (*ssl_func_type_t)(void);
59 typedef void * (*bio_func_type_t)(void);
60
61 typedef struct
62 {
63 ssl_func_type_t ssl_func_type;
64 } OPENSSL_CTX;
65
66 SSL_CTX * SSL_CTX_new(ssl_func_type_t meth)
67 {
68 SSL_CTX *ssl_ctx = ssl_ctx_new(0, 5);
69 ssl_ctx->bonus_attr = malloc(sizeof(OPENSSL_CTX));
70 OPENSSL_CTX_ATTR->ssl_func_type = meth;
71 return ssl_ctx;
72 }
73
74 void SSL_CTX_free(SSL_CTX * ssl_ctx)
75 {
76 free(ssl_ctx->bonus_attr);
77 ssl_ctx_free(ssl_ctx);
78 }
79
80 SSL * SSL_new(SSL_CTX *ssl_ctx)
81 {
82 SSL *ssl;
83 ssl_func_type_t ssl_func_type;
84
85 ssl = ssl_new(ssl_ctx, -1); /* fd is set later */
86 ssl_func_type = OPENSSL_CTX_ATTR->ssl_func_type;
87
88 #ifdef CONFIG_SSL_ENABLE_CLIENT
89 if (ssl_func_type == SSLv23_client_method ||
90 ssl_func_type == SSLv3_client_method ||
91 ssl_func_type == TLSv1_client_method)
92 {
93 SET_SSL_FLAG(SSL_IS_CLIENT);
94 }
95 else
96 #endif
97 {
98 ssl->next_state = HS_CLIENT_HELLO;
99 }
100
101 return ssl;
102 }
103
104 int SSL_set_fd(SSL *s, int fd)
105 {
106 s->client_fd = fd;
107 return 1; /* always succeeds */
108 }
109
110 int SSL_accept(SSL *ssl)
111 {
112 while (ssl_read(ssl, NULL) == SSL_OK)
113 {
114 if (ssl->next_state == HS_CLIENT_HELLO)
115 return 1; /* we're done */
116 }
117
118 return -1;
119 }
120
121 #ifdef CONFIG_SSL_ENABLE_CLIENT
122 int SSL_connect(SSL *ssl)
123 {
124 return do_client_connect(ssl) == SSL_OK ? 1 : -1;
125 }
126 #endif
127
128 void SSL_free(SSL *ssl)
129 {
130 ssl_free(ssl);
131 }
132
133 int SSL_read(SSL *ssl, void *buf, int num)
134 {
135 uint8_t *read_buf;
136 int ret;
137
138 while ((ret = ssl_read(ssl, &read_buf)) == SSL_OK);
139
140 if (ret > SSL_OK)
141 {
142 memcpy(buf, read_buf, ret > num ? num : ret);
143 }
144
145 return ret;
146 }
147
148 int SSL_write(SSL *ssl, const void *buf, int num)
149 {
150 return ssl_write(ssl, buf, num);
151 }
152
153 int SSL_CTX_use_certificate_file(SSL_CTX *ssl_ctx, const char *file, int type)
154 {
155 return (ssl_obj_load(ssl_ctx, SSL_OBJ_X509_CERT, file, NULL) == SSL_OK);
156 }
157
158 int SSL_CTX_use_PrivateKey_file(SSL_CTX *ssl_ctx, const char *file, int type)
159 {
160 return (ssl_obj_load(ssl_ctx, SSL_OBJ_RSA_KEY, file, key_password) == SSL_OK);
161 }
162
163 int SSL_CTX_use_certificate_ASN1(SSL_CTX *ssl_ctx, int len, const uint8_t *d)
164 {
165 return (ssl_obj_memory_load(ssl_ctx,
166 SSL_OBJ_X509_CERT, d, len, NULL) == SSL_OK);
167 }
168
169 int SSL_CTX_set_session_id_context(SSL_CTX *ctx, const unsigned char *sid_ctx,
170 unsigned int sid_ctx_len)
171 {
172 return 1;
173 }
174
175 int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx)
176 {
177 return 1;
178 }
179
180 int SSL_CTX_use_certificate_chain_file(SSL_CTX *ssl_ctx, const char *file)
181 {
182 return (ssl_obj_load(ssl_ctx,
183 SSL_OBJ_X509_CERT, file, NULL) == SSL_OK);
184 }
185
186 int SSL_shutdown(SSL *ssl)
187 {
188 return 1;
189 }
190
191 /*** get/set session ***/
192 SSL_SESSION *SSL_get1_session(SSL *ssl)
193 {
194 return (SSL_SESSION *)ssl_get_session_id(ssl); /* note: wrong cast */
195 }
196
197 int SSL_set_session(SSL *ssl, SSL_SESSION *session)
198 {
199 memcpy(ssl->session_id, (uint8_t *)session, SSL_SESSION_ID_SIZE);
200 return 1;
201 }
202
203 void SSL_SESSION_free(SSL_SESSION *session) { }
204 /*** end get/set session ***/
205
206 long SSL_CTX_ctrl(SSL_CTX *ctx, int cmd, long larg, void *parg)
207 {
208 return 0;
209 }
210
211 void SSL_CTX_set_verify(SSL_CTX *ctx, int mode,
212 int (*verify_callback)(int, void *)) { }
213
214 void SSL_CTX_set_verify_depth(SSL_CTX *ctx,int depth) { }
215
216 int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile,
217 const char *CApath)
218 {
219 return 1;
220 }
221
222 void *SSL_load_client_CA_file(const char *file)
223 {
224 return (void *)file;
225 }
226
227 void SSL_CTX_set_client_CA_list(SSL_CTX *ssl_ctx, void *file)
228 {
229
230 ssl_obj_load(ssl_ctx, SSL_OBJ_X509_CERT, (const char *)file, NULL);
231 }
232
233 void SSLv23_method(void) { }
234
235 void SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, void *cb) { }
236
237 void SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx, void *u)
238 {
239 key_password = (char *)u;
240 }
241
242 int SSL_peek(SSL *ssl, void *buf, int num)
243 {
244 memcpy(buf, ssl->bm_data, num);
245 return num;
246 }
247
248 void SSL_set_bio(SSL *ssl, void *rbio, void *wbio) { }
249
250 long SSL_get_verify_result(const SSL *ssl)
251 {
252 return ssl_handshake_status(ssl);
253 }
254
255 int SSL_state(SSL *ssl)
256 {
257 return 0x03; // ok state
258 }
259
260 /** end of could do better list */
261
262 void *SSL_get_peer_certificate(const SSL *ssl)
263 {
264 return &ssl->ssl_ctx->certs[0];
265 }
266
267 int SSL_clear(SSL *ssl)
268 {
269 return 1;
270 }
271
272
273 int SSL_CTX_check_private_key(const SSL_CTX *ctx)
274 {
275 return 1;
276 }
277
278 int SSL_CTX_set_cipher_list(SSL *s, const char *str)
279 {
280 return 1;
281 }
282
283 int SSL_get_error(const SSL *ssl, int ret)
284 {
285 ssl_display_error(ret);
286 return 0; /* TODO: return proper return code */
287 }
288
289 void SSL_CTX_set_options(SSL_CTX *ssl_ctx, int option) {}
290 int SSL_library_init(void ) { return 1; }
291 void SSL_load_error_strings(void ) {}
292 void ERR_print_errors_fp(FILE *fp) {}
293
294 #ifndef CONFIG_SSL_SKELETON_MODE
295 long SSL_CTX_get_timeout(const SSL_CTX *ssl_ctx) {
296 return CONFIG_SSL_EXPIRY_TIME*3600; }
297 long SSL_CTX_set_timeout(SSL_CTX *ssl_ctx, long t) {
298 return SSL_CTX_get_timeout(ssl_ctx); }
299 #endif
300 void BIO_printf(FILE *f, const char *format, ...)
301 {
302 va_list(ap);
303 va_start(ap, format);
304 vfprintf(f, format, ap);
305 va_end(ap);
306 }
307
308 void* BIO_s_null(void) { return NULL; }
309 FILE *BIO_new(bio_func_type_t func)
310 {
311 if (func == BIO_s_null)
312 return fopen("/dev/null", "r");
313 else
314 return NULL;
315 }
316
317 FILE *BIO_new_fp(FILE *stream, int close_flag) { return stream; }
318 int BIO_free(FILE *a) { if (a != stdout && a != stderr) fclose(a); return 1; }
319
320
321
322 #endif