PostgreSQL Source Code  git master
openssl.c
Go to the documentation of this file.
1 /*
2  * openssl.c
3  * Wrapper for OpenSSL library.
4  *
5  * Copyright (c) 2001 Marko Kreen
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in the
15  * documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * contrib/pgcrypto/openssl.c
30  */
31 
32 #include "postgres.h"
33 
34 #include <openssl/evp.h>
35 #include <openssl/err.h>
36 #include <openssl/rand.h>
37 
38 #include "px.h"
39 #include "utils/memutils.h"
40 #include "utils/resowner.h"
41 
42 /*
43  * Max lengths we might want to handle.
44  */
45 #define MAX_KEY (512/8)
46 #define MAX_IV (128/8)
47 
48 /*
49  * Hashes
50  */
51 
52 /*
53  * To make sure we don't leak OpenSSL handles, we use the ResourceOwner
54  * mechanism to free them on abort.
55  */
56 typedef struct OSSLDigest
57 {
58  const EVP_MD *algo;
59  EVP_MD_CTX *ctx;
60 
63 
64 /* ResourceOwner callbacks to hold OpenSSL digest handles */
66 
68 {
69  .name = "pgcrypto OpenSSL digest handle",
70  .release_phase = RESOURCE_RELEASE_BEFORE_LOCKS,
71  .release_priority = RELEASE_PRIO_FIRST,
72  .ReleaseResource = ResOwnerReleaseOSSLDigest,
73  .DebugPrint = NULL, /* default message is fine */
74 };
75 
76 /* Convenience wrappers over ResourceOwnerRemember/Forget */
77 static inline void
79 {
81 }
82 static inline void
84 {
86 }
87 
88 static void
90 {
91  EVP_MD_CTX_destroy(digest->ctx);
92  if (digest->owner != NULL)
93  ResourceOwnerForgetOSSLDigest(digest->owner, digest);
94  pfree(digest);
95 }
96 
97 static unsigned
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 }
108 
109 static unsigned
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 }
120 
121 static void
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 }
129 
130 static void
131 digest_update(PX_MD *h, const uint8 *data, unsigned dlen)
132 {
133  OSSLDigest *digest = (OSSLDigest *) h->p.ptr;
134 
135  if (!EVP_DigestUpdate(digest->ctx, data, dlen))
136  elog(ERROR, "EVP_DigestUpdate() failed");
137 }
138 
139 static void
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 }
147 
148 static void
150 {
151  OSSLDigest *digest = (OSSLDigest *) h->p.ptr;
152 
153  free_openssl_digest(digest);
154  pfree(h);
155 }
156 
157 static int px_openssl_initialized = 0;
158 
159 /* PUBLIC functions */
160 
161 int
162 px_find_digest(const char *name, PX_MD **res)
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 }
219 
220 /* ResourceOwner callbacks for OSSLDigest */
221 
222 static void
224 {
225  OSSLDigest *digest = (OSSLDigest *) DatumGetPointer(res);
226 
227  digest->owner = NULL;
228  free_openssl_digest(digest);
229 }
230 
231 /*
232  * Ciphers
233  *
234  * We use OpenSSL's EVP* family of functions for these.
235  */
236 
237 /*
238  * prototype for the EVP functions that return an algorithm, e.g.
239  * EVP_aes_128_cbc().
240  */
241 typedef const EVP_CIPHER *(*ossl_EVP_cipher_func) (void);
242 
243 /*
244  * ossl_cipher contains the static information about each cipher.
245  */
247 {
248  int (*init) (PX_Cipher *c, const uint8 *key, unsigned klen, const uint8 *iv);
252 };
253 
254 /*
255  * OSSLCipher contains the state for using a cipher. A separate OSSLCipher
256  * object is allocated in each px_find_cipher() call.
257  *
258  * To make sure we don't leak OpenSSL handles, we use the ResourceOwner
259  * mechanism to free them on abort.
260  */
261 typedef struct OSSLCipher
262 {
263  EVP_CIPHER_CTX *evp_ctx;
264  const EVP_CIPHER *evp_ciph;
267  unsigned klen;
268  unsigned init;
269  const struct ossl_cipher *ciph;
270 
273 
274 /* ResourceOwner callbacks to hold OpenSSL cipher state */
275 static void ResOwnerReleaseOSSLCipher(Datum res);
276 
278 {
279  .name = "pgcrypto OpenSSL cipher handle",
280  .release_phase = RESOURCE_RELEASE_BEFORE_LOCKS,
281  .release_priority = RELEASE_PRIO_FIRST,
282  .ReleaseResource = ResOwnerReleaseOSSLCipher,
283  .DebugPrint = NULL, /* default message is fine */
284 };
285 
286 /* Convenience wrappers over ResourceOwnerRemember/Forget */
287 static inline void
289 {
291 }
292 static inline void
294 {
296 }
297 
298 static void
300 {
301  EVP_CIPHER_CTX_free(od->evp_ctx);
302  if (od->owner != NULL)
304  pfree(od);
305 }
306 
307 /* Common routines for all algorithms */
308 
309 static unsigned
311 {
312  OSSLCipher *od = (OSSLCipher *) c->ptr;
313 
314  return od->ciph->block_size;
315 }
316 
317 static unsigned
319 {
320  OSSLCipher *od = (OSSLCipher *) c->ptr;
321 
322  return od->ciph->max_key_size;
323 }
324 
325 static unsigned
327 {
328  unsigned ivlen;
329  OSSLCipher *od = (OSSLCipher *) c->ptr;
330 
331  ivlen = od->ciph->block_size;
332  return ivlen;
333 }
334 
335 static void
337 {
338  OSSLCipher *od = (OSSLCipher *) c->ptr;
339 
341  pfree(c);
342 }
343 
344 static int
345 gen_ossl_decrypt(PX_Cipher *c, int padding, const uint8 *data, unsigned dlen,
346  uint8 *res, unsigned *rlen)
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 }
373 
374 static int
375 gen_ossl_encrypt(PX_Cipher *c, int padding, const uint8 *data, unsigned dlen,
376  uint8 *res, unsigned *rlen)
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 }
403 
404 /* Blowfish */
405 
406 /*
407  * Check if strong crypto is supported. Some OpenSSL installations
408  * support only short keys and unfortunately BF_set_key does not return any
409  * error value. This function tests if is possible to use strong key.
410  */
411 static int
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 }
453 
454 static int
455 bf_init(PX_Cipher *c, const uint8 *key, unsigned klen, const uint8 *iv)
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 }
483 
484 /* DES */
485 
486 static int
487 ossl_des_init(PX_Cipher *c, const uint8 *key, unsigned klen, const uint8 *iv)
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 }
502 
503 /* DES3 */
504 
505 static int
506 ossl_des3_init(PX_Cipher *c, const uint8 *key, unsigned klen, const uint8 *iv)
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 }
521 
522 /* CAST5 */
523 
524 static int
525 ossl_cast_init(PX_Cipher *c, const uint8 *key, unsigned klen, const uint8 *iv)
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 }
539 
540 /* AES */
541 
542 static int
543 ossl_aes_init(PX_Cipher *c, const uint8 *key, unsigned klen, const uint8 *iv)
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 }
566 
567 static int
568 ossl_aes_ecb_init(PX_Cipher *c, const uint8 *key, unsigned klen, const uint8 *iv)
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 }
596 
597 static int
598 ossl_aes_cbc_init(PX_Cipher *c, const uint8 *key, unsigned klen, const uint8 *iv)
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 }
626 
627 /*
628  * aliases
629  */
630 
631 static PX_Alias ossl_aliases[] = {
632  {"bf", "bf-cbc"},
633  {"blowfish", "bf-cbc"},
634  {"blowfish-cbc", "bf-cbc"},
635  {"blowfish-ecb", "bf-ecb"},
636  {"blowfish-cfb", "bf-cfb"},
637  {"des", "des-cbc"},
638  {"3des", "des3-cbc"},
639  {"3des-ecb", "des3-ecb"},
640  {"3des-cbc", "des3-cbc"},
641  {"cast5", "cast5-cbc"},
642  {"aes", "aes-cbc"},
643  {"rijndael", "aes-cbc"},
644  {"rijndael-cbc", "aes-cbc"},
645  {"rijndael-ecb", "aes-ecb"},
646  {NULL}
647 };
648 
649 static const struct ossl_cipher ossl_bf_cbc = {
650  bf_init,
651  EVP_bf_cbc,
652  64 / 8, 448 / 8
653 };
654 
655 static const struct ossl_cipher ossl_bf_ecb = {
656  bf_init,
657  EVP_bf_ecb,
658  64 / 8, 448 / 8
659 };
660 
661 static const struct ossl_cipher ossl_bf_cfb = {
662  bf_init,
663  EVP_bf_cfb,
664  64 / 8, 448 / 8
665 };
666 
667 static const struct ossl_cipher ossl_des_ecb = {
669  EVP_des_ecb,
670  64 / 8, 64 / 8
671 };
672 
673 static const struct ossl_cipher ossl_des_cbc = {
675  EVP_des_cbc,
676  64 / 8, 64 / 8
677 };
678 
679 static const struct ossl_cipher ossl_des3_ecb = {
681  EVP_des_ede3_ecb,
682  64 / 8, 192 / 8
683 };
684 
685 static const struct ossl_cipher ossl_des3_cbc = {
687  EVP_des_ede3_cbc,
688  64 / 8, 192 / 8
689 };
690 
691 static const struct ossl_cipher ossl_cast_ecb = {
693  EVP_cast5_ecb,
694  64 / 8, 128 / 8
695 };
696 
697 static const struct ossl_cipher ossl_cast_cbc = {
699  EVP_cast5_cbc,
700  64 / 8, 128 / 8
701 };
702 
703 static const struct ossl_cipher ossl_aes_ecb = {
705  NULL, /* EVP_aes_XXX_ecb(), determined in init
706  * function */
707  128 / 8, 256 / 8
708 };
709 
710 static const struct ossl_cipher ossl_aes_cbc = {
712  NULL, /* EVP_aes_XXX_cbc(), determined in init
713  * function */
714  128 / 8, 256 / 8
715 };
716 
717 /*
718  * Special handlers
719  */
721 {
722  const char *name;
723  const struct ossl_cipher *ciph;
724 };
725 
726 static const struct ossl_cipher_lookup ossl_cipher_types[] = {
727  {"bf-cbc", &ossl_bf_cbc},
728  {"bf-ecb", &ossl_bf_ecb},
729  {"bf-cfb", &ossl_bf_cfb},
730  {"des-ecb", &ossl_des_ecb},
731  {"des-cbc", &ossl_des_cbc},
732  {"des3-ecb", &ossl_des3_ecb},
733  {"des3-cbc", &ossl_des3_cbc},
734  {"cast5-ecb", &ossl_cast_ecb},
735  {"cast5-cbc", &ossl_cast_cbc},
736  {"aes-ecb", &ossl_aes_ecb},
737  {"aes-cbc", &ossl_aes_cbc},
738  {NULL}
739 };
740 
741 /* PUBLIC functions */
742 
743 int
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 }
797 
798 /* ResourceOwner callbacks for OSSLCipher */
799 
800 static void
802 {
804 }
unsigned char uint8
Definition: c.h:504
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:224
void err(int eval, const char *fmt,...)
Definition: err.c:43
int i
Definition: isn.c:73
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:77
void pfree(void *pointer)
Definition: mcxt.c:1520
MemoryContext TopMemoryContext
Definition: mcxt.c:149
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition: mcxt.c:1214
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:1180
void * palloc(Size size)
Definition: mcxt.c:1316
static unsigned gen_ossl_block_size(PX_Cipher *c)
Definition: openssl.c:310
int px_find_digest(const char *name, PX_MD **res)
Definition: openssl.c:162
static const struct ossl_cipher ossl_des_ecb
Definition: openssl.c:667
const EVP_CIPHER *(* ossl_EVP_cipher_func)(void)
Definition: openssl.c:241
static const struct ossl_cipher ossl_des3_cbc
Definition: openssl.c:685
static void digest_update(PX_MD *h, const uint8 *data, unsigned dlen)
Definition: openssl.c:131
static int bf_init(PX_Cipher *c, const uint8 *key, unsigned klen, const uint8 *iv)
Definition: openssl.c:455
static int bf_check_supported_key_len(void)
Definition: openssl.c:412
static void ResourceOwnerForgetOSSLDigest(ResourceOwner owner, OSSLDigest *digest)
Definition: openssl.c:83
static unsigned digest_result_size(PX_MD *h)
Definition: openssl.c:98
static const struct ossl_cipher ossl_aes_ecb
Definition: openssl.c:703
static void ResourceOwnerRememberOSSLDigest(ResourceOwner owner, OSSLDigest *digest)
Definition: openssl.c:78
#define MAX_KEY
Definition: openssl.c:45
static void digest_finish(PX_MD *h, uint8 *dst)
Definition: openssl.c:140
struct OSSLCipher OSSLCipher
static const ResourceOwnerDesc ossldigest_resowner_desc
Definition: openssl.c:67
static void free_openssl_cipher(OSSLCipher *od)
Definition: openssl.c:299
static void ResourceOwnerForgetOSSLCipher(ResourceOwner owner, OSSLCipher *od)
Definition: openssl.c:293
static unsigned gen_ossl_key_size(PX_Cipher *c)
Definition: openssl.c:318
static int ossl_aes_ecb_init(PX_Cipher *c, const uint8 *key, unsigned klen, const uint8 *iv)
Definition: openssl.c:568
static const struct ossl_cipher ossl_des3_ecb
Definition: openssl.c:679
static int px_openssl_initialized
Definition: openssl.c:157
static void ResOwnerReleaseOSSLCipher(Datum res)
Definition: openssl.c:801
static const struct ossl_cipher ossl_bf_cfb
Definition: openssl.c:661
struct OSSLDigest OSSLDigest
static int gen_ossl_encrypt(PX_Cipher *c, int padding, const uint8 *data, unsigned dlen, uint8 *res, unsigned *rlen)
Definition: openssl.c:375
static const struct ossl_cipher ossl_cast_cbc
Definition: openssl.c:697
static const ResourceOwnerDesc osslcipher_resowner_desc
Definition: openssl.c:277
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 const struct ossl_cipher ossl_des_cbc
Definition: openssl.c:673
static void digest_reset(PX_MD *h)
Definition: openssl.c:122
static void ResOwnerReleaseOSSLDigest(Datum res)
Definition: openssl.c:223
static int ossl_cast_init(PX_Cipher *c, const uint8 *key, unsigned klen, const uint8 *iv)
Definition: openssl.c:525
static unsigned gen_ossl_iv_size(PX_Cipher *c)
Definition: openssl.c:326
int px_find_cipher(const char *name, PX_Cipher **res)
Definition: openssl.c:744
static const struct ossl_cipher ossl_bf_ecb
Definition: openssl.c:655
static PX_Alias ossl_aliases[]
Definition: openssl.c:631
static const struct ossl_cipher ossl_bf_cbc
Definition: openssl.c:649
static const struct ossl_cipher_lookup ossl_cipher_types[]
Definition: openssl.c:726
static void free_openssl_digest(OSSLDigest *digest)
Definition: openssl.c:89
#define MAX_IV
Definition: openssl.c:46
static const struct ossl_cipher ossl_cast_ecb
Definition: openssl.c:691
static int ossl_aes_init(PX_Cipher *c, const uint8 *key, unsigned klen, const uint8 *iv)
Definition: openssl.c:543
static unsigned digest_block_size(PX_MD *h)
Definition: openssl.c:110
static const struct ossl_cipher ossl_aes_cbc
Definition: openssl.c:710
static int ossl_aes_cbc_init(PX_Cipher *c, const uint8 *key, unsigned klen, const uint8 *iv)
Definition: openssl.c:598
static void digest_free(PX_MD *h)
Definition: openssl.c:149
static int ossl_des3_init(PX_Cipher *c, const uint8 *key, unsigned klen, const uint8 *iv)
Definition: openssl.c:506
static void ResourceOwnerRememberOSSLCipher(ResourceOwner owner, OSSLCipher *od)
Definition: openssl.c:288
static int ossl_des_init(PX_Cipher *c, const uint8 *key, unsigned klen, const uint8 *iv)
Definition: openssl.c:487
const void * data
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:322
uintptr_t Datum
Definition: postgres.h:64
static Pointer DatumGetPointer(Datum X)
Definition: postgres.h:312
char * c
const char * px_resolve_alias(const PX_Alias *list, const char *name)
Definition: px.c:129
#define PXE_DECRYPT_FAILED
Definition: px.h:64
#define PXE_CIPHER_INIT
Definition: px.h:54
#define PXE_ENCRYPT_FAILED
Definition: px.h:65
#define PXE_NO_HASH
Definition: px.h:48
#define PXE_NO_CIPHER
Definition: px.h:49
#define PXE_KEY_TOO_BIG
Definition: px.h:53
ResourceOwner CurrentResourceOwner
Definition: resowner.c:165
void ResourceOwnerForget(ResourceOwner owner, Datum value, const ResourceOwnerDesc *kind)
Definition: resowner.c:554
void ResourceOwnerRemember(ResourceOwner owner, Datum value, const ResourceOwnerDesc *kind)
Definition: resowner.c:514
void ResourceOwnerEnlarge(ResourceOwner owner)
Definition: resowner.c:442
@ RESOURCE_RELEASE_BEFORE_LOCKS
Definition: resowner.h:54
#define RELEASE_PRIO_FIRST
Definition: resowner.h:80
unsigned klen
Definition: openssl.c:267
const struct ossl_cipher * ciph
Definition: openssl.c:269
uint8 iv[MAX_IV]
Definition: openssl.c:266
ResourceOwner owner
Definition: openssl.c:271
unsigned init
Definition: openssl.c:268
EVP_CIPHER_CTX * evp_ctx
Definition: openssl.c:263
uint8 key[MAX_KEY]
Definition: openssl.c:265
const EVP_CIPHER * evp_ciph
Definition: openssl.c:264
EVP_MD_CTX * ctx
Definition: openssl.c:59
const EVP_MD * algo
Definition: openssl.c:58
ResourceOwner owner
Definition: openssl.c:61
const char * name
Definition: resowner.h:93
const char * name
Definition: openssl.c:722
const struct ossl_cipher * ciph
Definition: openssl.c:723
int block_size
Definition: openssl.c:250
int max_key_size
Definition: openssl.c:251
int(* init)(PX_Cipher *c, const uint8 *key, unsigned klen, const uint8 *iv)
Definition: openssl.c:248
ossl_EVP_cipher_func cipher_func
Definition: openssl.c:249
Definition: px.h:116
Definition: px.h:141
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
const char * name