utility-pki

Getting started

Install utility-pki and check a TLS configuration, a handshake and an SM-PKI certificate against BDEW AS4 in four calls.

Install

cargo add utility-pki                    # profiles, audit, session check, OpenSSL export
cargo add utility-pki --features x509    # plus the certificate check

The crate has no dependencies, needs no allocator and runs on no_std targets, the x509 feature included.

Pick a profile

Your standard saysUse
BDEW AS4 market communicationprofiles::BDEW_AS4
BSI TR-03116-3 (smart metering WAN, SM-PKI)profiles::BSI_TR_03116_3
BSI TR-02102-2, or "state of the art" (IEC 62443)profiles::BSI_TR_02102_2

Each profile is a const. To tighten one for your deployment, use struct update: Profile { mutual_auth: true, ..BSI_TR_02102_2 }.

1. Configure your TLS library

For OpenSSL 3.2 or later, export the profile and paste the section into your openssl.cnf:

cargo run --example openssl -- bdew-as4

OpenSSL configuration explains how to wire the section in and what must still be set in code. For any other library, read the lists from the profile: BDEW_AS4.tls12, .tls13, each with required() and optional() items.

2. Audit the configuration

Describe what your endpoint offers in its hello, and audit it at start-up or in a test:

use utility_pki::profiles::BDEW_AS4;
use utility_pki::{CipherSuite as C, NamedGroup as G, Offer, TlsVersion as V};

let offer = Offer {
    versions: &[V::TLS13, V::TLS12],
    cipher_suites: &[C::TLS_AES_128_GCM_SHA256, C::TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256],
    groups: &[G::SECP256R1, G::BRAINPOOLP256R1, G::BRAINPOOLP256R1_TLS13],
    ..Offer::default()
};
for finding in BDEW_AS4.audit(&offer) {
    eprintln!("{finding}"); // "cipher suite … is required but not offered"
}

The audit reports two kinds of finding:

  • Missing: an item the standard marks MUST that you do not offer.
  • NotAllowed: an item you offer that the standard's tables do not list.

The audit is checked per TLS version.

3. Check every session

After each handshake, read what was negotiated and check it:

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

let session = Session {
    group: Some(NamedGroup::BRAINPOOLP256R1_TLS13),
    mutual_auth: true,
    peer_key: Some(PublicKey::Ec(Curve::BrainpoolP256r1)),
    ..Session::new(TlsVersion::TLS13, CipherSuite::TLS_AES_128_GCM_SHA256)
};
let report = BDEW_AS4.check(&session);
for v in report.violations() {
    eprintln!("refusing session: {v}");
}

A restricted value that you leave as None is reported as unknown (GroupUnknown, PeerKeyUnknown). It never passes.

4. Check the certificate

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

let cert = Certificate::from_der(&der)?;
for v in BDEW_AS4.check_certificate(&cert).violations() {
    eprintln!("{v}");
}
if cert.mp_id() != Some(party_id) { /* not the partner you expected */ }

The certificate also fills the session check:

let session = Session { peer_key: cert.public_key(), peer_cert_validity_days: Some(cert.validity_days()), ..session };

The rules, the facts it exposes and the test PKI are covered in Certificates.

What else is in a profile

Some rules show in neither a handshake nor a certificate, such as the 48-hour session limit, separate key pairs per purpose, and CRL timing. They are data (max_session_lifetime, renegotiation, sni, …) or listed in notes. To print everything:

cargo run --example profiles

Edit this page on GitHub