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 More checking of all...



details:   https://anonhg.NetBSD.org/src/rev/a541f0fca835
branches:  trunk
changeset: 747974:a541f0fca835
user:      agc <agc%NetBSD.org@localhost>
date:      Wed Oct 07 16:19:51 2009 +0000

description:
More checking of allocation return values where not already done.

Revamp hash initialisation to return a success/failure error code.

Document places where we prefer to continue with a NULL buffer,
rather than silently continue with possibly erroneous results.

diffstat:

 crypto/external/bsd/netpgp/dist/src/lib/create.c         |   7 +-
 crypto/external/bsd/netpgp/dist/src/lib/crypto.h         |   2 +-
 crypto/external/bsd/netpgp/dist/src/lib/misc.c           |  51 ++++++++++++---
 crypto/external/bsd/netpgp/dist/src/lib/openssl_crypto.c |  50 +++++++++++----
 crypto/external/bsd/netpgp/dist/src/lib/packet-parse.c   |  14 +++-
 crypto/external/bsd/netpgp/dist/src/lib/reader.c         |  20 +++++-
 crypto/external/bsd/netpgp/dist/src/lib/signature.c      |   9 ++-
 crypto/external/bsd/netpgp/dist/src/lib/validate.c       |   7 +-
 crypto/external/bsd/netpgp/dist/src/lib/writer.c         |  16 ++++-
 9 files changed, 135 insertions(+), 41 deletions(-)

diffs (truncated from 473 to 300 lines):

diff -r b64922aee59d -r a541f0fca835 crypto/external/bsd/netpgp/dist/src/lib/create.c
--- a/crypto/external/bsd/netpgp/dist/src/lib/create.c  Wed Oct 07 13:19:36 2009 +0000
+++ b/crypto/external/bsd/netpgp/dist/src/lib/create.c  Wed Oct 07 16:19:51 2009 +0000
@@ -57,7 +57,7 @@
 
 #if defined(__NetBSD__)
 __COPYRIGHT("@(#) Copyright (c) 2009 The NetBSD Foundation, Inc. All rights reserved.");
-__RCSID("$NetBSD: create.c,v 1.20 2009/10/06 02:26:05 agc Exp $");
+__RCSID("$NetBSD: create.c,v 1.21 2009/10/07 16:19:51 agc Exp $");
 #endif
 
 #include <sys/types.h>
@@ -371,7 +371,10 @@
                        size = MIN(needed, OPS_SHA1_HASH_SIZE);
 
                        __ops_hash_any(&hash, key->hash_alg);
-                       hash.init(&hash);
+                       if (!hash.init(&hash)) {
+                               (void) fprintf(stderr, "write_seckey_body: bad alloc\n");
+                               return 0;
+                       }
 
                        /* preload if iterating  */
                        for (j = 0; j < i; j++) {
diff -r b64922aee59d -r a541f0fca835 crypto/external/bsd/netpgp/dist/src/lib/crypto.h
--- a/crypto/external/bsd/netpgp/dist/src/lib/crypto.h  Wed Oct 07 13:19:36 2009 +0000
+++ b/crypto/external/bsd/netpgp/dist/src/lib/crypto.h  Wed Oct 07 16:19:51 2009 +0000
@@ -61,7 +61,7 @@
 
 #define OPS_MIN_HASH_SIZE      16
 
-typedef void __ops_hash_init_t(__ops_hash_t *);
+typedef int __ops_hash_init_t(__ops_hash_t *);
 typedef void __ops_hash_add_t(__ops_hash_t *, const unsigned char *, unsigned);
 typedef unsigned __ops_hash_finish_t(__ops_hash_t *, unsigned char *);
 
diff -r b64922aee59d -r a541f0fca835 crypto/external/bsd/netpgp/dist/src/lib/misc.c
--- a/crypto/external/bsd/netpgp/dist/src/lib/misc.c    Wed Oct 07 13:19:36 2009 +0000
+++ b/crypto/external/bsd/netpgp/dist/src/lib/misc.c    Wed Oct 07 16:19:51 2009 +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.21 2009/10/06 02:39:53 agc Exp $");
+__RCSID("$NetBSD: misc.c,v 1.22 2009/10/07 16:19:51 agc Exp $");
 #endif
 
 #include <sys/types.h>
@@ -439,7 +439,11 @@
                }
 
                __ops_hash_md5(&md5);
