postgresql/contrib/pgcrypto/px-crypt.c

168 lines
3.9 KiB
C
Raw Normal View History

2001-08-21 03:32:01 +02: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
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2001-08-21 03:32:01 +02: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-20 22:08:53 +02:00
* contrib/pgcrypto/px-crypt.c
2001-08-21 03:32:01 +02:00
*/
#include "postgres.h"
#include "px.h"
2001-08-21 03:32:01 +02:00
#include "px-crypt.h"
static char *
run_crypt_des(const char *psw, const char *salt,
char *buf, unsigned len)
2001-08-21 03:32:01 +02:00
{
char *res;
res = px_crypt_des(psw, salt);
if (res == NULL || strlen(res) > len - 1)
2001-08-21 03:32:01 +02:00
return NULL;
strcpy(buf, res);
return buf;
}
static char *
run_crypt_md5(const char *psw, const char *salt,
char *buf, unsigned len)
2001-08-21 03:32:01 +02:00
{
char *res;
2001-08-21 03:32:01 +02:00
res = px_crypt_md5(psw, salt, buf, len);
return res;
}
static char *
run_crypt_bf(const char *psw, const char *salt,
char *buf, unsigned len)
2001-08-21 03:32:01 +02:00
{
char *res;
2001-08-21 03:32:01 +02:00
res = _crypt_blowfish_rn(psw, salt, buf, len);
return res;
2001-08-21 03:32:01 +02:00
}
struct px_crypt_algo
2001-08-21 03:32:01 +02:00
{
char *id;
2001-08-21 03:32:01 +02:00
unsigned id_len;
char *(*crypt) (const char *psw, const char *salt,
char *buf, unsigned len);
};
2001-08-21 03:32:01 +02:00
static const struct px_crypt_algo
2005-10-15 04:49:52 +02:00
px_crypt_list[] = {
{"$2a$", 4, run_crypt_bf},
{"$2x$", 4, run_crypt_bf},
2005-10-15 04:49:52 +02:00
{"$2$", 3, NULL}, /* N/A */
{"$1$", 3, run_crypt_md5},
{"_", 1, run_crypt_des},
{"", 0, run_crypt_des},
{NULL, 0, NULL}
2001-08-21 03:32:01 +02:00
};
char *
px_crypt(const char *psw, const char *salt, char *buf, unsigned len)
{
const struct px_crypt_algo *c;
2001-08-21 03:32:01 +02:00
for (c = px_crypt_list; c->id; c++)
2001-08-21 03:32:01 +02:00
{
if (!c->id_len)
2001-08-21 03:32:01 +02:00
break;
if (strncmp(salt, c->id, c->id_len) == 0)
2001-08-21 03:32:01 +02:00
break;
}
if (c->crypt == NULL)
2001-08-21 03:32:01 +02:00
return NULL;
return c->crypt(psw, salt, buf, len);
2001-08-21 03:32:01 +02:00
}
/*
* salt generators
*/
struct generator
{
char *name;
char *(*gen) (unsigned long count, const char *input, int size,
char *output, int output_size);
int input_len;
int def_rounds;
int min_rounds;
int max_rounds;
2001-08-21 03:32:01 +02: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},
{NULL, NULL, 0, 0, 0, 0}
2001-08-21 03:32:01 +02:00
};
int
px_gen_salt(const char *salt_type, char *buf, int rounds)
2001-08-21 03:32:01 +02:00
{
int res;
2001-08-21 03:32:01 +02:00
struct generator *g;
char *p;
char rbuf[16];
for (g = gen_list; g->name; g++)
if (pg_strcasecmp(g->name, salt_type) == 0)
break;
if (g->name == NULL)
return PXE_UNKNOWN_SALT_ALGO;
if (g->def_rounds)
{
if (rounds == 0)
rounds = g->def_rounds;
if (rounds < g->min_rounds || rounds > g->max_rounds)
return PXE_BAD_SALT_ROUNDS;
}
Support OpenSSL 1.1.0. Changes needed to build at all: - Check for SSL_new in configure, now that SSL_library_init is a macro. - Do not access struct members directly. This includes some new code in pgcrypto, to use the resource owner mechanism to ensure that we don't leak OpenSSL handles, now that we can't embed them in other structs anymore. - RAND_SSLeay() -> RAND_OpenSSL() Changes that were needed to silence deprecation warnings, but were not strictly necessary: - RAND_pseudo_bytes() -> RAND_bytes(). - SSL_library_init() and OpenSSL_config() -> OPENSSL_init_ssl() - ASN1_STRING_data() -> ASN1_STRING_get0_data() - DH_generate_parameters() -> DH_generate_parameters() - Locking callbacks are not needed with OpenSSL 1.1.0 anymore. (Good riddance!) Also change references to SSLEAY_VERSION_NUMBER with OPENSSL_VERSION_NUMBER, for the sake of consistency. OPENSSL_VERSION_NUMBER has existed since time immemorial. Fix SSL test suite to work with OpenSSL 1.1.0. CA certificates must have the "CA:true" basic constraint extension now, or OpenSSL will refuse them. Regenerate the test certificates with that. The "openssl" binary, used to generate the certificates, is also now more picky, and throws an error if an X509 extension is specified in "req_extensions", but that section is empty. Backpatch to all supported branches, per popular demand. In back-branches, we still support OpenSSL 0.9.7 and above. OpenSSL 0.9.6 should still work too, but I didn't test it. In master, we only support 0.9.8 and above. Patch by Andreas Karlsson, with additional changes by me. Discussion: <20160627151604.GD1051@msg.df7cb.de>
2016-09-15 11:36:21 +02:00
res = px_get_random_bytes((uint8 *) rbuf, g->input_len);
if (res < 0)
return res;
p = g->gen(rounds, rbuf, g->input_len, buf, PX_MAX_SALT_LEN);
px_memset(rbuf, 0, sizeof(rbuf));
if (p == NULL)
return PXE_BAD_SALT_ROUNDS;
2001-08-21 03:32:01 +02:00
return strlen(p);
2001-08-21 03:32:01 +02:00
}