cmake: Find libubox/ustream.h header file
[project/ustream-ssl.git] / ustream-polarssl.c
index e5c84557381dd5a1d56e14923bdef6c2b1dbb134..39bde2111ce6227366107b9becae29aacfa3f0a7 100644 (file)
@@ -20,6 +20,7 @@
 #include <fcntl.h>
 #include <unistd.h>
 #include <stdlib.h>
+#include <string.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)
 {
-       read(urandom_fd, out, len);
+       if (read(urandom_fd, out, len) < 0)
+               return POLARSSL_ERR_ENTROPY_SOURCE_FAILED;
+
        return 0;
 }
 
@@ -96,11 +99,9 @@ __ustream_ssl_context_new(bool server)
                return NULL;
 
        ctx->server = server;
-#ifdef USE_VERSION_1_3
        pk_init(&ctx->key);
-#else
-       rsa_init(&ctx->key, RSA_PKCS_V15, 0);
-#endif
+       x509_crt_init(&ctx->ca_cert);
+       x509_crt_init(&ctx->cert);
 
        return ctx;
 }
@@ -109,11 +110,7 @@ __hidden int __ustream_ssl_add_ca_crt_file(struct ustream_ssl_ctx *ctx, const ch
 {
        int ret;
 
-#ifdef USE_VERSION_1_3
        ret = x509_crt_parse_file(&ctx->ca_cert, file);
-#else
-       ret = x509parse_crtfile(&ctx->ca_cert, file);
-#endif
        if (ret)
                return -1;
 
@@ -124,11 +121,7 @@ __hidden int __ustream_ssl_set_crt_file(struct ustream_ssl_ctx *ctx, const char
 {
        int ret;
 
-#ifdef USE_VERSION_1_3
        ret = x509_crt_parse_file(&ctx->cert, file);
-#else
-       ret = x509parse_crtfile(&ctx->cert, file);
-#endif
        if (ret)
                return -1;
 
@@ -139,11 +132,7 @@ __hidden int __ustream_ssl_set_key_file(struct ustream_ssl_ctx *ctx, const char
 {
        int ret;
 
-#ifdef USE_VERSION_1_3
        ret = pk_parse_keyfile(&ctx->key, file, NULL);
-#else
-       ret = x509parse_keyfile(&ctx->key, file, NULL);
-#endif
        if (ret)
                return -1;
 
@@ -152,13 +141,9 @@ __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)
 {
-#ifdef USE_VERSION_1_3
        pk_free(&ctx->key);
+       x509_crt_free(&ctx->ca_cert);
        x509_crt_free(&ctx->cert);
-#else
-       rsa_free(&ctx->key);
-       x509_free(&ctx->cert);
-#endif
        free(ctx);
 }
 
@@ -230,17 +215,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;
-       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)
@@ -251,6 +242,9 @@ __hidden int __ustream_ssl_read(struct ustream_ssl *us, char *buf, int len)
                if (ssl_do_wait(ret))
                        return U_SSL_PENDING;
 
+               if (ret == POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY)
+                       return 0;
+
                ustream_ssl_error(us, ret);
                return U_SSL_ERROR;
        }
@@ -258,46 +252,36 @@ __hidden int __ustream_ssl_read(struct ustream_ssl *us, char *buf, int len)
        return ret;
 }
 
+#define TLS_DEFAULT_CIPHERS                    \
+    TLS_CIPHER(AES_256_CBC_SHA256)             \
+    TLS_CIPHER(AES_256_GCM_SHA384)             \
+    TLS_CIPHER(AES_256_CBC_SHA)                        \
+    TLS_CIPHER(CAMELLIA_256_CBC_SHA256)                \
+    TLS_CIPHER(CAMELLIA_256_CBC_SHA)           \
+    TLS_CIPHER(AES_128_CBC_SHA256)             \
+    TLS_CIPHER(AES_128_GCM_SHA256)             \
+    TLS_CIPHER(AES_128_CBC_SHA)                        \
+    TLS_CIPHER(CAMELLIA_128_CBC_SHA256)                \
+    TLS_CIPHER(CAMELLIA_128_CBC_SHA)           \
+    TLS_CIPHER(3DES_EDE_CBC_SHA)
+
+static const int default_ciphersuites_nodhe[] =
+{
+#define TLS_CIPHER(v)                          \
+       TLS_RSA_WITH_##v,
+       TLS_DEFAULT_CIPHERS
+#undef TLS_CIPHER
+       0
+};
+
 static const int default_ciphersuites[] =
 {
-#if defined(POLARSSL_AES_C)
-#if defined(POLARSSL_SHA2_C)
-    TLS_RSA_WITH_AES_256_CBC_SHA256,
-#endif /* POLARSSL_SHA2_C */
-#if defined(POLARSSL_GCM_C) && defined(POLARSSL_SHA4_C)
-    TLS_RSA_WITH_AES_256_GCM_SHA384,
-#endif /* POLARSSL_SHA2_C */
-    TLS_RSA_WITH_AES_256_CBC_SHA,
-#endif
-#if defined(POLARSSL_CAMELLIA_C)
-#if defined(POLARSSL_SHA2_C)
-    TLS_RSA_WITH_CAMELLIA_256_CBC_SHA256,
-#endif /* POLARSSL_SHA2_C */
-    TLS_RSA_WITH_CAMELLIA_256_CBC_SHA,
-#endif
-#if defined(POLARSSL_AES_C)
-#if defined(POLARSSL_SHA2_C)
-    TLS_RSA_WITH_AES_128_CBC_SHA256,
-#endif /* POLARSSL_SHA2_C */
-#if defined(POLARSSL_GCM_C) && defined(POLARSSL_SHA2_C)
-    TLS_RSA_WITH_AES_128_GCM_SHA256,
-#endif /* POLARSSL_SHA2_C */
-    TLS_RSA_WITH_AES_128_CBC_SHA,
-#endif
-#if defined(POLARSSL_CAMELLIA_C)
-#if defined(POLARSSL_SHA2_C)
-    TLS_RSA_WITH_CAMELLIA_128_CBC_SHA256,
-#endif /* POLARSSL_SHA2_C */
-    TLS_RSA_WITH_CAMELLIA_128_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
+#define TLS_CIPHER(v)                          \
+       TLS_DHE_RSA_WITH_##v,                   \
+       TLS_RSA_WITH_##v,
+       TLS_DEFAULT_CIPHERS
+#undef TLS_CIPHER
+       0
 };
 
 __hidden void *__ustream_ssl_session_new(struct ustream_ssl_ctx *ctx)
@@ -323,17 +307,18 @@ __hidden void *__ustream_ssl_session_new(struct ustream_ssl_ctx *ctx)
                auth = SSL_VERIFY_OPTIONAL;
        }
 
-       ssl_set_ciphersuites(ssl, default_ciphersuites);
        ssl_set_endpoint(ssl, ep);
        ssl_set_authmode(ssl, auth);
        ssl_set_rng(ssl, _urandom, NULL);
 
        if (ctx->server) {
+               ssl_set_ciphersuites(ssl, default_ciphersuites_nodhe);
                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_set_ciphersuites(ssl, default_ciphersuites);
+               ssl_set_ca_chain(ssl, &ctx->ca_cert, NULL, NULL);
        }
 
        ssl_session_reset(ssl);