ustream-openssl: clear error stack before SSL_read/SSL_write
[project/ustream-ssl.git] / ustream-openssl.c
index c8306184d2dff71a99aaa85ed46577c123158890..f8e848d69fb3244ff296375288d9de62984c93df 100644 (file)
@@ -110,8 +110,12 @@ __ustream_ssl_context_new(bool server)
                SSL_library_init();
                _init = true;
        }
-# define TLS_server_method SSLv23_server_method
-# define TLS_client_method SSLv23_client_method
+# ifndef TLS_server_method
+#  define TLS_server_method SSLv23_server_method
+# endif
+# ifndef TLS_client_method
+#  define TLS_client_method SSLv23_client_method
+# endif
 #endif
 
        if (server) {
@@ -186,6 +190,16 @@ __hidden int __ustream_ssl_set_key_file(struct ustream_ssl_ctx *ctx, const char
        return 0;
 }
 
+__hidden int __ustream_ssl_set_ciphers(struct ustream_ssl_ctx *ctx, const char *ciphers)
+{
+       int ret = SSL_CTX_set_cipher_list((void *) ctx, ciphers);
+
+       if (ret == 0)
+               return -1;
+
+       return 0;
+}
+
 __hidden void __ustream_ssl_context_free(struct ustream_ssl_ctx *ctx)
 {
        SSL_CTX_free((void *) ctx);
@@ -252,6 +266,8 @@ __hidden enum ssl_conn_status __ustream_ssl_connect(struct ustream_ssl *us)
        void *ssl = us->ssl;
        int r;
 
+       ERR_clear_error();
+
        if (us->server)
                r = SSL_accept(ssl);
        else
@@ -273,7 +289,11 @@ __hidden enum ssl_conn_status __ustream_ssl_connect(struct ustream_ssl *us)
 __hidden int __ustream_ssl_write(struct ustream_ssl *us, const char *buf, int len)
 {
        void *ssl = us->ssl;
-       int ret = SSL_write(ssl, buf, len);
+       int ret;
+
+       ERR_clear_error();
+
+       ret = SSL_write(ssl, buf, len);
 
        if (ret < 0) {
                int err = SSL_get_error(ssl, ret);
@@ -289,7 +309,11 @@ __hidden int __ustream_ssl_write(struct ustream_ssl *us, const char *buf, int le
 
 __hidden int __ustream_ssl_read(struct ustream_ssl *us, char *buf, int len)
 {
-       int ret = SSL_read(us->ssl, buf, len);
+       int ret;
+
+       ERR_clear_error();
+
+       ret = SSL_read(us->ssl, buf, len);
 
        if (ret < 0) {
                ret = SSL_get_error(us->ssl, ret);
@@ -302,4 +326,3 @@ __hidden int __ustream_ssl_read(struct ustream_ssl *us, char *buf, int len)
 
        return ret;
 }
-