1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
//! 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:
//!
//! ```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.
//!
//! ```
//! # fn main() -> Result<(), Box<dyn core::error::Error>> {
//! 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);
//! # Ok(()) }
//! ```
//!
//! # 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]` marks `1..1`.
//! - 29 % of readings omit `TM`, relying on carry-forward from the reading
//! before them.
//! - 23 readings write `RV` as a JSON *string*, with leading zeros.
//! - Two records carry a bare 64-byte `r‖s` signature and a public key with no
//! SEC1 prefix and no `SubjectPublicKeyInfo`. 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:
//!
//! ```
//! # fn main() -> Result<(), Box<dyn core::error::Error>> {
//! # let text = r#"OCMF|{"FV":"1.0","PG":"T1","IS":true,"IF":[],"IT":"NONE","RD":[{"TM":"2018-07-24T13:22:04,000+0200 S","TX":"B","RV":1,"RI":"1-b:1.8.0","RU":"kWh","ST":"G"}]}|{"SD":"3045"}"#;
//! 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}");
//! }
//! # Ok(()) }
//! ```
//!
//! # 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? | [`mod@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.
extern crate alloc;
extern crate std;
/// Everything ECDSA needs to know about a signature scalar, derived from the
/// curve's own order. Nothing outside verification and signing has a use for
/// it, and both of those are behind `verify`.
pub use ;
pub use ;
pub use PublicKey;
pub use Limits;
pub use Number;
pub use ;
pub use ;
pub use ;
pub use ;
pub use RecordSummary;
pub use ;
pub use ;
pub use ;
pub use ;
/// Quotes a value for an error message, bounding its length.
///
/// Every field in an OCMF record is attacker-influenced text — the reference
/// corpus literally ships JNDI payloads inside a public key field — so nothing
/// unbounded and nothing unescaped ever reaches a message.
pub