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

Class PrivKey

scapy/layers/tls/cert.py:639–720  ·  view source on GitHub ↗

Parent class for PrivKeyRSA, PrivKeyECDSA and PrivKeyEdDSA. Provides common signTBSCert(), resignCert(), verifyCert() and export() methods.

Source from the content-addressed store, hash-verified

637
638
639class PrivKey(metaclass=_PrivKeyFactory):
640 """
641 Parent class for PrivKeyRSA, PrivKeyECDSA and PrivKeyEdDSA.
642 Provides common signTBSCert(), resignCert(), verifyCert()
643 and export() methods.
644 """
645
646 def signTBSCert(self, tbsCert, h="sha256"):
647 """
648 Note that this will always copy the signature field from the
649 tbsCertificate into the signatureAlgorithm field of the result,
650 regardless of the coherence between its contents (which might
651 indicate ecdsa-with-SHA512) and the result (e.g. RSA signing MD2).
652
653 There is a small inheritance trick for the computation of sigVal
654 below: in order to use a sign() method which would apply
655 to both PrivKeyRSA and PrivKeyECDSA, the sign() methods of the
656 subclasses accept any argument, be it from the RSA or ECDSA world,
657 and then they keep the ones they're interested in.
658 Here, t will be passed eventually to pkcs1._DecryptAndSignRSA.sign().
659 """
660 sigAlg = tbsCert.signature
661 h = h or hash_by_oid[sigAlg.algorithm.val]
662 sigVal = self.sign(bytes(tbsCert), h=h, t="pkcs")
663 c = X509_Cert()
664 c.tbsCertificate = tbsCert
665 c.signatureAlgorithm = sigAlg
666 c.signatureValue = _Raw_ASN1_BIT_STRING(sigVal, readable=True)
667 return c
668
669 def resignCert(self, cert):
670 """Rewrite the signature of either a Cert or an X509_Cert."""
671 return self.signTBSCert(cert.tbsCertificate, h=None)
672
673 def verifyCert(self, cert):
674 """Verifies either a Cert or an X509_Cert."""
675 return self.pubkey.verifyCert(cert)
676
677 def verifyCsr(self, cert):
678 """Verifies either a CSR."""
679 return self.pubkey.verifyCsr(cert)
680
681 @property
682 def pem(self):
683 return der2pem(self.der, self.marker)
684
685 @property
686 def der(self):
687 return self.key.private_bytes(
688 encoding=serialization.Encoding.DER,
689 format=serialization.PrivateFormat.PKCS8,
690 encryption_algorithm=serialization.NoEncryption(),
691 )
692
693 def export(self, filename, fmt=None):
694 """
695 Export private key in 'fmt' format (DER or PEM) to file 'filename'
696 """

Callers 2

__init__Method · 0.90
parse_argsMethod · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…