pygamlastan.crypto¶
Cryptographic operations: key management, signing, verification, encryption,
decryption, canonicalization, and PKCS#11/HSM signing. See the
Signing, verification, and encryption guide for worked examples. Errors raise
pygamlastan.SamlCryptoError.
Keys¶
- class pygamlastan.crypto.KeysManager¶
Holds private/public keys and trusted certificates.
- static build_sp(private_key_pem: bytes, idp_certificate_pem: bytes) KeysManager¶
SP setup: the SP signing key plus the trusted IdP certificate.
- static build_idp(private_key_pem: bytes) KeysManager¶
IdP setup: the IdP signing key.
- add_key_pem(pem: bytes, usage: str = 'sign', password: str | None = None) None¶
Load a PEM private key and add it.
usageis one of"sign","verify","encrypt","decrypt","any".
Signing¶
- class pygamlastan.crypto.SamlSigner(keys: KeysManager)¶
Sign with file-based keys from a
KeysManager.- static from_pem(private_key_pem: bytes, password: str | None = None) SamlSigner¶
Build a signer directly from a signing private key PEM.
- static with_pkcs11(signer: Pkcs11Signer, keys: KeysManager | None = None) SamlSigner¶
Build an HSM-backed signer.
keysmay be omitted: the certificate is taken from the signature template.
- sign_enveloped(xml_with_template: str) str¶
Apply an enveloped XML-DSig signature to a document that already carries a
<ds:Signature>template.
Verification¶
- class pygamlastan.crypto.SamlVerifier(keys: KeysManager)¶
Verify signatures against keys/trusted certs in a
KeysManager.- static from_cert(cert: bytes) SamlVerifier¶
Build a verifier trusting a single certificate (PEM or DER). The certificate’s public key is registered as a verification key and as a trust anchor.
- verify_enveloped(signed_xml: str) VerifyResult¶
- verify_redirect_query(query_string: bytes, signature: bytes, algorithm_uri: str, unsafe_allow_weak_sha1: bool = False) bool¶
Verify a HTTP-Redirect query signature. SHA-1 algorithms are rejected unless
unsafe_allow_weak_sha1=Trueis explicit.
- set_skip_time_checks(skip: bool, unsafe_allow_skip_time_checks: bool = False) None¶
skip=Trueraises unlessunsafe_allow_skip_time_checks=Trueis explicit.
- set_trusted_keys_only(trusted: bool, unsafe_allow_untrusted_keys: bool = False) None¶
trusted=Falseraises unlessunsafe_allow_untrusted_keys=Trueis explicit.
- class pygamlastan.crypto.VerifyResult¶
The outcome of
SamlVerifier.verify_enveloped(). Truthy when valid.- signed_reference_ids() list[str]¶
The reference ids whose digest was actually verified (with a leading
#stripped). Pass these topygamlastan.profiles.process_response()asverified_signed_ids.
Encryption¶
- class pygamlastan.crypto.SamlEncryptor(keys: KeysManager)¶
- static for_certificate(cert_der: bytes) SamlEncryptor¶
Encrypt to a recipient certificate (the per-request PEFIM flow).
Canonicalization¶
PKCS#11 / HSM¶
- class pygamlastan.crypto.Pkcs11Provider(module_path: str)¶
Load a PKCS#11 module (a shared library, e.g. SoftHSM2 or kryoptic).
- open_session(pin: str) Pkcs11Session¶
Open and log in to a session with the given user PIN.
- class pygamlastan.crypto.Pkcs11Session¶
- signer(key_label: str, algorithm: str) Pkcs11Signer¶
Create a signer bound to the private key identified by
key_label.algorithmis a name such as"rsa-sha256"or"ecdsa-p256-sha256".
- class pygamlastan.crypto.Pkcs11Signer(session: Pkcs11Session, key_label: str, algorithm: str)¶
A signer whose private key stays on the token. Pass it to
SamlSigner.with_pkcs11().