mirror of
https://github.com/postgres/postgres.git
synced 2025-07-02 09:02:37 +03:00
Make SCRAM iteration count configurable
Replace the hardcoded value with a GUC such that the iteration count can be raised in order to increase protection against brute-force attacks. The hardcoded value for SCRAM iteration count was defined to be 4096, which is taken from RFC 7677, so set the default for the GUC to 4096 to match. In RFC 7677 the recommendation is at least 15000 iterations but 4096 is listed as a SHOULD requirement given that it's estimated to yield a 0.5s processing time on a mobile handset of the time of RFC writing (late 2015). Raising the iteration count of SCRAM will make stored passwords more resilient to brute-force attacks at a higher computational cost during connection establishment. Lowering the count will reduce computational overhead during connections at the tradeoff of reducing strength against brute-force attacks. There are however platforms where even a modest iteration count yields a too high computational overhead, with weaker password encryption schemes chosen as a result. In these situations, SCRAM with a very low iteration count still gives benefits over weaker schemes like md5, so we allow the iteration count to be set to one at the low end. The new GUC is intentionally generically named such that it can be made to support future SCRAM standards should they emerge. At that point the value can be made into key:value pairs with an undefined key as a default which will be backwards compatible with this. Reviewed-by: Michael Paquier <michael@paquier.xyz> Reviewed-by: Jonathan S. Katz <jkatz@postgresql.org> Discussion: https://postgr.es/m/F72E7BC7-189F-4B17-BF47-9735EB72C364@yesql.se
This commit is contained in:
@ -895,7 +895,7 @@ verify_server_signature(fe_scram_state *state, bool *match,
|
||||
* error details.
|
||||
*/
|
||||
char *
|
||||
pg_fe_scram_build_secret(const char *password, const char **errstr)
|
||||
pg_fe_scram_build_secret(const char *password, int iterations, const char **errstr)
|
||||
{
|
||||
char *prep_password;
|
||||
pg_saslprep_rc rc;
|
||||
@ -927,7 +927,7 @@ pg_fe_scram_build_secret(const char *password, const char **errstr)
|
||||
|
||||
result = scram_build_secret(PG_SHA256, SCRAM_SHA_256_KEY_LEN, saltbuf,
|
||||
SCRAM_DEFAULT_SALT_LEN,
|
||||
SCRAM_DEFAULT_ITERATIONS, password,
|
||||
iterations, password,
|
||||
errstr);
|
||||
|
||||
free(prep_password);
|
||||
|
@ -1341,7 +1341,9 @@ PQencryptPasswordConn(PGconn *conn, const char *passwd, const char *user,
|
||||
{
|
||||
const char *errstr = NULL;
|
||||
|
||||
crypt_pwd = pg_fe_scram_build_secret(passwd, &errstr);
|
||||
crypt_pwd = pg_fe_scram_build_secret(passwd,
|
||||
conn->scram_sha_256_iterations,
|
||||
&errstr);
|
||||
if (!crypt_pwd)
|
||||
libpq_append_conn_error(conn, "could not encrypt password: %s", errstr);
|
||||
}
|
||||
|
@ -26,6 +26,7 @@ extern char *pg_fe_getauthname(PQExpBuffer errorMessage);
|
||||
/* Mechanisms in fe-auth-scram.c */
|
||||
extern const pg_fe_sasl_mech pg_scram_mech;
|
||||
extern char *pg_fe_scram_build_secret(const char *password,
|
||||
int iterations,
|
||||
const char **errstr);
|
||||
|
||||
#endif /* FE_AUTH_H */
|
||||
|
@ -596,6 +596,7 @@ pqDropServerData(PGconn *conn)
|
||||
conn->std_strings = false;
|
||||
conn->default_transaction_read_only = PG_BOOL_UNKNOWN;
|
||||
conn->in_hot_standby = PG_BOOL_UNKNOWN;
|
||||
conn->scram_sha_256_iterations = SCRAM_SHA_256_DEFAULT_ITERATIONS;
|
||||
conn->sversion = 0;
|
||||
|
||||
/* Drop large-object lookup data */
|
||||
@ -4182,6 +4183,7 @@ makeEmptyPGconn(void)
|
||||
conn->std_strings = false; /* unless server says differently */
|
||||
conn->default_transaction_read_only = PG_BOOL_UNKNOWN;
|
||||
conn->in_hot_standby = PG_BOOL_UNKNOWN;
|
||||
conn->scram_sha_256_iterations = SCRAM_SHA_256_DEFAULT_ITERATIONS;
|
||||
conn->verbosity = PQERRORS_DEFAULT;
|
||||
conn->show_context = PQSHOW_CONTEXT_ERRORS;
|
||||
conn->sock = PGINVALID_SOCKET;
|
||||
|
@ -1181,6 +1181,10 @@ pqSaveParameterStatus(PGconn *conn, const char *name, const char *value)
|
||||
conn->in_hot_standby =
|
||||
(strcmp(value, "on") == 0) ? PG_BOOL_YES : PG_BOOL_NO;
|
||||
}
|
||||
else if (strcmp(name, "scram_iterations") == 0)
|
||||
{
|
||||
conn->scram_sha_256_iterations = atoi(value);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -525,6 +525,7 @@ struct pg_conn
|
||||
/* Assorted state for SASL, SSL, GSS, etc */
|
||||
const pg_fe_sasl_mech *sasl;
|
||||
void *sasl_state;
|
||||
int scram_sha_256_iterations;
|
||||
|
||||
/* SSL structures */
|
||||
bool ssl_in_use;
|
||||
|
Reference in New Issue
Block a user