PostgreSQL Source Code  git master
openssl.c File Reference
#include "postgres.h"
#include <openssl/evp.h>
#include <openssl/err.h>
#include <openssl/rand.h>
#include "px.h"
#include "utils/memutils.h"
#include "utils/resowner.h"
Include dependency graph for openssl.c:

Go to the source code of this file.

Data Structures

struct  OSSLDigest
 
struct  ossl_cipher
 
struct  OSSLCipher
 
struct  ossl_cipher_lookup
 

Macros

#define MAX_KEY   (512/8)
 
#define MAX_IV   (128/8)
 

Typedefs

typedef struct OSSLDigest OSSLDigest
 
typedef const EVP_CIPHER *(* ossl_EVP_cipher_func) (void)
 
typedef struct OSSLCipher OSSLCipher
 

Functions

static void ResOwnerReleaseOSSLDigest (Datum res)
 
static void ResourceOwnerRememberOSSLDigest (ResourceOwner owner, OSSLDigest *digest)
 
static void ResourceOwnerForgetOSSLDigest (ResourceOwner owner, OSSLDigest *digest)
 
static void free_openssl_digest (OSSLDigest *digest)
 
static unsigned digest_result_size (PX_MD *h)
 
static unsigned digest_block_size (PX_MD *h)
 
static void digest_reset (PX_MD *h)
 
static void digest_update (PX_MD *h, const uint8 *data, unsigned dlen)
 
static void digest_finish (PX_MD *h, uint8 *dst)
 
static void digest_free (PX_MD *h)
 
int px_find_digest (const char *name, PX_MD **res)
 
static void ResOwnerReleaseOSSLCipher (Datum res)
 
static void ResourceOwnerRememberOSSLCipher (ResourceOwner owner, OSSLCipher *od)
 
static void ResourceOwnerForgetOSSLCipher (ResourceOwner owner, OSSLCipher *od)
 
static void free_openssl_cipher (OSSLCipher *od)
 
static unsigned gen_ossl_block_size (PX_Cipher *c)
 
static unsigned gen_ossl_key_size (PX_Cipher *c)
 
static unsigned gen_ossl_iv_size (PX_Cipher *c)
 
static void gen_ossl_free (PX_Cipher *c)
 
static int gen_ossl_decrypt (PX_Cipher *c, int padding, const uint8 *data, unsigned dlen, uint8 *res, unsigned *rlen)
 
static int gen_ossl_encrypt (PX_Cipher *c, int padding, const uint8 *data, unsigned dlen, uint8 *res, unsigned *rlen)
 
static int bf_check_supported_key_len (void)
 
static int bf_init (PX_Cipher *c, const uint8 *key, unsigned klen, const uint8 *iv)
 
static int ossl_des_init (PX_Cipher *c, const uint8 *key, unsigned klen, const uint8 *iv)
 
static int ossl_des3_init (PX_Cipher *c, const uint8 *key, unsigned klen, const uint8 *iv)
 
static int ossl_cast_init (PX_Cipher *c, const uint8 *key, unsigned klen, const uint8 *iv)
 
static int ossl_aes_init (PX_Cipher *c, const uint8 *key, unsigned klen, const uint8 *iv)
 
static int ossl_aes_ecb_init (PX_Cipher *c, const uint8 *key, unsigned klen, const uint8 *iv)
 
static int ossl_aes_cbc_init (PX_Cipher *c, const uint8 *key, unsigned klen, const uint8 *iv)
 
int px_find_cipher (const char *name, PX_Cipher **res)
 

Variables

static const ResourceOwnerDesc ossldigest_resowner_desc
 
static int px_openssl_initialized = 0
 
static const ResourceOwnerDesc osslcipher_resowner_desc
 
static PX_Alias ossl_aliases []
 
static const struct ossl_cipher ossl_bf_cbc
 
static const struct ossl_cipher ossl_bf_ecb
 
static const struct ossl_cipher ossl_bf_cfb
 
static const struct ossl_cipher ossl_des_ecb
 
static const struct ossl_cipher ossl_des_cbc
 
static const struct ossl_cipher ossl_des3_ecb
 
static const struct ossl_cipher ossl_des3_cbc
 
static const struct ossl_cipher ossl_cast_ecb
 
static const struct ossl_cipher ossl_cast_cbc
 
static const struct ossl_cipher ossl_aes_ecb
 
static const struct ossl_cipher ossl_aes_cbc
 
static const struct ossl_cipher_lookup ossl_cipher_types []
 

Macro Definition Documentation

◆ MAX_IV

#define MAX_IV   (128/8)

Definition at line 46 of file openssl.c.

◆ MAX_KEY

#define MAX_KEY   (512/8)

Definition at line 45 of file openssl.c.

Typedef Documentation

◆ ossl_EVP_cipher_func

typedef const EVP_CIPHER*(* ossl_EVP_cipher_func) (void)

Definition at line 241 of file openssl.c.

◆ OSSLCipher

typedef struct OSSLCipher OSSLCipher

◆ OSSLDigest

typedef struct OSSLDigest OSSLDigest

Function Documentation

◆ bf_check_supported_key_len()

static int bf_check_supported_key_len ( void  )
static

Definition at line 412 of file openssl.c.

