// The SCRAM-SHA-256 exchange against RFC 7677's recorded one (password // "pencil"), plus the failure modes. import scram import std/bytes import std/test.{assert, assert_equal} @test fn the_rfcs_exchange :: { // RFC 7677's exchange (password "pencil"), with PostgreSQL's // convention of an empty `n=` (the user goes in the startup message, // as libpq does): the expected values below are the RFC's exchange // adjusted for that, computed independently with Python's hashlib. // (With `n=user` instead, the proof is the RFC's own // `p=dHzbZapWIk4jUhN+Ute9ytag9zjfMHgsqmmiz7AndVQ=`.) let (state, first) = scram.client_first "rOprNGfwEbeRWgbNEkqO" assert_equal (debug_bytes first) "n,,n=,r=rOprNGfwEbeRWgbNEkqO" match scram.client_final state "pencil" "r=rOprNGfwEbeRWgbNEkqO%hvYDpWUa2RaTCAfuxFIlj)hNlF$k0,s=W22ZaJ0SNY7soEsUEjb6gQ==,i=4096" { Ok((state, reply)) -> { assert_equal reply "c=biws,r=rOprNGfwEbeRWgbNEkqO%hvYDpWUa2RaTCAfuxFIlj)hNlF$k0,p=qvT2SWdEH5Q06albL+hjSYuUhCG7VndFyzIb7CK4n9k=" assert_equal ( scram.verify_server state "v=3HO6Qt1M4MKJrmlKaoOqLAI0/0TV0HZe7J9H3MBtSOg=" ) Ok(()) } , Error(e) -> panic e } } @test fn a_server_nonce_that_does_not_extend_ours_is_rejected :: { let (state, _) = scram.client_first "clientnonce" match scram.client_final state "pencil" "r=other,s=W22ZaJ0SNY7soEsUEjb6gQ==,i=4096" { Error(_) -> (), Ok(_) -> panic "a nonce not extending ours must fail" } } @test fn a_wrong_server_signature_is_rejected :: { let (state, _) = scram.client_first "rOprNGfwEbeRWgbNEkqO" match scram.client_final state "pencil" "r=rOprNGfwEbeRWgbNEkqO%hvYDpWUa2RaTCAfuxFIlj)hNlF$k0,s=W22ZaJ0SNY7soEsUEjb6gQ==,i=4096" { Ok ( (state, _) ) -> match scram.verify_server state "v=AAAATRBi23WpRR/wtup+mMhUZUn/dB5nLTJRsjl95u4=" { Error(_) -> (), Ok(()) -> panic "a wrong server signature must fail" } , Error(e) -> panic e } } fn debug_bytes :: b: Bytes -> String { match bytes.to_string b { Ok(s) -> s, Error(e) -> panic e } }