@@ -6,7 +6,6 @@ use anyhow::{Context, Result};
66use log:: { debug, error, info} ;
77use pem_rfc7468:: LineEnding ;
88use rand_core:: { impls, CryptoRng , Error as RngError , RngCore } ;
9- use std:: collections:: HashSet ;
109use std:: {
1110 fs,
1211 io:: { self , Write } ,
@@ -64,81 +63,11 @@ pub enum HsmError {
6463 NotEnoughShares ,
6564}
6665
67- pub struct Alphabet {
68- chars : Vec < char > ,
69- }
70-
71- impl Default for Alphabet {
72- fn default ( ) -> Self {
73- Self :: new ( )
74- }
75- }
76-
77- impl Alphabet {
78- pub fn new ( ) -> Self {
79- let mut chars: HashSet < char > = HashSet :: new ( ) ;
80- chars. extend ( 'a' ..='z' ) ;
81- chars. extend ( 'A' ..='Z' ) ;
82- chars. extend ( '0' ..='9' ) ;
83-
84- // Remove visually similar characters
85- chars = & chars - & HashSet :: from ( [ 'l' , 'I' , '1' ] ) ;
86- chars = & chars - & HashSet :: from ( [ 'B' , '8' ] ) ;
87- chars = & chars - & HashSet :: from ( [ 'O' , '0' ] ) ;
88-
89- // We generate random passwords from this alphabet by getting a byte
90- // of random data from the HSM and using this value to pick
91- // characters from the alphabet. Our alphabet cannot be larger than
92- // the u8::MAX or it will ignore characters after the u8::MAXth.
93- assert ! ( usize :: from( u8 :: MAX ) > chars. len( ) ) ;
94-
95- Alphabet {
96- chars : chars. into_iter ( ) . collect ( ) ,
97- }
98- }
99-
100- pub fn get_char ( & self , val : u8 ) -> Option < char > {
101- let len = self . chars . len ( ) as u8 ;
102- // let rand = ;
103- // Avoid biasing results by ensuring the random values we use
104- // are a multiple of the length of the alphabet. If they aren't
105- // we just get another.
106- if val < u8:: MAX - u8:: MAX % len {
107- Some ( self . chars [ ( val % len) as usize ] )
108- } else {
109- None
110- }
111- }
112-
113- pub fn get_random_string (
114- & self ,
115- get_rand_u8 : impl Fn ( ) -> Result < u8 > ,
116- length : usize ,
117- ) -> Result < String > {
118- let mut passwd = String :: with_capacity ( length + 1 ) ;
119-
120- for _ in 0 ..length {
121- let char = loop {
122- let rand = get_rand_u8 ( ) ?;
123-
124- if let Some ( char) = self . get_char ( rand) {
125- break char;
126- }
127- } ;
128-
129- passwd. push ( char) ;
130- }
131-
132- Ok ( passwd)
133- }
134- }
135-
13666/// Structure holding common data used by OKS when interacting with the HSM.
13767pub struct Hsm {
13868 pub client : Client ,
13969 pub out_dir : PathBuf ,
14070 pub state_dir : PathBuf ,
141- pub alphabet : Alphabet ,
14271 pub backup : bool ,
14372}
14473
@@ -179,18 +108,10 @@ impl Hsm {
179108 client,
180109 out_dir : out_dir. to_path_buf ( ) ,
181110 state_dir : state_dir. to_path_buf ( ) ,
182- alphabet : Alphabet :: new ( ) ,
183111 backup,
184112 } )
185113 }
186114
187- pub fn rand_string ( & self , length : usize ) -> Result < String > {
188- self . alphabet . get_random_string (
189- || Ok ( self . client . get_pseudo_random ( 1 ) ?[ 0 ] ) ,
190- length,
191- )
192- }
193-
194115 /// Create a new wrap key, cut it up into shares, & a Feldman verifier,
195116 /// then put the key into the YubiHSM. The shares and the verifier are then
196117 /// returned to the caller. Generally they will then be distributed
0 commit comments