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