413 {
414  static const uint8 key[56] = {
415  0xf0, 0xe1, 0xd2, 0xc3, 0xb4, 0xa5, 0x96, 0x87, 0x78, 0x69,
416  0x5a, 0x4b, 0x3c, 0x2d, 0x1e, 0x0f, 0x00, 0x11, 0x22, 0x33,
417  0x44, 0x55, 0x66, 0x77, 0x04, 0x68, 0x91, 0x04, 0xc2, 0xfd,
418  0x3b, 0x2f, 0x58, 0x40, 0x23, 0x64, 0x1a, 0xba, 0x61, 0x76,
419  0x1f, 0x1f, 0x1f, 0x1f, 0x0e, 0x0e, 0x0e, 0x0e, 0xff, 0xff,
420  0xff, 0xff, 0xff, 0xff, 0xff, 0xff
421  };
422 
423  static const uint8 data[8] = {0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10};
424  static const uint8 res[8] = {0xc0, 0x45, 0x04, 0x01, 0x2e, 0x4e, 0x1f, 0x53};
425  uint8 out[8];
426  EVP_CIPHER_CTX *evp_ctx;
427  int outlen;
428  int status = 0;
429 
430  /* encrypt with 448bits key and verify output */
431  evp_ctx = EVP_CIPHER_CTX_new();
432  if (!evp_ctx)
433  return 0;
434  if (!EVP_EncryptInit_ex(evp_ctx, EVP_bf_ecb(), NULL, NULL, NULL))
435  goto leave;
436  if (!EVP_CIPHER_CTX_set_key_length(evp_ctx, 56))
437  goto leave;
438  if (!EVP_EncryptInit_ex(evp_ctx, NULL, NULL, key, NULL))
439  goto leave;
440 
441  if (!EVP_EncryptUpdate(evp_ctx, out, &outlen, data, 8))
442  goto leave;
443 
444  if (memcmp(out, res, 8) != 0)
445  goto leave; /* Output does not match -> strong cipher is
446  * not supported */
447  status = 1;
448 
449 leave:
450  EVP_CIPHER_CTX_free(evp_ctx);
451  return status;
452 }
unsigned char uint8
Definition: c.h:504
const void * data

References data, sort-test::key, and res.

Referenced by bf_init().

◆ bf_init()

static int bf_init ( PX_Cipher c,
const uint8 key,
unsigned  klen,
const uint8 iv 
)
static

Definition at line 455 of file openssl.c.

456 {
457  OSSLCipher *od = c->ptr;
458  unsigned bs = gen_ossl_block_size(c);
459  static int bf_is_strong = -1;
460 
461  /*
462  * Test if key len is supported. BF_set_key silently cut large keys and it
463  * could be a problem when user transfer encrypted data from one server to
464  * another.
465  */
466 
467  if (bf_is_strong == -1)
468  bf_is_strong = bf_check_supported_key_len();
469 
470  if (!bf_is_strong && klen > 16)
471  return PXE_KEY_TOO_BIG;
472 
473  /* Key len is supported. We can use it. */
474  od->klen = klen;
475  memcpy(od->key, key, klen);
476 
477  if (iv)
478  memcpy(od->iv, iv, bs);
479  else
480  memset(od->iv, 0, bs);
481  return 0;
482 }
static unsigned gen_ossl_block_size(PX_Cipher *c)
Definition: openssl.c:310
static int bf_check_supported_key_len(void)
Definition: openssl.c:412
char * c
#define PXE_KEY_TOO_BIG
Definition: px.h:53
unsigned klen
Definition: openssl.c:267
uint8 iv[MAX_IV]
Definition: openssl.c:266
uint8 key[MAX_KEY]
Definition: openssl.c:265

References bf_check_supported_key_len(), gen_ossl_block_size(), OSSLCipher::iv, OSSLCipher::key, sort-test::key, OSSLCipher::klen, and PXE_KEY_TOO_BIG.

◆ digest_block_size()

static unsigned digest_block_size ( PX_MD h)
static

Definition at line 110 of file openssl.c.

111 {
112  OSSLDigest *digest = (OSSLDigest *) h->p.ptr;
113  int result = EVP_MD_CTX_block_size(digest->ctx);
114 
115  if (result < 0)
116  elog(ERROR, "EVP_MD_CTX_block_size() failed");
117 
118  return result;
119 }
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:224
EVP_MD_CTX * ctx
Definition: openssl.c:59
union px_digest::@8 p
void * ptr
Definition: px.h:111

References OSSLDigest::ctx, elog, ERROR, px_digest::p, and px_digest::ptr.

Referenced by px_find_digest().

◆ digest_finish()

static void digest_finish ( PX_MD h,
uint8 dst 
)
static

Definition at line 140 of file openssl.c.

141 {
142  OSSLDigest *digest = (OSSLDigest *) h->p.ptr;
143 
144  if (!EVP_DigestFinal_ex(digest->ctx, dst, NULL))
145  elog(ERROR, "EVP_DigestFinal_ex() failed");
146 }
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:77

References OSSLDigest::ctx, elog, ERROR, if(), px_digest::p, and px_digest::ptr.

Referenced by px_find_digest().

◆ digest_free()

static void digest_free ( PX_MD h)
static

Definition at line 149 of file openssl.c.

150 {
151  OSSLDigest *digest = (OSSLDigest *) h->p.ptr;
152 
153  free_openssl_digest(digest);
154  pfree(h);
155 }
void pfree(void *pointer)
Definition: mcxt.c:1520
static void free_openssl_digest(OSSLDigest *digest)
Definition: openssl.c:89

