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

32

35#include "px.h"

36

37int

39{

41

43 *pk_p = pk;

44 return 0;

45}

46

47void

49{

50 if (pk == NULL)

51 return;

52

53 switch (pk->algo)

54 {

60 break;

70 break;

77 break;

78 }

81}

82

83static int

85{

86 int res;

91

93 if (res < 0)

94 return res;

95

96 len = 1 + 4 + 1;

97 switch (pk->algo)

98 {

103 break;

109 break;

115 break;

116 }

117

118 hdr[0] = 0x99;

119 hdr[1] = len >> 8;

120 hdr[2] = len & 0xFF;

122

126

127 switch (pk->algo)

128 {

133 break;

139 break;

145 break;

146 }

147

150

153

154 return 0;

155}

156

157int

159{

160 int res;

162

164 if (res < 0)

165 return res;

166

167

169 if (pk->ver != 4)

170 {

172 goto out;

173 }

174

175

177 if (res < 0)

178 goto out;

179

180

182

183 switch (pk->algo)

184 {

187 if (res < 0)

188 break;

190 if (res < 0)

191 break;

193 if (res < 0)

194 break;

196 if (res < 0)

197 break;

198

200 break;

201

206 if (res < 0)

207 break;

209 if (res < 0)

210 break;

211

213

216 break;

217

220 if (res < 0)

221 break;

223 if (res < 0)

224 break;

226 if (res < 0)

227 break;

228

230

232 break;

233

234 default:

235 px_debug("unknown public algo: %d", pk->algo);

237 }

238

239out:

240 if (res < 0)

242 else

243 *pk_p = pk;

244

245 return res;

246}

247

248#define HIDE_CLEAR 0

249#define HIDE_CKSUM 255

250#define HIDE_SHA1 254

251

252static int

254{

255 int res;

256 uint8 got_sha1[20];

257 uint8 my_sha1[20];

259

261 if (res < 0)

262 return res;

263

265 if (res < 0)

266 goto err;

267 switch (pk->algo)

268 {

271 break;

279 break;

282 break;

283 }

286

287 if (memcmp(my_sha1, got_sha1, 20) != 0)

288 {

289 px_debug("key sha1 check failed");

291 }

295 return res;

296}

297

298static int

300{

301 int res;

302 unsigned got_cksum,

303 my_cksum = 0;

305

307 if (res < 0)

308 return res;

309

310 got_cksum = ((unsigned) buf[0] << 8) + buf[1];

311 switch (pk->algo)

312 {

315 break;

323 break;

326 break;

327 }

328 if (my_cksum != got_cksum)

329 {

330 px_debug("key cksum check failed");

332 }

333 return 0;

334}

335

336static int

338 const uint8 *key, int key_len)

339{

340 int res;

341 int hide_type;

342 int cipher_algo;

343 int bs;

346 *pf_key;

350

351

353 if (res < 0)

354 return res;

355

356

357

358

361 {

362 if (key == NULL)

364 GETBYTE(pkt, cipher_algo);

366 if (res < 0)

367 return res;

368

370 if (res < 0)

371 return res;

372

374 if (bs == 0)

375 {

376 px_debug("unknown cipher algo=%d", cipher_algo);

378 }

380 if (res < 0)

381 return res;

382

383

384

385

387 if (res < 0)

388 return res;

390 if (res < 0)

391 return res;

392 pf_key = pf_decrypt;

393 }

395 {

396 pf_key = pkt;

397 }

398 else

399 {

400 px_debug("unknown hide type");

402 }

403

404

405 switch (pk->algo)

406 {

411 if (res < 0)

412 break;

414 if (res < 0)

415 break;

417 if (res < 0)

418 break;

420 if (res < 0)

421 break;

422 break;

425 break;

428 break;

429 default:

430 px_debug("unknown public algo: %d", pk->algo);

432 }

433

434 if (res >= 0)

435 {

438 else

440 }

441 if (res >= 0)

443

444 if (pf_decrypt)

446 if (cfb)

448

449 if (res < 0)

451 else

452 *pk_p = pk;

453

454 return res;

455}

456

457static int

459 const uint8 *psw, int psw_len, int pubtype)

