1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-27 12:41:57 +03:00

The large one adds support for RSA keys and reorganizes

the pubkey functions a bit.  The actual RSA-specific code
there is tiny, most of the patch consists of reorg of the
pubkey code, as lots of it was written as elgamal-only.

---------------------------------------------------------------------------

The SHLIB section was copy-pasted from somewhere and contains
several unnecessary libs.  This cleans it up a bit.

 -lcrypt
   we don't use system crypt()

 -lssl, -lssleay32
   no SSL here

 -lz in win32 section
   already added on previous line

 -ldes
   The chance anybody has it is pretty low.
   And the chance pgcrypto works with it is even lower.

Also trim the win32 section.

---------------------------------------------------------------------------

It is already disabled in Makefile, remove code too.

---------------------------------------------------------------------------

I was bit hasty making the random exponent 'k' a prime.  Further researh
shows that Elgamal encryption has no specific needs in respect to k,
any random number is fine.

It is bit different for signing, there it needs to be 'relatively prime'
to p - 1,  that means GCD(k, p-1) == 1, which is also a lot lighter than
full primality.  As we don't do signing, this can be ignored.

This brings major speedup to Elgamal encryption.

---------------------------------------------------------------------------

o  pgp_mpi_free: Accept NULLs
o  pgp_mpi_cksum: result should be 16bit
o  Remove function name from error messages - to be similar to other
   SQL functions, and it does not match anyway the called function
o  remove couple junk lines

---------------------------------------------------------------------------

o  Support for RSA encryption
o  Big reorg to better separate generic and algorithm-specific code.
o  Regression tests for RSA.

---------------------------------------------------------------------------

o  Tom stuck a CVS id into file.  I doubt the usefulness of it,
   but if it needs to be in the file then rather at the end.
   Also tag it as comment for asciidoc.
o  Mention bytea vs. text difference
o  Couple clarifications

---------------------------------------------------------------------------

There is a choice whether to update it with pgp functions or
remove it.  I decided to remove it, updating is pointless.

I've tried to keep the core of pgcrypto relatively independent
from main PostgreSQL, to make it easy to use externally if needed,
and that is good.  Eg. that made development of PGP functions much
nicer.

But I have no plans to release it as generic library, so keeping such
doc
up-to-date is waste of time.  If anyone is interested in using it in
other products, he can probably bother to read the source too.

Commented source is another thing - I'll try to make another pass
over code to see if there is anything non-obvious that would need
more comments.

---------------------------------------------------------------------------

Marko Kreen
This commit is contained in:
Bruce Momjian
2005-08-13 02:06:21 +00:00
parent abecf1afc2
commit 87688ddf87
23 changed files with 815 additions and 285 deletions

View File

