PostgreSQL Source Code: contrib/pgcrypto/pgp-pgsql.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

41#include "px.h"

44

45

46

47

52

57

59

63

64

65

66

69{

71 unsigned char *dst;

72 unsigned char *csrc = (unsigned char *) VARDATA_ANY(src);

74

76 if (dst == csrc)

77 return src;

78

81 return res;

82}

83

86{

88}

89

92{

94}

95

96static void

98{

101}

102

103

104

105

107{

119};

120

121static void

123{

135}

136

137#define EX_MSG(arg) \

138 ereport(NOTICE, (errmsg( \

139 "pgp_decrypt: unexpected %s: expected %d got %d", \

140 CppAsString(arg), ex->arg, ctx->arg)))

141

142#define EX_CHECK(arg) do { \

143 if (ex->arg >= 0 && ex->arg != ctx->arg) EX_MSG(arg); \

144 } while (0)

145

146static void

148{

159}

160

161static void

163{

165}

166

167static int

170{

171 int res = 0;

172

173 if (strcmp(key, "cipher-algo") == 0)

175 else if (strcmp(key, "disable-mdc") == 0)

177 else if (strcmp(key, "sess-key") == 0)

179 else if (strcmp(key, "s2k-mode") == 0)

181 else if (strcmp(key, "s2k-count") == 0)

183 else if (strcmp(key, "s2k-digest-algo") == 0)

185 else if (strcmp(key, "s2k-cipher-algo") == 0)

187 else if (strcmp(key, "compress-algo") == 0)

189 else if (strcmp(key, "compress-level") == 0)

191 else if (strcmp(key, "convert-crlf") == 0)

193 else if (strcmp(key, "unicode-mode") == 0)

195

196

197

198

199

200 else if (ex != NULL && strcmp(key, "debug") == 0)

202 else if (ex != NULL && strcmp(key, "expect-cipher-algo") == 0)

203 {

206 }

207 else if (ex != NULL && strcmp(key, "expect-disable-mdc") == 0)

208 {

211 }

212 else if (ex != NULL && strcmp(key, "expect-sess-key") == 0)

213 {

216 }

217 else if (ex != NULL && strcmp(key, "expect-s2k-mode") == 0)

218 {

221 }

222 else if (ex != NULL && strcmp(key, "expect-s2k-count") == 0)

223 {

226 }

227 else if (ex != NULL && strcmp(key, "expect-s2k-digest-algo") == 0)

228 {

231 }

232 else if (ex != NULL && strcmp(key, "expect-s2k-cipher-algo") == 0)

233 {

236 }

237 else if (ex != NULL && strcmp(key, "expect-compress-algo") == 0)

238 {

241 }

242 else if (ex != NULL && strcmp(key, "expect-unicode-mode") == 0)

243 {

246 }

247 else

249

250 return res;

251}

252

253

254

255

256

257

258static char *

259getword(char *p, char **res_p, int *res_len)

260{

261

262 while (*p && (*p == ' ' || *p == '\t' || *p == '\n'))

263 p++;

264

265

266 *res_p = p;

267 if (*p == '=' || *p == ',')

268 p++;

269 else

270 while (*p && !(*p == ' ' || *p == '\t' || *p == '\n'

271 || *p == '=' || *p == ','))

272 p++;

273

274

275 *res_len = p - *res_p;

276

277

278 while (*p && (*p == ' ' || *p == '\t' || *p == '\n'))

279 p++;

280

281 return p;

282}

283

284

285

286

287static char *

289{

290 int c,

291 i;

293

294 for (i = 0; i < len; i++)

295 {

296 c = s[i];

297 if (c >= 'A' && c <= 'Z')

298 c += 'a' - 'A';

299 res[i] = c;

300 }

301 res[len] = 0;

302 return res;

303}

304

305static int

308{

310 char *key,

312 int key_len,

313 val_len;

314 int res = 0;

315 char *p = str;

316

317 while (*p)

318 {

321 if (*p++ != '=')

322 break;

324 if (*p == '\0')

325 ;

326 else if (*p++ != ',')

327 break;

328

329 if (*key == 0 || *val == 0 || val_len == 0)

330 break;

331

332 key[key_len] = 0;

333 val[val_len] = 0;

334

336 if (res < 0)

337 break;

338 }

340 return res;

341}

342

345{

348}

349

350static void

353{

355

357

358 if (err == 0 && args != NULL)

361

364

367

369}

370

374{

376 *dst;

380 int res_len;

384 text *tmp_data = NULL;

385

387

389 {

391 if (tmp_data == data)

392 tmp_data = NULL;

393 else

394 data = tmp_data;

395 }

396

399

400

401

402

404

405

406

407

408 if (is_pubenc)

409 {

411

413 NULL, 0, 0);

415 }

416 else

419

420

421

422

423 if (err >= 0)

425

426

427

428

430 {

433 if (tmp_data)

439 }

440

441

443 res = (bytea *) restmp;

445

446 if (tmp_data)

