|
1 | 1 | use std::fmt::Debug; |
2 | 2 |
|
3 | 3 | use serde::{Deserialize, Serialize}; |
4 | | -use zeroize::ZeroizeOnDrop; |
5 | | - |
6 | 4 | use super::access_point::{AccessPoint, SecurityFeatures}; |
7 | 5 | use super::error::ConnectionError; |
| 6 | +use super::passphrase::Passphrase; |
8 | 7 | use super::saved_connection::SavedConnectionBrief; |
9 | 8 |
|
10 | 9 | /// Visible Wi-Fi access points grouped by interface and SSID for applet UIs. |
@@ -860,97 +859,6 @@ impl EapOptionsBuilder { |
860 | 859 | } |
861 | 860 | } |
862 | 861 |
|
863 | | -/// A memory-safe wrapper around [`String`] to protect secret passphrases. |
864 | | -/// |
865 | | -/// Guarantees that the underlying memory is zeroized on [`Drop`], preventing the passphrase from |
866 | | -/// leaking. Also hides the passphrase from [`Debug`]. |
867 | | -/// |
868 | | -/// # Usage |
869 | | -/// Passphrase data should always be held within a [`Passphrase`] for as long as possible within |
870 | | -/// its lifetime. |
871 | | -/// |
872 | | -/// [`Passphrase::reveal`] exists for flexibility and returns the inner [`String`], but it forfeits |
873 | | -/// the protection which this type provides - use with care. |
874 | | -/// |
875 | | -/// # Examples |
876 | | -/// ``` |
877 | | -/// use nmrs::Passphrase; |
878 | | -/// use zeroize::Zeroize; |
879 | | -/// |
880 | | -/// fn main() { |
881 | | -/// let s: String = "password".to_string(); |
882 | | -/// let mut pass = Passphrase::from(s); |
883 | | -/// |
884 | | -/// // Get the String back if needed. |
885 | | -/// let mut revealed = pass.reveal(); |
886 | | -/// |
887 | | -/// // ... |
888 | | -/// |
889 | | -/// // Revealed passphrases must be zeroized manually. |
890 | | -/// revealed.zeroize(); |
891 | | -/// } |
892 | | -/// ``` |
893 | | -#[derive(Clone, Default, Eq, PartialEq, ZeroizeOnDrop)] |
894 | | -pub struct Passphrase(String); |
895 | | - |
896 | | -impl Passphrase { |
897 | | - pub fn new(passphrase: String) -> Self { |
898 | | - Passphrase(passphrase) |
899 | | - } |
900 | | - |
901 | | - pub fn len(&self) -> usize { |
902 | | - self.0.len() |
903 | | - } |
904 | | - |
905 | | - pub fn is_empty(&self) -> bool { |
906 | | - self.0.is_empty() |
907 | | - } |
908 | | - |
909 | | - /// Moves the inner [`String`] outside of [`Passphrase`]. |
910 | | - /// |
911 | | - /// # Security |
912 | | - /// * [`Debug`] is no longer protected. |
913 | | - /// * [`ZeroizeOnDrop`] will no longer apply since the inner [`String`] is returned so |
914 | | - /// `zeroize()` *must* be called manually before [`Drop`] occurs: |
915 | | - /// ``` |
916 | | - /// use nmrs::Passphrase; |
917 | | - /// use zeroize::Zeroize; |
918 | | - /// { |
919 | | - /// let mut passphrase: Passphrase = Passphrase::new("password".to_string()); |
920 | | - /// let mut revealed = passphrase.reveal(); |
921 | | - /// |
922 | | - /// // ... |
923 | | - /// |
924 | | - /// revealed.zeroize(); |
925 | | - /// } // Dropped here |
926 | | - /// ``` |
927 | | - pub fn reveal(mut self) -> String { |
928 | | - std::mem::take(&mut self.0) |
929 | | - } |
930 | | - |
931 | | - /// Returns a borrowed reference to the inner [`String`]. See [`Passphrase::reveal`] for moving |
932 | | - /// the inner value. |
933 | | - /// |
934 | | - /// # Security |
935 | | - /// The returned reference is **not** protected by zeroization or from being logged and should not be |
936 | | - /// cloned. |
937 | | - pub fn reveal_ref(&self) -> &str { |
938 | | - &self.0 |
939 | | - } |
940 | | -} |
941 | | - |
942 | | -impl Debug for Passphrase { |
943 | | - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
944 | | - f.debug_tuple("Passphrase").field(&"[REDACTED]").finish() |
945 | | - } |
946 | | -} |
947 | | - |
948 | | -impl From<String> for Passphrase { |
949 | | - fn from(s: String) -> Self { |
950 | | - Passphrase(s) |
951 | | - } |
952 | | -} |
953 | | - |
954 | 862 | /// Wi-Fi connection security types. |
955 | 863 | /// |
956 | 864 | /// Represents the authentication method for connecting to a WiFi network. |
|
0 commit comments