PostgreSQL Source Code  git master
px.h File Reference
#include <sys/param.h>
Include dependency graph for px.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  px_digest
 
struct  px_alias
 
struct  px_hmac
 
struct  px_cipher
 
struct  px_combo
 

Macros

#define PX_DEBUG
 
#define PX_MAX_SALT_LEN   128
 
#define PXE_OK   0
 
#define PXE_NO_HASH   -2
 
#define PXE_NO_CIPHER   -3
 
#define PXE_BAD_OPTION   -5
 
#define PXE_BAD_FORMAT   -6
 
#define PXE_KEY_TOO_BIG   -7
 
#define PXE_CIPHER_INIT   -8
 
#define PXE_HASH_UNUSABLE_FOR_HMAC   -9
 
#define PXE_BUG   -12
 
#define PXE_ARGUMENT_ERROR   -13
 
#define PXE_UNKNOWN_SALT_ALGO   -14
 
#define PXE_BAD_SALT_ROUNDS   -15
 
#define PXE_NO_RANDOM   -17
 
#define PXE_DECRYPT_FAILED   -18
 
#define PXE_ENCRYPT_FAILED   -19
 
#define PXE_PGP_CORRUPT_DATA   -100
 
#define PXE_PGP_CORRUPT_ARMOR   -101
 
#define PXE_PGP_UNSUPPORTED_COMPR   -102
 
#define PXE_PGP_UNSUPPORTED_CIPHER   -103
 
#define PXE_PGP_UNSUPPORTED_HASH   -104
 
#define PXE_PGP_COMPRESSION_ERROR   -105
 
#define PXE_PGP_NOT_TEXT   -106
 
#define PXE_PGP_UNEXPECTED_PKT   -107
 
#define PXE_PGP_MATH_FAILED   -109
 
#define PXE_PGP_SHORT_ELGAMAL_KEY   -110
 
#define PXE_PGP_UNKNOWN_PUBALGO   -112
 
#define PXE_PGP_WRONG_KEY   -113
 
#define PXE_PGP_MULTIPLE_KEYS   -114
 
#define PXE_PGP_EXPECT_PUBLIC_KEY   -115
 
#define PXE_PGP_EXPECT_SECRET_KEY   -116
 
#define PXE_PGP_NOT_V4_KEYPKT   -117
 
#define PXE_PGP_KEYPKT_CORRUPT   -118
 
#define PXE_PGP_NO_USABLE_KEY   -119
 
#define PXE_PGP_NEED_SECRET_PSW   -120
 
#define PXE_PGP_BAD_S2K_MODE   -121
 
#define PXE_PGP_UNSUPPORTED_PUBALGO   -122
 
#define PXE_PGP_MULTIPLE_SUBKEYS   -123
 
#define px_md_result_size(md)   (md)->result_size(md)
 
#define px_md_block_size(md)   (md)->block_size(md)
 
#define px_md_reset(md)   (md)->reset(md)
 
#define px_md_update(md, data, dlen)   (md)->update(md, data, dlen)
 
#define px_md_finish(md, buf)   (md)->finish(md, buf)
 
#define px_md_free(md)   (md)->free(md)
 
#define px_hmac_result_size(hmac)   (hmac)->result_size(hmac)
 
#define px_hmac_block_size(hmac)   (hmac)->block_size(hmac)
 
#define px_hmac_reset(hmac)   (hmac)->reset(hmac)
 
#define px_hmac_init(hmac, key, klen)   (hmac)->init(hmac, key, klen)
 
#define px_hmac_update(hmac, data, dlen)   (hmac)->update(hmac, data, dlen)
 
#define px_hmac_finish(hmac, buf)   (hmac)->finish(hmac, buf)
 
#define px_hmac_free(hmac)   (hmac)->free(hmac)
 
#define px_cipher_key_size(c)   (c)->key_size(c)
 
#define px_cipher_block_size(c)   (c)->block_size(c)
 
#define px_cipher_iv_size(c)   (c)->iv_size(c)
 
#define px_cipher_init(c, k, klen, iv)   (c)->init(c, k, klen, iv)
 
#define px_cipher_encrypt(c, padding, data, dlen, res, rlen)    (c)->encrypt(c, padding, data, dlen, res, rlen)
 
#define px_cipher_decrypt(c, padding, data, dlen, res, rlen)    (c)->decrypt(c, padding, data, dlen, res, rlen)
 
#define px_cipher_free(c)   (c)->free(c)
 
