11//! SASL-based authentication support.
22
33use base64;
4- use generic_array:: GenericArray ;
54use generic_array:: typenum:: U32 ;
5+ use generic_array:: GenericArray ;
66use hmac:: { Hmac , Mac } ;
7- use sha2:: { Sha256 , Digest } ;
7+ use rand:: { OsRng , Rng } ;
8+ use sha2:: { Digest , Sha256 } ;
89use std:: fmt:: Write ;
910use std:: io;
1011use std:: iter;
1112use std:: mem;
1213use std:: str;
13- use rand:: { OsRng , Rng } ;
1414use stringprep;
1515
1616const NONCE_LENGTH : usize = 24 ;
@@ -34,16 +34,15 @@ fn normalize(pass: &[u8]) -> Vec<u8> {
3434}
3535
3636fn hi ( str : & [ u8 ] , salt : & [ u8 ] , i : u32 ) -> GenericArray < u8 , U32 > {
37- let mut hmac = Hmac :: < Sha256 > :: new ( str)
38- . expect ( "HMAC is able to accept all key sizes" ) ;
37+ let mut hmac = Hmac :: < Sha256 > :: new_varkey ( str) . expect ( "HMAC is able to accept all key sizes" ) ;
3938 hmac. input ( salt) ;
4039 hmac. input ( & [ 0 , 0 , 0 , 1 ] ) ;
4140 let mut prev = hmac. result ( ) . code ( ) ;
4241
4342 let mut hi = GenericArray :: < u8 , U32 > :: clone_from_slice ( & prev) ;
4443
4544 for _ in 1 ..i {
46- let mut hmac = Hmac :: < Sha256 > :: new ( str) . expect ( "already checked above" ) ;
45+ let mut hmac = Hmac :: < Sha256 > :: new_varkey ( str) . expect ( "already checked above" ) ;
4746 hmac. input ( prev. as_slice ( ) ) ;
4847 prev = hmac. result ( ) . code ( ) ;
4948
@@ -56,7 +55,10 @@ fn hi(str: &[u8], salt: &[u8], i: u32) -> GenericArray<u8, U32> {
5655}
5756
5857enum State {
59- Update { nonce : String , password : Vec < u8 > } ,
58+ Update {
59+ nonce : String ,
60+ password : Vec < u8 > ,
61+ } ,
6062 Finish {
6163 salted_password : GenericArray < u8 , U32 > ,
6264 auth_message : String ,
@@ -134,9 +136,8 @@ impl ScramSha256 {
134136 _ => return Err ( io:: Error :: new ( io:: ErrorKind :: Other , "invalid SCRAM state" ) ) ,
135137 } ;
136138
137- let message = str:: from_utf8 ( message) . map_err ( |e| {
138- io:: Error :: new ( io:: ErrorKind :: InvalidInput , e)
139- } ) ?;
139+ let message =
140+ str:: from_utf8 ( message) . map_err ( |e| io:: Error :: new ( io:: ErrorKind :: InvalidInput , e) ) ?;
140141
141142 let parsed = Parser :: new ( message) . server_first_message ( ) ?;
142143
@@ -151,7 +152,7 @@ impl ScramSha256 {
151152
152153 let salted_password = hi ( & password, & salt, parsed. iteration_count ) ;
153154
154- let mut hmac = Hmac :: < Sha256 > :: new ( & salted_password)
155+ let mut hmac = Hmac :: < Sha256 > :: new_varkey ( & salted_password)
155156 . expect ( "HMAC is able to accept all key sizes" ) ;
156157 hmac. input ( b"Client Key" ) ;
157158 let client_key = hmac. result ( ) . code ( ) ;
@@ -165,8 +166,8 @@ impl ScramSha256 {
165166
166167 let auth_message = format ! ( "n=,r={},{},{}" , client_nonce, message, self . message) ;
167168
168- let mut hmac = Hmac :: < Sha256 > :: new ( & stored_key )
169- . expect ( "HMAC is able to accept all key sizes" ) ;
169+ let mut hmac =
170+ Hmac :: < Sha256 > :: new_varkey ( & stored_key ) . expect ( "HMAC is able to accept all key sizes" ) ;
170171 hmac. input ( auth_message. as_bytes ( ) ) ;
171172 let client_signature = hmac. result ( ) ;
172173
@@ -197,9 +198,8 @@ impl ScramSha256 {
197198 _ => return Err ( io:: Error :: new ( io:: ErrorKind :: Other , "invalid SCRAM state" ) ) ,
198199 } ;
199200
200- let message = str:: from_utf8 ( message) . map_err ( |e| {
201- io:: Error :: new ( io:: ErrorKind :: InvalidInput , e)
202- } ) ?;
201+ let message =
202+ str:: from_utf8 ( message) . map_err ( |e| io:: Error :: new ( io:: ErrorKind :: InvalidInput , e) ) ?;
203203
204204 let parsed = Parser :: new ( message) . server_final_message ( ) ?;
205205
@@ -218,18 +218,16 @@ impl ScramSha256 {
218218 Err ( e) => return Err ( io:: Error :: new ( io:: ErrorKind :: InvalidInput , e) ) ,
219219 } ;
220220
221- let mut hmac = Hmac :: < Sha256 > :: new ( & salted_password)
221+ let mut hmac = Hmac :: < Sha256 > :: new_varkey ( & salted_password)
222222 . expect ( "HMAC is able to accept all key sizes" ) ;
223223 hmac. input ( b"Server Key" ) ;
224224 let server_key = hmac. result ( ) ;
225225
226- let mut hmac = Hmac :: < Sha256 > :: new ( & server_key. code ( ) )
226+ let mut hmac = Hmac :: < Sha256 > :: new_varkey ( & server_key. code ( ) )
227227 . expect ( "HMAC is able to accept all key sizes" ) ;
228228 hmac. input ( auth_message. as_bytes ( ) ) ;
229- hmac. verify ( & verifier) . map_err ( |_| io:: Error :: new (
230- io:: ErrorKind :: InvalidInput ,
231- "SCRAM verification error" ,
232- ) )
229+ hmac. verify ( & verifier)
230+ . map_err ( |_| io:: Error :: new ( io:: ErrorKind :: InvalidInput , "SCRAM verification error" ) )
233231 }
234232}
235233
@@ -252,9 +250,7 @@ impl<'a> Parser<'a> {
252250 Some ( ( i, c) ) => {
253251 let m = format ! (
254252 "unexpected character at byte {}: expected `{}` but got `{}" ,
255- i,
256- target,
257- c
253+ i, target, c
258254 ) ;
259255 Err ( io:: Error :: new ( io:: ErrorKind :: InvalidInput , m) )
260256 }
@@ -316,9 +312,8 @@ impl<'a> Parser<'a> {
316312 '0' ...'9' => true ,
317313 _ => false ,
318314 } ) ?;
319- n. parse ( ) . map_err (
320- |e| io:: Error :: new ( io:: ErrorKind :: InvalidInput , e) ,
321- )
315+ n. parse ( )
316+ . map_err ( |e| io:: Error :: new ( io:: ErrorKind :: InvalidInput , e) )
322317 }
323318
324319 fn iteration_count ( & mut self ) -> io:: Result < u32 > {
@@ -329,12 +324,10 @@ impl<'a> Parser<'a> {
329324
330325 fn eof ( & mut self ) -> io:: Result < ( ) > {
331326 match self . it . peek ( ) {
332- Some ( & ( i, _) ) => {
333- Err ( io:: Error :: new (
334- io:: ErrorKind :: InvalidInput ,
335- format ! ( "unexpected trailing data at byte {}" , i) ,
336- ) )
337- }
327+ Some ( & ( i, _) ) => Err ( io:: Error :: new (
328+ io:: ErrorKind :: InvalidInput ,
329+ format ! ( "unexpected trailing data at byte {}" , i) ,
330+ ) ) ,
338331 None => Ok ( ( ) ) ,
339332 }
340333 }
@@ -419,10 +412,12 @@ mod test {
419412 let nonce = "9IZ2O01zb9IgiIZ1WJ/zgpJB" ;
420413
421414 let client_first = "n,,n=,r=9IZ2O01zb9IgiIZ1WJ/zgpJB" ;
422- let server_first = "r=9IZ2O01zb9IgiIZ1WJ/zgpJBjx/oIRLs02gGSHcw1KEty3eY,s=fs3IXBy7U7+IvVjZ,i\
423- =4096";
424- let client_final = "c=biws,r=9IZ2O01zb9IgiIZ1WJ/zgpJBjx/oIRLs02gGSHcw1KEty3eY,p=AmNKosjJzS3\
425- 1NTlQYNs5BTeQjdHdk7lOflDo5re2an8=";
415+ let server_first =
416+ "r=9IZ2O01zb9IgiIZ1WJ/zgpJBjx/oIRLs02gGSHcw1KEty3eY,s=fs3IXBy7U7+IvVjZ,i\
417+ =4096";
418+ let client_final =
419+ "c=biws,r=9IZ2O01zb9IgiIZ1WJ/zgpJBjx/oIRLs02gGSHcw1KEty3eY,p=AmNKosjJzS3\
420+ 1NTlQYNs5BTeQjdHdk7lOflDo5re2an8=";
426421 let server_final = "v=U+ppxD5XUKtradnv8e2MkeupiA8FU87Sg8CXzXHDAzw=" ;
427422
428423 let mut scram = ScramSha256 :: new_inner ( password. as_bytes ( ) , nonce. to_string ( ) ) . unwrap ( ) ;
0 commit comments