References free_openssl_digest(), px_digest::p, pfree(), and px_digest::ptr.

Referenced by px_find_digest().

◆ digest_reset()

static void digest_reset ( PX_MD h)
static

Definition at line 122 of file openssl.c.

123 {
124  OSSLDigest *digest = (OSSLDigest *) h->p.ptr;
125 
126  if (!EVP_DigestInit_ex(digest->ctx, digest->algo, NULL))
127  elog(ERROR, "EVP_DigestInit_ex() failed");
128 }
const EVP_MD * algo
Definition: openssl.c:58

References OSSLDigest::algo, OSSLDigest::ctx, elog, ERROR, if(), px_digest::p, and px_digest::ptr.

Referenced by px_find_digest().

◆ digest_result_size()

static unsigned digest_result_size ( PX_MD h)
static

Definition at line 98 of file openssl.c.

99 {
100  OSSLDigest *digest = (OSSLDigest *) h->p.ptr;
101  int result = EVP_MD_CTX_size(digest->ctx);
102 
103  if (result < 0)
104  elog(ERROR, "EVP_MD_CTX_size() failed");
105 
106  return result;
107 }

References OSSLDigest::ctx, elog, ERROR, px_digest::p, and px_digest::ptr.

Referenced by px_find_digest().

◆ digest_update()

static void digest_update ( PX_MD h,
const uint8 data,
unsigned  dlen 
)
static

Definition at line 131 of file openssl.c.

132 {
133  OSSLDigest *digest = (OSSLDigest *) h->p.ptr;
134 
135  if (!EVP_DigestUpdate(digest->ctx, data, dlen))
136  elog(ERROR, "EVP_DigestUpdate() failed");
137 }

References OSSLDigest::ctx, data, elog, ERROR, if(), px_digest::p, and px_digest::ptr.

Referenced by px_find_digest().

◆ free_openssl_cipher()

static void free_openssl_cipher ( OSSLCipher od)
static

Definition at line 299 of file openssl.c.

300 {
301  EVP_CIPHER_CTX_free(od->evp_ctx);
302  if (od->owner != NULL)
304  pfree(od);
305 }
static void ResourceOwnerForgetOSSLCipher(ResourceOwner owner, OSSLCipher *od)
Definition: openssl.c:293
ResourceOwner owner
Definition: openssl.c:271
EVP_CIPHER_CTX * evp_ctx
Definition: openssl.c:263

References OSSLCipher::evp_ctx, OSSLCipher::owner, pfree(), and ResourceOwnerForgetOSSLCipher().

Referenced by gen_ossl_free(), and ResOwnerReleaseOSSLCipher().

◆ free_openssl_digest()

static void free_openssl_digest ( OSSLDigest digest)
static

Definition at line 89 of file openssl.c.

90 {
91  EVP_MD_CTX_destroy(digest->ctx);
92  if (digest->owner != NULL)
93  ResourceOwnerForgetOSSLDigest(digest->owner, digest);
94  pfree(digest);
95 }
static void ResourceOwnerForgetOSSLDigest(ResourceOwner owner, OSSLDigest *digest)
Definition: openssl.c:83
ResourceOwner owner
Definition: openssl.c:61

References OSSLDigest::ctx, OSSLDigest::owner, pfree(), and ResourceOwnerForgetOSSLDigest().

Referenced by digest_free(), and ResOwnerReleaseOSSLDigest().

◆ gen_ossl_block_size()

static unsigned gen_ossl_block_size ( PX_Cipher c)
static

Definition at line 310 of file openssl.c.

311 {
312  OSSLCipher *od = (OSSLCipher *) c->ptr;
313 
314  return od->ciph->block_size;
315 }
const struct ossl_cipher * ciph
Definition: openssl.c:269
int block_size
Definition: openssl.c:250

References ossl_cipher::block_size, and OSSLCipher::ciph.

Referenced by bf_init(), ossl_aes_init(), ossl_cast_init(), ossl_des3_init(), ossl_des_init(), and px_find_cipher().

◆ gen_ossl_decrypt()

static int gen_ossl_decrypt ( PX_Cipher c,
int  padding,
const uint8 data,
unsigned  dlen,
uint8 res,
unsigned *  rlen 
)
static

Definition at line 345 of file openssl.c.

347 {
348  OSSLCipher *od = c->ptr;
349  int outlen,
350  outlen2;
351 
352  if (!od->init)
353  {
354  if (!EVP_DecryptInit_ex(od->evp_ctx, od->evp_ciph, NULL, NULL, NULL))
355  return PXE_CIPHER_INIT;
356  if (!EVP_CIPHER_CTX_set_padding(od->evp_ctx, padding))
357  return PXE_CIPHER_INIT;
358  if (!EVP_CIPHER_CTX_set_key_length(od->evp_ctx, od->klen))
359  return PXE_CIPHER_INIT;
360  if (!EVP_DecryptInit_ex(od->evp_ctx, NULL, NULL, od->key, od->iv))
361  return PXE_CIPHER_INIT;
362  od->init = true;
363  }
364 
365  if (!EVP_DecryptUpdate(od->evp_ctx, res, &outlen, data, dlen))
366  return PXE_DECRYPT_FAILED;
367  if (!EVP_DecryptFinal_ex(od->evp_ctx, res + outlen, &outlen2))
368  return PXE_DECRYPT_FAILED;
369  *rlen = outlen + outlen2;
370 
371  return 0;
372 }
#define PXE_DECRYPT_FAILED
Definition: px.h:64
#define PXE_CIPHER_INIT
Definition: px.h:54
unsigned init
Definition: openssl.c:268
const EVP_CIPHER * evp_ciph
Definition: openssl.c:264