451

453

454 return res;

455}

456

460{

462 MBuf *src = NULL,

463 *dst = NULL;

467 int res_len;

470 int got_unicode = 0;

471

472

474

478

479

480

481

483

484

485

486

487 if (is_pubenc)

488 {

489 uint8 *psw = NULL;

490 int psw_len = 0;

492

493 if (keypsw)

494 {

497 }

501 }

502 else

505

506

507 if (err >= 0)

508 {

510

513

514

516 }

517

520

522 {

526 }

527

530

531

532 res = (bytea *) restmp;

534

535 if (need_text && got_unicode)

536 {

538

539 if (utf != res)

540 {

542 res = utf;

543 }

544 }

546

547 return res;

548}

549

550

551

552

555{

560

565

567

573}

574

577{

582

587

589

595}

596

597

600{

605

610

612

618}

619

622{

627

632

634

640}

641

642

643

644

645

648{

653

658

660

666}

667

670{

675

680

682

688}

689

690

693{

696 text *psw = NULL,

697 *arg = NULL;

699

706

708

716}

717

720{

723 text *psw = NULL,

724 *arg = NULL;

726

733

735

743}

744

745

746

747

748

749

750

751

752

753

754static int

756 char ***p_keys, char ***p_values)

757{

758 int nkdims = ARR_NDIM(key_array);

759 int nvdims = ARR_NDIM(val_array);

760 char **keys,

762 Datum *key_datums,

763 *val_datums;

764 bool *key_nulls,

765 *val_nulls;

766 int key_count,

767 val_count;

768 int i;

769

770 if (nkdims > 1 || nkdims != nvdims)

772 (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),

773 errmsg("wrong number of array subscripts")));

774 if (nkdims == 0)

775 return 0;

776

779

780 if (key_count != val_count)

782 (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),

783 errmsg("mismatched array dimensions")));

784

785 keys = (char **) palloc(sizeof(char *) * key_count);

786 values = (char **) palloc(sizeof(char *) * val_count);

787

788 for (i = 0; i < key_count; i++)

789 {

790 char *v;

791

792

793 if (key_nulls[i])

795 (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),

796 errmsg("null value not allowed for header key")));

797

799

802 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),

803 errmsg("header key must not contain non-ASCII characters")));

804 if (strstr(v, ": "))

806 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),

807 errmsg("header key must not contain \": \"")));

808 if (strchr(v, '\n'))

810 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),

811 errmsg("header key must not contain newlines")));

812 keys[i] = v;

813

814

815 if (val_nulls[i])

817 (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),

818 errmsg("null value not allowed for header value")));

819

821

824 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),

825 errmsg("header value must not contain non-ASCII characters")));

826 if (strchr(v, '\n'))

828 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),

829 errmsg("header value must not contain newlines")));

830

832 }

833

834 *p_keys = keys;

836 return key_count;

837}

838

841{

844 int data_len;

846 int num_headers;

847 char **keys = NULL,

849

853 {

857 }

859 num_headers = 0;

860 else

862

864

866 num_headers, keys, values);

867

872

875}

876

879{

882 int data_len;

883 int ret;

885

888

890

892 if (ret < 0)

898

901}

902

903

904typedef struct

905{

910

913{

916 char *utf8key;

917 char *utf8val;

921

923 {

925 int res;

927

929

930

932

933

935 elog(ERROR, "return type must be a row type");

936

939

941

946 if (res < 0)

948

951 }

952

955

958 else

959 {

961

962

965

968

969

972 }

973}

974

975

976

977

978

979

980

983{

986 int res_len;

988

992

995 if (res_len < 0)

998

1001}

#define PG_GETARG_ARRAYTYPE_P(n)

void deconstruct_array_builtin(ArrayType *array, Oid elmtype, Datum **elemsp, bool **nullsp, int *nelemsp)

static Datum values[MAXATTR]

#define TextDatumGetCString(d)

int errcode(int sqlerrcode)

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

#define ereport(elevel,...)

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

HeapTuple BuildTupleFromCStrings(AttInMetadata *attinmeta, char **values)

AttInMetadata * TupleDescGetAttInMetadata(TupleDesc tupdesc)

#define PG_FREE_IF_COPY(ptr, n)

#define PG_GETARG_BYTEA_PP(n)

#define PG_GETARG_TEXT_PP(n)

#define PG_RETURN_TEXT_P(x)

TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)

#define SRF_IS_FIRSTCALL()

#define SRF_PERCALL_SETUP()

#define SRF_RETURN_NEXT(_funcctx, _result)

#define SRF_FIRSTCALL_INIT()

static Datum HeapTupleGetDatum(const HeapTupleData *tuple)

#define SRF_RETURN_DONE(_funcctx)

if(TABLE==NULL||TABLE_index==NULL)

int mbuf_free(MBuf *mbuf)

int mbuf_append(MBuf *dst, const uint8 *buf, int len)

MBuf * mbuf_create_from_data(uint8 *data, int len)

