flexstack.security Module
The flexstack.security package implements ETSI TS 103 097 signing, verification, and certificate handling.
SignService
- exception flexstack.security.sign_service.SigningError[source]
Bases:
RuntimeErrorNo authorization ticket may sign the message: none has the ITS-AID with the requested SSP, is valid now and is valid at the current position (IEEE Std 1609.2-2016 clause 5.2.3.2.2). The message shall not be sent.
- flexstack.security.sign_service.ssp_covers(at_ssp, requested)[source]
True when the BitmapSsp of an AT grants every permission bit requested (octets after the SSP version octet). Without requested bits any SSP covers the request.
- Parameters:
at_ssp (bytes | None) –
requested (bytes) –
- Return type:
bool
- class flexstack.security.sign_service.CooperativeAwarenessMessageSecurityHandler(backend, clock=None)[source]
Bases:
objectClass that handles the signing of CAMs according the standard ETSI TS 103 097 V2.2.1 (2026-03) 7.1.1
- Parameters:
backend (ECDSABackend) –
clock (Callable[[], float] | None) –
- sign(signed_data, certificate)[source]
Sign the CAM.
- Parameters:
signed_data (dict) –
certificate (OwnCertificate) –
- Return type:
None
- set_up_signer(certificate)[source]
Set up the signer. According to the standard rules: - As default, the choice digest shall be included. - The choice certificate shall be included once, one second after the last inclusion of the choice certificate (at least one second elapsed). - If the ITS-S receives a CAM signed by a previously unknown AT, it shall include the choice certificate immediately in its next CAM, instead of including the choice digest. In this case, the timer for the next inclusion of the choice certificate shall be restarted. - If an ITS-S receives a CAM that includes a tbsdata.headerInfo component of type inlineP2pcdRequest, then the ITS-S shall evaluate the list of certificate digests included in that component: If the ITS-S finds a certificate digest of the currently used authorization ticket in that list, it shall include the choice certificate immediately in its next CAM, instead of including the choice digest.
- Parameters:
certificate (OwnCertificate) –
- Return type:
tuple
- set_up_signer_at(certificate, current_time)[source]
set_up_signer()atcurrent_time(UTC seconds).- Parameters:
certificate (OwnCertificate) –
current_time (float) –
- Return type:
tuple
- class flexstack.security.sign_service.SignService(backend, certificate_library, clock=None)[source]
Bases:
objectSign service class to sign and verify messages. Follows the specification of ETSI TS 102 723-8 V1.1.1 (2016-04) standard.
- Variables:
ecdsa_backend (ECDSABackend) – ECDSA backend to use.
certificate_library (CertificateLibrary) – Certificate library holding own certificates, known authorization tickets, authorization authorities and root certificates used during signing.
unknown_ats (list[bytes]) – List of unknown ATs. Each AT is represented by its certificate hashedId3.
requested_ats (list[bytes]) – List of requested ATs. Each AT is represented by its certificate hashedId3.
- Parameters:
backend (ECDSABackend) –
certificate_library (CertificateLibrary) –
clock (Callable[[], float] | None) –
- __init__(backend, certificate_library, clock=None)[source]
Initialize the Sign Service.
- Parameters:
backend (ECDSABackend) – ECDSA backend to use for cryptographic operations.
certificate_library (CertificateLibrary) – Certificate library holding own certificates and trusted chain certificates.
clock (Callable[[], float] | None) – Time source (UTC seconds since the Unix epoch) dating the messages (generationTime), driving the certificate-inclusion timers and selecting the ATs valid now. Defaults to
TimeService.time().
- Return type:
None
- now()[source]
Current UTC time in seconds (the injected clock, or the system time).
- Return type:
float
- now_its_us()[source]
Current ITS time (TAI) in microseconds, as
generationTime(Time64).- Return type:
int
- sign_request(request)[source]
Sign a SNSIGNRequest.
Routing: - its_aid == 36: CAM-related PKI signing — not implemented (CAMs are signed
directly via sign_cam()).
its_aid == 37: DENM — delegates to sign_denm() (§7.1.2).
any other its_aid: generic signed message — delegates to sign_other() (§7.1.3).
- Parameters:
request (SNSIGNRequest) –
- Return type:
SNSIGNConfirm
- sign_other(request)[source]
Sign a message according to ETSI TS 103 097 V2.2.1 §5.2 and §7.1.3.
§7.1.3 generic profile for signed messages other than CAM and DENM: - tbsData.headerInfo SHALL contain psid (set to request.its_aid) and
generationTime (§5.2). No other headerInfo fields are added or required by this profile.
The clause does not constrain the signer: the certificate is included once per second (own timer, as for CAMs) so that receivers can verify the message without having heard a CAM, the digest (hashedId8 of the signing AT) otherwise.
- Parameters:
request (SNSIGNRequest) –
- Return type:
SNSIGNConfirm
- sign_denm(request)[source]
Sign a DENM according to ETSI TS 103 097 V2.2.1 §5.2 and §7.1.2.
§7.1.2 constraints applied here: - signer shall always be choice ‘certificate’ (never ‘digest’). - tbsData.headerInfo shall contain psid (37), generationTime (§5.2),
and generationLocation (mandatory). No other optional fields are included (expiryTime, inlineP2pcdRequest, requestedCertificate, etc. shall all be absent).
- Parameters:
request (SNSIGNRequest) –
- Return type:
SNSIGNConfirm
- sign_cam(request)[source]
Sign a CAM according the standard ETSI TS 103 097 V2.2.1 §5.2 and §7.1.1
- Parameters:
request (SNSIGNRequest) –
- Return type:
SNSIGNConfirm
- get_present_at_for_signging(its_aid, permissions=b'', generation_location=None)[source]
Select the AT to sign with (IEEE Std 1609.2-2016 clause 5.2.3.2.2): its appPermissions contain the ITS-AID with an SSP granting the requested
permissions, the current time lies in the validity period of its chain and, whengeneration_locationis given, the position lies in the region of its chain.- Parameters:
its_aid (int) – ITS AID to look up.
permissions (bytes) – Requested SSP (BitmapSsp); only the bits after the version octet count.
generation_location (dict | None) – Current position ({“latitude”, “longitude”} in 1/10 µdeg).
- Returns:
The first AT satisfying every condition, or None.
- Return type:
OwnCertificate | None
- add_own_certificate(cert)[source]
Add an own certificate to the certificate library.
Delegates to
CertificateLibrary.add_own_certificate()so that the certificate is available for signing and can be verified against the trusted chain stored in the library.- Parameters:
cert (OwnCertificate) – The own certificate to add.
- Return type:
None
- notify_unknown_at(hashedid8)[source]
§7.1.1: An unknown AT cert was seen. Record its HashedId3 for the next inlineP2pcdRequest and force own certificate inclusion in the next CAM.
- Parameters:
hashedid8 (bytes) –
- Return type:
None
- notify_known_at(hashedid8)[source]
An AT was learned (received as certificate signer): stop requesting it.
- Parameters:
hashedid8 (bytes) –
- Return type:
None
- notify_inline_p2pcd_request(request_list)[source]
§7.1.1: Process a received inlineP2pcdRequest field.
If our own AT’s HashedId3 is present, include certificate in next CAM.
If a CA cert we hold is requested, schedule it as requestedCertificate.
- Parameters:
request_list (list) –
- Return type:
None
- notify_received_ca_certificate(cert_dict)[source]
§7.1.1: A peer sent the CA certificate we requested. Discard the pending request and add the certificate to the library.
- Parameters:
cert_dict (dict) –
- Return type:
None
- get_known_at_for_request(hashedid3)[source]
§7.1.1: Return the CA certificate dict for the given HashedId3 so it can be embedded in the requestedCertificate header field.
- Parameters:
hashedid3 (bytes) –
- Return type:
dict
VerifyService
Verification of received secured messages (ETSI TS 103 097 V2.2.1 (2026-03), clauses 5 and 7; IEEE Std 1609.2-2016, clauses 5.2.3.2.2 and 5.3.1).
Checks, in order (the first failing one gives the report):
decodable EtsiTs103097Data-Signed with protocolVersion 3 (clause 5.1);
signer identifier: certificate (exactly one) or digest; DENMs always use a certificate (clause 7.1.2);
the signer certificate is a known, valid authorization ticket;
headerInfo: generationTime present, p2pcdLearningRequest and missingCrlIdentifier absent (clause 5.2); CAM (clause 7.1.1) and DENM (clause 7.1.2) profiles;
the PSID is in the appPermissions of the AT (the SSP is returned);
the generationTime lies in the validity period of the AT chain;
the generationLocation, when present, lies in the region of the AT chain;
the signature, over Hash(Hash(tbsData) || Hash(AT)) (1609.2 clause 5.3.1), with an algorithm matching the verification key.
Mirrors rustflexstack::security::verify_service.
- flexstack.security.verify_service.received_tbs_data(message, signed_data)[source]
The tbsData octets of a received Ieee1609Dot2Data signedData: protocolVersion (1 octet), the Ieee1609Dot2Content tag of signedData (0x81), hashId (1 octet), then tbsData, signer and signature (SignedData has no extension marker).
Nonewhen the message does not end with the encodings of its signer and signature. Mirrorsreceived_tbs_datain RustFlexstack.- Parameters:
message (bytes) –
signed_data (dict) –
- Return type:
bytes | None
- class flexstack.security.verify_service.VerifyService(backend, certificate_library, sign_service=None)[source]
Bases:
objectClass to verify the signature of a message
- Parameters:
backend (ECDSABackend) –
certificate_library (CertificateLibrary) –
sign_service (SignService | None) –
- __init__(backend, certificate_library, sign_service=None)[source]
Constructor
- Parameters:
backend (ECDSABackend) – ECDSA backend to use for the verification of the signature.
certificate_library (CertificateLibrary) – Certificate library to use for the verification of the signature.
sign_service (SignService | None, optional) – Sign service to notify about P2PCD events (unknown certs, requests).
- verify(request)[source]
Verify a received secured message.
- Parameters:
request (SNVERIFYRequest) – Request to verify the signature of a message.
- Returns:
Report, signer certificate id, ITS-AID, SSP of the signer for that ITS-AID and, on success, the plain message.
- Return type:
SNVERIFYConfirm
Certificate & OwnCertificate
- flexstack.security.certificate.signing_input(data, signer, hashfunc=<built-in function openssl_sha256>)[source]
Input to the ECDSA signing and verification operations (IEEE Std 1609.2-2016, clause 5.3.1, adopted by ETSI TS 103 097 V2.2.1):
Hash(Data input) || Hash(Signer identifier input), which ECDSA hashes again.datais the COER encoding of the ToBeSignedData or ToBeSignedCertificate;signeris the canonical COER encoding of the signer’s certificate, orb""for self-signed data.- Parameters:
data (bytes) –
signer (bytes) –
- Return type:
bytes
- class flexstack.security.certificate.Certificate(certificate: 'dict' = <factory>, issuer: 'Certificate | None' = None)[source]
Bases:
object- Parameters:
certificate (dict) –
issuer (Certificate | None) –
- certificate: dict
- issuer: Certificate | None = None
- static from_dict(certificate, issuer=None)[source]
Sets up the certificate from a dictionary.
- Parameters:
certificate (dict) – The certificate to be set up.
issuer (Certificate) – The issuer of the certificate
- Return type:
Certificate
- decode(certificate, issuer=None)[source]
Decode the certificate and set it up in the Certificate object.
- Parameters:
certificate (bytes) – The certificate to be decoded.
issuer (Certificate | None) – The issuer of the certificate. None if self signed.
- Return type:
Certificate
- to_canonical()[source]
Convert certificate to IEEE 1609.2 / ETSI TS 103 097 canonical form: - Every elliptic curve point of toBeSigned compressed
(compressed-y-0/1): verification key or reconstruction value, on every curve, and the encryption key (IEEE 1609.2 clause 6.4.3).
Signature rSig as x-only, for every signature algorithm.
- Return type:
Certificate
- canonical_encoding()[source]
COER of the canonical form (
to_canonical()): the input of the HashedId8 and of the signer hash of every message this certificate signs or verifies (IEEE 1609.2 clauses 5.3.1, 6.4.3). Computed once per certificate (they are not modified after construction; a newcertificatevalue is detected) instead of once per message, which cost a deep copy and an encoding per signature.- Return type:
bytes
- as_hashedid8()[source]
Return the certificate as a canonical hashedid8.
- Returns:
The certificate as a hashedid8.
- Return type:
bytes
- hash_name()[source]
sha256orsha384: the hash for the curve of the certificate’s verification key (hash_name_for_key()), used for its HashedId8, the hashId of data it signs and the issuer choice of certificates it issues. SHA-256 for implicit certificates.- Return type:
str
- issuer_choice()[source]
IssuerIdentifier choice of a certificate issued by this one.
- Return type:
str
- encode()[source]
Return the certificate as bytes.
- Returns:
The certificate as bytes.
- Return type:
bytes
- get_list_of_its_aid()[source]
Return the list of ITS AID.
- Returns:
The list of ITS AID.
- Return type:
tuple[int]
- static as_clear_certificate()[source]
Generate a White Certificate and set it up to the Certificate object.
- Return type:
Certificate
- get_issuer_hashedid8()[source]
Returns the issuer HashedId8 stored in the dict.
- Returns:
The issuer HashedId8. Is none if it’s self signed
- Return type:
bytes
- Raises:
ValueError – If the issuer type is unknown or the certificate is not initialized.
- signature_is_nist_p256()[source]
Check if the signature is NISTP256.
- Returns:
True if the signature is NISTP256, False otherwise.
- Return type:
bool
- verification_key_is_nist_p256()[source]
Check if the verification key is NISTP256.
- Returns:
True if the verification key is NISTP256, False otherwise.
- Return type:
bool
- signature_is_supported()[source]
True when the signature algorithm is NIST P-256/P-384 or brainpoolP256r1/P384r1.
- Return type:
bool
- verification_key_is_supported()[source]
True when the verification key is NIST P-256/P-384 or brainpoolP256r1/P384r1.
- Return type:
bool
- certificate_is_self_signed()[source]
Check if the certificate is self signed.
- Returns:
True if the certificate is self signed, False otherwise.
- Return type:
bool
- certificate_is_issued()[source]
Check if the certificate is issued.
- Returns:
True if the certificate is issued, False otherwise.
- Return type:
bool
- check_corresponding_issuer(issuer)[source]
Check if the issuer corresponds to the certificate stated issuer.
- Parameters:
issuer (Certificate) – The issuer to check.
- Returns:
True if the issuer corresponds to the certificate stated issuer, False otherwise
- Return type:
bool
- certificate_wants_cert_issue_permissions()[source]
Check if the certificate wants certificate issue permissions.
- Parameters:
certificate (Certificate) – The certificate to check.
- Returns:
True if the certificate wants certificate issue permissions, False otherwise.
- Return type:
bool
- get_list_of_psid_from_cert_issue_permissions()[source]
Get the list of PSID from the certificate issue permissions.
- Parameters:
certificate (Certificate) – The certificate to get the PSID from.
- Returns:
The list of PSID.
- Return type:
list[int]
- get_list_of_psid_from_app_permissions()[source]
Get the list of PSID from the application permissions.
- Parameters:
certificate (Certificate) – The certificate to get the PSID from.
- Returns:
The list of PSID.
- Return type:
list[int]
- get_list_of_needed_permissions()[source]
Gets the list of needed permissions.
- Parameters:
certificate (Certificate) – The certificate to get the needed permissions from.
- Returns:
The list of needed permissions.
- Return type:
list[int]
- get_list_of_allowed_persmissions()[source]
Gets the list of allowed permissions.
- Parameters:
issuer (OwnCertificate) – The issuer of the certificate.
- Returns:
The list of allowed permissions.
- Return type:
list[int]
- certificate_has_all_permissions()[source]
Check if the certificate has all permissions.
- Parameters:
certificate (Certificate) – The certificate to check.
- Returns:
True if the certificate has all permissions, False otherwise.
- Return type:
bool
- static check_all_requested_permissions_are_allowed(certificate_permissions, issuer_permissions)[source]
Check if all the requested permissions are allowed.
- Parameters:
certificate_permissions (list[int]) – The requested permissions.
issuer_permissions (list[int]) – The allowed permissions.
- Returns:
True if all the requested permissions are allowed, False otherwise.
- Return type:
bool
- check_issuer_has_subject_permissions(issuer)[source]
Check if the issuer has the subject permissions.
- Parameters:
certificate (Certificate) – The certificate to check.
issuer (Certificate) – The issuer of the certificate.
- Returns:
True if the issuer has the subject permissions, False otherwise.
- Return type:
bool
- static verify_signature(backend, to_be_signed_certificate, signature, verification_key)[source]
Verify the signature of a self-signed certificate, computed over Hash(Hash(tbs) || Hash(“”)) (IEEE 1609.2 clause 5.3.1).
- Parameters:
to_be_signed_certificate (dict) – The to be signed certificate to be verified.
signature (tuple) – The signature to be verified.
verification_key (tuple) – The verification key to be used.
backend (ECDSABackend) –
- Returns:
True if the signature is valid, False otherwise.
- Return type:
bool
- verify(backend)[source]
Verify the certificate.
- Parameters:
backend (ECDSABackend) – The backend to be used for verification.
issuer (Certificate | None) – The issuer of the certificate. None if self signed.
- Returns:
True if the certificate is valid, False otherwise.
- Return type:
bool
- is_implicit()[source]
True for an implicit certificate (reconstruction value, no signature).
- Return type:
bool
- implicit_hash_integer()[source]
e = Hn(Hash(ToBeSignedCertificate) || Hash(issuer certificate)): the leftmost floor(log2 n) = 255 bits of SHA-256 over the 1609.2 hash input (IEEE Std 1609.2-2016 clauses 5.3.2 and 6.4.3; SEC 4 section 2.3).
- Return type:
int
- verification_key()[source]
Public verification key of the certificate: the explicit key, or the key reconstructed from the reconstruction value and the issuer key (ECQV, IEEE Std 1609.2-2016 clause 5.3.2). None when not available.
- Return type:
tuple | None
- is_authorization_ticket()[source]
Check whether this certificate conforms to the §7.2.1 Authorization Ticket profile.
§7.2.1 constraints: - issuer SHALL be sha256AndDigest or sha384AndDigest (never self). - toBeSigned.id SHALL be choice none. - toBeSigned.certIssuePermissions SHALL be absent. - toBeSigned.appPermissions SHALL be present.
- Return type:
bool
- is_enrolment_credential()[source]
Check whether this certificate conforms to the §7.2.2 Enrolment Credential profile.
§7.2.2 constraints: - type SHALL be explicit. - issuer SHALL be sha256AndDigest or sha384AndDigest. - toBeSigned.id SHALL be choice name. - toBeSigned.certIssuePermissions SHALL be absent. - toBeSigned.appPermissions SHALL be present.
- Return type:
bool
- is_root_ca_certificate()[source]
Check whether this certificate conforms to the §7.2.3 Root CA profile.
§7.2.3 constraints: - type SHALL be explicit. - issuer SHALL be self. - toBeSigned.id SHALL be choice name. - toBeSigned.certIssuePermissions SHALL be present. - toBeSigned.appPermissions SHALL be present.
- Return type:
bool
- is_subordinate_ca_certificate()[source]
Check whether this certificate conforms to the §7.2.4 Subordinate CA profile.
§7.2.4 constraints: - type SHALL be explicit. - issuer SHALL be sha256AndDigest or sha384AndDigest. - toBeSigned.id SHALL be choice name. - toBeSigned.encryptionKey SHALL be present. - toBeSigned.certIssuePermissions SHALL be present.
- Return type:
bool
- set_issuer_as_self()[source]
Set the issuer as self.
- Parameters:
certificate (Certificate) – The certificate to be set.
- Return type:
Certificate
- set_issuer(issuer)[source]
Set the issuer of the certificate.
- Parameters:
certificate (Certificate) – The certificate to be set.
issuer (OwnCertificate) – The issuer of the certificate.
- Return type:
Certificate
- set_chain_length_issue_permissions(issuer)[source]
Set the chain length issue permissions.
- Parameters:
issuer (OwnCertificate) – The issuer of the certificate.
- Return type:
Certificate
- class flexstack.security.certificate.OwnCertificate(certificate=<factory>, issuer=None, key_id=0)[source]
Bases:
CertificateClass that handles certificates that are generated by the user. And thus it has the private key.
- Variables:
key_id (int) – The key id of the pair of keys used to sign the certificate.
- Parameters:
certificate (dict) –
issuer (Certificate | None) –
key_id (int) –
- key_id: int = 0
- static initialize_certificate(backend, to_be_signed_certificate, issuer=None, algorithm='nistP256', compressed_key=True)[source]
Initializes the certificate.
- Parameters:
to_be_signed_certificate (dict) – The to be signed certificate to be initialized.
issuer (OwnCertificate) – The issuer of the certificate. If None, the certificate will be self signed.
algorithm (str) – Curve of the new key pair (an IEEE 1609.2 PublicVerificationKey curve).
compressed_key (bool) – Verification key as a compressed point (default) or uncompressed.
backend (ECDSABackend) –
- Return type:
OwnCertificate
- static verify_to_be_signed_certificate(to_be_signed_certificate)[source]
- Parameters:
to_be_signed_certificate (dict) –
- Return type:
bool
- sign_message(backend, message)[source]
Sign a message with the private key.
- Parameters:
message (bytes) –
backend (ECDSABackend) –
- Returns:
The signature of the message.
- Return type:
dict
- hash_function()[source]
Hash function of
hash_name()(for the 1609.2 signing input).
- check_enough_min_chain_length_for_issuer()[source]
Checks if the chain of trust is lengthy enough.
- Parameters:
issuer (OwnCertificate) – The issuer of the certificate.
- Returns:
True if the chain of trust is lengthy enough, False otherwise.
- Return type:
bool
- sign_certificate(backend, certificate)[source]
Sign the certificate.
- Parameters:
backend (ECDSABackend) – The ECDSA backend to use.
certificate (Certificate) – The certificate to be signed.
- Return type:
Certificate
- issue_implicit_certificate(backend, to_be_signed_certificate)[source]
Issue an implicit certificate with ECQV (SEC 4 sections 3.3 to 3.6, as profiled by IEEE Std 1609.2-2016 clause 5.3.2) and return it with the reconstructed private key of its holder. The requester and the CA role are combined here: k_U is the requester key, k the CA ephemeral key, P_U = k_U*G + k*G, r = e*k + d_CA and d_U = e*k_U + r.
Only a NIST P-256 issuer key held by
backendis supported.- Parameters:
backend (ECDSABackend) –
to_be_signed_certificate (dict) –
- Return type:
OwnCertificate
- issue_certificate(backend, certificate)[source]
Issue a certificate.
- Parameters:
backend (ECDSABackend) – The ECDSA backend to use.
certificate (Certificate) – The certificate to be issued.
- Return type:
Certificate
- set_issuer_as_self()[source]
Set the issuer as self.
- Parameters:
certificate (Certificate) – The certificate to be set.
- Return type:
OwnCertificate
- set_issuer(issuer)[source]
Set the issuer of the certificate.
- Parameters:
certificate (Certificate) – The certificate to be set.
issuer (OwnCertificate) – The issuer of the certificate.
- Return type:
OwnCertificate
- set_chain_length_issue_permissions(issuer)[source]
Set the chain length issue permissions.
- Parameters:
certificate (Certificate) – The certificate to set.
issuer (OwnCertificate) – The issuer of the certificate.
- Return type:
OwnCertificate
CertificateLibrary
- class flexstack.security.certificate_library.CertificateLibrary(ecdsa_backend, root_certificates, aa_certificates, at_certificates)[source]
Bases:
objectClass for managing certificates.
- Variables:
own_certificates (Dict[bytes, OwnCertificate]) – The list of own certificates. (Messages can be signed with those certificates) [Key: Hashedid8, Value: OwnCertificate]
known_authorization_tickets (Dict[bytes, Certificate]) – The list of known authorization tickets. (Messages can be verified with those certificates) [Key: Hashedid8, Value: Certificate]
known_authorization_authorities (Dict[bytes, Certificate]) – The list of known authorization authorities. (Authorization Tickets Certificates can be verified with those certificates) [Key: Hashedid8, Value: Certificate]
known_root_certificates (Dict[bytes, Certificate]) – The list of known root certificates. (Authorization Authorities Certificates can be verified with those certificates) [Key: Hashedid8, Value: Certificate]
- Parameters:
ecdsa_backend (ECDSABackend) –
root_certificates (list[Certificate]) –
aa_certificates (list[Certificate]) –
at_certificates (list[Certificate]) –
- __init__(ecdsa_backend, root_certificates, aa_certificates, at_certificates)[source]
Initialize the Certificate Library.
- Parameters:
root_certificates (list[Certificate]) – The list of root certificates.
aa_certificates (list[Certificate]) – The list of authorization authorities certificates.
at_certificates (list[Certificate]) – The list of authorization tickets certificates.
ecdsa_backend (ECDSABackend) –
- Return type:
None
- revoked: set[bytes]
HashedId8 of revoked CA certificates (CRL, ETSI TS 102 941 clause 6.3.6)
- get_issuer_certificate(certificate)[source]
Get the issuer certificate of a certificate.
- Parameters:
certificate (Certificate) – The certificate to get the issuer certificate from.
- Returns:
The issuer certificate. It returns None if the issuer is self. Or issuer not found.
- Return type:
Certificate
- add_authorization_ticket(certificate)[source]
Add an authorization ticket to the library.
- Parameters:
certificate (Certificate) – The authorization ticket to add.
- Return type:
None
- add_authorization_authority(certificate)[source]
Add an authorization authority to the library.
- Parameters:
certificate (Certificate) – The authorization authority to add.
- Return type:
None
- add_root_certificate(certificate)[source]
Add a root certificate to the library.
- Parameters:
certificate (Certificate) – The root certificate to add.
- Return type:
None
- learn_ca_certificate(certificate)[source]
Add a CA certificate received from a peer (P2PCD, IEEE 1609.2 clause 8.2.4) when its issuer is a known root and its signature verifies; returns True when it is (now) known.
- Parameters:
certificate (dict) –
- Return type:
bool
- remove_certificate(hashedid8)[source]
Forget a root or AA certificate (e.g. deleted from a CTL) and every certificate issued under it.
- Parameters:
hashedid8 (bytes) –
- Return type:
None
- revoke(hashedids8)[source]
Apply a CRL: the CA certificates
hashedids8are revoked permanently (ETSI TS 102 941 V2.2.1 clause 6.3.3); they and every certificate issued under them are no longer trusted, and they cannot be re-added.- Parameters:
hashedids8 (set[bytes]) –
- Return type:
None
- add_own_certificate(certificate)[source]
Add a certificate to the library.
- Parameters:
certificate (OwnCertificate) – The certificate to add
- Return type:
None
- get_authorization_ticket_by_hashedid8(hashedid8)[source]
Get an authorization ticket by its hashedid8.
- Parameters:
hashedid8 (bytes | None) – The hashedid8 of the authorization ticket, None if not found.
- Return type:
Certificate | None
- verify_sequence_of_certificates(certificates, backend)[source]
Verification of a sequence of certificates as specified in IEEE 1609.2. Signer Identifer. The first certificate is the authorization ticket. And then each one is the issuer of the one before.
- Parameters:
certificates (list[dict]) – The sequence of certificates to verify.
coder (SecurityCoder) – The coder to use for encoding and decoding.
backend (ECDSABackend) – The backend to use for ECDSA operations.
- Returns:
The Authorization ticket if verified. Returns none otherwise.
- Return type:
Certificate
- get_ca_certificate_by_hashedid3(hashedid3)[source]
Look up an Authorization Authority or Root CA certificate by the last 3 bytes of its HashedId8 (HashedId3).
- Parameters:
hashedid3 (bytes) – The last 3 bytes of the certificate’s HashedId8.
- Returns:
The matching certificate, or None if not found.
- Return type:
Certificate | None
ECDSABackend
- class flexstack.security.ecdsa_backend.ECDSABackend[source]
Bases:
objectInterface for ECDSA backends.
- create_key(algorithm='nistP256')[source]
Creates a new key pair. Returns the identifier of the key.
- Parameters:
algorithm (str) –
- Return type:
int
- get_public_key(identifier, compressed=True)[source]
Returns the public key of the key pair with the identifier. It returns as a dict to be oer encoded as PublicVerificationKey in oer.
- Parameters:
identifier (int) –
compressed (bool) –
- Return type:
tuple
- sign(data, identifier)[source]
Signs the array of bytes. With the Key that corresponds to the identifier of the private key. The signature must be received in IEEE 1609.2 format.
- Parameters:
data (bytes) –
identifier (int) –
- Return type:
tuple
- verify(data, signature, key)[source]
Verifies the array of bytes. With the Key that corresponds to the identifier.
- Parameters:
data (bytes) –
signature (dict) –
key (int) –
- Return type:
bool
- verify_with_pk(data, signature, pk)[source]
Verifies the array of bytes. With the public key. Both key and signature come in IEEE 1609.2 format.
- Parameters:
data (bytes) – Data to verify.
signature (tuple) – Signature to verify. In IEEE 1609.2 format (Signature).
pk (tuple) – Public key to verify. In IEEE 1609.2 format (PublicVerificationKey).
- Returns:
True if the signature is valid, False otherwise.
- Return type:
bool
- flexstack.security.ecdsa_backend.key_algorithm(pk)[source]
Algorithm (
nistP256,brainpoolP384r1, …) of a PublicVerificationKey.- Parameters:
pk (tuple) –
- Return type:
str
- flexstack.security.ecdsa_backend.hash_name_for_key(pk)[source]
sha256orsha384(the hashes approved by ETSI TS 103 097 V2.2.1 clause 4.2): SHA-384 for a key on a 384-bit curve, SHA-256 otherwise, as in the permutation tables of ETSI TS 103 096-2. Used for HashedId8, hashId and the issuer choice.- Parameters:
pk (tuple) –
- Return type:
str
- flexstack.security.ecdsa_backend.hash_for_key(pk)[source]
Hash function of a PublicVerificationKey (SHA-384 for 384-bit curves).
- Parameters:
pk (tuple) –
- flexstack.security.ecdsa_backend.reconstruct_public_key(reconstruction_value, issuer_key, e)[source]
ECQV public key extraction (SEC 4 v1.0 section 3.5, as profiled by IEEE Std 1609.2-2016 clause 5.3.2): Q_U = e * P_U + Q_CA, on the curve of the issuer key (NIST P-256 or brainpoolP256r1).
eis the integer hash of the implicit certificate (seeCertificate.implicit_hash_integer). Returns the PublicVerificationKey, or None when it cannot be built.- Parameters:
reconstruction_value (tuple) –
issuer_key (tuple) –
e (int) –
- Return type:
tuple | None
- class flexstack.security.ecdsa_backend.PythonECDSABackend[source]
Bases:
ECDSABackendPython ECDSA backend.
- __init__()[source]
Initialize the Python ECDSA backend.
- Return type:
None
- create_key(algorithm='nistP256')[source]
Creates a new key pair. Returns the identifier of the key.
- Parameters:
algorithm (str) –
nistP256(default),brainpoolP256r1,brainpoolP384r1ornistP384(the curves of the IEEE 1609.2 PublicVerificationKey choices).- Returns:
Identifier of the key.
- Return type:
int
- algorithm(identifier)[source]
Algorithm of a key pair (
nistP256for keys of unknown origin).- Parameters:
identifier (int) –
- Return type:
str
- get_public_key(identifier, compressed=True)[source]
Returns the public key of the key pair with the identifier. Defaults to compressed format (compressed-y-0/1) per IEEE 1609.2 / ETSI TS 103 097.
- Parameters:
identifier (int) – Identifier of the key.
compressed (bool, optional) – Whether to return compressed format (default True).
- Returns:
Public key in IEEE 1609.2 format.
- Return type:
tuple
- sign(data, identifier)[source]
Signs the array of bytes. With the Key that corresponds to the identifier of the private key.
- Parameters:
data (bytes) – Data to sign.
identifier (int) – Identifier of the private key.
- Returns:
Signature in IEEE 1609.2 format.
- Return type:
dict
- verify(data, signature, key)[source]
Verifies the array of bytes. With the Key that corresponds to the identifier.
- Parameters:
data (bytes) – Data to verify.
signature (dict) – Signature to verify. In IEEE 1609.2 format (Signature).
key (int) –
- Returns:
True if the signature is valid, False otherwise.
- Return type:
bool
- verify_with_pk(data, signature, pk)[source]
Verifies the array of bytes with a public key; both come in IEEE 1609.2 format.
datais hashed with the hash of the curve (SHA-256 for the 256-bit curves, SHA-384 for the 384-bit ones); for messages and certificates it is the 1609.2 signing input (seeflexstack.security.certificate.signing_input()).Supported (ETSI TS 103 097 V2.2.1, clause 5.3.1 of IEEE Std 1609.2): NIST P-256, brainpoolP256r1, brainpoolP384r1 and NIST P-384, with the r value given as x-only, compressed or uncompressed point. A signature whose algorithm does not match the key is not valid.
- Parameters:
data (bytes) – Data to verify.
signature (tuple) – Signature to verify. In IEEE 1609.2 format (Signature).
pk (tuple) – Public key to verify. In IEEE 1609.2 format (PublicVerificationKey).
- Returns:
True if the signature is valid, False otherwise.
- Return type:
bool
- private_scalar(identifier)[source]
Private key (scalar) of a key pair, needed by an ECQV issuer.
- Parameters:
identifier (int) –
- Return type:
int
- import_private_scalar(scalar)[source]
Store a NIST P-256 private key given as a scalar (e.g. an ECQV-reconstructed key) and return its identifier.
- Parameters:
scalar (int) –
- Return type:
int
- export_signing_key(identifier)[source]
Export the PEM-encoded signing key for the given identifier.
- Parameters:
identifier (int) – Identifier of the key pair to export.
- Returns:
PEM-encoded private signing key.
- Return type:
bytes
- import_signing_key(key_pem)[source]
Import a PEM-encoded signing key and return its new identifier.
The imported key is appended to the internal key store. Existing keys are not modified.
- Parameters:
key_pem (bytes) – PEM-encoded private signing key (NIST P-256/P-384, brainpoolP256r1/P384r1).
- Returns:
Identifier that can be used with
sign(),get_public_key(), andexport_signing_key().- Return type:
int