Debian Codegraph @

third_party/rust/neqo-crypto/src/secrets.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 use std::{mem, os::raw::c_void, pin::Pin};
8
9 use enum_map::EnumMap;
10 use neqo_common::qdebug;
11 use strum::FromRepr;
12
13 use crate::{
14 agentio::as_c_void,
15 constants::Epoch,
16 err::Res,
17 p11::{PK11SymKey, PK11_ReferenceSymKey, SymKey},
18 ssl::{PRFileDesc, SSLSecretCallback, SSLSecretDirection},
19 };
20
21 experimental_api!(SSL_SecretCallback(
22 fd: *mut PRFileDesc,
23 cb: SSLSecretCallback,
24 arg: *mut c_void,
25 ));
26
27 #[derive(Clone, Copy, Debug, FromRepr)]
28 // Use i32 for Windows MSVC, unless it is MinGW (see
29 // https://bugzilla.mozilla.org/show_bug.cgi?id=1960482). All other platforms
30 // use u32.
31 #[cfg_attr(all(windows, not(target_env = "gnu")), repr(i32))]
32 #[cfg_attr(not(all(windows, not(target_env = "gnu"))), repr(u32))]
33 pub enum SecretDirection {
34 Read = SSLSecretDirection::ssl_secret_read,
35 Write = SSLSecretDirection::ssl_secret_write,
36 }
37
38 impl From<SSLSecretDirection::Type> for SecretDirection {
39 fn from(dir: SSLSecretDirection::Type) -> Self {
40 Self::from_repr(dir).expect("Invalid secret direction")
41 }
42 }
43
44 #[derive(Debug, Default)]
45 pub struct DirectionalSecrets {
46 secrets: EnumMap<Epoch, SymKey>,
47 }
48
49 impl DirectionalSecrets {
50 fn put(&mut self, epoch: Epoch, key: SymKey) {
51 debug_assert!(epoch != Epoch::Initial);
52 self.secrets[epoch] = key;
53 }
54
55 pub fn take(&mut self, epoch: Epoch) -> Option<SymKey> {
56 if self.secrets[epoch].is_null() {
57 None
58 } else {
59 Some(mem::take(&mut self.secrets[epoch]))
60 }
61 }
62 }
63
64 #[derive(Debug, Default)]
65 pub struct Secrets {
66 r: DirectionalSecrets,
67 w: DirectionalSecrets,
68 }
69
70 impl Secrets {
71 unsafe extern "C" fn secret_available(
72 _fd: *mut PRFileDesc,
73 epoch: u16,
74 dir: SSLSecretDirection::Type,
75 secret: *mut PK11SymKey,
76 arg: *mut c_void,
77 ) {
78 let Ok(epoch) = Epoch::try_from(epoch) else {
79 debug_assert!(false, "Invalid epoch");
80 // Don't touch secrets.
81 return;
82 };
83 let Some(secrets) = arg.cast::<Self>().as_mut() else {
84 debug_assert!(false, "No secrets");
85 return;
86 };
87 secrets.put_raw(epoch, dir, secret);
88 }
89
90 fn put_raw(&mut self, epoch: Epoch, dir: SSLSecretDirection::Type, key_ptr: *mut PK11SymKey) {
91 let key_ptr = unsafe { PK11_ReferenceSymKey(key_ptr) };
92 let key = SymKey::from_ptr(key_ptr).expect("NSS shouldn't be passing out NULL secrets");
93 self.put(SecretDirection::from(dir), epoch, key);
94 }
95
96 fn put(&mut self, dir: SecretDirection, epoch: Epoch, key: SymKey) {
97 qdebug!("{dir:?} secret available for {epoch:?}: {key:?}");
98 let keys = match dir {
99 SecretDirection::Read => &mut self.r,
100 SecretDirection::Write => &mut self.w,
101 };
102 keys.put(epoch, key);
103 }
104 }
105
106 #[derive(Debug)]
107 pub struct SecretHolder {
108 secrets: Pin<Box<Secrets>>,
109 }
110
111 impl SecretHolder {
112 /// This registers with NSS. The lifetime of this object needs to match the lifetime
113 /// of the connection, or bad things might happen.
114 pub fn register(&mut self, fd: *mut PRFileDesc) -> Res<()> {
115 let p = as_c_void(&mut self.secrets);
116 unsafe { SSL_SecretCallback(fd, Some(Secrets::secret_available), p) }
117 }
118
119 pub fn take_read(&mut self, epoch: Epoch) -> Option<SymKey> {
120 self.secrets.r.take(epoch)
121 }
122
123 pub fn take_write(&mut self, epoch: Epoch) -> Option<SymKey> {
124 self.secrets.w.take(epoch)
125 }
126 }
127
128 impl Default for SecretHolder {
129 fn default() -> Self {
130 Self {
131 secrets: Box::pin(Secrets::default()),
132 }
133 }
134 }