#define px_combo_encrypt_len(c, dlen)   (c)->encrypt_len(c, dlen)
 
#define px_combo_decrypt_len(c, dlen)   (c)->decrypt_len(c, dlen)
 
#define px_combo_init(c, key, klen, iv, ivlen)    (c)->init(c, key, klen, iv, ivlen)
 
#define px_combo_encrypt(c, data, dlen, res, rlen)    (c)->encrypt(c, data, dlen, res, rlen)
 
#define px_combo_decrypt(c, data, dlen, res, rlen)    (c)->decrypt(c, data, dlen, res, rlen)
 
#define px_combo_free(c)   (c)->free(c)
 

Typedefs

typedef struct px_digest PX_MD
 
typedef struct px_alias PX_Alias
 
typedef struct px_hmac PX_HMAC
 
typedef struct px_cipher PX_Cipher
 
typedef struct px_combo PX_Combo
 

Functions

int px_find_digest (const char *name, PX_MD **res)
 
int px_find_hmac (const char *name, PX_HMAC **res)
 
int px_find_cipher (const char *name, PX_Cipher **res)
 
int px_find_combo (const char *name, PX_Combo **res)
 
void px_THROW_ERROR (int err) pg_attribute_noreturn()
 
const char * px_strerror (int err)
 
const char * px_resolve_alias (const PX_Alias *aliases, const char *name)
 
void px_set_debug_handler (void(*handler)(const char *))
 
void px_memset (void *ptr, int c, size_t len)
 
void px_debug (const char *fmt,...) pg_attribute_printf(1
 

Macro Definition Documentation

◆ px_cipher_block_size

#define px_cipher_block_size (   c)    (c)->block_size(c)

Definition at line 208 of file px.h.

◆ px_cipher_decrypt

#define px_cipher_decrypt (   c,
  padding,
  data,
  dlen,
  res,
  rlen 
)     (c)->decrypt(c, padding, data, dlen, res, rlen)

Definition at line 213 of file px.h.

◆ px_cipher_encrypt

#define px_cipher_encrypt (   c,
  padding,
  data,
  dlen,
  res,
  rlen 
)     (c)->encrypt(c, padding, data, dlen, res, rlen)

Definition at line 211 of file px.h.

◆ px_cipher_free

#define px_cipher_free (   c)    (c)->free(c)

Definition at line 215 of file px.h.

◆ px_cipher_init

#define px_cipher_init (   c,
  k,
  klen,
  iv 
)    (c)->init(c, k, klen, iv)

Definition at line 210 of file px.h.

◆ px_cipher_iv_size

#define px_cipher_iv_size (   c)    (c)->iv_size(c)

Definition at line 209 of file px.h.

◆ px_cipher_key_size

#define px_cipher_key_size (   c)    (c)->key_size(c)

Definition at line 207 of file px.h.

◆ px_combo_decrypt

#define px_combo_decrypt (   c,
  data,
  dlen,
  res,
  rlen 
)     (c)->decrypt(c, data, dlen, res, rlen)

Definition at line 224 of file px.h.

◆ px_combo_decrypt_len

#define px_combo_decrypt_len (   c,
  dlen 
)    (c)->decrypt_len(c, dlen)

Definition at line 219 of file px.h.

◆ px_combo_encrypt

#define px_combo_encrypt (   c,
  data,
  dlen,
  res,
  rlen 
)     (c)->encrypt(c, data, dlen, res, rlen)

Definition at line 222 of file px.h.

◆ px_combo_encrypt_len

#define px_combo_encrypt_len (   c,
  dlen 
)    (c)->encrypt_len(c, dlen)

Definition at line 218 of file px.h.

◆ px_combo_free

#define px_combo_free (   c)    (c)->free(c)

Definition at line 226 of file px.h.

◆ px_combo_init

#define px_combo_init (   c,
  key,
  klen,
  iv,
  ivlen 
)     (c)->init(c, key, klen, iv, ivlen)

Definition at line 220 of file px.h.

◆ PX_DEBUG

#define PX_DEBUG

Definition at line 38 of file px.h.

◆ px_hmac_block_size

#define px_hmac_block_size (   hmac)    (hmac)->block_size(hmac)

Definition at line 199 of file px.h.

◆ px_hmac_finish

#define px_hmac_finish (   hmac,
  buf 
)    (hmac)->finish(hmac, buf)

Definition at line 203 of file px.h.

◆ px_hmac_free

#define px_hmac_free (   hmac)    (hmac)->free(hmac)

Definition at line 204 of file px.h.

◆ px_hmac_init

