PostgreSQL Source Code  git master
scram.h File Reference
#include "common/cryptohash.h"
#include "lib/stringinfo.h"
#include "libpq/libpq-be.h"
#include "libpq/sasl.h"
Include dependency graph for scram.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

char * pg_be_scram_build_secret (const char *password)
 
bool parse_scram_secret (const char *secret, int *iterations, pg_cryptohash_type *hash_type, int *key_length, char **salt, uint8 *stored_key, uint8 *server_key)
 
bool scram_verify_plain_password (const char *username, const char *password, const char *secret)
 

Variables

PGDLLIMPORT int scram_sha_256_iterations
 
PGDLLIMPORT const pg_be_sasl_mech pg_be_scram_mech
 

Function Documentation

◆ parse_scram_secret()

bool parse_scram_secret ( const char *  secret,
int *  iterations,
pg_cryptohash_type hash_type,
int *  key_length,
char **  salt,
uint8 stored_key,
uint8 server_key 
)

Definition at line 591 of file auth-scram.c.

594 {
595  char *v;
596  char *p;
597  char *scheme_str;
598  char *salt_str;
599  char *iterations_str;
600  char *storedkey_str;
601  char *serverkey_str;
602  int decoded_len;
603  char *decoded_salt_buf;
604  char *decoded_stored_buf;
605  char *decoded_server_buf;
606 
607  /*
608  * The secret is of form:
609  *
610  * SCRAM-SHA-256$<iterations>:<salt>$<storedkey>:<serverkey>
611  */
612  v = pstrdup(secret);
613  scheme_str = strsep(&v, "$");
614  if (v == NULL)
615  goto invalid_secret;
616  iterations_str = strsep(&v, ":");
617  if (v == NULL)
618  goto invalid_secret;
619  salt_str = strsep(&v, "$");
620  if (v == NULL)
621  goto invalid_secret;
622  storedkey_str = strsep(&v, ":");
623  if (v == NULL)
624  goto invalid_secret;
625  serverkey_str = v;
626 
627  /* Parse the fields */
628  if (strcmp(scheme_str, "SCRAM-SHA-256") != 0)
629  goto invalid_secret;
630  *hash_type = PG_SHA256;
631  *key_length = SCRAM_SHA_256_KEY_LEN;
632 
633  errno = 0;
634  *iterations = strtol(iterations_str, &p, 10);
635  if (*p || errno != 0)
636  goto invalid_secret;
637 
638  /*
639  * Verify that the salt is in Base64-encoded format, by decoding it,
640  * although we return the encoded version to the caller.
641  */
642  decoded_len = pg_b64_dec_len(strlen(salt_str));
643  decoded_salt_buf = palloc(decoded_len);
644  decoded_len = pg_b64_decode(salt_str, strlen(salt_str),
645  decoded_salt_buf, decoded_len);
646  if (decoded_len < 0)
647  goto invalid_secret;
648  *salt = pstrdup(salt_str);
649 
650  /*
651  * Decode StoredKey and ServerKey.
652  */
653  decoded_len = pg_b64_dec_len(strlen(storedkey_str));
654  decoded_stored_buf = palloc(decoded_len);
655  decoded_len = pg_b64_decode(storedkey_str, strlen(storedkey_str),
656  decoded_stored_buf, decoded_len);
657  if (decoded_len != *key_length)
658  goto invalid_secret;
659  memcpy(stored_key, decoded_stored_buf, *key_length);
660 
661  decoded_len = pg_b64_dec_len(strlen(serverkey_str));
662  decoded_server_buf = palloc(decoded_len);
663  decoded_len = pg_b64_decode(serverkey_str, strlen(serverkey_str),
664  decoded_server_buf, decoded_len);
665  if (decoded_len != *key_length)
666  goto invalid_secret;
667  memcpy(server_key, decoded_server_buf, *key_length);
668 
669  return true;
670 
671 invalid_secret:
672  *salt = NULL;
673  return false;
674 }
int pg_b64_decode(const char *src, int len, char *dst, int dstlen)
Definition: base64.c:116
int pg_b64_dec_len(int srclen)
Definition: base64.c:239
@ PG_SHA256
Definition: cryptohash.h:24
char * pstrdup(const char *in)
Definition: mcxt.c:1696
void * palloc(Size size)
Definition: mcxt.c:1317
char * strsep(char **stringp, const char *delim)
Definition: strsep.c:49
#define SCRAM_SHA_256_KEY_LEN
Definition: scram-common.h:24
int iterations
Definition: thread-thread.c:39

References iterations, palloc(), pg_b64_dec_len(), pg_b64_decode(), PG_SHA256, pstrdup(), SCRAM_SHA_256_KEY_LEN, and strsep().

Referenced by get_password_type(), scram_init(), and scram_verify_plain_password().

◆ pg_be_scram_build_secret()

char* pg_be_scram_build_secret ( const char *  password)

Definition at line 474 of file auth-scram.c.

