give the main context a type instead of making it void *
[project/ustream-ssl.git] / ustream-polarssl.c
index c32d1d8c591f9e19350800b123cade2da09b88af..c0147ed948260974ce3da64ee546047afd72c7d4 100644 (file)
@@ -51,15 +51,16 @@ static int s_ustream_read(void *ctx, unsigned char *buf, size_t len)
 static int s_ustream_write(void *ctx, const unsigned char *buf, size_t len)
 {
        struct ustream *s = ctx;
+       int ret;
 
-       len = ustream_write(s, (const char *) buf, len, false);
-       if (len < 0 || s->write_error)
+       ret = ustream_write(s, (const char *) buf, len, false);
+       if (ret < 0 || s->write_error)
                return POLARSSL_ERR_NET_SEND_FAILED;
 
-       return len;
+       return ret;
 }
 
-__hidden void ustream_set_io(void *ctx, void *ssl, struct ustream *conn)
+__hidden void ustream_set_io(struct ustream_ssl_ctx *ctx, void *ssl, struct ustream *conn)
 {
        ssl_set_bio(ssl, s_ustream_read, conn, s_ustream_write, conn);
 }
@@ -82,49 +83,67 @@ static int _urandom(void *ctx, unsigned char *out, size_t len)
        return 0;
 }
 
-__hidden void * __ustream_ssl_context_new(bool server)
+__hidden struct ustream_ssl_ctx *
+__ustream_ssl_context_new(bool server)
 {
-       struct ustream_polarssl_ctx *uctx;
+       struct ustream_ssl_ctx *ctx;
 
        if (!urandom_init())
                return NULL;
 
-       uctx = calloc(1, sizeof(*uctx));
-       if (!uctx)
+       ctx = calloc(1, sizeof(*ctx));
+       if (!ctx)
                return NULL;
 
-       uctx->server = server;
-       rsa_init(&uctx->key, RSA_PKCS_V15, 0);
+       ctx->server = server;
+#ifdef USE_VERSION_1_3
+       pk_init(&ctx->key);
+#else
+       rsa_init(&ctx->key, RSA_PKCS_V15, 0);
+#endif
 
-       return uctx;
+       return ctx;
 }
 
-__hidden int __ustream_ssl_set_crt_file(void *ctx, const char *file)
+__hidden int __ustream_ssl_set_crt_file(struct ustream_ssl_ctx *ctx, const char *file)
 {
-       struct ustream_polarssl_ctx *uctx = ctx;
-
-       if (x509parse_crtfile(&uctx->cert, file))
+       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;
 
        return 0;
 }
 
-__hidden int __ustream_ssl_set_key_file(void *ctx, const char *file)
+__hidden int __ustream_ssl_set_key_file(struct ustream_ssl_ctx *ctx, const char *file)
 {
-       struct ustream_polarssl_ctx *uctx = ctx;
-
-       if (x509parse_keyfile(&uctx->key, file, NULL))
+       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;
 
        return 0;
 }
 
-__hidden void __ustream_ssl_context_free(void *ctx)
+__hidden void __ustream_ssl_context_free(struct ustream_ssl_ctx *ctx)
 {
-       struct ustream_polarssl_ctx *uctx = ctx;
-
-       rsa_free(&uctx->key);
-       x509_free(&uctx->cert);
+#ifdef USE_VERSION_1_3
+       pk_free(&ctx->key);
+       x509_crt_free(&ctx->cert);
+#else
+       rsa_free(&ctx->key);
+       x509_free(&ctx->cert);
+#endif
        free(ctx);
 }
 
@@ -192,9 +211,50 @@ __hidden int __ustream_ssl_read(struct ustream_ssl *us, char *buf, int len)
        return ret;
 }
 
-__hidden void *__ustream_ssl_session_new(void *ctx)
+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
+};
+
+__hidden void *__ustream_ssl_session_new(struct ustream_ssl_ctx *ctx)
 {
-       struct ustream_polarssl_ctx *uctx = ctx;
        ssl_context *ssl;
        int ep, auth;
 
@@ -207,7 +267,7 @@ __hidden void *__ustream_ssl_session_new(void *ctx)
                return NULL;
        }
 
-       if (uctx->server) {
+       if (ctx->server) {
                ep = SSL_IS_SERVER;
                auth = SSL_VERIFY_NONE;
        } else {
@@ -215,14 +275,15 @@ __hidden void *__ustream_ssl_session_new(void *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 (uctx->server) {
-               if (uctx->cert.next)
-                       ssl_set_ca_chain(ssl, uctx->cert.next, NULL, NULL);
-               ssl_set_own_cert(ssl, &uctx->cert, &uctx->key);
+       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_session_reset(ssl);