Line | Count | Source (jump to first uncovered line) |
1 | | /* export.c - Export keys in the OpenPGP defined format. |
2 | | * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, |
3 | | * 2005, 2010 Free Software Foundation, Inc. |
4 | | * Copyright (C) 1998-2016 Werner Koch |
5 | | * Copyright (C) 2022 g10 Code GmbH |
6 | | * |
7 | | * This file is part of GnuPG. |
8 | | * |
9 | | * GnuPG is free software; you can redistribute it and/or modify |
10 | | * it under the terms of the GNU General Public License as published by |
11 | | * the Free Software Foundation; either version 3 of the License, or |
12 | | * (at your option) any later version. |
13 | | * |
14 | | * GnuPG is distributed in the hope that it will be useful, |
15 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17 | | * GNU General Public License for more details. |
18 | | * |
19 | | * You should have received a copy of the GNU General Public License |
20 | | * along with this program; if not, see <https://www.gnu.org/licenses/>. |
21 | | */ |
22 | | |
23 | | #include <config.h> |
24 | | #include <stdio.h> |
25 | | #include <stdlib.h> |
26 | | #include <string.h> |
27 | | #include <errno.h> |
28 | | |
29 | | #include "gpg.h" |
30 | | #include "options.h" |
31 | | #include "packet.h" |
32 | | #include "../common/status.h" |
33 | | #include "keydb.h" |
34 | | #include "../common/util.h" |
35 | | #include "main.h" |
36 | | #include "../common/i18n.h" |
37 | | #include "../common/membuf.h" |
38 | | #include "../common/host2net.h" |
39 | | #include "../common/zb32.h" |
40 | | #include "../common/recsel.h" |
41 | | #include "../common/mbox-util.h" |
42 | | #include "../common/init.h" |
43 | | #include "trustdb.h" |
44 | | #include "call-agent.h" |
45 | | #include "key-clean.h" |
46 | | #include "pkglue.h" |
47 | | |
48 | | |
49 | | /* An object to keep track of subkeys. */ |
50 | | struct subkey_list_s |
51 | | { |
52 | | struct subkey_list_s *next; |
53 | | u32 kid[2]; |
54 | | }; |
55 | | typedef struct subkey_list_s *subkey_list_t; |
56 | | |
57 | | |
58 | | /* An object to track statistics for export operations. */ |
59 | | struct export_stats_s |
60 | | { |
61 | | ulong count; /* Number of processed keys. */ |
62 | | ulong secret_count; /* Number of secret keys seen. */ |
63 | | ulong exported; /* Number of actual exported keys. */ |
64 | | }; |
65 | | |
66 | | |
67 | | /* Global variables to store the selectors created from |
68 | | * --export-filter keep-uid=EXPR. |
69 | | * --export-filter drop-subkey=EXPR. |
70 | | * --export-filter select=EXPR. |
71 | | * |
72 | | * FIXME: We should put this into the CTRL object but that requires a |
73 | | * lot more changes right now. |
74 | | */ |
75 | | static recsel_expr_t export_keep_uid; |
76 | | static recsel_expr_t export_drop_subkey; |
77 | | static recsel_expr_t export_select_filter; |
78 | | |
79 | | |
80 | | /* An object used for a linked list to implement the |
81 | | * push_export_filter/pop_export_filters functions. */ |
82 | | struct export_filter_attic_s |
83 | | { |
84 | | struct export_filter_attic_s *next; |
85 | | recsel_expr_t export_keep_uid; |
86 | | recsel_expr_t export_drop_subkey; |
87 | | recsel_expr_t export_select_filter; |
88 | | }; |
89 | | static struct export_filter_attic_s *export_filter_attic; |
90 | | |
91 | | |
92 | | |
93 | | /* Local prototypes. */ |
94 | | static int do_export (ctrl_t ctrl, strlist_t users, int secret, |
95 | | unsigned int options, export_stats_t stats); |
96 | | static int do_export_stream (ctrl_t ctrl, iobuf_t out, |
97 | | strlist_t users, int secret, |
98 | | kbnode_t *keyblock_out, unsigned int options, |
99 | | export_stats_t stats, int *any); |
100 | | static gpg_error_t print_dane_records |
101 | | /**/ (iobuf_t out, kbnode_t keyblock, PKT_public_key *pk, |
102 | | const void *data, size_t datalen); |
103 | | |
104 | | |
105 | | static void |
106 | | cleanup_export_globals (void) |
107 | 0 | { |
108 | 0 | recsel_release (export_keep_uid); |
109 | 0 | export_keep_uid = NULL; |
110 | 0 | recsel_release (export_drop_subkey); |
111 | 0 | export_drop_subkey = NULL; |
112 | 0 | recsel_release (export_select_filter); |
113 | 0 | export_select_filter = NULL; |
114 | 0 | } |
115 | | |
116 | | |
117 | | /* Option parser for export options. See parse_options for |
118 | | details. */ |
119 | | int |
120 | | parse_export_options(char *str,unsigned int *options,int noisy) |
121 | 0 | { |
122 | 0 | struct parse_options export_opts[]= |
123 | 0 | { |
124 | 0 | {"export-local-sigs",EXPORT_LOCAL_SIGS,NULL, |
125 | 0 | N_("export signatures that are marked as local-only")}, |
126 | 0 | {"export-attributes",EXPORT_ATTRIBUTES,NULL, |
127 | 0 | N_("export attribute user IDs (generally photo IDs)")}, |
128 | 0 | {"export-sensitive-revkeys",EXPORT_SENSITIVE_REVKEYS,NULL, |
129 | 0 | N_("export revocation keys marked as \"sensitive\"")}, |
130 | 0 | {"export-clean",EXPORT_CLEAN,NULL, |
131 | 0 | N_("remove unusable parts from key during export")}, |
132 | 0 | {"export-minimal",EXPORT_MINIMAL|EXPORT_CLEAN,NULL, |
133 | 0 | N_("remove as much as possible from key during export")}, |
134 | |
|
135 | 0 | {"export-dane", EXPORT_DANE_FORMAT, NULL, NULL }, |
136 | |
|
137 | 0 | {"export-revocs", EXPORT_REVOCS, NULL, |
138 | 0 | N_("export only revocation certificates") }, |
139 | |
|
140 | 0 | {"backup", EXPORT_BACKUP, NULL, |
141 | 0 | N_("use the GnuPG key backup format")}, |
142 | 0 | {"export-backup", EXPORT_BACKUP, NULL, NULL }, |
143 | |
|
144 | 0 | {"mode1003", EXPORT_MODE1003, NULL, |
145 | 0 | N_("export secret keys using the GnuPG format") }, |
146 | | |
147 | | /* Aliases for backward compatibility */ |
148 | 0 | {"include-local-sigs",EXPORT_LOCAL_SIGS,NULL,NULL}, |
149 | 0 | {"include-attributes",EXPORT_ATTRIBUTES,NULL,NULL}, |
150 | 0 | {"include-sensitive-revkeys",EXPORT_SENSITIVE_REVKEYS,NULL,NULL}, |
151 | | /* dummy */ |
152 | 0 | {"export-unusable-sigs",0,NULL,NULL}, |
153 | 0 | {"export-clean-sigs",0,NULL,NULL}, |
154 | 0 | {"export-clean-uids",0,NULL,NULL}, |
155 | 0 | {NULL,0,NULL,NULL} |
156 | | /* add tags for include revoked and disabled? */ |
157 | 0 | }; |
158 | 0 | int rc; |
159 | |
|
160 | 0 | rc = parse_options (str, options, export_opts, noisy); |
161 | 0 | if (!rc) |
162 | 0 | return 0; |
163 | | |
164 | | /* Alter other options we want or don't want for restore. */ |
165 | 0 | if ((*options & EXPORT_BACKUP)) |
166 | 0 | { |
167 | 0 | *options |= (EXPORT_LOCAL_SIGS | EXPORT_ATTRIBUTES |
168 | 0 | | EXPORT_SENSITIVE_REVKEYS); |
169 | 0 | *options &= ~(EXPORT_CLEAN | EXPORT_MINIMAL |
170 | 0 | | EXPORT_DANE_FORMAT); |
171 | 0 | } |
172 | |
|
173 | 0 | return rc; |
174 | 0 | } |
175 | | |
176 | | |
177 | | /* Parse and set an export filter from string. STRING has the format |
178 | | * "NAME=EXPR" with NAME being the name of the filter. Spaces before |
179 | | * and after NAME are not allowed. If this function is called several |
180 | | * times all expressions for the same NAME are concatenated. |
181 | | * Supported filter names are: |
182 | | * |
183 | | * - keep-uid :: If the expression evaluates to true for a certain |
184 | | * user ID packet, that packet and all it dependencies |
185 | | * will be exported. The expression may use these |
186 | | * variables: |
187 | | * |
188 | | * - uid :: The entire user ID. |
189 | | * - mbox :: The mail box part of the user ID. |
190 | | * - primary :: Evaluate to true for the primary user ID. |
191 | | * |
192 | | * - drop-subkey :: If the expression evaluates to true for a subkey |
193 | | * packet that subkey and all it dependencies will be |
194 | | * remove from the keyblock. The expression may use these |
195 | | * variables: |
196 | | * |
197 | | * - secret :: 1 for a secret subkey, else 0. |
198 | | * - key_algo :: Public key algorithm id |
199 | | * |
200 | | * - select :: The key is only exported if the filter returns true. |
201 | | */ |
202 | | gpg_error_t |
203 | | parse_and_set_export_filter (const char *string) |
204 | 0 | { |
205 | 0 | gpg_error_t err; |
206 | | |
207 | | /* Auto register the cleanup function. */ |
208 | 0 | register_mem_cleanup_func (cleanup_export_globals); |
209 | |
|
210 | 0 | if (!strncmp (string, "keep-uid=", 9)) |
211 | 0 | err = recsel_parse_expr (&export_keep_uid, string+9); |
212 | 0 | else if (!strncmp (string, "drop-subkey=", 12)) |
213 | 0 | err = recsel_parse_expr (&export_drop_subkey, string+12); |
214 | 0 | else if (!strncmp (string, "select=", 7)) |
215 | 0 | err = recsel_parse_expr (&export_select_filter, string+7); |
216 | 0 | else |
217 | 0 | err = gpg_error (GPG_ERR_INV_NAME); |
218 | |
|
219 | 0 | return err; |
220 | 0 | } |
221 | | |
222 | | |
223 | | /* Push the current export filters onto a stack so that new export |
224 | | * filters can be defined which will be active until the next |
225 | | * pop_export_filters or another push_export_filters. */ |
226 | | void |
227 | | push_export_filters (void) |
228 | 0 | { |
229 | 0 | struct export_filter_attic_s *item; |
230 | |
|
231 | 0 | item = xcalloc (1, sizeof *item); |
232 | 0 | item->export_keep_uid = export_keep_uid; |
233 | 0 | export_keep_uid = NULL; |
234 | 0 | item->export_drop_subkey = export_drop_subkey; |
235 | 0 | export_drop_subkey = NULL; |
236 | 0 | item->export_select_filter = export_select_filter; |
237 | 0 | export_select_filter = NULL; |
238 | 0 | item->next = export_filter_attic; |
239 | 0 | export_filter_attic = item; |
240 | 0 | } |
241 | | |
242 | | |
243 | | /* Revert the last push_export_filters. */ |
244 | | void |
245 | | pop_export_filters (void) |
246 | 0 | { |
247 | 0 | struct export_filter_attic_s *item; |
248 | |
|
249 | 0 | item = export_filter_attic; |
250 | 0 | if (!item) |
251 | 0 | BUG (); /* No corresponding push. */ |
252 | 0 | export_filter_attic = item->next; |
253 | 0 | cleanup_export_globals (); |
254 | 0 | export_keep_uid = item->export_keep_uid; |
255 | 0 | export_drop_subkey = item->export_drop_subkey; |
256 | 0 | export_select_filter = item->export_select_filter; |
257 | 0 | } |
258 | | |
259 | | |
260 | | |
261 | | /* Create a new export stats object initialized to zero. On error |
262 | | returns NULL and sets ERRNO. */ |
263 | | export_stats_t |
264 | | export_new_stats (void) |
265 | 0 | { |
266 | 0 | export_stats_t stats; |
267 | |
|
268 | 0 | return xtrycalloc (1, sizeof *stats); |
269 | 0 | } |
270 | | |
271 | | |
272 | | /* Release an export stats object. */ |
273 | | void |
274 | | export_release_stats (export_stats_t stats) |
275 | 0 | { |
276 | 0 | xfree (stats); |
277 | 0 | } |
278 | | |
279 | | |
280 | | /* Print export statistics using the status interface. */ |
281 | | void |
282 | | export_print_stats (export_stats_t stats) |
283 | 0 | { |
284 | 0 | if (!stats) |
285 | 0 | return; |
286 | | |
287 | 0 | if (is_status_enabled ()) |
288 | 0 | { |
289 | 0 | char buf[15*20]; |
290 | |
|
291 | 0 | snprintf (buf, sizeof buf, "%lu %lu %lu", |
292 | 0 | stats->count, |
293 | 0 | stats->secret_count, |
294 | 0 | stats->exported ); |
295 | 0 | write_status_text (STATUS_EXPORT_RES, buf); |
296 | 0 | } |
297 | 0 | } |
298 | | |
299 | | |
300 | | /* |
301 | | * Export public keys (to stdout or to --output FILE). |
302 | | * |
303 | | * Depending on opt.armor the output is armored. OPTIONS are defined |
304 | | * in main.h. If USERS is NULL, all keys will be exported. STATS is |
305 | | * either an export stats object for update or NULL. |
306 | | * |
307 | | * This function is the core of "gpg --export". |
308 | | */ |
309 | | int |
310 | | export_pubkeys (ctrl_t ctrl, strlist_t users, unsigned int options, |
311 | | export_stats_t stats) |
312 | 0 | { |
313 | 0 | return do_export (ctrl, users, 0, options, stats); |
314 | 0 | } |
315 | | |
316 | | |
317 | | /* |
318 | | * Export secret keys (to stdout or to --output FILE). |
319 | | * |
320 | | * Depending on opt.armor the output is armored. OPTIONS are defined |
321 | | * in main.h. If USERS is NULL, all secret keys will be exported. |
322 | | * STATS is either an export stats object for update or NULL. |
323 | | * |
324 | | * This function is the core of "gpg --export-secret-keys". |
325 | | */ |
326 | | int |
327 | | export_seckeys (ctrl_t ctrl, strlist_t users, unsigned int options, |
328 | | export_stats_t stats) |
329 | 0 | { |
330 | 0 | return do_export (ctrl, users, 1, options, stats); |
331 | 0 | } |
332 | | |
333 | | |
334 | | /* |
335 | | * Export secret sub keys (to stdout or to --output FILE). |
336 | | * |
337 | | * This is the same as export_seckeys but replaces the primary key by |
338 | | * a stub key. Depending on opt.armor the output is armored. OPTIONS |
339 | | * are defined in main.h. If USERS is NULL, all secret subkeys will |
340 | | * be exported. STATS is either an export stats object for update or |
341 | | * NULL. |
342 | | * |
343 | | * This function is the core of "gpg --export-secret-subkeys". |
344 | | */ |
345 | | int |
346 | | export_secsubkeys (ctrl_t ctrl, strlist_t users, unsigned int options, |
347 | | export_stats_t stats) |
348 | 0 | { |
349 | 0 | return do_export (ctrl, users, 2, options, stats); |
350 | 0 | } |
351 | | |
352 | | |
353 | | /* |
354 | | * Export a single key into a memory buffer. STATS is either an |
355 | | * export stats object for update or NULL. If PREFIX is not NULL |
356 | | * PREFIXLEN bytes from PREFIX are prepended to the R_DATA. |
357 | | */ |
358 | | gpg_error_t |
359 | | export_pubkey_buffer (ctrl_t ctrl, const char *keyspec, unsigned int options, |
360 | | const void *prefix, size_t prefixlen, |
361 | | export_stats_t stats, |
362 | | kbnode_t *r_keyblock, void **r_data, size_t *r_datalen) |
363 | 0 | { |
364 | 0 | gpg_error_t err; |
365 | 0 | iobuf_t iobuf; |
366 | 0 | int any; |
367 | 0 | strlist_t helplist; |
368 | |
|
369 | 0 | *r_keyblock = NULL; |
370 | 0 | *r_data = NULL; |
371 | 0 | *r_datalen = 0; |
372 | |
|
373 | 0 | helplist = NULL; |
374 | 0 | if (!add_to_strlist_try (&helplist, keyspec)) |
375 | 0 | return gpg_error_from_syserror (); |
376 | | |
377 | 0 | iobuf = iobuf_temp (); |
378 | 0 | if (prefix && prefixlen) |
379 | 0 | iobuf_write (iobuf, prefix, prefixlen); |
380 | 0 | err = do_export_stream (ctrl, iobuf, helplist, 0, r_keyblock, options, |
381 | 0 | stats, &any); |
382 | 0 | if (!err && !any) |
383 | 0 | err = gpg_error (GPG_ERR_NOT_FOUND); |
384 | 0 | if (!err) |
385 | 0 | { |
386 | 0 | const void *src; |
387 | 0 | size_t datalen; |
388 | |
|
389 | 0 | iobuf_flush_temp (iobuf); |
390 | 0 | src = iobuf_get_temp_buffer (iobuf); |
391 | 0 | datalen = iobuf_get_temp_length (iobuf); |
392 | 0 | if (!datalen) |
393 | 0 | err = gpg_error (GPG_ERR_NO_PUBKEY); |
394 | 0 | else if (!(*r_data = xtrymalloc (datalen))) |
395 | 0 | err = gpg_error_from_syserror (); |
396 | 0 | else |
397 | 0 | { |
398 | 0 | memcpy (*r_data, src, datalen); |
399 | 0 | *r_datalen = datalen; |
400 | 0 | } |
401 | 0 | } |
402 | 0 | iobuf_close (iobuf); |
403 | 0 | free_strlist (helplist); |
404 | 0 | if (err && *r_keyblock) |
405 | 0 | { |
406 | 0 | release_kbnode (*r_keyblock); |
407 | 0 | *r_keyblock = NULL; |
408 | 0 | } |
409 | 0 | return err; |
410 | 0 | } |
411 | | |
412 | | |
413 | | /* Export the keys identified by the list of strings in USERS. If |
414 | | Secret is false public keys will be exported. With secret true |
415 | | secret keys will be exported; in this case 1 means the entire |
416 | | secret keyblock and 2 only the subkeys. OPTIONS are the export |
417 | | options to apply. */ |
418 | | static int |
419 | | do_export (ctrl_t ctrl, strlist_t users, int secret, unsigned int options, |
420 | | export_stats_t stats) |
421 | 0 | { |
422 | 0 | IOBUF out = NULL; |
423 | 0 | int any, rc; |
424 | 0 | armor_filter_context_t *afx = NULL; |
425 | 0 | compress_filter_context_t zfx; |
426 | |
|
427 | 0 | memset( &zfx, 0, sizeof zfx); |
428 | |
|
429 | 0 | rc = open_outfile (-1, NULL, 0, !!secret, &out ); |
430 | 0 | if (rc) |
431 | 0 | return rc; |
432 | | |
433 | 0 | if ( opt.armor && !(options & EXPORT_DANE_FORMAT) ) |
434 | 0 | { |
435 | 0 | afx = new_armor_context (); |
436 | 0 | afx->what = secret? 5 : 1; |
437 | 0 | push_armor_filter (afx, out); |
438 | 0 | } |
439 | |
|
440 | 0 | rc = do_export_stream (ctrl, out, users, secret, NULL, options, stats, &any); |
441 | |
|
442 | 0 | if ( rc || !any ) |
443 | 0 | iobuf_cancel (out); |
444 | 0 | else |
445 | 0 | iobuf_close (out); |
446 | 0 | release_armor_context (afx); |
447 | 0 | return rc; |
448 | 0 | } |
449 | | |
450 | | |
451 | | |
452 | | /* Release an entire subkey list. */ |
453 | | static void |
454 | | release_subkey_list (subkey_list_t list) |
455 | 0 | { |
456 | 0 | while (list) |
457 | 0 | { |
458 | 0 | subkey_list_t tmp = list->next;; |
459 | 0 | xfree (list); |
460 | 0 | list = tmp; |
461 | 0 | } |
462 | 0 | } |
463 | | |
464 | | |
465 | | /* Returns true if NODE is a subkey and contained in LIST. */ |
466 | | static int |
467 | | subkey_in_list_p (subkey_list_t list, KBNODE node) |
468 | 0 | { |
469 | 0 | if (node->pkt->pkttype == PKT_PUBLIC_SUBKEY |
470 | 0 | || node->pkt->pkttype == PKT_SECRET_SUBKEY ) |
471 | 0 | { |
472 | 0 | u32 kid[2]; |
473 | |
|
474 | 0 | keyid_from_pk (node->pkt->pkt.public_key, kid); |
475 | |
|
476 | 0 | for (; list; list = list->next) |
477 | 0 | if (list->kid[0] == kid[0] && list->kid[1] == kid[1]) |
478 | 0 | return 1; |
479 | 0 | } |
480 | 0 | return 0; |
481 | 0 | } |
482 | | |
483 | | /* Allocate a new subkey list item from NODE. */ |
484 | | static subkey_list_t |
485 | | new_subkey_list_item (KBNODE node) |
486 | 0 | { |
487 | 0 | subkey_list_t list = xcalloc (1, sizeof *list); |
488 | |
|
489 | 0 | if (node->pkt->pkttype == PKT_PUBLIC_SUBKEY |
490 | 0 | || node->pkt->pkttype == PKT_SECRET_SUBKEY) |
491 | 0 | keyid_from_pk (node->pkt->pkt.public_key, list->kid); |
492 | |
|
493 | 0 | return list; |
494 | 0 | } |
495 | | |
496 | | |
497 | | /* Helper function to check whether the subkey at NODE actually |
498 | | matches the description at DESC. The function returns true if the |
499 | | key under question has been specified by an exact specification |
500 | | (keyID or fingerprint) and does match the one at NODE. It is |
501 | | assumed that the packet at NODE is either a public or secret |
502 | | subkey. */ |
503 | | int |
504 | | exact_subkey_match_p (KEYDB_SEARCH_DESC *desc, kbnode_t node) |
505 | 0 | { |
506 | 0 | u32 kid[2]; |
507 | 0 | byte fpr[MAX_FINGERPRINT_LEN]; |
508 | 0 | size_t fprlen; |
509 | 0 | int result = 0; |
510 | |
|
511 | 0 | switch(desc->mode) |
512 | 0 | { |
513 | 0 | case KEYDB_SEARCH_MODE_SHORT_KID: |
514 | 0 | case KEYDB_SEARCH_MODE_LONG_KID: |
515 | 0 | keyid_from_pk (node->pkt->pkt.public_key, kid); |
516 | 0 | break; |
517 | | |
518 | 0 | case KEYDB_SEARCH_MODE_FPR: |
519 | 0 | fingerprint_from_pk (node->pkt->pkt.public_key, fpr, &fprlen); |
520 | 0 | break; |
521 | | |
522 | 0 | default: |
523 | 0 | break; |
524 | 0 | } |
525 | | |
526 | 0 | switch(desc->mode) |
527 | 0 | { |
528 | 0 | case KEYDB_SEARCH_MODE_SHORT_KID: |
529 | 0 | if (desc->u.kid[1] == kid[1]) |
530 | 0 | result = 1; |
531 | 0 | break; |
532 | | |
533 | 0 | case KEYDB_SEARCH_MODE_LONG_KID: |
534 | 0 | if (desc->u.kid[0] == kid[0] && desc->u.kid[1] == kid[1]) |
535 | 0 | result = 1; |
536 | 0 | break; |
537 | | |
538 | 0 | case KEYDB_SEARCH_MODE_FPR: |
539 | 0 | if (fprlen == desc->fprlen && !memcmp (desc->u.fpr, fpr, desc->fprlen)) |
540 | 0 | result = 1; |
541 | 0 | break; |
542 | | |
543 | 0 | default: |
544 | 0 | break; |
545 | 0 | } |
546 | | |
547 | 0 | return result; |
548 | 0 | } |
549 | | |
550 | | |
551 | | /* Return an error if the key represented by the S-expression S_KEY |
552 | | * and the OpenPGP key represented by PK do not use the same curve. */ |
553 | | static gpg_error_t |
554 | | match_curve_skey_pk (gcry_sexp_t s_key, PKT_public_key *pk) |
555 | 0 | { |
556 | 0 | gcry_sexp_t curve = NULL; |
557 | 0 | gcry_sexp_t flags = NULL; |
558 | 0 | char *curve_str = NULL; |
559 | 0 | char *flag; |
560 | 0 | const char *oidstr = NULL; |
561 | 0 | gcry_mpi_t curve_as_mpi = NULL; |
562 | 0 | gpg_error_t err; |
563 | 0 | int is_eddsa = 0; |
564 | 0 | int idx = 0; |
565 | |
|
566 | 0 | if (!(pk->pubkey_algo==PUBKEY_ALGO_ECDH |
567 | 0 | || pk->pubkey_algo==PUBKEY_ALGO_ECDSA |
568 | 0 | || pk->pubkey_algo==PUBKEY_ALGO_EDDSA)) |
569 | 0 | return gpg_error (GPG_ERR_PUBKEY_ALGO); |
570 | | |
571 | 0 | curve = gcry_sexp_find_token (s_key, "curve", 0); |
572 | 0 | if (!curve) |
573 | 0 | { |
574 | 0 | log_error ("no reported curve\n"); |
575 | 0 | return gpg_error (GPG_ERR_UNKNOWN_CURVE); |
576 | 0 | } |
577 | 0 | curve_str = gcry_sexp_nth_string (curve, 1); |
578 | 0 | gcry_sexp_release (curve); curve = NULL; |
579 | 0 | if (!curve_str) |
580 | 0 | { |
581 | 0 | log_error ("no curve name\n"); |
582 | 0 | return gpg_error (GPG_ERR_UNKNOWN_CURVE); |
583 | 0 | } |
584 | 0 | if (!strcmp (curve_str, "Ed448")) |
585 | 0 | is_eddsa = 1; |
586 | 0 | oidstr = openpgp_curve_to_oid (curve_str, NULL, NULL); |
587 | 0 | if (!oidstr) |
588 | 0 | { |
589 | 0 | log_error ("no OID known for curve '%s'\n", curve_str); |
590 | 0 | xfree (curve_str); |
591 | 0 | return gpg_error (GPG_ERR_UNKNOWN_CURVE); |
592 | 0 | } |
593 | 0 | xfree (curve_str); |
594 | 0 | err = openpgp_oid_from_str (oidstr, &curve_as_mpi); |
595 | 0 | if (err) |
596 | 0 | return err; |
597 | 0 | if (gcry_mpi_cmp (pk->pkey[0], curve_as_mpi)) |
598 | 0 | { |
599 | 0 | log_error ("curves do not match\n"); |
600 | 0 | gcry_mpi_release (curve_as_mpi); |
601 | 0 | return gpg_error (GPG_ERR_INV_CURVE); |
602 | 0 | } |
603 | 0 | gcry_mpi_release (curve_as_mpi); |
604 | 0 | flags = gcry_sexp_find_token (s_key, "flags", 0); |
605 | 0 | if (flags) |
606 | 0 | { |
607 | 0 | for (idx = 1; idx < gcry_sexp_length (flags); idx++) |
608 | 0 | { |
609 | 0 | flag = gcry_sexp_nth_string (flags, idx); |
610 | 0 | if (flag && (strcmp ("eddsa", flag) == 0)) |
611 | 0 | is_eddsa = 1; |
612 | 0 | gcry_free (flag); |
613 | 0 | } |
614 | 0 | } |
615 | 0 | if (is_eddsa != (pk->pubkey_algo == PUBKEY_ALGO_EDDSA)) |
616 | 0 | { |
617 | 0 | log_error ("disagreement about EdDSA\n"); |
618 | 0 | err = gpg_error (GPG_ERR_INV_CURVE); |
619 | 0 | } |
620 | |
|
621 | 0 | return err; |
622 | 0 | } |
623 | | |
624 | | |
625 | | /* Return a canonicalized public key algorithms. This is used to |
626 | | compare different flavors of algorithms (e.g. ELG and ELG_E are |
627 | | considered the same). */ |
628 | | static enum gcry_pk_algos |
629 | | canon_pk_algo (enum gcry_pk_algos algo) |
630 | 0 | { |
631 | 0 | switch (algo) |
632 | 0 | { |
633 | 0 | case GCRY_PK_RSA: |
634 | 0 | case GCRY_PK_RSA_E: |
635 | 0 | case GCRY_PK_RSA_S: return GCRY_PK_RSA; |
636 | 0 | case GCRY_PK_ELG: |
637 | 0 | case GCRY_PK_ELG_E: return GCRY_PK_ELG; |
638 | 0 | case GCRY_PK_ECC: |
639 | 0 | case GCRY_PK_ECDSA: |
640 | 0 | case GCRY_PK_ECDH: return GCRY_PK_ECC; |
641 | 0 | default: return algo; |
642 | 0 | } |
643 | 0 | } |
644 | | |
645 | | |
646 | | /* Take an s-expression wit the public and private key and change the |
647 | | * parameter array in PK to include the secret parameters. */ |
648 | | static gpg_error_t |
649 | | secret_key_to_mode1003 (gcry_sexp_t s_key, PKT_public_key *pk) |
650 | 0 | { |
651 | 0 | gpg_error_t err; |
652 | 0 | gcry_sexp_t list = NULL; |
653 | 0 | gcry_sexp_t l2; |
654 | 0 | enum gcry_pk_algos pk_algo; |
655 | 0 | struct seckey_info *ski; |
656 | 0 | int idx; |
657 | 0 | char *string; |
658 | 0 | size_t npkey, nskey; |
659 | 0 | gcry_mpi_t pub_params[10] = { NULL }; |
660 | | |
661 | | /* We look for a private-key, then the first element in it tells us |
662 | | the type */ |
663 | 0 | list = gcry_sexp_find_token (s_key, "protected-private-key", 0); |
664 | 0 | if (!list) |
665 | 0 | list = gcry_sexp_find_token (s_key, "private-key", 0); |
666 | 0 | if (!list) |
667 | 0 | { |
668 | 0 | err = gpg_error (GPG_ERR_BAD_SECKEY); |
669 | 0 | goto leave; |
670 | 0 | } |
671 | | |
672 | 0 | log_assert (!pk->seckey_info); |
673 | | |
674 | | /* Parse the gcrypt PK algo and check that it is okay. */ |
675 | 0 | l2 = gcry_sexp_cadr (list); |
676 | 0 | if (!l2) |
677 | 0 | { |
678 | 0 | err = gpg_error (GPG_ERR_BAD_SECKEY); |
679 | 0 | goto leave; |
680 | 0 | } |
681 | 0 | gcry_sexp_release (list); |
682 | 0 | list = l2; |
683 | 0 | string = gcry_sexp_nth_string (list, 0); |
684 | 0 | if (!string) |
685 | 0 | { |
686 | 0 | err = gpg_error (GPG_ERR_BAD_SECKEY); |
687 | 0 | goto leave; |
688 | 0 | } |
689 | 0 | pk_algo = gcry_pk_map_name (string); |
690 | 0 | xfree (string); string = NULL; |
691 | 0 | if (gcry_pk_algo_info (pk_algo, GCRYCTL_GET_ALGO_NPKEY, NULL, &npkey) |
692 | 0 | || gcry_pk_algo_info (pk_algo, GCRYCTL_GET_ALGO_NSKEY, NULL, &nskey) |
693 | 0 | || !npkey || npkey >= nskey) |
694 | 0 | { |
695 | 0 | err = gpg_error (GPG_ERR_BAD_SECKEY); |
696 | 0 | goto leave; |
697 | 0 | } |
698 | | |
699 | | /* Check that the pubkey algo and the received parameters matches |
700 | | * those from the public key. */ |
701 | 0 | switch (canon_pk_algo (pk_algo)) |
702 | 0 | { |
703 | 0 | case GCRY_PK_RSA: |
704 | 0 | if (!is_RSA (pk->pubkey_algo) || npkey != 2) |
705 | 0 | err = gpg_error (GPG_ERR_PUBKEY_ALGO); /* Does not match. */ |
706 | 0 | else |
707 | 0 | err = gcry_sexp_extract_param (list, NULL, "ne", |
708 | 0 | &pub_params[0], |
709 | 0 | &pub_params[1], |
710 | 0 | NULL); |
711 | 0 | break; |
712 | | |
713 | 0 | case GCRY_PK_DSA: |
714 | 0 | if (!is_DSA (pk->pubkey_algo) || npkey != 4) |
715 | 0 | err = gpg_error (GPG_ERR_PUBKEY_ALGO); /* Does not match. */ |
716 | 0 | else |
717 | 0 | err = gcry_sexp_extract_param (list, NULL, "pqgy", |
718 | 0 | &pub_params[0], |
719 | 0 | &pub_params[1], |
720 | 0 | &pub_params[2], |
721 | 0 | &pub_params[3], |
722 | 0 | NULL); |
723 | 0 | break; |
724 | | |
725 | 0 | case GCRY_PK_ELG: |
726 | 0 | if (!is_ELGAMAL (pk->pubkey_algo) || npkey != 3) |
727 | 0 | err = gpg_error (GPG_ERR_PUBKEY_ALGO); /* Does not match. */ |
728 | 0 | else |
729 | 0 | err = gcry_sexp_extract_param (list, NULL, "pgy", |
730 | 0 | &pub_params[0], |
731 | 0 | &pub_params[1], |
732 | 0 | &pub_params[2], |
733 | 0 | NULL); |
734 | 0 | break; |
735 | | |
736 | 0 | case GCRY_PK_ECC: |
737 | 0 | err = 0; |
738 | 0 | if (!(pk->pubkey_algo == PUBKEY_ALGO_ECDSA |
739 | 0 | || pk->pubkey_algo == PUBKEY_ALGO_ECDH |
740 | 0 | || pk->pubkey_algo == PUBKEY_ALGO_EDDSA)) |
741 | 0 | { |
742 | 0 | err = gpg_error (GPG_ERR_PUBKEY_ALGO); /* Does not match. */ |
743 | 0 | goto leave; |
744 | 0 | } |
745 | 0 | npkey = 2; |
746 | 0 | if (pk->pubkey_algo == PUBKEY_ALGO_ECDH) |
747 | 0 | npkey++; |
748 | | /* Dedicated check of the curve. */ |
749 | 0 | pub_params[0] = NULL; |
750 | 0 | err = match_curve_skey_pk (list, pk); |
751 | 0 | if (err) |
752 | 0 | goto leave; |
753 | | /* ... and of the Q parameter. */ |
754 | 0 | err = sexp_extract_param_sos (list, "q", &pub_params[1]); |
755 | 0 | if (!err && (gcry_mpi_cmp (pk->pkey[1], pub_params[1]))) |
756 | 0 | err = gpg_error (GPG_ERR_BAD_PUBKEY); |
757 | 0 | break; |
758 | | |
759 | 0 | default: |
760 | 0 | err = gpg_error (GPG_ERR_PUBKEY_ALGO); /* Unknown. */ |
761 | 0 | break; |
762 | 0 | } |
763 | 0 | if (err) |
764 | 0 | goto leave; |
765 | | |
766 | 0 | nskey = npkey + 1; /* We only have one skey param. */ |
767 | 0 | if (nskey > PUBKEY_MAX_NSKEY) |
768 | 0 | { |
769 | 0 | err = gpg_error (GPG_ERR_BAD_SECKEY); |
770 | 0 | goto leave; |
771 | 0 | } |
772 | | |
773 | | /* Check that the public key parameters match. For ECC we already |
774 | | * did this in the switch above. */ |
775 | 0 | if (canon_pk_algo (pk_algo) != GCRY_PK_ECC) |
776 | 0 | { |
777 | 0 | for (idx=0; idx < npkey; idx++) |
778 | 0 | if (gcry_mpi_cmp (pk->pkey[idx], pub_params[idx])) |
779 | 0 | { |
780 | 0 | err = gpg_error (GPG_ERR_BAD_PUBKEY); |
781 | 0 | goto leave; |
782 | 0 | } |
783 | 0 | } |
784 | | |
785 | | /* Store the maybe protected secret key as an s-expression. */ |
786 | 0 | pk->seckey_info = ski = xtrycalloc (1, sizeof *ski); |
787 | 0 | if (!ski) |
788 | 0 | { |
789 | 0 | err = gpg_error_from_syserror (); |
790 | 0 | goto leave; |
791 | 0 | } |
792 | | |
793 | 0 | pk->seckey_info = ski = xtrycalloc (1, sizeof *ski); |
794 | 0 | if (!ski) |
795 | 0 | { |
796 | 0 | err = gpg_error_from_syserror (); |
797 | 0 | goto leave; |
798 | 0 | } |
799 | | |
800 | 0 | ski->is_protected = 1; |
801 | 0 | ski->s2k.mode = 1003; |
802 | |
|
803 | 0 | { |
804 | 0 | unsigned char *buf; |
805 | 0 | size_t buflen; |
806 | |
|
807 | 0 | err = make_canon_sexp (s_key, &buf, &buflen); |
808 | 0 | if (err) |
809 | 0 | goto leave; |
810 | 0 | pk->pkey[npkey] = gcry_mpi_set_opaque (NULL, buf, buflen*8); |
811 | 0 | for (idx=npkey+1; idx < PUBKEY_MAX_NSKEY; idx++) |
812 | 0 | pk->pkey[idx] = NULL; |
813 | 0 | } |
814 | | |
815 | 0 | leave: |
816 | 0 | gcry_sexp_release (list); |
817 | 0 | for (idx=0; idx < DIM(pub_params); idx++) |
818 | 0 | gcry_mpi_release (pub_params[idx]); |
819 | 0 | return err; |
820 | 0 | } |
821 | | |
822 | | |
823 | | /* Take a cleartext dump of a secret key in PK and change the |
824 | | * parameter array in PK to include the secret parameters. */ |
825 | | static gpg_error_t |
826 | | cleartext_secret_key_to_openpgp (gcry_sexp_t s_key, PKT_public_key *pk) |
827 | 0 | { |
828 | 0 | gpg_error_t err; |
829 | 0 | gcry_sexp_t top_list; |
830 | 0 | gcry_sexp_t key = NULL; |
831 | 0 | char *key_type = NULL; |
832 | 0 | enum gcry_pk_algos pk_algo; |
833 | 0 | struct seckey_info *ski; |
834 | 0 | int idx, sec_start; |
835 | 0 | gcry_mpi_t pub_params[10] = { NULL }; |
836 | | |
837 | | /* we look for a private-key, then the first element in it tells us |
838 | | the type */ |
839 | 0 | top_list = gcry_sexp_find_token (s_key, "private-key", 0); |
840 | 0 | if (!top_list) |
841 | 0 | goto bad_seckey; |
842 | | |
843 | | /* ignore all S-expression after the first sublist -- we assume that |
844 | | they are comments or otherwise irrelevant to OpenPGP */ |
845 | 0 | if (gcry_sexp_length(top_list) < 2) |
846 | 0 | goto bad_seckey; |
847 | 0 | key = gcry_sexp_nth (top_list, 1); |
848 | 0 | if (!key) |
849 | 0 | goto bad_seckey; |
850 | 0 | key_type = gcry_sexp_nth_string(key, 0); |
851 | 0 | pk_algo = gcry_pk_map_name (key_type); |
852 | |
|
853 | 0 | log_assert (!pk->seckey_info); |
854 | | |
855 | 0 | pk->seckey_info = ski = xtrycalloc (1, sizeof *ski); |
856 | 0 | if (!ski) |
857 | 0 | { |
858 | 0 | err = gpg_error_from_syserror (); |
859 | 0 | goto leave; |
860 | 0 | } |
861 | | |
862 | 0 | switch (canon_pk_algo (pk_algo)) |
863 | 0 | { |
864 | 0 | case GCRY_PK_RSA: |
865 | 0 | if (!is_RSA (pk->pubkey_algo)) |
866 | 0 | goto bad_pubkey_algo; |
867 | 0 | err = gcry_sexp_extract_param (key, NULL, "ne", |
868 | 0 | &pub_params[0], |
869 | 0 | &pub_params[1], |
870 | 0 | NULL); |
871 | 0 | for (idx=0; idx < 2 && !err; idx++) |
872 | 0 | if (gcry_mpi_cmp(pk->pkey[idx], pub_params[idx])) |
873 | 0 | err = gpg_error (GPG_ERR_BAD_PUBKEY); |
874 | 0 | if (!err) |
875 | 0 | { |
876 | 0 | for (idx = 2; idx < 6 && !err; idx++) |
877 | 0 | { |
878 | 0 | gcry_mpi_release (pk->pkey[idx]); |
879 | 0 | pk->pkey[idx] = NULL; |
880 | 0 | } |
881 | 0 | err = gcry_sexp_extract_param (key, NULL, "dpqu", |
882 | 0 | &pk->pkey[2], |
883 | 0 | &pk->pkey[3], |
884 | 0 | &pk->pkey[4], |
885 | 0 | &pk->pkey[5], |
886 | 0 | NULL); |
887 | 0 | } |
888 | 0 | if (!err) |
889 | 0 | { |
890 | 0 | for (idx = 2; idx < 6; idx++) |
891 | 0 | ski->csum += checksum_mpi (pk->pkey[idx]); |
892 | 0 | } |
893 | 0 | break; |
894 | | |
895 | 0 | case GCRY_PK_DSA: |
896 | 0 | if (!is_DSA (pk->pubkey_algo)) |
897 | 0 | goto bad_pubkey_algo; |
898 | 0 | err = gcry_sexp_extract_param (key, NULL, "pqgy", |
899 | 0 | &pub_params[0], |
900 | 0 | &pub_params[1], |
901 | 0 | &pub_params[2], |
902 | 0 | &pub_params[3], |
903 | 0 | NULL); |
904 | 0 | for (idx=0; idx < 4 && !err; idx++) |
905 | 0 | if (gcry_mpi_cmp(pk->pkey[idx], pub_params[idx])) |
906 | 0 | err = gpg_error (GPG_ERR_BAD_PUBKEY); |
907 | 0 | if (!err) |
908 | 0 | { |
909 | 0 | gcry_mpi_release (pk->pkey[4]); |
910 | 0 | pk->pkey[4] = NULL; |
911 | 0 | err = gcry_sexp_extract_param (key, NULL, "x", |
912 | 0 | &pk->pkey[4], |
913 | 0 | NULL); |
914 | 0 | } |
915 | 0 | if (!err) |
916 | 0 | ski->csum += checksum_mpi (pk->pkey[4]); |
917 | 0 | break; |
918 | | |
919 | 0 | case GCRY_PK_ELG: |
920 | 0 | if (!is_ELGAMAL (pk->pubkey_algo)) |
921 | 0 | goto bad_pubkey_algo; |
922 | 0 | err = gcry_sexp_extract_param (key, NULL, "pgy", |
923 | 0 | &pub_params[0], |
924 | 0 | &pub_params[1], |
925 | 0 | &pub_params[2], |
926 | 0 | NULL); |
927 | 0 | for (idx=0; idx < 3 && !err; idx++) |
928 | 0 | if (gcry_mpi_cmp(pk->pkey[idx], pub_params[idx])) |
929 | 0 | err = gpg_error (GPG_ERR_BAD_PUBKEY); |
930 | 0 | if (!err) |
931 | 0 | { |
932 | 0 | gcry_mpi_release (pk->pkey[3]); |
933 | 0 | pk->pkey[3] = NULL; |
934 | 0 | err = gcry_sexp_extract_param (key, NULL, "x", |
935 | 0 | &pk->pkey[3], |
936 | 0 | NULL); |
937 | 0 | } |
938 | 0 | if (!err) |
939 | 0 | ski->csum += checksum_mpi (pk->pkey[3]); |
940 | 0 | break; |
941 | | |
942 | 0 | case GCRY_PK_ECC: |
943 | 0 | err = match_curve_skey_pk (key, pk); |
944 | 0 | if (err) |
945 | 0 | goto leave; |
946 | 0 | else |
947 | 0 | err = sexp_extract_param_sos (key, "q", &pub_params[0]); |
948 | 0 | if (!err && (gcry_mpi_cmp(pk->pkey[1], pub_params[0]))) |
949 | 0 | err = gpg_error (GPG_ERR_BAD_PUBKEY); |
950 | |
|
951 | 0 | sec_start = 2; |
952 | 0 | if (pk->pubkey_algo == PUBKEY_ALGO_ECDH) |
953 | 0 | sec_start += 1; |
954 | 0 | if (!err) |
955 | 0 | { |
956 | 0 | gcry_mpi_release (pk->pkey[sec_start]); |
957 | 0 | pk->pkey[sec_start] = NULL; |
958 | 0 | err = sexp_extract_param_sos (key, "d", &pk->pkey[sec_start]); |
959 | 0 | } |
960 | |
|
961 | 0 | if (!err) |
962 | 0 | ski->csum += checksum_mpi (pk->pkey[sec_start]); |
963 | 0 | break; |
964 | | |
965 | 0 | default: |
966 | 0 | pk->seckey_info = NULL; |
967 | 0 | xfree (ski); |
968 | 0 | err = gpg_error (GPG_ERR_NOT_IMPLEMENTED); |
969 | 0 | break; |
970 | 0 | } |
971 | | |
972 | 0 | leave: |
973 | 0 | gcry_sexp_release (top_list); |
974 | 0 | gcry_sexp_release (key); |
975 | 0 | gcry_free (key_type); |
976 | |
|
977 | 0 | for (idx=0; idx < DIM(pub_params); idx++) |
978 | 0 | gcry_mpi_release (pub_params[idx]); |
979 | 0 | return err; |
980 | | |
981 | 0 | bad_pubkey_algo: |
982 | 0 | err = gpg_error (GPG_ERR_PUBKEY_ALGO); |
983 | 0 | goto leave; |
984 | | |
985 | 0 | bad_seckey: |
986 | 0 | err = gpg_error (GPG_ERR_BAD_SECKEY); |
987 | 0 | goto leave; |
988 | 0 | } |
989 | | |
990 | | |
991 | | /* Use the key transfer format given in S_PGP to create the secinfo |
992 | | structure in PK and change the parameter array in PK to include the |
993 | | secret parameters. */ |
994 | | static gpg_error_t |
995 | | transfer_format_to_openpgp (gcry_sexp_t s_pgp, PKT_public_key *pk) |
996 | 0 | { |
997 | 0 | gpg_error_t err; |
998 | 0 | gcry_sexp_t top_list; |
999 | 0 | gcry_sexp_t list = NULL; |
1000 | 0 | char *curve = NULL; |
1001 | 0 | const char *value; |
1002 | 0 | size_t valuelen; |
1003 | 0 | char *string; |
1004 | 0 | int idx; |
1005 | 0 | int is_v4, is_protected; |
1006 | 0 | enum gcry_pk_algos pk_algo; |
1007 | 0 | int protect_algo = 0; |
1008 | 0 | char iv[16]; |
1009 | 0 | int ivlen = 0; |
1010 | 0 | int s2k_mode = 0; |
1011 | 0 | int s2k_algo = 0; |
1012 | 0 | byte s2k_salt[8]; |
1013 | 0 | u32 s2k_count = 0; |
1014 | 0 | int is_ecdh = 0; |
1015 | 0 | size_t npkey, nskey; |
1016 | 0 | gcry_mpi_t skey[10]; /* We support up to 9 parameters. */ |
1017 | 0 | int skeyidx = 0; |
1018 | 0 | struct seckey_info *ski; |
1019 | | |
1020 | | /* gcry_log_debugsxp ("transferkey", s_pgp); */ |
1021 | 0 | top_list = gcry_sexp_find_token (s_pgp, "openpgp-private-key", 0); |
1022 | 0 | if (!top_list) |
1023 | 0 | goto bad_seckey; |
1024 | | |
1025 | 0 | list = gcry_sexp_find_token (top_list, "version", 0); |
1026 | 0 | if (!list) |
1027 | 0 | goto bad_seckey; |
1028 | 0 | value = gcry_sexp_nth_data (list, 1, &valuelen); |
1029 | 0 | if (!value || valuelen != 1 || !(value[0] == '3' || value[0] == '4')) |
1030 | 0 | goto bad_seckey; |
1031 | 0 | is_v4 = (value[0] == '4'); |
1032 | |
|
1033 | 0 | gcry_sexp_release (list); |
1034 | 0 | list = gcry_sexp_find_token (top_list, "protection", 0); |
1035 | 0 | if (!list) |
1036 | 0 | goto bad_seckey; |
1037 | 0 | value = gcry_sexp_nth_data (list, 1, &valuelen); |
1038 | 0 | if (!value) |
1039 | 0 | goto bad_seckey; |
1040 | 0 | if (valuelen == 4 && !memcmp (value, "sha1", 4)) |
1041 | 0 | is_protected = 2; |
1042 | 0 | else if (valuelen == 3 && !memcmp (value, "sum", 3)) |
1043 | 0 | is_protected = 1; |
1044 | 0 | else if (valuelen == 4 && !memcmp (value, "none", 4)) |
1045 | 0 | is_protected = 0; |
1046 | 0 | else |
1047 | 0 | goto bad_seckey; |
1048 | 0 | if (is_protected) |
1049 | 0 | { |
1050 | 0 | string = gcry_sexp_nth_string (list, 2); |
1051 | 0 | if (!string) |
1052 | 0 | goto bad_seckey; |
1053 | 0 | protect_algo = gcry_cipher_map_name (string); |
1054 | 0 | xfree (string); |
1055 | |
|
1056 | 0 | value = gcry_sexp_nth_data (list, 3, &valuelen); |
1057 | 0 | if (!value || !valuelen || valuelen > sizeof iv) |
1058 | 0 | goto bad_seckey; |
1059 | 0 | memcpy (iv, value, valuelen); |
1060 | 0 | ivlen = valuelen; |
1061 | |
|
1062 | 0 | string = gcry_sexp_nth_string (list, 4); |
1063 | 0 | if (!string) |
1064 | 0 | goto bad_seckey; |
1065 | 0 | s2k_mode = strtol (string, NULL, 10); |
1066 | 0 | xfree (string); |
1067 | |
|
1068 | 0 | string = gcry_sexp_nth_string (list, 5); |
1069 | 0 | if (!string) |
1070 | 0 | goto bad_seckey; |
1071 | 0 | s2k_algo = gcry_md_map_name (string); |
1072 | 0 | xfree (string); |
1073 | |
|
1074 | 0 | value = gcry_sexp_nth_data (list, 6, &valuelen); |
1075 | 0 | if (!value || !valuelen || valuelen > sizeof s2k_salt) |
1076 | 0 | goto bad_seckey; |
1077 | 0 | memcpy (s2k_salt, value, valuelen); |
1078 | |
|
1079 | 0 | string = gcry_sexp_nth_string (list, 7); |
1080 | 0 | if (!string) |
1081 | 0 | goto bad_seckey; |
1082 | 0 | s2k_count = strtoul (string, NULL, 10); |
1083 | 0 | xfree (string); |
1084 | 0 | } |
1085 | | |
1086 | | /* Parse the gcrypt PK algo and check that it is okay. */ |
1087 | 0 | gcry_sexp_release (list); |
1088 | 0 | list = gcry_sexp_find_token (top_list, "algo", 0); |
1089 | 0 | if (!list) |
1090 | 0 | goto bad_seckey; |
1091 | 0 | string = gcry_sexp_nth_string (list, 1); |
1092 | 0 | if (!string) |
1093 | 0 | goto bad_seckey; |
1094 | 0 | pk_algo = gcry_pk_map_name (string); |
1095 | 0 | xfree (string); string = NULL; |
1096 | 0 | if (gcry_pk_algo_info (pk_algo, GCRYCTL_GET_ALGO_NPKEY, NULL, &npkey) |
1097 | 0 | || gcry_pk_algo_info (pk_algo, GCRYCTL_GET_ALGO_NSKEY, NULL, &nskey) |
1098 | 0 | || !npkey || npkey >= nskey) |
1099 | 0 | goto bad_seckey; |
1100 | | |
1101 | | /* Check that the pubkey algo matches the one from the public key. */ |
1102 | 0 | switch (canon_pk_algo (pk_algo)) |
1103 | 0 | { |
1104 | 0 | case GCRY_PK_RSA: |
1105 | 0 | if (!is_RSA (pk->pubkey_algo)) |
1106 | 0 | pk_algo = 0; /* Does not match. */ |
1107 | 0 | break; |
1108 | 0 | case GCRY_PK_DSA: |
1109 | 0 | if (!is_DSA (pk->pubkey_algo)) |
1110 | 0 | pk_algo = 0; /* Does not match. */ |
1111 | 0 | break; |
1112 | 0 | case GCRY_PK_ELG: |
1113 | 0 | if (!is_ELGAMAL (pk->pubkey_algo)) |
1114 | 0 | pk_algo = 0; /* Does not match. */ |
1115 | 0 | break; |
1116 | 0 | case GCRY_PK_ECC: |
1117 | 0 | if (pk->pubkey_algo == PUBKEY_ALGO_ECDSA) |
1118 | 0 | ; |
1119 | 0 | else if (pk->pubkey_algo == PUBKEY_ALGO_ECDH) |
1120 | 0 | is_ecdh = 1; |
1121 | 0 | else if (pk->pubkey_algo == PUBKEY_ALGO_EDDSA) |
1122 | 0 | ; |
1123 | 0 | else |
1124 | 0 | pk_algo = 0; /* Does not match. */ |
1125 | | /* For ECC we do not have the domain parameters thus fix our info. */ |
1126 | 0 | npkey = 1; |
1127 | 0 | nskey = 2; |
1128 | 0 | break; |
1129 | 0 | default: |
1130 | 0 | pk_algo = 0; /* Oops. */ |
1131 | 0 | break; |
1132 | 0 | } |
1133 | 0 | if (!pk_algo) |
1134 | 0 | { |
1135 | 0 | err = gpg_error (GPG_ERR_PUBKEY_ALGO); |
1136 | 0 | goto leave; |
1137 | 0 | } |
1138 | | |
1139 | | /* This check has to go after the ecc adjustments. */ |
1140 | 0 | if (nskey > PUBKEY_MAX_NSKEY) |
1141 | 0 | goto bad_seckey; |
1142 | | |
1143 | | /* Parse the key parameters. */ |
1144 | 0 | gcry_sexp_release (list); |
1145 | 0 | list = gcry_sexp_find_token (top_list, "skey", 0); |
1146 | 0 | if (!list) |
1147 | 0 | goto bad_seckey; |
1148 | 0 | for (idx=0;;) |
1149 | 0 | { |
1150 | 0 | int is_enc; |
1151 | |
|
1152 | 0 | value = gcry_sexp_nth_data (list, ++idx, &valuelen); |
1153 | 0 | if (!value && skeyidx >= npkey) |
1154 | 0 | break; /* Ready. */ |
1155 | | |
1156 | | /* Check for too many parameters. Note that depending on the |
1157 | | protection mode and version number we may see less than NSKEY |
1158 | | (but at least NPKEY+1) parameters. */ |
1159 | 0 | if (idx >= 2*nskey) |
1160 | 0 | goto bad_seckey; |
1161 | 0 | if (skeyidx >= DIM (skey)-1) |
1162 | 0 | goto bad_seckey; |
1163 | | |
1164 | 0 | if (!value || valuelen != 1 || !(value[0] == '_' || value[0] == 'e')) |
1165 | 0 | goto bad_seckey; |
1166 | 0 | is_enc = (value[0] == 'e'); |
1167 | 0 | value = gcry_sexp_nth_data (list, ++idx, &valuelen); |
1168 | 0 | if (!value || !valuelen) |
1169 | 0 | goto bad_seckey; |
1170 | 0 | if (is_enc |
1171 | 0 | || pk->pubkey_algo == PUBKEY_ALGO_ECDSA |
1172 | 0 | || pk->pubkey_algo == PUBKEY_ALGO_EDDSA |
1173 | 0 | || pk->pubkey_algo == PUBKEY_ALGO_ECDH) |
1174 | 0 | { |
1175 | 0 | unsigned int nbits = valuelen*8; |
1176 | 0 | const unsigned char *p = value; |
1177 | |
|
1178 | 0 | if (*p && nbits >= 8 && !(*p & 0x80)) |
1179 | 0 | if (--nbits >= 7 && !(*p & 0x40)) |
1180 | 0 | if (--nbits >= 6 && !(*p & 0x20)) |
1181 | 0 | if (--nbits >= 5 && !(*p & 0x10)) |
1182 | 0 | if (--nbits >= 4 && !(*p & 0x08)) |
1183 | 0 | if (--nbits >= 3 && !(*p & 0x04)) |
1184 | 0 | if (--nbits >= 2 && !(*p & 0x02)) |
1185 | 0 | if (--nbits >= 1 && !(*p & 0x01)) |
1186 | 0 | --nbits; |
1187 | |
|
1188 | 0 | skey[skeyidx] = gcry_mpi_set_opaque_copy (NULL, value, nbits); |
1189 | 0 | if (!skey[skeyidx]) |
1190 | 0 | goto outofmem; |
1191 | 0 | if (is_enc) |
1192 | 0 | gcry_mpi_set_flag (skey[skeyidx], GCRYMPI_FLAG_USER1); |
1193 | 0 | else |
1194 | 0 | gcry_mpi_set_flag (skey[skeyidx], GCRYMPI_FLAG_USER2); |
1195 | 0 | } |
1196 | 0 | else |
1197 | 0 | { |
1198 | 0 | if (gcry_mpi_scan (skey + skeyidx, GCRYMPI_FMT_STD, |
1199 | 0 | value, valuelen, NULL)) |
1200 | 0 | goto bad_seckey; |
1201 | 0 | } |
1202 | 0 | skeyidx++; |
1203 | 0 | } |
1204 | 0 | skey[skeyidx++] = NULL; |
1205 | |
|
1206 | 0 | gcry_sexp_release (list); list = NULL; |
1207 | | |
1208 | | /* We have no need for the CSUM value thus we don't parse it. */ |
1209 | | /* list = gcry_sexp_find_token (top_list, "csum", 0); */ |
1210 | | /* if (list) */ |
1211 | | /* { */ |
1212 | | /* string = gcry_sexp_nth_string (list, 1); */ |
1213 | | /* if (!string) */ |
1214 | | /* goto bad_seckey; */ |
1215 | | /* desired_csum = strtoul (string, NULL, 10); */ |
1216 | | /* xfree (string); */ |
1217 | | /* } */ |
1218 | | /* else */ |
1219 | | /* desired_csum = 0; */ |
1220 | | /* gcry_sexp_release (list); list = NULL; */ |
1221 | | |
1222 | | /* Get the curve name if any, */ |
1223 | 0 | list = gcry_sexp_find_token (top_list, "curve", 0); |
1224 | 0 | if (list) |
1225 | 0 | { |
1226 | 0 | curve = gcry_sexp_nth_string (list, 1); |
1227 | 0 | gcry_sexp_release (list); list = NULL; |
1228 | 0 | } |
1229 | |
|
1230 | 0 | gcry_sexp_release (top_list); top_list = NULL; |
1231 | | |
1232 | | /* log_debug ("XXX is_v4=%d\n", is_v4); */ |
1233 | | /* log_debug ("XXX pubkey_algo=%d\n", pubkey_algo); */ |
1234 | | /* log_debug ("XXX is_protected=%d\n", is_protected); */ |
1235 | | /* log_debug ("XXX protect_algo=%d\n", protect_algo); */ |
1236 | | /* log_printhex ("XXX iv", iv, ivlen); */ |
1237 | | /* log_debug ("XXX ivlen=%d\n", ivlen); */ |
1238 | | /* log_debug ("XXX s2k_mode=%d\n", s2k_mode); */ |
1239 | | /* log_debug ("XXX s2k_algo=%d\n", s2k_algo); */ |
1240 | | /* log_printhex ("XXX s2k_salt", s2k_salt, sizeof s2k_salt); */ |
1241 | | /* log_debug ("XXX s2k_count=%lu\n", (unsigned long)s2k_count); */ |
1242 | | /* for (idx=0; skey[idx]; idx++) */ |
1243 | | /* { */ |
1244 | | /* int is_enc = gcry_mpi_get_flag (skey[idx], GCRYMPI_FLAG_OPAQUE); */ |
1245 | | /* log_info ("XXX skey[%d]%s:", idx, is_enc? " (enc)":""); */ |
1246 | | /* if (is_enc) */ |
1247 | | /* { */ |
1248 | | /* void *p; */ |
1249 | | /* unsigned int nbits; */ |
1250 | | /* p = gcry_mpi_get_opaque (skey[idx], &nbits); */ |
1251 | | /* log_printhex (NULL, p, (nbits+7)/8); */ |
1252 | | /* } */ |
1253 | | /* else */ |
1254 | | /* gcry_mpi_dump (skey[idx]); */ |
1255 | | /* log_printf ("\n"); */ |
1256 | | /* } */ |
1257 | |
|
1258 | 0 | if (!is_v4 || is_protected != 2 ) |
1259 | 0 | { |
1260 | | /* We only support the v4 format and a SHA-1 checksum. */ |
1261 | 0 | err = gpg_error (GPG_ERR_NOT_IMPLEMENTED); |
1262 | 0 | goto leave; |
1263 | 0 | } |
1264 | | |
1265 | | /* We need to change the received parameters for ECC algorithms. |
1266 | | The transfer format has the curve name and the parameters |
1267 | | separate. We put them all into the SKEY array. */ |
1268 | 0 | if (canon_pk_algo (pk_algo) == GCRY_PK_ECC) |
1269 | 0 | { |
1270 | 0 | const char *oidstr; |
1271 | | |
1272 | | /* Assert that all required parameters are available. We also |
1273 | | check that the array does not contain more parameters than |
1274 | | needed (this was used by some beta versions of 2.1. */ |
1275 | 0 | if (!curve || !skey[0] || !skey[1] || skey[2]) |
1276 | 0 | { |
1277 | 0 | err = gpg_error (GPG_ERR_INTERNAL); |
1278 | 0 | goto leave; |
1279 | 0 | } |
1280 | | |
1281 | 0 | oidstr = openpgp_curve_to_oid (curve, NULL, NULL); |
1282 | 0 | if (!oidstr) |
1283 | 0 | { |
1284 | 0 | log_error ("no OID known for curve '%s'\n", curve); |
1285 | 0 | err = gpg_error (GPG_ERR_UNKNOWN_CURVE); |
1286 | 0 | goto leave; |
1287 | 0 | } |
1288 | | /* Put the curve's OID into the MPI array. This requires |
1289 | | that we shift Q and D. For ECDH also insert the KDF parms. */ |
1290 | 0 | if (is_ecdh) |
1291 | 0 | { |
1292 | 0 | skey[4] = NULL; |
1293 | 0 | skey[3] = skey[1]; |
1294 | 0 | skey[2] = gcry_mpi_copy (pk->pkey[2]); |
1295 | 0 | } |
1296 | 0 | else |
1297 | 0 | { |
1298 | 0 | skey[3] = NULL; |
1299 | 0 | skey[2] = skey[1]; |
1300 | 0 | } |
1301 | 0 | skey[1] = skey[0]; |
1302 | 0 | skey[0] = NULL; |
1303 | 0 | err = openpgp_oid_from_str (oidstr, skey + 0); |
1304 | 0 | if (err) |
1305 | 0 | goto leave; |
1306 | | /* Fixup the NPKEY and NSKEY to match OpenPGP reality. */ |
1307 | 0 | npkey = 2 + is_ecdh; |
1308 | 0 | nskey = 3 + is_ecdh; |
1309 | | |
1310 | | /* for (idx=0; skey[idx]; idx++) */ |
1311 | | /* { */ |
1312 | | /* log_info ("YYY skey[%d]:", idx); */ |
1313 | | /* if (gcry_mpi_get_flag (skey[idx], GCRYMPI_FLAG_OPAQUE)) */ |
1314 | | /* { */ |
1315 | | /* void *p; */ |
1316 | | /* unsigned int nbits; */ |
1317 | | /* p = gcry_mpi_get_opaque (skey[idx], &nbits); */ |
1318 | | /* log_printhex (NULL, p, (nbits+7)/8); */ |
1319 | | /* } */ |
1320 | | /* else */ |
1321 | | /* gcry_mpi_dump (skey[idx]); */ |
1322 | | /* log_printf ("\n"); */ |
1323 | | /* } */ |
1324 | 0 | } |
1325 | | |
1326 | | /* Do some sanity checks. */ |
1327 | 0 | if (s2k_count > 255) |
1328 | 0 | { |
1329 | | /* We expect an already encoded S2K count. */ |
1330 | 0 | err = gpg_error (GPG_ERR_INV_DATA); |
1331 | 0 | goto leave; |
1332 | 0 | } |
1333 | 0 | err = openpgp_cipher_test_algo (protect_algo); |
1334 | 0 | if (err) |
1335 | 0 | goto leave; |
1336 | 0 | err = openpgp_md_test_algo (s2k_algo); |
1337 | 0 | if (err) |
1338 | 0 | goto leave; |
1339 | | |
1340 | | /* Check that the public key parameters match. Note that since |
1341 | | Libgcrypt 1.5 gcry_mpi_cmp handles opaque MPI correctly. */ |
1342 | 0 | for (idx=0; idx < npkey; idx++) |
1343 | 0 | if (gcry_mpi_cmp (pk->pkey[idx], skey[idx])) |
1344 | 0 | { |
1345 | 0 | err = gpg_error (GPG_ERR_BAD_PUBKEY); |
1346 | 0 | goto leave; |
1347 | 0 | } |
1348 | | |
1349 | | /* Check that the first secret key parameter in SKEY is encrypted |
1350 | | and that there are no more secret key parameters. The latter is |
1351 | | guaranteed by the v4 packet format. */ |
1352 | 0 | if (!gcry_mpi_get_flag (skey[npkey], GCRYMPI_FLAG_USER1)) |
1353 | 0 | goto bad_seckey; |
1354 | 0 | if (npkey+1 < DIM (skey) && skey[npkey+1]) |
1355 | 0 | goto bad_seckey; |
1356 | | |
1357 | | /* Check that the secret key parameters in PK are all set to NULL. */ |
1358 | 0 | for (idx=npkey; idx < nskey; idx++) |
1359 | 0 | if (pk->pkey[idx]) |
1360 | 0 | goto bad_seckey; |
1361 | | |
1362 | | /* Now build the protection info. */ |
1363 | 0 | pk->seckey_info = ski = xtrycalloc (1, sizeof *ski); |
1364 | 0 | if (!ski) |
1365 | 0 | { |
1366 | 0 | err = gpg_error_from_syserror (); |
1367 | 0 | goto leave; |
1368 | 0 | } |
1369 | | |
1370 | 0 | ski->is_protected = 1; |
1371 | 0 | ski->sha1chk = 1; |
1372 | 0 | ski->algo = protect_algo; |
1373 | 0 | ski->s2k.mode = s2k_mode; |
1374 | 0 | ski->s2k.hash_algo = s2k_algo; |
1375 | 0 | log_assert (sizeof ski->s2k.salt == sizeof s2k_salt); |
1376 | 0 | memcpy (ski->s2k.salt, s2k_salt, sizeof s2k_salt); |
1377 | 0 | ski->s2k.count = s2k_count; |
1378 | 0 | log_assert (ivlen <= sizeof ski->iv); |
1379 | 0 | memcpy (ski->iv, iv, ivlen); |
1380 | 0 | ski->ivlen = ivlen; |
1381 | | |
1382 | | /* Store the protected secret key parameter. */ |
1383 | 0 | pk->pkey[npkey] = skey[npkey]; |
1384 | 0 | skey[npkey] = NULL; |
1385 | | |
1386 | | /* That's it. */ |
1387 | |
|
1388 | 0 | leave: |
1389 | 0 | gcry_free (curve); |
1390 | 0 | gcry_sexp_release (list); |
1391 | 0 | gcry_sexp_release (top_list); |
1392 | 0 | for (idx=0; idx < skeyidx; idx++) |
1393 | 0 | gcry_mpi_release (skey[idx]); |
1394 | 0 | return err; |
1395 | | |
1396 | 0 | bad_seckey: |
1397 | 0 | err = gpg_error (GPG_ERR_BAD_SECKEY); |
1398 | 0 | goto leave; |
1399 | | |
1400 | 0 | outofmem: |
1401 | 0 | err = gpg_error (GPG_ERR_ENOMEM); |
1402 | 0 | goto leave; |
1403 | 0 | } |
1404 | | |
1405 | | |
1406 | | /* Print an "EXPORTED" status line. PK is the primary public key. */ |
1407 | | static void |
1408 | | print_status_exported (PKT_public_key *pk) |
1409 | 0 | { |
1410 | 0 | char *hexfpr; |
1411 | |
|
1412 | 0 | if (!is_status_enabled ()) |
1413 | 0 | return; |
1414 | | |
1415 | 0 | hexfpr = hexfingerprint (pk, NULL, 0); |
1416 | 0 | write_status_text (STATUS_EXPORTED, hexfpr? hexfpr : "[?]"); |
1417 | 0 | xfree (hexfpr); |
1418 | 0 | } |
1419 | | |
1420 | | |
1421 | | /* |
1422 | | * Receive a secret key from agent specified by HEXGRIP. |
1423 | | * |
1424 | | * Since the key data from the agent is encrypted, decrypt it using |
1425 | | * CIPHERHD context. Then, parse the decrypted key data into transfer |
1426 | | * format, and put secret parameters into PK. |
1427 | | * |
1428 | | * If CLEARTEXT is 0, store the secret key material |
1429 | | * passphrase-protected. Otherwise, store secret key material in the |
1430 | | * clear. |
1431 | | * |
1432 | | * If MODE1003 is set, the key is requested in raw GnuPG format from |
1433 | | * the agent. This usually does not require a passphrase unless the |
1434 | | * gpg-agent has not yet used the key and needs to convert it to its |
1435 | | * internal format first. |
1436 | | * |
1437 | | * CACHE_NONCE_ADDR is used to share nonce for multiple key retrievals. |
1438 | | * |
1439 | | * If PK is NULL, the raw key is returned (e.g. for ssh export) at |
1440 | | * R_KEY. CLEARTEXT and CACHE_NONCE_ADDR ared ignored in this case. |
1441 | | */ |
1442 | | gpg_error_t |
1443 | | receive_seckey_from_agent (ctrl_t ctrl, gcry_cipher_hd_t cipherhd, |
1444 | | int cleartext, int mode1003, |
1445 | | char **cache_nonce_addr, const char *hexgrip, |
1446 | | PKT_public_key *pk, gcry_sexp_t *r_key) |
1447 | 0 | { |
1448 | 0 | gpg_error_t err = 0; |
1449 | 0 | unsigned char *wrappedkey = NULL; |
1450 | 0 | size_t wrappedkeylen; |
1451 | 0 | unsigned char *key = NULL; |
1452 | 0 | size_t keylen, realkeylen; |
1453 | 0 | gcry_sexp_t s_skey = NULL; |
1454 | 0 | char *prompt; |
1455 | |
|
1456 | 0 | if (r_key) |
1457 | 0 | *r_key = NULL; |
1458 | 0 | if (opt.verbose) |
1459 | 0 | log_info ("key %s: asking agent for the secret parts\n", hexgrip); |
1460 | |
|
1461 | 0 | if (pk) |
1462 | 0 | { |
1463 | 0 | prompt = gpg_format_keydesc (ctrl, pk, FORMAT_KEYDESC_EXPORT, 1); |
1464 | 0 | err = agent_export_key (ctrl, hexgrip, prompt, !cleartext, mode1003, |
1465 | 0 | cache_nonce_addr, |
1466 | 0 | &wrappedkey, &wrappedkeylen, |
1467 | 0 | pk->keyid, pk->main_keyid, pk->pubkey_algo); |
1468 | 0 | } |
1469 | 0 | else |
1470 | 0 | { |
1471 | 0 | prompt = gpg_format_keydesc (ctrl, NULL, FORMAT_KEYDESC_KEYGRIP, 1); |
1472 | 0 | err = agent_export_key (ctrl, hexgrip, prompt, 0, 0, |
1473 | 0 | NULL, |
1474 | 0 | &wrappedkey, &wrappedkeylen, |
1475 | 0 | NULL, NULL, 0); |
1476 | 0 | } |
1477 | 0 | xfree (prompt); |
1478 | |
|
1479 | 0 | if (err) |
1480 | 0 | goto unwraperror; |
1481 | 0 | if (wrappedkeylen < 24) |
1482 | 0 | { |
1483 | 0 | err = gpg_error (GPG_ERR_INV_LENGTH); |
1484 | 0 | goto unwraperror; |
1485 | 0 | } |
1486 | 0 | keylen = wrappedkeylen - 8; |
1487 | 0 | key = xtrymalloc_secure (keylen); |
1488 | 0 | if (!key) |
1489 | 0 | { |
1490 | 0 | err = gpg_error_from_syserror (); |
1491 | 0 | goto unwraperror; |
1492 | 0 | } |
1493 | 0 | err = gcry_cipher_decrypt (cipherhd, key, keylen, wrappedkey, wrappedkeylen); |
1494 | 0 | if (err) |
1495 | 0 | goto unwraperror; |
1496 | 0 | realkeylen = gcry_sexp_canon_len (key, keylen, NULL, &err); |
1497 | 0 | if (!realkeylen) |
1498 | 0 | goto unwraperror; /* Invalid csexp. */ |
1499 | | |
1500 | 0 | err = gcry_sexp_sscan (&s_skey, NULL, key, realkeylen); |
1501 | 0 | if (!err) |
1502 | 0 | { |
1503 | 0 | if (pk && mode1003) |
1504 | 0 | err = secret_key_to_mode1003 (s_skey, pk); |
1505 | 0 | else if (pk && cleartext) |
1506 | 0 | err = cleartext_secret_key_to_openpgp (s_skey, pk); |
1507 | 0 | else if (pk) |
1508 | 0 | err = transfer_format_to_openpgp (s_skey, pk); |
1509 | 0 | else if (r_key) |
1510 | 0 | { |
1511 | 0 | *r_key = s_skey; |
1512 | 0 | s_skey = NULL; |
1513 | 0 | } |
1514 | 0 | } |
1515 | |
|
1516 | 0 | unwraperror: |
1517 | 0 | gcry_sexp_release (s_skey); |
1518 | 0 | xfree (key); |
1519 | 0 | xfree (wrappedkey); |
1520 | 0 | if (err) |
1521 | 0 | { |
1522 | 0 | log_error ("key %s: error receiving key from agent:" |
1523 | 0 | " %s%s\n", hexgrip, gpg_strerror (err), |
1524 | 0 | gpg_err_code (err) == GPG_ERR_FULLY_CANCELED? |
1525 | 0 | "":_(" - skipped")); |
1526 | 0 | } |
1527 | 0 | return err; |
1528 | 0 | } |
1529 | | |
1530 | | |
1531 | | /* Write KEYBLOCK either to stdout or to the file set with the |
1532 | | * --output option. This is a simplified version of do_export_stream |
1533 | | * which supports only a few export options. */ |
1534 | | gpg_error_t |
1535 | | write_keyblock_to_output (kbnode_t keyblock, int with_armor, |
1536 | | unsigned int options) |
1537 | 0 | { |
1538 | 0 | gpg_error_t err; |
1539 | 0 | const char *fname; |
1540 | 0 | iobuf_t out; |
1541 | 0 | kbnode_t node; |
1542 | 0 | armor_filter_context_t *afx = NULL; |
1543 | 0 | iobuf_t out_help = NULL; |
1544 | 0 | PKT_public_key *pk = NULL; |
1545 | |
|
1546 | 0 | fname = opt.outfile? opt.outfile : "-"; |
1547 | 0 | if (is_secured_filename (fname) ) |
1548 | 0 | return gpg_error (GPG_ERR_EPERM); |
1549 | | |
1550 | 0 | out = iobuf_create (fname, 0); |
1551 | 0 | if (!out) |
1552 | 0 | { |
1553 | 0 | err = gpg_error_from_syserror (); |
1554 | 0 | log_error(_("can't create '%s': %s\n"), fname, gpg_strerror (err)); |
1555 | 0 | return err; |
1556 | 0 | } |
1557 | 0 | if (opt.verbose) |
1558 | 0 | log_info (_("writing to '%s'\n"), iobuf_get_fname_nonnull (out)); |
1559 | |
|
1560 | 0 | if ((options & EXPORT_DANE_FORMAT)) |
1561 | 0 | { |
1562 | 0 | with_armor = 0; |
1563 | 0 | out_help = iobuf_temp (); |
1564 | 0 | } |
1565 | |
|
1566 | 0 | if (with_armor) |
1567 | 0 | { |
1568 | 0 | afx = new_armor_context (); |
1569 | 0 | afx->what = 1; |
1570 | 0 | push_armor_filter (afx, out); |
1571 | 0 | } |
1572 | |
|
1573 | 0 | for (node = keyblock; node; node = node->next) |
1574 | 0 | { |
1575 | 0 | if (is_deleted_kbnode (node)) |
1576 | 0 | continue; |
1577 | 0 | if (node->pkt->pkttype == PKT_RING_TRUST) |
1578 | 0 | continue; /* Skip - they should not be here anyway. */ |
1579 | | |
1580 | 0 | if (!pk && (node->pkt->pkttype == PKT_PUBLIC_KEY |
1581 | 0 | || node->pkt->pkttype == PKT_SECRET_KEY)) |
1582 | 0 | pk = node->pkt->pkt.public_key; |
1583 | |
|
1584 | 0 | if ((options & EXPORT_BACKUP)) |
1585 | 0 | err = build_packet_and_meta (out_help? out_help : out, node->pkt); |
1586 | 0 | else |
1587 | 0 | err = build_packet (out_help? out_help : out, node->pkt); |
1588 | 0 | if (err) |
1589 | 0 | { |
1590 | 0 | log_error ("build_packet(%d) failed: %s\n", |
1591 | 0 | node->pkt->pkttype, gpg_strerror (err) ); |
1592 | 0 | goto leave; |
1593 | 0 | } |
1594 | 0 | } |
1595 | 0 | err = 0; |
1596 | |
|
1597 | 0 | if (out_help && pk && (options & EXPORT_DANE_FORMAT)) |
1598 | 0 | { |
1599 | 0 | const void *data; |
1600 | 0 | size_t datalen; |
1601 | |
|
1602 | 0 | iobuf_flush_temp (out_help); |
1603 | 0 | data = iobuf_get_temp_buffer (out_help); |
1604 | 0 | datalen = iobuf_get_temp_length (out_help); |
1605 | |
|
1606 | 0 | err = print_dane_records (out, keyblock, pk, data, datalen); |
1607 | 0 | } |
1608 | |
|
1609 | 0 | leave: |
1610 | 0 | if (err) |
1611 | 0 | iobuf_cancel (out); |
1612 | 0 | else |
1613 | 0 | iobuf_close (out); |
1614 | 0 | iobuf_cancel (out_help); |
1615 | 0 | release_armor_context (afx); |
1616 | 0 | return err; |
1617 | 0 | } |
1618 | | |
1619 | | |
1620 | | /* |
1621 | | * Apply the keep-uid filter to the keyblock. The deleted nodes are |
1622 | | * marked and thus the caller should call commit_kbnode afterwards. |
1623 | | * KEYBLOCK must not have any blocks marked as deleted. |
1624 | | */ |
1625 | | static void |
1626 | | apply_keep_uid_filter (ctrl_t ctrl, kbnode_t keyblock, recsel_expr_t selector) |
1627 | 0 | { |
1628 | 0 | kbnode_t node; |
1629 | 0 | struct impex_filter_parm_s parm; |
1630 | |
|
1631 | 0 | parm.ctrl = ctrl; |
1632 | |
|
1633 | 0 | for (node = keyblock->next; node; node = node->next ) |
1634 | 0 | { |
1635 | 0 | if (node->pkt->pkttype == PKT_USER_ID) |
1636 | 0 | { |
1637 | 0 | parm.node = node; |
1638 | 0 | if (!recsel_select (selector, impex_filter_getval, &parm)) |
1639 | 0 | { |
1640 | | /* log_debug ("keep-uid: deleting '%s'\n", */ |
1641 | | /* node->pkt->pkt.user_id->name); */ |
1642 | | /* The UID packet and all following packets up to the |
1643 | | * next UID or a subkey. */ |
1644 | 0 | delete_kbnode (node); |
1645 | 0 | for (; node->next |
1646 | 0 | && node->next->pkt->pkttype != PKT_USER_ID |
1647 | 0 | && node->next->pkt->pkttype != PKT_PUBLIC_SUBKEY |
1648 | 0 | && node->next->pkt->pkttype != PKT_SECRET_SUBKEY ; |
1649 | 0 | node = node->next) |
1650 | 0 | delete_kbnode (node->next); |
1651 | 0 | } |
1652 | | /* else */ |
1653 | | /* log_debug ("keep-uid: keeping '%s'\n", */ |
1654 | | /* node->pkt->pkt.user_id->name); */ |
1655 | 0 | } |
1656 | 0 | } |
1657 | 0 | } |
1658 | | |
1659 | | |
1660 | | /* |
1661 | | * Apply the drop-subkey filter to the keyblock. The deleted nodes are |
1662 | | * marked and thus the caller should call commit_kbnode afterwards. |
1663 | | * KEYBLOCK must not have any blocks marked as deleted. |
1664 | | */ |
1665 | | static void |
1666 | | apply_drop_subkey_filter (ctrl_t ctrl, kbnode_t keyblock, |
1667 | | recsel_expr_t selector) |
1668 | 0 | { |
1669 | 0 | kbnode_t node; |
1670 | 0 | struct impex_filter_parm_s parm; |
1671 | |
|
1672 | 0 | parm.ctrl = ctrl; |
1673 | |
|
1674 | 0 | for (node = keyblock->next; node; node = node->next ) |
1675 | 0 | { |
1676 | 0 | if (node->pkt->pkttype == PKT_PUBLIC_SUBKEY |
1677 | 0 | || node->pkt->pkttype == PKT_SECRET_SUBKEY) |
1678 | 0 | { |
1679 | 0 | parm.node = node; |
1680 | 0 | if (recsel_select (selector, impex_filter_getval, &parm)) |
1681 | 0 | { |
1682 | | /*log_debug ("drop-subkey: deleting a key\n");*/ |
1683 | | /* The subkey packet and all following packets up to the |
1684 | | * next subkey. */ |
1685 | 0 | delete_kbnode (node); |
1686 | 0 | for (; node->next |
1687 | 0 | && node->next->pkt->pkttype != PKT_PUBLIC_SUBKEY |
1688 | 0 | && node->next->pkt->pkttype != PKT_SECRET_SUBKEY ; |
1689 | 0 | node = node->next) |
1690 | 0 | delete_kbnode (node->next); |
1691 | 0 | } |
1692 | 0 | } |
1693 | 0 | } |
1694 | 0 | } |
1695 | | |
1696 | | |
1697 | | /* Print DANErecords for all user IDs in KEYBLOCK to OUT. The data |
1698 | | * for the record is taken from (DATA,DATELEN). PK is the public key |
1699 | | * packet with the primary key. */ |
1700 | | static gpg_error_t |
1701 | | print_dane_records (iobuf_t out, kbnode_t keyblock, PKT_public_key *pk, |
1702 | | const void *data, size_t datalen) |
1703 | 0 | { |
1704 | 0 | gpg_error_t err = 0; |
1705 | 0 | kbnode_t kbctx, node; |
1706 | 0 | PKT_user_id *uid; |
1707 | 0 | char *mbox = NULL; |
1708 | 0 | char hashbuf[32]; |
1709 | 0 | char *hash = NULL; |
1710 | 0 | char *domain; |
1711 | 0 | const char *s; |
1712 | 0 | unsigned int len; |
1713 | 0 | estream_t fp = NULL; |
1714 | 0 | char *hexdata = NULL; |
1715 | 0 | char *hexfpr; |
1716 | |
|
1717 | 0 | hexfpr = hexfingerprint (pk, NULL, 0); |
1718 | 0 | if (!hexfpr) |
1719 | 0 | { |
1720 | 0 | err = gpg_error_from_syserror (); |
1721 | 0 | goto leave; |
1722 | 0 | } |
1723 | 0 | hexdata = bin2hex (data, datalen, NULL); |
1724 | 0 | if (!hexdata) |
1725 | 0 | { |
1726 | 0 | err = gpg_error_from_syserror (); |
1727 | 0 | goto leave; |
1728 | 0 | } |
1729 | 0 | ascii_strlwr (hexdata); |
1730 | 0 | fp = es_fopenmem (0, "rw,samethread"); |
1731 | 0 | if (!fp) |
1732 | 0 | { |
1733 | 0 | err = gpg_error_from_syserror (); |
1734 | 0 | goto leave; |
1735 | 0 | } |
1736 | | |
1737 | 0 | for (kbctx = NULL; (node = walk_kbnode (keyblock, &kbctx, 0));) |
1738 | 0 | { |
1739 | 0 | if (node->pkt->pkttype != PKT_USER_ID) |
1740 | 0 | continue; |
1741 | 0 | uid = node->pkt->pkt.user_id; |
1742 | |
|
1743 | 0 | if (uid->flags.expired || uid->flags.revoked) |
1744 | 0 | continue; |
1745 | | |
1746 | 0 | xfree (mbox); |
1747 | 0 | mbox = mailbox_from_userid (uid->name, 0); |
1748 | 0 | if (!mbox) |
1749 | 0 | continue; |
1750 | | |
1751 | 0 | domain = strchr (mbox, '@'); |
1752 | 0 | *domain++ = 0; |
1753 | |
|
1754 | 0 | if (1) |
1755 | 0 | { |
1756 | 0 | es_fprintf (fp, "$ORIGIN _openpgpkey.%s.\n; %s\n; ", domain, hexfpr); |
1757 | 0 | print_utf8_buffer (fp, uid->name, uid->len); |
1758 | 0 | es_putc ('\n', fp); |
1759 | 0 | gcry_md_hash_buffer (GCRY_MD_SHA256, hashbuf, mbox, strlen (mbox)); |
1760 | 0 | xfree (hash); |
1761 | 0 | hash = bin2hex (hashbuf, 28, NULL); |
1762 | 0 | if (!hash) |
1763 | 0 | { |
1764 | 0 | err = gpg_error_from_syserror (); |
1765 | 0 | goto leave; |
1766 | 0 | } |
1767 | 0 | ascii_strlwr (hash); |
1768 | 0 | len = strlen (hexdata)/2; |
1769 | 0 | es_fprintf (fp, "%s TYPE61 \\# %u (\n", hash, len); |
1770 | 0 | for (s = hexdata; ;) |
1771 | 0 | { |
1772 | 0 | es_fprintf (fp, "\t%.64s\n", s); |
1773 | 0 | if (strlen (s) < 64) |
1774 | 0 | break; |
1775 | 0 | s += 64; |
1776 | 0 | } |
1777 | 0 | es_fputs ("\t)\n\n", fp); |
1778 | 0 | } |
1779 | 0 | } |
1780 | | |
1781 | | /* Make sure it is a string and write it. */ |
1782 | 0 | es_fputc (0, fp); |
1783 | 0 | { |
1784 | 0 | void *vp; |
1785 | |
|
1786 | 0 | if (es_fclose_snatch (fp, &vp, NULL)) |
1787 | 0 | { |
1788 | 0 | err = gpg_error_from_syserror (); |
1789 | 0 | goto leave; |
1790 | 0 | } |
1791 | 0 | fp = NULL; |
1792 | 0 | iobuf_writestr (out, vp); |
1793 | 0 | es_free (vp); |
1794 | 0 | } |
1795 | 0 | err = 0; |
1796 | |
|
1797 | 0 | leave: |
1798 | 0 | xfree (hash); |
1799 | 0 | xfree (mbox); |
1800 | 0 | es_fclose (fp); |
1801 | 0 | xfree (hexdata); |
1802 | 0 | xfree (hexfpr); |
1803 | 0 | return err; |
1804 | 0 | } |
1805 | | |
1806 | | |
1807 | | /* Helper for do_export_stream which writes one keyblock to OUT. */ |
1808 | | static gpg_error_t |
1809 | | do_export_one_keyblock (ctrl_t ctrl, kbnode_t keyblock, u32 *keyid, |
1810 | | iobuf_t out, int secret, unsigned int options, |
1811 | | export_stats_t stats, int *any, |
1812 | | KEYDB_SEARCH_DESC *desc, size_t ndesc, |
1813 | | size_t descindex, gcry_cipher_hd_t cipherhd) |
1814 | 0 | { |
1815 | 0 | gpg_error_t err = gpg_error (GPG_ERR_NOT_FOUND); |
1816 | 0 | char *cache_nonce = NULL; |
1817 | 0 | subkey_list_t subkey_list = NULL; /* Track already processed subkeys. */ |
1818 | 0 | int skip_until_subkey = 0; |
1819 | 0 | int cleartext = 0; |
1820 | 0 | char *hexgrip = NULL; |
1821 | 0 | char *serialno = NULL; |
1822 | 0 | PKT_public_key *pk; |
1823 | 0 | u32 subkidbuf[2], *subkid; |
1824 | 0 | kbnode_t kbctx, node; |
1825 | | |
1826 | | /* NB: walk_kbnode skips packets marked as deleted. */ |
1827 | 0 | for (kbctx=NULL; (node = walk_kbnode (keyblock, &kbctx, 0)); ) |
1828 | 0 | { |
1829 | 0 | if (skip_until_subkey) |
1830 | 0 | { |
1831 | 0 | if (node->pkt->pkttype == PKT_PUBLIC_SUBKEY) |
1832 | 0 | skip_until_subkey = 0; |
1833 | 0 | else |
1834 | 0 | continue; |
1835 | 0 | } |
1836 | | |
1837 | | /* We used to use comment packets, but not any longer. In |
1838 | | * case we still have comments on a key, strip them here |
1839 | | * before we call build_packet(). */ |
1840 | 0 | if (node->pkt->pkttype == PKT_COMMENT) |
1841 | 0 | continue; |
1842 | | |
1843 | | /* Skip ring trust packets - they should not be here anyway. */ |
1844 | 0 | if (node->pkt->pkttype == PKT_RING_TRUST) |
1845 | 0 | continue; |
1846 | | |
1847 | | /* If exact is set, then we only export what was requested |
1848 | | * (plus the primary key, if the user didn't specifically |
1849 | | * request it). */ |
1850 | 0 | if (desc[descindex].exact && node->pkt->pkttype == PKT_PUBLIC_SUBKEY) |
1851 | 0 | { |
1852 | 0 | if (!exact_subkey_match_p (desc+descindex, node)) |
1853 | 0 | { |
1854 | | /* Before skipping this subkey, check whether any |
1855 | | * other description wants an exact match on a |
1856 | | * subkey and include that subkey into the output |
1857 | | * too. Need to add this subkey to a list so that |
1858 | | * it won't get processed a second time. |
1859 | | * |
1860 | | * So the first step here is to check that list and |
1861 | | * skip in any case if the key is in that list. |
1862 | | * |
1863 | | * We need this whole mess because the import |
1864 | | * function of GnuPG < 2.1 is not able to merge |
1865 | | * secret keys and thus it is useless to output them |
1866 | | * as two separate keys and have import merge them. |
1867 | | */ |
1868 | 0 | if (subkey_in_list_p (subkey_list, node)) |
1869 | 0 | skip_until_subkey = 1; /* Already processed this one. */ |
1870 | 0 | else |
1871 | 0 | { |
1872 | 0 | size_t j; |
1873 | |
|
1874 | 0 | for (j=0; j < ndesc; j++) |
1875 | 0 | if (j != descindex && desc[j].exact |
1876 | 0 | && exact_subkey_match_p (desc+j, node)) |
1877 | 0 | break; |
1878 | 0 | if (!(j < ndesc)) |
1879 | 0 | skip_until_subkey = 1; /* No other one matching. */ |
1880 | 0 | } |
1881 | 0 | } |
1882 | |
|
1883 | 0 | if (skip_until_subkey) |
1884 | 0 | continue; |
1885 | | |
1886 | | /* Mark this one as processed. */ |
1887 | 0 | { |
1888 | 0 | subkey_list_t tmp = new_subkey_list_item (node); |
1889 | 0 | tmp->next = subkey_list; |
1890 | 0 | subkey_list = tmp; |
1891 | 0 | } |
1892 | 0 | } |
1893 | | |
1894 | 0 | if (node->pkt->pkttype == PKT_SIGNATURE) |
1895 | 0 | { |
1896 | | /* Do not export packets which are marked as not |
1897 | | * exportable. */ |
1898 | 0 | if (!(options & EXPORT_LOCAL_SIGS) |
1899 | 0 | && !node->pkt->pkt.signature->flags.exportable) |
1900 | 0 | continue; /* not exportable */ |
1901 | | |
1902 | | /* Do not export packets with a "sensitive" revocation key |
1903 | | * unless the user wants us to. Note that we do export |
1904 | | * these when issuing the actual revocation (see revoke.c). */ |
1905 | 0 | if (!(options & EXPORT_SENSITIVE_REVKEYS) |
1906 | 0 | && node->pkt->pkt.signature->revkey) |
1907 | 0 | { |
1908 | 0 | int i; |
1909 | |
|
1910 | 0 | for (i = 0; i < node->pkt->pkt.signature->numrevkeys; i++) |
1911 | 0 | if ((node->pkt->pkt.signature->revkey[i].class & 0x40)) |
1912 | 0 | break; |
1913 | 0 | if (i < node->pkt->pkt.signature->numrevkeys) |
1914 | 0 | continue; |
1915 | 0 | } |
1916 | 0 | } |
1917 | | |
1918 | | /* Don't export attribs? */ |
1919 | 0 | if (!(options & EXPORT_ATTRIBUTES) |
1920 | 0 | && node->pkt->pkttype == PKT_USER_ID |
1921 | 0 | && node->pkt->pkt.user_id->attrib_data) |
1922 | 0 | { |
1923 | | /* Skip until we get to something that is not an attrib or a |
1924 | | * signature on an attrib. */ |
1925 | 0 | while (kbctx->next && kbctx->next->pkt->pkttype == PKT_SIGNATURE) |
1926 | 0 | kbctx = kbctx->next; |
1927 | |
|
1928 | 0 | continue; |
1929 | 0 | } |
1930 | | |
1931 | 0 | if (secret && (node->pkt->pkttype == PKT_PUBLIC_KEY |
1932 | 0 | || node->pkt->pkttype == PKT_PUBLIC_SUBKEY)) |
1933 | 0 | { |
1934 | 0 | pk = node->pkt->pkt.public_key; |
1935 | 0 | if (node->pkt->pkttype == PKT_PUBLIC_KEY) |
1936 | 0 | subkid = NULL; |
1937 | 0 | else |
1938 | 0 | { |
1939 | 0 | keyid_from_pk (pk, subkidbuf); |
1940 | 0 | subkid = subkidbuf; |
1941 | 0 | } |
1942 | |
|
1943 | 0 | if (pk->seckey_info) |
1944 | 0 | { |
1945 | 0 | log_error ("key %s: oops: seckey_info already set" |
1946 | 0 | " - skipped\n", keystr_with_sub (keyid, subkid)); |
1947 | 0 | skip_until_subkey = 1; |
1948 | 0 | continue; |
1949 | 0 | } |
1950 | | |
1951 | 0 | xfree (hexgrip); |
1952 | 0 | err = hexkeygrip_from_pk (pk, &hexgrip); |
1953 | 0 | if (err) |
1954 | 0 | { |
1955 | 0 | log_error ("key %s: error computing keygrip: %s" |
1956 | 0 | " - skipped\n", keystr_with_sub (keyid, subkid), |
1957 | 0 | gpg_strerror (err)); |
1958 | 0 | skip_until_subkey = 1; |
1959 | 0 | err = 0; |
1960 | 0 | continue; |
1961 | 0 | } |
1962 | | |
1963 | 0 | xfree (serialno); |
1964 | 0 | serialno = NULL; |
1965 | 0 | if (secret == 2 && node->pkt->pkttype == PKT_PUBLIC_KEY) |
1966 | 0 | { |
1967 | | /* We are asked not to export the secret parts of the |
1968 | | * primary key. Make up an error code to create the |
1969 | | * stub. */ |
1970 | 0 | err = GPG_ERR_NOT_FOUND; |
1971 | 0 | } |
1972 | 0 | else |
1973 | 0 | err = agent_get_keyinfo (ctrl, hexgrip, &serialno, &cleartext); |
1974 | |
|
1975 | 0 | if ((!err && serialno) |
1976 | 0 | && secret == 2 && node->pkt->pkttype == PKT_PUBLIC_KEY) |
1977 | 0 | { |
1978 | | /* It does not make sense to export a key with its |
1979 | | * primary key on card using a non-key stub. Thus we |
1980 | | * skip those keys when used with --export-secret-subkeys. */ |
1981 | 0 | log_info (_("key %s: key material on-card - skipped\n"), |
1982 | 0 | keystr_with_sub (keyid, subkid)); |
1983 | 0 | skip_until_subkey = 1; |
1984 | 0 | } |
1985 | 0 | else if (gpg_err_code (err) == GPG_ERR_NOT_FOUND |
1986 | 0 | || (!err && serialno)) |
1987 | 0 | { |
1988 | | /* Create a key stub. */ |
1989 | 0 | struct seckey_info *ski; |
1990 | 0 | const char *s; |
1991 | |
|
1992 | 0 | pk->seckey_info = ski = xtrycalloc (1, sizeof *ski); |
1993 | 0 | if (!ski) |
1994 | 0 | { |
1995 | 0 | err = gpg_error_from_syserror (); |
1996 | 0 | goto leave; |
1997 | 0 | } |
1998 | | |
1999 | 0 | ski->is_protected = 1; |
2000 | 0 | if (err) |
2001 | 0 | ski->s2k.mode = 1001; /* GNU dummy (no secret key). */ |
2002 | 0 | else |
2003 | 0 | { |
2004 | 0 | ski->s2k.mode = 1002; /* GNU-divert-to-card. */ |
2005 | 0 | for (s=serialno; sizeof (ski->ivlen) && *s && s[1]; |
2006 | 0 | ski->ivlen++, s += 2) |
2007 | 0 | ski->iv[ski->ivlen] = xtoi_2 (s); |
2008 | 0 | } |
2009 | |
|
2010 | 0 | if ((options & EXPORT_BACKUP)) |
2011 | 0 | err = build_packet_and_meta (out, node->pkt); |
2012 | 0 | else |
2013 | 0 | err = build_packet (out, node->pkt); |
2014 | 0 | if (!err && node->pkt->pkttype == PKT_PUBLIC_KEY) |
2015 | 0 | { |
2016 | 0 | stats->exported++; |
2017 | 0 | print_status_exported (node->pkt->pkt.public_key); |
2018 | 0 | } |
2019 | 0 | } |
2020 | 0 | else if (!err) |
2021 | 0 | { |
2022 | 0 | err = receive_seckey_from_agent (ctrl, cipherhd, |
2023 | 0 | cleartext, |
2024 | 0 | !!(options & EXPORT_MODE1003), |
2025 | 0 | &cache_nonce, |
2026 | 0 | hexgrip, pk, NULL); |
2027 | 0 | if (err) |
2028 | 0 | { |
2029 | 0 | if (gpg_err_code (err) == GPG_ERR_FULLY_CANCELED) |
2030 | 0 | goto leave; |
2031 | 0 | write_status_error ("export_keys.secret", err); |
2032 | 0 | skip_until_subkey = 1; |
2033 | 0 | err = 0; |
2034 | 0 | } |
2035 | 0 | else |
2036 | 0 | { |
2037 | 0 | if ((options & EXPORT_BACKUP)) |
2038 | 0 | err = build_packet_and_meta (out, node->pkt); |
2039 | 0 | else |
2040 | 0 | err = build_packet (out, node->pkt); |
2041 | 0 | if (node->pkt->pkttype == PKT_PUBLIC_KEY) |
2042 | 0 | { |
2043 | 0 | stats->exported++; |
2044 | 0 | print_status_exported (node->pkt->pkt.public_key); |
2045 | 0 | } |
2046 | 0 | } |
2047 | 0 | } |
2048 | 0 | else |
2049 | 0 | { |
2050 | 0 | log_error ("key %s: error getting keyinfo from agent: %s" |
2051 | 0 | " - skipped\n", keystr_with_sub (keyid, subkid), |
2052 | 0 | gpg_strerror (err)); |
2053 | 0 | skip_until_subkey = 1; |
2054 | 0 | err = 0; |
2055 | 0 | } |
2056 | | |
2057 | 0 | xfree (pk->seckey_info); |
2058 | 0 | pk->seckey_info = NULL; |
2059 | 0 | { |
2060 | 0 | int i; |
2061 | 0 | for (i = pubkey_get_npkey (pk->pubkey_algo); |
2062 | 0 | i < pubkey_get_nskey (pk->pubkey_algo); i++) |
2063 | 0 | { |
2064 | 0 | gcry_mpi_release (pk->pkey[i]); |
2065 | 0 | pk->pkey[i] = NULL; |
2066 | 0 | } |
2067 | 0 | } |
2068 | 0 | } |
2069 | 0 | else /* Not secret or common packets. */ |
2070 | 0 | { |
2071 | 0 | if ((options & EXPORT_BACKUP)) |
2072 | 0 | err = build_packet_and_meta (out, node->pkt); |
2073 | 0 | else |
2074 | 0 | err = build_packet (out, node->pkt); |
2075 | 0 | if (!err && node->pkt->pkttype == PKT_PUBLIC_KEY) |
2076 | 0 | { |
2077 | 0 | stats->exported++; |
2078 | 0 | print_status_exported (node->pkt->pkt.public_key); |
2079 | 0 | } |
2080 | 0 | } |
2081 | | |
2082 | 0 | if (err) |
2083 | 0 | { |
2084 | 0 | log_error ("build_packet(%d) failed: %s\n", |
2085 | 0 | node->pkt->pkttype, gpg_strerror (err)); |
2086 | 0 | goto leave; |
2087 | 0 | } |
2088 | | |
2089 | 0 | if (!skip_until_subkey) |
2090 | 0 | *any = 1; |
2091 | 0 | } |
2092 | | |
2093 | 0 | leave: |
2094 | 0 | release_subkey_list (subkey_list); |
2095 | 0 | xfree (serialno); |
2096 | 0 | xfree (hexgrip); |
2097 | 0 | xfree (cache_nonce); |
2098 | 0 | return err; |
2099 | 0 | } |
2100 | | |
2101 | | |
2102 | | /* Helper for do_export_stream which writes the own revocations |
2103 | | * certificates (if any) from KEYBLOCK to OUT. */ |
2104 | | static gpg_error_t |
2105 | | do_export_revocs (ctrl_t ctrl, kbnode_t keyblock, u32 *keyid, |
2106 | | iobuf_t out, unsigned int options, int *any) |
2107 | 0 | { |
2108 | 0 | gpg_error_t err = 0; |
2109 | 0 | kbnode_t kbctx, node; |
2110 | 0 | PKT_signature *sig; |
2111 | |
|
2112 | 0 | (void)ctrl; |
2113 | | |
2114 | | /* NB: walk_kbnode skips packets marked as deleted. */ |
2115 | 0 | for (kbctx=NULL; (node = walk_kbnode (keyblock, &kbctx, 0)); ) |
2116 | 0 | { |
2117 | 0 | if (node->pkt->pkttype != PKT_SIGNATURE) |
2118 | 0 | continue; |
2119 | 0 | sig = node->pkt->pkt.signature; |
2120 | | |
2121 | | /* We are only interested in revocation certifcates. */ |
2122 | 0 | if (!(IS_KEY_REV (sig) || IS_UID_REV (sig) || IS_SUBKEY_REV (sig))) |
2123 | 0 | continue; |
2124 | | |
2125 | 0 | if (!(sig->keyid[0] == keyid[0] && sig->keyid[1] == keyid[1])) |
2126 | 0 | continue; /* Not a self-signature. */ |
2127 | | |
2128 | | /* Do not export signature packets which are marked as not |
2129 | | * exportable. */ |
2130 | 0 | if (!(options & EXPORT_LOCAL_SIGS) |
2131 | 0 | && !sig->flags.exportable) |
2132 | 0 | continue; /* not exportable */ |
2133 | | |
2134 | | /* Do not export packets with a "sensitive" revocation key |
2135 | | * unless the user wants us to. */ |
2136 | 0 | if (!(options & EXPORT_SENSITIVE_REVKEYS) |
2137 | 0 | && sig->revkey) |
2138 | 0 | { |
2139 | 0 | int i; |
2140 | |
|
2141 | 0 | for (i = 0; i < sig->numrevkeys; i++) |
2142 | 0 | if ((sig->revkey[i].class & 0x40)) |
2143 | 0 | break; |
2144 | 0 | if (i < sig->numrevkeys) |
2145 | 0 | continue; |
2146 | 0 | } |
2147 | | |
2148 | 0 | if (!sig->flags.checked) |
2149 | 0 | { |
2150 | 0 | log_info ("signature not marked as checked - ignored\n"); |
2151 | 0 | continue; |
2152 | 0 | } |
2153 | 0 | if (!sig->flags.valid) |
2154 | 0 | { |
2155 | 0 | log_info ("signature not not valid - ignored\n"); |
2156 | 0 | continue; |
2157 | 0 | } |
2158 | | |
2159 | 0 | err = build_packet (out, node->pkt); |
2160 | 0 | if (err) |
2161 | 0 | { |
2162 | 0 | log_error ("build_packet(%d) failed: %s\n", |
2163 | 0 | node->pkt->pkttype, gpg_strerror (err)); |
2164 | 0 | goto leave; |
2165 | 0 | } |
2166 | 0 | *any = 1; |
2167 | 0 | } |
2168 | | |
2169 | 0 | leave: |
2170 | 0 | return err; |
2171 | 0 | } |
2172 | | |
2173 | | |
2174 | | /* For secret key export we need to setup a decryption context. |
2175 | | * Returns 0 and the context at r_cipherhd. */ |
2176 | | static gpg_error_t |
2177 | | get_keywrap_key (ctrl_t ctrl, gcry_cipher_hd_t *r_cipherhd) |
2178 | 0 | { |
2179 | | #ifdef ENABLE_SELINUX_HACKS |
2180 | | (void)ctrl; |
2181 | | *r_cipherhd = NULL; |
2182 | | log_error (_("exporting secret keys not allowed\n")); |
2183 | | return gpg_error (GPG_ERR_NOT_SUPPORTED); |
2184 | | #else |
2185 | 0 | gpg_error_t err; |
2186 | 0 | void *kek = NULL; |
2187 | 0 | size_t keklen; |
2188 | 0 | gcry_cipher_hd_t cipherhd; |
2189 | |
|
2190 | 0 | *r_cipherhd = NULL; |
2191 | |
|
2192 | 0 | err = agent_keywrap_key (ctrl, 1, &kek, &keklen); |
2193 | 0 | if (err) |
2194 | 0 | { |
2195 | 0 | log_error ("error getting the KEK: %s\n", gpg_strerror (err)); |
2196 | 0 | return err; |
2197 | 0 | } |
2198 | | |
2199 | 0 | err = gcry_cipher_open (&cipherhd, GCRY_CIPHER_AES128, |
2200 | 0 | GCRY_CIPHER_MODE_AESWRAP, 0); |
2201 | 0 | if (!err) |
2202 | 0 | err = gcry_cipher_setkey (cipherhd, kek, keklen); |
2203 | 0 | if (err) |
2204 | 0 | log_error ("error setting up an encryption context: %s\n", |
2205 | 0 | gpg_strerror (err)); |
2206 | |
|
2207 | 0 | if (!err) |
2208 | 0 | *r_cipherhd = cipherhd; |
2209 | 0 | else |
2210 | 0 | gcry_cipher_close (cipherhd); |
2211 | 0 | xfree (kek); |
2212 | 0 | return err; |
2213 | 0 | #endif |
2214 | 0 | } |
2215 | | |
2216 | | |
2217 | | /* Export the keys identified by the list of strings in USERS to the |
2218 | | stream OUT. If SECRET is false public keys will be exported. With |
2219 | | secret true secret keys will be exported; in this case 1 means the |
2220 | | entire secret keyblock and 2 only the subkeys. OPTIONS are the |
2221 | | export options to apply. If KEYBLOCK_OUT is not NULL, AND the exit |
2222 | | code is zero, a pointer to the first keyblock found and exported |
2223 | | will be stored at this address; no other keyblocks are exported in |
2224 | | this case. The caller must free the returned keyblock. If any |
2225 | | key has been exported true is stored at ANY. */ |
2226 | | static int |
2227 | | do_export_stream (ctrl_t ctrl, iobuf_t out, strlist_t users, int secret, |
2228 | | kbnode_t *keyblock_out, unsigned int options, |
2229 | | export_stats_t stats, int *any) |
2230 | 0 | { |
2231 | 0 | gpg_error_t err = 0; |
2232 | 0 | PACKET pkt; |
2233 | 0 | kbnode_t keyblock = NULL; |
2234 | 0 | kbnode_t node; |
2235 | 0 | size_t ndesc, descindex; |
2236 | 0 | KEYDB_SEARCH_DESC *desc = NULL; |
2237 | 0 | KEYDB_HANDLE kdbhd; |
2238 | 0 | strlist_t sl; |
2239 | 0 | gcry_cipher_hd_t cipherhd = NULL; |
2240 | 0 | struct export_stats_s dummystats; |
2241 | 0 | iobuf_t out_help = NULL; |
2242 | |
|
2243 | 0 | if (!stats) |
2244 | 0 | stats = &dummystats; |
2245 | 0 | *any = 0; |
2246 | 0 | init_packet (&pkt); |
2247 | 0 | kdbhd = keydb_new (ctrl); |
2248 | 0 | if (!kdbhd) |
2249 | 0 | return gpg_error_from_syserror (); |
2250 | | |
2251 | | /* For the DANE format open a helper iobuf and |
2252 | | * enforce some options. */ |
2253 | 0 | if ((options & EXPORT_DANE_FORMAT)) |
2254 | 0 | { |
2255 | 0 | out_help = iobuf_temp (); |
2256 | 0 | options |= EXPORT_MINIMAL | EXPORT_CLEAN; |
2257 | 0 | } |
2258 | |
|
2259 | 0 | if (!users) |
2260 | 0 | { |
2261 | 0 | ndesc = 1; |
2262 | 0 | desc = xcalloc (ndesc, sizeof *desc); |
2263 | 0 | desc[0].mode = KEYDB_SEARCH_MODE_FIRST; |
2264 | 0 | } |
2265 | 0 | else |
2266 | 0 | { |
2267 | 0 | for (ndesc=0, sl=users; sl; sl = sl->next, ndesc++) |
2268 | 0 | ; |
2269 | 0 | desc = xmalloc ( ndesc * sizeof *desc); |
2270 | |
|
2271 | 0 | for (ndesc=0, sl=users; sl; sl = sl->next) |
2272 | 0 | { |
2273 | 0 | if (!(err=classify_user_id (sl->d, desc+ndesc, 1))) |
2274 | 0 | ndesc++; |
2275 | 0 | else |
2276 | 0 | log_error (_("key \"%s\" not found: %s\n"), |
2277 | 0 | sl->d, gpg_strerror (err)); |
2278 | 0 | } |
2279 | |
|
2280 | 0 | keydb_disable_caching (kdbhd); /* We are looping the search. */ |
2281 | | |
2282 | | /* It would be nice to see which of the given users did actually |
2283 | | match one in the keyring. To implement this we need to have |
2284 | | a found flag for each entry in desc. To set this flag we |
2285 | | must check all those entries after a match to mark all |
2286 | | matched one - currently we stop at the first match. To do |
2287 | | this we need an extra flag to enable this feature. */ |
2288 | 0 | } |
2289 | | |
2290 | | /* For secret key export we need to setup a decryption context. */ |
2291 | 0 | if (secret && (err = get_keywrap_key (ctrl, &cipherhd))) |
2292 | 0 | goto leave; |
2293 | | |
2294 | 0 | for (;;) |
2295 | 0 | { |
2296 | 0 | u32 keyid[2]; |
2297 | 0 | PKT_public_key *pk; |
2298 | |
|
2299 | 0 | err = keydb_search (kdbhd, desc, ndesc, &descindex); |
2300 | 0 | if (!users) |
2301 | 0 | desc[0].mode = KEYDB_SEARCH_MODE_NEXT; |
2302 | 0 | if (err) |
2303 | 0 | break; |
2304 | | |
2305 | | /* Read the keyblock. */ |
2306 | 0 | release_kbnode (keyblock); |
2307 | 0 | keyblock = NULL; |
2308 | 0 | err = keydb_get_keyblock (kdbhd, &keyblock); |
2309 | 0 | if (err) |
2310 | 0 | { |
2311 | 0 | log_error (_("error reading keyblock: %s\n"), gpg_strerror (err)); |
2312 | 0 | goto leave; |
2313 | 0 | } |
2314 | | |
2315 | 0 | node = find_kbnode (keyblock, PKT_PUBLIC_KEY); |
2316 | 0 | if (!node) |
2317 | 0 | { |
2318 | 0 | log_error ("public key packet not found in keyblock - skipped\n"); |
2319 | 0 | continue; |
2320 | 0 | } |
2321 | 0 | stats->count++; |
2322 | 0 | setup_main_keyids (keyblock); /* gpg_format_keydesc needs it. */ |
2323 | 0 | pk = node->pkt->pkt.public_key; |
2324 | 0 | keyid_from_pk (pk, keyid); |
2325 | | |
2326 | | /* If a secret key export is required we need to check whether |
2327 | | we have a secret key at all and if so create the seckey_info |
2328 | | structure. */ |
2329 | 0 | if (secret) |
2330 | 0 | { |
2331 | 0 | if (agent_probe_any_secret_key (ctrl, keyblock)) |
2332 | 0 | continue; /* No secret key (neither primary nor subkey). */ |
2333 | | |
2334 | | /* No v3 keys with GNU mode 1001. */ |
2335 | 0 | if (secret == 2 && pk->version == 3) |
2336 | 0 | { |
2337 | 0 | log_info (_("key %s: PGP 2.x style key - skipped\n"), |
2338 | 0 | keystr (keyid)); |
2339 | 0 | continue; |
2340 | 0 | } |
2341 | | |
2342 | | /* The agent does not yet allow export of v3 packets. It is |
2343 | | actually questionable whether we should allow them at |
2344 | | all. */ |
2345 | 0 | if (pk->version == 3) |
2346 | 0 | { |
2347 | 0 | log_info ("key %s: PGP 2.x style key (v3) export " |
2348 | 0 | "not yet supported - skipped\n", keystr (keyid)); |
2349 | 0 | continue; |
2350 | 0 | } |
2351 | 0 | stats->secret_count++; |
2352 | 0 | } |
2353 | | |
2354 | | /* Always do the cleaning on the public key part if requested. |
2355 | | * A designated revocation is never stripped, even with |
2356 | | * export-minimal set. */ |
2357 | 0 | if ((options & EXPORT_CLEAN)) |
2358 | 0 | { |
2359 | 0 | merge_keys_and_selfsig (ctrl, keyblock); |
2360 | 0 | clean_all_uids (ctrl, keyblock, opt.verbose, |
2361 | 0 | (options&EXPORT_MINIMAL), NULL, NULL); |
2362 | 0 | clean_all_subkeys (ctrl, keyblock, opt.verbose, |
2363 | 0 | (options&EXPORT_MINIMAL)? KEY_CLEAN_ALL |
2364 | 0 | /**/ : KEY_CLEAN_AUTHENCR, |
2365 | 0 | NULL, NULL); |
2366 | 0 | commit_kbnode (&keyblock); |
2367 | 0 | } |
2368 | 0 | else if (export_keep_uid || export_drop_subkey || export_select_filter) |
2369 | 0 | { |
2370 | | /* Need to merge so that for example the "usage" property |
2371 | | * has been setup. */ |
2372 | 0 | merge_keys_and_selfsig (ctrl, keyblock); |
2373 | 0 | } |
2374 | | |
2375 | |
|
2376 | 0 | if (export_select_filter) |
2377 | 0 | { |
2378 | 0 | int selected = 0; |
2379 | 0 | struct impex_filter_parm_s parm; |
2380 | 0 | parm.ctrl = ctrl; |
2381 | |
|
2382 | 0 | for (parm.node = keyblock; parm.node; parm.node = parm.node->next) |
2383 | 0 | { |
2384 | 0 | if (recsel_select (export_select_filter, |
2385 | 0 | impex_filter_getval, &parm)) |
2386 | 0 | { |
2387 | 0 | selected = 1; |
2388 | 0 | break; |
2389 | 0 | } |
2390 | 0 | } |
2391 | 0 | if (!selected) |
2392 | 0 | continue; /* Skip this keyblock. */ |
2393 | 0 | } |
2394 | | |
2395 | 0 | if (export_keep_uid) |
2396 | 0 | { |
2397 | 0 | commit_kbnode (&keyblock); |
2398 | 0 | apply_keep_uid_filter (ctrl, keyblock, export_keep_uid); |
2399 | 0 | commit_kbnode (&keyblock); |
2400 | 0 | } |
2401 | |
|
2402 | 0 | if (export_drop_subkey) |
2403 | 0 | { |
2404 | 0 | commit_kbnode (&keyblock); |
2405 | 0 | apply_drop_subkey_filter (ctrl, keyblock, export_drop_subkey); |
2406 | 0 | commit_kbnode (&keyblock); |
2407 | 0 | } |
2408 | | |
2409 | | /* And write it. */ |
2410 | 0 | if ((options & EXPORT_REVOCS)) |
2411 | 0 | err = do_export_revocs (ctrl, keyblock, keyid, |
2412 | 0 | out_help? out_help : out, |
2413 | 0 | options, any); |
2414 | 0 | else |
2415 | 0 | err = do_export_one_keyblock (ctrl, keyblock, keyid, |
2416 | 0 | out_help? out_help : out, |
2417 | 0 | secret, options, stats, any, |
2418 | 0 | desc, ndesc, descindex, cipherhd); |
2419 | 0 | if (err) |
2420 | 0 | break; |
2421 | | |
2422 | 0 | if (keyblock_out) |
2423 | 0 | { |
2424 | 0 | *keyblock_out = keyblock; |
2425 | 0 | break; |
2426 | 0 | } |
2427 | | |
2428 | 0 | if (out_help && (options & EXPORT_DANE_FORMAT)) |
2429 | 0 | { |
2430 | | /* We want to write DANE records. OUT_HELP has the |
2431 | | * keyblock and we print a record for each uid to OUT. */ |
2432 | 0 | const void *data; |
2433 | 0 | size_t datalen; |
2434 | |
|
2435 | 0 | iobuf_flush_temp (out_help); |
2436 | 0 | data = iobuf_get_temp_buffer (out_help); |
2437 | 0 | datalen = iobuf_get_temp_length (out_help); |
2438 | |
|
2439 | 0 | err = print_dane_records (out, keyblock, pk, data, datalen); |
2440 | 0 | if (err) |
2441 | 0 | goto leave; |
2442 | | |
2443 | 0 | iobuf_close (out_help); |
2444 | 0 | out_help = iobuf_temp (); |
2445 | 0 | } |
2446 | |
|
2447 | 0 | } |
2448 | 0 | if (gpg_err_code (err) == GPG_ERR_NOT_FOUND) |
2449 | 0 | err = 0; |
2450 | |
|
2451 | 0 | leave: |
2452 | 0 | iobuf_cancel (out_help); |
2453 | 0 | gcry_cipher_close (cipherhd); |
2454 | 0 | xfree(desc); |
2455 | 0 | keydb_release (kdbhd); |
2456 | 0 | if (err || !keyblock_out) |
2457 | 0 | release_kbnode( keyblock ); |
2458 | 0 | if( !*any && !opt.quiet) |
2459 | 0 | log_info (_("WARNING: nothing exported\n")); |
2460 | 0 | return err; |
2461 | 0 | } |
2462 | | |
2463 | | |
2464 | | |
2465 | | /* Write the uint32 VALUE to MB in networ byte order. */ |
2466 | | static void |
2467 | | mb_write_uint32 (membuf_t *mb, u32 value) |
2468 | 0 | { |
2469 | 0 | unsigned char buffer[4]; |
2470 | |
|
2471 | 0 | ulongtobuf (buffer, (ulong)value); |
2472 | 0 | put_membuf (mb, buffer, 4); |
2473 | 0 | } |
2474 | | |
2475 | | /* Write the byte C to MB. */ |
2476 | | static void |
2477 | | mb_write_uint8 (membuf_t *mb, int c) |
2478 | 0 | { |
2479 | 0 | unsigned char buf[1]; |
2480 | |
|
2481 | 0 | buf[0] = c; |
2482 | 0 | put_membuf (mb, buf, 1); |
2483 | 0 | } |
2484 | | |
2485 | | |
2486 | | /* Simple wrapper around put_membuf. */ |
2487 | | static void |
2488 | | mb_write_data (membuf_t *mb, const void *data, size_t datalen) |
2489 | 0 | { |
2490 | 0 | put_membuf (mb, data, datalen); |
2491 | 0 | } |
2492 | | |
2493 | | /* Write STRING with terminating Nul to MB. */ |
2494 | | static void |
2495 | | mb_write_cstring (membuf_t *mb, const char *string) |
2496 | 0 | { |
2497 | 0 | put_membuf (mb, string, strlen (string)+1); |
2498 | 0 | } |
2499 | | |
2500 | | /* Write an SSH style string to MB. */ |
2501 | | static void |
2502 | | mb_write_string (membuf_t *mb, const void *string, size_t n) |
2503 | 0 | { |
2504 | 0 | mb_write_uint32 (mb, (u32)n); |
2505 | 0 | mb_write_data (mb, string, n); |
2506 | 0 | } |
2507 | | |
2508 | | /* Write an MPI as SSH style string to MB */ |
2509 | | static void |
2510 | | mb_write_mpi (membuf_t *mb, gcry_mpi_t mpi, int strip_prefix) |
2511 | 0 | { |
2512 | 0 | unsigned int nbits; |
2513 | 0 | const unsigned char *p; |
2514 | 0 | size_t n; |
2515 | |
|
2516 | 0 | if (gcry_mpi_get_flag (mpi, GCRYMPI_FLAG_OPAQUE)) |
2517 | 0 | { |
2518 | 0 | p = gcry_mpi_get_opaque (mpi, &nbits); |
2519 | 0 | n = (nbits + 7) / 8; |
2520 | |
|
2521 | 0 | if (strip_prefix && n > 1 && p[0] == 0x40) |
2522 | 0 | { |
2523 | | /* We need to strip our 0x40 prefix. */ |
2524 | 0 | p++; |
2525 | 0 | n--; |
2526 | 0 | } |
2527 | 0 | mb_write_string (mb, p, n); |
2528 | 0 | } |
2529 | 0 | else |
2530 | 0 | { |
2531 | 0 | gpg_error_t err; |
2532 | 0 | unsigned char *buf; |
2533 | |
|
2534 | 0 | err = gcry_mpi_aprint (GCRYMPI_FMT_SSH, &buf, &n, mpi); |
2535 | 0 | if (err) |
2536 | 0 | set_membuf_err (mb, err); |
2537 | 0 | else |
2538 | 0 | { |
2539 | 0 | mb_write_data (mb, buf, n); |
2540 | 0 | gcry_free (buf); |
2541 | 0 | } |
2542 | 0 | } |
2543 | 0 | } |
2544 | | |
2545 | | |
2546 | | |
2547 | | static gpg_error_t |
2548 | | key_to_sshblob (membuf_t *mb, const char *identifier, ...) |
2549 | 0 | { |
2550 | 0 | va_list arg_ptr; |
2551 | 0 | gpg_error_t err = 0; |
2552 | 0 | unsigned char nbuf[4]; |
2553 | 0 | unsigned char *buf; |
2554 | 0 | size_t buflen; |
2555 | 0 | gcry_mpi_t a; |
2556 | |
|
2557 | 0 | buflen = strlen (identifier); |
2558 | 0 | ulongtobuf (nbuf, (ulong)buflen); |
2559 | 0 | put_membuf (mb, nbuf, 4); |
2560 | 0 | put_membuf (mb, identifier, buflen); |
2561 | 0 | if (buflen > 11 && !memcmp (identifier, "ecdsa-sha2-", 11)) |
2562 | 0 | { |
2563 | | /* Store the name of the curve taken from the identifier. */ |
2564 | 0 | ulongtobuf (nbuf, (ulong)(buflen - 11)); |
2565 | 0 | put_membuf (mb, nbuf, 4); |
2566 | 0 | put_membuf (mb, identifier+11, buflen - 11); |
2567 | 0 | } |
2568 | 0 | va_start (arg_ptr, identifier); |
2569 | 0 | while ((a = va_arg (arg_ptr, gcry_mpi_t))) |
2570 | 0 | { |
2571 | 0 | if (gcry_mpi_get_flag (a, GCRYMPI_FLAG_OPAQUE)) |
2572 | 0 | { |
2573 | 0 | unsigned int nbits; |
2574 | 0 | const unsigned char *p; |
2575 | |
|
2576 | 0 | p = gcry_mpi_get_opaque (a, &nbits); |
2577 | 0 | buflen = (nbits + 7) / 8; |
2578 | |
|
2579 | 0 | if (!strcmp (identifier, "ssh-ed25519") |
2580 | 0 | && buflen > 1 && p[0] == 0x40) |
2581 | 0 | { |
2582 | | /* We need to strip our 0x40 prefix. */ |
2583 | 0 | put_membuf (mb, "\x00\x00\x00\x20", 4); |
2584 | 0 | put_membuf (mb, p+1, buflen-1); |
2585 | 0 | } |
2586 | 0 | else |
2587 | 0 | { |
2588 | 0 | unsigned char c; |
2589 | |
|
2590 | 0 | c = buflen >> 24; |
2591 | 0 | put_membuf (mb, &c, 1); |
2592 | 0 | c = buflen >> 16; |
2593 | 0 | put_membuf (mb, &c, 1); |
2594 | 0 | c = buflen >> 8; |
2595 | 0 | put_membuf (mb, &c, 1); |
2596 | 0 | c = buflen; |
2597 | 0 | put_membuf (mb, &c, 1); |
2598 | 0 | put_membuf (mb, p, buflen); |
2599 | 0 | } |
2600 | 0 | } |
2601 | 0 | else |
2602 | 0 | { |
2603 | 0 | err = gcry_mpi_aprint (GCRYMPI_FMT_SSH, &buf, &buflen, a); |
2604 | 0 | if (err) |
2605 | 0 | break; |
2606 | 0 | put_membuf (mb, buf, buflen); |
2607 | 0 | gcry_free (buf); |
2608 | 0 | } |
2609 | 0 | } |
2610 | 0 | va_end (arg_ptr); |
2611 | 0 | return err; |
2612 | 0 | } |
2613 | | |
2614 | | |
2615 | | static gpg_error_t |
2616 | | export_one_ssh_key (estream_t fp, PKT_public_key *pk) |
2617 | 0 | { |
2618 | 0 | gpg_error_t err; |
2619 | 0 | const char *identifier = NULL; |
2620 | 0 | membuf_t mb; |
2621 | 0 | void *blob; |
2622 | 0 | size_t bloblen; |
2623 | |
|
2624 | 0 | init_membuf (&mb, 4096); |
2625 | |
|
2626 | 0 | switch (pk->pubkey_algo) |
2627 | 0 | { |
2628 | 0 | case PUBKEY_ALGO_DSA: |
2629 | 0 | identifier = "ssh-dss"; |
2630 | 0 | err = key_to_sshblob (&mb, identifier, |
2631 | 0 | pk->pkey[0], pk->pkey[1], pk->pkey[2], pk->pkey[3], |
2632 | 0 | NULL); |
2633 | 0 | break; |
2634 | | |
2635 | 0 | case PUBKEY_ALGO_RSA: |
2636 | 0 | case PUBKEY_ALGO_RSA_S: |
2637 | 0 | identifier = "ssh-rsa"; |
2638 | 0 | err = key_to_sshblob (&mb, identifier, pk->pkey[1], pk->pkey[0], NULL); |
2639 | 0 | break; |
2640 | | |
2641 | 0 | case PUBKEY_ALGO_ECDSA: |
2642 | 0 | { |
2643 | 0 | char *curveoid; |
2644 | 0 | const char *curve; |
2645 | |
|
2646 | 0 | curveoid = openpgp_oid_to_str (pk->pkey[0]); |
2647 | 0 | if (!curveoid) |
2648 | 0 | err = gpg_error_from_syserror (); |
2649 | 0 | else if (!(curve = openpgp_oid_to_curve (curveoid, 0))) |
2650 | 0 | err = gpg_error (GPG_ERR_UNKNOWN_CURVE); |
2651 | 0 | else |
2652 | 0 | { |
2653 | 0 | if (!strcmp (curve, "nistp256")) |
2654 | 0 | identifier = "ecdsa-sha2-nistp256"; |
2655 | 0 | else if (!strcmp (curve, "nistp384")) |
2656 | 0 | identifier = "ecdsa-sha2-nistp384"; |
2657 | 0 | else if (!strcmp (curve, "nistp521")) |
2658 | 0 | identifier = "ecdsa-sha2-nistp521"; |
2659 | |
|
2660 | 0 | if (!identifier) |
2661 | 0 | err = gpg_error (GPG_ERR_UNKNOWN_CURVE); |
2662 | 0 | else |
2663 | 0 | err = key_to_sshblob (&mb, identifier, pk->pkey[1], NULL); |
2664 | 0 | } |
2665 | 0 | xfree (curveoid); |
2666 | 0 | } |
2667 | 0 | break; |
2668 | | |
2669 | 0 | case PUBKEY_ALGO_EDDSA: |
2670 | 0 | if (openpgp_oid_is_ed25519 (pk->pkey[0])) |
2671 | 0 | { |
2672 | 0 | identifier = "ssh-ed25519"; |
2673 | 0 | err = key_to_sshblob (&mb, identifier, pk->pkey[1], NULL); |
2674 | 0 | } |
2675 | 0 | else if (openpgp_oid_is_ed448 (pk->pkey[0])) |
2676 | 0 | { |
2677 | 0 | identifier = "ssh-ed448"; |
2678 | 0 | err = key_to_sshblob (&mb, identifier, pk->pkey[1], NULL); |
2679 | 0 | } |
2680 | 0 | else |
2681 | 0 | err = gpg_error (GPG_ERR_UNKNOWN_CURVE); |
2682 | 0 | break; |
2683 | | |
2684 | 0 | case PUBKEY_ALGO_ELGAMAL_E: |
2685 | 0 | case PUBKEY_ALGO_ELGAMAL: |
2686 | 0 | err = gpg_error (GPG_ERR_UNUSABLE_PUBKEY); |
2687 | 0 | break; |
2688 | | |
2689 | 0 | default: |
2690 | 0 | err = GPG_ERR_PUBKEY_ALGO; |
2691 | 0 | break; |
2692 | 0 | } |
2693 | | |
2694 | 0 | if (err) |
2695 | 0 | goto leave; |
2696 | | |
2697 | 0 | blob = get_membuf (&mb, &bloblen); |
2698 | 0 | if (blob) |
2699 | 0 | { |
2700 | 0 | struct b64state b64_state; |
2701 | |
|
2702 | 0 | es_fprintf (fp, "%s ", identifier); |
2703 | 0 | err = b64enc_start_es (&b64_state, fp, ""); |
2704 | 0 | if (err) |
2705 | 0 | { |
2706 | 0 | xfree (blob); |
2707 | 0 | goto leave; |
2708 | 0 | } |
2709 | | |
2710 | 0 | err = b64enc_write (&b64_state, blob, bloblen); |
2711 | 0 | b64enc_finish (&b64_state); |
2712 | |
|
2713 | 0 | es_fprintf (fp, " openpgp:0x%08lX\n", (ulong)keyid_from_pk (pk, NULL)); |
2714 | 0 | xfree (blob); |
2715 | 0 | } |
2716 | | |
2717 | 0 | leave: |
2718 | 0 | xfree (get_membuf (&mb, NULL)); |
2719 | 0 | return err; |
2720 | 0 | } |
2721 | | |
2722 | | /* Export the key identified by USERID in the SSH public key format. |
2723 | | The function exports the latest subkey with Authentication |
2724 | | capability unless the '!' suffix is used to export a specific |
2725 | | key. */ |
2726 | | gpg_error_t |
2727 | | export_ssh_key (ctrl_t ctrl, const char *userid) |
2728 | 0 | { |
2729 | 0 | gpg_error_t err; |
2730 | 0 | kbnode_t keyblock = NULL; |
2731 | 0 | KEYDB_SEARCH_DESC desc; |
2732 | 0 | u32 latest_date; |
2733 | 0 | u32 curtime = make_timestamp (); |
2734 | 0 | kbnode_t latest_key, node; |
2735 | 0 | PKT_public_key *pk; |
2736 | 0 | estream_t fp = NULL; |
2737 | 0 | const char *fname = "-"; |
2738 | | |
2739 | | /* We need to know whether the key has been specified using the |
2740 | | exact syntax ('!' suffix). Thus we need to run a |
2741 | | classify_user_id on our own. */ |
2742 | 0 | err = classify_user_id (userid, &desc, 1); |
2743 | | |
2744 | | /* Get the public key. */ |
2745 | 0 | if (!err) |
2746 | 0 | { |
2747 | 0 | getkey_ctx_t getkeyctx; |
2748 | |
|
2749 | 0 | err = get_pubkey_byname (ctrl, GET_PUBKEY_NO_AKL, |
2750 | 0 | &getkeyctx, NULL, userid, &keyblock, |
2751 | 0 | NULL, |
2752 | 0 | 0 /* Only usable keys or given exact. */); |
2753 | 0 | if (!err) |
2754 | 0 | { |
2755 | 0 | err = getkey_next (ctrl, getkeyctx, NULL, NULL); |
2756 | 0 | if (!err) |
2757 | 0 | err = gpg_error (GPG_ERR_AMBIGUOUS_NAME); |
2758 | 0 | else if (gpg_err_code (err) == GPG_ERR_NO_PUBKEY) |
2759 | 0 | err = 0; |
2760 | 0 | } |
2761 | 0 | getkey_end (ctrl, getkeyctx); |
2762 | 0 | } |
2763 | 0 | if (err) |
2764 | 0 | { |
2765 | 0 | log_error (_("key \"%s\" not found: %s\n"), userid, gpg_strerror (err)); |
2766 | 0 | return err; |
2767 | 0 | } |
2768 | | |
2769 | | /* The finish_lookup code in getkey.c does not handle auth keys, |
2770 | | thus we have to duplicate the code here to find the latest |
2771 | | subkey. However, if the key has been found using an exact match |
2772 | | ('!' notation) we use that key without any further checks and |
2773 | | even allow the use of the primary key. */ |
2774 | 0 | latest_date = 0; |
2775 | 0 | latest_key = NULL; |
2776 | 0 | for (node = keyblock; node; node = node->next) |
2777 | 0 | { |
2778 | 0 | if ((node->pkt->pkttype == PKT_PUBLIC_SUBKEY |
2779 | 0 | || node->pkt->pkttype == PKT_PUBLIC_KEY) |
2780 | 0 | && node->pkt->pkt.public_key->flags.exact) |
2781 | 0 | { |
2782 | 0 | latest_key = node; |
2783 | 0 | break; |
2784 | 0 | } |
2785 | 0 | } |
2786 | 0 | if (!latest_key) |
2787 | 0 | { |
2788 | 0 | for (node = keyblock; node; node = node->next) |
2789 | 0 | { |
2790 | 0 | if (node->pkt->pkttype != PKT_PUBLIC_SUBKEY) |
2791 | 0 | continue; |
2792 | | |
2793 | 0 | pk = node->pkt->pkt.public_key; |
2794 | 0 | if (DBG_LOOKUP) |
2795 | 0 | log_debug ("\tchecking subkey %08lX\n", |
2796 | 0 | (ulong) keyid_from_pk (pk, NULL)); |
2797 | 0 | if (!(pk->pubkey_usage & PUBKEY_USAGE_AUTH)) |
2798 | 0 | { |
2799 | 0 | if (DBG_LOOKUP) |
2800 | 0 | log_debug ("\tsubkey not usable for authentication\n"); |
2801 | 0 | continue; |
2802 | 0 | } |
2803 | 0 | if (!pk->flags.valid) |
2804 | 0 | { |
2805 | 0 | if (DBG_LOOKUP) |
2806 | 0 | log_debug ("\tsubkey not valid\n"); |
2807 | 0 | continue; |
2808 | 0 | } |
2809 | 0 | if (pk->flags.revoked) |
2810 | 0 | { |
2811 | 0 | if (DBG_LOOKUP) |
2812 | 0 | log_debug ("\tsubkey has been revoked\n"); |
2813 | 0 | continue; |
2814 | 0 | } |
2815 | 0 | if (pk->has_expired) |
2816 | 0 | { |
2817 | 0 | if (DBG_LOOKUP) |
2818 | 0 | log_debug ("\tsubkey has expired\n"); |
2819 | 0 | continue; |
2820 | 0 | } |
2821 | 0 | if (pk->timestamp > curtime && !opt.ignore_valid_from) |
2822 | 0 | { |
2823 | 0 | if (DBG_LOOKUP) |
2824 | 0 | log_debug ("\tsubkey not yet valid\n"); |
2825 | 0 | continue; |
2826 | 0 | } |
2827 | 0 | if (DBG_LOOKUP) |
2828 | 0 | log_debug ("\tsubkey might be fine\n"); |
2829 | | /* In case a key has a timestamp of 0 set, we make sure that it |
2830 | | is used. A better change would be to compare ">=" but that |
2831 | | might also change the selected keys and is as such a more |
2832 | | intrusive change. */ |
2833 | 0 | if (pk->timestamp > latest_date || (!pk->timestamp && !latest_date)) |
2834 | 0 | { |
2835 | 0 | latest_date = pk->timestamp; |
2836 | 0 | latest_key = node; |
2837 | 0 | } |
2838 | 0 | } |
2839 | | |
2840 | | /* If no subkey was suitable check the primary key. */ |
2841 | 0 | if (!latest_key |
2842 | 0 | && (node = keyblock) && node->pkt->pkttype == PKT_PUBLIC_KEY) |
2843 | 0 | { |
2844 | 0 | pk = node->pkt->pkt.public_key; |
2845 | 0 | if (DBG_LOOKUP) |
2846 | 0 | log_debug ("\tchecking primary key %08lX\n", |
2847 | 0 | (ulong) keyid_from_pk (pk, NULL)); |
2848 | 0 | if (!(pk->pubkey_usage & PUBKEY_USAGE_AUTH)) |
2849 | 0 | { |
2850 | 0 | if (DBG_LOOKUP) |
2851 | 0 | log_debug ("\tprimary key not usable for authentication\n"); |
2852 | 0 | } |
2853 | 0 | else if (!pk->flags.valid) |
2854 | 0 | { |
2855 | 0 | if (DBG_LOOKUP) |
2856 | 0 | log_debug ("\tprimary key not valid\n"); |
2857 | 0 | } |
2858 | 0 | else if (pk->flags.revoked) |
2859 | 0 | { |
2860 | 0 | if (DBG_LOOKUP) |
2861 | 0 | log_debug ("\tprimary key has been revoked\n"); |
2862 | 0 | } |
2863 | 0 | else if (pk->has_expired) |
2864 | 0 | { |
2865 | 0 | if (DBG_LOOKUP) |
2866 | 0 | log_debug ("\tprimary key has expired\n"); |
2867 | 0 | } |
2868 | 0 | else if (pk->timestamp > curtime && !opt.ignore_valid_from) |
2869 | 0 | { |
2870 | 0 | if (DBG_LOOKUP) |
2871 | 0 | log_debug ("\tprimary key not yet valid\n"); |
2872 | 0 | } |
2873 | 0 | else |
2874 | 0 | { |
2875 | 0 | if (DBG_LOOKUP) |
2876 | 0 | log_debug ("\tprimary key is fine\n"); |
2877 | 0 | latest_date = pk->timestamp; |
2878 | 0 | latest_key = node; |
2879 | 0 | } |
2880 | 0 | } |
2881 | 0 | } |
2882 | |
|
2883 | 0 | if (!latest_key) |
2884 | 0 | { |
2885 | 0 | err = gpg_error (GPG_ERR_UNUSABLE_PUBKEY); |
2886 | 0 | log_error (_("key \"%s\" not found: %s\n"), userid, gpg_strerror (err)); |
2887 | 0 | goto leave; |
2888 | 0 | } |
2889 | | |
2890 | 0 | pk = latest_key->pkt->pkt.public_key; |
2891 | 0 | if (DBG_LOOKUP) |
2892 | 0 | log_debug ("\tusing key %08lX\n", (ulong) keyid_from_pk (pk, NULL)); |
2893 | |
|
2894 | 0 | if (opt.outfile && *opt.outfile && strcmp (opt.outfile, "-")) |
2895 | 0 | fp = es_fopen ((fname = opt.outfile), "w"); |
2896 | 0 | else |
2897 | 0 | fp = es_stdout; |
2898 | 0 | if (!fp) |
2899 | 0 | { |
2900 | 0 | err = gpg_error_from_syserror (); |
2901 | 0 | log_error (_("error creating '%s': %s\n"), fname, gpg_strerror (err)); |
2902 | 0 | goto leave; |
2903 | 0 | } |
2904 | | |
2905 | 0 | err = export_one_ssh_key (fp, pk); |
2906 | 0 | if (err) |
2907 | 0 | goto leave; |
2908 | | |
2909 | 0 | if (es_ferror (fp)) |
2910 | 0 | err = gpg_error_from_syserror (); |
2911 | 0 | else |
2912 | 0 | { |
2913 | 0 | if (fp != es_stdout && es_fclose (fp)) |
2914 | 0 | err = gpg_error_from_syserror (); |
2915 | 0 | fp = NULL; |
2916 | 0 | } |
2917 | |
|
2918 | 0 | if (err) |
2919 | 0 | log_error (_("error writing '%s': %s\n"), fname, gpg_strerror (err)); |
2920 | |
|
2921 | 0 | leave: |
2922 | 0 | if (fp != es_stdout) |
2923 | 0 | es_fclose (fp); |
2924 | 0 | release_kbnode (keyblock); |
2925 | 0 | return err; |
2926 | 0 | } |
2927 | | |
2928 | | |
2929 | | /* Export the key identified by USERID in the SSH secret key format. |
2930 | | * The USERID must be given in keygrip format (prefixed with a '&') |
2931 | | * and thus no OpenPGP key is required. The exported key is not |
2932 | | * protected. */ |
2933 | | gpg_error_t |
2934 | | export_secret_ssh_key (ctrl_t ctrl, const char *userid) |
2935 | 0 | { |
2936 | 0 | gpg_error_t err; |
2937 | 0 | KEYDB_SEARCH_DESC desc; |
2938 | 0 | estream_t fp = NULL; |
2939 | 0 | const char *fname = "-"; |
2940 | 0 | gcry_cipher_hd_t cipherhd = NULL; |
2941 | 0 | char hexgrip[KEYGRIP_LEN * 2 + 1]; |
2942 | 0 | gcry_sexp_t skey = NULL; |
2943 | 0 | gcry_sexp_t skeyalgo = NULL; |
2944 | 0 | const char *identifier = NULL; |
2945 | 0 | membuf_t mb; |
2946 | 0 | membuf_t mb2; |
2947 | 0 | void *blob = NULL; |
2948 | 0 | size_t bloblen; |
2949 | 0 | const char *s; |
2950 | 0 | size_t n; |
2951 | 0 | char *p; |
2952 | 0 | int pkalgo; |
2953 | 0 | int i; |
2954 | 0 | gcry_mpi_t keyparam[10] = { NULL }; |
2955 | 0 | struct b64state b64_state; |
2956 | |
|
2957 | 0 | init_membuf_secure (&mb, 1024); |
2958 | 0 | init_membuf_secure (&mb2, 1024); |
2959 | | |
2960 | | /* Check that a keygrip has been given. */ |
2961 | 0 | err = classify_user_id (userid, &desc, 1); |
2962 | 0 | if (err || desc.mode != KEYDB_SEARCH_MODE_KEYGRIP ) |
2963 | 0 | { |
2964 | 0 | log_error (_("key \"%s\" not found: %s\n"), userid, |
2965 | 0 | err? gpg_strerror (err) : "Not a Keygrip" ); |
2966 | 0 | return err; |
2967 | 0 | } |
2968 | | |
2969 | 0 | bin2hex (desc.u.grip, KEYGRIP_LEN, hexgrip); |
2970 | |
|
2971 | 0 | if ((err = get_keywrap_key (ctrl, &cipherhd))) |
2972 | 0 | goto leave; |
2973 | | |
2974 | 0 | err = receive_seckey_from_agent (ctrl, cipherhd, 0, 0, NULL, hexgrip, NULL, |
2975 | 0 | &skey); |
2976 | 0 | if (err) |
2977 | 0 | goto leave; |
2978 | | |
2979 | | /* Get the type of the key expression. */ |
2980 | 0 | s = gcry_sexp_nth_data (skey, 0, &n); |
2981 | 0 | if (!s || !(n == 11 && !memcmp (s, "private-key", 11))) |
2982 | 0 | { |
2983 | 0 | log_info ("Note: only on-disk secret keys may be exported\n"); |
2984 | 0 | err = gpg_error (GPG_ERR_NO_SECKEY); |
2985 | 0 | goto leave; |
2986 | 0 | } |
2987 | | |
2988 | 0 | mb_write_cstring (&mb, "openssh-key-v1"); /* Auth_Magic. */ |
2989 | 0 | mb_write_string (&mb, "none", 4); /* ciphername */ |
2990 | 0 | mb_write_string (&mb, "none", 4); /* kdfname */ |
2991 | 0 | mb_write_uint32 (&mb, 0); /* kdfoptions */ |
2992 | 0 | mb_write_uint32 (&mb, 1); /* number of keys */ |
2993 | |
|
2994 | 0 | pkalgo = get_pk_algo_from_key (skey); |
2995 | 0 | switch (pkalgo) |
2996 | 0 | { |
2997 | 0 | case PUBKEY_ALGO_RSA: |
2998 | 0 | case PUBKEY_ALGO_RSA_S: |
2999 | 0 | identifier = "ssh-rsa"; |
3000 | 0 | err = gcry_sexp_extract_param (skey, NULL, "nedpq", |
3001 | 0 | &keyparam[0], |
3002 | 0 | &keyparam[1], |
3003 | 0 | &keyparam[2], |
3004 | 0 | &keyparam[3], |
3005 | 0 | &keyparam[4], |
3006 | 0 | NULL); |
3007 | 0 | if (err) |
3008 | 0 | goto leave; |
3009 | 0 | mb_write_string (&mb2, identifier, strlen (identifier)); |
3010 | 0 | mb_write_mpi (&mb2, keyparam[1], 0); /* e (right, e is first here) */ |
3011 | 0 | mb_write_mpi (&mb2, keyparam[0], 0); /* n */ |
3012 | | /* Append public to the output block as an SSH string. */ |
3013 | 0 | p = get_membuf (&mb2, &n); |
3014 | 0 | if (!p) |
3015 | 0 | { |
3016 | 0 | err = gpg_error_from_syserror (); |
3017 | 0 | goto leave; |
3018 | 0 | } |
3019 | 0 | mb_write_string (&mb, p, n); |
3020 | 0 | xfree (p); |
3021 | 0 | init_membuf_secure (&mb2, 1024); |
3022 | 0 | mb_write_string (&mb2, identifier, strlen (identifier)); |
3023 | 0 | { |
3024 | 0 | char checkbytes[4]; |
3025 | 0 | gcry_create_nonce (checkbytes, sizeof checkbytes); |
3026 | 0 | mb_write_data (&mb2, checkbytes, sizeof checkbytes); |
3027 | 0 | mb_write_data (&mb2, checkbytes, sizeof checkbytes); |
3028 | 0 | } |
3029 | 0 | mb_write_mpi (&mb2, keyparam[0], 0); /* n */ |
3030 | 0 | mb_write_mpi (&mb2, keyparam[1], 0); /* e */ |
3031 | | /*FIXME: Fixup u,p,q to match the OpenSSH format. */ |
3032 | 0 | mb_write_mpi (&mb2, keyparam[2], 0); /* d */ |
3033 | 0 | mb_write_mpi (&mb2, keyparam[1], 0); /* iqmp1 */ |
3034 | 0 | mb_write_mpi (&mb2, keyparam[3], 0); /* p */ |
3035 | 0 | mb_write_mpi (&mb2, keyparam[4], 0); /* q */ |
3036 | | /* Fixme: take the comment from skey. */ |
3037 | 0 | mb_write_string (&mb2, "<comment>", 9); |
3038 | | /* Pad to a blocksize of 8 (for cipher "none"). */ |
3039 | 0 | i = 0; |
3040 | 0 | while (peek_membuf (&mb2, &n) && (n % 8)) |
3041 | 0 | mb_write_uint8 (&mb2, ++i); |
3042 | | /* Append encrypted block to the output as an SSH string. */ |
3043 | 0 | p = get_membuf (&mb2, &n); |
3044 | 0 | if (!p) |
3045 | 0 | { |
3046 | 0 | err = gpg_error_from_syserror (); |
3047 | 0 | goto leave; |
3048 | 0 | } |
3049 | 0 | mb_write_string (&mb, p, n); |
3050 | 0 | xfree (p); |
3051 | 0 | err = gpg_error (GPG_ERR_NOT_IMPLEMENTED); |
3052 | 0 | break; |
3053 | | |
3054 | | /* case PUBKEY_ALGO_ECDSA: */ |
3055 | | /* { */ |
3056 | | /* char *curveoid; */ |
3057 | | /* const char *curve; */ |
3058 | | |
3059 | | /* curveoid = openpgp_oid_to_str (pk->pkey[0]); */ |
3060 | | /* if (!curveoid) */ |
3061 | | /* err = gpg_error_from_syserror (); */ |
3062 | | /* else if (!(curve = openpgp_oid_to_curve (curveoid, 0))) */ |
3063 | | /* err = gpg_error (GPG_ERR_UNKNOWN_CURVE); */ |
3064 | | /* else */ |
3065 | | /* { */ |
3066 | | /* if (!strcmp (curve, "nistp256")) */ |
3067 | | /* identifier = "ecdsa-sha2-nistp256"; */ |
3068 | | /* else if (!strcmp (curve, "nistp384")) */ |
3069 | | /* identifier = "ecdsa-sha2-nistp384"; */ |
3070 | | /* else if (!strcmp (curve, "nistp521")) */ |
3071 | | /* identifier = "ecdsa-sha2-nistp521"; */ |
3072 | | |
3073 | | /* if (!identifier) */ |
3074 | | /* err = gpg_error (GPG_ERR_UNKNOWN_CURVE); */ |
3075 | | /* else */ |
3076 | | /* err = key_to_sshblob (&mb, identifier, pk->pkey[1], NULL); */ |
3077 | | /* } */ |
3078 | | /* xfree (curveoid); */ |
3079 | | /* } */ |
3080 | | /* break; */ |
3081 | | |
3082 | | /* case PUBKEY_ALGO_EDDSA: */ |
3083 | | /* if (openpgp_oid_is_ed25519 (pk->pkey[0])) */ |
3084 | | /* { */ |
3085 | | /* identifier = "ssh-ed25519"; */ |
3086 | | /* err = key_to_sshblob (&mb, identifier, pk->pkey[1], NULL); */ |
3087 | | /* } */ |
3088 | | /* else if (openpgp_oid_is_ed448 (pk->pkey[0])) */ |
3089 | | /* { */ |
3090 | | /* identifier = "ssh-ed448"; */ |
3091 | | /* err = key_to_sshblob (&mb, identifier, pk->pkey[1], NULL); */ |
3092 | | /* } */ |
3093 | | /* else */ |
3094 | | /* err = gpg_error (GPG_ERR_UNKNOWN_CURVE); */ |
3095 | | /* break; */ |
3096 | | |
3097 | 0 | case PUBKEY_ALGO_DSA: |
3098 | 0 | log_info ("Note: export of ssh-dsa keys is not supported\n"); |
3099 | 0 | err = gpg_error (GPG_ERR_NOT_SUPPORTED); |
3100 | 0 | break; |
3101 | | |
3102 | 0 | case PUBKEY_ALGO_ELGAMAL_E: |
3103 | 0 | case PUBKEY_ALGO_ELGAMAL: |
3104 | 0 | err = gpg_error (GPG_ERR_UNUSABLE_SECKEY); |
3105 | 0 | break; |
3106 | | |
3107 | 0 | default: |
3108 | 0 | err = GPG_ERR_PUBKEY_ALGO; |
3109 | 0 | break; |
3110 | 0 | } |
3111 | | |
3112 | 0 | if (err) |
3113 | 0 | goto leave; |
3114 | | |
3115 | 0 | blob = get_membuf (&mb, &bloblen); |
3116 | 0 | if (!blob) |
3117 | 0 | { |
3118 | 0 | err = gpg_error_from_syserror (); |
3119 | 0 | goto leave; |
3120 | 0 | } |
3121 | | |
3122 | 0 | if (opt.outfile && *opt.outfile && strcmp (opt.outfile, "-")) |
3123 | 0 | fp = es_fopen ((fname = opt.outfile), "w"); |
3124 | 0 | else |
3125 | 0 | fp = es_stdout; |
3126 | 0 | if (!fp) |
3127 | 0 | { |
3128 | 0 | err = gpg_error_from_syserror (); |
3129 | 0 | log_error (_("error creating '%s': %s\n"), fname, gpg_strerror (err)); |
3130 | 0 | goto leave; |
3131 | 0 | } |
3132 | | |
3133 | 0 | err = b64enc_start_es (&b64_state, fp, "OPENSSH PRIVATE_KEY"); |
3134 | 0 | if (err) |
3135 | 0 | goto leave; |
3136 | 0 | err = b64enc_write (&b64_state, blob, bloblen); |
3137 | 0 | b64enc_finish (&b64_state); |
3138 | 0 | if (err) |
3139 | 0 | goto leave; |
3140 | | |
3141 | 0 | if (es_ferror (fp)) |
3142 | 0 | err = gpg_error_from_syserror (); |
3143 | 0 | else |
3144 | 0 | { |
3145 | 0 | if (fp != es_stdout && es_fclose (fp)) |
3146 | 0 | err = gpg_error_from_syserror (); |
3147 | 0 | fp = NULL; |
3148 | 0 | } |
3149 | |
|
3150 | 0 | log_info ("Beware: the private key is not protected;" |
3151 | 0 | " use \"ssh-keygen -p\" to protect it\n"); |
3152 | 0 | if (err) |
3153 | 0 | log_error (_("error writing '%s': %s\n"), fname, gpg_strerror (err)); |
3154 | | |
3155 | |
|
3156 | 0 | leave: |
3157 | 0 | xfree (blob); |
3158 | 0 | gcry_sexp_release (skey); |
3159 | 0 | gcry_sexp_release (skeyalgo); |
3160 | 0 | gcry_cipher_close (cipherhd); |
3161 | 0 | xfree (get_membuf (&mb2, NULL)); |
3162 | 0 | xfree (get_membuf (&mb, NULL)); |
3163 | 0 | if (fp != es_stdout) |
3164 | 0 | es_fclose (fp); |
3165 | 0 | return err; |
3166 | 0 | } |