Blame 0016-Handle-curl-using-libnssckbi-for-TLS-RHBZ-1447777.patch

Harald Hoyer 3763a8
From 892b1fe6b74a04e7901db306231136a430326ee3 Mon Sep 17 00:00:00 2001
Harald Hoyer 3763a8
From: Adam Williamson <awilliam@redhat.com>
Harald Hoyer 3763a8
Date: Wed, 3 May 2017 12:32:43 -0700
Harald Hoyer 3763a8
Subject: [PATCH] Handle curl using libnssckbi for TLS (RHBZ #1447777)
Harald Hoyer 3763a8
Harald Hoyer 3763a8
curl in Fedora recently changed its default CA trust store. The
Harald Hoyer 3763a8
Fedora package no longer specifies an OpenSSL-format bundle file
Harald Hoyer 3763a8
during build, and curl itself has been patched to use an NSS
Harald Hoyer 3763a8
plugin called libnssckbi.so when no bundle file or directory is
Harald Hoyer 3763a8
specified. There are (at present) two possible providers of the
Harald Hoyer 3763a8
libnssckbi.so module: the original NSS implementation, which
Harald Hoyer 3763a8
uses a trust bundle built in at build time, and a compatible
Harald Hoyer 3763a8
implementation from the p11-kit project, which reads a trust
Harald Hoyer 3763a8
bundle at run time. So if we find a string in libcurl.so that
Harald Hoyer 3763a8
suggests libnssckbi might be in use, we must both install it and
Harald Hoyer 3763a8
make an effort to install any trust bundle files it may use.
Harald Hoyer 3763a8
Harald Hoyer 3763a8
The p11-kit libnssckbi implementation does include a string that
Harald Hoyer 3763a8
lists the top-level trust directories it will use, so we try to
Harald Hoyer 3763a8
find that string, though the best effort I can come up with will
Harald Hoyer 3763a8
also find many false positives too. To weed out the false
Harald Hoyer 3763a8
positives, we check whether the matches actually exist as dirs,
Harald Hoyer 3763a8
and if so, whether they contain some specific subdirectories we
Harald Hoyer 3763a8
know p11-kit trust dirs must have (thanks, @kaie). For the NSS
Harald Hoyer 3763a8
libnssckbi implementation, we will likely wind up not finding any
Harald Hoyer 3763a8
dirs that match the requirements, so we will simply install the
Harald Hoyer 3763a8
libnssckbi.so file itself, which is the correct action.
Harald Hoyer 3763a8
Harald Hoyer 3763a8
This fixes TLS transactions in the initramfs environment when
Harald Hoyer 3763a8
using a curl that's built this new way; it's significant for
Harald Hoyer 3763a8
use of kickstarts and update images with the Fedora / RHEL
Harald Hoyer 3763a8
installer, as these are retrieved in the initramfs environment,
Harald Hoyer 3763a8
and are frequently retrieved via HTTPS.
Harald Hoyer 3763a8
---
Harald Hoyer 3763a8
 modules.d/45url-lib/module-setup.sh | 38 +++++++++++++++++++++++++++++++++++--
Harald Hoyer 3763a8
 1 file changed, 36 insertions(+), 2 deletions(-)
Harald Hoyer 3763a8
Harald Hoyer 3763a8
diff --git a/modules.d/45url-lib/module-setup.sh b/modules.d/45url-lib/module-setup.sh
Harald Hoyer 3763a8
index 1ece400f..b3fe55a6 100755
Harald Hoyer 3763a8
--- a/modules.d/45url-lib/module-setup.sh
Harald Hoyer 3763a8
+++ b/modules.d/45url-lib/module-setup.sh
Harald Hoyer 3763a8
@@ -15,7 +15,7 @@ depends() {
Harald Hoyer 3763a8
 
Harald Hoyer 3763a8
 # called by dracut
Harald Hoyer 3763a8
 install() {
Harald Hoyer 3763a8
-    local _dir _crt _found _lib
Harald Hoyer 3763a8
+    local _dir _crt _found _lib _nssckbi _p11roots _p11root _p11item
Harald Hoyer 3763a8
     inst_simple "$moddir/url-lib.sh" "/lib/url-lib.sh"
Harald Hoyer 3763a8
     inst_multiple -o ctorrent
Harald Hoyer 3763a8
     inst_multiple curl
Harald Hoyer 3763a8
@@ -29,6 +29,7 @@ install() {
Harald Hoyer 3763a8
 	[[ -d $_dir ]] || continue
Harald Hoyer 3763a8
         for _lib in $_dir/libcurl.so.*; do
Harald Hoyer 3763a8
 	    [[ -e $_lib ]] || continue
Harald Hoyer 3763a8
+            [[ $_nssckbi ]] || _nssckbi=$(grep -F --binary-files=text -z libnssckbi $_lib)
Harald Hoyer 3763a8
             _crt=$(grep -F --binary-files=text -z .crt $_lib)
Harald Hoyer 3763a8
             [[ $_crt ]] || continue
Harald Hoyer 3763a8
             [[ $_crt == /*/* ]] || continue
Harald Hoyer 3763a8
@@ -39,6 +40,39 @@ install() {
Harald Hoyer 3763a8
             _found=1
Harald Hoyer 3763a8
         done
Harald Hoyer 3763a8
     done
Harald Hoyer 3763a8
-    [[ $_found ]] || dwarn "Couldn't find SSL CA cert bundle; HTTPS won't work."
Harald Hoyer 3763a8
+    # If we found no cert bundle files referenced in libcurl but we
Harald Hoyer 3763a8
+    # *did* find a mention of libnssckbi (checked above), install it.
Harald Hoyer 3763a8
+    # If its truly NSS libnssckbi, it includes its own trust bundle,
Harald Hoyer 3763a8
+    # but if it's really p11-kit-trust.so, we need to find the dirs
Harald Hoyer 3763a8
+    # where it will look for a trust bundle and install them too.
Harald Hoyer 3763a8
+    if ! [[ $_found ]] && [[ $_nssckbi ]] ; then
Harald Hoyer 3763a8
+        _found=1
Harald Hoyer 3763a8
+        inst_libdir_file "libnssckbi.so*" || _found=
Harald Hoyer 3763a8
+        for _dir in $libdirs; do
Harald Hoyer 3763a8
+            [[ -e $_dir/libnssckbi.so ]] || continue
Harald Hoyer 3763a8
+            # this looks for directory-ish strings in the file
Harald Hoyer 3763a8
+            for _p11roots in $(grep -o --binary-files=text "/[[:alpha:]][[:print:]]*" $_dir/libnssckbi.so) ; do
Harald Hoyer 3763a8
+                # the string can be a :-separated list of dirs
Harald Hoyer 3763a8
+                for _p11root in $(echo "$_p11roots" | tr ':' '\n') ; do
Harald Hoyer 3763a8
+                    # check if it's actually a directory (there are
Harald Hoyer 3763a8
+                    # several false positives in the results)
Harald Hoyer 3763a8
+                    [[ -d "$_p11root" ]] || continue
Harald Hoyer 3763a8
+                    # check if it has some specific subdirs that all
Harald Hoyer 3763a8
+                    # p11-kit trust dirs have
Harald Hoyer 3763a8
+                    [[ -d "${_p11root}/anchors" ]] || continue
Harald Hoyer 3763a8
+                    [[ -d "${_p11root}/blacklist" ]] || continue
Harald Hoyer 3763a8
+                    # so now we know it's really a p11-kit trust dir;
Harald Hoyer 3763a8
+                    # install everything in it
Harald Hoyer 3763a8
+                    for _p11item in $(find "$_p11root") ; do
Harald Hoyer 3763a8
+                        if ! inst "$_p11item" ; then
Harald Hoyer 3763a8
+                            dwarn "Couldn't install '$_p11item' from p11-kit trust dir '$_p11root'; HTTPS might not work."
Harald Hoyer 3763a8
+                            continue
Harald Hoyer 3763a8
+                        fi
Harald Hoyer 3763a8
+                    done
Harald Hoyer 3763a8
+                done
Harald Hoyer 3763a8
+            done
Harald Hoyer 3763a8
+        done
Harald Hoyer 3763a8
+    fi
Harald Hoyer 3763a8
+    [[ $_found ]] || dwarn "Couldn't find SSL CA cert bundle or libnssckbi.so; HTTPS won't work."
Harald Hoyer 3763a8
 }
Harald Hoyer 3763a8