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
46 static char *key_password = NULL;
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; }
57 SSL_CTX * SSL_CTX_new(void *meth)
59 SSL_CTX *ssl_ctx = ssl_ctx_new(SSL_SERVER_VERIFY_LATER, 5);
63 void SSL_CTX_free(SSL_CTX * ssl_ctx)
65 ssl_ctx_free(ssl_ctx);
68 SSL * SSL_new(SSL_CTX *ssl_ctx)
72 ssl = ssl_new(ssl_ctx, -1); /* fd is set later */
77 int SSL_set_fd(SSL *s, int fd)
80 return 1; /* always succeeds */
83 int SSL_accept(SSL *ssl)
85 ssl->next_state = HS_CLIENT_HELLO;
86 while (ssl_read(ssl, NULL) == SSL_OK)
88 if (ssl->next_state == HS_CLIENT_HELLO)
89 return 1; /* we're done */
95 int SSL_connect(SSL *ssl)
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;
103 void SSL_free(SSL *ssl)
108 int SSL_read(SSL *ssl, void *buf, int num)
113 while ((ret = ssl_read(ssl, &read_buf)) == SSL_OK);
117 memcpy(buf, read_buf, ret > num ? num : ret);
123 int SSL_write(SSL *ssl, const void *buf, int num)
125 return ssl_write(ssl, buf, num);
128 int SSL_CTX_use_certificate_file(SSL_CTX *ssl_ctx, const char *file, int type)
130 return (ssl_obj_load(ssl_ctx, SSL_OBJ_X509_CERT, file, NULL) == SSL_OK);
133 int SSL_CTX_use_PrivateKey_file(SSL_CTX *ssl_ctx, const char *file, int type)
135 return (ssl_obj_load(ssl_ctx, SSL_OBJ_RSA_KEY, file, key_password) == SSL_OK);
138 int SSL_CTX_use_certificate_ASN1(SSL_CTX *ssl_ctx, int len, const uint8_t *d)
140 return (ssl_obj_memory_load(ssl_ctx,
141 SSL_OBJ_X509_CERT, d, len, NULL) == SSL_OK);
144 int SSL_CTX_set_session_id_context(SSL_CTX *ctx, const unsigned char *sid_ctx,
145 unsigned int sid_ctx_len)
150 int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx)
155 int SSL_CTX_use_certificate_chain_file(SSL_CTX *ssl_ctx, const char *file)
157 return (ssl_obj_load(ssl_ctx,
158 SSL_OBJ_X509_CERT, file, NULL) == SSL_OK);
161 int SSL_shutdown(SSL *ssl)
166 /*** get/set session ***/
167 SSL_SESSION *SSL_get1_session(SSL *ssl)
169 return (SSL_SESSION *)ssl_get_session_id(ssl); /* note: wrong cast */
172 int SSL_set_session(SSL *ssl, SSL_SESSION *session)
174 memcpy(ssl->session_id, (uint8_t *)session, SSL_SESSION_ID_SIZE);
178 void SSL_SESSION_free(SSL_SESSION *session) { }
179 /*** end get/set session ***/
181 long SSL_CTX_ctrl(SSL_CTX *ctx, int cmd, long larg, void *parg)
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;
192 ctx->options |= SSL_SERVER_VERIFY_LATER;
193 ctx->options &= ~SSL_CLIENT_AUTHENTICATION;
197 void SSL_CTX_set_verify_depth(SSL_CTX *ctx,int depth) { }
199 int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile,
205 void *SSL_load_client_CA_file(const char *file)
210 void SSL_CTX_set_client_CA_list(SSL_CTX *ssl_ctx, void *file)
213 ssl_obj_load(ssl_ctx, SSL_OBJ_X509_CERT, (const char *)file, NULL);
216 void SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, void *cb) { }
218 void SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx, void *u)
220 key_password = (char *)u;
223 int SSL_peek(SSL *ssl, void *buf, int num)
225 memcpy(buf, ssl->bm_data, num);
229 void SSL_set_bio(SSL *ssl, void *rbio, void *wbio) { }
231 long SSL_get_verify_result(const SSL *ssl)
233 return ssl_handshake_status(ssl);
236 int SSL_state(SSL *ssl)
238 return 0x03; // ok state
241 /** end of could do better list */
243 void *SSL_get_peer_certificate(const SSL *ssl)
245 return &ssl->ssl_ctx->certs[0];
248 int SSL_clear(SSL *ssl)
254 int SSL_CTX_check_private_key(const SSL_CTX *ctx)
259 int SSL_CTX_set_cipher_list(SSL_CTX *s, const char *str)
264 int SSL_get_error(const SSL *ssl, int ret)
266 ssl_display_error(ret);
267 return ret; /* TODO: return proper return code */
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) {}
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, ...)
282 va_start(ap, format);
283 vfprintf(f, format, ap);
287 void* BIO_s_null(void) { return NULL; }
288 FILE *BIO_new(bio_func_type_t func)
290 if (func == BIO_s_null)
291 return fopen("/dev/null", "r");
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; }