CryptoParser

Bases: BaseModel, abc.ABC

CryptoParser is an abstract class used by all the types parsing cryptography objects into pki_tools pydantic classes.

from_cryptography abstractmethod classmethod
from_cryptography(crypto_obj: CryptoObject) -> CryptoParser

Parses a cryptography x509 object into a CryptoParser

Parameters:
  • crypto_obj (CryptoObject) –

    The cryptography object

Returns:

Encoding

Bases: Enum

Describes the encoding used for writing/reading InitCryptoParser objects.

Attributes:
  • PEM

    PEM string format

  • DER

    DER binary format

InitCryptoParser

Bases: CryptoParser, abc.ABC

Extends the CryptoParser into an object that requires initialization before it can be used (while created as a pki_tools object and not loaded from cryptography). This can, for example, be a Certificate that needs to be signed with a KeyPair containing the private key.

Attempt to e.g. dumping a certificate to a PEM string without using the sign (init) function first will result in a MissingInit exception.

Classes implementing the InitCryptoParser will also automatically get functions to load and write the objects with the supported Encoding as well as writing/reading the encoded content to/from files.

der_bytes property
der_bytes: bytes

Returns the DER bytes of the object

Returns:
  • bytes

    The DER bytes.

pem_bytes property
pem_bytes: bytes

Returns the PEM bytes of the object

Returns:
  • bytes

    The PEM bytes.

pem_string property
pem_string: str
Returns:
  • str

    PEM decoded into a string

from_der_bytes classmethod
from_der_bytes(der: bytes) -> InitCryptoParser

Loads the object from DER bytes

Parameters:
  • der (bytes) –

    The DER encoded object

Returns:
from_file classmethod
from_file(file_path: str, encoding: Encoding = Encoding.PEM) -> InitCryptoParser

Reads a file containing one PEM into the object

Parameters:
  • file_path (str) –

    The path to the file (can be relative the caller or absolute)

  • encoding (Encoding, default: Encoding.PEM ) –

    The encoding to use for the dumped data

Returns: The Certificate loaded from the specified file

from_pem_string classmethod
from_pem_string(pem: str) -> InitCryptoParser

Loads the object from a PEM string

Parameters:
  • pem (str) –

    The PEM encoded object in string format

Returns:
to_file
to_file(file_path: str, encoding: Encoding = Encoding.PEM) -> None

Saves the object with specified encoding to the specified file, creating it if it doesn't exist.

Parameters:
  • file_path (str) –

    The path to the file (can be relative the caller or absolute)

  • encoding (Encoding, default: Encoding.PEM ) –

    The encoding to use for the dumped data