@ -142,7 +142,7 @@ resultp(pgp_io_t *io,
pgp_keyring_t * ring )
{
const pgp_key_t * key ;
pgp_pubkey_t * sigkey ;
const pgp_pubkey_t * sigkey ;
unsigned from ;
unsigned i ;
time_t t ;
@ -165,11 +165,6 @@ resultp(pgp_io_t *io,
key = pgp_getkeybyid ( io , ring ,
( const uint8_t * ) res - > valid_sigs [ i ] . signer_id ,
& from , & sigkey ) ;
if ( sigkey = = & key - > enckey ) {
( void ) fprintf ( io - > res ,
" WARNING: signature for %s made with encryption key \n " ,
( f ) ? f : " <stdin> " ) ;
}
pgp_print_keydata ( io , ring , key , " signature " , & key - > key . pubkey , 0 ) ;
}
}
@ -223,10 +218,12 @@ findvar(netpgp_t *netpgp, const char *name)
}
/* read a keyring and return it */
static void *
readkeyring ( netpgp_t * netpgp , const char * name )
static unsigned
readkeyring ( netpgp_t * netpgp ,
const char * name ,
pgp_keyring_t * pubring ,
pgp_keyring_t * secring )
{
pgp_keyring_t * keyring ;
const unsigned noarmor = 0 ;
char f [ MAXPATHLEN ] ;
char * filename ;
@ -237,17 +234,12 @@ readkeyring(netpgp_t *netpgp, const char *name)
( void ) snprintf ( f , sizeof ( f ) , " %s/%s.gpg " , homedir , name ) ;
filename = f ;
}
if ( ( keyring = calloc ( 1 , sizeof ( * keyring ) ) ) = = NULL ) {
( void ) fprintf ( stderr , " readkeyring: bad alloc \n " ) ;
return NULL ;
}
if ( ! pgp_keyring_fileread ( keyring , noarmor , filename ) ) {
free ( keyring ) ;
if ( ! pgp_keyring_fileread ( pubring , secring , noarmor , filename ) ) {
( void ) fprintf ( stderr , " Can't read %s %s \n " , name , filename ) ;
return NULL ;
return 0 ;
}
netpgp_setvar ( netpgp , name , filename ) ;
return keyring ;
return 1 ;
}
/* read keys from ssh key files */
@ -342,7 +334,7 @@ get_first_ring(pgp_keyring_t *ring, char *id, size_t len, int last)
return 0 ;
}
( void ) memset ( id , 0x0 , len ) ;
src = ring - > keys [ ( last ) ? ring - > keyc - 1 : 0 ] . sig id;
src = ring - > keys [ ( last ) ? ring - > keyc - 1 : 0 ] . pubkey id;
for ( i = 0 , n = 0 ; i < PGP_KEY_ID_SIZE ; i + = 2 ) {
n + = snprintf ( & id [ n ] , len - n , " %02x%02x " , src [ i ] , src [ i + 1 ] ) ;
}
@ -457,7 +449,7 @@ appendkey(pgp_io_t *io, pgp_key_t *key, char *ringfile)
( void ) fprintf ( io - > errs , " can't open pubring '%s' \n " , ringfile ) ;
return 0 ;
}
if ( ! pgp_write_xfer_ pub key( create , key , noarmor ) ) {
if ( ! pgp_write_xfer_ key( create , key , noarmor ) ) {
( void ) fprintf ( io - > errs , " Cannot write pubkey \n " ) ;
return 0 ;
}
@ -721,43 +713,43 @@ formatbignum(char *buffer, BIGNUM *bn)
# define MAX_PASSPHRASE_ATTEMPTS 3
# define INFINITE_ATTEMPTS -1
/* get the passphrase from the user */
static int
find_passphrase ( FILE * passfp , const char * id , char * passphrase , size_t size , int attempts )
{
char prompt [ BUFSIZ ] ;
char buf [ 128 ] ;
char * cp ;
int cc ;
int i ;
if ( passfp ) {
if ( fgets ( passphrase , ( int ) size , passfp ) = = NULL ) {
return 0 ;
}
return ( int ) strlen ( passphrase ) ;
}
for ( i = 0 ; i < attempts ; i + + ) {
( void ) snprintf ( prompt , sizeof ( prompt ) , " Enter passphrase for %.16s: " , id ) ;
if ( ( cp = getpass ( prompt ) ) = = NULL ) {
break ;
}
cc = snprintf ( buf , sizeof ( buf ) , " %s " , cp ) ;
( void ) snprintf ( prompt , sizeof ( prompt ) , " Repeat passphrase for %.16s: " , id ) ;
if ( ( cp = getpass ( prompt ) ) = = NULL ) {
break ;
}
cc = snprintf ( passphrase , size , " %s " , cp ) ;
if ( strcmp ( buf , passphrase ) = = 0 ) {
( void ) memset ( buf , 0x0 , sizeof ( buf ) ) ;
return cc ;
}
}
( void ) memset ( buf , 0x0 , sizeof ( buf ) ) ;
( void ) memset ( passphrase , 0x0 , size ) ;
return 0 ;
}
// FIXME support passphrase again
// /* get the passphrase from the user */
// static int
// find_passphrase(FILE *passfp, const char *id, char *passphrase, size_t size, int attempts)
// {
// char prompt[BUFSIZ];
// char buf[128];
// char *cp;
// int cc;
// int i;
//
// if (passfp) {
// if (fgets(passphrase, (int)size, passfp) == NULL) {
// return 0;
// }
// return (int)strlen(passphrase);
// }
// for (i = 0 ; i < attempts ; i++) {
// (void) snprintf(prompt, sizeof(prompt), "Enter passphrase for %.16s: ", id);
// if ((cp = getpass(prompt)) == NULL) {
// break;
// }
// cc = snprintf(buf, sizeof(buf), "%s", cp);
// (void) snprintf(prompt, sizeof(prompt), "Repeat passphrase for %.16s: ", id);
// if ((cp = getpass(prompt)) == NULL) {
// break;
// }
// cc = snprintf(passphrase, size, "%s", cp);
// if (strcmp(buf, passphrase) == 0) {
// (void) memset(buf, 0x0, sizeof(buf));
// return cc;
// }
// }
// (void) memset(buf, 0x0, sizeof(buf));
// (void) memset(passphrase, 0x0, size);
// return 0;
// }
/***************************************************************************/
/* exported functions start here */
@ -840,9 +832,20 @@ netpgp_init(netpgp_t *netpgp)
return 0 ;
}
if ( netpgp_getvar ( netpgp , " ssh keys " ) = = NULL ) {
/* read from ordinary pgp keyrings */
netpgp - > pubring = readkeyring ( netpgp , " pubring " ) ;
if ( netpgp - > pubring = = NULL ) {
if ( ( netpgp - > pubring = calloc ( 1 , sizeof ( pgp_keyring_t ) ) ) = = NULL ) {
( void ) fprintf ( io - > errs , " Can't alloc pubring \n " ) ;
return 0 ;
}
if ( ( netpgp - > secring = calloc ( 1 , sizeof ( pgp_keyring_t ) ) ) = = NULL ) {
( void ) fprintf ( io - > errs , " Can't alloc secring \n " ) ;
return 0 ;
}
if ( ! readkeyring ( netpgp ,
" pubring " ,
netpgp - > pubring ,
netpgp - > secring ) ) {
( void ) fprintf ( io - > errs , " Can't read pub keyring \n " ) ;
return 0 ;
}
@ -858,8 +861,10 @@ netpgp_init(netpgp_t *netpgp)
/* only read secret keys if we need to */
if ( netpgp_getvar ( netpgp , " need seckey " ) ) {
/* read the secret ring */
netpgp - > secring = readkeyring ( netpgp , " secring " ) ;
if ( netpgp - > secring = = NULL ) {
if ( ! readkeyring ( netpgp ,
" secring " ,
netpgp - > pubring ,
netpgp - > secring ) ) {
( void ) fprintf ( io - > errs , " Can't read sec keyring \n " ) ;
return 0 ;
}
@ -1192,16 +1197,7 @@ netpgp_save_ring(netpgp_t *netpgp,
}
for ( n = 0 , key = keyring - > keys ; n < keyring - > keyc ; + + n , + + key ) {
/* Dump raw key packets to ring file */
unsigned j ;
for ( j = 0 ; j < key - > packetc ; j + + ) {
if ( ! pgp_write ( output , key - > packets [ j ] . raw , ( unsigned ) key - > packets [ j ] . length ) ) {
err = 1 ;
( void ) fprintf ( io - > errs ,
" netpgp_save_%s : couldn't save key #%d \n " , name , n ) ;
break ;
}
}
pgp_write_xfer_key ( output , key , 1 ) ;
}
pgp_teardown_file_write ( output , fd ) ;
@ -1246,7 +1242,10 @@ netpgp_import_key(netpgp_t *netpgp, char *f)
io = netpgp - > io ;
realarmor = isarmoured ( io , f , NULL , IMPORT_ARMOR_HEAD ) ;
done = pgp_keyring_fileread ( netpgp - > pubring , realarmor , f ) ;
done = pgp_keyring_fileread (
netpgp - > pubring ,
netpgp - > secring ,
realarmor , f ) ;
if ( ! done ) {
( void ) fprintf ( io - > errs , " Cannot import key from file %s \n " , f ) ;
return 0 ;
@ -1265,7 +1264,7 @@ netpgp_generate_key(netpgp_t *netpgp, char *id, int numbits)
pgp_key_t * key ;
pgp_io_t * io ;
uint8_t * uid ;
char passphrase [ 128 ] ;
/*char passphrase[128];*/
char newid [ 1024 ] ;
char filename [ MAXPATHLEN ] ;
char dir [ MAXPATHLEN ] ;
@ -1273,7 +1272,7 @@ netpgp_generate_key(netpgp_t *netpgp, char *id, int numbits)
char * ringfile ;
char * numtries ;
int attempts ;
int passc ;
/*int passc;*/
int fd ;
int cc ;
@ -1329,8 +1328,9 @@ netpgp_generate_key(netpgp_t *netpgp, char *id, int numbits)
} else if ( strcmp ( numtries , " unlimited " ) = = 0 ) {
attempts = INFINITE_ATTEMPTS ;
}
passc = find_passphrase ( netpgp - > passfp , & cp [ ID_OFFSET ] , passphrase , sizeof ( passphrase ) , attempts ) ;
if ( ! pgp_write_xfer_seckey ( create , key , ( uint8_t * ) passphrase , ( const unsigned ) passc , noarmor ) ) {
/* TODO use passphrase again */
/* passc = find_passphrase(netpgp->passfp, &cp[ID_OFFSET], passphrase, sizeof(passphrase), attempts);*/
if ( ! pgp_write_xfer_key ( create , key , noarmor ) ) {
( void ) fprintf ( io - > errs , " Cannot write seckey \n " ) ;
return 0 ;
}
@ -1423,7 +1423,7 @@ netpgp_sign_file(netpgp_t *netpgp,
const pgp_key_t * keypair ;
const pgp_key_t * pubkey ;
const unsigned overwrite = 1 ;
pgp_seckey_t * seckey ;
const pgp_seckey_t * seckey ;
const char * hashalg ;
pgp_io_t * io ;
char * numtries ;
@ -1464,7 +1464,9 @@ netpgp_sign_file(netpgp_t *netpgp,
}
if ( netpgp_getvar ( netpgp , " ssh keys " ) = = NULL ) {
/* now decrypt key */
seckey = pgp_decrypt_seckey ( keypair , netpgp - > passfp ) ;
seckey = pgp_get_seckey ( keypair ) ;
// FIXME : support encrypted seckeys again
// seckey = pgp_decrypt_seckey(keypair, netpgp->passfp);
if ( seckey = = NULL ) {
( void ) fprintf ( io - > errs , " Bad passphrase \n " ) ;
}
@ -1497,7 +1499,8 @@ netpgp_sign_file(netpgp_t *netpgp,
( unsigned ) armored , ( unsigned ) cleartext ,
overwrite ) ;
}
pgp_forget ( seckey , ( unsigned ) sizeof ( * seckey ) ) ;
/* FIXME should forget if key would have been unencrypted */
// pgp_forget(seckey, (unsigned)sizeof(*seckey));
return ret ;
}
@ -1552,7 +1555,7 @@ netpgp_sign_memory(netpgp_t *netpgp,
{
const pgp_key_t * keypair ;
const pgp_key_t * pubkey ;
pgp_seckey_t * seckey ;
const pgp_seckey_t * seckey ;
pgp_memory_t * signedmem ;
const char * hashalg ;
pgp_io_t * io ;
@ -1592,7 +1595,9 @@ netpgp_sign_memory(netpgp_t *netpgp,
}
}
/* now decrypt key */
seckey = pgp_decrypt_seckey ( keypair , netpgp - > passfp ) ;
seckey = pgp_get_seckey ( keypair ) ;
// FIXME : support encrypted seckeys again
// seckey = pgp_decrypt_seckey(keypair, netpgp->passfp);
if ( seckey = = NULL ) {
( void ) fprintf ( io - > errs , " Bad passphrase \n " ) ;
}
@ -1621,7 +1626,7 @@ netpgp_sign_memory(netpgp_t *netpgp,
} else {
ret = 0 ;
}
pgp_forget ( seckey , ( unsigned ) sizeof ( * seckey ) ) ;
// pgp_forget(seckey, (unsigned)sizeof(*seckey));
return ret ;
}
@ -1832,7 +1837,7 @@ netpgp_list_packets(netpgp_t *netpgp, char *f, int armor, char *pubringname)
( void ) fprintf ( io - > errs , " netpgp_list_packets: bad alloc \n " ) ;
return 0 ;
}
if ( ! pgp_keyring_fileread ( keyring , noarmor , pubringname ) ) {
if ( ! pgp_keyring_fileread ( keyring , NULL , noarmor , pubringname ) ) {
free ( keyring ) ;
( void ) fprintf ( io - > errs , " Cannot read pub keyring %s \n " ,
pubringname ) ;
@ -1959,9 +1964,8 @@ netpgp_set_homedir(netpgp_t *netpgp, char *home, const char *subdir, const int q
int
netpgp_validate_sigs ( netpgp_t * netpgp )
{
pgp_validation_t result ;
return ( int ) pgp_validate_all_sigs ( & result , netpgp - > pubring , NULL ) ;
/* keys in keyring are now forced to be valid */
return 1 ;
}
/* print the json out on 'fp' */
@ -2024,7 +2028,7 @@ netpgp_write_sshkey(netpgp_t *netpgp, char *s, const char *userid, char *out, si
( void ) fprintf ( stderr , " netpgp_save_sshpub: bad alloc 2 \n " ) ;
goto done ;
}
if ( ! pgp_keyring_fileread ( netpgp - > pubring = keyring , 1 , f ) ) {
if ( ! pgp_keyring_fileread ( netpgp - > pubring = keyring , NULL , 1 , f ) ) {
( void ) fprintf ( stderr , " can't import key \n " ) ;
goto done ;
}