PostgreSQL Source Code  git master
pgp.c File Reference
#include "postgres.h"
#include "pgp.h"
#include "px.h"
Include dependency graph for pgp.c:

Go to the source code of this file.

Data Structures

struct  digest_info
 
struct  cipher_info
 

Functions

static const struct cipher_infoget_cipher_info (int code)
 
int pgp_get_digest_code (const char *name)
 
int pgp_get_cipher_code (const char *name)
 
const char * pgp_get_digest_name (int code)
 
int pgp_get_cipher_key_size (int code)
 
int pgp_get_cipher_block_size (int code)
 
int pgp_load_cipher (int code, PX_Cipher **res)
 
int pgp_load_digest (int code, PX_MD **res)
 
int pgp_init (PGP_Context **ctx_p)
 
int pgp_free (PGP_Context *ctx)
 
int pgp_disable_mdc (PGP_Context *ctx, int disable)
 
int pgp_set_sess_key (PGP_Context *ctx, int use)
 
int pgp_set_convert_crlf (PGP_Context *ctx, int doit)
 
int pgp_set_s2k_mode (PGP_Context *ctx, int mode)
 
int pgp_set_s2k_count (PGP_Context *ctx, int count)
 
int pgp_set_compress_algo (PGP_Context *ctx, int algo)
 
int pgp_set_compress_level (PGP_Context *ctx, int level)
 
int pgp_set_text_mode (PGP_Context *ctx, int mode)
 
int pgp_set_cipher_algo (PGP_Context *ctx, const char *name)
 
int pgp_set_s2k_cipher_algo (PGP_Context *ctx, const char *name)
 
int pgp_set_s2k_digest_algo (PGP_Context *ctx, const char *name)
 
int pgp_get_unicode_mode (PGP_Context *ctx)
 
int pgp_set_unicode_mode (PGP_Context *ctx, int mode)
 
int pgp_set_symkey (PGP_Context *ctx, const uint8 *key, int len)
 

Variables

static int def_cipher_algo = PGP_SYM_AES_128
 
static int def_s2k_cipher_algo = -1
 
static int def_s2k_mode = PGP_S2K_ISALTED
 
static int def_s2k_count = -1
 
static int def_s2k_digest_algo = PGP_DIGEST_SHA1
 
static int def_compress_algo = PGP_COMPR_NONE
 
static int def_compress_level = 6
 
static int def_disable_mdc = 0
 
static int def_use_sess_key = 0
 
static int def_text_mode = 0
 
static int def_unicode_mode = 0
 
static int def_convert_crlf = 0
 
static const struct digest_info digest_list []
 
static const struct cipher_info cipher_list []
 

Function Documentation

◆ get_cipher_info()

static const struct cipher_info* get_cipher_info ( int  code)
static

Definition at line 93 of file pgp.c.

94 {
95  const struct cipher_info *i;
96 
97  for (i = cipher_list; i->name; i++)
98  if (i->code == code)
99  return i;
100  return NULL;
101 }
int i
Definition: isn.c:73
static const struct cipher_info cipher_list[]
Definition: pgp.c:79
int code
Definition: pgp.c:62

References cipher_list, cipher_info::code, and i.

Referenced by pgp_get_cipher_block_size(), pgp_get_cipher_key_size(), and pgp_load_cipher().

◆ pgp_disable_mdc()

int pgp_disable_mdc ( PGP_Context ctx,
int  disable 
)

Definition at line 223 of file pgp.c.

224 {
225  ctx->disable_mdc = disable ? 1 : 0;
226  return 0;
227 }
int disable_mdc
Definition: pgp.h:147

References PGP_Context::disable_mdc.

Referenced by set_arg().

◆ pgp_free()

int pgp_free ( PGP_Context ctx)

Definition at line 213 of file pgp.c.

