From c9fa28bfad297b17e76341ffb40383ce92da5d44 Mon Sep 17 00:00:00 2001 From: Werner Koch Date: Fri, 3 May 2019 14:24:07 +0200 Subject: common: In private key mode write "Key:" always last in name-value. * common/name-value.c (nvc_write): Take care of Key. Factor some code out to ... (write_one_entry): new. -- The key item is in general not manual editable thus we put it at the end of a file. Signed-off-by: Werner Koch --- common/name-value.c | 53 ++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 40 insertions(+), 13 deletions(-) (limited to 'common/name-value.c') diff --git a/common/name-value.c b/common/name-value.c index 5094acd03..89fd060c8 100644 --- a/common/name-value.c +++ b/common/name-value.c @@ -778,29 +778,56 @@ nvc_parse_private_key (nvc_t *result, int *errlinep, estream_t stream) } +/* Helper fpr nvc_write. */ +static gpg_error_t +write_one_entry (nve_t entry, estream_t stream) +{ + gpg_error_t err; + strlist_t sl; + + if (entry->name) + es_fputs (entry->name, stream); + + err = assert_raw_value (entry); + if (err) + return err; + + for (sl = entry->raw_value; sl; sl = sl->next) + es_fputs (sl->d, stream); + + if (es_ferror (stream)) + return my_error_from_syserror (); + + return 0; +} + + /* Write a representation of PK to STREAM. */ gpg_error_t nvc_write (nvc_t pk, estream_t stream) { - gpg_error_t err; + gpg_error_t err = 0; nve_t entry; - strlist_t s; + nve_t keyentry = NULL; for (entry = pk->first; entry; entry = entry->next) { - if (entry->name) - es_fputs (entry->name, stream); - - err = assert_raw_value (entry); + if (pk->private_key_mode + && entry->name && !ascii_strcasecmp (entry->name, "Key:")) + { + if (!keyentry) + keyentry = entry; + continue; + } + + err = write_one_entry (entry, stream); if (err) return err; - - for (s = entry->raw_value; s; s = s->next) - es_fputs (s->d, stream); - - if (es_ferror (stream)) - return my_error_from_syserror (); } - return 0; + /* In private key mode we write the Key always last. */ + if (keyentry) + err = write_one_entry (keyentry, stream); + + return err; } -- cgit v1.2.3