[package] uhttpd: support building against openssl instead of cyassl, minor cleanups...
[openwrt.git] / package / uhttpd / src / uhttpd-tls.c
index cb50616..6beae25 100644 (file)
@@ -23,7 +23,8 @@
 
 SSL_CTX * uh_tls_ctx_init()
 {
-       SSL_CTX *c = NULL;
+       SSL_CTX *c;
+
        SSL_load_error_strings();
        SSL_library_init();
 
@@ -35,12 +36,22 @@ SSL_CTX * uh_tls_ctx_init()
 
 int uh_tls_ctx_cert(SSL_CTX *c, const char *file)
 {
-       return SSL_CTX_use_certificate_file(c, file, SSL_FILETYPE_ASN1);
+       int rv;
+
+       if( (rv = SSL_CTX_use_certificate_file(c, file, SSL_FILETYPE_PEM)) < 1 )
+               rv = SSL_CTX_use_certificate_file(c, file, SSL_FILETYPE_ASN1);
+
+       return rv;
 }
 
 int uh_tls_ctx_key(SSL_CTX *c, const char *file)
 {
-       return SSL_CTX_use_PrivateKey_file(c, file, SSL_FILETYPE_ASN1);
+       int rv;
+
+       if( (rv = SSL_CTX_use_PrivateKey_file(c, file, SSL_FILETYPE_PEM)) < 1 )
+               rv = SSL_CTX_use_PrivateKey_file(c, file, SSL_FILETYPE_ASN1);
+
+       return rv;
 }
 
 void uh_tls_ctx_free(struct listener *l)
@@ -49,23 +60,48 @@ void uh_tls_ctx_free(struct listener *l)
 }
 
 
-void uh_tls_client_accept(struct client *c)
+int uh_tls_client_accept(struct client *c)
 {
+       int rv;
+
        if( c->server && c->server->tls )
        {
                c->tls = SSL_new(c->server->tls);
-               SSL_set_fd(c->tls, c->socket);
+               if( c->tls )
+               {
+                       if( (rv = SSL_set_fd(c->tls, c->socket)) < 1 )
+                               goto cleanup;
+                       if( (rv = SSL_accept(c->tls)) < 1 )
+                               goto cleanup;
+               }
+               else
+                       rv = 0;
        }
+       else
+       {
+               c->tls = NULL;
+               rv = 1;
+       }
+
+done:
+       return rv;
+
+cleanup:
+       SSL_free(c->tls);
+       c->tls = NULL;
+       goto done;
 }
 
 int uh_tls_client_recv(struct client *c, void *buf, int len)
 {
-       return SSL_read(c->tls, buf, len);
+       int rv = SSL_read(c->tls, buf, len);
+       return (rv > 0) ? rv : -1;
 }
 
 int uh_tls_client_send(struct client *c, void *buf, int len)
 {
-       return SSL_write(c->tls, buf, len);
+       int rv = SSL_write(c->tls, buf, len);
+       return (rv > 0) ? rv : -1;
 }
 
 void uh_tls_client_close(struct client *c)
@@ -78,5 +114,3 @@ void uh_tls_client_close(struct client *c)
                c->tls = NULL;
        }
 }
-
-