629
write_direct_sig( KBNODE root, KBNODE pub_root, PKT_secret_key *sk,
630
struct revocation_key *revkey )
639
log_info(_("writing direct signature\n"));
641
/* get the pk packet from the pub_tree */
642
node = find_kbnode( pub_root, PKT_PUBLIC_KEY );
645
pk = node->pkt->pkt.public_key;
647
/* we have to cache the key, so that the verification of the signature
648
* creation is able to retrieve the public key */
649
cache_public_key (pk);
651
/* and make the signature */
652
rc = make_keysig_packet(&sig,pk,NULL,NULL,sk,0x1F,0,0,0,0,
653
keygen_add_revkey,revkey);
655
log_error("make_keysig_packet failed: %s\n", gpg_strerror (rc) );
659
pkt = xcalloc (1, sizeof *pkt );
660
pkt->pkttype = PKT_SIGNATURE;
661
pkt->pkt.signature = sig;
662
add_kbnode( root, new_kbnode( pkt ) );
667
write_selfsig( KBNODE root, KBNODE pub_root, PKT_secret_key *sk,
678
log_info(_("writing self signature\n"));
680
/* get the uid packet from the list */
681
node = find_kbnode( root, PKT_USER_ID );
683
BUG(); /* no user id packet in tree */
684
uid = node->pkt->pkt.user_id;
685
/* get the pk packet from the pub_tree */
686
node = find_kbnode( pub_root, PKT_PUBLIC_KEY );
689
pk = node->pkt->pkt.public_key;
690
pk->pubkey_usage = use;
691
/* we have to cache the key, so that the verification of the signature
692
* creation is able to retrieve the public key */
693
cache_public_key (pk);
695
/* and make the signature */
696
rc = make_keysig_packet( &sig, pk, uid, NULL, sk, 0x13, 0, 0, 0, 0,
697
keygen_add_std_prefs, pk );
699
log_error("make_keysig_packet failed: %s\n", gpg_strerror (rc) );
703
pkt = xcalloc (1, sizeof *pkt );
704
pkt->pkttype = PKT_SIGNATURE;
705
pkt->pkt.signature = sig;
706
add_kbnode( root, new_kbnode( pkt ) );
711
write_keybinding( KBNODE root, KBNODE pub_root, PKT_secret_key *sk,
718
PKT_public_key *pk, *subpk;
719
struct opaque_data_usage_and_pk oduap;
722
log_info(_("writing key binding signature\n"));
724
/* get the pk packet from the pub_tree */
725
node = find_kbnode( pub_root, PKT_PUBLIC_KEY );
728
pk = node->pkt->pkt.public_key;
729
/* we have to cache the key, so that the verification of the signature
730
* creation is able to retrieve the public key */
731
cache_public_key (pk);
733
/* find the last subkey */
735
for(node=pub_root; node; node = node->next ) {
736
if( node->pkt->pkttype == PKT_PUBLIC_SUBKEY )
737
subpk = node->pkt->pkt.public_key;
742
/* and make the signature */
745
rc = make_keysig_packet( &sig, pk, NULL, subpk, sk, 0x18, 0, 0, 0, 0,
746
keygen_add_key_flags_and_expire, &oduap );
748
log_error("make_keysig_packet failed: %s\n", gpg_strerror (rc) );
752
pkt = xcalloc (1, sizeof *pkt );
753
pkt->pkttype = PKT_SIGNATURE;
754
pkt->pkt.signature = sig;
755
add_kbnode( root, new_kbnode( pkt ) );
779
/* Create a back-signature. If TIMESTAMP is not NULL, use it for the
780
signature creation time. */
782
make_backsig (PKT_signature *sig,PKT_public_key *pk,
783
PKT_public_key *sub_pk,PKT_secret_key *sub_sk,
786
PKT_signature *backsig;
789
cache_public_key(sub_pk);
791
rc = make_keysig_packet (&backsig, pk, NULL, sub_pk, sub_sk, 0x19,
792
0, 0, timestamp, 0, NULL, NULL);
794
log_error("make_keysig_packet failed for backsig: %s\n",g10_errstr(rc));
797
/* Get it into a binary packed form. */
798
IOBUF backsig_out=iobuf_temp();
801
init_packet(&backsig_pkt);
802
backsig_pkt.pkttype=PKT_SIGNATURE;
803
backsig_pkt.pkt.signature=backsig;
804
rc=build_packet(backsig_out,&backsig_pkt);
805
free_packet(&backsig_pkt);
807
log_error("build_packet failed for backsig: %s\n",g10_errstr(rc));
811
byte *buf=iobuf_get_temp_buffer(backsig_out);
813
/* Remove the packet header */
823
pktlen=(buf[1]-192)*256;
829
pktlen =buf[2] << 24;
830
pktlen|=buf[3] << 16;
849
pktlen =buf[mark++] << 24;
850
pktlen|=buf[mark++] << 16;
853
pktlen|=buf[mark++] << 8;
862
/* Now make the binary blob into a subpacket. */
863
build_sig_subpkt(sig,SIGSUBPKT_SIGNATURE,buf,pktlen);
865
iobuf_close(backsig_out);
874
write_direct_sig (KBNODE root, KBNODE pub_root, PKT_secret_key *sk,
875
struct revocation_key *revkey, u32 timestamp)
884
log_info(_("writing direct signature\n"));
886
/* Get the pk packet from the pub_tree. */
887
node = find_kbnode( pub_root, PKT_PUBLIC_KEY );
890
pk = node->pkt->pkt.public_key;
892
/* We have to cache the key, so that the verification of the
893
signature creation is able to retrieve the public key. */
894
cache_public_key (pk);
896
/* Make the signature. */
897
rc = make_keysig_packet (&sig,pk,NULL,NULL,sk,0x1F,
899
keygen_add_revkey, revkey);
902
log_error("make_keysig_packet failed: %s\n", g10_errstr(rc) );
906
pkt = xmalloc_clear( sizeof *pkt );
907
pkt->pkttype = PKT_SIGNATURE;
908
pkt->pkt.signature = sig;
909
add_kbnode( root, new_kbnode( pkt ) );
915
write_selfsigs( KBNODE sec_root, KBNODE pub_root, PKT_secret_key *sk,
916
unsigned int use, u32 timestamp )
926
log_info(_("writing self signature\n"));
928
/* Get the uid packet from the list. */
929
node = find_kbnode( pub_root, PKT_USER_ID );
931
BUG(); /* No user id packet in tree. */
932
uid = node->pkt->pkt.user_id;
934
/* Get the pk packet from the pub_tree. */
935
node = find_kbnode( pub_root, PKT_PUBLIC_KEY );
938
pk = node->pkt->pkt.public_key;
939
pk->pubkey_usage = use;
941
/* We have to cache the key, so that the verification of the
942
signature creation is able to retrieve the public key. */
943
cache_public_key (pk);
945
/* Make the signature. */
946
rc = make_keysig_packet (&sig, pk, uid, NULL, sk, 0x13,
948
keygen_add_std_prefs, pk);
951
log_error("make_keysig_packet failed: %s\n", g10_errstr(rc) );
955
pkt = xmalloc_clear( sizeof *pkt );
956
pkt->pkttype = PKT_SIGNATURE;
957
pkt->pkt.signature = sig;
958
add_kbnode( sec_root, new_kbnode( pkt ) );
960
pkt = xmalloc_clear( sizeof *pkt );
961
pkt->pkttype = PKT_SIGNATURE;
962
pkt->pkt.signature = copy_signature(NULL,sig);
963
add_kbnode( pub_root, new_kbnode( pkt ) );
968
/* Write the key binding signature. If TIMESTAMP is not NULL use the
969
signature creation times. */
971
write_keybinding (KBNODE root, KBNODE pub_root,
972
PKT_secret_key *pri_sk, PKT_secret_key *sub_sk,
973
unsigned int use, u32 timestamp)
979
PKT_public_key *pri_pk, *sub_pk;
980
struct opaque_data_usage_and_pk oduap;
983
log_info(_("writing key binding signature\n"));
985
/* Get the pk packet from the pub_tree. */
986
node = find_kbnode ( pub_root, PKT_PUBLIC_KEY );
989
pri_pk = node->pkt->pkt.public_key;
991
/* We have to cache the key, so that the verification of the
992
* signature creation is able to retrieve the public key. */
993
cache_public_key (pri_pk);
995
/* Find the last subkey. */
997
for (node=pub_root; node; node = node->next )
999
if ( node->pkt->pkttype == PKT_PUBLIC_SUBKEY )
1000
sub_pk = node->pkt->pkt.public_key;
1005
/* Make the signature. */
1008
rc = make_keysig_packet (&sig, pri_pk, NULL, sub_pk, pri_sk, 0x18,
1010
keygen_add_key_flags_and_expire, &oduap );
1013
log_error ("make_keysig_packet failed: %s\n", g10_errstr(rc) );
1017
/* Make a backsig. */
1018
if (use&PUBKEY_USAGE_SIG)
1020
rc = make_backsig (sig, pri_pk, sub_pk, sub_sk, timestamp);
1025
pkt = xmalloc_clear ( sizeof *pkt );
1026
pkt->pkttype = PKT_SIGNATURE;
1027
pkt->pkt.signature = sig;
1028
add_kbnode (root, new_kbnode (pkt) );
1259
1704
static unsigned
1260
1705
ask_keysize( int algo )
1265
if (algo != PUBKEY_ALGO_DSA && algo != PUBKEY_ALGO_RSA) {
1266
tty_printf (_("About to generate a new %s keypair.\n"
1267
" minimum keysize is 768 bits\n"
1268
" default keysize is 1024 bits\n"
1269
" highest suggested keysize is 2048 bits\n"),
1270
gcry_pk_algo_name (algo) );
1274
answer = cpr_get("keygen.size",
1275
_("What keysize do you want? (1024) "));
1277
nbits = *answer? atoi(answer): 1024;
1279
if( algo == PUBKEY_ALGO_DSA && (nbits < 512 || nbits > 1024) )
1280
tty_printf(_("DSA only allows keysizes from 512 to 1024\n"));
1281
else if( algo == PUBKEY_ALGO_RSA && nbits < 1024 )
1282
tty_printf(_("keysize too small;"
1283
" 1024 is smallest value allowed for RSA.\n"));
1284
else if( nbits < 768 )
1285
tty_printf(_("keysize too small;"
1286
" 768 is smallest value allowed.\n"));
1287
else if( nbits > 4096 ) {
1288
/* It is ridiculous and an annoyance to use larger key sizes!
1289
* GnuPG can handle much larger sizes; but it takes an eternity
1290
* to create such a key (but less than the time the Sirius
1291
* Computer Corporation needs to process one of the usual
1292
* complaints) and {de,en}cryption although needs some time.
1293
* So, before you complain about this limitation, I suggest that
1294
* you start a discussion with Marvin about this theme and then
1295
* do whatever you want. */
1296
tty_printf(_("keysize too large; %d is largest value allowed.\n"),
1299
else if( nbits > 2048 && !cpr_enabled() ) {
1301
_("Keysizes larger than 2048 are not suggested because\n"
1302
"computations take REALLY long!\n"));
1303
if( cpr_get_answer_is_yes("keygen.size.huge.okay",_(
1304
"Are you sure that you want this keysize? ")) ) {
1305
tty_printf(_("Okay, but keep in mind that your monitor "
1306
"and keyboard radiation is also very vulnerable "
1314
tty_printf(_("Requested keysize is %u bits\n"), nbits );
1315
if( algo == PUBKEY_ALGO_DSA && (nbits % 64) ) {
1316
nbits = ((nbits + 63) / 64) * 64;
1317
tty_printf(_("rounded up to %u bits\n"), nbits );
1319
else if( (nbits % 32) ) {
1320
nbits = ((nbits + 31) / 32) * 32;
1321
tty_printf(_("rounded up to %u bits\n"), nbits );
1707
unsigned int nbits, min, def=2048, max=4096;
1716
case PUBKEY_ALGO_DSA:
1724
tty_printf(_("DSA keypair will have %u bits.\n"),1024);
1729
case PUBKEY_ALGO_RSA:
1734
tty_printf(_("%s keys may be between %u and %u bits long.\n"),
1735
gcry_pk_algo_name (algo), min, max);
1739
char *prompt,*answer;
1741
#define PROMPTSTRING _("What keysize do you want? (%u) ")
1743
prompt=xmalloc(strlen(PROMPTSTRING)+20);
1744
sprintf(prompt,PROMPTSTRING,def);
1748
answer = cpr_get("keygen.size",prompt);
1750
nbits = *answer? atoi(answer): def;
1754
if(nbits<min || nbits>max)
1755
tty_printf(_("%s keysizes must be in the range %u-%u\n"),
1756
gcry_pk_algo_name (algo), min, max);
1761
tty_printf(_("Requested keysize is %u bits\n"), nbits );
1763
if( algo == PUBKEY_ALGO_DSA && (nbits % 64) )
1765
nbits = ((nbits + 63) / 64) * 64;
1766
tty_printf(_("rounded up to %u bits\n"), nbits );
1768
else if( (nbits % 32) )
1770
nbits = ((nbits + 31) / 32) * 32;
1771
tty_printf(_("rounded up to %u bits\n"), nbits );
1327
1778
/****************
1328
* Parse an expire string and return it's value in days.
1329
* Returns -1 on error.
1779
* Parse an expire string and return its value in seconds.
1780
* Returns (u32)-1 on error.
1781
* This isn't perfect since scan_isodatestr returns unix time, and
1782
* OpenPGP actually allows a 32-bit time *plus* a 32-bit offset.
1783
* Because of this, we only permit setting expirations up to 2106, but
1784
* OpenPGP could theoretically allow up to 2242. I think we'll all
1785
* just cope for the next few years until we get a 64-bit time_t or
1332
1789
parse_expire_string( const char *string )
1336
u32 curtime = make_timestamp();
1792
u32 seconds,abs_date=0,curtime = make_timestamp();
1341
else if( (abs_date = scan_isodatestr(string)) && abs_date > curtime ) {
1342
/* This calculation is not perfectly okay because we
1343
* are later going to simply multiply by 86400 and don't
1344
* correct for leapseconds. A solution would be to change
1345
* the whole implemenation to work with dates and not intervals
1346
* which are required for v3 keys.
1348
valid_days = abs_date/86400-curtime/86400+1;
1350
else if( (mult=check_valid_days(string)) ) {
1351
valid_days = atoi(string) * mult;
1352
if( valid_days < 0 || valid_days > 39447 )
1796
else if ( !strncmp (string, "seconds=", 8) )
1797
seconds = atoi (string+8);
1798
else if( (abs_date = scan_isodatestr(string)) && abs_date > curtime )
1799
seconds = abs_date - curtime;
1800
else if( (mult=check_valid_days(string)) )
1801
seconds = atoi(string) * 86400L * mult;
1808
/* Parsean Creation-Date string which is either "1986-04-26" or
1809
"19860426T042640". Returns 0 on error. */
1811
parse_creation_string (const char *string)
1817
else if ( !strncmp (string, "seconds=", 8) )
1818
seconds = atoi (string+8);
1819
else if ( !(seconds = scan_isodatestr (string)))
1821
time_t tmp = isotime2epoch (string);
1822
seconds = (tmp == (time_t)(-1))? 0 : tmp;
1361
1828
/* object == 0 for a key, and 1 for a sig */
1363
ask_expire_interval(int object)
1830
ask_expire_interval(int object,const char *def_expire)
1372
1840
tty_printf(_("Please specify how long the key should be valid.\n"
1373
1841
" 0 = key does not expire\n"
1374
1842
" <n> = key expires in n days\n"
1917
2389
proc_parameter_file( struct para_data_s *para, const char *fname,
1918
2390
struct output_control_s *outctrl, int card )
1920
struct para_data_s *r;
1921
const char *s1, *s2, *s3;
1926
/* check that we have all required parameters */
1927
assert( get_parameter( para, pKEYTYPE ) );
1928
i = get_parameter_algo( para, pKEYTYPE );
1929
if( i < 1 || openpgp_pk_test_algo ( i, PUBKEY_USAGE_SIG ) ) {
1930
r = get_parameter( para, pKEYTYPE );
1931
log_error("%s:%d: invalid algorithm\n", fname, r->lnr );
1935
if (parse_parameter_usage (fname, para, pKEYUSAGE))
1938
i = get_parameter_algo( para, pSUBKEYTYPE );
1939
if( i > 0 && openpgp_pk_test_algo ( i, 0 ) ) {
1940
r = get_parameter( para, pSUBKEYTYPE );
1941
log_error("%s:%d: invalid algorithm\n", fname, r->lnr );
1944
if (i > 0 && parse_parameter_usage (fname, para, pSUBKEYUSAGE))
1948
if( !get_parameter_value( para, pUSERID ) ) {
1949
/* create the formatted user ID */
1950
s1 = get_parameter_value( para, pNAMEREAL );
1951
s2 = get_parameter_value( para, pNAMECOMMENT );
1952
s3 = get_parameter_value( para, pNAMEEMAIL );
1953
if( s1 || s2 || s3 ) {
1954
n = (s1?strlen(s1):0) + (s2?strlen(s2):0) + (s3?strlen(s3):0);
1955
r = xcalloc (1, sizeof *r + n + 20 );
1961
p = stpcpy(stpcpy(stpcpy(p," ("), s2 ),")");
1963
p = stpcpy(stpcpy(stpcpy(p," <"), s3 ),">");
1969
/* Set preferences, if any. */
1970
keygen_set_std_prefs(get_parameter_value( para, pPREFERENCES ), 0);
1972
/* Set revoker, if any. */
1973
if (parse_revocation_key (fname, para, pREVOKER))
1976
/* make DEK and S2K from the Passphrase */
1977
r = get_parameter( para, pPASSPHRASE );
1978
if( r && *r->u.value ) {
1979
/* we have a plain text passphrase - create a DEK from it.
1980
* It is a little bit ridiculous to keep it in secure memory
1981
* but because we do this always, why not here. */
1985
s2k = xmalloc_secure ( sizeof *s2k );
1986
s2k->mode = opt.s2k_mode;
1987
s2k->hash_algo = opt.s2k_digest_algo;
1988
set_next_passphrase( r->u.value );
1989
dek = passphrase_to_dek( NULL, 0, opt.s2k_cipher_algo, s2k, 2,
1991
set_next_passphrase( NULL );
1993
memset( r->u.value, 0, strlen(r->u.value) );
1995
r = xcalloc (1, sizeof *r );
1996
r->key = pPASSPHRASE_S2K;
2000
r = xcalloc (1, sizeof *r );
2001
r->key = pPASSPHRASE_DEK;
2007
/* make KEYEXPIRE from Expire-Date */
2008
r = get_parameter( para, pEXPIREDATE );
2009
if( r && *r->u.value ) {
2010
i = parse_expire_string( r->u.value );
2012
log_error("%s:%d: invalid expire date\n", fname, r->lnr );
2015
r->u.expire = i * 86400L;
2016
r->key = pKEYEXPIRE; /* change hat entry */
2017
/* also set it for the subkey */
2018
r = xcalloc (1, sizeof *r + 20 );
2019
r->key = pSUBKEYEXPIRE;
2020
r->u.expire = i * 86400L;
2025
if( !!outctrl->pub.newfname ^ !!outctrl->sec.newfname ) {
2026
log_error("%s:%d: only one ring name is set\n", fname, outctrl->lnr );
2030
do_generate_keypair( para, outctrl, card);
2392
struct para_data_s *r;
2393
const char *s1, *s2, *s3;
2396
int have_user_id=0,err,algo;
2398
/* Check that we have all required parameters. */
2399
r = get_parameter( para, pKEYTYPE );
2402
algo=get_parameter_algo(para,pKEYTYPE);
2403
if (openpgp_pk_test_algo2 (algo, PUBKEY_USAGE_SIG))
2405
log_error("%s:%d: invalid algorithm\n", fname, r->lnr );
2411
log_error("%s: no Key-Type specified\n",fname);
2415
err=parse_parameter_usage (fname, para, pKEYUSAGE);
2418
/* Default to algo capabilities if key-usage is not provided */
2419
r=xmalloc_clear(sizeof(*r));
2421
r->u.usage=openpgp_pk_algo_usage(algo);
2428
r = get_parameter( para, pSUBKEYTYPE );
2431
algo=get_parameter_algo( para, pSUBKEYTYPE);
2432
if (openpgp_pk_test_algo (algo))
2434
log_error("%s:%d: invalid algorithm\n", fname, r->lnr );
2438
err=parse_parameter_usage (fname, para, pSUBKEYUSAGE);
2441
/* Default to algo capabilities if subkey-usage is not
2443
r=xmalloc_clear(sizeof(*r));
2444
r->key=pSUBKEYUSAGE;
2445
r->u.usage=openpgp_pk_algo_usage(algo);
2453
if( get_parameter_value( para, pUSERID ) )
2457
/* create the formatted user ID */
2458
s1 = get_parameter_value( para, pNAMEREAL );
2459
s2 = get_parameter_value( para, pNAMECOMMENT );
2460
s3 = get_parameter_value( para, pNAMEEMAIL );
2461
if( s1 || s2 || s3 )
2463
n = (s1?strlen(s1):0) + (s2?strlen(s2):0) + (s3?strlen(s3):0);
2464
r = xmalloc_clear( sizeof *r + n + 20 );
2470
p = stpcpy(stpcpy(stpcpy(p," ("), s2 ),")");
2472
p = stpcpy(stpcpy(stpcpy(p," <"), s3 ),">");
2481
log_error("%s: no User-ID specified\n",fname);
2485
/* Set preferences, if any. */
2486
keygen_set_std_prefs(get_parameter_value( para, pPREFERENCES ), 0);
2488
/* Set keyserver, if any. */
2489
s1=get_parameter_value( para, pKEYSERVER );
2492
struct keyserver_spec *spec;
2494
spec=parse_keyserver_uri(s1,1,NULL,0);
2497
free_keyserver_spec(spec);
2498
opt.def_keyserver_url=s1;
2502
log_error("%s:%d: invalid keyserver url\n", fname, r->lnr );
2507
/* Set revoker, if any. */
2508
if (parse_revocation_key (fname, para, pREVOKER))
2511
/* make DEK and S2K from the Passphrase */
2512
r = get_parameter( para, pPASSPHRASE );
2513
if( r && *r->u.value ) {
2514
/* We have a plain text passphrase - create a DEK from it.
2515
* It is a little bit ridiculous to keep it ih secure memory
2516
* but because we do this always, why not here */
2520
s2k = xmalloc_secure( sizeof *s2k );
2521
s2k->mode = opt.s2k_mode;
2522
s2k->hash_algo = S2K_DIGEST_ALGO;
2523
set_next_passphrase( r->u.value );
2524
dek = passphrase_to_dek( NULL, 0, opt.s2k_cipher_algo, s2k, 2,
2526
set_next_passphrase( NULL );
2528
memset( r->u.value, 0, strlen(r->u.value) );
2530
r = xmalloc_clear( sizeof *r );
2531
r->key = pPASSPHRASE_S2K;
2535
r = xmalloc_clear( sizeof *r );
2536
r->key = pPASSPHRASE_DEK;
2542
/* Make KEYCREATIONDATE from Creation-Date. */
2543
r = get_parameter (para, pCREATIONDATE);
2544
if (r && *r->u.value)
2548
seconds = parse_creation_string (r->u.value);
2551
log_error ("%s:%d: invalid creation date\n", fname, r->lnr );
2554
r->u.creation = seconds;
2555
r->key = pKEYCREATIONDATE; /* Change that entry. */
2558
/* Make KEYEXPIRE from Expire-Date. */
2559
r = get_parameter( para, pEXPIREDATE );
2560
if( r && *r->u.value )
2564
seconds = parse_expire_string( r->u.value );
2565
if( seconds == (u32)-1 )
2567
log_error("%s:%d: invalid expire date\n", fname, r->lnr );
2570
r->u.expire = seconds;
2571
r->key = pKEYEXPIRE; /* change hat entry */
2572
/* also set it for the subkey */
2573
r = xmalloc_clear( sizeof *r + 20 );
2574
r->key = pSUBKEYEXPIRE;
2575
r->u.expire = seconds;
2580
if( !!outctrl->pub.newfname ^ !!outctrl->sec.newfname ) {
2581
log_error("%s:%d: only one ring name is set\n", fname, outctrl->lnr );
2585
do_generate_keypair( para, outctrl, card );
2035
2590
/****************
2036
2591
* Kludge to allow non interactive key generation controlled
2037
* by a parameter file (which currently is only stdin)
2592
* by a parameter file.
2038
2593
* Note, that string parameters are expected to be in UTF-8
2283
2870
strcpy (r->u.value, "encrypt");
2284
2871
r->next = para;
2287
2874
r = xcalloc (1, sizeof *r + 20 );
2288
2875
r->key = pAUTHKEYTYPE;
2289
2876
sprintf( r->u.value, "%d", algo );
2290
2877
r->next = para;
2880
if (backup_encryption_dir)
2882
r = xcalloc (1, sizeof *r + strlen (backup_encryption_dir) );
2883
r->key = pBACKUPENCDIR;
2884
strcpy (r->u.value, backup_encryption_dir);
2888
#endif /*ENABLE_CARD_SUPPORT*/
2295
algo = ask_algo (0, &use);
2892
algo = ask_algo( 0, &use );
2298
2894
{ /* default: DSA with ElG subkey of the specified size */
2300
r = xcalloc (1, sizeof *r + 20 );
2896
r = xmalloc_clear( sizeof *r + 20 );
2301
2897
r->key = pKEYTYPE;
2302
2898
sprintf( r->u.value, "%d", PUBKEY_ALGO_DSA );
2303
2899
r->next = para;
2305
tty_printf(_("DSA keypair will have 1024 bits.\n"));
2306
r = xcalloc (1, sizeof *r + 20 );
2307
r->key = pKEYLENGTH;
2308
strcpy( r->u.value, "1024" );
2311
r = xcalloc (1, sizeof *r + 20 );
2901
nbits = ask_keysize( PUBKEY_ALGO_DSA );
2902
r = xmalloc_clear( sizeof *r + 20 );
2903
r->key = pKEYLENGTH;
2904
sprintf( r->u.value, "%u", nbits);
2907
r = xmalloc_clear( sizeof *r + 20 );
2312
2908
r->key = pKEYUSAGE;
2313
2909
strcpy( r->u.value, "sign" );
2314
2910
r->next = para;
2317
2913
algo = PUBKEY_ALGO_ELGAMAL_E;
2318
r = xcalloc (1, sizeof *r + 20 );
2914
r = xmalloc_clear( sizeof *r + 20 );
2319
2915
r->key = pSUBKEYTYPE;
2320
2916
sprintf( r->u.value, "%d", algo );
2321
2917
r->next = para;
2323
r = xcalloc (1, sizeof *r + 20 );
2919
r = xmalloc_clear( sizeof *r + 20 );
2324
2920
r->key = pSUBKEYUSAGE;
2325
2921
strcpy( r->u.value, "encrypt" );
2326
2922
r->next = para;
2332
r = xcalloc (1, sizeof *r + 20 );
2927
r = xmalloc_clear( sizeof *r + 20 );
2333
2928
r->key = pKEYTYPE;
2334
2929
sprintf( r->u.value, "%d", algo );
2335
2930
r->next = para;
2340
r = xcalloc (1, sizeof *r + 20 );
2935
r = xmalloc_clear( sizeof *r + 25 );
2341
2936
r->key = pKEYUSAGE;
2342
sprintf( r->u.value, "%s%s",
2937
sprintf( r->u.value, "%s%s%s",
2343
2938
(use & PUBKEY_USAGE_SIG)? "sign ":"",
2344
(use & PUBKEY_USAGE_ENC)? "encrypt ":"" );
2939
(use & PUBKEY_USAGE_ENC)? "encrypt ":"",
2940
(use & PUBKEY_USAGE_AUTH)? "auth":"" );
2345
2941
r->next = para;
2350
2947
nbits = ask_keysize( algo );
2351
r = xcalloc (1, sizeof *r + 20 );
2948
r = xmalloc_clear( sizeof *r + 20 );
2352
2949
r->key = both? pSUBKEYLENGTH : pKEYLENGTH;
2353
2950
sprintf( r->u.value, "%u", nbits);
2354
2951
r->next = para;
2358
expire = ask_expire_interval(0);
2359
r = xcalloc (1, sizeof *r + 20 );
2955
expire = ask_expire_interval(0,NULL);
2956
r = xmalloc_clear( sizeof *r + 20 );
2360
2957
r->key = pKEYEXPIRE;
2361
2958
r->u.expire = expire;
2362
2959
r->next = para;
2364
r = xcalloc (1, sizeof *r + 20 );
2961
r = xmalloc_clear( sizeof *r + 20 );
2365
2962
r->key = pSUBKEYEXPIRE;
2366
2963
r->u.expire = expire;
2367
2964
r->next = para;
2370
2967
uid = ask_user_id(0);
2373
2970
log_error(_("Key generation canceled.\n"));
2374
2971
release_parameter_list( para );
2377
r = xcalloc (1, sizeof *r + strlen(uid) );
2974
r = xmalloc_clear( sizeof *r + strlen(uid) );
2378
2975
r->key = pUSERID;
2379
2976
strcpy( r->u.value, uid );
2380
2977
r->next = para;
2383
dek = card_serialno? NULL : ask_passphrase( &s2k );
2981
dek = card_serialno? NULL : do_ask_passphrase ( &s2k, &canceled );
2386
r = xcalloc (1, sizeof *r );
2984
r = xmalloc_clear( sizeof *r );
2387
2985
r->key = pPASSPHRASE_DEK;
2388
2986
r->u.dek = dek;
2389
2987
r->next = para;
2391
r = xcalloc (1, sizeof *r );
2989
r = xmalloc_clear( sizeof *r );
2392
2990
r->key = pPASSPHRASE_S2K;
2393
2991
r->u.s2k = s2k;
2394
2992
r->next = para;
2997
log_error (_("Key generation canceled.\n"));
2999
proc_parameter_file( para, "[internal]", &outctrl, !!card_serialno);
3000
release_parameter_list( para );
3004
#ifdef ENABLE_CARD_SUPPORT
3005
/* Generate a raw key and return it as a secret key packet. The
3006
function will ask for the passphrase and return a protected as well
3007
as an unprotected copy of a new secret key packet. 0 is returned
3008
on success and the caller must then free the returned values. */
3010
generate_raw_key (int algo, unsigned int nbits, u32 created_at,
3011
PKT_secret_key **r_sk_unprotected,
3012
PKT_secret_key **r_sk_protected)
3016
STRING2KEY *s2k = NULL;
3017
PKT_secret_key *sk = NULL;
3019
size_t nskey, npkey;
3020
gcry_sexp_t s_parms, s_key;
3023
npkey = pubkey_get_npkey (algo);
3024
nskey = pubkey_get_nskey (algo);
3025
assert (nskey <= PUBKEY_MAX_NSKEY && npkey < nskey);
3030
log_info (_("keysize invalid; using %u bits\n"), nbits );
3035
nbits = ((nbits + 31) / 32) * 32;
3036
log_info(_("keysize rounded up to %u bits\n"), nbits );
3039
dek = do_ask_passphrase (&s2k, &canceled);
3042
rc = gpg_error (GPG_ERR_CANCELED);
3046
sk = xmalloc_clear (sizeof *sk);
3047
sk->timestamp = created_at;
3049
sk->pubkey_algo = algo;
3051
if ( !is_RSA (algo) )
3053
log_error ("only RSA is supported for offline generated keys\n");
3054
rc = gpg_error (GPG_ERR_NOT_IMPLEMENTED);
3057
rc = gcry_sexp_build (&s_parms, NULL,
3058
"(genkey(rsa(nbits %d)))",
3061
log_bug ("gcry_sexp_build failed: %s\n", gpg_strerror (rc));
3062
rc = gcry_pk_genkey (&s_key, s_parms);
3063
gcry_sexp_release (s_parms);
3066
log_error ("gcry_pk_genkey failed: %s\n", gpg_strerror (rc) );
3069
rc = key_from_sexp (sk->skey, s_key, "private-key", "nedpqu");
3070
gcry_sexp_release (s_key);
3073
log_error ("key_from_sexp failed: %s\n", gpg_strerror (rc) );
2398
proc_parameter_file (para, "[internal]", &outctrl, !!card_serialno);
2399
release_parameter_list (para);
3077
for (i=npkey; i < nskey; i++)
3078
sk->csum += checksum_mpi (sk->skey[i]);
3080
if (r_sk_unprotected)
3081
*r_sk_unprotected = copy_secret_key (NULL, sk);
3083
rc = genhelp_protect (dek, s2k, sk);
3089
*r_sk_protected = sk;
3095
free_secret_key (sk);
3100
#endif /* ENABLE_CARD_SUPPORT */
3102
/* Create and delete a dummy packet to start off a list of kbnodes. */
2404
print_status_key_created (int letter, PKT_public_key *pk)
3104
start_tree(KBNODE *tree)
2406
byte array[MAX_FINGERPRINT_LEN], *s;
2407
char buf[MAX_FINGERPRINT_LEN*2+30], *p;
2413
fingerprint_from_pk (pk, array, &n);
2415
for (i=0; i < n ; i++, s++, p += 2)
2416
sprintf (p, "%02X", *s);
2418
write_status_text (STATUS_KEY_CREATED, buf);
3108
pkt=xmalloc_clear(sizeof(*pkt));
3109
pkt->pkttype=PKT_NONE;
3110
*tree=new_kbnode(pkt);
3111
delete_kbnode(*tree);
2427
3119
KBNODE pub_root = NULL;
2428
3120
KBNODE sec_root = NULL;
2429
PKT_secret_key *sk = NULL;
3121
PKT_secret_key *pri_sk = NULL, *sub_sk = NULL;
2431
3123
struct revocation_key *revkey;
2433
3125
int did_sub = 0;
2435
if (outctrl->dryrun)
3128
if( outctrl->dryrun )
2437
log_info ("dry-run mode - key generation skipped\n");
3130
log_info("dry-run mode - key generation skipped\n");
2442
if (outctrl->use_files)
3134
if ( outctrl->use_files )
2444
if (outctrl->pub.newfname)
2446
iobuf_close (outctrl->pub.stream);
2447
outctrl->pub.stream = NULL;
2448
xfree (outctrl->pub.fname);
2449
outctrl->pub.fname = outctrl->pub.newfname;
2450
outctrl->pub.newfname = NULL;
2452
outctrl->pub.stream = iobuf_create (outctrl->pub.fname);
2453
if (!outctrl->pub.stream)
2455
log_error ("can't create `%s': %s\n", outctrl->pub.fname,
2461
outctrl->pub.afx.what = 1;
2462
iobuf_push_filter (outctrl->pub.stream, armor_filter,
3136
if ( outctrl->pub.newfname )
3138
iobuf_close(outctrl->pub.stream);
3139
outctrl->pub.stream = NULL;
3140
if (outctrl->pub.fname)
3141
iobuf_ioctl (NULL, 2, 0, (char*)outctrl->pub.fname);
3142
xfree( outctrl->pub.fname );
3143
outctrl->pub.fname = outctrl->pub.newfname;
3144
outctrl->pub.newfname = NULL;
3146
if (is_secured_filename (outctrl->pub.fname) )
3148
outctrl->pub.stream = NULL;
3152
outctrl->pub.stream = iobuf_create( outctrl->pub.fname );
3153
if (!outctrl->pub.stream)
3155
log_error(_("can't create `%s': %s\n"), outctrl->pub.newfname,
3161
outctrl->pub.afx->what = 1;
3162
push_armor_filter (outctrl->pub.afx, outctrl->pub.stream);
2466
3165
if (outctrl->sec.newfname)
2468
iobuf_close (outctrl->sec.stream);
2469
outctrl->sec.stream = NULL;
2470
xfree (outctrl->sec.fname);
2471
outctrl->sec.fname = outctrl->sec.newfname;
2472
outctrl->sec.newfname = NULL;
3169
iobuf_close(outctrl->sec.stream);
3170
outctrl->sec.stream = NULL;
3171
if (outctrl->sec.fname)
3172
iobuf_ioctl (NULL, 2, 0, (char*)outctrl->sec.fname);
3173
xfree( outctrl->sec.fname );
3174
outctrl->sec.fname = outctrl->sec.newfname;
3175
outctrl->sec.newfname = NULL;
2474
outctrl->sec.stream = iobuf_create (outctrl->sec.fname);
2475
if (!outctrl->sec.stream)
2477
log_error ("can't create `%s': %s\n", outctrl->sec.fname,
2483
outctrl->sec.afx.what = 5;
2484
iobuf_push_filter (outctrl->sec.stream, armor_filter,
2488
assert (outctrl->pub.stream);
2489
assert (outctrl->sec.stream);
3177
oldmask = umask (077);
3178
if (is_secured_filename (outctrl->sec.fname) )
3180
outctrl->sec.stream = NULL;
3184
outctrl->sec.stream = iobuf_create( outctrl->sec.fname );
3186
if (!outctrl->sec.stream)
3188
log_error(_("can't create `%s': %s\n"), outctrl->sec.newfname,
3194
outctrl->sec.afx->what = 5;
3195
push_armor_filter (outctrl->sec.afx, outctrl->sec.stream);
3198
assert( outctrl->pub.stream );
3199
assert( outctrl->sec.stream );
2490
3200
if (opt.verbose)
2492
log_info (_("writing public key to `%s'\n"), outctrl->pub.fname);
3202
log_info (_("writing public key to `%s'\n"), outctrl->pub.fname );
2494
3204
log_info (_("writing secret key stub to `%s'\n"),
2495
3205
outctrl->sec.fname);
2497
log_info (_("writing secret key to `%s'\n"), outctrl->sec.fname);
3207
log_info(_("writing secret key to `%s'\n"), outctrl->sec.fname );
2502
/* We create the packets as a tree of kbnodes. Because the structure
2503
* we create is known in advance we simply generate a linked list.
2504
* The first packet is a dummy comment packet which we flag
2505
* as deleted. The very first packet must always be a KEY packet.
2507
pub_root = make_comment_node ("#");
2508
delete_kbnode (pub_root);
2509
sec_root = make_comment_node ("#");
2510
delete_kbnode (sec_root);
3212
/* We create the packets as a tree of kbnodes. Because the
3213
structure we create is known in advance we simply generate a
3214
linked list. The first packet is a dummy packet which we flag as
3215
deleted. The very first packet must always be a KEY packet. */
3217
start_tree (&pub_root);
3218
start_tree (&sec_root);
3220
timestamp = get_parameter_u32 (para, pKEYCREATIONDATE);
3222
timestamp = make_timestamp ();
2513
rc = do_create (get_parameter_algo (para, pKEYTYPE),
2514
get_parameter_uint (para, pKEYLENGTH),
3226
rc = do_create (get_parameter_algo( para, pKEYTYPE ),
3227
get_parameter_uint( para, pKEYLENGTH ),
2515
3228
pub_root, sec_root,
2516
get_parameter_dek (para, pPASSPHRASE_DEK),
2517
get_parameter_s2k (para, pPASSPHRASE_S2K),
2518
&sk, get_parameter_u32 (para, pKEYEXPIRE));
3229
get_parameter_dek( para, pPASSPHRASE_DEK ),
3230
get_parameter_s2k( para, pPASSPHRASE_S2K ),
3233
get_parameter_u32( para, pKEYEXPIRE ), 0 );
2522
rc = gen_card_key (PUBKEY_ALGO_RSA, 1, pub_root, sec_root,
3237
/* Note, that depending on the backend, the card key generation
3238
may update TIMESTAMP. */
3239
rc = gen_card_key (PUBKEY_ALGO_RSA, 1, 1, pub_root, sec_root, NULL,
2523
3241
get_parameter_u32 (para, pKEYEXPIRE), para);
2526
sk = sec_root->next->pkt->pkt.secret_key;
3244
pri_sk = sec_root->next->pkt->pkt.secret_key;
2532
if (!rc && (revkey = get_parameter_revkey (para, pREVOKER)))
2534
rc = write_direct_sig (pub_root, pub_root, sk, revkey);
2536
write_direct_sig (sec_root, pub_root, sk, revkey);
2539
if (!rc && (s = get_parameter_value (para, pUSERID)))
2541
write_uid (pub_root, s);
2543
write_uid (sec_root, s);
2545
rc = write_selfsig (pub_root, pub_root, sk,
2546
get_parameter_uint (para, pKEYUSAGE));
2548
rc = write_selfsig (sec_root, pub_root, sk,
2549
get_parameter_uint (para, pKEYUSAGE));
2552
if ((! rc) && get_parameter (para, pSUBKEYTYPE))
3249
if(!rc && (revkey=get_parameter_revkey(para,pREVOKER)))
3251
rc = write_direct_sig (pub_root, pub_root, pri_sk, revkey, timestamp);
3253
rc = write_direct_sig (sec_root, pub_root, pri_sk, revkey, timestamp);
3256
if( !rc && (s=get_parameter_value(para, pUSERID)) )
3258
write_uid (pub_root, s );
3259
write_uid (sec_root, s );
3261
rc = write_selfsigs (sec_root, pub_root, pri_sk,
3262
get_parameter_uint (para, pKEYUSAGE), timestamp);
3265
/* Write the auth key to the card before the encryption key. This
3266
is a partial workaround for a PGP bug (as of this writing, all
3267
versions including 8.1), that causes it to try and encrypt to
3268
the most recent subkey regardless of whether that subkey is
3269
actually an encryption type. In this case, the auth key is an
3270
RSA key so it succeeds. */
3272
if (!rc && card && get_parameter (para, pAUTHKEYTYPE))
3274
/* Note, that depending on the backend, the card key generation
3275
may update TIMESTAMP. */
3276
rc = gen_card_key (PUBKEY_ALGO_RSA, 3, 0, pub_root, sec_root, NULL,
3278
get_parameter_u32 (para, pKEYEXPIRE), para);
3281
rc = write_keybinding (pub_root, pub_root, pri_sk, sub_sk,
3282
PUBKEY_USAGE_AUTH, timestamp);
3284
rc = write_keybinding (sec_root, pub_root, pri_sk, sub_sk,
3285
PUBKEY_USAGE_AUTH, timestamp);
3288
if( !rc && get_parameter( para, pSUBKEYTYPE ) )
2556
rc = do_create (get_parameter_algo (para, pSUBKEYTYPE),
2557
get_parameter_uint (para, pSUBKEYLENGTH),
3292
rc = do_create( get_parameter_algo( para, pSUBKEYTYPE ),
3293
get_parameter_uint( para, pSUBKEYLENGTH ),
2558
3294
pub_root, sec_root,
2559
get_parameter_dek (para, pPASSPHRASE_DEK),
2560
get_parameter_s2k (para, pPASSPHRASE_S2K),
2561
NULL, get_parameter_u32 (para, pSUBKEYEXPIRE));
3295
get_parameter_dek( para, pPASSPHRASE_DEK ),
3296
get_parameter_s2k( para, pPASSPHRASE_S2K ),
3299
get_parameter_u32( para, pSUBKEYEXPIRE ), 1 );
2565
rc = gen_card_key (PUBKEY_ALGO_RSA, 2, pub_root, sec_root,
2566
get_parameter_u32 (para, pKEYEXPIRE), para);
3303
if ((s = get_parameter_value (para, pBACKUPENCDIR)))
3305
/* A backup of the encryption key has been requested.
3306
Generate the key in software and import it then to
3307
the card. Write a backup file. */
3308
rc = gen_card_key_with_backup (PUBKEY_ALGO_RSA, 2, 0,
3311
get_parameter_u32 (para,
3317
/* Note, that depending on the backend, the card key
3318
generation may update TIMESTAMP. */
3319
rc = gen_card_key (PUBKEY_ALGO_RSA, 2, 0, pub_root, sec_root,
3322
get_parameter_u32 (para, pKEYEXPIRE), para);
2570
rc = write_keybinding (pub_root, pub_root, sk,
2571
get_parameter_uint (para, pSUBKEYUSAGE));
2573
rc = write_keybinding (sec_root, pub_root, sk,
2574
get_parameter_uint (para, pSUBKEYUSAGE));
3327
rc = write_keybinding(pub_root, pub_root, pri_sk, sub_sk,
3328
get_parameter_uint (para, pSUBKEYUSAGE),
3331
rc = write_keybinding(sec_root, pub_root, pri_sk, sub_sk,
3332
get_parameter_uint (para, pSUBKEYUSAGE),
2578
if ((! rc) && card && get_parameter (para, pAUTHKEYTYPE))
3337
if (!rc && outctrl->use_files) /* Direct write to specified files. */
2580
rc = gen_card_key (PUBKEY_ALGO_RSA, 3, pub_root, sec_root,
2581
get_parameter_u32 (para, pKEYEXPIRE), para);
2584
rc = write_keybinding (pub_root, pub_root, sk, PUBKEY_USAGE_AUTH);
2586
rc = write_keybinding (sec_root, pub_root, sk, PUBKEY_USAGE_AUTH);
2590
if (!rc && outctrl->use_files)
2591
{ /* direct write to specified files */
2592
rc = write_keyblock (outctrl->pub.stream, pub_root);
3339
rc = write_keyblock( outctrl->pub.stream, pub_root );
2594
log_error ("can't write public key: %s\n", gpg_strerror (rc));
2597
rc = write_keyblock (outctrl->sec.stream, sec_root);
2599
log_error ("can't write secret key: %s\n", gpg_strerror (rc));
3341
log_error ("can't write public key: %s\n", g10_errstr(rc) );
3344
rc = write_keyblock( outctrl->sec.stream, sec_root );
3346
log_error ("can't write secret key: %s\n", g10_errstr(rc) );
2604
{ /* write to the standard keyrings */
3349
else if (!rc) /* Write to the standard keyrings. */
2605
3351
KEYDB_HANDLE pub_hd = keydb_new (0);
2606
3352
KEYDB_HANDLE sec_hd = keydb_new (1);
2608
/* FIXME: we may have to create the keyring first */
2609
3354
rc = keydb_locate_writable (pub_hd, NULL);
2611
log_error (_("no writable public keyring found: %s\n"),
2616
rc = keydb_locate_writable (sec_hd, NULL);
2618
log_error (_("no writable secret keyring found: %s\n"),
3356
log_error (_("no writable public keyring found: %s\n"),
3361
rc = keydb_locate_writable (sec_hd, NULL);
3363
log_error (_("no writable secret keyring found: %s\n"),
2622
3367
if (!rc && opt.verbose)
2624
log_info (_("writing public key to `%s'\n"),
2625
keydb_get_resource_name (pub_hd));
3369
log_info (_("writing public key to `%s'\n"),
3370
keydb_get_resource_name (pub_hd));
2627
3372
log_info (_("writing secret key stub to `%s'\n"),
2628
3373
keydb_get_resource_name (sec_hd));
2630
3375
log_info (_("writing secret key to `%s'\n"),
2631
3376
keydb_get_resource_name (sec_hd));
2636
rc = keydb_insert_keyblock (pub_hd, pub_root);
2638
log_error (_("error writing public keyring `%s': %s\n"),
2639
keydb_get_resource_name (pub_hd), gpg_strerror (rc));
2644
rc = keydb_insert_keyblock (sec_hd, sec_root);
2646
log_error (_("error writing secret keyring `%s': %s\n"),
2647
keydb_get_resource_name (pub_hd), gpg_strerror (rc));
3381
rc = keydb_insert_keyblock (pub_hd, pub_root);
3383
log_error (_("error writing public keyring `%s': %s\n"),
3384
keydb_get_resource_name (pub_hd), g10_errstr(rc));
3389
rc = keydb_insert_keyblock (sec_hd, sec_root);
3391
log_error (_("error writing secret keyring `%s': %s\n"),
3392
keydb_get_resource_name (pub_hd), g10_errstr(rc));
2650
3395
keydb_release (pub_hd);
2651
3396
keydb_release (sec_hd);
2656
get_parameter_algo (para, pKEYTYPE) == PUBKEY_ALGO_RSA
2657
&& get_parameter_uint (para, pKEYUSAGE)
2658
&& !(get_parameter_uint (para, pKEYUSAGE) & PUBKEY_USAGE_ENC);
2659
PKT_public_key *pk = find_kbnode (pub_root,
2660
PKT_PUBLIC_KEY)->pkt->pkt.
2663
update_ownertrust (pk,
2664
((get_ownertrust (pk) & ~TRUST_MASK)
2669
tty_printf (_("public and secret key created and signed.\n"));
2670
tty_printf (_("key marked as ultimately trusted.\n"));
2672
list_keyblock (pub_root, 0, 1, NULL);
2677
&& (get_parameter_algo (para, pKEYTYPE) == PUBKEY_ALGO_DSA
2678
|| no_enc_rsa) && !get_parameter (para, pSUBKEYTYPE))
2680
tty_printf (_("Note that this key cannot be used for "
2681
"encryption. You may want to use\n"
2682
"the command \"--edit-key\" to generate a "
2683
"secondary key for this purpose.\n"));
2686
if (!opt.batch && card)
2689
"Please create a revocation certificate now, so that you are able\n"
2690
"to revoke the key if it ever happens that you lose your card or\n"
2691
"the card gets damaged. Use the command \"--gen-revoke\".\n"
3403
no_enc_rsa = (get_parameter_algo (para, pKEYTYPE) == PUBKEY_ALGO_RSA
3404
&& get_parameter_uint (para, pKEYUSAGE)
3405
&& !((get_parameter_uint (para, pKEYUSAGE)
3406
& PUBKEY_USAGE_ENC)) );
3408
pk = find_kbnode (pub_root, PKT_PUBLIC_KEY)->pkt->pkt.public_key;
3410
keyid_from_pk(pk,pk->main_keyid);
3411
register_trusted_keyid(pk->main_keyid);
3413
update_ownertrust (pk, ((get_ownertrust (pk) & ~TRUST_MASK)
3414
| TRUST_ULTIMATE ));
3418
tty_printf (_("public and secret key created and signed.\n") );
3420
list_keyblock(pub_root,0,1,NULL);
3425
&& (get_parameter_algo (para, pKEYTYPE) == PUBKEY_ALGO_DSA
3427
&& !get_parameter (para, pSUBKEYTYPE) )
3429
tty_printf(_("Note that this key cannot be used for "
3430
"encryption. You may want to use\n"
3431
"the command \"--edit-key\" to generate a "
3432
"subkey for this purpose.\n") );
2700
log_error ("key generation failed: %s\n", gpg_strerror (rc));
3440
log_error ("key generation failed: %s\n", g10_errstr(rc) );
2702
tty_printf (_("Key generation failed: %s\n"), gpg_strerror (rc));
3442
tty_printf (_("Key generation failed: %s\n"), g10_errstr(rc) );
3443
print_status_key_not_created ( get_parameter_value (para, pHANDLE) );
2706
PKT_public_key *pk = find_kbnode (pub_root,
2707
PKT_PUBLIC_KEY)->pkt->pkt.public_key;
2708
print_status_key_created (did_sub ? 'B' : 'P', pk);
2711
release_kbnode (pub_root);
2712
release_kbnode (sec_root);
2713
if (sk && !card) /* The unprotected secret key unless we have */
2714
free_secret_key (sk); /* a shallow copy in card mode. */
2719
* add a new subkey to an existing key.
2720
* Returns true if a new key has been generated and put into the keyblocks.
2723
generate_subkeypair( KBNODE pub_keyblock, KBNODE sec_keyblock )
2727
PKT_secret_key *sk = NULL; /* this is the primary sk */
2732
char *passphrase = NULL;
2734
STRING2KEY *s2k = NULL;
2737
/* break out the primary secret key */
2738
node = find_kbnode( sec_keyblock, PKT_SECRET_KEY );
2740
log_error("Oops; secret key not found anymore!\n");
2744
/* make a copy of the sk to keep the protected one in the keyblock */
2745
sk = copy_secret_key( NULL, node->pkt->pkt.secret_key );
2747
cur_time = make_timestamp();
2748
if( sk->timestamp > cur_time ) {
2749
ulong d = sk->timestamp - cur_time;
2750
log_info( d==1 ? _("key has been created %lu second "
2751
"in future (time warp or clock problem)\n")
2752
: _("key has been created %lu seconds "
2753
"in future (time warp or clock problem)\n"), d );
2754
if( !opt.ignore_time_conflict ) {
2755
rc = GPG_ERR_TIME_CONFLICT;
3447
PKT_public_key *pk = find_kbnode (pub_root,
3448
PKT_PUBLIC_KEY)->pkt->pkt.public_key;
3449
print_status_key_created (did_sub? 'B':'P', pk,
3450
get_parameter_value (para, pHANDLE));
3452
release_kbnode( pub_root );
3453
release_kbnode( sec_root );
3455
if (pri_sk && !card) /* The unprotected secret key unless we */
3456
free_secret_key (pri_sk); /* have a shallow copy in card mode. */
3458
free_secret_key(sub_sk);
3462
/* Add a new subkey to an existing key. Returns true if a new key has
3463
been generated and put into the keyblocks. */
3465
generate_subkeypair (KBNODE pub_keyblock, KBNODE sec_keyblock)
3469
PKT_secret_key *pri_sk = NULL, *sub_sk = NULL;
3474
char *passphrase = NULL;
3476
STRING2KEY *s2k = NULL;
3481
/* Break out the primary secret key. */
3482
node = find_kbnode( sec_keyblock, PKT_SECRET_KEY );
3485
log_error ("Oops; secret key not found anymore!\n");
3489
/* Make a copy of the sk to keep the protected one in the keyblock. */
3490
pri_sk = copy_secret_key (NULL, node->pkt->pkt.secret_key);
3492
cur_time = make_timestamp();
3494
if (pri_sk->timestamp > cur_time)
3496
ulong d = pri_sk->timestamp - cur_time;
3497
log_info ( d==1 ? _("key has been created %lu second "
3498
"in future (time warp or clock problem)\n")
3499
: _("key has been created %lu seconds "
3500
"in future (time warp or clock problem)\n"), d );
3501
if (!opt.ignore_time_conflict)
3503
rc = G10ERR_TIME_CONFLICT;
3508
if (pri_sk->version < 4)
3510
log_info (_("NOTE: creating subkeys for v3 keys "
3511
"is not OpenPGP compliant\n"));
3515
if (pri_sk->is_protected && pri_sk->protect.s2k.mode == 1001)
3517
tty_printf (_("Secret parts of primary key are not available.\n"));
3518
rc = G10ERR_NO_SECKEY;
3523
/* Unprotect to get the passphrase. */
3524
switch (is_secret_key_protected (pri_sk) )
3527
rc = G10ERR_PUBKEY_ALGO;
3530
tty_printf (_("This key is not protected.\n"));
3533
tty_printf (_("Secret parts of primary key are stored on-card.\n"));
3537
tty_printf (_("Key is protected.\n"));
3538
rc = check_secret_key ( pri_sk, 0 );
3540
passphrase = get_last_passphrase();
3546
algo = ask_algo (1, &use);
3548
nbits = ask_keysize (algo);
3549
expire = ask_expire_interval (0, NULL);
3550
if (!cpr_enabled() && !cpr_get_answer_is_yes("keygen.sub.okay",
3551
_("Really create? (y/N) ")))
3556
dek = do_ask_passphrase (&s2k, &canceled);
3557
else if (passphrase)
3559
s2k = xmalloc_secure ( sizeof *s2k );
3560
s2k->mode = opt.s2k_mode;
3561
s2k->hash_algo = S2K_DIGEST_ALGO;
3562
set_next_passphrase ( passphrase );
3563
dek = passphrase_to_dek (NULL, 0, opt.s2k_cipher_algo, s2k, 2,
3568
rc = GPG_ERR_CANCELED;
3571
rc = do_create (algo, nbits, pub_keyblock, sec_keyblock,
3572
dek, s2k, &sub_sk, cur_time, expire, 1 );
3574
rc = write_keybinding (pub_keyblock, pub_keyblock, pri_sk, sub_sk,
3577
rc = write_keybinding (sec_keyblock, pub_keyblock, pri_sk, sub_sk,
3582
write_status_text (STATUS_KEY_CREATED, "S");
3587
log_error (_("Key generation failed: %s\n"), g10_errstr(rc) );
3591
/* Release the copy of the (now unprotected) secret keys. */
3593
free_secret_key (pri_sk);
3595
free_secret_key (sub_sk);
3596
set_next_passphrase (NULL);
3601
#ifdef ENABLE_CARD_SUPPORT
3602
/* Generate a subkey on a card. */
3604
generate_card_subkeypair (KBNODE pub_keyblock, KBNODE sec_keyblock,
3605
int keyno, const char *serialno)
3609
PKT_secret_key *pri_sk = NULL, *sub_sk;
3613
char *passphrase = NULL;
3615
struct para_data_s *para = NULL;
3617
assert (keyno >= 1 && keyno <= 3);
3619
para = xcalloc (1, sizeof *para + strlen (serialno) );
3620
para->key = pSERIALNO;
3621
strcpy (para->u.value, serialno);
3623
/* Break out the primary secret key */
3624
node = find_kbnode (sec_keyblock, PKT_SECRET_KEY);
3627
log_error("Oops; secret key not found anymore!\n");
3631
/* Make a copy of the sk to keep the protected one in the keyblock */
3632
pri_sk = copy_secret_key (NULL, node->pkt->pkt.secret_key);
3634
cur_time = make_timestamp();
3635
if (pri_sk->timestamp > cur_time)
3637
ulong d = pri_sk->timestamp - cur_time;
3638
log_info (d==1 ? _("key has been created %lu second "
3639
"in future (time warp or clock problem)\n")
3640
: _("key has been created %lu seconds "
3641
"in future (time warp or clock problem)\n"), d );
3642
if (!opt.ignore_time_conflict)
3644
rc = G10ERR_TIME_CONFLICT;
2760
if (sk->version < 4) {
2761
log_info (_("NOTE: creating subkeys for v3 keys "
2762
"is not OpenPGP compliant\n"));
2766
/* unprotect to get the passphrase */
2767
switch( is_secret_key_protected( sk ) ) {
2769
rc = GPG_ERR_PUBKEY_ALGO;
2772
tty_printf("This key is not protected.\n");
2775
tty_printf("Key is protected.\n");
2776
rc = check_secret_key( sk, 0 );
2778
passphrase = get_last_passphrase();
2785
algo = ask_algo( 1, &use );
2787
nbits = ask_keysize( algo );
2788
expire = ask_expire_interval(0);
2789
if( !cpr_enabled() && !cpr_get_answer_is_yes("keygen.sub.okay",
2790
_("Really create? ") ) )
2794
s2k = xmalloc_secure ( sizeof *s2k );
2795
s2k->mode = opt.s2k_mode;
2796
s2k->hash_algo = opt.s2k_digest_algo;
2797
set_next_passphrase( passphrase );
2798
dek = passphrase_to_dek( NULL, 0, opt.s2k_cipher_algo, s2k, 2,
2802
rc = do_create( algo, nbits, pub_keyblock, sec_keyblock,
2803
dek, s2k, NULL, expire );
2805
rc = write_keybinding(pub_keyblock, pub_keyblock, sk, use);
2807
rc = write_keybinding(sec_keyblock, pub_keyblock, sk, use);
2810
write_status_text (STATUS_KEY_CREATED, "S");
2815
log_error(_("Key generation failed: %s\n"), gpg_strerror (rc) );
2816
xfree ( passphrase );
2819
if( sk ) /* release the copy of the (now unprotected) secret key */
2820
free_secret_key(sk);
2821
set_next_passphrase( NULL );
3649
if (pri_sk->version < 4)
3651
log_info (_("NOTE: creating subkeys for v3 keys "
3652
"is not OpenPGP compliant\n"));
3656
/* Unprotect to get the passphrase. */
3657
switch( is_secret_key_protected (pri_sk) )
3660
rc = G10ERR_PUBKEY_ALGO;
3663
tty_printf("This key is not protected.\n");
3666
tty_printf("Key is protected.\n");
3667
rc = check_secret_key( pri_sk, 0 );
3669
passphrase = get_last_passphrase();
3675
algo = PUBKEY_ALGO_RSA;
3676
expire = ask_expire_interval (0,NULL);
3678
use = PUBKEY_USAGE_SIG;
3679
else if (keyno == 2)
3680
use = PUBKEY_USAGE_ENC;
3682
use = PUBKEY_USAGE_AUTH;
3683
if (!cpr_enabled() && !cpr_get_answer_is_yes("keygen.cardsub.okay",
3684
_("Really create? (y/N) ")))
3688
set_next_passphrase (passphrase);
3690
/* Note, that depending on the backend, the card key generation may
3692
rc = gen_card_key (algo, keyno, 0, pub_keyblock, sec_keyblock,
3693
&sub_sk, &cur_time, expire, para);
3695
rc = write_keybinding (pub_keyblock, pub_keyblock, pri_sk, sub_sk,
3698
rc = write_keybinding (sec_keyblock, pub_keyblock, pri_sk, sub_sk,
3703
write_status_text (STATUS_KEY_CREATED, "S");
3708
log_error (_("Key generation failed: %s\n"), g10_errstr(rc) );
3710
/* Release the copy of the (now unprotected) secret keys. */
3712
free_secret_key (pri_sk);
3713
set_next_passphrase( NULL );
3714
release_parameter_list (para);
3717
#endif /* !ENABLE_CARD_SUPPORT */
2826
3721
* Write a keyblock to an output stream
2829
write_keyblock( iobuf_t out, KBNODE node )
3724
write_keyblock( IOBUF out, KBNODE node )
2831
for( ; node ; node = node->next ) {
2832
int rc = build_packet( out, node->pkt );
2834
log_error("build_packet(%d) failed: %s\n",
2835
node->pkt->pkttype, gpg_strerror (rc) );
3726
for( ; node ; node = node->next )
3728
if(!is_deleted_kbnode(node))
3730
int rc = build_packet( out, node->pkt );
3733
log_error("build_packet(%d) failed: %s\n",
3734
node->pkt->pkttype, g10_errstr(rc) );
3744
/* Note that timestamp is an in/out arg. */
2845
gen_card_key (int algo, int keyno, KBNODE pub_root, KBNODE sec_root,
2846
u32 expireval, struct para_data_s *para)
3746
gen_card_key (int algo, int keyno, int is_primary,
3747
KBNODE pub_root, KBNODE sec_root, PKT_secret_key **ret_sk,
3748
u32 *timestamp, u32 expireval, struct para_data_s *para)
3750
#ifdef ENABLE_CARD_SUPPORT
2850
3753
struct agent_card_genkey_s info;
2899
3808
sk->protect.iv[sk->protect.ivlen] = xtoi_2 (s);
2902
pkt = xcalloc (1,sizeof *pkt);
2903
pkt->pkttype = keyno == 1 ? PKT_PUBLIC_KEY : PKT_PUBLIC_SUBKEY;
2904
pkt->pkt.public_key = pk;
2905
add_kbnode(pub_root, new_kbnode( pkt ));
2907
pkt = xcalloc (1,sizeof *pkt);
2908
pkt->pkttype = keyno == 1 ? PKT_SECRET_KEY : PKT_SECRET_SUBKEY;
2909
pkt->pkt.secret_key = sk;
2910
add_kbnode(sec_root, new_kbnode( pkt ));
3814
pkt = xcalloc (1,sizeof *pkt);
3815
pkt->pkttype = is_primary ? PKT_PUBLIC_KEY : PKT_PUBLIC_SUBKEY;
3816
pkt->pkt.public_key = pk;
3817
add_kbnode(pub_root, new_kbnode( pkt ));
3819
pkt = xcalloc (1,sizeof *pkt);
3820
pkt->pkttype = is_primary ? PKT_SECRET_KEY : PKT_SECRET_SUBKEY;
3821
pkt->pkt.secret_key = sk;
3822
add_kbnode(sec_root, new_kbnode( pkt ));
3827
#endif /*!ENABLE_CARD_SUPPORT*/
3833
gen_card_key_with_backup (int algo, int keyno, int is_primary,
3834
KBNODE pub_root, KBNODE sec_root,
3836
u32 expireval, struct para_data_s *para,
3837
const char *backup_dir)
3839
#ifdef ENABLE_CARD_SUPPORT
3843
PKT_secret_key *sk, *sk_unprotected = NULL, *sk_protected = NULL;
3848
rc = generate_raw_key (algo, 1024, timestamp,
3849
&sk_unprotected, &sk_protected);
3853
/* First, store the key to the card. */
3854
rc = save_unprotected_key_to_card (sk_unprotected, keyno);
3857
log_error (_("storing key onto card failed: %s\n"), g10_errstr (rc));
3858
free_secret_key (sk_unprotected);
3859
free_secret_key (sk_protected);
3863
/* Get rid of the secret key parameters and store the serial numer. */
3864
sk = sk_unprotected;
3865
n = pubkey_get_nskey (sk->pubkey_algo);
3866
for (i=pubkey_get_npkey (sk->pubkey_algo); i < n; i++)
3868
gcry_mpi_release (sk->skey[i]);
3871
i = pubkey_get_npkey (sk->pubkey_algo);
3872
sk->skey[i] = gcry_mpi_set_opaque (NULL, xstrdup ("dummydata"), 10*8);
3873
sk->is_protected = 1;
3874
sk->protect.s2k.mode = 1002;
3875
s = get_parameter_value (para, pSERIALNO);
3877
for (sk->protect.ivlen=0; sk->protect.ivlen < 16 && *s && s[1];
3878
sk->protect.ivlen++, s += 2)
3879
sk->protect.iv[sk->protect.ivlen] = xtoi_2 (s);
3881
/* Now write the *protected* secret key to the file. */
3883
char name_buffer[50];
3888
keyid_from_sk (sk, NULL);
3889
sprintf (name_buffer,"sk_%08lX%08lX.gpg",
3890
(ulong)sk->keyid[0], (ulong)sk->keyid[1]);
3892
fname = make_filename (backup_dir, name_buffer, NULL);
3893
oldmask = umask (077);
3894
if (is_secured_filename (fname))
3900
fp = iobuf_create (fname);
3904
rc = gpg_error_from_syserror ();
3905
log_error (_("can't create backup file `%s': %s\n"),
3906
fname, strerror(errno) );
3908
free_secret_key (sk_unprotected);
3909
free_secret_key (sk_protected);
3913
pkt = xcalloc (1, sizeof *pkt);
3914
pkt->pkttype = PKT_SECRET_KEY;
3915
pkt->pkt.secret_key = sk_protected;
3916
sk_protected = NULL;
3918
rc = build_packet (fp, pkt);
3921
log_error("build packet failed: %s\n", g10_errstr(rc) );
3926
unsigned char array[MAX_FINGERPRINT_LEN];
3930
iobuf_ioctl (NULL, 2, 0, (char*)fname);
3931
log_info (_("NOTE: backup of card key saved to `%s'\n"), fname);
3933
fingerprint_from_sk (sk, array, &n);
3934
p = fprbuf = xmalloc (MAX_FINGERPRINT_LEN*2 + 1 + 1);
3935
for (i=0; i < n ; i++, p += 2)
3936
sprintf (p, "%02X", array[i]);
3940
write_status_text_and_buffer (STATUS_BACKUP_KEY_CREATED,
3942
fname, strlen (fname),
3951
free_secret_key (sk_unprotected);
3956
/* Create the public key from the secret key. */
3957
pk = xcalloc (1, sizeof *pk );
3958
pk->timestamp = sk->timestamp;
3959
pk->version = sk->version;
3961
pk->expiredate = sk->expiredate = sk->timestamp + expireval;
3962
pk->pubkey_algo = sk->pubkey_algo;
3963
n = pubkey_get_npkey (sk->pubkey_algo);
3964
for (i=0; i < n; i++)
3965
pk->pkey[i] = mpi_copy (sk->skey[i]);
3967
/* Build packets and add them to the node lists. */
3968
pkt = xcalloc (1,sizeof *pkt);
3969
pkt->pkttype = is_primary ? PKT_PUBLIC_KEY : PKT_PUBLIC_SUBKEY;
3970
pkt->pkt.public_key = pk;
3971
add_kbnode(pub_root, new_kbnode( pkt ));
3973
pkt = xcalloc (1,sizeof *pkt);
3974
pkt->pkttype = is_primary ? PKT_SECRET_KEY : PKT_SECRET_SUBKEY;
3975
pkt->pkt.secret_key = sk;
3976
add_kbnode(sec_root, new_kbnode( pkt ));
3981
#endif /*!ENABLE_CARD_SUPPORT*/
3985
#ifdef ENABLE_CARD_SUPPORT
3987
save_unprotected_key_to_card (PKT_secret_key *sk, int keyno)
3990
unsigned char *rsa_n = NULL;
3991
unsigned char *rsa_e = NULL;
3992
unsigned char *rsa_p = NULL;
3993
unsigned char *rsa_q = NULL;
3994
size_t rsa_n_len, rsa_e_len, rsa_p_len, rsa_q_len;
3995
unsigned char *sexp = NULL;
3997
char numbuf[55], numbuf2[50];
3999
assert (is_RSA (sk->pubkey_algo));
4000
assert (!sk->is_protected);
4002
/* Copy the parameters into straight buffers. */
4003
gcry_mpi_aprint (GCRYMPI_FMT_USG, &rsa_n, &rsa_n_len, sk->skey[0]);
4004
gcry_mpi_aprint (GCRYMPI_FMT_USG, &rsa_e, &rsa_e_len, sk->skey[1]);
4005
gcry_mpi_aprint (GCRYMPI_FMT_USG, &rsa_p, &rsa_p_len, sk->skey[3]);
4006
gcry_mpi_aprint (GCRYMPI_FMT_USG, &rsa_q, &rsa_q_len, sk->skey[4]);
4007
if (!rsa_n || !rsa_e || !rsa_p || !rsa_q)
4009
rc = G10ERR_INV_ARG;
4013
/* Put the key into an S-expression. */
4014
sexp = p = xmalloc_secure (30
4015
+ rsa_n_len + rsa_e_len + rsa_p_len + rsa_q_len
4016
+ 4*sizeof (numbuf) + 25 + sizeof(numbuf) + 20);
4018
p = stpcpy (p,"(11:private-key(3:rsa(1:n");
4019
sprintf (numbuf, "%u:", (unsigned int)rsa_n_len);
4020
p = stpcpy (p, numbuf);
4021
memcpy (p, rsa_n, rsa_n_len);
4024
sprintf (numbuf, ")(1:e%u:", (unsigned int)rsa_e_len);
4025
p = stpcpy (p, numbuf);
4026
memcpy (p, rsa_e, rsa_e_len);
4029
sprintf (numbuf, ")(1:p%u:", (unsigned int)rsa_p_len);
4030
p = stpcpy (p, numbuf);
4031
memcpy (p, rsa_p, rsa_p_len);
4034
sprintf (numbuf, ")(1:q%u:", (unsigned int)rsa_q_len);
4035
p = stpcpy (p, numbuf);
4036
memcpy (p, rsa_q, rsa_q_len);
4039
p = stpcpy (p,"))(10:created-at");
4040
sprintf (numbuf2, "%lu", (unsigned long)sk->timestamp);
4041
sprintf (numbuf, "%lu:", (unsigned long)strlen (numbuf2));
4042
p = stpcpy (stpcpy (stpcpy (p, numbuf), numbuf2), "))");
4044
/* Fixme: Unfortunately we don't have the serialnumber available -
4045
thus we can't pass it down to the agent. */
4046
rc = agent_scd_writekey (keyno, NULL, sexp, p - sexp);
4056
#endif /*ENABLE_CARD_SUPPORT*/