2 * Copyright (c) 2007, Cameron Rich
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
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.
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.
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.
35 * Only really tested on mini_httpd, so I'm not too sure how extensive this
41 #ifdef CONFIG_OPENSSL_COMPATIBLE
47 #define OPENSSL_CTX_ATTR ((OPENSSL_CTX *)ssl_ctx->bonus_attr)
49 static char *key_password = NULL;
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; }
58 typedef void * (*ssl_func_type_t)(void);
59 typedef void * (*bio_func_type_t)(void);
63 ssl_func_type_t ssl_func_type;
66 SSL_CTX * SSL_CTX_new(ssl_func_type_t meth)
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;
74 void SSL_CTX_free(SSL_CTX * ssl_ctx)
76 free(ssl_ctx->bonus_attr);
77 ssl_ctx_free(ssl_ctx);
80 SSL * SSL_new(SSL_CTX *ssl_ctx)
83 ssl_func_type_t ssl_func_type;
85 ssl = ssl_new(ssl_ctx, -1); /* fd is set later */
86 ssl_func_type = OPENSSL_CTX_ATTR->ssl_func_type;
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)
93 SET_SSL_FLAG(SSL_IS_CLIENT);
98 ssl->next_state = HS_CLIENT_HELLO;
104 int SSL_set_fd(SSL *s, int fd)
107 return 1; /* always succeeds */
110 int SSL_accept(SSL *ssl)
112 while (ssl_read(ssl, NULL) == SSL_OK)
114 if (ssl->next_state == HS_CLIENT_HELLO)
115 return 1; /* we're done */
121 #ifdef CONFIG_SSL_ENABLE_CLIENT
122 int SSL_connect(SSL *ssl)
124 return do_client_connect(ssl) == SSL_OK ? 1 : -1;
128 void SSL_free(SSL *ssl)
133 int SSL_read(SSL *ssl, void *buf, int num)
138 while ((ret = ssl_read(ssl, &read_buf)) == SSL_OK);
142 memcpy(buf, read_buf, ret > num ? num : ret);
148 int SSL_write(SSL *ssl, const void *buf, int num)
150 return ssl_write(ssl, buf, num);
153 int SSL_CTX_use_certificate_file(SSL_CTX *ssl_ctx, const char *file, int type)
155 return (ssl_obj_load(ssl_ctx, SSL_OBJ_X509_CERT, file, NULL) == SSL_OK);
158 int SSL_CTX_use_PrivateKey_file(SSL_CTX *ssl_ctx, const char *file, int type)
160 return (ssl_obj_load(ssl_ctx, SSL_OBJ_RSA_KEY, file, key_password) == SSL_OK);
163 int SSL_CTX_use_certificate_ASN1(SSL_CTX *ssl_ctx, int len, const uint8_t *d)
165 return (ssl_obj_memory_load(ssl_ctx,
166 SSL_OBJ_X509_CERT, d, len, NULL) == SSL_OK);
169 int SSL_CTX_set_session_id_context(SSL_CTX *ctx, const unsigned char *sid_ctx,
170 unsigned int sid_ctx_len)
175 int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx)
180 int SSL_CTX_use_certificate_chain_file(SSL_CTX *ssl_ctx, const char *file)
182 return (ssl_obj_load(ssl_ctx,
183 SSL_OBJ_X509_CERT, file, NULL) == SSL_OK);
186 int SSL_shutdown(SSL *ssl)
191 /*** get/set session ***/
192 SSL_SESSION *SSL_get1_session(SSL *ssl)
194 return (SSL_SESSION *)ssl_get_session_id(ssl); /* note: wrong cast */
197 int SSL_set_session(SSL *ssl, SSL_SESSION *session)
199 memcpy(ssl->session_id, (uint8_t *)session, SSL_SESSION_ID_SIZE);
203 void SSL_SESSION_free(SSL_SESSION *session) { }
204 /*** end get/set session ***/
206 long SSL_CTX_ctrl(SSL_CTX *ctx, int cmd, long larg, void *parg)
211 void SSL_CTX_set_verify(SSL_CTX *ctx, int mode,
212 int (*verify_callback)(int, void *)) { }
214 void SSL_CTX_set_verify_depth(SSL_CTX *ctx,int depth) { }
216 int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile,
222 void *SSL_load_client_CA_file(const char *file)
227 void SSL_CTX_set_client_CA_list(SSL_CTX *ssl_ctx, void *file)
230 ssl_obj_load(ssl_ctx, SSL_OBJ_X509_CERT, (const char *)file, NULL);
233 void SSLv23_method(void) { }
235 void SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, void *cb) { }
237 void SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx, void *u)
239 key_password = (char *)u;
242 int SSL_peek(SSL *ssl, void *buf, int num)
244 memcpy(buf, ssl->bm_data, num);
248 void SSL_set_bio(SSL *ssl, void *rbio, void *wbio) { }
250 long SSL_get_verify_result(const SSL *ssl)
252 return ssl_handshake_status(ssl);
255 int SSL_state(SSL *ssl)
257 return 0x03; // ok state
260 /** end of could do better list */
262 void *SSL_get_peer_certificate(const SSL *ssl)
264 return &ssl->ssl_ctx->certs[0];
267 int SSL_clear(SSL *ssl)
273 int SSL_CTX_check_private_key(const SSL_CTX *ctx)
278 int SSL_CTX_set_cipher_list(SSL *s, const char *str)
283 int SSL_get_error(const SSL *ssl, int ret)
285 ssl_display_error(ret);
286 return 0; /* TODO: return proper return code */
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) {}
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); }
300 void BIO_printf(FILE *f, const char *format, ...)
303 va_start(ap, format);
304 vfprintf(f, format, ap);
308 void* BIO_s_null(void) { return NULL; }
309 FILE *BIO_new(bio_func_type_t func)
311 if (func == BIO_s_null)
312 return fopen("/dev/null", "r");
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; }