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)]
108/// Corresponds to [s2n_blinding].
109pub enum Blinding {
110    SelfService,
111    BuiltIn,
112}
113
114impl From<Blinding> for s2n_blinding::Type {
115    fn from(input: Blinding) -> s2n_blinding::Type {
116        match input {
117            Blinding::SelfService => s2n_blinding::SELF_SERVICE_BLINDING,
118            Blinding::BuiltIn => s2n_blinding::BUILT_IN_BLINDING,
119        }
120    }
121}
122
123/// Corresponds to [s2n_cert_auth_type].
124#[non_exhaustive]
125#[derive(Debug, PartialEq, Copy, Clone)]
126pub enum ClientAuthType {
127    Required,
128    Optional,
129    None,
130}
131
132impl From<ClientAuthType> for s2n_cert_auth_type::Type {
133    fn from(input: ClientAuthType) -> s2n_cert_auth_type::Type {
134        match input {
135            ClientAuthType::Required => s2n_cert_auth_type::REQUIRED,
136            ClientAuthType::Optional => s2n_cert_auth_type::OPTIONAL,
137            ClientAuthType::None => s2n_cert_auth_type::NONE,
138        }
139    }
140}
141
142/// Corresponds to [s2n_alert_behavior].
143#[non_exhaustive]
144#[derive(Debug, PartialEq, Copy, Clone)]
145pub enum AlertBehavior {
146    FailOnWarnings,
147    IgnoreWarnings,
148}
149
150impl From<AlertBehavior> for s2n_alert_behavior::Type {
151    fn from(input: AlertBehavior) -> s2n_alert_behavior::Type {
152        match input {
153            AlertBehavior::FailOnWarnings => s2n_alert_behavior::FAIL_ON_WARNINGS,
154            AlertBehavior::IgnoreWarnings => s2n_alert_behavior::IGNORE_WARNINGS,
155        }
156    }
157}
158
159/// Corresponds to [s2n_tls_signature_algorithm].
160#[non_exhaustive]
161#[derive(Debug, PartialEq, Copy, Clone)]
162#[allow(non_camel_case_types)]
163pub enum SignatureAlgorithm {
164    RSA_PKCS1,
165    RSA_PSS_RSAE,
166    RSA_PSS_PSS,
167    ECDSA,
168}
169
170impl TryFrom<s2n_tls_signature_algorithm::Type> for SignatureAlgorithm {
171    type Error = Error;
172
173    fn try_from(input: s2n_tls_signature_algorithm::Type) -> Result<Self, Self::Error> {
174        let version = match input {
175            s2n_tls_signature_algorithm::RSA => Self::RSA_PKCS1,
176            s2n_tls_signature_algorithm::RSA_PSS_RSAE => Self::RSA_PSS_RSAE,
177            s2n_tls_signature_algorithm::RSA_PSS_PSS => Self::RSA_PSS_PSS,
178            s2n_tls_signature_algorithm::ECDSA => Self::ECDSA,
179            _ => return Err(Error::INVALID_INPUT),
180        };
181        Ok(version)
182    }
183}
184
185/// Corresponds to [s2n_tls_hash_algorithm].
186#[non_exhaustive]
187#[derive(Debug, PartialEq, Copy, Clone)]
188#[allow(non_camel_case_types)]
189pub enum HashAlgorithm {
190    MD5,
191    SHA1,
192    SHA224,
193    SHA256,
194    SHA384,
195    SHA512,
196}
197
198impl TryFrom<s2n_tls_hash_algorithm::Type> for HashAlgorithm {
199    type Error = Error;
200
201    fn try_from(input: s2n_tls_hash_algorithm::Type) -> Result<Self, Self::Error> {
202        let version = match input {
203            s2n_tls_hash_algorithm::MD5 => Self::MD5,
204            s2n_tls_hash_algorithm::SHA1 => Self::SHA1,
205            s2n_tls_hash_algorithm::SHA224 => Self::SHA224,
206            s2n_tls_hash_algorithm::SHA256 => Self::SHA256,
207            s2n_tls_hash_algorithm::SHA384 => Self::SHA384,
208            s2n_tls_hash_algorithm::SHA512 => Self::SHA512,
209            _ => return Err(Error::INVALID_INPUT),
210        };
211        Ok(version)
212    }
213}
214
215/// Corresponds to [s2n_peer_key_update].
216#[non_exhaustive]
217#[derive(Debug, PartialEq, Copy, Clone)]
218pub enum PeerKeyUpdate {
219    KeyUpdateNotRequested,
220    KeyUpdatedRequested,
221}
222
223impl From<PeerKeyUpdate> for s2n_peer_key_update::Type {
224    fn from(input: PeerKeyUpdate) -> s2n_peer_key_update::Type {
225        match input {
226            PeerKeyUpdate::KeyUpdateNotRequested => s2n_peer_key_update::KEY_UPDATE_NOT_REQUESTED,
227            PeerKeyUpdate::KeyUpdatedRequested => s2n_peer_key_update::KEY_UPDATE_REQUESTED,
228        }
229    }
230}
231
232#[non_exhaustive]
233#[derive(Debug)]
234pub enum PskMode {
235    Resumption,
236    External,
237}
238
239impl From<PskMode> for s2n_psk_mode::Type {
240    fn from(input: PskMode) -> Self {
241        match input {
242            PskMode::Resumption => s2n_psk_mode::RESUMPTION,
243            PskMode::External => s2n_psk_mode::EXTERNAL,
244        }
245    }
246}
247
248#[non_exhaustive]
249#[derive(Debug)]
250pub enum PskHmac {
251    SHA256,
252    SHA384,
253}
254
255impl From<PskHmac> for s2n_psk_hmac::Type {
256    fn from(input: PskHmac) -> Self {
257        match input {
258            PskHmac::SHA256 => s2n_psk_hmac::SHA256,
259            PskHmac::SHA384 => s2n_psk_hmac::SHA384,
260        }
261    }
262}
263
264/// Corresponds to [s2n_serialization_version].
265#[non_exhaustive]
266#[derive(Debug, PartialEq, Copy, Clone)]
267pub enum SerializationVersion {
268    None,
269    V1,
270}
271
272impl From<SerializationVersion> for s2n_serialization_version::Type {
273    fn from(input: SerializationVersion) -> s2n_serialization_version::Type {
274        match input {
275            SerializationVersion::None => s2n_serialization_version::SERIALIZED_CONN_NONE,
276            SerializationVersion::V1 => s2n_serialization_version::SERIALIZED_CONN_V1,
277        }
278    }
279}