#define px_hmac_init (   hmac,
  key,
  klen 
)    (hmac)->init(hmac, key, klen)

Definition at line 201 of file px.h.

◆ px_hmac_reset

#define px_hmac_reset (   hmac)    (hmac)->reset(hmac)

Definition at line 200 of file px.h.

◆ px_hmac_result_size

#define px_hmac_result_size (   hmac)    (hmac)->result_size(hmac)

Definition at line 198 of file px.h.

◆ px_hmac_update

#define px_hmac_update (   hmac,
  data,
  dlen 
)    (hmac)->update(hmac, data, dlen)

Definition at line 202 of file px.h.

◆ PX_MAX_SALT_LEN

#define PX_MAX_SALT_LEN   128

Definition at line 41 of file px.h.

◆ px_md_block_size

#define px_md_block_size (   md)    (md)->block_size(md)

Definition at line 192 of file px.h.

◆ px_md_finish

#define px_md_finish (   md,
  buf 
)    (md)->finish(md, buf)

Definition at line 195 of file px.h.

◆ px_md_free

#define px_md_free (   md)    (md)->free(md)

Definition at line 196 of file px.h.

◆ px_md_reset

#define px_md_reset (   md)    (md)->reset(md)

Definition at line 193 of file px.h.

◆ px_md_result_size

#define px_md_result_size (   md)    (md)->result_size(md)

Definition at line 191 of file px.h.

◆ px_md_update

#define px_md_update (   md,
  data,
  dlen 
)    (md)->update(md, data, dlen)

Definition at line 194 of file px.h.

◆ PXE_ARGUMENT_ERROR

#define PXE_ARGUMENT_ERROR   -13

Definition at line 59 of file px.h.

◆ PXE_BAD_FORMAT

#define PXE_BAD_FORMAT   -6

Definition at line 52 of file px.h.

◆ PXE_BAD_OPTION

#define PXE_BAD_OPTION   -5

Definition at line 51 of file px.h.

◆ PXE_BAD_SALT_ROUNDS

#define PXE_BAD_SALT_ROUNDS   -15

Definition at line 61 of file px.h.

◆ PXE_BUG

#define PXE_BUG   -12

Definition at line 58 of file px.h.

◆ PXE_CIPHER_INIT

#define PXE_CIPHER_INIT   -8

Definition at line 54 of file px.h.

◆ PXE_DECRYPT_FAILED

#define PXE_DECRYPT_FAILED   -18

Definition at line 64 of file px.h.

◆ PXE_ENCRYPT_FAILED

#define PXE_ENCRYPT_FAILED   -19

Definition at line 65 of file px.h.

◆ PXE_HASH_UNUSABLE_FOR_HMAC

#define PXE_HASH_UNUSABLE_FOR_HMAC   -9

Definition at line 55 of file px.h.

◆ PXE_KEY_TOO_BIG

#define PXE_KEY_TOO_BIG   -7

Definition at line 53 of file px.h.

◆ PXE_NO_CIPHER

#define PXE_NO_CIPHER   -3

Definition at line 49 of file px.h.

◆ PXE_NO_HASH

#define PXE_NO_HASH   -2

Definition at line 48 of file px.h.

◆ PXE_NO_RANDOM

#define PXE_NO_RANDOM   -17

Definition at line 63 of file px.h.

◆ PXE_OK

#define PXE_OK   0

Definition at line 46 of file px.h.

◆ PXE_PGP_BAD_S2K_MODE

#define PXE_PGP_BAD_S2K_MODE   -121

Definition at line 88 of file px.h.

◆ PXE_PGP_COMPRESSION_ERROR

#define PXE_PGP_COMPRESSION_ERROR   -105

Definition at line 72 of file px.h.

◆ PXE_PGP_CORRUPT_ARMOR

#define PXE_PGP_CORRUPT_ARMOR   -101

Definition at line 68 of file px.h.

◆ PXE_PGP_CORRUPT_DATA

#define PXE_PGP_CORRUPT_DATA   -100

Definition at line 67 of file px.h.

◆ PXE_PGP_EXPECT_PUBLIC_KEY

#define PXE_PGP_EXPECT_PUBLIC_KEY   -115

Definition at line 82 of file px.h.

◆ PXE_PGP_EXPECT_SECRET_KEY

#define PXE_PGP_EXPECT_SECRET_KEY   -116

Definition at line 83 of file px.h.

◆ PXE_PGP_KEYPKT_CORRUPT

#define PXE_PGP_KEYPKT_CORRUPT   -118

