MCPcopy Create free account
hub / github.com/secdev/scapy / verify

Method verify

scapy/layers/tls/cert.py:1818–1915  ·  view source on GitHub ↗

Verify a CMS message against the list of trusted certificates, and return the unpacked message if the verification succeeds. :param contentInfo: the ContentInfo whose signature to verify :param eContentType: if provided, verifies that the content type is valid

(
        self,
        contentInfo: CMS_ContentInfo,
        eContentType: Optional[ASN1_OID] = None,
    )

Source from the content-addressed store, hash-verified

1816 )
1817
1818 def verify(
1819 self,
1820 contentInfo: CMS_ContentInfo,
1821 eContentType: Optional[ASN1_OID] = None,
1822 ):
1823 """
1824 Verify a CMS message against the list of trusted certificates,
1825 and return the unpacked message if the verification succeeds.
1826
1827 :param contentInfo: the ContentInfo whose signature to verify
1828 :param eContentType: if provided, verifies that the content type is valid
1829 """
1830 if contentInfo.contentType.oidname != "id-signedData":
1831 raise ValueError("ContentInfo isn't signed !")
1832
1833 signeddata = contentInfo.content
1834
1835 # Build the certificate chain
1836 certificates = []
1837 if signeddata.certificates:
1838 certificates = [Cert(x.certificate) for x in signeddata.certificates]
1839 certTree = CertTree(certificates, self.store)
1840
1841 # Check there's at least one signature
1842 if not signeddata.signerInfos:
1843 raise ValueError("ContentInfo contained no signature !")
1844
1845 # Check all signatures
1846 for signerInfo in signeddata.signerInfos:
1847 # Find certificate in the chain that did this
1848 cert: Cert = certTree.findCertBySid(signerInfo.sid)
1849
1850 # Verify certificate signature
1851 certTree.verify(cert)
1852
1853 # Verify the message hash
1854 if signerInfo.signedAttrs:
1855 # Verify the contentType
1856 try:
1857 contentType = next(
1858 x.values[0].value
1859 for x in signerInfo.signedAttrs
1860 if x.type.oidname == "contentType"
1861 )
1862
1863 if contentType != signeddata.encapContentInfo.eContentType:
1864 raise ValueError(
1865 "Inconsistent 'contentType' was detected in packet !"
1866 )
1867
1868 if eContentType is not None and eContentType != contentType:
1869 raise ValueError(
1870 "Expected '%s' but got '%s' contentType !"
1871 % (
1872 eContentType,
1873 contentType,
1874 )
1875 )

Callers 1

verifySelfMethod · 0.95

Calls 8

verifyMethod · 0.95
_get_hashFunction · 0.90
CertClass · 0.85
CertTreeClass · 0.85
findCertBySidMethod · 0.80
updateMethod · 0.45
verifyMethod · 0.45

Tested by

no test coverage detected