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 Fix some error handling, ...



details:   https://anonhg.NetBSD.org/src/rev/149b251ab72f
branches:  trunk
changeset: 836943:149b251ab72f
user:      mlelstv <mlelstv%NetBSD.org@localhost>
date:      Tue Nov 13 14:52:30 2018 +0000

description:
Fix some error handling, json support, keyring handling.

diffstat:

 crypto/external/bsd/netpgp/dist/src/lib/keyring.c           |   93 ++++-
 crypto/external/bsd/netpgp/dist/src/lib/keyring.h           |    4 +-
 crypto/external/bsd/netpgp/dist/src/lib/misc.c              |   27 +-
 crypto/external/bsd/netpgp/dist/src/lib/netpgp.c            |  221 +++++++----
 crypto/external/bsd/netpgp/dist/src/lib/packet-parse.c      |   18 +-
 crypto/external/bsd/netpgp/dist/src/lib/packet.h            |    1 +
 crypto/external/bsd/netpgp/dist/src/libmj/libmj.3           |    4 +-
 crypto/external/bsd/netpgp/dist/src/libmj/mj.c              |  157 +++++---
 crypto/external/bsd/netpgp/dist/src/netpgpkeys/netpgpkeys.c |    3 +
 9 files changed, 356 insertions(+), 172 deletions(-)

diffs (truncated from 987 to 300 lines):

diff -r a294e9f8f108 -r 149b251ab72f crypto/external/bsd/netpgp/dist/src/lib/keyring.c
--- a/crypto/external/bsd/netpgp/dist/src/lib/keyring.c Tue Nov 13 11:06:19 2018 +0000
+++ b/crypto/external/bsd/netpgp/dist/src/lib/keyring.c Tue Nov 13 14:52:30 2018 +0000
@@ -57,7 +57,7 @@
 
 #if defined(__NetBSD__)
 __COPYRIGHT("@(#) Copyright (c) 2009 The NetBSD Foundation, Inc. All rights reserved.");
-__RCSID("$NetBSD: keyring.c,v 1.55 2017/03/27 21:19:12 khorben Exp $");
+__RCSID("$NetBSD: keyring.c,v 1.56 2018/11/13 14:52:30 mlelstv Exp $");
 #endif
 
 #ifdef HAVE_FCNTL_H
@@ -456,10 +456,12 @@
        }
        if ((dst->raw = calloc(1, src->length)) == NULL) {
                (void) fprintf(stderr, "copy_packet: bad alloc\n");
+               dst->length = 0;
        } else {
                dst->length = src->length;
                (void) memcpy(dst->raw, src->raw, src->length);
        }
+       dst->tag = src->tag;
        return dst;
 }
 
@@ -500,7 +502,6 @@
        EXPAND_ARRAY(keydata, packet);
        /* initialise new entry in array */
        subpktp = &keydata->packets[keydata->packetc++];
-       subpktp->length = 0;
        subpktp->raw = NULL;
        /* now copy it */
        return copy_packet(subpktp, packet);
@@ -545,6 +546,7 @@
        /* add this packet to key */
        sigpacket.length = pgp_mem_len(mem_sig);
        sigpacket.raw = pgp_mem_data(mem_sig);
+       sigpacket.tag = PGP_PTAG_CT_SIGNATURE;
 
        /* add userid to key */
        (void) pgp_add_userid(key, userid);
@@ -596,13 +598,14 @@
 
        cb = pgp_callback_arg(cbinfo);
        keyring = cb->keyring;