214 {
215  if (ctx->pub_key)
216  pgp_key_free(ctx->pub_key);
217  px_memset(ctx, 0, sizeof *ctx);
218  pfree(ctx);
219  return 0;
220 }
void pfree(void *pointer)
Definition: mcxt.c:1306
void pgp_key_free(PGP_PubKey *pk)
Definition: pgp-pubkey.c:48
void px_memset(void *ptr, int c, size_t len)
Definition: px.c:123
PGP_PubKey * pub_key
Definition: pgp.h:164

References pfree(), pgp_key_free(), PGP_Context::pub_key, and px_memset().

Referenced by decrypt_internal(), and encrypt_internal().

◆ pgp_get_cipher_block_size()

int pgp_get_cipher_block_size ( int  code)

Definition at line 147 of file pgp.c.

148 {
149  const struct cipher_info *i = get_cipher_info(code);
150 
151  if (i != NULL)
152  return i->block_len;
153  return 0;
154 }
static const struct cipher_info * get_cipher_info(int code)
Definition: pgp.c:93

References cipher_info::code, get_cipher_info(), and i.

Referenced by process_secret_key(), and write_prefix().

◆ pgp_get_cipher_code()

int pgp_get_cipher_code ( const char *  name)

Definition at line 115 of file pgp.c.

116 {
117  const struct cipher_info *i;
118 
119  for (i = cipher_list; i->name; i++)
120  if (pg_strcasecmp(i->name, name) == 0)
121  return i->code;
123 }
const char * name
Definition: encode.c:561
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
#define PXE_PGP_UNSUPPORTED_CIPHER
Definition: px.h:70

References cipher_list, i, name, pg_strcasecmp(), and PXE_PGP_UNSUPPORTED_CIPHER.

Referenced by pgp_set_cipher_algo(), pgp_set_s2k_cipher_algo(), and set_arg().

◆ pgp_get_cipher_key_size()

int pgp_get_cipher_key_size ( int  code)

Definition at line 137 of file pgp.c.

138 {
139  const struct cipher_info *i = get_cipher_info(code);
140 
141  if (i != NULL)
142  return i->key_len;
143  return 0;
144 }

References cipher_info::code, get_cipher_info(), and i.

Referenced by decrypt_key(), init_sess_key(), and pgp_s2k_process().

◆ pgp_get_digest_code()

int pgp_get_digest_code ( const char *  name)

Definition at line 104 of file pgp.c.

105 {
106  const struct digest_info *i;
107 
108  for (i = digest_list; i->name; i++)
109  if (pg_strcasecmp(i->name, name) == 0)
110  return i->code;
112 }
static const struct digest_info digest_list[]
Definition: pgp.c:68
#define PXE_PGP_UNSUPPORTED_HASH
Definition: px.h:71

References digest_list, i, name, pg_strcasecmp(), and PXE_PGP_UNSUPPORTED_HASH.

Referenced by pgp_set_s2k_digest_algo(), and set_arg().

◆ pgp_get_digest_name()

const char* pgp_get_digest_name ( int  code)

Definition at line 126 of file pgp.c.

127 {
128  const struct digest_info *i;
129 
130  for (i = digest_list; i->name; i++)
131  if (i->code == code)
132  return i->name;
133  return NULL;
134 }
int code
Definition: pgp.c:56

References digest_info::code, digest_list, and i.

Referenced by pgp_load_digest().

◆ pgp_get_unicode_mode()

int pgp_get_unicode_mode ( PGP_Context ctx)

Definition at line 340 of file pgp.c.

341 {
342  return ctx->unicode_mode;
343 }
int unicode_mode
Definition: pgp.h:151

References PGP_Context::unicode_mode.

Referenced by decrypt_internal(), and encrypt_internal().

◆ pgp_init()

int pgp_init ( PGP_Context **  ctx_p)

Definition at line 189 of file pgp.c.