Definition at line 85 of file px.h.

◆ PXE_PGP_MATH_FAILED

#define PXE_PGP_MATH_FAILED   -109

Definition at line 76 of file px.h.

◆ PXE_PGP_MULTIPLE_KEYS

#define PXE_PGP_MULTIPLE_KEYS   -114

Definition at line 81 of file px.h.

◆ PXE_PGP_MULTIPLE_SUBKEYS

#define PXE_PGP_MULTIPLE_SUBKEYS   -123

Definition at line 90 of file px.h.

◆ PXE_PGP_NEED_SECRET_PSW

#define PXE_PGP_NEED_SECRET_PSW   -120

Definition at line 87 of file px.h.

◆ PXE_PGP_NO_USABLE_KEY

#define PXE_PGP_NO_USABLE_KEY   -119

Definition at line 86 of file px.h.

◆ PXE_PGP_NOT_TEXT

#define PXE_PGP_NOT_TEXT   -106

Definition at line 73 of file px.h.

◆ PXE_PGP_NOT_V4_KEYPKT

#define PXE_PGP_NOT_V4_KEYPKT   -117

Definition at line 84 of file px.h.

◆ PXE_PGP_SHORT_ELGAMAL_KEY

#define PXE_PGP_SHORT_ELGAMAL_KEY   -110

Definition at line 77 of file px.h.

◆ PXE_PGP_UNEXPECTED_PKT

#define PXE_PGP_UNEXPECTED_PKT   -107

Definition at line 74 of file px.h.

◆ PXE_PGP_UNKNOWN_PUBALGO

#define PXE_PGP_UNKNOWN_PUBALGO   -112

Definition at line 79 of file px.h.

◆ PXE_PGP_UNSUPPORTED_CIPHER

#define PXE_PGP_UNSUPPORTED_CIPHER   -103

Definition at line 70 of file px.h.

◆ PXE_PGP_UNSUPPORTED_COMPR

#define PXE_PGP_UNSUPPORTED_COMPR   -102

Definition at line 69 of file px.h.

◆ PXE_PGP_UNSUPPORTED_HASH

#define PXE_PGP_UNSUPPORTED_HASH   -104

Definition at line 71 of file px.h.

◆ PXE_PGP_UNSUPPORTED_PUBALGO

#define PXE_PGP_UNSUPPORTED_PUBALGO   -122

Definition at line 89 of file px.h.

◆ PXE_PGP_WRONG_KEY

#define PXE_PGP_WRONG_KEY   -113

Definition at line 80 of file px.h.

◆ PXE_UNKNOWN_SALT_ALGO

#define PXE_UNKNOWN_SALT_ALGO   -14

Definition at line 60 of file px.h.

Typedef Documentation

◆ PX_Alias

typedef struct px_alias PX_Alias

Definition at line 1 of file px.h.

◆ PX_Cipher

typedef struct px_cipher PX_Cipher

Definition at line 1 of file px.h.

◆ PX_Combo

typedef struct px_combo PX_Combo

Definition at line 1 of file px.h.

◆ PX_HMAC

typedef struct px_hmac PX_HMAC

Definition at line 1 of file px.h.

◆ PX_MD

typedef struct px_digest PX_MD

Definition at line 1 of file px.h.

Function Documentation

◆ px_debug()

void px_debug ( const char *  fmt,
  ... 
)

◆ px_find_cipher()

int px_find_cipher ( const char *  name,
PX_Cipher **  res 
)

Definition at line 771 of file openssl.c.