460{

462 int res;

467 int got_main_key = 0;

468

469

470

471

472

473

474 while (1)

475 {

477 if (res <= 0)

478 break;

480 if (res < 0)

481 break;

482

483 switch (tag)

484 {

487 if (got_main_key)

488 {

490 break;

491 }

492 got_main_key = 1;

494 break;

495

497 if (pubtype != 0)

499 else

501 break;

502

504 if (pubtype != 1)

506 else

508 break;

509

517 break;

518 default:

519 px_debug("unknown/unexpected packet: %d", tag);

521 }

523 pkt = NULL;

524

525 if (pk != NULL)

526 {

528 {

529 if (enc_key == NULL)

530 {

531 enc_key = pk;

532 pk = NULL;

533 }

534 else

536 }

537

538 if (pk)

540 pk = NULL;

541 }

542

543 if (res < 0)

544 break;

545 }

546

547 if (pkt)

549

550 if (res < 0)

551 {

552 if (enc_key)

554 return res;

555 }

556

557 if (!enc_key)

559 else

560 *pk_p = enc_key;

561 return res;

562}

563

564int

566 const uint8 *key, int key_len, int pubtype)

567{

568 int res;

571

573 if (res < 0)

574 return res;

575

578

579 if (res >= 0)

581

582 return res < 0 ? res : 0;

583}

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

#define palloc0_object(type)

int pullf_read_fixed(PullFilter *src, int len, uint8 *dst)

int pullf_create(PullFilter **pf_p, const PullFilterOps *op, void *init_arg, PullFilter *src)

int pullf_create_mbuf_reader(PullFilter **mp_p, MBuf *src)

void pullf_free(PullFilter *pf)

void pfree(void *pointer)

static char buf[DEFAULT_XLOG_SEG_SIZE]

void pgp_cfb_free(PGP_CFB *ctx)

int pgp_cfb_create(PGP_CFB **ctx_p, int algo, const uint8 *key, int key_len, int resync, uint8 *iv)

int pgp_parse_pkt_hdr(PullFilter *src, uint8 *tag, int *len_p, int allow_ctx)

int pgp_expect_packet_end(PullFilter *pkt)

struct PullFilterOps pgp_decrypt_filter

int pgp_skip_packet(PullFilter *pkt)

int pgp_create_pkt_reader(PullFilter **pf_p, PullFilter *src, int len, int pkttype, PGP_Context *ctx)

int pgp_mpi_free(PGP_MPI *mpi)

int pgp_mpi_hash(PX_MD *md, PGP_MPI *n)

int pgp_mpi_read(PullFilter *src, PGP_MPI **mpi)

unsigned pgp_mpi_cksum(unsigned cksum, PGP_MPI *n)

static int calc_key_id(PGP_PubKey *pk)

static int process_secret_key(PullFilter *pkt, PGP_PubKey **pk_p, const uint8 *key, int key_len)

int pgp_key_alloc(PGP_PubKey **pk_p)

static int internal_read_key(PullFilter *src, PGP_PubKey **pk_p, const uint8 *psw, int psw_len, int pubtype)

static int check_key_cksum(PullFilter *src, PGP_PubKey *pk)

void pgp_key_free(PGP_PubKey *pk)

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

static int check_key_sha1(PullFilter *src, PGP_PubKey *pk)

int _pgp_read_public_key(PullFilter *pkt, PGP_PubKey **pk_p)

int pgp_s2k_process(PGP_S2K *s2k, int cipher, const uint8 *key, int key_len)

int pgp_s2k_read(PullFilter *src, PGP_S2K *s2k)

int pgp_get_cipher_block_size(int code)

int pgp_load_digest(int code, PX_MD **res)

@ PGP_PUB_RSA_ENCRYPT_SIGN

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

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

#define PXE_PGP_EXPECT_PUBLIC_KEY

#define px_md_finish(md, buf)

#define PXE_PGP_UNEXPECTED_PKT

#define PXE_PGP_MULTIPLE_KEYS

#define PXE_PGP_NO_USABLE_KEY

#define PXE_PGP_EXPECT_SECRET_KEY

#define PXE_PGP_NEED_SECRET_PSW

#define PXE_PGP_MULTIPLE_SUBKEYS

#define px_md_update(md, data, dlen)

#define PXE_PGP_UNSUPPORTED_CIPHER

#define PXE_PGP_UNKNOWN_PUBALGO

#define PXE_PGP_KEYPKT_CORRUPT

#define PXE_PGP_NOT_V4_KEYPKT

static unsigned hash(unsigned *uv, int n)

struct PGP_PubKey::@0::@3 rsa

struct PGP_PubKey::@0::@2 elg

struct PGP_PubKey::@0::@4 dsa