Module Name: src Committed By: agc Date: Sat May 8 00:31:07 UTC 2010
Modified Files: src/crypto/external/bsd/netpgp/dist/src/lib: netpgp.c signature.c signature.h Log Message: use hexdump() function where appropriate. change the way that we generate a detached signature file, so that ascii armour is used if --armor is specified. addresses PR 43248 from Juan RP. To generate a diff of this commit: cvs rdiff -u -r1.46 -r1.47 \ src/crypto/external/bsd/netpgp/dist/src/lib/netpgp.c cvs rdiff -u -r1.26 -r1.27 \ src/crypto/external/bsd/netpgp/dist/src/lib/signature.c cvs rdiff -u -r1.17 -r1.18 \ src/crypto/external/bsd/netpgp/dist/src/lib/signature.h 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/netpgp.c diff -u src/crypto/external/bsd/netpgp/dist/src/lib/netpgp.c:1.46 src/crypto/external/bsd/netpgp/dist/src/lib/netpgp.c:1.47 --- src/crypto/external/bsd/netpgp/dist/src/lib/netpgp.c:1.46 Fri May 7 16:22:39 2010 +++ src/crypto/external/bsd/netpgp/dist/src/lib/netpgp.c Sat May 8 00:31:07 2010 @@ -34,7 +34,7 @@ #if defined(__NetBSD__) __COPYRIGHT("@(#) Copyright (c) 2009 The NetBSD Foundation, Inc. All rights reserved."); -__RCSID("$NetBSD: netpgp.c,v 1.46 2010/05/07 16:22:39 agc Exp $"); +__RCSID("$NetBSD: netpgp.c,v 1.47 2010/05/08 00:31:07 agc Exp $"); #endif #include <sys/types.h> @@ -829,7 +829,7 @@ overwrite); } -#define ARMOR_HEAD "-----BEGIN PGP MESSAGE-----" +#define ARMOR_HEAD "-----BEGIN PGP MESSAGE-----\r\n" /* decrypt a file */ int @@ -856,7 +856,7 @@ if (fgets(buf, sizeof(buf), fp) == NULL) { realarmor = 0; } else { - realarmor = (strncmp(buf, ARMOR_HEAD, strlen(ARMOR_HEAD)) == 0); + realarmor = (strcmp(buf, ARMOR_HEAD) == 0); } (void) fclose(fp); return __ops_decrypt_file(netpgp->io, f, out, netpgp->secring, @@ -931,7 +931,9 @@ if (detached) { ret = __ops_sign_detached(io, f, out, seckey, hashalg, get_birthtime(netpgp_getvar(netpgp, "birthtime")), - get_duration(netpgp_getvar(netpgp, "duration"))); + get_duration(netpgp_getvar(netpgp, "duration")), + (unsigned)armored, + overwrite); } else { ret = __ops_sign_file(io, f, out, seckey, hashalg, get_birthtime(netpgp_getvar(netpgp, "birthtime")), @@ -943,7 +945,7 @@ return ret; } -#define ARMOR_SIG_HEAD "-----BEGIN PGP SIGNATURE-----" +#define ARMOR_SIG_HEAD "-----BEGIN PGP SIGNATURE-----\r\n" /* verify a file */ int @@ -971,8 +973,7 @@ if (fgets(buf, sizeof(buf), fp) == NULL) { realarmor = 0; } else { - realarmor = (strncmp(buf, ARMOR_SIG_HEAD, strlen(ARMOR_SIG_HEAD)) == 0 || - strncmp(buf, ARMOR_HEAD, strlen(ARMOR_HEAD)) == 0); + realarmor = (strcmp(buf, ARMOR_SIG_HEAD) == 0); } (void) fclose(fp); if (__ops_validate_file(io, &result, in, out, (const int)realarmor, netpgp->pubring)) { Index: src/crypto/external/bsd/netpgp/dist/src/lib/signature.c diff -u src/crypto/external/bsd/netpgp/dist/src/lib/signature.c:1.26 src/crypto/external/bsd/netpgp/dist/src/lib/signature.c:1.27 --- src/crypto/external/bsd/netpgp/dist/src/lib/signature.c:1.26 Fri Mar 5 16:01:10 2010 +++ src/crypto/external/bsd/netpgp/dist/src/lib/signature.c Sat May 8 00:31:07 2010 @@ -57,7 +57,7 @@ #if defined(__NetBSD__) __COPYRIGHT("@(#) Copyright (c) 2009 The NetBSD Foundation, Inc. All rights reserved."); -__RCSID("$NetBSD: signature.c,v 1.26 2010/03/05 16:01:10 agc Exp $"); +__RCSID("$NetBSD: signature.c,v 1.27 2010/05/08 00:31:07 agc Exp $"); #endif #include <sys/types.h> @@ -335,31 +335,14 @@ } if (__ops_get_debug_level(__FILE__)) { - unsigned zz; - unsigned uu; - - printf("\n"); - printf("hashbuf_from_sig\n"); - for (zz = 0; zz < debug_len_decrypted; zz++) { - printf("%02x ", hashbuf_from_sig[n + zz]); - } - printf("\n"); - printf("prefix\n"); - for (zz = 0; zz < plen; zz++) { - printf("%02x ", prefix[zz]); - } - printf("\n"); - - printf("\n"); - printf("hash from sig\n"); - for (uu = 0; uu < hash_length; uu++) { - printf("%02x ", hashbuf_from_sig[n + plen + uu]); - } - printf("\n"); - printf("hash passed in (should match hash from sig)\n"); - for (uu = 0; uu < hash_length; uu++) { - printf("%02x ", hash[uu]); - } + (void) fprintf(stderr, "\nhashbuf_from_sig\n"); + hexdump(stderr, hashbuf_from_sig, debug_len_decrypted, " "); + printf("\nprefix\n"); + hexdump(stderr, prefix, plen, " "); + (void) fprintf(stderr, "\nhash from sig\n"); + hexdump(stderr, &hashbuf_from_sig[n + plen], hash_length, " "); + (void) fprintf(stderr, "\nhash passed in (should match hash from sig)\n"); + hexdump(stderr, hash, hash_length, " "); printf("\n"); } return (memcmp(&hashbuf_from_sig[n], prefix, plen) == 0 && @@ -938,11 +921,12 @@ return &sig->hash; } +/* open up an output file */ static int open_output_file(__ops_output_t **output, const char *inname, const char *outname, - const unsigned armored, + const char *suffix, const unsigned overwrite) { int fd; @@ -958,8 +942,7 @@ (void) fprintf(stderr, "open_output_file: bad alloc\n"); fd = -1; } else { - (void) snprintf(f, flen, "%s.%s", inname, - (armored) ? "asc" : "gpg"); + (void) snprintf(f, flen, "%s.%s", inname, suffix); fd = __ops_setup_file_write(output, f, overwrite); free(f); } @@ -995,9 +978,9 @@ __ops_hash_alg_t hash_alg; __ops_memory_t *infile; __ops_output_t *output; - uint8_t keyid[OPS_KEY_ID_SIZE]; __ops_hash_t *hash; unsigned ret; + uint8_t keyid[OPS_KEY_ID_SIZE]; int fd_out; sig = NULL; @@ -1023,7 +1006,8 @@ } /* setup output file */ - fd_out = open_output_file(&output, inname, outname, armored, overwrite); + fd_out = open_output_file(&output, inname, outname, + (armored) ? "asc" : "gpg", overwrite); if (fd_out < 0) { __ops_memory_free(infile); return 0; @@ -1148,7 +1132,7 @@ __ops_hash_alg_t hash_alg; __ops_output_t *output; __ops_memory_t *mem; - uint8_t keyid[OPS_KEY_ID_SIZE]; + uint8_t keyid[OPS_KEY_ID_SIZE]; __ops_hash_t *hash; unsigned ret; @@ -1249,14 +1233,14 @@ __ops_seckey_t *seckey, const char *hash, const int64_t from, - const uint64_t duration) + const uint64_t duration, + const unsigned armored, const unsigned overwrite) { __ops_create_sig_t *sig; __ops_hash_alg_t alg; __ops_output_t *output; __ops_memory_t *mem; - uint8_t keyid[OPS_KEY_ID_SIZE]; - char fname[MAXPATHLEN]; + uint8_t keyid[OPS_KEY_ID_SIZE]; int fd; /* find out which hash algorithm to use */ @@ -1266,6 +1250,14 @@ return 0; } + /* setup output file */ + fd = open_output_file(&output, f, sigfile, + (armored) ? "asc" : "sig", overwrite); + if (fd < 0) { + (void) fprintf(io->errs,"Can't open output file: %s\n", f); + return 0; + } + /* create a new signature */ sig = __ops_create_sig_new(); __ops_start_sig(sig, seckey, alg, OPS_SIG_BINARY); @@ -1273,8 +1265,13 @@ /* read the contents of 'f', and add that to the signature */ mem = __ops_memory_new(); if (!__ops_mem_readfile(mem, f)) { + __ops_teardown_file_write(output, fd); return 0; } + /* set armoured/not armoured here */ + if (armored) { + __ops_writer_push_armor_msg(output); + } __ops_sig_add_data(sig, __ops_mem_data(mem), __ops_mem_len(mem)); __ops_memory_free(mem); @@ -1284,24 +1281,9 @@ __ops_keyid(keyid, sizeof(keyid), &seckey->pubkey); __ops_add_issuer_keyid(sig, keyid); __ops_end_hashed_subpkts(sig); - - /* write the signature to the detached file */ - if (sigfile == NULL) { - (void) snprintf(fname, sizeof(fname), "%s.sig", f); - sigfile = fname; - } - fd = open(sigfile, O_CREAT|O_TRUNC|O_WRONLY, 0666); - if (fd < 0) { - (void) fprintf(io->errs, "can't write signature to \"%s\"\n", - sigfile); - return 0; - } - - output = __ops_output_new(); - __ops_writer_set_fd(output, fd); __ops_write_sig(output, sig, &seckey->pubkey, seckey); + __ops_teardown_file_write(output, fd); __ops_seckey_free(seckey); - (void) close(fd); return 1; } Index: src/crypto/external/bsd/netpgp/dist/src/lib/signature.h diff -u src/crypto/external/bsd/netpgp/dist/src/lib/signature.h:1.17 src/crypto/external/bsd/netpgp/dist/src/lib/signature.h:1.18 --- src/crypto/external/bsd/netpgp/dist/src/lib/signature.h:1.17 Fri Mar 5 16:01:10 2010 +++ src/crypto/external/bsd/netpgp/dist/src/lib/signature.h Sat May 8 00:31:07 2010 @@ -126,7 +126,9 @@ __ops_seckey_t *, const char *, const int64_t, - const uint64_t); + const uint64_t, + const unsigned, + const unsigned); /* armoured stuff */ unsigned __ops_crc24(unsigned, uint8_t);