use alloc::string::String;
use crate::deviation::Deviation;
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
#[non_exhaustive]
pub enum ParseError {
#[error("not an OCMF record: expected the header `OCMF`")]
NotOcmf,
#[error("truncated record: the header is not followed by `|`")]
MissingDelimiter,
#[error("truncated record: the payload section is not followed by `|`")]
MissingSignatureSection,
#[error("unexpected input at byte {offset} between the end of a section and `|`")]
TrailingSectionBytes {
offset: usize,
},
#[error("too many sections: OCMF defines at most four")]
TooManySections,
#[error("invalid JSON at byte {offset}: expected {expected}")]
Json {
offset: usize,
expected: &'static str,
},
#[error("the {section} section is a JSON {found}, not an object")]
SectionNotAnObject {
section: &'static str,
found: &'static str,
},
#[error("record deviates from the specification: {}", .0.first().map_or_else(|| String::from("(none)"), |d| alloc::format!("{d}")))]
Deviates(alloc::vec::Vec<Deviation>),
#[error("input exceeds the {limit} limit of {allowed}")]
LimitExceeded {
limit: &'static str,
allowed: usize,
},
}
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
#[non_exhaustive]
pub enum KeyError {
#[error("unrecognised public key encoding ({len} bytes)")]
Unrecognised {
len: usize,
},
#[error("malformed DER in the public key: {0}")]
Der(&'static str),
#[error("public key algorithm is not id-ecPublicKey")]
NotEcdsa,
#[error("unknown curve OID `{0}` [OCMF Tab. 23]")]
UnknownCurve(String),
#[error("public key text is neither hexadecimal nor Base64")]
NotEncoded,
#[error("malformed OCA public key composition: {0}")]
OcaComposition(&'static str),
#[error("public key is not a {curve} point: wrong length")]
InvalidPoint {
curve: &'static str,
},
}
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
#[non_exhaustive]
pub enum VerifyError {
#[error("unknown signature algorithm `{algorithm}` [OCMF Tab. 22]")]
UnknownAlgorithm {
algorithm: String,
},
#[error("{algorithm} is not supported by this build ({reason})")]
Unsupported {
algorithm: &'static str,
reason: &'static str,
},
#[error("key is on {key_curve} but the record is signed {algorithm} [OCMF Tab. 22-23]")]
AlgorithmKeyMismatch {
key_curve: &'static str,
algorithm: &'static str,
},
#[error("signature data is not valid {encoding}")]
SignatureEncoding {
encoding: String,
},
#[error("signature is neither DER nor a {expected}-byte raw r||s ({len} bytes)")]
SignatureShape {
len: usize,
expected: usize,
},
#[error("signature scalars are out of range [1, n)")]
SignatureScalars,
#[error("signature `s` is above n/2 on {curve} and the low form was required")]
HighSSignature {
curve: &'static str,
},
#[error("public key is not a point on {curve}")]
KeyNotOnCurve {
curve: &'static str,
},
#[error("signature does not verify against this key")]
NotVerified,
#[error(transparent)]
Key(#[from] KeyError),
}
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
#[non_exhaustive]
pub enum BuildError {
#[error("field `{field}` contains `|`, which is not allowed within a section")]
PipeInField {
field: &'static str,
},
#[error("field `{field}` is required [{spec}]")]
MissingField {
field: &'static str,
spec: &'static str,
},
#[error("field `{field}` is not one of the values [{spec}] defines")]
FieldValue {
field: &'static str,
spec: &'static str,
},
#[error("`SA` must be written: the default algorithm is secp256r1, not {curve} [OCMF Tab. 22]")]
AlgorithmMustBeWritten {
curve: &'static str,
},
#[error("a record needs at least one reading [OCMF §Readings]")]
NoReadings,
#[error("incoherent transaction markers: {reason} [OCMF Tab. 7]")]
TransactionMarkers {
reason: &'static str,
},
#[error("field `{field}` is {len} characters, the maximum is {max} [{spec}]")]
TooLong {
field: &'static str,
len: usize,
max: usize,
spec: &'static str,
},
#[error("the signer failed")]
Signer,
#[error("internal: the signed record failed its own verification ({0})")]
SelfCheck(&'static str),
}
#[cfg(test)]
mod tests {
use super::*;
use alloc::string::ToString;
#[test]
fn messages_name_the_rule_they_enforce() {
let e = ParseError::SectionNotAnObject {
section: "payload",
found: "array",
};
assert_eq!(
e.to_string(),
"the payload section is a JSON array, not an object"
);
}
#[test]
fn unsupported_is_not_not_verified() {
let e = VerifyError::Unsupported {
algorithm: "ECDSA-brainpool256r1-SHA256",
reason: "no pure-Rust implementation; enable `backend-openssl`",
};
assert!(e.to_string().contains("not supported"));
assert_ne!(e, VerifyError::NotVerified);
}
}