475 {
476  char *prep_password;
477  pg_saslprep_rc rc;
478  char saltbuf[SCRAM_DEFAULT_SALT_LEN];
479  char *result;
480  const char *errstr = NULL;
481 
482  /*
483  * Normalize the password with SASLprep. If that doesn't work, because
484  * the password isn't valid UTF-8 or contains prohibited characters, just
485  * proceed with the original password. (See comments at top of file.)
486  */
487  rc = pg_saslprep(password, &prep_password);
488  if (rc == SASLPREP_SUCCESS)
489  password = (const char *) prep_password;
490 
491  /* Generate random salt */
493  ereport(ERROR,
494  (errcode(ERRCODE_INTERNAL_ERROR),
495  errmsg("could not generate random salt")));
496 
498  saltbuf, SCRAM_DEFAULT_SALT_LEN,
500  &errstr);
501 
502  if (prep_password)
503  pfree(prep_password);
504 
505  return result;
506 }
int scram_sha_256_iterations
Definition: auth-scram.c:194
int errcode(int sqlerrcode)
Definition: elog.c:853
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
void pfree(void *pointer)
Definition: mcxt.c:1521
bool pg_strong_random(void *buf, size_t len)
pg_saslprep_rc pg_saslprep(const char *input, char **output)
Definition: saslprep.c:1046
pg_saslprep_rc
Definition: saslprep.h:21
@ SASLPREP_SUCCESS
Definition: saslprep.h:22
char * scram_build_secret(pg_cryptohash_type hash_type, int key_length, const char *salt, int saltlen, int iterations, const char *password, const char **errstr)
Definition: scram-common.c:210
#define SCRAM_DEFAULT_SALT_LEN
Definition: scram-common.h:44
static char * password
Definition: streamutil.c:54

References ereport, errcode(), errmsg(), ERROR, password, pfree(), pg_saslprep(), PG_SHA256, pg_strong_random(), SASLPREP_SUCCESS, scram_build_secret(), SCRAM_DEFAULT_SALT_LEN, scram_sha_256_iterations, and SCRAM_SHA_256_KEY_LEN.

Referenced by encrypt_password().

◆ scram_verify_plain_password()

bool scram_verify_plain_password ( const char *  username,
const char *  password,
const char *  secret 
)

Definition at line 514 of file auth-scram.c.

516 {
517  char *encoded_salt;
518  char *salt;
519  int saltlen;
520  int iterations;
521  int key_length = 0;
522  pg_cryptohash_type hash_type;
523  uint8 salted_password[SCRAM_MAX_KEY_LEN];
524  uint8 stored_key[SCRAM_MAX_KEY_LEN];
525  uint8 server_key[SCRAM_MAX_KEY_LEN];
526  uint8 computed_key[SCRAM_MAX_KEY_LEN];
527  char *prep_password;
528  pg_saslprep_rc rc;
529  const char *errstr = NULL;
530 
531  if (!parse_scram_secret(secret, &iterations, &hash_type, &key_length,
532  &encoded_salt, stored_key, server_key))
533  {
534  /*
535  * The password looked like a SCRAM secret, but could not be parsed.
536  */
537  ereport(LOG,
538  (errmsg("invalid SCRAM secret for user \"%s\"", username)));
539  return false;
540  }
541 
542  saltlen = pg_b64_dec_len(strlen(encoded_salt));
543  salt = palloc(saltlen);
544  saltlen = pg_b64_decode(encoded_salt, strlen(encoded_salt), salt,
545  saltlen);
546  if (saltlen < 0)
547  {
548  ereport(LOG,
549  (errmsg("invalid SCRAM secret for user \"%s\"", username)));
550  return false;
551  }
552 
553  /* Normalize the password */
554  rc = pg_saslprep(password, &prep_password);
555  if (rc == SASLPREP_SUCCESS)
556  password = prep_password;
557 
558  /* Compute Server Key based on the user-supplied plaintext password */
559  if (scram_SaltedPassword(password, hash_type, key_length,
560  salt, saltlen, iterations,
561  salted_password, &errstr) < 0 ||
562  scram_ServerKey(salted_password, hash_type, key_length,
563  computed_key, &errstr) < 0)
564  {
565  elog(ERROR, "could not compute server key: %s", errstr);
566  }
567 
568  if (prep_password)
569  pfree(prep_password);
570 
571  /*
572  * Compare the secret's Server Key with the one computed from the
573  * user-supplied password.
574  */
575  return memcmp(computed_key, server_key, key_length) == 0;
576 }
bool parse_scram_secret(const char *secret, int *iterations, pg_cryptohash_type *hash_type, int *key_length, char **salt, uint8 *stored_key, uint8 *server_key)
Definition: auth-scram.c:591
unsigned char uint8
Definition: c.h:504
pg_cryptohash_type
Definition: cryptohash.h:20
#define LOG
Definition: elog.h:31
#define elog(elevel,...)
Definition: elog.h:225
static char * username
Definition: initdb.c:153
int scram_ServerKey(const uint8 *salted_password, pg_cryptohash_type hash_type, int key_length, uint8 *result, const char **errstr)
Definition: scram-common.c:172
int scram_SaltedPassword(const char *password, pg_cryptohash_type hash_type, int key_length, const char *salt, int saltlen, int iterations, uint8 *result, const char **errstr)
Definition: scram-common.c:38
#define SCRAM_MAX_KEY_LEN
Definition: scram-common.h:30

References elog, ereport, errmsg(), ERROR, iterations, LOG, palloc(), parse_scram_secret(), password, pfree(), pg_b64_dec_len(), pg_b64_decode(), pg_saslprep(), SASLPREP_SUCCESS, SCRAM_MAX_KEY_LEN, scram_SaltedPassword(), scram_ServerKey(), and username.

Referenced by plain_crypt_verify().

Variable Documentation

◆ pg_be_scram_mech

PGDLLIMPORT const pg_be_sasl_mech pg_be_scram_mech
extern

Definition at line 113 of file auth-scram.c.

Referenced by CheckPWChallengeAuth().

◆ scram_sha_256_iterations

PGDLLIMPORT int scram_sha_256_iterations
extern

Definition at line 194 of file auth-scram.c.

Referenced by pg_be_scram_build_secret().