int mbuf_steal_data(MBuf *mbuf, uint8 **data_p)

MBuf * mbuf_create(int len)

int GetDatabaseEncoding(void)

char * pg_any_to_server(const char *s, int len, int encoding)

unsigned char * pg_do_encoding_conversion(unsigned char *src, int len, int src_encoding, int dest_encoding)

void pfree(void *pointer)

static MemoryContext MemoryContextSwitchTo(MemoryContext context)

int pgp_extract_armor_headers(const uint8 *src, unsigned len, int *nheaders, char ***keys, char ***values)

int pgp_armor_decode(const uint8 *src, int len, StringInfo dst)

void pgp_armor_encode(const uint8 *src, unsigned len, StringInfo dst, int num_headers, char **keys, char **values)

int pgp_decrypt(PGP_Context *ctx, MBuf *msrc, MBuf *mdst)

int pgp_encrypt(PGP_Context *ctx, MBuf *src, MBuf *dst)

int pgp_get_keyid(MBuf *pgp_data, char *dst)

static MBuf * create_mbuf_from_vardata(text *data)

static text * convert_to_utf8(text *src)

static text * convert_charset(text *src, int cset_from, int cset_to)

static void fill_expect(struct debug_expect *ex, int text_mode)

Datum pgp_pub_decrypt_bytea(PG_FUNCTION_ARGS)

static char * getword(char *p, char **res_p, int *res_len)

Datum pgp_sym_decrypt_text(PG_FUNCTION_ARGS)

Datum pgp_sym_encrypt_bytea(PG_FUNCTION_ARGS)

Datum pg_armor(PG_FUNCTION_ARGS)

Datum pg_dearmor(PG_FUNCTION_ARGS)

Datum pgp_pub_encrypt_text(PG_FUNCTION_ARGS)

static text * convert_from_utf8(text *src)

static void init_work(PGP_Context **ctx_p, int is_text, text *args, struct debug_expect *ex)

static void check_expect(PGP_Context *ctx, struct debug_expect *ex)

Datum pgp_key_id_w(PG_FUNCTION_ARGS)

static bytea * encrypt_internal(int is_pubenc, int is_text, text *data, text *key, text *args)

Datum pgp_sym_decrypt_bytea(PG_FUNCTION_ARGS)

static void show_debug(const char *msg)

static int parse_args(PGP_Context *ctx, uint8 *args, int arg_len, struct debug_expect *ex)

Datum pgp_sym_encrypt_text(PG_FUNCTION_ARGS)

Datum pgp_armor_headers(PG_FUNCTION_ARGS)

Datum pgp_pub_decrypt_text(PG_FUNCTION_ARGS)

static int parse_key_value_arrays(ArrayType *key_array, ArrayType *val_array, char ***p_keys, char ***p_values)

static bytea * decrypt_internal(int is_pubenc, int need_text, text *data, text *key, text *keypsw, text *args)

PG_FUNCTION_INFO_V1(pgp_sym_encrypt_bytea)

static char * downcase_convert(const uint8 *s, int len)

static int set_arg(PGP_Context *ctx, char *key, char *val, struct debug_expect *ex)

static void clear_and_pfree(text *p)

Datum pgp_pub_encrypt_bytea(PG_FUNCTION_ARGS)

int pgp_set_pubkey(PGP_Context *ctx, MBuf *keypkt, const uint8 *key, int key_len, int pubtype)

int pgp_disable_mdc(PGP_Context *ctx, int disable)

int pgp_get_cipher_code(const char *name)

int pgp_init(PGP_Context **ctx_p)

int pgp_set_text_mode(PGP_Context *ctx, int mode)

int pgp_set_s2k_digest_algo(PGP_Context *ctx, const char *name)

int pgp_set_s2k_mode(PGP_Context *ctx, int mode)

int pgp_get_digest_code(const char *name)

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

int pgp_set_s2k_cipher_algo(PGP_Context *ctx, const char *name)

int pgp_set_compress_algo(PGP_Context *ctx, int algo)

int pgp_get_unicode_mode(PGP_Context *ctx)

int pgp_set_cipher_algo(PGP_Context *ctx, const char *name)

int pgp_set_convert_crlf(PGP_Context *ctx, int doit)

int pgp_set_unicode_mode(PGP_Context *ctx, int mode)

int pgp_free(PGP_Context *ctx)

int pgp_set_s2k_count(PGP_Context *ctx, int count)

int pgp_set_sess_key(PGP_Context *ctx, int use)

int pgp_set_compress_level(PGP_Context *ctx, int level)

void px_THROW_ERROR(int err)

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

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

#define PXE_ARGUMENT_ERROR

bool pg_is_ascii(const char *str)

void initStringInfo(StringInfo str)

AttInMetadata * attinmeta

MemoryContext multi_call_memory_ctx

#define SET_VARSIZE(PTR, len)

#define VARSIZE_ANY_EXHDR(PTR)

text * cstring_to_text(const char *s)