Skip to main content

Crate utility_pki

Crate utility_pki 

Source
Expand description

§utility-pki

TLS and certificate profiles of the German energy sector for Rust: BDEW AS4, BSI TR-03116-3 and BSI TR-02102-2, checkable and taken table by table from the primary texts.

CI License unsafe forbidden no_std

Website and guides · API reference · Background: AS4 and the SM-PKI

AS4 market communication and the smart-metering WAN have strict TLS rules, spread across the BSI technical guidelines, the SM-PKI certificate policy and the BDEW AS4 documents. utility-pki holds those rules as Rust data and checks four things against them, one call each:

You haveCallYou get
an OpenSSL server or proxyprofile.openssl()?a ready openssl.cnf section, every name verified against OpenSSL
a TLS configurationprofile.audit(&offer)what is required but missing, and what is offered but not allowed, per TLS version
a finished handshakeprofile.check(&session)every violation: version, suite, group, key, mutual TLS, Encrypt-then-MAC
a certificate (DER)profile.check_certificate(&cert)every SM-PKI and AS4 certificate rule it breaks, plus its MP-ID and endpoint URI
cargo add utility-pki --features x509

§Check a handshake

use utility_pki::profiles::BDEW_AS4;
use utility_pki::{CipherSuite, Curve, NamedGroup, PublicKey, Session, TlsVersion};

let session = Session {
    group: Some(NamedGroup::BRAINPOOLP256R1),
    mutual_auth: true,
    peer_key: Some(PublicKey::Ec(Curve::BrainpoolP256r1)),
    ..Session::new(TlsVersion::TLS12, CipherSuite::TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256)
};
let report = BDEW_AS4.check(&session);
assert!(report.is_ok());
for violation in report.violations() {
    eprintln!("refusing: {violation}");
}

§Check a certificate (x509 feature)

ⓘ
use utility_pki::profiles::BDEW_AS4;
use utility_pki::x509::Certificate;

let cert = Certificate::from_der(&der)?; // strict DER, never panics
for v in BDEW_AS4.check_certificate(&cert).violations() {
    eprintln!("{v}"); // "2 URIs in SubjectAltName, exactly 1 required (the AS4 endpoint)"
}
assert_eq!(cert.mp_id(), Some(party_id)); // the OU, only if there is exactly one

The check covers the following rules:

  • Key: ECDSA on brainpoolP256r1.
  • Extensions: exactly the set TR-03109-4 lists, with the right criticality.
  • KeyUsage and ExtendedKeyUsage, and cA = FALSE.
  • Subject: O = SM-PKI-DE, which refuses test-PKI certificates; for AS4, also the MP-ID in exactly one OU.
  • SubjectAltName: no wildcards; for AS4, exactly one endpoint URI.
  • Validity: at most two years for AS4.

Every rule →

§Configure OpenSSL

$ cargo run --example openssl -- bdew-as4
MinProtocol = TLSv1.2
MaxProtocol = TLSv1.3
CipherString = ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-ECDSA-AES256-SHA384
Ciphersuites = TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_SHA384:TLS_AES_128_CCM_SHA256
Groups = brainpoolP256r1tls13:brainpoolP384r1tls13:secp256r1:brainpoolP256r1:brainpoolP384r1:brainpoolP512r1tls13:secp384r1:brainpoolP512r1
SignatureAlgorithms = ecdsa_brainpoolP256r1tls13_sha256:…:ecdsa_secp521r1_sha512
Options = ServerPreference,NoRenegotiation,EncryptThenMac,ExtendedMasterSecret

OpenSSL 3.2 or later is the common library that meets BDEW AS4. rustls has no CBC, CCM or Brainpool suites.

§Profiles

ProfileForPrimary texts
BDEW_AS4market communication over AS4 (electricity, gas)AS4-Profil 1.2, RzÜ AS4 2.6, TR-03116-3, TR-03109-4, CP SM-PKI
BSI_TR_03116_3the Smart Metering PKI: WAN and market communicationTR-03116-3 Stand 2025, TR-03109-4 1.2.1, CP SM-PKI 1.1.2
BSI_TR_02102_2general TLS; the crypto baseline for IEC 62443-4-2 CR 4.3TR-02102-2 2026-01

Profiles are const data. Derive your own with struct update, for example Profile { mutual_auth: true, ..BSI_TR_02102_2 }. The guides cover the details:

§Design

  • Primary texts only. Each rule cites document, version and table. What cannot be verified is left out, not guessed.
  • MUST is not SHOULD. required and optional are kept apart, per TLS version. “Anything” must be written Allowed::Any, never an empty list.
  • Unknown never passes. An unreported value, an unknown curve or extension, bytes that are not strict DER: each is a finding or an error.
  • no_std, no allocator, no dependencies, no panics, with every feature. CI scans the release build for panic paths and fuzzes the DER reader.

utility-pki judges; it does not perform TLS, verify signatures or fetch CRLs. A passing check is evidence for your conformity assessment, not a certification. See the threat model and security policy.

§License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.

Not affiliated with the BSI, the BDEW, the Bundesnetzagentur or the IEC.

Re-exports§

pub use cert::CertRules;
pub use cert::CertSignature;
pub use cert::ExtendedKeyUsage;
pub use cert::Extension;
pub use cert::KeyUsage;
pub use cert::SanRules;
pub use cert::SubjectRules;
pub use check::Finding;
pub use check::Item;
pub use check::Offer;
pub use check::Report;
pub use check::Session;
pub use check::Violation;
pub use ids::CipherSuite;
pub use ids::NamedGroup;
pub use ids::SignatureScheme;
pub use ids::TlsVersion;
pub use openssl::OpensslConfig;
pub use openssl::Unsupported;
pub use profile::Allowed;
pub use profile::Curve;
pub use profile::KeyRule;
pub use profile::Level;
pub use profile::Profile;
pub use profile::PublicKey;
pub use profile::Revocation;
pub use profile::VersionRules;

Modules§

cert
What a profile requires of a TLS certificate, as data. The check itself, which reads DER, is in the x509 module behind the feature of the same name.
check
Checking against a profile: a negotiated Session with Profile::check, and a configuration with Profile::audit.
ids
TLS identifiers from the IANA registries: protocol versions, cipher suites, named groups and signature schemes. Each is a newtype over its wire value, so an unknown code from a peer is representable and simply has no name.
openssl
A profile as OpenSSL configuration (openssl.cnf, SSL_CONF_cmd), without allocating.
profile
A security profile: what a standard allows for TLS and certificates, as data.
profiles
The profiles this crate ships, each checked table by table against its primary text.
x509x509
Certificate checks: read a DER certificate once, then check it against any profile.