Browse code

Merge pull request #1051 from chazer/ssl-auth

Allow setting SSL client authentication

Florent Viard authored on 2020/04/22 05:29:54
Showing 4 changed files
... ...
@@ -153,6 +153,8 @@ class Config(object):
153 153
     gpg_decrypt = u"%(gpg_command)s -d --verbose --no-use-agent --batch --yes --passphrase-fd %(passphrase_fd)s -o %(output_file)s %(input_file)s"
154 154
     use_https = True
155 155
     ca_certs_file = u""
156
+    ssl_client_key_file = u""
157
+    ssl_client_cert_file = u""
156 158
     check_ssl_certificate = True
157 159
     check_ssl_hostname = True
158 160
     bucket_location = u"US"
... ...
@@ -61,6 +61,19 @@ class http_connection(object):
61 61
         return context
62 62
 
63 63
     @staticmethod
64
+    def _ssl_client_auth_context(certfile, keyfile, check_server_cert, cafile):
65
+        context = None
66
+        try:
67
+            cert_reqs = ssl.CERT_REQUIRED if check_server_cert else ssl.CERT_NONE
68
+            context = ssl._create_unverified_context(cafile=cafile,
69
+                                                     keyfile=keyfile,
70
+                                                     certfile=certfile,
71
+                                                     cert_reqs=cert_reqs)
72
+        except AttributeError: # no ssl._create_unverified_context
73
+            pass
74
+        return context
75
+
76
+    @staticmethod
64 77
     def _ssl_context():
65 78
         if http_connection.context_set:
66 79
             return http_connection.context
... ...
@@ -69,9 +82,16 @@ class http_connection(object):
69 69
         cafile = cfg.ca_certs_file
70 70
         if cafile == "":
71 71
             cafile = None
72
+        certfile = cfg.ssl_client_cert_file or None
73
+        keyfile = cfg.ssl_client_key_file or None # the key may be embedded into cert file
74
+
72 75
         debug(u"Using ca_certs_file %s", cafile)
76
+        debug(u"Using ssl_client_cert_file %s", certfile)
77
+        debug(u"Using ssl_client_key_file %s", keyfile)
73 78
 
74
-        if cfg.check_ssl_certificate:
79
+        if certfile is not None:
80
+            context = http_connection._ssl_client_auth_context(certfile, keyfile, cfg.check_ssl_certificate, cafile)
81
+        elif cfg.check_ssl_certificate:
75 82
             context = http_connection._ssl_verified_context(cafile)
76 83
         else:
77 84
             context = http_connection._ssl_unverified_context(cafile)
... ...
@@ -2813,6 +2813,8 @@ def main():
2813 2813
     optparser.add_option(      "--cache-file", dest="cache_file", action="store", default="",  metavar="FILE", help="Cache FILE containing local source MD5 values")
2814 2814
     optparser.add_option("-q", "--quiet", dest="quiet", action="store_true", default=False, help="Silence output on stdout")
2815 2815
     optparser.add_option(      "--ca-certs", dest="ca_certs_file", action="store", default=None, help="Path to SSL CA certificate FILE (instead of system default)")
2816
+    optparser.add_option(      "--ssl-cert", dest="ssl_client_cert_file", action="store", default=None, help="Path to client own SSL certificate CRT_FILE")
2817
+    optparser.add_option(      "--ssl-key", dest="ssl_client_key_file", action="store", default=None, help="Path to client own SSL certificate private key KEY_FILE")
2816 2818
     optparser.add_option(      "--check-certificate", dest="check_ssl_certificate", action="store_true", help="Check SSL certificate validity")
2817 2819
     optparser.add_option(      "--no-check-certificate", dest="check_ssl_certificate", action="store_false", help="Do not check SSL certificate validity")
2818 2820
     optparser.add_option(      "--check-hostname", dest="check_ssl_hostname", action="store_true", help="Check SSL certificate hostname validity")
... ...
@@ -539,6 +539,12 @@ Silence output on stdout
539 539
 Path to SSL CA certificate FILE (instead of system
540 540
 default)
541 541
 .TP
542
+\fB\-\-ssl\-cert\fR=CRT_FILE
543
+Path to client own SSL certificate CRT_FILE
544
+.TP
545
+\fB\-\-ssl\-key\fR=KEY_FILE
546
+Path to client own SSL certificate private key KEY_FILE
547
+.TP
542 548
 \fB\-\-check\-certificate\fR
543 549
 Check SSL certificate validity
544 550
 .TP