s2n_tls/
enums.rs

1// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2// SPDX-License-Identifier: Apache-2.0
3
4#![allow(clippy::missing_safety_doc)] // TODO add safety docs
5
6use crate::error::Error;
7use core::convert::TryFrom;
8use s2n_tls_sys::*;
9
10#[derive(Debug, PartialEq, Copy, Clone)]
11pub enum CallbackResult {
12    Success,
13    Failure,
14}
15
16impl From<CallbackResult> for s2n_status_code::Type {
17    fn from(input: CallbackResult) -> s2n_status_code::Type {
18        match input {
19            CallbackResult::Success => s2n_status_code::SUCCESS,
20            CallbackResult::Failure => s2n_status_code::FAILURE,
21        }
22    }
23}
24
25impl<T, E> From<Result<T, E>> for CallbackResult {
26    fn from(result: Result<T, E>) -> CallbackResult {
27        match result {
28            Ok(_) => CallbackResult::Success,
29            Err(_) => CallbackResult::Failure,
30        }
31    }
32}
33
34/// Corresponds to [s2n_fips_mode].
35#[non_exhaustive]
36#[derive(Debug, PartialEq, Copy, Clone)]
37pub enum FipsMode {
38    Disabled,
39    Enabled,
40}
41
42impl FipsMode {
43    pub fn is_enabled(&self) -> bool {
44        matches!(self, FipsMode::Enabled)
45    }
46}
47
48impl TryFrom<s2n_fips_mode::Type> for FipsMode {
49    type Error = Error;
50
51    fn try_from(input: s2n_fips_mode::Type) -> Result<Self, Self::Error> {
52        let mode = match input {
53            s2n_fips_mode::FIPS_MODE_DISABLED => FipsMode::Disabled,
54            s2n_fips_mode::FIPS_MODE_ENABLED => FipsMode::Enabled,
55            _ => return Err(Error::INVALID_INPUT),
56        };
57
58        Ok(mode)
59    }
60}
61
62/// Corresponds to [s2n_mode].
63#[derive(Debug, PartialEq, Copy, Clone)]
64pub enum Mode {
65    Server,
66    Client,
67}
68
69impl From<Mode> for s2n_mode::Type {
70    fn from(input: Mode) -> s2n_mode::Type {
71        match input {
72            Mode::Server => s2n_mode::SERVER,
73            Mode::Client => s2n_mode::CLIENT,
74        }
75    }
76}
77
78#[non_exhaustive]
79#[derive(Debug, PartialEq, Copy, Clone)]
80pub enum Version {
81    SSLV2,
82    SSLV3,
83    TLS10,
84    TLS11,
85    TLS12,
86    TLS13,
87}
88
89impl TryFrom<s2n_tls_version::Type> for Version {
90    type Error = Error;
91
92    fn try_from(input: s2n_tls_version::Type) -> Result<Self, Self::Error> {
93        let version = match input {
94            s2n_tls_version::SSLV2 => Self::SSLV2,
95            s2n_tls_version::SSLV3 => Self::SSLV3,
96            s2n_tls_version::TLS10 => Self::TLS10,
97            s2n_tls_version::TLS11 => Self::TLS11,
98            s2n_tls_version::TLS12 => Self::TLS12,
99            s2n_tls_version::TLS13 => Self::TLS13,
100            _ => return Err(Error::INVALID_INPUT),
101        };
102        Ok(version)
103    }
104}
105
106#[non_exhaustive]
107#[derive(Debug, PartialEq, Copy, Clone)]
108pub enum CertSNIMatch {
109    /// The client did not supply an SNI
110    NoSNI,
111    ExactMatch,
112    WildcardMatch,
113    NoMatch,
114}
115
116impl TryFrom<s2n_cert_sni_match::Type> for CertSNIMatch {
117    type Error = Error;
118    fn try_from(input: s2n_cert_sni_match::Type) -> Result<Self, Self::Error> {
119        let match_type = match input {
120            s2n_cert_sni_match::SNI_NONE => Self::NoSNI,
121            s2n_cert_sni_match::SNI_EXACT_MATCH => Self::ExactMatch,
122            s2n_cert_sni_match::SNI_WILDCARD_MATCH => Self::WildcardMatch,
123            s2n_cert_sni_match::SNI_NO_MATCH => Self::NoMatch,
124            _ => return Err(Error::INVALID_INPUT),
125        };
126        Ok(match_type)
127    }
128}
129
130#[non_exhaustive]
131#[derive(Debug, PartialEq, Copy, Clone)]
132/// Corresponds to [s2n_blinding].
133pub enum Blinding {
134    SelfService,
135    BuiltIn,
136}
137
138impl From<Blinding> for s2n_blinding::Type {
139    fn from(input: Blinding) -> s2n_blinding::Type {
140        match input {
141            Blinding::SelfService => s2n_blinding::SELF_SERVICE_BLINDING,
142            Blinding::BuiltIn => s2n_blinding::BUILT_IN_BLINDING,
143        }
144    }
145}
146
147/// Corresponds to [s2n_cert_auth_type].
148#[non_exhaustive]
149#[derive(Debug, PartialEq, Copy, Clone)]
150pub enum ClientAuthType {
151    Required,
152    Optional,
153    None,
154}
155
156impl From<ClientAuthType> for s2n_cert_auth_type::Type {
157    fn from(input: ClientAuthType) -> s2n_cert_auth_type::Type {
158        match input {
159            ClientAuthType::Required => s2n_cert_auth_type::REQUIRED,
160            ClientAuthType::Optional => s2n_cert_auth_type::OPTIONAL,
161            ClientAuthType::None => s2n_cert_auth_type::NONE,
162        }
163    }
164}
165
166/// Corresponds to [s2n_alert_behavior].
167#[non_exhaustive]
168#[derive(Debug, PartialEq, Copy, Clone)]
169pub enum AlertBehavior {
170    FailOnWarnings,
171    IgnoreWarnings,
172}
173
174impl From<AlertBehavior> for s2n_alert_behavior::Type {
175    fn from(input: AlertBehavior) -> s2n_alert_behavior::Type {
176        match input {
177            AlertBehavior::FailOnWarnings => s2n_alert_behavior::FAIL_ON_WARNINGS,
178            AlertBehavior::IgnoreWarnings => s2n_alert_behavior::IGNORE_WARNINGS,
179        }
180    }
181}
182
183/// Corresponds to [s2n_tls_signature_algorithm].
184#[non_exhaustive]
185#[derive(Debug, PartialEq, Copy, Clone)]
186#[allow(non_camel_case_types)]
187pub enum SignatureAlgorithm {
188    RSA_PKCS1,
189    RSA_PSS_RSAE,
190    RSA_PSS_PSS,
191    ECDSA,
192    MLDSA,
193}
194
195impl TryFrom<s2n_tls_signature_algorithm::Type> for SignatureAlgorithm {
196    type Error = Error;
197
198    fn try_from(input: s2n_tls_signature_algorithm::Type) -> Result<Self, Self::Error> {
199        let version = match input {
200            s2n_tls_signature_algorithm::RSA => Self::RSA_PKCS1,
201            s2n_tls_signature_algorithm::RSA_PSS_RSAE => Self::RSA_PSS_RSAE,
202            s2n_tls_signature_algorithm::RSA_PSS_PSS => Self::RSA_PSS_PSS,
203            s2n_tls_signature_algorithm::ECDSA => Self::ECDSA,
204            s2n_tls_signature_algorithm::MLDSA => Self::MLDSA,
205            _ => return Err(Error::INVALID_INPUT),
206        };
207        Ok(version)
208    }
209}
210
211/// Corresponds to [s2n_tls_hash_algorithm].
212#[non_exhaustive]
213#[derive(Debug, PartialEq, Copy, Clone)]
214#[allow(non_camel_case_types)]
215pub enum HashAlgorithm {
216    MD5,
217    SHA1,
218    SHA224,
219    SHA256,
220    SHA384,
221    SHA512,
222}
223
224impl TryFrom<s2n_tls_hash_algorithm::Type> for HashAlgorithm {
225    type Error = Error;
226
227    fn try_from(input: s2n_tls_hash_algorithm::Type) -> Result<Self, Self::Error> {
228        let version = match input {
229            s2n_tls_hash_algorithm::MD5 => Self::MD5,
230            s2n_tls_hash_algorithm::SHA1 => Self::SHA1,
231            s2n_tls_hash_algorithm::SHA224 => Self::SHA224,
232            s2n_tls_hash_algorithm::SHA256 => Self::SHA256,
233            s2n_tls_hash_algorithm::SHA384 => Self::SHA384,
234            s2n_tls_hash_algorithm::SHA512 => Self::SHA512,
235            _ => return Err(Error::INVALID_INPUT),
236        };
237        Ok(version)
238    }
239}
240
241/// Corresponds to [s2n_peer_key_update].
242#[non_exhaustive]
243#[derive(Debug, PartialEq, Copy, Clone)]
244pub enum PeerKeyUpdate {
245    KeyUpdateNotRequested,
246    KeyUpdatedRequested,
247}
248
249impl From<PeerKeyUpdate> for s2n_peer_key_update::Type {
250    fn from(input: PeerKeyUpdate) -> s2n_peer_key_update::Type {
251        match input {
252            PeerKeyUpdate::KeyUpdateNotRequested => s2n_peer_key_update::KEY_UPDATE_NOT_REQUESTED,
253            PeerKeyUpdate::KeyUpdatedRequested => s2n_peer_key_update::KEY_UPDATE_REQUESTED,
254        }
255    }
256}
257
258#[non_exhaustive]
259#[derive(Debug)]
260pub enum PskMode {
261    Resumption,
262    External,
263}
264
265impl From<PskMode> for s2n_psk_mode::Type {
266    fn from(input: PskMode) -> Self {
267        match input {
268            PskMode::Resumption => s2n_psk_mode::RESUMPTION,
269            PskMode::External => s2n_psk_mode::EXTERNAL,
270        }
271    }
272}
273
274#[non_exhaustive]
275#[derive(Debug)]
276pub enum PskHmac {
277    SHA256,
278    SHA384,
279}
280
281impl From<PskHmac> for s2n_psk_hmac::Type {
282    fn from(input: PskHmac) -> Self {
283        match input {
284            PskHmac::SHA256 => s2n_psk_hmac::SHA256,
285            PskHmac::SHA384 => s2n_psk_hmac::SHA384,
286        }
287    }
288}
289
290/// Corresponds to [s2n_serialization_version].
291#[non_exhaustive]
292#[derive(Debug, PartialEq, Copy, Clone)]
293pub enum SerializationVersion {
294    None,
295    V1,
296}
297
298impl From<SerializationVersion> for s2n_serialization_version::Type {
299    fn from(input: SerializationVersion) -> s2n_serialization_version::Type {
300        match input {
301            SerializationVersion::None => s2n_serialization_version::SERIALIZED_CONN_NONE,
302            SerializationVersion::V1 => s2n_serialization_version::SERIALIZED_CONN_V1,
303        }
304    }
305}