References data, OSSLCipher::evp_ciph, OSSLCipher::evp_ctx, OSSLCipher::init, OSSLCipher::iv, OSSLCipher::key, OSSLCipher::klen, PXE_CIPHER_INIT, PXE_DECRYPT_FAILED, and res.

Referenced by px_find_cipher().

◆ gen_ossl_encrypt()

static int gen_ossl_encrypt ( PX_Cipher c,
int  padding,
const uint8 data,
unsigned  dlen,
uint8 res,
unsigned *  rlen 
)
static

Definition at line 375 of file openssl.c.

377 {
378  OSSLCipher *od = c->ptr;
379  int outlen,
380  outlen2;
381 
382  if (!od->init)
383  {
384  if (!EVP_EncryptInit_ex(od->evp_ctx, od->evp_ciph, NULL, NULL, NULL))
385  return PXE_CIPHER_INIT;
386  if (!EVP_CIPHER_CTX_set_padding(od->evp_ctx, padding))
387  return PXE_CIPHER_INIT;
388  if (!EVP_CIPHER_CTX_set_key_length(od->evp_ctx, od->klen))
389  return PXE_CIPHER_INIT;
390  if (!EVP_EncryptInit_ex(od->evp_ctx, NULL, NULL, od->key, od->iv))
391  return PXE_CIPHER_INIT;
392  od->init = true;
393  }
394 
395  if (!EVP_EncryptUpdate(od->evp_ctx, res, &outlen, data, dlen))
396  return PXE_ENCRYPT_FAILED;
397  if (!EVP_EncryptFinal_ex(od->evp_ctx, res + outlen, &outlen2))
398  return PXE_ENCRYPT_FAILED;
399  *rlen = outlen + outlen2;
400 
401  return 0;
402 }
#define PXE_ENCRYPT_FAILED
Definition: px.h:65

References data, OSSLCipher::evp_ciph, OSSLCipher::evp_ctx, OSSLCipher::init, OSSLCipher::iv, OSSLCipher::key, OSSLCipher::klen, PXE_CIPHER_INIT, PXE_ENCRYPT_FAILED, and res.

Referenced by px_find_cipher().

◆ gen_ossl_free()

static void gen_ossl_free ( PX_Cipher c)
static

Definition at line 336 of file openssl.c.

337 {
338  OSSLCipher *od = (OSSLCipher *) c->ptr;
339 
341  pfree(c);
342 }
static void free_openssl_cipher(OSSLCipher *od)
Definition: openssl.c:299

References free_openssl_cipher(), and pfree().

Referenced by px_find_cipher().

◆ gen_ossl_iv_size()

static unsigned gen_ossl_iv_size ( PX_Cipher c)
static

Definition at line 326 of file openssl.c.

327 {
328  unsigned ivlen;
329  OSSLCipher *od = (OSSLCipher *) c->ptr;
330 
331  ivlen = od->ciph->block_size;
332  return ivlen;
333 }

References ossl_cipher::block_size, and OSSLCipher::ciph.

Referenced by px_find_cipher().

◆ gen_ossl_key_size()

static unsigned gen_ossl_key_size ( PX_Cipher c)
static

Definition at line 318 of file openssl.c.

319 {
320  OSSLCipher *od = (OSSLCipher *) c->ptr;
321 
322  return od->ciph->max_key_size;
323 }
int max_key_size
Definition: openssl.c:251

References OSSLCipher::ciph, and ossl_cipher::max_key_size.

Referenced by px_find_cipher().

◆ ossl_aes_cbc_init()

static int ossl_aes_cbc_init ( PX_Cipher c,
const uint8 key,
unsigned  klen,
const uint8 iv 
)
static

Definition at line 598 of file openssl.c.

599 {
600  OSSLCipher *od = c->ptr;
601  int err;
602 
603  err = ossl_aes_init(c, key, klen, iv);
604  if (err)
605  return err;
606 
607  switch (od->klen)
608  {
609  case 128 / 8:
610  od->evp_ciph = EVP_aes_128_cbc();
611  break;
612  case 192 / 8:
613  od->evp_ciph = EVP_aes_192_cbc();
614  break;
615  case 256 / 8:
616  od->evp_ciph = EVP_aes_256_cbc();
617  break;
618  default:
619  /* shouldn't happen */
621  break;
622  }
623 
624  return err;
625 }
void err(int eval, const char *fmt,...)
Definition: err.c:43
static int ossl_aes_init(PX_Cipher *c, const uint8 *key, unsigned klen, const uint8 *iv)
Definition: openssl.c:543

References err(), OSSLCipher::evp_ciph, sort-test::key, OSSLCipher::klen, ossl_aes_init(), and PXE_CIPHER_INIT.

◆ ossl_aes_ecb_init()

static int ossl_aes_ecb_init ( PX_Cipher c,
const uint8 key,
unsigned  klen,
const uint8 iv 
)
static

