1
0
mirror of git://git.gnupg.org/gnupg.git synced 2024-06-07 23:27:48 +02:00
gnupg/g10/keygen.c

1007 lines
27 KiB
C
Raw Normal View History

1997-11-18 15:06:00 +01:00
/* keygen.c - generate a key pair
1998-02-24 19:50:46 +01:00
* Copyright (C) 1998 Free Software Foundation, Inc.
1997-11-18 15:06:00 +01:00
*
1998-02-24 19:50:46 +01:00
* This file is part of GNUPG.
1997-11-18 15:06:00 +01:00
*
1998-02-24 19:50:46 +01:00
* GNUPG is free software; you can redistribute it and/or modify
1997-11-18 15:06:00 +01:00
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
1998-02-24 19:50:46 +01:00
* GNUPG is distributed in the hope that it will be useful,
1997-11-18 15:06:00 +01:00
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*/
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
1998-01-12 11:18:17 +01:00
#include <ctype.h>
1997-11-18 15:06:00 +01:00
#include <errno.h>
#include <assert.h>
#include "util.h"
#include "main.h"
#include "packet.h"
#include "cipher.h"
#include "ttyio.h"
#include "options.h"
1997-12-16 20:15:09 +01:00
#include "keydb.h"
1998-01-28 17:09:43 +01:00
#include "i18n.h"
1997-11-18 15:06:00 +01:00
1997-11-24 23:24:04 +01:00
1998-06-13 19:00:02 +02:00
#if 0
#define ENABLE_RSA_KEYGEN 1
#endif
1997-11-18 15:06:00 +01:00
static void
1997-12-16 20:15:09 +01:00
write_uid( KBNODE root, const char *s )
1997-11-18 15:06:00 +01:00
{
1997-12-16 20:15:09 +01:00
PACKET *pkt = m_alloc_clear(sizeof *pkt );
1997-11-18 15:06:00 +01:00
size_t n = strlen(s);
1997-12-16 20:15:09 +01:00
pkt->pkttype = PKT_USER_ID;
pkt->pkt.user_id = m_alloc( sizeof *pkt->pkt.user_id + n - 1 );
pkt->pkt.user_id->len = n;
strcpy(pkt->pkt.user_id->name, s);
add_kbnode( root, new_kbnode( pkt ) );
1997-11-18 15:06:00 +01:00
}
1997-12-09 13:46:23 +01:00
static int
1997-12-16 20:15:09 +01:00
write_selfsig( KBNODE root, KBNODE pub_root, PKT_secret_cert *skc )
1997-12-09 13:46:23 +01:00
{
1997-12-16 20:15:09 +01:00
PACKET *pkt;
1997-12-09 13:46:23 +01:00
PKT_signature *sig;
1997-12-16 20:15:09 +01:00
PKT_user_id *uid;
1997-12-09 13:46:23 +01:00
int rc=0;
KBNODE node;
1997-12-16 20:15:09 +01:00
PKT_public_cert *pkc;
1997-12-09 13:46:23 +01:00
if( opt.verbose )
1998-01-28 17:09:43 +01:00
log_info(_("writing self signature\n"));
1997-12-09 13:46:23 +01:00
/* get the uid packet from the list */
node = find_kbnode( root, PKT_USER_ID );
1997-12-16 20:15:09 +01:00
if( !node )
1998-01-16 22:15:24 +01:00
BUG(); /* no user id packet in tree */
1997-12-16 20:15:09 +01:00
uid = node->pkt->pkt.user_id;
/* get the pkc packet from the pub_tree */
1998-02-12 00:22:09 +01:00
node = find_kbnode( pub_root, PKT_PUBLIC_CERT );
1997-12-16 20:15:09 +01:00
if( !node )
1998-01-16 22:15:24 +01:00
BUG();
1997-12-16 20:15:09 +01:00
pkc = node->pkt->pkt.public_cert;
/* and make the signature */
1998-05-26 15:38:00 +02:00
rc = make_keysig_packet( &sig, pkc, uid, NULL, skc, 0x13, 0 );
if( rc ) {
log_error("make_keysig_packet failed: %s\n", g10_errstr(rc) );
return rc;
}
pkt = m_alloc_clear( sizeof *pkt );
pkt->pkttype = PKT_SIGNATURE;
pkt->pkt.signature = sig;
add_kbnode( root, new_kbnode( pkt ) );
return rc;
}
static int
write_keybinding( KBNODE root, KBNODE pub_root, PKT_secret_cert *skc )
{
PACKET *pkt;
PKT_signature *sig;
int rc=0;
KBNODE node;
PKT_public_cert *pkc, *subpkc;
if( opt.verbose )
log_info(_("writing key binding signature\n"));
/* get the pkc packet from the pub_tree */
node = find_kbnode( pub_root, PKT_PUBLIC_CERT );
if( !node )
BUG();
pkc = node->pkt->pkt.public_cert;
/* find the last subkey */
subpkc = NULL;
for(node=pub_root; node; node = node->next ) {
if( node->pkt->pkttype == PKT_PUBKEY_SUBCERT )
subpkc = node->pkt->pkt.public_cert;
}
if( !subpkc )
BUG();
/* and make the signature */
rc = make_keysig_packet( &sig, pkc, NULL, subpkc, skc, 0x18, 0 );
1997-12-09 13:46:23 +01:00
if( rc ) {
log_error("make_keysig_packet failed: %s\n", g10_errstr(rc) );
return rc;
}
1997-12-16 20:15:09 +01:00
pkt = m_alloc_clear( sizeof *pkt );
pkt->pkttype = PKT_SIGNATURE;
pkt->pkt.signature = sig;
add_kbnode( root, new_kbnode( pkt ) );
1997-12-09 13:46:23 +01:00
return rc;
}
1997-12-16 20:15:09 +01:00
static int
gen_elg(unsigned nbits, KBNODE pub_root, KBNODE sec_root, DEK *dek,
1998-05-26 15:38:00 +02:00
STRING2KEY *s2k, PKT_secret_cert **ret_skc, u16 valid_days,
int version )
1997-12-16 20:15:09 +01:00
{
int rc;
1998-02-09 18:43:42 +01:00
int i;
1997-12-16 20:15:09 +01:00
PACKET *pkt;
PKT_secret_cert *skc;
PKT_public_cert *pkc;
1998-06-13 19:00:02 +02:00
MPI skey[4];
1998-02-09 18:43:42 +01:00
MPI *factors;
1997-12-16 20:15:09 +01:00
1998-06-13 19:00:02 +02:00
rc = pubkey_generate( PUBKEY_ALGO_ELGAMAL, nbits, skey, &factors );
if( rc ) {
log_error("pubkey_generate failed: %s\n", g10_errstr(rc) );
return rc;
}
1997-12-16 20:15:09 +01:00
1998-01-16 22:15:24 +01:00
skc = m_alloc_clear( sizeof *skc );
pkc = m_alloc_clear( sizeof *pkc );
1997-12-16 20:15:09 +01:00
skc->timestamp = pkc->timestamp = make_timestamp();
1998-05-26 15:38:00 +02:00
skc->version = pkc->version = version;
1998-04-02 12:30:03 +02:00
skc->valid_days = pkc->valid_days = valid_days;
1997-12-16 20:15:09 +01:00
skc->pubkey_algo = pkc->pubkey_algo = PUBKEY_ALGO_ELGAMAL;
1998-06-13 19:00:02 +02:00
pkc->pkey[0] = mpi_copy( skey[0] );
pkc->pkey[1] = mpi_copy( skey[1] );
pkc->pkey[2] = mpi_copy( skey[2] );
skc->skey[0] = skey[0];
skc->skey[1] = skey[1];
skc->skey[2] = skey[2];
skc->skey[3] = skey[3];
1998-04-02 12:30:03 +02:00
skc->is_protected = 0;
skc->protect.algo = 0;
1997-12-16 20:15:09 +01:00
skc->csum = checksum_mpi_counted_nbits( skc->skey[3] );
1998-05-26 15:38:00 +02:00
if( ret_skc ) /* not a subkey: return an unprotected version of the skc */
*ret_skc = copy_secret_cert( NULL, skc );
1997-12-16 20:15:09 +01:00
if( dek ) {
1998-05-03 17:42:08 +02:00
skc->protect.algo = dek->algo;
skc->protect.s2k = *s2k;
1997-12-16 20:15:09 +01:00
rc = protect_secret_key( skc, dek );
if( rc ) {
log_error("protect_secret_key failed: %s\n", g10_errstr(rc) );
free_public_cert(pkc);
free_secret_cert(skc);
return rc;
}
}
pkt = m_alloc_clear(sizeof *pkt);
1998-05-26 15:38:00 +02:00
pkt->pkttype = ret_skc ? PKT_PUBLIC_CERT : PKT_PUBKEY_SUBCERT;
1997-12-16 20:15:09 +01:00
pkt->pkt.public_cert = pkc;
add_kbnode(pub_root, new_kbnode( pkt ));
1998-04-14 19:51:16 +02:00
/* don't know whether it makes sense to have the factors, so for now
1998-05-03 17:42:08 +02:00
* we store them in the secret keyring (but they are not secret) */
1997-12-16 20:15:09 +01:00
pkt = m_alloc_clear(sizeof *pkt);
1998-05-26 15:38:00 +02:00
pkt->pkttype = ret_skc ? PKT_SECRET_CERT : PKT_SECKEY_SUBCERT;
1997-12-16 20:15:09 +01:00
pkt->pkt.secret_cert = skc;
add_kbnode(sec_root, new_kbnode( pkt ));
1998-02-09 18:43:42 +01:00
for(i=0; factors[i]; i++ )
add_kbnode( sec_root,
make_mpi_comment_node("#:ELG_factor:", factors[i] ));
1997-12-16 20:15:09 +01:00
return 0;
}
#ifdef ENABLE_RSA_KEYGEN
1997-11-18 15:06:00 +01:00
static int
1998-01-28 17:09:43 +01:00
gen_rsa(unsigned nbits, KBNODE pub_root, KBNODE sec_root, DEK *dek,
1998-05-03 17:42:08 +02:00
STRING2KEY *s2k, PKT_secret_cert **ret_skc, u16 valid_days )
1997-11-18 15:06:00 +01:00
{
int rc;
1998-01-28 17:09:43 +01:00
PACKET *pkt;
1997-12-01 11:33:23 +01:00
PKT_secret_cert *skc;
PKT_public_cert *pkc;
1997-11-18 15:06:00 +01:00
RSA_public_key pk;
RSA_secret_key sk;
rsa_generate( &pk, &sk, nbits );
1998-01-16 22:15:24 +01:00
skc = m_alloc_clear( sizeof *skc );
pkc = m_alloc_clear( sizeof *pkc );
1997-11-18 15:06:00 +01:00
skc->timestamp = pkc->timestamp = make_timestamp();
1998-04-02 12:30:03 +02:00
skc->valid_days = pkc->valid_days = valid_days;
1997-11-18 15:06:00 +01:00
skc->pubkey_algo = pkc->pubkey_algo = PUBKEY_ALGO_RSA;
memset(&pkc->mfx, 0, sizeof pkc->mfx);
pkc->d.rsa.rsa_n = pk.n;
pkc->d.rsa.rsa_e = pk.e;
skc->d.rsa.rsa_n = sk.n;
skc->d.rsa.rsa_e = sk.e;
skc->d.rsa.rsa_d = sk.d;
skc->d.rsa.rsa_p = sk.p;
skc->d.rsa.rsa_q = sk.q;
skc->d.rsa.rsa_u = sk.u;
skc->d.rsa.csum = checksum_mpi_counted_nbits( skc->d.rsa.rsa_d );
skc->d.rsa.csum += checksum_mpi_counted_nbits( skc->d.rsa.rsa_p );
skc->d.rsa.csum += checksum_mpi_counted_nbits( skc->d.rsa.rsa_q );
skc->d.rsa.csum += checksum_mpi_counted_nbits( skc->d.rsa.rsa_u );
1998-01-28 17:09:43 +01:00
1998-05-26 15:38:00 +02:00
if( ret_skc ) /* not a subkey: return an unprotected version of the skc */
*ret_skc = copy_secret_cert( NULL, skc );
1998-01-28 17:09:43 +01:00
if( dek ) {
1997-12-09 13:46:23 +01:00
skc->d.rsa.is_protected = 1;
skc->d.rsa.protect_algo = CIPHER_ALGO_BLOWFISH;
randomize_buffer( skc->d.rsa.protect.blowfish.iv, 8, 1);
skc->d.rsa.csum += checksum_counted_nbits(
skc->d.rsa.protect.blowfish.iv, 8 );
1997-12-09 13:46:23 +01:00
rc = protect_secret_key( skc, dek );
if( rc ) {
log_error("protect_secret_key failed: %s\n", g10_errstr(rc) );
1998-01-28 17:09:43 +01:00
free_public_cert(pkc);
free_secret_cert(skc);
return rc;
1997-12-09 13:46:23 +01:00
}
}
1997-11-18 15:06:00 +01:00
1998-01-28 17:09:43 +01:00
pkt = m_alloc_clear(sizeof *pkt);
1998-05-26 15:38:00 +02:00
pkt->pkttype = ret_skc ? PKT_PUBLIC_CERT : PKT_PUBKEY_SUBCERT;
1998-01-28 17:09:43 +01:00
pkt->pkt.public_cert = pkc;
add_kbnode(pub_root, new_kbnode( pkt ));
pkt = m_alloc_clear(sizeof *pkt);
1998-05-26 15:38:00 +02:00
pkt->pkttype = ret_skc ? PKT_SECRET_CERT : PKT_SECKEY_SUBCERT;
1998-01-28 17:09:43 +01:00
pkt->pkt.secret_cert = skc;
add_kbnode(sec_root, new_kbnode( pkt ));
1997-11-18 15:06:00 +01:00
return rc;
}
#endif /*ENABLE_RSA_KEYGEN*/
1997-11-24 23:24:04 +01:00
1997-12-16 20:15:09 +01:00
1998-05-05 22:34:20 +02:00
/****************
* Generate a DSA key
*/
1997-11-24 23:24:04 +01:00
static int
1997-12-16 20:15:09 +01:00
gen_dsa(unsigned nbits, KBNODE pub_root, KBNODE sec_root, DEK *dek,
1998-05-05 22:34:20 +02:00
STRING2KEY *s2k, PKT_secret_cert **ret_skc, u16 valid_days )
1997-11-24 23:24:04 +01:00
{
1998-05-05 22:34:20 +02:00
int rc;
int i;
PACKET *pkt;
PKT_secret_cert *skc;
PKT_public_cert *pkc;
1998-06-13 19:00:02 +02:00
MPI skey[5];
1998-05-05 22:34:20 +02:00
MPI *factors;
if( nbits > 1024 )
nbits = 1024;
1998-06-13 19:00:02 +02:00
rc = pubkey_generate( PUBKEY_ALGO_DSA, nbits, skey, &factors );
if( rc ) {
log_error("pubkey_generate failed: %s\n", g10_errstr(rc) );
return rc;
}
1998-05-05 22:34:20 +02:00
skc = m_alloc_clear( sizeof *skc );
pkc = m_alloc_clear( sizeof *pkc );
skc->timestamp = pkc->timestamp = make_timestamp();
skc->version = pkc->version = 4;
/* valid days are not stored in the packet, but it is
* used here to put it into the signature.
*/
skc->valid_days = pkc->valid_days = valid_days;
skc->pubkey_algo = pkc->pubkey_algo = PUBKEY_ALGO_DSA;
1998-06-13 19:00:02 +02:00
pkc->pkey[0] = skey[0];
pkc->pkey[1] = skey[1];
pkc->pkey[2] = skey[2];
pkc->pkey[3] = skey[3];
skc->skey[0] = skey[0];
skc->skey[1] = skey[1];
skc->skey[2] = skey[2];
skc->skey[3] = skey[3];
skc->skey[4] = skey[4];
1998-05-05 22:34:20 +02:00
skc->is_protected = 0;
skc->protect.algo = 0;
skc->csum = checksum_mpi_counted_nbits( skc->skey[4] );
1998-05-26 15:38:00 +02:00
if( ret_skc ) /* not a subkey: return an unprotected version of the skc */
*ret_skc = copy_secret_cert( NULL, skc );
1998-05-05 22:34:20 +02:00
if( dek ) {
skc->protect.algo = dek->algo;
skc->protect.s2k = *s2k;
rc = protect_secret_key( skc, dek );
if( rc ) {
log_error("protect_secret_key failed: %s\n", g10_errstr(rc) );
free_public_cert(pkc);
free_secret_cert(skc);
return rc;
}
}
pkt = m_alloc_clear(sizeof *pkt);
1998-05-26 15:38:00 +02:00
pkt->pkttype = ret_skc ? PKT_PUBLIC_CERT : PKT_PUBKEY_SUBCERT;
1998-05-05 22:34:20 +02:00
pkt->pkt.public_cert = pkc;
add_kbnode(pub_root, new_kbnode( pkt ));
/* don't know whether it makes sense to have the factors, so for now
* we store them in the secret keyring (but they are not secret)
* p = 2 * q * f1 * f2 * ... * fn
1998-05-13 19:53:36 +02:00
* We store only f1 to f_n-1; fn can be calculated because p and q
1998-05-05 22:34:20 +02:00
* are known.
*/
pkt = m_alloc_clear(sizeof *pkt);
1998-05-26 15:38:00 +02:00
pkt->pkttype = ret_skc ? PKT_SECRET_CERT : PKT_SECKEY_SUBCERT;
1998-05-05 22:34:20 +02:00
pkt->pkt.secret_cert = skc;
add_kbnode(sec_root, new_kbnode( pkt ));
for(i=1; factors[i]; i++ ) /* the first one is q */
add_kbnode( sec_root,
make_mpi_comment_node("#:DSA_factor:", factors[i] ));
return 0;
1997-11-24 23:24:04 +01:00
}
1997-11-18 15:06:00 +01:00
1998-04-02 12:30:03 +02:00
/****************
* check valid days:
* return 0 on error or the multiplier
*/
static int
check_valid_days( const char *s )
{
if( !isdigit(*s) )
return 0;
for( s++; *s; s++)
if( !isdigit(*s) )
break;
if( !*s )
return 1;
if( s[1] )
return 0; /* e.g. "2323wc" */
if( *s == 'd' || *s == 'D' )
return 1;
if( *s == 'w' || *s == 'W' )
return 7;
if( *s == 'm' || *s == 'M' )
return 30;
if( *s == 'y' || *s == 'Y' )
return 365;
return 0;
}
1998-05-26 15:38:00 +02:00
static int
ask_algo( int *ret_v4 )
1997-11-18 15:06:00 +01:00
{
char *answer;
1997-11-24 23:24:04 +01:00
int algo;
1998-01-28 17:09:43 +01:00
tty_printf(_("Please select the algorithm to use:\n"
" (1) ElGamal is the suggested one.\n"
1998-05-26 15:38:00 +02:00
" (2) ElGamal using v4 packets (OpenPGP)\n"
" (3) DSA can only be used for signatures.\n"));
1998-01-28 17:09:43 +01:00
#ifdef ENABLE_RSA_KEYGEN
1998-05-26 15:38:00 +02:00
tty_printf(_(" (4) RSA cannot be used in the U.S.\n"));
1997-11-24 23:24:04 +01:00
#endif
1998-05-26 15:38:00 +02:00
*ret_v4 = 0;
1997-11-24 23:24:04 +01:00
for(;;) {
1998-04-02 12:30:03 +02:00
#ifdef ENABLE_RSA_KEYGEN
1998-05-26 15:38:00 +02:00
answer = tty_get(_("Your selection? (1,2,3,4) "));
1997-11-24 23:24:04 +01:00
#else
1998-05-26 15:38:00 +02:00
answer = tty_get(_("Your selection? (1,2,3) "));
1998-04-02 12:30:03 +02:00
#endif
1997-11-24 23:24:04 +01:00
tty_kill_prompt();
algo = *answer? atoi(answer): 1;
m_free(answer);
1998-05-26 15:38:00 +02:00
if( algo == 1 || algo == 2 ) {
if( algo == 2 )
*ret_v4 = 1;
1997-11-24 23:24:04 +01:00
algo = PUBKEY_ALGO_ELGAMAL;
break;
}
1998-05-26 15:38:00 +02:00
else if( algo == 3 ) {
*ret_v4 = 1;
algo = PUBKEY_ALGO_DSA;
1998-05-05 22:34:20 +02:00
break;
}
#ifdef ENABLE_RSA_KEYGEN
1998-05-26 15:38:00 +02:00
else if( algo == 4 ) {
1997-11-24 23:24:04 +01:00
algo = PUBKEY_ALGO_RSA;
break;
}
#endif
}
1998-05-26 15:38:00 +02:00
return algo;
}
1997-11-24 23:24:04 +01:00
1998-05-26 15:38:00 +02:00
static unsigned
ask_keysize( int algo )
{
char *answer;
unsigned nbits;
1997-11-24 23:24:04 +01:00
1998-01-28 17:09:43 +01:00
tty_printf(_("About to generate a new %s keypair.\n"
" minimum keysize is 768 bits\n"
" default keysize is 1024 bits\n"
1998-05-26 15:38:00 +02:00
" highest suggested keysize is 2048 bits\n"),
pubkey_algo_to_string(algo) );
1997-11-18 15:06:00 +01:00
for(;;) {
1998-01-28 17:09:43 +01:00
answer = tty_get(_("What keysize do you want? (1024) "));
1997-11-18 15:06:00 +01:00
tty_kill_prompt();
1997-11-24 23:24:04 +01:00
nbits = *answer? atoi(answer): 1024;
1997-11-18 15:06:00 +01:00
m_free(answer);
1997-12-16 20:15:09 +01:00
if( algo == PUBKEY_ALGO_DSA && (nbits < 512 || nbits > 1024) )
1998-04-14 19:51:16 +02:00
tty_printf(_("DSA only allows keysizes from 512 to 1024\n"));
1997-12-20 18:23:29 +01:00
else if( nbits < 768 )
1998-01-28 17:09:43 +01:00
tty_printf(_("keysize too small; 768 is smallest value allowed.\n"));
1997-11-18 15:06:00 +01:00
else if( nbits > 2048 ) {
1998-01-28 17:09:43 +01:00
tty_printf(_("Keysizes larger than 2048 are not suggested, because "
"computations take REALLY long!\n"));
answer = tty_get(_("Are you sure, that you want this keysize? "));
1997-11-18 15:06:00 +01:00
tty_kill_prompt();
if( answer_is_yes(answer) ) {
m_free(answer);
1998-01-28 17:09:43 +01:00
tty_printf(_("Okay, but keep in mind that your monitor "
"and keyboard radiation is also very vulnerable "
"to attacks!\n"));
1997-11-18 15:06:00 +01:00
break;
}
m_free(answer);
}
1998-04-02 12:30:03 +02:00
else if( nbits > 1536 ) {
answer = tty_get(_("Do you really need such a large keysize? "));
tty_kill_prompt();
if( answer_is_yes(answer) ) {
m_free(answer);
break;
}
m_free(answer);
}
1997-11-18 15:06:00 +01:00
else
break;
}
1998-01-28 17:09:43 +01:00
tty_printf(_("Requested keysize is %u bits\n"), nbits );
1997-12-16 20:15:09 +01:00
if( algo == PUBKEY_ALGO_DSA && (nbits % 64) ) {
nbits = ((nbits + 63) / 64) * 64;
1998-01-28 17:09:43 +01:00
tty_printf(_("rounded up to %u bits\n"), nbits );
1997-12-16 20:15:09 +01:00
}
else if( (nbits % 32) ) {
1997-11-18 15:06:00 +01:00
nbits = ((nbits + 31) / 32) * 32;
1998-01-28 17:09:43 +01:00
tty_printf(_("rounded up to %u bits\n"), nbits );
1997-11-18 15:06:00 +01:00
}
1998-05-26 15:38:00 +02:00
return nbits;
}
static int
ask_valid_days()
{
char *answer;
int valid_days=0;
1997-11-24 23:24:04 +01:00
1998-04-02 12:30:03 +02:00
tty_printf(_("Please specify how long the key should be valid.\n"
" 0 = key does not expire\n"
" <n> = key expires in n days\n"
" <n>w = key expires in n weeks\n"
" <n>m = key expires in n months\n"
" <n>y = key expires in n years\n"));
1998-05-05 22:34:20 +02:00
/* Note: The elgamal subkey for DSA has no exiration date because
* is must be signed with the DSA key and this one has the expiration
* date */
1998-04-02 12:30:03 +02:00
answer = NULL;
for(;;) {
int mult;
m_free(answer);
answer = tty_get(_("Key is valid for? (0) "));
tty_kill_prompt();
trim_spaces(answer);
if( !*answer )
valid_days = 0;
else if( (mult=check_valid_days(answer)) ) {
valid_days = atoi(answer) * mult;
if( valid_days < 0 || valid_days > 32767 )
valid_days = 0;
}
else {
tty_printf(_("invalid value\n"));
continue;
}
if( !valid_days )
tty_printf(_("Key does not expire at all\n"));
else {
tty_printf(_("Key expires at %s\n"), strtimestamp(
add_days_to_timestamp( make_timestamp(), valid_days )));
}
m_free(answer);
answer = tty_get(_("Is this correct (y/n)? "));
tty_kill_prompt();
if( answer_is_yes(answer) )
break;
}
m_free(answer);
1998-05-26 15:38:00 +02:00
return valid_days;
}
1998-04-02 12:30:03 +02:00
1998-05-26 15:38:00 +02:00
static char *
ask_user_id()
{
char *answer;
char *aname, *acomment, *amail, *uid;
1998-04-02 12:30:03 +02:00
1998-01-28 17:09:43 +01:00
tty_printf( _("\n"
1998-01-07 21:47:46 +01:00
"You need a User-ID to identify your key; the software constructs the user id\n"
"from Real Name, Comment and Email Address in this form:\n"
1998-04-02 12:30:03 +02:00
" \"Heinrich Heine (Der Dichter) <heinrichh@duesseldorf.de>\"\n\n") );
1998-05-26 15:38:00 +02:00
uid = aname = acomment = amail = NULL;
1997-11-18 15:06:00 +01:00
for(;;) {
1998-01-07 21:47:46 +01:00
char *p;
if( !aname ) {
for(;;) {
m_free(aname);
1998-01-28 17:09:43 +01:00
aname = tty_get(_("Real name: "));
1998-01-07 21:47:46 +01:00
trim_spaces(aname);
tty_kill_prompt();
if( strpbrk( aname, "<([])>" ) )
1998-01-28 17:09:43 +01:00
tty_printf(_("Invalid character in name\n"));
1998-01-12 11:18:17 +01:00
else if( isdigit(*aname) )
1998-01-28 17:09:43 +01:00
tty_printf(_("Name may not start with a digit\n"));
1998-01-07 21:47:46 +01:00
else if( strlen(aname) < 5 )
1998-01-28 17:09:43 +01:00
tty_printf(_("Name must be at least 5 characters long\n"));
1998-01-07 21:47:46 +01:00
else
break;
}
}
if( !amail ) {
for(;;) {
m_free(amail);
1998-01-28 17:09:43 +01:00
amail = tty_get(_("Email address: "));
1998-01-07 21:47:46 +01:00
trim_spaces(amail);
strlwr(amail);
tty_kill_prompt();
if( !*amail )
break; /* no email address is okay */
else if( strcspn( amail, "abcdefghijklmnopqrstuvwxyz_-.@" )
|| string_count_chr(amail,'@') != 1
|| *amail == '@'
|| amail[strlen(amail)-1] == '@'
|| amail[strlen(amail)-1] == '.'
|| strstr(amail, "..") )
1998-01-28 17:09:43 +01:00
tty_printf(_("Not a valid email address\n"));
1998-01-07 21:47:46 +01:00
else
break;
}
}
if( !acomment ) {
for(;;) {
m_free(acomment);
1998-01-28 17:09:43 +01:00
acomment = tty_get(_("Comment: "));
1998-01-07 21:47:46 +01:00
trim_spaces(acomment);
tty_kill_prompt();
if( !*acomment )
break; /* no comment is okay */
else if( strpbrk( acomment, "()" ) )
1998-01-28 17:09:43 +01:00
tty_printf(_("Invalid character in comment\n"));
1998-01-07 21:47:46 +01:00
else
break;
}
}
1997-11-18 15:06:00 +01:00
m_free(uid);
1998-01-12 11:18:17 +01:00
uid = p = m_alloc(strlen(aname)+strlen(amail)+strlen(acomment)+12+10);
1998-01-07 21:47:46 +01:00
p = stpcpy(p, aname );
if( *acomment )
p = stpcpy(stpcpy(stpcpy(p," ("), acomment),")");
if( *amail )
p = stpcpy(stpcpy(stpcpy(p," <"), amail),">");
1998-01-16 22:15:24 +01:00
/* append a warning if we do not have dev/random
* or it is switched into quick testmode */
if( quick_random_gen(-1) )
strcpy(p, " (INSECURE!)" );
1998-01-07 21:47:46 +01:00
1998-01-28 17:09:43 +01:00
tty_printf(_("You selected this USER-ID:\n \"%s\"\n\n"), uid);
1998-04-14 19:51:16 +02:00
/* fixme: add a warning if this user-id already exists */
1998-01-07 21:47:46 +01:00
for(;;) {
1998-01-28 17:09:43 +01:00
answer = tty_get(_("Edit (N)ame, (C)omment, (E)mail or (O)kay? "));
1997-11-18 15:06:00 +01:00
tty_kill_prompt();
1998-01-07 21:47:46 +01:00
if( strlen(answer) > 1 )
;
else if( *answer == 'N' || *answer == 'n' ) {
m_free(aname); aname = NULL;
break;
}
else if( *answer == 'C' || *answer == 'c' ) {
m_free(acomment); acomment = NULL;
break;
}
else if( *answer == 'E' || *answer == 'e' ) {
m_free(amail); amail = NULL;
break;
}
else if( *answer == 'O' || *answer == 'o' ) {
m_free(aname); aname = NULL;
m_free(acomment); acomment = NULL;
m_free(amail); amail = NULL;
1997-11-18 15:06:00 +01:00
break;
}
m_free(answer);
}
1998-01-07 21:47:46 +01:00
m_free(answer);
if( !amail && !acomment && !amail )
break;
m_free(uid); uid = NULL;
1997-11-18 15:06:00 +01:00
}
1998-05-26 15:38:00 +02:00
return uid;
}
1997-12-09 13:46:23 +01:00
1998-05-26 15:38:00 +02:00
static DEK *
ask_passphrase( STRING2KEY **ret_s2k )
{
DEK *dek = NULL;
STRING2KEY *s2k;
1997-12-09 13:46:23 +01:00
1998-01-28 17:09:43 +01:00
tty_printf(_("You need a Passphrase to protect your secret key.\n\n") );
1997-12-09 13:46:23 +01:00
1998-05-03 17:42:08 +02:00
s2k = m_alloc_secure( sizeof *s2k );
1997-12-20 18:23:29 +01:00
for(;;) {
1998-05-03 17:42:08 +02:00
s2k->mode = 1;
1998-05-03 21:35:33 +02:00
s2k->hash_algo = DIGEST_ALGO_RMD160;
1998-05-04 20:49:26 +02:00
dek = passphrase_to_dek( NULL, CIPHER_ALGO_BLOWFISH, s2k, 2 );
if( !dek ) {
tty_printf(_("passphrase not correctly repeated; try again.\n"));
}
else if( !dek->keylen ) {
1997-12-20 18:23:29 +01:00
m_free(dek); dek = NULL;
1998-05-03 17:42:08 +02:00
m_free(s2k); s2k = NULL;
1998-01-28 17:09:43 +01:00
tty_printf(_(
1998-04-14 19:51:16 +02:00
"You don't want a passphrase - this is probably a *bad* idea!\n"
"I will do it anyway. You can change your passphrase at any time,\n"
1998-01-28 17:09:43 +01:00
"using this program with the option \"--change-passphrase\"\n\n"));
1997-12-20 18:23:29 +01:00
break;
}
else
break; /* okay */
1997-12-09 13:46:23 +01:00
}
1998-05-26 15:38:00 +02:00
*ret_s2k = s2k;
return dek;
}
1997-12-09 13:46:23 +01:00
1998-05-26 15:38:00 +02:00
static int
do_create( int algo, unsigned nbits, KBNODE pub_root, KBNODE sec_root,
DEK *dek, STRING2KEY *s2k, PKT_secret_cert **skc, int valid_days,
int v4_packet )
{
int rc=0;
1997-11-18 15:06:00 +01:00
1998-01-28 17:09:43 +01:00
tty_printf(_(
1997-12-20 18:23:29 +01:00
"We need to generate a lot of random bytes. It is a good idea to perform\n"
"some other action (work in another window, move the mouse, utilize the\n"
"network and the disks) during the prime generation; this gives the random\n"
1998-01-28 17:09:43 +01:00
"number generator a better chance to gain enough entropy.\n") );
1997-12-20 18:23:29 +01:00
1997-11-24 23:24:04 +01:00
if( algo == PUBKEY_ALGO_ELGAMAL )
1998-05-26 15:38:00 +02:00
rc = gen_elg(nbits, pub_root, sec_root, dek, s2k,
skc, valid_days, v4_packet? 4:3 );
#ifdef ENABLE_RSA_KEYGEN
1997-11-24 23:24:04 +01:00
else if( algo == PUBKEY_ALGO_RSA )
1998-05-26 15:38:00 +02:00
rc = gen_rsa(nbits, pub_root, sec_root, dek, s2k, skc, valid_days );
1997-11-24 23:24:04 +01:00
#endif
1997-12-16 20:15:09 +01:00
else if( algo == PUBKEY_ALGO_DSA )
1998-05-26 15:38:00 +02:00
rc = gen_dsa(nbits, pub_root, sec_root, dek, s2k, skc, valid_days);
1997-11-24 23:24:04 +01:00
else
1998-01-16 22:15:24 +01:00
BUG();
1998-02-13 21:58:50 +01:00
if( !rc ) {
add_kbnode( pub_root,
1998-02-24 19:50:46 +01:00
make_comment_node("#created by GNUPG v" VERSION " ("
1998-02-16 21:05:02 +01:00
PRINTABLE_OS_NAME ")"));
1998-02-13 21:58:50 +01:00
add_kbnode( sec_root,
1998-02-24 19:50:46 +01:00
make_comment_node("#created by GNUPG v" VERSION " ("
1998-02-16 21:05:02 +01:00
PRINTABLE_OS_NAME ")"));
1998-02-13 21:58:50 +01:00
}
1998-05-26 15:38:00 +02:00
return rc;
}
/****************
* Generate a keypair
*/
void
generate_keypair()
{
unsigned nbits;
char *pub_fname = NULL;
char *sec_fname = NULL;
char *uid = NULL;
KBNODE pub_root = NULL;
KBNODE sec_root = NULL;
PKT_secret_cert *skc = NULL;
DEK *dek;
STRING2KEY *s2k;
int rc;
int algo;
int ndays;
int v4;
if( opt.batch || opt.answer_yes || opt.answer_no ) {
log_error(_("Key generation can only be used in interactive mode\n"));
return;
}
algo = ask_algo( &v4 );
nbits = ask_keysize( algo );
ndays = ask_valid_days();
uid = ask_user_id();
dek = ask_passphrase( &s2k );
/* now check whether we are allowed to write to the keyrings */
pub_fname = make_filename(opt.homedir, "pubring.gpg", NULL );
sec_fname = make_filename(opt.homedir, "secring.gpg", NULL );
if( opt.verbose ) {
tty_printf(_("writing public certificate to '%s'\n"), pub_fname );
tty_printf(_("writing secret certificate to '%s'\n"), sec_fname );
}
/* we create the packets as a tree of kbnodes. Because the structure
* we create is known in advance we simply generate a linked list
* The first packet is a dummy comment packet which we flag
* as deleted. The very first packet must always be a CERT packet.
*/
pub_root = make_comment_node("#"); delete_kbnode(pub_root);
sec_root = make_comment_node("#"); delete_kbnode(sec_root);
rc = do_create( algo, nbits, pub_root, sec_root, dek, s2k, &skc, ndays, v4);
1997-12-09 13:46:23 +01:00
if( !rc )
1997-12-16 20:15:09 +01:00
write_uid(pub_root, uid );
if( !rc )
write_uid(sec_root, uid );
1997-12-09 13:46:23 +01:00
if( !rc )
1997-12-16 20:15:09 +01:00
rc = write_selfsig(pub_root, pub_root, skc);
1997-12-09 13:46:23 +01:00
if( !rc )
1997-12-16 20:15:09 +01:00
rc = write_selfsig(sec_root, pub_root, skc);
1997-12-09 13:46:23 +01:00
1997-12-16 20:15:09 +01:00
if( !rc ) {
KBPOS pub_kbpos;
KBPOS sec_kbpos;
int rc1 = -1;
int rc2 = -1;
/* we can now write the certificates */
1998-01-06 22:01:36 +01:00
if( get_keyblock_handle( pub_fname, 0, &pub_kbpos ) ) {
if( add_keyblock_resource( pub_fname, 1, 0 ) ) {
1997-12-16 20:15:09 +01:00
log_error("can add keyblock file '%s'\n", pub_fname );
rc = G10ERR_CREATE_FILE;
}
1998-01-06 22:01:36 +01:00
else if( get_keyblock_handle( pub_fname, 0, &pub_kbpos ) ) {
1997-12-16 20:15:09 +01:00
log_error("can get keyblock handle for '%s'\n", pub_fname );
rc = G10ERR_CREATE_FILE;
}
}
if( rc )
;
1998-01-06 22:01:36 +01:00
else if( get_keyblock_handle( sec_fname, 1, &sec_kbpos ) ) {
if( add_keyblock_resource( sec_fname, 1, 1 ) ) {
1997-12-16 20:15:09 +01:00
log_error("can add keyblock file '%s'\n", sec_fname );
rc = G10ERR_CREATE_FILE;
}
1998-01-06 22:01:36 +01:00
else if( get_keyblock_handle( sec_fname, 1, &sec_kbpos ) ) {
1997-12-16 20:15:09 +01:00
log_error("can get keyblock handle for '%s'\n", sec_fname );
rc = G10ERR_CREATE_FILE;
}
}
if( rc )
;
else if( (rc=rc1=lock_keyblock( &pub_kbpos )) )
log_error("can't lock public keyring: %s\n", g10_errstr(rc) );
else if( (rc=rc2=lock_keyblock( &sec_kbpos )) )
log_error("can't lock secret keyring: %s\n", g10_errstr(rc) );
else if( (rc=insert_keyblock( &pub_kbpos, pub_root )) )
log_error("can't write public key: %s\n", g10_errstr(rc) );
else if( (rc=insert_keyblock( &sec_kbpos, sec_root )) )
log_error("can't write secret key: %s\n", g10_errstr(rc) );
else {
1998-01-28 17:09:43 +01:00
tty_printf(_("public and secret key created and signed.\n") );
1998-05-26 15:38:00 +02:00
if( algo == PUBKEY_ALGO_DSA )
tty_printf(_("Note that this key cannot be used for "
"encryption. You may want to use\n"
"the command \"--add-key\" to generate a "
"secondary key for this purpose.\n") );
1997-12-16 20:15:09 +01:00
}
if( !rc1 )
unlock_keyblock( &pub_kbpos );
if( !rc2 )
unlock_keyblock( &sec_kbpos );
1997-12-09 13:46:23 +01:00
}
1997-12-16 20:15:09 +01:00
if( rc )
1998-01-28 17:09:43 +01:00
tty_printf(_("Key generation failed: %s\n"), g10_errstr(rc) );
1997-12-16 20:15:09 +01:00
release_kbnode( pub_root );
release_kbnode( sec_root );
if( skc ) /* the unprotected secret certificate */
free_secret_cert(skc);
1997-11-18 15:06:00 +01:00
m_free(uid);
1997-12-09 13:46:23 +01:00
m_free(dek);
1998-05-03 17:42:08 +02:00
m_free(s2k);
1997-12-16 20:15:09 +01:00
m_free(pub_fname);
m_free(sec_fname);
1997-11-18 15:06:00 +01:00
}
1998-05-05 22:34:20 +02:00
/****************
* add a new subkey to an existing key.
*/
void
1998-05-26 15:38:00 +02:00
generate_subkeypair( const char *username )
1998-05-05 22:34:20 +02:00
{
1998-05-26 15:38:00 +02:00
int rc=0;
KBPOS pub_kbpos, sec_kbpos;
KBNODE pub_keyblock = NULL;
KBNODE sec_keyblock = NULL;
KBNODE node;
PKT_secret_cert *skc = NULL; /* this is the primary skc */
u32 keyid[2];
int v4, algo, ndays;
unsigned nbits;
char *passphrase = NULL;
DEK *dek = NULL;
STRING2KEY *s2k = NULL;
if( opt.batch || opt.answer_yes || opt.answer_no ) {
log_error(_("Key generation can only be used in interactive mode\n"));
return;
}
/* search the userid */
rc = find_secret_keyblock_byname( &sec_kbpos, username );
if( rc ) {
log_error("user '%s' not found\n", username );
goto leave;
}
rc = read_keyblock( &sec_kbpos, &sec_keyblock );
if( rc ) {
log_error("error reading the secret key: %s\n", g10_errstr(rc) );
goto leave;
}
/* and the public key */
rc = find_keyblock_byname( &pub_kbpos, username );
if( rc ) {
log_error("user '%s' not found in public ring\n", username );
goto leave;
}
rc = read_keyblock( &pub_kbpos, &pub_keyblock );
if( rc ) {
log_error("error reading the public key: %s\n", g10_errstr(rc) );
goto leave;
}
/* break out the primary key */
node = find_kbnode( sec_keyblock, PKT_SECRET_CERT );
if( !node ) {
log_error("Oops; secret key not found anymore!\n");
rc = G10ERR_GENERAL;
goto leave;
}
/* make a copy of the skc to keep the protected one in the keyblock */
skc = copy_secret_cert( NULL, node->pkt->pkt.secret_cert );
keyid_from_skc( skc, keyid );
/* display primary and all secondary keys */
tty_printf("sec %4u%c/%08lX %s ",
nbits_from_skc( skc ),
pubkey_letter( skc->pubkey_algo ),
keyid[1], datestr_from_skc(skc) );
{
size_t n;
char *p = get_user_id( keyid, &n );
tty_print_string( p, n );
m_free(p);
tty_printf("\n");
}
for(node=sec_keyblock; node; node = node->next ) {
if( node->pkt->pkttype == PKT_SECKEY_SUBCERT ) {
PKT_secret_cert *subskc = node->pkt->pkt.secret_cert;
keyid_from_skc( subskc, keyid );
tty_printf("sub %4u%c/%08lX %s\n",
nbits_from_skc( subskc ),
pubkey_letter( subskc->pubkey_algo ),
keyid[1], datestr_from_skc(subskc) );
}
}
tty_printf("\n");
/* unprotect to get the passphrase */
switch( is_secret_key_protected( skc ) ) {
case -1:
rc = G10ERR_PUBKEY_ALGO;
break;
case 0:
tty_printf("This key is not protected.\n");
break;
default:
tty_printf("Key is protected.\n");
rc = check_secret_key( skc );
if( !rc )
passphrase = get_last_passphrase();
break;
}
if( rc )
goto leave;
algo = ask_algo( &v4 );
nbits = ask_keysize( algo );
ndays = ask_valid_days();
if( passphrase ) {
s2k = m_alloc_secure( sizeof *s2k );
s2k->mode = 1;
s2k->hash_algo = DIGEST_ALGO_RMD160;
set_next_passphrase( passphrase );
dek = passphrase_to_dek( NULL, CIPHER_ALGO_BLOWFISH, s2k, 2 );
}
rc = do_create( algo, nbits, pub_keyblock, sec_keyblock,
dek, s2k, NULL, ndays, v4 );
if( !rc )
rc = write_keybinding(pub_keyblock, pub_keyblock, skc);
if( !rc )
rc = write_keybinding(sec_keyblock, pub_keyblock, skc);
/* write back */
if( !rc ) {
rc = update_keyblock( &pub_kbpos, pub_keyblock );
if( rc )
log_error("update_public_keyblock failed\n" );
}
if( !rc ) {
rc = update_keyblock( &sec_kbpos, sec_keyblock );
if( rc )
log_error("update_secret_keyblock failed\n" );
}
if( !rc )
tty_printf(_("public and secret subkey created.\n") );
leave:
if( rc )
tty_printf(_("Key generation failed: %s\n"), g10_errstr(rc) );
m_free( passphrase );
m_free( dek );
m_free( s2k );
if( skc ) /* release the copy of the (now unprotected) secret key */
free_secret_cert(skc);
release_kbnode( sec_keyblock );
release_kbnode( pub_keyblock );
set_next_passphrase( NULL );
1998-05-05 22:34:20 +02:00
}