|
4 | 4 | import click_log
|
5 | 5 | import pytest
|
6 | 6 | import requests
|
| 7 | +from cryptography import x509 |
| 8 | +from cryptography.hazmat.primitives import hashes |
7 | 9 |
|
8 | 10 | from vdirsyncer import http
|
9 | 11 | from vdirsyncer import utils
|
@@ -38,27 +40,55 @@ def _fingerprints_broken():
|
38 | 40 | return broken_urllib3
|
39 | 41 |
|
40 | 42 |
|
| 43 | +def fingerprint_of_cert(cert, hash=hashes.SHA256): |
| 44 | + return x509.load_pem_x509_certificate(cert.bytes()).fingerprint(hash()).hex() |
| 45 | + |
| 46 | + |
41 | 47 | @pytest.mark.skipif(
|
42 | 48 | _fingerprints_broken(), reason="https://github.com/shazow/urllib3/issues/529"
|
43 | 49 | )
|
44 |
| -@pytest.mark.parametrize( |
45 |
| - "fingerprint", |
46 |
| - [ |
47 |
| - "94:FD:7A:CB:50:75:A4:69:82:0A:F8:23:DF:07:FC:69:3E:CD:90:CA", |
48 |
| - "19:90:F7:23:94:F2:EF:AB:2B:64:2D:57:3D:25:95:2D", |
49 |
| - ], |
| 50 | +@pytest.mark.parametrize("hash_algorithm", [hashes.MD5, hashes.SHA256]) |
| 51 | +def test_request_ssl_leaf_fingerprint(httpserver, localhost_cert, hash_algorithm): |
| 52 | + fingerprint = fingerprint_of_cert(localhost_cert.cert_chain_pems[0], hash_algorithm) |
| 53 | + |
| 54 | + # We have to serve something: |
| 55 | + httpserver.expect_request("/").respond_with_data("OK") |
| 56 | + url = f"https://{httpserver.host}:{httpserver.port}/" |
| 57 | + |
| 58 | + http.request("GET", url, verify=False, verify_fingerprint=fingerprint) |
| 59 | + with pytest.raises(requests.exceptions.ConnectionError) as excinfo: |
| 60 | + http.request("GET", url, verify_fingerprint=fingerprint) |
| 61 | + |
| 62 | + with pytest.raises(requests.exceptions.ConnectionError) as excinfo: |
| 63 | + http.request( |
| 64 | + "GET", |
| 65 | + url, |
| 66 | + verify=False, |
| 67 | + verify_fingerprint="".join(reversed(fingerprint)), |
| 68 | + ) |
| 69 | + assert "Fingerprints did not match" in str(excinfo.value) |
| 70 | + |
| 71 | + |
| 72 | +@pytest.mark.skipif( |
| 73 | + _fingerprints_broken(), reason="https://github.com/shazow/urllib3/issues/529" |
50 | 74 | )
|
51 |
| -def test_request_ssl_fingerprints(httpsserver, fingerprint): |
52 |
| - httpsserver.serve_content("") # we need to serve something |
| 75 | +@pytest.mark.xfail(reason="Not implemented") |
| 76 | +@pytest.mark.parametrize("hash_algorithm", [hashes.MD5, hashes.SHA256]) |
| 77 | +def test_request_ssl_ca_fingerprint(httpserver, ca, hash_algorithm): |
| 78 | + fingerprint = fingerprint_of_cert(ca.cert_pem) |
| 79 | + |
| 80 | + # We have to serve something: |
| 81 | + httpserver.expect_request("/").respond_with_data("OK") |
| 82 | + url = f"https://{httpserver.host}:{httpserver.port}/" |
53 | 83 |
|
54 |
| - http.request("GET", httpsserver.url, verify=False, verify_fingerprint=fingerprint) |
| 84 | + http.request("GET", url, verify=False, verify_fingerprint=fingerprint) |
55 | 85 | with pytest.raises(requests.exceptions.ConnectionError) as excinfo:
|
56 |
| - http.request("GET", httpsserver.url, verify_fingerprint=fingerprint) |
| 86 | + http.request("GET", url, verify_fingerprint=fingerprint) |
57 | 87 |
|
58 | 88 | with pytest.raises(requests.exceptions.ConnectionError) as excinfo:
|
59 | 89 | http.request(
|
60 | 90 | "GET",
|
61 |
| - httpsserver.url, |
| 91 | + url, |
62 | 92 | verify=False,
|
63 | 93 | verify_fingerprint="".join(reversed(fingerprint)),
|
64 | 94 | )
|
|
0 commit comments