2 * ustream-ssl - library for SSL over ustream
4 * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include "ustream-ssl.h"
20 #include "ustream-internal.h"
22 __hidden
struct ustream_ssl_ctx
*
23 __ustream_ssl_context_new(bool server
)
25 static bool _init
= false;
30 SSL_load_error_strings();
35 #ifdef CYASSL_OPENSSL_H_
37 m
= SSLv23_server_method();
39 m
= SSLv23_client_method();
42 m
= TLSv1_server_method();
44 m
= TLSv1_client_method();
47 c
= SSL_CTX_new((void *) m
);
52 SSL_CTX_set_verify(c
, SSL_VERIFY_NONE
, NULL
);
57 __hidden
int __ustream_ssl_set_crt_file(struct ustream_ssl_ctx
*ctx
, const char *file
)
61 ret
= SSL_CTX_use_certificate_file((void *) ctx
, file
, SSL_FILETYPE_PEM
);
63 ret
= SSL_CTX_use_certificate_file((void *) ctx
, file
, SSL_FILETYPE_ASN1
);
71 __hidden
int __ustream_ssl_set_key_file(struct ustream_ssl_ctx
*ctx
, const char *file
)
75 ret
= SSL_CTX_use_PrivateKey_file((void *) ctx
, file
, SSL_FILETYPE_PEM
);
77 ret
= SSL_CTX_use_PrivateKey_file((void *) ctx
, file
, SSL_FILETYPE_ASN1
);
85 __hidden
void __ustream_ssl_context_free(struct ustream_ssl_ctx
*ctx
)
87 SSL_CTX_free((void *) ctx
);
90 static void ustream_ssl_error(struct ustream_ssl
*us
, int ret
)
93 uloop_timeout_set(&us
->error_timer
, 0);
96 __hidden
enum ssl_conn_status
__ustream_ssl_connect(struct ustream_ssl
*us
)
104 r
= SSL_connect(ssl
);
109 r
= SSL_get_error(ssl
, r
);
110 if (r
== SSL_ERROR_WANT_READ
|| r
== SSL_ERROR_WANT_WRITE
)
111 return U_SSL_PENDING
;
113 ustream_ssl_error(us
, r
);
117 __hidden
int __ustream_ssl_write(struct ustream_ssl
*us
, const char *buf
, int len
)
120 int ret
= SSL_write(ssl
, buf
, len
);
123 int err
= SSL_get_error(ssl
, ret
);
124 if (err
== SSL_ERROR_WANT_WRITE
)
127 ustream_ssl_error(us
, err
);
134 __hidden
int __ustream_ssl_read(struct ustream_ssl
*us
, char *buf
, int len
)
136 int ret
= SSL_read(us
->ssl
, buf
, len
);
139 ret
= SSL_get_error(us
->ssl
, ret
);
140 if (ret
== SSL_ERROR_WANT_READ
)
141 return U_SSL_PENDING
;
143 ustream_ssl_error(us
, ret
);