# 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.**
[](https://github.com/hupe1980/utility-pki/actions/workflows/ci.yml)
[](#license)
[](https://github.com/hupe1980/utility-pki/blob/main/src/lib.rs)
[](#design)
**[Website and guides](https://hupe1980.github.io/utility-pki/) · [API reference](https://docs.rs/utility-pki) · [Background: AS4 and the SM-PKI](https://hupe1980.github.io/utility-pki/docs/background/)**
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:
| an OpenSSL server or proxy | `profile.openssl()?` | a ready `openssl.cnf` section, every name verified against OpenSSL |
| a TLS configuration | `profile.audit(&offer)` | what is required but missing, and what is offered but not allowed, per TLS version |
| a finished handshake | `profile.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 |
```sh
cargo add utility-pki --features x509
```
## Check a handshake
```rust
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)
```rust,ignore
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 →](https://hupe1980.github.io/utility-pki/docs/certificates/)
## Configure OpenSSL
```console
$ 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
| `BDEW_AS4` | market 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_3` | the Smart Metering PKI: WAN and market communication | TR-03116-3 Stand 2025, TR-03109-4 1.2.1, CP SM-PKI 1.1.2 |
| `BSI_TR_02102_2` | general TLS; the crypto baseline for IEC 62443-4-2 CR 4.3 | TR-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:
- [all TLS rules with their tables](https://hupe1980.github.io/utility-pki/docs/profiles/);
- [onboarding against the SM-Test-PKI](https://hupe1980.github.io/utility-pki/docs/certificates/#the-test-pki);
- [IEC 62443](https://hupe1980.github.io/utility-pki/docs/iec-62443/).
## 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](https://github.com/hupe1980/utility-pki/blob/main/THREAT_MODEL.md) and [security policy](https://github.com/hupe1980/utility-pki/blob/main/SECURITY.md).
## License
Licensed under either of [Apache License, Version 2.0](https://github.com/hupe1980/utility-pki/blob/main/LICENSE-APACHE) or
[MIT license](https://github.com/hupe1980/utility-pki/blob/main/LICENSE-MIT) at your option.
Not affiliated with the BSI, the BDEW, the Bundesnetzagentur or the IEC.