-               md5.init(&md5);
+               if (!md5.init(&md5)) {
+                       (void) fprintf(stderr,
+                               "__ops_fingerprint: bad md5 alloc\n");
+                               return;
+               }
 
                n = (size_t) BN_num_bytes(key->key.rsa.n);
                if ((bn = calloc(1, n)) == NULL) {
@@ -474,7 +478,11 @@
                        fprintf(stderr, "-> creating key fingerprint\n");
                }
                __ops_hash_sha1(&sha1);
-               sha1.init(&sha1);
+               if (!sha1.init(&sha1)) {
+                       (void) fprintf(stderr,
+                               "__ops_fingerprint: bad sha1 alloc\n");
+                       return;
+               }
 
                len = __ops_mem_len(mem);
 
@@ -669,7 +677,11 @@
        __ops_hash_t      hash;
 
        __ops_hash_any(&hash, alg);
-       hash.init(&hash);
+       if (!hash.init(&hash)) {
+               (void) fprintf(stderr, "__ops_hash: bad alloc\n");
+               /* we'll just continue here - don't want to return a 0 hash */
+               /* XXX - agc - no way to return failure */
+       }
        hash.add(&hash, in, length);
        return hash.finish(&hash, out);
 }
@@ -708,7 +720,11 @@
        }
        /* init */
        __ops_hash_any(&hash, OPS_HASH_SHA1);
-       hash.init(&hash);
+       if (!hash.init(&hash)) {
+               (void) fprintf(stderr, "__ops_calc_mdc_hash: bad alloc\n");
+               /* we'll just continue here - it will die anyway */
+               /* agc - XXX - no way to return failure */
+       }
 
        /* preamble */
        hash.add(&hash, preamble, sz_preamble);
@@ -771,16 +787,25 @@
 void 
 __ops_memory_init(__ops_memory_t *mem, size_t needed)
 {
+       unsigned char   *temp;
+
        mem->length = 0;
        if (mem->buf) {
                if (mem->allocated < needed) {
-                       mem->buf = realloc(mem->buf, needed);
+                       if ((temp = realloc(mem->buf, needed)) == NULL) {
+                               (void) fprintf(stderr, "__ops_memory_init: bad alloc\n");
+                       } else {
+                               mem->buf = temp;
+                               mem->allocated = needed;
+                       }
+               }
+       } else {
+               if ((mem->buf = calloc(1, needed)) == NULL) {
+                       (void) fprintf(stderr, "__ops_memory_init: bad alloc\n");
+               } else {
                        mem->allocated = needed;
                }
-               return;
        }
-       mem->buf = calloc(1, needed);
-       mem->allocated = needed;
 }
 
 /**
@@ -1100,9 +1125,13 @@
 void 
 __ops_reader_push_sum16(__ops_stream_t *stream)
 {
-       sum16_t    *arg = calloc(1, sizeof(*arg));
+       sum16_t    *arg;
 
-       __ops_reader_push(stream, sum16_reader, sum16_destroyer, arg);
+       if ((arg = calloc(1, sizeof(*arg))) == NULL) {
+               (void) fprintf(stderr, "__ops_reader_push_sum16: bad alloc\n");
+       } else {
+               __ops_reader_push(stream, sum16_reader, sum16_destroyer, arg);
+       }
 }
 
 /**
diff -r b64922aee59d -r a541f0fca835 crypto/external/bsd/netpgp/dist/src/lib/openssl_crypto.c
--- a/crypto/external/bsd/netpgp/dist/src/lib/openssl_crypto.c  Wed Oct 07 13:19:36 2009 +0000
+++ b/crypto/external/bsd/netpgp/dist/src/lib/openssl_crypto.c  Wed Oct 07 16:19:51 2009 +0000
@@ -57,7 +57,7 @@
 
 #if defined(__NetBSD__)
 __COPYRIGHT("@(#) Copyright (c) 2009 The NetBSD Foundation, Inc. All rights reserved.");
-__RCSID("$NetBSD: openssl_crypto.c,v 1.16 2009/10/06 03:30:59 agc Exp $");
+__RCSID("$NetBSD: openssl_crypto.c,v 1.17 2009/10/07 16:19:51 agc Exp $");
 #endif
 
 #ifdef HAVE_OPENSSL_DSA_H
@@ -102,14 +102,18 @@
        RSA_free(test);
 }
 
-static void 
+static int 
 md5_init(__ops_hash_t *hash)
 {
        if (hash->data) {
                (void) fprintf(stderr, "md5_init: hash data non-null\n");
        }
-       hash->data = calloc(1, sizeof(MD5_CTX));
+       if ((hash->data = calloc(1, sizeof(MD5_CTX))) == NULL) {
+               (void) fprintf(stderr, "md5_init: bad alloc\n");
+               return 0;
+       }
        MD5_Init(hash->data);
+       return 1;
 }
 
 static void 
@@ -148,7 +152,7 @@
        *hash = md5;
 }
 
-static void 
+static int 
 sha1_init(__ops_hash_t *hash)
 {
        if (__ops_get_debug_level(__FILE__)) {
@@ -157,8 +161,12 @@
        if (hash->data) {
                (void) fprintf(stderr, "sha1_init: hash data non-null\n");
        }
-       hash->data = calloc(1, sizeof(SHA_CTX));
+       if ((hash->data = calloc(1, sizeof(SHA_CTX))) == NULL) {
+               (void) fprintf(stderr, "sha1_init: bad alloc\n");
+               return 0;
+       }
        SHA1_Init(hash->data);
+       return 1;
 }
 
 static void 
@@ -219,7 +227,7 @@
        *hash = sha1;
 }
 
-static void 
+static int 
 sha256_init(__ops_hash_t *hash)
 {
        if (__ops_get_debug_level(__FILE__)) {
@@ -228,8 +236,12 @@
        if (hash->data) {
                (void) fprintf(stderr, "sha256_init: hash data non-null\n");
        }
-       hash->data = calloc(1, sizeof(SHA256_CTX));
+       if ((hash->data = calloc(1, sizeof(SHA256_CTX))) == NULL) {
+               (void) fprintf(stderr, "sha256_init: bad alloc\n");
+               return 0;
+       }
        SHA256_Init(hash->data);
+       return 1;
 }
 
 static void 
@@ -287,7 +299,7 @@
 /*
  * SHA384
  */
