Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/crypto/external/bsd/netpgp/dist/src/lib Changes to 3.99.15/2...



details:   https://anonhg.NetBSD.org/src/rev/2fb3aa946751
branches:  trunk
changeset: 758605:2fb3aa946751
user:      agc <agc%NetBSD.org@localhost>
date:      Thu Nov 11 00:58:04 2010 +0000

description:
Changes to 3.99.15/20101110

+ add support for partial blocks, defined in rfc 4880, and used fairly
extensively by gnupg where the input size may not be known in advance
(e.g. for encrypted compressed data, as produced by default by gpg -e)

diffstat:

 crypto/external/bsd/netpgp/dist/src/lib/compress.c     |   10 +-
 crypto/external/bsd/netpgp/dist/src/lib/crypto.h       |    6 +
 crypto/external/bsd/netpgp/dist/src/lib/misc.c         |    6 +-
 crypto/external/bsd/netpgp/dist/src/lib/packet-parse.c |  157 ++++++++++++---
 crypto/external/bsd/netpgp/dist/src/lib/packet-parse.h |    6 +-
 crypto/external/bsd/netpgp/dist/src/lib/packet.h       |    6 +-
 crypto/external/bsd/netpgp/dist/src/lib/reader.c       |  173 ++++++++++------
 crypto/external/bsd/netpgp/dist/src/lib/validate.c     |    5 +-
 crypto/external/bsd/netpgp/dist/src/lib/version.h      |    2 +-
 9 files changed, 249 insertions(+), 122 deletions(-)

diffs (truncated from 1112 to 300 lines):

diff -r c88823307cb6 -r 2fb3aa946751 crypto/external/bsd/netpgp/dist/src/lib/compress.c
--- a/crypto/external/bsd/netpgp/dist/src/lib/compress.c        Wed Nov 10 22:37:23 2010 +0000
+++ b/crypto/external/bsd/netpgp/dist/src/lib/compress.c        Thu Nov 11 00:58:04 2010 +0000
@@ -57,7 +57,7 @@
 
 #if defined(__NetBSD__)
 __COPYRIGHT("@(#) Copyright (c) 2009 The NetBSD Foundation, Inc. All rights reserved.");
-__RCSID("$NetBSD: compress.c,v 1.19 2010/11/07 08:39:59 agc Exp $");
+__RCSID("$NetBSD: compress.c,v 1.20 2010/11/11 00:58:04 agc Exp $");
 #endif
 
 #ifdef HAVE_ZLIB_H
@@ -112,7 +112,7 @@
  * bzip2_compressed_data_reader
  */
 static int 
-zlib_compressed_data_reader(void *dest, size_t length,
+zlib_compressed_data_reader(pgp_stream_t *stream, void *dest, size_t length,
                            pgp_error_t **errors,
                            pgp_reader_t *readinfo,
                            pgp_cbdata_t *cbinfo)
@@ -164,7 +164,7 @@
                                } else {
                                        n = sizeof(z->in);
                                }
