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