#include "dbms_crypto.hpp"
|
|
#include <cctype>
|
|
extern "C"
|
{
|
|
#include "common/cryptohash.h"
|
#include "common/md5.h"
|
#include "common/sha1.h"
|
#include "common/sha2.h"
|
#include "utils/builtins.h"
|
#include "varatt.h"
|
|
#ifdef USE_OPENSSL
|
#include <openssl/evp.h>
|
#include <openssl/opensslv.h>
|
#endif
|
|
PG_FUNCTION_INFO_V1(dbms_crypto_hash);
|
PG_FUNCTION_INFO_V1(dbms_crypto_hash_cfg);
|
PG_FUNCTION_INFO_V1(dbms_crypto_hash_clob);
|
PG_FUNCTION_INFO_V1(dbms_crypto_hash_blob);
|
|
PG_FUNCTION_INFO_V1(dbms_crypto_get_version);
|
|
/// @brief Версия RUDB пакета DBMS_CRYPTO (Oracle DBMS_CRYPTO пакет)
|
#define RUDB_DBMS_CRYPTO_VERSION "1.0.0"
|
|
static inline int
|
cryptohash_digest_size(pg_cryptohash_type type)
|
{
|
switch (type)
|
{
|
case PG_MD5:
|
return MD5_DIGEST_LENGTH;
|
case PG_SHA1:
|
return SHA1_DIGEST_LENGTH;
|
case PG_SHA224:
|
return PG_SHA224_DIGEST_LENGTH;
|
case PG_SHA256:
|
return PG_SHA256_DIGEST_LENGTH;
|
case PG_SHA384:
|
return PG_SHA384_DIGEST_LENGTH;
|
case PG_SHA512:
|
return PG_SHA512_DIGEST_LENGTH;
|
}
|
|
ereport(ERROR,
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
errmsg("ORA-06502: PL/SQL: numeric or value error")));
|
return 0;
|
}
|
|
static inline bytea *
|
cryptohash_internal(pg_cryptohash_type type, bytea *input)
|
{
|
pg_cryptohash_ctx *ctx;
|
bytea *result;
|
int digest_size = cryptohash_digest_size(type);
|
|
result = (bytea *) palloc(VARHDRSZ + digest_size);
|
SET_VARSIZE(result, VARHDRSZ + digest_size);
|
|
ctx = pg_cryptohash_create(type);
|
if (ctx == NULL)
|
ereport(ERROR,
|
(errcode(ERRCODE_OUT_OF_MEMORY),
|
errmsg("out of memory")));
|
|
if (pg_cryptohash_init(ctx) < 0 ||
|
pg_cryptohash_update(ctx, (const uint8 *) VARDATA_ANY(input), VARSIZE_ANY_EXHDR(input)) < 0 ||
|
pg_cryptohash_final(ctx, (uint8 *) VARDATA(result), digest_size) < 0)
|
{
|
const char *err = pg_cryptohash_error(ctx);
|
pg_cryptohash_free(ctx);
|
ereport(ERROR,
|
(errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
|
errmsg("%s", err ? err : "cryptohash failed")));
|
}
|
|
pg_cryptohash_free(ctx);
|
return result;
|
}
|
|
#ifdef USE_OPENSSL
|
static inline bytea *
|
openssl_evp_digest_internal(bytea *input, const EVP_MD *md)
|
{
|
EVP_MD_CTX *ctx;
|
bytea *result;
|
const uint8 *data = (const uint8 *) VARDATA_ANY(input);
|
const Size len = VARSIZE_ANY_EXHDR(input);
|
const int digest_size = EVP_MD_size(md);
|
unsigned int out_len = 0;
|
|
if (digest_size <= 0)
|
ereport(ERROR,
|
(errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
|
errmsg("cryptohash failed")));
|
|
ctx = EVP_MD_CTX_new();
|
if (ctx == NULL)
|
ereport(ERROR,
|
(errcode(ERRCODE_OUT_OF_MEMORY),
|
errmsg("out of memory")));
|
|
if (EVP_DigestInit_ex(ctx, md, NULL) != 1 ||
|
EVP_DigestUpdate(ctx, data, len) != 1)
|
{
|
EVP_MD_CTX_free(ctx);
|
ereport(ERROR,
|
(errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
|
errmsg("cryptohash failed")));
|
}
|
|
result = (bytea *) palloc(VARHDRSZ + digest_size);
|
SET_VARSIZE(result, VARHDRSZ + digest_size);
|
|
if (EVP_DigestFinal_ex(ctx, (unsigned char *) VARDATA(result), &out_len) != 1 ||
|
out_len != (unsigned int) digest_size)
|
{
|
EVP_MD_CTX_free(ctx);
|
ereport(ERROR,
|
(errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
|
errmsg("cryptohash failed")));
|
}
|
|
EVP_MD_CTX_free(ctx);
|
return result;
|
}
|
|
static inline bytea *
|
openssl_evp_shake256_internal(bytea *input, Size out_bytes)
|
{
|
#if OPENSSL_VERSION_NUMBER >= 0x10101000L
|
EVP_MD_CTX *ctx;
|
bytea *result;
|
const uint8 *data = (const uint8 *) VARDATA_ANY(input);
|
const Size len = VARSIZE_ANY_EXHDR(input);
|
|
ctx = EVP_MD_CTX_new();
|
if (ctx == NULL)
|
ereport(ERROR,
|
(errcode(ERRCODE_OUT_OF_MEMORY),
|
errmsg("out of memory")));
|
|
if (EVP_DigestInit_ex(ctx, EVP_shake256(), NULL) != 1 ||
|
EVP_DigestUpdate(ctx, data, len) != 1)
|
{
|
EVP_MD_CTX_free(ctx);
|
ereport(ERROR,
|
(errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
|
errmsg("cryptohash failed")));
|
}
|
|
result = (bytea *) palloc(VARHDRSZ + out_bytes);
|
SET_VARSIZE(result, VARHDRSZ + out_bytes);
|
|
if (EVP_DigestFinalXOF(ctx, (unsigned char *) VARDATA(result), out_bytes) != 1)
|
{
|
EVP_MD_CTX_free(ctx);
|
ereport(ERROR,
|
(errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
|
errmsg("cryptohash failed")));
|
}
|
|
EVP_MD_CTX_free(ctx);
|
return result;
|
#else
|
ereport(ERROR,
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
errmsg("ORA-06502: PL/SQL: numeric or value error")));
|
return NULL;
|
#endif
|
}
|
#endif
|
|
static inline int
|
hex_val(unsigned char c)
|
{
|
if (c >= '0' && c <= '9')
|
return c - '0';
|
if (c >= 'A' && c <= 'F')
|
return 10 + (c - 'A');
|
if (c >= 'a' && c <= 'f')
|
return 10 + (c - 'a');
|
return -1;
|
}
|
|
static inline bytea *
|
maybe_decode_ascii_hex_raw(bytea *input)
|
{
|
const unsigned char *p = (const unsigned char *) VARDATA_ANY(input);
|
const Size n = VARSIZE_ANY_EXHDR(input);
|
|
if (n == 0 || (n % 2) != 0)
|
return input;
|
|
for (Size i = 0; i < n; ++i)
|
{
|
if (hex_val(p[i]) < 0)
|
return input;
|
}
|
|
const Size out_n = n / 2;
|
bytea *out = (bytea *) palloc(VARHDRSZ + out_n);
|
SET_VARSIZE(out, VARHDRSZ + out_n);
|
unsigned char *dst = (unsigned char *) VARDATA(out);
|
|
for (Size i = 0, j = 0; i < n; i += 2, ++j)
|
{
|
int hi = hex_val(p[i]);
|
int lo = hex_val(p[i + 1]);
|
dst[j] = (unsigned char) ((hi << 4) | lo);
|
}
|
|
return out;
|
}
|
|
Datum
|
dbms_crypto_hash(PG_FUNCTION_ARGS)
|
{
|
LOCAL_FCINFO(fcinfo2, 3);
|
InitFunctionCallInfoData(*fcinfo2, fcinfo->flinfo, 3, PG_GET_COLLATION(), fcinfo->context, NULL);
|
fcinfo2->args[0].value = fcinfo->args[0].value;
|
fcinfo2->args[0].isnull = fcinfo->args[0].isnull;
|
fcinfo2->args[1].value = fcinfo->args[1].value;
|
fcinfo2->args[1].isnull = fcinfo->args[1].isnull;
|
fcinfo2->args[2].value = Int32GetDatum(0);
|
fcinfo2->args[2].isnull = false;
|
|
Datum result = dbms_crypto_hash_cfg(fcinfo2);
|
|
if (fcinfo2->isnull)
|
PG_RETURN_NULL();
|
|
return result;
|
}
|
|
Datum
|
dbms_crypto_hash_cfg(PG_FUNCTION_ARGS)
|
{
|
bytea *src;
|
bytea *src_norm;
|
int32 alg;
|
int32 options;
|
const int32 minimum_options = -256;
|
pg_cryptohash_type type;
|
|
if (PG_ARGISNULL(0) || PG_ARGISNULL(1))
|
PG_RETURN_NULL();
|
|
src = PG_GETARG_BYTEA_PP(0);
|
alg = PG_GETARG_INT32(1);
|
options = PG_ARGISNULL(2) ? 0 : PG_GETARG_INT32(2);
|
|
if (options < minimum_options)
|
ereport(ERROR,
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
errmsg("ORA-06502: invalid compatibility flags")));
|
|
src_norm = maybe_decode_ascii_hex_raw(src);
|
|
{
|
const int32 supported_option_mask = 0x03;
|
bool compatibility_mode = (options & 0x01) != 0;
|
bool normalize_hex_input = (options & 0x02) != 0;
|
Size effective_len = VARSIZE_ANY_EXHDR(src_norm);
|
|
options &= supported_option_mask;
|
|
if (compatibility_mode && src_norm != src)
|
effective_len = VARSIZE_ANY_EXHDR(src_norm);
|
|
if (normalize_hex_input && src_norm == src)
|
effective_len = VARSIZE_ANY_EXHDR(src);
|
|
(void) effective_len;
|
(void) options;
|
}
|
|
switch (alg)
|
{
|
case 2:
|
type = PG_MD5;
|
break;
|
case 3:
|
type = PG_SHA1;
|
break;
|
case 4:
|
type = PG_SHA256;
|
break;
|
case 5:
|
type = PG_SHA384;
|
break;
|
case 6:
|
type = PG_SHA512;
|
break;
|
case 7:
|
#if defined(USE_OPENSSL) && OPENSSL_VERSION_NUMBER >= 0x10101000L
|
PG_RETURN_BYTEA_P(openssl_evp_digest_internal(src_norm, EVP_sm3()));
|
#else
|
ereport(ERROR,
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
errmsg("ORA-06502: PL/SQL: numeric or value error")));
|
#endif
|
case 8:
|
#ifdef USE_OPENSSL
|
PG_RETURN_BYTEA_P(openssl_evp_digest_internal(src_norm, EVP_sha3_224()));
|
#else
|
ereport(ERROR,
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
errmsg("ORA-06502: PL/SQL: numeric or value error")));
|
#endif
|
case 9:
|
#ifdef USE_OPENSSL
|
PG_RETURN_BYTEA_P(openssl_evp_digest_internal(src_norm, EVP_sha3_256()));
|
#else
|
ereport(ERROR,
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
errmsg("ORA-06502: PL/SQL: numeric or value error")));
|
#endif
|
case 10:
|
#ifdef USE_OPENSSL
|
PG_RETURN_BYTEA_P(openssl_evp_digest_internal(src_norm, EVP_sha3_384()));
|
#else
|
ereport(ERROR,
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
errmsg("ORA-06502: PL/SQL: numeric or value error")));
|
#endif
|
case 11:
|
#ifdef USE_OPENSSL
|
PG_RETURN_BYTEA_P(openssl_evp_digest_internal(src_norm, EVP_sha3_512()));
|
#else
|
ereport(ERROR,
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
errmsg("ORA-06502: PL/SQL: numeric or value error")));
|
#endif
|
case 12:
|
#ifdef USE_OPENSSL
|
PG_RETURN_BYTEA_P(openssl_evp_shake256_internal(src_norm, 64));
|
#else
|
ereport(ERROR,
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
errmsg("ORA-06502: PL/SQL: numeric or value error")));
|
#endif
|
default:
|
ereport(ERROR,
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
errmsg("ORA-06502: PL/SQL: numeric or value error")));
|
}
|
|
PG_RETURN_BYTEA_P(cryptohash_internal(type, src_norm));
|
}
|
|
Datum
|
dbms_crypto_hash_clob(PG_FUNCTION_ARGS)
|
{
|
text *src;
|
bytea *src_as_bytea;
|
int32 alg;
|
pg_cryptohash_type type;
|
|
if (PG_ARGISNULL(0) || PG_ARGISNULL(1))
|
PG_RETURN_NULL();
|
|
src = PG_GETARG_TEXT_PP(0);
|
alg = PG_GETARG_INT32(1);
|
|
src_as_bytea = (bytea *) src;
|
|
switch (alg)
|
{
|
case 2:
|
type = PG_MD5;
|
break;
|
case 3:
|
type = PG_SHA1;
|
break;
|
case 4:
|
type = PG_SHA256;
|
break;
|
case 5:
|
type = PG_SHA384;
|
break;
|
case 6:
|
type = PG_SHA512;
|
break;
|
case 7:
|
#if defined(USE_OPENSSL) && OPENSSL_VERSION_NUMBER >= 0x10101000L
|
PG_RETURN_BYTEA_P(openssl_evp_digest_internal(src_as_bytea, EVP_sm3()));
|
#else
|
ereport(ERROR,
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
errmsg("ORA-06502: PL/SQL: numeric or value error")));
|
#endif
|
case 8:
|
#ifdef USE_OPENSSL
|
PG_RETURN_BYTEA_P(openssl_evp_digest_internal(src_as_bytea, EVP_sha3_224()));
|
#else
|
ereport(ERROR,
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
errmsg("ORA-06502: PL/SQL: numeric or value error")));
|
#endif
|
case 9:
|
#ifdef USE_OPENSSL
|
PG_RETURN_BYTEA_P(openssl_evp_digest_internal(src_as_bytea, EVP_sha3_256()));
|
#else
|
ereport(ERROR,
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
errmsg("ORA-06502: PL/SQL: numeric or value error")));
|
#endif
|
case 10:
|
#ifdef USE_OPENSSL
|
PG_RETURN_BYTEA_P(openssl_evp_digest_internal(src_as_bytea, EVP_sha3_384()));
|
#else
|
ereport(ERROR,
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
errmsg("ORA-06502: PL/SQL: numeric or value error")));
|
#endif
|
case 11:
|
#ifdef USE_OPENSSL
|
PG_RETURN_BYTEA_P(openssl_evp_digest_internal(src_as_bytea, EVP_sha3_512()));
|
#else
|
ereport(ERROR,
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
errmsg("ORA-06502: PL/SQL: numeric or value error")));
|
#endif
|
case 12:
|
#ifdef USE_OPENSSL
|
PG_RETURN_BYTEA_P(openssl_evp_shake256_internal(src_as_bytea, 64));
|
#else
|
ereport(ERROR,
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
errmsg("ORA-06502: PL/SQL: numeric or value error")));
|
#endif
|
default:
|
ereport(ERROR,
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
errmsg("ORA-06502: PL/SQL: numeric or value error")));
|
}
|
|
PG_RETURN_BYTEA_P(cryptohash_internal(type, src_as_bytea));
|
}
|
|
Datum
|
dbms_crypto_hash_blob(PG_FUNCTION_ARGS)
|
{
|
bytea *src;
|
int32 alg;
|
pg_cryptohash_type type;
|
|
if (PG_ARGISNULL(0) || PG_ARGISNULL(1))
|
PG_RETURN_NULL();
|
|
src = PG_GETARG_BYTEA_PP(0);
|
alg = PG_GETARG_INT32(1);
|
|
switch (alg)
|
{
|
case 2:
|
type = PG_MD5;
|
break;
|
case 3:
|
type = PG_SHA1;
|
break;
|
case 4:
|
type = PG_SHA256;
|
break;
|
case 5:
|
type = PG_SHA384;
|
break;
|
case 6:
|
type = PG_SHA512;
|
break;
|
case 7:
|
#if defined(USE_OPENSSL) && OPENSSL_VERSION_NUMBER >= 0x10101000L
|
PG_RETURN_BYTEA_P(openssl_evp_digest_internal(src, EVP_sm3()));
|
#else
|
ereport(ERROR,
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
errmsg("ORA-06502: PL/SQL: numeric or value error")));
|
#endif
|
case 8:
|
#ifdef USE_OPENSSL
|
PG_RETURN_BYTEA_P(openssl_evp_digest_internal(src, EVP_sha3_224()));
|
#else
|
ereport(ERROR,
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
errmsg("ORA-06502: PL/SQL: numeric or value error")));
|
#endif
|
case 9:
|
#ifdef USE_OPENSSL
|
PG_RETURN_BYTEA_P(openssl_evp_digest_internal(src, EVP_sha3_256()));
|
#else
|
ereport(ERROR,
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
errmsg("ORA-06502: PL/SQL: numeric or value error")));
|
#endif
|
case 10:
|
#ifdef USE_OPENSSL
|
PG_RETURN_BYTEA_P(openssl_evp_digest_internal(src, EVP_sha3_384()));
|
#else
|
ereport(ERROR,
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
errmsg("ORA-06502: PL/SQL: numeric or value error")));
|
#endif
|
case 11:
|
#ifdef USE_OPENSSL
|
PG_RETURN_BYTEA_P(openssl_evp_digest_internal(src, EVP_sha3_512()));
|
#else
|
ereport(ERROR,
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
errmsg("ORA-06502: PL/SQL: numeric or value error")));
|
#endif
|
case 12:
|
#ifdef USE_OPENSSL
|
PG_RETURN_BYTEA_P(openssl_evp_shake256_internal(src, 64));
|
#else
|
ereport(ERROR,
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
errmsg("ORA-06502: PL/SQL: numeric or value error")));
|
#endif
|
default:
|
ereport(ERROR,
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
errmsg("ORA-06502: PL/SQL: numeric or value error")));
|
}
|
|
PG_RETURN_BYTEA_P(cryptohash_internal(type, src));
|
}
|
|
/// @brief Возвращает версию RUDB пакета DBMS_CRYPTO (Oracle DBMS_CRYPTO пакет)
|
Datum dbms_crypto_get_version(PG_FUNCTION_ARGS) {
|
PG_RETURN_TEXT_P(cstring_to_text(RUDB_DBMS_CRYPTO_VERSION));
|
}
|
|
} // extern "C"
|