Definition at line 568 of file openssl.c.

569 {
570  OSSLCipher *od = c->ptr;
571  int err;
572 
573  err = ossl_aes_init(c, key, klen, iv);
574  if (err)
575  return err;
576 
577  switch (od->klen)
578  {
579  case 128 / 8:
580  od->evp_ciph = EVP_aes_128_ecb();
581  break;
582  case 192 / 8:
583  od->evp_ciph = EVP_aes_192_ecb();
584  break;
585  case 256 / 8:
586  od->evp_ciph = EVP_aes_256_ecb();
587  break;
588  default:
589  /* shouldn't happen */
591  break;
592  }
593 
594  return err;
595 }

References err(), OSSLCipher::evp_ciph, sort-test::key, OSSLCipher::klen, ossl_aes_init(), and PXE_CIPHER_INIT.

◆ ossl_aes_init()

static int ossl_aes_init ( PX_Cipher c,
const uint8 key,
unsigned  klen,
const uint8 iv 
)
static

Definition at line 543 of file openssl.c.

544 {
545  OSSLCipher *od = c->ptr;
546  unsigned bs = gen_ossl_block_size(c);
547 
548  if (klen <= 128 / 8)
549  od->klen = 128 / 8;
550  else if (klen <= 192 / 8)
551  od->klen = 192 / 8;
552  else if (klen <= 256 / 8)
553  od->klen = 256 / 8;
554  else
555  return PXE_KEY_TOO_BIG;
556 
557  memcpy(od->key, key, klen);
558 
559  if (iv)
560  memcpy(od->iv, iv, bs);
561  else
562  memset(od->iv, 0, bs);
563 
564  return 0;
565 }

References gen_ossl_block_size(), OSSLCipher::iv, OSSLCipher::key, sort-test::key, OSSLCipher::klen, and PXE_KEY_TOO_BIG.

Referenced by ossl_aes_cbc_init(), and ossl_aes_ecb_init().

◆ ossl_cast_init()

static int ossl_cast_init ( PX_Cipher c,
const uint8 key,
unsigned  klen,
const uint8 iv 
)
static

Definition at line 525 of file openssl.c.

526 {
527  OSSLCipher *od = c->ptr;
528  unsigned bs = gen_ossl_block_size(c);
529 
530  od->klen = klen;
531  memcpy(od->key, key, klen);
532 
533  if (iv)
534  memcpy(od->iv, iv, bs);
535  else
536  memset(od->iv, 0, bs);
537  return 0;
538 }

References gen_ossl_block_size(), OSSLCipher::iv, OSSLCipher::key, sort-test::key, and OSSLCipher::klen.

◆ ossl_des3_init()

static int ossl_des3_init ( PX_Cipher c,
const uint8 key,
unsigned  klen,
const uint8 iv 
)
static

Definition at line 506 of file openssl.c.

507 {
508  OSSLCipher *od = c->ptr;
509  unsigned bs = gen_ossl_block_size(c);
510 
511  od->klen = 24;
512  memset(od->key, 0, 24);
513  memcpy(od->key, key, klen > 24 ? 24 : klen);
514 
515  if (iv)
516  memcpy(od->iv, iv, bs);
517  else
518  memset(od->iv, 0, bs);
519  return 0;
520 }

References gen_ossl_block_size(), OSSLCipher::iv, OSSLCipher::key, sort-test::key, and OSSLCipher::klen.

◆ ossl_des_init()

static int ossl_des_init ( PX_Cipher c,
const uint8 key,
unsigned  klen,
const uint8 iv 
)
static

Definition at line 487 of file openssl.c.

488 {
489  OSSLCipher *od = c->ptr;
490  unsigned bs = gen_ossl_block_size(c);
491 
492  od->klen = 8;
493  memset(od->key, 0, 8);
494  memcpy(od->key, key, klen > 8 ? 8 : klen);
495 
496  if (iv)
497  memcpy(od->iv, iv, bs);
498  else
499  memset(od->iv, 0, bs);
500  return 0;
501 }

References gen_ossl_block_size(), OSSLCipher::iv, OSSLCipher::key, sort-test::key, and OSSLCipher::klen.

◆ px_find_cipher()

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

Definition at line 744 of file openssl.c.

