add mbedtls variant
[project/ustream-ssl.git] / ustream-polarssl.c
index 8516d7f..9ddf931 100644 (file)
@@ -20,6 +20,7 @@
 #include <fcntl.h>
 #include <unistd.h>
 #include <stdlib.h>
 #include <fcntl.h>
 #include <unistd.h>
 #include <stdlib.h>
+#include <string.h>
 
 #include "ustream-ssl.h"
 #include "ustream-internal.h"
 
 #include "ustream-ssl.h"
 #include "ustream-internal.h"
@@ -79,7 +80,9 @@ static bool urandom_init(void)
 
 static int _urandom(void *ctx, unsigned char *out, size_t len)
 {
 
 static int _urandom(void *ctx, unsigned char *out, size_t len)
 {
-       read(urandom_fd, out, len);
+       if (read(urandom_fd, out, len) < 0)
+               return POLARSSL_ERR_ENTROPY_SOURCE_FAILED;
+
        return 0;
 }
 
        return 0;
 }
 
@@ -95,32 +98,31 @@ __ustream_ssl_context_new(bool server)
        if (!ctx)
                return NULL;
 
        if (!ctx)
                return NULL;
 
-       ctx->auth = SSL_VERIFY_NONE;
        ctx->server = server;
        ctx->server = server;
-#ifdef USE_VERSION_1_3
        pk_init(&ctx->key);
        pk_init(&ctx->key);
-#else
-       rsa_init(&ctx->key, RSA_PKCS_V15, 0);
-#endif
 
        return ctx;
 }
 
 
        return ctx;
 }
 
+__hidden int __ustream_ssl_add_ca_crt_file(struct ustream_ssl_ctx *ctx, const char *file)
+{
+       int ret;
+
+       ret = x509_crt_parse_file(&ctx->ca_cert, file);
+       if (ret)
+               return -1;
+
+       return 0;
+}
+
 __hidden int __ustream_ssl_set_crt_file(struct ustream_ssl_ctx *ctx, const char *file)
 {
        int ret;
 
 __hidden int __ustream_ssl_set_crt_file(struct ustream_ssl_ctx *ctx, const char *file)
 {
        int ret;
 
-#ifdef USE_VERSION_1_3
        ret = x509_crt_parse_file(&ctx->cert, file);
        ret = x509_crt_parse_file(&ctx->cert, file);
-#else
-       ret = x509parse_crtfile(&ctx->cert, file);
-#endif
        if (ret)
                return -1;
 
        if (ret)
                return -1;
 
-       if (!ctx->server)
-               ctx->auth = SSL_VERIFY_OPTIONAL;
-
        return 0;
 }
 
        return 0;
 }
 
@@ -128,11 +130,7 @@ __hidden int __ustream_ssl_set_key_file(struct ustream_ssl_ctx *ctx, const char
 {
        int ret;
 
 {
        int ret;
 
-#ifdef USE_VERSION_1_3
        ret = pk_parse_keyfile(&ctx->key, file, NULL);
        ret = pk_parse_keyfile(&ctx->key, file, NULL);
-#else
-       ret = x509parse_keyfile(&ctx->key, file, NULL);
-#endif
        if (ret)
                return -1;
 
        if (ret)
                return -1;
 
@@ -141,13 +139,8 @@ __hidden int __ustream_ssl_set_key_file(struct ustream_ssl_ctx *ctx, const char
 
 __hidden void __ustream_ssl_context_free(struct ustream_ssl_ctx *ctx)
 {
 
 __hidden void __ustream_ssl_context_free(struct ustream_ssl_ctx *ctx)
 {
-#ifdef USE_VERSION_1_3
        pk_free(&ctx->key);
        x509_crt_free(&ctx->cert);
        pk_free(&ctx->key);
        x509_crt_free(&ctx->cert);
-#else
-       rsa_free(&ctx->key);
-       x509_free(&ctx->cert);
-#endif
        free(ctx);
 }
 
        free(ctx);
 }
 
@@ -168,14 +161,46 @@ static bool ssl_do_wait(int ret)
        }
 }
 
        }
 }
 
