Line | Count | Source |
1 | | /* import.c - import a key into our key storage. |
2 | | * Copyright (C) 1998-2007, 2010-2011 Free Software Foundation, Inc. |
3 | | * Copyright (C) 2014, 2016, 2017, 2019 Werner Koch |
4 | | * |
5 | | * This file is part of GnuPG. |
6 | | * |
7 | | * GnuPG is free software; you can redistribute it and/or modify |
8 | | * it under the terms of the GNU General Public License as published by |
9 | | * the Free Software Foundation; either version 3 of the License, or |
10 | | * (at your option) any later version. |
11 | | * |
12 | | * GnuPG is distributed in the hope that it will be useful, |
13 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | | * GNU General Public License for more details. |
16 | | * |
17 | | * You should have received a copy of the GNU General Public License |
18 | | * along with this program; if not, see <https://www.gnu.org/licenses/>. |
19 | | */ |
20 | | |
21 | | #include <config.h> |
22 | | #include <stdio.h> |
23 | | #include <stdlib.h> |
24 | | #include <string.h> |
25 | | #include <errno.h> |
26 | | |
27 | | #include "gpg.h" |
28 | | #include "options.h" |
29 | | #include "packet.h" |
30 | | #include "../common/status.h" |
31 | | #include "keydb.h" |
32 | | #include "../common/util.h" |
33 | | #include "trustdb.h" |
34 | | #include "main.h" |
35 | | #include "../common/i18n.h" |
36 | | #include "../common/ttyio.h" |
37 | | #include "../common/recsel.h" |
38 | | #include "keyserver-internal.h" |
39 | | #include "call-agent.h" |
40 | | #include "../common/membuf.h" |
41 | | #include "../common/init.h" |
42 | | #include "../common/mbox-util.h" |
43 | | #include "key-check.h" |
44 | | #include "key-clean.h" |
45 | | |
46 | | |
47 | | struct import_stats_s |
48 | | { |
49 | | ulong count; |
50 | | ulong no_user_id; |
51 | | ulong imported; |
52 | | ulong n_uids; |
53 | | ulong n_sigs; |
54 | | ulong n_subk; |
55 | | ulong unchanged; |
56 | | ulong n_revoc; |
57 | | ulong secret_read; |
58 | | ulong secret_imported; |
59 | | ulong secret_dups; |
60 | | ulong skipped_new_keys; |
61 | | ulong not_imported; |
62 | | ulong n_sigs_cleaned; |
63 | | ulong n_uids_cleaned; |
64 | | ulong v3keys; /* Number of V3 keys seen. */ |
65 | | }; |
66 | | |
67 | | |
68 | | /* Node flag to indicate that a user ID or a subkey has a |
69 | | * valid self-signature. */ |
70 | 0 | #define NODE_GOOD_SELFSIG 1 |
71 | | /* Node flag to indicate that a user ID or subkey has |
72 | | * an invalid self-signature. */ |
73 | 0 | #define NODE_BAD_SELFSIG 2 |
74 | | /* Node flag to indicate that the node shall be deleted. */ |
75 | 0 | #define NODE_DELETION_MARK 4 |
76 | | /* A node flag used to temporary mark a node. */ |
77 | 0 | #define NODE_FLAG_A 8 |
78 | | /* A flag used by transfer_secret_keys. */ |
79 | 0 | #define NODE_TRANSFER_SECKEY 16 |
80 | | |
81 | | |
82 | | /* An object and a global instance to store selectors created from |
83 | | * --import-filter keep-uid=EXPR. |
84 | | * --import-filter drop-sig=EXPR. |
85 | | * |
86 | | * FIXME: We should put this into the CTRL object but that requires a |
87 | | * lot more changes right now. For now we use save and restore |
88 | | * function to temporary change them. |
89 | | */ |
90 | | /* Definition of the import filters. */ |
91 | | struct import_filter_s |
92 | | { |
93 | | recsel_expr_t keep_uid; |
94 | | recsel_expr_t drop_sig; |
95 | | }; |
96 | | /* The current instance. */ |
97 | | struct import_filter_s import_filter; |
98 | | |
99 | | |
100 | | static int import (ctrl_t ctrl, |
101 | | IOBUF inp, const char* fname, struct import_stats_s *stats, |
102 | | unsigned char **fpr, size_t *fpr_len, unsigned int options, |
103 | | import_screener_t screener, void *screener_arg, |
104 | | int origin, const char *url); |
105 | | static int read_block (IOBUF a, unsigned int options, |
106 | | PACKET **pending_pkt, kbnode_t *ret_root, int *r_v3keys); |
107 | | static void revocation_present (ctrl_t ctrl, kbnode_t keyblock); |
108 | | static gpg_error_t import_one (ctrl_t ctrl, |
109 | | kbnode_t keyblock, |
110 | | struct import_stats_s *stats, |
111 | | unsigned char **fpr, size_t *fpr_len, |
112 | | unsigned int options, int from_sk, int silent, |
113 | | import_screener_t screener, void *screener_arg, |
114 | | int origin, const char *url, int *r_valid); |
115 | | static gpg_error_t import_matching_seckeys ( |
116 | | ctrl_t ctrl, kbnode_t seckeys, |
117 | | const byte *mainfpr, size_t mainfprlen, |
118 | | struct import_stats_s *stats, int batch); |
119 | | static gpg_error_t import_secret_one (ctrl_t ctrl, kbnode_t keyblock, |
120 | | struct import_stats_s *stats, int batch, |
121 | | unsigned int options, int for_migration, |
122 | | import_screener_t screener, void *screener_arg, |
123 | | kbnode_t *r_secattic); |
124 | | static int import_revoke_cert (ctrl_t ctrl, kbnode_t node, unsigned int options, |
125 | | struct import_stats_s *stats); |
126 | | static int chk_self_sigs (ctrl_t ctrl, kbnode_t keyblock, u32 *keyid, |
127 | | int *non_self); |
128 | | static int delete_inv_parts (ctrl_t ctrl, kbnode_t keyblock, |
129 | | u32 *keyid, unsigned int options, |
130 | | kbnode_t *r_otherrevsigs); |
131 | | static int any_uid_left (kbnode_t keyblock); |
132 | | static void remove_all_non_self_sigs (kbnode_t *keyblock, u32 *keyid); |
133 | | static int merge_blocks (ctrl_t ctrl, unsigned int options, |
134 | | kbnode_t keyblock_orig, |
135 | | kbnode_t keyblock, u32 *keyid, |
136 | | u32 curtime, int origin, const char *url, |
137 | | int *n_uids, int *n_sigs, int *n_subk ); |
138 | | static gpg_error_t append_new_uid (unsigned int options, |
139 | | kbnode_t keyblock, kbnode_t node, |
140 | | u32 curtime, int origin, const char *url, |
141 | | int *n_sigs); |
142 | | static int append_key (kbnode_t keyblock, kbnode_t node, int *n_sigs); |
143 | | static int merge_sigs (kbnode_t dst, kbnode_t src, int *n_sigs); |
144 | | static int merge_keysigs (kbnode_t dst, kbnode_t src, int *n_sigs); |
145 | | |
146 | | |
147 | | |
148 | | static void |
149 | | release_import_filter (import_filter_t filt) |
150 | 0 | { |
151 | 0 | recsel_release (filt->keep_uid); |
152 | 0 | filt->keep_uid = NULL; |
153 | 0 | recsel_release (filt->drop_sig); |
154 | 0 | filt->drop_sig = NULL; |
155 | 0 | } |
156 | | |
157 | | static void |
158 | | cleanup_import_globals (void) |
159 | 0 | { |
160 | 0 | release_import_filter (&import_filter); |
161 | 0 | } |
162 | | |
163 | | |
164 | | int |
165 | | parse_import_options(char *str,unsigned int *options,int noisy) |
166 | 0 | { |
167 | 0 | struct parse_options import_opts[]= |
168 | 0 | { |
169 | 0 | {"import-local-sigs",IMPORT_LOCAL_SIGS,NULL, |
170 | 0 | N_("import signatures that are marked as local-only")}, |
171 | |
|
172 | 0 | {"repair-pks-subkey-bug",IMPORT_REPAIR_PKS_SUBKEY_BUG,NULL, |
173 | 0 | N_("repair damage from the pks keyserver during import")}, |
174 | |
|
175 | 0 | {"keep-ownertrust", IMPORT_KEEP_OWNERTTRUST, NULL, |
176 | 0 | N_("do not clear the ownertrust values during import")}, |
177 | |
|
178 | 0 | {"fast-import",IMPORT_FAST,NULL, |
179 | 0 | N_("do not update the trustdb after import")}, |
180 | |
|
181 | 0 | {"bulk-import",IMPORT_BULK, NULL, |
182 | 0 | N_("enable bulk import mode")}, |
183 | |
|
184 | 0 | {"import-show",IMPORT_SHOW,NULL, |
185 | 0 | N_("show key during import")}, |
186 | |
|
187 | 0 | {"show-only", (IMPORT_SHOW | IMPORT_DRY_RUN), NULL, |
188 | 0 | N_("show key but do not actually import") }, |
189 | |
|
190 | 0 | {"merge-only",IMPORT_MERGE_ONLY,NULL, |
191 | 0 | N_("only accept updates to existing keys")}, |
192 | |
|
193 | 0 | {"import-clean",IMPORT_CLEAN,NULL, |
194 | 0 | N_("remove unusable parts from key after import")}, |
195 | |
|
196 | 0 | {"import-minimal",IMPORT_MINIMAL|IMPORT_CLEAN,NULL, |
197 | 0 | N_("remove as much as possible from key after import")}, |
198 | |
|
199 | 0 | {"self-sigs-only", IMPORT_SELF_SIGS_ONLY, NULL, |
200 | 0 | N_("ignore key-signatures which are not self-signatures")}, |
201 | |
|
202 | 0 | {"import-export", IMPORT_EXPORT, NULL, |
203 | 0 | N_("run import filters and export key immediately")}, |
204 | |
|
205 | 0 | {"restore", IMPORT_RESTORE, NULL, |
206 | 0 | N_("assume the GnuPG key backup format")}, |
207 | 0 | {"import-restore", IMPORT_RESTORE, NULL, NULL}, |
208 | |
|
209 | 0 | {"repair-keys", IMPORT_REPAIR_KEYS, NULL, |
210 | 0 | N_("repair keys on import")}, |
211 | |
|
212 | 0 | {"force-update", IMPORT_FORCE_UPDATE, NULL, |
213 | 0 | N_("update even unchanged keys")}, |
214 | | |
215 | | /* New options. Right now, without description string. */ |
216 | 0 | {"ignore-attributes", IMPORT_IGNORE_ATTRIBUTES, NULL, NULL}, |
217 | |
|
218 | 0 | {"only-pubkeys", IMPORT_ONLY_PUBKEYS, NULL, |
219 | 0 | N_("do not import secret keys")}, |
220 | | |
221 | | /* Hidden options which are enabled by default and are provided |
222 | | * in case of problems with the respective implementation. */ |
223 | 0 | {"collapse-uids", IMPORT_COLLAPSE_UIDS, NULL, NULL}, |
224 | 0 | {"collapse-subkeys", IMPORT_COLLAPSE_SUBKEYS, NULL, NULL}, |
225 | | |
226 | | /* Aliases for backward compatibility */ |
227 | 0 | {"allow-local-sigs",IMPORT_LOCAL_SIGS,NULL,NULL}, |
228 | 0 | {"repair-hkp-subkey-bug",IMPORT_REPAIR_PKS_SUBKEY_BUG,NULL,NULL}, |
229 | | /* dummy */ |
230 | 0 | {"import-unusable-sigs",0,NULL,NULL}, |
231 | 0 | {"import-clean-sigs",0,NULL,NULL}, |
232 | 0 | {"import-clean-uids",0,NULL,NULL}, |
233 | 0 | {"convert-sk-to-pk",0, NULL,NULL}, /* Not anymore needed due to |
234 | | the new design. */ |
235 | 0 | {NULL,0,NULL,NULL} |
236 | 0 | }; |
237 | 0 | int rc; |
238 | 0 | int saved_self_sigs_only, saved_import_clean; |
239 | | |
240 | | /* We need to set flags indicating whether the user has set certain |
241 | | * options or if they came from the default. */ |
242 | 0 | saved_self_sigs_only = (*options & IMPORT_SELF_SIGS_ONLY); |
243 | 0 | saved_self_sigs_only &= ~IMPORT_SELF_SIGS_ONLY; |
244 | 0 | saved_import_clean = (*options & IMPORT_CLEAN); |
245 | 0 | saved_import_clean &= ~IMPORT_CLEAN; |
246 | |
|
247 | 0 | rc = parse_options (str, options, import_opts, noisy); |
248 | |
|
249 | 0 | if (rc && (*options & IMPORT_SELF_SIGS_ONLY)) |
250 | 0 | opt.flags.expl_import_self_sigs_only = 1; |
251 | 0 | else |
252 | 0 | *options |= saved_self_sigs_only; |
253 | |
|
254 | 0 | if (rc && (*options & IMPORT_CLEAN)) |
255 | 0 | opt.flags.expl_import_clean = 1; |
256 | 0 | else |
257 | 0 | *options |= saved_import_clean; |
258 | | |
259 | |
|
260 | 0 | if (rc && (*options & IMPORT_RESTORE)) |
261 | 0 | { |
262 | | /* Alter other options we want or don't want for restore. */ |
263 | 0 | *options |= (IMPORT_LOCAL_SIGS | IMPORT_KEEP_OWNERTTRUST); |
264 | 0 | *options &= ~(IMPORT_MINIMAL | IMPORT_CLEAN |
265 | 0 | | IMPORT_REPAIR_PKS_SUBKEY_BUG |
266 | 0 | | IMPORT_MERGE_ONLY); |
267 | 0 | } |
268 | 0 | return rc; |
269 | 0 | } |
270 | | |
271 | | |
272 | | /* Parse and set an import filter from string. STRING has the format |
273 | | * "NAME=EXPR" with NAME being the name of the filter. Spaces before |
274 | | * and after NAME are not allowed. If this function is all called |
275 | | * several times all expressions for the same NAME are concatenated. |
276 | | * Supported filter names are: |
277 | | * |
278 | | * - keep-uid :: If the expression evaluates to true for a certain |
279 | | * user ID packet, that packet and all it dependencies |
280 | | * will be imported. The expression may use these |
281 | | * variables: |
282 | | * |
283 | | * - uid :: The entire user ID. |
284 | | * - mbox :: The mail box part of the user ID. |
285 | | * - primary :: Evaluate to true for the primary user ID. |
286 | | */ |
287 | | gpg_error_t |
288 | | parse_and_set_import_filter (const char *string) |
289 | 0 | { |
290 | 0 | gpg_error_t err; |
291 | | |
292 | | /* Auto register the cleanup function. */ |
293 | 0 | register_mem_cleanup_func (cleanup_import_globals); |
294 | |
|
295 | 0 | if (!strncmp (string, "keep-uid=", 9)) |
296 | 0 | err = recsel_parse_expr (&import_filter.keep_uid, string+9); |
297 | 0 | else if (!strncmp (string, "drop-sig=", 9)) |
298 | 0 | err = recsel_parse_expr (&import_filter.drop_sig, string+9); |
299 | 0 | else |
300 | 0 | err = gpg_error (GPG_ERR_INV_NAME); |
301 | |
|
302 | 0 | return err; |
303 | 0 | } |
304 | | |
305 | | |
306 | | /* Save the current import filters, return them, and clear the current |
307 | | * filters. Returns NULL on error and sets ERRNO. */ |
308 | | import_filter_t |
309 | | save_and_clear_import_filter (void) |
310 | 0 | { |
311 | 0 | import_filter_t filt; |
312 | |
|
313 | 0 | filt = xtrycalloc (1, sizeof *filt); |
314 | 0 | if (!filt) |
315 | 0 | return NULL; |
316 | 0 | *filt = import_filter; |
317 | 0 | memset (&import_filter, 0, sizeof import_filter); |
318 | |
|
319 | 0 | return filt; |
320 | 0 | } |
321 | | |
322 | | |
323 | | /* Release the current import filters and restore them from NEWFILT. |
324 | | * Ownership of NEWFILT is moved to this function. */ |
325 | | void |
326 | | restore_import_filter (import_filter_t filt) |
327 | 0 | { |
328 | 0 | if (filt) |
329 | 0 | { |
330 | 0 | release_import_filter (&import_filter); |
331 | 0 | import_filter = *filt; |
332 | 0 | xfree (filt); |
333 | 0 | } |
334 | 0 | } |
335 | | |
336 | | |
337 | | import_stats_t |
338 | | import_new_stats_handle (void) |
339 | 0 | { |
340 | 0 | return xmalloc_clear ( sizeof (struct import_stats_s) ); |
341 | 0 | } |
342 | | |
343 | | |
344 | | void |
345 | | import_release_stats_handle (import_stats_t p) |
346 | 0 | { |
347 | 0 | xfree (p); |
348 | 0 | } |
349 | | |
350 | | |
351 | | /* Read a key from a file. Only the first key in the file is |
352 | | * considered and stored at R_KEYBLOCK. FNAME is the name of the |
353 | | * file. |
354 | | */ |
355 | | gpg_error_t |
356 | | read_key_from_file_or_buffer (ctrl_t ctrl, const char *fname, |
357 | | const void *buffer, size_t buflen, |
358 | | kbnode_t *r_keyblock) |
359 | 0 | { |
360 | 0 | gpg_error_t err; |
361 | 0 | iobuf_t inp; |
362 | 0 | PACKET *pending_pkt = NULL; |
363 | 0 | kbnode_t keyblock = NULL; |
364 | 0 | u32 keyid[2]; |
365 | 0 | int v3keys; /* Dummy */ |
366 | 0 | int non_self; /* Dummy */ |
367 | |
|
368 | 0 | (void)ctrl; |
369 | |
|
370 | 0 | *r_keyblock = NULL; |
371 | |
|
372 | 0 | log_assert (!!fname ^ !!buffer); |
373 | | |
374 | 0 | if (fname) |
375 | 0 | { |
376 | 0 | inp = iobuf_open (fname); |
377 | 0 | if (!inp) |
378 | 0 | err = gpg_error_from_syserror (); |
379 | 0 | else if (is_secured_file (iobuf_get_fd (inp))) |
380 | 0 | { |
381 | 0 | iobuf_close (inp); |
382 | 0 | inp = NULL; |
383 | 0 | err = gpg_error (GPG_ERR_EPERM); |
384 | 0 | } |
385 | 0 | else |
386 | 0 | err = 0; |
387 | 0 | if (err) |
388 | 0 | { |
389 | 0 | log_error (_("can't open '%s': %s\n"), |
390 | 0 | iobuf_is_pipe_filename (fname)? "[stdin]": fname, |
391 | 0 | gpg_strerror (err)); |
392 | 0 | if (gpg_err_code (err) == GPG_ERR_ENOENT) |
393 | 0 | err = gpg_error (GPG_ERR_NO_PUBKEY); |
394 | 0 | goto leave; |
395 | 0 | } |
396 | | |
397 | | /* Push the armor filter. */ |
398 | 0 | { |
399 | 0 | armor_filter_context_t *afx; |
400 | 0 | afx = new_armor_context (); |
401 | 0 | afx->only_keyblocks = 1; |
402 | 0 | push_armor_filter (afx, inp); |
403 | 0 | release_armor_context (afx); |
404 | 0 | } |
405 | |
|
406 | 0 | } |
407 | 0 | else /* Read from buffer (No armor expected). */ |
408 | 0 | { |
409 | 0 | inp = iobuf_temp_with_content (buffer, buflen); |
410 | 0 | } |
411 | | |
412 | | /* Read the first non-v3 keyblock. */ |
413 | 0 | while (!(err = read_block (inp, 0, &pending_pkt, &keyblock, &v3keys))) |
414 | 0 | { |
415 | 0 | if (keyblock->pkt->pkttype == PKT_PUBLIC_KEY) |
416 | 0 | break; |
417 | 0 | log_info (_("skipping block of type %d\n"), keyblock->pkt->pkttype); |
418 | 0 | release_kbnode (keyblock); |
419 | 0 | keyblock = NULL; |
420 | 0 | } |
421 | 0 | if (err) |
422 | 0 | { |
423 | 0 | if (gpg_err_code (err) != GPG_ERR_INV_KEYRING) |
424 | 0 | log_error (_("error reading '%s': %s\n"), |
425 | 0 | fname? (iobuf_is_pipe_filename (fname)? "[stdin]": fname) |
426 | 0 | /* */ : "[buffer]", |
427 | 0 | gpg_strerror (err)); |
428 | 0 | goto leave; |
429 | 0 | } |
430 | | |
431 | 0 | keyid_from_pk (keyblock->pkt->pkt.public_key, keyid); |
432 | |
|
433 | 0 | if (!find_next_kbnode (keyblock, PKT_USER_ID)) |
434 | 0 | { |
435 | 0 | err = gpg_error (GPG_ERR_NO_USER_ID); |
436 | 0 | goto leave; |
437 | 0 | } |
438 | | |
439 | | /* We do the collapsing unconditionally although it is expected that |
440 | | * clean keys are provided here. */ |
441 | 0 | collapse_uids (&keyblock); |
442 | 0 | collapse_subkeys (&keyblock); |
443 | |
|
444 | 0 | clear_kbnode_flags (keyblock); |
445 | 0 | if (chk_self_sigs (ctrl, keyblock, keyid, &non_self)) |
446 | 0 | { |
447 | 0 | err = gpg_error (GPG_ERR_INV_KEYRING); |
448 | 0 | goto leave; |
449 | 0 | } |
450 | | |
451 | 0 | if (!delete_inv_parts (ctrl, keyblock, keyid, 0, NULL) ) |
452 | 0 | { |
453 | 0 | err = gpg_error (GPG_ERR_NO_USER_ID); |
454 | 0 | goto leave; |
455 | 0 | } |
456 | | |
457 | 0 | *r_keyblock = keyblock; |
458 | 0 | keyblock = NULL; |
459 | |
|
460 | 0 | leave: |
461 | 0 | if (inp) |
462 | 0 | { |
463 | 0 | iobuf_close (inp); |
464 | | /* Must invalidate that ugly cache to actually close the file. */ |
465 | 0 | if (fname) |
466 | 0 | iobuf_ioctl (NULL, IOBUF_IOCTL_INVALIDATE_CACHE, 0, (char*)fname); |
467 | 0 | } |
468 | 0 | release_kbnode (keyblock); |
469 | | /* FIXME: Do we need to free PENDING_PKT ? */ |
470 | 0 | return err; |
471 | 0 | } |
472 | | |
473 | | |
474 | | /* Import an already checked public key which was included in a |
475 | | * signature and the signature verified out using this key. */ |
476 | | gpg_error_t |
477 | | import_included_key_block (ctrl_t ctrl, kbnode_t keyblock) |
478 | 0 | { |
479 | 0 | gpg_error_t err; |
480 | 0 | struct import_stats_s *stats; |
481 | 0 | import_filter_t save_filt; |
482 | 0 | int save_armor = opt.armor; |
483 | |
|
484 | 0 | opt.armor = 0; |
485 | 0 | stats = import_new_stats_handle (); |
486 | 0 | save_filt = save_and_clear_import_filter (); |
487 | 0 | if (!save_filt) |
488 | 0 | { |
489 | 0 | err = gpg_error_from_syserror (); |
490 | 0 | goto leave; |
491 | 0 | } |
492 | | |
493 | | /* FIXME: Should we introduce a dedicated KEYORG ? */ |
494 | 0 | err = import_one (ctrl, keyblock, |
495 | 0 | stats, NULL, 0, 0, 0, 0, |
496 | 0 | NULL, NULL, KEYORG_UNKNOWN, NULL, NULL); |
497 | |
|
498 | 0 | leave: |
499 | 0 | restore_import_filter (save_filt); |
500 | 0 | import_release_stats_handle (stats); |
501 | 0 | opt.armor = save_armor; |
502 | 0 | return err; |
503 | 0 | } |
504 | | |
505 | | |
506 | | |
507 | | /* |
508 | | * Import the public keys from the given filename. Input may be armored. |
509 | | * This function rejects all keys which are not validly self signed on at |
510 | | * least one userid. Only user ids which are self signed will be imported. |
511 | | * Other signatures are not checked. |
512 | | * |
513 | | * Actually this function does a merge. It works like this: |
514 | | * |
515 | | * - get the keyblock |
516 | | * - check self-signatures and remove all userids and their signatures |
517 | | * without/invalid self-signatures. |
518 | | * - reject the keyblock, if we have no valid userid. |
519 | | * - See whether we have this key already in one of our pubrings. |
520 | | * If not, simply add it to the default keyring. |
521 | | * - Compare the key and the self-signatures of the new and the one in |
522 | | * our keyring. If they are different something weird is going on; |
523 | | * ask what to do. |
524 | | * - See whether we have only non-self-signature on one user id; if not |
525 | | * ask the user what to do. |
526 | | * - compare the signatures: If we already have this signature, check |
527 | | * that they compare okay; if not, issue a warning and ask the user. |
528 | | * (consider looking at the timestamp and use the newest?) |
529 | | * - Simply add the signature. Can't verify here because we may not have |
530 | | * the signature's public key yet; verification is done when putting it |
531 | | * into the trustdb, which is done automagically as soon as this pubkey |
532 | | * is used. |
533 | | * - Proceed with next signature. |
534 | | * |
535 | | * Key revocation certificates have special handling. |
536 | | */ |
537 | | static gpg_error_t |
538 | | import_keys_internal (ctrl_t ctrl, iobuf_t inp, char **fnames, int nnames, |
539 | | import_stats_t stats_handle, |
540 | | unsigned char **fpr, size_t *fpr_len, |
541 | | unsigned int options, |
542 | | import_screener_t screener, void *screener_arg, |
543 | | int origin, const char *url) |
544 | 0 | { |
545 | 0 | int i; |
546 | 0 | gpg_error_t err = 0; |
547 | 0 | struct import_stats_s *stats = stats_handle; |
548 | |
|
549 | 0 | if (!stats) |
550 | 0 | stats = import_new_stats_handle (); |
551 | |
|
552 | 0 | if (inp) |
553 | 0 | { |
554 | 0 | err = import (ctrl, inp, "[stream]", stats, fpr, fpr_len, options, |
555 | 0 | screener, screener_arg, origin, url); |
556 | 0 | } |
557 | 0 | else |
558 | 0 | { |
559 | 0 | if (!fnames && !nnames) |
560 | 0 | nnames = 1; /* Ohh what a ugly hack to jump into the loop */ |
561 | |
|
562 | 0 | for (i=0; i < nnames; i++) |
563 | 0 | { |
564 | 0 | const char *fname = fnames? fnames[i] : NULL; |
565 | 0 | IOBUF inp2 = iobuf_open(fname); |
566 | |
|
567 | 0 | if (!fname) |
568 | 0 | fname = "[stdin]"; |
569 | 0 | if (inp2 && is_secured_file (iobuf_get_fd (inp2))) |
570 | 0 | { |
571 | 0 | iobuf_close (inp2); |
572 | 0 | inp2 = NULL; |
573 | 0 | gpg_err_set_errno (EPERM); |
574 | 0 | } |
575 | 0 | if (!inp2) |
576 | 0 | log_error (_("can't open '%s': %s\n"), fname, strerror (errno)); |
577 | 0 | else |
578 | 0 | { |
579 | 0 | err = import (ctrl, inp2, fname, stats, fpr, fpr_len, options, |
580 | 0 | screener, screener_arg, origin, url); |
581 | 0 | iobuf_close (inp2); |
582 | | /* Must invalidate that ugly cache to actually close it. */ |
583 | 0 | iobuf_ioctl (NULL, IOBUF_IOCTL_INVALIDATE_CACHE, 0, (char*)fname); |
584 | 0 | if (err) |
585 | 0 | log_error ("import from '%s' failed: %s\n", |
586 | 0 | fname, gpg_strerror (err) ); |
587 | 0 | } |
588 | 0 | if (!fname) |
589 | 0 | break; |
590 | 0 | } |
591 | 0 | } |
592 | |
|
593 | 0 | if (!stats_handle) |
594 | 0 | { |
595 | 0 | if ((options & (IMPORT_SHOW | IMPORT_DRY_RUN)) |
596 | 0 | != (IMPORT_SHOW | IMPORT_DRY_RUN)) |
597 | 0 | import_print_stats (stats); |
598 | 0 | import_release_stats_handle (stats); |
599 | 0 | } |
600 | | |
601 | | /* If no fast import and the trustdb is dirty (i.e. we added a key |
602 | | or userID that had something other than a selfsig, a signature |
603 | | that was other than a selfsig, or any revocation), then |
604 | | update/check the trustdb if the user specified by setting |
605 | | interactive or by not setting no-auto-check-trustdb */ |
606 | |
|
607 | 0 | if (!(options & IMPORT_FAST)) |
608 | 0 | check_or_update_trustdb (ctrl); |
609 | |
|
610 | 0 | return err; |
611 | 0 | } |
612 | | |
613 | | |
614 | | void |
615 | | import_keys (ctrl_t ctrl, char **fnames, int nnames, |
616 | | import_stats_t stats_handle, unsigned int options, |
617 | | int origin, const char *url) |
618 | 0 | { |
619 | 0 | import_keys_internal (ctrl, NULL, fnames, nnames, stats_handle, |
620 | 0 | NULL, NULL, options, NULL, NULL, origin, url); |
621 | 0 | } |
622 | | |
623 | | |
624 | | gpg_error_t |
625 | | import_keys_es_stream (ctrl_t ctrl, estream_t fp, |
626 | | import_stats_t stats_handle, |
627 | | unsigned char **fpr, size_t *fpr_len, |
628 | | unsigned int options, |
629 | | import_screener_t screener, void *screener_arg, |
630 | | int origin, const char *url) |
631 | 0 | { |
632 | 0 | gpg_error_t err; |
633 | 0 | iobuf_t inp; |
634 | |
|
635 | 0 | inp = iobuf_esopen (fp, "rb", 1, 0); |
636 | 0 | if (!inp) |
637 | 0 | { |
638 | 0 | err = gpg_error_from_syserror (); |
639 | 0 | log_error ("iobuf_esopen failed: %s\n", gpg_strerror (err)); |
640 | 0 | return err; |
641 | 0 | } |
642 | | |
643 | 0 | err = import_keys_internal (ctrl, inp, NULL, 0, stats_handle, |
644 | 0 | fpr, fpr_len, options, |
645 | 0 | screener, screener_arg, origin, url); |
646 | |
|
647 | 0 | iobuf_close (inp); |
648 | 0 | return err; |
649 | 0 | } |
650 | | |
651 | | |
652 | | static int |
653 | | import (ctrl_t ctrl, IOBUF inp, const char* fname,struct import_stats_s *stats, |
654 | | unsigned char **fpr,size_t *fpr_len, unsigned int options, |
655 | | import_screener_t screener, void *screener_arg, |
656 | | int origin, const char *url) |
657 | 0 | { |
658 | 0 | PACKET *pending_pkt = NULL; |
659 | 0 | kbnode_t keyblock = NULL; /* Need to initialize because gcc can't |
660 | | grasp the return semantics of |
661 | | read_block. */ |
662 | 0 | kbnode_t secattic = NULL; /* Kludge for PGP desktop percularity */ |
663 | 0 | int rc = 0; |
664 | 0 | int v3keys; |
665 | |
|
666 | 0 | getkey_disable_caches (); |
667 | |
|
668 | 0 | if (!opt.no_armor) /* Armored reading is not disabled. */ |
669 | 0 | { |
670 | 0 | armor_filter_context_t *afx; |
671 | |
|
672 | 0 | afx = new_armor_context (); |
673 | 0 | afx->only_keyblocks = 1; |
674 | 0 | push_armor_filter (afx, inp); |
675 | 0 | release_armor_context (afx); |
676 | 0 | } |
677 | |
|
678 | 0 | while (!(rc = read_block (inp, options, &pending_pkt, &keyblock, &v3keys))) |
679 | 0 | { |
680 | 0 | stats->v3keys += v3keys; |
681 | 0 | if (keyblock->pkt->pkttype == PKT_PUBLIC_KEY) |
682 | 0 | { |
683 | 0 | rc = import_one (ctrl, keyblock, |
684 | 0 | stats, fpr, fpr_len, options, 0, 0, |
685 | 0 | screener, screener_arg, origin, url, NULL); |
686 | 0 | if (secattic) |
687 | 0 | { |
688 | 0 | byte tmpfpr[MAX_FINGERPRINT_LEN]; |
689 | 0 | size_t tmpfprlen; |
690 | |
|
691 | 0 | if (!rc && !(opt.dry_run || (options & IMPORT_DRY_RUN))) |
692 | 0 | { |
693 | | /* Kudge for PGP desktop - see below. */ |
694 | 0 | fingerprint_from_pk (keyblock->pkt->pkt.public_key, |
695 | 0 | tmpfpr, &tmpfprlen); |
696 | 0 | rc = import_matching_seckeys (ctrl, secattic, |
697 | 0 | tmpfpr, tmpfprlen, |
698 | 0 | stats, opt.batch); |
699 | 0 | } |
700 | 0 | release_kbnode (secattic); |
701 | 0 | secattic = NULL; |
702 | 0 | } |
703 | 0 | } |
704 | 0 | else if (keyblock->pkt->pkttype == PKT_SECRET_KEY) |
705 | 0 | { |
706 | 0 | release_kbnode (secattic); |
707 | 0 | secattic = NULL; |
708 | 0 | rc = import_secret_one (ctrl, keyblock, stats, |
709 | 0 | opt.batch, options, 0, |
710 | 0 | screener, screener_arg, &secattic); |
711 | 0 | keyblock = NULL; /* Ownership was transferred. */ |
712 | 0 | if (secattic) |
713 | 0 | { |
714 | 0 | if (gpg_err_code (rc) == GPG_ERR_NO_PUBKEY) |
715 | 0 | rc = 0; /* Try import after the next pubkey. */ |
716 | | |
717 | | /* The attic is a workaround for the peculiar PGP |
718 | | * Desktop method of exporting a secret key: The |
719 | | * exported file is the concatenation of two armored |
720 | | * keyblocks; first the private one and then the public |
721 | | * one. The strange thing is that the secret one has no |
722 | | * binding signatures at all and thus we have not |
723 | | * imported it. The attic stores that secret keys and |
724 | | * we try to import it once after the very next public |
725 | | * keyblock. */ |
726 | 0 | } |
727 | 0 | } |
728 | 0 | else if (keyblock->pkt->pkttype == PKT_SIGNATURE |
729 | 0 | && IS_KEY_REV (keyblock->pkt->pkt.signature) ) |
730 | 0 | { |
731 | 0 | release_kbnode (secattic); |
732 | 0 | secattic = NULL; |
733 | 0 | rc = import_revoke_cert (ctrl, keyblock, options, stats); |
734 | 0 | } |
735 | 0 | else |
736 | 0 | { |
737 | 0 | release_kbnode (secattic); |
738 | 0 | secattic = NULL; |
739 | 0 | log_info (_("skipping block of type %d\n"), keyblock->pkt->pkttype); |
740 | 0 | } |
741 | 0 | release_kbnode (keyblock); |
742 | | |
743 | | /* fixme: we should increment the not imported counter but |
744 | | this does only make sense if we keep on going despite of |
745 | | errors. For now we do this only if the imported key is too |
746 | | large. */ |
747 | 0 | if (gpg_err_code (rc) == GPG_ERR_TOO_LARGE |
748 | 0 | && gpg_err_source (rc) == GPG_ERR_SOURCE_KEYBOX) |
749 | 0 | { |
750 | 0 | stats->not_imported++; |
751 | 0 | } |
752 | 0 | else if (rc) |
753 | 0 | break; |
754 | | |
755 | 0 | if (!(++stats->count % 100) && !opt.quiet) |
756 | 0 | log_info (_("%lu keys processed so far\n"), stats->count ); |
757 | |
|
758 | 0 | if (origin == KEYORG_WKD && stats->count >= 5) |
759 | 0 | { |
760 | | /* We limit the number of keys _received_ from the WKD to 5. |
761 | | * In fact there should be only one key but some sites want |
762 | | * to store a few expired keys there also. gpg's key |
763 | | * selection will later figure out which key to use. Note |
764 | | * that for WKD we always return the fingerprint of the |
765 | | * first imported key. */ |
766 | 0 | log_info ("import from WKD stopped after %d keys\n", 5); |
767 | 0 | break; |
768 | 0 | } |
769 | 0 | } |
770 | 0 | stats->v3keys += v3keys; |
771 | 0 | if (rc == -1) |
772 | 0 | rc = 0; |
773 | 0 | else if (rc && gpg_err_code (rc) != GPG_ERR_INV_KEYRING) |
774 | 0 | log_error (_("error reading '%s': %s\n"), fname, gpg_strerror (rc)); |
775 | |
|
776 | 0 | release_kbnode (secattic); |
777 | | |
778 | | /* When read_block loop was stopped by error, we have PENDING_PKT left. */ |
779 | 0 | if (pending_pkt) |
780 | 0 | { |
781 | 0 | free_packet (pending_pkt, NULL); |
782 | 0 | xfree (pending_pkt); |
783 | 0 | } |
784 | 0 | return rc; |
785 | 0 | } |
786 | | |
787 | | |
788 | | /* Helper to migrate secring.gpg to GnuPG 2.1. */ |
789 | | gpg_error_t |
790 | | import_old_secring (ctrl_t ctrl, const char *fname) |
791 | 0 | { |
792 | 0 | gpg_error_t err; |
793 | 0 | iobuf_t inp; |
794 | 0 | PACKET *pending_pkt = NULL; |
795 | 0 | kbnode_t keyblock = NULL; /* Need to initialize because gcc can't |
796 | | grasp the return semantics of |
797 | | read_block. */ |
798 | 0 | struct import_stats_s *stats; |
799 | 0 | int v3keys; |
800 | |
|
801 | 0 | inp = iobuf_open (fname); |
802 | 0 | if (inp && is_secured_file (iobuf_get_fd (inp))) |
803 | 0 | { |
804 | 0 | iobuf_close (inp); |
805 | 0 | inp = NULL; |
806 | 0 | gpg_err_set_errno (EPERM); |
807 | 0 | } |
808 | 0 | if (!inp) |
809 | 0 | { |
810 | 0 | err = gpg_error_from_syserror (); |
811 | 0 | log_error (_("can't open '%s': %s\n"), fname, gpg_strerror (err)); |
812 | 0 | return err; |
813 | 0 | } |
814 | | |
815 | 0 | getkey_disable_caches(); |
816 | 0 | stats = import_new_stats_handle (); |
817 | 0 | while (!(err = read_block (inp, 0, &pending_pkt, &keyblock, &v3keys))) |
818 | 0 | { |
819 | 0 | if (keyblock->pkt->pkttype == PKT_SECRET_KEY) |
820 | 0 | { |
821 | 0 | err = import_secret_one (ctrl, keyblock, stats, 1, 0, 1, |
822 | 0 | NULL, NULL, NULL); |
823 | 0 | keyblock = NULL; /* Ownership was transferred. */ |
824 | 0 | } |
825 | 0 | release_kbnode (keyblock); |
826 | 0 | if (err) |
827 | 0 | break; |
828 | 0 | } |
829 | 0 | import_release_stats_handle (stats); |
830 | 0 | if (err == -1) |
831 | 0 | err = 0; |
832 | 0 | else if (err && gpg_err_code (err) != GPG_ERR_INV_KEYRING) |
833 | 0 | log_error (_("error reading '%s': %s\n"), fname, gpg_strerror (err)); |
834 | 0 | else if (err) |
835 | 0 | log_error ("import from '%s' failed: %s\n", fname, gpg_strerror (err)); |
836 | |
|
837 | 0 | iobuf_close (inp); |
838 | 0 | iobuf_ioctl (NULL, IOBUF_IOCTL_INVALIDATE_CACHE, 0, (char*)fname); |
839 | |
|
840 | 0 | return err; |
841 | 0 | } |
842 | | |
843 | | |
844 | | void |
845 | | import_print_stats (import_stats_t stats) |
846 | 0 | { |
847 | 0 | if (!opt.quiet) |
848 | 0 | { |
849 | 0 | log_info(_("Total number processed: %lu\n"), |
850 | 0 | stats->count + stats->v3keys); |
851 | 0 | if (stats->v3keys) |
852 | 0 | log_info(_(" skipped PGP-2 keys: %lu\n"), stats->v3keys); |
853 | 0 | if (stats->skipped_new_keys ) |
854 | 0 | log_info(_(" skipped new keys: %lu\n"), |
855 | 0 | stats->skipped_new_keys ); |
856 | 0 | if (stats->no_user_id ) |
857 | 0 | log_info(_(" w/o user IDs: %lu\n"), stats->no_user_id ); |
858 | 0 | if (stats->imported) |
859 | 0 | { |
860 | 0 | log_info(_(" imported: %lu"), stats->imported ); |
861 | 0 | log_printf ("\n"); |
862 | 0 | } |
863 | 0 | if (stats->unchanged ) |
864 | 0 | log_info(_(" unchanged: %lu\n"), stats->unchanged ); |
865 | 0 | if (stats->n_uids ) |
866 | 0 | log_info(_(" new user IDs: %lu\n"), stats->n_uids ); |
867 | 0 | if (stats->n_subk ) |
868 | 0 | log_info(_(" new subkeys: %lu\n"), stats->n_subk ); |
869 | 0 | if (stats->n_sigs ) |
870 | 0 | log_info(_(" new signatures: %lu\n"), stats->n_sigs ); |
871 | 0 | if (stats->n_revoc ) |
872 | 0 | log_info(_(" new key revocations: %lu\n"), stats->n_revoc ); |
873 | 0 | if (stats->secret_read ) |
874 | 0 | log_info(_(" secret keys read: %lu\n"), stats->secret_read ); |
875 | 0 | if (stats->secret_imported ) |
876 | 0 | log_info(_(" secret keys imported: %lu\n"), stats->secret_imported ); |
877 | 0 | if (stats->secret_dups ) |
878 | 0 | log_info(_(" secret keys unchanged: %lu\n"), stats->secret_dups ); |
879 | 0 | if (stats->not_imported ) |
880 | 0 | log_info(_(" not imported: %lu\n"), stats->not_imported ); |
881 | 0 | if (stats->n_sigs_cleaned) |
882 | 0 | log_info(_(" signatures cleaned: %lu\n"),stats->n_sigs_cleaned); |
883 | 0 | if (stats->n_uids_cleaned) |
884 | 0 | log_info(_(" user IDs cleaned: %lu\n"),stats->n_uids_cleaned); |
885 | 0 | } |
886 | |
|
887 | 0 | if (is_status_enabled ()) |
888 | 0 | { |
889 | 0 | char buf[15*20]; |
890 | |
|
891 | 0 | snprintf (buf, sizeof buf, |
892 | 0 | "%lu %lu %lu 0 %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu", |
893 | 0 | stats->count + stats->v3keys, |
894 | 0 | stats->no_user_id, |
895 | 0 | stats->imported, |
896 | 0 | stats->unchanged, |
897 | 0 | stats->n_uids, |
898 | 0 | stats->n_subk, |
899 | 0 | stats->n_sigs, |
900 | 0 | stats->n_revoc, |
901 | 0 | stats->secret_read, |
902 | 0 | stats->secret_imported, |
903 | 0 | stats->secret_dups, |
904 | 0 | stats->skipped_new_keys, |
905 | 0 | stats->not_imported, |
906 | 0 | stats->v3keys ); |
907 | 0 | write_status_text (STATUS_IMPORT_RES, buf); |
908 | 0 | } |
909 | 0 | } |
910 | | |
911 | | |
912 | | /* Return true if PKTTYPE is valid in a keyblock. */ |
913 | | static int |
914 | | valid_keyblock_packet (int pkttype) |
915 | 0 | { |
916 | 0 | switch (pkttype) |
917 | 0 | { |
918 | 0 | case PKT_PUBLIC_KEY: |
919 | 0 | case PKT_PUBLIC_SUBKEY: |
920 | 0 | case PKT_SECRET_KEY: |
921 | 0 | case PKT_SECRET_SUBKEY: |
922 | 0 | case PKT_SIGNATURE: |
923 | 0 | case PKT_USER_ID: |
924 | 0 | case PKT_ATTRIBUTE: |
925 | 0 | case PKT_RING_TRUST: |
926 | 0 | return 1; |
927 | 0 | default: |
928 | 0 | return 0; |
929 | 0 | } |
930 | 0 | } |
931 | | |
932 | | |
933 | | /* Read the next keyblock from stream A. Meta data (ring trust |
934 | | * packets) are only considered if OPTIONS has the IMPORT_RESTORE flag |
935 | | * set. PENDING_PKT should be initialized to NULL and not changed by |
936 | | * the caller. |
937 | | * |
938 | | * Returns 0 for okay, -1 no more blocks, or any other errorcode. The |
939 | | * integer at R_V3KEY counts the number of unsupported v3 keyblocks. |
940 | | */ |
941 | | static int |
942 | | read_block( IOBUF a, unsigned int options, |
943 | | PACKET **pending_pkt, kbnode_t *ret_root, int *r_v3keys) |
944 | 0 | { |
945 | 0 | int rc; |
946 | 0 | struct parse_packet_ctx_s parsectx; |
947 | 0 | PACKET *pkt; |
948 | 0 | kbnode_t root = NULL; |
949 | 0 | kbnode_t lastnode = NULL; |
950 | 0 | int in_cert, in_v3key, skip_sigs; |
951 | 0 | u32 keyid[2]; |
952 | 0 | int got_keyid = 0; |
953 | 0 | unsigned int dropped_nonselfsigs = 0; |
954 | |
|
955 | 0 | *r_v3keys = 0; |
956 | |
|
957 | 0 | if (*pending_pkt) |
958 | 0 | { |
959 | 0 | root = lastnode = new_kbnode( *pending_pkt ); |
960 | 0 | *pending_pkt = NULL; |
961 | 0 | log_assert (root->pkt->pkttype == PKT_PUBLIC_KEY |
962 | 0 | || root->pkt->pkttype == PKT_SECRET_KEY); |
963 | 0 | in_cert = 1; |
964 | 0 | keyid_from_pk (root->pkt->pkt.public_key, keyid); |
965 | 0 | got_keyid = 1; |
966 | 0 | } |
967 | 0 | else |
968 | 0 | in_cert = 0; |
969 | | |
970 | 0 | pkt = xmalloc (sizeof *pkt); |
971 | 0 | init_packet (pkt); |
972 | 0 | init_parse_packet (&parsectx, a); |
973 | 0 | if (!(options & IMPORT_RESTORE)) |
974 | 0 | parsectx.skip_meta = 1; |
975 | 0 | in_v3key = 0; |
976 | 0 | skip_sigs = 0; |
977 | 0 | while ((rc=parse_packet (&parsectx, pkt)) != -1) |
978 | 0 | { |
979 | 0 | if (rc && (gpg_err_code (rc) == GPG_ERR_LEGACY_KEY |
980 | 0 | && (pkt->pkttype == PKT_PUBLIC_KEY |
981 | 0 | || pkt->pkttype == PKT_SECRET_KEY))) |
982 | 0 | { |
983 | 0 | in_v3key = 1; |
984 | 0 | ++*r_v3keys; |
985 | 0 | free_packet (pkt, &parsectx); |
986 | 0 | init_packet (pkt); |
987 | 0 | continue; |
988 | 0 | } |
989 | 0 | else if (rc ) /* (ignore errors) */ |
990 | 0 | { |
991 | 0 | skip_sigs = 0; |
992 | 0 | if (gpg_err_code (rc) == GPG_ERR_UNKNOWN_PACKET) |
993 | 0 | ; /* Do not show a diagnostic. */ |
994 | 0 | else if (gpg_err_code (rc) == GPG_ERR_INV_PACKET |
995 | 0 | && (pkt->pkttype == PKT_USER_ID |
996 | 0 | || pkt->pkttype == PKT_ATTRIBUTE)) |
997 | 0 | { |
998 | | /* This indicates a too large user id or attribute |
999 | | * packet. We skip this packet and all following |
1000 | | * signatures. Sure, this won't allow to repair a |
1001 | | * garbled keyring in case one of the signatures belong |
1002 | | * to another user id. However, this better mitigates |
1003 | | * DoS using inserted user ids. */ |
1004 | 0 | skip_sigs = 1; |
1005 | 0 | } |
1006 | 0 | else if (gpg_err_code (rc) == GPG_ERR_INV_PACKET |
1007 | 0 | && (pkt->pkttype == PKT_OLD_COMMENT |
1008 | 0 | || pkt->pkttype == PKT_COMMENT)) |
1009 | 0 | ; /* Ignore too large comment packets. */ |
1010 | 0 | else |
1011 | 0 | { |
1012 | 0 | log_error("read_block: read error: %s\n", gpg_strerror (rc) ); |
1013 | 0 | rc = GPG_ERR_INV_KEYRING; |
1014 | 0 | goto ready; |
1015 | 0 | } |
1016 | 0 | free_packet (pkt, &parsectx); |
1017 | 0 | init_packet(pkt); |
1018 | 0 | continue; |
1019 | 0 | } |
1020 | 0 | else if ((opt.import_options & IMPORT_IGNORE_ATTRIBUTES) |
1021 | 0 | && (pkt->pkttype == PKT_USER_ID || pkt->pkttype == PKT_ATTRIBUTE) |
1022 | 0 | && pkt->pkt.user_id->attrib_data) |
1023 | 0 | { |
1024 | 0 | skip_sigs = 1; |
1025 | 0 | free_packet (pkt, &parsectx); |
1026 | 0 | init_packet (pkt); |
1027 | 0 | continue; |
1028 | 0 | } |
1029 | | |
1030 | 0 | if (skip_sigs) |
1031 | 0 | { |
1032 | 0 | if (pkt->pkttype == PKT_SIGNATURE) |
1033 | 0 | { |
1034 | 0 | free_packet (pkt, &parsectx); |
1035 | 0 | init_packet (pkt); |
1036 | 0 | continue; |
1037 | 0 | } |
1038 | 0 | skip_sigs = 0; |
1039 | 0 | } |
1040 | | |
1041 | 0 | if (in_v3key && !(pkt->pkttype == PKT_PUBLIC_KEY |
1042 | 0 | || pkt->pkttype == PKT_SECRET_KEY)) |
1043 | 0 | { |
1044 | 0 | free_packet (pkt, &parsectx); |
1045 | 0 | init_packet(pkt); |
1046 | 0 | continue; |
1047 | 0 | } |
1048 | 0 | in_v3key = 0; |
1049 | |
|
1050 | 0 | if (!root && pkt->pkttype == PKT_SIGNATURE |
1051 | 0 | && IS_KEY_REV (pkt->pkt.signature) ) |
1052 | 0 | { |
1053 | | /* This is a revocation certificate which is handled in a |
1054 | | * special way. */ |
1055 | 0 | root = new_kbnode( pkt ); |
1056 | 0 | pkt = NULL; |
1057 | 0 | goto ready; |
1058 | 0 | } |
1059 | | |
1060 | | /* Make a linked list of all packets. */ |
1061 | 0 | switch (pkt->pkttype) |
1062 | 0 | { |
1063 | 0 | case PKT_COMPRESSED: |
1064 | 0 | if (!(opt.compat_flags & COMPAT_COMPR_KEYS)) |
1065 | 0 | { |
1066 | 0 | rc = GPG_ERR_UNEXPECTED_PACKET; |
1067 | 0 | goto ready; |
1068 | 0 | } |
1069 | 0 | else if (check_compress_algo (pkt->pkt.compressed->algorithm)) |
1070 | 0 | { |
1071 | 0 | rc = GPG_ERR_COMPR_ALGO; |
1072 | 0 | goto ready; |
1073 | 0 | } |
1074 | 0 | else |
1075 | 0 | { |
1076 | 0 | compress_filter_context_t *cfx = xmalloc_clear( sizeof *cfx ); |
1077 | 0 | pkt->pkt.compressed->buf = NULL; |
1078 | 0 | if (push_compress_filter2 (a, cfx, |
1079 | 0 | pkt->pkt.compressed->algorithm, 1)) |
1080 | 0 | xfree (cfx); /* e.g. in case of compression_algo NONE. */ |
1081 | 0 | } |
1082 | 0 | free_packet (pkt, &parsectx); |
1083 | 0 | init_packet(pkt); |
1084 | 0 | break; |
1085 | | |
1086 | 0 | case PKT_RING_TRUST: |
1087 | | /* Skip those packets unless we are in restore mode. */ |
1088 | 0 | if ((opt.import_options & IMPORT_RESTORE)) |
1089 | 0 | goto x_default; |
1090 | 0 | free_packet (pkt, &parsectx); |
1091 | 0 | init_packet(pkt); |
1092 | 0 | break; |
1093 | | |
1094 | 0 | case PKT_SIGNATURE: |
1095 | 0 | if (!in_cert) |
1096 | 0 | goto x_default; |
1097 | 0 | if (!(options & IMPORT_SELF_SIGS_ONLY)) |
1098 | 0 | goto x_default; |
1099 | 0 | log_assert (got_keyid); |
1100 | 0 | if (pkt->pkt.signature->keyid[0] == keyid[0] |
1101 | 0 | && pkt->pkt.signature->keyid[1] == keyid[1]) |
1102 | 0 | { /* This is likely a self-signature. We import this one. |
1103 | | * Eventually we should use the ISSUER_FPR to compare |
1104 | | * self-signatures, but that will work only for v5 keys |
1105 | | * which are currently not even deployed. |
1106 | | * Note that we do not do any crypto verify here because |
1107 | | * that would defeat this very mitigation of DoS by |
1108 | | * importing a key with a huge amount of faked |
1109 | | * key-signatures. A verification will be done later in |
1110 | | * the processing anyway. Here we want a cheap an early |
1111 | | * way to drop non-self-signatures. */ |
1112 | 0 | goto x_default; |
1113 | 0 | } |
1114 | | /* Skip this signature. */ |
1115 | 0 | dropped_nonselfsigs++; |
1116 | 0 | free_packet (pkt, &parsectx); |
1117 | 0 | init_packet(pkt); |
1118 | 0 | break; |
1119 | | |
1120 | 0 | case PKT_PUBLIC_KEY: |
1121 | 0 | case PKT_SECRET_KEY: |
1122 | 0 | if (!got_keyid) |
1123 | 0 | { |
1124 | 0 | keyid_from_pk (pkt->pkt.public_key, keyid); |
1125 | 0 | got_keyid = 1; |
1126 | 0 | } |
1127 | 0 | if (in_cert) /* Store this packet. */ |
1128 | 0 | { |
1129 | 0 | *pending_pkt = pkt; |
1130 | 0 | pkt = NULL; |
1131 | 0 | goto ready; |
1132 | 0 | } |
1133 | 0 | in_cert = 1; |
1134 | 0 | goto x_default; |
1135 | | |
1136 | 0 | default: |
1137 | 0 | x_default: |
1138 | 0 | if (in_cert && valid_keyblock_packet (pkt->pkttype)) |
1139 | 0 | { |
1140 | 0 | if (!root ) |
1141 | 0 | root = lastnode = new_kbnode (pkt); |
1142 | 0 | else |
1143 | 0 | { |
1144 | 0 | lastnode->next = new_kbnode (pkt); |
1145 | 0 | lastnode = lastnode->next; |
1146 | 0 | } |
1147 | 0 | pkt = xmalloc (sizeof *pkt); |
1148 | 0 | } |
1149 | 0 | else |
1150 | 0 | free_packet (pkt, &parsectx); |
1151 | 0 | init_packet(pkt); |
1152 | 0 | break; |
1153 | 0 | } |
1154 | 0 | } |
1155 | | |
1156 | 0 | ready: |
1157 | 0 | if (rc == -1 && root ) |
1158 | 0 | rc = 0; |
1159 | |
|
1160 | 0 | if (rc ) |
1161 | 0 | release_kbnode( root ); |
1162 | 0 | else |
1163 | 0 | *ret_root = root; |
1164 | 0 | free_packet (pkt, &parsectx); |
1165 | 0 | deinit_parse_packet (&parsectx); |
1166 | 0 | xfree( pkt ); |
1167 | 0 | if (!rc && dropped_nonselfsigs && opt.verbose) |
1168 | 0 | log_info ("key %s: number of dropped non-self-signatures: %u\n", |
1169 | 0 | keystr (keyid), dropped_nonselfsigs); |
1170 | |
|
1171 | 0 | return rc; |
1172 | 0 | } |
1173 | | |
1174 | | |
1175 | | /* Walk through the subkeys on a pk to find if we have the PKS |
1176 | | disease: multiple subkeys with their binding sigs stripped, and the |
1177 | | sig for the first subkey placed after the last subkey. That is, |
1178 | | instead of "pk uid sig sub1 bind1 sub2 bind2 sub3 bind3" we have |
1179 | | "pk uid sig sub1 sub2 sub3 bind1". We can't do anything about sub2 |
1180 | | and sub3, as they are already lost, but we can try and rescue sub1 |
1181 | | by reordering the keyblock so that it reads "pk uid sig sub1 bind1 |
1182 | | sub2 sub3". Returns TRUE if the keyblock was modified. */ |
1183 | | static int |
1184 | | fix_pks_corruption (ctrl_t ctrl, kbnode_t keyblock) |
1185 | 0 | { |
1186 | 0 | int changed = 0; |
1187 | 0 | int keycount = 0; |
1188 | 0 | kbnode_t node; |
1189 | 0 | kbnode_t last = NULL; |
1190 | 0 | kbnode_t sknode=NULL; |
1191 | | |
1192 | | /* First determine if we have the problem at all. Look for 2 or |
1193 | | more subkeys in a row, followed by a single binding sig. */ |
1194 | 0 | for (node=keyblock; node; last=node, node=node->next) |
1195 | 0 | { |
1196 | 0 | if (node->pkt->pkttype == PKT_PUBLIC_SUBKEY) |
1197 | 0 | { |
1198 | 0 | keycount++; |
1199 | 0 | if(!sknode) |
1200 | 0 | sknode=node; |
1201 | 0 | } |
1202 | 0 | else if (node->pkt->pkttype == PKT_SIGNATURE |
1203 | 0 | && IS_SUBKEY_SIG (node->pkt->pkt.signature) |
1204 | 0 | && keycount >= 2 |
1205 | 0 | && !node->next) |
1206 | 0 | { |
1207 | | /* We might have the problem, as this key has two subkeys in |
1208 | | a row without any intervening packets. */ |
1209 | | |
1210 | | /* Sanity check */ |
1211 | 0 | if (!last) |
1212 | 0 | break; |
1213 | | |
1214 | | /* Temporarily attach node to sknode. */ |
1215 | 0 | node->next = sknode->next; |
1216 | 0 | sknode->next = node; |
1217 | 0 | last->next = NULL; |
1218 | | |
1219 | | /* Note we aren't checking whether this binding sig is a |
1220 | | selfsig. This is not necessary here as the subkey and |
1221 | | binding sig will be rejected later if that is the |
1222 | | case. */ |
1223 | 0 | if (check_key_signature (ctrl, keyblock,node,NULL)) |
1224 | 0 | { |
1225 | | /* Not a match, so undo the changes. */ |
1226 | 0 | sknode->next = node->next; |
1227 | 0 | last->next = node; |
1228 | 0 | node->next = NULL; |
1229 | 0 | break; |
1230 | 0 | } |
1231 | 0 | else |
1232 | 0 | { |
1233 | | /* Mark it good so we don't need to check it again */ |
1234 | 0 | sknode->flag |= NODE_GOOD_SELFSIG; |
1235 | 0 | changed = 1; |
1236 | 0 | break; |
1237 | 0 | } |
1238 | 0 | } |
1239 | 0 | else |
1240 | 0 | keycount = 0; |
1241 | 0 | } |
1242 | |
|
1243 | 0 | return changed; |
1244 | 0 | } |
1245 | | |
1246 | | |
1247 | | /* Versions of GnuPG before 1.4.11 and 2.0.16 allowed to import bogus |
1248 | | direct key signatures. A side effect of this was that a later |
1249 | | import of the same good direct key signatures was not possible |
1250 | | because the cmp_signature check in merge_blocks considered them |
1251 | | equal. Although direct key signatures are now checked during |
1252 | | import, there might still be bogus signatures sitting in a keyring. |
1253 | | We need to detect and delete them before doing a merge. This |
1254 | | function returns the number of removed sigs. */ |
1255 | | static int |
1256 | | fix_bad_direct_key_sigs (ctrl_t ctrl, kbnode_t keyblock, u32 *keyid) |
1257 | 0 | { |
1258 | 0 | gpg_error_t err; |
1259 | 0 | kbnode_t node; |
1260 | 0 | int count = 0; |
1261 | |
|
1262 | 0 | for (node = keyblock->next; node; node=node->next) |
1263 | 0 | { |
1264 | 0 | if (node->pkt->pkttype == PKT_USER_ID) |
1265 | 0 | break; |
1266 | 0 | if (node->pkt->pkttype == PKT_SIGNATURE |
1267 | 0 | && IS_KEY_SIG (node->pkt->pkt.signature)) |
1268 | 0 | { |
1269 | 0 | err = check_key_signature (ctrl, keyblock, node, NULL); |
1270 | 0 | if (err && gpg_err_code (err) != GPG_ERR_PUBKEY_ALGO ) |
1271 | 0 | { |
1272 | | /* If we don't know the error, we can't decide; this is |
1273 | | not a problem because cmp_signature can't compare the |
1274 | | signature either. */ |
1275 | 0 | log_info ("key %s: invalid direct key signature removed\n", |
1276 | 0 | keystr (keyid)); |
1277 | 0 | delete_kbnode (node); |
1278 | 0 | count++; |
1279 | 0 | } |
1280 | 0 | } |
1281 | 0 | } |
1282 | |
|
1283 | 0 | return count; |
1284 | 0 | } |
1285 | | |
1286 | | |
1287 | | static void |
1288 | | print_import_ok (PKT_public_key *pk, unsigned int reason) |
1289 | 0 | { |
1290 | 0 | byte array[MAX_FINGERPRINT_LEN], *s; |
1291 | 0 | char buf[MAX_FINGERPRINT_LEN*2+30], *p; |
1292 | 0 | size_t i, n; |
1293 | |
|
1294 | 0 | snprintf (buf, sizeof buf, "%u ", reason); |
1295 | 0 | p = buf + strlen (buf); |
1296 | |
|
1297 | 0 | fingerprint_from_pk (pk, array, &n); |
1298 | 0 | s = array; |
1299 | 0 | for (i=0; i < n ; i++, s++, p += 2) |
1300 | 0 | sprintf (p, "%02X", *s); |
1301 | |
|
1302 | 0 | write_status_text (STATUS_IMPORT_OK, buf); |
1303 | 0 | } |
1304 | | |
1305 | | |
1306 | | static void |
1307 | | print_import_check (PKT_public_key * pk, PKT_user_id * id) |
1308 | 0 | { |
1309 | 0 | byte hexfpr[2*MAX_FINGERPRINT_LEN+1]; |
1310 | 0 | u32 keyid[2]; |
1311 | |
|
1312 | 0 | keyid_from_pk (pk, keyid); |
1313 | 0 | hexfingerprint (pk, hexfpr, sizeof hexfpr); |
1314 | 0 | write_status_printf (STATUS_IMPORT_CHECK, "%08X%08X %s %s", |
1315 | 0 | keyid[0], keyid[1], hexfpr, id->name); |
1316 | |
|
1317 | 0 | } |
1318 | | |
1319 | | |
1320 | | static void |
1321 | | check_prefs_warning(PKT_public_key *pk) |
1322 | 0 | { |
1323 | 0 | log_info(_("WARNING: key %s contains preferences for unavailable\n" |
1324 | 0 | "algorithms on these user IDs:\n"), keystr_from_pk(pk)); |
1325 | 0 | } |
1326 | | |
1327 | | |
1328 | | static void |
1329 | | check_prefs (ctrl_t ctrl, kbnode_t keyblock) |
1330 | 0 | { |
1331 | 0 | kbnode_t node; |
1332 | 0 | PKT_public_key *pk; |
1333 | 0 | int problem=0; |
1334 | |
|
1335 | 0 | merge_keys_and_selfsig (ctrl, keyblock); |
1336 | 0 | pk=keyblock->pkt->pkt.public_key; |
1337 | |
|
1338 | 0 | for(node=keyblock;node;node=node->next) |
1339 | 0 | { |
1340 | 0 | if(node->pkt->pkttype==PKT_USER_ID |
1341 | 0 | && node->pkt->pkt.user_id->created |
1342 | 0 | && node->pkt->pkt.user_id->prefs) |
1343 | 0 | { |
1344 | 0 | PKT_user_id *uid = node->pkt->pkt.user_id; |
1345 | 0 | prefitem_t *prefs = uid->prefs; |
1346 | 0 | char *user = utf8_to_native(uid->name,strlen(uid->name),0); |
1347 | |
|
1348 | 0 | for(;prefs->type;prefs++) |
1349 | 0 | { |
1350 | 0 | char num[10]; /* prefs->value is a byte, so we're over |
1351 | | safe here */ |
1352 | |
|
1353 | 0 | sprintf(num,"%u",prefs->value); |
1354 | |
|
1355 | 0 | if(prefs->type==PREFTYPE_SYM) |
1356 | 0 | { |
1357 | 0 | if (openpgp_cipher_test_algo (prefs->value)) |
1358 | 0 | { |
1359 | 0 | const char *algo = |
1360 | 0 | (openpgp_cipher_test_algo (prefs->value) |
1361 | 0 | ? num |
1362 | 0 | : openpgp_cipher_algo_name (prefs->value)); |
1363 | 0 | if(!problem) |
1364 | 0 | check_prefs_warning(pk); |
1365 | 0 | log_info(_(" \"%s\": preference for cipher" |
1366 | 0 | " algorithm %s\n"), user, algo); |
1367 | 0 | problem=1; |
1368 | 0 | } |
1369 | 0 | } |
1370 | 0 | else if(prefs->type==PREFTYPE_AEAD) |
1371 | 0 | { |
1372 | 0 | if (openpgp_aead_test_algo (prefs->value)) |
1373 | 0 | { |
1374 | | /* FIXME: The test below is wrong. We should |
1375 | | * check if ...algo_name yields a "?" and |
1376 | | * only in that case use NUM. */ |
1377 | 0 | const char *algo = |
1378 | 0 | (openpgp_aead_test_algo (prefs->value) |
1379 | 0 | ? num |
1380 | 0 | : openpgp_aead_algo_name (prefs->value)); |
1381 | 0 | if(!problem) |
1382 | 0 | check_prefs_warning(pk); |
1383 | 0 | log_info(_(" \"%s\": preference for AEAD" |
1384 | 0 | " algorithm %s\n"), user, algo); |
1385 | 0 | problem=1; |
1386 | 0 | } |
1387 | 0 | } |
1388 | 0 | else if(prefs->type==PREFTYPE_HASH) |
1389 | 0 | { |
1390 | 0 | if(openpgp_md_test_algo(prefs->value)) |
1391 | 0 | { |
1392 | 0 | const char *algo = |
1393 | 0 | (gcry_md_test_algo (prefs->value) |
1394 | 0 | ? num |
1395 | 0 | : gcry_md_algo_name (prefs->value)); |
1396 | 0 | if(!problem) |
1397 | 0 | check_prefs_warning(pk); |
1398 | 0 | log_info(_(" \"%s\": preference for digest" |
1399 | 0 | " algorithm %s\n"), user, algo); |
1400 | 0 | problem=1; |
1401 | 0 | } |
1402 | 0 | } |
1403 | 0 | else if(prefs->type==PREFTYPE_ZIP) |
1404 | 0 | { |
1405 | 0 | if(check_compress_algo (prefs->value)) |
1406 | 0 | { |
1407 | 0 | const char *algo=compress_algo_to_string(prefs->value); |
1408 | 0 | if(!problem) |
1409 | 0 | check_prefs_warning(pk); |
1410 | 0 | log_info(_(" \"%s\": preference for compression" |
1411 | 0 | " algorithm %s\n"),user,algo?algo:num); |
1412 | 0 | problem=1; |
1413 | 0 | } |
1414 | 0 | } |
1415 | 0 | } |
1416 | |
|
1417 | 0 | xfree(user); |
1418 | 0 | } |
1419 | 0 | } |
1420 | |
|
1421 | 0 | if(problem) |
1422 | 0 | { |
1423 | 0 | log_info(_("it is strongly suggested that you update" |
1424 | 0 | " your preferences and\n")); |
1425 | 0 | log_info(_("re-distribute this key to avoid potential algorithm" |
1426 | 0 | " mismatch problems\n")); |
1427 | |
|
1428 | 0 | if(!opt.batch) |
1429 | 0 | { |
1430 | 0 | strlist_t sl = NULL; |
1431 | 0 | strlist_t locusr = NULL; |
1432 | 0 | size_t fprlen=0; |
1433 | 0 | byte fpr[MAX_FINGERPRINT_LEN], *p; |
1434 | 0 | char username[(MAX_FINGERPRINT_LEN*2)+1]; |
1435 | 0 | unsigned int i; |
1436 | |
|
1437 | 0 | p = fingerprint_from_pk (pk,fpr,&fprlen); |
1438 | 0 | for(i=0;i<fprlen;i++,p++) |
1439 | 0 | sprintf(username+2*i,"%02X",*p); |
1440 | 0 | add_to_strlist(&locusr,username); |
1441 | |
|
1442 | 0 | append_to_strlist(&sl,"updpref"); |
1443 | 0 | append_to_strlist(&sl,"save"); |
1444 | |
|
1445 | 0 | keyedit_menu (ctrl, username, locusr, sl, 1, 1 ); |
1446 | 0 | free_strlist(sl); |
1447 | 0 | free_strlist(locusr); |
1448 | 0 | } |
1449 | 0 | else if(!opt.quiet) |
1450 | 0 | log_info(_("you can update your preferences with:" |
1451 | 0 | " gpg --edit-key %s updpref save\n"),keystr_from_pk(pk)); |
1452 | 0 | } |
1453 | 0 | } |
1454 | | |
1455 | | |
1456 | | /* Helper for apply_*_filter in import.c and export.c and also used by |
1457 | | * keylist.c. */ |
1458 | | const char * |
1459 | | impex_filter_getval (void *cookie, const char *propname) |
1460 | 0 | { |
1461 | | /* FIXME: Malloc our static buffers and access them via PARM. */ |
1462 | 0 | struct impex_filter_parm_s *parm = cookie; |
1463 | 0 | ctrl_t ctrl = parm->ctrl; |
1464 | 0 | kbnode_t node = parm->node; |
1465 | 0 | static char numbuf[20]; |
1466 | 0 | const char *result; |
1467 | 0 | const char *s; |
1468 | 0 | enum { scpNone = 0, scpPub, scpSub, scpUid, scpSig} scope = 0; |
1469 | |
|
1470 | 0 | log_assert (ctrl && ctrl->magic == SERVER_CONTROL_MAGIC); |
1471 | | |
1472 | | /* We allow a prefix delimited by a slash to limit the scope of the |
1473 | | * keyword. Note that "pub" also includes "sec" and "sub" includes |
1474 | | * "ssb". */ |
1475 | 0 | if (DBG_RECSEL) /* Printing the packet type is useful. */ |
1476 | 0 | log_debug ("%s: pkttype=%s\n", __func__, pkttype_str (node->pkt->pkttype)); |
1477 | 0 | if ((s=strchr (propname, '/')) && s != propname) |
1478 | 0 | { |
1479 | 0 | size_t n = s - propname; |
1480 | 0 | if (!strncmp (propname, "pub", n)) |
1481 | 0 | scope = scpPub; |
1482 | 0 | else if (!strncmp (propname, "sub", n)) |
1483 | 0 | scope = scpSub; |
1484 | 0 | else if (!strncmp (propname, "uid", n)) |
1485 | 0 | scope = scpUid; |
1486 | 0 | else if (!strncmp (propname, "sig", n)) |
1487 | 0 | scope = scpSig; |
1488 | |
|
1489 | 0 | propname = s + 1; |
1490 | 0 | } |
1491 | |
|
1492 | 0 | if ((node->pkt->pkttype == PKT_USER_ID |
1493 | 0 | || node->pkt->pkttype == PKT_ATTRIBUTE) |
1494 | 0 | && (!scope || scope == scpUid)) |
1495 | 0 | { |
1496 | 0 | PKT_user_id *uid = node->pkt->pkt.user_id; |
1497 | |
|
1498 | 0 | if (!strcmp (propname, "uid")) |
1499 | 0 | result = uid->name; |
1500 | 0 | else if (!strcmp (propname, "mbox")) |
1501 | 0 | { |
1502 | 0 | if (!uid->mbox) |
1503 | 0 | { |
1504 | 0 | uid->mbox = mailbox_from_userid (uid->name, 0); |
1505 | 0 | } |
1506 | 0 | result = uid->mbox; |
1507 | 0 | } |
1508 | 0 | else if (!strcmp (propname, "primary")) |
1509 | 0 | { |
1510 | 0 | result = uid->flags.primary? "1":"0"; |
1511 | 0 | } |
1512 | 0 | else if (!strcmp (propname, "expired")) |
1513 | 0 | { |
1514 | 0 | result = uid->flags.expired? "1":"0"; |
1515 | 0 | } |
1516 | 0 | else if (!strcmp (propname, "revoked")) |
1517 | 0 | { |
1518 | 0 | result = uid->flags.revoked? "1":"0"; |
1519 | 0 | } |
1520 | 0 | else |
1521 | 0 | result = NULL; |
1522 | 0 | } |
1523 | 0 | else if (node->pkt->pkttype == PKT_SIGNATURE |
1524 | 0 | && (!scope || scope == scpSig)) |
1525 | 0 | { |
1526 | 0 | PKT_signature *sig = node->pkt->pkt.signature; |
1527 | |
|
1528 | 0 | if (!strcmp (propname, "sig_created")) |
1529 | 0 | { |
1530 | 0 | snprintf (numbuf, sizeof numbuf, "%lu", (ulong)sig->timestamp); |
1531 | 0 | result = numbuf; |
1532 | 0 | } |
1533 | 0 | else if (!strcmp (propname, "sig_created_d")) |
1534 | 0 | { |
1535 | 0 | result = dateonlystr_from_sig (sig); |
1536 | 0 | } |
1537 | 0 | else if (!strcmp (propname, "sig_expires")) |
1538 | 0 | { |
1539 | 0 | snprintf (numbuf, sizeof numbuf, "%lu", (ulong)sig->expiredate); |
1540 | 0 | result = numbuf; |
1541 | 0 | } |
1542 | 0 | else if (!strcmp (propname, "sig_expires_d")) |
1543 | 0 | { |
1544 | 0 | static char exdatestr[MK_DATESTR_SIZE]; |
1545 | |
|
1546 | 0 | if (sig->expiredate) |
1547 | 0 | result = mk_datestr (exdatestr, sizeof exdatestr, sig->expiredate); |
1548 | 0 | else |
1549 | 0 | result = ""; |
1550 | 0 | } |
1551 | 0 | else if (!strcmp (propname, "sig_algo")) |
1552 | 0 | { |
1553 | 0 | snprintf (numbuf, sizeof numbuf, "%d", sig->pubkey_algo); |
1554 | 0 | result = numbuf; |
1555 | 0 | } |
1556 | 0 | else if (!strcmp (propname, "sig_digest_algo")) |
1557 | 0 | { |
1558 | 0 | snprintf (numbuf, sizeof numbuf, "%d", sig->digest_algo); |
1559 | 0 | result = numbuf; |
1560 | 0 | } |
1561 | 0 | else if (!strcmp (propname, "expired")) |
1562 | 0 | { |
1563 | 0 | result = sig->flags.expired? "1":"0"; |
1564 | 0 | } |
1565 | 0 | else |
1566 | 0 | result = NULL; |
1567 | 0 | } |
1568 | 0 | else if (((node->pkt->pkttype == PKT_PUBLIC_KEY |
1569 | 0 | || node->pkt->pkttype == PKT_SECRET_KEY) |
1570 | 0 | && (!scope || scope == scpPub)) |
1571 | 0 | || ((node->pkt->pkttype == PKT_PUBLIC_SUBKEY |
1572 | 0 | || node->pkt->pkttype == PKT_SECRET_SUBKEY) |
1573 | 0 | && (!scope || scope == scpSub))) |
1574 | 0 | { |
1575 | 0 | PKT_public_key *pk = node->pkt->pkt.public_key; |
1576 | |
|
1577 | 0 | if (!strcmp (propname, "secret")) |
1578 | 0 | { |
1579 | 0 | result = (node->pkt->pkttype == PKT_SECRET_KEY |
1580 | 0 | || node->pkt->pkttype == PKT_SECRET_SUBKEY)? "1":"0"; |
1581 | 0 | } |
1582 | 0 | else if (!strcmp (propname, "key_algo")) |
1583 | 0 | { |
1584 | 0 | snprintf (numbuf, sizeof numbuf, "%d", pk->pubkey_algo); |
1585 | 0 | result = numbuf; |
1586 | 0 | } |
1587 | 0 | else if (!strcmp (propname, "key_size")) |
1588 | 0 | { |
1589 | 0 | snprintf (numbuf, sizeof numbuf, "%u", nbits_from_pk (pk)); |
1590 | 0 | result = numbuf; |
1591 | 0 | } |
1592 | 0 | else if (!strcmp (propname, "algostr")) |
1593 | 0 | { |
1594 | 0 | pubkey_string (pk, parm->hexfpr, sizeof parm->hexfpr); |
1595 | 0 | result = parm->hexfpr; |
1596 | 0 | } |
1597 | 0 | else if (!strcmp (propname, "key_created")) |
1598 | 0 | { |
1599 | 0 | snprintf (numbuf, sizeof numbuf, "%lu", (ulong)pk->timestamp); |
1600 | 0 | result = numbuf; |
1601 | 0 | } |
1602 | 0 | else if (!strcmp (propname, "key_created_d")) |
1603 | 0 | { |
1604 | 0 | result = dateonlystr_from_pk (pk); |
1605 | 0 | } |
1606 | 0 | else if (!strcmp (propname, "key_expires")) |
1607 | 0 | { |
1608 | 0 | snprintf (numbuf, sizeof numbuf, "%lu", (ulong)pk->expiredate); |
1609 | 0 | result = numbuf; |
1610 | 0 | } |
1611 | 0 | else if (!strcmp (propname, "key_expires_d")) |
1612 | 0 | { |
1613 | 0 | static char exdatestr[MK_DATESTR_SIZE]; |
1614 | |
|
1615 | 0 | if (pk->expiredate) |
1616 | 0 | result = mk_datestr (exdatestr, sizeof exdatestr, pk->expiredate); |
1617 | 0 | else |
1618 | 0 | result = ""; |
1619 | 0 | } |
1620 | 0 | else if (!strcmp (propname, "expired")) |
1621 | 0 | { |
1622 | 0 | result = pk->has_expired? "1":"0"; |
1623 | 0 | } |
1624 | 0 | else if (!strcmp (propname, "revoked")) |
1625 | 0 | { |
1626 | 0 | result = pk->flags.revoked? "1":"0"; |
1627 | 0 | } |
1628 | 0 | else if (!strcmp (propname, "disabled")) |
1629 | 0 | { |
1630 | 0 | result = pk_is_disabled (pk)? "1":"0"; |
1631 | 0 | } |
1632 | 0 | else if (!strcmp (propname, "usage")) |
1633 | 0 | { |
1634 | 0 | snprintf (numbuf, sizeof numbuf, "%s%s%s%s%s", |
1635 | 0 | (pk->pubkey_usage & PUBKEY_USAGE_ENC)?"e":"", |
1636 | 0 | (pk->pubkey_usage & PUBKEY_USAGE_SIG)?"s":"", |
1637 | 0 | (pk->pubkey_usage & PUBKEY_USAGE_CERT)?"c":"", |
1638 | 0 | (pk->pubkey_usage & PUBKEY_USAGE_AUTH)?"a":"", |
1639 | 0 | (pk->pubkey_usage & PUBKEY_USAGE_UNKNOWN)?"?":""); |
1640 | 0 | result = numbuf; |
1641 | 0 | } |
1642 | 0 | else if (!strcmp (propname, "fpr")) |
1643 | 0 | { |
1644 | 0 | hexfingerprint (pk, parm->hexfpr, sizeof parm->hexfpr); |
1645 | 0 | result = parm->hexfpr; |
1646 | 0 | } |
1647 | 0 | else if (!strcmp (propname, "origin")) |
1648 | 0 | { |
1649 | 0 | result = key_origin_string (pk->keyorg); |
1650 | 0 | } |
1651 | 0 | else if (!strcmp (propname, "lastupd")) |
1652 | 0 | { |
1653 | 0 | snprintf (numbuf, sizeof numbuf, "%lu", (ulong)pk->keyupdate); |
1654 | 0 | result = numbuf; |
1655 | 0 | } |
1656 | 0 | else if (!strcmp (propname, "url")) |
1657 | 0 | { |
1658 | 0 | if (pk->updateurl && *pk->updateurl) |
1659 | 0 | { |
1660 | | /* Fixme: This might get truncated. */ |
1661 | 0 | mem2str (parm->hexfpr, pk->updateurl, sizeof parm->hexfpr); |
1662 | 0 | result = parm->hexfpr; |
1663 | 0 | } |
1664 | 0 | else |
1665 | 0 | result = ""; |
1666 | 0 | } |
1667 | 0 | else |
1668 | 0 | result = NULL; |
1669 | 0 | } |
1670 | 0 | else |
1671 | 0 | result = NULL; |
1672 | |
|
1673 | 0 | return result; |
1674 | 0 | } |
1675 | | |
1676 | | |
1677 | | /* |
1678 | | * Apply the keep-uid filter to the keyblock. The deleted nodes are |
1679 | | * marked and thus the caller should call commit_kbnode afterwards. |
1680 | | * KEYBLOCK must not have any blocks marked as deleted. |
1681 | | */ |
1682 | | static void |
1683 | | apply_keep_uid_filter (ctrl_t ctrl, kbnode_t keyblock, recsel_expr_t selector) |
1684 | 0 | { |
1685 | 0 | kbnode_t node; |
1686 | 0 | struct impex_filter_parm_s parm; |
1687 | |
|
1688 | 0 | parm.ctrl = ctrl; |
1689 | |
|
1690 | 0 | for (node = keyblock->next; node; node = node->next ) |
1691 | 0 | { |
1692 | 0 | if (node->pkt->pkttype == PKT_USER_ID) |
1693 | 0 | { |
1694 | 0 | parm.node = node; |
1695 | 0 | if (!recsel_select (selector, impex_filter_getval, &parm)) |
1696 | 0 | { |
1697 | | |
1698 | | /* log_debug ("keep-uid: deleting '%s'\n", */ |
1699 | | /* node->pkt->pkt.user_id->name); */ |
1700 | | /* The UID packet and all following packets up to the |
1701 | | * next UID or a subkey. */ |
1702 | 0 | delete_kbnode (node); |
1703 | 0 | for (; node->next |
1704 | 0 | && node->next->pkt->pkttype != PKT_USER_ID |
1705 | 0 | && node->next->pkt->pkttype != PKT_PUBLIC_SUBKEY |
1706 | 0 | && node->next->pkt->pkttype != PKT_SECRET_SUBKEY ; |
1707 | 0 | node = node->next) |
1708 | 0 | delete_kbnode (node->next); |
1709 | 0 | } |
1710 | | /* else */ |
1711 | | /* log_debug ("keep-uid: keeping '%s'\n", */ |
1712 | | /* node->pkt->pkt.user_id->name); */ |
1713 | 0 | } |
1714 | 0 | } |
1715 | 0 | } |
1716 | | |
1717 | | |
1718 | | /* |
1719 | | * Apply the drop-sig filter to the keyblock. The deleted nodes are |
1720 | | * marked and thus the caller should call commit_kbnode afterwards. |
1721 | | * KEYBLOCK must not have any blocks marked as deleted. |
1722 | | */ |
1723 | | static void |
1724 | | apply_drop_sig_filter (ctrl_t ctrl, kbnode_t keyblock, recsel_expr_t selector) |
1725 | 0 | { |
1726 | 0 | kbnode_t node; |
1727 | 0 | int active = 0; |
1728 | 0 | u32 main_keyid[2]; |
1729 | 0 | PKT_signature *sig; |
1730 | 0 | struct impex_filter_parm_s parm; |
1731 | |
|
1732 | 0 | parm.ctrl = ctrl; |
1733 | |
|
1734 | 0 | keyid_from_pk (keyblock->pkt->pkt.public_key, main_keyid); |
1735 | | |
1736 | | /* Loop over all signatures for user id and attribute packets which |
1737 | | * are not self signatures. */ |
1738 | 0 | for (node = keyblock->next; node; node = node->next ) |
1739 | 0 | { |
1740 | 0 | if (node->pkt->pkttype == PKT_PUBLIC_SUBKEY |
1741 | 0 | || node->pkt->pkttype == PKT_SECRET_SUBKEY) |
1742 | 0 | break; /* ready. */ |
1743 | 0 | if (node->pkt->pkttype == PKT_USER_ID |
1744 | 0 | || node->pkt->pkttype == PKT_ATTRIBUTE) |
1745 | 0 | active = 1; |
1746 | 0 | if (!active) |
1747 | 0 | continue; |
1748 | 0 | if (node->pkt->pkttype != PKT_SIGNATURE) |
1749 | 0 | continue; |
1750 | | |
1751 | 0 | sig = node->pkt->pkt.signature; |
1752 | 0 | if (main_keyid[0] == sig->keyid[0] || main_keyid[1] == sig->keyid[1]) |
1753 | 0 | continue; /* Skip self-signatures. */ |
1754 | | |
1755 | 0 | if (IS_UID_SIG(sig) || IS_UID_REV(sig)) |
1756 | 0 | { |
1757 | 0 | parm.node = node; |
1758 | 0 | if (recsel_select (selector, impex_filter_getval, &parm)) |
1759 | 0 | delete_kbnode (node); |
1760 | 0 | } |
1761 | 0 | } |
1762 | 0 | } |
1763 | | |
1764 | | |
1765 | | /* Insert a key origin into a public key packet. */ |
1766 | | static gpg_error_t |
1767 | | insert_key_origin_pk (PKT_public_key *pk, u32 curtime, |
1768 | | int origin, const char *url) |
1769 | 0 | { |
1770 | 0 | if (origin == KEYORG_WKD || origin == KEYORG_DANE) |
1771 | 0 | { |
1772 | | /* For WKD and DANE we insert origin information also for the |
1773 | | * key but we don't record the URL because we have have no use |
1774 | | * for that: An update using a keyserver has higher precedence |
1775 | | * and will thus update this origin info. For refresh using WKD |
1776 | | * or DANE we need to go via the User ID anyway. Recall that we |
1777 | | * are only inserting a new key. */ |
1778 | 0 | pk->keyorg = origin; |
1779 | 0 | pk->keyupdate = curtime; |
1780 | 0 | } |
1781 | 0 | else if (origin == KEYORG_KS && url) |
1782 | 0 | { |
1783 | | /* If the key was retrieved from a keyserver using a fingerprint |
1784 | | * request we add the meta information. Note that the use of a |
1785 | | * fingerprint needs to be enforced by the caller of the import |
1786 | | * function. This is commonly triggered by verifying a modern |
1787 | | * signature which has an Issuer Fingerprint signature |
1788 | | * subpacket. */ |
1789 | 0 | pk->keyorg = origin; |
1790 | 0 | pk->keyupdate = curtime; |
1791 | 0 | xfree (pk->updateurl); |
1792 | 0 | pk->updateurl = xtrystrdup (url); |
1793 | 0 | if (!pk->updateurl) |
1794 | 0 | return gpg_error_from_syserror (); |
1795 | 0 | } |
1796 | 0 | else if (origin == KEYORG_FILE) |
1797 | 0 | { |
1798 | 0 | pk->keyorg = origin; |
1799 | 0 | pk->keyupdate = curtime; |
1800 | 0 | } |
1801 | 0 | else if (origin == KEYORG_URL) |
1802 | 0 | { |
1803 | 0 | pk->keyorg = origin; |
1804 | 0 | pk->keyupdate = curtime; |
1805 | 0 | if (url) |
1806 | 0 | { |
1807 | 0 | xfree (pk->updateurl); |
1808 | 0 | pk->updateurl = xtrystrdup (url); |
1809 | 0 | if (!pk->updateurl) |
1810 | 0 | return gpg_error_from_syserror (); |
1811 | 0 | } |
1812 | 0 | } |
1813 | | |
1814 | 0 | return 0; |
1815 | 0 | } |
1816 | | |
1817 | | |
1818 | | /* Insert a key origin into a user id packet. */ |
1819 | | static gpg_error_t |
1820 | | insert_key_origin_uid (PKT_user_id *uid, u32 curtime, |
1821 | | int origin, const char *url) |
1822 | | |
1823 | 0 | { |
1824 | 0 | if (origin == KEYORG_WKD || origin == KEYORG_DANE) |
1825 | 0 | { |
1826 | | /* We insert origin information on a UID only when we received |
1827 | | * them via the Web Key Directory or a DANE record. The key we |
1828 | | * receive here from the WKD has been filtered to contain only |
1829 | | * the user ID as looked up in the WKD. For a DANE origin |
1830 | | * this should also be the case. Thus we will see here only one |
1831 | | * user id. */ |
1832 | 0 | uid->keyorg = origin; |
1833 | 0 | uid->keyupdate = curtime; |
1834 | 0 | if (url) |
1835 | 0 | { |
1836 | 0 | xfree (uid->updateurl); |
1837 | 0 | uid->updateurl = xtrystrdup (url); |
1838 | 0 | if (!uid->updateurl) |
1839 | 0 | return gpg_error_from_syserror (); |
1840 | 0 | } |
1841 | 0 | } |
1842 | 0 | else if (origin == KEYORG_KS && url) |
1843 | 0 | { |
1844 | | /* If the key was retrieved from a keyserver using a fingerprint |
1845 | | * request we mark that also in the user ID. However we do not |
1846 | | * store the keyserver URL in the UID. A later update (merge) |
1847 | | * from a more trusted source will replace this info. */ |
1848 | 0 | uid->keyorg = origin; |
1849 | 0 | uid->keyupdate = curtime; |
1850 | 0 | } |
1851 | 0 | else if (origin == KEYORG_FILE) |
1852 | 0 | { |
1853 | 0 | uid->keyorg = origin; |
1854 | 0 | uid->keyupdate = curtime; |
1855 | 0 | } |
1856 | 0 | else if (origin == KEYORG_URL) |
1857 | 0 | { |
1858 | 0 | uid->keyorg = origin; |
1859 | 0 | uid->keyupdate = curtime; |
1860 | 0 | } |
1861 | | |
1862 | 0 | return 0; |
1863 | 0 | } |
1864 | | |
1865 | | |
1866 | | /* Apply meta data to KEYBLOCK. This sets the origin of the key to |
1867 | | * ORIGIN and the updateurl to URL. Note that this function is only |
1868 | | * used for a new key, that is not when we are merging keys. */ |
1869 | | static gpg_error_t |
1870 | | insert_key_origin (kbnode_t keyblock, int origin, const char *url) |
1871 | 0 | { |
1872 | 0 | gpg_error_t err; |
1873 | 0 | kbnode_t node; |
1874 | 0 | u32 curtime = make_timestamp (); |
1875 | |
|
1876 | 0 | for (node = keyblock; node; node = node->next) |
1877 | 0 | { |
1878 | 0 | if (is_deleted_kbnode (node)) |
1879 | 0 | ; |
1880 | 0 | else if (node->pkt->pkttype == PKT_PUBLIC_KEY) |
1881 | 0 | { |
1882 | 0 | err = insert_key_origin_pk (node->pkt->pkt.public_key, curtime, |
1883 | 0 | origin, url); |
1884 | 0 | if (err) |
1885 | 0 | return err; |
1886 | 0 | } |
1887 | 0 | else if (node->pkt->pkttype == PKT_USER_ID) |
1888 | 0 | { |
1889 | 0 | err = insert_key_origin_uid (node->pkt->pkt.user_id, curtime, |
1890 | 0 | origin, url); |
1891 | 0 | if (err) |
1892 | 0 | return err; |
1893 | 0 | } |
1894 | 0 | } |
1895 | | |
1896 | 0 | return 0; |
1897 | 0 | } |
1898 | | |
1899 | | |
1900 | | /* Update meta data on KEYBLOCK. This updates the key origin on the |
1901 | | * public key according to ORIGIN and URL. The UIDs are already |
1902 | | * updated when this function is called. */ |
1903 | | static gpg_error_t |
1904 | | update_key_origin (kbnode_t keyblock, u32 curtime, int origin, const char *url) |
1905 | 0 | { |
1906 | 0 | PKT_public_key *pk; |
1907 | |
|
1908 | 0 | log_assert (keyblock->pkt->pkttype == PKT_PUBLIC_KEY); |
1909 | 0 | pk = keyblock->pkt->pkt.public_key; |
1910 | |
|
1911 | 0 | if (pk->keyupdate > curtime) |
1912 | 0 | ; /* Don't do it for a time warp. */ |
1913 | 0 | else if (origin == KEYORG_WKD || origin == KEYORG_DANE) |
1914 | 0 | { |
1915 | | /* We only update the origin info if they either have never been |
1916 | | * set or are the origin was the same as the new one. If this |
1917 | | * is WKD we also update the UID to show from which user id this |
1918 | | * was updated. */ |
1919 | 0 | if (!pk->keyorg || pk->keyorg == KEYORG_WKD || pk->keyorg == KEYORG_DANE) |
1920 | 0 | { |
1921 | 0 | pk->keyorg = origin; |
1922 | 0 | pk->keyupdate = curtime; |
1923 | 0 | xfree (pk->updateurl); |
1924 | 0 | pk->updateurl = NULL; |
1925 | 0 | if (origin == KEYORG_WKD && url) |
1926 | 0 | { |
1927 | 0 | pk->updateurl = xtrystrdup (url); |
1928 | 0 | if (!pk->updateurl) |
1929 | 0 | return gpg_error_from_syserror (); |
1930 | 0 | } |
1931 | 0 | } |
1932 | 0 | } |
1933 | 0 | else if (origin == KEYORG_KS) |
1934 | 0 | { |
1935 | | /* All updates from a keyserver are considered to have the |
1936 | | * freshed key. Thus we always set the new key origin. */ |
1937 | 0 | pk->keyorg = origin; |
1938 | 0 | pk->keyupdate = curtime; |
1939 | 0 | xfree (pk->updateurl); |
1940 | 0 | pk->updateurl = NULL; |
1941 | 0 | if (url) |
1942 | 0 | { |
1943 | 0 | pk->updateurl = xtrystrdup (url); |
1944 | 0 | if (!pk->updateurl) |
1945 | 0 | return gpg_error_from_syserror (); |
1946 | 0 | } |
1947 | 0 | } |
1948 | 0 | else if (origin == KEYORG_FILE) |
1949 | 0 | { |
1950 | | /* Updates from a file are considered to be fresh. */ |
1951 | 0 | pk->keyorg = origin; |
1952 | 0 | pk->keyupdate = curtime; |
1953 | 0 | xfree (pk->updateurl); |
1954 | 0 | pk->updateurl = NULL; |
1955 | 0 | } |
1956 | 0 | else if (origin == KEYORG_URL) |
1957 | 0 | { |
1958 | | /* Updates from a URL are considered to be fresh. */ |
1959 | 0 | pk->keyorg = origin; |
1960 | 0 | pk->keyupdate = curtime; |
1961 | 0 | xfree (pk->updateurl); |
1962 | 0 | pk->updateurl = NULL; |
1963 | 0 | if (url) |
1964 | 0 | { |
1965 | 0 | pk->updateurl = xtrystrdup (url); |
1966 | 0 | if (!pk->updateurl) |
1967 | 0 | return gpg_error_from_syserror (); |
1968 | 0 | } |
1969 | 0 | } |
1970 | | |
1971 | 0 | return 0; |
1972 | 0 | } |
1973 | | |
1974 | | |
1975 | | /* |
1976 | | * Try to import one keyblock. Return an error only in serious cases, |
1977 | | * but never for an invalid keyblock. It uses log_error to increase |
1978 | | * the internal errorcount, so that invalid input can be detected by |
1979 | | * programs which called gpg. If SILENT is no messages are printed - |
1980 | | * even most error messages are suppressed. ORIGIN is the origin of |
1981 | | * the key (0 for unknown) and URL the corresponding URL. FROM_SK |
1982 | | * indicates that the key has been made from a secret key. If R_SAVED |
1983 | | * is not NULL a boolean will be stored indicating whether the |
1984 | | * keyblock has valid parts. Unless OTHERREVSIGS is NULL it is |
1985 | | * updated with encountered new revocation signatures. |
1986 | | */ |
1987 | | static gpg_error_t |
1988 | | import_one_real (ctrl_t ctrl, |
1989 | | kbnode_t keyblock, struct import_stats_s *stats, |
1990 | | unsigned char **fpr, size_t *fpr_len, unsigned int options, |
1991 | | int from_sk, int silent, |
1992 | | import_screener_t screener, void *screener_arg, |
1993 | | int origin, const char *url, int *r_valid, |
1994 | | kbnode_t *otherrevsigs) |
1995 | 0 | { |
1996 | 0 | gpg_error_t err = 0; |
1997 | 0 | PKT_public_key *pk; |
1998 | 0 | kbnode_t node, uidnode; |
1999 | 0 | kbnode_t keyblock_orig = NULL; |
2000 | 0 | byte fpr2[MAX_FINGERPRINT_LEN]; |
2001 | 0 | size_t fpr2len; |
2002 | 0 | u32 keyid[2]; |
2003 | 0 | int new_key = 0; |
2004 | 0 | int mod_key = 0; |
2005 | 0 | int same_key = 0; |
2006 | 0 | int non_self_or_utk = 0; |
2007 | 0 | char pkstrbuf[PUBKEY_STRING_SIZE]; |
2008 | 0 | int merge_keys_done = 0; |
2009 | 0 | int any_filter = 0; |
2010 | 0 | KEYDB_HANDLE hd = NULL; |
2011 | |
|
2012 | 0 | if (r_valid) |
2013 | 0 | *r_valid = 0; |
2014 | | |
2015 | | /* If show-only is active we don't won't any extra output. */ |
2016 | 0 | if ((options & (IMPORT_SHOW | IMPORT_DRY_RUN))) |
2017 | 0 | silent = 1; |
2018 | | |
2019 | | /* Get the key and print some info about it. */ |
2020 | 0 | node = find_kbnode( keyblock, PKT_PUBLIC_KEY ); |
2021 | 0 | if (!node ) |
2022 | 0 | BUG(); |
2023 | | |
2024 | 0 | pk = node->pkt->pkt.public_key; |
2025 | |
|
2026 | 0 | fingerprint_from_pk (pk, fpr2, &fpr2len); |
2027 | 0 | if (MAX_FINGERPRINT_LEN > fpr2len) |
2028 | 0 | memset (fpr2+fpr2len, 0, MAX_FINGERPRINT_LEN - fpr2len); |
2029 | 0 | keyid_from_pk( pk, keyid ); |
2030 | 0 | uidnode = find_next_kbnode( keyblock, PKT_USER_ID ); |
2031 | |
|
2032 | 0 | if (opt.verbose && !opt.interactive && !silent && !from_sk) |
2033 | 0 | { |
2034 | | /* Note that we do not print this info in FROM_SK mode |
2035 | | * because import_secret_one already printed that. */ |
2036 | 0 | log_info ("pub %s/%s %s ", |
2037 | 0 | pubkey_string (pk, pkstrbuf, sizeof pkstrbuf), |
2038 | 0 | keystr_from_pk(pk), datestr_from_pk(pk) ); |
2039 | 0 | if (uidnode) |
2040 | 0 | print_utf8_buffer (log_get_stream (), |
2041 | 0 | uidnode->pkt->pkt.user_id->name, |
2042 | 0 | uidnode->pkt->pkt.user_id->len ); |
2043 | 0 | log_printf ("\n"); |
2044 | 0 | } |
2045 | | |
2046 | |
|
2047 | 0 | if (!uidnode) |
2048 | 0 | { |
2049 | 0 | if (!silent) |
2050 | 0 | log_error( _("key %s: no user ID\n"), keystr_from_pk(pk)); |
2051 | 0 | return 0; |
2052 | 0 | } |
2053 | | |
2054 | 0 | if (screener && screener (keyblock, screener_arg)) |
2055 | 0 | { |
2056 | 0 | log_error (_("key %s: %s\n"), keystr_from_pk (pk), |
2057 | 0 | _("rejected by import screener")); |
2058 | 0 | return 0; |
2059 | 0 | } |
2060 | | |
2061 | 0 | if (opt.interactive && !silent) |
2062 | 0 | { |
2063 | 0 | if (is_status_enabled()) |
2064 | 0 | print_import_check (pk, uidnode->pkt->pkt.user_id); |
2065 | 0 | merge_keys_and_selfsig (ctrl, keyblock); |
2066 | 0 | tty_printf ("\n"); |
2067 | 0 | show_basic_key_info (ctrl, keyblock, from_sk); |
2068 | 0 | tty_printf ("\n"); |
2069 | 0 | if (!cpr_get_answer_is_yes ("import.okay", |
2070 | 0 | "Do you want to import this key? (y/N) ")) |
2071 | 0 | return 0; |
2072 | 0 | } |
2073 | | |
2074 | | /* Remove all non-self-sigs if requested. Note that this is a NOP if |
2075 | | * that option has been globally set but we may also be called |
2076 | | * latter with the already parsed keyblock and a locally changed |
2077 | | * option. This is why we need to remove them here as well. */ |
2078 | 0 | if ((options & IMPORT_SELF_SIGS_ONLY)) |
2079 | 0 | remove_all_non_self_sigs (&keyblock, keyid); |
2080 | | |
2081 | | /* Remove or collapse the user ids. */ |
2082 | 0 | if ((options & IMPORT_COLLAPSE_UIDS)) |
2083 | 0 | collapse_uids (&keyblock); |
2084 | |
|
2085 | 0 | if ((options & IMPORT_COLLAPSE_SUBKEYS)) |
2086 | 0 | collapse_subkeys (&keyblock); |
2087 | | |
2088 | | /* Clean the key that we're about to import, to cut down on things |
2089 | | that we have to clean later. This has no practical impact on the |
2090 | | end result, but does result in less logging which might confuse |
2091 | | the user. */ |
2092 | 0 | if ((options & IMPORT_CLEAN)) |
2093 | 0 | { |
2094 | 0 | merge_keys_and_selfsig (ctrl, keyblock); |
2095 | 0 | clean_all_uids (ctrl, keyblock, |
2096 | 0 | opt.verbose, |
2097 | 0 | (options&IMPORT_MINIMAL)? EXPORT_MINIMAL : 0, |
2098 | 0 | NULL, NULL); |
2099 | 0 | clean_all_subkeys (ctrl, keyblock, opt.verbose, KEY_CLEAN_NONE, |
2100 | 0 | NULL, NULL); |
2101 | 0 | } |
2102 | |
|
2103 | 0 | clear_kbnode_flags( keyblock ); |
2104 | |
|
2105 | 0 | if ((options&IMPORT_REPAIR_PKS_SUBKEY_BUG) |
2106 | 0 | && fix_pks_corruption (ctrl, keyblock) |
2107 | 0 | && opt.verbose) |
2108 | 0 | log_info (_("key %s: PKS subkey corruption repaired\n"), |
2109 | 0 | keystr_from_pk(pk)); |
2110 | |
|
2111 | 0 | if ((options & IMPORT_REPAIR_KEYS)) |
2112 | 0 | key_check_all_keysigs (ctrl, 1, keyblock, 0, 0); |
2113 | |
|
2114 | 0 | if (chk_self_sigs (ctrl, keyblock, keyid, &non_self_or_utk)) |
2115 | 0 | return 0; /* Invalid keyblock - error already printed. */ |
2116 | | |
2117 | | /* If the imported key is marked as ultimately trusted key (using |
2118 | | * --trusted-key), we set the flag so that we can later set the |
2119 | | * revalidation mark. */ |
2120 | 0 | if (!non_self_or_utk) |
2121 | 0 | { |
2122 | | /* Make sure the trustdb is initialized so that the UTK list is |
2123 | | * available. */ |
2124 | 0 | init_trustdb (ctrl, 1); |
2125 | 0 | if (tdb_keyid_is_utk (keyid)) |
2126 | 0 | non_self_or_utk = 2; |
2127 | 0 | } |
2128 | | |
2129 | | /* If we allow such a thing, mark unsigned uids as valid */ |
2130 | 0 | if (opt.allow_non_selfsigned_uid) |
2131 | 0 | { |
2132 | 0 | for (node=keyblock; node; node = node->next ) |
2133 | 0 | if (node->pkt->pkttype == PKT_USER_ID |
2134 | 0 | && !(node->flag & NODE_GOOD_SELFSIG) |
2135 | 0 | && !(node->flag & NODE_BAD_SELFSIG) ) |
2136 | 0 | { |
2137 | 0 | char *user=utf8_to_native(node->pkt->pkt.user_id->name, |
2138 | 0 | node->pkt->pkt.user_id->len,0); |
2139 | | /* Fake a good signature status for the user id. */ |
2140 | 0 | node->flag |= NODE_GOOD_SELFSIG; |
2141 | 0 | log_info( _("key %s: accepted non self-signed user ID \"%s\"\n"), |
2142 | 0 | keystr_from_pk(pk),user); |
2143 | 0 | xfree(user); |
2144 | 0 | } |
2145 | 0 | } |
2146 | | |
2147 | | /* Delete invalid parts and bail out if there are no user ids left. */ |
2148 | 0 | if (!delete_inv_parts (ctrl, keyblock, keyid, options, otherrevsigs)) |
2149 | 0 | { |
2150 | 0 | if (!silent) |
2151 | 0 | { |
2152 | 0 | log_error ( _("key %s: no valid user IDs\n"), keystr_from_pk(pk)); |
2153 | 0 | if (!opt.quiet) |
2154 | 0 | log_info(_("this may be caused by a missing self-signature\n")); |
2155 | 0 | } |
2156 | 0 | stats->no_user_id++; |
2157 | 0 | return 0; |
2158 | 0 | } |
2159 | | |
2160 | | /* Get rid of deleted nodes. */ |
2161 | 0 | commit_kbnode (&keyblock); |
2162 | | |
2163 | | /* Apply import filter. */ |
2164 | 0 | if (import_filter.keep_uid) |
2165 | 0 | { |
2166 | 0 | apply_keep_uid_filter (ctrl, keyblock, import_filter.keep_uid); |
2167 | 0 | commit_kbnode (&keyblock); |
2168 | 0 | any_filter = 1; |
2169 | 0 | } |
2170 | 0 | if (import_filter.drop_sig) |
2171 | 0 | { |
2172 | 0 | apply_drop_sig_filter (ctrl, keyblock, import_filter.drop_sig); |
2173 | 0 | commit_kbnode (&keyblock); |
2174 | 0 | any_filter = 1; |
2175 | 0 | } |
2176 | | |
2177 | | /* If we ran any filter we need to check that at least one user id |
2178 | | * is left in the keyring. Note that we do not use log_error in |
2179 | | * this case. */ |
2180 | 0 | if (any_filter && !any_uid_left (keyblock)) |
2181 | 0 | { |
2182 | 0 | if (!opt.quiet ) |
2183 | 0 | log_info ( _("key %s: no valid user IDs\n"), keystr_from_pk (pk)); |
2184 | 0 | stats->no_user_id++; |
2185 | 0 | return 0; |
2186 | 0 | } |
2187 | | |
2188 | | /* The keyblock is valid and ready for real import. */ |
2189 | 0 | if (r_valid) |
2190 | 0 | *r_valid = 1; |
2191 | | |
2192 | | /* Show the key in the form it is merged or inserted. We skip this |
2193 | | * if "import-export" is also active without --armor or the output |
2194 | | * file has explicily been given. */ |
2195 | 0 | if ((options & IMPORT_SHOW) |
2196 | 0 | && !((options & IMPORT_EXPORT) && !opt.armor && !opt.outfile)) |
2197 | 0 | { |
2198 | 0 | merge_keys_and_selfsig (ctrl, keyblock); |
2199 | 0 | merge_keys_done = 1; |
2200 | | /* Note that we do not want to show the validity because the key |
2201 | | * has not yet imported. */ |
2202 | 0 | err = list_keyblock_direct (ctrl, keyblock, from_sk, 0, |
2203 | 0 | opt.fingerprint || opt.with_fingerprint, 1); |
2204 | 0 | es_fflush (es_stdout); |
2205 | 0 | no_usable_encr_subkeys_warning (keyblock); |
2206 | 0 | if (err) |
2207 | 0 | goto leave; |
2208 | 0 | } |
2209 | | |
2210 | | /* Write the keyblock to the output and do not actually import. */ |
2211 | 0 | if ((options & IMPORT_EXPORT)) |
2212 | 0 | { |
2213 | 0 | if (!merge_keys_done) |
2214 | 0 | { |
2215 | 0 | merge_keys_and_selfsig (ctrl, keyblock); |
2216 | 0 | merge_keys_done = 1; |
2217 | 0 | } |
2218 | 0 | err = write_keyblock_to_output (keyblock, opt.armor, opt.export_options); |
2219 | 0 | goto leave; |
2220 | 0 | } |
2221 | | |
2222 | 0 | if (opt.dry_run || (options & IMPORT_DRY_RUN)) |
2223 | 0 | goto leave; |
2224 | | |
2225 | | /* Do we have this key already in one of our pubrings ? */ |
2226 | 0 | err = get_keyblock_byfpr_fast (ctrl, &keyblock_orig, &hd, |
2227 | 0 | 1 /*primary only */, |
2228 | 0 | fpr2, fpr2len, 1/*locked*/); |
2229 | 0 | if ((err |
2230 | 0 | && gpg_err_code (err) != GPG_ERR_NO_PUBKEY |
2231 | 0 | && gpg_err_code (err) != GPG_ERR_UNUSABLE_PUBKEY) |
2232 | 0 | || !hd) |
2233 | 0 | { |
2234 | | /* The !hd above is to catch a misbehaving function which |
2235 | | * returns NO_PUBKEY for failing to allocate a handle. */ |
2236 | 0 | if (!silent) |
2237 | 0 | log_error (_("key %s: public key not found: %s\n"), |
2238 | 0 | keystr(keyid), gpg_strerror (err)); |
2239 | 0 | } |
2240 | 0 | else if (err && ((opt.import_options|options)&IMPORT_MERGE_ONLY) ) |
2241 | 0 | { |
2242 | 0 | if (opt.verbose && !silent ) |
2243 | 0 | log_info( _("key %s: new key - skipped\n"), keystr(keyid)); |
2244 | 0 | err = 0; |
2245 | 0 | stats->skipped_new_keys++; |
2246 | 0 | } |
2247 | 0 | else if (err) /* Insert this key. */ |
2248 | 0 | { |
2249 | | /* Note: ERR can only be NO_PUBKEY or UNUSABLE_PUBKEY. */ |
2250 | 0 | int n_sigs_cleaned, n_uids_cleaned; |
2251 | |
|
2252 | 0 | err = keydb_locate_writable (hd); |
2253 | 0 | if (err) |
2254 | 0 | { |
2255 | 0 | log_error (_("no writable keyring found: %s\n"), gpg_strerror (err)); |
2256 | 0 | err = gpg_error (GPG_ERR_GENERAL); |
2257 | 0 | goto leave; |
2258 | 0 | } |
2259 | 0 | if (opt.verbose > 1 ) |
2260 | 0 | log_info (_("writing to '%s'\n"), keydb_get_resource_name (hd) ); |
2261 | |
|
2262 | 0 | if ((options & IMPORT_CLEAN)) |
2263 | 0 | { |
2264 | 0 | merge_keys_and_selfsig (ctrl, keyblock); |
2265 | 0 | clean_all_uids (ctrl, keyblock, opt.verbose, |
2266 | 0 | (options&IMPORT_MINIMAL)? EXPORT_MINIMAL : 0, |
2267 | 0 | &n_uids_cleaned,&n_sigs_cleaned); |
2268 | 0 | clean_all_subkeys (ctrl, keyblock, opt.verbose, KEY_CLEAN_NONE, |
2269 | 0 | NULL, NULL); |
2270 | 0 | } |
2271 | | |
2272 | | /* Unless we are in restore mode apply meta data to the |
2273 | | * keyblock. Note that this will never change the first packet |
2274 | | * and thus the address of KEYBLOCK won't change. */ |
2275 | 0 | if ( !(options & IMPORT_RESTORE) ) |
2276 | 0 | { |
2277 | 0 | err = insert_key_origin (keyblock, origin, url); |
2278 | 0 | if (err) |
2279 | 0 | { |
2280 | 0 | log_error ("insert_key_origin failed: %s\n", gpg_strerror (err)); |
2281 | 0 | err = gpg_error (GPG_ERR_GENERAL); |
2282 | 0 | goto leave; |
2283 | 0 | } |
2284 | 0 | } |
2285 | | |
2286 | 0 | err = keydb_insert_keyblock (hd, keyblock); |
2287 | 0 | if (err) |
2288 | 0 | log_error (_("error writing keyring '%s': %s\n"), |
2289 | 0 | keydb_get_resource_name (hd), gpg_strerror (err)); |
2290 | 0 | else if (!(opt.import_options & IMPORT_KEEP_OWNERTTRUST)) |
2291 | 0 | { |
2292 | | /* This should not be possible since we delete the |
2293 | | ownertrust when a key is deleted, but it can happen if |
2294 | | the keyring and trustdb are out of sync. It can also |
2295 | | be made to happen with the trusted-key command and by |
2296 | | importing and locally exported key. */ |
2297 | |
|
2298 | 0 | clear_ownertrusts (ctrl, pk); |
2299 | 0 | if (non_self_or_utk) |
2300 | 0 | revalidation_mark (ctrl); |
2301 | 0 | } |
2302 | | |
2303 | | /* Release the handle and thus unlock the keyring asap. */ |
2304 | 0 | keydb_release (hd); |
2305 | 0 | hd = NULL; |
2306 | | |
2307 | | /* We are ready. */ |
2308 | 0 | if (!err && !opt.quiet && !silent) |
2309 | 0 | { |
2310 | 0 | char *p = get_user_id_byfpr_native (ctrl, fpr2, fpr2len); |
2311 | 0 | log_info (_("key %s: public key \"%s\" imported\n"), |
2312 | 0 | keystr(keyid), p); |
2313 | 0 | xfree(p); |
2314 | 0 | } |
2315 | 0 | if (!err && is_status_enabled()) |
2316 | 0 | { |
2317 | 0 | char *us = get_long_user_id_string (ctrl, keyid); |
2318 | 0 | write_status_text( STATUS_IMPORTED, us ); |
2319 | 0 | xfree(us); |
2320 | 0 | print_import_ok (pk, 1); |
2321 | 0 | } |
2322 | 0 | if (!err) |
2323 | 0 | { |
2324 | 0 | stats->imported++; |
2325 | 0 | new_key = 1; |
2326 | 0 | } |
2327 | 0 | } |
2328 | 0 | else /* Key already exists - merge. */ |
2329 | 0 | { |
2330 | 0 | int n_uids, n_sigs, n_subk, n_sigs_cleaned, n_uids_cleaned; |
2331 | 0 | u32 curtime = make_timestamp (); |
2332 | | |
2333 | | /* Compare the original against the new key; just to be sure nothing |
2334 | | * weird is going on */ |
2335 | 0 | if (cmp_public_keys (keyblock_orig->pkt->pkt.public_key, pk)) |
2336 | 0 | { |
2337 | 0 | if (!silent) |
2338 | 0 | log_error( _("key %s: doesn't match our copy\n"),keystr(keyid)); |
2339 | 0 | goto leave; |
2340 | 0 | } |
2341 | | |
2342 | | /* Make sure the original direct key sigs are all sane. */ |
2343 | 0 | n_sigs_cleaned = fix_bad_direct_key_sigs (ctrl, keyblock_orig, keyid); |
2344 | 0 | if (n_sigs_cleaned) |
2345 | 0 | commit_kbnode (&keyblock_orig); |
2346 | | |
2347 | | /* Try to merge KEYBLOCK into KEYBLOCK_ORIG. */ |
2348 | 0 | clear_kbnode_flags( keyblock_orig ); |
2349 | 0 | clear_kbnode_flags( keyblock ); |
2350 | 0 | n_uids = n_sigs = n_subk = n_uids_cleaned = 0; |
2351 | 0 | err = merge_blocks (ctrl, options, keyblock_orig, keyblock, keyid, |
2352 | 0 | curtime, origin, url, |
2353 | 0 | &n_uids, &n_sigs, &n_subk ); |
2354 | 0 | if (err) |
2355 | 0 | goto leave; |
2356 | | |
2357 | | /* Clean the final keyblock again if requested. we can't do |
2358 | | * this if only self-signatures are imported; see bug #4628. */ |
2359 | 0 | if ((options & IMPORT_CLEAN) |
2360 | 0 | && !(options & IMPORT_SELF_SIGS_ONLY)) |
2361 | 0 | { |
2362 | 0 | merge_keys_and_selfsig (ctrl, keyblock_orig); |
2363 | 0 | clean_all_uids (ctrl, keyblock_orig, opt.verbose, |
2364 | 0 | (options&IMPORT_MINIMAL)? EXPORT_MINIMAL : 0, |
2365 | 0 | &n_uids_cleaned,&n_sigs_cleaned); |
2366 | 0 | clean_all_subkeys (ctrl, keyblock_orig, opt.verbose, KEY_CLEAN_NONE, |
2367 | 0 | NULL, NULL); |
2368 | 0 | } |
2369 | |
|
2370 | 0 | if (n_uids || n_sigs || n_subk || n_sigs_cleaned || n_uids_cleaned |
2371 | 0 | || (options & IMPORT_FORCE_UPDATE)) |
2372 | 0 | { |
2373 | | /* Unless we are in restore mode apply meta data to the |
2374 | | * keyblock. Note that this will never change the first packet |
2375 | | * and thus the address of KEYBLOCK won't change. */ |
2376 | 0 | if ( !(options & IMPORT_RESTORE) ) |
2377 | 0 | { |
2378 | 0 | err = update_key_origin (keyblock_orig, curtime, origin, url); |
2379 | 0 | if (err) |
2380 | 0 | { |
2381 | 0 | log_error ("update_key_origin failed: %s\n", |
2382 | 0 | gpg_strerror (err)); |
2383 | 0 | goto leave; |
2384 | 0 | } |
2385 | 0 | } |
2386 | | |
2387 | 0 | mod_key = 1; |
2388 | | /* KEYBLOCK_ORIG has been updated; write */ |
2389 | 0 | err = keydb_update_keyblock (ctrl, hd, keyblock_orig); |
2390 | 0 | if (err) |
2391 | 0 | log_error (_("error writing keyring '%s': %s\n"), |
2392 | 0 | keydb_get_resource_name (hd), gpg_strerror (err)); |
2393 | 0 | else if (non_self_or_utk) |
2394 | 0 | revalidation_mark (ctrl); |
2395 | | |
2396 | | /* Release the handle and thus unlock the keyring asap. */ |
2397 | 0 | keydb_release (hd); |
2398 | 0 | hd = NULL; |
2399 | | |
2400 | | /* We are ready. Print and update stats if we got no error. |
2401 | | * An error here comes from writing the keyblock and thus |
2402 | | * very likely means that no update happened. */ |
2403 | 0 | if (!err && !opt.quiet && !silent) |
2404 | 0 | { |
2405 | 0 | char *p = get_user_id_byfpr_native (ctrl, fpr2, fpr2len); |
2406 | 0 | if (n_uids == 1 ) |
2407 | 0 | log_info( _("key %s: \"%s\" 1 new user ID\n"), |
2408 | 0 | keystr(keyid),p); |
2409 | 0 | else if (n_uids ) |
2410 | 0 | log_info( _("key %s: \"%s\" %d new user IDs\n"), |
2411 | 0 | keystr(keyid),p,n_uids); |
2412 | 0 | if (n_sigs == 1 ) |
2413 | 0 | log_info( _("key %s: \"%s\" 1 new signature\n"), |
2414 | 0 | keystr(keyid), p); |
2415 | 0 | else if (n_sigs ) |
2416 | 0 | log_info( _("key %s: \"%s\" %d new signatures\n"), |
2417 | 0 | keystr(keyid), p, n_sigs ); |
2418 | 0 | if (n_subk == 1 ) |
2419 | 0 | log_info( _("key %s: \"%s\" 1 new subkey\n"), |
2420 | 0 | keystr(keyid), p); |
2421 | 0 | else if (n_subk ) |
2422 | 0 | log_info( _("key %s: \"%s\" %d new subkeys\n"), |
2423 | 0 | keystr(keyid), p, n_subk ); |
2424 | 0 | if (n_sigs_cleaned==1) |
2425 | 0 | log_info(_("key %s: \"%s\" %d signature cleaned\n"), |
2426 | 0 | keystr(keyid),p,n_sigs_cleaned); |
2427 | 0 | else if (n_sigs_cleaned) |
2428 | 0 | log_info(_("key %s: \"%s\" %d signatures cleaned\n"), |
2429 | 0 | keystr(keyid),p,n_sigs_cleaned); |
2430 | 0 | if (n_uids_cleaned==1) |
2431 | 0 | log_info(_("key %s: \"%s\" %d user ID cleaned\n"), |
2432 | 0 | keystr(keyid),p,n_uids_cleaned); |
2433 | 0 | else if (n_uids_cleaned) |
2434 | 0 | log_info(_("key %s: \"%s\" %d user IDs cleaned\n"), |
2435 | 0 | keystr(keyid),p,n_uids_cleaned); |
2436 | 0 | xfree(p); |
2437 | 0 | } |
2438 | |
|
2439 | 0 | if (!err) |
2440 | 0 | { |
2441 | 0 | stats->n_uids +=n_uids; |
2442 | 0 | stats->n_sigs +=n_sigs; |
2443 | 0 | stats->n_subk +=n_subk; |
2444 | 0 | stats->n_sigs_cleaned +=n_sigs_cleaned; |
2445 | 0 | stats->n_uids_cleaned +=n_uids_cleaned; |
2446 | |
|
2447 | 0 | if (is_status_enabled () && !silent) |
2448 | 0 | print_import_ok (pk, ((n_uids?2:0)|(n_sigs?4:0)|(n_subk?8:0))); |
2449 | 0 | } |
2450 | 0 | } |
2451 | 0 | else |
2452 | 0 | { |
2453 | | /* Release the handle and thus unlock the keyring asap. */ |
2454 | 0 | keydb_release (hd); |
2455 | 0 | hd = NULL; |
2456 | | |
2457 | | /* FIXME: We do not track the time we last checked a key for |
2458 | | * updates. To do this we would need to rewrite even the |
2459 | | * keys which have no changes. Adding this would be useful |
2460 | | * for the automatic update of expired keys via the WKD in |
2461 | | * case the WKD still carries the expired key. See |
2462 | | * get_best_pubkey_byname. */ |
2463 | 0 | same_key = 1; |
2464 | 0 | if (is_status_enabled ()) |
2465 | 0 | print_import_ok (pk, 0); |
2466 | |
|
2467 | 0 | if (!opt.quiet && !silent) |
2468 | 0 | { |
2469 | 0 | char *p = get_user_id_byfpr_native (ctrl, fpr2, fpr2len); |
2470 | 0 | log_info( _("key %s: \"%s\" not changed\n"),keystr(keyid),p); |
2471 | 0 | xfree(p); |
2472 | 0 | } |
2473 | |
|
2474 | 0 | stats->unchanged++; |
2475 | 0 | } |
2476 | 0 | } |
2477 | | |
2478 | 0 | leave: |
2479 | 0 | keydb_release (hd); |
2480 | 0 | if (mod_key || new_key || same_key) |
2481 | 0 | { |
2482 | | /* A little explanation for this: we fill in the fingerprint |
2483 | | when importing keys as it can be useful to know the |
2484 | | fingerprint in certain keyserver-related cases (a keyserver |
2485 | | asked for a particular name, but the key doesn't have that |
2486 | | name). However, in cases where we're importing more than |
2487 | | one key at a time, we cannot know which key to fingerprint. |
2488 | | In these cases, rather than guessing, we do not |
2489 | | fingerprinting at all, and we must hope the user ID on the |
2490 | | keys are useful. Note that we need to do this for new |
2491 | | keys, merged keys and even for unchanged keys. This is |
2492 | | required because for example the --auto-key-locate feature |
2493 | | may import an already imported key and needs to know the |
2494 | | fingerprint of the key in all cases. */ |
2495 | 0 | if (fpr) |
2496 | 0 | { |
2497 | | /* Note that we need to compare against 0 here because |
2498 | | COUNT gets only incremented after returning from this |
2499 | | function. */ |
2500 | 0 | if (!stats->count) |
2501 | 0 | { |
2502 | 0 | xfree (*fpr); |
2503 | 0 | *fpr = fingerprint_from_pk (pk, NULL, fpr_len); |
2504 | 0 | } |
2505 | 0 | else if (origin != KEYORG_WKD) |
2506 | 0 | { |
2507 | 0 | xfree (*fpr); |
2508 | 0 | *fpr = NULL; |
2509 | 0 | } |
2510 | 0 | } |
2511 | 0 | } |
2512 | | |
2513 | | /* Now that the key is definitely incorporated into the keydb, we |
2514 | | need to check if a designated revocation is present or if the |
2515 | | prefs are not rational so we can warn the user. */ |
2516 | |
|
2517 | 0 | if (mod_key) |
2518 | 0 | { |
2519 | 0 | revocation_present (ctrl, keyblock_orig); |
2520 | 0 | if (!from_sk && have_secret_key_with_kid (ctrl, keyid)) |
2521 | 0 | check_prefs (ctrl, keyblock_orig); |
2522 | 0 | } |
2523 | 0 | else if (new_key) |
2524 | 0 | { |
2525 | 0 | revocation_present (ctrl, keyblock); |
2526 | 0 | if (!from_sk && have_secret_key_with_kid (ctrl, keyid)) |
2527 | 0 | check_prefs (ctrl, keyblock); |
2528 | 0 | } |
2529 | |
|
2530 | 0 | release_kbnode( keyblock_orig ); |
2531 | |
|
2532 | 0 | return err; |
2533 | 0 | } |
2534 | | |
2535 | | |
2536 | | /* Wrapper around import_one_real to retry the import in some cases. */ |
2537 | | static gpg_error_t |
2538 | | import_one (ctrl_t ctrl, |
2539 | | kbnode_t keyblock, struct import_stats_s *stats, |
2540 | | unsigned char **fpr, size_t *fpr_len, unsigned int options, |
2541 | | int from_sk, int silent, |
2542 | | import_screener_t screener, void *screener_arg, |
2543 | | int origin, const char *url, int *r_valid) |
2544 | 0 | { |
2545 | 0 | gpg_error_t err; |
2546 | 0 | kbnode_t otherrevsigs = NULL; |
2547 | 0 | kbnode_t node; |
2548 | |
|
2549 | 0 | err = import_one_real (ctrl, keyblock, stats, fpr, fpr_len, options, |
2550 | 0 | from_sk, silent, screener, screener_arg, |
2551 | 0 | origin, url, r_valid, &otherrevsigs); |
2552 | 0 | if (gpg_err_code (err) == GPG_ERR_TOO_LARGE |
2553 | 0 | && gpg_err_source (err) == GPG_ERR_SOURCE_KEYBOX |
2554 | 0 | && ((options & (IMPORT_SELF_SIGS_ONLY | IMPORT_CLEAN)) |
2555 | 0 | != (IMPORT_SELF_SIGS_ONLY | IMPORT_CLEAN))) |
2556 | 0 | { |
2557 | | /* We hit the maximum image length. Ask the wrapper to do |
2558 | | * everything again but this time with some extra options. */ |
2559 | 0 | u32 keyid[2]; |
2560 | |
|
2561 | 0 | keyid_from_pk (keyblock->pkt->pkt.public_key, keyid); |
2562 | 0 | log_info ("key %s: keyblock too large, retrying with self-sigs-only\n", |
2563 | 0 | keystr (keyid)); |
2564 | 0 | options |= IMPORT_SELF_SIGS_ONLY | IMPORT_CLEAN; |
2565 | 0 | err = import_one_real (ctrl, keyblock, stats, fpr, fpr_len, options, |
2566 | 0 | from_sk, silent, screener, screener_arg, |
2567 | 0 | origin, url, r_valid, &otherrevsigs); |
2568 | 0 | } |
2569 | | |
2570 | | /* Finally try to import other revocation certificates. For example |
2571 | | * those of a former key appended to the current key. */ |
2572 | 0 | if (!err) |
2573 | 0 | { |
2574 | 0 | for (node = otherrevsigs; node; node = node->next) |
2575 | 0 | import_revoke_cert (ctrl, node, options, stats); |
2576 | 0 | } |
2577 | 0 | release_kbnode (otherrevsigs); |
2578 | 0 | return err; |
2579 | 0 | } |
2580 | | |
2581 | | |
2582 | | |
2583 | | /* Convert our internal secret key object into an S-expression. PK is |
2584 | | * the public key. R_CURVE received an sexp with the name of the |
2585 | | * curve; caller must free this. R_SKEY will receive the result; |
2586 | | * caller must of course also free this. */ |
2587 | | static gpg_error_t |
2588 | | internal_skey_object_to_sexp (PKT_public_key *pk, gcry_sexp_t *r_curve, |
2589 | | gcry_sexp_t *r_skey) |
2590 | 0 | { |
2591 | 0 | gpg_error_t err; |
2592 | 0 | int nskey; |
2593 | 0 | int i, j; |
2594 | 0 | membuf_t mbuf; |
2595 | 0 | const char *curvename; |
2596 | 0 | char *curvestr = NULL; |
2597 | 0 | void *format_args[2*PUBKEY_MAX_NSKEY]; |
2598 | |
|
2599 | 0 | *r_curve = NULL; |
2600 | 0 | *r_skey = NULL; |
2601 | 0 | init_membuf (&mbuf, 50); |
2602 | |
|
2603 | 0 | nskey = pubkey_get_nskey (pk->pubkey_algo); |
2604 | 0 | if (!nskey || nskey > PUBKEY_MAX_NSKEY) |
2605 | 0 | { |
2606 | 0 | err = gpg_error (GPG_ERR_BAD_SECKEY); |
2607 | 0 | log_error ("internal error: %s\n", gpg_strerror (err)); |
2608 | 0 | goto leave; |
2609 | 0 | } |
2610 | | |
2611 | 0 | put_membuf_str (&mbuf, "(skey"); |
2612 | |
|
2613 | 0 | if (pk->pubkey_algo == PUBKEY_ALGO_ECDSA |
2614 | 0 | || pk->pubkey_algo == PUBKEY_ALGO_EDDSA |
2615 | 0 | || pk->pubkey_algo == PUBKEY_ALGO_ECDH) |
2616 | 0 | { |
2617 | | /* The ECC case. */ |
2618 | 0 | curvestr = openpgp_oid_to_str (pk->pkey[0]); |
2619 | 0 | if (!curvestr) |
2620 | 0 | { |
2621 | 0 | err = gpg_error_from_syserror (); |
2622 | 0 | goto leave; |
2623 | 0 | } |
2624 | | |
2625 | 0 | curvename = openpgp_oid_to_curve (curvestr, 1); |
2626 | 0 | gcry_sexp_release (*r_curve); |
2627 | 0 | err = gcry_sexp_build (r_curve, NULL, "(curve %s)", |
2628 | 0 | curvename?curvename:curvestr); |
2629 | 0 | if (err) |
2630 | 0 | goto leave; |
2631 | | |
2632 | 0 | j = 0; |
2633 | | /* Append the public key element Q. */ |
2634 | 0 | put_membuf_str (&mbuf, " _ %m"); |
2635 | 0 | format_args[j++] = pk->pkey + 1; |
2636 | | |
2637 | | /* Append the secret key element D. For ECDH we skip PKEY[2] |
2638 | | * because this holds the KEK which is not needed by gpg-agent. */ |
2639 | 0 | i = pk->pubkey_algo == PUBKEY_ALGO_ECDH? 3 : 2; |
2640 | 0 | if (gcry_mpi_get_flag (pk->pkey[i], GCRYMPI_FLAG_USER1)) |
2641 | 0 | put_membuf_str (&mbuf, " e %m"); |
2642 | 0 | else |
2643 | 0 | put_membuf_str (&mbuf, " _ %m"); |
2644 | 0 | format_args[j++] = pk->pkey + i; |
2645 | | |
2646 | | /* Simple hack to print a warning for an invalid key in case of |
2647 | | * cv25519. We have only opaque MPIs here. */ |
2648 | 0 | if (pk->pubkey_algo == PUBKEY_ALGO_ECDH |
2649 | 0 | && !strcmp (curvestr, "1.3.6.1.4.1.3029.1.5.1") |
2650 | 0 | && !gcry_mpi_get_flag (pk->pkey[i], GCRYMPI_FLAG_USER1) |
2651 | 0 | && gcry_mpi_get_flag (pk->pkey[i], GCRYMPI_FLAG_OPAQUE)) |
2652 | 0 | { |
2653 | 0 | const unsigned char *pp; |
2654 | 0 | unsigned int nn; |
2655 | |
|
2656 | 0 | pp = gcry_mpi_get_opaque (pk->pkey[i], &nn); |
2657 | 0 | nn = (nn+7)/8; |
2658 | 0 | if (pp && nn && (pp[nn-1] & 7)) |
2659 | 0 | log_info ("warning: lower 3 bits of the secret key" |
2660 | 0 | " are not cleared\n"); |
2661 | 0 | } |
2662 | 0 | } |
2663 | 0 | else /* Standard case for the old (non-ECC) algorithms. */ |
2664 | 0 | { |
2665 | 0 | for (i=j=0; i < nskey; i++) |
2666 | 0 | { |
2667 | 0 | if (!pk->pkey[i]) |
2668 | 0 | continue; /* Protected keys only have NPKEY+1 elements. */ |
2669 | | |
2670 | 0 | if (gcry_mpi_get_flag (pk->pkey[i], GCRYMPI_FLAG_USER1)) |
2671 | 0 | put_membuf_str (&mbuf, " e %m"); |
2672 | 0 | else |
2673 | 0 | put_membuf_str (&mbuf, " _ %m"); |
2674 | 0 | format_args[j++] = pk->pkey + i; |
2675 | 0 | } |
2676 | 0 | } |
2677 | 0 | put_membuf_str (&mbuf, ")"); |
2678 | 0 | put_membuf (&mbuf, "", 1); |
2679 | | |
2680 | | /* Finally convert to an sexp and store that at R_SKEY. */ |
2681 | 0 | { |
2682 | 0 | char *format = get_membuf (&mbuf, NULL); |
2683 | 0 | if (!format) |
2684 | 0 | err = gpg_error_from_syserror (); |
2685 | 0 | else |
2686 | 0 | err = gcry_sexp_build_array (r_skey, NULL, format, format_args); |
2687 | 0 | xfree (format); |
2688 | 0 | } |
2689 | |
|
2690 | 0 | leave: |
2691 | 0 | xfree (curvestr); |
2692 | 0 | xfree (get_membuf (&mbuf, NULL)); |
2693 | 0 | return err; |
2694 | 0 | } |
2695 | | |
2696 | | |
2697 | | static gpg_error_t |
2698 | | build_classic_transfer_sexp (PKT_public_key *pk, gcry_sexp_t *result) |
2699 | 0 | { |
2700 | 0 | gpg_error_t err; |
2701 | 0 | gcry_sexp_t skey; |
2702 | 0 | gcry_sexp_t prot = NULL; |
2703 | 0 | gcry_sexp_t curve = NULL; |
2704 | 0 | struct seckey_info *ski = pk->seckey_info; |
2705 | |
|
2706 | 0 | *result = NULL; |
2707 | | |
2708 | | /* Convert our internal secret key object into an S-expression. */ |
2709 | 0 | err = internal_skey_object_to_sexp (pk, &curve, &skey); |
2710 | 0 | if (err) |
2711 | 0 | { |
2712 | 0 | log_error ("error building skey array: %s\n", gpg_strerror (err)); |
2713 | 0 | goto leave; |
2714 | 0 | } |
2715 | 0 | log_assert (skey); |
2716 | | |
2717 | 0 | if (ski->is_protected) |
2718 | 0 | { |
2719 | 0 | char countbuf[35]; |
2720 | | |
2721 | | /* FIXME: Support AEAD */ |
2722 | | /* Note that the IVLEN may be zero if we are working on a dummy |
2723 | | * key. We can't express that in an S-expression and thus we |
2724 | | * send dummy data for the IV. */ |
2725 | 0 | snprintf (countbuf, sizeof countbuf, "%lu",(unsigned long)ski->s2k.count); |
2726 | 0 | err = gcry_sexp_build (&prot, NULL, |
2727 | 0 | " (protection %s %s %b %d %s %b %s)\n", |
2728 | 0 | ski->sha1chk? "sha1":"sum", |
2729 | 0 | openpgp_cipher_algo_name (ski->algo), |
2730 | 0 | ski->ivlen? (int)ski->ivlen:1, |
2731 | 0 | ski->ivlen? ski->iv: (const unsigned char*)"X", |
2732 | 0 | ski->s2k.mode, |
2733 | 0 | openpgp_md_algo_name (ski->s2k.hash_algo), |
2734 | 0 | (int)sizeof (ski->s2k.salt), ski->s2k.salt, |
2735 | 0 | countbuf); |
2736 | 0 | } |
2737 | 0 | else |
2738 | 0 | err = gcry_sexp_build (&prot, NULL, " (protection none)\n"); |
2739 | 0 | if (err) |
2740 | 0 | goto leave; |
2741 | | |
2742 | 0 | err = gcry_sexp_build (result, NULL, |
2743 | 0 | "(openpgp-private-key\n" |
2744 | 0 | " (version %d)\n" |
2745 | 0 | " (algo %s)\n" |
2746 | 0 | " %S%S\n" |
2747 | 0 | " (csum %d)\n" |
2748 | 0 | " %S)\n", |
2749 | 0 | pk->version, |
2750 | 0 | openpgp_pk_algo_name (pk->pubkey_algo), |
2751 | 0 | curve, skey, |
2752 | 0 | (int)(unsigned long)ski->csum, prot); |
2753 | |
|
2754 | 0 | leave: |
2755 | 0 | gcry_sexp_release (prot); |
2756 | 0 | gcry_sexp_release (skey); |
2757 | 0 | gcry_sexp_release (curve); |
2758 | 0 | return err; |
2759 | 0 | } |
2760 | | |
2761 | | |
2762 | | /* Transfer all the secret keys in SEC_KEYBLOCK to the gpg-agent. The |
2763 | | * function prints diagnostics and returns an error code. If BATCH is |
2764 | | * true the secret keys are stored by gpg-agent in the transfer format |
2765 | | * (i.e. no re-protection and aksing for passphrases). If ONLY_MARKED |
2766 | | * is set, only those nodes with flag NODE_TRANSFER_SECKEY are |
2767 | | * processed. */ |
2768 | | gpg_error_t |
2769 | | transfer_secret_keys (ctrl_t ctrl, struct import_stats_s *stats, |
2770 | | kbnode_t sec_keyblock, int batch, int force, |
2771 | | int only_marked) |
2772 | 0 | { |
2773 | 0 | gpg_error_t err = 0; |
2774 | 0 | void *kek = NULL; |
2775 | 0 | size_t keklen; |
2776 | 0 | kbnode_t ctx = NULL; |
2777 | 0 | kbnode_t node; |
2778 | 0 | PKT_public_key *main_pk, *pk; |
2779 | 0 | struct seckey_info *ski; |
2780 | 0 | gcry_sexp_t tmpsexp; |
2781 | 0 | unsigned char *transferkey = NULL; |
2782 | 0 | size_t transferkeylen; |
2783 | 0 | gcry_cipher_hd_t cipherhd = NULL; |
2784 | 0 | unsigned char *wrappedkey = NULL; |
2785 | 0 | size_t wrappedkeylen; |
2786 | 0 | char *cache_nonce = NULL; |
2787 | 0 | int stub_key_skipped = 0; |
2788 | | |
2789 | | /* Get the current KEK. */ |
2790 | 0 | err = agent_keywrap_key (ctrl, 0, &kek, &keklen); |
2791 | 0 | if (err) |
2792 | 0 | { |
2793 | 0 | log_error ("error getting the KEK: %s\n", gpg_strerror (err)); |
2794 | 0 | goto leave; |
2795 | 0 | } |
2796 | | |
2797 | | /* Prepare a cipher context. */ |
2798 | 0 | err = gcry_cipher_open (&cipherhd, GCRY_CIPHER_AES128, |
2799 | 0 | GCRY_CIPHER_MODE_AESWRAP, 0); |
2800 | 0 | if (!err) |
2801 | 0 | err = gcry_cipher_setkey (cipherhd, kek, keklen); |
2802 | 0 | if (err) |
2803 | 0 | goto leave; |
2804 | 0 | xfree (kek); |
2805 | 0 | kek = NULL; |
2806 | | |
2807 | | /* Note: We need to use walk_kbnode so that we skip nodes which are |
2808 | | * marked as deleted. */ |
2809 | 0 | main_pk = NULL; |
2810 | 0 | while ((node = walk_kbnode (sec_keyblock, &ctx, 0))) |
2811 | 0 | { |
2812 | 0 | if (node->pkt->pkttype != PKT_SECRET_KEY |
2813 | 0 | && node->pkt->pkttype != PKT_SECRET_SUBKEY) |
2814 | 0 | continue; |
2815 | 0 | if (only_marked && !(node->flag & NODE_TRANSFER_SECKEY)) |
2816 | 0 | continue; |
2817 | 0 | pk = node->pkt->pkt.public_key; |
2818 | 0 | if (!main_pk) |
2819 | 0 | main_pk = pk; |
2820 | | |
2821 | | /* Make sure the keyids are available. */ |
2822 | 0 | keyid_from_pk (pk, NULL); |
2823 | 0 | if (node->pkt->pkttype == PKT_SECRET_KEY) |
2824 | 0 | { |
2825 | 0 | pk->main_keyid[0] = pk->keyid[0]; |
2826 | 0 | pk->main_keyid[1] = pk->keyid[1]; |
2827 | 0 | } |
2828 | 0 | else |
2829 | 0 | { |
2830 | 0 | pk->main_keyid[0] = main_pk->keyid[0]; |
2831 | 0 | pk->main_keyid[1] = main_pk->keyid[1]; |
2832 | 0 | } |
2833 | | |
2834 | |
|
2835 | 0 | ski = pk->seckey_info; |
2836 | 0 | if (!ski) |
2837 | 0 | BUG (); |
2838 | | |
2839 | 0 | if (stats) |
2840 | 0 | { |
2841 | 0 | stats->count++; |
2842 | 0 | stats->secret_read++; |
2843 | 0 | } |
2844 | | |
2845 | | /* We ignore stub keys. The way we handle them in other parts |
2846 | | of the code is by asking the agent whether any secret key is |
2847 | | available for a given keyblock and then concluding that we |
2848 | | have a secret key; all secret (sub)keys of the keyblock the |
2849 | | agent does not know of are then stub keys. This works also |
2850 | | for card stub keys. The learn command or the card-status |
2851 | | command may be used to check with the agent whether a card |
2852 | | has been inserted and a stub key is in turn generated by the |
2853 | | agent. */ |
2854 | 0 | if (ski->s2k.mode == 1001 || ski->s2k.mode == 1002) |
2855 | 0 | { |
2856 | 0 | stub_key_skipped = 1; |
2857 | 0 | continue; |
2858 | 0 | } |
2859 | | |
2860 | 0 | tmpsexp = NULL; |
2861 | 0 | if (ski->s2k.mode == 1003) |
2862 | 0 | { |
2863 | 0 | const void *tmpbuf; |
2864 | 0 | unsigned int tmpbuflen; |
2865 | 0 | int npkey; |
2866 | | |
2867 | | /* Fixme: Check that the public key parameters in pkey match |
2868 | | * those in the s-expression of the secret key. */ |
2869 | 0 | npkey = pubkey_get_npkey (pk->pubkey_algo); |
2870 | 0 | if (npkey+1 > PUBKEY_MAX_NSKEY) |
2871 | 0 | err = gpg_error (GPG_ERR_BAD_SECKEY); |
2872 | 0 | else if (!pk->pkey[npkey] |
2873 | 0 | || !gcry_mpi_get_flag (pk->pkey[npkey], GCRYMPI_FLAG_OPAQUE)) |
2874 | 0 | err = gpg_error (GPG_ERR_BAD_SECKEY); |
2875 | 0 | else |
2876 | 0 | { |
2877 | 0 | tmpbuf = gcry_mpi_get_opaque (pk->pkey[npkey], &tmpbuflen); |
2878 | 0 | tmpbuflen = (tmpbuflen +7)/8; /* Fixup bits to bytes */ |
2879 | 0 | err = gcry_sexp_new (&tmpsexp, tmpbuf, tmpbuflen, 0); |
2880 | 0 | } |
2881 | 0 | } |
2882 | 0 | else |
2883 | 0 | err = build_classic_transfer_sexp (pk, &tmpsexp); |
2884 | |
|
2885 | 0 | xfree (transferkey); |
2886 | 0 | transferkey = NULL; |
2887 | 0 | if (!err) |
2888 | 0 | err = make_canon_sexp_pad (tmpsexp, 1, &transferkey, &transferkeylen); |
2889 | 0 | gcry_sexp_release (tmpsexp); |
2890 | 0 | tmpsexp = NULL; |
2891 | 0 | if (err) |
2892 | 0 | { |
2893 | 0 | log_error ("error building transfer key: %s\n", gpg_strerror (err)); |
2894 | 0 | goto leave; |
2895 | 0 | } |
2896 | | |
2897 | | /* Wrap the key. */ |
2898 | 0 | wrappedkeylen = transferkeylen + 8; |
2899 | 0 | xfree (wrappedkey); |
2900 | 0 | wrappedkey = xtrymalloc (wrappedkeylen); |
2901 | 0 | if (!wrappedkey) |
2902 | 0 | err = gpg_error_from_syserror (); |
2903 | 0 | else |
2904 | 0 | err = gcry_cipher_encrypt (cipherhd, wrappedkey, wrappedkeylen, |
2905 | 0 | transferkey, transferkeylen); |
2906 | 0 | if (err) |
2907 | 0 | goto leave; |
2908 | 0 | xfree (transferkey); |
2909 | 0 | transferkey = NULL; |
2910 | | |
2911 | | /* Send the wrapped key to the agent. */ |
2912 | 0 | { |
2913 | 0 | char *desc = gpg_format_keydesc (ctrl, pk, FORMAT_KEYDESC_IMPORT, 1); |
2914 | 0 | err = agent_import_key (ctrl, desc, ski->s2k.mode == 1003, |
2915 | 0 | &cache_nonce, |
2916 | 0 | wrappedkey, wrappedkeylen, batch, force, |
2917 | 0 | pk->keyid, pk->main_keyid, pk->pubkey_algo, |
2918 | 0 | pk->timestamp); |
2919 | 0 | xfree (desc); |
2920 | 0 | } |
2921 | 0 | if (!err) |
2922 | 0 | { |
2923 | 0 | if (opt.verbose) |
2924 | 0 | log_info (_("key %s: secret key imported\n"), |
2925 | 0 | keystr_from_pk_with_sub (main_pk, pk)); |
2926 | 0 | if (stats) |
2927 | 0 | stats->secret_imported++; |
2928 | 0 | } |
2929 | 0 | else if ( gpg_err_code (err) == GPG_ERR_EEXIST ) |
2930 | 0 | { |
2931 | 0 | if (opt.verbose) |
2932 | 0 | log_info (_("key %s: secret key already exists\n"), |
2933 | 0 | keystr_from_pk_with_sub (main_pk, pk)); |
2934 | 0 | err = 0; |
2935 | 0 | if (stats) |
2936 | 0 | stats->secret_dups++; |
2937 | 0 | } |
2938 | 0 | else |
2939 | 0 | { |
2940 | 0 | log_error (_("key %s: error sending to agent: %s\n"), |
2941 | 0 | keystr_from_pk_with_sub (main_pk, pk), |
2942 | 0 | gpg_strerror (err)); |
2943 | 0 | if (gpg_err_code (err) == GPG_ERR_CANCELED |
2944 | 0 | || gpg_err_code (err) == GPG_ERR_FULLY_CANCELED) |
2945 | 0 | break; /* Don't try the other subkeys. */ |
2946 | 0 | } |
2947 | 0 | } |
2948 | | |
2949 | 0 | if (!err && stub_key_skipped) |
2950 | | /* We need to notify user how to migrate stub keys. */ |
2951 | 0 | err = gpg_error (GPG_ERR_NOT_PROCESSED); |
2952 | |
|
2953 | 0 | leave: |
2954 | 0 | xfree (cache_nonce); |
2955 | 0 | xfree (wrappedkey); |
2956 | 0 | xfree (transferkey); |
2957 | 0 | gcry_cipher_close (cipherhd); |
2958 | 0 | xfree (kek); |
2959 | 0 | return err; |
2960 | 0 | } |
2961 | | |
2962 | | |
2963 | | /* Walk a secret keyblock and produce a public keyblock out of it. |
2964 | | * Returns a new node or NULL on error. Modifies the tag field of the |
2965 | | * nodes. */ |
2966 | | static kbnode_t |
2967 | | sec_to_pub_keyblock (kbnode_t sec_keyblock) |
2968 | 0 | { |
2969 | 0 | kbnode_t pub_keyblock = NULL; |
2970 | 0 | kbnode_t ctx = NULL; |
2971 | 0 | kbnode_t secnode, pubnode; |
2972 | 0 | kbnode_t lastnode = NULL; |
2973 | 0 | unsigned int tag = 0; |
2974 | | |
2975 | | /* Set a tag to all nodes. */ |
2976 | 0 | for (secnode = sec_keyblock; secnode; secnode = secnode->next) |
2977 | 0 | secnode->tag = ++tag; |
2978 | | |
2979 | | /* Copy. */ |
2980 | 0 | while ((secnode = walk_kbnode (sec_keyblock, &ctx, 0))) |
2981 | 0 | { |
2982 | 0 | if (secnode->pkt->pkttype == PKT_SECRET_KEY |
2983 | 0 | || secnode->pkt->pkttype == PKT_SECRET_SUBKEY) |
2984 | 0 | { |
2985 | | /* Make a public key. */ |
2986 | 0 | PACKET *pkt; |
2987 | 0 | PKT_public_key *pk; |
2988 | |
|
2989 | 0 | pkt = xtrycalloc (1, sizeof *pkt); |
2990 | 0 | pk = pkt? copy_public_key (NULL, secnode->pkt->pkt.public_key): NULL; |
2991 | 0 | if (!pk) |
2992 | 0 | { |
2993 | 0 | xfree (pkt); |
2994 | 0 | release_kbnode (pub_keyblock); |
2995 | 0 | return NULL; |
2996 | 0 | } |
2997 | 0 | if (secnode->pkt->pkttype == PKT_SECRET_KEY) |
2998 | 0 | pkt->pkttype = PKT_PUBLIC_KEY; |
2999 | 0 | else |
3000 | 0 | pkt->pkttype = PKT_PUBLIC_SUBKEY; |
3001 | 0 | pkt->pkt.public_key = pk; |
3002 | |
|
3003 | 0 | pubnode = new_kbnode (pkt); |
3004 | 0 | } |
3005 | 0 | else |
3006 | 0 | { |
3007 | 0 | pubnode = clone_kbnode (secnode); |
3008 | 0 | } |
3009 | 0 | pubnode->tag = secnode->tag; |
3010 | |
|
3011 | 0 | if (!pub_keyblock) |
3012 | 0 | pub_keyblock = lastnode = pubnode; |
3013 | 0 | else |
3014 | 0 | { |
3015 | 0 | lastnode->next = pubnode; |
3016 | 0 | lastnode = pubnode; |
3017 | 0 | } |
3018 | 0 | } |
3019 | | |
3020 | 0 | return pub_keyblock; |
3021 | 0 | } |
3022 | | |
3023 | | |
3024 | | /* Delete all notes in the keyblock at R_KEYBLOCK which are not in |
3025 | | * PUB_KEYBLOCK. Modifies the tags of both keyblock's nodes. */ |
3026 | | static gpg_error_t |
3027 | | resync_sec_with_pub_keyblock (kbnode_t *r_keyblock, kbnode_t pub_keyblock, |
3028 | | kbnode_t *r_removedsecs) |
3029 | 0 | { |
3030 | 0 | kbnode_t sec_keyblock = *r_keyblock; |
3031 | 0 | kbnode_t node, prevnode; |
3032 | 0 | unsigned int *taglist; |
3033 | 0 | unsigned int ntaglist, n; |
3034 | 0 | kbnode_t attic = NULL; |
3035 | 0 | kbnode_t *attic_head = &attic; |
3036 | | |
3037 | | /* Collect all tags in an array for faster searching. */ |
3038 | 0 | for (ntaglist = 0, node = pub_keyblock; node; node = node->next) |
3039 | 0 | ntaglist++; |
3040 | 0 | taglist = xtrycalloc (ntaglist, sizeof *taglist); |
3041 | 0 | if (!taglist) |
3042 | 0 | return gpg_error_from_syserror (); |
3043 | 0 | for (ntaglist = 0, node = pub_keyblock; node; node = node->next) |
3044 | 0 | taglist[ntaglist++] = node->tag; |
3045 | | |
3046 | | /* Walks over the secret keyblock and delete all nodes which are not |
3047 | | * in the tag list. Those nodes have been deleted in the |
3048 | | * pub_keyblock. Sequential search is a bit lazy and could be |
3049 | | * optimized by sorting and bsearch; however secret keyrings are |
3050 | | * short and there are easier ways to DoS the import. */ |
3051 | 0 | again: |
3052 | 0 | for (prevnode=NULL, node=sec_keyblock; node; prevnode=node, node=node->next) |
3053 | 0 | { |
3054 | 0 | for (n=0; n < ntaglist; n++) |
3055 | 0 | if (taglist[n] == node->tag) |
3056 | 0 | break; |
3057 | 0 | if (n == ntaglist) /* Not in public keyblock. */ |
3058 | 0 | { |
3059 | 0 | if (node->pkt->pkttype == PKT_SECRET_KEY |
3060 | 0 | || node->pkt->pkttype == PKT_SECRET_SUBKEY) |
3061 | 0 | { |
3062 | 0 | if (!prevnode) |
3063 | 0 | sec_keyblock = node->next; |
3064 | 0 | else |
3065 | 0 | prevnode->next = node->next; |
3066 | 0 | node->next = NULL; |
3067 | 0 | *attic_head = node; |
3068 | 0 | attic_head = &node->next; |
3069 | 0 | goto again; /* That's lame; I know. */ |
3070 | 0 | } |
3071 | 0 | else |
3072 | 0 | delete_kbnode (node); |
3073 | 0 | } |
3074 | 0 | } |
3075 | | |
3076 | 0 | xfree (taglist); |
3077 | | |
3078 | | /* Commit the as deleted marked nodes and return the possibly |
3079 | | * modified keyblock and a list of removed secret key nodes. */ |
3080 | 0 | commit_kbnode (&sec_keyblock); |
3081 | 0 | *r_keyblock = sec_keyblock; |
3082 | 0 | *r_removedsecs = attic; |
3083 | 0 | return 0; |
3084 | 0 | } |
3085 | | |
3086 | | |
3087 | | /* Helper for import_secret_one. */ |
3088 | | static gpg_error_t |
3089 | | do_transfer (ctrl_t ctrl, kbnode_t keyblock, PKT_public_key *pk, |
3090 | | struct import_stats_s *stats, int batch, int only_marked) |
3091 | | |
3092 | 0 | { |
3093 | 0 | gpg_error_t err; |
3094 | 0 | struct import_stats_s subkey_stats = {0}; |
3095 | 0 | int force = 0; |
3096 | 0 | int already_exist = agent_probe_secret_key (ctrl, pk); |
3097 | |
|
3098 | 0 | if (already_exist == 2 || already_exist == 4) |
3099 | 0 | { |
3100 | 0 | if (!opt.quiet) |
3101 | 0 | log_info (_("key %s: card reference is overridden by key material\n"), |
3102 | 0 | keystr_from_pk (pk)); |
3103 | 0 | force = 1; |
3104 | 0 | } |
3105 | |
|
3106 | 0 | err = transfer_secret_keys (ctrl, &subkey_stats, keyblock, |
3107 | 0 | batch, force, only_marked); |
3108 | 0 | if (gpg_err_code (err) == GPG_ERR_NOT_PROCESSED) |
3109 | 0 | { |
3110 | | /* TRANSLATORS: For a smartcard, each private key on host has a |
3111 | | * reference (stub) to a smartcard and actual private key data |
3112 | | * is stored on the card. A single smartcard can have up to |
3113 | | * three private key data. Importing private key stub is always |
3114 | | * skipped in 2.1, and it returns GPG_ERR_NOT_PROCESSED. |
3115 | | * Instead, user should be suggested to run 'gpg --card-status', |
3116 | | * then, references to a card will be automatically created |
3117 | | * again. */ |
3118 | 0 | log_info (_("To migrate '%s', with each smartcard, " |
3119 | 0 | "run: %s\n"), "secring.gpg", "gpg --card-status"); |
3120 | 0 | err = 0; |
3121 | 0 | } |
3122 | |
|
3123 | 0 | if (!err) |
3124 | 0 | { |
3125 | 0 | int status = 16; |
3126 | |
|
3127 | 0 | if (!opt.quiet) |
3128 | 0 | log_info (_("key %s: secret key imported\n"), keystr_from_pk (pk)); |
3129 | 0 | if (subkey_stats.secret_imported) |
3130 | 0 | { |
3131 | 0 | status |= 1; |
3132 | 0 | stats->secret_imported += 1; |
3133 | 0 | } |
3134 | 0 | if (subkey_stats.secret_dups) |
3135 | 0 | stats->secret_dups += 1; |
3136 | |
|
3137 | 0 | if (is_status_enabled ()) |
3138 | 0 | print_import_ok (pk, status); |
3139 | 0 | } |
3140 | |
|
3141 | 0 | return err; |
3142 | 0 | } |
3143 | | |
3144 | | |
3145 | | /* If the secret keys (main or subkey) in SECKEYS have a corresponding |
3146 | | * public key in the public key described by (FPR,FPRLEN) import these |
3147 | | * parts. |
3148 | | */ |
3149 | | static gpg_error_t |
3150 | | import_matching_seckeys (ctrl_t ctrl, kbnode_t seckeys, |
3151 | | const byte *mainfpr, size_t mainfprlen, |
3152 | | struct import_stats_s *stats, int batch) |
3153 | 0 | { |
3154 | 0 | gpg_error_t err; |
3155 | 0 | kbnode_t pub_keyblock = NULL; |
3156 | 0 | kbnode_t node; |
3157 | 0 | struct { byte fpr[MAX_FINGERPRINT_LEN]; size_t fprlen; } *fprlist = NULL; |
3158 | 0 | size_t n, nfprlist; |
3159 | 0 | byte fpr[MAX_FINGERPRINT_LEN]; |
3160 | 0 | size_t fprlen; |
3161 | 0 | PKT_public_key *pk; |
3162 | | |
3163 | | /* Get the entire public key block from our keystore and put all its |
3164 | | * fingerprints into an array. */ |
3165 | 0 | err = get_pubkey_byfpr (ctrl, NULL, &pub_keyblock, mainfpr, mainfprlen); |
3166 | 0 | if (err) |
3167 | 0 | goto leave; |
3168 | 0 | log_assert (pub_keyblock && pub_keyblock->pkt->pkttype == PKT_PUBLIC_KEY); |
3169 | 0 | pk = pub_keyblock->pkt->pkt.public_key; |
3170 | |
|
3171 | 0 | for (nfprlist = 0, node = pub_keyblock; node; node = node->next) |
3172 | 0 | if (node->pkt->pkttype == PKT_PUBLIC_KEY |
3173 | 0 | || node->pkt->pkttype == PKT_PUBLIC_SUBKEY) |
3174 | 0 | nfprlist++; |
3175 | 0 | log_assert (nfprlist); |
3176 | 0 | fprlist = xtrycalloc (nfprlist, sizeof *fprlist); |
3177 | 0 | if (!fprlist) |
3178 | 0 | { |
3179 | 0 | err = gpg_error_from_syserror (); |
3180 | 0 | goto leave; |
3181 | 0 | } |
3182 | 0 | for (n = 0, node = pub_keyblock; node; node = node->next) |
3183 | 0 | if (node->pkt->pkttype == PKT_PUBLIC_KEY |
3184 | 0 | || node->pkt->pkttype == PKT_PUBLIC_SUBKEY) |
3185 | 0 | { |
3186 | 0 | fingerprint_from_pk (node->pkt->pkt.public_key, |
3187 | 0 | fprlist[n].fpr, &fprlist[n].fprlen); |
3188 | 0 | n++; |
3189 | 0 | } |
3190 | 0 | log_assert (n == nfprlist); |
3191 | | |
3192 | | /* for (n=0; n < nfprlist; n++) */ |
3193 | | /* log_printhex (fprlist[n].fpr, fprlist[n].fprlen, "pubkey %zu:", n); */ |
3194 | | |
3195 | | /* Mark all secret keys which have a matching public key part in |
3196 | | * PUB_KEYBLOCK. */ |
3197 | 0 | for (node = seckeys; node; node = node->next) |
3198 | 0 | { |
3199 | 0 | if (node->pkt->pkttype != PKT_SECRET_KEY |
3200 | 0 | && node->pkt->pkttype != PKT_SECRET_SUBKEY) |
3201 | 0 | continue; /* Should not happen. */ |
3202 | 0 | fingerprint_from_pk (node->pkt->pkt.public_key, fpr, &fprlen); |
3203 | 0 | node->flag &= ~NODE_TRANSFER_SECKEY; |
3204 | 0 | for (n=0; n < nfprlist; n++) |
3205 | 0 | if (fprlist[n].fprlen == fprlen && !memcmp (fprlist[n].fpr,fpr,fprlen)) |
3206 | 0 | { |
3207 | 0 | node->flag |= NODE_TRANSFER_SECKEY; |
3208 | | /* log_debug ("found matching seckey\n"); */ |
3209 | 0 | break; |
3210 | 0 | } |
3211 | 0 | } |
3212 | | |
3213 | | /* Transfer all marked keys. */ |
3214 | 0 | err = do_transfer (ctrl, seckeys, pk, stats, batch, 1); |
3215 | |
|
3216 | 0 | leave: |
3217 | 0 | xfree (fprlist); |
3218 | 0 | release_kbnode (pub_keyblock); |
3219 | 0 | return err; |
3220 | 0 | } |
3221 | | |
3222 | | |
3223 | | /* Import function for a single secret keyblock. Handling is simpler |
3224 | | * than for public keys. We allow secret key importing only when |
3225 | | * allow is true, this is so that a secret key can not be imported |
3226 | | * accidentally and thereby tampering with the trust calculation. |
3227 | | * |
3228 | | * Ownership of KEYBLOCK is transferred to this function! |
3229 | | * |
3230 | | * If R_SECATTIC is not null the last special sec_keyblock is stored |
3231 | | * there. |
3232 | | */ |
3233 | | static gpg_error_t |
3234 | | import_secret_one (ctrl_t ctrl, kbnode_t keyblock, |
3235 | | struct import_stats_s *stats, int batch, |
3236 | | unsigned int options, int for_migration, |
3237 | | import_screener_t screener, void *screener_arg, |
3238 | | kbnode_t *r_secattic) |
3239 | 0 | { |
3240 | 0 | PKT_public_key *pk; |
3241 | 0 | struct seckey_info *ski; |
3242 | 0 | kbnode_t node, uidnode; |
3243 | 0 | u32 keyid[2]; |
3244 | 0 | gpg_error_t err = 0; |
3245 | 0 | int nr_prev; |
3246 | 0 | kbnode_t pub_keyblock; |
3247 | 0 | kbnode_t attic = NULL; |
3248 | 0 | byte fpr[MAX_FINGERPRINT_LEN]; |
3249 | 0 | size_t fprlen; |
3250 | 0 | char pkstrbuf[PUBKEY_STRING_SIZE]; |
3251 | | |
3252 | | /* Get the key and print some info about it */ |
3253 | 0 | node = find_kbnode (keyblock, PKT_SECRET_KEY); |
3254 | 0 | if (!node) |
3255 | 0 | BUG (); |
3256 | | |
3257 | 0 | pk = node->pkt->pkt.public_key; |
3258 | |
|
3259 | 0 | fingerprint_from_pk (pk, fpr, &fprlen); |
3260 | 0 | keyid_from_pk (pk, keyid); |
3261 | 0 | uidnode = find_next_kbnode (keyblock, PKT_USER_ID); |
3262 | |
|
3263 | 0 | if (screener && screener (keyblock, screener_arg)) |
3264 | 0 | { |
3265 | 0 | log_error (_("secret key %s: %s\n"), keystr_from_pk (pk), |
3266 | 0 | _("rejected by import screener")); |
3267 | 0 | release_kbnode (keyblock); |
3268 | 0 | return 0; |
3269 | 0 | } |
3270 | | |
3271 | 0 | if (opt.verbose && !for_migration) |
3272 | 0 | { |
3273 | 0 | log_info ("sec %s/%s %s ", |
3274 | 0 | pubkey_string (pk, pkstrbuf, sizeof pkstrbuf), |
3275 | 0 | keystr_from_pk (pk), datestr_from_pk (pk)); |
3276 | 0 | if (uidnode) |
3277 | 0 | print_utf8_buffer (log_get_stream (), uidnode->pkt->pkt.user_id->name, |
3278 | 0 | uidnode->pkt->pkt.user_id->len); |
3279 | 0 | log_printf ("\n"); |
3280 | 0 | } |
3281 | 0 | stats->secret_read++; |
3282 | |
|
3283 | 0 | if ((options & IMPORT_ONLY_PUBKEYS)) |
3284 | 0 | { |
3285 | 0 | if (!for_migration) |
3286 | 0 | log_error (_("importing secret keys not allowed\n")); |
3287 | 0 | release_kbnode (keyblock); |
3288 | 0 | return 0; |
3289 | 0 | } |
3290 | | |
3291 | 0 | if (!uidnode) |
3292 | 0 | { |
3293 | 0 | if (!for_migration) |
3294 | 0 | log_error( _("key %s: no user ID\n"), keystr_from_pk (pk)); |
3295 | 0 | release_kbnode (keyblock); |
3296 | 0 | return 0; |
3297 | 0 | } |
3298 | | |
3299 | 0 | ski = pk->seckey_info; |
3300 | 0 | if (!ski) |
3301 | 0 | { |
3302 | | /* Actually an internal error. */ |
3303 | 0 | log_error ("key %s: secret key info missing\n", keystr_from_pk (pk)); |
3304 | 0 | release_kbnode (keyblock); |
3305 | 0 | return 0; |
3306 | 0 | } |
3307 | | |
3308 | | /* A quick check to not import keys with an invalid protection |
3309 | | cipher algorithm (only checks the primary key, though). */ |
3310 | 0 | if (ski->algo > 110) |
3311 | 0 | { |
3312 | 0 | if (!for_migration) |
3313 | 0 | log_error (_("key %s: secret key with invalid cipher %d" |
3314 | 0 | " - skipped\n"), keystr_from_pk (pk), ski->algo); |
3315 | 0 | release_kbnode (keyblock); |
3316 | 0 | return 0; |
3317 | 0 | } |
3318 | | |
3319 | | #ifdef ENABLE_SELINUX_HACKS |
3320 | | if (1) |
3321 | | { |
3322 | | /* We don't allow importing secret keys because that may be used |
3323 | | to put a secret key into the keyring and the user might later |
3324 | | be tricked into signing stuff with that key. */ |
3325 | | log_error (_("importing secret keys not allowed\n")); |
3326 | | release_kbnode (keyblock); |
3327 | | return 0; |
3328 | | } |
3329 | | #endif |
3330 | | |
3331 | 0 | clear_kbnode_flags (keyblock); |
3332 | |
|
3333 | 0 | nr_prev = stats->skipped_new_keys; |
3334 | | |
3335 | | /* Make a public key out of the key. */ |
3336 | 0 | pub_keyblock = sec_to_pub_keyblock (keyblock); |
3337 | 0 | if (!pub_keyblock) |
3338 | 0 | { |
3339 | 0 | err = gpg_error_from_syserror (); |
3340 | 0 | log_error ("key %s: failed to create public key from secret key\n", |
3341 | 0 | keystr_from_pk (pk)); |
3342 | 0 | } |
3343 | 0 | else |
3344 | 0 | { |
3345 | 0 | int valid; |
3346 | | |
3347 | | /* Note that this outputs an IMPORT_OK status message for the |
3348 | | public key block, and below we will output another one for |
3349 | | the secret keys. FIXME? */ |
3350 | 0 | import_one (ctrl, pub_keyblock, stats, |
3351 | 0 | NULL, NULL, options, 1, for_migration, |
3352 | 0 | screener, screener_arg, 0, NULL, &valid); |
3353 | | |
3354 | | /* The secret keyblock may not have nodes which are deleted in |
3355 | | * the public keyblock. Otherwise we would import just the |
3356 | | * secret key without having the public key. That would be |
3357 | | * surprising and clutters our private-keys-v1.d. */ |
3358 | 0 | err = resync_sec_with_pub_keyblock (&keyblock, pub_keyblock, &attic); |
3359 | 0 | if (err) |
3360 | 0 | goto leave; |
3361 | | |
3362 | 0 | if (!valid) |
3363 | 0 | { |
3364 | | /* If the block was not valid the primary key is left in the |
3365 | | * original keyblock because we require that for the first |
3366 | | * node. Move it to ATTIC. */ |
3367 | 0 | if (keyblock && keyblock->pkt->pkttype == PKT_SECRET_KEY) |
3368 | 0 | { |
3369 | 0 | node = keyblock; |
3370 | 0 | keyblock = node->next; |
3371 | 0 | node->next = NULL; |
3372 | 0 | if (attic) |
3373 | 0 | { |
3374 | 0 | node->next = attic; |
3375 | 0 | attic = node; |
3376 | 0 | } |
3377 | 0 | else |
3378 | 0 | attic = node; |
3379 | 0 | } |
3380 | | |
3381 | | /* Try to import the secret key iff we have a public key. */ |
3382 | 0 | if (attic && !(opt.dry_run || (options & IMPORT_DRY_RUN))) |
3383 | 0 | err = import_matching_seckeys (ctrl, attic, fpr, fprlen, |
3384 | 0 | stats, batch); |
3385 | 0 | else |
3386 | 0 | err = gpg_error (GPG_ERR_NO_SECKEY); |
3387 | 0 | goto leave; |
3388 | 0 | } |
3389 | | |
3390 | | /* log_debug ("attic is:\n"); */ |
3391 | | /* dump_kbnode (attic); */ |
3392 | | |
3393 | | /* Proceed with the valid parts of PUBKEYBLOCK. */ |
3394 | | |
3395 | | /* At least we cancel the secret key import when the public key |
3396 | | import was skipped due to MERGE_ONLY option and a new |
3397 | | key. */ |
3398 | 0 | if (!(opt.dry_run || (options & IMPORT_DRY_RUN)) |
3399 | 0 | && stats->skipped_new_keys <= nr_prev) |
3400 | 0 | { |
3401 | | /* Read the keyblock again to get the effects of a merge for |
3402 | | * the public key. */ |
3403 | 0 | err = get_pubkey_byfpr (ctrl, NULL, &node, fpr, fprlen); |
3404 | 0 | if (err || !node) |
3405 | 0 | log_error ("key %s: failed to re-lookup public key: %s\n", |
3406 | 0 | keystr_from_pk (pk), gpg_strerror (err)); |
3407 | 0 | else |
3408 | 0 | { |
3409 | 0 | err = do_transfer (ctrl, keyblock, pk, stats, batch, 0); |
3410 | 0 | if (!err) |
3411 | 0 | check_prefs (ctrl, node); |
3412 | 0 | release_kbnode (node); |
3413 | |
|
3414 | 0 | if (!err && attic) |
3415 | 0 | { |
3416 | | /* Try to import invalid subkeys. This can be the |
3417 | | * case if the primary secret key was imported due |
3418 | | * to --allow-non-selfsigned-uid. */ |
3419 | 0 | err = import_matching_seckeys (ctrl, attic, fpr, fprlen, |
3420 | 0 | stats, batch); |
3421 | 0 | } |
3422 | |
|
3423 | 0 | } |
3424 | 0 | } |
3425 | 0 | } |
3426 | | |
3427 | 0 | leave: |
3428 | 0 | release_kbnode (keyblock); |
3429 | 0 | release_kbnode (pub_keyblock); |
3430 | 0 | if (r_secattic) |
3431 | 0 | *r_secattic = attic; |
3432 | 0 | else |
3433 | 0 | release_kbnode (attic); |
3434 | 0 | return err; |
3435 | 0 | } |
3436 | | |
3437 | | |
3438 | | /* Return a string for the revocation reason CODE. R_FREEM must be an |
3439 | | * possibly unintialized ptr which should be freed by the caller after |
3440 | | * the return value has been consumed. */ |
3441 | | const char * |
3442 | | revocation_reason_code_to_str (int code, char **freeme) |
3443 | 0 | { |
3444 | | /* Take care: get_revocation_reason has knowledge of the internal |
3445 | | * working of this fucntion. */ |
3446 | 0 | const char *result; |
3447 | |
|
3448 | 0 | *freeme = NULL; |
3449 | 0 | switch (code) |
3450 | 0 | { |
3451 | 0 | case 0x00: result = _("No reason specified"); break; |
3452 | 0 | case 0x01: result = _("Key is superseded"); break; |
3453 | 0 | case 0x02: result = _("Key has been compromised"); break; |
3454 | 0 | case 0x03: result = _("Key is no longer used"); break; |
3455 | 0 | case 0x20: result = _("User ID is no longer valid"); break; |
3456 | 0 | default: |
3457 | 0 | *freeme = xasprintf ("code=%02x", code); |
3458 | 0 | result = *freeme; |
3459 | 0 | break; |
3460 | 0 | } |
3461 | | |
3462 | 0 | return result; |
3463 | 0 | } |
3464 | | |
3465 | | |
3466 | | /* Return the recocation reason from signature SIG. If no revocation |
3467 | | * reason is available 0 is returned, in other cases the reason |
3468 | | * (0..255). If R_REASON is not NULL a malloced textual |
3469 | | * representation of the code is stored there. If R_COMMENT is not |
3470 | | * NULL the comment from the reason is stored there and its length at |
3471 | | * R_COMMENTLEN. Note that the value at R_COMMENT is not filtered but |
3472 | | * user supplied data in UTF8; thus it needs to be escaped for display |
3473 | | * purposes. Both return values are either NULL or a malloced |
3474 | | * string/buffer. */ |
3475 | | int |
3476 | | get_revocation_reason (PKT_signature *sig, char **r_reason, |
3477 | | char **r_comment, size_t *r_commentlen) |
3478 | 0 | { |
3479 | 0 | int reason_seq = 0; |
3480 | 0 | size_t reason_n; |
3481 | 0 | const byte *reason_p; |
3482 | 0 | int reason_code = 0; |
3483 | 0 | const char *reason_string; |
3484 | 0 | char *freeme; |
3485 | |
|
3486 | 0 | if (r_reason) |
3487 | 0 | *r_reason = NULL; |
3488 | 0 | if (r_comment) |
3489 | 0 | *r_comment = NULL; |
3490 | | |
3491 | | /* Skip over empty reason packets. */ |
3492 | 0 | while ((reason_p = enum_sig_subpkt (sig, 1, SIGSUBPKT_REVOC_REASON, |
3493 | 0 | &reason_n, &reason_seq, NULL)) |
3494 | 0 | && !reason_n) |
3495 | 0 | ; |
3496 | 0 | if (reason_p && reason_n) |
3497 | 0 | { |
3498 | 0 | reason_code = *reason_p; |
3499 | 0 | reason_n--; reason_p++; |
3500 | 0 | reason_string = revocation_reason_code_to_str (reason_code, &freeme); |
3501 | 0 | if (r_reason && freeme) |
3502 | 0 | *r_reason = freeme; |
3503 | 0 | else if (r_reason && reason_string) |
3504 | 0 | *r_reason = xstrdup (reason_string); |
3505 | 0 | else |
3506 | 0 | xfree (freeme); |
3507 | |
|
3508 | 0 | if (r_comment && reason_n) |
3509 | 0 | { |
3510 | 0 | *r_comment = xmalloc (reason_n); |
3511 | 0 | memcpy (*r_comment, reason_p, reason_n); |
3512 | 0 | *r_commentlen = reason_n; |
3513 | 0 | } |
3514 | 0 | } |
3515 | |
|
3516 | 0 | return reason_code; |
3517 | 0 | } |
3518 | | |
3519 | | |
3520 | | /* List the recocation signature as a "rvs" record. SIGRC shows the |
3521 | | * character from the signature verification or 0 if no public key was |
3522 | | * found. */ |
3523 | | static void |
3524 | | list_standalone_revocation (ctrl_t ctrl, PKT_signature *sig, int sigrc) |
3525 | 0 | { |
3526 | 0 | char *siguid = NULL; |
3527 | 0 | size_t siguidlen = 0; |
3528 | 0 | char *issuer_fpr = NULL; |
3529 | 0 | int reason_code = 0; |
3530 | 0 | char *reason_text = NULL; |
3531 | 0 | char *reason_comment = NULL; |
3532 | 0 | size_t reason_commentlen; |
3533 | |
|
3534 | 0 | if (sigrc != '%' && sigrc != '?' && !opt.fast_list_mode) |
3535 | 0 | { |
3536 | 0 | int nouid; |
3537 | 0 | siguid = get_user_id (ctrl, sig->keyid, &siguidlen, &nouid); |
3538 | 0 | if (nouid) |
3539 | 0 | sigrc = '?'; |
3540 | 0 | } |
3541 | |
|
3542 | 0 | reason_code = get_revocation_reason (sig, &reason_text, |
3543 | 0 | &reason_comment, &reason_commentlen); |
3544 | |
|
3545 | 0 | if (opt.with_colons) |
3546 | 0 | { |
3547 | 0 | es_fputs ("rvs:", es_stdout); |
3548 | 0 | if (sigrc) |
3549 | 0 | es_putc (sigrc, es_stdout); |
3550 | 0 | es_fprintf (es_stdout, "::%d:%08lX%08lX:%s:%s:::", |
3551 | 0 | sig->pubkey_algo, |
3552 | 0 | (ulong) sig->keyid[0], (ulong) sig->keyid[1], |
3553 | 0 | colon_datestr_from_sig (sig), |
3554 | 0 | colon_expirestr_from_sig (sig)); |
3555 | |
|
3556 | 0 | if (siguid) |
3557 | 0 | es_write_sanitized (es_stdout, siguid, siguidlen, ":", NULL); |
3558 | |
|
3559 | 0 | es_fprintf (es_stdout, ":%02x%c", sig->sig_class, |
3560 | 0 | sig->flags.exportable ? 'x' : 'l'); |
3561 | 0 | if (reason_text) |
3562 | 0 | es_fprintf (es_stdout, ",%02x", reason_code); |
3563 | 0 | es_fputs ("::", es_stdout); |
3564 | |
|
3565 | 0 | if ((issuer_fpr = issuer_fpr_string (sig))) |
3566 | 0 | es_fputs (issuer_fpr, es_stdout); |
3567 | |
|
3568 | 0 | es_fprintf (es_stdout, ":::%d:", sig->digest_algo); |
3569 | |
|
3570 | 0 | if (reason_comment) |
3571 | 0 | { |
3572 | 0 | es_fputs ("::::", es_stdout); |
3573 | 0 | es_write_sanitized (es_stdout, reason_comment, reason_commentlen, |
3574 | 0 | ":", NULL); |
3575 | 0 | es_putc (':', es_stdout); |
3576 | 0 | } |
3577 | 0 | es_putc ('\n', es_stdout); |
3578 | |
|
3579 | 0 | if (opt.show_subpackets) |
3580 | 0 | print_subpackets_colon (sig); |
3581 | 0 | } |
3582 | 0 | else /* Human readable. */ |
3583 | 0 | { |
3584 | 0 | es_fputs ("rvs", es_stdout); |
3585 | 0 | es_fprintf (es_stdout, "%c%c %c%c%c%c%c%c %s %s", |
3586 | 0 | sigrc, (sig->sig_class - 0x10 > 0 && |
3587 | 0 | sig->sig_class - 0x10 < |
3588 | 0 | 4) ? '0' + sig->sig_class - 0x10 : ' ', |
3589 | 0 | sig->flags.exportable ? ' ' : 'L', |
3590 | 0 | sig->flags.revocable ? ' ' : 'R', |
3591 | 0 | sig->flags.policy_url ? 'P' : ' ', |
3592 | 0 | sig->flags.notation ? 'N' : ' ', |
3593 | 0 | sig->flags.expired ? 'X' : ' ', |
3594 | 0 | (sig->trust_depth > 9) ? 'T' : (sig->trust_depth > |
3595 | 0 | 0) ? '0' + |
3596 | 0 | sig->trust_depth : ' ', keystr (sig->keyid), |
3597 | 0 | datestr_from_sig (sig)); |
3598 | 0 | if (siguid) |
3599 | 0 | { |
3600 | 0 | es_fprintf (es_stdout, " "); |
3601 | 0 | print_utf8_buffer (es_stdout, siguid, siguidlen); |
3602 | 0 | } |
3603 | 0 | es_putc ('\n', es_stdout); |
3604 | |
|
3605 | 0 | if (sig->flags.policy_url |
3606 | 0 | && (opt.list_options & LIST_SHOW_POLICY_URLS)) |
3607 | 0 | show_policy_url (sig, 3, 0); |
3608 | |
|
3609 | 0 | if (sig->flags.notation && (opt.list_options & LIST_SHOW_NOTATIONS)) |
3610 | 0 | show_notation (sig, 3, 0, |
3611 | 0 | ((opt.list_options & LIST_SHOW_STD_NOTATIONS) ? 1 : 0) |
3612 | 0 | + |
3613 | 0 | ((opt.list_options & LIST_SHOW_USER_NOTATIONS) ? 2 : 0) |
3614 | 0 | + |
3615 | 0 | ((opt.list_options & LIST_SHOW_HIDDEN_NOTATIONS) ? 4:0)); |
3616 | |
|
3617 | 0 | if (sig->flags.pref_ks |
3618 | 0 | && (opt.list_options & LIST_SHOW_KEYSERVER_URLS)) |
3619 | 0 | show_keyserver_url (sig, 3, 0); |
3620 | |
|
3621 | 0 | if (reason_text) |
3622 | 0 | { |
3623 | 0 | es_fprintf (es_stdout, " %s%s\n", |
3624 | 0 | _("reason for revocation: "), reason_text); |
3625 | 0 | print_revocation_reason_comment (reason_comment, reason_commentlen); |
3626 | 0 | } |
3627 | 0 | } |
3628 | |
|
3629 | 0 | es_fflush (es_stdout); |
3630 | |
|
3631 | 0 | xfree (reason_text); |
3632 | 0 | xfree (reason_comment); |
3633 | 0 | xfree (siguid); |
3634 | 0 | xfree (issuer_fpr); |
3635 | 0 | } |
3636 | | |
3637 | | |
3638 | | /* Import a revocation certificate; only the first packet in the |
3639 | | * NODE-list is considered. */ |
3640 | | static int |
3641 | | import_revoke_cert (ctrl_t ctrl, kbnode_t node, unsigned int options, |
3642 | | struct import_stats_s *stats) |
3643 | 0 | { |
3644 | 0 | PKT_public_key *pk = NULL; |
3645 | 0 | kbnode_t onode; |
3646 | 0 | kbnode_t keyblock = NULL; |
3647 | 0 | KEYDB_HANDLE hd = NULL; |
3648 | 0 | u32 keyid[2]; |
3649 | 0 | int rc = 0; |
3650 | 0 | int sigrc = 0; |
3651 | 0 | int silent; |
3652 | | |
3653 | | /* No error output for --show-keys. */ |
3654 | 0 | silent = (options & (IMPORT_SHOW | IMPORT_DRY_RUN)); |
3655 | |
|
3656 | 0 | log_assert (node->pkt->pkttype == PKT_SIGNATURE ); |
3657 | 0 | log_assert (IS_KEY_REV (node->pkt->pkt.signature)); |
3658 | | |
3659 | | /* FIXME: We can do better here by using the issuer fingerprint if |
3660 | | * available. We should also make use of get_keyblock_byfprint_fast. */ |
3661 | | |
3662 | 0 | keyid[0] = node->pkt->pkt.signature->keyid[0]; |
3663 | 0 | keyid[1] = node->pkt->pkt.signature->keyid[1]; |
3664 | |
|
3665 | 0 | pk = xmalloc_clear( sizeof *pk ); |
3666 | 0 | rc = get_pubkey (ctrl, pk, keyid ); |
3667 | 0 | if (gpg_err_code (rc) == GPG_ERR_NO_PUBKEY ) |
3668 | 0 | { |
3669 | 0 | if (!silent) |
3670 | 0 | log_error (_("key %s: no public key -" |
3671 | 0 | " can't apply revocation certificate\n"), keystr(keyid)); |
3672 | 0 | rc = 0; |
3673 | 0 | goto leave; |
3674 | 0 | } |
3675 | 0 | else if (rc ) |
3676 | 0 | { |
3677 | 0 | log_error (_("key %s: public key not found: %s\n"), |
3678 | 0 | keystr(keyid), gpg_strerror (rc)); |
3679 | 0 | goto leave; |
3680 | 0 | } |
3681 | | |
3682 | | /* Read the original keyblock. */ |
3683 | 0 | hd = keydb_new (ctrl); |
3684 | 0 | if (!hd) |
3685 | 0 | { |
3686 | 0 | rc = gpg_error_from_syserror (); |
3687 | 0 | goto leave; |
3688 | 0 | } |
3689 | | |
3690 | 0 | rc = keydb_lock (hd); |
3691 | 0 | if (rc) |
3692 | 0 | { |
3693 | 0 | keydb_release (hd); |
3694 | 0 | goto leave; |
3695 | 0 | } |
3696 | | |
3697 | 0 | { |
3698 | 0 | byte afp[MAX_FINGERPRINT_LEN]; |
3699 | 0 | size_t an; |
3700 | |
|
3701 | 0 | fingerprint_from_pk (pk, afp, &an); |
3702 | 0 | rc = keydb_search_fpr (hd, afp, an); |
3703 | 0 | } |
3704 | 0 | if (rc) |
3705 | 0 | { |
3706 | 0 | log_error (_("key %s: can't locate original keyblock: %s\n"), |
3707 | 0 | keystr(keyid), gpg_strerror (rc)); |
3708 | 0 | goto leave; |
3709 | 0 | } |
3710 | 0 | rc = keydb_get_keyblock (hd, &keyblock ); |
3711 | 0 | if (rc) |
3712 | 0 | { |
3713 | 0 | log_error (_("key %s: can't read original keyblock: %s\n"), |
3714 | 0 | keystr(keyid), gpg_strerror (rc)); |
3715 | 0 | goto leave; |
3716 | 0 | } |
3717 | | |
3718 | | /* it is okay, that node is not in keyblock because |
3719 | | * check_key_signature works fine for sig_class 0x20 (KEY_REV) in |
3720 | | * this special case. SIGRC is only used for IMPORT_SHOW. */ |
3721 | 0 | rc = check_key_signature (ctrl, keyblock, node, NULL); |
3722 | 0 | switch (gpg_err_code (rc)) |
3723 | 0 | { |
3724 | 0 | case 0: sigrc = '!'; break; |
3725 | 0 | case GPG_ERR_BAD_SIGNATURE: sigrc = '-'; break; |
3726 | 0 | case GPG_ERR_NO_PUBKEY: sigrc = '?'; break; |
3727 | 0 | case GPG_ERR_UNUSABLE_PUBKEY: sigrc = '?'; break; |
3728 | 0 | default: sigrc = '%'; break; |
3729 | 0 | } |
3730 | 0 | if (rc ) |
3731 | 0 | { |
3732 | 0 | if (!silent) |
3733 | 0 | log_error (_("key %s: invalid revocation certificate" |
3734 | 0 | ": %s - rejected\n"), keystr(keyid), gpg_strerror (rc)); |
3735 | 0 | goto leave; |
3736 | 0 | } |
3737 | | |
3738 | | /* check whether we already have this */ |
3739 | 0 | for(onode=keyblock->next; onode; onode=onode->next ) { |
3740 | 0 | if (onode->pkt->pkttype == PKT_USER_ID ) |
3741 | 0 | break; |
3742 | 0 | else if (onode->pkt->pkttype == PKT_SIGNATURE |
3743 | 0 | && !cmp_signatures(node->pkt->pkt.signature, |
3744 | 0 | onode->pkt->pkt.signature)) |
3745 | 0 | { |
3746 | 0 | rc = 0; |
3747 | 0 | goto leave; /* yes, we already know about it */ |
3748 | 0 | } |
3749 | 0 | } |
3750 | | |
3751 | | /* insert it */ |
3752 | 0 | insert_kbnode( keyblock, clone_kbnode(node), 0 ); |
3753 | | |
3754 | | /* and write the keyblock back unless in dry run mode. */ |
3755 | 0 | if (!(opt.dry_run || (options & IMPORT_DRY_RUN))) |
3756 | 0 | { |
3757 | 0 | rc = keydb_update_keyblock (ctrl, hd, keyblock ); |
3758 | 0 | if (rc) |
3759 | 0 | log_error (_("error writing keyring '%s': %s\n"), |
3760 | 0 | keydb_get_resource_name (hd), gpg_strerror (rc) ); |
3761 | 0 | keydb_release (hd); |
3762 | 0 | hd = NULL; |
3763 | | |
3764 | | /* we are ready */ |
3765 | 0 | if (!opt.quiet ) |
3766 | 0 | { |
3767 | 0 | char *p=get_user_id_native (ctrl, keyid); |
3768 | 0 | log_info( _("key %s: \"%s\" revocation certificate imported\n"), |
3769 | 0 | keystr(keyid),p); |
3770 | 0 | xfree(p); |
3771 | 0 | } |
3772 | | |
3773 | | /* If the key we just revoked was ultimately trusted, remove its |
3774 | | * ultimate trust. This doesn't stop the user from putting the |
3775 | | * ultimate trust back, but is a reasonable solution for now. */ |
3776 | 0 | if (get_ownertrust (ctrl, pk) == TRUST_ULTIMATE) |
3777 | 0 | clear_ownertrusts (ctrl, pk); |
3778 | |
|
3779 | 0 | revalidation_mark (ctrl); |
3780 | 0 | } |
3781 | 0 | stats->n_revoc++; |
3782 | |
|
3783 | 0 | leave: |
3784 | 0 | if ((options & IMPORT_SHOW)) |
3785 | 0 | list_standalone_revocation (ctrl, node->pkt->pkt.signature, sigrc); |
3786 | |
|
3787 | 0 | keydb_release (hd); |
3788 | 0 | release_kbnode( keyblock ); |
3789 | 0 | free_public_key( pk ); |
3790 | 0 | return rc; |
3791 | 0 | } |
3792 | | |
3793 | | |
3794 | | /* Loop over the KEYBLOCK and check all self signatures. KEYID is the |
3795 | | * keyid of the primary key for reporting purposes. On return the |
3796 | | * following bits in the node flags are set: |
3797 | | * |
3798 | | * - NODE_GOOD_SELFSIG :: User ID or subkey has a self-signature |
3799 | | * - NODE_BAD_SELFSIG :: Used ID or subkey has an invalid self-signature |
3800 | | * - NODE_DELETION_MARK :: This node shall be deleted |
3801 | | * |
3802 | | * NON_SELF is set to true if there are any sigs other than self-sigs |
3803 | | * in this keyblock. |
3804 | | * |
3805 | | * Returns 0 on success or -1 (but not an error code) if the keyblock |
3806 | | * is invalid. |
3807 | | */ |
3808 | | static int |
3809 | | chk_self_sigs (ctrl_t ctrl, kbnode_t keyblock, u32 *keyid, int *non_self) |
3810 | 0 | { |
3811 | 0 | kbnode_t knode = NULL; /* The node of the current subkey. */ |
3812 | 0 | PKT_public_key *subpk = NULL; /* and its packet. */ |
3813 | 0 | kbnode_t bsnode = NULL; /* Subkey binding signature node. */ |
3814 | 0 | u32 bsdate = 0; /* Timestamp of that node. */ |
3815 | 0 | kbnode_t rsnode = NULL; /* Subkey recocation signature node. */ |
3816 | 0 | u32 rsdate = 0; /* Timestamp of that node. */ |
3817 | 0 | PKT_signature *sig; |
3818 | 0 | int rc; |
3819 | 0 | kbnode_t n; |
3820 | |
|
3821 | 0 | for (n=keyblock; (n = find_next_kbnode (n, 0)); ) |
3822 | 0 | { |
3823 | 0 | if (n->pkt->pkttype == PKT_PUBLIC_SUBKEY) |
3824 | 0 | { |
3825 | 0 | knode = n; |
3826 | 0 | subpk = knode->pkt->pkt.public_key; |
3827 | 0 | bsdate = 0; |
3828 | 0 | rsdate = 0; |
3829 | 0 | bsnode = NULL; |
3830 | 0 | rsnode = NULL; |
3831 | 0 | continue; |
3832 | 0 | } |
3833 | | |
3834 | 0 | if ( n->pkt->pkttype != PKT_SIGNATURE ) |
3835 | 0 | continue; |
3836 | | |
3837 | 0 | sig = n->pkt->pkt.signature; |
3838 | 0 | if ( keyid[0] != sig->keyid[0] || keyid[1] != sig->keyid[1] ) |
3839 | 0 | { |
3840 | 0 | *non_self = 1; |
3841 | 0 | continue; |
3842 | 0 | } |
3843 | | |
3844 | | /* This just caches the sigs for later use. That way we |
3845 | | import a fully-cached key which speeds things up. */ |
3846 | 0 | if (!opt.no_sig_cache) |
3847 | 0 | check_key_signature (ctrl, keyblock, n, NULL); |
3848 | |
|
3849 | 0 | if ( IS_UID_SIG(sig) || IS_UID_REV(sig) ) |
3850 | 0 | { |
3851 | 0 | kbnode_t unode = find_prev_kbnode( keyblock, n, PKT_USER_ID ); |
3852 | 0 | if ( !unode ) |
3853 | 0 | { |
3854 | 0 | log_error( _("key %s: no user ID for signature\n"), |
3855 | 0 | keystr(keyid)); |
3856 | 0 | return -1; /* The complete keyblock is invalid. */ |
3857 | 0 | } |
3858 | | |
3859 | | /* If it hasn't been marked valid yet, keep trying. */ |
3860 | 0 | if (!(unode->flag & NODE_GOOD_SELFSIG)) |
3861 | 0 | { |
3862 | 0 | rc = check_key_signature (ctrl, keyblock, n, NULL); |
3863 | 0 | if ( rc ) |
3864 | 0 | { |
3865 | 0 | if ( opt.verbose ) |
3866 | 0 | { |
3867 | 0 | char *p = utf8_to_native |
3868 | 0 | (unode->pkt->pkt.user_id->name, |
3869 | 0 | strlen (unode->pkt->pkt.user_id->name),0); |
3870 | 0 | log_info (gpg_err_code(rc) == GPG_ERR_PUBKEY_ALGO ? |
3871 | 0 | _("key %s: unsupported public key " |
3872 | 0 | "algorithm on user ID \"%s\"\n"): |
3873 | 0 | _("key %s: invalid self-signature " |
3874 | 0 | "on user ID \"%s\"\n"), |
3875 | 0 | keystr (keyid),p); |
3876 | 0 | xfree (p); |
3877 | 0 | } |
3878 | 0 | } |
3879 | 0 | else |
3880 | 0 | unode->flag |= NODE_GOOD_SELFSIG; |
3881 | 0 | } |
3882 | 0 | } |
3883 | 0 | else if (IS_KEY_SIG (sig)) |
3884 | 0 | { |
3885 | 0 | rc = check_key_signature (ctrl, keyblock, n, NULL); |
3886 | 0 | if ( rc ) |
3887 | 0 | { |
3888 | 0 | if (opt.verbose) |
3889 | 0 | log_info (gpg_err_code (rc) == GPG_ERR_PUBKEY_ALGO ? |
3890 | 0 | _("key %s: unsupported public key algorithm\n"): |
3891 | 0 | _("key %s: invalid direct key signature\n"), |
3892 | 0 | keystr (keyid)); |
3893 | 0 | n->flag |= NODE_DELETION_MARK; |
3894 | 0 | } |
3895 | 0 | } |
3896 | 0 | else if ( IS_SUBKEY_SIG (sig) ) |
3897 | 0 | { |
3898 | | /* Note that this works based solely on the timestamps like |
3899 | | the rest of gpg. If the standard gets revocation |
3900 | | targets, this may need to be revised. */ |
3901 | |
|
3902 | 0 | if ( !knode ) |
3903 | 0 | { |
3904 | 0 | if (opt.verbose) |
3905 | 0 | log_info (_("key %s: no subkey for key binding\n"), |
3906 | 0 | keystr (keyid)); |
3907 | 0 | n->flag |= NODE_DELETION_MARK; |
3908 | 0 | } |
3909 | 0 | else |
3910 | 0 | { |
3911 | 0 | rc = check_key_signature (ctrl, keyblock, n, NULL); |
3912 | 0 | if ( rc ) |
3913 | 0 | { |
3914 | 0 | if (opt.verbose) |
3915 | 0 | { |
3916 | 0 | keyid_from_pk (subpk, NULL); |
3917 | 0 | log_info (gpg_err_code (rc) == GPG_ERR_PUBKEY_ALGO ? |
3918 | 0 | _("key %s: unsupported public key" |
3919 | 0 | " algorithm\n"): |
3920 | 0 | _("key %s: invalid subkey binding\n"), |
3921 | 0 | keystr_with_sub (keyid, subpk->keyid)); |
3922 | 0 | } |
3923 | 0 | n->flag |= NODE_DELETION_MARK; |
3924 | 0 | } |
3925 | 0 | else |
3926 | 0 | { |
3927 | | /* It's valid, so is it newer? */ |
3928 | 0 | if (sig->timestamp >= bsdate) |
3929 | 0 | { |
3930 | 0 | knode->flag |= NODE_GOOD_SELFSIG; /* Subkey is valid. */ |
3931 | 0 | if (bsnode) |
3932 | 0 | { |
3933 | | /* Delete the last binding sig since this |
3934 | | one is newer */ |
3935 | 0 | bsnode->flag |= NODE_DELETION_MARK; |
3936 | 0 | if (opt.verbose) |
3937 | 0 | { |
3938 | 0 | keyid_from_pk (subpk, NULL); |
3939 | 0 | log_info (_("key %s: removed multiple subkey" |
3940 | 0 | " binding\n"), |
3941 | 0 | keystr_with_sub (keyid, subpk->keyid)); |
3942 | 0 | } |
3943 | 0 | } |
3944 | |
|
3945 | 0 | bsnode = n; |
3946 | 0 | bsdate = sig->timestamp; |
3947 | 0 | } |
3948 | 0 | else |
3949 | 0 | n->flag |= NODE_DELETION_MARK; /* older */ |
3950 | 0 | } |
3951 | 0 | } |
3952 | 0 | } |
3953 | 0 | else if ( IS_SUBKEY_REV (sig) ) |
3954 | 0 | { |
3955 | | /* We don't actually mark the subkey as revoked right now, |
3956 | | so just check that the revocation sig is the most recent |
3957 | | valid one. Note that we don't care if the binding sig is |
3958 | | newer than the revocation sig. See the comment in |
3959 | | getkey.c:merge_selfsigs_subkey for more. */ |
3960 | 0 | if ( !knode ) |
3961 | 0 | { |
3962 | 0 | if (opt.verbose) |
3963 | 0 | log_info (_("key %s: no subkey for key revocation\n"), |
3964 | 0 | keystr(keyid)); |
3965 | 0 | n->flag |= NODE_DELETION_MARK; |
3966 | 0 | } |
3967 | 0 | else |
3968 | 0 | { |
3969 | 0 | rc = check_key_signature (ctrl, keyblock, n, NULL); |
3970 | 0 | if ( rc ) |
3971 | 0 | { |
3972 | 0 | if(opt.verbose) |
3973 | 0 | log_info (gpg_err_code (rc) == GPG_ERR_PUBKEY_ALGO ? |
3974 | 0 | _("key %s: unsupported public" |
3975 | 0 | " key algorithm\n"): |
3976 | 0 | _("key %s: invalid subkey revocation\n"), |
3977 | 0 | keystr(keyid)); |
3978 | 0 | n->flag |= NODE_DELETION_MARK; |
3979 | 0 | } |
3980 | 0 | else |
3981 | 0 | { |
3982 | | /* It's valid, so is it newer? */ |
3983 | 0 | if (sig->timestamp >= rsdate) |
3984 | 0 | { |
3985 | 0 | if (rsnode) |
3986 | 0 | { |
3987 | | /* Delete the last revocation sig since |
3988 | | this one is newer. */ |
3989 | 0 | rsnode->flag |= NODE_DELETION_MARK; |
3990 | 0 | if (opt.verbose) |
3991 | 0 | log_info (_("key %s: removed multiple subkey" |
3992 | 0 | " revocation\n"),keystr(keyid)); |
3993 | 0 | } |
3994 | |
|
3995 | 0 | rsnode = n; |
3996 | 0 | rsdate = sig->timestamp; |
3997 | 0 | } |
3998 | 0 | else |
3999 | 0 | n->flag |= NODE_DELETION_MARK; /* older */ |
4000 | 0 | } |
4001 | 0 | } |
4002 | 0 | } |
4003 | 0 | } |
4004 | | |
4005 | 0 | return 0; |
4006 | 0 | } |
4007 | | |
4008 | | |
4009 | | /* Delete all parts which are invalid and those signatures whose |
4010 | | * public key algorithm is not available in this implementation; but |
4011 | | * consider RSA as valid, because parse/build_packets knows about it. |
4012 | | * If R_OTHERREVSIGS is not NULL, it is used to return a list of |
4013 | | * revocation certificates which have been deleted from KEYBLOCK but |
4014 | | * should be handled later. |
4015 | | * |
4016 | | * Returns: True if at least one valid user-id is left over. |
4017 | | */ |
4018 | | static int |
4019 | | delete_inv_parts (ctrl_t ctrl, kbnode_t keyblock, u32 *keyid, |
4020 | | unsigned int options, kbnode_t *r_otherrevsigs) |
4021 | 0 | { |
4022 | 0 | kbnode_t node; |
4023 | 0 | int nvalid=0, uid_seen=0, subkey_seen=0; |
4024 | 0 | PKT_public_key *pk; |
4025 | |
|
4026 | 0 | for (node=keyblock->next; node; node = node->next ) |
4027 | 0 | { |
4028 | 0 | if (node->pkt->pkttype == PKT_USER_ID) |
4029 | 0 | { |
4030 | 0 | uid_seen = 1; |
4031 | 0 | if ((node->flag & NODE_BAD_SELFSIG) |
4032 | 0 | || !(node->flag & NODE_GOOD_SELFSIG)) |
4033 | 0 | { |
4034 | 0 | if (opt.verbose ) |
4035 | 0 | { |
4036 | 0 | char *p=utf8_to_native(node->pkt->pkt.user_id->name, |
4037 | 0 | node->pkt->pkt.user_id->len,0); |
4038 | 0 | log_info( _("key %s: skipped user ID \"%s\"\n"), |
4039 | 0 | keystr(keyid),p); |
4040 | 0 | xfree(p); |
4041 | 0 | } |
4042 | 0 | delete_kbnode( node ); /* the user-id */ |
4043 | | /* and all following packets up to the next user-id */ |
4044 | 0 | while (node->next |
4045 | 0 | && node->next->pkt->pkttype != PKT_USER_ID |
4046 | 0 | && node->next->pkt->pkttype != PKT_PUBLIC_SUBKEY |
4047 | 0 | && node->next->pkt->pkttype != PKT_SECRET_SUBKEY ){ |
4048 | 0 | delete_kbnode( node->next ); |
4049 | 0 | node = node->next; |
4050 | 0 | } |
4051 | 0 | } |
4052 | 0 | else |
4053 | 0 | nvalid++; |
4054 | 0 | } |
4055 | 0 | else if ( node->pkt->pkttype == PKT_PUBLIC_SUBKEY |
4056 | 0 | || node->pkt->pkttype == PKT_SECRET_SUBKEY ) |
4057 | 0 | { |
4058 | 0 | if ((node->flag & NODE_BAD_SELFSIG) |
4059 | 0 | || !(node->flag & NODE_GOOD_SELFSIG)) |
4060 | 0 | { |
4061 | 0 | if (opt.verbose ) |
4062 | 0 | { |
4063 | 0 | pk = node->pkt->pkt.public_key; |
4064 | 0 | keyid_from_pk (pk, NULL); |
4065 | 0 | log_info (_("key %s: skipped subkey\n"), |
4066 | 0 | keystr_with_sub (keyid, pk->keyid)); |
4067 | 0 | } |
4068 | |
|
4069 | 0 | delete_kbnode( node ); /* the subkey */ |
4070 | | /* and all following signature packets */ |
4071 | 0 | while (node->next |
4072 | 0 | && node->next->pkt->pkttype == PKT_SIGNATURE ) { |
4073 | 0 | delete_kbnode( node->next ); |
4074 | 0 | node = node->next; |
4075 | 0 | } |
4076 | 0 | } |
4077 | 0 | else |
4078 | 0 | subkey_seen = 1; |
4079 | 0 | } |
4080 | 0 | else if (node->pkt->pkttype == PKT_SIGNATURE |
4081 | 0 | && openpgp_pk_test_algo (node->pkt->pkt.signature->pubkey_algo) |
4082 | 0 | && node->pkt->pkt.signature->pubkey_algo != PUBKEY_ALGO_RSA ) |
4083 | 0 | { |
4084 | 0 | delete_kbnode( node ); /* build_packet() can't handle this */ |
4085 | 0 | } |
4086 | 0 | else if (node->pkt->pkttype == PKT_SIGNATURE |
4087 | 0 | && !node->pkt->pkt.signature->flags.exportable |
4088 | 0 | && !(options&IMPORT_LOCAL_SIGS) |
4089 | 0 | && !have_secret_key_with_kid (ctrl, |
4090 | 0 | node->pkt->pkt.signature->keyid)) |
4091 | 0 | { |
4092 | | /* here we violate the rfc a bit by still allowing |
4093 | | * to import non-exportable signature when we have the |
4094 | | * the secret key used to create this signature - it |
4095 | | * seems that this makes sense */ |
4096 | 0 | if(opt.verbose) |
4097 | 0 | log_info( _("key %s: non exportable signature" |
4098 | 0 | " (class 0x%02X) - skipped\n"), |
4099 | 0 | keystr(keyid), node->pkt->pkt.signature->sig_class ); |
4100 | 0 | delete_kbnode( node ); |
4101 | 0 | } |
4102 | 0 | else if (node->pkt->pkttype == PKT_SIGNATURE |
4103 | 0 | && IS_KEY_REV (node->pkt->pkt.signature)) |
4104 | 0 | { |
4105 | 0 | if (uid_seen ) |
4106 | 0 | { |
4107 | 0 | if(opt.verbose) |
4108 | 0 | log_info( _("key %s: revocation certificate" |
4109 | 0 | " at wrong place - skipped\n"),keystr(keyid)); |
4110 | 0 | if (r_otherrevsigs) |
4111 | 0 | { |
4112 | 0 | PACKET *pkt; |
4113 | |
|
4114 | 0 | pkt = xcalloc (1, sizeof *pkt); |
4115 | 0 | pkt->pkttype = PKT_SIGNATURE; |
4116 | 0 | pkt->pkt.signature = copy_signature |
4117 | 0 | (NULL, node->pkt->pkt.signature); |
4118 | 0 | *r_otherrevsigs = new_kbnode2 (*r_otherrevsigs, pkt); |
4119 | 0 | } |
4120 | 0 | delete_kbnode( node ); |
4121 | 0 | } |
4122 | 0 | else |
4123 | 0 | { |
4124 | | /* If the revocation cert is from a different key than |
4125 | | the one we're working on don't check it - it's |
4126 | | probably from a revocation key and won't be |
4127 | | verifiable with this key anyway. */ |
4128 | |
|
4129 | 0 | if(node->pkt->pkt.signature->keyid[0]==keyid[0] |
4130 | 0 | && node->pkt->pkt.signature->keyid[1]==keyid[1]) |
4131 | 0 | { |
4132 | 0 | int rc = check_key_signature (ctrl, keyblock, node, NULL); |
4133 | 0 | if (rc ) |
4134 | 0 | { |
4135 | 0 | if(opt.verbose) |
4136 | 0 | log_info( _("key %s: invalid revocation" |
4137 | 0 | " certificate: %s - skipped\n"), |
4138 | 0 | keystr(keyid), gpg_strerror (rc)); |
4139 | 0 | delete_kbnode( node ); |
4140 | 0 | } |
4141 | 0 | } |
4142 | 0 | else if (r_otherrevsigs) |
4143 | 0 | { |
4144 | 0 | PACKET *pkt; |
4145 | |
|
4146 | 0 | pkt = xcalloc (1, sizeof *pkt); |
4147 | 0 | pkt->pkttype = PKT_SIGNATURE; |
4148 | 0 | pkt->pkt.signature = copy_signature |
4149 | 0 | (NULL, node->pkt->pkt.signature); |
4150 | 0 | *r_otherrevsigs = new_kbnode2 (*r_otherrevsigs, pkt); |
4151 | 0 | } |
4152 | 0 | } |
4153 | 0 | } |
4154 | 0 | else if (node->pkt->pkttype == PKT_SIGNATURE |
4155 | 0 | && (IS_SUBKEY_SIG (node->pkt->pkt.signature) |
4156 | 0 | || IS_SUBKEY_REV (node->pkt->pkt.signature)) |
4157 | 0 | && !subkey_seen ) |
4158 | 0 | { |
4159 | 0 | if(opt.verbose) |
4160 | 0 | log_info( _("key %s: subkey signature" |
4161 | 0 | " in wrong place - skipped\n"), keystr(keyid)); |
4162 | 0 | delete_kbnode( node ); |
4163 | 0 | } |
4164 | 0 | else if (node->pkt->pkttype == PKT_SIGNATURE |
4165 | 0 | && !IS_CERT(node->pkt->pkt.signature)) |
4166 | 0 | { |
4167 | 0 | if(opt.verbose) |
4168 | 0 | log_info(_("key %s: unexpected signature class (0x%02X) -" |
4169 | 0 | " skipped\n"),keystr(keyid), |
4170 | 0 | node->pkt->pkt.signature->sig_class); |
4171 | 0 | delete_kbnode(node); |
4172 | 0 | } |
4173 | 0 | else if ((node->flag & NODE_DELETION_MARK)) |
4174 | 0 | delete_kbnode( node ); |
4175 | 0 | } |
4176 | | |
4177 | | /* note: because keyblock is the public key, it is never marked |
4178 | | * for deletion and so keyblock cannot change */ |
4179 | 0 | commit_kbnode( &keyblock ); |
4180 | 0 | return nvalid; |
4181 | 0 | } |
4182 | | |
4183 | | /* This function returns true if any UID is left in the keyring. */ |
4184 | | static int |
4185 | | any_uid_left (kbnode_t keyblock) |
4186 | 0 | { |
4187 | 0 | kbnode_t node; |
4188 | |
|
4189 | 0 | for (node=keyblock->next; node; node = node->next) |
4190 | 0 | if (node->pkt->pkttype == PKT_USER_ID) |
4191 | 0 | return 1; |
4192 | 0 | return 0; |
4193 | 0 | } |
4194 | | |
4195 | | |
4196 | | /* Delete all non-self-sigs from KEYBLOCK. |
4197 | | * Returns: True if the keyblock has changed. */ |
4198 | | static void |
4199 | | remove_all_non_self_sigs (kbnode_t *keyblock, u32 *keyid) |
4200 | 0 | { |
4201 | 0 | kbnode_t node; |
4202 | 0 | unsigned int dropped = 0; |
4203 | |
|
4204 | 0 | for (node = *keyblock; node; node = node->next) |
4205 | 0 | { |
4206 | 0 | if (is_deleted_kbnode (node)) |
4207 | 0 | continue; |
4208 | | |
4209 | 0 | if (node->pkt->pkttype != PKT_SIGNATURE) |
4210 | 0 | continue; |
4211 | | |
4212 | 0 | if (node->pkt->pkt.signature->keyid[0] == keyid[0] |
4213 | 0 | && node->pkt->pkt.signature->keyid[1] == keyid[1]) |
4214 | 0 | continue; |
4215 | 0 | delete_kbnode (node); |
4216 | 0 | dropped++; |
4217 | 0 | } |
4218 | |
|
4219 | 0 | if (dropped) |
4220 | 0 | commit_kbnode (keyblock); |
4221 | |
|
4222 | 0 | if (dropped && opt.verbose) |
4223 | 0 | log_info ("key %s: number of dropped non-self-signatures: %u\n", |
4224 | 0 | keystr (keyid), dropped); |
4225 | 0 | } |
4226 | | |
4227 | | |
4228 | | /* |
4229 | | * It may happen that the imported keyblock has duplicated user IDs. |
4230 | | * We check this here and collapse those user IDs together with their |
4231 | | * sigs into one. |
4232 | | * Returns: True if the keyblock has changed. |
4233 | | */ |
4234 | | int |
4235 | | collapse_uids (kbnode_t *keyblock) |
4236 | 0 | { |
4237 | 0 | kbnode_t uid1; |
4238 | 0 | int any=0; |
4239 | |
|
4240 | 0 | for(uid1=*keyblock;uid1;uid1=uid1->next) |
4241 | 0 | { |
4242 | 0 | kbnode_t uid2; |
4243 | |
|
4244 | 0 | if(is_deleted_kbnode(uid1)) |
4245 | 0 | continue; |
4246 | | |
4247 | 0 | if(uid1->pkt->pkttype!=PKT_USER_ID) |
4248 | 0 | continue; |
4249 | | |
4250 | 0 | for(uid2=uid1->next;uid2;uid2=uid2->next) |
4251 | 0 | { |
4252 | 0 | if(is_deleted_kbnode(uid2)) |
4253 | 0 | continue; |
4254 | | |
4255 | 0 | if(uid2->pkt->pkttype!=PKT_USER_ID) |
4256 | 0 | continue; |
4257 | | |
4258 | 0 | if(cmp_user_ids(uid1->pkt->pkt.user_id, |
4259 | 0 | uid2->pkt->pkt.user_id)==0) |
4260 | 0 | { |
4261 | | /* We have a duplicated uid */ |
4262 | 0 | kbnode_t sig1,last; |
4263 | |
|
4264 | 0 | any=1; |
4265 | | |
4266 | | /* Now take uid2's signatures, and attach them to |
4267 | | uid1 */ |
4268 | 0 | for(last=uid2;last->next;last=last->next) |
4269 | 0 | { |
4270 | 0 | if(is_deleted_kbnode(last)) |
4271 | 0 | continue; |
4272 | | |
4273 | 0 | if(last->next->pkt->pkttype==PKT_USER_ID |
4274 | 0 | || last->next->pkt->pkttype==PKT_PUBLIC_SUBKEY |
4275 | 0 | || last->next->pkt->pkttype==PKT_SECRET_SUBKEY) |
4276 | 0 | break; |
4277 | 0 | } |
4278 | | |
4279 | | /* Snip out uid2 */ |
4280 | 0 | (find_prev_kbnode(*keyblock,uid2,0))->next=last->next; |
4281 | | |
4282 | | /* Now put uid2 in place as part of uid1 */ |
4283 | 0 | last->next=uid1->next; |
4284 | 0 | uid1->next=uid2; |
4285 | 0 | delete_kbnode(uid2); |
4286 | | |
4287 | | /* Now dedupe uid1 */ |
4288 | 0 | for(sig1=uid1->next;sig1;sig1=sig1->next) |
4289 | 0 | { |
4290 | 0 | kbnode_t sig2; |
4291 | |
|
4292 | 0 | if(is_deleted_kbnode(sig1)) |
4293 | 0 | continue; |
4294 | | |
4295 | 0 | if(sig1->pkt->pkttype==PKT_USER_ID |
4296 | 0 | || sig1->pkt->pkttype==PKT_PUBLIC_SUBKEY |
4297 | 0 | || sig1->pkt->pkttype==PKT_SECRET_SUBKEY) |
4298 | 0 | break; |
4299 | | |
4300 | 0 | if(sig1->pkt->pkttype!=PKT_SIGNATURE) |
4301 | 0 | continue; |
4302 | | |
4303 | 0 | for(sig2=sig1->next,last=sig1;sig2;last=sig2,sig2=sig2->next) |
4304 | 0 | { |
4305 | 0 | if(is_deleted_kbnode(sig2)) |
4306 | 0 | continue; |
4307 | | |
4308 | 0 | if(sig2->pkt->pkttype==PKT_USER_ID |
4309 | 0 | || sig2->pkt->pkttype==PKT_PUBLIC_SUBKEY |
4310 | 0 | || sig2->pkt->pkttype==PKT_SECRET_SUBKEY) |
4311 | 0 | break; |
4312 | | |
4313 | 0 | if(sig2->pkt->pkttype!=PKT_SIGNATURE) |
4314 | 0 | continue; |
4315 | | |
4316 | 0 | if(cmp_signatures(sig1->pkt->pkt.signature, |
4317 | 0 | sig2->pkt->pkt.signature)==0) |
4318 | 0 | { |
4319 | | /* We have a match, so delete the second |
4320 | | signature */ |
4321 | 0 | delete_kbnode(sig2); |
4322 | 0 | sig2=last; |
4323 | 0 | } |
4324 | 0 | } |
4325 | 0 | } |
4326 | 0 | } |
4327 | 0 | } |
4328 | 0 | } |
4329 | |
|
4330 | 0 | commit_kbnode(keyblock); |
4331 | |
|
4332 | 0 | if(any && !opt.quiet) |
4333 | 0 | { |
4334 | 0 | const char *key="???"; |
4335 | |
|
4336 | 0 | if ((uid1 = find_kbnode (*keyblock, PKT_PUBLIC_KEY)) ) |
4337 | 0 | key = keystr_from_pk (uid1->pkt->pkt.public_key); |
4338 | 0 | else if ((uid1 = find_kbnode( *keyblock, PKT_SECRET_KEY)) ) |
4339 | 0 | key = keystr_from_pk (uid1->pkt->pkt.public_key); |
4340 | |
|
4341 | 0 | log_info (_("key %s: duplicated user ID detected - merged\n"), key); |
4342 | 0 | } |
4343 | |
|
4344 | 0 | return any; |
4345 | 0 | } |
4346 | | |
4347 | | |
4348 | | /* |
4349 | | * It may happen that the imported keyblock has duplicated subkeys. |
4350 | | * We check this here and collapse those subkeys along with their |
4351 | | * binding self-signatures. |
4352 | | * Returns: True if the keyblock has changed. |
4353 | | */ |
4354 | | int |
4355 | | collapse_subkeys (kbnode_t *keyblock) |
4356 | 0 | { |
4357 | 0 | kbnode_t kb1, kb2, sig1, sig2, last; |
4358 | 0 | int any = 0; |
4359 | |
|
4360 | 0 | for (kb1 = *keyblock; kb1; kb1 = kb1->next) |
4361 | 0 | { |
4362 | 0 | if (is_deleted_kbnode (kb1)) |
4363 | 0 | continue; |
4364 | | |
4365 | 0 | if (kb1->pkt->pkttype != PKT_PUBLIC_SUBKEY |
4366 | 0 | && kb1->pkt->pkttype != PKT_SECRET_SUBKEY) |
4367 | 0 | continue; |
4368 | | |
4369 | | /* We assume just a few duplicates and use a straightforward |
4370 | | * algorithm. */ |
4371 | 0 | for (kb2 = kb1->next; kb2; kb2 = kb2->next) |
4372 | 0 | { |
4373 | 0 | if (is_deleted_kbnode (kb2)) |
4374 | 0 | continue; |
4375 | | |
4376 | 0 | if (kb2->pkt->pkttype != PKT_PUBLIC_SUBKEY |
4377 | 0 | && kb2->pkt->pkttype != PKT_SECRET_SUBKEY) |
4378 | 0 | continue; |
4379 | | |
4380 | 0 | if (cmp_public_keys (kb1->pkt->pkt.public_key, |
4381 | 0 | kb2->pkt->pkt.public_key)) |
4382 | 0 | continue; |
4383 | | |
4384 | | /* We have a duplicated subkey. */ |
4385 | 0 | any = 1; |
4386 | | |
4387 | | /* Take subkey-2's signatures, and attach them to subkey-1. */ |
4388 | 0 | for (last = kb2; last->next; last = last->next) |
4389 | 0 | { |
4390 | 0 | if (is_deleted_kbnode (last)) |
4391 | 0 | continue; |
4392 | | |
4393 | 0 | if (last->next->pkt->pkttype != PKT_SIGNATURE) |
4394 | 0 | break; |
4395 | 0 | } |
4396 | | |
4397 | | /* Snip out subkye-2 */ |
4398 | 0 | find_prev_kbnode (*keyblock, kb2, 0)->next = last->next; |
4399 | | |
4400 | | /* Put subkey-2 in place as part of subkey-1 */ |
4401 | 0 | last->next = kb1->next; |
4402 | 0 | kb1->next = kb2; |
4403 | 0 | delete_kbnode (kb2); |
4404 | | |
4405 | | /* Now dedupe kb1 */ |
4406 | 0 | for (sig1 = kb1->next; sig1; sig1 = sig1->next) |
4407 | 0 | { |
4408 | 0 | if (is_deleted_kbnode (sig1)) |
4409 | 0 | continue; |
4410 | | |
4411 | 0 | if (sig1->pkt->pkttype != PKT_SIGNATURE) |
4412 | 0 | break; |
4413 | | |
4414 | 0 | for (sig2 = sig1->next, last = sig1; |
4415 | 0 | sig2; |
4416 | 0 | last = sig2, sig2 = sig2->next) |
4417 | 0 | { |
4418 | 0 | if (is_deleted_kbnode (sig2)) |
4419 | 0 | continue; |
4420 | | |
4421 | 0 | if (sig2->pkt->pkttype != PKT_SIGNATURE) |
4422 | 0 | break; |
4423 | | |
4424 | 0 | if (!cmp_signatures (sig1->pkt->pkt.signature, |
4425 | 0 | sig2->pkt->pkt.signature)) |
4426 | 0 | { |
4427 | | /* We have a match, so delete the second |
4428 | | signature */ |
4429 | 0 | delete_kbnode (sig2); |
4430 | 0 | sig2 = last; |
4431 | 0 | } |
4432 | 0 | } |
4433 | 0 | } |
4434 | 0 | } |
4435 | 0 | } |
4436 | |
|
4437 | 0 | commit_kbnode (keyblock); |
4438 | |
|
4439 | 0 | if (any && !opt.quiet) |
4440 | 0 | { |
4441 | 0 | const char *key="???"; |
4442 | |
|
4443 | 0 | if ((kb1 = find_kbnode (*keyblock, PKT_PUBLIC_KEY)) ) |
4444 | 0 | key = keystr_from_pk (kb1->pkt->pkt.public_key); |
4445 | 0 | else if ((kb1 = find_kbnode (*keyblock, PKT_SECRET_KEY)) ) |
4446 | 0 | key = keystr_from_pk (kb1->pkt->pkt.public_key); |
4447 | |
|
4448 | 0 | log_info (_("key %s: duplicated subkeys detected - merged\n"), key); |
4449 | 0 | } |
4450 | |
|
4451 | 0 | return any; |
4452 | 0 | } |
4453 | | |
4454 | | |
4455 | | /* Check for a 0x20 revocation from a revocation key that is not |
4456 | | present. This may be called without the benefit of merge_xxxx so |
4457 | | you can't rely on pk->revkey and friends. */ |
4458 | | static void |
4459 | | revocation_present (ctrl_t ctrl, kbnode_t keyblock) |
4460 | 0 | { |
4461 | 0 | kbnode_t onode, inode; |
4462 | 0 | PKT_public_key *pk = keyblock->pkt->pkt.public_key; |
4463 | |
|
4464 | 0 | for(onode=keyblock->next;onode;onode=onode->next) |
4465 | 0 | { |
4466 | | /* If we reach user IDs, we're done. */ |
4467 | 0 | if(onode->pkt->pkttype==PKT_USER_ID) |
4468 | 0 | break; |
4469 | | |
4470 | 0 | if (onode->pkt->pkttype == PKT_SIGNATURE |
4471 | 0 | && IS_KEY_SIG (onode->pkt->pkt.signature) |
4472 | 0 | && onode->pkt->pkt.signature->revkey) |
4473 | 0 | { |
4474 | 0 | int idx; |
4475 | 0 | PKT_signature *sig=onode->pkt->pkt.signature; |
4476 | |
|
4477 | 0 | for(idx=0;idx<sig->numrevkeys;idx++) |
4478 | 0 | { |
4479 | 0 | u32 keyid[2]; |
4480 | |
|
4481 | 0 | keyid_from_fingerprint (ctrl, sig->revkey[idx].fpr, |
4482 | 0 | sig->revkey[idx].fprlen, keyid); |
4483 | |
|
4484 | 0 | for(inode=keyblock->next;inode;inode=inode->next) |
4485 | 0 | { |
4486 | | /* If we reach user IDs, we're done. */ |
4487 | 0 | if(inode->pkt->pkttype==PKT_USER_ID) |
4488 | 0 | break; |
4489 | | |
4490 | 0 | if (inode->pkt->pkttype == PKT_SIGNATURE |
4491 | 0 | && IS_KEY_REV (inode->pkt->pkt.signature) |
4492 | 0 | && inode->pkt->pkt.signature->keyid[0]==keyid[0] |
4493 | 0 | && inode->pkt->pkt.signature->keyid[1]==keyid[1]) |
4494 | 0 | { |
4495 | | /* Okay, we have a revocation key, and a |
4496 | | * revocation issued by it. Do we have the key |
4497 | | * itself? */ |
4498 | 0 | gpg_error_t err; |
4499 | |
|
4500 | 0 | err = get_pubkey_byfpr_fast (ctrl, NULL, |
4501 | 0 | sig->revkey[idx].fpr, |
4502 | 0 | sig->revkey[idx].fprlen); |
4503 | 0 | if (gpg_err_code (err) == GPG_ERR_NO_PUBKEY |
4504 | 0 | || gpg_err_code (err) == GPG_ERR_UNUSABLE_PUBKEY) |
4505 | 0 | { |
4506 | 0 | char *tempkeystr = xstrdup (keystr_from_pk (pk)); |
4507 | | |
4508 | | /* No, so try and get it */ |
4509 | 0 | if ((opt.keyserver_options.options |
4510 | 0 | & KEYSERVER_AUTO_KEY_RETRIEVE) |
4511 | 0 | && keyserver_any_configured (ctrl)) |
4512 | 0 | { |
4513 | 0 | log_info(_("WARNING: key %s may be revoked:" |
4514 | 0 | " fetching revocation key %s\n"), |
4515 | 0 | tempkeystr,keystr(keyid)); |
4516 | 0 | keyserver_import_fpr (ctrl, |
4517 | 0 | sig->revkey[idx].fpr, |
4518 | 0 | sig->revkey[idx].fprlen, |
4519 | 0 | opt.keyserver, 0); |
4520 | | |
4521 | | /* Do we have it now? */ |
4522 | 0 | err = get_pubkey_byfpr_fast (ctrl, NULL, |
4523 | 0 | sig->revkey[idx].fpr, |
4524 | 0 | sig->revkey[idx].fprlen); |
4525 | 0 | } |
4526 | |
|
4527 | 0 | if (gpg_err_code (err) == GPG_ERR_NO_PUBKEY |
4528 | 0 | || gpg_err_code (err) == GPG_ERR_UNUSABLE_PUBKEY) |
4529 | 0 | log_info(_("WARNING: key %s may be revoked:" |
4530 | 0 | " revocation key %s not present.\n"), |
4531 | 0 | tempkeystr,keystr(keyid)); |
4532 | |
|
4533 | 0 | xfree(tempkeystr); |
4534 | 0 | } |
4535 | 0 | } |
4536 | 0 | } |
4537 | 0 | } |
4538 | 0 | } |
4539 | 0 | } |
4540 | 0 | } |
4541 | | |
4542 | | |
4543 | | /* |
4544 | | * compare and merge the blocks |
4545 | | * |
4546 | | * o compare the signatures: If we already have this signature, check |
4547 | | * that they compare okay; if not, issue a warning and ask the user. |
4548 | | * o Simply add the signature. Can't verify here because we may not have |
4549 | | * the signature's public key yet; verification is done when putting it |
4550 | | * into the trustdb, which is done automagically as soon as this pubkey |
4551 | | * is used. |
4552 | | * Note: We indicate newly inserted packets with NODE_FLAG_A. |
4553 | | */ |
4554 | | static int |
4555 | | merge_blocks (ctrl_t ctrl, unsigned int options, |
4556 | | kbnode_t keyblock_orig, kbnode_t keyblock, |
4557 | | u32 *keyid, u32 curtime, int origin, const char *url, |
4558 | | int *n_uids, int *n_sigs, int *n_subk ) |
4559 | 0 | { |
4560 | 0 | kbnode_t onode, node; |
4561 | 0 | int rc, found; |
4562 | | |
4563 | | /* 1st: handle revocation certificates */ |
4564 | 0 | for (node=keyblock->next; node; node=node->next ) |
4565 | 0 | { |
4566 | 0 | if (node->pkt->pkttype == PKT_USER_ID ) |
4567 | 0 | break; |
4568 | 0 | else if (node->pkt->pkttype == PKT_SIGNATURE |
4569 | 0 | && IS_KEY_REV (node->pkt->pkt.signature)) |
4570 | 0 | { |
4571 | | /* check whether we already have this */ |
4572 | 0 | found = 0; |
4573 | 0 | for (onode=keyblock_orig->next; onode; onode=onode->next) |
4574 | 0 | { |
4575 | 0 | if (onode->pkt->pkttype == PKT_USER_ID ) |
4576 | 0 | break; |
4577 | 0 | else if (onode->pkt->pkttype == PKT_SIGNATURE |
4578 | 0 | && IS_KEY_REV (onode->pkt->pkt.signature) |
4579 | 0 | && !cmp_signatures(onode->pkt->pkt.signature, |
4580 | 0 | node->pkt->pkt.signature)) |
4581 | 0 | { |
4582 | 0 | found = 1; |
4583 | 0 | break; |
4584 | 0 | } |
4585 | 0 | } |
4586 | 0 | if (!found) |
4587 | 0 | { |
4588 | 0 | kbnode_t n2 = clone_kbnode(node); |
4589 | 0 | insert_kbnode( keyblock_orig, n2, 0 ); |
4590 | 0 | n2->flag |= NODE_FLAG_A; |
4591 | 0 | ++*n_sigs; |
4592 | 0 | if(!opt.quiet) |
4593 | 0 | { |
4594 | 0 | char *p = get_user_id_native (ctrl, keyid); |
4595 | 0 | log_info(_("key %s: \"%s\" revocation" |
4596 | 0 | " certificate added\n"), keystr(keyid),p); |
4597 | 0 | xfree(p); |
4598 | 0 | } |
4599 | 0 | } |
4600 | 0 | } |
4601 | 0 | } |
4602 | | |
4603 | | /* 2nd: merge in any direct key (0x1F) sigs */ |
4604 | 0 | for(node=keyblock->next; node; node=node->next) |
4605 | 0 | { |
4606 | 0 | if (node->pkt->pkttype == PKT_USER_ID ) |
4607 | 0 | break; |
4608 | 0 | else if (node->pkt->pkttype == PKT_SIGNATURE |
4609 | 0 | && IS_KEY_SIG (node->pkt->pkt.signature)) |
4610 | 0 | { |
4611 | | /* check whether we already have this */ |
4612 | 0 | found = 0; |
4613 | 0 | for (onode=keyblock_orig->next; onode; onode=onode->next) |
4614 | 0 | { |
4615 | 0 | if (onode->pkt->pkttype == PKT_USER_ID) |
4616 | 0 | break; |
4617 | 0 | else if (onode->pkt->pkttype == PKT_SIGNATURE |
4618 | 0 | && IS_KEY_SIG (onode->pkt->pkt.signature) |
4619 | 0 | && !cmp_signatures(onode->pkt->pkt.signature, |
4620 | 0 | node->pkt->pkt.signature)) |
4621 | 0 | { |
4622 | 0 | found = 1; |
4623 | 0 | break; |
4624 | 0 | } |
4625 | 0 | } |
4626 | 0 | if (!found ) |
4627 | 0 | { |
4628 | 0 | kbnode_t n2 = clone_kbnode(node); |
4629 | 0 | insert_kbnode( keyblock_orig, n2, 0 ); |
4630 | 0 | n2->flag |= NODE_FLAG_A; |
4631 | 0 | ++*n_sigs; |
4632 | 0 | if(!opt.quiet) |
4633 | 0 | log_info( _("key %s: direct key signature added\n"), |
4634 | 0 | keystr(keyid)); |
4635 | 0 | } |
4636 | 0 | } |
4637 | 0 | } |
4638 | | |
4639 | | /* 3rd: try to merge new certificates in */ |
4640 | 0 | for (onode=keyblock_orig->next; onode; onode=onode->next) |
4641 | 0 | { |
4642 | 0 | if (!(onode->flag & NODE_FLAG_A) && onode->pkt->pkttype == PKT_USER_ID) |
4643 | 0 | { |
4644 | | /* find the user id in the imported keyblock */ |
4645 | 0 | for (node=keyblock->next; node; node=node->next) |
4646 | 0 | if (node->pkt->pkttype == PKT_USER_ID |
4647 | 0 | && !cmp_user_ids( onode->pkt->pkt.user_id, |
4648 | 0 | node->pkt->pkt.user_id ) ) |
4649 | 0 | break; |
4650 | 0 | if (node ) /* found: merge */ |
4651 | 0 | { |
4652 | 0 | rc = merge_sigs (onode, node, n_sigs); |
4653 | 0 | if (rc ) |
4654 | 0 | return rc; |
4655 | 0 | } |
4656 | 0 | } |
4657 | 0 | } |
4658 | | |
4659 | | /* 4th: add new user-ids */ |
4660 | 0 | for (node=keyblock->next; node; node=node->next) |
4661 | 0 | { |
4662 | 0 | if (node->pkt->pkttype == PKT_USER_ID) |
4663 | 0 | { |
4664 | | /* do we have this in the original keyblock */ |
4665 | 0 | for (onode=keyblock_orig->next; onode; onode=onode->next ) |
4666 | 0 | if (onode->pkt->pkttype == PKT_USER_ID |
4667 | 0 | && !cmp_user_ids( onode->pkt->pkt.user_id, |
4668 | 0 | node->pkt->pkt.user_id ) ) |
4669 | 0 | break; |
4670 | 0 | if (!onode ) /* this is a new user id: append */ |
4671 | 0 | { |
4672 | 0 | rc = append_new_uid (options, keyblock_orig, node, |
4673 | 0 | curtime, origin, url, n_sigs); |
4674 | 0 | if (rc ) |
4675 | 0 | return rc; |
4676 | 0 | ++*n_uids; |
4677 | 0 | } |
4678 | 0 | } |
4679 | 0 | } |
4680 | | |
4681 | | /* 5th: add new subkeys */ |
4682 | 0 | for (node=keyblock->next; node; node=node->next) |
4683 | 0 | { |
4684 | 0 | onode = NULL; |
4685 | 0 | if (node->pkt->pkttype == PKT_PUBLIC_SUBKEY) |
4686 | 0 | { |
4687 | | /* do we have this in the original keyblock? */ |
4688 | 0 | for(onode=keyblock_orig->next; onode; onode=onode->next) |
4689 | 0 | if (onode->pkt->pkttype == PKT_PUBLIC_SUBKEY |
4690 | 0 | && !cmp_public_keys( onode->pkt->pkt.public_key, |
4691 | 0 | node->pkt->pkt.public_key)) |
4692 | 0 | break; |
4693 | 0 | if (!onode ) /* This is a new subkey: append. */ |
4694 | 0 | { |
4695 | 0 | rc = append_key (keyblock_orig, node, n_sigs); |
4696 | 0 | if (rc) |
4697 | 0 | return rc; |
4698 | 0 | ++*n_subk; |
4699 | 0 | } |
4700 | 0 | } |
4701 | 0 | else if (node->pkt->pkttype == PKT_SECRET_SUBKEY) |
4702 | 0 | { |
4703 | | /* do we have this in the original keyblock? */ |
4704 | 0 | for (onode=keyblock_orig->next; onode; onode=onode->next ) |
4705 | 0 | if (onode->pkt->pkttype == PKT_SECRET_SUBKEY |
4706 | 0 | && !cmp_public_keys (onode->pkt->pkt.public_key, |
4707 | 0 | node->pkt->pkt.public_key) ) |
4708 | 0 | break; |
4709 | 0 | if (!onode ) /* This is a new subkey: append. */ |
4710 | 0 | { |
4711 | 0 | rc = append_key (keyblock_orig, node, n_sigs); |
4712 | 0 | if (rc ) |
4713 | 0 | return rc; |
4714 | 0 | ++*n_subk; |
4715 | 0 | } |
4716 | 0 | } |
4717 | 0 | } |
4718 | | |
4719 | | /* 6th: merge subkey certificates */ |
4720 | 0 | for (onode=keyblock_orig->next; onode; onode=onode->next) |
4721 | 0 | { |
4722 | 0 | if (!(onode->flag & NODE_FLAG_A) |
4723 | 0 | && (onode->pkt->pkttype == PKT_PUBLIC_SUBKEY |
4724 | 0 | || onode->pkt->pkttype == PKT_SECRET_SUBKEY)) |
4725 | 0 | { |
4726 | | /* find the subkey in the imported keyblock */ |
4727 | 0 | for(node=keyblock->next; node; node=node->next) |
4728 | 0 | { |
4729 | 0 | if ((node->pkt->pkttype == PKT_PUBLIC_SUBKEY |
4730 | 0 | || node->pkt->pkttype == PKT_SECRET_SUBKEY) |
4731 | 0 | && !cmp_public_keys( onode->pkt->pkt.public_key, |
4732 | 0 | node->pkt->pkt.public_key ) ) |
4733 | 0 | break; |
4734 | 0 | } |
4735 | 0 | if (node) /* Found: merge. */ |
4736 | 0 | { |
4737 | 0 | rc = merge_keysigs( onode, node, n_sigs); |
4738 | 0 | if (rc ) |
4739 | 0 | return rc; |
4740 | 0 | } |
4741 | 0 | } |
4742 | 0 | } |
4743 | | |
4744 | 0 | return 0; |
4745 | 0 | } |
4746 | | |
4747 | | |
4748 | | /* Helper function for merge_blocks. |
4749 | | * |
4750 | | * Append the new userid starting with NODE and all signatures to |
4751 | | * KEYBLOCK. ORIGIN and URL conveys the usual key origin info. The |
4752 | | * integer at N_SIGS is updated with the number of new signatures. |
4753 | | */ |
4754 | | static gpg_error_t |
4755 | | append_new_uid (unsigned int options, |
4756 | | kbnode_t keyblock, kbnode_t node, u32 curtime, |
4757 | | int origin, const char *url, int *n_sigs) |
4758 | 0 | { |
4759 | 0 | gpg_error_t err; |
4760 | 0 | kbnode_t n; |
4761 | 0 | kbnode_t n_where = NULL; |
4762 | |
|
4763 | 0 | log_assert (node->pkt->pkttype == PKT_USER_ID); |
4764 | | |
4765 | | /* Find the right position for the new user id and its signatures. */ |
4766 | 0 | for (n = keyblock; n; n_where = n, n = n->next) |
4767 | 0 | { |
4768 | 0 | if (n->pkt->pkttype == PKT_PUBLIC_SUBKEY |
4769 | 0 | || n->pkt->pkttype == PKT_SECRET_SUBKEY ) |
4770 | 0 | break; |
4771 | 0 | } |
4772 | 0 | if (!n) |
4773 | 0 | n_where = NULL; |
4774 | | |
4775 | | /* and append/insert */ |
4776 | 0 | while (node) |
4777 | 0 | { |
4778 | | /* we add a clone to the original keyblock, because this |
4779 | | * one is released first. */ |
4780 | 0 | n = clone_kbnode(node); |
4781 | 0 | if (n->pkt->pkttype == PKT_USER_ID |
4782 | 0 | && !(options & IMPORT_RESTORE) ) |
4783 | 0 | { |
4784 | 0 | err = insert_key_origin_uid (n->pkt->pkt.user_id, |
4785 | 0 | curtime, origin, url); |
4786 | 0 | if (err) |
4787 | 0 | { |
4788 | 0 | release_kbnode (n); |
4789 | 0 | return err; |
4790 | 0 | } |
4791 | 0 | } |
4792 | | |
4793 | 0 | if (n_where) |
4794 | 0 | { |
4795 | 0 | insert_kbnode( n_where, n, 0 ); |
4796 | 0 | n_where = n; |
4797 | 0 | } |
4798 | 0 | else |
4799 | 0 | add_kbnode( keyblock, n ); |
4800 | 0 | n->flag |= NODE_FLAG_A; |
4801 | 0 | node->flag |= NODE_FLAG_A; |
4802 | 0 | if (n->pkt->pkttype == PKT_SIGNATURE ) |
4803 | 0 | ++*n_sigs; |
4804 | |
|
4805 | 0 | node = node->next; |
4806 | 0 | if (node && node->pkt->pkttype != PKT_SIGNATURE ) |
4807 | 0 | break; |
4808 | 0 | } |
4809 | | |
4810 | 0 | return 0; |
4811 | 0 | } |
4812 | | |
4813 | | |
4814 | | /* Helper function for merge_blocks |
4815 | | * Merge the sigs from SRC onto DST. SRC and DST are both a PKT_USER_ID. |
4816 | | * (how should we handle comment packets here?) |
4817 | | */ |
4818 | | static int |
4819 | | merge_sigs (kbnode_t dst, kbnode_t src, int *n_sigs) |
4820 | 0 | { |
4821 | 0 | kbnode_t n, n2; |
4822 | 0 | int found = 0; |
4823 | |
|
4824 | 0 | log_assert (dst->pkt->pkttype == PKT_USER_ID); |
4825 | 0 | log_assert (src->pkt->pkttype == PKT_USER_ID); |
4826 | | |
4827 | 0 | for (n=src->next; n && n->pkt->pkttype != PKT_USER_ID; n = n->next) |
4828 | 0 | { |
4829 | 0 | if (n->pkt->pkttype != PKT_SIGNATURE ) |
4830 | 0 | continue; |
4831 | 0 | if (IS_SUBKEY_SIG (n->pkt->pkt.signature) |
4832 | 0 | || IS_SUBKEY_REV (n->pkt->pkt.signature) ) |
4833 | 0 | continue; /* skip signatures which are only valid on subkeys */ |
4834 | | |
4835 | 0 | found = 0; |
4836 | 0 | for (n2=dst->next; n2 && n2->pkt->pkttype != PKT_USER_ID; n2 = n2->next) |
4837 | 0 | if (!cmp_signatures(n->pkt->pkt.signature,n2->pkt->pkt.signature)) |
4838 | 0 | { |
4839 | 0 | found++; |
4840 | 0 | break; |
4841 | 0 | } |
4842 | 0 | if (!found ) |
4843 | 0 | { |
4844 | | /* This signature is new or newer, append N to DST. |
4845 | | * We add a clone to the original keyblock, because this |
4846 | | * one is released first */ |
4847 | 0 | n2 = clone_kbnode(n); |
4848 | 0 | insert_kbnode( dst, n2, PKT_SIGNATURE ); |
4849 | 0 | n2->flag |= NODE_FLAG_A; |
4850 | 0 | n->flag |= NODE_FLAG_A; |
4851 | 0 | ++*n_sigs; |
4852 | 0 | } |
4853 | 0 | } |
4854 | |
|
4855 | 0 | return 0; |
4856 | 0 | } |
4857 | | |
4858 | | |
4859 | | /* Helper function for merge_blocks |
4860 | | * Merge the sigs from SRC onto DST. SRC and DST are both a PKT_xxx_SUBKEY. |
4861 | | */ |
4862 | | static int |
4863 | | merge_keysigs (kbnode_t dst, kbnode_t src, int *n_sigs) |
4864 | 0 | { |
4865 | 0 | kbnode_t n, n2; |
4866 | 0 | int found = 0; |
4867 | |
|
4868 | 0 | log_assert (dst->pkt->pkttype == PKT_PUBLIC_SUBKEY |
4869 | 0 | || dst->pkt->pkttype == PKT_SECRET_SUBKEY); |
4870 | | |
4871 | 0 | for (n=src->next; n ; n = n->next) |
4872 | 0 | { |
4873 | 0 | if (n->pkt->pkttype == PKT_PUBLIC_SUBKEY |
4874 | 0 | || n->pkt->pkttype == PKT_PUBLIC_KEY ) |
4875 | 0 | break; |
4876 | 0 | if (n->pkt->pkttype != PKT_SIGNATURE ) |
4877 | 0 | continue; |
4878 | | |
4879 | 0 | found = 0; |
4880 | 0 | for (n2=dst->next; n2; n2 = n2->next) |
4881 | 0 | { |
4882 | 0 | if (n2->pkt->pkttype == PKT_PUBLIC_SUBKEY |
4883 | 0 | || n2->pkt->pkttype == PKT_PUBLIC_KEY ) |
4884 | 0 | break; |
4885 | 0 | if (n2->pkt->pkttype == PKT_SIGNATURE |
4886 | 0 | && (n->pkt->pkt.signature->keyid[0] |
4887 | 0 | == n2->pkt->pkt.signature->keyid[0]) |
4888 | 0 | && (n->pkt->pkt.signature->keyid[1] |
4889 | 0 | == n2->pkt->pkt.signature->keyid[1]) |
4890 | 0 | && (n->pkt->pkt.signature->timestamp |
4891 | 0 | <= n2->pkt->pkt.signature->timestamp) |
4892 | 0 | && (n->pkt->pkt.signature->sig_class |
4893 | 0 | == n2->pkt->pkt.signature->sig_class)) |
4894 | 0 | { |
4895 | 0 | found++; |
4896 | 0 | break; |
4897 | 0 | } |
4898 | 0 | } |
4899 | 0 | if (!found ) |
4900 | 0 | { |
4901 | | /* This signature is new or newer, append N to DST. |
4902 | | * We add a clone to the original keyblock, because this |
4903 | | * one is released first */ |
4904 | 0 | n2 = clone_kbnode(n); |
4905 | 0 | insert_kbnode( dst, n2, PKT_SIGNATURE ); |
4906 | 0 | n2->flag |= NODE_FLAG_A; |
4907 | 0 | n->flag |= NODE_FLAG_A; |
4908 | 0 | ++*n_sigs; |
4909 | 0 | } |
4910 | 0 | } |
4911 | |
|
4912 | 0 | return 0; |
4913 | 0 | } |
4914 | | |
4915 | | |
4916 | | /* Helper function for merge_blocks. |
4917 | | * Append the subkey starting with NODE and all signatures to KEYBLOCK. |
4918 | | * Mark all new and copied packets by setting flag bit 0. |
4919 | | */ |
4920 | | static int |
4921 | | append_key (kbnode_t keyblock, kbnode_t node, int *n_sigs) |
4922 | 0 | { |
4923 | 0 | kbnode_t n; |
4924 | |
|
4925 | 0 | log_assert (node->pkt->pkttype == PKT_PUBLIC_SUBKEY |
4926 | 0 | || node->pkt->pkttype == PKT_SECRET_SUBKEY); |
4927 | | |
4928 | 0 | while (node) |
4929 | 0 | { |
4930 | | /* we add a clone to the original keyblock, because this |
4931 | | * one is released first */ |
4932 | 0 | n = clone_kbnode(node); |
4933 | 0 | add_kbnode( keyblock, n ); |
4934 | 0 | n->flag |= NODE_FLAG_A; |
4935 | 0 | node->flag |= NODE_FLAG_A; |
4936 | 0 | if (n->pkt->pkttype == PKT_SIGNATURE ) |
4937 | 0 | ++*n_sigs; |
4938 | |
|
4939 | 0 | node = node->next; |
4940 | 0 | if (node && node->pkt->pkttype != PKT_SIGNATURE ) |
4941 | 0 | break; |
4942 | 0 | } |
4943 | |
|
4944 | 0 | return 0; |
4945 | 0 | } |