.. _python_security: =================================== Python Security Subsystem =================================== The security subsystem (``flexstack.security``) implements **ETSI TS 103 097 V2.2.1** on the IEEE 1609.2 data structures, and a PKI client for **ETSI TS 102 941 V2.2.1** and **TS 103 601 V2.1.1**. Key Components ============== .. list-table:: :widths: 30 70 :header-rows: 1 * - Class - Role * - ``PythonECDSABackend`` - ECDSA signing and verification in software (``ecdsa`` library) on NIST P-256, NIST P-384, brainpoolP256r1 and brainpoolP384r1; holds the private keys. * - ``Certificate`` / ``OwnCertificate`` - A decoded certificate with its issuer; an ``OwnCertificate`` also refers to the private key held by the backend. * - ``CertificateLibrary`` - The trust store: Root CAs, Authorization Authorities and known Authorization Tickets (ATs), plus the station's own ATs. * - ``SignService`` - Selects an AT whose ITS-AID, SSP, validity period and region permit the message, signs it, and switches between the AT digest and the full AT. * - ``VerifyService`` - Verifies the signature, the chain (AT, AA, Root CA), the permissions and the generation time and location of received messages. * - ``PkiClient`` - Enrolment and authorization with the EA and AA, CTL and CRL retrieval. Setting Up a Secured Station ============================ The chain used below (Root CA, AA, two ATs and their keys) is created by ``examples/generate_certificate_chain.py`` in the FlexStack repository, in a ``certs/`` folder; see :ref:`certificates_pki_tutorial`. .. code-block:: python from flexstack.security.certificate import Certificate, OwnCertificate from flexstack.security.certificate_library import CertificateLibrary from flexstack.security.ecdsa_backend import PythonECDSABackend from flexstack.security.sign_service import SignService from flexstack.security.verify_service import VerifyService backend = PythonECDSABackend() root_ca = Certificate().decode(open("certs/root_ca.cert", "rb").read(), issuer=None) aa = Certificate().decode(open("certs/aa.cert", "rb").read(), issuer=root_ca) # The station's AT: import its private key into the backend key_id = backend.import_signing_key(open("certs/at1.pem", "rb").read()) at1 = Certificate().decode(open("certs/at1.cert", "rb").read(), issuer=aa) own_at = OwnCertificate(certificate=at1.certificate, issuer=aa, key_id=key_id) cert_library = CertificateLibrary( ecdsa_backend=backend, root_certificates=[root_ca], aa_certificates=[aa], at_certificates=[own_at], ) cert_library.add_own_certificate(own_at) sign_service = SignService(backend=backend, certificate_library=cert_library) verify_service = VerifyService(backend=backend, certificate_library=cert_library, sign_service=sign_service) The services are given to the GeoNetworking router, and security is enabled in the MIB: .. code-block:: python from flexstack.geonet.gn_address import GNAddress, M, ST, MID from flexstack.geonet.mib import GnIfType, GnSecurity, MIB from flexstack.geonet.router import Router as GNRouter mac_address = b"\x02\x11\x22\x33\x44\x55" mib = MIB( itsGnIfType=GnIfType.ITS_G5, itsGnLocalGnAddr=GNAddress(m=M.GN_MULTICAST, st=ST.PASSENGER_CAR, mid=MID(mac_address)), itsGnSecurity=GnSecurity.ENABLED, ) gn_router = GNRouter(mib=mib, sign_service=sign_service, verify_service=verify_service) From then on the router signs every packet it originates (CAMs, DENMs and VAMs with their profiles; Beacons and location-service packets with the GN-MGMT ITS-AID 141), and with ``itsGnSecurity`` enabled it accepts unsecured packets only as single-hop broadcasts for ports that allow them. The complete runnable station is ``examples/secured_cam_sender_and_receiver.py``. Signer and Certificate Rules ============================ - **Digest or certificate.** Messages normally carry the 8-byte digest of the AT (``HashedId8``); the full AT is included when at least one second has passed since it was last included (CAMs and other messages), or when a neighbour requests it through peer-to-peer certificate distribution. DENMs always carry the full AT. - **Signature input.** The signature covers ``Hash(Hash(tbsData) || Hash(signer))``, as required by IEEE 1609.2; received messages are verified over the octets as received. - **Permissions.** Messages that the AT's ITS-AID, SSP, validity period or region do not permit are not sent; received ones are rejected. Obtaining ATs from a PKI ======================== ``PkiClient`` speaks the official TS 102 941 V2.2.1 messages (ECIES-encrypted, AES-CCM). The ITS-S first needs its canonical key pair, which the EA must know before the first enrolment: .. code-block:: python from flexstack.security.pki_client import PkiClient, load_or_create_canonical_key canonical = load_or_create_canonical_key("pki_certs/canonical_key.pem") if canonical.created: # Register the new ITS-S with the EA first, then run again print(canonical.registration_command(canonical.its_id)) ``client = PkiClient(ea_url=..., aa_url=..., ea_cert=..., aa_cert=..., rca_cert=..., canonical_key_pem=canonical.private_pem, certs_dir=..., its_id=canonical.its_id)`` then provisions an AT with ``await client.provision(app_permissions=...)``: enrolment with the EA, then authorization with the AA. The enrolment request is repeated while no response arrives (TS 103 601); the EC and AT are saved to ``certs_dir`` and reused. The complete example is ``examples/secured_cam_with_pki.py``. Butterfly key expansion is not implemented.