+       key = keyring->keyc > 0 ? &keyring->keys[keyring->keyc - 1] : NULL;
+
        switch (pkt->tag) {
        case PGP_PARSER_PTAG:
        case PGP_PTAG_CT_ENCRYPTED_SECRET_KEY:
                /* we get these because we didn't prompt */
                break;
        case PGP_PTAG_CT_SIGNATURE_HEADER:
-               key = &keyring->keys[keyring->keyc - 1];
                EXPAND_ARRAY(key, subsig);
                key->subsigs[key->subsigc].uid = key->uidc - 1;
                (void) memcpy(&key->subsigs[key->subsigc].sig, &pkt->u.sig,
@@ -610,7 +613,6 @@
                key->subsigc += 1;
                break;
        case PGP_PTAG_CT_SIGNATURE:
-               key = &keyring->keys[keyring->keyc - 1];
                EXPAND_ARRAY(key, subsig);
                key->subsigs[key->subsigc].uid = key->uidc - 1;
                (void) memcpy(&key->subsigs[key->subsigc].sig, &pkt->u.sig,
@@ -618,7 +620,6 @@
                key->subsigc += 1;
                break;
        case PGP_PTAG_CT_TRUST:
-               key = &keyring->keys[keyring->keyc - 1];
                key->subsigs[key->subsigc - 1].trustlevel = pkt->u.ss_trust.level;
                key->subsigs[key->subsigc - 1].trustamount = pkt->u.ss_trust.amount;
                break;
@@ -629,28 +630,23 @@
                }
                break;
        case PGP_PTAG_SS_ISSUER_KEY_ID:
-               key = &keyring->keys[keyring->keyc - 1];
                (void) memcpy(&key->subsigs[key->subsigc - 1].sig.info.signer_id,
                              pkt->u.ss_issuer,
                              sizeof(pkt->u.ss_issuer));
                key->subsigs[key->subsigc - 1].sig.info.signer_id_set = 1;
                break;
        case PGP_PTAG_SS_CREATION_TIME:
-               key = &keyring->keys[keyring->keyc - 1];
                key->subsigs[key->subsigc - 1].sig.info.birthtime = pkt->u.ss_time;
                key->subsigs[key->subsigc - 1].sig.info.birthtime_set = 1;
                break;
        case PGP_PTAG_SS_EXPIRATION_TIME:
-               key = &keyring->keys[keyring->keyc - 1];
                key->subsigs[key->subsigc - 1].sig.info.duration = pkt->u.ss_time;
                key->subsigs[key->subsigc - 1].sig.info.duration_set = 1;
                break;
        case PGP_PTAG_SS_PRIMARY_USER_ID:
-               key = &keyring->keys[keyring->keyc - 1];
                key->uid0 = key->uidc - 1;
                break;
        case PGP_PTAG_SS_REVOCATION_REASON:
-               key = &keyring->keys[keyring->keyc - 1];
                if (key->uidc == 0) {
                        /* revoke whole key */
                        key->revoked = 1;
@@ -668,7 +664,6 @@
        case PGP_PTAG_CT_SIGNATURE_FOOTER:
        case PGP_PARSER_ERRCODE:
                break;
-
        default:
                break;
        }
@@ -813,6 +808,77 @@
 }
 
 /**
+   \ingroup HighLevel_KeyringWrite
+
+   \brief Writes a keyring to a file
+
+   \param keyring Pointer to an existing pgp_keyring_t struct
+   \param armour 1 if file is armoured; else 0
+   \param filename Filename of keyring to be written
+
+   \return pgp 1 if OK; 0 on error
+
+   \note Keyring struct must already exist.
+
+   \note Can be used with either a public or secret keyring.
+*/
+
+unsigned 
+pgp_keyring_filewrite(pgp_keyring_t *keyring,
+                       unsigned armour,
+                       const char *filename,
+                       uint8_t *passphrase)
+{
+       pgp_output_t            *output;
+       int                     fd;
+       unsigned                res = 1;
+       pgp_key_t               *key;
+       unsigned                n;
+       unsigned                keyc = (keyring != NULL) ? keyring->keyc : 0;
+       char                    *cp;
+       pgp_content_enum        type;
+       pgp_armor_type_t        atype;
+       char                    keyid[PGP_KEY_ID_SIZE * 3];
+
+       fd = pgp_setup_file_write(&output, filename, 1);
+       if (fd < 0) {
+               perror(filename);
+               return 0;
+       }
+
+       type = keyring->keyc > 0 ? keyring->keys->type : PGP_PTAG_CT_PUBLIC_KEY;
+
+       if (armour) {
+               if (type == PGP_PTAG_CT_PUBLIC_KEY)
+                       atype = PGP_PGP_PUBLIC_KEY_BLOCK;
+               else
+                       atype = PGP_PGP_PRIVATE_KEY_BLOCK;
+               pgp_writer_push_armoured(output, atype);
+       }
+       for (n = 0, key = keyring->keys; n < keyring->keyc; ++n, ++key) {
+               /* write only keys of a single type */
+               if (key->type != type) {
+                       (void) fprintf(stderr, "ERROR: skip key %d\n", n);
+                       continue;
+               }
+               if (key->type == PGP_PTAG_CT_PUBLIC_KEY) {
+                       pgp_write_xfer_pubkey(output, key, 0);
+               } else {
+                       pgp_write_xfer_seckey(output, key, passphrase,
+                                       strlen((char *)passphrase), 0);
+               }
+       }
+       if (armour) {
+               pgp_writer_info_finalise(&output->errors, &output->writer);
+               pgp_writer_pop(output);
+       }
+
+       pgp_teardown_file_write(output, fd);
+
+       return res;
+}
+
+/**
    \ingroup HighLevel_KeyringRead
 
    \brief Frees keyring's contents (but not keyring itself)
@@ -1030,7 +1096,8 @@
                        pgp_print_keydata(io, keyring, key, "sec",
                                &key->key.seckey.pubkey, 0);
                } else {
-                       pgp_print_keydata(io, keyring, key, "signature ", &key->key.pubkey, psigs);
+                       pgp_print_keydata(io, keyring, key, "pub",
+                               &key->key.pubkey, psigs);
                }
                (void) fputc('\n', io->res);
        }
@@ -1059,7 +1126,7 @@
                                "sec", &key->key.seckey.pubkey, psigs);
                } else {
                        pgp_sprint_mj(io, keyring, key, &obj->value.v[obj->c],
-                               "signature ", &key->key.pubkey, psigs);
+                               "pub", &key->key.pubkey, psigs);
                }
                if (obj->value.v[obj->c].type != 0) {
                        obj->c += 1;
diff -r a294e9f8f108 -r 149b251ab72f crypto/external/bsd/netpgp/dist/src/lib/keyring.h
--- a/crypto/external/bsd/netpgp/dist/src/lib/keyring.h Tue Nov 13 11:06:19 2018 +0000
+++ b/crypto/external/bsd/netpgp/dist/src/lib/keyring.h Tue Nov 13 14:52:30 2018 +0000
@@ -96,6 +96,8 @@
 
 unsigned   pgp_keyring_fileread(pgp_keyring_t *, const unsigned,
                                        const char *);
+unsigned   pgp_keyring_filewrite(pgp_keyring_t *, const unsigned,
+                                       const char *, uint8_t *);
 
 int pgp_keyring_list(pgp_io_t *, const pgp_keyring_t *, const int);
 int pgp_keyring_json(pgp_io_t *, const pgp_keyring_t *, mj_t *, const int);
@@ -110,7 +112,7 @@
 
 uint8_t *pgp_add_userid(pgp_key_t *, const uint8_t *);
 pgp_subpacket_t *pgp_add_subpacket(pgp_key_t *,
-                                               const pgp_subpacket_t *);
+                                       const pgp_subpacket_t *);
 
 unsigned pgp_add_selfsigned_userid(pgp_key_t *, uint8_t *);
 
diff -r a294e9f8f108 -r 149b251ab72f crypto/external/bsd/netpgp/dist/src/lib/misc.c
--- a/crypto/external/bsd/netpgp/dist/src/lib/misc.c    Tue Nov 13 11:06:19 2018 +0000
+++ b/crypto/external/bsd/netpgp/dist/src/lib/misc.c    Tue Nov 13 14:52:30 2018 +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.41 2012/03/05 02:20:18 christos Exp $");
+__RCSID("$NetBSD: misc.c,v 1.42 2018/11/13 14:52:30 mlelstv Exp $");
 #endif
 
 #include <sys/types.h>
@@ -110,12 +110,14 @@
        const pgp_contents_t    *content = &pkt->u;
        pgp_keyring_t           *keyring;
        accumulate_t            *accumulate;
+       pgp_key_t               *key;
 
        if (pgp_get_debug_level(__FILE__)) {
                (void) fprintf(stderr, "accumulate callback: packet tag %u\n", pkt->tag);
        }
        accumulate = pgp_callback_arg(cbinfo);
        keyring = accumulate->keyring;
+       key = keyring->keyc > 0 ? &keyring->keys[keyring->keyc - 1] : NULL;
        switch (pkt->tag) {
        case PGP_PTAG_CT_PUBLIC_KEY:
        case PGP_PTAG_CT_PUBLIC_SUBKEY:
@@ -131,17 +133,26 @@
                                        content->userid,
                                        keyring->keyc - 1);
                }
-               if (keyring->keyc == 0) {
+               if (key != NULL) {
+                       pgp_add_userid(key, content->userid);
+               } else {
                        PGP_ERROR_1(cbinfo->errors, PGP_E_P_NO_USERID, "%s",
-                           "No userid found");
-               } else {
-                       pgp_add_userid(&keyring->keys[keyring->keyc - 1], content->userid);
+                           "No key for userid found");
                }
                return PGP_KEEP_MEMORY;
        case PGP_PARSER_PACKET_END:
-               if (keyring->keyc > 0) {
-                       pgp_add_subpacket(&keyring->keys[keyring->keyc - 1],
-                                               &content->packet);
+               if (key != NULL) {
+                       switch (content->packet.tag) {
+                       case PGP_PTAG_CT_RESERVED:
+                               (void) fprintf(stderr, "Invalid packet tag\n");
+                               break;
+                       case PGP_PTAG_CT_PUBLIC_KEY:
+                       case PGP_PTAG_CT_USER_ID:
+                               break;
+                       default:
+                               pgp_add_subpacket(key, &content->packet);
+                               break;
+                       }
                        return PGP_KEEP_MEMORY;
                }
                return PGP_RELEASE_MEMORY;
diff -r a294e9f8f108 -r 149b251ab72f crypto/external/bsd/netpgp/dist/src/lib/netpgp.c
--- a/crypto/external/bsd/netpgp/dist/src/lib/netpgp.c  Tue Nov 13 11:06:19 2018 +0000
+++ b/crypto/external/bsd/netpgp/dist/src/lib/netpgp.c  Tue Nov 13 14:52:30 2018 +0000
@@ -34,7 +34,7 @@
 
 #if defined(__NetBSD__)
 __COPYRIGHT("@(#) Copyright (c) 2009 The NetBSD Foundation, Inc. All rights reserved.");
-__RCSID("$NetBSD: netpgp.c,v 1.101 2017/03/27 20:55:13 khorben Exp $");
+__RCSID("$NetBSD: netpgp.c,v 1.102 2018/11/13 14:52:30 mlelstv Exp $");
 #endif
 



Home | Main Index | Thread Index | Old Index