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 594 of file auth-scram.c.

597 {
598  char *v;
599  char *p;
600  char *scheme_str;
601  char *salt_str;
602  char *iterations_str;
603  char *storedkey_str;
604  char *serverkey_str;
605  int decoded_len;
606  char *decoded_salt_buf;
607  char *decoded_stored_buf;
608  char *decoded_server_buf;
609 
610  /*
611  * The secret is of form:
612  *
613  * SCRAM-SHA-256$<iterations>:<salt>$<storedkey>:<serverkey>
614  */
615  v = pstrdup(secret);
616  if ((scheme_str = strtok(v, "$")) == NULL)
617  goto invalid_secret;
618  if ((iterations_str = strtok(NULL, ":")) == NULL)
619  goto invalid_secret;
620  if ((salt_str = strtok(NULL, "$")) == NULL)
621  goto invalid_secret;
622  if ((storedkey_str = strtok(NULL, ":")) == NULL)
623  goto invalid_secret;
624  if ((serverkey_str = strtok(NULL, "")) == NULL)
625  goto invalid_secret;
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:1644
void * palloc(Size size)
Definition: mcxt.c:1226
#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(), and SCRAM_SHA_256_KEY_LEN.

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 477 of file auth-scram.c.

478 {
479  char *prep_password;
480  pg_saslprep_rc rc;
481  char saltbuf[SCRAM_DEFAULT_SALT_LEN];
482  char *result;
483  const char *errstr = NULL;
484 
485  /*
486  * Normalize the password with SASLprep. If that doesn't work, because
487  * the password isn't valid UTF-8 or contains prohibited characters, just
488  * proceed with the original password. (See comments at top of file.)
489  */
490  rc = pg_saslprep(password, &prep_password);
491  if (rc == SASLPREP_SUCCESS)
492  password = (const char *) prep_password;
493 
494  /* Generate random salt */
496  ereport(ERROR,
497  (errcode(ERRCODE_INTERNAL_ERROR),
498  errmsg("could not generate random salt")));
499 
501  saltbuf, SCRAM_DEFAULT_SALT_LEN,
503  &errstr);
504 
505  if (prep_password)
506  pfree(prep_password);
507 
508  return result;
509 }
int scram_sha_256_iterations
Definition: auth-scram.c:197
int errcode(int sqlerrcode)
Definition: elog.c:858
int errmsg(const char *fmt,...)
Definition: elog.c:1069
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
void pfree(void *pointer)
Definition: mcxt.c:1456
bool pg_strong_random(void *buf, size_t len)
pg_saslprep_rc pg_saslprep(const char *input, char **output)
Definition: saslprep.c:1044
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:53

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 517 of file auth-scram.c.

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

Referenced by CheckPWChallengeAuth().

◆ scram_sha_256_iterations

PGDLLIMPORT int scram_sha_256_iterations
extern

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

Referenced by pg_be_scram_build_secret().