745 {
746  const struct ossl_cipher_lookup *i;
747  PX_Cipher *c = NULL;
748  EVP_CIPHER_CTX *ctx;
749  OSSLCipher *od;
750 
752  for (i = ossl_cipher_types; i->name; i++)
753  if (strcmp(i->name, name) == 0)
754  break;
755  if (i->name == NULL)
756  return PXE_NO_CIPHER;
757 
759 
760  /*
761  * Create an OSSLCipher object, an EVP_CIPHER_CTX object and a PX_Cipher.
762  * The order is crucial, to make sure we don't leak anything on
763  * out-of-memory or other error.
764  */
765  od = MemoryContextAllocZero(TopMemoryContext, sizeof(*od));
766  od->ciph = i->ciph;
767 
768  /* Allocate an EVP_CIPHER_CTX object. */
769  ctx = EVP_CIPHER_CTX_new();
770  if (!ctx)
771  {
772  pfree(od);
773  return PXE_CIPHER_INIT;
774  }
775 
776  od->evp_ctx = ctx;
779 
780  if (i->ciph->cipher_func)
781  od->evp_ciph = i->ciph->cipher_func();
782 
783  /* The PX_Cipher is allocated in current memory context */
784  c = palloc(sizeof(*c));
785  c->block_size = gen_ossl_block_size;
786  c->key_size = gen_ossl_key_size;
787  c->iv_size = gen_ossl_iv_size;
788  c->free = gen_ossl_free;
789  c->init = od->ciph->init;
790  c->encrypt = gen_ossl_encrypt;
791  c->decrypt = gen_ossl_decrypt;
792  c->ptr = od;
793 
794  *res = c;
795  return 0;
796 }
int i
Definition: isn.c:73
MemoryContext TopMemoryContext
Definition: mcxt.c:149
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition: mcxt.c:1214
void * palloc(Size size)
Definition: mcxt.c:1316
static unsigned gen_ossl_key_size(PX_Cipher *c)
Definition: openssl.c:318
static int gen_ossl_encrypt(PX_Cipher *c, int padding, const uint8 *data, unsigned dlen, uint8 *res, unsigned *rlen)
Definition: openssl.c:375
static int gen_ossl_decrypt(PX_Cipher *c, int padding, const uint8 *data, unsigned dlen, uint8 *res, unsigned *rlen)
Definition: openssl.c:345
static void gen_ossl_free(PX_Cipher *c)
Definition: openssl.c:336
static unsigned gen_ossl_iv_size(PX_Cipher *c)
Definition: openssl.c:326
static PX_Alias ossl_aliases[]
Definition: openssl.c:631
static const struct ossl_cipher_lookup ossl_cipher_types[]
Definition: openssl.c:726
static void ResourceOwnerRememberOSSLCipher(ResourceOwner owner, OSSLCipher *od)
Definition: openssl.c:288
const char * px_resolve_alias(const PX_Alias *list, const char *name)
Definition: px.c:129
#define PXE_NO_CIPHER
Definition: px.h:49
ResourceOwner CurrentResourceOwner
Definition: resowner.c:165
void ResourceOwnerEnlarge(ResourceOwner owner)
Definition: resowner.c:442
int(* init)(PX_Cipher *c, const uint8 *key, unsigned klen, const uint8 *iv)
Definition: openssl.c:248
Definition: px.h:141
const char * name

References OSSLCipher::ciph, 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, ossl_aliases, ossl_cipher_types, OSSLCipher::owner, palloc(), pfree(), px_resolve_alias(), PXE_CIPHER_INIT, PXE_NO_CIPHER, res, ResourceOwnerEnlarge(), ResourceOwnerRememberOSSLCipher(), and TopMemoryContext.

Referenced by pgp_load_cipher(), and px_find_combo().

◆ px_find_digest()

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

Definition at line 162 of file openssl.c.

163 {
164  const EVP_MD *md;
165  EVP_MD_CTX *ctx;
166  PX_MD *h;
167  OSSLDigest *digest;
168 
170  {
172  OpenSSL_add_all_algorithms();
173  }
174 
175  md = EVP_get_digestbyname(name);
176  if (md == NULL)
177  return PXE_NO_HASH;
178 
180 
181  /*
182  * Create an OSSLDigest object, an OpenSSL MD object, and a PX_MD object.
183  * The order is crucial, to make sure we don't leak anything on
184  * out-of-memory or other error.
185  */
186  digest = MemoryContextAlloc(TopMemoryContext, sizeof(*digest));
187 
188  ctx = EVP_MD_CTX_create();
189  if (!ctx)
190  {
191  pfree(digest);
192  return PXE_CIPHER_INIT;
193  }
194  if (EVP_DigestInit_ex(ctx, md, NULL) == 0)
195  {
196  EVP_MD_CTX_destroy(ctx);
197  pfree(digest);
198  return PXE_CIPHER_INIT;
199  }
200 
201  digest->algo = md;
202  digest->ctx = ctx;
203  digest->owner = CurrentResourceOwner;
204  ResourceOwnerRememberOSSLDigest(digest->owner, digest);
205 
206  /* The PX_MD object is allocated in the current memory context. */
207  h = palloc(sizeof(*h));
210  h->reset = digest_reset;
211  h->update = digest_update;
212  h->finish = digest_finish;
213  h->free = digest_free;
214  h->p.ptr = (void *) digest;
215 
216  *res = h;
217  return 0;
218 }
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:1180
static void digest_update(PX_MD *h, const uint8 *data, unsigned dlen)
Definition: openssl.c:131
static unsigned digest_result_size(PX_MD *h)
Definition: openssl.c:98
static void ResourceOwnerRememberOSSLDigest(ResourceOwner owner, OSSLDigest *digest)
Definition: openssl.c:78
static void digest_finish(PX_MD *h, uint8 *dst)
Definition: openssl.c:140
static int px_openssl_initialized
Definition: openssl.c:157
static void digest_reset(PX_MD *h)
Definition: openssl.c:122
static unsigned digest_block_size(PX_MD *h)
Definition: openssl.c:110
static void digest_free(PX_MD *h)
Definition: openssl.c:149
#define PXE_NO_HASH
Definition: px.h:48
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
void(* reset)(PX_MD *h)
Definition: px.h:103
unsigned(* block_size)(PX_MD *h)
Definition: px.h:102
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_reset(), digest_result_size(), digest_update(), px_digest::finish, px_digest::free, MemoryContextAlloc(), name, OSSLDigest::owner, px_digest::p, palloc(), pfree(), px_digest::ptr, px_openssl_initialized, PXE_CIPHER_INIT, PXE_NO_HASH, res, px_digest::reset, ResourceOwnerEnlarge(), ResourceOwnerRememberOSSLDigest(), px_digest::result_size, TopMemoryContext, and px_digest::update.

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

