Browse Source

Backup in case laptop explodes

master
David Lanzendörfer 2 weeks ago
parent
commit
4c813b4c5d
6 changed files with 149 additions and 78 deletions
  1. +12
    -2
      src/create.c
  2. +13
    -1
      src/crypto.c
  3. +15
    -3
      src/keyring.c
  4. +75
    -60
      src/packet-parse.c
  5. +15
    -3
      src/signature.c
  6. +19
    -9
      src/writer.c

+ 12
- 2
src/create.c View File

@ -189,10 +189,20 @@ mpi_length(const BIGNUM *bn)
return (unsigned)(2 +(BN_num_bits(bn)+7)/8);
}
#elif defined(HAVE_GSKSSL)
static unsigned gsk_num_bits ( gsk_buffer *bn )
{
unsigned r = (bn->length-1)*8;
uint8_t x = ((uint8_t*)bn->data)[0];
do {
r++;
} while (x >>= 1);
return r;
}
static unsigned
mpi_length(gsk_buffer *bn)
{
return (unsigned)(2 + bn->length);
return (unsigned)(2 +(gsk_num_bits(bn)+7)/8);
}
#endif
@ -344,7 +354,7 @@ write_seckey_body(const pgp_seckey_t *key, pgp_output_t *output)
pgp_hash_t hash;
unsigned done = 0;
unsigned i = 0;
uint8_t *sesskey;
uint8_t *sesskey = NULL;
memset(&hash, 0, sizeof(pgp_hash_t));
if (!write_pubkey_body(&key->pubkey, output)) {


+ 13
- 1
src/crypto.c View File

@ -76,6 +76,18 @@ __RCSID("$NetBSD$");
#include <netpgp/netpgpsdk.h>
#include <netpgp/validate.h>
#if defined(HAVE_GSKSSL)
static unsigned gsk_num_bits ( gsk_buffer *bn )
{
unsigned r = (bn->length-1)*8;
uint8_t x = ((uint8_t*)bn->data)[0];
do {
r++;
} while (x >>= 1);
return r;
}
#endif
/**
\ingroup Core_MPI
\brief Decrypt and unencode MPI
@ -130,7 +142,7 @@ pgp_decrypt_decode_mpi(uint8_t *buf,
#if defined(HAVE_OPENSSL)
(unsigned)(BN_num_bits(encmpi) + 7) / 8,
#elif defined(HAVE_GSKSSL)
(unsigned)encmpi->length,
(unsigned)(gsk_num_bits(encmpi) + 7) / 8,
#endif
&seckey->key.rsa, &seckey->pubkey.key.rsa);
if (n == -1) {


+ 15
- 3
src/keyring.c View File

@ -516,6 +516,18 @@ const uint8_t *pgp_key_get_primary_userid(pgp_key_t *key)
return NULL;
}
#if defined(HAVE_GSKSSL)
static unsigned gsk_num_bits ( gsk_buffer *bn )
{
unsigned r = (bn->length-1)*8;
uint8_t x = ((uint8_t*)bn->data)[0];
do {
r++;
} while (x >>= 1);
return r;
}
#endif
unsigned key_bit_len(const pgp_pubkey_t *key)
{
switch (key->alg) {
@ -530,13 +542,13 @@ unsigned key_bit_len(const pgp_pubkey_t *key)
return BN_num_bits(key->key.elgamal.p);
#elif defined(HAVE_GSKSSL)
case PGP_PKA_DSA:
return key->key.dsa.p->length*8-7;
return gsk_num_bits(key->key.dsa.p);
case PGP_PKA_RSA:
return key->key.rsa.n->length*8-7;
return gsk_num_bits(key->key.rsa.n);
case PGP_PKA_ELGAMAL:
return key->key.elgamal.p->length*8-7;
return gsk_num_bits(key->key.elgamal.p);
#endif
default:
return 0;


+ 75
- 60
src/packet-parse.c View File

@ -651,14 +651,17 @@ limread_mpi(BIGNUM **pbn, pgp_region_t *region, pgp_stream_t *stream)
limread_mpi(gsk_buffer **pbn, pgp_region_t *region, pgp_stream_t *stream)
#endif
{
uint8_t buf[NETPGP_BUFSIZ] = "";
/* an MPI has a 2 byte length part.
* Length is given in bits, so the
* largest we should ever need for
* the buffer is NETPGP_BUFSIZ bytes. */
uint8_t buf[NETPGP_BUFSIZ];
/*
* An MPI has a 2 byte length part.
* Length is given in bits, so the
* largest we should ever need for
* the buffer is NETPGP_BUFSIZ bytes.
*/
unsigned length;
unsigned nonzero;
unsigned ret;
memset(buf, 0, NETPGP_BUFSIZ);
ret = (unsigned)limread_scalar(&length, 2, region, stream);
@ -666,11 +669,15 @@ limread_mpi(gsk_buffer **pbn, pgp_region_t *region, pgp_stream_t *stream)
return 0;
}
nonzero = length & 7; /* there should be this many zero bits in the
* MS byte */
nonzero = length & 7;
/*
* There should be this many zero bits in the
* MS byte
*/
if (!nonzero)
nonzero = 8;
length = (length + 7) / 8;
length = ( length + 7 ) / 8;
if (length == 0) {
/* if we try to read a length of 0, then fail */
@ -684,9 +691,9 @@ limread_mpi(gsk_buffer **pbn, pgp_region_t *region, pgp_stream_t *stream)
return 0;
}
if (!limread(buf, length, region, stream)) {
printf("Read error\n");
return 0;
}
#if 0
if (((unsigned)buf[0] >> nonzero) != 0 || !((unsigned)buf[0] & (1U << (nonzero - 1U)))) {
printf("MPI Format error");
PGP_ERROR_1(&stream->errors, PGP_E_P_MPI_FORMAT_ERROR, "%s", "MPI Format error");
@ -695,22 +702,19 @@ limread_mpi(gsk_buffer **pbn, pgp_region_t *region, pgp_stream_t *stream)
* not apply to
* encrypted MPIs the
* draft says. -- peter */
hexdump(stdout,"buf",buf,length);
printf("%x\n",((unsigned)buf[0] >> nonzero));
printf("%x\n",(unsigned)buf[0]);
printf("%x\n",nonzero);
hexdump(stderr,"buf",buf,1);
hexdump(stdout, "buf", buf, length);
printf("%x\n", ((unsigned)buf[0] >> nonzero));
printf("%x\n", (unsigned)buf[0]);
printf("%x\n", nonzero);
return 0;
}
#endif
#if defined(HAVE_OPENSSL)
*pbn = BN_bin2bn(buf, (int)length, NULL);
#elif defined(HAVE_GSKSSL)
(*pbn)=malloc(sizeof(gsk_buffer));
(*pbn)->length=length;
(*pbn)->data=malloc(length);
memcpy((*pbn)->data,buf,length);
(*pbn) = malloc(sizeof(gsk_buffer));
(*pbn)->length = length;
(*pbn)->data = malloc(length);
memcpy((*pbn)->data, buf, length);
#endif
return 1;
}
@ -722,34 +726,44 @@ sos_read(BIGNUM **pbn, pgp_region_t *region, pgp_stream_t *stream)
sos_read(gsk_buffer **pbn, pgp_region_t *region, pgp_stream_t *stream)
#endif
{
uint8_t buf[NETPGP_BUFSIZ] = "";
/* an MPI has a 2 byte length part.
* Length is given in bits, so the
* largest we should ever need for
* the buffer is NETPGP_BUFSIZ bytes. */
uint8_t buf[NETPGP_BUFSIZ];
/*
* An MPI has a 2 byte length part.
* Length is given in bits, so the
* largest we should ever need for
* the buffer is NETPGP_BUFSIZ bytes.
*/
uint8_t c, c1, c2;
int32_t i, ret;
unsigned int nbits, nbytes;
size_t nread = 0;
uint8_t *p;
ret=limread(&c1, 1, region, stream);
if(!ret) return 0;
*pbn = NULL;
ret = limread(&c1, 1, region, stream);
if(!ret) {
return 0;
}
c = c1;
nbits = c << 8;
ret=limread(&c2, 1, region, stream);
if(!ret) return 0;
ret = limread(&c2, 1, region, stream);
if(!ret) {
return 0;
}
c = c2;
nbits |= c;
nbytes = (nbits + 7) / 8;
p = buf;
for (i = 0; i < nbytes; i++)
{
for (i = 0; i < nbytes; i++) {
ret = limread(&c, 1, region, stream);
if (!ret)
break;
if (!ret) {
break;
}
p[i] = c;
nread ++;
}
@ -757,10 +771,10 @@ sos_read(gsk_buffer **pbn, pgp_region_t *region, pgp_stream_t *stream)
#if defined(HAVE_OPENSSL)
*pbn = BN_bin2bn(buf, nread, NULL);
#elif defined(HAVE_GSKSSL)
(*pbn)=malloc(sizeof(gsk_buffer));
(*pbn)->length=nread;
(*pbn)->data=malloc(nread);
memcpy((*pbn)->data,buf,nread);
(*pbn) = malloc(sizeof(gsk_buffer));
(*pbn)->length = nread;
(*pbn)->data = malloc(nread);
memcpy((*pbn)->data, buf, nread);
#endif
return 1;
@ -1009,7 +1023,7 @@ copy_sig_info(pgp_sig_info_t *dst, const pgp_sig_info_t *src)
#if defined(HAVE_OPENSSL)
dup_BN(&dst->sig.rsa.sig, src->sig.rsa.sig);
#elif defined(HAVE_GSKSSL)
dst->sig.rsa.sig=malloc(sizeof(gsk_buffer));
dst->sig.rsa.sig = malloc(sizeof(gsk_buffer));
gsk_copy_buffer(src->sig.rsa.sig, dst->sig.rsa.sig);
#endif
break;
@ -1019,8 +1033,8 @@ copy_sig_info(pgp_sig_info_t *dst, const pgp_sig_info_t *src)
dup_BN(&dst->sig.dsa.r, src->sig.dsa.r);
dup_BN(&dst->sig.dsa.s, src->sig.dsa.s);
#elif defined(HAVE_GSKSSL)
dst->sig.dsa.r=malloc(sizeof(gsk_buffer));
dst->sig.dsa.s=malloc(sizeof(gsk_buffer));
dst->sig.dsa.r = malloc(sizeof(gsk_buffer));
dst->sig.dsa.s = malloc(sizeof(gsk_buffer));
gsk_copy_buffer(src->sig.dsa.r, dst->sig.dsa.r);
gsk_copy_buffer(src->sig.dsa.s, dst->sig.dsa.s);
#endif
@ -1031,8 +1045,8 @@ copy_sig_info(pgp_sig_info_t *dst, const pgp_sig_info_t *src)
dup_BN(&dst->sig.elgamal.r, src->sig.elgamal.r);
dup_BN(&dst->sig.elgamal.s, src->sig.elgamal.s);
#elif defined(HAVE_GSKSSL)
dst->sig.elgamal.r=malloc(sizeof(gsk_buffer));
dst->sig.elgamal.s=malloc(sizeof(gsk_buffer));
dst->sig.elgamal.r = malloc(sizeof(gsk_buffer));
dst->sig.elgamal.s = malloc(sizeof(gsk_buffer));
gsk_copy_buffer(src->sig.elgamal.r, dst->sig.elgamal.r);
gsk_copy_buffer(src->sig.elgamal.s, dst->sig.elgamal.s);
#endif
@ -1452,8 +1466,8 @@ pgp_pubkey_dup(pgp_pubkey_t *dst, pgp_pubkey_t *src)
dup_BN(&dst->key.rsa.n, src->key.rsa.n);
dup_BN(&dst->key.rsa.e, src->key.rsa.e);
#elif defined(HAVE_GSKSSL)
dst->key.rsa.n=malloc(sizeof(gsk_buffer));
dst->key.rsa.e=malloc(sizeof(gsk_buffer));
dst->key.rsa.n = malloc(sizeof(gsk_buffer));
dst->key.rsa.e = malloc(sizeof(gsk_buffer));
gsk_copy_buffer(src->key.rsa.n, dst->key.rsa.n);
gsk_copy_buffer(src->key.rsa.e, dst->key.rsa.e);
#endif
@ -1466,10 +1480,10 @@ pgp_pubkey_dup(pgp_pubkey_t *dst, pgp_pubkey_t *src)
dup_BN(&dst->key.dsa.g, src->key.dsa.g);
dup_BN(&dst->key.dsa.y, src->key.dsa.y);
#elif defined(HAVE_GSKSSL)
dst->key.dsa.p=malloc(sizeof(gsk_buffer));
dst->key.dsa.q=malloc(sizeof(gsk_buffer));
dst->key.dsa.g=malloc(sizeof(gsk_buffer));
dst->key.dsa.y=malloc(sizeof(gsk_buffer));
dst->key.dsa.p = malloc(sizeof(gsk_buffer));
dst->key.dsa.q = malloc(sizeof(gsk_buffer));
dst->key.dsa.g = malloc(sizeof(gsk_buffer));
dst->key.dsa.y = malloc(sizeof(gsk_buffer));
gsk_copy_buffer(src->key.dsa.p, dst->key.dsa.p);
gsk_copy_buffer(src->key.dsa.q, dst->key.dsa.q);
gsk_copy_buffer(src->key.dsa.g, dst->key.dsa.g);
@ -1484,9 +1498,9 @@ pgp_pubkey_dup(pgp_pubkey_t *dst, pgp_pubkey_t *src)
dup_BN(&dst->key.elgamal.g, src->key.elgamal.g);
dup_BN(&dst->key.elgamal.y, src->key.elgamal.y);
#elif defined(HAVE_GSKSSL)
dst->key.elgamal.p=malloc(sizeof(gsk_buffer));
dst->key.elgamal.g=malloc(sizeof(gsk_buffer));
dst->key.elgamal.y=malloc(sizeof(gsk_buffer));
dst->key.elgamal.p = malloc(sizeof(gsk_buffer));
dst->key.elgamal.g = malloc(sizeof(gsk_buffer));
dst->key.elgamal.y = malloc(sizeof(gsk_buffer));
gsk_copy_buffer(src->key.elgamal.p, dst->key.elgamal.p);
gsk_copy_buffer(src->key.elgamal.g, dst->key.elgamal.g);
gsk_copy_buffer(src->key.elgamal.y, dst->key.elgamal.y);
@ -2702,10 +2716,10 @@ pgp_seckey_dup(pgp_seckey_t *dst, pgp_seckey_t *src)
dup_BN(&dst->key.rsa.q, src->key.rsa.q);
dup_BN(&dst->key.rsa.u, src->key.rsa.u);
#elif defined(HAVE_GSKSSL)
dst->key.rsa.d=malloc(sizeof(gsk_buffer));
dst->key.rsa.p=malloc(sizeof(gsk_buffer));
dst->key.rsa.q=malloc(sizeof(gsk_buffer));
dst->key.rsa.u=malloc(sizeof(gsk_buffer));
dst->key.rsa.d = malloc(sizeof(gsk_buffer));
dst->key.rsa.p = malloc(sizeof(gsk_buffer));
dst->key.rsa.q = malloc(sizeof(gsk_buffer));
dst->key.rsa.u = malloc(sizeof(gsk_buffer));
gsk_copy_buffer(src->key.rsa.d, dst->key.rsa.d);
gsk_copy_buffer(src->key.rsa.p, dst->key.rsa.p);
gsk_copy_buffer(src->key.rsa.q, dst->key.rsa.q);
@ -2717,7 +2731,7 @@ pgp_seckey_dup(pgp_seckey_t *dst, pgp_seckey_t *src)
#if defined(HAVE_OPENSSL)
dup_BN(&dst->key.dsa.x, src->key.dsa.x);
#elif defined(HAVE_GSKSSL)
dst->key.dsa.x=malloc(sizeof(gsk_buffer));
dst->key.dsa.x = malloc(sizeof(gsk_buffer));
gsk_copy_buffer(src->key.dsa.x, dst->key.dsa.x);
#endif
break;
@ -2808,7 +2822,7 @@ parse_seckey(pgp_content_enum tag, pgp_region_t *region, pgp_stream_t *stream)
}
if (pgp_get_debug_level(__FILE__)) {
printf(stderr, "parse_seckey: public key parsed\n");
fprintf(stderr, "parse_seckey: public key parsed\n");
}
//stream->reading_v3_secret = (pkt.u.seckey.pubkey.version != PGP_V4);
if (pkt.u.seckey.pubkey.version != PGP_V4){
@ -3127,11 +3141,12 @@ parse_pk_sesskey(pgp_region_t *region,
BIGNUM *g_to_k;
BIGNUM *enc_m;
#elif defined(HAVE_GSKSSL)
gsk_buffer *g_to_k;
gsk_buffer *enc_m;
gsk_buffer *g_to_k = NULL;
gsk_buffer *enc_m = NULL;
#endif
int n;
uint8_t unencoded_m_buf[1024];
memset(unencoded_m_buf, 0, 1024);
if (!limread(&c, 1, region, stream)) {
(void) fprintf(stderr, "parse_pk_sesskey - can't read char in region\n");


+ 15
- 3
src/signature.c View File

@ -144,6 +144,18 @@ static uint8_t prefix_sha512[] = {
0x00, 0x04, 0x40
};
#if defined(HAVE_GSKSSL)
static unsigned gsk_num_bits ( gsk_buffer *bn )
{
unsigned r = (bn->length-1)*8;
uint8_t x = ((uint8_t*)bn->data)[0];
do {
r++;
} while (x >>= 1);
return r;
}
#endif
/* XXX: both this and verify would be clearer if the signature were */
/* treated as an MPI. */
static int
@ -186,7 +198,7 @@ rsa_sign(pgp_hash_t *hash,
#if defined(HAVE_OPENSSL)
keysize = (BN_num_bits(pubrsa->n) + 7) / 8;
#elif defined(HAVE_GSKSSL)
keysize = pubrsa->n->length;
keysize = (gsk_num_bits(pubrsa->n) + 7) / 8;
#endif
if (keysize > sizeof(hashbuf)) {
@ -321,13 +333,13 @@ rsa_verify(pgp_hash_alg_t type,
BN_bn2bin(sig->sig, sigbuf);
n = pgp_rsa_public_decrypt(hashbuf_from_sig, sigbuf, (unsigned)(BN_num_bits(sig->sig) + 7) / 8, pubrsa);
#elif defined(HAVE_GSKSSL)
if ((unsigned) sig->sig->length > sizeof(sigbuf)) {
if ((unsigned) gsk_num_bits(sig->sig) > 8 * sizeof(sigbuf)) {
(void) fprintf(stderr, "rsa_verify: numbits too big\n");
return 0;
}
memcpy(sigbuf, sig->sig->data, sig->sig->length);
memcpy(hashbuf_from_sig, hash, hash_length);
n = pgp_rsa_public_decrypt(hashbuf_from_sig, sigbuf, sig->sig->length, pubrsa);
n = pgp_rsa_public_decrypt(hashbuf_from_sig, sigbuf, (gsk_num_bits(sig->sig) + 7 ) / 8, pubrsa);
#endif
debug_len_decrypted = n;


+ 19
- 9
src/writer.c View File

@ -117,8 +117,7 @@ pgp_write(pgp_output_t *output, const void *src, unsigned len)
unsigned
pgp_write_scalar(pgp_output_t *output, unsigned n, unsigned len)
{
uint8_t c;
uint8_t c;
while (len-- > 0) {
c = n >> (len * 8);
if (!base_write(output, &c, 1)) {
@ -128,6 +127,18 @@ pgp_write_scalar(pgp_output_t *output, unsigned n, unsigned len)
return 1;
}
#if defined(HAVE_GSKSSL)
static unsigned gsk_num_bits ( gsk_buffer *bn )
{
unsigned r = (bn->length-1)*8;
uint8_t x = ((uint8_t*)bn->data)[0];
do {
r++;
} while (x >>= 1);
return r;
}
#endif
/**
* \ingroup Core_WritePackets
* \param bn
@ -135,18 +146,17 @@ pgp_write_scalar(pgp_output_t *output, unsigned n, unsigned len)
* \return 1 if OK, otherwise 0
*/
unsigned
#ifdef HAVE_OPENSSL
#if defined(HAVE_OPENSSL)
pgp_write_mpi(pgp_output_t *output, const BIGNUM *bn)
#endif
#ifdef HAVE_GSKSSL
#elif defined(HAVE_GSKSSL)
pgp_write_mpi(pgp_output_t *output, gsk_buffer *bn)
#endif
{
unsigned bits;
uint8_t buf[NETPGP_BUFSIZ];
memset(buf,0,NETPGP_BUFSIZ);
memset(buf, 0, NETPGP_BUFSIZ);
#if defined(HAVE_GSKSSL)
bits = (unsigned)((bn->length*8)-((bn->length%2)*7));
bits = (unsigned)gsk_num_bits(bn);
#elif defined(HAVE_OPENSSL)
bits = (unsigned)BN_num_bits(bn);
#endif
@ -157,7 +167,7 @@ pgp_write_mpi(pgp_output_t *output, gsk_buffer *bn)
#if defined(HAVE_OPENSSL)
BN_bn2bin(bn, buf);
#elif defined(HAVE_GSKSSL)
memcpy(buf,bn->data,bn->length);
memcpy(buf, bn->data, (bits + 7) / 8);
#endif
return pgp_write_scalar(output, bits, 2) && pgp_write(output, buf, (bits + 7) / 8);
}
@ -187,7 +197,7 @@ pgp_write_ptag(pgp_output_t *output, pgp_content_enum tag)
unsigned
pgp_write_length(pgp_output_t *output, unsigned len)
{
uint8_t c[2];
uint8_t c[2];
if (len < 192) {
c[0] = len;
return base_write(output, c, 1);


Loading…
Cancel
Save