-                               if (!pgp_stacked_limited_read(z->in, n,
+                               if (!pgp_stacked_limited_read(stream, z->in, n,
                                                z->region,
                                                errors, readinfo, cbinfo)) {
                                        return -1;
@@ -207,7 +207,7 @@
 #ifdef HAVE_BZLIB_H
 /* \todo remove code duplication between this and zlib_compressed_data_reader */
 static int 
-bzip2_compressed_data_reader(void *dest, size_t length,
+bzip2_compressed_data_reader(pgp_stream_t *stream, void *dest, size_t length,
                             pgp_error_t **errors,
                             pgp_reader_t *readinfo,
                             pgp_cbdata_t *cbinfo)
@@ -249,7 +249,7 @@
                                } else
                                        n = sizeof(bz->in);
 
-                               if (!pgp_stacked_limited_read(
+                               if (!pgp_stacked_limited_read(stream,
                                                (uint8_t *) bz->in,
                                                n, bz->region,
                                                errors, readinfo, cbinfo))
diff -r c88823307cb6 -r 2fb3aa946751 crypto/external/bsd/netpgp/dist/src/lib/crypto.h
--- a/crypto/external/bsd/netpgp/dist/src/lib/crypto.h  Wed Nov 10 22:37:23 2010 +0000
+++ b/crypto/external/bsd/netpgp/dist/src/lib/crypto.h  Thu Nov 11 00:58:04 2010 +0000
@@ -300,6 +300,12 @@
        unsigned                 reading_v3_secret:1;
        unsigned                 reading_mpi_len:1;
        unsigned                 exact_read:1;
+       unsigned                 partial_read:1;
+       unsigned                 coalescing:1;
+       /* used for partial length coalescing */
+       unsigned                 virtualc;
+       unsigned                 virtualoff;
+       uint8_t                 *virtualpkt;
 };
 
 #endif /* CRYPTO_H_ */
diff -r c88823307cb6 -r 2fb3aa946751 crypto/external/bsd/netpgp/dist/src/lib/misc.c
--- a/crypto/external/bsd/netpgp/dist/src/lib/misc.c    Wed Nov 10 22:37:23 2010 +0000
+++ b/crypto/external/bsd/netpgp/dist/src/lib/misc.c    Thu Nov 11 00:58:04 2010 +0000
@@ -57,7 +57,7 @@
 
 #if defined(__NetBSD__)
 __COPYRIGHT("@(#) Copyright (c) 2009 The NetBSD Foundation, Inc. All rights reserved.");
-__RCSID("$NetBSD: misc.c,v 1.38 2010/11/07 08:39:59 agc Exp $");
+__RCSID("$NetBSD: misc.c,v 1.39 2010/11/11 00:58:04 agc Exp $");
 #endif
 
 #include <sys/types.h>
@@ -1159,7 +1159,7 @@
 }
 
 static int 
-sum16_reader(void *dest_, size_t length, pgp_error_t **errors,
+sum16_reader(pgp_stream_t *stream, void *dest_, size_t length, pgp_error_t **errors,
             pgp_reader_t *readinfo, pgp_cbdata_t *cbinfo)
 {
        const uint8_t   *dest = dest_;
@@ -1167,7 +1167,7 @@
        int              r;
        int              n;
 
-       r = pgp_stacked_read(dest_, length, errors, readinfo, cbinfo);
+       r = pgp_stacked_read(stream, dest_, length, errors, readinfo, cbinfo);
        if (r < 0) {
                return r;
        }
diff -r c88823307cb6 -r 2fb3aa946751 crypto/external/bsd/netpgp/dist/src/lib/packet-parse.c
--- a/crypto/external/bsd/netpgp/dist/src/lib/packet-parse.c    Wed Nov 10 22:37:23 2010 +0000
+++ b/crypto/external/bsd/netpgp/dist/src/lib/packet-parse.c    Thu Nov 11 00:58:04 2010 +0000
@@ -58,9 +58,12 @@
 
 #if defined(__NetBSD__)
 __COPYRIGHT("@(#) Copyright (c) 2009 The NetBSD Foundation, Inc. All rights reserved.");
-__RCSID("$NetBSD: packet-parse.c,v 1.45 2010/11/07 08:39:59 agc Exp $");
+__RCSID("$NetBSD: packet-parse.c,v 1.46 2010/11/11 00:58:04 agc Exp $");
 #endif
 
+#include <sys/types.h>
+#include <sys/param.h>
+
 #ifdef HAVE_OPENSSL_CAST_H
 #include <openssl/cast.h>
 #endif
@@ -122,7 +125,7 @@
                return 0;
        }
 
-       return pgp_limited_read(data->contents, data->len, subregion,
+       return pgp_limited_read(stream, data->contents, data->len, subregion,
                        &stream->errors, &stream->readinfo, &stream->cbinfo);
 }
 
@@ -161,7 +164,7 @@
                return 0;
        }
        if (len &&
-           !pgp_limited_read(*str, len, subregion, &stream->errors,
+           !pgp_limited_read(stream, *str, len, subregion, &stream->errors,
                                     &stream->readinfo, &stream->cbinfo)) {
                return 0;
        }
@@ -216,7 +219,7 @@
  */
 
 static int 
-sub_base_read(void *dest, size_t length, pgp_error_t **errors,
+sub_base_read(pgp_stream_t *stream, void *dest, size_t length, pgp_error_t **errors,
              pgp_reader_t *readinfo, pgp_cbdata_t *cbinfo)
 {
        size_t          n;
@@ -228,8 +231,8 @@
        for (n = 0; n < length;) {
                int     r;
 
-               r = readinfo->reader((char *) dest + n, length - n, errors,
-                               readinfo, cbinfo);
+               r = readinfo->reader(stream, (char *) dest + n, length - n, errors,
+                       readinfo, cbinfo);
                if (r > (int)(length - n)) {
                        (void) fprintf(stderr, "sub_base_read: bad read\n");
                        return 0;
@@ -279,17 +282,17 @@
 }
 
 int 
-pgp_stacked_read(void *dest, size_t length, pgp_error_t **errors,
+pgp_stacked_read(pgp_stream_t *stream, void *dest, size_t length, pgp_error_t **errors,
                 pgp_reader_t *readinfo, pgp_cbdata_t *cbinfo)
 {
-       return sub_base_read(dest, length, errors, readinfo->next, cbinfo);
+       return sub_base_read(stream, dest, length, errors, readinfo->next, cbinfo);
 }
 
 /* This will do a full read so long as length < MAX_INT */
 static int 
 base_read(uint8_t *dest, size_t length, pgp_stream_t *stream)
 {
-       return sub_base_read(dest, length, &stream->errors, &stream->readinfo,
+       return sub_base_read(stream, dest, length, &stream->errors, &stream->readinfo,
                             &stream->cbinfo);
 }
 
@@ -299,7 +302,7 @@
  */
 
 static size_t 
-full_read(uint8_t *dest,
+full_read(pgp_stream_t *stream, uint8_t *dest,
                size_t length,
                int *last_read,
                pgp_error_t **errors,
@@ -311,7 +314,7 @@
                                 * == 0 */
 
        for (t = 0; t < length;) {
-               r = sub_base_read(dest + t, length - t, errors, readinfo,
+               r = sub_base_read(stream, dest + t, length - t, errors, readinfo,
                                cbinfo);
                if (r <= 0) {
                        *last_read = r;
@@ -391,7 +394,7 @@
  * \return             1 on success, 0 on error
  */
 unsigned 
-pgp_limited_read(uint8_t *dest,
+pgp_limited_read(pgp_stream_t *stream, uint8_t *dest,
                        size_t length,
                        pgp_region_t *region,
                        pgp_error_t **errors,
@@ -406,7 +409,7 @@
                PGP_ERROR(errors, PGP_E_P_NOT_ENOUGH_DATA, "Not enough data");
                return 0;
        }
-       r = full_read(dest, length, &lr, errors, readinfo, cbinfo);
+       r = full_read(stream, dest, length, &lr, errors, readinfo, cbinfo);
        if (lr < 0) {
                PGP_ERROR(errors, PGP_E_R_READ_FAILED, "Read failed");
                return 0;
@@ -432,13 +435,13 @@
    \brief Call pgp_limited_read on next in stack
 */
 unsigned 
-pgp_stacked_limited_read(uint8_t *dest, unsigned length,
+pgp_stacked_limited_read(pgp_stream_t *stream, uint8_t *dest, unsigned length,
                         pgp_region_t *region,
                         pgp_error_t **errors,
                         pgp_reader_t *readinfo,
                         pgp_cbdata_t *cbinfo)
 {
-       return pgp_limited_read(dest, length, region, errors,
+       return pgp_limited_read(stream, dest, length, region, errors,
                                readinfo->next, cbinfo);
 }
 
@@ -446,7 +449,7 @@
 limread(uint8_t *dest, unsigned length,
             pgp_region_t *region, pgp_stream_t *info)
 {
-       return pgp_limited_read(dest, length, region, &info->errors,
+       return pgp_limited_read(info, dest, length, region, &info->errors,
                                &info->readinfo, &info->cbinfo);
 }
 
@@ -692,6 +695,39 @@
        return 1;
 }
 
+static unsigned read_new_length(unsigned *, pgp_stream_t *);
+
+/* allocate space, read, and stash data away in a virtual pkt */
+static void
+streamread(pgp_stream_t *stream, unsigned c)
+{
+       int     cc;
+
+       stream->virtualpkt = realloc(stream->virtualpkt, stream->virtualc + c);
+       cc = stream->readinfo.reader(stream, &stream->virtualpkt[stream->virtualc],
+               c, &stream->errors, &stream->readinfo, &stream->cbinfo);
+       stream->virtualc += cc;
+}
+
+/* coalesce all the partial blocks together */
+static int
+coalesce_blocks(pgp_stream_t *stream, unsigned length)
+{
+       unsigned        c;
+
+       stream->coalescing = 1;
+       /* already read a partial block length - prime the array */
+       streamread(stream, length);
+       while (read_new_length(&c, stream) && stream->partial_read) {
+               /* length we read is partial - add to end of array */
+               streamread(stream, c);
+       }
+       /* not partial - add the last extent to the end of the array */
+       streamread(stream, c);
+       stream->coalescing = 0;
+       return 1;
+}
+
 /** Read some data with a New-Format length from reader.
  *
  * \sa Internet-Draft RFC4880.txt Section 4.2.2
@@ -707,31 +743,39 @@
 {
        uint8_t   c;
 
-       if (base_read(&c, 1, stream) != 1)
+       stream->partial_read = 0;
+       if (base_read(&c, 1, stream) != 1) {
                return 0;
+       }
        if (c < 192) {
                /* 1. One-octet packet */
                *length = c;
                return 1;
-       } else if (c >= 192 && c <= 223) {
+       }
+       if (c < 224) {
                /* 2. Two-octet packet */
                unsigned        t = (c - 192) << 8;
 
-               if (base_read(&c, 1, stream) != 1)
+               if (base_read(&c, 1, stream) != 1) {
                        return 0;
+               }
                *length = t + c + 192;
                return 1;
-       } else if (c == 255) {
-               /* 3. Five-Octet packet */
-               return _read_scalar(length, 4, stream);
-       } else if (c >= 224 && c < 255) {



Home | Main Index | Thread Index | Old Index