Blame SOURCES/elinks-nss.patch

ab432b
 configure.in             |   34 +++++++++++++++++++++++++++++++++-
ab432b
 src/network/ssl/socket.c |   28 ++++++++++++++++++++++------
ab432b
 src/network/ssl/ssl.c    |   32 ++++++++++++++++++++++++++------
ab432b
 src/network/ssl/ssl.h    |    2 +-
ab432b
 4 files changed, 82 insertions(+), 14 deletions(-)
ab432b
ab432b
diff --git a/configure.in b/configure.in
ab432b
index 0e534db..972a305 100644
ab432b
--- a/configure.in
ab432b
+++ b/configure.in
ab432b
@@ -970,6 +970,37 @@ AC_ARG_WITH(openssl, [[  --with-openssl[=DIR]    enable OpenSSL support (default
ab432b
 		*)	chosen_ssl_library="OpenSSL" ;;
ab432b
 	     esac])
ab432b
 
ab432b
+AC_ARG_WITH(nss_compat_ossl, [[  --with-nss_compat_ossl[=DIR]
ab432b
+                          NSS compatibility SSL libraries/include files]])
ab432b
+
ab432b
+# nss_compat_ossl
ab432b
+if test -n "$with_nss_compat_ossl" && test "$with_nss_compat_ossl" != "no"; then
ab432b
+	EL_SAVE_FLAGS
ab432b
+	if test "$with_nss_compat_ossl" = yes; then
ab432b
+		if pkg-config nss; then
ab432b
+			CFLAGS="$CFLAGS_X `pkg-config --cflags nss`"
ab432b
+			LIBS="$LIBS_X `pkg-config --libs nss`"
ab432b
+		else
ab432b
+			with_nss_compat_ossl=no
ab432b
+		fi
ab432b
+	else
ab432b
+		# Without pkg-config, we'll kludge in some defaults
ab432b
+		CFLAGS="$CFLAGS_X -I$with_nss_compat_ossl/include -I/usr/include/nss3 -I/usr/include/nspr4"
ab432b
+		LIBS="$LIBS_X -L$with_nss_compat_ossl/lib -lssl3 -lsmime3 -lnss3 -lplds4 -lplc4 -lnspr4 -lpthread -ldl"
ab432b
+	fi
ab432b
+	AC_CHECK_HEADERS(nss_compat_ossl/nss_compat_ossl.h,, [with_nss_compat_ossl=no], [#define NSS_COMPAT_OSSL_H])
ab432b
+	AC_CHECK_LIB(nss_compat_ossl, X509_free,, [with_nss_compat_ossl=no])
ab432b
+
ab432b
+	if test "$with_nss_compat_ossl" = "no"; then
ab432b
+		EL_RESTORE_FLAGS
ab432b
+	else
ab432b
+		LIBS="$LIBS -lnss_compat_ossl"
ab432b
+		EL_CONFIG(CONFIG_NSS_COMPAT_OSSL, [nss_compat_ossl])
ab432b
+		disable_openssl="yes"
ab432b
+		disable_gnutls="yes"
ab432b
+	fi
ab432b
+fi
ab432b
+
ab432b
 # ---- OpenSSL
ab432b
 
ab432b
 AC_MSG_CHECKING([for OpenSSL])
ab432b
@@ -1092,10 +1123,11 @@ fi
ab432b
 
ab432b
 # Final SSL setup
ab432b
 
ab432b
-EL_CONFIG_DEPENDS(CONFIG_SSL, [CONFIG_OPENSSL CONFIG_GNUTLS], [SSL])
ab432b
+EL_CONFIG_DEPENDS(CONFIG_SSL, [CONFIG_OPENSSL CONFIG_GNUTLS CONFIG_NSS_COMPAT_OSSL], [SSL])
ab432b
 AC_SUBST(CONFIG_GNUTLS_OPENSSL_COMPAT)
ab432b
 AC_SUBST(CONFIG_OPENSSL)
ab432b
 AC_SUBST(CONFIG_GNUTLS)
ab432b
+AC_SUBST(CONFIG_NSS_COMPAT_OSSL)
ab432b
 
ab432b
 #endif
ab432b
 
ab432b
diff --git a/src/network/ssl/socket.c b/src/network/ssl/socket.c
ab432b
index 45b4b4a..3265107 100644
ab432b
--- a/src/network/ssl/socket.c
ab432b
+++ b/src/network/ssl/socket.c
ab432b
@@ -6,6 +6,10 @@
ab432b
 
ab432b
 #ifdef CONFIG_OPENSSL
ab432b
 #include <openssl/ssl.h>
ab432b
+#define USE_OPENSSL
ab432b
+#elif defined(CONFIG_NSS_COMPAT_OSSL)
ab432b
+#include <nss_compat_ossl/nss_compat_ossl.h>
ab432b
+#define USE_OPENSSL
ab432b
 #elif defined(CONFIG_GNUTLS)
ab432b
 #include <gnutls/gnutls.h>
ab432b
 #else
ab432b
@@ -26,7 +30,7 @@
ab432b
 
ab432b
 
ab432b
 /* SSL errors */
ab432b
-#ifdef CONFIG_OPENSSL
ab432b
+#ifdef USE_OPENSSL
ab432b
 #define	SSL_ERROR_WANT_READ2	9999 /* XXX */
ab432b
 #define	SSL_ERROR_WANT_WRITE2	SSL_ERROR_WANT_WRITE
ab432b
 #define	SSL_ERROR_SYSCALL2	SSL_ERROR_SYSCALL
ab432b
@@ -40,7 +44,7 @@
ab432b
 #define	SSL_ERROR_SYSCALL2	GNUTLS_E_PULL_ERROR
ab432b
 #endif
ab432b
 
ab432b
-#ifdef CONFIG_OPENSSL
ab432b
+#ifdef USE_OPENSSL
ab432b
 
ab432b
 #define ssl_do_connect(socket)		SSL_get_error(socket->ssl, SSL_connect(socket->ssl))
ab432b
 #define ssl_do_write(socket, data, len)	SSL_write(socket->ssl, data, len)
ab432b
@@ -126,7 +130,7 @@ ssl_connect(struct socket *socket)
ab432b
 	if (socket->no_tls)
ab432b
 		ssl_set_no_tls(socket);
ab432b
 
ab432b
-#ifdef CONFIG_OPENSSL
ab432b
+#ifdef USE_OPENSSL
ab432b
 	SSL_set_fd(socket->ssl, socket->fd);
ab432b
 
ab432b
 	if (get_opt_bool("connection.ssl.cert_verify"))
ab432b
@@ -137,7 +141,13 @@ ssl_connect(struct socket *socket)
ab432b
 	if (get_opt_bool("connection.ssl.client_cert.enable")) {
ab432b
 		unsigned char *client_cert;
ab432b
 
ab432b
-		client_cert = get_opt_str("connection.ssl.client_cert.file");
ab432b
+#ifdef CONFIG_NSS_COMPAT_OSSL
ab432b
+		client_cert = get_opt_str(
ab432b
+				"connection.ssl.client_cert.nickname");
ab432b
+#else
ab432b
+		client_cert = get_opt_str(
ab432b
+				"connection.ssl.client_cert.file");
ab432b
+#endif
ab432b
 		if (!*client_cert) {
ab432b
 			client_cert = getenv("X509_CLIENT_CERT");
ab432b
 			if (client_cert && !*client_cert)
ab432b
@@ -145,11 +155,17 @@ ssl_connect(struct socket *socket)
ab432b
 		}
ab432b
 
ab432b
 		if (client_cert) {
ab432b
+#ifdef CONFIG_NSS_COMPAT_OSSL
ab432b
+			SSL_CTX_use_certificate_chain_file(
ab432b
+					(SSL *) socket->ssl,
ab432b
+					client_cert);
ab432b
+#else
ab432b
 			SSL_CTX *ctx = ((SSL *) socket->ssl)->ctx;
ab432b
 
ab432b
 			SSL_CTX_use_certificate_chain_file(ctx, client_cert);
ab432b
 			SSL_CTX_use_PrivateKey_file(ctx, client_cert,
ab432b
 						    SSL_FILETYPE_PEM);
ab432b
+#endif
ab432b
 		}
ab432b
 	}
ab432b
 
ab432b
@@ -206,7 +222,7 @@ ssl_write(struct socket *socket, unsigned char *data, int len)
ab432b
 	ssize_t wr = ssl_do_write(socket, data, len);
ab432b
 
ab432b
 	if (wr <= 0) {
ab432b
-#ifdef CONFIG_OPENSSL
ab432b
+#ifdef USE_OPENSSL
ab432b
 		int err = SSL_get_error(socket->ssl, wr);
ab432b
 #elif defined(CONFIG_GNUTLS)
ab432b
 		int err = wr;
ab432b
@@ -235,7 +251,7 @@ ssl_read(struct socket *socket, unsigned char *data, int len)
ab432b
 	ssize_t rd = ssl_do_read(socket, data, len);
ab432b
 
ab432b
 	if (rd <= 0) {
ab432b
-#ifdef CONFIG_OPENSSL
ab432b
+#ifdef USE_OPENSSL
ab432b
 		int err = SSL_get_error(socket->ssl, rd);
ab432b
 #elif defined(CONFIG_GNUTLS)
ab432b
 		int err = rd;
ab432b
diff --git a/src/network/ssl/ssl.c b/src/network/ssl/ssl.c
ab432b
index 685c31e..73446b5 100644
ab432b
--- a/src/network/ssl/ssl.c
ab432b
+++ b/src/network/ssl/ssl.c
ab432b
@@ -7,6 +7,10 @@
ab432b
 #ifdef CONFIG_OPENSSL
ab432b
 #include <openssl/ssl.h>
ab432b
 #include <openssl/rand.h>
ab432b
+#define USE_OPENSSL
ab432b
+#elif defined(CONFIG_NSS_COMPAT_OSSL)
ab432b
+#include <nss_compat_ossl/nss_compat_ossl.h>
ab432b
+#define USE_OPENSSL
ab432b
 #elif defined(CONFIG_GNUTLS)
ab432b
 #include <gnutls/gnutls.h>
ab432b
 #include <gnutls/x509.h>
ab432b
@@ -33,7 +37,7 @@
ab432b
 /* FIXME: As you can see, SSL is currently implemented in very, erm,
ab432b
  * decentralized manner. */
ab432b
 
ab432b
-#ifdef CONFIG_OPENSSL
ab432b
+#ifdef USE_OPENSSL
ab432b
 
ab432b
 #ifndef PATH_MAX
ab432b
 #define	PATH_MAX	256 /* according to my /usr/include/bits/posix1_lim.h */
ab432b
@@ -71,12 +75,28 @@ static union option_info openssl_options[] = {
ab432b
 		N_("Enable or not the sending of X509 client certificates "
ab432b
 		"to servers which request them.")),
ab432b
 
ab432b
+#ifdef CONFIG_NSS_COMPAT_OSSL
ab432b
+	INIT_OPT_STRING("connection.ssl.client_cert", N_("Certificate nickname"),
ab432b
+		"nickname", 0, "",
ab432b
+		N_("The nickname of the client certificate stored in NSS "
ab432b
+		"database. If this value is unset, the nickname from "
ab432b
+		"the X509_CLIENT_CERT variable is used instead. If you "
ab432b
+		"have a PKCS#12 file containing client certificate, you "
ab432b
+		"can import it into your NSS database with:\n"
ab432b
+		"\n"
ab432b
+		"$ pk12util -i mycert.p12 -d /path/to/database\n"
ab432b
+		"\n"
ab432b
+		"The NSS database location can be changed by SSL_DIR "
ab432b
+		"environment variable. The database can be also shared "
ab432b
+		"with Mozilla browsers.")),
ab432b
+#else
ab432b
 	INIT_OPT_STRING("connection.ssl.client_cert", N_("Certificate File"),
ab432b
 		"file", 0, "",
ab432b
 		N_("The location of a file containing the client certificate "
ab432b
 		"and unencrypted private key in PEM format. If unset, the "
ab432b
 		"file pointed to by the X509_CLIENT_CERT variable is used "
ab432b
 		"instead.")),
ab432b
+#endif
ab432b
 
ab432b
 	NULL_OPTION_INFO,
ab432b
 };
ab432b
@@ -182,7 +202,7 @@ static struct module gnutls_module = struct_module(
ab432b
 	/* done: */		done_gnutls
ab432b
 );
ab432b
 
ab432b
-#endif /* CONFIG_OPENSSL or CONFIG_GNUTLS */
ab432b
+#endif /* USE_OPENSSL or CONFIG_GNUTLS */
ab432b
 
ab432b
 static union option_info ssl_options[] = {
ab432b
 	INIT_OPT_TREE("connection", N_("SSL"),
ab432b
@@ -193,7 +213,7 @@ static union option_info ssl_options[] = {
ab432b
 };
ab432b
 
ab432b
 static struct module *ssl_modules[] = {
ab432b
-#ifdef CONFIG_OPENSSL
ab432b
+#ifdef USE_OPENSSL
ab432b
 	&openssl_module,
ab432b
 #elif defined(CONFIG_GNUTLS)
ab432b
 	&gnutls_module,
ab432b
@@ -214,7 +234,7 @@ struct module ssl_module = struct_module(
ab432b
 int
ab432b
 init_ssl_connection(struct socket *socket)
ab432b
 {
ab432b
-#ifdef CONFIG_OPENSSL
ab432b
+#ifdef USE_OPENSSL
ab432b
 	socket->ssl = SSL_new(context);
ab432b
 	if (!socket->ssl) return S_SSL_ERROR;
ab432b
 #elif defined(CONFIG_GNUTLS)
ab432b
@@ -271,7 +291,7 @@ done_ssl_connection(struct socket *socket)
ab432b
 	ssl_t *ssl = socket->ssl;
ab432b
 
ab432b
 	if (!ssl) return;
ab432b
-#ifdef CONFIG_OPENSSL
ab432b
+#ifdef USE_OPENSSL
ab432b
 	SSL_free(ssl);
ab432b
 #elif defined(CONFIG_GNUTLS)
ab432b
 	gnutls_deinit(*ssl);
ab432b
@@ -288,7 +308,7 @@ get_ssl_connection_cipher(struct socket *socket)
ab432b
 
ab432b
 	if (!init_string(&str)) return NULL;
ab432b
 
ab432b
-#ifdef CONFIG_OPENSSL
ab432b
+#ifdef USE_OPENSSL
ab432b
 	add_format_to_string(&str, "%ld-bit %s %s",
ab432b
 		SSL_get_cipher_bits(ssl, NULL),
ab432b
 		SSL_get_cipher_version(ssl),
ab432b
diff --git a/src/network/ssl/ssl.h b/src/network/ssl/ssl.h
ab432b
index 7c54a7a..21ca142 100644
ab432b
--- a/src/network/ssl/ssl.h
ab432b
+++ b/src/network/ssl/ssl.h
ab432b
@@ -22,7 +22,7 @@ unsigned char *get_ssl_connection_cipher(struct socket *socket);
ab432b
 
ab432b
 /* Internal type used in ssl module. */
ab432b
 
ab432b
-#ifdef CONFIG_OPENSSL
ab432b
+#if defined(CONFIG_OPENSSL) || defined(CONFIG_NSS_COMPAT_OSSL)
ab432b
 #define	ssl_t	SSL
ab432b
 #elif defined(CONFIG_GNUTLS)
ab432b
 #define	ssl_t	gnutls_session_t