Debian Codegraph @

third_party/rust/nss-gk-api/src/ssl.rs

raw
1 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
2 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
3 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
4 // option. This file may not be copied, modified, or distributed
5 // except according to those terms.
6
7 #![allow(
8 dead_code,
9 non_camel_case_types,
10 non_upper_case_globals,
11 non_snake_case,
12 clippy::cognitive_complexity,
13 clippy::too_many_lines,
14 clippy::upper_case_acronyms,
15 unknown_lints,
16 clippy::borrow_as_ptr
17 )]
18
19 use crate::err::{secstatus_to_res, Res};
20 use crate::nss_prelude::*;
21 use crate::prio::PRFileDesc;
22
23 mod nss_ssl {
24 use crate::err::PRErrorCode;
25 use crate::nss_prelude::*;
26 use crate::p11::CERTCertList;
27 use crate::prio::{PRFileDesc, PRFileInfo, PRFileInfo64, PRIOVec};
28
29 include!(concat!(env!("OUT_DIR"), "/nss_ssl.rs"));
30 }
31 pub use nss_ssl::*;
32
33 mod SSLOption {
34 include!(concat!(env!("OUT_DIR"), "/nss_sslopt.rs"));
35 }
36
37 #[derive(Debug, Copy, Clone)]
38 pub enum Opt {
39 Locking,
40 Tickets,
41 OcspStapling,
42 Alpn,
43 ExtendedMasterSecret,
44 SignedCertificateTimestamps,
45 EarlyData,
46 RecordSizeLimit,
47 Tls13CompatMode,
48 HelloDowngradeCheck,
49 SuppressEndOfEarlyData,
50 }
51
52 impl Opt {
53 // Cast is safe here because SSLOptions are within the i32 range
54 #[allow(clippy::cast_possible_wrap)]
55 pub(crate) fn as_int(self) -> PRInt32 {
56 let i = match self {
57 Self::Locking => SSLOption::SSL_NO_LOCKS,
58 Self::Tickets => SSLOption::SSL_ENABLE_SESSION_TICKETS,
59 Self::OcspStapling => SSLOption::SSL_ENABLE_OCSP_STAPLING,
60 Self::Alpn => SSLOption::SSL_ENABLE_ALPN,
61 Self::ExtendedMasterSecret => SSLOption::SSL_ENABLE_EXTENDED_MASTER_SECRET,
62 Self::SignedCertificateTimestamps => SSLOption::SSL_ENABLE_SIGNED_CERT_TIMESTAMPS,
63 Self::EarlyData => SSLOption::SSL_ENABLE_0RTT_DATA,
64 Self::RecordSizeLimit => SSLOption::SSL_RECORD_SIZE_LIMIT,
65 Self::Tls13CompatMode => SSLOption::SSL_ENABLE_TLS13_COMPAT_MODE,
66 Self::HelloDowngradeCheck => SSLOption::SSL_ENABLE_HELLO_DOWNGRADE_CHECK,
67 Self::SuppressEndOfEarlyData => SSLOption::SSL_SUPPRESS_END_OF_EARLY_DATA,
68 };
69 i as PRInt32
70 }
71
72 // Some options are backwards, like SSL_NO_LOCKS, so use this to manage that.
73 fn map_enabled(self, enabled: bool) -> PRIntn {
74 let v = match self {
75 Self::Locking => !enabled,
76 _ => enabled,
77 };
78 PRIntn::from(v)
79 }
80
81 pub(crate) fn set(self, fd: *mut PRFileDesc, value: bool) -> Res<()> {
82 secstatus_to_res(unsafe { SSL_OptionSet(fd, self.as_int(), self.map_enabled(value)) })
83 }
84 }
85
86 /*
87 * TODO: these will be moved to a dedicated module
88 *
89 experimental_api!(SSL_GetCurrentEpoch(
90 fd: *mut PRFileDesc,
91 read_epoch: *mut u16,
92 write_epoch: *mut u16,
93 ));
94 experimental_api!(SSL_HelloRetryRequestCallback(
95 fd: *mut PRFileDesc,
96 cb: SSLHelloRetryRequestCallback,
97 arg: *mut c_void,
98 ));
99 experimental_api!(SSL_RecordLayerWriteCallback(
100 fd: *mut PRFileDesc,
101 cb: SSLRecordWriteCallback,
102 arg: *mut c_void,
103 ));
104 experimental_api!(SSL_RecordLayerData(
105 fd: *mut PRFileDesc,
106 epoch: Epoch,
107 ct: SSLContentType::Type,
108 data: *const u8,
109 len: c_uint,
110 ));
111 experimental_api!(SSL_SendSessionTicket(
112 fd: *mut PRFileDesc,
113 extra: *const u8,
114 len: c_uint,
115 ));
116 experimental_api!(SSL_SetMaxEarlyDataSize(fd: *mut PRFileDesc, size: u32));
117 experimental_api!(SSL_SetResumptionToken(
118 fd: *mut PRFileDesc,
119 token: *const u8,
120 len: c_uint,
121 ));
122 experimental_api!(SSL_SetResumptionTokenCallback(
123 fd: *mut PRFileDesc,
124 cb: SSLResumptionTokenCallback,
125 arg: *mut c_void,
126 ));
127
128 experimental_api!(SSL_GetResumptionTokenInfo(
129 token: *const u8,
130 token_len: c_uint,
131 info: *mut SSLResumptionTokenInfo,
132 len: c_uint,
133 ));
134
135 experimental_api!(SSL_DestroyResumptionTokenInfo(
136 info: *mut SSLResumptionTokenInfo,
137 ));
138 */
139
140 #[cfg(test)]
141 mod tests {
142 use super::{SSL_GetNumImplementedCiphers, SSL_NumImplementedCiphers};
143
144 #[test]
145 fn num_ciphers() {
146 assert!(unsafe { SSL_NumImplementedCiphers } > 0);
147 assert!(unsafe { SSL_GetNumImplementedCiphers() } > 0);
148 assert_eq!(unsafe { SSL_NumImplementedCiphers }, unsafe {
149 SSL_GetNumImplementedCiphers()
150 });
151 }
152 }