Skip to content

Commit e7169e7

Browse files
committed
Cert.py: fix TreeChain tests
1 parent 96e215b commit e7169e7

File tree

2 files changed

+54
-20
lines changed

2 files changed

+54
-20
lines changed

scapy/layers/tls/cert.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1357,7 +1357,12 @@ def __init__(
13571357
for cert in self.rootCAs:
13581358
certList.remove(cert)
13591359
else:
1360+
# Store cert store.
13601361
self.rootCAs = CertList(rootCAs)
1362+
# And remove those certs from the list if present (remove dups)
1363+
for cert in self.rootCAs:
1364+
if cert in certList:
1365+
certList.remove(cert)
13611366

13621367
# Append our root CAs to the certList
13631368
certList.extend(self.rootCAs)
@@ -1403,17 +1408,23 @@ def _rec_getchain(chain, curtree):
14031408
# the chain, else recurse.
14041409
for c, subtree in curtree:
14051410
curchain = chain + [c]
1411+
# If 'cert' is issued by c
14061412
if cert.isIssuerCert(c):
1413+
# Final node of the chain !
1414+
# (add the final cert if not self signed)
1415+
if c != cert:
1416+
curchain += [cert]
14071417
return curchain
14081418
else:
1419+
# Not the final node of the chain ! Recurse.
14091420
curchain = _rec_getchain(curchain, subtree)
14101421
if curchain:
14111422
return curchain
14121423
return None
14131424

14141425
chain = _rec_getchain([], self.tree)
14151426
if chain is not None:
1416-
return CertTree(cert, chain)
1427+
return CertTree(chain)
14171428
else:
14181429
return None
14191430

test/scapy/layers/tls/cert.uts

Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -614,30 +614,53 @@ pL/QlwVKvOoYKAKQvVR4CSFx09F9HdkWsKlhPdAKACL8x3vLCWRFCztAgfd9fDL1
614614
mMpYjn0q7pBZc2T5NnReJaH1ZgUufzkVqSr7UIuOhWn0
615615
-----END CERTIFICATE-----
616616
""")
617-
c0.isIssuerCert(c1) and c1.isIssuerCert(c2) and not c0.isIssuerCert(c2)
617+
assert c0.isIssuerCert(c1) and c1.isIssuerCert(c2) and not c0.isIssuerCert(c2)
618618

619619
= Cert class : Checking isSelfSigned()
620-
c2.isSelfSigned() and not c1.isSelfSigned() and not c0.isSelfSigned()
620+
assert c2.isSelfSigned() and not c1.isSelfSigned() and not c0.isSelfSigned()
621621

622622
= PubKey class : Checking verifyCert()
623-
c2.pubKey.verifyCert(c2) and c1.pubKey.verifyCert(c0)
623+
assert c2.pubKey.verifyCert(c2) and c1.pubKey.verifyCert(c0)
624+
625+
= CertTree class : Checking verification of chain
626+
chain0 = CertTree([c0, c1, c2]).getchain(c0)
627+
assert len(chain0) == 3
628+
assert chain0[0] == c1
629+
assert chain0[1] == c0
630+
assert chain0[2] == c2
631+
chain1 = CertTree([c2, c1, c0]).getchain(c1)
632+
assert len(chain1) == 2
633+
assert chain1[0] == c1
634+
assert chain1[1] == c2
635+
chain2 = CertTree([c0, c2, c1]).getchain(c2)
636+
assert len(chain2) == 1
637+
assert chain2[0] == c2
638+
639+
= CertTree class : show()
640+
641+
expected_repr = '/C=US/ST=Arizona/L=Scottsdale/O=Starfield Technologies, Inc./CN=Starfield Root Certificate Authority - G2 [Self Signed]\n /C=US/ST=Arizona/L=Scottsdale/O=Starfield Technologies, Inc./OU=http://certs.starfieldtech.com/repository//CN=Starfield Secure Certificate Authority - G2 [Not Self Signed]\n /OU=Domain Control Validated/CN=*.tools.ietf.org [Not Self Signed]\n'
642+
assert CertTree([c0, c1, c2]).show(ret=True) == expected_repr
643+
644+
repr_str = CertTree([], c0).show(ret=True)
645+
assert repr_str == '/OU=Domain Control Validated/CN=*.tools.ietf.org [Not Self Signed]\n'
646+
647+
= CertTree class : verify
648+
649+
CertTree([c1, c2]).verify(c0)
650+
CertTree([c2]).verify(c1)
651+
652+
try:
653+
CertTree([c1]).verify(c0)
654+
assert False
655+
except ValueError:
656+
pass
657+
658+
try:
659+
CertTree([c2]).verify(c0)
660+
assert False
661+
except ValueError:
662+
pass
624663

625-
= Chain class : Checking chain construction
626-
assert len(Chain([c0, c1, c2])) == 3
627-
assert len(Chain([c0], c1)) == 2
628-
len(Chain([c0], c2)) == 1
629-
630-
= Chain class : repr
631-
632-
expected_repr = """__ /C=US/ST=Arizona/L=Scottsdale/O=Starfield Technologies, Inc./CN=Starfield Root Certificate Authority - G2 [Self Signed]
633-
_ /C=US/ST=Arizona/L=Scottsdale/O=Starfield Technologies, Inc./OU=http://certs.starfieldtech.com/repository//CN=Starfield Secure Certificate Authority - G2
634-
_ /OU=Domain Control Validated/CN=*.tools.ietf.org"""
635-
assert str(Chain([c0, c1, c2])) == expected_repr
636-
637-
= Test __repr__
638-
639-
repr_str = Chain([], c0).__repr__()
640-
assert repr_str == '__ /OU=Domain Control Validated/CN=*.tools.ietf.org [Not Self Signed]\n'
641664

642665
= Test GeneralizedTime
643666

0 commit comments

Comments
 (0)