772 {
773  const struct ossl_cipher_lookup *i;
774  PX_Cipher *c = NULL;
775  EVP_CIPHER_CTX *ctx;
776  OSSLCipher *od;
777 
779  for (i = ossl_cipher_types; i->name; i++)
780  if (strcmp(i->name, name) == 0)
781  break;
782  if (i->name == NULL)
783  return PXE_NO_CIPHER;
784 
786  {
789  }
790 
791  /*
792  * Create an OSSLCipher object, an EVP_CIPHER_CTX object and a PX_Cipher.
793  * The order is crucial, to make sure we don't leak anything on
794  * out-of-memory or other error.
795  */
796  od = MemoryContextAllocZero(TopMemoryContext, sizeof(*od));
797  od->ciph = i->ciph;
798 
799  /* Allocate an EVP_CIPHER_CTX object. */
800  ctx = EVP_CIPHER_CTX_new();
801  if (!ctx)
802  {
803  pfree(od);
804  return PXE_CIPHER_INIT;
805  }
806 
807  od->evp_ctx = ctx;
809  od->next = open_ciphers;
810  od->prev = NULL;
811  open_ciphers = od;
812 
813  if (i->ciph->cipher_func)
814  od->evp_ciph = i->ciph->cipher_func();
815 
816  /* The PX_Cipher is allocated in current memory context */
817  c = palloc(sizeof(*c));
818  c->block_size = gen_ossl_block_size;
819  c->key_size = gen_ossl_key_size;
820  c->iv_size = gen_ossl_iv_size;
821  c->free = gen_ossl_free;
822  c->init = od->ciph->init;
823  c->encrypt = gen_ossl_encrypt;
824  c->decrypt = gen_ossl_decrypt;
825  c->ptr = od;
826 
827  *res = c;
828  return 0;
829 }
const char * name
Definition: encode.c:561
int i
Definition: isn.c:73
void pfree(void *pointer)
Definition: mcxt.c:1175
MemoryContext TopMemoryContext
Definition: mcxt.c:48
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition: mcxt.c:906
void * palloc(Size size)
Definition: mcxt.c:1068
static bool cipher_resowner_callback_registered
Definition: openssl.c:289
static unsigned gen_ossl_block_size(PX_Cipher *c)
Definition: openssl.c:337
static OSSLCipher * open_ciphers
Definition: openssl.c:288
static unsigned gen_ossl_key_size(PX_Cipher *c)
Definition: openssl.c:345
static int gen_ossl_encrypt(PX_Cipher *c, int padding, const uint8 *data, unsigned dlen, uint8 *res, unsigned *rlen)
Definition: openssl.c:402
static int gen_ossl_decrypt(PX_Cipher *c, int padding, const uint8 *data, unsigned dlen, uint8 *res, unsigned *rlen)
Definition: openssl.c:372
static void gen_ossl_free(PX_Cipher *c)
Definition: openssl.c:363
static unsigned gen_ossl_iv_size(PX_Cipher *c)
Definition: openssl.c:353
static PX_Alias ossl_aliases[]
Definition: openssl.c:658
static const struct ossl_cipher_lookup ossl_cipher_types[]
Definition: openssl.c:753
static void cipher_free_callback(ResourceReleasePhase phase, bool isCommit, bool isTopLevel, void *arg)
Definition: openssl.c:308
char * c
const char * px_resolve_alias(const PX_Alias *list, const char *name)
Definition: px.c:129
#define PXE_CIPHER_INIT
Definition: px.h:54
#define PXE_NO_CIPHER
Definition: px.h:49
ResourceOwner CurrentResourceOwner
Definition: resowner.c:146
void RegisterResourceReleaseCallback(ResourceReleaseCallback callback, void *arg)
Definition: resowner.c:849
struct OSSLCipher * next
Definition: openssl.c:284
const struct ossl_cipher * ciph
Definition: openssl.c:281
struct OSSLCipher * prev
Definition: openssl.c:285
ResourceOwner owner
Definition: openssl.c:283
EVP_CIPHER_CTX * evp_ctx
Definition: openssl.c:275
const EVP_CIPHER * evp_ciph
Definition: openssl.c:276
int(* init)(PX_Cipher *c, const uint8 *key, unsigned klen, const uint8 *iv)
Definition: openssl.c:259
Definition: px.h:141

References OSSLCipher::ciph, cipher_free_callback(), cipher_resowner_callback_registered, CurrentResourceOwner, OSSLCipher::evp_ciph, OSSLCipher::evp_ctx, gen_ossl_block_size(), gen_ossl_decrypt(), gen_ossl_encrypt(), gen_ossl_free(), gen_ossl_iv_size(), gen_ossl_key_size(), i, ossl_cipher::init, MemoryContextAllocZero(), name, OSSLCipher::next, open_ciphers, ossl_aliases, ossl_cipher_types, OSSLCipher::owner, palloc(), pfree(), OSSLCipher::prev, px_resolve_alias(), PXE_CIPHER_INIT, PXE_NO_CIPHER, RegisterResourceReleaseCallback(), res, and TopMemoryContext.

Referenced by pgp_load_cipher(), and px_find_combo().

◆ px_find_combo()

int px_find_combo ( const char *  name,
PX_Combo **  res 
)

Definition at line 286 of file px.c.

