|
22 | 22 | |
23 | 23 | """ |
24 | 24 |
|
25 | | -import urllib2 |
26 | 25 | import logging |
| 26 | +import os.path |
| 27 | +import socket |
27 | 28 |
|
| 29 | +import tuf |
28 | 30 | import tuf.hash |
29 | 31 | import tuf.util |
30 | 32 | import tuf.formats |
31 | 33 |
|
| 34 | +from tuf.compatibility import httplib, ssl, urllib2, urlparse |
| 35 | +if ssl: |
| 36 | + from tuf.compatibility import match_hostname |
| 37 | +else: |
| 38 | + raise tuf.Error( "No SSL support!" ) # TODO: degrade gracefully |
| 39 | + |
| 40 | + |
32 | 41 | # See 'log.py' to learn how logging is handled in TUF. |
33 | 42 | logger = logging.getLogger('tuf.download') |
34 | 43 |
|
35 | 44 |
|
| 45 | +class VerifiedHTTPSConnection( httplib.HTTPSConnection ): |
| 46 | + """ |
| 47 | + A connection that wraps connections with ssl certificate verification. |
| 48 | +
|
| 49 | + https://github.com/pypa/pip/blob/d0fa66ecc03ab20b7411b35f7c7b423f31f77761/pip/download.py#L72 |
| 50 | + """ |
| 51 | + def connect(self): |
| 52 | + |
| 53 | + self.connection_kwargs = {} |
| 54 | + |
| 55 | + #TODO: refactor compatibility logic into tuf.compatibility? |
| 56 | + |
| 57 | + # for > py2.5 |
| 58 | + if hasattr(self, 'timeout'): |
| 59 | + self.connection_kwargs.update(timeout = self.timeout) |
| 60 | + |
| 61 | + # for >= py2.7 |
| 62 | + if hasattr(self, 'source_address'): |
| 63 | + self.connection_kwargs.update(source_address = self.source_address) |
| 64 | + |
| 65 | + sock = socket.create_connection((self.host, self.port), **self.connection_kwargs) |
| 66 | + |
| 67 | + # for >= py2.7 |
| 68 | + if getattr(self, '_tunnel_host', None): |
| 69 | + self.sock = sock |
| 70 | + self._tunnel() |
| 71 | + |
| 72 | + # set location of certificate authorities |
| 73 | + assert os.path.isfile( tuf.conf.ca_certs ) |
| 74 | + cert_path = tuf.conf.ca_certs |
| 75 | + |
| 76 | + self.sock = ssl.wrap_socket(sock, |
| 77 | + self.key_file, |
| 78 | + self.cert_file, |
| 79 | + cert_reqs=ssl.CERT_REQUIRED, |
| 80 | + ca_certs=cert_path) |
| 81 | + |
| 82 | + match_hostname(self.sock.getpeercert(), self.host) |
| 83 | + |
| 84 | + |
| 85 | +class VerifiedHTTPSHandler( urllib2.HTTPSHandler ): |
| 86 | + """ |
| 87 | + A HTTPSHandler that uses our own VerifiedHTTPSConnection. |
| 88 | +
|
| 89 | + https://github.com/pypa/pip/blob/d0fa66ecc03ab20b7411b35f7c7b423f31f77761/pip/download.py#L109 |
| 90 | + """ |
| 91 | + def __init__(self, connection_class = VerifiedHTTPSConnection): |
| 92 | + self.specialized_conn_class = connection_class |
| 93 | + urllib2.HTTPSHandler.__init__(self) |
| 94 | + def https_open(self, req): |
| 95 | + return self.do_open(self.specialized_conn_class, req) |
| 96 | + |
| 97 | + |
| 98 | +def _get_request(url): |
| 99 | + """ |
| 100 | + Wraps the URL to retrieve to protects against "creative" |
| 101 | + interpretation of the RFC: http://bugs.python.org/issue8732 |
| 102 | +
|
| 103 | + https://github.com/pypa/pip/blob/d0fa66ecc03ab20b7411b35f7c7b423f31f77761/pip/download.py#L147 |
| 104 | + """ |
| 105 | + |
| 106 | + return urllib2.Request(url, headers={'Accept-encoding': 'identity'}) |
| 107 | + |
| 108 | + |
| 109 | +def _get_opener( scheme = None ): |
| 110 | + """ |
| 111 | + Build a urllib2 opener based on whether the user now wants SSL. |
| 112 | +
|
| 113 | + https://github.com/pypa/pip/blob/d0fa66ecc03ab20b7411b35f7c7b423f31f77761/pip/download.py#L178 |
| 114 | + """ |
| 115 | + |
| 116 | + if scheme == "https": |
| 117 | + assert os.path.isfile( tuf.conf.ca_certs ) |
| 118 | + |
| 119 | + # If we are going over https, use an opener which will provide SSL |
| 120 | + # certificate verification. |
| 121 | + https_handler = VerifiedHTTPSHandler() |
| 122 | + opener = urllib2.build_opener( https_handler ) |
| 123 | + |
| 124 | + # strip out HTTPHandler to prevent MITM spoof |
| 125 | + for handler in opener.handlers: |
| 126 | + if isinstance( handler, urllib2.HTTPHandler ): |
| 127 | + opener.handlers.remove( handler ) |
| 128 | + else: |
| 129 | + # Otherwise, use the default opener. |
| 130 | + opener = urllib2.build_opener() |
| 131 | + |
| 132 | + return opener |
| 133 | + |
| 134 | + |
36 | 135 | def _open_connection(url): |
37 | 136 | """ |
38 | 137 | <Purpose> |
39 | 138 | Helper function that opens a connection to the url. urllib2 supports http, |
40 | 139 | ftp, and file. In python (2.6+) where the ssl module is available, urllib2 |
41 | 140 | also supports https. |
42 | | - |
43 | | - TODO: Do proper ssl cert/name checking. |
| 141 | +
|
44 | 142 | TODO: Disallow SSLv2. |
45 | 143 | TODO: Support ssl with MCrypto. |
46 | 144 | TODO: Determine whether this follows http redirects and decide if we like |
@@ -71,11 +169,12 @@ def _open_connection(url): |
71 | 169 | # servers do not recognize connections that originates from |
72 | 170 | # Python-urllib/x.y. |
73 | 171 |
|
74 | | - request = urllib2.Request(url) |
75 | | - connection = urllib2.urlopen(request) |
76 | | - # urllib2.urlopen returns a file-like object: a handle to the remote data. |
77 | | - return connection |
| 172 | + parsed_url = urlparse.urlparse( url ) |
| 173 | + opener = _get_opener( scheme = parsed_url.scheme ) |
| 174 | + request = _get_request( url ) |
| 175 | + return opener.open( request ) |
78 | 176 | except Exception, e: |
| 177 | + raise |
79 | 178 | raise tuf.DownloadError(e) |
80 | 179 |
|
81 | 180 |
|
|
0 commit comments