PageRenderTime 51ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/mercurial/sslutil.py

https://bitbucket.org/mirror/mercurial/
Python | 170 lines | 132 code | 15 blank | 23 comment | 40 complexity | 5f29c7af68f15e28d2c305532c57c0bc MD5 | raw file
Possible License(s): GPL-2.0
  1. # sslutil.py - SSL handling for mercurial
  2. #
  3. # Copyright 2005, 2006, 2007, 2008 Matt Mackall <mpm@selenic.com>
  4. # Copyright 2006, 2007 Alexis S. L. Carvalho <alexis@cecm.usp.br>
  5. # Copyright 2006 Vadim Gelfer <vadim.gelfer@gmail.com>
  6. #
  7. # This software may be used and distributed according to the terms of the
  8. # GNU General Public License version 2 or any later version.
  9. import os
  10. from mercurial import util
  11. from mercurial.i18n import _
  12. try:
  13. # avoid using deprecated/broken FakeSocket in python 2.6
  14. import ssl
  15. CERT_REQUIRED = ssl.CERT_REQUIRED
  16. PROTOCOL_SSLv23 = ssl.PROTOCOL_SSLv23
  17. PROTOCOL_TLSv1 = ssl.PROTOCOL_TLSv1
  18. def ssl_wrap_socket(sock, keyfile, certfile, ssl_version=PROTOCOL_TLSv1,
  19. cert_reqs=ssl.CERT_NONE, ca_certs=None):
  20. sslsocket = ssl.wrap_socket(sock, keyfile, certfile,
  21. cert_reqs=cert_reqs, ca_certs=ca_certs,
  22. ssl_version=ssl_version)
  23. # check if wrap_socket failed silently because socket had been closed
  24. # - see http://bugs.python.org/issue13721
  25. if not sslsocket.cipher():
  26. raise util.Abort(_('ssl connection failed'))
  27. return sslsocket
  28. except ImportError:
  29. CERT_REQUIRED = 2
  30. PROTOCOL_SSLv23 = 2
  31. PROTOCOL_TLSv1 = 3
  32. import socket, httplib
  33. def ssl_wrap_socket(sock, keyfile, certfile, ssl_version=PROTOCOL_TLSv1,
  34. cert_reqs=CERT_REQUIRED, ca_certs=None):
  35. if not util.safehasattr(socket, 'ssl'):
  36. raise util.Abort(_('Python SSL support not found'))
  37. if ca_certs:
  38. raise util.Abort(_(
  39. 'certificate checking requires Python 2.6'))
  40. ssl = socket.ssl(sock, keyfile, certfile)
  41. return httplib.FakeSocket(sock, ssl)
  42. def _verifycert(cert, hostname):
  43. '''Verify that cert (in socket.getpeercert() format) matches hostname.
  44. CRLs is not handled.
  45. Returns error message if any problems are found and None on success.
  46. '''
  47. if not cert:
  48. return _('no certificate received')
  49. dnsname = hostname.lower()
  50. def matchdnsname(certname):
  51. return (certname == dnsname or
  52. '.' in dnsname and certname == '*.' + dnsname.split('.', 1)[1])
  53. san = cert.get('subjectAltName', [])
  54. if san:
  55. certnames = [value.lower() for key, value in san if key == 'DNS']
  56. for name in certnames:
  57. if matchdnsname(name):
  58. return None
  59. if certnames:
  60. return _('certificate is for %s') % ', '.join(certnames)
  61. # subject is only checked when subjectAltName is empty
  62. for s in cert.get('subject', []):
  63. key, value = s[0]
  64. if key == 'commonName':
  65. try:
  66. # 'subject' entries are unicode
  67. certname = value.lower().encode('ascii')
  68. except UnicodeEncodeError:
  69. return _('IDN in certificate not supported')
  70. if matchdnsname(certname):
  71. return None
  72. return _('certificate is for %s') % certname
  73. return _('no commonName or subjectAltName found in certificate')
  74. # CERT_REQUIRED means fetch the cert from the server all the time AND
  75. # validate it against the CA store provided in web.cacerts.
  76. #
  77. # We COMPLETELY ignore CERT_REQUIRED on Python <= 2.5, as it's totally
  78. # busted on those versions.
  79. def sslkwargs(ui, host):
  80. cacerts = ui.config('web', 'cacerts')
  81. forcetls = ui.configbool('ui', 'tls', default=True)
  82. if forcetls:
  83. ssl_version = PROTOCOL_TLSv1
  84. else:
  85. ssl_version = PROTOCOL_SSLv23
  86. hostfingerprint = ui.config('hostfingerprints', host)
  87. kws = {'ssl_version': ssl_version,
  88. }
  89. if cacerts and not hostfingerprint:
  90. cacerts = util.expandpath(cacerts)
  91. if not os.path.exists(cacerts):
  92. raise util.Abort(_('could not find web.cacerts: %s') % cacerts)
  93. kws.update({'ca_certs': cacerts,
  94. 'cert_reqs': CERT_REQUIRED,
  95. })
  96. return kws
  97. class validator(object):
  98. def __init__(self, ui, host):
  99. self.ui = ui
  100. self.host = host
  101. def __call__(self, sock, strict=False):
  102. host = self.host
  103. cacerts = self.ui.config('web', 'cacerts')
  104. hostfingerprint = self.ui.config('hostfingerprints', host)
  105. if not getattr(sock, 'getpeercert', False): # python 2.5 ?
  106. if hostfingerprint:
  107. raise util.Abort(_("host fingerprint for %s can't be "
  108. "verified (Python too old)") % host)
  109. if strict:
  110. raise util.Abort(_("certificate for %s can't be verified "
  111. "(Python too old)") % host)
  112. if self.ui.configbool('ui', 'reportoldssl', True):
  113. self.ui.warn(_("warning: certificate for %s can't be verified "
  114. "(Python too old)\n") % host)
  115. return
  116. if not sock.cipher(): # work around http://bugs.python.org/issue13721
  117. raise util.Abort(_('%s ssl connection error') % host)
  118. try:
  119. peercert = sock.getpeercert(True)
  120. peercert2 = sock.getpeercert()
  121. except AttributeError:
  122. raise util.Abort(_('%s ssl connection error') % host)
  123. if not peercert:
  124. raise util.Abort(_('%s certificate error: '
  125. 'no certificate received') % host)
  126. peerfingerprint = util.sha1(peercert).hexdigest()
  127. nicefingerprint = ":".join([peerfingerprint[x:x + 2]
  128. for x in xrange(0, len(peerfingerprint), 2)])
  129. if hostfingerprint:
  130. if peerfingerprint.lower() != \
  131. hostfingerprint.replace(':', '').lower():
  132. raise util.Abort(_('certificate for %s has unexpected '
  133. 'fingerprint %s') % (host, nicefingerprint),
  134. hint=_('check hostfingerprint configuration'))
  135. self.ui.debug('%s certificate matched fingerprint %s\n' %
  136. (host, nicefingerprint))
  137. elif cacerts:
  138. msg = _verifycert(peercert2, host)
  139. if msg:
  140. raise util.Abort(_('%s certificate error: %s') % (host, msg),
  141. hint=_('configure hostfingerprint %s or use '
  142. '--insecure to connect insecurely') %
  143. nicefingerprint)
  144. self.ui.debug('%s certificate successfully verified\n' % host)
  145. elif strict:
  146. raise util.Abort(_('%s certificate with fingerprint %s not '
  147. 'verified') % (host, nicefingerprint),
  148. hint=_('check hostfingerprints or web.cacerts '
  149. 'config setting'))
  150. else:
  151. self.ui.warn(_('warning: %s certificate with fingerprint %s not '
  152. 'verified (check hostfingerprints or web.cacerts '
  153. 'config setting)\n') %
  154. (host, nicefingerprint))