190 {
191  PGP_Context *ctx;
192 
193  ctx = palloc0(sizeof *ctx);
194 
197  ctx->s2k_mode = def_s2k_mode;
198  ctx->s2k_count = def_s2k_count;
206  ctx->text_mode = def_text_mode;
207 
208  *ctx_p = ctx;
209  return 0;
210 }
void * palloc0(Size size)
Definition: mcxt.c:1230
static int def_compress_algo
Definition: pgp.c:45
static int def_s2k_cipher_algo
Definition: pgp.c:41
static int def_convert_crlf
Definition: pgp.c:51
static int def_compress_level
Definition: pgp.c:46
static int def_s2k_digest_algo
Definition: pgp.c:44
static int def_unicode_mode
Definition: pgp.c:50
static int def_s2k_mode
Definition: pgp.c:42
static int def_cipher_algo
Definition: pgp.c:40
static int def_disable_mdc
Definition: pgp.c:47
static int def_s2k_count
Definition: pgp.c:43
static int def_text_mode
Definition: pgp.c:49
static int def_use_sess_key
Definition: pgp.c:48
int compress_level
Definition: pgp.h:146
int cipher_algo
Definition: pgp.h:144
int s2k_mode
Definition: pgp.h:140
int text_mode
Definition: pgp.h:149
int s2k_cipher_algo
Definition: pgp.h:143
int convert_crlf
Definition: pgp.h:150
int s2k_count
Definition: pgp.h:141
int compress_algo
Definition: pgp.h:145
int use_sess_key
Definition: pgp.h:148
int s2k_digest_algo
Definition: pgp.h:142

References PGP_Context::cipher_algo, PGP_Context::compress_algo, PGP_Context::compress_level, PGP_Context::convert_crlf, def_cipher_algo, def_compress_algo, def_compress_level, def_convert_crlf, def_disable_mdc, def_s2k_cipher_algo, def_s2k_count, def_s2k_digest_algo, def_s2k_mode, def_text_mode, def_unicode_mode, def_use_sess_key, PGP_Context::disable_mdc, palloc0(), PGP_Context::s2k_cipher_algo, PGP_Context::s2k_count, PGP_Context::s2k_digest_algo, PGP_Context::s2k_mode, PGP_Context::text_mode, PGP_Context::unicode_mode, and PGP_Context::use_sess_key.

Referenced by init_work().

◆ pgp_load_cipher()

int pgp_load_cipher ( int  code,
PX_Cipher **  res 
)

Definition at line 157 of file pgp.c.

158 {
159  int err;
160  const struct cipher_info *i = get_cipher_info(code);
161 
162  if (i == NULL)
163  return PXE_PGP_CORRUPT_DATA;
164 
165  err = px_find_cipher(i->int_name, res);
166  if (err == 0)
167  return 0;
168 
170 }
int px_find_cipher(const char *name, PX_Cipher **res)
Definition: openssl.c:771
#define PXE_PGP_CORRUPT_DATA
Definition: px.h:67

References cipher_info::code, get_cipher_info(), i, px_find_cipher(), PXE_PGP_CORRUPT_DATA, PXE_PGP_UNSUPPORTED_CIPHER, and res.

Referenced by pgp_cfb_create().

◆ pgp_load_digest()

int pgp_load_digest ( int  code,
PX_MD **  res 
)

Definition at line 173 of file pgp.c.

174 {
175  int err;
176  const char *name = pgp_get_digest_name(code);
177 
178  if (name == NULL)
179  return PXE_PGP_CORRUPT_DATA;
180 
181  err = px_find_digest(name, res);
182  if (err == 0)
183  return 0;
184 
186 }
int px_find_digest(const char *name, PX_MD **res)
Definition: openssl.c:178
const char * pgp_get_digest_name(int code)
Definition: pgp.c:126

References cipher_info::code, name, pgp_get_digest_name(), px_find_digest(), PXE_PGP_CORRUPT_DATA, PXE_PGP_UNSUPPORTED_HASH, and res.

