Mercurial > hg
changeset 29334:ecc9b788fd69
sslutil: per-host config option to define certificates
Recent work has introduced the [hostsecurity] config section for
defining per-host security settings. This patch builds on top
of this foundation and implements the ability to define a per-host
path to a file containing certificates used for verifying the server
certificate. It is logically a per-host web.cacerts setting.
This patch also introduces a warning when both per-host
certificates and fingerprints are defined. These are mutually
exclusive for host verification and I think the user should be
alerted when security settings are ambiguous because, well,
security is important.
Tests validating the new behavior have been added.
I decided against putting "ca" in the option name because a
non-CA certificate can be specified and used to validate the server
certificate (commonly this will be the exact public certificate
used by the server). It's worth noting that the underlying
Python API used is load_verify_locations(cafile=X) and it calls
into OpenSSL's SSL_CTX_load_verify_locations(). Even OpenSSL's
documentation seems to omit that the file can contain a non-CA
certificate if it matches the server's certificate exactly. I
thought a CA certificate was a special kind of x509 certificate.
Perhaps I'm wrong and any x509 certificate can be used as a
CA certificate [as far as OpenSSL is concerned]. In any case,
I thought it best to drop "ca" from the name because this reflects
reality.
author | Gregory Szorc <gregory.szorc@gmail.com> |
---|---|
date | Tue, 07 Jun 2016 20:29:54 -0700 |
parents | cdef60d9f442 |
children | 631617262e55 |
files | mercurial/help/config.txt mercurial/sslutil.py tests/test-https.t |
diffstat | 3 files changed, 103 insertions(+), 8 deletions(-) [+] |
line wrap: on
line diff
--- a/mercurial/help/config.txt Fri May 27 23:18:38 2016 +0900 +++ b/mercurial/help/config.txt Tue Jun 07 20:29:54 2016 -0700 @@ -1024,11 +1024,39 @@ This can provide stronger security than traditional CA-based validation at the expense of convenience. + This option takes precedence over ``verifycertsfile``. + +``verifycertsfile`` + Path to file a containing a list of PEM encoded certificates used to + verify the server certificate. Environment variables and ``~user`` + constructs are expanded in the filename. + + The server certificate or the certificate's certificate authority (CA) + must match a certificate from this file or certificate verification + will fail and connections to the server will be refused. + + If defined, only certificates provided by this file will be used: + ``web.cacerts`` and any system/default certificates will not be + used. + + This option has no effect if the per-host ``fingerprints`` option + is set. + + The format of the file is as follows: + + -----BEGIN CERTIFICATE----- + ... (certificate in base64 PEM encoding) ... + -----END CERTIFICATE----- + -----BEGIN CERTIFICATE----- + ... (certificate in base64 PEM encoding) ... + -----END CERTIFICATE----- + For example:: [hostsecurity] hg.example.com:fingerprints = sha256:c3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2 hg2.example.com:fingerprints = sha1:914f1aff87249c09b6859b88b1906d30756491ca, sha1:fc:e2:8d:d9:51:cd:cb:c1:4d:18:6b:b7:44:8d:49:72:57:e6:cd:33 + foo.example.com:verifycertsfile = /etc/ssl/trusted-ca-certs.pem ``http_proxy`` --------------
--- a/mercurial/sslutil.py Fri May 27 23:18:38 2016 +0900 +++ b/mercurial/sslutil.py Tue Jun 07 20:29:54 2016 -0700 @@ -162,23 +162,42 @@ if ui.configbool('devel', 'disableloaddefaultcerts'): s['allowloaddefaultcerts'] = False + # If both fingerprints and a per-host ca file are specified, issue a warning + # because users should not be surprised about what security is or isn't + # being performed. + cafile = ui.config('hostsecurity', '%s:verifycertsfile' % hostname) + if s['certfingerprints'] and cafile: + ui.warn(_('(hostsecurity.%s:verifycertsfile ignored when host ' + 'fingerprints defined; using host fingerprints for ' + 'verification)\n') % hostname) + # Try to hook up CA certificate validation unless something above # makes it not necessary. if s['verifymode'] is None: - # Find global certificates file in config. - cafile = ui.config('web', 'cacerts') - + # Look at per-host ca file first. if cafile: cafile = util.expandpath(cafile) if not os.path.exists(cafile): - raise error.Abort(_('could not find web.cacerts: %s') % cafile) + raise error.Abort(_('path specified by %s does not exist: %s') % + ('hostsecurity.%s:verifycertsfile' % hostname, + cafile)) + s['cafile'] = cafile else: - # No global CA certs. See if we can load defaults. - cafile = _defaultcacerts() + # Find global certificates file in config. + cafile = ui.config('web', 'cacerts') + if cafile: - ui.debug('using %s to enable OS X system CA\n' % cafile) + cafile = util.expandpath(cafile) + if not os.path.exists(cafile): + raise error.Abort(_('could not find web.cacerts: %s') % + cafile) + else: + # No global CA certs. See if we can load defaults. + cafile = _defaultcacerts() + if cafile: + ui.debug('using %s to enable OS X system CA\n' % cafile) - s['cafile'] = cafile + s['cafile'] = cafile # Require certificate validation if CA certs are being loaded and # verification hasn't been disabled above.
--- a/tests/test-https.t Fri May 27 23:18:38 2016 +0900 +++ b/tests/test-https.t Tue Jun 07 20:29:54 2016 -0700 @@ -53,6 +53,54 @@ [255] #endif +Specifying a per-host certificate file that doesn't exist will abort + + $ hg --config hostsecurity.localhost:verifycertsfile=/does/not/exist clone https://localhost:$HGPORT/ + abort: path specified by hostsecurity.localhost:verifycertsfile does not exist: /does/not/exist + [255] + +A malformed per-host certificate file will raise an error + + $ echo baddata > badca.pem + $ hg --config hostsecurity.localhost:verifycertsfile=badca.pem clone https://localhost:$HGPORT/ + abort: error: unknown error* (glob) + [255] + +A per-host certificate mismatching the server will fail verification + + $ hg --config hostsecurity.localhost:verifycertsfile="$CERTSDIR/client-cert.pem" clone https://localhost:$HGPORT/ + abort: error: *certificate verify failed* (glob) + [255] + +A per-host certificate matching the server's cert will be accepted + + $ hg --config hostsecurity.localhost:verifycertsfile="$CERTSDIR/pub.pem" clone -U https://localhost:$HGPORT/ perhostgood1 + requesting all changes + adding changesets + adding manifests + adding file changes + added 1 changesets with 4 changes to 4 files + +A per-host certificate with multiple certs and one matching will be accepted + + $ cat "$CERTSDIR/client-cert.pem" "$CERTSDIR/pub.pem" > perhost.pem + $ hg --config hostsecurity.localhost:verifycertsfile=perhost.pem clone -U https://localhost:$HGPORT/ perhostgood2 + requesting all changes + adding changesets + adding manifests + adding file changes + added 1 changesets with 4 changes to 4 files + +Defining both per-host certificate and a fingerprint will print a warning + + $ hg --config hostsecurity.localhost:verifycertsfile="$CERTSDIR/pub.pem" --config hostsecurity.localhost:fingerprints=sha1:914f1aff87249c09b6859b88b1906d30756491ca clone -U https://localhost:$HGPORT/ caandfingerwarning + (hostsecurity.localhost:verifycertsfile ignored when host fingerprints defined; using host fingerprints for verification) + requesting all changes + adding changesets + adding manifests + adding file changes + added 1 changesets with 4 changes to 4 files + $ DISABLECACERTS="--config devel.disableloaddefaultcerts=true" clone via pull