39 if( p ==
nullptr )
throw std::runtime_error(
"no tsl library" ) ;
42 struct DigesterAdaptor
44 explicit DigesterAdaptor(
const std::string & name ) :
46 m_digester(lib().digester(name)) ,
50 std::string operator()(
const std::string & data_1 ,
const std::string & data_2 )
57 m_digester.add( data_1 ) ;
58 m_digester.add( data_2 ) ;
59 return m_digester.value() ;
61 size_t blocksize()
const
63 return m_digester.blocksize() ;
69 struct PostDigesterAdaptor
71 explicit PostDigesterAdaptor(
const std::string & name ) :
76 throw GAuth::Cram::NoState( m_name ) ;
78 std::string operator()(
const std::string & state_pair ,
const std::string & data )
80 GSsl::Digester xi = lib().digester( m_name , state_pair.substr(0U,state_pair.size()/2U) ) ;
82 GSsl::Digester xo = lib().digester( m_name , state_pair.substr(state_pair.size()/2U) ) ;
91 const Secret & secret ,
const std::string & challenge ,
92 const std::string & id_prefix )
96 G_DEBUG(
"GAuth::Cram::response: [" << hash_type <<
"][" << as_hmac <<
"]"
99 "[" << responseImp(hash_type,as_hmac,secret,challenge) <<
"]" ) ;
101 return id_prefix +
" " + responseImp(hash_type,as_hmac,secret,challenge) ;
103 catch( std::exception & e )
105 G_DEBUG(
"GAuth::Cram::response: exception: " << e.what() ) ; G_IGNORE_VARIABLE( e ) ;
106 return std::string() ;
111 const Secret & secret ,
const std::string & challenge ,
112 const std::string & response_in )
116 G_DEBUG(
"GAuth::Cram::validate: [" << hash_type <<
"][" << as_hmac <<
"]"
118 "[" << responseImp(hash_type,as_hmac,secret,challenge) <<
"]" ) ;
120 std::string expectation =
G::Str::tail( response_in ,
" " ) ;
121 return !expectation.empty() && responseImp(hash_type,as_hmac,secret,challenge) == expectation ;
123 catch( std::exception & e )
125 G_DEBUG(
"GAuth::Cram::validate: exception: " << e.what() ) ; G_IGNORE_VARIABLE( e ) ;
135 std::string GAuth::Cram::responseImp(
const std::string & mechanism_hash_type ,
bool as_hmac ,
136 const Secret & secret ,
const std::string & challenge )
138 G_DEBUG(
"GAuth::Cram::responseImp: mechanism-hash=[" << mechanism_hash_type <<
"] secret-hash=[" << secret.
maskType() <<
"] as-hmac=" << as_hmac ) ;
142 throw BadType( secret.
maskType() ) ;
144 if( mechanism_hash_type ==
"MD5" )
150 DigesterAdaptor digest( mechanism_hash_type ) ;
154 else if( secret.
masked() )
157 throw Mismatch( secret.
maskType() , mechanism_hash_type ) ;
159 if( mechanism_hash_type ==
"MD5" )
165 PostDigesterAdaptor postdigest( mechanism_hash_type ) ;
171 if( mechanism_hash_type ==
"MD5" )
177 DigesterAdaptor digest( mechanism_hash_type ) ;
191 result.push_back(
"FAKE" ) ;
194 G_DEBUG(
"GAuth::Cram::hashTypes: tls library hash types: [" <<
G::Str::join(
",",result) <<
"] (" << (require_state?1:0) <<
")" ) ;
197 if( std::find(result.begin(),result.end(),
"MD5") == result.end() )
198 result.push_back(
"MD5" ) ;
200 if( !prefix.empty() )
202 for( G::StringArray::iterator p = result.begin() ; p != result.end() ; ++p )
210 std::ostringstream ss ;
static Library * instance()
Returns a pointer to a library object, if any.
An interface to an underlying TLS library.
static std::string printable(const std::string &in, char escape= '\\')
Returns a printable represention of the given input string.
static std::string canonicalName()
Returns the canonical network name assiciated with hostname().
static std::string tail(const std::string &in, size_type pos, const std::string &default_=std::string())
Returns the last part of the string after the given position.
static std::string hmac(Fn2 digest, size_t blocksize, const std::string &key, const std::string &input)
Computes a Hashed Message Authentication Code using given hash function.
static G::StringArray digesters(bool require_state=false)
Returns a list of hash function names (such as "MD5") that the TLS library can do, ordered roughly from strongest to weakest.
static std::string head(const std::string &in, size_type pos, const std::string &default_=std::string())
Returns the first part of the string up to just before the given position.
static size_t blocksize()
Returns the block size in bytes (64).
static bool imatch(const std::string &, const std::string &)
Returns true if the two strings are the same, ignoring Latin-1 case.
std::vector< std::string > StringArray
A std::vector of std::strings.
std::string key() const
Returns the key. Throws if not valid().
static bool validate(const std::string &hash_type, bool hmac, const Secret &secret, const std::string &challenge, const std::string &response)
Validates the response with respect to the original challenge.
static EpochTime now()
Returns the current epoch time.
static std::string challenge()
Returns a challenge string that incorporates a random number and the current time.
An overload discriminator for G::Hash::hmac()
static std::string id(const std::string &response)
Returns the leading id part of the response.
Returns an object that can perform a cryptographic hash.
static bool enabled()
Returns true if test features are enabled.
std::string value()
Returns the hash value.
static std::string printable(const std::string &input)
Converts a binary string into a printable form, using a lowercase hexadecimal encoding.
static std::string response(const std::string &hash_type, bool hmac, const Secret &secret, const std::string &challenge, const std::string &response_prefix)
Constructs a response to a challenge comprising the response-prefix, space, and digest-or-hmac of key...
static std::string postdigest(const std::string &state_pair, const std::string &message)
A convenience function that returns the value() from an outer digest that is initialised with the sec...
void add(const std::string &)
Adds data of arbitrary size.
static std::string digest(const std::string &input)
A convenience function that returns a digest from one input.
static std::string ids()
Returns a concatenation of all available TLS library names and versions.
A singleton class for initialising the underlying TLS library.
static std::string join(const std::string &sep, const StringArray &strings)
Concatenates an array of strings with separators.
bool masked() const
Returns true if key() is masked.
size_t statesize() const
Returns the size of the state() string in bytes, or zero if state() is not implemented.
std::string maskType() const
Returns the masking function name, such as "MD5", or the empty string if not masked().
static std::string digest2(const std::string &input_1, const std::string &input_2)
A non-overloaded name for the digest() overload taking two parameters.
Encapsulates a shared secret from the secrets file plus the associated userid.
static G::StringArray hashTypes(const std::string &prefix=std::string(), bool require_state=false)
Returns a list of supported hash types, such as "MD5" and "SHA1", ordered with the strongest first...