Referenced by calc_key_id(), check_key_sha1(), mdc_init(), and pgp_s2k_process().

◆ pgp_set_cipher_algo()

int pgp_set_cipher_algo ( PGP_Context ctx,
const char *  name 
)

Definition at line 307 of file pgp.c.

308 {
309  int code = pgp_get_cipher_code(name);
310 
311  if (code < 0)
312  return code;
313  ctx->cipher_algo = code;
314  return 0;
315 }
int pgp_get_cipher_code(const char *name)
Definition: pgp.c:115

References PGP_Context::cipher_algo, cipher_info::code, name, and pgp_get_cipher_code().

Referenced by set_arg().

◆ pgp_set_compress_algo()

int pgp_set_compress_algo ( PGP_Context ctx,
int  algo 
)

Definition at line 274 of file pgp.c.

275 {
276  switch (algo)
277  {
278  case PGP_COMPR_NONE:
279  case PGP_COMPR_ZIP:
280  case PGP_COMPR_ZLIB:
281  case PGP_COMPR_BZIP2:
282  ctx->compress_algo = algo;
283  return 0;
284  }
285  return PXE_ARGUMENT_ERROR;
286 }
@ PGP_COMPR_BZIP2
Definition: pgp.h:95
@ PGP_COMPR_ZLIB
Definition: pgp.h:94
@ PGP_COMPR_NONE
Definition: pgp.h:92
@ PGP_COMPR_ZIP
Definition: pgp.h:93
#define PXE_ARGUMENT_ERROR
Definition: px.h:59

References PGP_Context::compress_algo, PGP_COMPR_BZIP2, PGP_COMPR_NONE, PGP_COMPR_ZIP, PGP_COMPR_ZLIB, and PXE_ARGUMENT_ERROR.

Referenced by set_arg().

◆ pgp_set_compress_level()

int pgp_set_compress_level ( PGP_Context ctx,
int  level 
)

Definition at line 289 of file pgp.c.

290 {
291  if (level >= 0 && level <= 9)
292  {
293  ctx->compress_level = level;
294  return 0;
295  }
296  return PXE_ARGUMENT_ERROR;
297 }

References PGP_Context::compress_level, and PXE_ARGUMENT_ERROR.

Referenced by set_arg().

◆ pgp_set_convert_crlf()

int pgp_set_convert_crlf ( PGP_Context ctx,
int  doit 
)

Definition at line 237 of file pgp.c.

238 {
239  ctx->convert_crlf = doit ? 1 : 0;
240  return 0;
241 }

References PGP_Context::convert_crlf.

Referenced by set_arg().

◆ pgp_set_s2k_cipher_algo()

int pgp_set_s2k_cipher_algo ( PGP_Context ctx,
const char *  name 
)

Definition at line 318 of file pgp.c.

319 {
320  int code = pgp_get_cipher_code(name);
321 
322  if (code < 0)
323  return code;
324  ctx->s2k_cipher_algo = code;
325  return 0;
326 }

References cipher_info::code, name, pgp_get_cipher_code(), and PGP_Context::s2k_cipher_algo.

Referenced by set_arg().

◆ pgp_set_s2k_count()

int pgp_set_s2k_count ( PGP_Context ctx,
int  count 
)

Definition at line 263 of file pgp.c.

264 {
265  if (ctx->s2k_mode == PGP_S2K_ISALTED && count >= 1024 && count <= 65011712)
266  {
267  ctx->s2k_count = count;
268  return PXE_OK;
269  }
270  return PXE_ARGUMENT_ERROR;
271 }
@ PGP_S2K_ISALTED
Definition: pgp.h:41
#define PXE_OK
Definition: px.h:46

References PGP_S2K_ISALTED, PXE_ARGUMENT_ERROR, PXE_OK, PGP_Context::s2k_count, and PGP_Context::s2k_mode.

Referenced by set_arg().

◆ pgp_set_s2k_digest_algo()

