Skip to main content

zerodds_security/
error.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2026 ZeroDDS Contributors
3
4//! Security-Error-Typen. OMG DDS-Security 1.1 §8.1.2 `SecurityException`.
5//!
6//! Ein gemeinsamer Error-Typ ueber alle Plugins — damit Call-Sites nicht
7//! fuenf verschiedene Result-Variants handhaben muessen. Die `Kind`-
8//! Varianten matchen die Spec-Fehlercodes.
9
10extern crate alloc;
11
12use alloc::borrow::Cow;
13use core::fmt;
14
15/// Security-Operation-Fehler.
16#[derive(Debug, Clone, PartialEq, Eq)]
17#[non_exhaustive]
18pub struct SecurityError {
19    /// Fehler-Kategorie.
20    pub kind: SecurityErrorKind,
21    /// Menschenlesbarer Hinweis (nicht fuer Endnutzer — intern).
22    pub detail: Cow<'static, str>,
23}
24
25/// Kategorie des Security-Fehlers.
26///
27/// Spec-Bezug OMG DDS-Security 1.1 §8.1.2. Die Codes sind offen
28/// gehalten (`#[non_exhaustive]`), damit v1.4 zusaetzliche Varianten
29/// ohne Breaking-Change einfuegen kann.
30#[derive(Debug, Clone, Copy, PartialEq, Eq)]
31#[non_exhaustive]
32pub enum SecurityErrorKind {
33    /// Identity-Handshake fehlgeschlagen (Zertifikat ungueltig,
34    /// Signatur falsch, Peer nicht im Trust-Store).
35    AuthenticationFailed,
36    /// Peer hat keine Permission fuer diese Operation.
37    AccessDenied,
38    /// Cryptographic-Operation fehlgeschlagen (AES-GCM-Tag-Verify,
39    /// HMAC-Mismatch, Key-unbekannt).
40    CryptoFailed,
41    /// Fehlende oder unplausible Konfiguration (z.B. Cert fehlt,
42    /// Permissions-XML parst nicht).
43    InvalidConfiguration,
44    /// Ungueltiges Argument (None wo Some erwartet, leerer Key usw.).
45    BadArgument,
46    /// Feature nicht implementiert. v1.3-Plugin-SPI signalisiert so,
47    /// wenn eine Methode in v1.4 vorgesehen ist.
48    NotImplemented,
49    /// Interne unerwartete Error — Plugin-Bug.
50    Internal,
51}
52
53impl SecurityError {
54    /// Konstruktor.
55    #[must_use]
56    pub fn new(kind: SecurityErrorKind, detail: impl Into<Cow<'static, str>>) -> Self {
57        Self {
58            kind,
59            detail: detail.into(),
60        }
61    }
62
63    /// Shortcut fuer `NotImplemented`.
64    #[must_use]
65    pub fn not_implemented(detail: impl Into<Cow<'static, str>>) -> Self {
66        Self::new(SecurityErrorKind::NotImplemented, detail)
67    }
68
69    /// Shortcut fuer `BadArgument`.
70    #[must_use]
71    pub fn bad_argument(detail: impl Into<Cow<'static, str>>) -> Self {
72        Self::new(SecurityErrorKind::BadArgument, detail)
73    }
74}
75
76impl fmt::Display for SecurityError {
77    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
78        write!(f, "security error [{:?}]: {}", self.kind, self.detail)
79    }
80}
81
82#[cfg(feature = "std")]
83impl std::error::Error for SecurityError {}
84
85/// Plugin-Operation-Result.
86pub type SecurityResult<T> = core::result::Result<T, SecurityError>;