Module Name: src Committed By: agc Date: Wed Jun 10 16:01:37 UTC 2009
Modified Files: src/crypto/external/bsd/netpgp/dist/src/lib: keyring.c misc.c Log Message: Get rid of an unusual architectural construct: The original code had dynamic arrays indexed by unsigned indices, except for the array of keys, which was indexed by a signed integer, and initialised to -1. Subsequently, when a new id was created, the index was pre-incremented, and later on, in a different call, the userid (a different packet) was assigned to the current index. This has implications for growing the array, for signed comparison checks, and just general cleanliness. This change overhauls the construct: don't special case anything, just address the array from 0, use unsigned indices same as everything else, and complain if we get a user id for which we haven't received a public or secret key. To generate a diff of this commit: cvs rdiff -u -r1.17 -r1.18 \ src/crypto/external/bsd/netpgp/dist/src/lib/keyring.c cvs rdiff -u -r1.18 -r1.19 src/crypto/external/bsd/netpgp/dist/src/lib/misc.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/crypto/external/bsd/netpgp/dist/src/lib/keyring.c diff -u src/crypto/external/bsd/netpgp/dist/src/lib/keyring.c:1.17 src/crypto/external/bsd/netpgp/dist/src/lib/keyring.c:1.18 --- src/crypto/external/bsd/netpgp/dist/src/lib/keyring.c:1.17 Wed Jun 10 00:38:09 2009 +++ src/crypto/external/bsd/netpgp/dist/src/lib/keyring.c Wed Jun 10 16:01:37 2009 @@ -57,7 +57,7 @@ #if defined(__NetBSD__) __COPYRIGHT("@(#) Copyright (c) 2009 The NetBSD Foundation, Inc. All rights reserved."); -__RCSID("$NetBSD: keyring.c,v 1.17 2009/06/10 00:38:09 agc Exp $"); +__RCSID("$NetBSD: keyring.c,v 1.18 2009/06/10 16:01:37 agc Exp $"); #endif #ifdef HAVE_FCNTL_H @@ -763,7 +763,7 @@ { unsigned n; - for (n = 1; keyring && n < keyring->keyc + 1; n++) { + for (n = 0; keyring && n < keyring->keyc; n++) { if (__ops_get_debug_level(__FILE__)) { int i; @@ -858,8 +858,8 @@ return NULL; } len = strlen(name); - n = 1; - for (keyp = &keyring->keys[n]; n < keyring->keyc + 1; ++n, keyp++) { + n = 0; + for (keyp = &keyring->keys[n]; n < keyring->keyc; ++n, keyp++) { for (i = 0, uidp = keyp->uids; i < keyp->uidc; i++, uidp++) { if (__ops_get_debug_level(__FILE__)) { (void) fprintf(io->outs, @@ -889,8 +889,8 @@ return kp; } /* match on full name */ - keyp = &keyring->keys[1]; - for (n = 1; n < keyring->keyc + 1; ++n, keyp++) { + keyp = keyring->keys; + for (n = 0; n < keyring->keyc; ++n, keyp++) { uidp = keyp->uids; for (i = 0 ; i < keyp->uidc; i++, uidp++) { if (__ops_get_debug_level(__FILE__)) { @@ -908,8 +908,8 @@ } } /* match on <em...@address> */ - keyp = &keyring->keys[1]; - for (n = 1; n < keyring->keyc + 1; ++n, keyp++) { + keyp = keyring->keys; + for (n = 0; n < keyring->keyc; ++n, keyp++) { for (i = 0, uidp = keyp->uids; i < keyp->uidc; i++, uidp++) { /* * look for the rightmost '<', in case there is one @@ -951,14 +951,14 @@ __ops_key_t *key; unsigned n; - (void) fprintf(io->outs, "%d keys\n", keyring->keyc); - for (n = 0, key = &keyring->keys[n+1]; n < keyring->keyc; ++n, ++key) { + (void) fprintf(io->res, "%d keys\n", keyring->keyc); + for (n = 0, key = keyring->keys; n < keyring->keyc; ++n, ++key) { if (__ops_is_key_secret(key)) { - __ops_print_seckeydata(key); + __ops_print_seckeydata(io, key); } else { __ops_print_pubkeydata(io, key); } - (void) fputc('\n', io->outs); + (void) fputc('\n', io->res); } return 1; } Index: src/crypto/external/bsd/netpgp/dist/src/lib/misc.c diff -u src/crypto/external/bsd/netpgp/dist/src/lib/misc.c:1.18 src/crypto/external/bsd/netpgp/dist/src/lib/misc.c:1.19 --- src/crypto/external/bsd/netpgp/dist/src/lib/misc.c:1.18 Wed Jun 10 00:38:09 2009 +++ src/crypto/external/bsd/netpgp/dist/src/lib/misc.c Wed Jun 10 16:01:37 2009 @@ -57,7 +57,7 @@ #if defined(__NetBSD__) __COPYRIGHT("@(#) Copyright (c) 2009 The NetBSD Foundation, Inc. All rights reserved."); -__RCSID("$NetBSD: misc.c,v 1.18 2009/06/10 00:38:09 agc Exp $"); +__RCSID("$NetBSD: misc.c,v 1.19 2009/06/10 16:01:37 agc Exp $"); #endif #include <sys/types.h> @@ -114,20 +114,19 @@ accumulate = __ops_callback_arg(cbinfo); keyring = accumulate->keyring; - key = (keyring->keyc > 0) ? &keyring->keys[keyring->keyc] : NULL; - switch (pkt->tag) { case OPS_PTAG_CT_PUBLIC_KEY: case OPS_PTAG_CT_SECRET_KEY: case OPS_PTAG_CT_ENCRYPTED_SECRET_KEY: if (__ops_get_debug_level(__FILE__)) { - (void) fprintf(stderr, "New key - tag %d\n", pkt->tag); + (void) fprintf(stderr, "Creating key %d - tag %d\n", + keyring->keyc + 1, pkt->tag); } EXPAND_ARRAY(keyring, key); pubkey = (pkt->tag == OPS_PTAG_CT_PUBLIC_KEY) ? &content->pubkey : &content->seckey.pubkey; - key = &keyring->keys[++keyring->keyc]; + key = &keyring->keys[keyring->keyc++]; (void) memset(key, 0x0, sizeof(*key)); __ops_keyid(key->key_id, OPS_KEY_ID_SIZE, OPS_KEY_ID_SIZE, pubkey); @@ -142,19 +141,22 @@ case OPS_PTAG_CT_USER_ID: if (__ops_get_debug_level(__FILE__)) { - (void) fprintf(stderr, "User ID: %s\n", - content->userid.userid); + (void) fprintf(stderr, "User ID: %s for key %d\n", + content->userid.userid, + keyring->keyc - 1); } - if (key) { - __ops_add_userid(key, &content->userid); + if (keyring->keyc > 0) { + __ops_add_userid(&keyring->keys[keyring->keyc - 1], + &content->userid); return OPS_KEEP_MEMORY; } OPS_ERROR(cbinfo->errors, OPS_E_P_NO_USERID, "No userid found"); return OPS_KEEP_MEMORY; case OPS_PARSER_PACKET_END: - if (key) { - __ops_add_subpacket(key, &content->packet); + if (keyring->keyc > 0) { + __ops_add_subpacket(&keyring->keys[keyring->keyc - 1], + &content->packet); return OPS_KEEP_MEMORY; } return OPS_RELEASE_MEMORY; @@ -187,7 +189,6 @@ * \param keyring Pointer to an existing keyring * \param parse Options to use when parsing */ - int __ops_parse_and_accumulate(__ops_keyring_t *keyring, __ops_stream_t *parse) { @@ -451,7 +452,7 @@ __ops_build_pubkey(mem, key, 0); if (__ops_get_debug_level(__FILE__)) { - fprintf(stderr, "--- creating key fingerprint\n"); + fprintf(stderr, "-> creating key fingerprint\n"); } __ops_hash_sha1(&sha1); sha1.init(&sha1); @@ -464,7 +465,7 @@ sha1.finish(&sha1, fp->fingerprint); if (__ops_get_debug_level(__FILE__)) { - fprintf(stderr, "finished making key fingerprint\n"); + fprintf(stderr, "<- finished making key fingerprint\n"); } fp->length = OPS_FINGERPRINT_SIZE;