int pgp_set_s2k_digest_algo ( PGP_Context ctx,
const char *  name 
)

Definition at line 329 of file pgp.c.

330 {
331  int code = pgp_get_digest_code(name);
332 
333  if (code < 0)
334  return code;
335  ctx->s2k_digest_algo = code;
336  return 0;
337 }
int pgp_get_digest_code(const char *name)
Definition: pgp.c:104

References cipher_info::code, name, pgp_get_digest_code(), and PGP_Context::s2k_digest_algo.

Referenced by set_arg().

◆ pgp_set_s2k_mode()

int pgp_set_s2k_mode ( PGP_Context ctx,
int  mode 
)

Definition at line 244 of file pgp.c.

245 {
246  int err = PXE_OK;
247 
248  switch (mode)
249  {
250  case PGP_S2K_SIMPLE:
251  case PGP_S2K_SALTED:
252  case PGP_S2K_ISALTED:
253  ctx->s2k_mode = mode;
254  break;
255  default:
256  err = PXE_ARGUMENT_ERROR;
257  break;
258  }
259  return err;
260 }
static PgChecksumMode mode
Definition: pg_checksums.c:65
@ PGP_S2K_SALTED
Definition: pgp.h:40
@ PGP_S2K_SIMPLE
Definition: pgp.h:39

References mode, PGP_S2K_ISALTED, PGP_S2K_SALTED, PGP_S2K_SIMPLE, PXE_ARGUMENT_ERROR, PXE_OK, and PGP_Context::s2k_mode.

Referenced by set_arg().

◆ pgp_set_sess_key()

int pgp_set_sess_key ( PGP_Context ctx,
int  use 
)

Definition at line 230 of file pgp.c.

231 {
232  ctx->use_sess_key = use ? 1 : 0;
233  return 0;
234 }

References PGP_Context::use_sess_key.

Referenced by set_arg().

◆ pgp_set_symkey()

int pgp_set_symkey ( PGP_Context ctx,
const uint8 key,
int  len 
)

Definition at line 353 of file pgp.c.

354 {
355  if (key == NULL || len < 1)
356  return PXE_ARGUMENT_ERROR;
357  ctx->sym_key = key;
358  ctx->sym_key_len = len;
359  return 0;
360 }
const void size_t len
int sym_key_len
Definition: pgp.h:166
const uint8 * sym_key
Definition: pgp.h:165

References sort-test::key, len, PXE_ARGUMENT_ERROR, PGP_Context::sym_key, and PGP_Context::sym_key_len.

Referenced by decrypt_internal(), and encrypt_internal().

◆ pgp_set_text_mode()

int pgp_set_text_mode ( PGP_Context ctx,
int  mode 
)

Definition at line 300 of file pgp.c.

301 {
302  ctx->text_mode = mode;
303  return 0;
304 }

References mode, and PGP_Context::text_mode.

Referenced by init_work().

◆ pgp_set_unicode_mode()

int pgp_set_unicode_mode ( PGP_Context ctx,
int  mode 
)

Definition at line 346 of file pgp.c.

347 {
348  ctx->unicode_mode = mode ? 1 : 0;
349  return 0;
350 }

References mode, and PGP_Context::unicode_mode.

Referenced by set_arg().

Variable Documentation

◆ cipher_list

