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 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570
|
import datetime
import os
import pytest
from asn1crypto import cms, crl, ocsp, x509
from pyhanko_certvalidator import validate
from pyhanko_certvalidator.authority import CertTrustAnchor
from pyhanko_certvalidator.context import ACTargetDescription, ValidationContext
from pyhanko_certvalidator.errors import (
InvalidCertificateError,
PathValidationError,
RevokedError,
)
from pyhanko_certvalidator.path import ValidationPath
from pyhanko_certvalidator.revinfo.validate_crl import verify_crl
from pyhanko_certvalidator.revinfo.validate_ocsp import verify_ocsp_response
from .test_validate import FIXTURES_DIR
attr_cert_dir = os.path.join(FIXTURES_DIR, 'attribute-certs')
basic_aa_dir = os.path.join(attr_cert_dir, 'basic-aa')
def load_cert(fname) -> x509.Certificate:
with open(fname, 'rb') as inf:
return x509.Certificate.load(inf.read())
def load_attr_cert(fname) -> cms.AttributeCertificateV2:
with open(fname, 'rb') as inf:
return cms.AttributeCertificateV2.load(inf.read())
def load_crl(fname) -> crl.CertificateList:
with open(fname, 'rb') as inf:
return crl.CertificateList.load(inf.read())
def load_ocsp_response(fname) -> ocsp.OCSPResponse:
with open(fname, 'rb') as inf:
return ocsp.OCSPResponse.load(inf.read())
@pytest.mark.asyncio
async def test_basic_ac_validation_aacontrols_norev():
ac = load_attr_cert(
os.path.join(basic_aa_dir, 'aa', 'alice-role-norev.attr.crt')
)
root = load_cert(os.path.join(basic_aa_dir, 'root', 'root.crt'))
interm = load_cert(os.path.join(basic_aa_dir, 'root', 'interm-role.crt'))
role_aa = load_cert(os.path.join(basic_aa_dir, 'interm', 'role-aa.crt'))
vc = ValidationContext(
trust_roots=[root],
other_certs=[interm, role_aa],
)
result = await validate.async_validate_ac(ac, vc)
assert len(result.aa_path) == 3
assert 'role' in result.approved_attributes
assert 'group' not in result.approved_attributes
@pytest.mark.asyncio
async def test_basic_ac_validation_bad_signature():
ac = load_attr_cert(os.path.join(basic_aa_dir, 'aa', 'badsig.attr.crt'))
root = load_cert(os.path.join(basic_aa_dir, 'root', 'root.crt'))
interm = load_cert(os.path.join(basic_aa_dir, 'root', 'interm-role.crt'))
role_aa = load_cert(os.path.join(basic_aa_dir, 'interm', 'role-aa.crt'))
vc = ValidationContext(
trust_roots=[root],
other_certs=[interm, role_aa],
)
msg = 'signature could not be verified'
with pytest.raises(InvalidCertificateError, match=msg):
await validate.async_validate_ac(ac, vc)
@pytest.mark.asyncio
async def test_ac_validation_expired():
ac = load_attr_cert(
os.path.join(basic_aa_dir, 'aa', 'alice-role-norev.attr.crt')
)
root = load_cert(os.path.join(basic_aa_dir, 'root', 'root.crt'))
interm = load_cert(os.path.join(basic_aa_dir, 'root', 'interm-role.crt'))
role_aa = load_cert(os.path.join(basic_aa_dir, 'interm', 'role-aa.crt'))
vc = ValidationContext(
trust_roots=[root],
other_certs=[interm, role_aa],
moment=datetime.datetime(3000, 1, 1, tzinfo=datetime.timezone.utc),
)
msg = 'intermediate certificate 1 expired'
with pytest.raises(PathValidationError, match=msg):
await validate.async_validate_ac(ac, vc)
@pytest.mark.asyncio
async def test_basic_ac_validation_sig_algo_mismatch():
ac = load_attr_cert(os.path.join(basic_aa_dir, 'aa', 'badsig.attr.crt'))
# manipulate the signature algorithm
ac = cms.AttributeCertificateV2(
{
'ac_info': ac['ac_info'],
'signature_algorithm': {'algorithm': 'md5_rsa'},
'signature': ac['signature'],
}
)
ac.dump()
root = load_cert(os.path.join(basic_aa_dir, 'root', 'root.crt'))
interm = load_cert(os.path.join(basic_aa_dir, 'root', 'interm-role.crt'))
role_aa = load_cert(os.path.join(basic_aa_dir, 'interm', 'role-aa.crt'))
vc = ValidationContext(
trust_roots=[root],
other_certs=[interm, role_aa],
)
msg = 'algorithm declaration.*does not match'
with pytest.raises(InvalidCertificateError, match=msg):
await validate.async_validate_ac(ac, vc)
@pytest.mark.asyncio
async def test_basic_ac_validation_bad_aa_controls():
ac = load_attr_cert(
os.path.join(basic_aa_dir, 'aa', 'alice-role-norev.attr.crt')
)
root = load_cert(os.path.join(basic_aa_dir, 'root', 'root.crt'))
# no AA controls on this one
interm = load_cert(
os.path.join(basic_aa_dir, 'root', 'interm-unrestricted.crt')
)
role_aa = load_cert(os.path.join(basic_aa_dir, 'interm', 'role-aa.crt'))
vc = ValidationContext(
trust_roots=[root],
other_certs=[interm, role_aa],
)
msg = 'AA controls extension only present on part '
with pytest.raises(PathValidationError, match=msg):
await validate.async_validate_ac(ac, vc)
@pytest.mark.asyncio
async def test_basic_ac_validation_aa_controls_path_too_long():
ac = load_attr_cert(
os.path.join(basic_aa_dir, 'aa', 'alice-role-norev.attr.crt')
)
root = load_cert(os.path.join(basic_aa_dir, 'root', 'root.crt'))
# no AA controls on this one
interm = load_cert(
os.path.join(basic_aa_dir, 'inbetween', 'interm-pathlen-violation.crt')
)
inbetween = load_cert(
os.path.join(basic_aa_dir, 'root', 'inbetween-aa.crt')
)
role_aa = load_cert(os.path.join(basic_aa_dir, 'interm', 'role-aa.crt'))
vc = ValidationContext(
trust_roots=[root],
other_certs=[interm, role_aa, inbetween],
)
msg = 'exceeds the maximum path length for an AA certificate'
with pytest.raises(PathValidationError, match=msg):
await validate.async_validate_ac(ac, vc)
def _load_targeted_ac():
ac = load_attr_cert(
os.path.join(basic_aa_dir, 'aa', 'alice-norev-targeted.attr.crt')
)
root = load_cert(os.path.join(basic_aa_dir, 'root', 'root.crt'))
interm = load_cert(
os.path.join(basic_aa_dir, 'root', 'interm-unrestricted.crt')
)
aa = load_cert(os.path.join(basic_aa_dir, 'interm', 'aa-unrestricted.crt'))
return root, interm, aa, ac
@pytest.mark.asyncio
async def test_basic_ac_validation_no_targeting():
root, interm, aa, ac = _load_targeted_ac()
vc = ValidationContext(
trust_roots=[root],
other_certs=[interm, aa],
)
msg = 'no targeting information'
with pytest.raises(InvalidCertificateError, match=msg):
await validate.async_validate_ac(ac, vc)
@pytest.mark.asyncio
async def test_basic_ac_validation_bad_targeting_name():
root, interm, aa, ac = _load_targeted_ac()
vc = ValidationContext(
trust_roots=[root],
other_certs=[interm, aa],
acceptable_ac_targets=ACTargetDescription(
validator_names=[
x509.GeneralName(
name='directory_name',
value=x509.Name.build(
{
'country_name': 'XX',
'organization_name': 'Testing Attribute Authority',
'organizational_unit_name': 'Validators',
'common_name': 'Not Validator',
}
),
)
]
),
)
msg = 'AC targeting'
with pytest.raises(InvalidCertificateError, match=msg):
await validate.async_validate_ac(ac, vc)
@pytest.mark.asyncio
async def test_basic_ac_validation_bad_targeting_group():
root, interm, aa, ac = _load_targeted_ac()
vc = ValidationContext(
trust_roots=[root],
other_certs=[interm, aa],
acceptable_ac_targets=ACTargetDescription(
group_memberships=[
x509.GeneralName(
name='directory_name',
value=x509.Name.build(
{
'country_name': 'XX',
'organization_name': 'Testing Attribute Authority',
'organizational_unit_name': 'Not Validators',
}
),
)
]
),
)
msg = 'AC targeting'
with pytest.raises(InvalidCertificateError, match=msg):
await validate.async_validate_ac(ac, vc)
@pytest.mark.asyncio
async def test_basic_ac_validation_good_targeting_name():
root, interm, aa, ac = _load_targeted_ac()
vc = ValidationContext(
trust_roots=[root],
other_certs=[interm, aa],
acceptable_ac_targets=ACTargetDescription(
validator_names=[
x509.GeneralName(
name='directory_name',
value=x509.Name.build(
{
'country_name': 'XX',
'organization_name': 'Testing Attribute Authority',
'organizational_unit_name': 'Validators',
'common_name': 'Validator',
}
),
)
]
),
)
result = await validate.async_validate_ac(ac, vc)
assert len(result.aa_path) == 3
assert 'role' in result.approved_attributes
assert 'group' in result.approved_attributes
@pytest.mark.asyncio
async def test_basic_ac_validation_good_targeting_group():
root, interm, aa, ac = _load_targeted_ac()
vc = ValidationContext(
trust_roots=[root],
other_certs=[interm, aa],
acceptable_ac_targets=ACTargetDescription(
group_memberships=[
x509.GeneralName(
name='directory_name',
value=x509.Name.build(
{
'country_name': 'XX',
'organization_name': 'Testing Attribute Authority',
'organizational_unit_name': 'Validators',
}
),
)
]
),
)
result = await validate.async_validate_ac(ac, vc)
assert len(result.aa_path) == 3
assert 'role' in result.approved_attributes
assert 'group' in result.approved_attributes
@pytest.mark.asyncio
async def test_match_holder_ac():
ac = load_attr_cert(
os.path.join(basic_aa_dir, 'aa', 'alice-role-norev.attr.crt')
)
root = load_cert(os.path.join(basic_aa_dir, 'root', 'root.crt'))
interm = load_cert(os.path.join(basic_aa_dir, 'root', 'interm-role.crt'))
role_aa = load_cert(os.path.join(basic_aa_dir, 'interm', 'role-aa.crt'))
alice = load_cert(os.path.join(basic_aa_dir, 'people-ca', 'alice.crt'))
vc = ValidationContext(
trust_roots=[root],
other_certs=[interm, role_aa],
)
await validate.async_validate_ac(ac, vc, holder_cert=alice)
@pytest.mark.asyncio
async def test_match_holder_ac_mismatch():
ac = load_attr_cert(
os.path.join(basic_aa_dir, 'aa', 'alice-role-norev.attr.crt')
)
root = load_cert(os.path.join(basic_aa_dir, 'root', 'root.crt'))
interm = load_cert(os.path.join(basic_aa_dir, 'root', 'interm-role.crt'))
role_aa = load_cert(os.path.join(basic_aa_dir, 'interm', 'role-aa.crt'))
bob = load_cert(os.path.join(basic_aa_dir, 'people-ca', 'bob.crt'))
vc = ValidationContext(
trust_roots=[root],
other_certs=[interm, role_aa],
)
msg = 'Could not match.*base_certificate_id'
with pytest.raises(InvalidCertificateError, match=msg):
await validate.async_validate_ac(ac, vc, holder_cert=bob)
@pytest.mark.asyncio
async def test_ac_revoked():
ac = load_attr_cert(
os.path.join(basic_aa_dir, 'aa', 'alice-role-with-rev.attr.crt')
)
root = load_cert(os.path.join(basic_aa_dir, 'root', 'root.crt'))
interm = load_cert(os.path.join(basic_aa_dir, 'root', 'interm-role.crt'))
role_aa = load_cert(os.path.join(basic_aa_dir, 'interm', 'role-aa.crt'))
role_aa_crl = load_crl(
os.path.join(basic_aa_dir, 'role-aa-some-revoked.crl')
)
vc = ValidationContext(
trust_roots=[root],
other_certs=[interm, role_aa],
crls=[role_aa_crl],
moment=datetime.datetime(
year=2021, month=12, day=12, tzinfo=datetime.timezone.utc
),
)
ac_path = ValidationPath(CertTrustAnchor(root), [interm, role_aa], ac)
with pytest.raises(RevokedError):
await verify_crl(ac, ac_path, vc)
@pytest.mark.asyncio
async def test_ac_unrevoked():
ac = load_attr_cert(
os.path.join(basic_aa_dir, 'aa', 'alice-role-with-rev.attr.crt')
)
root = load_cert(os.path.join(basic_aa_dir, 'root', 'root.crt'))
interm = load_cert(os.path.join(basic_aa_dir, 'root', 'interm-role.crt'))
role_aa = load_cert(os.path.join(basic_aa_dir, 'interm', 'role-aa.crt'))
role_aa_crl = load_crl(os.path.join(basic_aa_dir, 'role-aa-all-good.crl'))
vc = ValidationContext(
trust_roots=[root],
other_certs=[interm, role_aa],
crls=[role_aa_crl],
moment=datetime.datetime(
year=2019, month=12, day=12, tzinfo=datetime.timezone.utc
),
)
ac_path = ValidationPath(CertTrustAnchor(root), [interm, role_aa], ac)
await verify_crl(ac, ac_path, vc)
@pytest.mark.asyncio
async def test_ac_revoked_full_path_validation():
ac = load_attr_cert(
os.path.join(basic_aa_dir, 'aa', 'alice-role-with-rev.attr.crt')
)
root = load_cert(os.path.join(basic_aa_dir, 'root', 'root.crt'))
interm = load_cert(os.path.join(basic_aa_dir, 'root', 'interm-role.crt'))
role_aa = load_cert(os.path.join(basic_aa_dir, 'interm', 'role-aa.crt'))
role_aa_crl = load_crl(
os.path.join(basic_aa_dir, 'role-aa-some-revoked.crl')
)
vc = ValidationContext(
trust_roots=[root],
other_certs=[interm, role_aa],
crls=[role_aa_crl],
moment=datetime.datetime(
year=2021, month=12, day=12, tzinfo=datetime.timezone.utc
),
)
with pytest.raises(RevokedError):
await validate.async_validate_ac(ac, vc)
@pytest.mark.asyncio
async def test_ac_unrevoked_full_path_validation():
ac = load_attr_cert(
os.path.join(basic_aa_dir, 'aa', 'alice-role-with-rev.attr.crt')
)
root = load_cert(os.path.join(basic_aa_dir, 'root', 'root.crt'))
interm = load_cert(os.path.join(basic_aa_dir, 'root', 'interm-role.crt'))
role_aa = load_cert(os.path.join(basic_aa_dir, 'interm', 'role-aa.crt'))
role_aa_crl = load_crl(os.path.join(basic_aa_dir, 'role-aa-all-good.crl'))
vc = ValidationContext(
trust_roots=[root],
other_certs=[interm, role_aa],
crls=[role_aa_crl],
moment=datetime.datetime(
year=2019, month=12, day=12, tzinfo=datetime.timezone.utc
),
)
await validate.async_validate_ac(ac, vc)
@pytest.mark.asyncio
async def test_ac_revoked():
ac = load_attr_cert(
os.path.join(basic_aa_dir, 'aa', 'alice-role-with-rev.attr.crt')
)
root = load_cert(os.path.join(basic_aa_dir, 'root', 'root.crt'))
interm = load_cert(os.path.join(basic_aa_dir, 'root', 'interm-role.crt'))
role_aa = load_cert(os.path.join(basic_aa_dir, 'interm', 'role-aa.crt'))
ocsp_resp = load_ocsp_response(
os.path.join(basic_aa_dir, 'alice-revoked.ors')
)
vc = ValidationContext(
trust_roots=[root],
other_certs=[interm, role_aa],
ocsps=[ocsp_resp],
moment=datetime.datetime(
year=2021, month=12, day=12, tzinfo=datetime.timezone.utc
),
)
ac_path = ValidationPath(CertTrustAnchor(root), [interm, role_aa], ac)
with pytest.raises(RevokedError):
await verify_ocsp_response(ac, ac_path, vc)
@pytest.mark.asyncio
async def test_ac_unrevoked():
ac = load_attr_cert(
os.path.join(basic_aa_dir, 'aa', 'alice-role-with-rev.attr.crt')
)
root = load_cert(os.path.join(basic_aa_dir, 'root', 'root.crt'))
interm = load_cert(os.path.join(basic_aa_dir, 'root', 'interm-role.crt'))
role_aa = load_cert(os.path.join(basic_aa_dir, 'interm', 'role-aa.crt'))
ocsp_resp = load_ocsp_response(
os.path.join(basic_aa_dir, 'alice-all-good.ors')
)
vc = ValidationContext(
trust_roots=[root],
other_certs=[interm, role_aa],
ocsps=[ocsp_resp],
moment=datetime.datetime(
year=2019, month=12, day=12, tzinfo=datetime.timezone.utc
),
)
ac_path = ValidationPath(CertTrustAnchor(root), [interm, role_aa], ac)
await verify_ocsp_response(ac, ac_path, vc)
@pytest.mark.asyncio
async def test_ac_revoked_full_path_validation():
ac = load_attr_cert(
os.path.join(basic_aa_dir, 'aa', 'alice-role-with-rev.attr.crt')
)
root = load_cert(os.path.join(basic_aa_dir, 'root', 'root.crt'))
interm = load_cert(os.path.join(basic_aa_dir, 'root', 'interm-role.crt'))
role_aa = load_cert(os.path.join(basic_aa_dir, 'interm', 'role-aa.crt'))
ocsp_resp = load_ocsp_response(
os.path.join(basic_aa_dir, 'alice-revoked.ors')
)
vc = ValidationContext(
trust_roots=[root],
other_certs=[interm, role_aa],
ocsps=[ocsp_resp],
moment=datetime.datetime(
year=2021, month=12, day=12, tzinfo=datetime.timezone.utc
),
)
with pytest.raises(RevokedError):
await validate.async_validate_ac(ac, vc)
@pytest.mark.asyncio
async def test_ac_unrevoked_full_path_validation():
ac = load_attr_cert(
os.path.join(basic_aa_dir, 'aa', 'alice-role-with-rev.attr.crt')
)
root = load_cert(os.path.join(basic_aa_dir, 'root', 'root.crt'))
interm = load_cert(os.path.join(basic_aa_dir, 'root', 'interm-role.crt'))
role_aa = load_cert(os.path.join(basic_aa_dir, 'interm', 'role-aa.crt'))
ocsp_resp = load_ocsp_response(
os.path.join(basic_aa_dir, 'alice-all-good.ors')
)
vc = ValidationContext(
trust_roots=[root],
other_certs=[interm, role_aa],
ocsps=[ocsp_resp],
moment=datetime.datetime(
year=2019, month=12, day=12, tzinfo=datetime.timezone.utc
),
)
await validate.async_validate_ac(ac, vc)
|