Line | Count | Source |
1 | | /* keygen.c - Generate a key pair |
2 | | * Copyright (C) 1998-2007, 2009-2011 Free Software Foundation, Inc. |
3 | | * Copyright (C) 2014, 2015, 2016, 2017, 2018 Werner Koch |
4 | | * Copyright (C) 2020, 2024 g10 Code GmbH |
5 | | * |
6 | | * This file is part of GnuPG. |
7 | | * |
8 | | * GnuPG is free software; you can redistribute it and/or modify |
9 | | * it under the terms of the GNU General Public License as published by |
10 | | * the Free Software Foundation; either version 3 of the License, or |
11 | | * (at your option) any later version. |
12 | | * |
13 | | * GnuPG is distributed in the hope that it will be useful, |
14 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | | * GNU General Public License for more details. |
17 | | * |
18 | | * You should have received a copy of the GNU General Public License |
19 | | * along with this program; if not, see <https://www.gnu.org/licenses/>. |
20 | | */ |
21 | | |
22 | | #include <config.h> |
23 | | #include <stdio.h> |
24 | | #include <stdlib.h> |
25 | | #include <string.h> |
26 | | #include <ctype.h> |
27 | | #include <errno.h> |
28 | | #include <sys/types.h> |
29 | | #include <sys/stat.h> |
30 | | #include <unistd.h> |
31 | | |
32 | | #include "gpg.h" |
33 | | #include "../common/util.h" |
34 | | #include "main.h" |
35 | | #include "packet.h" |
36 | | #include "../common/ttyio.h" |
37 | | #include "options.h" |
38 | | #include "keydb.h" |
39 | | #include "trustdb.h" |
40 | | #include "../common/status.h" |
41 | | #include "../common/i18n.h" |
42 | | #include "keyserver-internal.h" |
43 | | #include "call-agent.h" |
44 | | #include "pkglue.h" |
45 | | #include "../common/shareddefs.h" |
46 | | #include "../common/host2net.h" |
47 | | #include "../common/mbox-util.h" |
48 | | |
49 | | |
50 | | /* The default algorithms. If you change them, you should ensure the |
51 | | * value is inside the bounds enforced by ask_keysize and gen_xxx. |
52 | | * See also get_keysize_range which encodes the allowed ranges. The |
53 | | * default answer in ask_algo also needs to be adjusted. For Kyber |
54 | | * keep the values set in generate_subkeypair in sync. */ |
55 | 0 | #define DEFAULT_STD_KEY_PARAM "ed25519/cert,sign+cv25519/encr" |
56 | 0 | #define FUTURE_STD_KEY_PARAM "ed25519/cert,sign+cv25519/encr" |
57 | 0 | #define PQC_STD_KEY_PARAM_PRI "bp384/cert,sign" |
58 | 0 | #define PQC_STD_KEY_PARAM_SUB "kyber768_bp256/encr" |
59 | 0 | #define PQC_STD_KEY_PARAM PQC_STD_KEY_PARAM_PRI "+" PQC_STD_KEY_PARAM_SUB |
60 | 0 | #define PQC9980_STD_KEY_PARAM_PRI "ietf27/cert,sign" |
61 | 0 | #define PQC9980_STD_KEY_PARAM_SUB "mlk768_bp384/encr" |
62 | 0 | #define PQC9980_STD_KEY_PARAM PQC9980_STD_KEY_PARAM_PRI \ |
63 | 0 | "+" PQC9980_STD_KEY_PARAM_SUB |
64 | | |
65 | | /* When generating keys using the streamlined key generation dialog, |
66 | | use this as a default expiration interval. */ |
67 | | const char *default_expiration_interval = "3y"; |
68 | | |
69 | | /* Flag bits used during key generation. */ |
70 | 0 | #define KEYGEN_FLAG_NO_PROTECTION 1 |
71 | 0 | #define KEYGEN_FLAG_TRANSIENT_KEY 2 |
72 | 0 | #define KEYGEN_FLAG_CREATE_V5_KEY 4 |
73 | 0 | #define KEYGEN_FLAG_CREATE_V6_KEY 8 |
74 | | |
75 | | /* Maximum number of supported algorithm preferences. */ |
76 | 0 | #define MAX_PREFS 30 |
77 | | |
78 | | enum para_name { |
79 | | pKEYTYPE, |
80 | | pKEYLENGTH, |
81 | | pKEYCURVE, |
82 | | pKEYUSAGE, |
83 | | pSUBKEYTYPE, |
84 | | pSUBKEYLENGTH, |
85 | | pSUBKEYCURVE, |
86 | | pSUBKEYUSAGE, |
87 | | pAUTHKEYTYPE, |
88 | | pNAMEREAL, |
89 | | pNAMEEMAIL, |
90 | | pNAMECOMMENT, |
91 | | pPREFERENCES, |
92 | | pREVOKER, |
93 | | pUSERID, |
94 | | pCREATIONDATE, |
95 | | pKEYCREATIONDATE, /* Same in seconds since epoch. */ |
96 | | pEXPIREDATE, |
97 | | pKEYEXPIRE, /* in n seconds */ |
98 | | pSUBKEYCREATIONDATE, |
99 | | pSUBKEYEXPIREDATE, |
100 | | pSUBKEYEXPIRE, /* in n seconds */ |
101 | | pAUTHKEYCREATIONDATE, /* Not yet used. */ |
102 | | pPASSPHRASE, |
103 | | pSERIALNO, |
104 | | pCARDBACKUPKEY, |
105 | | pHANDLE, |
106 | | pKEYSERVER, |
107 | | pKEYGRIP, |
108 | | pSUBKEYGRIP, |
109 | | pADSK, /* this uses u.adsk */ |
110 | | pVERSION, /* Desired version of the key packet. */ |
111 | | pSUBVERSION, /* Ditto for the subpacket. */ |
112 | | pCARDKEY /* The keygrips have been taken from active card (bool). */ |
113 | | }; |
114 | | |
115 | | struct para_data_s { |
116 | | struct para_data_s *next; |
117 | | int lnr; |
118 | | enum para_name key; |
119 | | union { |
120 | | u32 expire; |
121 | | u32 creation; |
122 | | int abool; |
123 | | unsigned int usage; |
124 | | struct revocation_key revkey; |
125 | | PKT_public_key *adsk; /* used with key == pADSK */ |
126 | | char value[1]; |
127 | | } u; |
128 | | }; |
129 | | |
130 | | struct output_control_s |
131 | | { |
132 | | int lnr; |
133 | | int dryrun; |
134 | | unsigned int keygen_flags; |
135 | | int use_files; |
136 | | struct { |
137 | | char *fname; |
138 | | char *newfname; |
139 | | IOBUF stream; |
140 | | armor_filter_context_t *afx; |
141 | | } pub; |
142 | | }; |
143 | | |
144 | | |
145 | | /* An object to help communicating with the actual key generation |
146 | | * code. */ |
147 | | struct common_gen_cb_parm_s |
148 | | { |
149 | | /* This variable set to the result of agent_genkey. The callback |
150 | | * may take a copy of this so that the result can be used after we |
151 | | * are back from the deep key generation call stack. */ |
152 | | gcry_sexp_t genkey_result; |
153 | | /* For a dual algorithms the result of the second algorithm |
154 | | * (e.g. Kyber). */ |
155 | | gcry_sexp_t genkey_result2; |
156 | | }; |
157 | | typedef struct common_gen_cb_parm_s *common_gen_cb_parm_t; |
158 | | |
159 | | |
160 | | /* A communication object to help adding certain notations to a key |
161 | | * binding signature. */ |
162 | | struct opaque_data_usage_and_pk |
163 | | { |
164 | | unsigned int usage; |
165 | | const char *cpl_notation; |
166 | | PKT_public_key *pk; |
167 | | }; |
168 | | |
169 | | |
170 | | /* FIXME: These globals vars are ugly. And using MAX_PREFS even for |
171 | | * aeads is useless, given that we don't expects more than a very few |
172 | | * algorithms. */ |
173 | | static int prefs_initialized = 0; |
174 | | static byte sym_prefs[MAX_PREFS]; |
175 | | static int nsym_prefs; |
176 | | static byte hash_prefs[MAX_PREFS]; |
177 | | static int nhash_prefs; |
178 | | static byte zip_prefs[MAX_PREFS]; |
179 | | static int nzip_prefs; |
180 | | static byte aead_prefs[MAX_PREFS]; |
181 | | static int naead_prefs; |
182 | | static int mdc_available; |
183 | | static int ks_modify; |
184 | | static int aead_available; |
185 | | |
186 | | static void release_parameter_list (struct para_data_s *r); |
187 | | static struct para_data_s *prepare_adsk (ctrl_t ctrl, const char *name); |
188 | | static gpg_error_t parse_algo_usage_expire (ctrl_t ctrl, int for_subkey, |
189 | | const char *algostr, const char *usagestr, |
190 | | const char *expirestr, |
191 | | int *r_algo, unsigned int *r_usage, |
192 | | u32 *r_expire, unsigned int *r_nbits, |
193 | | const char **r_curve, int *r_version, |
194 | | char **r_keygrip, u32 *r_keytime); |
195 | | static void do_generate_keypair (ctrl_t ctrl, struct para_data_s *para, |
196 | | struct output_control_s *outctrl, int card ); |
197 | | static int write_keyblock (iobuf_t out, kbnode_t node); |
198 | | static gpg_error_t gen_card_key (int keyno, int algo, int is_primary, |
199 | | kbnode_t pub_root, u32 *timestamp, |
200 | | u32 expireval, int *keygen_flags); |
201 | | static unsigned int get_keysize_range (int algo, |
202 | | unsigned int *min, unsigned int *max); |
203 | | static void do_add_notation (PKT_signature *sig, |
204 | | const char *name, const char *value, |
205 | | int critical); |
206 | | |
207 | | |
208 | | |
209 | | /* Return the algo string for a default new key. */ |
210 | | const char * |
211 | | get_default_pubkey_algo (void) |
212 | 0 | { |
213 | 0 | if (opt.def_new_key_algo) |
214 | 0 | { |
215 | 0 | if (*opt.def_new_key_algo && !strchr (opt.def_new_key_algo, ':')) |
216 | 0 | return opt.def_new_key_algo; |
217 | | /* To avoid checking that option every time we delay that until |
218 | | * here. The only thing we really need to make sure is that |
219 | | * there is no colon in the string so that the --gpgconf-list |
220 | | * command won't mess up its output. */ |
221 | 0 | log_info (_("invalid value for option '%s'\n"), "--default-new-key-algo"); |
222 | 0 | } |
223 | 0 | return DEFAULT_STD_KEY_PARAM; |
224 | 0 | } |
225 | | |
226 | | |
227 | | /* Depending on the USE some public key algorithms need to be changed. |
228 | | * In particular this is the case for standard EC curves which may |
229 | | * have either ECDSA or ECDH as their algo. The function returns the |
230 | | * new algo if demanded by USE. IF the function can't decide the algo |
231 | | * is returned as is and it is expected that a letter error check will |
232 | | * kick in. If no change is required ALGO is returned as is. */ |
233 | | static int |
234 | | adjust_algo_for_ecdh_ecdsa (int algo, unsigned int use, const char *curve) |
235 | 0 | { |
236 | 0 | int needalgo; |
237 | |
|
238 | 0 | if (algo != PUBKEY_ALGO_ECDSA && algo != PUBKEY_ALGO_ECDH) |
239 | 0 | return algo; /* Not an algo we need to adjust. */ |
240 | | |
241 | 0 | if (!curve || !*curve) |
242 | 0 | return algo; /* No curve given and thus we can't decide. */ |
243 | 0 | if (!openpgp_is_curve_supported (curve, &needalgo, NULL)) |
244 | 0 | return algo; /* Curve not supported - can't decide. */ |
245 | 0 | if (needalgo) |
246 | 0 | return algo; /* No need to map the X{25519,488} curves because we |
247 | | * would also need to change the curve. */ |
248 | | |
249 | 0 | if (algo == PUBKEY_ALGO_ECDH |
250 | 0 | && (use & (PUBKEY_USAGE_SIG|PUBKEY_USAGE_AUTH|PUBKEY_USAGE_CERT))) |
251 | 0 | return PUBKEY_ALGO_ECDSA; /* Switch to the signing variant. */ |
252 | | |
253 | 0 | if (algo == PUBKEY_ALGO_ECDSA |
254 | 0 | && (use & (PUBKEY_USAGE_ENC))) |
255 | 0 | return PUBKEY_ALGO_ECDH; /* Switch to the encryption variant. */ |
256 | | |
257 | 0 | return algo; /* Return as is. */ |
258 | 0 | } |
259 | | |
260 | | |
261 | | static void |
262 | | print_status_key_created (int letter, PKT_public_key *pk, const char *handle) |
263 | 0 | { |
264 | 0 | byte array[MAX_FINGERPRINT_LEN], *s; |
265 | 0 | char *buf, *p; |
266 | 0 | size_t i, n; |
267 | |
|
268 | 0 | if (!handle) |
269 | 0 | handle = ""; |
270 | |
|
271 | 0 | buf = xmalloc (MAX_FINGERPRINT_LEN*2+31 + strlen (handle) + 1); |
272 | |
|
273 | 0 | p = buf; |
274 | 0 | if (letter || pk) |
275 | 0 | { |
276 | 0 | *p++ = letter; |
277 | 0 | if (pk) |
278 | 0 | { |
279 | 0 | *p++ = ' '; |
280 | 0 | fingerprint_from_pk (pk, array, &n); |
281 | 0 | s = array; |
282 | | /* Fixme: Use bin2hex */ |
283 | 0 | for (i=0; i < n ; i++, s++, p += 2) |
284 | 0 | snprintf (p, 3, "%02X", *s); |
285 | 0 | } |
286 | 0 | } |
287 | 0 | if (*handle) |
288 | 0 | { |
289 | 0 | *p++ = ' '; |
290 | 0 | for (i=0; handle[i] && i < 100; i++) |
291 | 0 | *p++ = isspace ((unsigned int)handle[i])? '_':handle[i]; |
292 | 0 | } |
293 | 0 | *p = 0; |
294 | 0 | write_status_text ((letter || pk)?STATUS_KEY_CREATED:STATUS_KEY_NOT_CREATED, |
295 | 0 | buf); |
296 | 0 | xfree (buf); |
297 | 0 | } |
298 | | |
299 | | static void |
300 | | print_status_key_not_created (const char *handle) |
301 | 0 | { |
302 | 0 | print_status_key_created (0, NULL, handle); |
303 | 0 | } |
304 | | |
305 | | |
306 | | |
307 | | static gpg_error_t |
308 | | write_uid (kbnode_t root, const char *s) |
309 | 0 | { |
310 | 0 | PACKET *pkt = NULL; |
311 | 0 | size_t n = strlen (s); |
312 | |
|
313 | 0 | if (n > MAX_UID_PACKET_LENGTH - 10) |
314 | 0 | return gpg_error (GPG_ERR_INV_USER_ID); |
315 | | |
316 | 0 | pkt = xmalloc_clear (sizeof *pkt); |
317 | 0 | pkt->pkttype = PKT_USER_ID; |
318 | 0 | pkt->pkt.user_id = xmalloc_clear (sizeof *pkt->pkt.user_id + n); |
319 | 0 | pkt->pkt.user_id->len = n; |
320 | 0 | pkt->pkt.user_id->ref = 1; |
321 | 0 | strcpy (pkt->pkt.user_id->name, s); |
322 | 0 | add_kbnode (root, new_kbnode (pkt)); |
323 | 0 | return 0; |
324 | 0 | } |
325 | | |
326 | | static void |
327 | | do_add_key_flags (PKT_signature *sig, unsigned int use) |
328 | 0 | { |
329 | 0 | byte buf[2] = { 0, 0 }; |
330 | | |
331 | | /* The spec says that all primary keys MUST be able to certify. */ |
332 | 0 | if ( sig->sig_class != 0x18 ) |
333 | 0 | buf[0] |= 0x01; |
334 | |
|
335 | 0 | if (use & PUBKEY_USAGE_SIG) |
336 | 0 | buf[0] |= 0x02; |
337 | 0 | if (use & PUBKEY_USAGE_ENC) |
338 | 0 | buf[0] |= 0x04 | 0x08; |
339 | 0 | if (use & PUBKEY_USAGE_AUTH) |
340 | 0 | buf[0] |= 0x20; |
341 | 0 | if (use & PUBKEY_USAGE_GROUP) |
342 | 0 | buf[0] |= 0x80; |
343 | |
|
344 | 0 | if (use & PUBKEY_USAGE_RENC) |
345 | 0 | buf[1] |= 0x04; |
346 | 0 | if (use & PUBKEY_USAGE_TIME) |
347 | 0 | buf[1] |= 0x08; |
348 | |
|
349 | 0 | build_sig_subpkt (sig, SIGSUBPKT_KEY_FLAGS, buf, buf[1]? 2:1); |
350 | 0 | } |
351 | | |
352 | | |
353 | | int |
354 | | keygen_add_key_expire (PKT_signature *sig, void *opaque) |
355 | 0 | { |
356 | 0 | PKT_public_key *pk = opaque; |
357 | 0 | byte buf[8]; |
358 | 0 | u32 u; |
359 | |
|
360 | 0 | if (pk->expiredate) |
361 | 0 | { |
362 | 0 | if (pk->expiredate > pk->timestamp) |
363 | 0 | u = pk->expiredate - pk->timestamp; |
364 | 0 | else |
365 | 0 | u = 1; |
366 | |
|
367 | 0 | buf[0] = (u >> 24) & 0xff; |
368 | 0 | buf[1] = (u >> 16) & 0xff; |
369 | 0 | buf[2] = (u >> 8) & 0xff; |
370 | 0 | buf[3] = u & 0xff; |
371 | 0 | build_sig_subpkt (sig, SIGSUBPKT_KEY_EXPIRE, buf, 4); |
372 | 0 | } |
373 | 0 | else |
374 | 0 | { |
375 | | /* Make sure we don't leave a key expiration subpacket lying |
376 | | around */ |
377 | 0 | delete_sig_subpkt (sig->hashed, SIGSUBPKT_KEY_EXPIRE); |
378 | 0 | } |
379 | |
|
380 | 0 | return 0; |
381 | 0 | } |
382 | | |
383 | | |
384 | | /* Add the key usage (i.e. key flags) in SIG from the public keys |
385 | | * pubkey_usage field. OPAQUE has the public key. */ |
386 | | int |
387 | | keygen_add_key_flags (PKT_signature *sig, void *opaque) |
388 | 0 | { |
389 | 0 | PKT_public_key *pk = opaque; |
390 | |
|
391 | 0 | do_add_key_flags (sig, pk->pubkey_usage); |
392 | 0 | return 0; |
393 | 0 | } |
394 | | |
395 | | |
396 | | int |
397 | | keygen_add_key_flags_and_expire (PKT_signature *sig, void *opaque) |
398 | 0 | { |
399 | 0 | keygen_add_key_flags (sig, opaque); |
400 | 0 | return keygen_add_key_expire (sig, opaque); |
401 | 0 | } |
402 | | |
403 | | |
404 | | /* This is only used to write the key binding signature. It is not |
405 | | * used for the primary key. */ |
406 | | static int |
407 | | keygen_add_key_flags_from_oduap (PKT_signature *sig, void *opaque) |
408 | 0 | { |
409 | 0 | struct opaque_data_usage_and_pk *oduap = opaque; |
410 | |
|
411 | 0 | do_add_key_flags (sig, oduap->usage); |
412 | 0 | if (oduap->cpl_notation) |
413 | 0 | do_add_notation (sig, "cpl@gnupg.org", oduap->cpl_notation, 0); |
414 | 0 | return keygen_add_key_expire (sig, oduap->pk); |
415 | 0 | } |
416 | | |
417 | | |
418 | | static int |
419 | | set_one_pref (int val, int type, const char *item, byte *buf, int *nbuf) |
420 | 0 | { |
421 | 0 | int i; |
422 | |
|
423 | 0 | for (i=0; i < *nbuf; i++ ) |
424 | 0 | if (buf[i] == val) |
425 | 0 | { |
426 | 0 | log_info (_("preference '%s' duplicated\n"), item); |
427 | 0 | return -1; |
428 | 0 | } |
429 | | |
430 | 0 | if (*nbuf >= MAX_PREFS) |
431 | 0 | { |
432 | 0 | if(type==1) |
433 | 0 | log_info(_("too many cipher preferences\n")); |
434 | 0 | else if(type==2) |
435 | 0 | log_info(_("too many digest preferences\n")); |
436 | 0 | else if(type==3) |
437 | 0 | log_info(_("too many compression preferences\n")); |
438 | 0 | else if(type==4) |
439 | 0 | log_info(_("too many AEAD preferences\n")); |
440 | 0 | else |
441 | 0 | BUG(); |
442 | | |
443 | 0 | return -1; |
444 | 0 | } |
445 | | |
446 | 0 | buf[(*nbuf)++] = val; |
447 | 0 | return 0; |
448 | 0 | } |
449 | | |
450 | | /* |
451 | | * Parse the supplied string and use it to set the standard |
452 | | * preferences. The string may be in a form like the one printed by |
453 | | * "pref" (something like: "S10 S3 H3 H2 Z2 Z1") or the actual |
454 | | * cipher/hash/compress names. Use NULL to set the default |
455 | | * preferences. Returns: 0 = okay |
456 | | * PERSONAL is either 0 or one PREFTYPE_* |
457 | | */ |
458 | | int |
459 | | keygen_set_std_prefs (const char *string,int personal) |
460 | 0 | { |
461 | 0 | byte sym[MAX_PREFS], hash[MAX_PREFS], zip[MAX_PREFS], aead[MAX_PREFS]; |
462 | 0 | int nsym=0, nhash=0, nzip=0, naead=0, val, rc=0; |
463 | 0 | int mdc=1, modify=0; /* mdc defaults on, modify defaults off. */ |
464 | 0 | char dummy_string[25*4+1]; /* Enough for 25 items. */ |
465 | |
|
466 | 0 | if (!string || !ascii_strcasecmp (string, "default")) |
467 | 0 | { |
468 | 0 | if (opt.def_preference_list) |
469 | 0 | string=opt.def_preference_list; |
470 | 0 | else |
471 | 0 | { |
472 | 0 | int any_compress = 0; |
473 | 0 | dummy_string[0]='\0'; |
474 | | |
475 | | /* The rationale why we use the order AES256,192,128 is |
476 | | for compatibility reasons with PGP. If gpg would |
477 | | define AES128 first, we would get the somewhat |
478 | | confusing situation: |
479 | | |
480 | | gpg -r pgpkey -r gpgkey ---gives--> AES256 |
481 | | gpg -r gpgkey -r pgpkey ---gives--> AES |
482 | | |
483 | | Note that by using --personal-cipher-preferences it is |
484 | | possible to prefer AES128. |
485 | | */ |
486 | | |
487 | | /* Make sure we do not add more than a total of 15 items |
488 | | * here, as we could overflow the size of dummy_string. |
489 | | * Note further that we try to add AES/3DES despite that |
490 | | * they are anyway implictly used by LibrePGP/rfc4880. */ |
491 | 0 | if ( !openpgp_cipher_test_algo (CIPHER_ALGO_AES256) ) |
492 | 0 | strcat(dummy_string,"S9 "); |
493 | 0 | if ( !openpgp_cipher_test_algo (CIPHER_ALGO_AES192) ) |
494 | 0 | strcat(dummy_string,"S8 "); |
495 | 0 | if ( !openpgp_cipher_test_algo (CIPHER_ALGO_AES) ) |
496 | 0 | strcat(dummy_string,"S7 "); /* AES-128 - LibrePGP default. */ |
497 | 0 | if ( !openpgp_cipher_test_algo (CIPHER_ALGO_3DES) ) |
498 | 0 | strcat(dummy_string,"S2 "); /* 3DES - RFC4880 default. */ |
499 | |
|
500 | 0 | if (!openpgp_aead_test_algo (AEAD_ALGO_OCB)) |
501 | 0 | strcat(dummy_string,"A2 "); |
502 | |
|
503 | 0 | if (personal) |
504 | 0 | { |
505 | | /* The default internal hash algo order is: |
506 | | * SHA-256, SHA-384, SHA-512, SHA-224, SHA-1. |
507 | | */ |
508 | 0 | if (!openpgp_md_test_algo (DIGEST_ALGO_SHA256)) |
509 | 0 | strcat (dummy_string, "H8 "); |
510 | |
|
511 | 0 | if (!openpgp_md_test_algo (DIGEST_ALGO_SHA384)) |
512 | 0 | strcat (dummy_string, "H9 "); |
513 | |
|
514 | 0 | if (!openpgp_md_test_algo (DIGEST_ALGO_SHA512)) |
515 | 0 | strcat (dummy_string, "H10 "); |
516 | 0 | } |
517 | 0 | else |
518 | 0 | { |
519 | | /* The default advertised hash algo order is: |
520 | | * SHA-512, SHA-384, SHA-256, SHA-224, SHA-1. |
521 | | */ |
522 | 0 | if (!openpgp_md_test_algo (DIGEST_ALGO_SHA512)) |
523 | 0 | strcat (dummy_string, "H10 "); |
524 | |
|
525 | 0 | if (!openpgp_md_test_algo (DIGEST_ALGO_SHA384)) |
526 | 0 | strcat (dummy_string, "H9 "); |
527 | |
|
528 | 0 | if (!openpgp_md_test_algo (DIGEST_ALGO_SHA256)) |
529 | 0 | strcat (dummy_string, "H8 "); |
530 | 0 | } |
531 | |
|
532 | 0 | if (!openpgp_md_test_algo (DIGEST_ALGO_SHA224)) |
533 | 0 | strcat (dummy_string, "H11 "); |
534 | |
|
535 | 0 | strcat (dummy_string, "H2 "); /* SHA-1 */ |
536 | |
|
537 | 0 | if(!check_compress_algo(COMPRESS_ALGO_ZLIB)) |
538 | 0 | { |
539 | 0 | strcat(dummy_string,"Z2 "); |
540 | 0 | any_compress = 1; |
541 | 0 | } |
542 | |
|
543 | 0 | if(!check_compress_algo(COMPRESS_ALGO_BZIP2)) |
544 | 0 | { |
545 | 0 | strcat(dummy_string,"Z3 "); |
546 | 0 | any_compress = 1; |
547 | 0 | } |
548 | |
|
549 | 0 | if(!check_compress_algo(COMPRESS_ALGO_ZIP)) |
550 | 0 | { |
551 | 0 | strcat(dummy_string,"Z1 "); |
552 | 0 | any_compress = 1; |
553 | 0 | } |
554 | | |
555 | | /* In case we have no compress algo at all, declare that |
556 | | we prefer no compression. */ |
557 | 0 | if (!any_compress) |
558 | 0 | strcat(dummy_string,"Z0 "); |
559 | | |
560 | | /* Remove the trailing space. */ |
561 | 0 | if (*dummy_string && dummy_string[strlen (dummy_string)-1] == ' ') |
562 | 0 | dummy_string[strlen (dummy_string)-1] = 0; |
563 | |
|
564 | 0 | string=dummy_string; |
565 | 0 | } |
566 | 0 | } |
567 | 0 | else if (!ascii_strcasecmp (string, "none")) |
568 | 0 | string = ""; |
569 | |
|
570 | 0 | if(strlen(string)) |
571 | 0 | { |
572 | 0 | char *prefstringbuf; |
573 | 0 | char *tok, *prefstring; |
574 | 0 | int any_cipher=0, any_digest=0, any_compress=0, any_aead=0; |
575 | 0 | int err_cipher=0, err_digest=0, err_compress=0, err_aead=0; |
576 | | |
577 | | /* We need a writable string. */ |
578 | 0 | prefstring = prefstringbuf = xstrdup (string); |
579 | |
|
580 | 0 | while((tok=strsep(&prefstring," ,"))) |
581 | 0 | { |
582 | 0 | if (!*tok) |
583 | 0 | ; |
584 | 0 | else if((val=string_to_cipher_algo (tok))) |
585 | 0 | { |
586 | 0 | if(set_one_pref(val,1,tok,sym,&nsym)) |
587 | 0 | err_cipher = 1; |
588 | 0 | else |
589 | 0 | any_cipher = 1; |
590 | 0 | } |
591 | 0 | else if((val=string_to_digest_algo (tok))) |
592 | 0 | { |
593 | 0 | if(set_one_pref(val,2,tok,hash,&nhash)) |
594 | 0 | err_digest = 1; |
595 | 0 | else |
596 | 0 | any_digest = 1; |
597 | 0 | } |
598 | 0 | else if((val=string_to_compress_algo(tok))>-1) |
599 | 0 | { |
600 | 0 | if(set_one_pref(val,3,tok,zip,&nzip)) |
601 | 0 | err_compress = 1; |
602 | 0 | else |
603 | 0 | any_compress = 1; |
604 | 0 | } |
605 | 0 | else if ((val=string_to_aead_algo (tok))) |
606 | 0 | { |
607 | 0 | if (set_one_pref (val, 4, tok, aead, &naead)) |
608 | 0 | err_aead = 1; |
609 | 0 | else |
610 | 0 | any_aead = 1; |
611 | 0 | } |
612 | 0 | else if (!ascii_strcasecmp(tok, "mdc") |
613 | 0 | || !ascii_strcasecmp(tok, "[mdc]")) |
614 | 0 | mdc=1; |
615 | 0 | else if (!ascii_strcasecmp(tok, "no-mdc") |
616 | 0 | || !ascii_strcasecmp(tok, "[no-mdc]")) |
617 | 0 | mdc=0; |
618 | 0 | else if (!ascii_strcasecmp(tok, "ks-modify") |
619 | 0 | || !ascii_strcasecmp(tok, "[ks-modify]")) |
620 | 0 | modify=1; |
621 | 0 | else if (!ascii_strcasecmp(tok,"no-ks-modify") |
622 | 0 | || !ascii_strcasecmp(tok,"[no-ks-modify]")) |
623 | 0 | modify=0; |
624 | 0 | else if (!ascii_strcasecmp(tok,"aead") |
625 | 0 | || !ascii_strcasecmp(tok,"[aead]")) |
626 | 0 | { |
627 | | /* Ignore because this is set from the preferences but |
628 | | * shown in the in the preferences/features list. */ |
629 | 0 | } |
630 | 0 | else |
631 | 0 | { |
632 | 0 | log_info (_("invalid item '%s' in preference string\n"),tok); |
633 | 0 | rc=-1; |
634 | 0 | } |
635 | 0 | } |
636 | | |
637 | | /* We return an error only if we have seen a parsing error for |
638 | | * one class but did not add any algorithm of that class. |
639 | | * Note that the set_one_pref functions already print log_info |
640 | | * diagnostics so that the user is made aware of the problems. |
641 | | * But tjhis way things work better even if an algorithm has |
642 | | * been disabled at the Libgcrypt level. */ |
643 | 0 | if (!rc && ((err_cipher && !any_cipher) |
644 | 0 | || (err_digest && !any_digest) |
645 | 0 | || (err_compress && !any_compress) |
646 | 0 | || (err_aead && !any_aead))) |
647 | 0 | rc = 1; |
648 | |
|
649 | 0 | xfree (prefstringbuf); |
650 | 0 | } |
651 | |
|
652 | 0 | if(!rc) |
653 | 0 | { |
654 | 0 | if(personal) |
655 | 0 | { |
656 | 0 | if(personal==PREFTYPE_SYM) |
657 | 0 | { |
658 | 0 | xfree(opt.personal_cipher_prefs); |
659 | |
|
660 | 0 | if(nsym==0) |
661 | 0 | opt.personal_cipher_prefs=NULL; |
662 | 0 | else |
663 | 0 | { |
664 | 0 | int i; |
665 | |
|
666 | 0 | opt.personal_cipher_prefs= |
667 | 0 | xmalloc(sizeof(prefitem_t *)*(nsym+1)); |
668 | |
|
669 | 0 | for (i=0; i<nsym; i++) |
670 | 0 | { |
671 | 0 | opt.personal_cipher_prefs[i].type = PREFTYPE_SYM; |
672 | 0 | opt.personal_cipher_prefs[i].value = sym[i]; |
673 | 0 | } |
674 | |
|
675 | 0 | opt.personal_cipher_prefs[i].type = PREFTYPE_NONE; |
676 | 0 | opt.personal_cipher_prefs[i].value = 0; |
677 | 0 | } |
678 | 0 | } |
679 | 0 | else if(personal==PREFTYPE_HASH) |
680 | 0 | { |
681 | 0 | xfree(opt.personal_digest_prefs); |
682 | |
|
683 | 0 | if(nhash==0) |
684 | 0 | opt.personal_digest_prefs=NULL; |
685 | 0 | else |
686 | 0 | { |
687 | 0 | int i; |
688 | |
|
689 | 0 | opt.personal_digest_prefs= |
690 | 0 | xmalloc(sizeof(prefitem_t *)*(nhash+1)); |
691 | |
|
692 | 0 | for (i=0; i<nhash; i++) |
693 | 0 | { |
694 | 0 | opt.personal_digest_prefs[i].type = PREFTYPE_HASH; |
695 | 0 | opt.personal_digest_prefs[i].value = hash[i]; |
696 | 0 | } |
697 | |
|
698 | 0 | opt.personal_digest_prefs[i].type = PREFTYPE_NONE; |
699 | 0 | opt.personal_digest_prefs[i].value = 0; |
700 | 0 | } |
701 | 0 | } |
702 | 0 | else if(personal==PREFTYPE_ZIP) |
703 | 0 | { |
704 | 0 | xfree(opt.personal_compress_prefs); |
705 | |
|
706 | 0 | if(nzip==0) |
707 | 0 | opt.personal_compress_prefs=NULL; |
708 | 0 | else |
709 | 0 | { |
710 | 0 | int i; |
711 | |
|
712 | 0 | opt.personal_compress_prefs= |
713 | 0 | xmalloc(sizeof(prefitem_t *)*(nzip+1)); |
714 | |
|
715 | 0 | for (i=0; i<nzip; i++) |
716 | 0 | { |
717 | 0 | opt.personal_compress_prefs[i].type = PREFTYPE_ZIP; |
718 | 0 | opt.personal_compress_prefs[i].value = zip[i]; |
719 | 0 | } |
720 | |
|
721 | 0 | opt.personal_compress_prefs[i].type = PREFTYPE_NONE; |
722 | 0 | opt.personal_compress_prefs[i].value = 0; |
723 | 0 | } |
724 | 0 | } |
725 | 0 | } |
726 | 0 | else |
727 | 0 | { |
728 | 0 | memcpy (sym_prefs, sym, (nsym_prefs=nsym)); |
729 | 0 | memcpy (hash_prefs, hash, (nhash_prefs=nhash)); |
730 | 0 | memcpy (zip_prefs, zip, (nzip_prefs=nzip)); |
731 | 0 | memcpy (aead_prefs, aead, (naead_prefs=naead)); |
732 | 0 | mdc_available = mdc; |
733 | 0 | aead_available = !!naead; |
734 | 0 | ks_modify = modify; |
735 | 0 | prefs_initialized = 1; |
736 | 0 | } |
737 | 0 | } |
738 | |
|
739 | 0 | return rc; |
740 | 0 | } |
741 | | |
742 | | |
743 | | /* Return a fake user ID containing the preferences. Caller must |
744 | | free. */ |
745 | | PKT_user_id * |
746 | | keygen_get_std_prefs(void) |
747 | 0 | { |
748 | 0 | int i,j=0; |
749 | 0 | PKT_user_id *uid=xmalloc_clear(sizeof(PKT_user_id)); |
750 | |
|
751 | 0 | if(!prefs_initialized) |
752 | 0 | keygen_set_std_prefs(NULL,0); |
753 | |
|
754 | 0 | uid->ref=1; |
755 | |
|
756 | 0 | uid->prefs = xmalloc ((sizeof(prefitem_t *)* |
757 | 0 | (nsym_prefs+naead_prefs+nhash_prefs+nzip_prefs+1))); |
758 | |
|
759 | 0 | for(i=0;i<nsym_prefs;i++,j++) |
760 | 0 | { |
761 | 0 | uid->prefs[j].type=PREFTYPE_SYM; |
762 | 0 | uid->prefs[j].value=sym_prefs[i]; |
763 | 0 | } |
764 | |
|
765 | 0 | for (i=0; i < naead_prefs; i++, j++) |
766 | 0 | { |
767 | 0 | uid->prefs[j].type = PREFTYPE_AEAD; |
768 | 0 | uid->prefs[j].value = aead_prefs[i]; |
769 | 0 | } |
770 | |
|
771 | 0 | for(i=0;i<nhash_prefs;i++,j++) |
772 | 0 | { |
773 | 0 | uid->prefs[j].type=PREFTYPE_HASH; |
774 | 0 | uid->prefs[j].value=hash_prefs[i]; |
775 | 0 | } |
776 | |
|
777 | 0 | for(i=0;i<nzip_prefs;i++,j++) |
778 | 0 | { |
779 | 0 | uid->prefs[j].type=PREFTYPE_ZIP; |
780 | 0 | uid->prefs[j].value=zip_prefs[i]; |
781 | 0 | } |
782 | |
|
783 | 0 | uid->prefs[j].type=PREFTYPE_NONE; |
784 | 0 | uid->prefs[j].value=0; |
785 | |
|
786 | 0 | uid->flags.mdc = mdc_available; |
787 | 0 | uid->flags.aead = aead_available; |
788 | 0 | uid->flags.ks_modify = ks_modify; |
789 | |
|
790 | 0 | return uid; |
791 | 0 | } |
792 | | |
793 | | static void |
794 | | add_feature_mdc (PKT_signature *sig,int enabled) |
795 | 0 | { |
796 | 0 | const byte *s; |
797 | 0 | size_t n; |
798 | 0 | int i; |
799 | 0 | char *buf; |
800 | |
|
801 | 0 | s = parse_sig_subpkt (sig, 1, SIGSUBPKT_FEATURES, &n ); |
802 | | /* Already set or cleared */ |
803 | 0 | if (s && n && |
804 | 0 | ((enabled && (s[0] & 0x01)) || (!enabled && !(s[0] & 0x01)))) |
805 | 0 | return; |
806 | | |
807 | 0 | if (!s || !n) { /* create a new one */ |
808 | 0 | n = 1; |
809 | 0 | buf = xmalloc_clear (n); |
810 | 0 | } |
811 | 0 | else { |
812 | 0 | buf = xmalloc (n); |
813 | 0 | memcpy (buf, s, n); |
814 | 0 | } |
815 | |
|
816 | 0 | if(enabled) |
817 | 0 | buf[0] |= 0x01; /* MDC feature */ |
818 | 0 | else |
819 | 0 | buf[0] &= ~0x01; |
820 | | |
821 | | /* Are there any bits set? */ |
822 | 0 | for(i=0;i<n;i++) |
823 | 0 | if(buf[i]!=0) |
824 | 0 | break; |
825 | |
|
826 | 0 | if(i==n) |
827 | 0 | delete_sig_subpkt (sig->hashed, SIGSUBPKT_FEATURES); |
828 | 0 | else |
829 | 0 | build_sig_subpkt (sig, SIGSUBPKT_FEATURES, buf, n); |
830 | |
|
831 | 0 | xfree (buf); |
832 | 0 | } |
833 | | |
834 | | |
835 | | static void |
836 | | add_feature_aead (PKT_signature *sig, int enabled) |
837 | 0 | { |
838 | 0 | const byte *s; |
839 | 0 | size_t n; |
840 | 0 | int i; |
841 | 0 | char *buf; |
842 | |
|
843 | 0 | s = parse_sig_subpkt (sig, 1, SIGSUBPKT_FEATURES, &n ); |
844 | 0 | if (s && n && ((enabled && (s[0] & 0x02)) || (!enabled && !(s[0] & 0x02)))) |
845 | 0 | return; /* Already set or cleared */ |
846 | | |
847 | 0 | if (!s || !n) |
848 | 0 | { /* Create a new one */ |
849 | 0 | n = 1; |
850 | 0 | buf = xmalloc_clear (n); |
851 | 0 | } |
852 | 0 | else |
853 | 0 | { |
854 | 0 | buf = xmalloc (n); |
855 | 0 | memcpy (buf, s, n); |
856 | 0 | } |
857 | |
|
858 | 0 | if (enabled) |
859 | 0 | buf[0] |= 0x02; /* AEAD supported */ |
860 | 0 | else |
861 | 0 | buf[0] &= ~0x02; |
862 | | |
863 | | /* Are there any bits set? */ |
864 | 0 | for (i=0; i < n; i++) |
865 | 0 | if (buf[i]) |
866 | 0 | break; |
867 | |
|
868 | 0 | if (i == n) |
869 | 0 | delete_sig_subpkt (sig->hashed, SIGSUBPKT_FEATURES); |
870 | 0 | else |
871 | 0 | build_sig_subpkt (sig, SIGSUBPKT_FEATURES, buf, n); |
872 | |
|
873 | 0 | xfree (buf); |
874 | 0 | } |
875 | | |
876 | | |
877 | | static void |
878 | | add_feature_v5 (PKT_signature *sig, int enabled) |
879 | 0 | { |
880 | 0 | const byte *s; |
881 | 0 | size_t n; |
882 | 0 | int i; |
883 | 0 | char *buf; |
884 | |
|
885 | 0 | s = parse_sig_subpkt (sig, 1, SIGSUBPKT_FEATURES, &n ); |
886 | 0 | if (s && n && ((enabled && (s[0] & 0x04)) || (!enabled && !(s[0] & 0x04)))) |
887 | 0 | return; /* Already set or cleared */ |
888 | | |
889 | 0 | if (!s || !n) |
890 | 0 | { /* Create a new one */ |
891 | 0 | n = 1; |
892 | 0 | buf = xmalloc_clear (n); |
893 | 0 | } |
894 | 0 | else |
895 | 0 | { |
896 | 0 | buf = xmalloc (n); |
897 | 0 | memcpy (buf, s, n); |
898 | 0 | } |
899 | |
|
900 | 0 | if (enabled) |
901 | 0 | buf[0] |= 0x04; /* v5 key supported */ |
902 | 0 | else |
903 | 0 | buf[0] &= ~0x04; |
904 | | |
905 | | /* Are there any bits set? */ |
906 | 0 | for (i=0; i < n; i++) |
907 | 0 | if (buf[i]) |
908 | 0 | break; |
909 | |
|
910 | 0 | if (i == n) |
911 | 0 | delete_sig_subpkt (sig->hashed, SIGSUBPKT_FEATURES); |
912 | 0 | else |
913 | 0 | build_sig_subpkt (sig, SIGSUBPKT_FEATURES, buf, n); |
914 | |
|
915 | 0 | xfree (buf); |
916 | 0 | } |
917 | | |
918 | | |
919 | | static void |
920 | | add_keyserver_modify (PKT_signature *sig,int enabled) |
921 | 0 | { |
922 | 0 | const byte *s; |
923 | 0 | size_t n; |
924 | 0 | int i; |
925 | 0 | char *buf; |
926 | | |
927 | | /* The keyserver modify flag is a negative flag (i.e. no-modify) */ |
928 | 0 | enabled=!enabled; |
929 | |
|
930 | 0 | s = parse_sig_subpkt (sig, 1, SIGSUBPKT_KS_FLAGS, &n ); |
931 | | /* Already set or cleared */ |
932 | 0 | if (s && n && |
933 | 0 | ((enabled && (s[0] & 0x80)) || (!enabled && !(s[0] & 0x80)))) |
934 | 0 | return; |
935 | | |
936 | 0 | if (!s || !n) { /* create a new one */ |
937 | 0 | n = 1; |
938 | 0 | buf = xmalloc_clear (n); |
939 | 0 | } |
940 | 0 | else { |
941 | 0 | buf = xmalloc (n); |
942 | 0 | memcpy (buf, s, n); |
943 | 0 | } |
944 | |
|
945 | 0 | if(enabled) |
946 | 0 | buf[0] |= 0x80; /* no-modify flag */ |
947 | 0 | else |
948 | 0 | buf[0] &= ~0x80; |
949 | | |
950 | | /* Are there any bits set? */ |
951 | 0 | for(i=0;i<n;i++) |
952 | 0 | if(buf[i]!=0) |
953 | 0 | break; |
954 | |
|
955 | 0 | if(i==n) |
956 | 0 | delete_sig_subpkt (sig->hashed, SIGSUBPKT_KS_FLAGS); |
957 | 0 | else |
958 | 0 | build_sig_subpkt (sig, SIGSUBPKT_KS_FLAGS, buf, n); |
959 | |
|
960 | 0 | xfree (buf); |
961 | 0 | } |
962 | | |
963 | | |
964 | | int |
965 | | keygen_upd_std_prefs (PKT_signature *sig, void *opaque) |
966 | 0 | { |
967 | 0 | (void)opaque; |
968 | |
|
969 | 0 | if (!prefs_initialized) |
970 | 0 | keygen_set_std_prefs (NULL, 0); |
971 | |
|
972 | 0 | if (nsym_prefs) |
973 | 0 | build_sig_subpkt (sig, SIGSUBPKT_PREF_SYM, sym_prefs, nsym_prefs); |
974 | 0 | else |
975 | 0 | { |
976 | 0 | delete_sig_subpkt (sig->hashed, SIGSUBPKT_PREF_SYM); |
977 | 0 | delete_sig_subpkt (sig->unhashed, SIGSUBPKT_PREF_SYM); |
978 | 0 | } |
979 | |
|
980 | 0 | if (naead_prefs) |
981 | 0 | build_sig_subpkt (sig, SIGSUBPKT_PREF_AEAD, aead_prefs, naead_prefs); |
982 | 0 | else |
983 | 0 | { |
984 | 0 | delete_sig_subpkt (sig->hashed, SIGSUBPKT_PREF_AEAD); |
985 | 0 | delete_sig_subpkt (sig->unhashed, SIGSUBPKT_PREF_AEAD); |
986 | 0 | } |
987 | |
|
988 | 0 | if (nhash_prefs) |
989 | 0 | build_sig_subpkt (sig, SIGSUBPKT_PREF_HASH, hash_prefs, nhash_prefs); |
990 | 0 | else |
991 | 0 | { |
992 | 0 | delete_sig_subpkt (sig->hashed, SIGSUBPKT_PREF_HASH); |
993 | 0 | delete_sig_subpkt (sig->unhashed, SIGSUBPKT_PREF_HASH); |
994 | 0 | } |
995 | |
|
996 | 0 | if (nzip_prefs) |
997 | 0 | build_sig_subpkt (sig, SIGSUBPKT_PREF_COMPR, zip_prefs, nzip_prefs); |
998 | 0 | else |
999 | 0 | { |
1000 | 0 | delete_sig_subpkt (sig->hashed, SIGSUBPKT_PREF_COMPR); |
1001 | 0 | delete_sig_subpkt (sig->unhashed, SIGSUBPKT_PREF_COMPR); |
1002 | 0 | } |
1003 | | |
1004 | | /* Make sure that the MDC feature flag is set if needed. */ |
1005 | 0 | add_feature_mdc (sig,mdc_available); |
1006 | 0 | add_feature_aead (sig, aead_available); |
1007 | 0 | add_feature_v5 (sig, 1); |
1008 | 0 | add_keyserver_modify (sig,ks_modify); |
1009 | 0 | keygen_add_keyserver_url(sig,NULL); |
1010 | |
|
1011 | 0 | return 0; |
1012 | 0 | } |
1013 | | |
1014 | | |
1015 | | /**************** |
1016 | | * Add preference to the self signature packet. |
1017 | | * This is only called for packets with version > 3. |
1018 | | */ |
1019 | | int |
1020 | | keygen_add_std_prefs (PKT_signature *sig, void *opaque) |
1021 | 0 | { |
1022 | 0 | PKT_public_key *pk = opaque; |
1023 | |
|
1024 | 0 | do_add_key_flags (sig, pk->pubkey_usage); |
1025 | 0 | keygen_add_key_expire (sig, opaque ); |
1026 | 0 | keygen_upd_std_prefs (sig, opaque); |
1027 | 0 | keygen_add_keyserver_url (sig,NULL); |
1028 | |
|
1029 | 0 | return 0; |
1030 | 0 | } |
1031 | | |
1032 | | int |
1033 | | keygen_add_keyserver_url(PKT_signature *sig, void *opaque) |
1034 | 0 | { |
1035 | 0 | const char *url=opaque; |
1036 | |
|
1037 | 0 | if(!url) |
1038 | 0 | url=opt.def_keyserver_url; |
1039 | |
|
1040 | 0 | if(url) |
1041 | 0 | build_sig_subpkt(sig,SIGSUBPKT_PREF_KS,url,strlen(url)); |
1042 | 0 | else |
1043 | 0 | delete_sig_subpkt (sig->hashed,SIGSUBPKT_PREF_KS); |
1044 | |
|
1045 | 0 | return 0; |
1046 | 0 | } |
1047 | | |
1048 | | |
1049 | | /* This function is used to add a notations to a signature. In |
1050 | | * general the caller should have cleared exiting notations before |
1051 | | * adding new ones. For example by calling: |
1052 | | * |
1053 | | * delete_sig_subpkt(sig->hashed,SIGSUBPKT_NOTATION); |
1054 | | * delete_sig_subpkt(sig->unhashed,SIGSUBPKT_NOTATION); |
1055 | | * |
1056 | | * Only human readable notaions may be added. NAME and value are |
1057 | | * expected to be UTF-* strings. |
1058 | | */ |
1059 | | static void |
1060 | | do_add_notation (PKT_signature *sig, const char *name, const char *value, |
1061 | | int critical) |
1062 | 0 | { |
1063 | 0 | unsigned char *buf; |
1064 | 0 | unsigned int n1,n2; |
1065 | |
|
1066 | 0 | n1 = strlen (name); |
1067 | 0 | n2 = strlen (value); |
1068 | |
|
1069 | 0 | buf = xmalloc (8 + n1 + n2); |
1070 | |
|
1071 | 0 | buf[0] = 0x80; /* human readable. */ |
1072 | 0 | buf[1] = buf[2] = buf[3] = 0; |
1073 | 0 | buf[4] = n1 >> 8; |
1074 | 0 | buf[5] = n1; |
1075 | 0 | buf[6] = n2 >> 8; |
1076 | 0 | buf[7] = n2; |
1077 | 0 | memcpy (buf+8, name, n1); |
1078 | 0 | memcpy (buf+8+n1, value, n2); |
1079 | 0 | build_sig_subpkt (sig, |
1080 | 0 | (SIGSUBPKT_NOTATION|(critical?SIGSUBPKT_FLAG_CRITICAL:0)), |
1081 | 0 | buf, 8+n1+n2 ); |
1082 | 0 | xfree (buf); |
1083 | 0 | } |
1084 | | |
1085 | | |
1086 | | int |
1087 | | keygen_add_notations(PKT_signature *sig,void *opaque) |
1088 | 0 | { |
1089 | 0 | struct notation *notation; |
1090 | | |
1091 | | /* We always start clean */ |
1092 | 0 | delete_sig_subpkt(sig->hashed,SIGSUBPKT_NOTATION); |
1093 | 0 | delete_sig_subpkt(sig->unhashed,SIGSUBPKT_NOTATION); |
1094 | 0 | sig->flags.notation=0; |
1095 | |
|
1096 | 0 | for(notation=opaque;notation;notation=notation->next) |
1097 | 0 | if(!notation->flags.ignore) |
1098 | 0 | { |
1099 | 0 | unsigned char *buf; |
1100 | 0 | unsigned int n1,n2; |
1101 | |
|
1102 | 0 | n1=strlen(notation->name); |
1103 | 0 | if(notation->altvalue) |
1104 | 0 | n2=strlen(notation->altvalue); |
1105 | 0 | else if(notation->bdat) |
1106 | 0 | n2=notation->blen; |
1107 | 0 | else |
1108 | 0 | n2=strlen(notation->value); |
1109 | |
|
1110 | 0 | buf = xmalloc( 8 + n1 + n2 ); |
1111 | | |
1112 | | /* human readable or not */ |
1113 | 0 | buf[0] = notation->bdat?0:0x80; |
1114 | 0 | buf[1] = buf[2] = buf[3] = 0; |
1115 | 0 | buf[4] = n1 >> 8; |
1116 | 0 | buf[5] = n1; |
1117 | 0 | buf[6] = n2 >> 8; |
1118 | 0 | buf[7] = n2; |
1119 | 0 | memcpy(buf+8, notation->name, n1 ); |
1120 | 0 | if(notation->altvalue) |
1121 | 0 | memcpy(buf+8+n1, notation->altvalue, n2 ); |
1122 | 0 | else if(notation->bdat) |
1123 | 0 | memcpy(buf+8+n1, notation->bdat, n2 ); |
1124 | 0 | else |
1125 | 0 | memcpy(buf+8+n1, notation->value, n2 ); |
1126 | 0 | build_sig_subpkt( sig, SIGSUBPKT_NOTATION | |
1127 | 0 | (notation->flags.critical?SIGSUBPKT_FLAG_CRITICAL:0), |
1128 | 0 | buf, 8+n1+n2 ); |
1129 | 0 | xfree(buf); |
1130 | 0 | } |
1131 | |
|
1132 | 0 | return 0; |
1133 | 0 | } |
1134 | | |
1135 | | |
1136 | | int |
1137 | | keygen_add_revkey (PKT_signature *sig, void *opaque) |
1138 | 0 | { |
1139 | 0 | struct revocation_key *revkey = opaque; |
1140 | 0 | byte buf[2+MAX_FINGERPRINT_LEN]; |
1141 | |
|
1142 | 0 | log_assert (revkey->fprlen <= MAX_FINGERPRINT_LEN); |
1143 | 0 | buf[0] = revkey->class; |
1144 | 0 | buf[1] = revkey->algid; |
1145 | 0 | memcpy (buf + 2, revkey->fpr, revkey->fprlen); |
1146 | 0 | memset (buf + 2 + revkey->fprlen, 0, sizeof (revkey->fpr) - revkey->fprlen); |
1147 | |
|
1148 | 0 | build_sig_subpkt (sig, SIGSUBPKT_REV_KEY, buf, 2+revkey->fprlen); |
1149 | | |
1150 | | /* All sigs with revocation keys set are nonrevocable. */ |
1151 | 0 | sig->flags.revocable = 0; |
1152 | 0 | buf[0] = 0; |
1153 | 0 | build_sig_subpkt (sig, SIGSUBPKT_REVOCABLE, buf, 1); |
1154 | |
|
1155 | 0 | parse_revkeys (sig); |
1156 | |
|
1157 | 0 | return 0; |
1158 | 0 | } |
1159 | | |
1160 | | |
1161 | | |
1162 | | /* Create a back-signature. If TIMESTAMP is not NULL, use it for the |
1163 | | signature creation time. */ |
1164 | | gpg_error_t |
1165 | | make_backsig (ctrl_t ctrl, PKT_signature *sig, PKT_public_key *pk, |
1166 | | PKT_public_key *sub_pk, PKT_public_key *sub_psk, |
1167 | | u32 timestamp, const char *cache_nonce) |
1168 | 0 | { |
1169 | 0 | gpg_error_t err; |
1170 | 0 | PKT_signature *backsig; |
1171 | |
|
1172 | 0 | cache_public_key (sub_pk); |
1173 | |
|
1174 | 0 | err = make_keysig_packet (ctrl, &backsig, pk, NULL, sub_pk, sub_psk, 0x19, |
1175 | 0 | timestamp, 0, NULL, NULL, cache_nonce); |
1176 | 0 | if (err) |
1177 | 0 | log_error ("make_keysig_packet failed for backsig: %s\n", |
1178 | 0 | gpg_strerror (err)); |
1179 | 0 | else |
1180 | 0 | { |
1181 | | /* Get it into a binary packed form. */ |
1182 | 0 | IOBUF backsig_out = iobuf_temp(); |
1183 | 0 | PACKET backsig_pkt; |
1184 | |
|
1185 | 0 | init_packet (&backsig_pkt); |
1186 | 0 | backsig_pkt.pkttype = PKT_SIGNATURE; |
1187 | 0 | backsig_pkt.pkt.signature = backsig; |
1188 | 0 | err = build_packet (backsig_out, &backsig_pkt); |
1189 | 0 | free_packet (&backsig_pkt, NULL); |
1190 | 0 | if (err) |
1191 | 0 | log_error ("build_packet failed for backsig: %s\n", gpg_strerror (err)); |
1192 | 0 | else |
1193 | 0 | { |
1194 | 0 | size_t pktlen = 0; |
1195 | 0 | byte *buf = iobuf_get_temp_buffer (backsig_out); |
1196 | | |
1197 | | /* Remove the packet header. */ |
1198 | 0 | if(buf[0]&0x40) |
1199 | 0 | { |
1200 | 0 | if (buf[1] < 192) |
1201 | 0 | { |
1202 | 0 | pktlen = buf[1]; |
1203 | 0 | buf += 2; |
1204 | 0 | } |
1205 | 0 | else if(buf[1] < 224) |
1206 | 0 | { |
1207 | 0 | pktlen = (buf[1]-192)*256; |
1208 | 0 | pktlen += buf[2]+192; |
1209 | 0 | buf += 3; |
1210 | 0 | } |
1211 | 0 | else if (buf[1] == 255) |
1212 | 0 | { |
1213 | 0 | pktlen = buf32_to_size_t (buf+2); |
1214 | 0 | buf += 6; |
1215 | 0 | } |
1216 | 0 | else |
1217 | 0 | BUG (); |
1218 | 0 | } |
1219 | 0 | else |
1220 | 0 | { |
1221 | 0 | int mark = 1; |
1222 | |
|
1223 | 0 | switch (buf[0]&3) |
1224 | 0 | { |
1225 | 0 | case 3: |
1226 | 0 | BUG (); |
1227 | 0 | break; |
1228 | | |
1229 | 0 | case 2: |
1230 | 0 | pktlen = (size_t)buf[mark++] << 24; |
1231 | 0 | pktlen |= buf[mark++] << 16; |
1232 | | /* fall through */ |
1233 | 0 | case 1: |
1234 | 0 | pktlen |= buf[mark++] << 8; |
1235 | | /* fall through */ |
1236 | 0 | case 0: |
1237 | 0 | pktlen |= buf[mark++]; |
1238 | 0 | } |
1239 | | |
1240 | 0 | buf += mark; |
1241 | 0 | } |
1242 | | |
1243 | | /* Now make the binary blob into a subpacket. */ |
1244 | 0 | build_sig_subpkt (sig, SIGSUBPKT_SIGNATURE, buf, pktlen); |
1245 | |
|
1246 | 0 | iobuf_close (backsig_out); |
1247 | 0 | } |
1248 | 0 | } |
1249 | | |
1250 | 0 | return err; |
1251 | 0 | } |
1252 | | |
1253 | | |
1254 | | /* This function should be called to make sure that |
1255 | | * opt.def_new_key_adsks has no duplicates and that tehre is no '!' |
1256 | | * suffix. We don't do this during normal option processing because |
1257 | | * this list is only needed for a very few operations. Callingit |
1258 | | * twice does not harm. Users of the option list should skip empty |
1259 | | * items. */ |
1260 | | void |
1261 | | keygen_prepare_new_key_adsks (void) |
1262 | 0 | { |
1263 | 0 | strlist_t sl, slr; |
1264 | 0 | char *p; |
1265 | |
|
1266 | 0 | for (sl = opt.def_new_key_adsks; sl; sl = sl->next) |
1267 | 0 | { |
1268 | 0 | if (!*sl->d) |
1269 | 0 | continue; |
1270 | 0 | p = strchr (sl->d, '!'); |
1271 | 0 | if (p) |
1272 | 0 | *p = 0; |
1273 | 0 | for (slr = opt.def_new_key_adsks; slr != sl; slr = slr->next) |
1274 | 0 | if (!ascii_strcasecmp (sl->d, slr->d)) |
1275 | 0 | { |
1276 | 0 | *sl->d = 0; /* clear fpr to mark this as a duplicate. */ |
1277 | 0 | break; |
1278 | 0 | } |
1279 | 0 | } |
1280 | 0 | } |
1281 | | |
1282 | | |
1283 | | /* Append all default ADSKs to the KEYBLOCK but ignore those which are |
1284 | | * already on that keyblock. Returns 0 if any key has been added; |
1285 | | * GPG_ERR_FALSE if no key was added or any other error code. */ |
1286 | | gpg_error_t |
1287 | | append_all_default_adsks (ctrl_t ctrl, kbnode_t keyblock) |
1288 | 0 | { |
1289 | 0 | gpg_error_t err = 0; |
1290 | 0 | int any_done = 0; |
1291 | 0 | strlist_t sl; |
1292 | 0 | struct para_data_s *para; |
1293 | 0 | byte adskfpr[MAX_FINGERPRINT_LEN]; |
1294 | 0 | size_t adskfprlen; |
1295 | 0 | u32 sigtimestamp = make_timestamp (); |
1296 | |
|
1297 | 0 | keygen_prepare_new_key_adsks (); |
1298 | 0 | for (sl = opt.def_new_key_adsks; sl && !err; sl = sl->next) |
1299 | 0 | { |
1300 | 0 | if (!*sl->d) |
1301 | 0 | continue; |
1302 | 0 | para = prepare_adsk (ctrl, sl->d); |
1303 | 0 | if (para) |
1304 | 0 | { |
1305 | 0 | fingerprint_from_pk (para->u.adsk, adskfpr, &adskfprlen); |
1306 | 0 | if (!has_key_with_fingerprint (keyblock, adskfpr, adskfprlen)) |
1307 | 0 | { |
1308 | | /* Fixme: We should use a cache nonce so that only one |
1309 | | * pinentry pops up. */ |
1310 | 0 | err = append_adsk_to_key (ctrl, keyblock, para->u.adsk, |
1311 | 0 | sigtimestamp, NULL); |
1312 | 0 | if (!err) |
1313 | 0 | any_done = 1; |
1314 | 0 | } |
1315 | 0 | release_parameter_list (para); |
1316 | 0 | } |
1317 | 0 | } |
1318 | |
|
1319 | 0 | if (!err && !any_done) |
1320 | 0 | err = gpg_error (GPG_ERR_FALSE); |
1321 | |
|
1322 | 0 | return err; |
1323 | 0 | } |
1324 | | |
1325 | | |
1326 | | /* Write a direct key signature to the first key in ROOT using the key |
1327 | | PSK. REVKEY is describes the direct key signature and TIMESTAMP is |
1328 | | the timestamp to set on the signature. */ |
1329 | | static gpg_error_t |
1330 | | write_direct_sig (ctrl_t ctrl, kbnode_t root, PKT_public_key *psk, |
1331 | | struct revocation_key *revkey, u32 timestamp, |
1332 | | const char *cache_nonce) |
1333 | 0 | { |
1334 | 0 | gpg_error_t err; |
1335 | 0 | PACKET *pkt; |
1336 | 0 | PKT_signature *sig; |
1337 | 0 | KBNODE node; |
1338 | 0 | PKT_public_key *pk; |
1339 | |
|
1340 | 0 | if (opt.verbose) |
1341 | 0 | log_info (_("writing direct signature\n")); |
1342 | | |
1343 | | /* Get the pk packet from the pub_tree. */ |
1344 | 0 | node = find_kbnode (root, PKT_PUBLIC_KEY); |
1345 | 0 | if (!node) |
1346 | 0 | BUG (); |
1347 | 0 | pk = node->pkt->pkt.public_key; |
1348 | | |
1349 | | /* We have to cache the key, so that the verification of the |
1350 | | signature creation is able to retrieve the public key. */ |
1351 | 0 | cache_public_key (pk); |
1352 | | |
1353 | | /* Make the signature. */ |
1354 | 0 | err = make_keysig_packet (ctrl, &sig, pk, NULL,NULL, psk, 0x1F, |
1355 | 0 | timestamp, 0, |
1356 | 0 | keygen_add_revkey, revkey, cache_nonce); |
1357 | 0 | if (err) |
1358 | 0 | { |
1359 | 0 | log_error ("make_keysig_packet failed: %s\n", gpg_strerror (err) ); |
1360 | 0 | return err; |
1361 | 0 | } |
1362 | | |
1363 | 0 | pkt = xmalloc_clear (sizeof *pkt); |
1364 | 0 | pkt->pkttype = PKT_SIGNATURE; |
1365 | 0 | pkt->pkt.signature = sig; |
1366 | 0 | add_kbnode (root, new_kbnode (pkt)); |
1367 | 0 | return err; |
1368 | 0 | } |
1369 | | |
1370 | | |
1371 | | |
1372 | | /* Write a self-signature to the first user id in ROOT using the key |
1373 | | PSK. USE and TIMESTAMP give the extra data we need for the |
1374 | | signature. */ |
1375 | | static gpg_error_t |
1376 | | write_selfsigs (ctrl_t ctrl, kbnode_t root, PKT_public_key *psk, |
1377 | | unsigned int use, u32 timestamp, const char *cache_nonce) |
1378 | 0 | { |
1379 | 0 | gpg_error_t err; |
1380 | 0 | PACKET *pkt; |
1381 | 0 | PKT_signature *sig; |
1382 | 0 | PKT_user_id *uid; |
1383 | 0 | KBNODE node; |
1384 | 0 | PKT_public_key *pk; |
1385 | |
|
1386 | 0 | if (opt.verbose) |
1387 | 0 | log_info (_("writing self signature\n")); |
1388 | | |
1389 | | /* Get the uid packet from the list. */ |
1390 | 0 | node = find_kbnode (root, PKT_USER_ID); |
1391 | 0 | if (!node) |
1392 | 0 | BUG(); /* No user id packet in tree. */ |
1393 | 0 | uid = node->pkt->pkt.user_id; |
1394 | | |
1395 | | /* Get the pk packet from the pub_tree. */ |
1396 | 0 | node = find_kbnode (root, PKT_PUBLIC_KEY); |
1397 | 0 | if (!node) |
1398 | 0 | BUG(); |
1399 | 0 | pk = node->pkt->pkt.public_key; |
1400 | | |
1401 | | /* The usage has not yet been set - do it now. */ |
1402 | 0 | pk->pubkey_usage = use; |
1403 | | |
1404 | | /* We have to cache the key, so that the verification of the |
1405 | | signature creation is able to retrieve the public key. */ |
1406 | 0 | cache_public_key (pk); |
1407 | | |
1408 | | /* Make the signature. */ |
1409 | 0 | err = make_keysig_packet (ctrl, &sig, pk, uid, NULL, psk, 0x13, |
1410 | 0 | timestamp, 0, |
1411 | 0 | keygen_add_std_prefs, pk, cache_nonce); |
1412 | 0 | if (err) |
1413 | 0 | { |
1414 | 0 | log_error ("make_keysig_packet failed: %s\n", gpg_strerror (err)); |
1415 | 0 | return err; |
1416 | 0 | } |
1417 | | |
1418 | 0 | pkt = xmalloc_clear (sizeof *pkt); |
1419 | 0 | pkt->pkttype = PKT_SIGNATURE; |
1420 | 0 | pkt->pkt.signature = sig; |
1421 | 0 | add_kbnode (root, new_kbnode (pkt)); |
1422 | |
|
1423 | 0 | return err; |
1424 | 0 | } |
1425 | | |
1426 | | |
1427 | | /* Append a key binding signature to the last subkey in KEYBLOCK. |
1428 | | * PRI_PSK is the key used to create (sign) the key binding signature |
1429 | | * this is usuallay the primary key. SUB_PSK is a key used to create |
1430 | | * a back-signature; this is required if the PUBKEY_USAGE_SIG |
1431 | | * capability is passed in USE (which gives the key usage). TIMESTAMP |
1432 | | * is the signature creation time with 0 meaning "now". CACHE_NONCE |
1433 | | * is passed to the agent to indentify the passphrase cache slot. */ |
1434 | | static int |
1435 | | write_keybinding (ctrl_t ctrl, kbnode_t keyblock, |
1436 | | PKT_public_key *pri_psk, PKT_public_key *sub_psk, |
1437 | | unsigned int use, u32 timestamp, const char *cache_nonce) |
1438 | 0 | { |
1439 | 0 | gpg_error_t err; |
1440 | 0 | PACKET *pkt; |
1441 | 0 | PKT_signature *sig; |
1442 | 0 | KBNODE node; |
1443 | 0 | PKT_public_key *pri_pk, *sub_pk; |
1444 | 0 | struct opaque_data_usage_and_pk oduap; |
1445 | |
|
1446 | 0 | if (opt.verbose) |
1447 | 0 | log_info(_("writing key binding signature\n")); |
1448 | | |
1449 | | /* Get the primary pk packet from the tree. */ |
1450 | 0 | node = find_kbnode (keyblock, PKT_PUBLIC_KEY); |
1451 | 0 | if (!node) |
1452 | 0 | BUG(); |
1453 | 0 | pri_pk = node->pkt->pkt.public_key; |
1454 | | |
1455 | | /* We have to cache the key, so that the verification of the |
1456 | | * signature creation is able to retrieve the public key. */ |
1457 | 0 | cache_public_key (pri_pk); |
1458 | | |
1459 | | /* Find the last subkey. */ |
1460 | 0 | sub_pk = NULL; |
1461 | 0 | for (node = keyblock; node; node = node->next ) |
1462 | 0 | { |
1463 | 0 | if (node->pkt->pkttype == PKT_PUBLIC_SUBKEY) |
1464 | 0 | sub_pk = node->pkt->pkt.public_key; |
1465 | 0 | } |
1466 | 0 | if (!sub_pk) |
1467 | 0 | BUG(); |
1468 | | |
1469 | | /* Make the signature. */ |
1470 | 0 | oduap.usage = use; |
1471 | 0 | if ((use & PUBKEY_USAGE_ENC) |
1472 | 0 | && opt.compliance == CO_DE_VS |
1473 | | /* The required libgcrypt 1.11 won't yet claim a compliant RNG. */ |
1474 | 0 | && gnupg_rng_is_compliant (CO_DE_VS)) |
1475 | 0 | oduap.cpl_notation = "de-vs"; |
1476 | 0 | else if ((use & PUBKEY_USAGE_ENC) |
1477 | 0 | && opt.compliance == CO_FIPS) |
1478 | 0 | oduap.cpl_notation = "fips"; |
1479 | 0 | else |
1480 | 0 | oduap.cpl_notation = NULL; |
1481 | 0 | oduap.pk = sub_pk; |
1482 | 0 | err = make_keysig_packet (ctrl, &sig, pri_pk, NULL, sub_pk, pri_psk, 0x18, |
1483 | 0 | timestamp, 0, |
1484 | 0 | keygen_add_key_flags_from_oduap, &oduap, |
1485 | 0 | cache_nonce); |
1486 | 0 | if (err) |
1487 | 0 | { |
1488 | 0 | log_error ("make_keysig_packet failed: %s\n", gpg_strerror (err)); |
1489 | 0 | return err; |
1490 | 0 | } |
1491 | | |
1492 | | /* Make a backsig. */ |
1493 | 0 | if (use & PUBKEY_USAGE_SIG) |
1494 | 0 | { |
1495 | 0 | err = make_backsig (ctrl, |
1496 | 0 | sig, pri_pk, sub_pk, sub_psk, timestamp, cache_nonce); |
1497 | 0 | if (err) |
1498 | 0 | return err; |
1499 | 0 | } |
1500 | | |
1501 | 0 | pkt = xmalloc_clear ( sizeof *pkt ); |
1502 | 0 | pkt->pkttype = PKT_SIGNATURE; |
1503 | 0 | pkt->pkt.signature = sig; |
1504 | 0 | add_kbnode (keyblock, new_kbnode (pkt) ); |
1505 | 0 | return err; |
1506 | 0 | } |
1507 | | |
1508 | | |
1509 | | /* Returns true if SEXP specified the curve ED448 or X448. */ |
1510 | | static int |
1511 | | curve_is_448 (gcry_sexp_t sexp) |
1512 | 0 | { |
1513 | 0 | gcry_sexp_t list, l2; |
1514 | 0 | char *curve; |
1515 | 0 | int result; |
1516 | |
|
1517 | 0 | list = gcry_sexp_find_token (sexp, "public-key", 0); |
1518 | 0 | if (!list) |
1519 | 0 | return 0; /* Not a public key. */ |
1520 | 0 | l2 = gcry_sexp_cadr (list); |
1521 | 0 | gcry_sexp_release (list); |
1522 | 0 | list = l2; |
1523 | 0 | if (!list) |
1524 | 0 | return 0; /* Bad public key. */ |
1525 | | |
1526 | 0 | l2 = gcry_sexp_find_token (list, "curve", 0); |
1527 | 0 | gcry_sexp_release (list); |
1528 | 0 | if (!l2) |
1529 | 0 | return 0; /* No curve parameter. */ |
1530 | 0 | curve = gcry_sexp_nth_string (l2, 1); |
1531 | 0 | gcry_sexp_release (l2); |
1532 | 0 | if (!curve) |
1533 | 0 | return 0; /* Bad curve parameter. */ |
1534 | 0 | result = (!ascii_strcasecmp (curve, "X448") |
1535 | 0 | || !ascii_strcasecmp (curve, "Ed448") |
1536 | 0 | || !ascii_strcasecmp (curve, "cv448")); |
1537 | 0 | xfree (curve); |
1538 | 0 | return result; |
1539 | 0 | } |
1540 | | |
1541 | | |
1542 | | /* Extract the parameters in OpenPGP format from SEXP and put them |
1543 | | * into the caller provided ARRAY. SEXP2 is used to provide the |
1544 | | * parameters for dual algorithm (e.g. Kyber). */ |
1545 | | static gpg_error_t |
1546 | | ecckey_from_sexp (gcry_mpi_t *array, gcry_sexp_t sexp, |
1547 | | gcry_sexp_t sexp2, int algo, int pkversion) |
1548 | 0 | { |
1549 | 0 | gpg_error_t err; |
1550 | 0 | gcry_sexp_t list, l2; |
1551 | 0 | char *curve = NULL; |
1552 | 0 | int i; |
1553 | 0 | const char *oidstr; |
1554 | 0 | unsigned int nbits; |
1555 | |
|
1556 | 0 | array[0] = NULL; |
1557 | 0 | array[1] = NULL; |
1558 | 0 | array[2] = NULL; |
1559 | |
|
1560 | 0 | list = gcry_sexp_find_token (sexp, "public-key", 0); |
1561 | 0 | if (!list) |
1562 | 0 | return gpg_error (GPG_ERR_INV_OBJ); |
1563 | 0 | l2 = gcry_sexp_cadr (list); |
1564 | 0 | gcry_sexp_release (list); |
1565 | 0 | list = l2; |
1566 | 0 | if (!list) |
1567 | 0 | return gpg_error (GPG_ERR_NO_OBJ); |
1568 | | |
1569 | 0 | l2 = gcry_sexp_find_token (list, "curve", 0); |
1570 | 0 | if (!l2) |
1571 | 0 | { |
1572 | 0 | err = gpg_error (GPG_ERR_NO_OBJ); |
1573 | 0 | goto leave; |
1574 | 0 | } |
1575 | 0 | curve = gcry_sexp_nth_string (l2, 1); |
1576 | 0 | if (!curve) |
1577 | 0 | { |
1578 | 0 | err = gpg_error (GPG_ERR_NO_OBJ); |
1579 | 0 | goto leave; |
1580 | 0 | } |
1581 | 0 | gcry_sexp_release (l2); |
1582 | 0 | oidstr = openpgp_curve_to_oid (curve, &nbits, NULL, pkversion > 4); |
1583 | 0 | if (!oidstr) |
1584 | 0 | { |
1585 | | /* That can't happen because we used one of the curves |
1586 | | gpg_curve_to_oid knows about. */ |
1587 | 0 | err = gpg_error (GPG_ERR_INV_OBJ); |
1588 | 0 | goto leave; |
1589 | 0 | } |
1590 | | |
1591 | 0 | if (algo == PUBKEY_ALGO_ED25519 |
1592 | 0 | || algo == PUBKEY_ALGO_X25519) |
1593 | 0 | { |
1594 | 0 | err = 0; |
1595 | 0 | array[0] = get_mpi_from_sexp (list, "q", GCRYMPI_FMT_OPAQUE); |
1596 | 0 | if (!array[0]) |
1597 | 0 | { |
1598 | 0 | err = gpg_error (GPG_ERR_NO_OBJ); |
1599 | 0 | goto leave; |
1600 | 0 | } |
1601 | 0 | } |
1602 | 0 | else if (IS_PUBKEY_ALGO_MLK (algo)) |
1603 | 0 | { |
1604 | 0 | err = 0; |
1605 | 0 | array[0] = get_mpi_from_sexp_strip_0x40 (list, "q"); |
1606 | 0 | if (!array[0]) |
1607 | 0 | { |
1608 | 0 | err = gpg_error (GPG_ERR_NO_OBJ); |
1609 | 0 | goto leave; |
1610 | 0 | } |
1611 | 0 | } |
1612 | 0 | else |
1613 | 0 | { |
1614 | 0 | err = openpgp_oid_from_str (oidstr, &array[0]); |
1615 | 0 | if (err) |
1616 | 0 | goto leave; |
1617 | | |
1618 | 0 | err = sexp_extract_param_sos (list, "q", &array[1]); |
1619 | 0 | if (err) |
1620 | 0 | goto leave; |
1621 | 0 | } |
1622 | | |
1623 | 0 | gcry_sexp_release (list); |
1624 | 0 | list = NULL; |
1625 | |
|
1626 | 0 | if (algo == PUBKEY_ALGO_KYBER || IS_PUBKEY_ALGO_MLK (algo)) |
1627 | 0 | { |
1628 | 0 | int arrayidx = algo == PUBKEY_ALGO_KYBER? 2 : 1; |
1629 | |
|
1630 | 0 | if (!sexp2) |
1631 | 0 | { |
1632 | 0 | err = gpg_error (GPG_ERR_MISSING_VALUE); |
1633 | 0 | goto leave; |
1634 | 0 | } |
1635 | | |
1636 | 0 | list = gcry_sexp_find_token (sexp2, "public-key", 0); |
1637 | 0 | if (!list) |
1638 | 0 | { |
1639 | 0 | err = gpg_error (GPG_ERR_INV_OBJ); |
1640 | 0 | goto leave; |
1641 | 0 | } |
1642 | 0 | l2 = gcry_sexp_cadr (list); |
1643 | 0 | gcry_sexp_release (list); |
1644 | 0 | list = l2; |
1645 | 0 | if (!list) |
1646 | 0 | { |
1647 | 0 | err = gpg_error (GPG_ERR_NO_OBJ); |
1648 | 0 | goto leave; |
1649 | 0 | } |
1650 | | |
1651 | 0 | l2 = gcry_sexp_find_token (list, "p", 1); |
1652 | 0 | if (!l2) |
1653 | 0 | { |
1654 | 0 | err = gpg_error (GPG_ERR_NO_OBJ); /* required parameter not found */ |
1655 | 0 | goto leave; |
1656 | 0 | } |
1657 | 0 | array[arrayidx] = gcry_sexp_nth_mpi (l2, 1, GCRYMPI_FMT_OPAQUE); |
1658 | 0 | gcry_sexp_release (l2); |
1659 | 0 | if (!array[arrayidx]) |
1660 | 0 | { |
1661 | 0 | err = gpg_error (GPG_ERR_INV_OBJ); /* required parameter invalid */ |
1662 | 0 | goto leave; |
1663 | 0 | } |
1664 | 0 | } |
1665 | 0 | else if (algo == PUBKEY_ALGO_ECDH) |
1666 | 0 | { |
1667 | 0 | array[2] = pk_ecdh_default_params (nbits); |
1668 | 0 | if (!array[2]) |
1669 | 0 | { |
1670 | 0 | err = gpg_error_from_syserror (); |
1671 | 0 | goto leave; |
1672 | 0 | } |
1673 | 0 | } |
1674 | | |
1675 | 0 | leave: |
1676 | 0 | xfree (curve); |
1677 | 0 | gcry_sexp_release (list); |
1678 | 0 | if (err) |
1679 | 0 | { |
1680 | 0 | for (i=0; i < 3; i++) |
1681 | 0 | { |
1682 | 0 | gcry_mpi_release (array[i]); |
1683 | 0 | array[i] = NULL; |
1684 | 0 | } |
1685 | 0 | } |
1686 | 0 | return err; |
1687 | 0 | } |
1688 | | |
1689 | | |
1690 | | /* Extract key parameters from SEXP and store them in ARRAY. ELEMS is |
1691 | | a string where each character denotes a parameter name. TOPNAME is |
1692 | | the name of the top element above the elements. */ |
1693 | | static int |
1694 | | key_from_sexp (gcry_mpi_t *array, gcry_sexp_t sexp, |
1695 | | const char *topname, const char *elems) |
1696 | 0 | { |
1697 | 0 | gcry_sexp_t list, l2; |
1698 | 0 | const char *s; |
1699 | 0 | int i, idx; |
1700 | 0 | int rc = 0; |
1701 | |
|
1702 | 0 | list = gcry_sexp_find_token (sexp, topname, 0); |
1703 | 0 | if (!list) |
1704 | 0 | return gpg_error (GPG_ERR_INV_OBJ); |
1705 | 0 | l2 = gcry_sexp_cadr (list); |
1706 | 0 | gcry_sexp_release (list); |
1707 | 0 | list = l2; |
1708 | 0 | if (!list) |
1709 | 0 | return gpg_error (GPG_ERR_NO_OBJ); |
1710 | | |
1711 | 0 | for (idx=0,s=elems; *s; s++, idx++) |
1712 | 0 | { |
1713 | 0 | l2 = gcry_sexp_find_token (list, s, 1); |
1714 | 0 | if (!l2) |
1715 | 0 | { |
1716 | 0 | rc = gpg_error (GPG_ERR_NO_OBJ); /* required parameter not found */ |
1717 | 0 | goto leave; |
1718 | 0 | } |
1719 | 0 | array[idx] = gcry_sexp_nth_mpi (l2, 1, GCRYMPI_FMT_USG); |
1720 | 0 | gcry_sexp_release (l2); |
1721 | 0 | if (!array[idx]) |
1722 | 0 | { |
1723 | 0 | rc = gpg_error (GPG_ERR_INV_OBJ); /* required parameter invalid */ |
1724 | 0 | goto leave; |
1725 | 0 | } |
1726 | 0 | } |
1727 | 0 | gcry_sexp_release (list); |
1728 | |
|
1729 | 0 | leave: |
1730 | 0 | if (rc) |
1731 | 0 | { |
1732 | 0 | for (i=0; i<idx; i++) |
1733 | 0 | { |
1734 | 0 | gcry_mpi_release (array[i]); |
1735 | 0 | array[i] = NULL; |
1736 | 0 | } |
1737 | 0 | gcry_sexp_release (list); |
1738 | 0 | } |
1739 | 0 | return rc; |
1740 | 0 | } |
1741 | | |
1742 | | |
1743 | | /* Create a keyblock using the given KEYGRIP. ALGO is the OpenPGP |
1744 | | * algorithm of that keygrip. If CARDKEY is true the key is expected |
1745 | | * to already live on the active card. */ |
1746 | | static int |
1747 | | do_create_from_keygrip (ctrl_t ctrl, int algo, |
1748 | | const char *hexkeygrip, int cardkey, |
1749 | | kbnode_t pub_root, u32 timestamp, u32 expireval, |
1750 | | int is_subkey, int *keygen_flags) |
1751 | 0 | { |
1752 | 0 | int err; |
1753 | 0 | PACKET *pkt; |
1754 | 0 | PKT_public_key *pk; |
1755 | 0 | gcry_sexp_t s_key; |
1756 | 0 | gcry_sexp_t s_key2 = NULL; |
1757 | 0 | const char *algoelem; |
1758 | 0 | char *hexkeygrip_buffer = NULL; |
1759 | 0 | char *hexkeygrip2 = NULL; |
1760 | |
|
1761 | 0 | if (hexkeygrip[0] == '&') |
1762 | 0 | hexkeygrip++; |
1763 | 0 | if (strchr (hexkeygrip, ',')) |
1764 | 0 | { |
1765 | 0 | hexkeygrip_buffer = xtrystrdup (hexkeygrip); |
1766 | 0 | if (!hexkeygrip_buffer) |
1767 | 0 | return gpg_error_from_syserror (); |
1768 | 0 | hexkeygrip = hexkeygrip_buffer; |
1769 | 0 | hexkeygrip2 = strchr (hexkeygrip_buffer, ','); |
1770 | 0 | if (hexkeygrip2) |
1771 | 0 | *hexkeygrip2++ = 0; |
1772 | 0 | } |
1773 | | |
1774 | | |
1775 | 0 | switch (algo) |
1776 | 0 | { |
1777 | 0 | case PUBKEY_ALGO_RSA: algoelem = "ne"; break; |
1778 | 0 | case PUBKEY_ALGO_DSA: algoelem = "pqgy"; break; |
1779 | 0 | case PUBKEY_ALGO_ELGAMAL_E: algoelem = "pgy"; break; |
1780 | 0 | case PUBKEY_ALGO_ECDH: |
1781 | 0 | case PUBKEY_ALGO_ECDSA: algoelem = ""; break; |
1782 | 0 | case PUBKEY_ALGO_EDDSA: algoelem = ""; break; |
1783 | 0 | case PUBKEY_ALGO_KYBER: algoelem = ""; break; |
1784 | 0 | default: |
1785 | 0 | xfree (hexkeygrip_buffer); |
1786 | 0 | return gpg_error (GPG_ERR_INTERNAL); |
1787 | 0 | } |
1788 | | |
1789 | | /* Ask the agent for the public key matching HEXKEYGRIP. */ |
1790 | 0 | if (cardkey) |
1791 | 0 | { |
1792 | 0 | err = agent_scd_readkey (ctrl, hexkeygrip, &s_key, NULL); |
1793 | 0 | if (err) |
1794 | 0 | { |
1795 | 0 | xfree (hexkeygrip_buffer); |
1796 | 0 | return err; |
1797 | 0 | } |
1798 | 0 | } |
1799 | 0 | else |
1800 | 0 | { |
1801 | 0 | unsigned char *public; |
1802 | |
|
1803 | 0 | err = agent_readkey (ctrl, 0, hexkeygrip, &public); |
1804 | 0 | if (err) |
1805 | 0 | { |
1806 | 0 | xfree (hexkeygrip_buffer); |
1807 | 0 | return err; |
1808 | 0 | } |
1809 | 0 | err = gcry_sexp_sscan (&s_key, NULL, public, |
1810 | 0 | gcry_sexp_canon_len (public, 0, NULL, NULL)); |
1811 | 0 | xfree (public); |
1812 | 0 | if (err) |
1813 | 0 | { |
1814 | 0 | xfree (hexkeygrip_buffer); |
1815 | 0 | return err; |
1816 | 0 | } |
1817 | 0 | if (hexkeygrip2) |
1818 | 0 | { |
1819 | 0 | err = agent_readkey (ctrl, 0, hexkeygrip2, &public); |
1820 | 0 | if (err) |
1821 | 0 | { |
1822 | 0 | gcry_sexp_release (s_key); |
1823 | 0 | xfree (hexkeygrip_buffer); |
1824 | 0 | return err; |
1825 | 0 | } |
1826 | 0 | err = gcry_sexp_sscan (&s_key2, NULL, public, |
1827 | 0 | gcry_sexp_canon_len (public, 0, NULL, NULL)); |
1828 | 0 | xfree (public); |
1829 | 0 | if (err) |
1830 | 0 | { |
1831 | 0 | gcry_sexp_release (s_key); |
1832 | 0 | xfree (hexkeygrip_buffer); |
1833 | 0 | return err; |
1834 | 0 | } |
1835 | 0 | } |
1836 | 0 | } |
1837 | | |
1838 | | /* For X448 and Kyber we force the use of v5 packets. For the 9980 |
1839 | | * algos we use v6 keys. */ |
1840 | 0 | if (algo == PUBKEY_ALGO_ED25519 |
1841 | 0 | || algo == PUBKEY_ALGO_X25519) |
1842 | 0 | *keygen_flags |= KEYGEN_FLAG_CREATE_V6_KEY; |
1843 | 0 | else if (curve_is_448 (s_key) || algo == PUBKEY_ALGO_KYBER) |
1844 | 0 | *keygen_flags |= KEYGEN_FLAG_CREATE_V5_KEY; |
1845 | | |
1846 | | /* Build a public key packet. */ |
1847 | 0 | pk = xtrycalloc (1, sizeof *pk); |
1848 | 0 | if (!pk) |
1849 | 0 | { |
1850 | 0 | err = gpg_error_from_syserror (); |
1851 | 0 | gcry_sexp_release (s_key); |
1852 | 0 | gcry_sexp_release (s_key2); |
1853 | 0 | xfree (hexkeygrip_buffer); |
1854 | 0 | return err; |
1855 | 0 | } |
1856 | | |
1857 | 0 | pk->timestamp = timestamp; |
1858 | 0 | pk->version = (*keygen_flags & KEYGEN_FLAG_CREATE_V6_KEY)? 6 : |
1859 | 0 | (*keygen_flags & KEYGEN_FLAG_CREATE_V5_KEY)? 5 : 4; |
1860 | 0 | if (expireval) |
1861 | 0 | pk->expiredate = pk->timestamp + expireval; |
1862 | 0 | pk->pubkey_algo = algo; |
1863 | |
|
1864 | 0 | if (algo == PUBKEY_ALGO_KYBER) |
1865 | 0 | err = ecckey_from_sexp (pk->pkey, s_key, s_key2, algo, pk->version); |
1866 | 0 | else if (algo == PUBKEY_ALGO_ECDSA |
1867 | 0 | || algo == PUBKEY_ALGO_EDDSA |
1868 | 0 | || algo == PUBKEY_ALGO_ECDH ) |
1869 | 0 | err = ecckey_from_sexp (pk->pkey, s_key, NULL, algo, pk->version); |
1870 | 0 | else |
1871 | 0 | err = key_from_sexp (pk->pkey, s_key, "public-key", algoelem); |
1872 | 0 | if (err) |
1873 | 0 | { |
1874 | 0 | log_error ("key_from_sexp failed: %s\n", gpg_strerror (err) ); |
1875 | 0 | gcry_sexp_release (s_key); |
1876 | 0 | gcry_sexp_release (s_key2); |
1877 | 0 | free_public_key (pk); |
1878 | 0 | xfree (hexkeygrip_buffer); |
1879 | 0 | return err; |
1880 | 0 | } |
1881 | 0 | gcry_sexp_release (s_key); |
1882 | 0 | gcry_sexp_release (s_key2); |
1883 | |
|
1884 | 0 | pkt = xtrycalloc (1, sizeof *pkt); |
1885 | 0 | if (!pkt) |
1886 | 0 | { |
1887 | 0 | err = gpg_error_from_syserror (); |
1888 | 0 | free_public_key (pk); |
1889 | 0 | xfree (hexkeygrip_buffer); |
1890 | 0 | return err; |
1891 | 0 | } |
1892 | | |
1893 | 0 | pkt->pkttype = is_subkey ? PKT_PUBLIC_SUBKEY : PKT_PUBLIC_KEY; |
1894 | 0 | pkt->pkt.public_key = pk; |
1895 | 0 | add_kbnode (pub_root, new_kbnode (pkt)); |
1896 | |
|
1897 | 0 | xfree (hexkeygrip_buffer); |
1898 | 0 | return 0; |
1899 | 0 | } |
1900 | | |
1901 | | |
1902 | | /* Common code for the key generation function gen_xxx. The optional |
1903 | | * (COMMON_GEN_CB,COMMON_GEN_CB_PARM) can be used as communication |
1904 | | * object. A KEYPARMS2 forces the use of a composite key (e.g. Kyber+ECC). |
1905 | | */ |
1906 | | static int |
1907 | | common_gen (const char *keyparms, const char *keyparms2, |
1908 | | int algo, const char *algoelem, |
1909 | | kbnode_t pub_root, u32 timestamp, u32 expireval, int is_subkey, |
1910 | | int keygen_flags, const char *passphrase, |
1911 | | char **cache_nonce_addr, char **passwd_nonce_addr, |
1912 | | gpg_error_t (*common_gen_cb)(common_gen_cb_parm_t), |
1913 | | common_gen_cb_parm_t common_gen_cb_parm) |
1914 | 0 | { |
1915 | 0 | int err; |
1916 | 0 | PACKET *pkt; |
1917 | 0 | PKT_public_key *pk; |
1918 | 0 | gcry_sexp_t s_key; |
1919 | 0 | gcry_sexp_t s_key2 = NULL; |
1920 | |
|
1921 | 0 | err = agent_genkey (NULL, cache_nonce_addr, passwd_nonce_addr, keyparms, |
1922 | 0 | !!(keygen_flags & KEYGEN_FLAG_NO_PROTECTION), |
1923 | 0 | passphrase, timestamp, |
1924 | 0 | &s_key); |
1925 | 0 | if (err) |
1926 | 0 | { |
1927 | 0 | log_error ("agent_genkey failed: %s\n", gpg_strerror (err) ); |
1928 | 0 | return err; |
1929 | 0 | } |
1930 | | |
1931 | 0 | if (keyparms2) |
1932 | 0 | { |
1933 | 0 | unsigned char tmpgrip[KEYGRIP_LEN]; |
1934 | 0 | char hexgrip1[2*KEYGRIP_LEN+1]; |
1935 | 0 | char hexgrip2[2*KEYGRIP_LEN+1]; |
1936 | |
|
1937 | 0 | err = agent_genkey (NULL, NULL, NULL, keyparms2, |
1938 | 0 | 1 /* No protection */, |
1939 | 0 | NULL, timestamp, |
1940 | 0 | &s_key2); |
1941 | 0 | if (err) |
1942 | 0 | { |
1943 | 0 | log_error ("agent_genkey failed for second algo: %s\n", |
1944 | 0 | gpg_strerror (err) ); |
1945 | 0 | gcry_sexp_release (s_key); |
1946 | 0 | return err; |
1947 | 0 | } |
1948 | | |
1949 | 0 | if (!gcry_pk_get_keygrip (s_key, tmpgrip)) |
1950 | 0 | { |
1951 | 0 | log_error ("error computing keygrip for generated key\n"); |
1952 | 0 | gcry_sexp_release (s_key); |
1953 | 0 | gcry_sexp_release (s_key2); |
1954 | 0 | return gpg_error (GPG_ERR_GENERAL); |
1955 | 0 | } |
1956 | 0 | bin2hex (tmpgrip, KEYGRIP_LEN, hexgrip1); |
1957 | 0 | if (!gcry_pk_get_keygrip (s_key2, tmpgrip)) |
1958 | 0 | { |
1959 | 0 | log_error ("error computing keygrip for generated key\n"); |
1960 | 0 | gcry_sexp_release (s_key); |
1961 | 0 | gcry_sexp_release (s_key2); |
1962 | 0 | return gpg_error (GPG_ERR_GENERAL); |
1963 | 0 | } |
1964 | 0 | bin2hex (tmpgrip, KEYGRIP_LEN, hexgrip2); |
1965 | 0 | err = agent_crosslink_keys (NULL, hexgrip1, hexgrip2); |
1966 | 0 | if (err) |
1967 | 0 | { |
1968 | 0 | log_error ("error setting link attributes for generated keys\n"); |
1969 | 0 | gcry_sexp_release (s_key); |
1970 | 0 | gcry_sexp_release (s_key2); |
1971 | 0 | return gpg_error (GPG_ERR_GENERAL); |
1972 | 0 | } |
1973 | 0 | } |
1974 | | |
1975 | 0 | if (common_gen_cb && common_gen_cb_parm) |
1976 | 0 | { |
1977 | 0 | common_gen_cb_parm->genkey_result = s_key; |
1978 | 0 | common_gen_cb_parm->genkey_result2 = s_key2; |
1979 | 0 | err = common_gen_cb (common_gen_cb_parm); |
1980 | 0 | common_gen_cb_parm->genkey_result = NULL; |
1981 | 0 | common_gen_cb_parm->genkey_result2 = NULL; |
1982 | 0 | if (err) |
1983 | 0 | { |
1984 | 0 | gcry_sexp_release (s_key); |
1985 | 0 | gcry_sexp_release (s_key2); |
1986 | 0 | return err; |
1987 | 0 | } |
1988 | 0 | } |
1989 | | |
1990 | 0 | pk = xtrycalloc (1, sizeof *pk); |
1991 | 0 | if (!pk) |
1992 | 0 | { |
1993 | 0 | err = gpg_error_from_syserror (); |
1994 | 0 | gcry_sexp_release (s_key); |
1995 | 0 | return err; |
1996 | 0 | } |
1997 | | |
1998 | 0 | pk->timestamp = timestamp; |
1999 | 0 | pk->version = (keygen_flags & KEYGEN_FLAG_CREATE_V6_KEY)? 6 : |
2000 | 0 | (keygen_flags & KEYGEN_FLAG_CREATE_V5_KEY)? 5 : 4; |
2001 | 0 | if (expireval) |
2002 | 0 | pk->expiredate = pk->timestamp + expireval; |
2003 | 0 | pk->pubkey_algo = algo; |
2004 | |
|
2005 | 0 | if (algo == PUBKEY_ALGO_KYBER || IS_PUBKEY_ALGO_MLK (algo)) |
2006 | 0 | err = ecckey_from_sexp (pk->pkey, s_key, s_key2, algo, pk->version); |
2007 | 0 | else if (algo == PUBKEY_ALGO_ECDSA |
2008 | 0 | || algo == PUBKEY_ALGO_EDDSA |
2009 | 0 | || algo == PUBKEY_ALGO_ECDH |
2010 | 0 | || algo == PUBKEY_ALGO_X25519 |
2011 | 0 | || algo == PUBKEY_ALGO_ED25519) |
2012 | 0 | err = ecckey_from_sexp (pk->pkey, s_key, NULL, algo, pk->version); |
2013 | 0 | else |
2014 | 0 | err = key_from_sexp (pk->pkey, s_key, "public-key", algoelem); |
2015 | 0 | if (err) |
2016 | 0 | { |
2017 | 0 | log_error ("key_from_sexp failed: %s\n", gpg_strerror (err) ); |
2018 | 0 | gcry_sexp_release (s_key); |
2019 | 0 | free_public_key (pk); |
2020 | 0 | return err; |
2021 | 0 | } |
2022 | 0 | gcry_sexp_release (s_key); |
2023 | 0 | gcry_sexp_release (s_key2); |
2024 | |
|
2025 | 0 | pkt = xtrycalloc (1, sizeof *pkt); |
2026 | 0 | if (!pkt) |
2027 | 0 | { |
2028 | 0 | err = gpg_error_from_syserror (); |
2029 | 0 | free_public_key (pk); |
2030 | 0 | return err; |
2031 | 0 | } |
2032 | | |
2033 | 0 | pkt->pkttype = is_subkey ? PKT_PUBLIC_SUBKEY : PKT_PUBLIC_KEY; |
2034 | 0 | pkt->pkt.public_key = pk; |
2035 | 0 | add_kbnode (pub_root, new_kbnode (pkt)); |
2036 | |
|
2037 | 0 | return 0; |
2038 | 0 | } |
2039 | | |
2040 | | |
2041 | | /* |
2042 | | * Generate an Elgamal key. |
2043 | | */ |
2044 | | static int |
2045 | | gen_elg (int algo, unsigned int nbits, KBNODE pub_root, |
2046 | | u32 timestamp, u32 expireval, int is_subkey, |
2047 | | int keygen_flags, const char *passphrase, |
2048 | | char **cache_nonce_addr, char **passwd_nonce_addr, |
2049 | | gpg_error_t (*common_gen_cb)(common_gen_cb_parm_t), |
2050 | | common_gen_cb_parm_t common_gen_cb_parm) |
2051 | 0 | { |
2052 | 0 | int err; |
2053 | 0 | char *keyparms; |
2054 | 0 | char nbitsstr[35]; |
2055 | |
|
2056 | 0 | log_assert (is_ELGAMAL (algo)); |
2057 | | |
2058 | 0 | if (nbits < 1024) |
2059 | 0 | { |
2060 | 0 | nbits = 2048; |
2061 | 0 | log_info (_("keysize invalid; using %u bits\n"), nbits ); |
2062 | 0 | } |
2063 | 0 | else if (nbits > 4096) |
2064 | 0 | { |
2065 | 0 | nbits = 4096; |
2066 | 0 | log_info (_("keysize invalid; using %u bits\n"), nbits ); |
2067 | 0 | } |
2068 | |
|
2069 | 0 | if ((nbits % 32)) |
2070 | 0 | { |
2071 | 0 | nbits = ((nbits + 31) / 32) * 32; |
2072 | 0 | log_info (_("keysize rounded up to %u bits\n"), nbits ); |
2073 | 0 | } |
2074 | | |
2075 | | /* Note that we use transient-key only if no-protection has also |
2076 | | been enabled. */ |
2077 | 0 | snprintf (nbitsstr, sizeof nbitsstr, "%u", nbits); |
2078 | 0 | keyparms = xtryasprintf ("(genkey(%s(nbits %zu:%s)%s))", |
2079 | 0 | algo == GCRY_PK_ELG_E ? "openpgp-elg" : |
2080 | 0 | algo == GCRY_PK_ELG ? "elg" : "x-oops" , |
2081 | 0 | strlen (nbitsstr), nbitsstr, |
2082 | 0 | ((keygen_flags & KEYGEN_FLAG_TRANSIENT_KEY) |
2083 | 0 | && (keygen_flags & KEYGEN_FLAG_NO_PROTECTION))? |
2084 | 0 | "(transient-key)" : "" ); |
2085 | 0 | if (!keyparms) |
2086 | 0 | err = gpg_error_from_syserror (); |
2087 | 0 | else |
2088 | 0 | { |
2089 | 0 | err = common_gen (keyparms, NULL, algo, "pgy", |
2090 | 0 | pub_root, timestamp, expireval, is_subkey, |
2091 | 0 | keygen_flags, passphrase, |
2092 | 0 | cache_nonce_addr, passwd_nonce_addr, |
2093 | 0 | common_gen_cb, common_gen_cb_parm); |
2094 | 0 | xfree (keyparms); |
2095 | 0 | } |
2096 | |
|
2097 | 0 | return err; |
2098 | 0 | } |
2099 | | |
2100 | | |
2101 | | /* |
2102 | | * Generate an DSA key |
2103 | | */ |
2104 | | static gpg_error_t |
2105 | | gen_dsa (unsigned int nbits, KBNODE pub_root, |
2106 | | u32 timestamp, u32 expireval, int is_subkey, |
2107 | | int keygen_flags, const char *passphrase, |
2108 | | char **cache_nonce_addr, char **passwd_nonce_addr, |
2109 | | gpg_error_t (*common_gen_cb)(common_gen_cb_parm_t), |
2110 | | common_gen_cb_parm_t common_gen_cb_parm) |
2111 | 0 | { |
2112 | 0 | int err; |
2113 | 0 | unsigned int qbits; |
2114 | 0 | char *keyparms; |
2115 | 0 | char nbitsstr[35]; |
2116 | 0 | char qbitsstr[35]; |
2117 | |
|
2118 | 0 | if (nbits < 768) |
2119 | 0 | { |
2120 | 0 | nbits = 2048; |
2121 | 0 | log_info(_("keysize invalid; using %u bits\n"), nbits ); |
2122 | 0 | } |
2123 | 0 | else if ( nbits > 3072 ) |
2124 | 0 | { |
2125 | 0 | nbits = 3072; |
2126 | 0 | log_info(_("keysize invalid; using %u bits\n"), nbits ); |
2127 | 0 | } |
2128 | |
|
2129 | 0 | if( (nbits % 64) ) |
2130 | 0 | { |
2131 | 0 | nbits = ((nbits + 63) / 64) * 64; |
2132 | 0 | log_info(_("keysize rounded up to %u bits\n"), nbits ); |
2133 | 0 | } |
2134 | | |
2135 | | /* To comply with FIPS rules we round up to the next value unless in |
2136 | | expert mode. */ |
2137 | 0 | if (!opt.expert && nbits > 1024 && (nbits % 1024)) |
2138 | 0 | { |
2139 | 0 | nbits = ((nbits + 1023) / 1024) * 1024; |
2140 | 0 | log_info(_("keysize rounded up to %u bits\n"), nbits ); |
2141 | 0 | } |
2142 | | |
2143 | | /* |
2144 | | Figure out a q size based on the key size. FIPS 180-3 says: |
2145 | | |
2146 | | L = 1024, N = 160 |
2147 | | L = 2048, N = 224 |
2148 | | L = 2048, N = 256 |
2149 | | L = 3072, N = 256 |
2150 | | |
2151 | | 2048/256 is an odd pair since there is also a 2048/224 and |
2152 | | 3072/256. Matching sizes is not a very exact science. |
2153 | | |
2154 | | We'll do 256 qbits for nbits over 2047, 224 for nbits over 1024 |
2155 | | but less than 2048, and 160 for 1024 (DSA1). |
2156 | | */ |
2157 | |
|
2158 | 0 | if (nbits > 2047) |
2159 | 0 | qbits = 256; |
2160 | 0 | else if ( nbits > 1024) |
2161 | 0 | qbits = 224; |
2162 | 0 | else |
2163 | 0 | qbits = 160; |
2164 | |
|
2165 | 0 | if (qbits != 160 ) |
2166 | 0 | log_info (_("WARNING: some OpenPGP programs can't" |
2167 | 0 | " handle a DSA key with this digest size\n")); |
2168 | |
|
2169 | 0 | snprintf (nbitsstr, sizeof nbitsstr, "%u", nbits); |
2170 | 0 | snprintf (qbitsstr, sizeof qbitsstr, "%u", qbits); |
2171 | 0 | keyparms = xtryasprintf ("(genkey(dsa(nbits %zu:%s)(qbits %zu:%s)%s))", |
2172 | 0 | strlen (nbitsstr), nbitsstr, |
2173 | 0 | strlen (qbitsstr), qbitsstr, |
2174 | 0 | ((keygen_flags & KEYGEN_FLAG_TRANSIENT_KEY) |
2175 | 0 | && (keygen_flags & KEYGEN_FLAG_NO_PROTECTION))? |
2176 | 0 | "(transient-key)" : "" ); |
2177 | 0 | if (!keyparms) |
2178 | 0 | err = gpg_error_from_syserror (); |
2179 | 0 | else |
2180 | 0 | { |
2181 | 0 | err = common_gen (keyparms, NULL, PUBKEY_ALGO_DSA, "pqgy", |
2182 | 0 | pub_root, timestamp, expireval, is_subkey, |
2183 | 0 | keygen_flags, passphrase, |
2184 | 0 | cache_nonce_addr, passwd_nonce_addr, |
2185 | 0 | common_gen_cb, common_gen_cb_parm); |
2186 | 0 | xfree (keyparms); |
2187 | 0 | } |
2188 | |
|
2189 | 0 | return err; |
2190 | 0 | } |
2191 | | |
2192 | | |
2193 | | |
2194 | | /* |
2195 | | * Generate an ECC key. |
2196 | | * Note that KEYGEN_FLAGS might be updated by this function to |
2197 | | * indicate the forced creation of a v5 key. |
2198 | | */ |
2199 | | static gpg_error_t |
2200 | | gen_ecc (int algo, const char *curve, kbnode_t pub_root, |
2201 | | u32 timestamp, u32 expireval, int is_subkey, |
2202 | | int *keygen_flags, const char *passphrase, |
2203 | | char **cache_nonce_addr, char **passwd_nonce_addr, |
2204 | | gpg_error_t (*common_gen_cb)(common_gen_cb_parm_t), |
2205 | | common_gen_cb_parm_t common_gen_cb_parm) |
2206 | 0 | { |
2207 | 0 | gpg_error_t err; |
2208 | 0 | char *keyparms; |
2209 | |
|
2210 | 0 | log_assert (algo == PUBKEY_ALGO_ECDSA |
2211 | 0 | || algo == PUBKEY_ALGO_EDDSA |
2212 | 0 | || algo == PUBKEY_ALGO_ECDH |
2213 | 0 | || algo == PUBKEY_ALGO_X25519 |
2214 | 0 | || algo == PUBKEY_ALGO_ED25519); |
2215 | | |
2216 | 0 | if (!curve || !*curve) |
2217 | 0 | return gpg_error (GPG_ERR_UNKNOWN_CURVE); |
2218 | | |
2219 | | /* Map the displayed short forms of some curves to their canonical |
2220 | | * names. */ |
2221 | 0 | if (!ascii_strcasecmp (curve, "cv25519")) |
2222 | 0 | curve = "Curve25519"; |
2223 | 0 | else if (!ascii_strcasecmp (curve, "ed25519")) |
2224 | 0 | curve = "Ed25519"; |
2225 | 0 | else if (!ascii_strcasecmp (curve, "cv448")) |
2226 | 0 | curve = "X448"; |
2227 | 0 | else if (!ascii_strcasecmp (curve, "ed448")) |
2228 | 0 | curve = "Ed448"; |
2229 | | |
2230 | | /* Note that we use the "comp" flag with EdDSA to request the use of |
2231 | | a 0x40 compression prefix octet. */ |
2232 | 0 | if (algo == PUBKEY_ALGO_EDDSA && !strcmp (curve, "Ed25519")) |
2233 | 0 | { |
2234 | 0 | keyparms = xtryasprintf |
2235 | 0 | ("(genkey(ecc(curve %zu:%s)(flags eddsa comp%s)))", |
2236 | 0 | strlen (curve), curve, |
2237 | 0 | (((*keygen_flags & KEYGEN_FLAG_TRANSIENT_KEY) |
2238 | 0 | && (*keygen_flags & KEYGEN_FLAG_NO_PROTECTION))? |
2239 | 0 | " transient-key" : "")); |
2240 | 0 | } |
2241 | 0 | else if (algo == PUBKEY_ALGO_X25519) |
2242 | 0 | { |
2243 | 0 | *keygen_flags |= KEYGEN_FLAG_CREATE_V6_KEY; |
2244 | 0 | keyparms = xtryasprintf |
2245 | 0 | ("(genkey(ecc(curve ietf25)(flags%s)))", |
2246 | 0 | (((*keygen_flags & KEYGEN_FLAG_TRANSIENT_KEY) |
2247 | 0 | && (*keygen_flags & KEYGEN_FLAG_NO_PROTECTION))? |
2248 | 0 | " transient-key" : "")); |
2249 | 0 | } |
2250 | 0 | else if (algo == PUBKEY_ALGO_ED25519) |
2251 | 0 | { |
2252 | 0 | *keygen_flags |= KEYGEN_FLAG_CREATE_V6_KEY; |
2253 | 0 | keyparms = xtryasprintf |
2254 | 0 | ("(genkey(ecc(curve Ed25519)(flags eddsa%s)))", |
2255 | 0 | (((*keygen_flags & KEYGEN_FLAG_TRANSIENT_KEY) |
2256 | 0 | && (*keygen_flags & KEYGEN_FLAG_NO_PROTECTION))? |
2257 | 0 | " transient-key" : "")); |
2258 | 0 | } |
2259 | 0 | else if (algo == PUBKEY_ALGO_EDDSA && !strcmp (curve, "Ed448")) |
2260 | 0 | { |
2261 | 0 | *keygen_flags |= KEYGEN_FLAG_CREATE_V5_KEY; |
2262 | 0 | keyparms = xtryasprintf |
2263 | 0 | ("(genkey(ecc(curve %zu:%s)(flags comp%s)))", |
2264 | 0 | strlen (curve), curve, |
2265 | 0 | (((*keygen_flags & KEYGEN_FLAG_TRANSIENT_KEY) |
2266 | 0 | && (*keygen_flags & KEYGEN_FLAG_NO_PROTECTION))? |
2267 | 0 | " transient-key" : "")); |
2268 | 0 | } |
2269 | 0 | else if (algo == PUBKEY_ALGO_ECDH && !strcmp (curve, "Curve25519")) |
2270 | 0 | { |
2271 | 0 | keyparms = xtryasprintf |
2272 | 0 | ("(genkey(ecc(curve %zu:%s)(flags djb-tweak comp%s)))", |
2273 | 0 | strlen (curve), curve, |
2274 | 0 | (((*keygen_flags & KEYGEN_FLAG_TRANSIENT_KEY) |
2275 | 0 | && (*keygen_flags & KEYGEN_FLAG_NO_PROTECTION))? |
2276 | 0 | " transient-key" : "")); |
2277 | 0 | } |
2278 | 0 | else if (algo == PUBKEY_ALGO_ECDH && !strcmp (curve, "X448")) |
2279 | 0 | { |
2280 | 0 | *keygen_flags |= KEYGEN_FLAG_CREATE_V5_KEY; |
2281 | 0 | keyparms = xtryasprintf |
2282 | 0 | ("(genkey(ecc(curve %zu:%s)(flags comp%s)))", |
2283 | 0 | strlen (curve), curve, |
2284 | 0 | (((*keygen_flags & KEYGEN_FLAG_TRANSIENT_KEY) |
2285 | 0 | && (*keygen_flags & KEYGEN_FLAG_NO_PROTECTION))? |
2286 | 0 | " transient-key" : "")); |
2287 | 0 | } |
2288 | 0 | else |
2289 | 0 | { |
2290 | 0 | keyparms = xtryasprintf |
2291 | 0 | ("(genkey(ecc(curve %zu:%s)(flags nocomp%s)))", |
2292 | 0 | strlen (curve), curve, |
2293 | 0 | (((*keygen_flags & KEYGEN_FLAG_TRANSIENT_KEY) |
2294 | 0 | && (*keygen_flags & KEYGEN_FLAG_NO_PROTECTION))? |
2295 | 0 | " transient-key" : "")); |
2296 | 0 | } |
2297 | |
|
2298 | 0 | if (!keyparms) |
2299 | 0 | err = gpg_error_from_syserror (); |
2300 | 0 | else |
2301 | 0 | { |
2302 | 0 | err = common_gen (keyparms, NULL, algo, "", |
2303 | 0 | pub_root, timestamp, expireval, is_subkey, |
2304 | 0 | *keygen_flags, passphrase, |
2305 | 0 | cache_nonce_addr, passwd_nonce_addr, |
2306 | 0 | common_gen_cb, common_gen_cb_parm); |
2307 | 0 | xfree (keyparms); |
2308 | 0 | } |
2309 | |
|
2310 | 0 | return err; |
2311 | 0 | } |
2312 | | |
2313 | | |
2314 | | /* Generate a dual ECC+Kyber key. Note that KEYGEN_FLAGS will be |
2315 | | * updated by this function to indicate the forced creation of a v5 |
2316 | | * key. */ |
2317 | | static gpg_error_t |
2318 | | gen_kyber (int algo, unsigned int nbits, const char *curve, kbnode_t pub_root, |
2319 | | u32 timestamp, u32 expireval, int is_subkey, |
2320 | | int *keygen_flags, const char *passphrase, |
2321 | | char **cache_nonce_addr, char **passwd_nonce_addr, |
2322 | | gpg_error_t (*common_gen_cb)(common_gen_cb_parm_t), |
2323 | | common_gen_cb_parm_t common_gen_cb_parm) |
2324 | 0 | { |
2325 | 0 | gpg_error_t err; |
2326 | 0 | char *keyparms1; |
2327 | 0 | const char *keyparms2; |
2328 | |
|
2329 | 0 | log_assert (algo == PUBKEY_ALGO_KYBER || IS_PUBKEY_ALGO_MLK (algo)); |
2330 | | |
2331 | 0 | if (nbits == 768) |
2332 | 0 | keyparms2 = "(genkey(kyber768))"; |
2333 | 0 | else if (nbits == 1024) |
2334 | 0 | keyparms2 = "(genkey(kyber1024))"; |
2335 | 0 | else |
2336 | 0 | return gpg_error (GPG_ERR_UNSUPPORTED_ALGORITHM); |
2337 | | |
2338 | 0 | if (!curve || !*curve) |
2339 | 0 | return gpg_error (GPG_ERR_UNKNOWN_CURVE); |
2340 | | |
2341 | 0 | *keygen_flags |= KEYGEN_FLAG_CREATE_V5_KEY; |
2342 | |
|
2343 | 0 | if (algo == PUBKEY_ALGO_MLK768_25519) |
2344 | 0 | { |
2345 | 0 | keyparms1 = xtryasprintf |
2346 | 0 | ("(genkey(ecc(curve ietf25)(flags%s)))", |
2347 | 0 | (((*keygen_flags & KEYGEN_FLAG_TRANSIENT_KEY) |
2348 | 0 | && (*keygen_flags & KEYGEN_FLAG_NO_PROTECTION))? |
2349 | 0 | " transient-key" : "")); |
2350 | 0 | } |
2351 | 0 | else if (algo == PUBKEY_ALGO_MLK768_NP384) |
2352 | 0 | { |
2353 | 0 | keyparms1 = xtryasprintf |
2354 | 0 | ("(genkey(ecc(curve nistp384)(flags%s)))", |
2355 | 0 | (((*keygen_flags & KEYGEN_FLAG_TRANSIENT_KEY) |
2356 | 0 | && (*keygen_flags & KEYGEN_FLAG_NO_PROTECTION))? |
2357 | 0 | " transient-key" : "")); |
2358 | 0 | } |
2359 | 0 | else if (algo == PUBKEY_ALGO_MLK768_BP384) |
2360 | 0 | { |
2361 | 0 | keyparms1 = xtryasprintf |
2362 | 0 | ("(genkey(ecc(curve bp384)(flags%s)))", |
2363 | 0 | (((*keygen_flags & KEYGEN_FLAG_TRANSIENT_KEY) |
2364 | 0 | && (*keygen_flags & KEYGEN_FLAG_NO_PROTECTION))? |
2365 | 0 | " transient-key" : "")); |
2366 | 0 | } |
2367 | 0 | else if (algo == PUBKEY_ALGO_MLK1024_448) |
2368 | 0 | { |
2369 | 0 | keyparms1 = xtryasprintf |
2370 | 0 | ("(genkey(ecc(curve X448)(flags%s)))", |
2371 | 0 | (((*keygen_flags & KEYGEN_FLAG_TRANSIENT_KEY) |
2372 | 0 | && (*keygen_flags & KEYGEN_FLAG_NO_PROTECTION))? |
2373 | 0 | " transient-key" : "")); |
2374 | 0 | } |
2375 | 0 | else if (algo == PUBKEY_ALGO_MLK1024_NP521) |
2376 | 0 | { |
2377 | 0 | keyparms1 = xtryasprintf |
2378 | 0 | ("(genkey(ecc(curve nistp521)(flags%s)))", |
2379 | 0 | (((*keygen_flags & KEYGEN_FLAG_TRANSIENT_KEY) |
2380 | 0 | && (*keygen_flags & KEYGEN_FLAG_NO_PROTECTION))? |
2381 | 0 | " transient-key" : "")); |
2382 | 0 | } |
2383 | 0 | else if (algo == PUBKEY_ALGO_MLK1024_BP512) |
2384 | 0 | { |
2385 | 0 | keyparms1 = xtryasprintf |
2386 | 0 | ("(genkey(ecc(curve bp512)(flags%s)))", |
2387 | 0 | (((*keygen_flags & KEYGEN_FLAG_TRANSIENT_KEY) |
2388 | 0 | && (*keygen_flags & KEYGEN_FLAG_NO_PROTECTION))? |
2389 | 0 | " transient-key" : "")); |
2390 | 0 | } |
2391 | 0 | else if (!strcmp (curve, "Curve25519")||!ascii_strcasecmp (curve, "cv25519")) |
2392 | 0 | { |
2393 | 0 | curve = "Curve25519"; |
2394 | 0 | keyparms1 = xtryasprintf |
2395 | 0 | ("(genkey(ecc(curve %zu:%s)(flags djb-tweak comp%s)))", |
2396 | 0 | strlen (curve), curve, |
2397 | 0 | (((*keygen_flags & KEYGEN_FLAG_TRANSIENT_KEY) |
2398 | 0 | && (*keygen_flags & KEYGEN_FLAG_NO_PROTECTION))? |
2399 | 0 | " transient-key" : "")); |
2400 | 0 | } |
2401 | 0 | else if (!strcmp (curve, "X448") || !ascii_strcasecmp (curve, "cv448")) |
2402 | 0 | { |
2403 | 0 | curve = "X448"; |
2404 | 0 | keyparms1 = xtryasprintf |
2405 | 0 | ("(genkey(ecc(curve %zu:%s)(flags comp%s)))", |
2406 | 0 | strlen (curve), curve, |
2407 | 0 | (((*keygen_flags & KEYGEN_FLAG_TRANSIENT_KEY) |
2408 | 0 | && (*keygen_flags & KEYGEN_FLAG_NO_PROTECTION))? |
2409 | 0 | " transient-key" : "")); |
2410 | 0 | } |
2411 | 0 | else /* Should we use the compressed format? Check smartcard support. */ |
2412 | 0 | { |
2413 | 0 | keyparms1 = xtryasprintf |
2414 | 0 | ("(genkey(ecc(curve %zu:%s)(flags nocomp%s)))", |
2415 | 0 | strlen (curve), curve, |
2416 | 0 | (((*keygen_flags & KEYGEN_FLAG_TRANSIENT_KEY) |
2417 | 0 | && (*keygen_flags & KEYGEN_FLAG_NO_PROTECTION))? |
2418 | 0 | " transient-key" : "")); |
2419 | 0 | } |
2420 | |
|
2421 | 0 | if (!keyparms1) |
2422 | 0 | err = gpg_error_from_syserror (); |
2423 | 0 | else |
2424 | 0 | { |
2425 | 0 | err = common_gen (keyparms1, keyparms2, algo, "", |
2426 | 0 | pub_root, timestamp, expireval, is_subkey, |
2427 | 0 | *keygen_flags, passphrase, |
2428 | 0 | cache_nonce_addr, passwd_nonce_addr, |
2429 | 0 | common_gen_cb, common_gen_cb_parm); |
2430 | 0 | xfree (keyparms1); |
2431 | 0 | } |
2432 | |
|
2433 | 0 | return err; |
2434 | 0 | } |
2435 | | |
2436 | | |
2437 | | /* |
2438 | | * Generate an RSA key. |
2439 | | */ |
2440 | | static int |
2441 | | gen_rsa (int algo, unsigned int nbits, KBNODE pub_root, |
2442 | | u32 timestamp, u32 expireval, int is_subkey, |
2443 | | int keygen_flags, const char *passphrase, |
2444 | | char **cache_nonce_addr, char **passwd_nonce_addr, |
2445 | | gpg_error_t (*common_gen_cb)(common_gen_cb_parm_t), |
2446 | | common_gen_cb_parm_t common_gen_cb_parm) |
2447 | 0 | { |
2448 | 0 | int err; |
2449 | 0 | char *keyparms; |
2450 | 0 | char nbitsstr[35]; |
2451 | 0 | const unsigned maxsize = (opt.flags.large_rsa ? 8192 : 4096); |
2452 | |
|
2453 | 0 | log_assert (is_RSA(algo)); |
2454 | | |
2455 | 0 | if (!nbits) |
2456 | 0 | nbits = get_keysize_range (algo, NULL, NULL); |
2457 | |
|
2458 | 0 | if (nbits < 1024) |
2459 | 0 | { |
2460 | 0 | nbits = 3072; |
2461 | 0 | log_info (_("keysize invalid; using %u bits\n"), nbits ); |
2462 | 0 | } |
2463 | 0 | else if (nbits > maxsize) |
2464 | 0 | { |
2465 | 0 | nbits = maxsize; |
2466 | 0 | log_info (_("keysize invalid; using %u bits\n"), nbits ); |
2467 | 0 | } |
2468 | |
|
2469 | 0 | if ((nbits % 32)) |
2470 | 0 | { |
2471 | 0 | nbits = ((nbits + 31) / 32) * 32; |
2472 | 0 | log_info (_("keysize rounded up to %u bits\n"), nbits ); |
2473 | 0 | } |
2474 | |
|
2475 | 0 | snprintf (nbitsstr, sizeof nbitsstr, "%u", nbits); |
2476 | 0 | keyparms = xtryasprintf ("(genkey(rsa(nbits %zu:%s)%s))", |
2477 | 0 | strlen (nbitsstr), nbitsstr, |
2478 | 0 | ((keygen_flags & KEYGEN_FLAG_TRANSIENT_KEY) |
2479 | 0 | && (keygen_flags & KEYGEN_FLAG_NO_PROTECTION))? |
2480 | 0 | "(transient-key)" : "" ); |
2481 | 0 | if (!keyparms) |
2482 | 0 | err = gpg_error_from_syserror (); |
2483 | 0 | else |
2484 | 0 | { |
2485 | 0 | err = common_gen (keyparms, NULL, algo, "ne", |
2486 | 0 | pub_root, timestamp, expireval, is_subkey, |
2487 | 0 | keygen_flags, passphrase, |
2488 | 0 | cache_nonce_addr, passwd_nonce_addr, |
2489 | 0 | common_gen_cb, common_gen_cb_parm); |
2490 | 0 | xfree (keyparms); |
2491 | 0 | } |
2492 | |
|
2493 | 0 | return err; |
2494 | 0 | } |
2495 | | |
2496 | | |
2497 | | /**************** |
2498 | | * check valid days: |
2499 | | * return 0 on error or the multiplier |
2500 | | */ |
2501 | | static int |
2502 | | check_valid_days( const char *s ) |
2503 | 0 | { |
2504 | 0 | if( !digitp(s) ) |
2505 | 0 | return 0; |
2506 | 0 | for( s++; *s; s++) |
2507 | 0 | if( !digitp(s) ) |
2508 | 0 | break; |
2509 | 0 | if( !*s ) |
2510 | 0 | return 1; |
2511 | 0 | if( s[1] ) |
2512 | 0 | return 0; /* e.g. "2323wc" */ |
2513 | 0 | if( *s == 'd' || *s == 'D' ) |
2514 | 0 | return 1; |
2515 | 0 | if( *s == 'w' || *s == 'W' ) |
2516 | 0 | return 7; |
2517 | 0 | if( *s == 'm' || *s == 'M' ) |
2518 | 0 | return 30; |
2519 | 0 | if( *s == 'y' || *s == 'Y' ) |
2520 | 0 | return 365; |
2521 | 0 | return 0; |
2522 | 0 | } |
2523 | | |
2524 | | |
2525 | | static void |
2526 | | print_key_flags(int flags) |
2527 | 0 | { |
2528 | 0 | if(flags&PUBKEY_USAGE_SIG) |
2529 | 0 | tty_printf("%s ",_("Sign")); |
2530 | |
|
2531 | 0 | if(flags&PUBKEY_USAGE_CERT) |
2532 | 0 | tty_printf("%s ",_("Certify")); |
2533 | |
|
2534 | 0 | if(flags&PUBKEY_USAGE_ENC) |
2535 | 0 | tty_printf("%s ",_("Encrypt")); |
2536 | |
|
2537 | 0 | if(flags&PUBKEY_USAGE_AUTH) |
2538 | 0 | tty_printf("%s ",_("Authenticate")); |
2539 | |
|
2540 | 0 | if(flags&PUBKEY_USAGE_RENC) |
2541 | 0 | tty_printf("%s ", "RENC"); |
2542 | 0 | } |
2543 | | |
2544 | | |
2545 | | /* Ask for the key flags and return them. CURRENT gives the current |
2546 | | * usage which should normally be given as 0. MASK gives the allowed |
2547 | | * flags. */ |
2548 | | unsigned int |
2549 | | ask_key_flags_with_mask (int algo, int subkey, unsigned int current, |
2550 | | unsigned int mask) |
2551 | 0 | { |
2552 | | /* TRANSLATORS: Please use only plain ASCII characters for the |
2553 | | * translation. If this is not possible use single digits. The |
2554 | | * string needs to 8 bytes long. Here is a description of the |
2555 | | * functions: |
2556 | | * |
2557 | | * s = Toggle signing capability |
2558 | | * e = Toggle encryption capability |
2559 | | * a = Toggle authentication capability |
2560 | | * q = Finish |
2561 | | */ |
2562 | 0 | const char *togglers = _("SsEeAaQq"); |
2563 | 0 | char *answer = NULL; |
2564 | 0 | const char *s; |
2565 | 0 | unsigned int possible; |
2566 | |
|
2567 | 0 | if ( strlen(togglers) != 8 ) |
2568 | 0 | { |
2569 | 0 | tty_printf ("NOTE: Bad translation at %s:%d. " |
2570 | 0 | "Please report.\n", __FILE__, __LINE__); |
2571 | 0 | togglers = "11223300"; |
2572 | 0 | } |
2573 | | |
2574 | | /* restrict the mask to the actual useful bits. */ |
2575 | | |
2576 | | /* Mask the possible usage flags. This is for example used for a |
2577 | | * card based key. For ECDH we need to allows additional usages if |
2578 | | * they are provided. RENC is not directly poissible here but see |
2579 | | * below for a workaround. */ |
2580 | 0 | possible = (openpgp_pk_algo_usage (algo) & mask); |
2581 | 0 | possible &= ~PUBKEY_USAGE_RENC; |
2582 | 0 | possible &= ~PUBKEY_USAGE_GROUP; |
2583 | 0 | if (algo == PUBKEY_ALGO_ECDH) |
2584 | 0 | possible |= (current & (PUBKEY_USAGE_ENC |
2585 | 0 | |PUBKEY_USAGE_CERT |
2586 | 0 | |PUBKEY_USAGE_SIG |
2587 | 0 | |PUBKEY_USAGE_AUTH)); |
2588 | | |
2589 | | /* However, only primary keys may certify. */ |
2590 | 0 | if (subkey) |
2591 | 0 | possible &= ~PUBKEY_USAGE_CERT; |
2592 | | |
2593 | | /* Preload the current set with the possible set, without |
2594 | | * authentication if CURRENT is 0. If CURRENT is non-zero we mask |
2595 | | * with all possible usages. */ |
2596 | 0 | if (current) |
2597 | 0 | current &= possible; |
2598 | 0 | else |
2599 | 0 | current = (possible&~PUBKEY_USAGE_AUTH); |
2600 | |
|
2601 | 0 | for (;;) |
2602 | 0 | { |
2603 | 0 | tty_printf("\n"); |
2604 | 0 | tty_printf(_("Possible actions for this %s key: "), |
2605 | 0 | (algo == PUBKEY_ALGO_ECDH |
2606 | 0 | || algo == PUBKEY_ALGO_ECDSA |
2607 | 0 | || algo == PUBKEY_ALGO_EDDSA) |
2608 | 0 | ? "ECC" : openpgp_pk_algo_name (algo)); |
2609 | 0 | print_key_flags(possible); |
2610 | 0 | tty_printf("\n"); |
2611 | 0 | tty_printf(_("Current allowed actions: ")); |
2612 | 0 | print_key_flags(current); |
2613 | 0 | tty_printf("\n\n"); |
2614 | |
|
2615 | 0 | if(possible&PUBKEY_USAGE_SIG) |
2616 | 0 | tty_printf(_(" (%c) Toggle the sign capability\n"), |
2617 | 0 | togglers[0]); |
2618 | 0 | if(possible&PUBKEY_USAGE_ENC) |
2619 | 0 | tty_printf(_(" (%c) Toggle the encrypt capability\n"), |
2620 | 0 | togglers[2]); |
2621 | 0 | if(possible&PUBKEY_USAGE_AUTH) |
2622 | 0 | tty_printf(_(" (%c) Toggle the authenticate capability\n"), |
2623 | 0 | togglers[4]); |
2624 | |
|
2625 | 0 | tty_printf(_(" (%c) Finished\n"),togglers[6]); |
2626 | 0 | tty_printf("\n"); |
2627 | |
|
2628 | 0 | xfree(answer); |
2629 | 0 | answer = cpr_get("keygen.flags",_("Your selection? ")); |
2630 | 0 | cpr_kill_prompt(); |
2631 | |
|
2632 | 0 | if (*answer == '=') |
2633 | 0 | { |
2634 | | /* Hack to allow direct entry of the capabilities. */ |
2635 | 0 | current = 0; |
2636 | 0 | for (s=answer+1; *s; s++) |
2637 | 0 | { |
2638 | 0 | if ((*s == 's' || *s == 'S') && (possible&PUBKEY_USAGE_SIG)) |
2639 | 0 | current |= PUBKEY_USAGE_SIG; |
2640 | 0 | else if ((*s == 'e' || *s == 'E') && (possible&PUBKEY_USAGE_ENC)) |
2641 | 0 | current |= PUBKEY_USAGE_ENC; |
2642 | 0 | else if ((*s == 'a' || *s == 'A') && (possible&PUBKEY_USAGE_AUTH)) |
2643 | 0 | current |= PUBKEY_USAGE_AUTH; |
2644 | 0 | else if (!subkey && *s == 'c') |
2645 | 0 | { |
2646 | | /* Accept 'c' for the primary key because USAGE_CERT |
2647 | | will be set anyway. This is for folks who |
2648 | | want to experiment with a cert-only primary key. */ |
2649 | 0 | current |= PUBKEY_USAGE_CERT; |
2650 | 0 | } |
2651 | 0 | else if ((*s == 'r' || *s == 'R') && (possible&PUBKEY_USAGE_ENC)) |
2652 | 0 | { |
2653 | | /* Allow to set RENC or an encryption capable key. |
2654 | | * This is on purpose not shown in the menu. */ |
2655 | 0 | current |= PUBKEY_USAGE_RENC; |
2656 | 0 | } |
2657 | 0 | } |
2658 | 0 | break; |
2659 | 0 | } |
2660 | 0 | else if (strlen(answer)>1) |
2661 | 0 | tty_printf(_("Invalid selection.\n")); |
2662 | 0 | else if(*answer=='\0' || *answer==togglers[6] || *answer==togglers[7]) |
2663 | 0 | break; |
2664 | 0 | else if((*answer==togglers[0] || *answer==togglers[1]) |
2665 | 0 | && possible&PUBKEY_USAGE_SIG) |
2666 | 0 | { |
2667 | 0 | if(current&PUBKEY_USAGE_SIG) |
2668 | 0 | current&=~PUBKEY_USAGE_SIG; |
2669 | 0 | else |
2670 | 0 | current|=PUBKEY_USAGE_SIG; |
2671 | 0 | } |
2672 | 0 | else if((*answer==togglers[2] || *answer==togglers[3]) |
2673 | 0 | && possible&PUBKEY_USAGE_ENC) |
2674 | 0 | { |
2675 | 0 | if(current&PUBKEY_USAGE_ENC) |
2676 | 0 | current&=~PUBKEY_USAGE_ENC; |
2677 | 0 | else |
2678 | 0 | current|=PUBKEY_USAGE_ENC; |
2679 | 0 | } |
2680 | 0 | else if((*answer==togglers[4] || *answer==togglers[5]) |
2681 | 0 | && possible&PUBKEY_USAGE_AUTH) |
2682 | 0 | { |
2683 | 0 | if(current&PUBKEY_USAGE_AUTH) |
2684 | 0 | current&=~PUBKEY_USAGE_AUTH; |
2685 | 0 | else |
2686 | 0 | current|=PUBKEY_USAGE_AUTH; |
2687 | 0 | } |
2688 | 0 | else |
2689 | 0 | tty_printf(_("Invalid selection.\n")); |
2690 | 0 | } |
2691 | |
|
2692 | 0 | xfree(answer); |
2693 | |
|
2694 | 0 | return current; |
2695 | 0 | } |
2696 | | |
2697 | | |
2698 | | unsigned int |
2699 | | ask_key_flags (int algo, int subkey, unsigned int current) |
2700 | 0 | { |
2701 | 0 | return ask_key_flags_with_mask (algo, subkey, current, ~0); |
2702 | 0 | } |
2703 | | |
2704 | | |
2705 | | /* Check whether we have a key for the key with HEXGRIP. Returns 0 if |
2706 | | there is no such key or the OpenPGP algo number for the key. */ |
2707 | | static int |
2708 | | check_keygrip (ctrl_t ctrl, const char *hexgrip) |
2709 | 0 | { |
2710 | 0 | gpg_error_t err; |
2711 | 0 | unsigned char *public; |
2712 | 0 | size_t publiclen; |
2713 | 0 | int algo; |
2714 | |
|
2715 | 0 | if (hexgrip[0] == '&') |
2716 | 0 | hexgrip++; |
2717 | |
|
2718 | 0 | err = agent_readkey (ctrl, 0, hexgrip, &public); |
2719 | 0 | if (err) |
2720 | 0 | return 0; |
2721 | 0 | publiclen = gcry_sexp_canon_len (public, 0, NULL, NULL); |
2722 | |
|
2723 | 0 | algo = get_pk_algo_from_canon_sexp (public, publiclen); |
2724 | 0 | xfree (public); |
2725 | |
|
2726 | 0 | return map_gcry_pk_to_openpgp (algo); |
2727 | 0 | } |
2728 | | |
2729 | | |
2730 | | |
2731 | | /* Ask for an algorithm. The function returns the algorithm id to |
2732 | | * create. If ADDMODE is false the function won't show an option to |
2733 | | * create the primary and subkey combined and won't set R_USAGE |
2734 | | * either. If a combined algorithm has been selected, the subkey |
2735 | | * algorithm is stored at R_SUBKEY_ALGO. If R_KEYGRIP is given, the |
2736 | | * user has the choice to enter the keygrip of an existing key. That |
2737 | | * keygrip is then stored at this address. The caller needs to free |
2738 | | * it. If R_CARDKEY is not NULL and the keygrip has been taken from |
2739 | | * an active card, true is stored there; if R_KEYTIME is not NULL the |
2740 | | * creation time of that key is then stored there. */ |
2741 | | static int |
2742 | | ask_algo (ctrl_t ctrl, int addmode, int *r_subkey_algo, unsigned int *r_usage, |
2743 | | char **r_keygrip, int *r_cardkey, u32 *r_keytime) |
2744 | 0 | { |
2745 | 0 | gpg_error_t err; |
2746 | 0 | char *keygrip = NULL; |
2747 | 0 | u32 keytime = 0; |
2748 | 0 | char *answer = NULL; |
2749 | 0 | int cardkey = 0; |
2750 | 0 | int algo; |
2751 | 0 | int dummy_algo; |
2752 | |
|
2753 | 0 | if (!r_subkey_algo) |
2754 | 0 | r_subkey_algo = &dummy_algo; |
2755 | |
|
2756 | 0 | tty_printf (_("Please select what kind of key you want:\n")); |
2757 | |
|
2758 | 0 | #if GPG_USE_RSA |
2759 | 0 | if (!addmode) |
2760 | 0 | tty_printf (_(" (%d) RSA and RSA%s\n"), 1, ""); |
2761 | 0 | #endif |
2762 | |
|
2763 | 0 | if (!addmode && opt.compliance != CO_DE_VS) |
2764 | 0 | tty_printf (_(" (%d) DSA and Elgamal%s\n"), 2, ""); |
2765 | |
|
2766 | 0 | if (opt.compliance != CO_DE_VS) |
2767 | 0 | tty_printf (_(" (%d) DSA (sign only)%s\n"), 3, ""); |
2768 | 0 | #if GPG_USE_RSA |
2769 | 0 | tty_printf (_(" (%d) RSA (sign only)%s\n"), 4, ""); |
2770 | 0 | #endif |
2771 | |
|
2772 | 0 | if (addmode) |
2773 | 0 | { |
2774 | 0 | if (opt.compliance != CO_DE_VS) |
2775 | 0 | tty_printf (_(" (%d) Elgamal (encrypt only)%s\n"), 5, ""); |
2776 | 0 | #if GPG_USE_RSA |
2777 | 0 | tty_printf (_(" (%d) RSA (encrypt only)%s\n"), 6, ""); |
2778 | 0 | #endif |
2779 | 0 | } |
2780 | 0 | if (opt.expert) |
2781 | 0 | { |
2782 | 0 | if (opt.compliance != CO_DE_VS) |
2783 | 0 | tty_printf (_(" (%d) DSA (set your own capabilities)%s\n"), 7, ""); |
2784 | 0 | #if GPG_USE_RSA |
2785 | 0 | tty_printf (_(" (%d) RSA (set your own capabilities)%s\n"), 8, ""); |
2786 | 0 | #endif |
2787 | 0 | } |
2788 | |
|
2789 | 0 | #if GPG_USE_ECDSA || GPG_USE_ECDH || GPG_USE_EDDSA |
2790 | 0 | if (!addmode) |
2791 | 0 | tty_printf (_(" (%d) ECC (sign and encrypt)%s\n"), 9, _(" *default*") ); |
2792 | 0 | tty_printf (_(" (%d) ECC (sign only)\n"), 10 ); |
2793 | 0 | if (opt.expert) |
2794 | 0 | tty_printf (_(" (%d) ECC (set your own capabilities)%s\n"), 11, ""); |
2795 | 0 | if (addmode) |
2796 | 0 | tty_printf (_(" (%d) ECC (encrypt only)%s\n"), 12, ""); |
2797 | 0 | #endif |
2798 | |
|
2799 | 0 | if (opt.expert && r_keygrip) |
2800 | 0 | tty_printf (_(" (%d) Existing key%s\n"), 13, ""); |
2801 | 0 | if (r_keygrip) |
2802 | 0 | tty_printf (_(" (%d) Existing key from card%s\n"), 14, ""); |
2803 | | |
2804 | | /* Reserve 15 for Dilithium primary + Kyber subkey. */ |
2805 | 0 | if (!addmode) |
2806 | 0 | tty_printf (_(" (%d) ECC and Kyber%s\n"), 16, ""); |
2807 | 0 | if (addmode) |
2808 | 0 | tty_printf (_(" (%d) Kyber (encrypt only)%s\n"), 17, ""); |
2809 | |
|
2810 | 0 | for (;;) |
2811 | 0 | { |
2812 | 0 | *r_usage = 0; |
2813 | 0 | *r_subkey_algo = 0; |
2814 | 0 | xfree (answer); |
2815 | 0 | answer = cpr_get ("keygen.algo", _("Your selection? ")); |
2816 | 0 | cpr_kill_prompt (); |
2817 | 0 | algo = *answer? atoi (answer) : 9; /* Default algo is 9 */ |
2818 | |
|
2819 | 0 | if (opt.compliance == CO_DE_VS |
2820 | 0 | && (algo == 2 || algo == 3 || algo == 5 || algo == 7)) |
2821 | 0 | { |
2822 | 0 | tty_printf (_("Invalid selection.\n")); |
2823 | 0 | } |
2824 | 0 | else if ((algo == 1 || !strcmp (answer, "rsa+rsa")) && !addmode) |
2825 | 0 | { |
2826 | 0 | algo = PUBKEY_ALGO_RSA; |
2827 | 0 | *r_subkey_algo = PUBKEY_ALGO_RSA; |
2828 | 0 | break; |
2829 | 0 | } |
2830 | 0 | else if ((algo == 2 || !strcmp (answer, "dsa+elg")) && !addmode) |
2831 | 0 | { |
2832 | 0 | algo = PUBKEY_ALGO_DSA; |
2833 | 0 | *r_subkey_algo = PUBKEY_ALGO_ELGAMAL_E; |
2834 | 0 | break; |
2835 | 0 | } |
2836 | 0 | else if (algo == 3 || !strcmp (answer, "dsa")) |
2837 | 0 | { |
2838 | 0 | algo = PUBKEY_ALGO_DSA; |
2839 | 0 | *r_usage = PUBKEY_USAGE_SIG; |
2840 | 0 | break; |
2841 | 0 | } |
2842 | 0 | else if (algo == 4 || !strcmp (answer, "rsa/s")) |
2843 | 0 | { |
2844 | 0 | algo = PUBKEY_ALGO_RSA; |
2845 | 0 | *r_usage = PUBKEY_USAGE_SIG; |
2846 | 0 | break; |
2847 | 0 | } |
2848 | 0 | else if ((algo == 5 || !strcmp (answer, "elg")) && addmode) |
2849 | 0 | { |
2850 | 0 | algo = PUBKEY_ALGO_ELGAMAL_E; |
2851 | 0 | *r_usage = PUBKEY_USAGE_ENC; |
2852 | 0 | break; |
2853 | 0 | } |
2854 | 0 | else if ((algo == 6 || !strcmp (answer, "rsa/e")) && addmode) |
2855 | 0 | { |
2856 | 0 | algo = PUBKEY_ALGO_RSA; |
2857 | 0 | *r_usage = PUBKEY_USAGE_ENC; |
2858 | 0 | break; |
2859 | 0 | } |
2860 | 0 | else if ((algo == 7 || !strcmp (answer, "dsa/*")) && opt.expert) |
2861 | 0 | { |
2862 | 0 | algo = PUBKEY_ALGO_DSA; |
2863 | 0 | *r_usage = ask_key_flags (algo, addmode, 0); |
2864 | 0 | break; |
2865 | 0 | } |
2866 | 0 | else if ((algo == 8 || !strcmp (answer, "rsa/*")) && opt.expert) |
2867 | 0 | { |
2868 | 0 | algo = PUBKEY_ALGO_RSA; |
2869 | 0 | *r_usage = ask_key_flags (algo, addmode, 0); |
2870 | 0 | break; |
2871 | 0 | } |
2872 | 0 | else if ((algo == 9 || !strcmp (answer, "ecc+ecc")) |
2873 | 0 | && !addmode) |
2874 | 0 | { |
2875 | 0 | algo = PUBKEY_ALGO_ECDSA; |
2876 | 0 | *r_subkey_algo = PUBKEY_ALGO_ECDH; |
2877 | 0 | break; |
2878 | 0 | } |
2879 | 0 | else if ((algo == 10 || !strcmp (answer, "ecc/s"))) |
2880 | 0 | { |
2881 | 0 | algo = PUBKEY_ALGO_ECDSA; |
2882 | 0 | *r_usage = PUBKEY_USAGE_SIG; |
2883 | 0 | break; |
2884 | 0 | } |
2885 | 0 | else if ((algo == 11 || !strcmp (answer, "ecc/*")) && opt.expert) |
2886 | 0 | { |
2887 | 0 | algo = PUBKEY_ALGO_ECDSA; |
2888 | 0 | *r_usage = ask_key_flags (algo, addmode, 0); |
2889 | 0 | break; |
2890 | 0 | } |
2891 | 0 | else if ((algo == 12 || !strcmp (answer, "ecc/e")) |
2892 | 0 | && addmode) |
2893 | 0 | { |
2894 | 0 | algo = PUBKEY_ALGO_ECDH; |
2895 | 0 | *r_usage = PUBKEY_USAGE_ENC; |
2896 | 0 | break; |
2897 | 0 | } |
2898 | 0 | else if ((algo == 13 || !strcmp (answer, "keygrip")) |
2899 | 0 | && opt.expert && r_keygrip) |
2900 | 0 | { |
2901 | 0 | for (;;) |
2902 | 0 | { |
2903 | 0 | xfree (answer); |
2904 | 0 | answer = cpr_get ("keygen.keygrip", _("Enter the keygrip: ")); |
2905 | 0 | cpr_kill_prompt (); |
2906 | 0 | trim_spaces (answer); |
2907 | 0 | if (!*answer) |
2908 | 0 | { |
2909 | 0 | xfree (answer); |
2910 | 0 | answer = NULL; |
2911 | 0 | continue; |
2912 | 0 | } |
2913 | | |
2914 | 0 | if (strlen (answer) == 40+1+40 && answer[40]==',') |
2915 | 0 | { |
2916 | 0 | int algo1, algo2; |
2917 | |
|
2918 | 0 | answer[40] = 0; |
2919 | 0 | algo1 = check_keygrip (ctrl, answer); |
2920 | 0 | algo2 = check_keygrip (ctrl, answer+41); |
2921 | 0 | answer[40] = ','; |
2922 | 0 | if (algo1 == PUBKEY_ALGO_ECDH && algo2 == PUBKEY_ALGO_KYBER) |
2923 | 0 | { |
2924 | 0 | algo = PUBKEY_ALGO_KYBER; |
2925 | 0 | break; |
2926 | 0 | } |
2927 | 0 | else if (!algo1 || !algo2) |
2928 | 0 | tty_printf (_("No key with this keygrip\n")); |
2929 | 0 | else |
2930 | 0 | tty_printf ("Invalid combination for dual algo (%d,%d)\n", |
2931 | 0 | algo1, algo2); |
2932 | 0 | } |
2933 | 0 | else if (strlen (answer) != 40 && |
2934 | 0 | !(answer[0] == '&' && strlen (answer+1) == 40)) |
2935 | 0 | tty_printf |
2936 | 0 | (_("Not a valid keygrip (expecting 40 hex digits)\n")); |
2937 | 0 | else if (!(algo = check_keygrip (ctrl, answer)) ) |
2938 | 0 | tty_printf (_("No key with this keygrip\n")); |
2939 | 0 | else |
2940 | 0 | break; /* Okay. */ |
2941 | 0 | } |
2942 | 0 | xfree (keygrip); |
2943 | 0 | keygrip = answer; |
2944 | 0 | answer = NULL; |
2945 | 0 | *r_usage = ask_key_flags (algo, addmode, 0); |
2946 | 0 | break; |
2947 | 0 | } |
2948 | 0 | else if ((algo == 14 || !strcmp (answer, "cardkey")) && r_keygrip) |
2949 | 0 | { |
2950 | 0 | char *serialno; |
2951 | 0 | keypair_info_t keypairlist, kpi; |
2952 | 0 | int count, selection; |
2953 | |
|
2954 | 0 | err = agent_scd_serialno (&serialno, NULL); |
2955 | 0 | if (err) |
2956 | 0 | { |
2957 | 0 | tty_printf (_("error reading the card: %s\n"), |
2958 | 0 | gpg_strerror (err)); |
2959 | 0 | goto ask_again; |
2960 | 0 | } |
2961 | 0 | tty_printf (_("Serial number of the card: %s\n"), serialno); |
2962 | 0 | xfree (serialno); |
2963 | |
|
2964 | 0 | err = agent_scd_keypairinfo (ctrl, NULL, &keypairlist); |
2965 | 0 | if (err) |
2966 | 0 | { |
2967 | 0 | tty_printf (_("error reading the card: %s\n"), |
2968 | 0 | gpg_strerror (err)); |
2969 | 0 | goto ask_again; |
2970 | 0 | } |
2971 | | |
2972 | 0 | do |
2973 | 0 | { |
2974 | 0 | char *authkeyref, *encrkeyref, *signkeyref; |
2975 | |
|
2976 | 0 | agent_scd_getattr_one ("$AUTHKEYID", &authkeyref); |
2977 | 0 | agent_scd_getattr_one ("$ENCRKEYID", &encrkeyref); |
2978 | 0 | agent_scd_getattr_one ("$SIGNKEYID", &signkeyref); |
2979 | |
|
2980 | 0 | tty_printf (_("Available keys:\n")); |
2981 | 0 | for (count=1, kpi=keypairlist; kpi; kpi = kpi->next, count++) |
2982 | 0 | { |
2983 | 0 | gcry_sexp_t s_pkey; |
2984 | 0 | char *algostr = NULL; |
2985 | 0 | enum gcry_pk_algos algoid = 0; |
2986 | 0 | const char *keyref = kpi->idstr; |
2987 | 0 | int any = 0; |
2988 | |
|
2989 | 0 | if (!keyref) |
2990 | 0 | continue; |
2991 | | |
2992 | 0 | if (agent_scd_readkey (ctrl, keyref, &s_pkey, NULL)) |
2993 | 0 | continue; |
2994 | | |
2995 | 0 | algostr = pubkey_algo_string (s_pkey, &algoid); |
2996 | 0 | gcry_sexp_release (s_pkey); |
2997 | | |
2998 | | /* We need to tweak the algo in case GCRY_PK_ECC is |
2999 | | * returned because pubkey_algo_string is not aware |
3000 | | * of the OpenPGP algo mapping. We need to |
3001 | | * distinguish between ECDH and ECDSA but we can do |
3002 | | * that only if we got usage flags. |
3003 | | * Note: Keep this in sync with parse_key_parameter_part. |
3004 | | */ |
3005 | 0 | if (algoid == GCRY_PK_ECC && algostr) |
3006 | 0 | { |
3007 | 0 | if (!strcmp (algostr, "ed25519")) |
3008 | 0 | kpi->algo = PUBKEY_ALGO_EDDSA; |
3009 | 0 | else if (!strcmp (algostr, "ed448")) |
3010 | 0 | kpi->algo = PUBKEY_ALGO_EDDSA; |
3011 | 0 | else if (!strcmp (algostr, "cv25519")) |
3012 | 0 | kpi->algo = PUBKEY_ALGO_ECDH; |
3013 | 0 | else if (!strcmp (algostr, "cv448")) |
3014 | 0 | kpi->algo = PUBKEY_ALGO_ECDH; |
3015 | 0 | else if ((kpi->usage & GCRY_PK_USAGE_ENCR)) |
3016 | 0 | kpi->algo = PUBKEY_ALGO_ECDH; |
3017 | 0 | else |
3018 | 0 | kpi->algo = PUBKEY_ALGO_ECDSA; |
3019 | 0 | } |
3020 | 0 | else |
3021 | 0 | kpi->algo = map_gcry_pk_to_openpgp (algoid); |
3022 | |
|
3023 | 0 | tty_printf (" (%d) %s %s %s", |
3024 | 0 | count, kpi->keygrip, keyref, algostr); |
3025 | 0 | if ((kpi->usage & GCRY_PK_USAGE_CERT)) |
3026 | 0 | { |
3027 | 0 | tty_printf ("%scert", any?",":" ("); |
3028 | 0 | any = 1; |
3029 | 0 | } |
3030 | 0 | if ((kpi->usage & GCRY_PK_USAGE_SIGN)) |
3031 | 0 | { |
3032 | 0 | tty_printf ("%ssign%s", any?",":" (", |
3033 | 0 | (signkeyref && keyref |
3034 | 0 | && !strcmp (signkeyref, keyref))? "*":""); |
3035 | 0 | any = 1; |
3036 | 0 | } |
3037 | 0 | if ((kpi->usage & GCRY_PK_USAGE_AUTH)) |
3038 | 0 | { |
3039 | 0 | tty_printf ("%sauth%s", any?",":" (", |
3040 | 0 | (authkeyref && keyref |
3041 | 0 | && !strcmp (authkeyref, keyref))? "*":""); |
3042 | 0 | any = 1; |
3043 | 0 | } |
3044 | 0 | if ((kpi->usage & GCRY_PK_USAGE_ENCR)) |
3045 | 0 | { |
3046 | 0 | tty_printf ("%sencr%s", any?",":" (", |
3047 | 0 | (encrkeyref && keyref |
3048 | 0 | && !strcmp (encrkeyref, keyref))? "*":""); |
3049 | 0 | any = 1; |
3050 | 0 | } |
3051 | 0 | tty_printf ("%s\n", any?")":""); |
3052 | 0 | xfree (algostr); |
3053 | 0 | } |
3054 | |
|
3055 | 0 | xfree (answer); |
3056 | 0 | answer = cpr_get ("keygen.cardkey", _("Your selection? ")); |
3057 | 0 | cpr_kill_prompt (); |
3058 | 0 | trim_spaces (answer); |
3059 | 0 | selection = atoi (answer); |
3060 | 0 | xfree (authkeyref); |
3061 | 0 | xfree (encrkeyref); |
3062 | 0 | xfree (signkeyref); |
3063 | 0 | } |
3064 | 0 | while (!(selection > 0 && selection < count)); |
3065 | |
|
3066 | 0 | for (count=1,kpi=keypairlist; kpi; kpi = kpi->next, count++) |
3067 | 0 | if (count == selection) |
3068 | 0 | break; |
3069 | 0 | if (!kpi || !kpi->algo) |
3070 | 0 | { |
3071 | | /* Just in case no good key. */ |
3072 | 0 | free_keypair_info (keypairlist); |
3073 | 0 | goto ask_again; |
3074 | 0 | } |
3075 | | |
3076 | 0 | xfree (keygrip); |
3077 | 0 | keygrip = xstrdup (kpi->keygrip); |
3078 | 0 | cardkey = 1; |
3079 | 0 | algo = kpi->algo; |
3080 | 0 | keytime = kpi->keytime; |
3081 | | |
3082 | | /* In expert mode allow to change the usage flags. */ |
3083 | 0 | if (opt.expert) |
3084 | 0 | *r_usage = ask_key_flags_with_mask (algo, addmode, |
3085 | 0 | kpi->usage, kpi->usage); |
3086 | 0 | else |
3087 | 0 | { |
3088 | 0 | *r_usage = kpi->usage; |
3089 | 0 | if (addmode) |
3090 | 0 | *r_usage &= ~GCRY_PK_USAGE_CERT; |
3091 | 0 | } |
3092 | 0 | free_keypair_info (keypairlist); |
3093 | 0 | break; |
3094 | 0 | } |
3095 | 0 | else if ((algo == 16 || !strcmp (answer, "ecc+kyber")) && !addmode) |
3096 | 0 | { |
3097 | 0 | algo = PUBKEY_ALGO_ECDSA; |
3098 | 0 | *r_subkey_algo = PUBKEY_ALGO_KYBER; |
3099 | 0 | break; |
3100 | 0 | } |
3101 | 0 | else if ((algo == 17 || !strcmp (answer, "kyber")) && addmode) |
3102 | 0 | { |
3103 | 0 | algo = PUBKEY_ALGO_KYBER; |
3104 | 0 | *r_usage = PUBKEY_USAGE_ENC; |
3105 | 0 | break; |
3106 | 0 | } |
3107 | 0 | else |
3108 | 0 | tty_printf (_("Invalid selection.\n")); |
3109 | | |
3110 | 0 | ask_again: |
3111 | 0 | ; |
3112 | 0 | } |
3113 | | |
3114 | 0 | xfree(answer); |
3115 | 0 | if (r_keygrip) |
3116 | 0 | *r_keygrip = keygrip; |
3117 | 0 | if (r_cardkey) |
3118 | 0 | *r_cardkey = cardkey; |
3119 | 0 | if (r_keytime) |
3120 | 0 | *r_keytime = keytime; |
3121 | 0 | return algo; |
3122 | 0 | } |
3123 | | |
3124 | | |
3125 | | static unsigned int |
3126 | | get_keysize_range (int algo, unsigned int *min, unsigned int *max) |
3127 | 0 | { |
3128 | 0 | unsigned int def; |
3129 | 0 | unsigned int dummy1, dummy2; |
3130 | |
|
3131 | 0 | if (!min) |
3132 | 0 | min = &dummy1; |
3133 | 0 | if (!max) |
3134 | 0 | max = &dummy2; |
3135 | |
|
3136 | 0 | switch(algo) |
3137 | 0 | { |
3138 | 0 | case PUBKEY_ALGO_DSA: |
3139 | 0 | *min = opt.expert? 768 : 1024; |
3140 | 0 | *max=3072; |
3141 | 0 | def=2048; |
3142 | 0 | break; |
3143 | | |
3144 | 0 | case PUBKEY_ALGO_ECDSA: |
3145 | 0 | case PUBKEY_ALGO_ECDH: |
3146 | 0 | *min=256; |
3147 | 0 | *max=521; |
3148 | 0 | def=256; |
3149 | 0 | break; |
3150 | | |
3151 | 0 | case PUBKEY_ALGO_EDDSA: |
3152 | 0 | case PUBKEY_ALGO_ED25519: |
3153 | 0 | *min=255; |
3154 | 0 | *max=441; /* FIXME: Really? What about X448? */ |
3155 | 0 | def=255; |
3156 | 0 | break; |
3157 | | |
3158 | 0 | case PUBKEY_ALGO_KYBER: |
3159 | 0 | case PUBKEY_ALGO_MLK768_25519: |
3160 | 0 | case PUBKEY_ALGO_MLK768_NP384: |
3161 | 0 | case PUBKEY_ALGO_MLK768_BP384: |
3162 | 0 | case PUBKEY_ALGO_MLK1024_448: |
3163 | 0 | case PUBKEY_ALGO_MLK1024_NP521: |
3164 | 0 | case PUBKEY_ALGO_MLK1024_BP512: |
3165 | 0 | *min = 768; |
3166 | 0 | *max = 1024; |
3167 | 0 | def = 768; |
3168 | 0 | break; |
3169 | | |
3170 | 0 | default: |
3171 | 0 | *min = opt.compliance == CO_DE_VS ? 2048: 1024; |
3172 | 0 | *max = 4096; |
3173 | 0 | def = 3072; |
3174 | 0 | break; |
3175 | 0 | } |
3176 | | |
3177 | 0 | return def; |
3178 | 0 | } |
3179 | | |
3180 | | |
3181 | | /* Return a fixed up keysize depending on ALGO. */ |
3182 | | static unsigned int |
3183 | | fixup_keysize (unsigned int nbits, int algo, int silent) |
3184 | 0 | { |
3185 | 0 | unsigned int orig_nbits = nbits; |
3186 | |
|
3187 | 0 | if (algo == PUBKEY_ALGO_DSA && (nbits % 64)) |
3188 | 0 | { |
3189 | 0 | nbits = ((nbits + 63) / 64) * 64; |
3190 | 0 | } |
3191 | 0 | else if (algo == PUBKEY_ALGO_EDDSA || algo == PUBKEY_ALGO_ED25519) |
3192 | 0 | { |
3193 | 0 | if (nbits < 256) |
3194 | 0 | nbits = 255; |
3195 | 0 | else |
3196 | 0 | nbits = 441; |
3197 | 0 | } |
3198 | 0 | else if (algo == PUBKEY_ALGO_ECDH || algo == PUBKEY_ALGO_ECDSA) |
3199 | 0 | { |
3200 | 0 | if (nbits < 256) |
3201 | 0 | nbits = 256; |
3202 | 0 | else if (nbits < 384) |
3203 | 0 | nbits = 384; |
3204 | 0 | else |
3205 | 0 | nbits = 521; |
3206 | 0 | } |
3207 | 0 | else if (algo == PUBKEY_ALGO_KYBER || IS_PUBKEY_ALGO_MLK (algo)) |
3208 | 0 | { |
3209 | | /* (in reality the numbers are not bits) */ |
3210 | 0 | if (nbits < 768) |
3211 | 0 | nbits = 768; |
3212 | 0 | else if (nbits > 1024) |
3213 | 0 | nbits = 1024; |
3214 | 0 | } |
3215 | 0 | else if ((nbits % 32)) |
3216 | 0 | { |
3217 | 0 | nbits = ((nbits + 31) / 32) * 32; |
3218 | 0 | } |
3219 | |
|
3220 | 0 | if (!silent && orig_nbits != nbits) |
3221 | 0 | tty_printf (_("rounded to %u bits\n"), nbits); |
3222 | |
|
3223 | 0 | return nbits; |
3224 | 0 | } |
3225 | | |
3226 | | |
3227 | | /* Ask for the key size. ALGO is the algorithm. If PRIMARY_KEYSIZE |
3228 | | is not 0, the function asks for the size of the encryption |
3229 | | subkey. */ |
3230 | | static unsigned |
3231 | | ask_keysize (int algo, unsigned int primary_keysize) |
3232 | 0 | { |
3233 | 0 | unsigned int nbits; |
3234 | 0 | unsigned int min, def, max; |
3235 | 0 | int for_subkey = !!primary_keysize; |
3236 | 0 | int autocomp = 0; |
3237 | |
|
3238 | 0 | def = get_keysize_range (algo, &min, &max); |
3239 | |
|
3240 | 0 | if (primary_keysize && !opt.expert) |
3241 | 0 | { |
3242 | | /* Deduce the subkey size from the primary key size. */ |
3243 | 0 | if (algo == PUBKEY_ALGO_DSA && primary_keysize > 3072) |
3244 | 0 | nbits = 3072; /* For performance reasons we don't support more |
3245 | | than 3072 bit DSA. However we won't see this |
3246 | | case anyway because DSA can't be used as an |
3247 | | encryption subkey ;-). */ |
3248 | 0 | else |
3249 | 0 | nbits = primary_keysize; |
3250 | 0 | autocomp = 1; |
3251 | 0 | goto leave; |
3252 | 0 | } |
3253 | | |
3254 | 0 | tty_printf(_("%s keys may be between %u and %u bits long.\n"), |
3255 | 0 | openpgp_pk_algo_name (algo), min, max); |
3256 | |
|
3257 | 0 | for (;;) |
3258 | 0 | { |
3259 | 0 | char *prompt, *answer; |
3260 | |
|
3261 | 0 | if (for_subkey) |
3262 | 0 | prompt = xasprintf (_("What keysize do you want " |
3263 | 0 | "for the subkey? (%u) "), def); |
3264 | 0 | else |
3265 | 0 | prompt = xasprintf (_("What keysize do you want? (%u) "), def); |
3266 | 0 | answer = cpr_get ("keygen.size", prompt); |
3267 | 0 | cpr_kill_prompt (); |
3268 | 0 | nbits = *answer? atoi (answer): def; |
3269 | 0 | xfree(prompt); |
3270 | 0 | xfree(answer); |
3271 | |
|
3272 | 0 | if(nbits<min || nbits>max) |
3273 | 0 | tty_printf(_("%s keysizes must be in the range %u-%u\n"), |
3274 | 0 | openpgp_pk_algo_name (algo), min, max); |
3275 | 0 | else |
3276 | 0 | break; |
3277 | 0 | } |
3278 | |
|
3279 | 0 | tty_printf (_("Requested keysize is %u bits\n"), nbits); |
3280 | |
|
3281 | 0 | leave: |
3282 | 0 | nbits = fixup_keysize (nbits, algo, autocomp); |
3283 | 0 | return nbits; |
3284 | 0 | } |
3285 | | |
3286 | | |
3287 | | /* Ask for the curve. ALGO is the selected algorithm which this |
3288 | | function may adjust. Returns a const string of the name of the |
3289 | | curve. */ |
3290 | | const char * |
3291 | | ask_curve (int *algo, int *subkey_algo, const char *current) |
3292 | 0 | { |
3293 | | /* NB: We always use a complete algo list so that we have stable |
3294 | | numbers in the menu regardless on how Gpg was configured. */ |
3295 | 0 | struct { |
3296 | 0 | const char *name; |
3297 | 0 | const char* eddsa_curve; /* Corresponding EdDSA curve. */ |
3298 | 0 | const char *pretty_name; |
3299 | 0 | unsigned int supported : 1; /* Supported by gpg. */ |
3300 | 0 | unsigned int de_vs : 1; /* Allowed in CO_DE_VS. */ |
3301 | 0 | unsigned int expert_only : 1; /* Only with --expert */ |
3302 | 0 | unsigned int no_listing : 1; /* Do not show in the menu */ |
3303 | 0 | unsigned int available : 1; /* Available in Libycrypt (runtime checked) */ |
3304 | 0 | } curves[] = { |
3305 | 0 | #if GPG_USE_ECDSA || GPG_USE_ECDH |
3306 | 0 | # define MY_USE_ECDSADH 1 |
3307 | | #else |
3308 | | # define MY_USE_ECDSADH 0 |
3309 | | #endif |
3310 | 0 | { "Curve25519", "Ed25519", "Curve 25519", !!GPG_USE_EDDSA, 0,0,0,0 }, |
3311 | 0 | { "X448", "Ed448", "Curve 448", !!GPG_USE_EDDSA, 0,1,0,0 }, |
3312 | 0 | { "NIST P-256", NULL, NULL, MY_USE_ECDSADH, 0,1,0,0 }, |
3313 | 0 | { "NIST P-384", NULL, NULL, MY_USE_ECDSADH, 0,0,0,0 }, |
3314 | 0 | { "NIST P-521", NULL, NULL, MY_USE_ECDSADH, 0,1,0,0 }, |
3315 | 0 | { "brainpoolP256r1", NULL, "Brainpool P-256", MY_USE_ECDSADH, 1,0,0,0 }, |
3316 | 0 | { "brainpoolP384r1", NULL, "Brainpool P-384", MY_USE_ECDSADH, 1,1,0,0 }, |
3317 | 0 | { "brainpoolP512r1", NULL, "Brainpool P-512", MY_USE_ECDSADH, 1,1,0,0 }, |
3318 | 0 | { "secp256k1", NULL, NULL, MY_USE_ECDSADH, 0,1,1,0 }, |
3319 | 0 | }; |
3320 | 0 | #undef MY_USE_ECDSADH |
3321 | 0 | int idx; |
3322 | 0 | char *answer; |
3323 | 0 | const char *result = NULL; |
3324 | 0 | gcry_sexp_t keyparms; |
3325 | |
|
3326 | 0 | tty_printf (_("Please select which elliptic curve you want:\n")); |
3327 | |
|
3328 | 0 | keyparms = NULL; |
3329 | 0 | for (idx=0; idx < DIM(curves); idx++) |
3330 | 0 | { |
3331 | 0 | int rc; |
3332 | |
|
3333 | 0 | curves[idx].available = 0; |
3334 | 0 | if (!curves[idx].supported) |
3335 | 0 | continue; |
3336 | | |
3337 | 0 | if (opt.compliance==CO_DE_VS) |
3338 | 0 | { |
3339 | 0 | if (!curves[idx].de_vs) |
3340 | 0 | continue; /* Not allowed. */ |
3341 | 0 | } |
3342 | 0 | else if (!opt.expert && curves[idx].expert_only) |
3343 | 0 | continue; |
3344 | | |
3345 | | /* We need to switch from the ECDH name of the curve to the |
3346 | | EDDSA name of the curve if we want a signing key. */ |
3347 | 0 | gcry_sexp_release (keyparms); |
3348 | 0 | rc = gcry_sexp_build (&keyparms, NULL, |
3349 | 0 | "(public-key(ecc(curve %s)))", |
3350 | 0 | curves[idx].eddsa_curve? curves[idx].eddsa_curve |
3351 | 0 | /**/ : curves[idx].name); |
3352 | 0 | if (rc) |
3353 | 0 | continue; |
3354 | 0 | if (!gcry_pk_get_curve (keyparms, 0, NULL)) |
3355 | 0 | continue; |
3356 | 0 | if (subkey_algo && curves[idx].eddsa_curve) |
3357 | 0 | { |
3358 | | /* Both Curve 25519 (or 448) keys are to be created. Check that |
3359 | | Libgcrypt also supports the real Curve25519 (or 448). */ |
3360 | 0 | gcry_sexp_release (keyparms); |
3361 | 0 | rc = gcry_sexp_build (&keyparms, NULL, |
3362 | 0 | "(public-key(ecc(curve %s)))", |
3363 | 0 | curves[idx].name); |
3364 | 0 | if (rc) |
3365 | 0 | continue; |
3366 | 0 | if (!gcry_pk_get_curve (keyparms, 0, NULL)) |
3367 | 0 | continue; |
3368 | 0 | } |
3369 | | |
3370 | 0 | curves[idx].available = 1; |
3371 | 0 | if (!curves[idx].no_listing) |
3372 | 0 | tty_printf (" (%d) %s%s\n", idx + 1, |
3373 | 0 | curves[idx].pretty_name? |
3374 | 0 | curves[idx].pretty_name:curves[idx].name, |
3375 | 0 | idx == 0? _(" *default*"):""); |
3376 | 0 | } |
3377 | 0 | gcry_sexp_release (keyparms); |
3378 | | |
3379 | |
|
3380 | 0 | for (;;) |
3381 | 0 | { |
3382 | 0 | answer = cpr_get ("keygen.curve", _("Your selection? ")); |
3383 | 0 | cpr_kill_prompt (); |
3384 | 0 | idx = *answer? atoi (answer) : 1; |
3385 | 0 | if (!*answer && current) |
3386 | 0 | { |
3387 | 0 | xfree(answer); |
3388 | 0 | return NULL; |
3389 | 0 | } |
3390 | 0 | else if (*answer && (!idx || (idx > 0 && idx <= DIM (curves) |
3391 | 0 | && curves[idx-1].no_listing))) |
3392 | 0 | { |
3393 | | /* See whether the user entered the name of the curve. */ |
3394 | 0 | for (idx=0; idx < DIM(curves); idx++) |
3395 | 0 | { |
3396 | 0 | if (!opt.expert && curves[idx].expert_only) |
3397 | 0 | continue; |
3398 | 0 | if (!stricmp (curves[idx].name, answer) |
3399 | 0 | || (curves[idx].pretty_name |
3400 | 0 | && !stricmp (curves[idx].pretty_name, answer))) |
3401 | 0 | break; |
3402 | 0 | } |
3403 | 0 | if (idx == DIM(curves)) |
3404 | 0 | idx = -1; |
3405 | 0 | } |
3406 | 0 | else |
3407 | 0 | idx--; |
3408 | 0 | xfree(answer); |
3409 | 0 | answer = NULL; |
3410 | 0 | if (idx < 0 || idx >= DIM (curves) || !curves[idx].available) |
3411 | 0 | tty_printf (_("Invalid selection.\n")); |
3412 | 0 | else |
3413 | 0 | { |
3414 | | /* If the user selected a signing algorithm and Curve25519 |
3415 | | we need to set the algo to EdDSA and update the curve name. |
3416 | | If switching away from EdDSA, we need to set the algo back |
3417 | | to ECDSA. */ |
3418 | 0 | if (*algo == PUBKEY_ALGO_ECDSA || *algo == PUBKEY_ALGO_EDDSA) |
3419 | 0 | { |
3420 | 0 | if (curves[idx].eddsa_curve) |
3421 | 0 | { |
3422 | 0 | if (subkey_algo && *subkey_algo == PUBKEY_ALGO_ECDSA) |
3423 | 0 | *subkey_algo = PUBKEY_ALGO_EDDSA; |
3424 | 0 | *algo = PUBKEY_ALGO_EDDSA; |
3425 | 0 | result = curves[idx].eddsa_curve; |
3426 | 0 | } |
3427 | 0 | else |
3428 | 0 | { |
3429 | 0 | if (subkey_algo && *subkey_algo == PUBKEY_ALGO_EDDSA) |
3430 | 0 | *subkey_algo = PUBKEY_ALGO_ECDSA; |
3431 | 0 | *algo = PUBKEY_ALGO_ECDSA; |
3432 | 0 | result = curves[idx].name; |
3433 | 0 | } |
3434 | 0 | } |
3435 | 0 | else |
3436 | 0 | result = curves[idx].name; |
3437 | 0 | break; |
3438 | 0 | } |
3439 | 0 | } |
3440 | | |
3441 | 0 | if (!result) |
3442 | 0 | result = curves[0].name; |
3443 | |
|
3444 | 0 | return result; |
3445 | 0 | } |
3446 | | |
3447 | | |
3448 | | /* Ask for the Kyber variant. Returns a const algo string like |
3449 | | * kyber768_bp256 or NULL on error. */ |
3450 | | const char * |
3451 | | ask_kyber_variant (void) |
3452 | 0 | { |
3453 | 0 | struct { |
3454 | 0 | const char *desc; /* e.g. "Kyber 768 (bp256)" */ |
3455 | 0 | const char *variant; /* e.g. "kyber768_bp256" */ |
3456 | 0 | unsigned int de_vs : 1; /* Allowed in CO_DE_VS. */ |
3457 | 0 | } table[] = { |
3458 | 0 | { "Kyber 768 (bp256)", "kyber768_bp256", 1 }, |
3459 | 0 | { "Kyber 1024 (bp384)", "kyber1024_bp384", 1 }, |
3460 | 0 | { "Kyber 768 (X25519)", "kyber768_cv25519", 0 }, |
3461 | 0 | { "Kyber 1024 (X448)", "kyber1024_cv448", 0 }, |
3462 | 0 | }; |
3463 | 0 | int idx; |
3464 | 0 | char *answer; |
3465 | 0 | const char *result = NULL; |
3466 | |
|
3467 | 0 | tty_printf (_("Please select the %s variant you want:\n"), "Kyber"); |
3468 | |
|
3469 | 0 | for (idx=0; idx < DIM(table); idx++) |
3470 | 0 | { |
3471 | 0 | if (opt.compliance==CO_DE_VS) |
3472 | 0 | { |
3473 | 0 | if (!table[idx].de_vs) |
3474 | 0 | continue; /* Not allowed. */ |
3475 | 0 | } |
3476 | | |
3477 | 0 | tty_printf (" (%d) %s%s\n", idx + 1, |
3478 | 0 | table[idx].desc, |
3479 | 0 | idx == 0? _(" *default*"):""); |
3480 | 0 | } |
3481 | |
|
3482 | 0 | for (;;) |
3483 | 0 | { |
3484 | 0 | answer = cpr_get ("keygen.kyber_variant", _("Your selection? ")); |
3485 | 0 | cpr_kill_prompt (); |
3486 | 0 | idx = *answer? atoi (answer) : 1 /* default */; |
3487 | 0 | if (*answer && !idx) |
3488 | 0 | { |
3489 | | /* See whether the user entered the name of the algo. */ |
3490 | 0 | for (idx=0; idx < DIM(table); idx++) |
3491 | 0 | { |
3492 | 0 | if (!stricmp (table[idx].variant, answer)) |
3493 | 0 | break; |
3494 | 0 | } |
3495 | 0 | if (idx == DIM(table)) |
3496 | 0 | idx = -1; |
3497 | 0 | } |
3498 | 0 | else |
3499 | 0 | idx--; /* Map back to 0 based index. */ |
3500 | 0 | xfree(answer); |
3501 | 0 | answer = NULL; |
3502 | 0 | if (idx < 0 || idx >= DIM (table) |
3503 | 0 | || (opt.compliance==CO_DE_VS && !table[idx].de_vs)) |
3504 | 0 | tty_printf (_("Invalid selection.\n")); |
3505 | 0 | else |
3506 | 0 | { |
3507 | 0 | result = table[idx].variant; |
3508 | 0 | break; |
3509 | 0 | } |
3510 | 0 | } |
3511 | |
|
3512 | 0 | if (!result) |
3513 | 0 | result = table[0].variant; |
3514 | |
|
3515 | 0 | return result; |
3516 | 0 | } |
3517 | | |
3518 | | |
3519 | | /**************** |
3520 | | * Parse an expire string and return its value in seconds. |
3521 | | * Returns (u32)-1 on error. |
3522 | | * This isn't perfect since scan_isodatestr returns unix time, and |
3523 | | * OpenPGP actually allows a 32-bit time *plus* a 32-bit offset. |
3524 | | * Because of this, we only permit setting expirations up to 2106, but |
3525 | | * OpenPGP could theoretically allow up to 2242. I think we'll all |
3526 | | * just cope for the next few years until we get a 64-bit time_t or |
3527 | | * similar. |
3528 | | */ |
3529 | | static u32 |
3530 | | parse_expire_string_with_ct (const char *string, u32 creation_time) |
3531 | 0 | { |
3532 | 0 | int mult; |
3533 | 0 | u32 seconds; |
3534 | 0 | u32 abs_date = 0; |
3535 | 0 | time_t tt; |
3536 | 0 | uint64_t tmp64; |
3537 | 0 | u32 curtime; |
3538 | |
|
3539 | 0 | if (creation_time == (u32)-1) |
3540 | 0 | curtime = make_timestamp (); |
3541 | 0 | else |
3542 | 0 | curtime = creation_time; |
3543 | |
|
3544 | 0 | if (!string || !*string || !strcmp (string, "none") |
3545 | 0 | || !strcmp (string, "never") || !strcmp (string, "-")) |
3546 | 0 | seconds = 0; |
3547 | 0 | else if (!strncmp (string, "seconds=", 8)) |
3548 | 0 | seconds = scan_secondsstr (string+8); |
3549 | 0 | else if ((abs_date = scan_isodatestr(string)) |
3550 | 0 | && (abs_date+86400/2) > curtime) |
3551 | 0 | seconds = (abs_date+86400/2) - curtime; |
3552 | 0 | else if ((tt = isotime2epoch_u64 (string)) != (uint64_t)(-1)) |
3553 | 0 | { |
3554 | 0 | tmp64 = tt - curtime; |
3555 | 0 | if (tmp64 >= (u32)(-1)) |
3556 | 0 | seconds = (u32)(-1) - 1; /* cap value. */ |
3557 | 0 | else |
3558 | 0 | seconds = (u32)tmp64; |
3559 | 0 | } |
3560 | 0 | else if ((mult = check_valid_days (string))) |
3561 | 0 | { |
3562 | 0 | tmp64 = scan_secondsstr (string) * 86400L * mult; |
3563 | 0 | if (tmp64 >= (u32)(-1)) |
3564 | 0 | seconds = (u32)(-1) - 1; /* cap value. */ |
3565 | 0 | else |
3566 | 0 | seconds = (u32)tmp64; |
3567 | 0 | } |
3568 | 0 | else |
3569 | 0 | seconds = (u32)(-1); |
3570 | |
|
3571 | 0 | return seconds; |
3572 | 0 | } |
3573 | | |
3574 | | u32 |
3575 | | parse_expire_string ( const char *string ) |
3576 | 0 | { |
3577 | 0 | return parse_expire_string_with_ct (string, (u32)-1); |
3578 | 0 | } |
3579 | | |
3580 | | |
3581 | | /* Parse a Creation-Date string which is either "1986-04-26" or |
3582 | | "19860426T042640". Returns 0 on error. */ |
3583 | | static u32 |
3584 | | parse_creation_string (const char *string) |
3585 | 0 | { |
3586 | 0 | u32 seconds; |
3587 | |
|
3588 | 0 | if (!*string) |
3589 | 0 | seconds = 0; |
3590 | 0 | else if ( !strncmp (string, "seconds=", 8) ) |
3591 | 0 | seconds = scan_secondsstr (string+8); |
3592 | 0 | else if ( !(seconds = scan_isodatestr (string))) |
3593 | 0 | { |
3594 | 0 | uint64_t tmp = isotime2epoch_u64 (string); |
3595 | 0 | if (tmp == (uint64_t)(-1)) |
3596 | 0 | seconds = 0; |
3597 | 0 | else if (tmp > (u32)(-1)) |
3598 | 0 | seconds = 0; |
3599 | 0 | else |
3600 | 0 | seconds = tmp; |
3601 | 0 | } |
3602 | 0 | return seconds; |
3603 | 0 | } |
3604 | | |
3605 | | |
3606 | | /* object == 0 for a key, and 1 for a sig */ |
3607 | | u32 |
3608 | | ask_expire_interval(int object,const char *def_expire) |
3609 | 0 | { |
3610 | 0 | u32 interval; |
3611 | 0 | char *answer; |
3612 | |
|
3613 | 0 | switch(object) |
3614 | 0 | { |
3615 | 0 | case 0: |
3616 | 0 | if(def_expire) |
3617 | 0 | BUG(); |
3618 | 0 | tty_printf(_("Please specify how long the key should be valid.\n" |
3619 | 0 | " 0 = key does not expire\n" |
3620 | 0 | " <n> = key expires in n days\n" |
3621 | 0 | " <n>w = key expires in n weeks\n" |
3622 | 0 | " <n>m = key expires in n months\n" |
3623 | 0 | " <n>y = key expires in n years\n")); |
3624 | 0 | break; |
3625 | | |
3626 | 0 | case 1: |
3627 | 0 | if(!def_expire) |
3628 | 0 | BUG(); |
3629 | 0 | tty_printf(_("Please specify how long the signature should be valid.\n" |
3630 | 0 | " 0 = signature does not expire\n" |
3631 | 0 | " <n> = signature expires in n days\n" |
3632 | 0 | " <n>w = signature expires in n weeks\n" |
3633 | 0 | " <n>m = signature expires in n months\n" |
3634 | 0 | " <n>y = signature expires in n years\n")); |
3635 | 0 | break; |
3636 | | |
3637 | 0 | default: |
3638 | 0 | BUG(); |
3639 | 0 | } |
3640 | | |
3641 | | /* Note: The elgamal subkey for DSA has no expiration date because |
3642 | | * it must be signed with the DSA key and this one has the expiration |
3643 | | * date */ |
3644 | | |
3645 | 0 | answer = NULL; |
3646 | 0 | for(;;) |
3647 | 0 | { |
3648 | 0 | u32 curtime; |
3649 | |
|
3650 | 0 | xfree(answer); |
3651 | 0 | if(object==0) |
3652 | 0 | answer = cpr_get("keygen.valid",_("Key is valid for? (0) ")); |
3653 | 0 | else |
3654 | 0 | { |
3655 | 0 | char *prompt; |
3656 | |
|
3657 | 0 | prompt = xasprintf (_("Signature is valid for? (%s) "), def_expire); |
3658 | 0 | answer = cpr_get("siggen.valid",prompt); |
3659 | 0 | xfree(prompt); |
3660 | |
|
3661 | 0 | if(*answer=='\0') |
3662 | 0 | { |
3663 | 0 | xfree (answer); |
3664 | 0 | answer = xstrdup (def_expire); |
3665 | 0 | } |
3666 | 0 | } |
3667 | 0 | cpr_kill_prompt(); |
3668 | 0 | trim_spaces(answer); |
3669 | 0 | curtime = make_timestamp (); |
3670 | 0 | interval = parse_expire_string( answer ); |
3671 | 0 | if( interval == (u32)-1 ) |
3672 | 0 | { |
3673 | 0 | tty_printf(_("invalid value\n")); |
3674 | 0 | continue; |
3675 | 0 | } |
3676 | | |
3677 | 0 | if( !interval ) |
3678 | 0 | { |
3679 | 0 | tty_printf((object==0) |
3680 | 0 | ? _("Key does not expire at all\n") |
3681 | 0 | : _("Signature does not expire at all\n")); |
3682 | 0 | } |
3683 | 0 | else |
3684 | 0 | { |
3685 | 0 | tty_printf(object==0 |
3686 | 0 | ? _("Key expires at %s\n") |
3687 | 0 | : _("Signature expires at %s\n"), |
3688 | 0 | asctimestamp((ulong)(curtime + interval) ) ); |
3689 | | #if SIZEOF_TIME_T <= 4 && !defined (HAVE_UNSIGNED_TIME_T) |
3690 | | if ( (time_t)((ulong)(curtime+interval)) < 0 ) |
3691 | | tty_printf (_("Your system can't display dates beyond 2038.\n" |
3692 | | "However, it will be correctly handled up to" |
3693 | | " 2106.\n")); |
3694 | | else |
3695 | | #endif /*SIZEOF_TIME_T*/ |
3696 | 0 | if ( (time_t)((unsigned long)(curtime+interval)) < curtime ) |
3697 | 0 | { |
3698 | 0 | tty_printf (_("invalid value\n")); |
3699 | 0 | continue; |
3700 | 0 | } |
3701 | 0 | } |
3702 | | |
3703 | 0 | if( cpr_enabled() || cpr_get_answer_is_yes("keygen.valid.okay", |
3704 | 0 | _("Is this correct? (y/N) ")) ) |
3705 | 0 | break; |
3706 | 0 | } |
3707 | |
|
3708 | 0 | xfree(answer); |
3709 | 0 | return interval; |
3710 | 0 | } |
3711 | | |
3712 | | u32 |
3713 | | ask_expiredate (void) |
3714 | 0 | { |
3715 | 0 | u32 x = ask_expire_interval(0,NULL); |
3716 | 0 | return x? make_timestamp() + x : 0; |
3717 | 0 | } |
3718 | | |
3719 | | |
3720 | | |
3721 | | static PKT_user_id * |
3722 | | uid_from_string (const char *string) |
3723 | 0 | { |
3724 | 0 | size_t n; |
3725 | 0 | PKT_user_id *uid; |
3726 | |
|
3727 | 0 | n = strlen (string); |
3728 | 0 | uid = xmalloc_clear (sizeof *uid + n); |
3729 | 0 | uid->len = n; |
3730 | 0 | strcpy (uid->name, string); |
3731 | 0 | uid->ref = 1; |
3732 | 0 | return uid; |
3733 | 0 | } |
3734 | | |
3735 | | |
3736 | | /* Return true if the user id UID already exists in the keyblock. */ |
3737 | | static int |
3738 | | uid_already_in_keyblock (kbnode_t keyblock, const char *uid) |
3739 | 0 | { |
3740 | 0 | PKT_user_id *uidpkt = uid_from_string (uid); |
3741 | 0 | kbnode_t node; |
3742 | 0 | int result = 0; |
3743 | |
|
3744 | 0 | for (node=keyblock; node && !result; node=node->next) |
3745 | 0 | if (!is_deleted_kbnode (node) |
3746 | 0 | && node->pkt->pkttype == PKT_USER_ID |
3747 | 0 | && !cmp_user_ids (uidpkt, node->pkt->pkt.user_id)) |
3748 | 0 | result = 1; |
3749 | 0 | free_user_id (uidpkt); |
3750 | 0 | return result; |
3751 | 0 | } |
3752 | | |
3753 | | |
3754 | | /* Ask for a user ID. With a MODE of 1 an extra help prompt is |
3755 | | printed for use during a new key creation. If KEYBLOCK is not NULL |
3756 | | the function prevents the creation of an already existing user |
3757 | | ID. IF FULL is not set some prompts are not shown. */ |
3758 | | static char * |
3759 | | ask_user_id (int mode, int full, KBNODE keyblock) |
3760 | 0 | { |
3761 | 0 | char *answer; |
3762 | 0 | char *aname, *acomment, *amail, *uid; |
3763 | |
|
3764 | 0 | if ( !mode ) |
3765 | 0 | { |
3766 | | /* TRANSLATORS: This is the new string telling the user what |
3767 | | gpg is now going to do (i.e. ask for the parts of the user |
3768 | | ID). Note that if you do not translate this string, a |
3769 | | different string will be used, which might still have |
3770 | | a correct translation. */ |
3771 | 0 | const char *s1 = |
3772 | 0 | N_("\n" |
3773 | 0 | "GnuPG needs to construct a user ID to identify your key.\n" |
3774 | 0 | "\n"); |
3775 | 0 | const char *s2 = _(s1); |
3776 | |
|
3777 | 0 | if (!strcmp (s1, s2)) |
3778 | 0 | { |
3779 | | /* There is no translation for the string thus we to use |
3780 | | the old info text. gettext has no way to tell whether |
3781 | | a translation is actually available, thus we need to |
3782 | | to compare again. */ |
3783 | | /* TRANSLATORS: This string is in general not anymore used |
3784 | | but you should keep your existing translation. In case |
3785 | | the new string is not translated this old string will |
3786 | | be used. */ |
3787 | 0 | const char *s3 = N_("\n" |
3788 | 0 | "You need a user ID to identify your key; " |
3789 | 0 | "the software constructs the user ID\n" |
3790 | 0 | "from the Real Name, Comment and Email Address in this form:\n" |
3791 | 0 | " \"Heinrich Heine (Der Dichter) <heinrichh@duesseldorf.de>\"\n\n"); |
3792 | 0 | const char *s4 = _(s3); |
3793 | 0 | if (strcmp (s3, s4)) |
3794 | 0 | s2 = s3; /* A translation exists - use it. */ |
3795 | 0 | } |
3796 | 0 | tty_printf ("%s", s2) ; |
3797 | 0 | } |
3798 | 0 | uid = aname = acomment = amail = NULL; |
3799 | 0 | for(;;) { |
3800 | 0 | char *p; |
3801 | 0 | int fail=0; |
3802 | |
|
3803 | 0 | if( !aname ) { |
3804 | 0 | for(;;) { |
3805 | 0 | xfree(aname); |
3806 | 0 | aname = cpr_get("keygen.name",_("Real name: ")); |
3807 | 0 | trim_spaces(aname); |
3808 | 0 | cpr_kill_prompt(); |
3809 | |
|
3810 | 0 | if( opt.allow_freeform_uid ) |
3811 | 0 | break; |
3812 | | |
3813 | 0 | if( strpbrk( aname, "<>" ) ) |
3814 | 0 | { |
3815 | 0 | tty_printf(_("Invalid character in name\n")); |
3816 | 0 | tty_printf(_("The characters '%s' and '%s' may not " |
3817 | 0 | "appear in name\n"), "<", ">"); |
3818 | 0 | } |
3819 | 0 | else |
3820 | 0 | break; |
3821 | 0 | } |
3822 | 0 | } |
3823 | 0 | if( !amail ) { |
3824 | 0 | for(;;) { |
3825 | 0 | xfree(amail); |
3826 | 0 | amail = cpr_get("keygen.email",_("Email address: ")); |
3827 | 0 | trim_spaces(amail); |
3828 | 0 | cpr_kill_prompt(); |
3829 | 0 | if( !*amail || opt.allow_freeform_uid ) |
3830 | 0 | break; /* no email address is okay */ |
3831 | 0 | else if ( !is_valid_mailbox (amail) ) |
3832 | 0 | tty_printf(_("Not a valid email address\n")); |
3833 | 0 | else |
3834 | 0 | break; |
3835 | 0 | } |
3836 | 0 | } |
3837 | 0 | if (!acomment) { |
3838 | 0 | if (full) { |
3839 | 0 | for(;;) { |
3840 | 0 | xfree(acomment); |
3841 | 0 | acomment = cpr_get("keygen.comment",_("Comment: ")); |
3842 | 0 | trim_spaces(acomment); |
3843 | 0 | cpr_kill_prompt(); |
3844 | 0 | if( !*acomment ) |
3845 | 0 | break; /* no comment is okay */ |
3846 | 0 | else if( strpbrk( acomment, "()" ) ) |
3847 | 0 | tty_printf(_("Invalid character in comment\n")); |
3848 | 0 | else |
3849 | 0 | break; |
3850 | 0 | } |
3851 | 0 | } |
3852 | 0 | else { |
3853 | 0 | xfree (acomment); |
3854 | 0 | acomment = xstrdup (""); |
3855 | 0 | } |
3856 | 0 | } |
3857 | | |
3858 | |
|
3859 | 0 | xfree(uid); |
3860 | 0 | uid = p = xmalloc(strlen(aname)+strlen(amail)+strlen(acomment)+12+10); |
3861 | 0 | if (!*aname && *amail && !*acomment && !random_is_faked ()) |
3862 | 0 | { /* Empty name and comment but with mail address. Use |
3863 | | simplified form with only the non-angle-bracketed mail |
3864 | | address. */ |
3865 | 0 | p = stpcpy (p, amail); |
3866 | 0 | } |
3867 | 0 | else |
3868 | 0 | { |
3869 | 0 | p = stpcpy (p, aname ); |
3870 | 0 | if (*acomment) |
3871 | 0 | p = stpcpy(stpcpy(stpcpy(p," ("), acomment),")"); |
3872 | 0 | if (*amail) |
3873 | 0 | p = stpcpy(stpcpy(stpcpy(p," <"), amail),">"); |
3874 | 0 | } |
3875 | | |
3876 | | /* Append a warning if the RNG is switched into fake mode. */ |
3877 | 0 | if ( random_is_faked () ) |
3878 | 0 | strcpy(p, " (insecure!)" ); |
3879 | | |
3880 | | /* print a note in case that UTF8 mapping has to be done */ |
3881 | 0 | for(p=uid; *p; p++ ) { |
3882 | 0 | if( *p & 0x80 ) { |
3883 | 0 | tty_printf(_("You are using the '%s' character set.\n"), |
3884 | 0 | get_native_charset() ); |
3885 | 0 | break; |
3886 | 0 | } |
3887 | 0 | } |
3888 | |
|
3889 | 0 | tty_printf(_("You selected this USER-ID:\n \"%s\"\n\n"), uid); |
3890 | |
|
3891 | 0 | if( !*amail && !opt.allow_freeform_uid |
3892 | 0 | && (strchr( aname, '@' ) || strchr( acomment, '@'))) { |
3893 | 0 | fail = 1; |
3894 | 0 | tty_printf(_("Please don't put the email address " |
3895 | 0 | "into the real name or the comment\n") ); |
3896 | 0 | } |
3897 | |
|
3898 | 0 | if (!fail && keyblock) |
3899 | 0 | { |
3900 | 0 | if (uid_already_in_keyblock (keyblock, uid)) |
3901 | 0 | { |
3902 | 0 | tty_printf (_("Such a user ID already exists on this key!\n")); |
3903 | 0 | fail = 1; |
3904 | 0 | } |
3905 | 0 | } |
3906 | |
|
3907 | 0 | for(;;) { |
3908 | | /* TRANSLATORS: These are the allowed answers in |
3909 | | lower and uppercase. Below you will find the matching |
3910 | | string which should be translated accordingly and the |
3911 | | letter changed to match the one in the answer string. |
3912 | | |
3913 | | n = Change name |
3914 | | c = Change comment |
3915 | | e = Change email |
3916 | | o = Okay (ready, continue) |
3917 | | q = Quit |
3918 | | */ |
3919 | 0 | const char *ansstr = _("NnCcEeOoQq"); |
3920 | |
|
3921 | 0 | if( strlen(ansstr) != 10 ) |
3922 | 0 | BUG(); |
3923 | 0 | if( cpr_enabled() ) { |
3924 | 0 | answer = xstrdup (ansstr + (fail?8:6)); |
3925 | 0 | answer[1] = 0; |
3926 | 0 | } |
3927 | 0 | else if (full) { |
3928 | 0 | answer = cpr_get("keygen.userid.cmd", fail? |
3929 | 0 | _("Change (N)ame, (C)omment, (E)mail or (Q)uit? ") : |
3930 | 0 | _("Change (N)ame, (C)omment, (E)mail or (O)kay/(Q)uit? ")); |
3931 | 0 | cpr_kill_prompt(); |
3932 | 0 | } |
3933 | 0 | else { |
3934 | 0 | answer = cpr_get("keygen.userid.cmd", fail? |
3935 | 0 | _("Change (N)ame, (E)mail, or (Q)uit? ") : |
3936 | 0 | _("Change (N)ame, (E)mail, or (O)kay/(Q)uit? ")); |
3937 | 0 | cpr_kill_prompt(); |
3938 | 0 | } |
3939 | 0 | if( strlen(answer) > 1 ) |
3940 | 0 | ; |
3941 | 0 | else if( *answer == ansstr[0] || *answer == ansstr[1] ) { |
3942 | 0 | xfree(aname); aname = NULL; |
3943 | 0 | break; |
3944 | 0 | } |
3945 | 0 | else if( *answer == ansstr[2] || *answer == ansstr[3] ) { |
3946 | 0 | xfree(acomment); acomment = NULL; |
3947 | 0 | break; |
3948 | 0 | } |
3949 | 0 | else if( *answer == ansstr[4] || *answer == ansstr[5] ) { |
3950 | 0 | xfree(amail); amail = NULL; |
3951 | 0 | break; |
3952 | 0 | } |
3953 | 0 | else if( *answer == ansstr[6] || *answer == ansstr[7] ) { |
3954 | 0 | if( fail ) { |
3955 | 0 | tty_printf(_("Please correct the error first\n")); |
3956 | 0 | } |
3957 | 0 | else { |
3958 | 0 | xfree(aname); aname = NULL; |
3959 | 0 | xfree(acomment); acomment = NULL; |
3960 | 0 | xfree(amail); amail = NULL; |
3961 | 0 | break; |
3962 | 0 | } |
3963 | 0 | } |
3964 | 0 | else if( *answer == ansstr[8] || *answer == ansstr[9] ) { |
3965 | 0 | xfree(aname); aname = NULL; |
3966 | 0 | xfree(acomment); acomment = NULL; |
3967 | 0 | xfree(amail); amail = NULL; |
3968 | 0 | xfree(uid); uid = NULL; |
3969 | 0 | break; |
3970 | 0 | } |
3971 | 0 | xfree(answer); |
3972 | 0 | } |
3973 | 0 | xfree(answer); |
3974 | 0 | if (!amail && !acomment) |
3975 | 0 | break; |
3976 | 0 | xfree(uid); uid = NULL; |
3977 | 0 | } |
3978 | 0 | if( uid ) { |
3979 | 0 | char *p = native_to_utf8( uid ); |
3980 | 0 | xfree( uid ); |
3981 | 0 | uid = p; |
3982 | 0 | } |
3983 | 0 | return uid; |
3984 | 0 | } |
3985 | | |
3986 | | |
3987 | | /* Basic key generation. Here we divert to the actual generation |
3988 | | * routines based on the requested algorithm. KEYGEN_FLAGS might be |
3989 | | * updated by this function. */ |
3990 | | static int |
3991 | | do_create (int algo, unsigned int nbits, const char *curve, kbnode_t pub_root, |
3992 | | u32 timestamp, u32 expiredate, int is_subkey, |
3993 | | int *keygen_flags, const char *passphrase, |
3994 | | char **cache_nonce_addr, char **passwd_nonce_addr, |
3995 | | gpg_error_t (*common_gen_cb)(common_gen_cb_parm_t), |
3996 | | common_gen_cb_parm_t common_gen_cb_parm) |
3997 | 0 | { |
3998 | 0 | gpg_error_t err; |
3999 | | |
4000 | | /* Fixme: The entropy collecting message should be moved to a |
4001 | | libgcrypt progress handler. */ |
4002 | 0 | if (!opt.batch) |
4003 | 0 | tty_printf (_( |
4004 | 0 | "We need to generate a lot of random bytes. It is a good idea to perform\n" |
4005 | 0 | "some other action (type on the keyboard, move the mouse, utilize the\n" |
4006 | 0 | "disks) during the prime generation; this gives the random number\n" |
4007 | 0 | "generator a better chance to gain enough entropy.\n") ); |
4008 | |
|
4009 | 0 | if (algo == PUBKEY_ALGO_ELGAMAL_E) |
4010 | 0 | err = gen_elg (algo, nbits, pub_root, timestamp, expiredate, is_subkey, |
4011 | 0 | *keygen_flags, passphrase, |
4012 | 0 | cache_nonce_addr, passwd_nonce_addr, |
4013 | 0 | common_gen_cb, common_gen_cb_parm); |
4014 | 0 | else if (algo == PUBKEY_ALGO_DSA) |
4015 | 0 | err = gen_dsa (nbits, pub_root, timestamp, expiredate, is_subkey, |
4016 | 0 | *keygen_flags, passphrase, |
4017 | 0 | cache_nonce_addr, passwd_nonce_addr, |
4018 | 0 | common_gen_cb, common_gen_cb_parm); |
4019 | 0 | else if (algo == PUBKEY_ALGO_ECDSA |
4020 | 0 | || algo == PUBKEY_ALGO_EDDSA |
4021 | 0 | || algo == PUBKEY_ALGO_ECDH |
4022 | 0 | || algo == PUBKEY_ALGO_X25519 |
4023 | 0 | || algo == PUBKEY_ALGO_ED25519) |
4024 | 0 | err = gen_ecc (algo, curve, pub_root, timestamp, expiredate, is_subkey, |
4025 | 0 | keygen_flags, passphrase, |
4026 | 0 | cache_nonce_addr, passwd_nonce_addr, |
4027 | 0 | common_gen_cb, common_gen_cb_parm); |
4028 | 0 | else if (algo == PUBKEY_ALGO_KYBER || IS_PUBKEY_ALGO_MLK (algo)) |
4029 | 0 | err = gen_kyber (algo, nbits, curve, |
4030 | 0 | pub_root, timestamp, expiredate, is_subkey, |
4031 | 0 | keygen_flags, passphrase, |
4032 | 0 | cache_nonce_addr, passwd_nonce_addr, |
4033 | 0 | common_gen_cb, common_gen_cb_parm); |
4034 | 0 | else if (algo == PUBKEY_ALGO_RSA) |
4035 | 0 | err = gen_rsa (algo, nbits, pub_root, timestamp, expiredate, is_subkey, |
4036 | 0 | *keygen_flags, passphrase, |
4037 | 0 | cache_nonce_addr, passwd_nonce_addr, |
4038 | 0 | common_gen_cb, common_gen_cb_parm); |
4039 | 0 | else |
4040 | 0 | BUG(); |
4041 | | |
4042 | 0 | return err; |
4043 | 0 | } |
4044 | | |
4045 | | |
4046 | | /* Generate a new user id packet or return NULL if canceled. If |
4047 | | KEYBLOCK is not NULL the function prevents the creation of an |
4048 | | already existing user ID. If UIDSTR is not NULL the user is not |
4049 | | asked but UIDSTR is used to create the user id packet; if the user |
4050 | | id already exists NULL is returned. UIDSTR is expected to be utf-8 |
4051 | | encoded and should have already been checked for a valid length |
4052 | | etc. */ |
4053 | | PKT_user_id * |
4054 | | generate_user_id (KBNODE keyblock, const char *uidstr) |
4055 | 0 | { |
4056 | 0 | PKT_user_id *uid; |
4057 | 0 | char *p; |
4058 | |
|
4059 | 0 | if (uidstr) |
4060 | 0 | { |
4061 | 0 | if (uid_already_in_keyblock (keyblock, uidstr)) |
4062 | 0 | return NULL; /* Already exists. */ |
4063 | 0 | uid = uid_from_string (uidstr); |
4064 | 0 | } |
4065 | 0 | else |
4066 | 0 | { |
4067 | 0 | p = ask_user_id (1, 1, keyblock); |
4068 | 0 | if (!p) |
4069 | 0 | return NULL; /* Canceled. */ |
4070 | 0 | uid = uid_from_string (p); |
4071 | 0 | xfree (p); |
4072 | 0 | } |
4073 | 0 | return uid; |
4074 | 0 | } |
4075 | | |
4076 | | |
4077 | | /* Helper for parse_key_parameter_part_parameter_string for one part of the |
4078 | | * specification string; i.e. ALGO/FLAGS. If STRING is NULL or empty |
4079 | | * success is returned. On error an error code is returned. Note |
4080 | | * that STRING may be modified by this function. NULL may be passed |
4081 | | * for any parameter. FOR_SUBKEY shall be true if this is used as a |
4082 | | * subkey. If CLEAR_CERT is set a default CERT usage will be cleared; |
4083 | | * this is useful if for example the default algorithm is used for a |
4084 | | * subkey. If R_KEYVERSION is not NULL it will receive the version of |
4085 | | * the key; this is currently 4 but can be changed with the flag "v5" |
4086 | | * to create a v5 key. If R_KEYTIME is not NULL and the key has been |
4087 | | * taken from active OpenPGP card, its creation time is stored |
4088 | | * there. */ |
4089 | | static gpg_error_t |
4090 | | parse_key_parameter_part (ctrl_t ctrl, |
4091 | | char *string, int for_subkey, int clear_cert, |
4092 | | int *r_algo, unsigned int *r_size, |
4093 | | unsigned int *r_keyuse, |
4094 | | char const **r_curve, int *r_keyversion, |
4095 | | char **r_keygrip, u32 *r_keytime) |
4096 | 0 | { |
4097 | 0 | gpg_error_t err; |
4098 | 0 | char *flags; |
4099 | 0 | int algo; |
4100 | 0 | char *endp; |
4101 | 0 | const char *curve = NULL; |
4102 | 0 | int ecdh_or_ecdsa = 0; |
4103 | 0 | unsigned int size; |
4104 | 0 | int keyuse; |
4105 | 0 | int keyversion = 0; /* Not specified. */ |
4106 | 0 | int i; |
4107 | 0 | const char *s; |
4108 | 0 | int from_card = 0; |
4109 | 0 | char *keygrip = NULL; |
4110 | 0 | u32 keytime = 0; |
4111 | 0 | int is_448 = 0; |
4112 | 0 | int is_pqc = 0; |
4113 | |
|
4114 | 0 | if (!string || !*string) |
4115 | 0 | return 0; /* Success. */ |
4116 | | |
4117 | 0 | flags = strchr (string, '/'); |
4118 | 0 | if (flags) |
4119 | 0 | *flags++ = 0; |
4120 | |
|
4121 | 0 | algo = 0; |
4122 | 0 | if (!ascii_strcasecmp (string, "card")) |
4123 | 0 | from_card = 1; |
4124 | 0 | else if (strlen (string) >= 3 && (digitp (string+3) || !string[3])) |
4125 | 0 | { |
4126 | 0 | if (!ascii_memcasecmp (string, "rsa", 3)) |
4127 | 0 | algo = PUBKEY_ALGO_RSA; |
4128 | 0 | else if (!ascii_memcasecmp (string, "dsa", 3)) |
4129 | 0 | algo = PUBKEY_ALGO_DSA; |
4130 | 0 | else if (!ascii_memcasecmp (string, "elg", 3)) |
4131 | 0 | algo = PUBKEY_ALGO_ELGAMAL_E; |
4132 | 0 | } |
4133 | |
|
4134 | 0 | if (from_card) |
4135 | 0 | ; /* We need the flags before we can figure out the key to use. */ |
4136 | 0 | else if (algo) |
4137 | 0 | { |
4138 | | /* This is one of the algos parsed above (rsa, dsa, or elg). */ |
4139 | 0 | if (!string[3]) |
4140 | 0 | size = get_keysize_range (algo, NULL, NULL); |
4141 | 0 | else |
4142 | 0 | { |
4143 | 0 | size = strtoul (string+3, &endp, 10); |
4144 | 0 | if (size < 512 || size > 16384 || *endp) |
4145 | 0 | return gpg_error (GPG_ERR_INV_VALUE); |
4146 | 0 | } |
4147 | 0 | } |
4148 | 0 | else if (!ascii_strcasecmp (string, "kyber") |
4149 | 0 | || !ascii_strcasecmp (string, "kyber768")) |
4150 | 0 | { |
4151 | | /* Get the curve and check that it can technically be used |
4152 | | * (i.e. everything except the EdXXXX curves. */ |
4153 | 0 | curve = openpgp_is_curve_supported ("brainpoolP256r1", &algo, NULL); |
4154 | 0 | if (!curve || algo == PUBKEY_ALGO_EDDSA) |
4155 | 0 | return gpg_error (GPG_ERR_UNKNOWN_CURVE); |
4156 | 0 | algo = PUBKEY_ALGO_KYBER; |
4157 | 0 | size = 768; |
4158 | 0 | is_pqc = 1; |
4159 | 0 | } |
4160 | 0 | else if (!ascii_strcasecmp (string, "kyber1024")) |
4161 | 0 | { |
4162 | | /* Get the curve and check that it can technically be used |
4163 | | * (i.e. everything except the EdXXXX curves. */ |
4164 | 0 | curve = openpgp_is_curve_supported ("brainpoolP384r1", &algo, NULL); |
4165 | 0 | if (!curve || algo == PUBKEY_ALGO_EDDSA) |
4166 | 0 | return gpg_error (GPG_ERR_UNKNOWN_CURVE); |
4167 | 0 | algo = PUBKEY_ALGO_KYBER; |
4168 | 0 | size = 1024; |
4169 | 0 | is_pqc = 1; |
4170 | 0 | } |
4171 | 0 | else if (!ascii_strncasecmp (string, "ky768_", 6) |
4172 | 0 | || !ascii_strncasecmp (string, "ky1024_", 7) |
4173 | 0 | || !ascii_strncasecmp (string, "kyber768_", 9) |
4174 | 0 | || !ascii_strncasecmp (string, "kyber1024_", 10) |
4175 | 0 | ) |
4176 | 0 | { |
4177 | | /* Get the curve and check that it can technically be used |
4178 | | * (i.e. everything except the EdXXXX curves. */ |
4179 | 0 | s = strchr (string, '_'); |
4180 | 0 | log_assert (s); |
4181 | 0 | s++; |
4182 | 0 | curve = openpgp_is_curve_supported (s, &algo, NULL); |
4183 | 0 | if (!curve || algo == PUBKEY_ALGO_EDDSA) |
4184 | 0 | return gpg_error (GPG_ERR_UNKNOWN_CURVE); |
4185 | 0 | algo = PUBKEY_ALGO_KYBER; |
4186 | 0 | size = strstr (string, "768_")? 768 : 1024; |
4187 | 0 | is_pqc = 1; |
4188 | 0 | } |
4189 | 0 | else if (!ascii_strcasecmp (string, "mlk768") && RFC9980) |
4190 | 0 | { |
4191 | 0 | curve = "ietf25"; |
4192 | 0 | algo = PUBKEY_ALGO_MLK768_25519; |
4193 | 0 | size = 768; |
4194 | 0 | is_pqc = 1; |
4195 | 0 | keyversion = 6; |
4196 | 0 | } |
4197 | 0 | else if (!ascii_strcasecmp (string, "mlk768_np384") && RFC9980) |
4198 | 0 | { |
4199 | 0 | curve = "nistp384"; |
4200 | 0 | algo = PUBKEY_ALGO_MLK768_NP384; |
4201 | 0 | size = 768; |
4202 | 0 | is_pqc = 1; |
4203 | 0 | keyversion = 6; |
4204 | 0 | } |
4205 | 0 | else if (!ascii_strcasecmp (string, "mlk768_bp384") && RFC9980) |
4206 | 0 | { |
4207 | 0 | curve = "bp384"; |
4208 | 0 | algo = PUBKEY_ALGO_MLK768_BP384; |
4209 | 0 | size = 768; |
4210 | 0 | is_pqc = 1; |
4211 | 0 | keyversion = 6; |
4212 | 0 | } |
4213 | 0 | else if (!ascii_strcasecmp (string, "mlk1024") && RFC9980) |
4214 | 0 | { |
4215 | 0 | curve = "X448"; |
4216 | 0 | algo = PUBKEY_ALGO_MLK1024_448; |
4217 | 0 | size = 1024; |
4218 | 0 | is_pqc = 1; |
4219 | 0 | keyversion = 6; |
4220 | 0 | } |
4221 | 0 | else if (!ascii_strcasecmp (string, "mlk1024_np521") && RFC9980) |
4222 | 0 | { |
4223 | 0 | curve = "nistp521"; |
4224 | 0 | algo = PUBKEY_ALGO_MLK1024_NP521; |
4225 | 0 | size = 1024; |
4226 | 0 | is_pqc = 1; |
4227 | 0 | keyversion = 6; |
4228 | 0 | } |
4229 | 0 | else if (!ascii_strcasecmp (string, "mlk1024_bp512") && RFC9980) |
4230 | 0 | { |
4231 | 0 | curve = "bp512"; |
4232 | 0 | algo = PUBKEY_ALGO_MLK1024_BP512; |
4233 | 0 | size = 1024; |
4234 | 0 | is_pqc = 1; |
4235 | 0 | keyversion = 6; |
4236 | 0 | } |
4237 | 0 | else if (!ascii_strcasecmp (string, "ietf25") && RFC9980) |
4238 | 0 | { |
4239 | 0 | curve = "Ed25519"; |
4240 | 0 | algo = PUBKEY_ALGO_X25519; |
4241 | 0 | size = 255; |
4242 | 0 | keyversion = 6; |
4243 | 0 | } |
4244 | 0 | else if (!ascii_strcasecmp (string, "ietf27") && RFC9980) |
4245 | 0 | { |
4246 | 0 | curve = "Ed25519"; |
4247 | 0 | algo = PUBKEY_ALGO_ED25519; |
4248 | 0 | size = 255; |
4249 | 0 | keyversion = 6; |
4250 | 0 | } |
4251 | 0 | else if (!ascii_strcasecmp (string, "mld65") && RFC9980) |
4252 | 0 | { |
4253 | 0 | algo = PUBKEY_ALGO_MLD65_25519; |
4254 | 0 | is_pqc = 1; |
4255 | 0 | } |
4256 | 0 | else if (!ascii_strcasecmp (string, "mld87") && RFC9980) |
4257 | 0 | { |
4258 | 0 | algo = PUBKEY_ALGO_MLD87_448; |
4259 | 0 | is_pqc = 1; |
4260 | 0 | } |
4261 | 0 | else if ((curve = openpgp_is_curve_supported (string, &algo, &size))) |
4262 | 0 | { |
4263 | 0 | if (!algo) |
4264 | 0 | { |
4265 | 0 | algo = PUBKEY_ALGO_ECDH; /* Default ECC algorithm. */ |
4266 | 0 | ecdh_or_ecdsa = 1; /* We may need to switch the algo. */ |
4267 | 0 | } |
4268 | 0 | if (curve && (!strcmp (curve, "X448") || !strcmp (curve, "Ed448"))) |
4269 | 0 | is_448 = 1; |
4270 | 0 | } |
4271 | 0 | else |
4272 | 0 | return gpg_error (GPG_ERR_UNKNOWN_CURVE); |
4273 | | |
4274 | | /* Parse the flags. */ |
4275 | 0 | keyuse = 0; |
4276 | 0 | if (flags) |
4277 | 0 | { |
4278 | 0 | char **tokens = NULL; |
4279 | |
|
4280 | 0 | tokens = strtokenize (flags, ","); |
4281 | 0 | if (!tokens) |
4282 | 0 | return gpg_error_from_syserror (); |
4283 | | |
4284 | 0 | for (i=0; (s = tokens[i]); i++) |
4285 | 0 | { |
4286 | 0 | if (!*s) |
4287 | 0 | ; |
4288 | 0 | else if (!ascii_strcasecmp (s, "sign")) |
4289 | 0 | keyuse |= PUBKEY_USAGE_SIG; |
4290 | 0 | else if (!ascii_strcasecmp (s, "encrypt") |
4291 | 0 | || !ascii_strcasecmp (s, "encr")) |
4292 | 0 | keyuse |= PUBKEY_USAGE_ENC; |
4293 | 0 | else if (!ascii_strcasecmp (s, "auth")) |
4294 | 0 | keyuse |= PUBKEY_USAGE_AUTH; |
4295 | 0 | else if (!ascii_strcasecmp (s, "cert")) |
4296 | 0 | keyuse |= PUBKEY_USAGE_CERT; |
4297 | 0 | else if (!ascii_strcasecmp (s, "ecdsa") && !from_card) |
4298 | 0 | { |
4299 | 0 | if (algo == PUBKEY_ALGO_ECDH || algo == PUBKEY_ALGO_ECDSA) |
4300 | 0 | algo = PUBKEY_ALGO_ECDSA; |
4301 | 0 | else |
4302 | 0 | { |
4303 | 0 | xfree (tokens); |
4304 | 0 | return gpg_error (GPG_ERR_INV_FLAG); |
4305 | 0 | } |
4306 | 0 | ecdh_or_ecdsa = 0; |
4307 | 0 | } |
4308 | 0 | else if (!ascii_strcasecmp (s, "ecdh") && !from_card) |
4309 | 0 | { |
4310 | 0 | if (algo == PUBKEY_ALGO_ECDH || algo == PUBKEY_ALGO_ECDSA) |
4311 | 0 | algo = PUBKEY_ALGO_ECDH; |
4312 | 0 | else |
4313 | 0 | { |
4314 | 0 | xfree (tokens); |
4315 | 0 | return gpg_error (GPG_ERR_INV_FLAG); |
4316 | 0 | } |
4317 | 0 | ecdh_or_ecdsa = 0; |
4318 | 0 | } |
4319 | 0 | else if (!ascii_strcasecmp (s, "eddsa") && !from_card) |
4320 | 0 | { |
4321 | | /* Not required but we allow it for consistency. */ |
4322 | 0 | if (algo == PUBKEY_ALGO_EDDSA) |
4323 | 0 | ; |
4324 | 0 | else |
4325 | 0 | { |
4326 | 0 | xfree (tokens); |
4327 | 0 | return gpg_error (GPG_ERR_INV_FLAG); |
4328 | 0 | } |
4329 | 0 | } |
4330 | 0 | else if (!ascii_strcasecmp (s, "v5")) |
4331 | 0 | keyversion = 5; |
4332 | 0 | else if (!ascii_strcasecmp (s, "v4")) |
4333 | 0 | keyversion = 4; |
4334 | 0 | else |
4335 | 0 | { |
4336 | 0 | xfree (tokens); |
4337 | 0 | return gpg_error (GPG_ERR_UNKNOWN_FLAG); |
4338 | 0 | } |
4339 | 0 | } |
4340 | | |
4341 | 0 | xfree (tokens); |
4342 | 0 | } |
4343 | | |
4344 | | /* If not yet decided switch between ecdh and ecdsa unless we want |
4345 | | * to read the algo from the current card. */ |
4346 | 0 | if (from_card) |
4347 | 0 | { |
4348 | 0 | keypair_info_t keypairlist, kpi; |
4349 | 0 | char *reqkeyref; |
4350 | |
|
4351 | 0 | if (!keyuse) |
4352 | 0 | keyuse = (for_subkey? PUBKEY_USAGE_ENC |
4353 | 0 | /* */ : (PUBKEY_USAGE_CERT|PUBKEY_USAGE_SIG)); |
4354 | | |
4355 | | /* Access the card to make sure we have one and to show the S/N. */ |
4356 | 0 | { |
4357 | 0 | char *serialno; |
4358 | |
|
4359 | 0 | err = agent_scd_serialno (&serialno, NULL); |
4360 | 0 | if (err) |
4361 | 0 | { |
4362 | 0 | log_error (_("error reading the card: %s\n"), gpg_strerror (err)); |
4363 | 0 | return err; |
4364 | 0 | } |
4365 | 0 | if (!opt.quiet) |
4366 | 0 | log_info (_("Serial number of the card: %s\n"), serialno); |
4367 | 0 | xfree (serialno); |
4368 | 0 | } |
4369 | | |
4370 | 0 | err = agent_scd_keypairinfo (ctrl, NULL, &keypairlist); |
4371 | 0 | if (err) |
4372 | 0 | { |
4373 | 0 | log_error (_("error reading the card: %s\n"), gpg_strerror (err)); |
4374 | 0 | return err; |
4375 | 0 | } |
4376 | 0 | agent_scd_getattr_one ((keyuse & (PUBKEY_USAGE_SIG|PUBKEY_USAGE_CERT)) |
4377 | 0 | ? "$SIGNKEYID":"$ENCRKEYID", &reqkeyref); |
4378 | |
|
4379 | 0 | algo = 0; /* Should already be the case. */ |
4380 | 0 | for (kpi=keypairlist; kpi && !algo; kpi = kpi->next) |
4381 | 0 | { |
4382 | 0 | gcry_sexp_t s_pkey; |
4383 | 0 | char *algostr = NULL; |
4384 | 0 | enum gcry_pk_algos algoid = 0; |
4385 | 0 | const char *keyref = kpi->idstr; |
4386 | |
|
4387 | 0 | if (!reqkeyref) |
4388 | 0 | continue; /* Card does not provide the info (skip all). */ |
4389 | | |
4390 | 0 | if (!keyref) |
4391 | 0 | continue; /* Ooops. */ |
4392 | 0 | if (strcmp (reqkeyref, keyref)) |
4393 | 0 | continue; /* This is not the requested keyref. */ |
4394 | | |
4395 | 0 | if ((keyuse & (PUBKEY_USAGE_SIG|PUBKEY_USAGE_CERT)) |
4396 | 0 | && (kpi->usage & (GCRY_PK_USAGE_SIGN|GCRY_PK_USAGE_CERT))) |
4397 | 0 | ; /* Okay */ |
4398 | 0 | else if ((keyuse & PUBKEY_USAGE_ENC) |
4399 | 0 | && (kpi->usage & GCRY_PK_USAGE_ENCR)) |
4400 | 0 | ; /* Okay */ |
4401 | 0 | else |
4402 | 0 | continue; /* Not usable for us. */ |
4403 | | |
4404 | 0 | if (agent_scd_readkey (ctrl, keyref, &s_pkey, NULL)) |
4405 | 0 | continue; /* Could not read the key. */ |
4406 | | |
4407 | 0 | algostr = pubkey_algo_string (s_pkey, &algoid); |
4408 | 0 | gcry_sexp_release (s_pkey); |
4409 | | |
4410 | | /* Map to OpenPGP algo number. |
4411 | | * We need to tweak the algo in case GCRY_PK_ECC is |
4412 | | * returned because pubkey_algo_string is not aware |
4413 | | * of the OpenPGP algo mapping. We need to |
4414 | | * distinguish between ECDH and ECDSA but we can do |
4415 | | * that only if we got usage flags. |
4416 | | * Note: Keep this in sync with ask_algo. */ |
4417 | 0 | if (algoid == GCRY_PK_ECC && algostr) |
4418 | 0 | { |
4419 | 0 | if (!strcmp (algostr, "ed25519")) |
4420 | 0 | algo = PUBKEY_ALGO_EDDSA; |
4421 | 0 | else if (!strcmp (algostr, "ed448")) |
4422 | 0 | { |
4423 | 0 | algo = PUBKEY_ALGO_EDDSA; |
4424 | 0 | is_448 = 1; |
4425 | 0 | } |
4426 | 0 | else if (!strcmp (algostr, "cv25519")) |
4427 | 0 | algo = PUBKEY_ALGO_ECDH; |
4428 | 0 | else if (!strcmp (algostr, "cv448")) |
4429 | 0 | { |
4430 | 0 | algo = PUBKEY_ALGO_ECDH; |
4431 | 0 | is_448 = 1; |
4432 | 0 | } |
4433 | 0 | else if ((kpi->usage & GCRY_PK_USAGE_ENCR)) |
4434 | 0 | algo = PUBKEY_ALGO_ECDH; |
4435 | 0 | else |
4436 | 0 | algo = PUBKEY_ALGO_ECDSA; |
4437 | 0 | } |
4438 | 0 | else |
4439 | 0 | algo = map_gcry_pk_to_openpgp (algoid); |
4440 | |
|
4441 | 0 | xfree (algostr); |
4442 | 0 | xfree (keygrip); |
4443 | 0 | keygrip = xtrystrdup (kpi->keygrip); |
4444 | 0 | if (!keygrip) |
4445 | 0 | { |
4446 | 0 | err = gpg_error_from_syserror (); |
4447 | 0 | xfree (reqkeyref); |
4448 | 0 | free_keypair_info (keypairlist); |
4449 | 0 | return err; |
4450 | 0 | } |
4451 | 0 | keytime = kpi->keytime; |
4452 | 0 | } |
4453 | | |
4454 | 0 | xfree (reqkeyref); |
4455 | 0 | free_keypair_info (keypairlist); |
4456 | 0 | if (!algo || !keygrip) |
4457 | 0 | { |
4458 | 0 | err = gpg_error (GPG_ERR_PUBKEY_ALGO); |
4459 | 0 | log_error ("no usable key on the card: %s\n", gpg_strerror (err)); |
4460 | 0 | xfree (keygrip); |
4461 | 0 | return err; |
4462 | 0 | } |
4463 | 0 | } |
4464 | 0 | else if (ecdh_or_ecdsa && keyuse) |
4465 | 0 | algo = (keyuse & PUBKEY_USAGE_ENC)? PUBKEY_ALGO_ECDH : PUBKEY_ALGO_ECDSA; |
4466 | 0 | else if (ecdh_or_ecdsa) |
4467 | 0 | algo = for_subkey? PUBKEY_ALGO_ECDH : PUBKEY_ALGO_ECDSA; |
4468 | | |
4469 | | /* Set or fix key usage. */ |
4470 | 0 | if (!keyuse) |
4471 | 0 | { |
4472 | 0 | if (algo == PUBKEY_ALGO_ECDSA || algo == PUBKEY_ALGO_EDDSA |
4473 | 0 | || algo == PUBKEY_ALGO_DSA || algo == PUBKEY_ALGO_ED25519) |
4474 | 0 | keyuse = PUBKEY_USAGE_SIG; |
4475 | 0 | else if (algo == PUBKEY_ALGO_RSA) |
4476 | 0 | keyuse = for_subkey? PUBKEY_USAGE_ENC : PUBKEY_USAGE_SIG; |
4477 | 0 | else |
4478 | 0 | keyuse = PUBKEY_USAGE_ENC; |
4479 | 0 | } |
4480 | 0 | else if (algo == PUBKEY_ALGO_ECDSA || algo == PUBKEY_ALGO_EDDSA |
4481 | 0 | || algo == PUBKEY_ALGO_DSA || algo == PUBKEY_ALGO_ED25519) |
4482 | 0 | { |
4483 | 0 | keyuse &= ~PUBKEY_USAGE_ENC; /* Forbid encryption. */ |
4484 | 0 | } |
4485 | 0 | else if (algo == PUBKEY_ALGO_ECDH || algo == PUBKEY_ALGO_ELGAMAL_E) |
4486 | 0 | { |
4487 | 0 | keyuse = PUBKEY_USAGE_ENC; /* Allow only encryption. */ |
4488 | 0 | } |
4489 | | |
4490 | | /* Make sure a primary key can certify. */ |
4491 | 0 | if (!for_subkey) |
4492 | 0 | keyuse |= PUBKEY_USAGE_CERT; |
4493 | | |
4494 | | /* But if requested remove th cert usage. */ |
4495 | 0 | if (clear_cert) |
4496 | 0 | keyuse &= ~PUBKEY_USAGE_CERT; |
4497 | | |
4498 | | /* Check that usage is actually possible. */ |
4499 | 0 | if (/**/((keyuse & (PUBKEY_USAGE_SIG|PUBKEY_USAGE_AUTH|PUBKEY_USAGE_CERT)) |
4500 | 0 | && !pubkey_get_nsig (algo)) |
4501 | 0 | || ((keyuse & PUBKEY_USAGE_ENC) |
4502 | 0 | && !pubkey_get_nenc (algo)) |
4503 | 0 | || (for_subkey && (keyuse & PUBKEY_USAGE_CERT))) |
4504 | 0 | { |
4505 | 0 | xfree (keygrip); |
4506 | 0 | return gpg_error (GPG_ERR_WRONG_KEY_USAGE); |
4507 | 0 | } |
4508 | | |
4509 | | /* Ed448, X448 and the PQC algos must only be used as v5 keys. */ |
4510 | 0 | if (keyversion == 6) |
4511 | 0 | ; |
4512 | 0 | else if (is_448 || is_pqc) |
4513 | 0 | { |
4514 | 0 | if (keyversion == 4) |
4515 | 0 | log_info (_("WARNING: v4 is specified, but overridden by v5.\n")); |
4516 | |
|
4517 | 0 | keyversion = 5; |
4518 | 0 | } |
4519 | 0 | else if (keyversion == 0) |
4520 | 0 | keyversion = 4; |
4521 | | |
4522 | | /* Return values. */ |
4523 | 0 | if (r_algo) |
4524 | 0 | *r_algo = algo; |
4525 | 0 | if (r_size) |
4526 | 0 | { |
4527 | 0 | unsigned int min, def, max; |
4528 | | |
4529 | | /* Make sure the keysize is in the allowed range. */ |
4530 | 0 | def = get_keysize_range (algo, &min, &max); |
4531 | 0 | if (!size) |
4532 | 0 | size = def; |
4533 | 0 | else if (size < min) |
4534 | 0 | size = min; |
4535 | 0 | else if (size > max) |
4536 | 0 | size = max; |
4537 | |
|
4538 | 0 | *r_size = fixup_keysize (size, algo, 1); |
4539 | 0 | } |
4540 | |
|
4541 | 0 | if (r_keyuse) |
4542 | 0 | *r_keyuse = keyuse; |
4543 | 0 | if (r_curve) |
4544 | 0 | *r_curve = curve; |
4545 | 0 | if (r_keyversion) |
4546 | 0 | *r_keyversion = keyversion; |
4547 | |
|
4548 | 0 | if (r_keygrip) |
4549 | 0 | *r_keygrip = keygrip; |
4550 | 0 | else |
4551 | 0 | xfree (keygrip); |
4552 | |
|
4553 | 0 | if (r_keytime) |
4554 | 0 | *r_keytime = keytime; |
4555 | |
|
4556 | 0 | return 0; |
4557 | 0 | } |
4558 | | |
4559 | | |
4560 | | /* Parse and return the standard key generation parameter. |
4561 | | * The string is expected to be in this format: |
4562 | | * |
4563 | | * ALGO[/FLAGS][+SUBALGO[/FLAGS]] |
4564 | | * |
4565 | | * Here ALGO is a string in the same format as printed by the |
4566 | | * keylisting. For example: |
4567 | | * |
4568 | | * rsa3072 := RSA with 3072 bit. |
4569 | | * dsa2048 := DSA with 2048 bit. |
4570 | | * elg2048 := Elgamal with 2048 bit. |
4571 | | * ed25519 := EDDSA using curve Ed25519. |
4572 | | * ed448 := EDDSA using curve Ed448. |
4573 | | * cv25519 := ECDH using curve Curve25519. |
4574 | | * cv448 := ECDH using curve X448. |
4575 | | * nistp256:= ECDSA or ECDH using curve NIST P-256 |
4576 | | * kyber := Kyber with the default parameters |
4577 | | * ky768_bp384 := Kyber-768 with BrainpoolP256r1 as second algo |
4578 | | * |
4579 | | * All strings with an unknown prefix are considered an elliptic |
4580 | | * curve. Curves which have no implicit algorithm require that FLAGS |
4581 | | * is given to select whether ECDSA or ECDH is used; this can either |
4582 | | * be done using an algorithm keyword or usage keywords. |
4583 | | * |
4584 | | * FLAGS is a comma delimited string of keywords: |
4585 | | * |
4586 | | * cert := Allow usage Certify |
4587 | | * sign := Allow usage Sign |
4588 | | * encr := Allow usage Encrypt |
4589 | | * auth := Allow usage Authentication |
4590 | | * encrypt := Alias for "encr" |
4591 | | * ecdsa := Use algorithm ECDSA. |
4592 | | * eddsa := Use algorithm EdDSA. |
4593 | | * ecdh := Use algorithm ECDH. |
4594 | | * v5 := Create version 5 key |
4595 | | * |
4596 | | * There are several defaults and fallbacks depending on the |
4597 | | * algorithm. PART can be used to select which part of STRING is |
4598 | | * used: |
4599 | | * -1 := Both parts |
4600 | | * 0 := Only the part of the primary key |
4601 | | * 1 := If there is one part parse that one, if there are |
4602 | | * two parts parse the part which best matches the |
4603 | | * SUGGESTED_USE or in case that can't be evaluated the second part. |
4604 | | * Always return using the args for the primary key (R_ALGO,....). |
4605 | | * |
4606 | | */ |
4607 | | gpg_error_t |
4608 | | parse_key_parameter_string (ctrl_t ctrl, |
4609 | | const char *string, int part, |
4610 | | unsigned int suggested_use, |
4611 | | int *r_algo, unsigned int *r_size, |
4612 | | unsigned int *r_keyuse, |
4613 | | char const **r_curve, |
4614 | | int *r_version, |
4615 | | char **r_keygrip, |
4616 | | u32 *r_keytime, |
4617 | | int *r_subalgo, unsigned int *r_subsize, |
4618 | | unsigned int *r_subkeyuse, |
4619 | | char const **r_subcurve, |
4620 | | int *r_subversion, |
4621 | | char **r_subkeygrip, |
4622 | | u32 *r_subkeytime) |
4623 | 0 | { |
4624 | 0 | gpg_error_t err = 0; |
4625 | 0 | char *primary, *secondary; |
4626 | |
|
4627 | 0 | if (r_algo) |
4628 | 0 | *r_algo = 0; |
4629 | 0 | if (r_size) |
4630 | 0 | *r_size = 0; |
4631 | 0 | if (r_keyuse) |
4632 | 0 | *r_keyuse = 0; |
4633 | 0 | if (r_curve) |
4634 | 0 | *r_curve = NULL; |
4635 | 0 | if (r_version) |
4636 | 0 | *r_version = 4; |
4637 | 0 | if (r_keygrip) |
4638 | 0 | *r_keygrip = NULL; |
4639 | 0 | if (r_keytime) |
4640 | 0 | *r_keytime = 0; |
4641 | 0 | if (r_subalgo) |
4642 | 0 | *r_subalgo = 0; |
4643 | 0 | if (r_subsize) |
4644 | 0 | *r_subsize = 0; |
4645 | 0 | if (r_subkeyuse) |
4646 | 0 | *r_subkeyuse = 0; |
4647 | 0 | if (r_subcurve) |
4648 | 0 | *r_subcurve = NULL; |
4649 | 0 | if (r_subversion) |
4650 | 0 | *r_subversion = 4; |
4651 | 0 | if (r_subkeygrip) |
4652 | 0 | *r_subkeygrip = NULL; |
4653 | 0 | if (r_subkeytime) |
4654 | 0 | *r_subkeytime = 0; |
4655 | |
|
4656 | 0 | if (!string || !*string |
4657 | 0 | || !ascii_strcasecmp (string, "default") || !strcmp (string, "-")) |
4658 | 0 | string = get_default_pubkey_algo (); |
4659 | 0 | else if (!ascii_strcasecmp (string, "future-default") |
4660 | 0 | || !ascii_strcasecmp (string, "futuredefault")) |
4661 | 0 | string = FUTURE_STD_KEY_PARAM; |
4662 | 0 | else if (!ascii_strcasecmp (string, "pqc")) |
4663 | 0 | string = PQC_STD_KEY_PARAM; |
4664 | 0 | else if (!ascii_strcasecmp (string, "pqc9980") && RFC9980) |
4665 | 0 | string = PQC9980_STD_KEY_PARAM; |
4666 | 0 | else if (!ascii_strcasecmp (string, "card")) |
4667 | 0 | string = "card/cert,sign+card/encr"; |
4668 | |
|
4669 | 0 | primary = xstrdup (string); |
4670 | 0 | secondary = strchr (primary, '+'); |
4671 | 0 | if (secondary) |
4672 | 0 | *secondary++ = 0; |
4673 | 0 | if (part == -1 || part == 0) |
4674 | 0 | { |
4675 | 0 | err = parse_key_parameter_part (ctrl, primary, |
4676 | 0 | 0, 0, r_algo, r_size, |
4677 | 0 | r_keyuse, r_curve, r_version, |
4678 | 0 | r_keygrip, r_keytime); |
4679 | 0 | if (!err && part == -1) |
4680 | 0 | err = parse_key_parameter_part (ctrl, secondary, |
4681 | 0 | 1, 0, r_subalgo, r_subsize, |
4682 | 0 | r_subkeyuse, r_subcurve, r_subversion, |
4683 | 0 | r_subkeygrip, r_subkeytime); |
4684 | 0 | } |
4685 | 0 | else if (part == 1) |
4686 | 0 | { |
4687 | | /* If we have SECONDARY, use that part. If there is only one |
4688 | | * part consider this to be the subkey algo. In case a |
4689 | | * SUGGESTED_USE has been given and the usage of the secondary |
4690 | | * part does not match SUGGESTED_USE try again using the primary |
4691 | | * part. Note that when falling back to the primary key we need |
4692 | | * to force clearing the cert usage. */ |
4693 | 0 | if (secondary) |
4694 | 0 | { |
4695 | 0 | err = parse_key_parameter_part (ctrl, secondary, |
4696 | 0 | 1, 0, |
4697 | 0 | r_algo, r_size, r_keyuse, r_curve, |
4698 | 0 | r_version, r_keygrip, r_keytime); |
4699 | 0 | if (!err && suggested_use && r_keyuse && !(suggested_use & *r_keyuse)) |
4700 | 0 | err = parse_key_parameter_part (ctrl, primary, |
4701 | 0 | 1, 1 /*(clear cert)*/, |
4702 | 0 | r_algo, r_size, r_keyuse, r_curve, |
4703 | 0 | r_version, r_keygrip, r_keytime); |
4704 | 0 | } |
4705 | 0 | else |
4706 | 0 | err = parse_key_parameter_part (ctrl, primary, |
4707 | 0 | 1, 0, |
4708 | 0 | r_algo, r_size, r_keyuse, r_curve, |
4709 | 0 | r_version, r_keygrip, r_keytime); |
4710 | 0 | } |
4711 | |
|
4712 | 0 | xfree (primary); |
4713 | |
|
4714 | 0 | return err; |
4715 | 0 | } |
4716 | | |
4717 | | |
4718 | | |
4719 | | /* Append R to the linked list PARA. */ |
4720 | | static void |
4721 | | append_to_parameter (struct para_data_s *para, struct para_data_s *r) |
4722 | 0 | { |
4723 | 0 | log_assert (para); |
4724 | 0 | while (para->next) |
4725 | 0 | para = para->next; |
4726 | 0 | para->next = r; |
4727 | 0 | } |
4728 | | |
4729 | | /* Release the parameter list R. */ |
4730 | | static void |
4731 | | release_parameter_list (struct para_data_s *r) |
4732 | 0 | { |
4733 | 0 | struct para_data_s *r2; |
4734 | |
|
4735 | 0 | for (; r ; r = r2) |
4736 | 0 | { |
4737 | 0 | r2 = r->next; |
4738 | 0 | if (r->key == pPASSPHRASE && *r->u.value) |
4739 | 0 | wipememory (r->u.value, strlen (r->u.value)); |
4740 | 0 | else if (r->key == pADSK) |
4741 | 0 | free_public_key (r->u.adsk); |
4742 | |
|
4743 | 0 | xfree (r); |
4744 | 0 | } |
4745 | 0 | } |
4746 | | |
4747 | | /* Return the N-th parameter of name KEY from PARA. An IDX of 0 |
4748 | | * returns the first and so on. */ |
4749 | | static struct para_data_s * |
4750 | | get_parameter_idx (struct para_data_s *para, enum para_name key, |
4751 | | unsigned int idx) |
4752 | 0 | { |
4753 | 0 | struct para_data_s *r; |
4754 | |
|
4755 | 0 | for(r = para; r; r = r->next) |
4756 | 0 | if (r->key == key) |
4757 | 0 | { |
4758 | 0 | if (!idx) |
4759 | 0 | return r; |
4760 | 0 | idx--; |
4761 | 0 | } |
4762 | 0 | return NULL; |
4763 | 0 | } |
4764 | | |
4765 | | /* Return the first parameter of name KEY from PARA. */ |
4766 | | static struct para_data_s * |
4767 | | get_parameter (struct para_data_s *para, enum para_name key) |
4768 | 0 | { |
4769 | 0 | return get_parameter_idx (para, key, 0); |
4770 | 0 | } |
4771 | | |
4772 | | static const char * |
4773 | | get_parameter_value( struct para_data_s *para, enum para_name key ) |
4774 | 0 | { |
4775 | 0 | struct para_data_s *r = get_parameter( para, key ); |
4776 | 0 | return (r && *r->u.value)? r->u.value : NULL; |
4777 | 0 | } |
4778 | | |
4779 | | |
4780 | | /* This is similar to get_parameter_value but also returns the empty |
4781 | | string. This is required so that quick_generate_keypair can use an |
4782 | | empty Passphrase to specify no-protection. */ |
4783 | | static const char * |
4784 | | get_parameter_passphrase (struct para_data_s *para) |
4785 | 0 | { |
4786 | 0 | struct para_data_s *r = get_parameter (para, pPASSPHRASE); |
4787 | 0 | return r ? r->u.value : NULL; |
4788 | 0 | } |
4789 | | |
4790 | | |
4791 | | static int |
4792 | | get_parameter_algo (ctrl_t ctrl, struct para_data_s *para, enum para_name key, |
4793 | | int *r_default) |
4794 | 0 | { |
4795 | 0 | int i; |
4796 | 0 | struct para_data_s *r = get_parameter( para, key ); |
4797 | |
|
4798 | 0 | if (r_default) |
4799 | 0 | *r_default = 0; |
4800 | |
|
4801 | 0 | if (!r) |
4802 | 0 | return -1; |
4803 | | |
4804 | | /* Note that we need to handle the ECC algorithms specified as |
4805 | | strings directly because Libgcrypt folds them all to ECC. */ |
4806 | 0 | if (!ascii_strcasecmp (r->u.value, "default")) |
4807 | 0 | { |
4808 | | /* Note: If you change this default algo, remember to change it |
4809 | | * also in gpg.c:gpgconf_list. */ |
4810 | | /* FIXME: We only allow the algo here and have a separate thing |
4811 | | * for the curve etc. That is a ugly but demanded for backward |
4812 | | * compatibility with the batch key generation. It would be |
4813 | | * better to make full use of parse_key_parameter_string. */ |
4814 | 0 | parse_key_parameter_string (ctrl, NULL, 0, 0, |
4815 | 0 | &i, NULL, NULL, NULL, NULL, NULL, NULL, |
4816 | 0 | NULL, NULL, NULL, NULL, NULL, NULL, NULL); |
4817 | 0 | if (r_default) |
4818 | 0 | *r_default = 1; |
4819 | 0 | } |
4820 | 0 | else if (digitp (r->u.value)) |
4821 | 0 | i = atoi( r->u.value ); |
4822 | 0 | else if (!strcmp (r->u.value, "ELG-E") |
4823 | 0 | || !strcmp (r->u.value, "ELG")) |
4824 | 0 | i = PUBKEY_ALGO_ELGAMAL_E; |
4825 | 0 | else if (!ascii_strcasecmp (r->u.value, "EdDSA")) |
4826 | 0 | i = PUBKEY_ALGO_EDDSA; |
4827 | 0 | else if (!ascii_strcasecmp (r->u.value, "ECDSA")) |
4828 | 0 | i = PUBKEY_ALGO_ECDSA; |
4829 | 0 | else if (!ascii_strcasecmp (r->u.value, "ECDH")) |
4830 | 0 | i = PUBKEY_ALGO_ECDH; |
4831 | 0 | else if (!ascii_strcasecmp (r->u.value, "KYBER")) |
4832 | 0 | i = PUBKEY_ALGO_KYBER; |
4833 | 0 | else |
4834 | 0 | i = map_gcry_pk_to_openpgp (gcry_pk_map_name (r->u.value)); |
4835 | |
|
4836 | 0 | if (i == PUBKEY_ALGO_RSA_E || i == PUBKEY_ALGO_RSA_S) |
4837 | 0 | i = 0; /* we don't want to allow generation of these algorithms */ |
4838 | 0 | return i; |
4839 | 0 | } |
4840 | | |
4841 | | |
4842 | | /* Parse a usage string. The usage keywords "auth", "sign", "encr" |
4843 | | * may be delimited by space, tab, or comma. On error -1 is returned |
4844 | | * instead of the usage flags. */ |
4845 | | static int |
4846 | | parse_usagestr (const char *usagestr) |
4847 | 0 | { |
4848 | 0 | gpg_error_t err; |
4849 | 0 | char **tokens = NULL; |
4850 | 0 | const char *s; |
4851 | 0 | int i; |
4852 | 0 | unsigned int use = 0; |
4853 | |
|
4854 | 0 | tokens = strtokenize (usagestr, " \t,"); |
4855 | 0 | if (!tokens) |
4856 | 0 | { |
4857 | 0 | err = gpg_error_from_syserror (); |
4858 | 0 | log_error ("strtokenize failed: %s\n", gpg_strerror (err)); |
4859 | 0 | return -1; |
4860 | 0 | } |
4861 | | |
4862 | 0 | for (i=0; (s = tokens[i]); i++) |
4863 | 0 | { |
4864 | 0 | if (!*s) |
4865 | 0 | ; |
4866 | 0 | else if (!ascii_strcasecmp (s, "sign")) |
4867 | 0 | use |= PUBKEY_USAGE_SIG; |
4868 | 0 | else if (!ascii_strcasecmp (s, "encrypt") |
4869 | 0 | || !ascii_strcasecmp (s, "encr")) |
4870 | 0 | use |= PUBKEY_USAGE_ENC; |
4871 | 0 | else if (!ascii_strcasecmp (s, "auth")) |
4872 | 0 | use |= PUBKEY_USAGE_AUTH; |
4873 | 0 | else if (!ascii_strcasecmp (s, "cert")) |
4874 | 0 | use |= PUBKEY_USAGE_CERT; |
4875 | 0 | else if (!ascii_strcasecmp (s, "renc")) |
4876 | 0 | use |= PUBKEY_USAGE_RENC; |
4877 | 0 | else if (!ascii_strcasecmp (s, "time")) |
4878 | 0 | use |= PUBKEY_USAGE_TIME; |
4879 | 0 | else if (!ascii_strcasecmp (s, "group")) |
4880 | 0 | use |= PUBKEY_USAGE_GROUP; |
4881 | 0 | else |
4882 | 0 | { |
4883 | 0 | xfree (tokens); |
4884 | 0 | return -1; /* error */ |
4885 | 0 | } |
4886 | 0 | } |
4887 | | |
4888 | 0 | xfree (tokens); |
4889 | 0 | return use; |
4890 | 0 | } |
4891 | | |
4892 | | |
4893 | | /* |
4894 | | * Parse the usage parameter and set the keyflags. Returns -1 on |
4895 | | * error, 0 for no usage given or 1 for usage available. |
4896 | | */ |
4897 | | static int |
4898 | | parse_parameter_usage (const char *fname, |
4899 | | struct para_data_s *para, enum para_name key) |
4900 | 0 | { |
4901 | 0 | struct para_data_s *r = get_parameter( para, key ); |
4902 | 0 | int i; |
4903 | |
|
4904 | 0 | if (!r) |
4905 | 0 | return 0; /* none (this is an optional parameter)*/ |
4906 | | |
4907 | 0 | i = parse_usagestr (r->u.value); |
4908 | 0 | if (i == -1) |
4909 | 0 | { |
4910 | 0 | log_error ("%s:%d: invalid usage list\n", fname, r->lnr ); |
4911 | 0 | return -1; /* error */ |
4912 | 0 | } |
4913 | | |
4914 | 0 | r->u.usage = i; |
4915 | 0 | return 1; |
4916 | 0 | } |
4917 | | |
4918 | | |
4919 | | /* Parse the revocation key specified by NAME, check that the public |
4920 | | * key exists (so that we can get the required public key algorithm), |
4921 | | * and return a parameter with the revocation key information. On |
4922 | | * error print a diagnostic and return NULL. */ |
4923 | | static struct para_data_s * |
4924 | | prepare_desig_revoker (ctrl_t ctrl, const char *name) |
4925 | 0 | { |
4926 | 0 | gpg_error_t err; |
4927 | 0 | struct para_data_s *para = NULL; |
4928 | 0 | KEYDB_SEARCH_DESC desc; |
4929 | 0 | int sensitive = 0; |
4930 | 0 | struct revocation_key revkey; |
4931 | 0 | PKT_public_key *revoker_pk = NULL; |
4932 | 0 | size_t fprlen; |
4933 | |
|
4934 | 0 | if (!ascii_strncasecmp (name, "sensitive:", 10) && !spacep (name+10)) |
4935 | 0 | { |
4936 | 0 | name += 10; |
4937 | 0 | sensitive = 1; |
4938 | 0 | } |
4939 | |
|
4940 | 0 | if (classify_user_id (name, &desc, 1) |
4941 | 0 | || desc.mode != KEYDB_SEARCH_MODE_FPR) |
4942 | 0 | { |
4943 | 0 | log_info (_("\"%s\" is not a fingerprint\n"), name); |
4944 | 0 | err = gpg_error (GPG_ERR_INV_NAME); |
4945 | 0 | goto leave; |
4946 | 0 | } |
4947 | | |
4948 | 0 | revoker_pk = xcalloc (1, sizeof *revoker_pk); |
4949 | 0 | revoker_pk->req_usage = PUBKEY_USAGE_CERT; |
4950 | 0 | err = get_pubkey_byname (ctrl, GET_PUBKEY_TRY_LDAP, |
4951 | 0 | NULL, revoker_pk, name, NULL, NULL, 1); |
4952 | 0 | if (err) |
4953 | 0 | goto leave; |
4954 | | |
4955 | 0 | fingerprint_from_pk (revoker_pk, revkey.fpr, &fprlen); |
4956 | 0 | if (fprlen != 20 && fprlen != 32) |
4957 | 0 | { |
4958 | 0 | log_info (_("cannot appoint a PGP 2.x style key as a " |
4959 | 0 | "designated revoker\n")); |
4960 | 0 | err = gpg_error (GPG_ERR_UNUSABLE_PUBKEY); |
4961 | 0 | goto leave; |
4962 | 0 | } |
4963 | 0 | revkey.fprlen = fprlen; |
4964 | 0 | revkey.class = 0x80; |
4965 | 0 | if (sensitive) |
4966 | 0 | revkey.class |= 0x40; |
4967 | 0 | revkey.algid = revoker_pk->pubkey_algo; |
4968 | |
|
4969 | 0 | para = xcalloc (1, sizeof *para); |
4970 | 0 | para->key = pREVOKER; |
4971 | 0 | memcpy (¶->u.revkey, &revkey, sizeof revkey); |
4972 | |
|
4973 | 0 | leave: |
4974 | 0 | if (err) |
4975 | 0 | log_error ("invalid revocation key '%s': %s\n", name, gpg_strerror (err)); |
4976 | 0 | free_public_key (revoker_pk); |
4977 | 0 | return para; |
4978 | 0 | } |
4979 | | |
4980 | | |
4981 | | /* Parse an ADSK specified by NAME, check that the public key exists |
4982 | | * and return a parameter with the adsk information. On error print a |
4983 | | * diagnostic and return NULL. */ |
4984 | | static struct para_data_s * |
4985 | | prepare_adsk (ctrl_t ctrl, const char *name) |
4986 | 0 | { |
4987 | 0 | gpg_error_t err; |
4988 | 0 | char *namebuffer = NULL; |
4989 | 0 | struct para_data_s *para = NULL; |
4990 | 0 | KEYDB_SEARCH_DESC desc; |
4991 | 0 | PKT_public_key *adsk_pk = NULL; |
4992 | 0 | char *p; |
4993 | |
|
4994 | 0 | if (classify_user_id (name, &desc, 1) |
4995 | 0 | || desc.mode != KEYDB_SEARCH_MODE_FPR) |
4996 | 0 | { |
4997 | 0 | log_info (_("\"%s\" is not a fingerprint\n"), name); |
4998 | 0 | err = gpg_error (GPG_ERR_INV_NAME); |
4999 | 0 | goto leave; |
5000 | 0 | } |
5001 | | |
5002 | | /* Force searching for that exact fingerprint. */ |
5003 | 0 | if (!strchr (name, '!')) |
5004 | 0 | { |
5005 | 0 | namebuffer = xstrconcat (name, "!", NULL); |
5006 | 0 | name = namebuffer; |
5007 | 0 | } |
5008 | |
|
5009 | 0 | adsk_pk = xcalloc (1, sizeof *adsk_pk); |
5010 | 0 | adsk_pk->req_usage = PUBKEY_USAGE_ENC | PUBKEY_USAGE_RENC; |
5011 | 0 | err = get_pubkey_byname (ctrl, GET_PUBKEY_TRY_LDAP, |
5012 | 0 | NULL, adsk_pk, name, NULL, NULL, 1); |
5013 | 0 | if (err) |
5014 | 0 | goto leave; |
5015 | | |
5016 | 0 | para = xcalloc (1, sizeof *para); |
5017 | 0 | para->key = pADSK; |
5018 | 0 | para->u.adsk = adsk_pk; |
5019 | 0 | adsk_pk = NULL; |
5020 | |
|
5021 | 0 | leave: |
5022 | 0 | if (err) |
5023 | 0 | { |
5024 | 0 | if (namebuffer && (p=strchr (namebuffer, '!'))) |
5025 | 0 | *p = 0; /* Strip the ! for the diagnostic. */ |
5026 | 0 | write_status_error ("add_adsk", err); |
5027 | 0 | log_error ("invalid ADSK '%s' specified: %s\n", name, gpg_strerror (err)); |
5028 | 0 | } |
5029 | 0 | free_public_key (adsk_pk); |
5030 | 0 | xfree (namebuffer); |
5031 | 0 | return para; |
5032 | 0 | } |
5033 | | |
5034 | | |
5035 | | /* Parse a pREVOKER parameter into its dedicated parts. */ |
5036 | | static int |
5037 | | parse_revocation_key (const char *fname, |
5038 | | struct para_data_s *para, enum para_name key) |
5039 | 0 | { |
5040 | 0 | struct para_data_s *r = get_parameter( para, key ); |
5041 | 0 | struct revocation_key revkey; |
5042 | 0 | char *pn; |
5043 | 0 | int i; |
5044 | |
|
5045 | 0 | if( !r ) |
5046 | 0 | return 0; /* none (this is an optional parameter) */ |
5047 | | |
5048 | 0 | pn = r->u.value; |
5049 | |
|
5050 | 0 | revkey.class=0x80; |
5051 | 0 | revkey.algid=atoi(pn); |
5052 | 0 | if(!revkey.algid) |
5053 | 0 | goto fail; |
5054 | | |
5055 | | /* Skip to the fpr */ |
5056 | 0 | while(*pn && *pn!=':') |
5057 | 0 | pn++; |
5058 | |
|
5059 | 0 | if(*pn!=':') |
5060 | 0 | goto fail; |
5061 | | |
5062 | 0 | pn++; |
5063 | |
|
5064 | 0 | for(i=0;i<MAX_FINGERPRINT_LEN && *pn && !spacep (pn);i++,pn+=2) |
5065 | 0 | { |
5066 | 0 | int c=hextobyte(pn); |
5067 | 0 | if(c==-1) |
5068 | 0 | goto fail; |
5069 | | |
5070 | 0 | revkey.fpr[i]=c; |
5071 | 0 | } |
5072 | 0 | if (i != 20 && i != 32) |
5073 | 0 | goto fail; |
5074 | | |
5075 | 0 | revkey.fprlen = i; |
5076 | | |
5077 | | /* skip to the tag */ |
5078 | 0 | while(*pn && *pn!='s' && *pn!='S') |
5079 | 0 | pn++; |
5080 | |
|
5081 | 0 | if(ascii_strcasecmp(pn,"sensitive")==0) |
5082 | 0 | revkey.class|=0x40; |
5083 | |
|
5084 | 0 | memcpy(&r->u.revkey,&revkey,sizeof(struct revocation_key)); |
5085 | |
|
5086 | 0 | return 0; |
5087 | | |
5088 | 0 | fail: |
5089 | 0 | log_error("%s:%d: invalid revocation key\n", fname, r->lnr ); |
5090 | 0 | return -1; /* error */ |
5091 | 0 | } |
5092 | | |
5093 | | |
5094 | | static u32 |
5095 | | get_parameter_u32( struct para_data_s *para, enum para_name key ) |
5096 | 0 | { |
5097 | 0 | struct para_data_s *r = get_parameter( para, key ); |
5098 | |
|
5099 | 0 | if( !r ) |
5100 | 0 | return 0; |
5101 | 0 | if (r->key == pKEYCREATIONDATE || r->key == pSUBKEYCREATIONDATE |
5102 | 0 | || r->key == pAUTHKEYCREATIONDATE) |
5103 | 0 | return r->u.creation; |
5104 | 0 | if( r->key == pKEYEXPIRE || r->key == pSUBKEYEXPIRE ) |
5105 | 0 | return r->u.expire; |
5106 | 0 | if( r->key == pKEYUSAGE || r->key == pSUBKEYUSAGE ) |
5107 | 0 | return r->u.usage; |
5108 | | |
5109 | 0 | return (unsigned int)strtoul( r->u.value, NULL, 10 ); |
5110 | 0 | } |
5111 | | |
5112 | | static unsigned int |
5113 | | get_parameter_uint( struct para_data_s *para, enum para_name key ) |
5114 | 0 | { |
5115 | 0 | return get_parameter_u32( para, key ); |
5116 | 0 | } |
5117 | | |
5118 | | static struct revocation_key * |
5119 | | get_parameter_revkey (struct para_data_s *para, unsigned int idx) |
5120 | 0 | { |
5121 | 0 | struct para_data_s *r = get_parameter_idx (para, pREVOKER, idx); |
5122 | 0 | return r? &r->u.revkey : NULL; |
5123 | 0 | } |
5124 | | |
5125 | | static PKT_public_key * |
5126 | | get_parameter_adsk (struct para_data_s *para, unsigned int idx) |
5127 | 0 | { |
5128 | 0 | struct para_data_s *r = get_parameter_idx (para, pADSK, idx); |
5129 | 0 | return r? r->u.adsk : NULL; |
5130 | 0 | } |
5131 | | |
5132 | | static int |
5133 | | get_parameter_bool (struct para_data_s *para, enum para_name key) |
5134 | 0 | { |
5135 | 0 | struct para_data_s *r = get_parameter (para, key); |
5136 | 0 | return (r && r->u.abool); |
5137 | 0 | } |
5138 | | |
5139 | | |
5140 | | static int |
5141 | | proc_parameter_file (ctrl_t ctrl, struct para_data_s *para, const char *fname, |
5142 | | struct output_control_s *outctrl, int card ) |
5143 | 0 | { |
5144 | 0 | struct para_data_s *r; |
5145 | 0 | const char *s1, *s2, *s3; |
5146 | 0 | size_t n; |
5147 | 0 | char *p; |
5148 | 0 | strlist_t sl; |
5149 | 0 | int is_default = 0; |
5150 | 0 | int have_user_id = 0; |
5151 | 0 | const char *saved_def_keyserver_url = opt.def_keyserver_url; |
5152 | 0 | int err, algo; |
5153 | 0 | u32 creation_time = (u32)-1; |
5154 | 0 | int res = -1; |
5155 | | |
5156 | | /* Check that we have all required parameters. */ |
5157 | 0 | r = get_parameter( para, pKEYTYPE ); |
5158 | 0 | if(r) |
5159 | 0 | { |
5160 | 0 | algo = get_parameter_algo (ctrl, para, pKEYTYPE, &is_default); |
5161 | 0 | if (openpgp_pk_test_algo2 (algo, PUBKEY_USAGE_SIG)) |
5162 | 0 | { |
5163 | 0 | log_error ("%s:%d: invalid algorithm\n", fname, r->lnr ); |
5164 | 0 | goto leave; |
5165 | 0 | } |
5166 | 0 | } |
5167 | 0 | else |
5168 | 0 | { |
5169 | 0 | log_error ("%s: no Key-Type specified\n",fname); |
5170 | 0 | goto leave; |
5171 | 0 | } |
5172 | | |
5173 | 0 | err = parse_parameter_usage (fname, para, pKEYUSAGE); |
5174 | 0 | if (!err) |
5175 | 0 | { |
5176 | | /* Default to algo capabilities if key-usage is not provided and |
5177 | | no default algorithm has been requested. */ |
5178 | 0 | r = xmalloc_clear(sizeof(*r)); |
5179 | 0 | r->key = pKEYUSAGE; |
5180 | 0 | r->u.usage = (is_default |
5181 | 0 | ? (PUBKEY_USAGE_CERT | PUBKEY_USAGE_SIG) |
5182 | 0 | : openpgp_pk_algo_usage(algo)); |
5183 | 0 | append_to_parameter (para, r); |
5184 | 0 | } |
5185 | 0 | else if (err == -1) |
5186 | 0 | goto leave; |
5187 | 0 | else |
5188 | 0 | { |
5189 | 0 | r = get_parameter (para, pKEYUSAGE); |
5190 | 0 | if (r && (r->u.usage |
5191 | 0 | & ~(openpgp_pk_algo_usage (algo) | PUBKEY_USAGE_GROUP))) |
5192 | 0 | { |
5193 | 0 | log_error ("%s:%d: specified Key-Usage not allowed for algo %d\n", |
5194 | 0 | fname, r->lnr, algo); |
5195 | 0 | goto leave; |
5196 | 0 | } |
5197 | 0 | } |
5198 | | |
5199 | 0 | is_default = 0; |
5200 | 0 | r = get_parameter( para, pSUBKEYTYPE ); |
5201 | 0 | if(r) |
5202 | 0 | { |
5203 | 0 | algo = get_parameter_algo (ctrl, para, pSUBKEYTYPE, &is_default); |
5204 | 0 | if (openpgp_pk_test_algo (algo)) |
5205 | 0 | { |
5206 | 0 | log_error ("%s:%d: invalid algorithm\n", fname, r->lnr ); |
5207 | 0 | goto leave; |
5208 | 0 | } |
5209 | | |
5210 | 0 | err = parse_parameter_usage (fname, para, pSUBKEYUSAGE); |
5211 | 0 | if (!err) |
5212 | 0 | { |
5213 | | /* Default to algo capabilities if subkey-usage is not |
5214 | | provided. Take care not to include RENC. */ |
5215 | 0 | r = xmalloc_clear (sizeof(*r)); |
5216 | 0 | r->key = pSUBKEYUSAGE; |
5217 | 0 | r->u.usage = (is_default |
5218 | 0 | ? PUBKEY_USAGE_ENC |
5219 | 0 | : (openpgp_pk_algo_usage (algo) |
5220 | 0 | & ~PUBKEY_USAGE_RENC) ); |
5221 | 0 | append_to_parameter (para, r); |
5222 | 0 | } |
5223 | 0 | else if (err == -1) |
5224 | 0 | goto leave; |
5225 | 0 | else |
5226 | 0 | { |
5227 | 0 | r = get_parameter (para, pSUBKEYUSAGE); |
5228 | 0 | if (r && (r->u.usage |
5229 | 0 | & ~(openpgp_pk_algo_usage (algo)|PUBKEY_USAGE_GROUP))) |
5230 | 0 | { |
5231 | 0 | log_error ("%s:%d: specified Subkey-Usage not allowed" |
5232 | 0 | " for algo %d\n", fname, r->lnr, algo); |
5233 | 0 | goto leave; |
5234 | 0 | } |
5235 | 0 | } |
5236 | 0 | } |
5237 | | |
5238 | | |
5239 | 0 | if( get_parameter_value( para, pUSERID ) ) |
5240 | 0 | have_user_id=1; |
5241 | 0 | else |
5242 | 0 | { |
5243 | | /* create the formatted user ID */ |
5244 | 0 | s1 = get_parameter_value( para, pNAMEREAL ); |
5245 | 0 | s2 = get_parameter_value( para, pNAMECOMMENT ); |
5246 | 0 | s3 = get_parameter_value( para, pNAMEEMAIL ); |
5247 | 0 | if( s1 || s2 || s3 ) |
5248 | 0 | { |
5249 | 0 | n = (s1?strlen(s1):0) + (s2?strlen(s2):0) + (s3?strlen(s3):0); |
5250 | 0 | r = xmalloc_clear( sizeof *r + n + 20 ); |
5251 | 0 | r->key = pUSERID; |
5252 | 0 | p = r->u.value; |
5253 | 0 | if( s1 ) |
5254 | 0 | p = stpcpy(p, s1 ); |
5255 | 0 | if( s2 ) |
5256 | 0 | p = stpcpy(stpcpy(stpcpy(p," ("), s2 ),")"); |
5257 | 0 | if( s3 ) |
5258 | 0 | { |
5259 | | /* If we have only the email part, do not add the space |
5260 | | * and the angle brackets. */ |
5261 | 0 | if (*r->u.value) |
5262 | 0 | p = stpcpy(stpcpy(stpcpy(p," <"), s3 ),">"); |
5263 | 0 | else |
5264 | 0 | p = stpcpy (p, s3); |
5265 | 0 | } |
5266 | 0 | append_to_parameter (para, r); |
5267 | 0 | have_user_id=1; |
5268 | 0 | } |
5269 | 0 | } |
5270 | |
|
5271 | 0 | if(!have_user_id) |
5272 | 0 | { |
5273 | 0 | log_error("%s: no User-ID specified\n",fname); |
5274 | 0 | goto leave; |
5275 | 0 | } |
5276 | | |
5277 | | /* Set preferences, if any. */ |
5278 | 0 | keygen_set_std_prefs(get_parameter_value( para, pPREFERENCES ), 0); |
5279 | | |
5280 | | /* Set keyserver, if any. */ |
5281 | 0 | s1=get_parameter_value( para, pKEYSERVER ); |
5282 | 0 | if(s1) |
5283 | 0 | { |
5284 | 0 | struct keyserver_spec *spec; |
5285 | |
|
5286 | 0 | spec = parse_keyserver_uri (s1, 1); |
5287 | 0 | if(spec) |
5288 | 0 | { |
5289 | 0 | free_keyserver_spec(spec); |
5290 | 0 | opt.def_keyserver_url=s1; |
5291 | 0 | } |
5292 | 0 | else |
5293 | 0 | { |
5294 | 0 | r = get_parameter (para, pKEYSERVER); |
5295 | 0 | log_error("%s:%d: invalid keyserver url\n", fname, r->lnr ); |
5296 | 0 | goto leave; |
5297 | 0 | } |
5298 | 0 | } |
5299 | | |
5300 | | /* Set revoker from parameter file, if any. Must be done first so |
5301 | | * that we don't find a parameter set via prepare_desig_revoker. */ |
5302 | 0 | if (parse_revocation_key (fname, para, pREVOKER)) |
5303 | 0 | goto leave; |
5304 | | |
5305 | | /* Check and append revokers from the config file. */ |
5306 | 0 | for (sl = opt.desig_revokers; sl; sl = sl->next) |
5307 | 0 | { |
5308 | 0 | r = prepare_desig_revoker (ctrl, sl->d); |
5309 | 0 | if (!r) |
5310 | 0 | goto leave; |
5311 | 0 | append_to_parameter (para, r); |
5312 | 0 | } |
5313 | | |
5314 | | |
5315 | | /* Check and append ADSKs from the config file. While doing this |
5316 | | * also check for duplicate specifications. In addition we remove |
5317 | | * an optional '!' suffix for easier comparing; the suffix is anyway |
5318 | | * re-added later. */ |
5319 | 0 | keygen_prepare_new_key_adsks (); |
5320 | 0 | for (sl = opt.def_new_key_adsks; sl; sl = sl->next) |
5321 | 0 | { |
5322 | 0 | if (!*sl->d) |
5323 | 0 | continue; |
5324 | | |
5325 | 0 | r = prepare_adsk (ctrl, sl->d); |
5326 | 0 | if (!r) |
5327 | 0 | goto leave; |
5328 | 0 | append_to_parameter (para, r); |
5329 | 0 | } |
5330 | | |
5331 | | |
5332 | | /* Make KEYCREATIONDATE from Creation-Date. We ignore this if the |
5333 | | * key has been taken from a card and a keycreationtime has already |
5334 | | * been set. This is so that we don't generate a key with a |
5335 | | * fingerprint different from the one stored on the OpenPGP card. */ |
5336 | 0 | r = get_parameter (para, pCREATIONDATE); |
5337 | 0 | if (r && *r->u.value && !(get_parameter_bool (para, pCARDKEY) |
5338 | 0 | && get_parameter_u32 (para, pKEYCREATIONDATE))) |
5339 | 0 | { |
5340 | 0 | creation_time = parse_creation_string (r->u.value); |
5341 | 0 | if (!creation_time) |
5342 | 0 | { |
5343 | 0 | log_error ("%s:%d: invalid creation date\n", fname, r->lnr ); |
5344 | 0 | goto leave; |
5345 | 0 | } |
5346 | 0 | r->u.creation = creation_time; |
5347 | 0 | r->key = pKEYCREATIONDATE; /* Change that entry. */ |
5348 | 0 | } |
5349 | | |
5350 | | /* Make KEYEXPIRE from Expire-Date. */ |
5351 | 0 | r = get_parameter( para, pEXPIREDATE ); |
5352 | 0 | if( r && *r->u.value ) |
5353 | 0 | { |
5354 | 0 | u32 seconds; |
5355 | |
|
5356 | 0 | seconds = parse_expire_string_with_ct (r->u.value, creation_time); |
5357 | 0 | if( seconds == (u32)-1 ) |
5358 | 0 | { |
5359 | 0 | log_error("%s:%d: invalid expire date\n", fname, r->lnr ); |
5360 | 0 | goto leave; |
5361 | 0 | } |
5362 | 0 | r->u.expire = seconds; |
5363 | 0 | r->key = pKEYEXPIRE; /* change that entry */ |
5364 | | |
5365 | | /* Make SUBKEYEXPIRE from Subkey-Expire-Date, if any. */ |
5366 | 0 | r = get_parameter( para, pSUBKEYEXPIREDATE ); |
5367 | 0 | if( r && *r->u.value ) |
5368 | 0 | { |
5369 | 0 | seconds = parse_expire_string_with_ct (r->u.value, creation_time); |
5370 | 0 | if( seconds == (u32)-1 ) |
5371 | 0 | { |
5372 | 0 | log_error("%s:%d: invalid subkey expire date\n", fname, r->lnr ); |
5373 | 0 | goto leave; |
5374 | 0 | } |
5375 | 0 | r->key = pSUBKEYEXPIRE; /* change that entry */ |
5376 | 0 | r->u.expire = seconds; |
5377 | 0 | } |
5378 | 0 | else |
5379 | 0 | { |
5380 | | /* Or else, set Expire-Date for the subkey */ |
5381 | 0 | r = xmalloc_clear( sizeof *r + 20 ); |
5382 | 0 | r->key = pSUBKEYEXPIRE; |
5383 | 0 | r->u.expire = seconds; |
5384 | 0 | append_to_parameter (para, r); |
5385 | 0 | } |
5386 | 0 | } |
5387 | 0 | res = 0; |
5388 | |
|
5389 | 0 | do_generate_keypair (ctrl, para, outctrl, card ); |
5390 | 0 | leave: |
5391 | 0 | opt.def_keyserver_url = saved_def_keyserver_url; |
5392 | 0 | return res; |
5393 | 0 | } |
5394 | | |
5395 | | |
5396 | | /**************** |
5397 | | * Kludge to allow non interactive key generation controlled |
5398 | | * by a parameter file. |
5399 | | * Note, that string parameters are expected to be in UTF-8 |
5400 | | */ |
5401 | | static void |
5402 | | read_parameter_file (ctrl_t ctrl, const char *fname ) |
5403 | 0 | { |
5404 | 0 | static struct { const char *name; |
5405 | 0 | enum para_name key; |
5406 | 0 | } keywords[] = { |
5407 | 0 | { "Key-Type", pKEYTYPE}, |
5408 | 0 | { "Key-Length", pKEYLENGTH }, |
5409 | 0 | { "Key-Curve", pKEYCURVE }, |
5410 | 0 | { "Key-Usage", pKEYUSAGE }, |
5411 | 0 | { "Subkey-Type", pSUBKEYTYPE }, |
5412 | 0 | { "Subkey-Length", pSUBKEYLENGTH }, |
5413 | 0 | { "Subkey-Curve", pSUBKEYCURVE }, |
5414 | 0 | { "Subkey-Usage", pSUBKEYUSAGE }, |
5415 | 0 | { "Name-Real", pNAMEREAL }, |
5416 | 0 | { "Name-Email", pNAMEEMAIL }, |
5417 | 0 | { "Name-Comment", pNAMECOMMENT }, |
5418 | 0 | { "User-Id", pUSERID }, |
5419 | 0 | { "Expire-Date", pEXPIREDATE }, |
5420 | 0 | { "Subkey-Expire-Date", pSUBKEYEXPIREDATE }, |
5421 | 0 | { "Creation-Date", pCREATIONDATE }, |
5422 | 0 | { "Passphrase", pPASSPHRASE }, |
5423 | 0 | { "Preferences", pPREFERENCES }, |
5424 | 0 | { "Revoker", pREVOKER }, |
5425 | 0 | { "Handle", pHANDLE }, |
5426 | 0 | { "Keyserver", pKEYSERVER }, |
5427 | 0 | { "Keygrip", pKEYGRIP }, |
5428 | 0 | { "Key-Grip", pKEYGRIP }, |
5429 | 0 | { "Subkey-grip", pSUBKEYGRIP }, |
5430 | 0 | { "Key-Version", pVERSION }, |
5431 | 0 | { "Subkey-Version", pSUBVERSION }, |
5432 | 0 | { NULL, 0 } |
5433 | 0 | }; |
5434 | 0 | IOBUF fp; |
5435 | 0 | byte *line; |
5436 | 0 | unsigned int maxlen, nline; |
5437 | 0 | char *p; |
5438 | 0 | int lnr; |
5439 | 0 | const char *err = NULL; |
5440 | 0 | struct para_data_s *para, *r; |
5441 | 0 | int i; |
5442 | 0 | struct output_control_s outctrl; |
5443 | |
|
5444 | 0 | memset( &outctrl, 0, sizeof( outctrl ) ); |
5445 | 0 | outctrl.pub.afx = new_armor_context (); |
5446 | |
|
5447 | 0 | if( !fname || !*fname) |
5448 | 0 | fname = "-"; |
5449 | |
|
5450 | 0 | fp = iobuf_open (fname); |
5451 | 0 | if (fp && is_secured_file (iobuf_get_fd (fp))) |
5452 | 0 | { |
5453 | 0 | iobuf_close (fp); |
5454 | 0 | fp = NULL; |
5455 | 0 | gpg_err_set_errno (EPERM); |
5456 | 0 | } |
5457 | 0 | if (!fp) { |
5458 | 0 | log_error (_("can't open '%s': %s\n"), fname, strerror(errno) ); |
5459 | 0 | return; |
5460 | 0 | } |
5461 | 0 | iobuf_ioctl (fp, IOBUF_IOCTL_NO_CACHE, 1, NULL); |
5462 | |
|
5463 | 0 | lnr = 0; |
5464 | 0 | err = NULL; |
5465 | 0 | para = NULL; |
5466 | 0 | maxlen = 1024; |
5467 | 0 | line = NULL; |
5468 | 0 | nline = 0; |
5469 | 0 | while ( iobuf_read_line (fp, &line, &nline, &maxlen) ) { |
5470 | 0 | char *keyword, *value; |
5471 | |
|
5472 | 0 | lnr++; |
5473 | 0 | if( !maxlen ) { |
5474 | 0 | err = "line too long"; |
5475 | 0 | break; |
5476 | 0 | } |
5477 | 0 | for( p = line; isspace(*(byte*)p); p++ ) |
5478 | 0 | ; |
5479 | 0 | if( !*p || *p == '#' ) |
5480 | 0 | continue; |
5481 | 0 | keyword = p; |
5482 | 0 | if( *keyword == '%' ) { |
5483 | 0 | for( ; !isspace(*(byte*)p); p++ ) |
5484 | 0 | ; |
5485 | 0 | if( *p ) |
5486 | 0 | *p++ = 0; |
5487 | 0 | for( ; isspace(*(byte*)p); p++ ) |
5488 | 0 | ; |
5489 | 0 | value = p; |
5490 | 0 | trim_trailing_ws( value, strlen(value) ); |
5491 | 0 | if( !ascii_strcasecmp( keyword, "%echo" ) ) |
5492 | 0 | log_info("%s\n", value ); |
5493 | 0 | else if( !ascii_strcasecmp( keyword, "%dry-run" ) ) |
5494 | 0 | outctrl.dryrun = 1; |
5495 | 0 | else if( !ascii_strcasecmp( keyword, "%ask-passphrase" ) ) |
5496 | 0 | ; /* Dummy for backward compatibility. */ |
5497 | 0 | else if( !ascii_strcasecmp( keyword, "%no-ask-passphrase" ) ) |
5498 | 0 | ; /* Dummy for backward compatibility. */ |
5499 | 0 | else if( !ascii_strcasecmp( keyword, "%no-protection" ) ) |
5500 | 0 | outctrl.keygen_flags |= KEYGEN_FLAG_NO_PROTECTION; |
5501 | 0 | else if( !ascii_strcasecmp( keyword, "%transient-key" ) ) |
5502 | 0 | outctrl.keygen_flags |= KEYGEN_FLAG_TRANSIENT_KEY; |
5503 | 0 | else if( !ascii_strcasecmp( keyword, "%commit" ) ) { |
5504 | 0 | outctrl.lnr = lnr; |
5505 | 0 | if (proc_parameter_file (ctrl, para, fname, &outctrl, 0 )) |
5506 | 0 | print_status_key_not_created |
5507 | 0 | (get_parameter_value (para, pHANDLE)); |
5508 | 0 | release_parameter_list( para ); |
5509 | 0 | para = NULL; |
5510 | 0 | } |
5511 | 0 | else if( !ascii_strcasecmp( keyword, "%pubring" ) ) { |
5512 | 0 | if( outctrl.pub.fname && !strcmp( outctrl.pub.fname, value ) ) |
5513 | 0 | ; /* still the same file - ignore it */ |
5514 | 0 | else { |
5515 | 0 | xfree( outctrl.pub.newfname ); |
5516 | 0 | outctrl.pub.newfname = xstrdup( value ); |
5517 | 0 | outctrl.use_files = 1; |
5518 | 0 | } |
5519 | 0 | } |
5520 | 0 | else if( !ascii_strcasecmp( keyword, "%secring" ) ) { |
5521 | | /* Ignore this command. */ |
5522 | 0 | } |
5523 | 0 | else |
5524 | 0 | log_info("skipping control '%s' (%s)\n", keyword, value ); |
5525 | | |
5526 | |
|
5527 | 0 | continue; |
5528 | 0 | } |
5529 | | |
5530 | | |
5531 | 0 | if( !(p = strchr( p, ':' )) || p == keyword ) { |
5532 | 0 | err = "missing colon"; |
5533 | 0 | break; |
5534 | 0 | } |
5535 | 0 | if( *p ) |
5536 | 0 | *p++ = 0; |
5537 | 0 | for( ; isspace(*(byte*)p); p++ ) |
5538 | 0 | ; |
5539 | 0 | if( !*p ) { |
5540 | 0 | err = "missing argument"; |
5541 | 0 | break; |
5542 | 0 | } |
5543 | 0 | value = p; |
5544 | 0 | trim_trailing_ws( value, strlen(value) ); |
5545 | |
|
5546 | 0 | for(i=0; keywords[i].name; i++ ) { |
5547 | 0 | if( !ascii_strcasecmp( keywords[i].name, keyword ) ) |
5548 | 0 | break; |
5549 | 0 | } |
5550 | 0 | if( !keywords[i].name ) { |
5551 | 0 | err = "unknown keyword"; |
5552 | 0 | break; |
5553 | 0 | } |
5554 | 0 | if( keywords[i].key != pKEYTYPE && !para ) { |
5555 | 0 | err = "parameter block does not start with \"Key-Type\""; |
5556 | 0 | break; |
5557 | 0 | } |
5558 | | |
5559 | 0 | if( keywords[i].key == pKEYTYPE && para ) { |
5560 | 0 | outctrl.lnr = lnr; |
5561 | 0 | if (proc_parameter_file (ctrl, para, fname, &outctrl, 0 )) |
5562 | 0 | print_status_key_not_created |
5563 | 0 | (get_parameter_value (para, pHANDLE)); |
5564 | 0 | release_parameter_list( para ); |
5565 | 0 | para = NULL; |
5566 | 0 | } |
5567 | 0 | else { |
5568 | 0 | for( r = para; r; r = r->next ) { |
5569 | 0 | if( r->key == keywords[i].key ) |
5570 | 0 | break; |
5571 | 0 | } |
5572 | 0 | if( r ) { |
5573 | 0 | err = "duplicate keyword"; |
5574 | 0 | break; |
5575 | 0 | } |
5576 | 0 | } |
5577 | | |
5578 | 0 | if ((keywords[i].key == pVERSION |
5579 | 0 | || keywords[i].key == pSUBVERSION)) |
5580 | 0 | ; /* Ignore version. */ |
5581 | 0 | else |
5582 | 0 | { |
5583 | 0 | r = xmalloc_clear( sizeof *r + strlen( value ) ); |
5584 | 0 | r->lnr = lnr; |
5585 | 0 | r->key = keywords[i].key; |
5586 | 0 | strcpy( r->u.value, value ); |
5587 | 0 | r->next = para; |
5588 | 0 | para = r; |
5589 | 0 | } |
5590 | 0 | } |
5591 | 0 | if( err ) |
5592 | 0 | log_error("%s:%d: %s\n", fname, lnr, err ); |
5593 | 0 | else if( iobuf_error (fp) ) { |
5594 | 0 | log_error("%s:%d: read error\n", fname, lnr); |
5595 | 0 | } |
5596 | 0 | else if( para ) { |
5597 | 0 | outctrl.lnr = lnr; |
5598 | 0 | if (proc_parameter_file (ctrl, para, fname, &outctrl, 0 )) |
5599 | 0 | print_status_key_not_created (get_parameter_value (para, pHANDLE)); |
5600 | 0 | } |
5601 | |
|
5602 | 0 | if( outctrl.use_files ) { /* close open streams */ |
5603 | 0 | iobuf_close( outctrl.pub.stream ); |
5604 | | |
5605 | | /* Must invalidate that ugly cache to actually close it. */ |
5606 | 0 | if (outctrl.pub.fname) |
5607 | 0 | iobuf_ioctl (NULL, IOBUF_IOCTL_INVALIDATE_CACHE, |
5608 | 0 | 0, (char*)outctrl.pub.fname); |
5609 | |
|
5610 | 0 | xfree( outctrl.pub.fname ); |
5611 | 0 | xfree( outctrl.pub.newfname ); |
5612 | 0 | } |
5613 | |
|
5614 | 0 | xfree (line); |
5615 | 0 | release_parameter_list( para ); |
5616 | 0 | iobuf_close (fp); |
5617 | 0 | release_armor_context (outctrl.pub.afx); |
5618 | 0 | } |
5619 | | |
5620 | | |
5621 | | /* Helper for quick_generate_keypair. */ |
5622 | | static struct para_data_s * |
5623 | | quickgen_set_para (struct para_data_s *para, int for_subkey, |
5624 | | int algo, int nbits, const char *curve, unsigned int use, |
5625 | | int version, const char *keygrip, u32 keytime) |
5626 | 0 | { |
5627 | 0 | struct para_data_s *r; |
5628 | |
|
5629 | 0 | r = xmalloc_clear (sizeof *r + 50); |
5630 | 0 | r->key = for_subkey? pSUBKEYUSAGE : pKEYUSAGE; |
5631 | 0 | if (use) |
5632 | 0 | snprintf (r->u.value, 30, "%s%s%s%s%s%s%s", |
5633 | 0 | (use & PUBKEY_USAGE_ENC)? "encr " : "", |
5634 | 0 | (use & PUBKEY_USAGE_SIG)? "sign " : "", |
5635 | 0 | (use & PUBKEY_USAGE_AUTH)? "auth " : "", |
5636 | 0 | (use & PUBKEY_USAGE_CERT)? "cert " : "", |
5637 | 0 | (use & PUBKEY_USAGE_RENC)? "renc " : "", |
5638 | 0 | (use & PUBKEY_USAGE_TIME)? "time " : "", |
5639 | 0 | (use & PUBKEY_USAGE_GROUP)?"group ": ""); |
5640 | 0 | else |
5641 | 0 | strcpy (r->u.value, for_subkey ? "encr" : "sign"); |
5642 | 0 | r->next = para; |
5643 | 0 | para = r; |
5644 | 0 | r = xmalloc_clear (sizeof *r + 20); |
5645 | 0 | r->key = for_subkey? pSUBKEYTYPE : pKEYTYPE; |
5646 | 0 | snprintf (r->u.value, 20, "%d", algo); |
5647 | 0 | r->next = para; |
5648 | 0 | para = r; |
5649 | |
|
5650 | 0 | if (keygrip) |
5651 | 0 | { |
5652 | 0 | r = xmalloc_clear (sizeof *r + strlen (keygrip)); |
5653 | 0 | r->key = for_subkey? pSUBKEYGRIP : pKEYGRIP; |
5654 | 0 | strcpy (r->u.value, keygrip); |
5655 | 0 | r->next = para; |
5656 | 0 | para = r; |
5657 | 0 | } |
5658 | 0 | else if (curve) |
5659 | 0 | { |
5660 | 0 | r = xmalloc_clear (sizeof *r + strlen (curve)); |
5661 | 0 | r->key = for_subkey? pSUBKEYCURVE : pKEYCURVE; |
5662 | 0 | strcpy (r->u.value, curve); |
5663 | 0 | r->next = para; |
5664 | 0 | para = r; |
5665 | 0 | } |
5666 | | |
5667 | | /* Always store the size - although not required for ECC it is |
5668 | | * required for composite algos. Should not harm anyway. */ |
5669 | 0 | r = xmalloc_clear (sizeof *r + 20); |
5670 | 0 | r->key = for_subkey? pSUBKEYLENGTH : pKEYLENGTH; |
5671 | 0 | sprintf (r->u.value, "%u", nbits); |
5672 | 0 | r->next = para; |
5673 | 0 | para = r; |
5674 | |
|
5675 | 0 | r = xmalloc_clear (sizeof *r + 20); |
5676 | 0 | r->key = for_subkey? pSUBVERSION : pVERSION; |
5677 | 0 | snprintf (r->u.value, 20, "%d", version); |
5678 | 0 | r->next = para; |
5679 | 0 | para = r; |
5680 | |
|
5681 | 0 | if (keytime) |
5682 | 0 | { |
5683 | 0 | r = xmalloc_clear (sizeof *r); |
5684 | 0 | r->key = for_subkey? pSUBKEYCREATIONDATE : pKEYCREATIONDATE; |
5685 | 0 | r->u.creation = keytime; |
5686 | 0 | r->next = para; |
5687 | 0 | para = r; |
5688 | |
|
5689 | 0 | } |
5690 | |
|
5691 | 0 | return para; |
5692 | 0 | } |
5693 | | |
5694 | | |
5695 | | /* |
5696 | | * Unattended generation of a standard key. |
5697 | | */ |
5698 | | void |
5699 | | quick_generate_keypair (ctrl_t ctrl, const char *uid, const char *algostr, |
5700 | | const char *usagestr, const char *expirestr) |
5701 | 0 | { |
5702 | 0 | gpg_error_t err; |
5703 | 0 | struct para_data_s *para = NULL; |
5704 | 0 | struct para_data_s *r; |
5705 | 0 | struct output_control_s outctrl; |
5706 | 0 | int use_tty; |
5707 | |
|
5708 | 0 | memset (&outctrl, 0, sizeof outctrl); |
5709 | |
|
5710 | 0 | use_tty = (!opt.batch && !opt.answer_yes |
5711 | 0 | && !*algostr && !*usagestr && !*expirestr |
5712 | 0 | && !cpr_enabled () |
5713 | 0 | && gnupg_isatty (fileno (stdin)) |
5714 | 0 | && gnupg_isatty (fileno (stdout)) |
5715 | 0 | && gnupg_isatty (fileno (stderr))); |
5716 | |
|
5717 | 0 | r = xmalloc_clear (sizeof *r + strlen (uid)); |
5718 | 0 | r->key = pUSERID; |
5719 | 0 | strcpy (r->u.value, uid); |
5720 | 0 | r->next = para; |
5721 | 0 | para = r; |
5722 | |
|
5723 | 0 | uid = trim_spaces (r->u.value); |
5724 | 0 | if (!*uid || (!opt.allow_freeform_uid && !is_valid_user_id (uid))) |
5725 | 0 | { |
5726 | 0 | log_error (_("Key generation failed: %s\n"), |
5727 | 0 | gpg_strerror (GPG_ERR_INV_USER_ID)); |
5728 | 0 | goto leave; |
5729 | 0 | } |
5730 | | |
5731 | | /* If gpg is directly used on the console ask whether a key with the |
5732 | | given user id shall really be created. */ |
5733 | 0 | if (use_tty) |
5734 | 0 | { |
5735 | 0 | tty_printf (_("About to create a key for:\n \"%s\"\n\n"), uid); |
5736 | 0 | if (!cpr_get_answer_is_yes_def ("quick_keygen.okay", |
5737 | 0 | _("Continue? (Y/n) "), 1)) |
5738 | 0 | goto leave; |
5739 | 0 | } |
5740 | | |
5741 | | /* Check whether such a user ID already exists. */ |
5742 | 0 | { |
5743 | 0 | KEYDB_HANDLE kdbhd; |
5744 | 0 | KEYDB_SEARCH_DESC desc; |
5745 | |
|
5746 | 0 | memset (&desc, 0, sizeof desc); |
5747 | 0 | desc.mode = KEYDB_SEARCH_MODE_EXACT; |
5748 | 0 | desc.u.name = uid; |
5749 | |
|
5750 | 0 | kdbhd = keydb_new (ctrl); |
5751 | 0 | if (!kdbhd) |
5752 | 0 | goto leave; |
5753 | | |
5754 | 0 | err = keydb_search (kdbhd, &desc, 1, NULL); |
5755 | 0 | keydb_release (kdbhd); |
5756 | 0 | if (gpg_err_code (err) != GPG_ERR_NOT_FOUND) |
5757 | 0 | { |
5758 | 0 | log_info (_("A key for \"%s\" already exists\n"), uid); |
5759 | 0 | if (opt.answer_yes) |
5760 | 0 | ; |
5761 | 0 | else if (!use_tty |
5762 | 0 | || !cpr_get_answer_is_yes_def ("quick_keygen.force", |
5763 | 0 | _("Create anyway? (y/N) "), 0)) |
5764 | 0 | { |
5765 | 0 | write_status_error ("genkey", gpg_error (304)); |
5766 | 0 | log_inc_errorcount (); /* we used log_info */ |
5767 | 0 | goto leave; |
5768 | 0 | } |
5769 | 0 | log_info (_("creating anyway\n")); |
5770 | 0 | } |
5771 | 0 | } |
5772 | | |
5773 | 0 | if (!*expirestr || strcmp (expirestr, "-") == 0) |
5774 | 0 | expirestr = default_expiration_interval; |
5775 | |
|
5776 | 0 | if ((!*algostr || !ascii_strcasecmp (algostr, "default") |
5777 | 0 | || !ascii_strcasecmp (algostr, "future-default") |
5778 | 0 | || !ascii_strcasecmp (algostr, "futuredefault") |
5779 | 0 | || !ascii_strcasecmp (algostr, "pqc") |
5780 | 0 | || (!ascii_strcasecmp (algostr, "pqc9980") && RFC9980) |
5781 | 0 | || !ascii_strcasecmp (algostr, "card")) |
5782 | 0 | && (!*usagestr || !ascii_strcasecmp (usagestr, "default") |
5783 | 0 | || !strcmp (usagestr, "-"))) |
5784 | 0 | { |
5785 | | /* Use default key parameters. */ |
5786 | 0 | int algo, subalgo, version, subversion; |
5787 | 0 | unsigned int size, subsize; |
5788 | 0 | unsigned int keyuse, subkeyuse; |
5789 | 0 | const char *curve, *subcurve; |
5790 | 0 | char *keygrip, *subkeygrip; |
5791 | 0 | u32 keytime, subkeytime; |
5792 | |
|
5793 | 0 | err = parse_key_parameter_string (ctrl, algostr, -1, 0, |
5794 | 0 | &algo, &size, &keyuse, &curve, &version, |
5795 | 0 | &keygrip, &keytime, |
5796 | 0 | &subalgo, &subsize, &subkeyuse, |
5797 | 0 | &subcurve, &subversion, |
5798 | 0 | &subkeygrip, &subkeytime); |
5799 | 0 | if (err) |
5800 | 0 | { |
5801 | 0 | log_error (_("Key generation failed: %s\n"), gpg_strerror (err)); |
5802 | 0 | goto leave; |
5803 | 0 | } |
5804 | | |
5805 | 0 | para = quickgen_set_para (para, 0, algo, size, curve, keyuse, version, |
5806 | 0 | keygrip, keytime); |
5807 | 0 | if (subalgo) |
5808 | 0 | para = quickgen_set_para (para, 1, |
5809 | 0 | subalgo, subsize, subcurve, subkeyuse, |
5810 | 0 | subversion, subkeygrip, subkeytime); |
5811 | 0 | if (*expirestr) |
5812 | 0 | { |
5813 | 0 | u32 expire; |
5814 | |
|
5815 | 0 | expire = parse_expire_string (expirestr); |
5816 | 0 | if (expire == (u32)-1 ) |
5817 | 0 | { |
5818 | 0 | err = gpg_error (GPG_ERR_INV_VALUE); |
5819 | 0 | log_error (_("Key generation failed: %s\n"), gpg_strerror (err)); |
5820 | 0 | goto leave; |
5821 | 0 | } |
5822 | 0 | r = xmalloc_clear (sizeof *r + 20); |
5823 | 0 | r->key = pKEYEXPIRE; |
5824 | 0 | r->u.expire = expire; |
5825 | 0 | r->next = para; |
5826 | 0 | para = r; |
5827 | 0 | } |
5828 | | |
5829 | 0 | xfree (keygrip); |
5830 | 0 | xfree (subkeygrip); |
5831 | 0 | } |
5832 | 0 | else |
5833 | 0 | { |
5834 | | /* Extended unattended mode. Creates only the primary key. */ |
5835 | 0 | int algo, version; |
5836 | 0 | unsigned int use; |
5837 | 0 | u32 expire; |
5838 | 0 | unsigned int nbits; |
5839 | 0 | const char *curve; |
5840 | 0 | char *keygrip; |
5841 | 0 | u32 keytime; |
5842 | |
|
5843 | 0 | err = parse_algo_usage_expire (ctrl, 0, algostr, usagestr, expirestr, |
5844 | 0 | &algo, &use, &expire, &nbits, &curve, |
5845 | 0 | &version, &keygrip, &keytime); |
5846 | 0 | if (err) |
5847 | 0 | { |
5848 | 0 | log_error (_("Key generation failed: %s\n"), gpg_strerror (err) ); |
5849 | 0 | goto leave; |
5850 | 0 | } |
5851 | | |
5852 | 0 | para = quickgen_set_para (para, 0, algo, nbits, curve, use, version, |
5853 | 0 | keygrip, keytime); |
5854 | 0 | r = xmalloc_clear (sizeof *r + 20); |
5855 | 0 | r->key = pKEYEXPIRE; |
5856 | 0 | r->u.expire = expire; |
5857 | 0 | r->next = para; |
5858 | 0 | para = r; |
5859 | |
|
5860 | 0 | xfree (keygrip); |
5861 | 0 | } |
5862 | | |
5863 | | /* If the pinentry loopback mode is not and we have a static |
5864 | | passphrase (i.e. set with --passphrase{,-fd,-file} while in batch |
5865 | | mode), we use that passphrase for the new key. */ |
5866 | 0 | if (opt.pinentry_mode != PINENTRY_MODE_LOOPBACK |
5867 | 0 | && have_static_passphrase ()) |
5868 | 0 | { |
5869 | 0 | const char *s = get_static_passphrase (); |
5870 | |
|
5871 | 0 | r = xmalloc_clear (sizeof *r + strlen (s)); |
5872 | 0 | r->key = pPASSPHRASE; |
5873 | 0 | strcpy (r->u.value, s); |
5874 | 0 | r->next = para; |
5875 | 0 | para = r; |
5876 | 0 | } |
5877 | |
|
5878 | 0 | if (!ascii_strcasecmp (algostr, "card") |
5879 | 0 | || !ascii_strncasecmp (algostr, "card/", 5)) |
5880 | 0 | { |
5881 | 0 | r = xmalloc_clear (sizeof *r); |
5882 | 0 | r->key = pCARDKEY; |
5883 | 0 | r->u.abool = 1; |
5884 | 0 | r->next = para; |
5885 | 0 | para = r; |
5886 | 0 | } |
5887 | |
|
5888 | 0 | proc_parameter_file (ctrl, para, "[internal]", &outctrl, 0); |
5889 | |
|
5890 | 0 | leave: |
5891 | 0 | release_parameter_list (para); |
5892 | 0 | } |
5893 | | |
5894 | | |
5895 | | /* |
5896 | | * Generate a keypair (fname is only used in batch mode) If |
5897 | | * CARD_SERIALNO is not NULL the function will create the keys on an |
5898 | | * OpenPGP Card. If GENFLAGS has the GENERATE_KEYPAIR_CARDBACKUP bit |
5899 | | * set and CARD_SERIALNO is NOT NULL, the encryption key for the card |
5900 | | * is generated on the host, imported to the card and a backup file |
5901 | | * created by gpg-agent. If GENFLAGS has the GENERATE_KEYPAIR_FULL |
5902 | | * bit cleared only the basic prompts are used (except for batch |
5903 | | * mode). |
5904 | | */ |
5905 | | void |
5906 | | generate_keypair (ctrl_t ctrl, const char *fname, |
5907 | | const char *card_serialno, unsigned int genflags) |
5908 | 0 | { |
5909 | 0 | gpg_error_t err; |
5910 | 0 | unsigned int nbits; |
5911 | 0 | char *uid = NULL; |
5912 | 0 | int algo; |
5913 | 0 | unsigned int use; |
5914 | 0 | int both = 0; |
5915 | 0 | u32 expire; |
5916 | 0 | struct para_data_s *para = NULL; |
5917 | 0 | struct para_data_s *r; |
5918 | 0 | struct output_control_s outctrl; |
5919 | |
|
5920 | 0 | memset( &outctrl, 0, sizeof( outctrl ) ); |
5921 | |
|
5922 | 0 | if (opt.batch && card_serialno) |
5923 | 0 | { |
5924 | | /* We don't yet support unattended key generation with a card |
5925 | | * serial number. */ |
5926 | 0 | log_error (_("can't do this in batch mode\n")); |
5927 | 0 | print_further_info ("key generation with card serial number"); |
5928 | 0 | return; |
5929 | 0 | } |
5930 | | |
5931 | 0 | if (opt.batch) |
5932 | 0 | { |
5933 | 0 | read_parameter_file (ctrl, fname); |
5934 | 0 | return; |
5935 | 0 | } |
5936 | | |
5937 | 0 | if (card_serialno) |
5938 | 0 | { |
5939 | | #ifdef ENABLE_CARD_SUPPORT |
5940 | | struct agent_card_info_s info; |
5941 | | |
5942 | | memset (&info, 0, sizeof (info)); |
5943 | | err = agent_scd_getattr ("KEY-ATTR", &info); |
5944 | | if (err) |
5945 | | { |
5946 | | log_error (_("error getting current key info: %s\n"), |
5947 | | gpg_strerror (err)); |
5948 | | return; |
5949 | | } |
5950 | | |
5951 | | r = xcalloc (1, sizeof *r + strlen (card_serialno) ); |
5952 | | r->key = pSERIALNO; |
5953 | | strcpy( r->u.value, card_serialno); |
5954 | | r->next = para; |
5955 | | para = r; |
5956 | | |
5957 | | r = xcalloc (1, sizeof *r + 20 ); |
5958 | | r->key = pKEYTYPE; |
5959 | | sprintf( r->u.value, "%d", info.key_attr[0].algo ); |
5960 | | r->next = para; |
5961 | | para = r; |
5962 | | r = xcalloc (1, sizeof *r + 20 ); |
5963 | | r->key = pKEYUSAGE; |
5964 | | strcpy (r->u.value, "sign"); |
5965 | | r->next = para; |
5966 | | para = r; |
5967 | | |
5968 | | if (!(genflags & GENERATE_KEYPAIR_CARDPRIMARY)) |
5969 | | { |
5970 | | r = xcalloc (1, sizeof *r + 20 ); |
5971 | | r->key = pSUBKEYTYPE; |
5972 | | sprintf( r->u.value, "%d", info.key_attr[1].algo ); |
5973 | | r->next = para; |
5974 | | para = r; |
5975 | | r = xcalloc (1, sizeof *r + 20 ); |
5976 | | r->key = pSUBKEYUSAGE; |
5977 | | strcpy (r->u.value, "encrypt"); |
5978 | | r->next = para; |
5979 | | para = r; |
5980 | | if (info.key_attr[1].algo == PUBKEY_ALGO_RSA) |
5981 | | { |
5982 | | r = xcalloc (1, sizeof *r + 20 ); |
5983 | | r->key = pSUBKEYLENGTH; |
5984 | | sprintf( r->u.value, "%u", info.key_attr[1].nbits); |
5985 | | r->next = para; |
5986 | | para = r; |
5987 | | } |
5988 | | else if (info.key_attr[1].algo == PUBKEY_ALGO_ECDSA |
5989 | | || info.key_attr[1].algo == PUBKEY_ALGO_EDDSA |
5990 | | || info.key_attr[1].algo == PUBKEY_ALGO_ECDH) |
5991 | | { |
5992 | | r = xcalloc (1, sizeof *r + strlen (info.key_attr[1].curve)); |
5993 | | r->key = pSUBKEYCURVE; |
5994 | | strcpy (r->u.value, info.key_attr[1].curve); |
5995 | | r->next = para; |
5996 | | para = r; |
5997 | | } |
5998 | | |
5999 | | r = xcalloc (1, sizeof *r + 20 ); |
6000 | | r->key = pAUTHKEYTYPE; |
6001 | | sprintf( r->u.value, "%d", info.key_attr[2].algo ); |
6002 | | r->next = para; |
6003 | | para = r; |
6004 | | |
6005 | | if ((genflags & GENERATE_KEYPAIR_CARDBACKUP)) |
6006 | | { |
6007 | | r = xcalloc (1, sizeof *r + 1); |
6008 | | r->key = pCARDBACKUPKEY; |
6009 | | strcpy (r->u.value, "1"); |
6010 | | r->next = para; |
6011 | | para = r; |
6012 | | } |
6013 | | } |
6014 | | #endif /*ENABLE_CARD_SUPPORT*/ |
6015 | 0 | } |
6016 | 0 | else if ((genflags & GENERATE_KEYPAIR_FULL)) |
6017 | 0 | { |
6018 | | /* This is the full featured key generation. */ |
6019 | 0 | int subkey_algo; |
6020 | 0 | char *key_from_hexgrip = NULL; |
6021 | 0 | int cardkey; |
6022 | 0 | u32 keytime; |
6023 | |
|
6024 | 0 | algo = ask_algo (ctrl, 0, &subkey_algo, &use, |
6025 | 0 | &key_from_hexgrip, &cardkey, &keytime); |
6026 | 0 | if (key_from_hexgrip) |
6027 | 0 | { |
6028 | 0 | r = xmalloc_clear( sizeof *r + 20 ); |
6029 | 0 | r->key = pKEYTYPE; |
6030 | 0 | sprintf( r->u.value, "%d", algo); |
6031 | 0 | r->next = para; |
6032 | 0 | para = r; |
6033 | |
|
6034 | 0 | if (use) |
6035 | 0 | { |
6036 | 0 | r = xmalloc_clear( sizeof *r + 25 ); |
6037 | 0 | r->key = pKEYUSAGE; |
6038 | 0 | sprintf( r->u.value, "%s%s%s", |
6039 | 0 | (use & PUBKEY_USAGE_SIG)? "sign ":"", |
6040 | 0 | (use & PUBKEY_USAGE_ENC)? "encrypt ":"", |
6041 | 0 | (use & PUBKEY_USAGE_AUTH)? "auth":"" ); |
6042 | 0 | r->next = para; |
6043 | 0 | para = r; |
6044 | 0 | } |
6045 | |
|
6046 | 0 | r = xmalloc_clear( sizeof *r + 40 ); |
6047 | 0 | r->key = pKEYGRIP; |
6048 | 0 | strcpy (r->u.value, key_from_hexgrip); |
6049 | 0 | r->next = para; |
6050 | 0 | para = r; |
6051 | |
|
6052 | 0 | r = xmalloc_clear (sizeof *r); |
6053 | 0 | r->key = pCARDKEY; |
6054 | 0 | r->u.abool = cardkey; |
6055 | 0 | r->next = para; |
6056 | 0 | para = r; |
6057 | |
|
6058 | 0 | if (cardkey) |
6059 | 0 | { |
6060 | 0 | r = xmalloc_clear (sizeof *r); |
6061 | 0 | r->key = pKEYCREATIONDATE; |
6062 | 0 | r->u.creation = keytime; |
6063 | 0 | r->next = para; |
6064 | 0 | para = r; |
6065 | 0 | } |
6066 | |
|
6067 | 0 | xfree (key_from_hexgrip); |
6068 | 0 | } |
6069 | 0 | else |
6070 | 0 | { |
6071 | 0 | const char *curve = NULL; |
6072 | |
|
6073 | 0 | if (algo == PUBKEY_ALGO_ECDSA && subkey_algo == PUBKEY_ALGO_KYBER) |
6074 | 0 | { |
6075 | | /* Create primary and subkey at once. */ |
6076 | 0 | const char *subalgostr; |
6077 | 0 | const char *s; |
6078 | 0 | const char *pricurve; |
6079 | 0 | int prialgo = PUBKEY_ALGO_ECDSA; |
6080 | |
|
6081 | 0 | both = 1; |
6082 | 0 | subalgostr = ask_kyber_variant (); |
6083 | 0 | if (!subalgostr) /* Should not happen. */ |
6084 | 0 | subalgostr = PQC_STD_KEY_PARAM_SUB; |
6085 | | |
6086 | | /* Determine the primary key algo from the subkey algo. */ |
6087 | 0 | if (strstr (subalgostr, "bp384")) |
6088 | 0 | pricurve = "brainpoolP384r1"; |
6089 | 0 | else if (strstr (subalgostr, "bp256")) |
6090 | 0 | pricurve = "brainpoolP256r1"; |
6091 | 0 | else if (strstr (subalgostr, "cv448")) |
6092 | 0 | { |
6093 | 0 | pricurve = "Ed448"; |
6094 | 0 | prialgo = PUBKEY_ALGO_EDDSA; |
6095 | 0 | } |
6096 | 0 | else |
6097 | 0 | { |
6098 | 0 | pricurve = "Ed25519"; |
6099 | 0 | prialgo = PUBKEY_ALGO_EDDSA; |
6100 | 0 | } |
6101 | |
|
6102 | 0 | r = xmalloc_clear (sizeof *r + 20); |
6103 | 0 | r->key = pKEYTYPE; |
6104 | 0 | sprintf (r->u.value, "%d", prialgo); |
6105 | 0 | r->next = para; |
6106 | 0 | para = r; |
6107 | |
|
6108 | 0 | r = xmalloc_clear (sizeof *r + strlen (pricurve)); |
6109 | 0 | r->key = pKEYCURVE; |
6110 | 0 | strcpy (r->u.value, pricurve); |
6111 | 0 | r->next = para; |
6112 | 0 | para = r; |
6113 | |
|
6114 | 0 | r = xmalloc_clear (sizeof *r + 20); |
6115 | 0 | r->key = pKEYUSAGE; |
6116 | 0 | strcpy (r->u.value, "sign"); |
6117 | 0 | r->next = para; |
6118 | 0 | para = r; |
6119 | |
|
6120 | 0 | r = xmalloc_clear (sizeof *r + 20); |
6121 | 0 | r->key = pSUBKEYTYPE; |
6122 | 0 | sprintf (r->u.value, "%d", PUBKEY_ALGO_KYBER); |
6123 | 0 | r->next = para; |
6124 | 0 | para = r; |
6125 | |
|
6126 | 0 | r = xmalloc_clear (sizeof *r + 20); |
6127 | 0 | r->key = pSUBKEYLENGTH; |
6128 | 0 | sprintf (r->u.value, "%u", |
6129 | 0 | strstr (subalgostr, "768_")? 768 : 1024); |
6130 | 0 | r->next = para; |
6131 | 0 | para = r; |
6132 | |
|
6133 | 0 | s = strchr (subalgostr, '_'); |
6134 | 0 | log_assert (s && s[1]); |
6135 | 0 | s++; |
6136 | 0 | r = xmalloc_clear (sizeof *r + strlen (s)); |
6137 | 0 | r->key = pSUBKEYCURVE; |
6138 | 0 | strcpy (r->u.value, s); |
6139 | 0 | r->next = para; |
6140 | 0 | para = r; |
6141 | |
|
6142 | 0 | r = xmalloc_clear (sizeof *r + 20); |
6143 | 0 | r->key = pSUBKEYUSAGE; |
6144 | 0 | strcpy( r->u.value, "encrypt" ); |
6145 | 0 | r->next = para; |
6146 | 0 | para = r; |
6147 | 0 | } |
6148 | 0 | else if (subkey_algo) |
6149 | 0 | { |
6150 | | /* Create primary and subkey at once. */ |
6151 | 0 | both = 1; |
6152 | 0 | if (algo == PUBKEY_ALGO_ECDSA |
6153 | 0 | || algo == PUBKEY_ALGO_EDDSA |
6154 | 0 | || algo == PUBKEY_ALGO_ECDH) |
6155 | 0 | { |
6156 | 0 | curve = ask_curve (&algo, &subkey_algo, NULL); |
6157 | 0 | r = xmalloc_clear( sizeof *r + 20 ); |
6158 | 0 | r->key = pKEYTYPE; |
6159 | 0 | sprintf( r->u.value, "%d", algo); |
6160 | 0 | r->next = para; |
6161 | 0 | para = r; |
6162 | 0 | nbits = 0; |
6163 | 0 | r = xmalloc_clear (sizeof *r + strlen (curve)); |
6164 | 0 | r->key = pKEYCURVE; |
6165 | 0 | strcpy (r->u.value, curve); |
6166 | 0 | r->next = para; |
6167 | 0 | para = r; |
6168 | 0 | if (!strcmp (curve, "X448") || !strcmp (curve, "Ed448")) |
6169 | 0 | { |
6170 | 0 | r = xmalloc_clear (sizeof *r + 20); |
6171 | 0 | r->key = pVERSION; |
6172 | 0 | snprintf (r->u.value, 20, "%d", 5); |
6173 | 0 | r->next = para; |
6174 | 0 | para = r; |
6175 | 0 | } |
6176 | 0 | } |
6177 | 0 | else |
6178 | 0 | { |
6179 | 0 | r = xmalloc_clear( sizeof *r + 20 ); |
6180 | 0 | r->key = pKEYTYPE; |
6181 | 0 | sprintf( r->u.value, "%d", algo); |
6182 | 0 | r->next = para; |
6183 | 0 | para = r; |
6184 | 0 | nbits = ask_keysize (algo, 0); |
6185 | 0 | r = xmalloc_clear( sizeof *r + 20 ); |
6186 | 0 | r->key = pKEYLENGTH; |
6187 | 0 | sprintf( r->u.value, "%u", nbits); |
6188 | 0 | r->next = para; |
6189 | 0 | para = r; |
6190 | 0 | } |
6191 | 0 | r = xmalloc_clear( sizeof *r + 20 ); |
6192 | 0 | r->key = pKEYUSAGE; |
6193 | 0 | strcpy( r->u.value, "sign" ); |
6194 | 0 | r->next = para; |
6195 | 0 | para = r; |
6196 | |
|
6197 | 0 | r = xmalloc_clear( sizeof *r + 20 ); |
6198 | 0 | r->key = pSUBKEYTYPE; |
6199 | 0 | sprintf( r->u.value, "%d", subkey_algo); |
6200 | 0 | r->next = para; |
6201 | 0 | para = r; |
6202 | 0 | r = xmalloc_clear( sizeof *r + 20 ); |
6203 | 0 | r->key = pSUBKEYUSAGE; |
6204 | 0 | strcpy( r->u.value, "encrypt" ); |
6205 | 0 | r->next = para; |
6206 | 0 | para = r; |
6207 | |
|
6208 | 0 | if (algo == PUBKEY_ALGO_ECDSA |
6209 | 0 | || algo == PUBKEY_ALGO_EDDSA |
6210 | 0 | || algo == PUBKEY_ALGO_ECDH) |
6211 | 0 | { |
6212 | 0 | if (algo == PUBKEY_ALGO_EDDSA |
6213 | 0 | && subkey_algo == PUBKEY_ALGO_ECDH) |
6214 | 0 | { |
6215 | | /* Need to switch to a different curve for the |
6216 | | encryption key. */ |
6217 | 0 | if (!strcmp (curve, "Ed25519")) |
6218 | 0 | curve = "Curve25519"; |
6219 | 0 | else |
6220 | 0 | { |
6221 | 0 | curve = "X448"; |
6222 | 0 | r = xmalloc_clear (sizeof *r + 20); |
6223 | 0 | r->key = pSUBVERSION; |
6224 | 0 | snprintf (r->u.value, 20, "%d", 5); |
6225 | 0 | r->next = para; |
6226 | 0 | para = r; |
6227 | 0 | } |
6228 | 0 | } |
6229 | 0 | r = xmalloc_clear (sizeof *r + strlen (curve)); |
6230 | 0 | r->key = pSUBKEYCURVE; |
6231 | 0 | strcpy (r->u.value, curve); |
6232 | 0 | r->next = para; |
6233 | 0 | para = r; |
6234 | 0 | } |
6235 | 0 | } |
6236 | 0 | else /* Create only a single key. */ |
6237 | 0 | { |
6238 | | /* For ECC we need to ask for the curve before storing the |
6239 | | algo because ask_curve may change the algo. */ |
6240 | 0 | if (algo == PUBKEY_ALGO_ECDSA |
6241 | 0 | || algo == PUBKEY_ALGO_EDDSA |
6242 | 0 | || algo == PUBKEY_ALGO_ECDH) |
6243 | 0 | { |
6244 | 0 | curve = ask_curve (&algo, NULL, NULL); |
6245 | 0 | r = xmalloc_clear (sizeof *r + strlen (curve)); |
6246 | 0 | r->key = pKEYCURVE; |
6247 | 0 | strcpy (r->u.value, curve); |
6248 | 0 | r->next = para; |
6249 | 0 | para = r; |
6250 | 0 | if (!strcmp (curve, "X448") || !strcmp (curve, "Ed448")) |
6251 | 0 | { |
6252 | 0 | r = xmalloc_clear (sizeof *r + 20); |
6253 | 0 | r->key = pVERSION; |
6254 | 0 | snprintf (r->u.value, 20, "%d", 5); |
6255 | 0 | r->next = para; |
6256 | 0 | para = r; |
6257 | 0 | } |
6258 | 0 | } |
6259 | |
|
6260 | 0 | r = xmalloc_clear( sizeof *r + 20 ); |
6261 | 0 | r->key = pKEYTYPE; |
6262 | 0 | sprintf( r->u.value, "%d", algo ); |
6263 | 0 | r->next = para; |
6264 | 0 | para = r; |
6265 | |
|
6266 | 0 | if (use) |
6267 | 0 | { |
6268 | 0 | r = xmalloc_clear( sizeof *r + 25 ); |
6269 | 0 | r->key = pKEYUSAGE; |
6270 | 0 | sprintf( r->u.value, "%s%s%s", |
6271 | 0 | (use & PUBKEY_USAGE_SIG)? "sign ":"", |
6272 | 0 | (use & PUBKEY_USAGE_ENC)? "encrypt ":"", |
6273 | 0 | (use & PUBKEY_USAGE_AUTH)? "auth":"" ); |
6274 | 0 | r->next = para; |
6275 | 0 | para = r; |
6276 | 0 | } |
6277 | 0 | nbits = 0; |
6278 | 0 | } |
6279 | | |
6280 | 0 | if (algo == PUBKEY_ALGO_ECDSA |
6281 | 0 | || algo == PUBKEY_ALGO_EDDSA |
6282 | 0 | || algo == PUBKEY_ALGO_ECDH) |
6283 | 0 | { |
6284 | | /* The curve has already been set. */ |
6285 | 0 | } |
6286 | 0 | else |
6287 | 0 | { |
6288 | 0 | nbits = ask_keysize (both? subkey_algo : algo, nbits); |
6289 | 0 | r = xmalloc_clear( sizeof *r + 20 ); |
6290 | 0 | r->key = both? pSUBKEYLENGTH : pKEYLENGTH; |
6291 | 0 | sprintf( r->u.value, "%u", nbits); |
6292 | 0 | r->next = para; |
6293 | 0 | para = r; |
6294 | 0 | } |
6295 | 0 | } |
6296 | 0 | } |
6297 | 0 | else /* Default key generation. */ |
6298 | 0 | { |
6299 | 0 | int subalgo, version, subversion; |
6300 | 0 | unsigned int size, subsize; |
6301 | 0 | unsigned int keyuse, subkeyuse; |
6302 | 0 | const char *curve, *subcurve; |
6303 | 0 | char *keygrip, *subkeygrip; |
6304 | 0 | u32 keytime, subkeytime; |
6305 | |
|
6306 | 0 | tty_printf ( _("Note: Use \"%s %s\"" |
6307 | 0 | " for a full featured key generation dialog.\n"), |
6308 | 0 | GPG_NAME |
6309 | 0 | , "--full-generate-key" ); |
6310 | |
|
6311 | 0 | err = parse_key_parameter_string (ctrl, NULL, -1, 0, |
6312 | 0 | &algo, &size, &keyuse, &curve, &version, |
6313 | 0 | &keygrip, &keytime, |
6314 | 0 | &subalgo, &subsize, |
6315 | 0 | &subkeyuse, &subcurve, &subversion, |
6316 | 0 | &subkeygrip, &subkeytime); |
6317 | 0 | if (err) |
6318 | 0 | { |
6319 | 0 | log_error (_("Key generation failed: %s\n"), gpg_strerror (err)); |
6320 | 0 | return; |
6321 | 0 | } |
6322 | 0 | para = quickgen_set_para (para, 0, |
6323 | 0 | algo, size, curve, keyuse, |
6324 | 0 | version, keygrip, keytime); |
6325 | 0 | if (subalgo) |
6326 | 0 | para = quickgen_set_para (para, 1, |
6327 | 0 | subalgo, subsize, subcurve, subkeyuse, |
6328 | 0 | subversion, subkeygrip, subkeytime); |
6329 | |
|
6330 | 0 | xfree (keygrip); |
6331 | 0 | xfree (subkeygrip); |
6332 | 0 | } |
6333 | | |
6334 | | |
6335 | 0 | expire = (genflags & GENERATE_KEYPAIR_FULL)? ask_expire_interval (0, NULL) |
6336 | 0 | : parse_expire_string (default_expiration_interval); |
6337 | 0 | r = xcalloc (1, sizeof *r + 20); |
6338 | 0 | r->key = pKEYEXPIRE; |
6339 | 0 | r->u.expire = expire; |
6340 | 0 | r->next = para; |
6341 | 0 | para = r; |
6342 | 0 | r = xcalloc (1, sizeof *r + 20); |
6343 | 0 | r->key = pSUBKEYEXPIRE; |
6344 | 0 | r->u.expire = expire; |
6345 | 0 | r->next = para; |
6346 | 0 | para = r; |
6347 | |
|
6348 | 0 | uid = ask_user_id (0, !!(genflags & GENERATE_KEYPAIR_FULL), NULL); |
6349 | 0 | if (!uid) |
6350 | 0 | { |
6351 | 0 | log_error(_("Key generation canceled.\n")); |
6352 | 0 | release_parameter_list( para ); |
6353 | 0 | return; |
6354 | 0 | } |
6355 | 0 | r = xcalloc (1, sizeof *r + strlen (uid)); |
6356 | 0 | r->key = pUSERID; |
6357 | 0 | strcpy (r->u.value, uid); |
6358 | 0 | r->next = para; |
6359 | 0 | para = r; |
6360 | |
|
6361 | 0 | proc_parameter_file (ctrl, para, "[internal]", &outctrl, !!card_serialno); |
6362 | 0 | release_parameter_list (para); |
6363 | 0 | } |
6364 | | |
6365 | | |
6366 | | /* Create and delete a dummy packet to start off a list of kbnodes. */ |
6367 | | static void |
6368 | | start_tree(KBNODE *tree) |
6369 | 0 | { |
6370 | 0 | PACKET *pkt; |
6371 | |
|
6372 | 0 | pkt=xmalloc_clear(sizeof(*pkt)); |
6373 | 0 | pkt->pkttype=PKT_NONE; |
6374 | 0 | *tree=new_kbnode(pkt); |
6375 | 0 | delete_kbnode(*tree); |
6376 | 0 | } |
6377 | | |
6378 | | |
6379 | | /* Write the *protected* secret key to the file. */ |
6380 | | static gpg_error_t |
6381 | | card_write_key_to_backup_file (PKT_public_key *sk, const char *backup_dir) |
6382 | 0 | { |
6383 | 0 | gpg_error_t err = 0; |
6384 | 0 | char keyid_buffer[2 * 8 + 1]; |
6385 | 0 | char name_buffer[50]; |
6386 | 0 | char *fname; |
6387 | 0 | IOBUF fp; |
6388 | 0 | mode_t oldmask; |
6389 | 0 | PACKET *pkt = NULL; |
6390 | |
|
6391 | 0 | format_keyid (pk_keyid (sk), KF_LONG, keyid_buffer, sizeof (keyid_buffer)); |
6392 | 0 | snprintf (name_buffer, sizeof name_buffer, "sk_%s.gpg", keyid_buffer); |
6393 | |
|
6394 | 0 | fname = make_filename (backup_dir, name_buffer, NULL); |
6395 | | /* Note that the umask call is not anymore needed because |
6396 | | iobuf_create now takes care of it. However, it does not harm |
6397 | | and thus we keep it. */ |
6398 | 0 | oldmask = umask (077); |
6399 | 0 | if (is_secured_filename (fname)) |
6400 | 0 | { |
6401 | 0 | fp = NULL; |
6402 | 0 | gpg_err_set_errno (EPERM); |
6403 | 0 | } |
6404 | 0 | else |
6405 | 0 | fp = iobuf_create (fname, 1); |
6406 | 0 | umask (oldmask); |
6407 | 0 | if (!fp) |
6408 | 0 | { |
6409 | 0 | err = gpg_error_from_syserror (); |
6410 | 0 | log_error (_("can't create backup file '%s': %s\n"), fname, strerror (errno) ); |
6411 | 0 | goto leave; |
6412 | 0 | } |
6413 | | |
6414 | 0 | pkt = xcalloc (1, sizeof *pkt); |
6415 | 0 | pkt->pkttype = PKT_SECRET_KEY; |
6416 | 0 | pkt->pkt.secret_key = sk; |
6417 | |
|
6418 | 0 | err = build_packet (fp, pkt); |
6419 | 0 | if (err) |
6420 | 0 | { |
6421 | 0 | log_error ("build packet failed: %s\n", gpg_strerror (err)); |
6422 | 0 | iobuf_cancel (fp); |
6423 | 0 | } |
6424 | 0 | else |
6425 | 0 | { |
6426 | 0 | char *fprbuf; |
6427 | |
|
6428 | 0 | iobuf_close (fp); |
6429 | 0 | iobuf_ioctl (NULL, IOBUF_IOCTL_INVALIDATE_CACHE, 0, (char*)fname); |
6430 | 0 | log_info (_("Note: backup of card key saved to '%s'\n"), fname); |
6431 | |
|
6432 | 0 | fprbuf = hexfingerprint (sk, NULL, 0); |
6433 | 0 | if (!fprbuf) |
6434 | 0 | { |
6435 | 0 | err = gpg_error_from_syserror (); |
6436 | 0 | goto leave; |
6437 | 0 | } |
6438 | 0 | write_status_text_and_buffer (STATUS_BACKUP_KEY_CREATED, fprbuf, |
6439 | 0 | fname, strlen (fname), 0); |
6440 | 0 | xfree (fprbuf); |
6441 | 0 | } |
6442 | | |
6443 | 0 | leave: |
6444 | 0 | xfree (pkt); |
6445 | 0 | xfree (fname); |
6446 | 0 | return err; |
6447 | 0 | } |
6448 | | |
6449 | | |
6450 | | /* Store key to card and make a backup file in OpenPGP format. */ |
6451 | | static gpg_error_t |
6452 | | card_store_key_with_backup (ctrl_t ctrl, PKT_public_key *sub_psk, |
6453 | | const char *backup_dir) |
6454 | 0 | { |
6455 | 0 | gpg_error_t err; |
6456 | 0 | PKT_public_key *sk; |
6457 | 0 | gnupg_isotime_t timestamp; |
6458 | 0 | char *hexgrip = NULL; |
6459 | 0 | struct agent_card_info_s info; |
6460 | 0 | gcry_cipher_hd_t cipherhd = NULL; |
6461 | 0 | char *cache_nonce = NULL; |
6462 | 0 | void *kek = NULL; |
6463 | 0 | size_t keklen; |
6464 | 0 | char *ecdh_param_str = NULL; |
6465 | 0 | int key_is_on_card = 0; |
6466 | |
|
6467 | 0 | memset (&info, 0, sizeof (info)); |
6468 | |
|
6469 | 0 | sk = copy_public_key (NULL, sub_psk); |
6470 | 0 | if (!sk) |
6471 | 0 | { |
6472 | 0 | err = gpg_error_from_syserror (); |
6473 | 0 | goto leave; |
6474 | 0 | } |
6475 | | |
6476 | 0 | epoch2isotime (timestamp, (time_t)sk->timestamp); |
6477 | 0 | if (sk->pubkey_algo == PUBKEY_ALGO_ECDH) |
6478 | 0 | { |
6479 | 0 | ecdh_param_str = ecdh_param_str_from_pk (sk); |
6480 | 0 | if (!ecdh_param_str) |
6481 | 0 | { |
6482 | 0 | err = gpg_error_from_syserror (); |
6483 | 0 | goto leave; |
6484 | 0 | } |
6485 | 0 | } |
6486 | | |
6487 | 0 | err = hexkeygrip_from_pk (sk, &hexgrip); |
6488 | 0 | if (err) |
6489 | 0 | goto leave; |
6490 | | |
6491 | 0 | err = agent_scd_getattr ("SERIALNO", &info); |
6492 | 0 | if (err) |
6493 | 0 | goto leave; |
6494 | | |
6495 | 0 | err = agent_keytocard (hexgrip, 2, 1, info.serialno, |
6496 | 0 | timestamp, ecdh_param_str); |
6497 | 0 | if (err) |
6498 | 0 | goto leave; |
6499 | | |
6500 | 0 | key_is_on_card = 1; |
6501 | 0 | err = agent_keywrap_key (ctrl, 1, &kek, &keklen); |
6502 | 0 | if (err) |
6503 | 0 | { |
6504 | 0 | log_error ("error getting the KEK: %s\n", gpg_strerror (err)); |
6505 | 0 | goto leave; |
6506 | 0 | } |
6507 | | |
6508 | 0 | err = gcry_cipher_open (&cipherhd, GCRY_CIPHER_AES128, |
6509 | 0 | GCRY_CIPHER_MODE_AESWRAP, 0); |
6510 | 0 | if (!err) |
6511 | 0 | err = gcry_cipher_setkey (cipherhd, kek, keklen); |
6512 | 0 | if (err) |
6513 | 0 | { |
6514 | 0 | log_error ("error setting up an encryption context: %s\n", |
6515 | 0 | gpg_strerror (err)); |
6516 | 0 | goto leave; |
6517 | 0 | } |
6518 | | |
6519 | 0 | err = receive_seckey_from_agent (ctrl, cipherhd, 0, 0, 0, |
6520 | 0 | &cache_nonce, hexgrip, sk, NULL); |
6521 | 0 | if (err) |
6522 | 0 | { |
6523 | 0 | log_error ("error getting secret key from agent: %s\n", |
6524 | 0 | gpg_strerror (err)); |
6525 | 0 | goto leave; |
6526 | 0 | } |
6527 | | |
6528 | 0 | err = card_write_key_to_backup_file (sk, backup_dir); |
6529 | 0 | if (err) |
6530 | 0 | log_error ("writing card key to backup file: %s\n", gpg_strerror (err)); |
6531 | 0 | else |
6532 | 0 | { |
6533 | | /* Remove secret key data in agent side. */ |
6534 | 0 | agent_scd_learn (NULL, 1); |
6535 | 0 | } |
6536 | |
|
6537 | 0 | leave: |
6538 | 0 | if (err && key_is_on_card) |
6539 | 0 | { |
6540 | 0 | tty_printf (_( |
6541 | 0 | "Warning: Although the key has been written to the card, a backup file was\n" |
6542 | 0 | " not properly written to the disk. You may want to repeat the\n" |
6543 | 0 | " entire operation or just create a new encryption key on the card.\n" |
6544 | 0 | )); |
6545 | 0 | } |
6546 | |
|
6547 | 0 | xfree (info.serialno); |
6548 | 0 | xfree (ecdh_param_str); |
6549 | 0 | xfree (cache_nonce); |
6550 | 0 | gcry_cipher_close (cipherhd); |
6551 | 0 | xfree (kek); |
6552 | 0 | xfree (hexgrip); |
6553 | 0 | free_public_key (sk); |
6554 | 0 | return err; |
6555 | 0 | } |
6556 | | |
6557 | | |
6558 | | static void |
6559 | | do_generate_keypair (ctrl_t ctrl, struct para_data_s *para, |
6560 | | struct output_control_s *outctrl, int card) |
6561 | 0 | { |
6562 | 0 | gpg_error_t err; |
6563 | 0 | KBNODE pub_root = NULL; |
6564 | 0 | const char *s; |
6565 | 0 | PKT_public_key *pri_psk = NULL; |
6566 | 0 | PKT_public_key *sub_psk = NULL; |
6567 | 0 | struct revocation_key *revkey; |
6568 | 0 | int did_sub = 0; |
6569 | 0 | u32 keytimestamp, subkeytimestamp, authkeytimestamp, signtimestamp; |
6570 | 0 | char *cache_nonce = NULL; |
6571 | 0 | int algo; |
6572 | 0 | u32 expire; |
6573 | 0 | const char *key_from_hexgrip = NULL; |
6574 | 0 | int cardkey; |
6575 | 0 | unsigned int keygen_flags; |
6576 | 0 | unsigned int idx; |
6577 | 0 | int any_adsk = 0; |
6578 | |
|
6579 | 0 | if (outctrl->dryrun) |
6580 | 0 | { |
6581 | 0 | log_info("dry-run mode - key generation skipped\n"); |
6582 | 0 | return; |
6583 | 0 | } |
6584 | | |
6585 | 0 | if ( outctrl->use_files ) |
6586 | 0 | { |
6587 | 0 | if ( outctrl->pub.newfname ) |
6588 | 0 | { |
6589 | 0 | iobuf_close(outctrl->pub.stream); |
6590 | 0 | outctrl->pub.stream = NULL; |
6591 | 0 | if (outctrl->pub.fname) |
6592 | 0 | iobuf_ioctl (NULL, IOBUF_IOCTL_INVALIDATE_CACHE, |
6593 | 0 | 0, (char*)outctrl->pub.fname); |
6594 | 0 | xfree( outctrl->pub.fname ); |
6595 | 0 | outctrl->pub.fname = outctrl->pub.newfname; |
6596 | 0 | outctrl->pub.newfname = NULL; |
6597 | |
|
6598 | 0 | if (is_secured_filename (outctrl->pub.fname) ) |
6599 | 0 | { |
6600 | 0 | outctrl->pub.stream = NULL; |
6601 | 0 | gpg_err_set_errno (EPERM); |
6602 | 0 | } |
6603 | 0 | else |
6604 | 0 | outctrl->pub.stream = iobuf_create (outctrl->pub.fname, 0); |
6605 | 0 | if (!outctrl->pub.stream) |
6606 | 0 | { |
6607 | 0 | log_error(_("can't create '%s': %s\n"), outctrl->pub.newfname, |
6608 | 0 | strerror(errno) ); |
6609 | 0 | return; |
6610 | 0 | } |
6611 | 0 | if (opt.armor) |
6612 | 0 | { |
6613 | 0 | outctrl->pub.afx->what = 1; |
6614 | 0 | push_armor_filter (outctrl->pub.afx, outctrl->pub.stream); |
6615 | 0 | } |
6616 | 0 | } |
6617 | 0 | log_assert( outctrl->pub.stream ); |
6618 | 0 | if (opt.verbose) |
6619 | 0 | log_info (_("writing public key to '%s'\n"), outctrl->pub.fname ); |
6620 | 0 | } |
6621 | | |
6622 | | |
6623 | | /* We create the packets as a tree of kbnodes. Because the |
6624 | | structure we create is known in advance we simply generate a |
6625 | | linked list. The first packet is a dummy packet which we flag as |
6626 | | deleted. The very first packet must always be a KEY packet. */ |
6627 | | |
6628 | 0 | start_tree (&pub_root); |
6629 | |
|
6630 | 0 | cardkey = get_parameter_bool (para, pCARDKEY); |
6631 | | |
6632 | | /* In the case that the keys are created from the card we need to |
6633 | | * take the timestamps from the card. Only in this case a |
6634 | | * pSUBKEYCREATIONDATE or pAUTHKEYCREATIONDATE might be defined and |
6635 | | * then we need to use that so that the fingerprint of the subkey |
6636 | | * also matches the pre-computed and stored one on the card. In |
6637 | | * this case we also use the current time to create the |
6638 | | * self-signatures. */ |
6639 | 0 | keytimestamp = get_parameter_u32 (para, pKEYCREATIONDATE); |
6640 | 0 | if (!keytimestamp) |
6641 | 0 | keytimestamp = make_timestamp (); |
6642 | 0 | subkeytimestamp = cardkey? get_parameter_u32 (para, pSUBKEYCREATIONDATE) : 0; |
6643 | 0 | if (!subkeytimestamp) |
6644 | 0 | subkeytimestamp = keytimestamp; |
6645 | 0 | authkeytimestamp = cardkey? get_parameter_u32 (para, pAUTHKEYCREATIONDATE): 0; |
6646 | 0 | if (!authkeytimestamp) |
6647 | 0 | authkeytimestamp = keytimestamp; |
6648 | |
|
6649 | 0 | signtimestamp = cardkey? make_timestamp () : keytimestamp; |
6650 | | |
6651 | | /* log_debug ("XXX: cardkey ..: %d\n", cardkey); */ |
6652 | | /* log_debug ("XXX: keytime ..: %s\n", isotimestamp (keytimestamp)); */ |
6653 | | /* log_debug ("XXX: subkeytime: %s\n", isotimestamp (subkeytimestamp)); */ |
6654 | | /* log_debug ("XXX: authkeytim: %s\n", isotimestamp (authkeytimestamp)); */ |
6655 | | /* log_debug ("XXX: signtime .: %s\n", isotimestamp (signtimestamp)); */ |
6656 | | |
6657 | | /* Fixme: Check that this comment is still valid: |
6658 | | Note that, depending on the backend (i.e. the used scdaemon |
6659 | | version), the card key generation may update TIMESTAMP for each |
6660 | | key. Thus we need to pass TIMESTAMP to all signing function to |
6661 | | make sure that the binding signature is done using the timestamp |
6662 | | of the corresponding (sub)key and not that of the primary key. |
6663 | | An alternative implementation could tell the signing function the |
6664 | | node of the subkey but that is more work than just to pass the |
6665 | | current timestamp. */ |
6666 | |
|
6667 | 0 | algo = get_parameter_algo (ctrl, para, pKEYTYPE, NULL ); |
6668 | 0 | expire = get_parameter_u32( para, pKEYEXPIRE ); |
6669 | 0 | key_from_hexgrip = get_parameter_value (para, pKEYGRIP); |
6670 | 0 | if (cardkey && !key_from_hexgrip) |
6671 | 0 | BUG (); |
6672 | | |
6673 | 0 | keygen_flags = outctrl->keygen_flags; |
6674 | 0 | if (get_parameter_uint (para, pVERSION) == 5) |
6675 | 0 | keygen_flags |= KEYGEN_FLAG_CREATE_V5_KEY; |
6676 | |
|
6677 | 0 | if (key_from_hexgrip) |
6678 | 0 | err = do_create_from_keygrip (ctrl, algo, key_from_hexgrip, cardkey, |
6679 | 0 | pub_root, |
6680 | 0 | keytimestamp, |
6681 | 0 | expire, 0, &keygen_flags); |
6682 | 0 | else if (!card) |
6683 | 0 | err = do_create (algo, |
6684 | 0 | get_parameter_uint( para, pKEYLENGTH ), |
6685 | 0 | get_parameter_value (para, pKEYCURVE), |
6686 | 0 | pub_root, |
6687 | 0 | keytimestamp, |
6688 | 0 | expire, 0, |
6689 | 0 | &keygen_flags, |
6690 | 0 | get_parameter_passphrase (para), |
6691 | 0 | &cache_nonce, NULL, |
6692 | 0 | NULL, NULL); |
6693 | 0 | else |
6694 | 0 | err = gen_card_key (1, algo, |
6695 | 0 | 1, pub_root, &keytimestamp, |
6696 | 0 | expire, &keygen_flags); |
6697 | | |
6698 | | /* Get the pointer to the generated public key packet. */ |
6699 | 0 | if (!err) |
6700 | 0 | { |
6701 | 0 | pri_psk = pub_root->next->pkt->pkt.public_key; |
6702 | 0 | log_assert (pri_psk); |
6703 | | |
6704 | | /* Make sure a few fields are correctly set up before going |
6705 | | further. */ |
6706 | 0 | pri_psk->flags.primary = 1; |
6707 | 0 | keyid_from_pk (pri_psk, NULL); |
6708 | | /* We don't use pk_keyid to get keyid, because it also asserts |
6709 | | that main_keyid is set! */ |
6710 | 0 | keyid_copy (pri_psk->main_keyid, pri_psk->keyid); |
6711 | 0 | } |
6712 | | |
6713 | | /* Write all signatures specifying designated revokers. */ |
6714 | 0 | for (idx=0; !err && (revkey = get_parameter_revkey (para, idx)); idx++) |
6715 | 0 | { |
6716 | 0 | err = write_direct_sig (ctrl, pub_root, pri_psk, |
6717 | 0 | revkey, signtimestamp, cache_nonce); |
6718 | 0 | } |
6719 | |
|
6720 | 0 | if (!err && (s = get_parameter_value (para, pUSERID))) |
6721 | 0 | { |
6722 | 0 | err = write_uid (pub_root, s ); |
6723 | 0 | if (!err) |
6724 | 0 | err = write_selfsigs (ctrl, pub_root, pri_psk, |
6725 | 0 | get_parameter_uint (para, pKEYUSAGE), |
6726 | 0 | signtimestamp, cache_nonce); |
6727 | 0 | } |
6728 | | |
6729 | | /* Write the auth key to the card before the encryption key. This |
6730 | | is a partial workaround for a PGP bug (as of this writing, all |
6731 | | versions including 8.1), that causes it to try and encrypt to |
6732 | | the most recent subkey regardless of whether that subkey is |
6733 | | actually an encryption type. In this case, the auth key is an |
6734 | | RSA key so it succeeds. */ |
6735 | |
|
6736 | 0 | if (!err && card && get_parameter (para, pAUTHKEYTYPE)) |
6737 | 0 | { |
6738 | 0 | err = gen_card_key (3, get_parameter_algo (ctrl, para, |
6739 | 0 | pAUTHKEYTYPE, NULL ), |
6740 | 0 | 0, pub_root, &authkeytimestamp, expire, |
6741 | 0 | &keygen_flags); |
6742 | 0 | if (!err) |
6743 | 0 | err = write_keybinding (ctrl, pub_root, pri_psk, NULL, |
6744 | 0 | PUBKEY_USAGE_AUTH, signtimestamp, cache_nonce); |
6745 | 0 | } |
6746 | |
|
6747 | 0 | if (!err && get_parameter (para, pSUBKEYTYPE)) |
6748 | 0 | { |
6749 | 0 | int subkey_algo = get_parameter_algo (ctrl, para, pSUBKEYTYPE, NULL); |
6750 | |
|
6751 | 0 | key_from_hexgrip = get_parameter_value (para, pSUBKEYGRIP); |
6752 | |
|
6753 | 0 | keygen_flags = outctrl->keygen_flags; |
6754 | 0 | if (get_parameter_uint (para, pSUBVERSION) == 5) |
6755 | 0 | keygen_flags |= KEYGEN_FLAG_CREATE_V5_KEY; |
6756 | |
|
6757 | 0 | if (key_from_hexgrip) |
6758 | 0 | err = do_create_from_keygrip (ctrl, subkey_algo, |
6759 | 0 | key_from_hexgrip, cardkey, |
6760 | 0 | pub_root, subkeytimestamp, |
6761 | 0 | get_parameter_u32 (para, pSUBKEYEXPIRE), |
6762 | 0 | 1, &keygen_flags); |
6763 | 0 | else if (get_parameter_value (para, pCARDBACKUPKEY)) |
6764 | 0 | { |
6765 | 0 | int lastmode; |
6766 | 0 | unsigned int mykeygenflags = KEYGEN_FLAG_NO_PROTECTION; |
6767 | |
|
6768 | 0 | err = agent_set_ephemeral_mode (ctrl, 1, &lastmode); |
6769 | 0 | if (err) |
6770 | 0 | log_error ("error switching to ephemeral mode: %s\n", |
6771 | 0 | gpg_strerror (err)); |
6772 | 0 | else |
6773 | 0 | { |
6774 | 0 | err = do_create (subkey_algo, |
6775 | 0 | get_parameter_uint (para, pSUBKEYLENGTH), |
6776 | 0 | get_parameter_value (para, pSUBKEYCURVE), |
6777 | 0 | pub_root, |
6778 | 0 | subkeytimestamp, |
6779 | 0 | get_parameter_u32 (para, pSUBKEYEXPIRE), 1, |
6780 | 0 | &mykeygenflags, |
6781 | 0 | get_parameter_passphrase (para), |
6782 | 0 | &cache_nonce, NULL, |
6783 | 0 | NULL, NULL); |
6784 | | /* Get the pointer to the generated public subkey packet. */ |
6785 | 0 | if (!err) |
6786 | 0 | { |
6787 | 0 | kbnode_t node; |
6788 | |
|
6789 | 0 | for (node = pub_root; node; node = node->next) |
6790 | 0 | if (node->pkt->pkttype == PKT_PUBLIC_SUBKEY) |
6791 | 0 | sub_psk = node->pkt->pkt.public_key; |
6792 | 0 | log_assert (sub_psk); |
6793 | 0 | err = card_store_key_with_backup (ctrl, |
6794 | 0 | sub_psk, gnupg_homedir ()); |
6795 | 0 | } |
6796 | | |
6797 | | /* Reset the ephemeral mode as needed. */ |
6798 | 0 | if (!lastmode && agent_set_ephemeral_mode (ctrl, 0, NULL)) |
6799 | 0 | log_error ("error clearing the ephemeral mode\n"); |
6800 | 0 | } |
6801 | 0 | } |
6802 | 0 | else if (!card) |
6803 | 0 | { |
6804 | 0 | err = do_create (subkey_algo, |
6805 | 0 | get_parameter_uint (para, pSUBKEYLENGTH), |
6806 | 0 | get_parameter_value (para, pSUBKEYCURVE), |
6807 | 0 | pub_root, |
6808 | 0 | subkeytimestamp, |
6809 | 0 | get_parameter_u32 (para, pSUBKEYEXPIRE), 1, |
6810 | 0 | &keygen_flags, |
6811 | 0 | get_parameter_passphrase (para), |
6812 | 0 | &cache_nonce, NULL, |
6813 | 0 | NULL, NULL); |
6814 | 0 | if (!err) |
6815 | 0 | { |
6816 | 0 | kbnode_t node; |
6817 | |
|
6818 | 0 | for (node = pub_root; node; node = node->next) |
6819 | 0 | if (node->pkt->pkttype == PKT_PUBLIC_SUBKEY) |
6820 | 0 | sub_psk = node->pkt->pkt.public_key; |
6821 | 0 | log_assert (sub_psk); |
6822 | 0 | } |
6823 | 0 | } |
6824 | 0 | else |
6825 | 0 | { |
6826 | 0 | err = gen_card_key (2, subkey_algo, 0, pub_root, |
6827 | 0 | &subkeytimestamp, expire, &keygen_flags); |
6828 | 0 | } |
6829 | | |
6830 | 0 | if (!err) |
6831 | 0 | err = write_keybinding (ctrl, pub_root, pri_psk, sub_psk, |
6832 | 0 | get_parameter_uint (para, pSUBKEYUSAGE), |
6833 | 0 | signtimestamp, cache_nonce); |
6834 | 0 | did_sub = 1; |
6835 | 0 | } |
6836 | | |
6837 | | |
6838 | | /* Get rid of the first empty packet. */ |
6839 | 0 | if (!err) |
6840 | 0 | commit_kbnode (&pub_root); |
6841 | | |
6842 | | /* Add ADSKs if any are specified. */ |
6843 | 0 | if (!err) |
6844 | 0 | { |
6845 | 0 | PKT_public_key *adsk; |
6846 | |
|
6847 | 0 | for (idx=0; (adsk = get_parameter_adsk (para, idx)); idx++) |
6848 | 0 | { |
6849 | 0 | err = append_adsk_to_key (ctrl, pub_root, adsk, |
6850 | 0 | signtimestamp, cache_nonce); |
6851 | 0 | if (err) |
6852 | 0 | break; |
6853 | 0 | any_adsk++; |
6854 | 0 | } |
6855 | 0 | } |
6856 | |
|
6857 | 0 | if (!err && outctrl->use_files) /* Direct write to specified files. */ |
6858 | 0 | { |
6859 | 0 | err = write_keyblock (outctrl->pub.stream, pub_root); |
6860 | 0 | if (err) |
6861 | 0 | log_error ("can't write public key: %s\n", gpg_strerror (err)); |
6862 | 0 | } |
6863 | 0 | else if (!err) /* Write to the standard keyrings. */ |
6864 | 0 | { |
6865 | 0 | KEYDB_HANDLE pub_hd; |
6866 | |
|
6867 | 0 | pub_hd = keydb_new (ctrl); |
6868 | 0 | if (!pub_hd) |
6869 | 0 | err = gpg_error_from_syserror (); |
6870 | 0 | else |
6871 | 0 | { |
6872 | 0 | err = keydb_locate_writable (pub_hd); |
6873 | 0 | if (err) |
6874 | 0 | log_error (_("no writable public keyring found: %s\n"), |
6875 | 0 | gpg_strerror (err)); |
6876 | 0 | else |
6877 | 0 | err = keydb_lock (pub_hd); |
6878 | 0 | } |
6879 | |
|
6880 | 0 | if (!err && opt.verbose) |
6881 | 0 | { |
6882 | 0 | log_info (_("writing public key to '%s'\n"), |
6883 | 0 | keydb_get_resource_name (pub_hd)); |
6884 | 0 | } |
6885 | |
|
6886 | 0 | if (!err) |
6887 | 0 | { |
6888 | 0 | err = keydb_insert_keyblock (pub_hd, pub_root); |
6889 | 0 | if (err) |
6890 | 0 | log_error (_("error writing public keyring '%s': %s\n"), |
6891 | 0 | keydb_get_resource_name (pub_hd), gpg_strerror (err)); |
6892 | 0 | } |
6893 | |
|
6894 | 0 | keydb_release (pub_hd); |
6895 | |
|
6896 | 0 | if (!err) |
6897 | 0 | { |
6898 | 0 | int no_enc_rsa; |
6899 | 0 | PKT_public_key *pk; |
6900 | |
|
6901 | 0 | no_enc_rsa = ((get_parameter_algo (ctrl, para, pKEYTYPE, NULL) |
6902 | 0 | == PUBKEY_ALGO_RSA) |
6903 | 0 | && get_parameter_uint (para, pKEYUSAGE) |
6904 | 0 | && !((get_parameter_uint (para, pKEYUSAGE) |
6905 | 0 | & PUBKEY_USAGE_ENC)) ); |
6906 | |
|
6907 | 0 | pk = find_kbnode (pub_root, PKT_PUBLIC_KEY)->pkt->pkt.public_key; |
6908 | |
|
6909 | 0 | if (!opt.flags.no_auto_trust_new_key) |
6910 | 0 | update_ownertrust (ctrl, pk, |
6911 | 0 | ((get_ownertrust (ctrl, pk) & ~TRUST_MASK) |
6912 | 0 | | TRUST_ULTIMATE )); |
6913 | |
|
6914 | 0 | gen_standard_revoke (ctrl, pk, cache_nonce); |
6915 | |
|
6916 | 0 | if (!opt.batch) |
6917 | 0 | { |
6918 | 0 | tty_printf (_("public and secret key created and signed.\n") ); |
6919 | 0 | tty_printf ("\n"); |
6920 | 0 | merge_keys_and_selfsig (ctrl, pub_root); |
6921 | |
|
6922 | 0 | list_keyblock_direct (ctrl, pub_root, 0, 1, |
6923 | 0 | opt.fingerprint || opt.with_fingerprint, |
6924 | 0 | 1); |
6925 | | /* Note that we ignore errors from the list function |
6926 | | * because that would only be an additional info. It |
6927 | | * has already been remarked that the key has been |
6928 | | * created. */ |
6929 | 0 | } |
6930 | |
|
6931 | 0 | if (!opt.batch |
6932 | 0 | && (get_parameter_algo (ctrl, para, |
6933 | 0 | pKEYTYPE, NULL) == PUBKEY_ALGO_DSA |
6934 | 0 | || no_enc_rsa ) |
6935 | 0 | && !get_parameter (para, pSUBKEYTYPE) ) |
6936 | 0 | { |
6937 | 0 | tty_printf(_("Note that this key cannot be used for " |
6938 | 0 | "encryption. You may want to use\n" |
6939 | 0 | "the command \"--edit-key\" to generate a " |
6940 | 0 | "subkey for this purpose.\n") ); |
6941 | 0 | } |
6942 | 0 | } |
6943 | 0 | } |
6944 | |
|
6945 | 0 | if (err) |
6946 | 0 | { |
6947 | 0 | if (opt.batch) |
6948 | 0 | log_error ("key generation failed: %s\n", gpg_strerror (err) ); |
6949 | 0 | else |
6950 | 0 | tty_printf (_("Key generation failed: %s\n"), gpg_strerror (err) ); |
6951 | 0 | write_status_error (card? "card_key_generate":"key_generate", err); |
6952 | 0 | print_status_key_not_created ( get_parameter_value (para, pHANDLE) ); |
6953 | 0 | } |
6954 | 0 | else |
6955 | 0 | { |
6956 | 0 | PKT_public_key *pk = find_kbnode (pub_root, |
6957 | 0 | PKT_PUBLIC_KEY)->pkt->pkt.public_key; |
6958 | 0 | print_status_key_created (did_sub? 'B':'P', pk, |
6959 | 0 | get_parameter_value (para, pHANDLE)); |
6960 | 0 | es_fflush (es_stdout); |
6961 | 0 | if (any_adsk) |
6962 | 0 | log_info (_("Note: The key has been created with one or more ADSK!\n")); |
6963 | |
|
6964 | 0 | if (opt.flags.auto_key_upload) |
6965 | 0 | { |
6966 | 0 | unsigned int saved_options = opt.keyserver_options.options; |
6967 | |
|
6968 | 0 | opt.keyserver_options.options |= KEYSERVER_LDAP_ONLY; |
6969 | 0 | opt.keyserver_options.options |= KEYSERVER_WARN_ONLY; |
6970 | 0 | keyserver_export_pubkey (ctrl, pk, 1/*Assume new key*/); |
6971 | 0 | opt.keyserver_options.options = saved_options; |
6972 | 0 | } |
6973 | 0 | } |
6974 | |
|
6975 | 0 | release_kbnode (pub_root); |
6976 | 0 | xfree (cache_nonce); |
6977 | 0 | } |
6978 | | |
6979 | | |
6980 | | static gpg_error_t |
6981 | | parse_algo_usage_expire (ctrl_t ctrl, int for_subkey, |
6982 | | const char *algostr, const char *usagestr, |
6983 | | const char *expirestr, |
6984 | | int *r_algo, unsigned int *r_usage, u32 *r_expire, |
6985 | | unsigned int *r_nbits, const char **r_curve, |
6986 | | int *r_version, char **r_keygrip, u32 *r_keytime) |
6987 | 0 | { |
6988 | 0 | gpg_error_t err; |
6989 | 0 | int algo; |
6990 | 0 | unsigned int use, nbits; |
6991 | 0 | u32 expire; |
6992 | 0 | int wantuse; |
6993 | 0 | int version = 4; |
6994 | 0 | const char *curve = NULL; |
6995 | |
|
6996 | 0 | *r_curve = NULL; |
6997 | 0 | if (r_keygrip) |
6998 | 0 | *r_keygrip = NULL; |
6999 | 0 | if (r_keytime) |
7000 | 0 | *r_keytime = 0; |
7001 | |
|
7002 | 0 | nbits = 0; |
7003 | | |
7004 | | /* Parse the algo string. */ |
7005 | 0 | if (algostr && *algostr == '&' && strlen (algostr) == 41) |
7006 | 0 | { |
7007 | | /* Take algo from existing key. */ |
7008 | 0 | algo = check_keygrip (ctrl, algostr+1); |
7009 | | /* FIXME: We need the curve name as well. */ |
7010 | 0 | return gpg_error (GPG_ERR_NOT_IMPLEMENTED); |
7011 | 0 | } |
7012 | | |
7013 | 0 | err = parse_key_parameter_string (ctrl, algostr, for_subkey? 1 : 0, |
7014 | 0 | usagestr? parse_usagestr (usagestr):0, |
7015 | 0 | &algo, &nbits, &use, &curve, &version, |
7016 | 0 | r_keygrip, r_keytime, |
7017 | 0 | NULL, NULL, NULL, NULL, NULL, NULL, NULL); |
7018 | 0 | if (err) |
7019 | 0 | { |
7020 | 0 | if (r_keygrip) |
7021 | 0 | { |
7022 | 0 | xfree (*r_keygrip); |
7023 | 0 | *r_keygrip = NULL; |
7024 | 0 | } |
7025 | 0 | return err; |
7026 | 0 | } |
7027 | | |
7028 | | /* Parse the usage string. */ |
7029 | 0 | if (!usagestr || !*usagestr |
7030 | 0 | || !ascii_strcasecmp (usagestr, "default") || !strcmp (usagestr, "-")) |
7031 | 0 | ; /* Keep usage from parse_key_parameter_string. */ |
7032 | 0 | else if ((wantuse = parse_usagestr (usagestr)) != -1) |
7033 | 0 | use = wantuse; |
7034 | 0 | else |
7035 | 0 | { |
7036 | 0 | if (r_keygrip) |
7037 | 0 | { |
7038 | 0 | xfree (*r_keygrip); |
7039 | 0 | *r_keygrip = NULL; |
7040 | 0 | } |
7041 | 0 | return gpg_error (GPG_ERR_INV_VALUE); |
7042 | 0 | } |
7043 | | |
7044 | | /* Now do the tricky ECDSA/ECDH adjustment. */ |
7045 | 0 | algo = adjust_algo_for_ecdh_ecdsa (algo, use, curve); |
7046 | | |
7047 | | /* Make sure a primary key has the CERT usage. */ |
7048 | 0 | if (!for_subkey) |
7049 | 0 | use |= PUBKEY_USAGE_CERT; |
7050 | | |
7051 | | /* Check that usage is possible. NB: We have the same check in |
7052 | | * parse_key_parameter_string but need it here again in case the |
7053 | | * separate usage value has been given. */ |
7054 | 0 | if (/**/((use & (PUBKEY_USAGE_SIG|PUBKEY_USAGE_AUTH|PUBKEY_USAGE_CERT)) |
7055 | 0 | && !pubkey_get_nsig (algo)) |
7056 | 0 | || ((use & PUBKEY_USAGE_ENC) |
7057 | 0 | && !pubkey_get_nenc (algo)) |
7058 | 0 | || (for_subkey && (use & PUBKEY_USAGE_CERT))) |
7059 | 0 | { |
7060 | 0 | if (r_keygrip) |
7061 | 0 | { |
7062 | 0 | xfree (*r_keygrip); |
7063 | 0 | *r_keygrip = NULL; |
7064 | 0 | } |
7065 | 0 | return gpg_error (GPG_ERR_WRONG_KEY_USAGE); |
7066 | 0 | } |
7067 | | |
7068 | | /* Parse the expire string. */ |
7069 | 0 | expire = parse_expire_string (expirestr); |
7070 | 0 | if (expire == (u32)-1 ) |
7071 | 0 | { |
7072 | 0 | if (r_keygrip) |
7073 | 0 | { |
7074 | 0 | xfree (*r_keygrip); |
7075 | 0 | *r_keygrip = NULL; |
7076 | 0 | } |
7077 | 0 | return gpg_error (GPG_ERR_INV_VALUE); |
7078 | 0 | } |
7079 | | |
7080 | 0 | if (curve) |
7081 | 0 | *r_curve = curve; |
7082 | 0 | *r_algo = algo; |
7083 | 0 | *r_usage = use; |
7084 | 0 | *r_expire = expire; |
7085 | 0 | *r_nbits = nbits; |
7086 | 0 | *r_version = version; |
7087 | 0 | return 0; |
7088 | 0 | } |
7089 | | |
7090 | | |
7091 | | /* Add a new subkey to an existing key. Returns 0 if a new key has |
7092 | | been generated and put into the keyblocks. If any of ALGOSTR, |
7093 | | USAGESTR, or EXPIRESTR is NULL interactive mode is used. */ |
7094 | | gpg_error_t |
7095 | | generate_subkeypair (ctrl_t ctrl, kbnode_t keyblock, const char *algostr, |
7096 | | const char *usagestr, const char *expirestr) |
7097 | 0 | { |
7098 | 0 | gpg_error_t err = 0; |
7099 | 0 | int interactive; |
7100 | 0 | kbnode_t node; |
7101 | 0 | PKT_public_key *pri_psk = NULL; |
7102 | 0 | PKT_public_key *sub_psk = NULL; |
7103 | 0 | int algo; |
7104 | 0 | unsigned int use; |
7105 | 0 | u32 expire; |
7106 | 0 | unsigned int nbits = 0; |
7107 | 0 | const char *curve = NULL; |
7108 | 0 | u32 cur_time; |
7109 | 0 | char *key_from_hexgrip = NULL; |
7110 | 0 | u32 keytime = 0; |
7111 | 0 | int cardkey = 0; |
7112 | 0 | char *hexgrip = NULL; |
7113 | 0 | char *serialno = NULL; |
7114 | 0 | char *cache_nonce = NULL; |
7115 | 0 | char *passwd_nonce = NULL; |
7116 | 0 | int keygen_flags = 0; |
7117 | |
|
7118 | 0 | interactive = (!algostr || !usagestr || !expirestr); |
7119 | | |
7120 | | /* Break out the primary key. */ |
7121 | 0 | node = find_kbnode (keyblock, PKT_PUBLIC_KEY); |
7122 | 0 | if (!node) |
7123 | 0 | { |
7124 | 0 | log_error ("Oops; primary key missing in keyblock!\n"); |
7125 | 0 | err = gpg_error (GPG_ERR_BUG); |
7126 | 0 | goto leave; |
7127 | 0 | } |
7128 | 0 | pri_psk = node->pkt->pkt.public_key; |
7129 | |
|
7130 | 0 | cur_time = make_timestamp (); |
7131 | |
|
7132 | 0 | if (pri_psk->timestamp > cur_time) |
7133 | 0 | { |
7134 | 0 | ulong d = pri_psk->timestamp - cur_time; |
7135 | 0 | log_info ( d==1 ? _("key has been created %lu second " |
7136 | 0 | "in future (time warp or clock problem)\n") |
7137 | 0 | : _("key has been created %lu seconds " |
7138 | 0 | "in future (time warp or clock problem)\n"), d ); |
7139 | 0 | if (!opt.ignore_time_conflict) |
7140 | 0 | { |
7141 | 0 | err = gpg_error (GPG_ERR_TIME_CONFLICT); |
7142 | 0 | goto leave; |
7143 | 0 | } |
7144 | 0 | } |
7145 | | |
7146 | 0 | if (pri_psk->version < 4) |
7147 | 0 | { |
7148 | 0 | log_info (_("Note: creating subkeys for v3 keys " |
7149 | 0 | "is not OpenPGP compliant\n")); |
7150 | 0 | err = gpg_error (GPG_ERR_CONFLICT); |
7151 | 0 | goto leave; |
7152 | 0 | } |
7153 | | |
7154 | 0 | err = hexkeygrip_from_pk (pri_psk, &hexgrip); |
7155 | 0 | if (err) |
7156 | 0 | goto leave; |
7157 | | /* FIXME: Right now the primary key won't be a composite key. But this |
7158 | | * will change */ |
7159 | 0 | if (agent_get_keyinfo (NULL, hexgrip, &serialno, NULL)) |
7160 | 0 | { |
7161 | 0 | if (interactive) |
7162 | 0 | tty_printf (_("Secret parts of primary key are not available.\n")); |
7163 | 0 | else |
7164 | 0 | log_info ( _("Secret parts of primary key are not available.\n")); |
7165 | 0 | err = gpg_error (GPG_ERR_NO_SECKEY); |
7166 | 0 | goto leave; |
7167 | 0 | } |
7168 | 0 | if (serialno) |
7169 | 0 | { |
7170 | 0 | if (interactive) |
7171 | 0 | tty_printf (_("Secret parts of primary key are stored on-card.\n")); |
7172 | 0 | else |
7173 | 0 | log_info ( _("Secret parts of primary key are stored on-card.\n")); |
7174 | 0 | } |
7175 | |
|
7176 | 0 | if (interactive) |
7177 | 0 | { |
7178 | 0 | algo = ask_algo (ctrl, 1, NULL, &use, &key_from_hexgrip, &cardkey, |
7179 | 0 | &keytime); |
7180 | 0 | log_assert (algo); |
7181 | | |
7182 | 0 | if (key_from_hexgrip) |
7183 | 0 | nbits = 0; |
7184 | 0 | else if (algo == PUBKEY_ALGO_ECDSA |
7185 | 0 | || algo == PUBKEY_ALGO_EDDSA |
7186 | 0 | || algo == PUBKEY_ALGO_ECDH) |
7187 | 0 | { |
7188 | 0 | curve = ask_curve (&algo, NULL, NULL); |
7189 | |
|
7190 | 0 | if (curve && (!strcmp (curve, "X448") || !strcmp (curve, "Ed448"))) |
7191 | 0 | keygen_flags |= KEYGEN_FLAG_CREATE_V5_KEY; |
7192 | 0 | } |
7193 | 0 | else if (algo == PUBKEY_ALGO_KYBER) |
7194 | 0 | { |
7195 | 0 | const char *kyberalgostr; |
7196 | |
|
7197 | 0 | kyberalgostr = ask_kyber_variant (); |
7198 | 0 | if (!kyberalgostr) /* Should not happen. */ |
7199 | 0 | kyberalgostr = PQC_STD_KEY_PARAM_SUB; |
7200 | |
|
7201 | 0 | nbits = strstr (kyberalgostr, "768_")? 768 : 1024; |
7202 | 0 | curve = strchr (kyberalgostr, '_'); |
7203 | 0 | log_assert (curve && curve[1]); |
7204 | 0 | curve++; |
7205 | 0 | } |
7206 | 0 | else |
7207 | 0 | nbits = ask_keysize (algo, 0); |
7208 | | |
7209 | 0 | expire = ask_expire_interval (0, NULL); |
7210 | 0 | if (!cpr_enabled() && !cpr_get_answer_is_yes("keygen.sub.okay", |
7211 | 0 | _("Really create? (y/N) "))) |
7212 | 0 | { |
7213 | 0 | err = gpg_error (GPG_ERR_CANCELED); |
7214 | 0 | goto leave; |
7215 | 0 | } |
7216 | 0 | } |
7217 | 0 | else /* Unattended mode. */ |
7218 | 0 | { |
7219 | 0 | int version; |
7220 | |
|
7221 | 0 | err = parse_algo_usage_expire (ctrl, 1, algostr, usagestr, expirestr, |
7222 | 0 | &algo, &use, &expire, &nbits, &curve, |
7223 | 0 | &version, &key_from_hexgrip, &keytime); |
7224 | 0 | if (err) |
7225 | 0 | goto leave; |
7226 | | |
7227 | 0 | if (version == 5) |
7228 | 0 | keygen_flags |= KEYGEN_FLAG_CREATE_V5_KEY; |
7229 | 0 | } |
7230 | | |
7231 | | /* Verify the passphrase now so that we get a cache item for the |
7232 | | * primary key passphrase. The agent also returns a passphrase |
7233 | | * nonce, which we can use to set the passphrase for the subkey to |
7234 | | * that of the primary key. */ |
7235 | 0 | { |
7236 | 0 | char *desc = gpg_format_keydesc (ctrl, pri_psk, FORMAT_KEYDESC_NORMAL, 1); |
7237 | 0 | err = agent_passwd (ctrl, hexgrip, desc, 1 /*=verify*/, |
7238 | 0 | &cache_nonce, &passwd_nonce); |
7239 | 0 | xfree (desc); |
7240 | 0 | if (gpg_err_code (err) == GPG_ERR_NOT_IMPLEMENTED |
7241 | 0 | && gpg_err_source (err) == GPG_ERR_SOURCE_GPGAGENT) |
7242 | 0 | err = 0; /* Very likely that the key is on a card. */ |
7243 | 0 | if (err) |
7244 | 0 | goto leave; |
7245 | 0 | } |
7246 | | |
7247 | | /* Start creation. */ |
7248 | 0 | if (key_from_hexgrip) |
7249 | 0 | { |
7250 | 0 | err = do_create_from_keygrip (ctrl, algo, key_from_hexgrip, cardkey, |
7251 | 0 | keyblock, |
7252 | 0 | keytime? keytime : cur_time, |
7253 | 0 | expire, 1, |
7254 | 0 | &keygen_flags); |
7255 | 0 | } |
7256 | 0 | else |
7257 | 0 | { |
7258 | 0 | const char *passwd; |
7259 | | |
7260 | | /* If the pinentry loopback mode is not and we have a static |
7261 | | passphrase (i.e. set with --passphrase{,-fd,-file} while in batch |
7262 | | mode), we use that passphrase for the new subkey. */ |
7263 | 0 | if (opt.pinentry_mode != PINENTRY_MODE_LOOPBACK |
7264 | 0 | && have_static_passphrase ()) |
7265 | 0 | passwd = get_static_passphrase (); |
7266 | 0 | else |
7267 | 0 | passwd = NULL; |
7268 | |
|
7269 | 0 | err = do_create (algo, nbits, curve, |
7270 | 0 | keyblock, cur_time, expire, 1, &keygen_flags, |
7271 | 0 | passwd, &cache_nonce, &passwd_nonce, NULL, NULL); |
7272 | 0 | } |
7273 | 0 | if (err) |
7274 | 0 | goto leave; |
7275 | | |
7276 | | /* Get the pointer to the generated public subkey packet. */ |
7277 | 0 | for (node = keyblock; node; node = node->next) |
7278 | 0 | if (node->pkt->pkttype == PKT_PUBLIC_SUBKEY) |
7279 | 0 | sub_psk = node->pkt->pkt.public_key; |
7280 | | |
7281 | | /* Write the binding signature. */ |
7282 | 0 | err = write_keybinding (ctrl, keyblock, pri_psk, sub_psk, use, cur_time, |
7283 | 0 | cache_nonce); |
7284 | 0 | if (err) |
7285 | 0 | goto leave; |
7286 | | |
7287 | 0 | print_status_key_created ('S', sub_psk, NULL); |
7288 | | |
7289 | |
|
7290 | 0 | leave: |
7291 | 0 | xfree (key_from_hexgrip); |
7292 | 0 | xfree (hexgrip); |
7293 | 0 | xfree (serialno); |
7294 | 0 | xfree (cache_nonce); |
7295 | 0 | xfree (passwd_nonce); |
7296 | 0 | if (err) |
7297 | 0 | { |
7298 | 0 | log_error (_("Key generation failed: %s\n"), gpg_strerror (err) ); |
7299 | 0 | write_status_error (cardkey? "card_key_generate":"key_generate", err); |
7300 | 0 | print_status_key_not_created ( NULL ); |
7301 | 0 | } |
7302 | 0 | return err; |
7303 | 0 | } |
7304 | | |
7305 | | |
7306 | | /* Append the subkey SUB_PK to the KEYBLOCK but first verify that this |
7307 | | * key is not yet part of that keyblock; return GPG_ERR_EEXIST in that |
7308 | | * case. USE gives the usage to set for the new key (take care: no |
7309 | | * check for compatibiliy with the algorithm is done). Return 0 if |
7310 | | * the key was sucessfully added to keyblock. On error KEYBLOCK might |
7311 | | * be modified but missing the key binding; the caller should take |
7312 | | * care of this. */ |
7313 | | gpg_error_t |
7314 | | append_subkey_to_keyblock (ctrl_t ctrl, kbnode_t keyblock, |
7315 | | PKT_public_key *sub_pk, int use) |
7316 | 0 | { |
7317 | 0 | gpg_error_t err = 0; |
7318 | 0 | kbnode_t node; |
7319 | 0 | PKT_public_key *pri_pk = NULL; |
7320 | 0 | u32 cur_time; |
7321 | 0 | PACKET *pkt; |
7322 | | |
7323 | | /* First make a copy of SUB_PK so that we do not interfere with the |
7324 | | * callers copy. */ |
7325 | 0 | sub_pk = copy_public_key (NULL, sub_pk); |
7326 | | |
7327 | | /* Break out the primary key. */ |
7328 | 0 | node = find_kbnode (keyblock, PKT_PUBLIC_KEY); |
7329 | 0 | if (!node) |
7330 | 0 | { |
7331 | 0 | log_error ("Oops; primary key missing in keyblock!\n"); |
7332 | 0 | err = gpg_error (GPG_ERR_BUG); |
7333 | 0 | goto leave; |
7334 | 0 | } |
7335 | 0 | pri_pk = node->pkt->pkt.public_key; |
7336 | |
|
7337 | 0 | cur_time = make_timestamp(); |
7338 | | /* FIXME: Factor out the next two checks. */ |
7339 | 0 | if (pri_pk->timestamp > cur_time) |
7340 | 0 | { |
7341 | 0 | ulong d = pri_pk->timestamp - cur_time; |
7342 | 0 | log_info (d==1 ? _("key has been created %lu second " |
7343 | 0 | "in future (time warp or clock problem)\n") |
7344 | 0 | : _("key has been created %lu seconds " |
7345 | 0 | "in future (time warp or clock problem)\n"), d ); |
7346 | 0 | if (!opt.ignore_time_conflict) |
7347 | 0 | { |
7348 | 0 | err = gpg_error (GPG_ERR_TIME_CONFLICT); |
7349 | 0 | goto leave; |
7350 | 0 | } |
7351 | 0 | } |
7352 | | |
7353 | 0 | if (pri_pk->version < 4) |
7354 | 0 | { |
7355 | 0 | log_info (_("Note: creating subkeys for v3 keys " |
7356 | 0 | "is not OpenPGP compliant\n")); |
7357 | 0 | err = gpg_error (GPG_ERR_NOT_SUPPORTED); |
7358 | 0 | goto leave; |
7359 | 0 | } |
7360 | | |
7361 | | /* Check that the key is not yet on the keyblock */ |
7362 | 0 | for (node = keyblock; node; node = node->next) |
7363 | 0 | if ((node->pkt->pkttype == PKT_PUBLIC_KEY |
7364 | 0 | || node->pkt->pkttype == PKT_PUBLIC_SUBKEY) |
7365 | 0 | && !cmp_public_keys (node->pkt->pkt.public_key, sub_pk)) |
7366 | 0 | { |
7367 | 0 | log_info (_("key \"%s\" is already on this keyblock\n"), |
7368 | 0 | keystr_from_pk (sub_pk)); |
7369 | 0 | err = gpg_error (GPG_ERR_EEXIST); |
7370 | 0 | goto leave; |
7371 | 0 | } |
7372 | | |
7373 | | /* Append a copy of the subkey to the keyblock. */ |
7374 | 0 | pkt = xmalloc_clear (sizeof *pkt); |
7375 | 0 | pkt->pkttype = PKT_PUBLIC_SUBKEY; |
7376 | 0 | pkt->pkt.public_key = sub_pk; |
7377 | 0 | sub_pk = NULL; |
7378 | 0 | add_kbnode (keyblock, new_kbnode (pkt)); |
7379 | | /* Write a new keybinding. */ |
7380 | 0 | err = write_keybinding (ctrl, keyblock, pri_pk, sub_pk, |
7381 | 0 | use, cur_time, NULL); |
7382 | |
|
7383 | 0 | leave: |
7384 | 0 | free_public_key (sub_pk); |
7385 | 0 | return err; |
7386 | 0 | } |
7387 | | |
7388 | | |
7389 | | #ifdef ENABLE_CARD_SUPPORT |
7390 | | /* Generate a subkey on a card. */ |
7391 | | gpg_error_t |
7392 | | generate_card_subkeypair (ctrl_t ctrl, kbnode_t pub_keyblock, |
7393 | | int keyno, const char *serialno) |
7394 | | { |
7395 | | gpg_error_t err = 0; |
7396 | | kbnode_t node; |
7397 | | PKT_public_key *pri_pk = NULL; |
7398 | | unsigned int use; |
7399 | | u32 expire; |
7400 | | u32 cur_time; |
7401 | | struct para_data_s *para = NULL; |
7402 | | PKT_public_key *sub_pk = NULL; |
7403 | | int algo; |
7404 | | struct agent_card_info_s info; |
7405 | | int keygen_flags = 0; /* FIXME!!! */ |
7406 | | |
7407 | | log_assert (keyno >= 1 && keyno <= 3); |
7408 | | |
7409 | | memset (&info, 0, sizeof (info)); |
7410 | | err = agent_scd_getattr ("KEY-ATTR", &info); |
7411 | | if (err) |
7412 | | { |
7413 | | log_error (_("error getting current key info: %s\n"), gpg_strerror (err)); |
7414 | | return err; |
7415 | | } |
7416 | | algo = info.key_attr[keyno-1].algo; |
7417 | | |
7418 | | para = xtrycalloc (1, sizeof *para + strlen (serialno) ); |
7419 | | if (!para) |
7420 | | { |
7421 | | err = gpg_error_from_syserror (); |
7422 | | goto leave; |
7423 | | } |
7424 | | para->key = pSERIALNO; |
7425 | | strcpy (para->u.value, serialno); |
7426 | | |
7427 | | /* Break out the primary secret key */ |
7428 | | node = find_kbnode (pub_keyblock, PKT_PUBLIC_KEY); |
7429 | | if (!node) |
7430 | | { |
7431 | | log_error ("Oops; public key lost!\n"); |
7432 | | err = gpg_error (GPG_ERR_INTERNAL); |
7433 | | goto leave; |
7434 | | } |
7435 | | pri_pk = node->pkt->pkt.public_key; |
7436 | | |
7437 | | cur_time = make_timestamp(); |
7438 | | if (pri_pk->timestamp > cur_time) |
7439 | | { |
7440 | | ulong d = pri_pk->timestamp - cur_time; |
7441 | | log_info (d==1 ? _("key has been created %lu second " |
7442 | | "in future (time warp or clock problem)\n") |
7443 | | : _("key has been created %lu seconds " |
7444 | | "in future (time warp or clock problem)\n"), d ); |
7445 | | if (!opt.ignore_time_conflict) |
7446 | | { |
7447 | | err = gpg_error (GPG_ERR_TIME_CONFLICT); |
7448 | | goto leave; |
7449 | | } |
7450 | | } |
7451 | | |
7452 | | if (pri_pk->version < 4) |
7453 | | { |
7454 | | log_info (_("Note: creating subkeys for v3 keys " |
7455 | | "is not OpenPGP compliant\n")); |
7456 | | err = gpg_error (GPG_ERR_NOT_SUPPORTED); |
7457 | | goto leave; |
7458 | | } |
7459 | | |
7460 | | expire = ask_expire_interval (0, NULL); |
7461 | | if (keyno == 1) |
7462 | | use = PUBKEY_USAGE_SIG; |
7463 | | else if (keyno == 2) |
7464 | | use = PUBKEY_USAGE_ENC; |
7465 | | else |
7466 | | use = PUBKEY_USAGE_AUTH; |
7467 | | if (!cpr_enabled() && !cpr_get_answer_is_yes("keygen.cardsub.okay", |
7468 | | _("Really create? (y/N) "))) |
7469 | | { |
7470 | | err = gpg_error (GPG_ERR_CANCELED); |
7471 | | goto leave; |
7472 | | } |
7473 | | |
7474 | | /* Note, that depending on the backend, the card key generation may |
7475 | | update CUR_TIME. */ |
7476 | | err = gen_card_key (keyno, algo, 0, pub_keyblock, &cur_time, expire, |
7477 | | &keygen_flags); |
7478 | | /* Get the pointer to the generated public subkey packet. */ |
7479 | | if (!err) |
7480 | | { |
7481 | | for (node = pub_keyblock; node; node = node->next) |
7482 | | if (node->pkt->pkttype == PKT_PUBLIC_SUBKEY) |
7483 | | sub_pk = node->pkt->pkt.public_key; |
7484 | | log_assert (sub_pk); |
7485 | | err = write_keybinding (ctrl, pub_keyblock, pri_pk, sub_pk, |
7486 | | use, cur_time, NULL); |
7487 | | } |
7488 | | |
7489 | | leave: |
7490 | | if (err) |
7491 | | log_error (_("Key generation failed: %s\n"), gpg_strerror (err) ); |
7492 | | else |
7493 | | print_status_key_created ('S', sub_pk, NULL); |
7494 | | release_parameter_list (para); |
7495 | | return err; |
7496 | | } |
7497 | | #endif /* !ENABLE_CARD_SUPPORT */ |
7498 | | |
7499 | | /* |
7500 | | * Write a keyblock to an output stream |
7501 | | */ |
7502 | | static int |
7503 | | write_keyblock( IOBUF out, KBNODE node ) |
7504 | 0 | { |
7505 | 0 | for( ; node ; node = node->next ) |
7506 | 0 | { |
7507 | 0 | if(!is_deleted_kbnode(node)) |
7508 | 0 | { |
7509 | 0 | int rc = build_packet( out, node->pkt ); |
7510 | 0 | if( rc ) |
7511 | 0 | { |
7512 | 0 | log_error("build_packet(%d) failed: %s\n", |
7513 | 0 | node->pkt->pkttype, gpg_strerror (rc) ); |
7514 | 0 | return rc; |
7515 | 0 | } |
7516 | 0 | } |
7517 | 0 | } |
7518 | | |
7519 | 0 | return 0; |
7520 | 0 | } |
7521 | | |
7522 | | |
7523 | | /* Note that timestamp is an in/out arg. */ |
7524 | | static gpg_error_t |
7525 | | gen_card_key (int keyno, int algo, int is_primary, kbnode_t pub_root, |
7526 | | u32 *timestamp, u32 expireval, int *keygen_flags) |
7527 | 0 | { |
7528 | | #ifdef ENABLE_CARD_SUPPORT |
7529 | | gpg_error_t err; |
7530 | | PACKET *pkt; |
7531 | | PKT_public_key *pk; |
7532 | | char keyid[10]; |
7533 | | unsigned char *public; |
7534 | | gcry_sexp_t s_key; |
7535 | | |
7536 | | snprintf (keyid, DIM(keyid), "OPENPGP.%d", keyno); |
7537 | | |
7538 | | pk = xtrycalloc (1, sizeof *pk ); |
7539 | | if (!pk) |
7540 | | return gpg_error_from_syserror (); |
7541 | | pkt = xtrycalloc (1, sizeof *pkt); |
7542 | | if (!pkt) |
7543 | | { |
7544 | | xfree (pk); |
7545 | | return gpg_error_from_syserror (); |
7546 | | } |
7547 | | |
7548 | | /* Note: SCD knows the serialnumber, thus there is no point in passing it. */ |
7549 | | err = agent_scd_genkey (keyno, 1, timestamp); |
7550 | | /* The code below is not used because we force creation of |
7551 | | * the a card key (3rd arg). |
7552 | | * if (gpg_err_code (rc) == GPG_ERR_EEXIST) |
7553 | | * { |
7554 | | * tty_printf ("\n"); |
7555 | | * log_error ("WARNING: key does already exists!\n"); |
7556 | | * tty_printf ("\n"); |
7557 | | * if ( cpr_get_answer_is_yes( "keygen.card.replace_key", |
7558 | | * _("Replace existing key? "))) |
7559 | | * rc = agent_scd_genkey (keyno, 1, timestamp); |
7560 | | * } |
7561 | | */ |
7562 | | if (err) |
7563 | | { |
7564 | | log_error ("key generation failed: %s\n", gpg_strerror (err)); |
7565 | | xfree (pkt); |
7566 | | xfree (pk); |
7567 | | return err; |
7568 | | } |
7569 | | |
7570 | | /* Send the READKEY command so that the agent creates a shadow key for |
7571 | | card key. We need to do that now so that we are able to create |
7572 | | the self-signatures. */ |
7573 | | err = agent_readkey (NULL, 1, keyid, &public); |
7574 | | if (err) |
7575 | | { |
7576 | | xfree (pkt); |
7577 | | xfree (pk); |
7578 | | return err; |
7579 | | } |
7580 | | err = gcry_sexp_sscan (&s_key, NULL, public, |
7581 | | gcry_sexp_canon_len (public, 0, NULL, NULL)); |
7582 | | xfree (public); |
7583 | | if (err) |
7584 | | { |
7585 | | xfree (pkt); |
7586 | | xfree (pk); |
7587 | | return err; |
7588 | | } |
7589 | | |
7590 | | /* Force creation of v5 keys for X448. */ |
7591 | | if (curve_is_448 (s_key)) |
7592 | | *keygen_flags |= KEYGEN_FLAG_CREATE_V5_KEY; |
7593 | | |
7594 | | pk->version = (*keygen_flags & KEYGEN_FLAG_CREATE_V5_KEY)? 5 : 4; |
7595 | | |
7596 | | if (algo == PUBKEY_ALGO_RSA) |
7597 | | err = key_from_sexp (pk->pkey, s_key, "public-key", "ne"); |
7598 | | else if (algo == PUBKEY_ALGO_ECDSA |
7599 | | || algo == PUBKEY_ALGO_EDDSA |
7600 | | || algo == PUBKEY_ALGO_ECDH ) |
7601 | | err = ecckey_from_sexp (pk->pkey, s_key, NULL, algo, pk->version); |
7602 | | else |
7603 | | err = gpg_error (GPG_ERR_PUBKEY_ALGO); |
7604 | | gcry_sexp_release (s_key); |
7605 | | |
7606 | | if (err) |
7607 | | { |
7608 | | log_error ("key_from_sexp failed: %s\n", gpg_strerror (err) ); |
7609 | | free_public_key (pk); |
7610 | | return err; |
7611 | | } |
7612 | | |
7613 | | pk->timestamp = *timestamp; |
7614 | | if (expireval) |
7615 | | pk->expiredate = pk->timestamp + expireval; |
7616 | | pk->pubkey_algo = algo; |
7617 | | |
7618 | | pkt->pkttype = is_primary ? PKT_PUBLIC_KEY : PKT_PUBLIC_SUBKEY; |
7619 | | pkt->pkt.public_key = pk; |
7620 | | add_kbnode (pub_root, new_kbnode (pkt)); |
7621 | | |
7622 | | return 0; |
7623 | | #else |
7624 | 0 | (void)keyno; |
7625 | 0 | (void)is_primary; |
7626 | 0 | (void)pub_root; |
7627 | 0 | (void)timestamp; |
7628 | 0 | (void)expireval; |
7629 | 0 | return gpg_error (GPG_ERR_NOT_SUPPORTED); |
7630 | 0 | #endif /*!ENABLE_CARD_SUPPORT*/ |
7631 | 0 | } |