Expand description
The Open Charge Metering Format (OCMF), read without disturbing the bytes its signature covers.
OCMF is the container a certified meter in an EV charging station puts a reading into and signs, so that a driver can check months later that the kilowatt-hours on the invoice are the kilowatt-hours the meter measured. One record is one line of text:
OCMF|{…payload JSON…}|{…signature JSON…}§The rule everything else follows from
The signature is ECDSA over SHA-256 of the payload section exactly as it
was written. [OCMF §JSON based OCMF Format] is explicit: “between signing
and validation, the payload section must not be manipulated (removing and
adding white spaces), otherwise positive validation is not possible”.
A parser that deserialises into a struct and re-serialises to verify has
already lost — key order, whitespace, number formatting and Unicode escapes
are all free to change, and every one of them changes the hash. So
Record::signed_bytes returns a slice of the input, and there is no API
anywhere in this crate that produces signable bytes from a typed value.
let text = r#"OCMF|{"FV":"1.0","PG":"T1","MS":"12345678","IS":true,"IF":[],"IT":"ISO14443","ID":"1F2D3A4F","RD":[{"TM":"2018-07-24T13:22:04,000+0200 S","TX":"B","RV":2935.600,"RI":"1-b:1.8.0","RU":"kWh","EF":"","ST":"G"}]}|{"SD":"3045"}"#;
let record = ocmf::Record::parse(text)?;
// Reading a record and writing it back is the identity function.
assert_eq!(record.to_string(), text);
// `2935.600` states three valid decimal places, and keeps them.
let reading = &record.payload().readings()[0];
assert_eq!(reading.value().unwrap().as_str(), "2935.600");
assert_eq!(reading.value().unwrap().value().scale(), 3);§What real records look like
This crate was written from the specification and from every OCMF record in the S.A.F.E. reference corpus — 256 records, 705 readings, measured. The specification and the field diverge, a lot:
- 89 % of records omit
MS, which[OCMF Tab. 3]marks1..1. - 29 % of readings omit
TM, relying on carry-forward from the reading before them. - 23 readings write
RVas a JSON string, with leading zeros. - Two records carry a bare 64-byte
r‖ssignature and a public key with no SEC1 prefix and noSubjectPublicKeyInfo. One of them verifies — and no other implementation, including the reference verifier that ships it as test data, can check it.
So parsing runs in a Profile, and every departure from the specification
becomes a Deviation with a citation rather than an error or a silence:
let record = ocmf::Record::parse(text)?;
for d in record.deviations() {
// "`MS` is absent although it is mandatory at byte 5 [OCMF Tab. 3, S.A.F.E. issue #41]"
println!("{d}");
}§What this crate answers, and what it does not
| Question | Answered by |
|---|---|
| Did this key sign these bytes? | verify() |
| Is this key this charge point’s key? | out of band — a register, the station’s display, a contract |
| Were records removed from the session? | session |
| May these values be billed? | not here: that is law, tariffs and a key registry |
Conflating the four is how a “verified” charging session turns out to be a signed fragment of a session somebody edited.
§No I/O, no clock, no randomness, no unsafe
#![no_std] with alloc, #![forbid(unsafe_code)]. Nothing here opens a
socket, reads a file or asks the time — every instant is an argument — so a
fleet’s worth of verification is a deterministic unit test, and a dispute
from four years ago replays exactly as it happened. Signing is RFC 6979
deterministic and verification is arithmetic over public data, so the crate
builds for thumbv7em-none-eabihf and wasm32-unknown-unknown alike.
Re-exports§
pub use deviation::Departure;pub use deviation::Deviation;pub use deviation::DeviationKind;pub use deviation::Location;pub use deviation::Profile;pub use error::BuildError;pub use error::KeyError;pub use error::ParseError;pub use error::VerifyError;pub use key::PublicKey;pub use limits::Limits;pub use number::Number;pub use obis::ObisCode;pub use obis::Register;pub use payload::ChargePointIdType;pub use payload::CurrentType;pub use payload::ErrorFlags;pub use payload::Explicit;pub use payload::IdentificationFlag;pub use payload::IdentificationLevel;pub use payload::IdentificationType;pub use payload::LossCompensation;pub use payload::MeterState;pub use payload::Pagination;pub use payload::PaginationContext;pub use payload::Payload;pub use payload::Reading;pub use payload::RegisterSeries;pub use payload::TransactionMarker;pub use payload::Unit;pub use record::HEADER;pub use record::Record;pub use record::RecordBuf;pub use signature::Curve;pub use signature::SignatureAlgorithm;pub use signature::SignatureEncoding;pub use signature::SignatureSection;pub use summary::RecordSummary;pub use time::OcmfTime;pub use time::TimeStatus;pub use verify::Malleability;verifypub use verify::Verified;verifypub use verify::VerifyOptions;verifypub use verify::is_supported;verifypub use verify::supported_curves;verifypub use verify::verify;verifypub use verify::verify_bytes;verifypub use verify::verify_key_text;verifypub use verify::verify_with;verifypub use sign::ExternalSigner;signpub use sign::LossCompensationSpec;signpub use sign::ReadingSpec;signpub use sign::RecordBuilder;signpub use sign::Signer;signpub use session::Finding;sessionpub use session::RegisterTotal;sessionpub use session::SequenceKind;sessionpub use session::SessionReport;session
Modules§
- der
- A deliberately lenient DER reader for the two structures OCMF needs.
- deviation
- Departures from the specification, reported rather than swallowed.
- encoding
- Hex and Base64, tolerant in exactly the ways OCMF data requires.
- error
- Errors, one enum per phase.
- json
- A borrowed, span-tracking JSON reader.
- key
- Public keys, in the five shapes they actually arrive in.
- limits
- Bounds on what a piece of input may cost to read.
- number
- Exact decimals that remember how they were written.
- obis
RI: OBIS codes, as they are written rather than as they are specified.- ocpp
ocpp - Carrying a record over OCPP, per the OCA application note.
- payload
- The payload section: everything the signature covers.
- record
- The record: three sections, and the bytes the signature covers.
- session
session - The rules the specification assigns to a check component, not to the format.
- sign
sign - Building and signing records.
- signature
- The signature section:
SA,SE,SM,SD[OCMF Tab. 8]. - summary
- A serialisable report about a record — never the record itself.
- time
TM: a timestamp and a synchronisation state, welded into one field.- verify
verify - ECDSA verification, across every algorithm
[OCMF Tab. 22]defines. - xml
xml - The S.A.F.E. transparency container.