+static void ustream_ssl_verify_cert(struct ustream_ssl *us)
+{
+       void *ssl = us->ssl;
+       const char *msg = NULL;
+       bool cn_mismatch;
+       int r;
+
+       r = ssl_get_verify_result(ssl);
+       cn_mismatch = r & BADCERT_CN_MISMATCH;
+       r &= ~BADCERT_CN_MISMATCH;
+
+       if (r & BADCERT_EXPIRED)
+               msg = "certificate has expired";
+       else if (r & BADCERT_REVOKED)
+               msg = "certificate has been revoked";
+       else if (r & BADCERT_NOT_TRUSTED)
+               msg = "certificate is self-signed or not signed by a trusted CA";
+       else
+               msg = "unknown error";
+
+       if (r) {
+               if (us->notify_verify_error)
+                       us->notify_verify_error(us, r, msg);
+               return;
+       }
+
+       if (!cn_mismatch)
+               us->valid_cn = true;
+}
+
 __hidden enum ssl_conn_status __ustream_ssl_connect(struct ustream_ssl *us)
 {
        void *ssl = us->ssl;
        int r;
 
        r = ssl_handshake(ssl);
 __hidden enum ssl_conn_status __ustream_ssl_connect(struct ustream_ssl *us)
 {
        void *ssl = us->ssl;
        int r;
 
        r = ssl_handshake(ssl);
-       if (r == 0)
+       if (r == 0) {
+               ustream_ssl_verify_cert(us);
                return U_SSL_OK;
                return U_SSL_OK;
+       }
 
        if (ssl_do_wait(r))
                return U_SSL_PENDING;
 
        if (ssl_do_wait(r))
                return U_SSL_PENDING;
@@ -187,17 +212,23 @@ __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;
 __hidden int __ustream_ssl_write(struct ustream_ssl *us, const char *buf, int len)
 {
        void *ssl = us->ssl;
-       int ret = ssl_write(ssl, (const unsigned char *) buf, len);
+       int done = 0, ret = 0;
 
 
-       if (ret < 0) {
-               if (ssl_do_wait(ret))
-                       return 0;
+       while (done != len) {
+               ret = ssl_write(ssl, (const unsigned char *) buf + done, len - done);
 
 
-               ustream_ssl_error(us, ret);
-               return -1;
+               if (ret < 0) {
+                       if (ssl_do_wait(ret))
+                               return done;
+
+                       ustream_ssl_error(us, ret);
+                       return -1;
+               }
+
+               done += ret;
        }
 
        }
 
-       return ret;
+       return done;
 }
 
 __hidden int __ustream_ssl_read(struct ustream_ssl *us, char *buf, int len)
 }
 
 __hidden int __ustream_ssl_read(struct ustream_ssl *us, char *buf, int len)
@@ -250,16 +281,13 @@ static const int default_ciphersuites[] =
 #if defined(POLARSSL_DES_C)
     TLS_RSA_WITH_3DES_EDE_CBC_SHA,
 #endif
 #if defined(POLARSSL_DES_C)
     TLS_RSA_WITH_3DES_EDE_CBC_SHA,
 #endif
-#if defined(POLARSSL_ARC4_C)
-    TLS_RSA_WITH_RC4_128_SHA,
-    TLS_RSA_WITH_RC4_128_MD5,
-#endif
     0
 };
 
 __hidden void *__ustream_ssl_session_new(struct ustream_ssl_ctx *ctx)
 {
        ssl_context *ssl;
     0
 };
 
 __hidden void *__ustream_ssl_session_new(struct ustream_ssl_ctx *ctx)
 {
        ssl_context *ssl;
+       int auth;
        int ep;
 
        ssl = calloc(1, sizeof(ssl_context));
        int ep;
 
        ssl = calloc(1, sizeof(ssl_context));
@@ -271,20 +299,25 @@ __hidden void *__ustream_ssl_session_new(struct ustream_ssl_ctx *ctx)
                return NULL;
        }
 
                return NULL;
        }
 
-       if (ctx->server)
+       if (ctx->server) {
                ep = SSL_IS_SERVER;
                ep = SSL_IS_SERVER;
-       else
+               auth = SSL_VERIFY_NONE;
+       } else {
                ep = SSL_IS_CLIENT;
                ep = SSL_IS_CLIENT;
+               auth = SSL_VERIFY_OPTIONAL;
+       }
 
        ssl_set_ciphersuites(ssl, default_ciphersuites);
        ssl_set_endpoint(ssl, ep);
 
        ssl_set_ciphersuites(ssl, default_ciphersuites);
        ssl_set_endpoint(ssl, ep);
-       ssl_set_authmode(ssl, ctx->auth);
+       ssl_set_authmode(ssl, auth);
        ssl_set_rng(ssl, _urandom, NULL);
 
        if (ctx->server) {
                if (ctx->cert.next)
                        ssl_set_ca_chain(ssl, ctx->cert.next, NULL, NULL);
                ssl_set_own_cert(ssl, &ctx->cert, &ctx->key);
        ssl_set_rng(ssl, _urandom, NULL);
 
        if (ctx->server) {
                if (ctx->cert.next)
                        ssl_set_ca_chain(ssl, ctx->cert.next, NULL, NULL);
                ssl_set_own_cert(ssl, &ctx->cert, &ctx->key);
+       } else {
+               ssl_set_ca_chain(ssl, &ctx->cert, NULL, NULL);
        }
 
        ssl_session_reset(ssl);
        }
 
        ssl_session_reset(ssl);
@@ -297,3 +330,10 @@ __hidden void __ustream_ssl_session_free(void *ssl)
        ssl_free(ssl);
        free(ssl);
 }
        ssl_free(ssl);
        free(ssl);
 }
+
+__hidden void __ustream_ssl_update_peer_cn(struct ustream_ssl *us)
+{
+       struct ustream_ssl_ctx *ctx = us->ctx;
+
+       ssl_set_ca_chain(us->ssl, &ctx->ca_cert, NULL, us->peer_cn);
+}