const struct cipher_info cipher_list[]
static
Initial value:
= {
{"3des", PGP_SYM_DES3, "3des-ecb", 192 / 8, 64 / 8},
{"cast5", PGP_SYM_CAST5, "cast5-ecb", 128 / 8, 64 / 8},
{"bf", PGP_SYM_BLOWFISH, "bf-ecb", 128 / 8, 64 / 8},
{"blowfish", PGP_SYM_BLOWFISH, "bf-ecb", 128 / 8, 64 / 8},
{"aes", PGP_SYM_AES_128, "aes-ecb", 128 / 8, 128 / 8},
{"aes128", PGP_SYM_AES_128, "aes-ecb", 128 / 8, 128 / 8},
{"aes192", PGP_SYM_AES_192, "aes-ecb", 192 / 8, 128 / 8},
{"aes256", PGP_SYM_AES_256, "aes-ecb", 256 / 8, 128 / 8},
{"twofish", PGP_SYM_TWOFISH, "twofish-ecb", 256 / 8, 128 / 8},
{NULL, 0, NULL}
}
@ PGP_SYM_TWOFISH
Definition: pgp.h:87
@ PGP_SYM_DES3
Definition: pgp.h:79
@ PGP_SYM_AES_256
Definition: pgp.h:86
@ PGP_SYM_BLOWFISH
Definition: pgp.h:81
@ PGP_SYM_CAST5
Definition: pgp.h:80
@ PGP_SYM_AES_128
Definition: pgp.h:84
@ PGP_SYM_AES_192
Definition: pgp.h:85

Definition at line 51 of file pgp.c.

Referenced by get_cipher_info(), and pgp_get_cipher_code().

◆ def_cipher_algo

int def_cipher_algo = PGP_SYM_AES_128
static

Definition at line 40 of file pgp.c.

Referenced by pgp_init().

◆ def_compress_algo

int def_compress_algo = PGP_COMPR_NONE
static

Definition at line 45 of file pgp.c.

Referenced by pgp_init().

◆ def_compress_level

int def_compress_level = 6
static

Definition at line 46 of file pgp.c.

Referenced by pgp_init().

◆ def_convert_crlf

int def_convert_crlf = 0
static

Definition at line 51 of file pgp.c.

Referenced by pgp_init().

◆ def_disable_mdc

int def_disable_mdc = 0
static

Definition at line 47 of file pgp.c.

Referenced by pgp_init().

◆ def_s2k_cipher_algo

int def_s2k_cipher_algo = -1
static

Definition at line 41 of file pgp.c.

Referenced by pgp_init().

◆ def_s2k_count

int def_s2k_count = -1
static

Definition at line 43 of file pgp.c.

Referenced by pgp_init().

◆ def_s2k_digest_algo

int def_s2k_digest_algo = PGP_DIGEST_SHA1
static

Definition at line 44 of file pgp.c.

Referenced by pgp_init().

◆ def_s2k_mode

int def_s2k_mode = PGP_S2K_ISALTED
static

Definition at line 42 of file pgp.c.

Referenced by pgp_init().

◆ def_text_mode

int def_text_mode = 0
static

Definition at line 49 of file pgp.c.

Referenced by pgp_init().

◆ def_unicode_mode

int def_unicode_mode = 0
static

Definition at line 50 of file pgp.c.

Referenced by pgp_init().

◆ def_use_sess_key

int def_use_sess_key = 0
static

Definition at line 48 of file pgp.c.

Referenced by pgp_init().

◆ digest_list

const struct digest_info digest_list[]
static
Initial value:
= {
{"md5", PGP_DIGEST_MD5},
{"sha1", PGP_DIGEST_SHA1},
{"sha-1", PGP_DIGEST_SHA1},
{"ripemd160", PGP_DIGEST_RIPEMD160},
{"sha256", PGP_DIGEST_SHA256},
{"sha384", PGP_DIGEST_SHA384},
{"sha512", PGP_DIGEST_SHA512},
{NULL, 0}
}
@ PGP_DIGEST_SHA1
Definition: pgp.h:101
@ PGP_DIGEST_SHA256
Definition: pgp.h:107
@ PGP_DIGEST_MD5
Definition: pgp.h:100
@ PGP_DIGEST_SHA512
Definition: pgp.h:109
@ PGP_DIGEST_SHA384
Definition: pgp.h:108
@ PGP_DIGEST_RIPEMD160
Definition: pgp.h:102

Definition at line 51 of file pgp.c.

Referenced by pgp_get_digest_code(), and pgp_get_digest_name().