@ -26,7 +26,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $PostgreSQL: pgsql/contrib/pgcrypto/pgp-pubenc.c,v 1.2 2005/07/11 15:07:59 tgl Exp $
* $PostgreSQL: pgsql/contrib/pgcrypto/pgp-pubenc.c,v 1.3 2005/08/13 02:06:20 momjian Exp $
*/
#include "postgres.h"
@ -84,39 +84,16 @@ pad_eme_pkcs1_v15(uint8 *data, int data_len, int res_len, uint8 **res_p)
return 0;
}
/*
* Decide the padded message length in bytes.
* It should be as large as possible, but not larger
* than p.
*
* To get max size (and assuming p may have weird sizes):
* ((p->bytes * 8 - 6) > p->bits) ? (p->bytes - 1) : p->bytes
*
* Following mirrors gnupg behaviour.
*/
static int
decide_msglen(PGP_MPI *p)
{
return p->bytes - 1;
}
static int
create_secmsg(PGP_Context *ctx, PGP_MPI **msg_p)
create_secmsg(PGP_Context *ctx, PGP_MPI **msg_p, int full_bytes)
{
uint8 *secmsg;
int res, i, full_bytes;
int res, i;
unsigned cksum = 0;
int klen = ctx->sess_key_len;
uint8 *padded = NULL;
PGP_MPI *m = NULL;
PGP_PubKey *pk = ctx->pub_key;
/*
* Refuse to operate with keys < 1024
*/
if (pk->elg_p->bits < 1024)
return PXE_PGP_SHORT_ELGAMAL_KEY;
/* calc checksum */
for (i = 0; i < klen; i++)
cksum += ctx->sess_key[i];
@ -133,7 +110,6 @@ create_secmsg(PGP_Context *ctx, PGP_MPI **msg_p)
/*
* now create a large integer of it
*/
full_bytes = decide_msglen(pk->elg_p);
res = pad_eme_pkcs1_v15(secmsg, klen + 3, full_bytes, &padded);
if (res >= 0)
{
@ -156,37 +132,72 @@ create_secmsg(PGP_Context *ctx, PGP_MPI **msg_p)
return res;
}
static int
encrypt_and_write_elgamal(PGP_Context *ctx, PGP_PubKey *pk, PushFilter *pkt)
{
int res;
PGP_MPI *m = NULL, *c1 = NULL, *c2 = NULL;
/* create padded msg */
res = create_secmsg(ctx, &m, pk->pub.elg.p->bytes - 1);
if (res < 0)
goto err;
/* encrypt it */
res = pgp_elgamal_encrypt(pk, m, &c1, &c2);
if (res < 0)
goto err;
/* write out */
res = pgp_mpi_write(pkt, c1);
if (res < 0)
goto err;
res = pgp_mpi_write(pkt, c2);
err:
pgp_mpi_free(m);
pgp_mpi_free(c1);
pgp_mpi_free(c2);
return res;
}
static int
encrypt_and_write_rsa(PGP_Context *ctx, PGP_PubKey *pk, PushFilter *pkt)
{
int res;
PGP_MPI *m = NULL, *c = NULL;
/* create padded msg */
res = create_secmsg(ctx, &m, pk->pub.rsa.n->bytes - 1);
if (res < 0)
goto err;
/* encrypt it */
res = pgp_rsa_encrypt(pk, m, &c);
if (res < 0)
goto err;
/* write out */
res = pgp_mpi_write(pkt, c);
err:
pgp_mpi_free(m);
pgp_mpi_free(c);
return res;
}
int pgp_write_pubenc_sesskey(PGP_Context *ctx, PushFilter *dst)
{
int res;
PGP_PubKey *pk = ctx->pub_key;
PGP_MPI *m = NULL, *c1 = NULL, *c2 = NULL;
uint8 ver = 3;
uint8 algo = PGP_PUB_ELG_ENCRYPT;
PushFilter *pkt = NULL;
uint8 algo = pk->algo;
if (pk == NULL) {
px_debug("no pubkey?\n");
return PXE_BUG;
}
if (!pk->elg_p || !pk->elg_g || !pk->elg_y) {
px_debug("pubkey not loaded?\n");
return PXE_BUG;
}
/*
* sesskey packet
*/
res = create_secmsg(ctx, &m);
if (res < 0)
goto err;
/*
* encrypt it
*/
res = pgp_elgamal_encrypt(pk, m, &c1, &c2);
if (res < 0)
goto err;
/*
* now write packet
@ -203,10 +214,17 @@ int pgp_write_pubenc_sesskey(PGP_Context *ctx, PushFilter *dst)
res = pushf_write(pkt, &algo, 1);
if (res < 0)
goto err;
res = pgp_mpi_write(pkt, c1);
if (res < 0)
goto err;
res = pgp_mpi_write(pkt, c2);
switch (algo)
{
case PGP_PUB_ELG_ENCRYPT:
res = encrypt_and_write_elgamal(ctx, pk, pkt);
break;
case PGP_PUB_RSA_ENCRYPT:
case PGP_PUB_RSA_ENCRYPT_SIGN:
res = encrypt_and_write_rsa(ctx, pk, pkt);
break;
}
if (res < 0)
goto err;
@ -217,12 +235,6 @@ int pgp_write_pubenc_sesskey(PGP_Context *ctx, PushFilter *dst)
err:
if (pkt)
pushf_free(pkt);
if (m)
pgp_mpi_free(m);
if (c1)
pgp_mpi_free(c1);
if (c2)
pgp_mpi_free(c2);
return res;
}