-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathtest_verify.py
More file actions
292 lines (249 loc) · 10.6 KB
/
Copy pathtest_verify.py
File metadata and controls
292 lines (249 loc) · 10.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import pytest
import shutil
from pathlib import Path
from manage.urlutils import IndexDownloader
from manage.exceptions import InvalidFeedError
from _native import verify_trust
TESTDATA = Path(__file__).absolute().parent / "data"
INDEX_NAMES = ["index-windows.json", "index-windows-recent.json", "index-windows-legacy.json"]
TEST_INDEX = str(TESTDATA / INDEX_NAMES[0])
TEST_INDEX_URI = (TESTDATA / INDEX_NAMES[0]).as_uri()
TEST_CAT = str(TESTDATA / "psf-signed.cat")
UNTRUSTED_CAT = str(TESTDATA / "self-signed.cat")
AZURE_SIGN_SUBJECT = "CN=Microsoft Identity Verification Root Certificate Authority 2020, O=Microsoft Corporation, C=US"
PSF_SIGN_SUBJECT = "CN=Python Software Foundation, O=Python Software Foundation, L=Beaverton, S=Oregon, C=US"
PSF_SIGN_EKU = "1.3.6.1.4.1.311.97.608394634.79987812.305991749.578777327"
def test_verify_all_fields():
verify_trust(TEST_INDEX, TEST_CAT, AZURE_SIGN_SUBJECT, PSF_SIGN_SUBJECT, PSF_SIGN_EKU)
def test_verify_leaf():
verify_trust(TEST_INDEX, TEST_CAT, None, PSF_SIGN_SUBJECT, None)
def test_verify_partial_leaf():
verify_trust(TEST_INDEX, TEST_CAT, None, "CN=Python Software Foundation", None)
verify_trust(TEST_INDEX, TEST_CAT, None, "C=US", None)
subj = ",".join(sorted(PSF_SIGN_SUBJECT.split(","), reverse=True))
verify_trust(TEST_INDEX, TEST_CAT, None, subj, None)
def test_verify_eku():
verify_trust(TEST_INDEX, TEST_CAT, None, None, PSF_SIGN_EKU)
def test_verify_root():
verify_trust(TEST_INDEX, TEST_CAT, AZURE_SIGN_SUBJECT, None, None)
def test_verify_os_only():
verify_trust(TEST_INDEX, TEST_CAT, None, None, None)
def test_verify_invalid_leaf():
with pytest.raises(OSError):
verify_trust(TEST_INDEX, TEST_CAT, None, "CN=King Arthur", None)
def test_verify_invalid_eku():
with pytest.raises(OSError):
verify_trust(TEST_INDEX, TEST_CAT, None, None, "1.2.3.4.5")
def test_verify_invalid_root():
with pytest.raises(OSError):
verify_trust(TEST_INDEX, TEST_CAT, "CN=King Arthur", None, None)
def test_verify_unknown_cert():
with pytest.raises(OSError):
verify_trust(TEST_INDEX, UNTRUSTED_CAT, None, None, None)
def test_verify_tampered_file(tmp_path):
p = tmp_path / "test_file.json"
p.write_text("This is not the expected contents")
with pytest.raises(OSError):
verify_trust(str(p), TEST_CAT, None, None, None)
class MockConfig:
REQUIRE_FULL = dict(
requires_signature=True,
required_root_subject=AZURE_SIGN_SUBJECT,
required_publisher_subject=PSF_SIGN_SUBJECT,
required_publisher_eku=PSF_SIGN_EKU,
)
REQUIRE_ROOT = dict(
requires_signature=True,
required_root_subject=AZURE_SIGN_SUBJECT,
)
REQUIRE_LEAF = dict(
requires_signature=True,
required_publisher_subject=PSF_SIGN_SUBJECT,
required_publisher_eku=PSF_SIGN_EKU,
)
REQUIRE_VALID = dict(
requires_signature=True,
)
REQUIRE_FULL_WRONG = dict(
requires_signature=True,
required_root_subject="C=Camelot",
required_publisher_subject="CN=King Arthur, O=Knights of the Round Table, C=Camelot",
required_publisher_eku="1.2.3.4.5",
)
REQUIRE_ROOT_WRONG = dict(
requires_signature=True,
required_root_subject="CN=King Arthur, O=Knights of the Round Table, C=Camelot",
)
REQUIRE_LEAF_WRONG = dict(
requires_signature=True,
required_publisher_subject="CN=King Arthur, O=Knights of the Round Table, C=Camelot",
required_publisher_eku="1.2.3.4.5",
)
NOT_REQUIRED = dict(
requires_signature=False,
required_root_subject=AZURE_SIGN_SUBJECT,
required_publisher_subject=PSF_SIGN_SUBJECT,
required_publisher_eku=PSF_SIGN_EKU,
)
def __init__(self):
self.asked = []
self.response = True
self.source_settings = {}
def ask_ny(self, question):
self.asked.append(question)
return self.response
class MockIndex:
def __init__(self, url, data):
self.url = url
self.data = data
@pytest.mark.parametrize("verify_with", [
pytest.param(MockConfig.REQUIRE_FULL, id="full"),
pytest.param(MockConfig.REQUIRE_ROOT, id="root"),
pytest.param(MockConfig.REQUIRE_LEAF, id="publisher"),
pytest.param(MockConfig.REQUIRE_VALID, id="only-valid"),
pytest.param(MockConfig.NOT_REQUIRED, id="unchecked"),
])
def test_verify_index_ok(verify_with, tmp_path, assert_log):
# All settings result in successful match
cmd = MockConfig()
for src, dest in [(TESTDATA / n, tmp_path / n) for n in INDEX_NAMES]:
shutil.copy2(src, dest)
shutil.copy2(TEST_CAT, dest.with_suffix(".json.cat"))
cmd.source_settings[dest.as_uri()] = verify_with
idx = IndexDownloader(cmd, (tmp_path / INDEX_NAMES[0]).as_uri(), MockIndex)
indexes = list(idx)
assert [Path(i.url).name for i in indexes] == INDEX_NAMES
@pytest.mark.parametrize("verify_with", [
pytest.param(MockConfig.REQUIRE_FULL_WRONG, id="full"),
pytest.param(MockConfig.REQUIRE_ROOT_WRONG, id="root"),
pytest.param(MockConfig.REQUIRE_LEAF_WRONG, id="publisher"),
])
def test_verify_index_wrong(verify_with, tmp_path, assert_log):
# Certs exist and verify, but don't match required settings
cmd = MockConfig()
for src, dest in [(TESTDATA / n, tmp_path / n) for n in INDEX_NAMES]:
shutil.copy2(src, dest)
shutil.copy2(TEST_CAT, dest.with_suffix(".json.cat"))
cmd.source_settings[dest.as_uri()] = verify_with
idx = IndexDownloader(cmd, (tmp_path / INDEX_NAMES[0]).as_uri(), MockIndex)
with pytest.raises(InvalidFeedError):
indexes = list(idx)
assert_log(
"Fetching.+",
"The signature for %s could not be verified.",
)
@pytest.mark.parametrize("verify_with, expect_fail", [
pytest.param(MockConfig.REQUIRE_FULL, True, id="full"),
pytest.param(MockConfig.REQUIRE_ROOT, True, id="root"),
pytest.param(MockConfig.REQUIRE_LEAF, True, id="publisher"),
pytest.param(MockConfig.REQUIRE_VALID, True, id="only-valid"),
pytest.param(MockConfig.NOT_REQUIRED, False, id="unchecked"),
])
def test_verify_index_unsigned(verify_with, expect_fail, tmp_path, assert_log):
# No certs exist, so mostly fail due to being required
cmd = MockConfig()
for src, dest in [(TESTDATA / n, tmp_path / n) for n in INDEX_NAMES]:
shutil.copy2(src, dest)
cmd.source_settings[dest.as_uri()] = verify_with
idx = IndexDownloader(cmd, (tmp_path / INDEX_NAMES[0]).as_uri(), MockIndex)
if expect_fail:
with pytest.raises(InvalidFeedError):
indexes = list(idx)
assert_log(
"Fetching.+",
"The signature for %s could not be loaded."
)
else:
indexes = list(idx)
assert [Path(i.url).name for i in indexes] == INDEX_NAMES
@pytest.mark.parametrize("verify_with", [
pytest.param(MockConfig.REQUIRE_FULL, id="full"),
pytest.param(MockConfig.REQUIRE_ROOT, id="root"),
pytest.param(MockConfig.REQUIRE_LEAF, id="publisher"),
pytest.param(MockConfig.REQUIRE_VALID, id="only-valid"),
])
def test_verify_index_selfsigned_bypass(verify_with, tmp_path, assert_log):
# Invalid cert, but user "responds" to continue
cmd = MockConfig()
cmd.response = False
for src, dest in [(TESTDATA / n, tmp_path / n) for n in INDEX_NAMES]:
shutil.copy2(src, dest)
shutil.copy2(UNTRUSTED_CAT, dest.with_suffix(".json.cat"))
cmd.source_settings[dest.as_uri()] = verify_with
idx = IndexDownloader(cmd, (tmp_path / INDEX_NAMES[0]).as_uri(), MockIndex)
indexes = list(idx)
assert [Path(i.url).name for i in indexes] == INDEX_NAMES
assert_log(
"Fetching.+",
"The signature for %s could not be verified.",
"TRACEBACK",
"Signature verification failure ignored for %s",
)
@pytest.mark.parametrize("verify_with, expect_fail", [
pytest.param(MockConfig.REQUIRE_FULL, True, id="full"),
# The 'wrong' settings actually match the selfsigned cert we're testing with
pytest.param(MockConfig.REQUIRE_ROOT, True, id="wrong-root"),
pytest.param(MockConfig.REQUIRE_ROOT_WRONG, True, id="root"),
pytest.param(MockConfig.REQUIRE_LEAF, True, id="wrong-publisher"),
pytest.param(MockConfig.REQUIRE_LEAF_WRONG, True, id="publisher"),
pytest.param(MockConfig.REQUIRE_VALID, True, id="only-valid"),
pytest.param(MockConfig.NOT_REQUIRED, False, id="unchecked"),
])
def test_verify_index_selfsigned(verify_with, expect_fail, tmp_path, assert_log):
# Wrong cert returned, mostly fail due to being untrusted by OS
cmd = MockConfig()
for src, dest in [(TESTDATA / n, tmp_path / n) for n in INDEX_NAMES]:
shutil.copy2(src, dest)
shutil.copy2(UNTRUSTED_CAT, dest.with_suffix(".json.cat"))
cmd.source_settings[dest.as_uri()] = verify_with
idx = IndexDownloader(cmd, (tmp_path / INDEX_NAMES[0]).as_uri(), MockIndex)
if expect_fail:
with pytest.raises(InvalidFeedError):
indexes = list(idx)
assert_log(
"Fetching.+",
"The signature for %s could not be verified."
)
else:
indexes = list(idx)
assert [Path(i.url).name for i in indexes] == INDEX_NAMES
def test_verify_index_later(assert_log):
# Signature not required until reading the index file
cmd = MockConfig()
u = (TESTDATA / "index-require-sig.json").as_uri()
expect_settings = {
u: {
"requires_signature": True,
"required_publisher_subject": "CN=King Arthur, O=Knights of the Round Table, C=Camelot",
}
}
idx = IndexDownloader(cmd, u, MockIndex)
with pytest.raises(InvalidFeedError):
indexes = list(idx)
assert_log(
"Fetching.+",
"Using verification settings from the index.",
"Check the log file.+",
("Verifying with the below settings.+", [expect_settings]),
"The signature for %s could not be loaded.",
)
def test_verify_index_overridden_later(assert_log):
# Signature not required until reading the index file
cmd = MockConfig()
u = (TESTDATA / "index-require-sig.json").as_uri()
cmd.source_settings[u] = {"requires_signature": False}
idx = IndexDownloader(cmd, u, MockIndex)
indexes = list(idx)
assert_log(
"Fetching.+",
"No signature to verify for %s",
)
def test_verify_index_not_later(assert_log):
# Signature not required until reading the index file
cmd = MockConfig()
idx = IndexDownloader(cmd, (TESTDATA / "index-require-no-sig.json").as_uri(), MockIndex)
indexes = list(idx)
assert_log(
"Fetching.+",
"No signature to verify for %s",
)