287 {
288  int err;
289  char *buf,
290  *s_cipher,
291  *s_pad;
292 
293  PX_Combo *cx;
294 
295  cx = palloc0(sizeof(*cx));
296  buf = pstrdup(name);
297 
298  err = parse_cipher_name(buf, &s_cipher, &s_pad);
299  if (err)
300  {
301  pfree(buf);
302  pfree(cx);
303  return err;
304  }
305 
306  err = px_find_cipher(s_cipher, &cx->cipher);
307  if (err)
308  goto err1;
309 
310  if (s_pad != NULL)
311  {
312  if (strcmp(s_pad, "pkcs") == 0)
313  cx->padding = 1;
314  else if (strcmp(s_pad, "none") == 0)
315  cx->padding = 0;
316  else
317  goto err1;
318  }
319  else
320  cx->padding = 1;
321 
322  cx->init = combo_init;
323  cx->encrypt = combo_encrypt;
324  cx->decrypt = combo_decrypt;
325  cx->encrypt_len = combo_encrypt_len;
326  cx->decrypt_len = combo_decrypt_len;
327  cx->free = combo_free;
328 
329  pfree(buf);
330 
331  *res = cx;
332 
333  return 0;
334 
335 err1:
336  if (cx->cipher)
337  px_cipher_free(cx->cipher);
338  pfree(cx);
339  pfree(buf);
340  return PXE_NO_CIPHER;
341 }
int cx(PlannerInfo *root, Gene *tour1, Gene *tour2, Gene *offspring, int num_gene, City *city_table)
char * pstrdup(const char *in)
Definition: mcxt.c:1305
void * palloc0(Size size)
Definition: mcxt.c:1099
int px_find_cipher(const char *name, PX_Cipher **res)
Definition: openssl.c:771
static char * buf
Definition: pg_test_fsync.c:67
static void combo_free(PX_Combo *cx)
Definition: px.c:233
static int combo_encrypt(PX_Combo *cx, const uint8 *data, unsigned dlen, uint8 *res, unsigned *rlen)
Definition: px.c:219
static int parse_cipher_name(char *full, char **cipher, char **pad)
Definition: px.c:244
static int combo_decrypt(PX_Combo *cx, const uint8 *data, unsigned dlen, uint8 *res, unsigned *rlen)
Definition: px.c:226
static unsigned combo_decrypt_len(PX_Combo *cx, unsigned dlen)
Definition: px.c:175
static unsigned combo_encrypt_len(PX_Combo *cx, unsigned dlen)
Definition: px.c:169
static int combo_init(PX_Combo *cx, const uint8 *key, unsigned klen, const uint8 *iv, unsigned ivlen)
Definition: px.c:181
#define px_cipher_free(c)
Definition: px.h:215
Definition: px.h:156

References buf, combo_decrypt(), combo_decrypt_len(), combo_encrypt(), combo_encrypt_len(), combo_free(), combo_init(), cx(), error_desc::err, name, palloc0(), parse_cipher_name(), pfree(), pstrdup(), px_cipher_free, px_find_cipher(), PXE_NO_CIPHER, and res.

Referenced by pg_decrypt(), pg_decrypt_iv(), pg_encrypt(), and pg_encrypt_iv().

◆ px_find_digest()

int px_find_digest ( const char *  name,
PX_MD **  res 
)

Definition at line 178 of file openssl.c.

