Greenbone Vulnerability Management Libraries  22.8.0
passwordbasedauthentication.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  PBASettings
 

Macros

#define MAX_PEPPER_SIZE   4
 
#define COUNT_DEFAULT   20000
 
#define PREFIX_DEFAULT   "$6$"
 

Enumerations

enum  pba_rc { VALID, UPDATE_RECOMMENDED, INVALID, ERR }
 

Functions

struct PBASettingspba_init (const char *pepper, unsigned int pepper_size, unsigned int count, char *prefix)
 
char * pba_hash (struct PBASettings *setting, const char *password)
 
enum pba_rc pba_verify_hash (const struct PBASettings *settings, const char *hash, const char *password)
 
void pba_finalize (struct PBASettings *settings)
 

Macro Definition Documentation

◆ COUNT_DEFAULT

#define COUNT_DEFAULT   20000

Definition at line 12 of file passwordbasedauthentication.h.

◆ MAX_PEPPER_SIZE

#define MAX_PEPPER_SIZE   4

Definition at line 10 of file passwordbasedauthentication.h.

◆ PREFIX_DEFAULT

#define PREFIX_DEFAULT   "$6$"

Definition at line 14 of file passwordbasedauthentication.h.

Enumeration Type Documentation

◆ pba_rc

enum pba_rc
Enumerator
VALID 
UPDATE_RECOMMENDED 
INVALID 
ERR 

Definition at line 45 of file passwordbasedauthentication.h.

46 {
47  VALID, /* hash and password are correct */
48  UPDATE_RECOMMENDED, /* password is correct but in an outdated format*/
49  INVALID, /* password is incorrect */
50  ERR, /* unexpected error */
51 };

Function Documentation

◆ pba_finalize()

void pba_finalize ( struct PBASettings settings)

Definition at line 152 of file passwordbasedauthentication.c.

153 {
154  free (settings);
155 }

Referenced by Ensure().

Here is the caller graph for this function:

◆ pba_hash()

char* pba_hash ( struct PBASettings setting,
const char *  password 
)

pba_hash tries to create a hash based SETTING and PASSWORD. Returns a hash on success or a NULL pointer on failure

Definition at line 168 of file passwordbasedauthentication.c.