◆ ResourceOwnerForgetOSSLCipher()

static void ResourceOwnerForgetOSSLCipher ( ResourceOwner  owner,
OSSLCipher od 
)
inlinestatic

Definition at line 293 of file openssl.c.

294 {
296 }
static const ResourceOwnerDesc osslcipher_resowner_desc
Definition: openssl.c:277
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:322
void ResourceOwnerForget(ResourceOwner owner, Datum value, const ResourceOwnerDesc *kind)
Definition: resowner.c:554

References osslcipher_resowner_desc, PointerGetDatum(), and ResourceOwnerForget().

Referenced by free_openssl_cipher().

◆ ResourceOwnerForgetOSSLDigest()

static void ResourceOwnerForgetOSSLDigest ( ResourceOwner  owner,
OSSLDigest digest 
)
inlinestatic

Definition at line 83 of file openssl.c.

84 {
86 }
static const ResourceOwnerDesc ossldigest_resowner_desc
Definition: openssl.c:67

References ossldigest_resowner_desc, PointerGetDatum(), and ResourceOwnerForget().

Referenced by free_openssl_digest().

◆ ResourceOwnerRememberOSSLCipher()

static void ResourceOwnerRememberOSSLCipher ( ResourceOwner  owner,
OSSLCipher od 
)
inlinestatic

Definition at line 288 of file openssl.c.

289 {
291 }
void ResourceOwnerRemember(ResourceOwner owner, Datum value, const ResourceOwnerDesc *kind)
Definition: resowner.c:514

References osslcipher_resowner_desc, PointerGetDatum(), and ResourceOwnerRemember().

Referenced by px_find_cipher().

◆ ResourceOwnerRememberOSSLDigest()

static void ResourceOwnerRememberOSSLDigest ( ResourceOwner  owner,
OSSLDigest digest 
)
inlinestatic

Definition at line 78 of file openssl.c.

79 {
81 }

References ossldigest_resowner_desc, PointerGetDatum(), and ResourceOwnerRemember().

Referenced by px_find_digest().

◆ ResOwnerReleaseOSSLCipher()

static void ResOwnerReleaseOSSLCipher ( Datum  res)
static

Definition at line 801 of file openssl.c.

802 {
804 }
static Pointer DatumGetPointer(Datum X)
Definition: postgres.h:312

References DatumGetPointer(), free_openssl_cipher(), and res.

◆ ResOwnerReleaseOSSLDigest()

static void ResOwnerReleaseOSSLDigest ( Datum  res)
static

Definition at line 223 of file openssl.c.

224 {
225  OSSLDigest *digest = (OSSLDigest *) DatumGetPointer(res);
226 
227  digest->owner = NULL;
228  free_openssl_digest(digest);
229 }

References DatumGetPointer(), free_openssl_digest(), OSSLDigest::owner, and res.

Variable Documentation

◆ ossl_aes_cbc

const struct ossl_cipher ossl_aes_cbc
static
Initial value:
= {
NULL,
128 / 8, 256 / 8
}
static int ossl_aes_cbc_init(PX_Cipher *c, const uint8 *key, unsigned klen, const uint8 *iv)
Definition: openssl.c:598

Definition at line 631 of file openssl.c.

◆ ossl_aes_ecb

const struct ossl_cipher ossl_aes_ecb
static
Initial value:
= {
NULL,
128 / 8, 256 / 8
}
static int ossl_aes_ecb_init(PX_Cipher *c, const uint8 *key, unsigned klen, const uint8 *iv)
Definition: openssl.c:568

Definition at line 631 of file openssl.c.

◆ ossl_aliases

PX_Alias ossl_aliases[]
static
Initial value:
= {
{"bf", "bf-cbc"},
{"blowfish", "bf-cbc"},
{"blowfish-cbc", "bf-cbc"},
{"blowfish-ecb", "bf-ecb"},
{"blowfish-cfb", "bf-cfb"},
{"des", "des-cbc"},
{"3des", "des3-cbc"},
{"3des-ecb", "des3-ecb"},
{"3des-cbc", "des3-cbc"},
{"cast5", "cast5-cbc"},
{"aes", "aes-cbc"},
{"rijndael", "aes-cbc"},
{"rijndael-cbc", "aes-cbc"},
{"rijndael-ecb", "aes-ecb"},
{NULL}
}

Definition at line 631 of file openssl.c.

Referenced by px_find_cipher().

◆ ossl_bf_cbc

const struct ossl_cipher ossl_bf_cbc
static
Initial value:
= {
EVP_bf_cbc,
64 / 8, 448 / 8
}
static int bf_init(PX_Cipher *c, const uint8 *key, unsigned klen, const uint8 *iv)
Definition: openssl.c:455

Definition at line 631 of file openssl.c.

◆ ossl_bf_cfb

const struct ossl_cipher ossl_bf_cfb
static
Initial value:
= {
EVP_bf_cfb,
64 / 8, 448 / 8
}