179 {
180  const EVP_MD *md;
181  EVP_MD_CTX *ctx;
182  PX_MD *h;
183  OSSLDigest *digest;
184 
186  {
188  OpenSSL_add_all_algorithms();
189  }
190 
192  {
195  }
196 
197  md = EVP_get_digestbyname(name);
198  if (md == NULL)
199  return PXE_NO_HASH;
200 
201  /*
202  * Create an OSSLDigest object, an OpenSSL MD object, and a PX_MD object.
203  * The order is crucial, to make sure we don't leak anything on
204  * out-of-memory or other error.
205  */
206  digest = MemoryContextAlloc(TopMemoryContext, sizeof(*digest));
207 
208  ctx = EVP_MD_CTX_create();
209  if (!ctx)
210  {
211  pfree(digest);
212  return PXE_CIPHER_INIT;
213  }
214  if (EVP_DigestInit_ex(ctx, md, NULL) == 0)
215  {
216  EVP_MD_CTX_destroy(ctx);
217  pfree(digest);
218  return PXE_CIPHER_INIT;
219  }
220 
221  digest->algo = md;
222  digest->ctx = ctx;
223  digest->owner = CurrentResourceOwner;
224  digest->next = open_digests;
225  digest->prev = NULL;
226  open_digests = digest;
227 
228  /* The PX_MD object is allocated in the current memory context. */
229  h = palloc(sizeof(*h));
232  h->reset = digest_reset;
233  h->update = digest_update;
234  h->finish = digest_finish;
235  h->free = digest_free;
236  h->p.ptr = (void *) digest;
237 
238  *res = h;
239  return 0;
240 }
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:863
static bool digest_resowner_callback_registered
Definition: openssl.c:68
static void digest_free_callback(ResourceReleasePhase phase, bool isCommit, bool isTopLevel, void *arg)
Definition: openssl.c:87
static void digest_update(PX_MD *h, const uint8 *data, unsigned dlen)
Definition: openssl.c:147
static unsigned digest_result_size(PX_MD *h)
Definition: openssl.c:114
static void digest_finish(PX_MD *h, uint8 *dst)
Definition: openssl.c:156
static int px_openssl_initialized
Definition: openssl.c:173
static void digest_reset(PX_MD *h)
Definition: openssl.c:138
static unsigned digest_block_size(PX_MD *h)
Definition: openssl.c:126
static void digest_free(PX_MD *h)
Definition: openssl.c:165
static OSSLDigest * open_digests
Definition: openssl.c:67
#define PXE_NO_HASH
Definition: px.h:48
EVP_MD_CTX * ctx
Definition: openssl.c:60
struct OSSLDigest * next
Definition: openssl.c:63
const EVP_MD * algo
Definition: openssl.c:59
ResourceOwner owner
Definition: openssl.c:62
struct OSSLDigest * prev
Definition: openssl.c:64
Definition: px.h:100
void(* free)(PX_MD *h)
Definition: px.h:106
void(* update)(PX_MD *h, const uint8 *data, unsigned dlen)
Definition: px.h:104
unsigned(* result_size)(PX_MD *h)
Definition: px.h:101
union px_digest::@8 p
void(* reset)(PX_MD *h)
Definition: px.h:103
unsigned(* block_size)(PX_MD *h)
Definition: px.h:102
void * ptr
Definition: px.h:111
void(* finish)(PX_MD *h, uint8 *dst)
Definition: px.h:105

References OSSLDigest::algo, px_digest::block_size, OSSLDigest::ctx, CurrentResourceOwner, digest_block_size(), digest_finish(), digest_free(), digest_free_callback(), digest_reset(), digest_resowner_callback_registered, digest_result_size(), digest_update(), px_digest::finish, px_digest::free, MemoryContextAlloc(), name, OSSLDigest::next, open_digests, OSSLDigest::owner, px_digest::p, palloc(), pfree(), OSSLDigest::prev, px_digest::ptr, px_openssl_initialized, PXE_CIPHER_INIT, PXE_NO_HASH, RegisterResourceReleaseCallback(), res, px_digest::reset, px_digest::result_size, TopMemoryContext, and px_digest::update.

Referenced by pg_digest(), pgp_load_digest(), px_crypt_md5(), and px_find_hmac().

◆ px_find_hmac()

int px_find_hmac ( const char *  name,
PX_HMAC **  res 
)

Definition at line 142 of file px-hmac.c.

143 {
144  int err;
145  PX_MD *md;
146  PX_HMAC *h;
147  unsigned bs;
148 
149  err = px_find_digest(name, &md);
150  if (err)
151  return err;
152 
153  bs = px_md_block_size(md);
154  if (bs < 2)
155  {
156  px_md_free(md);
158  }
159 
160  h = palloc(sizeof(*h));
161  h->p.ipad = palloc(bs);
162  h->p.opad = palloc(bs);
163  h->md = md;
164 
167  h->reset = hmac_reset;
168  h->update = hmac_update;
169  h->finish = hmac_finish;
170  h->free = hmac_free;
171  h->init = hmac_init;
172 
173  *res = h;
174 
175  return 0;
176 }
int px_find_digest(const char *name, PX_MD **res)
Definition: openssl.c:178
static unsigned hmac_result_size(PX_HMAC *h)
Definition: px-hmac.c:40
static void hmac_init(PX_HMAC *h, const uint8 *key, unsigned klen)
Definition: px-hmac.c:52
static void hmac_finish(PX_HMAC *h, uint8 *dst)
Definition: px-hmac.c:100
static void hmac_update(PX_HMAC *h, const uint8 *data, unsigned dlen)
Definition: px-hmac.c:94
static void hmac_free(PX_HMAC *h)
Definition: px-hmac.c:124
static unsigned hmac_block_size(PX_HMAC *h)
Definition: px-hmac.c:46
static void hmac_reset(PX_HMAC *h)
Definition: px-hmac.c:84
#define px_md_free(md)
Definition: px.h:196
#define PXE_HASH_UNUSABLE_FOR_HMAC
Definition: px.h:55
#define px_md_block_size(md)
Definition: px.h:192
Definition: px.h:122
PX_MD * md
Definition: px.h:131
unsigned(* block_size)(PX_HMAC *h)
Definition: px.h:124
void(* finish)(PX_HMAC *h, uint8 *dst)
Definition: px.h:127
uint8 * ipad
Definition: px.h:135
struct px_hmac::@9 p
void(* init)(PX_HMAC *h, const uint8 *key, unsigned klen)
Definition: px.h:129
uint8 * opad
Definition: px.h:136
unsigned(* result_size)(PX_HMAC *h)
Definition: px.h:123
void(* update)(PX_HMAC *h, const uint8 *data, unsigned dlen)
Definition: px.h:126
void(* free)(PX_HMAC *h)
Definition: px.h:128
void(* reset)(PX_HMAC *h)
Definition: px.h:125

