2001-08-21 09:32:01 +08:00
|
|
|
/*
|
|
|
|
* px-crypt.c
|
|
|
|
* Wrapper for various crypt algorithms.
|
|
|
|
*
|
|
|
|
* Copyright (c) 2001 Marko Kreen
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
2014-05-07 00:12:18 +08:00
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
2001-08-21 09:32:01 +08:00
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
2010-09-21 04:08:53 +08:00
|
|
|
* contrib/pgcrypto/px-crypt.c
|
2001-08-21 09:32:01 +08:00
|
|
|
*/
|
|
|
|
|
2005-07-11 23:07:59 +08:00
|
|
|
#include "postgres.h"
|
|
|
|
|
2001-09-23 12:12:44 +08:00
|
|
|
#include "px.h"
|
2001-08-21 09:32:01 +08:00
|
|
|
#include "px-crypt.h"
|
|
|
|
|
|
|
|
|
|
|
|
static char *
|
|
|
|
run_crypt_des(const char *psw, const char *salt,
|
2001-10-25 13:50:21 +08:00
|
|
|
char *buf, unsigned len)
|
2001-08-21 09:32:01 +08:00
|
|
|
{
|
|
|
|
char *res;
|
|
|
|
|
|
|
|
res = px_crypt_des(psw, salt);
|
2015-10-05 22:06:29 +08:00
|
|
|
if (res == NULL || strlen(res) > len - 1)
|
2001-08-21 09:32:01 +08:00
|
|
|
return NULL;
|
|
|
|
strcpy(buf, res);
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
|
|
|
run_crypt_md5(const char *psw, const char *salt,
|
2001-10-25 13:50:21 +08:00
|
|
|
char *buf, unsigned len)
|
2001-08-21 09:32:01 +08:00
|
|
|
{
|
|
|
|
char *res;
|
2001-10-25 13:50:21 +08:00
|
|
|
|
2001-08-21 09:32:01 +08:00
|
|
|
res = px_crypt_md5(psw, salt, buf, len);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
|
|
|
run_crypt_bf(const char *psw, const char *salt,
|
2001-10-25 13:50:21 +08:00
|
|
|
char *buf, unsigned len)
|
2001-08-21 09:32:01 +08:00
|
|
|
{
|
|
|
|
char *res;
|
2001-10-25 13:50:21 +08:00
|
|
|
|
2001-08-21 09:32:01 +08:00
|
|
|
res = _crypt_blowfish_rn(psw, salt, buf, len);
|
2001-09-23 12:12:44 +08:00
|
|
|
return res;
|
2001-08-21 09:32:01 +08:00
|
|
|
}
|
|
|
|
|
2005-03-21 13:18:46 +08:00
|
|
|
struct px_crypt_algo
|
2001-08-21 09:32:01 +08:00
|
|
|
{
|
2001-10-25 13:50:21 +08:00
|
|
|
char *id;
|
2001-08-21 09:32:01 +08:00
|
|
|
unsigned id_len;
|
|
|
|
char *(*crypt) (const char *psw, const char *salt,
|
2017-06-22 02:39:04 +08:00
|
|
|
char *buf, unsigned len);
|
2005-03-21 13:18:46 +08:00
|
|
|
};
|
2001-08-21 09:32:01 +08:00
|
|
|
|
2005-03-21 13:18:46 +08:00
|
|
|
static const struct px_crypt_algo
|
2005-10-15 10:49:52 +08:00
|
|
|
px_crypt_list[] = {
|
2005-03-21 13:18:46 +08:00
|
|
|
{"$2a$", 4, run_crypt_bf},
|
2011-06-22 02:41:05 +08:00
|
|
|
{"$2x$", 4, run_crypt_bf},
|
2005-10-15 10:49:52 +08:00
|
|
|
{"$2$", 3, NULL}, /* N/A */
|
2005-03-21 13:18:46 +08:00
|
|
|
{"$1$", 3, run_crypt_md5},
|
|
|
|
{"_", 1, run_crypt_des},
|
|
|
|
{"", 0, run_crypt_des},
|
|
|
|
{NULL, 0, NULL}
|
2001-08-21 09:32:01 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
char *
|
|
|
|
px_crypt(const char *psw, const char *salt, char *buf, unsigned len)
|
|
|
|
{
|
2005-03-21 13:18:46 +08:00
|
|
|
const struct px_crypt_algo *c;
|
2001-08-21 09:32:01 +08:00
|
|
|
|
2005-03-21 13:18:46 +08:00
|
|
|
for (c = px_crypt_list; c->id; c++)
|
2001-08-21 09:32:01 +08:00
|
|
|
{
|
2005-03-21 13:18:46 +08:00
|
|
|
if (!c->id_len)
|
2001-08-21 09:32:01 +08:00
|
|
|
break;
|
2011-12-28 03:19:09 +08:00
|
|
|
if (strncmp(salt, c->id, c->id_len) == 0)
|
2001-08-21 09:32:01 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2005-03-21 13:18:46 +08:00
|
|
|
if (c->crypt == NULL)
|
2001-08-21 09:32:01 +08:00
|
|
|
return NULL;
|
|
|
|
|
2005-03-21 13:18:46 +08:00
|
|
|
return c->crypt(psw, salt, buf, len);
|
2001-08-21 09:32:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* salt generators
|
|
|
|
*/
|
|
|
|
|
2001-10-25 13:50:21 +08:00
|
|
|
struct generator
|
|
|
|
{
|
|
|
|
char *name;
|
|
|
|
char *(*gen) (unsigned long count, const char *input, int size,
|
2017-06-22 02:39:04 +08:00
|
|
|
char *output, int output_size);
|
2001-10-25 13:50:21 +08:00
|
|
|
int input_len;
|
|
|
|
int def_rounds;
|
|
|
|
int min_rounds;
|
|
|
|
int max_rounds;
|
2001-08-21 09:32:01 +08:00
|
|
|
};
|
|
|
|
|
2001-10-25 13:50:21 +08:00
|
|
|
static struct generator gen_list[] = {
|
|
|
|
{"des", _crypt_gensalt_traditional_rn, 2, 0, 0, 0},
|
|
|
|
{"md5", _crypt_gensalt_md5_rn, 6, 0, 0, 0},
|
|
|
|
{"xdes", _crypt_gensalt_extended_rn, 3, PX_XDES_ROUNDS, 1, 0xFFFFFF},
|
|
|
|
{"bf", _crypt_gensalt_blowfish_rn, 16, PX_BF_ROUNDS, 4, 31},
|
2005-03-21 13:18:46 +08:00
|
|
|
{NULL, NULL, 0, 0, 0, 0}
|
2001-08-21 09:32:01 +08:00
|
|
|
};
|
|
|
|
|
2005-03-21 13:19:55 +08:00
|
|
|
int
|
2001-09-23 12:12:44 +08:00
|
|
|
px_gen_salt(const char *salt_type, char *buf, int rounds)
|
2001-08-21 09:32:01 +08:00
|
|
|
{
|
|
|
|
struct generator *g;
|
2001-10-25 13:50:21 +08:00
|
|
|
char *p;
|
|
|
|
char rbuf[16];
|
|
|
|
|
2005-03-21 13:19:55 +08:00
|
|
|
for (g = gen_list; g->name; g++)
|
|
|
|
if (pg_strcasecmp(g->name, salt_type) == 0)
|
|
|
|
break;
|
2001-09-23 12:12:44 +08:00
|
|
|
|
2005-03-21 13:19:55 +08:00
|
|
|
if (g->name == NULL)
|
|
|
|
return PXE_UNKNOWN_SALT_ALGO;
|
2001-10-25 13:50:21 +08:00
|
|
|
|
2005-03-21 13:19:55 +08:00
|
|
|
if (g->def_rounds)
|
|
|
|
{
|
|
|
|
if (rounds == 0)
|
|
|
|
rounds = g->def_rounds;
|
2001-09-23 12:12:44 +08:00
|
|
|
|
2005-03-21 13:19:55 +08:00
|
|
|
if (rounds < g->min_rounds || rounds > g->max_rounds)
|
|
|
|
return PXE_BAD_SALT_ROUNDS;
|
|
|
|
}
|
2001-09-23 12:12:44 +08:00
|
|
|
|
2019-01-01 19:05:51 +08:00
|
|
|
if (!pg_strong_random(rbuf, g->input_len))
|
Replace PostmasterRandom() with a stronger source, second attempt.
This adds a new routine, pg_strong_random() for generating random bytes,
for use in both frontend and backend. At the moment, it's only used in
the backend, but the upcoming SCRAM authentication patches need strong
random numbers in libpq as well.
pg_strong_random() is based on, and replaces, the existing implementation
in pgcrypto. It can acquire strong random numbers from a number of sources,
depending on what's available:
- OpenSSL RAND_bytes(), if built with OpenSSL
- On Windows, the native cryptographic functions are used
- /dev/urandom
Unlike the current pgcrypto function, the source is chosen by configure.
That makes it easier to test different implementations, and ensures that
we don't accidentally fall back to a less secure implementation, if the
primary source fails. All of those methods are quite reliable, it would be
pretty surprising for them to fail, so we'd rather find out by failing
hard.
If no strong random source is available, we fall back to using erand48(),
seeded from current timestamp, like PostmasterRandom() was. That isn't
cryptographically secure, but allows us to still work on platforms that
don't have any of the above stronger sources. Because it's not very secure,
the built-in implementation is only used if explicitly requested with
--disable-strong-random.
This replaces the more complicated Fortuna algorithm we used to have in
pgcrypto, which is unfortunate, but all modern platforms have /dev/urandom,
so it doesn't seem worth the maintenance effort to keep that. pgcrypto
functions that require strong random numbers will be disabled with
--disable-strong-random.
Original patch by Magnus Hagander, tons of further work by Michael Paquier
and me.
Discussion: https://www.postgresql.org/message-id/CAB7nPqRy3krN8quR9XujMVVHYtXJ0_60nqgVc6oUk8ygyVkZsA@mail.gmail.com
Discussion: https://www.postgresql.org/message-id/CAB7nPqRWkNYRRPJA7-cF+LfroYV10pvjdz6GNvxk-Eee9FypKA@mail.gmail.com
2016-12-05 19:42:59 +08:00
|
|
|
return PXE_NO_RANDOM;
|
2001-10-25 13:50:21 +08:00
|
|
|
|
2005-03-21 13:19:55 +08:00
|
|
|
p = g->gen(rounds, rbuf, g->input_len, buf, PX_MAX_SALT_LEN);
|
2014-04-18 00:37:53 +08:00
|
|
|
px_memset(rbuf, 0, sizeof(rbuf));
|
2005-03-21 13:19:55 +08:00
|
|
|
|
|
|
|
if (p == NULL)
|
|
|
|
return PXE_BAD_SALT_ROUNDS;
|
2001-08-21 09:32:01 +08:00
|
|
|
|
2005-03-21 13:19:55 +08:00
|
|
|
return strlen(p);
|
2001-08-21 09:32:01 +08:00
|
|
|
}
|