Definition at line 631 of file openssl.c.

◆ ossl_bf_ecb

const struct ossl_cipher ossl_bf_ecb
static
Initial value:
= {
EVP_bf_ecb,
64 / 8, 448 / 8
}

Definition at line 631 of file openssl.c.

◆ ossl_cast_cbc

const struct ossl_cipher ossl_cast_cbc
static
Initial value:
= {
EVP_cast5_cbc,
64 / 8, 128 / 8
}
static int ossl_cast_init(PX_Cipher *c, const uint8 *key, unsigned klen, const uint8 *iv)
Definition: openssl.c:525

Definition at line 631 of file openssl.c.

◆ ossl_cast_ecb

const struct ossl_cipher ossl_cast_ecb
static
Initial value:
= {
EVP_cast5_ecb,
64 / 8, 128 / 8
}

Definition at line 631 of file openssl.c.

◆ ossl_cipher_types

const struct ossl_cipher_lookup ossl_cipher_types[]
static
Initial value:
= {
{"bf-cbc", &ossl_bf_cbc},
{"bf-ecb", &ossl_bf_ecb},
{"bf-cfb", &ossl_bf_cfb},
{"des-ecb", &ossl_des_ecb},
{"des-cbc", &ossl_des_cbc},
{"des3-ecb", &ossl_des3_ecb},
{"des3-cbc", &ossl_des3_cbc},
{"cast5-ecb", &ossl_cast_ecb},
{"cast5-cbc", &ossl_cast_cbc},
{"aes-ecb", &ossl_aes_ecb},
{"aes-cbc", &ossl_aes_cbc},
{NULL}
}
static const struct ossl_cipher ossl_des_ecb
Definition: openssl.c:667
static const struct ossl_cipher ossl_des3_cbc
Definition: openssl.c:685
static const struct ossl_cipher ossl_aes_ecb
Definition: openssl.c:703
static const struct ossl_cipher ossl_des3_ecb
Definition: openssl.c:679
static const struct ossl_cipher ossl_bf_cfb
Definition: openssl.c:661
static const struct ossl_cipher ossl_cast_cbc
Definition: openssl.c:697
static const struct ossl_cipher ossl_des_cbc
Definition: openssl.c:673
static const struct ossl_cipher ossl_bf_ecb
Definition: openssl.c:655
static const struct ossl_cipher ossl_bf_cbc
Definition: openssl.c:649
static const struct ossl_cipher ossl_cast_ecb
Definition: openssl.c:691
static const struct ossl_cipher ossl_aes_cbc
Definition: openssl.c:710

Definition at line 631 of file openssl.c.

Referenced by px_find_cipher().

◆ ossl_des3_cbc

const struct ossl_cipher ossl_des3_cbc
static
Initial value:
= {
EVP_des_ede3_cbc,
64 / 8, 192 / 8
}
static int ossl_des3_init(PX_Cipher *c, const uint8 *key, unsigned klen, const uint8 *iv)
Definition: openssl.c:506

Definition at line 631 of file openssl.c.

◆ ossl_des3_ecb

const struct ossl_cipher ossl_des3_ecb
static
Initial value:
= {
EVP_des_ede3_ecb,
64 / 8, 192 / 8
}

Definition at line 631 of file openssl.c.

◆ ossl_des_cbc

const struct ossl_cipher ossl_des_cbc
static
Initial value:
= {
EVP_des_cbc,
64 / 8, 64 / 8
}
static int ossl_des_init(PX_Cipher *c, const uint8 *key, unsigned klen, const uint8 *iv)
Definition: openssl.c:487

Definition at line 631 of file openssl.c.

◆ ossl_des_ecb

const struct ossl_cipher ossl_des_ecb
static
Initial value:
= {
EVP_des_ecb,
64 / 8, 64 / 8
}

Definition at line 631 of file openssl.c.

◆ osslcipher_resowner_desc

const ResourceOwnerDesc osslcipher_resowner_desc
static
Initial value:
=
{
.name = "pgcrypto OpenSSL cipher handle",
.release_priority = RELEASE_PRIO_FIRST,
.ReleaseResource = ResOwnerReleaseOSSLCipher,
.DebugPrint = NULL,
}
static void ResOwnerReleaseOSSLCipher(Datum res)
Definition: openssl.c:801
@ RESOURCE_RELEASE_BEFORE_LOCKS
Definition: resowner.h:54
#define RELEASE_PRIO_FIRST
Definition: resowner.h:80

Definition at line 277 of file openssl.c.

Referenced by ResourceOwnerForgetOSSLCipher(), and ResourceOwnerRememberOSSLCipher().

◆ ossldigest_resowner_desc

const ResourceOwnerDesc ossldigest_resowner_desc
static
Initial value:
=
{
.name = "pgcrypto OpenSSL digest handle",
.release_priority = RELEASE_PRIO_FIRST,
.ReleaseResource = ResOwnerReleaseOSSLDigest,
.DebugPrint = NULL,
}
static void ResOwnerReleaseOSSLDigest(Datum res)
Definition: openssl.c:223

Definition at line 67 of file openssl.c.

Referenced by ResourceOwnerForgetOSSLDigest(), and ResourceOwnerRememberOSSLDigest().

◆ px_openssl_initialized

int px_openssl_initialized = 0
static

Definition at line 157 of file openssl.c.

Referenced by px_find_digest().