PostgreSQL Source Code: contrib/pgcrypto/px.c Source File (original) (raw)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

33

34#include "px.h"

35

37{

40};

41

43 {PXE_OK, "Everything ok"},

51 {PXE_BUG, "pgcrypto bug"},

55 {PXE_NO_RANDOM, "Failed to generate strong random bits"},

71 "Several keys given - pgcrypto does not handle keyring"},

81

82 {0, NULL},

83};

84

85

86

87

88

89

90

91

92void

94{

96 {

98 (errcode(ERRCODE_INTERNAL_ERROR),

99 errmsg("could not generate a random number")));

100 }

101 else

102 {

103

105 (errcode(ERRCODE_EXTERNAL_ROUTINE_INVOCATION_EXCEPTION),

107 }

108}

109

110const char *

112{

114

116 if (e->err == err)

117 return e->desc;

118 return "Bad error code";

119}

120

121

122void

124{

125 memset(ptr, c, len);

126}

127

128const char *

130{

131 while (list->name)

132 {

134 return list->name;

136 }

138}

139

141

142void

144{

146}

147

148void

150{

151 va_list ap;

152

153 va_start(ap, fmt);

155 {

156 char buf[512];

157

160 }

161 va_end(ap);

162}

163

164

165

166

167

168static unsigned

170{

171 return dlen + 512;

172}

173

174static unsigned

176{

177 return dlen;

178}

179

180static int

182 const uint8 *iv, unsigned ivlen)

183{

185 unsigned ks,

186 ivs;

188 uint8 *ivbuf = NULL;

190

192

194 if (ivs > 0)

195 {

197 if (ivlen > ivs)

198 memcpy(ivbuf, iv, ivs);

199 else if (ivlen > 0)

200 memcpy(ivbuf, iv, ivlen);

201 }

202

203 if (klen > ks)

204 klen = ks;

206 memcpy(keybuf, key, klen);

207

209

210 if (ivbuf)

213

214 return err;

215}

216

217static int

219 uint8 *res, unsigned *rlen)

220{

222}

223

224static int

226 uint8 *res, unsigned *rlen)

227{

229}

230

231static void

233{

234 if (cx->cipher)

238}

239

240

241

242static int

244{

245 char *p,

246 *p2,

247 *q;

248

249 *cipher = full;

250 *pad = NULL;

251

252 p = strchr(full, '/');

253 if (p != NULL)

254 *p++ = 0;

255 while (p != NULL)

256 {

257 if ((q = strchr(p, '/')) != NULL)

258 *q++ = 0;

259

260 if (!*p)

261 {

262 p = q;

263 continue;

264 }

265 p2 = strchr(p, ':');

266 if (p2 != NULL)

267 {

268 *p2++ = 0;

269 if (strcmp(p, "pad") == 0)

270 *pad = p2;

271 else

273 }

274 else

276

277 p = q;

278 }

279 return 0;

280}

281

282

283

284int

286{

288 char *buf,

289 *s_cipher,

290 *s_pad;

291

293

296

299 {

302 return err;

303 }

304

307 goto err1;

308

309 if (s_pad != NULL)

310 {

311 if (strcmp(s_pad, "pkcs") == 0)

312 cx->padding = 1;

313 else if (strcmp(s_pad, "none") == 0)

314 cx->padding = 0;

315 else

316 goto err1;

317 }

318 else

319 cx->padding = 1;

320

327

329

330 *res = cx;

331

332 return 0;

333

334err1:

335 if (cx->cipher)

340}

int errcode(int sqlerrcode)

int errmsg(const char *fmt,...)

#define ereport(elevel,...)

void err(int eval, const char *fmt,...)

int cx(PlannerInfo *root, Gene *tour1, Gene *tour2, Gene *offspring, int num_gene, City *city_table)

char * pstrdup(const char *in)

void pfree(void *pointer)

void * palloc0(Size size)

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

int pg_strcasecmp(const char *s1, const char *s2)

void px_THROW_ERROR(int err)

static void combo_free(PX_Combo *cx)

static int combo_encrypt(PX_Combo *cx, const uint8 *data, unsigned dlen, uint8 *res, unsigned *rlen)

static int parse_cipher_name(char *full, char **cipher, char **pad)

static const struct error_desc px_err_list[]

static int combo_decrypt(PX_Combo *cx, const uint8 *data, unsigned dlen, uint8 *res, unsigned *rlen)

static unsigned combo_decrypt_len(PX_Combo *cx, unsigned dlen)

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

static unsigned combo_encrypt_len(PX_Combo *cx, unsigned dlen)

const char * px_strerror(int err)

static void(* debug_handler)(const char *)

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

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

static int combo_init(PX_Combo *cx, const uint8 *key, unsigned klen, const uint8 *iv, unsigned ivlen)

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

void px_memset(void *ptr, int c, size_t len)

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

#define PXE_PGP_EXPECT_PUBLIC_KEY

#define px_cipher_free(c)

#define PXE_PGP_UNSUPPORTED_COMPR

#define PXE_PGP_BAD_S2K_MODE

#define PXE_DECRYPT_FAILED

#define px_cipher_iv_size(c)

#define PXE_ARGUMENT_ERROR

#define PXE_BAD_SALT_ROUNDS

#define PXE_PGP_UNEXPECTED_PKT

#define PXE_PGP_MULTIPLE_KEYS

#define PXE_ENCRYPT_FAILED

#define PXE_PGP_UNSUPPORTED_PUBALGO

#define PXE_PGP_NO_USABLE_KEY

#define PXE_PGP_EXPECT_SECRET_KEY

#define PXE_HASH_UNUSABLE_FOR_HMAC

#define PXE_PGP_NEED_SECRET_PSW

#define PXE_PGP_MULTIPLE_SUBKEYS

#define PXE_PGP_COMPRESSION_ERROR

#define PXE_PGP_WRONG_KEY

#define PXE_PGP_CORRUPT_DATA

#define PXE_UNKNOWN_SALT_ALGO

#define PXE_PGP_MATH_FAILED

#define PXE_PGP_UNSUPPORTED_CIPHER

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

#define PXE_PGP_UNKNOWN_PUBALGO

#define PXE_PGP_SHORT_ELGAMAL_KEY

#define PXE_PGP_CORRUPT_ARMOR

#define PXE_PGP_KEYPKT_CORRUPT

#define px_cipher_init(c, k, klen, iv)

#define PXE_PGP_UNSUPPORTED_HASH

#define px_cipher_key_size(c)

#define PXE_PGP_NOT_V4_KEYPKT