|
19 | 19 | import os |
20 | 20 | import base64 |
21 | 21 | import typing |
| 22 | +import six |
22 | 23 |
|
23 | 24 | from dateutil import tz |
24 | 25 | from datetime import datetime |
|
32 | 33 | from cryptography.hazmat.primitives.asymmetric.padding import PKCS1v15 |
33 | 34 | from cryptography.hazmat.primitives.hashes import SHA1 |
34 | 35 | from cryptography.exceptions import InvalidSignature |
| 36 | +from contextlib import closing |
35 | 37 |
|
36 | 38 | from .verifier_constants import ( |
37 | 39 | SIGNATURE_CERT_CHAIN_URL_HEADER, SIGNATURE_HEADER, |
@@ -285,12 +287,16 @@ def _load_cert_chain(self, cert_url, x509_backend=default_backend()): |
285 | 287 | if cert_url in self._cert_cache: |
286 | 288 | return self._cert_cache.get(cert_url) |
287 | 289 | else: |
288 | | - with urlopen(cert_url) as cert_response: |
289 | | - cert_data = cert_response.read() |
290 | | - x509_certificate = load_pem_x509_certificate( |
291 | | - cert_data, x509_backend) |
292 | | - self._cert_cache[cert_url] = x509_certificate |
293 | | - return x509_certificate |
| 290 | + if six.PY2: |
| 291 | + with closing(urlopen(cert_url)) as cert_response: |
| 292 | + cert_data = cert_response.read() |
| 293 | + else: |
| 294 | + with urlopen(cert_url) as cert_response: |
| 295 | + cert_data = cert_response.read() |
| 296 | + x509_certificate = load_pem_x509_certificate( |
| 297 | + cert_data, x509_backend) |
| 298 | + self._cert_cache[cert_url] = x509_certificate |
| 299 | + return x509_certificate |
294 | 300 | except ValueError as e: |
295 | 301 | raise VerificationException( |
296 | 302 | "Unable to load certificate from URL", e) |
|
0 commit comments