169 {
170  char *result = NULL, *settings = NULL, *tmp, *rslt;
171  int i;
172  struct crypt_data *data = NULL;
173 
174  if (!setting || !password)
175  goto exit;
176  if (!is_prefix_supported (setting->prefix))
177  goto exit;
178  settings = malloc (CRYPT_GENSALT_OUTPUT_SIZE);
179  if (crypt_gensalt_r (setting->prefix, setting->count, NULL, 0, settings,
181  == NULL)
182  goto exit;
183  tmp = settings + strlen (settings) - 1;
184  for (i = MAX_PEPPER_SIZE - 1; i > -1; i--)
185  {
186  if (setting->pepper[i] != 0)
187  tmp[0] = setting->pepper[i];
188  tmp--;
189  }
190 
191  data = calloc (1, sizeof (struct crypt_data));
192  rslt = crypt_r (password, settings, data);
193  if (rslt == NULL)
194  goto exit;
195  result = calloc (1, CRYPT_OUTPUT_SIZE);
196  memcpy (result, rslt, CRYPT_OUTPUT_SIZE);
197  // remove pepper, by jumping to begin of applied pepper within result
198  // and overriding it.
199  tmp = result + (tmp - settings);
200  for (i = 0; i < MAX_PEPPER_SIZE; i++)
201  {
202  tmp++;
203  if (setting->pepper[i] != 0)
204  tmp[0] = '0';
205  }
206 exit:
207  if (data != NULL)
208  free (data);
209  if (settings != NULL)
210  free (settings);
211  return result;
212 }

Referenced by Ensure().

Here is the caller graph for this function:

◆ pba_init()

struct PBASettings* pba_init ( const char *  pepper,
unsigned int  pepper_size,
unsigned int  count,
char *  prefix 
)

Intitializes PBASettings with given PEPPER, PREFIX, COUNT.

PEPPER_SIZE must be lower or equal MAX_PEPPER_SIZE when PEPPER is set, when PEPPER is a NULL pointer, no pepper will be used and PEPPER_SIZE is ignored.

COUNT is set to COUNT_DEFAULT when it is 0, PREFIX is set to PREFIX_DEFAULT when prefix is a nullpointer.

Returns a pointer to PBASettings on success or NULL on failure.

Definition at line 133 of file passwordbasedauthentication.c.

135 {
136  unsigned int i = 0;
137  struct PBASettings *result = NULL;
138  if (pepper_size > MAX_PEPPER_SIZE)
139  goto exit;
140  if (prefix != NULL && !is_prefix_supported (prefix))
141  goto exit;
142  result = malloc (sizeof (struct PBASettings));
143  for (i = 0; i < MAX_PEPPER_SIZE; i++)
144  result->pepper[i] = pepper != NULL && i < pepper_size ? pepper[i] : 0;
145  result->count = count == 0 ? COUNT_DEFAULT : count;
146  result->prefix = prefix == NULL ? PREFIX_DEFAULT : prefix;
147 exit:
148  return result;
149 }

References PBASettings::count, COUNT_DEFAULT, is_prefix_supported(), MAX_PEPPER_SIZE, PBASettings::pepper, PBASettings::prefix, and PREFIX_DEFAULT.

Referenced by Ensure().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ pba_verify_hash()

enum pba_rc pba_verify_hash ( const struct PBASettings settings,
const char *  hash,
const char *  password 
)

pba_verify_hash tries to create hash based on PASSWORD and settings found via HASH and compares that with HASH.

Returns VALID if HASH and PASSWORD are correct; UPDATE_RECOMMENDED when the HASH and PASSWORD are correct but based on a deprecated algorithm; IVALID if HASH does not match PASSWORD; ERR if an unexpected error occurs.

Definition at line 168 of file passwordbasedauthentication.c.

217 {
218  char *cmp, *tmp = NULL;
219  struct crypt_data *data = NULL;
220  int i = 0;
221  enum pba_rc result = ERR;
222 
223  char *invalid_hash = calloc (1, CRYPT_OUTPUT_SIZE);
224  memset (invalid_hash, 0, CRYPT_OUTPUT_SIZE);
225  memcpy (invalid_hash, INVALID_HASH, strlen (INVALID_HASH));
226 
227  if (!setting)
228  goto exit;
229  if (!is_prefix_supported (setting->prefix))
230  goto exit;
231  if (pba_is_phc_compliant (hash) != 0)
232  {
233  int hash_size;
234  hash_size = hash ? strlen (hash) : strlen (invalid_hash);
235 
236  data = calloc (1, sizeof (struct crypt_data));
237  // manipulate hash to reapply pepper
238  tmp = calloc (1, CRYPT_OUTPUT_SIZE);
239 
240  memset (tmp, 0, CRYPT_OUTPUT_SIZE);
241  memcpy (tmp, hash ? hash : invalid_hash,
242  (hash_size < CRYPT_OUTPUT_SIZE) ? hash_size
243  : CRYPT_OUTPUT_SIZE - 1);
244  cmp = strrchr (tmp, '$');
245  for (i = MAX_PEPPER_SIZE - 1; i > -1; i--)
246  {
247  cmp--;
248  if (setting->pepper[i] != 0)
249  cmp[0] = setting->pepper[i];
250  }
251  // some crypt_r implementations cannot handle if password is a
252  // NULL pointer and run into SEGMENTATION faults.
253  // Therefore we set it to ""
254  cmp = crypt_r (password ? password : "", tmp, data);
255  if (strcmp (tmp, cmp) == 0)
256  result = VALID;
257  else
258  result = INVALID;
259  }
260  else
261  {
262  // assume authutils hash handling
263  // initialize gvm_auth utils if not already initialized
264  if (initialized == FALSE && gvm_auth_init () != 0)
265  {
266  goto exit;
267  }
268  // verify result of gvm_authenticate_classic
269  i = gvm_authenticate_classic (NULL, password, hash);
270  if (i == 0)
271  result = UPDATE_RECOMMENDED;
272  else if (i == 1)
273  result = INVALID;
274  }
275 exit:
276  free (invalid_hash);
277  if (data != NULL)
278  free (data);
279  if (tmp != NULL)
280  free (tmp);
281  return result;
282 }

References PBASettings::count, CRYPT_GENSALT_OUTPUT_SIZE, crypt_gensalt_r(), CRYPT_OUTPUT_SIZE, is_prefix_supported(), MAX_PEPPER_SIZE, PBASettings::pepper, and PBASettings::prefix.

Referenced by Ensure().

Here is the call graph for this function:
Here is the caller graph for this function:
PBASettings::pepper
char pepper[MAX_PEPPER_SIZE]
Definition: passwordbasedauthentication.h:24
ERR
@ ERR
Definition: passwordbasedauthentication.h:50
is_prefix_supported
static int is_prefix_supported(const char *id)
Definition: passwordbasedauthentication.c:32
crypt_gensalt_r
char * crypt_gensalt_r(const char *prefix, unsigned long count, const char *rbytes, int nrbytes, char *output, int output_size)
Definition: passwordbasedauthentication.c:85
PREFIX_DEFAULT
#define PREFIX_DEFAULT
Definition: passwordbasedauthentication.h:14
pba_is_phc_compliant
static int pba_is_phc_compliant(const char *setting)
Definition: passwordbasedauthentication.c:158
MAX_PEPPER_SIZE
#define MAX_PEPPER_SIZE
Definition: passwordbasedauthentication.h:10
UPDATE_RECOMMENDED
@ UPDATE_RECOMMENDED
Definition: passwordbasedauthentication.h:48
gvm_auth_init
int gvm_auth_init(void)
Initializes Gcrypt.
Definition: authutils.c:89
gvm_authenticate_classic
int gvm_authenticate_classic(const gchar *username, const gchar *password, const gchar *hash_arg)
Authenticate a credential pair against user file contents.
Definition: authutils.c:253
VALID
@ VALID
Definition: passwordbasedauthentication.h:47
CRYPT_OUTPUT_SIZE
#define CRYPT_OUTPUT_SIZE
Definition: passwordbasedauthentication.c:28
PBASettings
Definition: passwordbasedauthentication.h:23
PBASettings::count
unsigned int count
Definition: passwordbasedauthentication.h:25
pba_rc
pba_rc
Definition: passwordbasedauthentication.h:46
COUNT_DEFAULT
#define COUNT_DEFAULT
Definition: passwordbasedauthentication.h:12
CRYPT_GENSALT_OUTPUT_SIZE
#define CRYPT_GENSALT_OUTPUT_SIZE
Definition: passwordbasedauthentication.c:24
initialized
static gboolean initialized
Flag whether the config file was read.
Definition: authutils.c:33
PBASettings::prefix
char * prefix
Definition: passwordbasedauthentication.h:26
INVALID
@ INVALID
Definition: passwordbasedauthentication.h:49
INVALID_HASH
#define INVALID_HASH
Definition: passwordbasedauthentication.c:22