add mbedtls variant
[project/ustream-ssl.git] / ustream-ssl.c
index 4526db0..dd0faf9 100644 (file)
@@ -17,6 +17,8 @@
  */
 
 #include <errno.h>
+#include <stdlib.h>
+#include <string.h>
 #include <libubox/ustream.h>
 
 #include "ustream-ssl.h"
@@ -41,6 +43,7 @@ static void ustream_ssl_check_conn(struct ustream_ssl *us)
                us->connected = true;
                if (us->notify_connected)
                        us->notify_connected(us);
+               ustream_write_pending(&us->stream);
        }
 }
 
@@ -132,11 +135,16 @@ static void ustream_ssl_free(struct ustream *s)
 
        uloop_timeout_cancel(&us->error_timer);
        __ustream_ssl_session_free(us->ssl);
+       free(us->peer_cn);
+
        us->ctx = NULL;
        us->ssl = NULL;
        us->conn = NULL;
+       us->peer_cn = NULL;
        us->connected = false;
        us->error = false;
+       us->valid_cert = false;
+       us->valid_cn = false;
 }
 
 static bool ustream_ssl_poll(struct ustream *s)
@@ -145,7 +153,7 @@ static bool ustream_ssl_poll(struct ustream *s)
        bool fd_poll;
 
        fd_poll = ustream_poll(us->conn);
-       return __ustream_ssl_poll(s) || fd_poll;
+       return __ustream_ssl_poll(us->conn) || fd_poll;
 }
 
 static void ustream_ssl_stream_init(struct ustream_ssl *us)
@@ -178,15 +186,29 @@ static int _ustream_ssl_init(struct ustream_ssl *us, struct ustream *conn, struc
        conn->next = &us->stream;
        ustream_set_io(ctx, us->ssl, conn);
        ustream_ssl_stream_init(us);
+
+       if (us->server_name)
+               __ustream_ssl_set_server_name(us);
+
        ustream_ssl_check_conn(us);
 
        return 0;
 }
 
+static int _ustream_ssl_set_peer_cn(struct ustream_ssl *us, const char *name)
+{
+       us->peer_cn = strdup(name);
+       __ustream_ssl_update_peer_cn(us);
+
+       return 0;
+}
+
 const struct ustream_ssl_ops ustream_ssl_ops = {
        .context_new = __ustream_ssl_context_new,
        .context_set_crt_file = __ustream_ssl_set_crt_file,
        .context_set_key_file = __ustream_ssl_set_key_file,
+       .context_add_ca_crt_file = __ustream_ssl_add_ca_crt_file,
        .context_free = __ustream_ssl_context_free,
        .init = _ustream_ssl_init,
+       .set_peer_cn = _ustream_ssl_set_peer_cn,
 };