References px_hmac::block_size, px_hmac::finish, px_hmac::free, hmac_block_size(), hmac_finish(), hmac_free(), hmac_init(), hmac_reset(), hmac_result_size(), hmac_update(), px_hmac::init, px_hmac::ipad, px_hmac::md, name, px_hmac::opad, px_hmac::p, palloc(), px_find_digest(), px_md_block_size, px_md_free, PXE_HASH_UNUSABLE_FOR_HMAC, res, px_hmac::reset, px_hmac::result_size, and px_hmac::update.

Referenced by pg_hmac().

◆ px_memset()

◆ px_resolve_alias()

const char* px_resolve_alias ( const PX_Alias aliases,
const char *  name 
)

Definition at line 129 of file px.c.

130 {
131  while (list->name)
132  {
133  if (pg_strcasecmp(list->alias, name) == 0)
134  return list->name;
135  list++;
136  }
137  return name;
138 }
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36

References sort-test::list, name, and pg_strcasecmp().

Referenced by px_find_cipher().

◆ px_set_debug_handler()

void px_set_debug_handler ( void(*)(const char *)  handler)

Definition at line 143 of file px.c.

144 {
145  debug_handler = handler;
146 }
static void(* debug_handler)(const char *)
Definition: px.c:140

References debug_handler.

Referenced by decrypt_internal(), encrypt_internal(), and init_work().

◆ px_strerror()

const char* px_strerror ( int  err)

Definition at line 111 of file px.c.

112 {
113  const struct error_desc *e;
114 
115  for (e = px_err_list; e->desc; e++)
116  if (e->err == err)
117  return e->desc;
118  return "Bad error code";
119 }
e
Definition: preproc-init.c:82
static const struct error_desc px_err_list[]
Definition: px.c:42
Definition: px.c:37
const char * desc
Definition: px.c:39
int err
Definition: px.c:38

References error_desc::desc, error_desc::err, and px_err_list.

Referenced by find_provider(), pg_decrypt(), pg_decrypt_iv(), pg_encrypt(), pg_encrypt_iv(), pg_gen_salt(), pg_gen_salt_rounds(), and px_THROW_ERROR().

◆ px_THROW_ERROR()

void px_THROW_ERROR ( int  err)

Definition at line 93 of file px.c.

94 {
95  if (err == PXE_NO_RANDOM)
96  {
97  ereport(ERROR,
98  (errcode(ERRCODE_INTERNAL_ERROR),
99  errmsg("could not generate a random number")));
100  }
101  else
102  {
103  /* For other errors, use the message from the above list. */
104  ereport(ERROR,
105  (errcode(ERRCODE_EXTERNAL_ROUTINE_INVOCATION_EXCEPTION),
106  errmsg("%s", px_strerror(err))));
107  }
108 }
int errcode(int sqlerrcode)
Definition: elog.c:693
int errmsg(const char *fmt,...)
Definition: elog.c:904
#define ERROR
Definition: elog.h:33
#define ereport(elevel,...)
Definition: elog.h:143
const char * px_strerror(int err)
Definition: px.c:111
#define PXE_NO_RANDOM
Definition: px.h:63

References ereport, error_desc::err, errcode(), errmsg(), ERROR, px_strerror(), and PXE_NO_RANDOM.

Referenced by decrypt_internal(), encrypt_internal(), init_work(), pg_dearmor(), pg_random_bytes(), pgp_armor_headers(), and pgp_key_id_w().