-static void 
+static int 
 sha384_init(__ops_hash_t *hash)
 {
        if (__ops_get_debug_level(__FILE__)) {
@@ -296,8 +308,12 @@
        if (hash->data) {
                (void) fprintf(stderr, "sha384_init: hash data non-null\n");
        }
-       hash->data = calloc(1, sizeof(SHA512_CTX));
+       if ((hash->data = calloc(1, sizeof(SHA512_CTX))) == NULL) {
+               (void) fprintf(stderr, "sha512_init: bad alloc\n");
+               return 0;
+       }
        SHA384_Init(hash->data);
+       return 1;
 }
 
 static void 
@@ -355,7 +371,7 @@
 /*
  * SHA512
  */
-static void 
+static int 
 sha512_init(__ops_hash_t *hash)
 {
        if (__ops_get_debug_level(__FILE__)) {
@@ -364,8 +380,12 @@
        if (hash->data) {
                (void) fprintf(stderr, "sha512_init: hash data non-null\n");
        }
-       hash->data = calloc(1, sizeof(SHA512_CTX));
+       if ((hash->data = calloc(1, sizeof(SHA512_CTX))) == NULL) {
+               (void) fprintf(stderr, "sha512_init: bad alloc\n");
+               return 0;
+       }
        SHA512_Init(hash->data);
+       return 1;
 }
 
 static void 
@@ -424,7 +444,7 @@
  * SHA224
  */
 
-static void 
+static int 
 sha224_init(__ops_hash_t *hash)
 {
        if (__ops_get_debug_level(__FILE__)) {
@@ -433,8 +453,12 @@
        if (hash->data) {
                (void) fprintf(stderr, "sha224_init: hash data non-null\n");
        }
-       hash->data = calloc(1, sizeof(SHA256_CTX));
+       if ((hash->data = calloc(1, sizeof(SHA256_CTX))) == NULL) {
+               (void) fprintf(stderr, "sha256_init: bad alloc\n");
+               return 0;
+       }
        SHA224_Init(hash->data);
+       return 1;
 }
 
 static void 
diff -r b64922aee59d -r a541f0fca835 crypto/external/bsd/netpgp/dist/src/lib/packet-parse.c
--- a/crypto/external/bsd/netpgp/dist/src/lib/packet-parse.c    Wed Oct 07 13:19:36 2009 +0000
+++ b/crypto/external/bsd/netpgp/dist/src/lib/packet-parse.c    Wed Oct 07 16:19:51 2009 +0000
@@ -58,7 +58,7 @@
 
 #if defined(__NetBSD__)



Home | Main Index | Thread Index | Old Index