1997-11-18 15:06:00 +01:00
|
|
|
/* passphrase.c - Get a passphrase
|
2000-07-14 19:34:53 +02:00
|
|
|
* Copyright (C) 1998, 1999, 2000 Free Software Foundation, Inc.
|
1997-11-18 15:06:00 +01:00
|
|
|
*
|
1998-12-23 13:41:40 +01:00
|
|
|
* This file is part of GnuPG.
|
1997-11-18 15:06:00 +01:00
|
|
|
*
|
1998-12-23 13:41:40 +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-12-23 13:41:40 +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>
|
2000-08-21 17:54:37 +02:00
|
|
|
#include <stddef.h>
|
1997-11-18 15:06:00 +01:00
|
|
|
#include <string.h>
|
1998-01-16 22:15:24 +01:00
|
|
|
#include <unistd.h>
|
1997-11-18 15:06:00 +01:00
|
|
|
#include <assert.h>
|
2000-08-21 17:54:37 +02:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/un.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
2000-07-14 19:34:53 +02:00
|
|
|
|
2000-01-24 12:55:49 +01:00
|
|
|
#include <gcrypt.h>
|
2000-07-14 19:34:53 +02:00
|
|
|
#include "util.h"
|
1998-01-02 21:40:10 +01:00
|
|
|
#include "options.h"
|
1997-11-18 15:06:00 +01:00
|
|
|
#include "ttyio.h"
|
1997-12-20 18:23:29 +01:00
|
|
|
#include "keydb.h"
|
1998-05-15 20:49:19 +02:00
|
|
|
#include "main.h"
|
1998-05-29 13:53:54 +02:00
|
|
|
#include "i18n.h"
|
1998-08-08 21:27:00 +02:00
|
|
|
#include "status.h"
|
2000-08-21 17:54:37 +02:00
|
|
|
#include "gpga-prot.h"
|
|
|
|
|
|
|
|
#define buftou32( p ) ((*(byte*)(p) << 24) | (*((byte*)(p)+1)<< 16) | \
|
|
|
|
(*((byte*)(p)+2) << 8) | (*((byte*)(p)+3)))
|
|
|
|
#define u32tobuf( p, a ) do { \
|
|
|
|
((byte*)p)[0] = (byte)((a) >> 24); \
|
|
|
|
((byte*)p)[1] = (byte)((a) >> 16); \
|
|
|
|
((byte*)p)[2] = (byte)((a) >> 8); \
|
|
|
|
((byte*)p)[3] = (byte)((a) ); \
|
|
|
|
} while(0)
|
|
|
|
|
1997-11-18 15:06:00 +01:00
|
|
|
|
1998-09-28 21:25:31 +02:00
|
|
|
static char *fd_passwd = NULL;
|
1998-05-26 15:38:00 +02:00
|
|
|
static char *next_pw = NULL;
|
|
|
|
static char *last_pw = NULL;
|
1997-11-18 15:06:00 +01:00
|
|
|
|
1998-05-03 21:35:33 +02:00
|
|
|
static void hash_passphrase( DEK *dek, char *pw, STRING2KEY *s2k, int create );
|
1997-11-18 15:06:00 +01:00
|
|
|
|
1998-01-02 21:40:10 +01:00
|
|
|
int
|
1998-09-28 21:25:31 +02:00
|
|
|
have_static_passphrase()
|
1998-01-02 21:40:10 +01:00
|
|
|
{
|
2000-08-21 17:54:37 +02:00
|
|
|
if ( opt.use_agent )
|
|
|
|
return 0;
|
1998-09-28 21:25:31 +02:00
|
|
|
return !!fd_passwd;
|
1998-01-02 21:40:10 +01:00
|
|
|
}
|
|
|
|
|
1998-05-26 15:38:00 +02:00
|
|
|
/****************
|
|
|
|
* Set the passphrase to be used for the next query and only for the next
|
|
|
|
* one.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
set_next_passphrase( const char *s )
|
|
|
|
{
|
2000-01-24 12:55:49 +01:00
|
|
|
gcry_free(next_pw);
|
1998-05-26 15:38:00 +02:00
|
|
|
next_pw = NULL;
|
|
|
|
if( s ) {
|
2000-01-24 12:55:49 +01:00
|
|
|
next_pw = gcry_xmalloc_secure( strlen(s)+1 );
|
1998-05-26 15:38:00 +02:00
|
|
|
strcpy(next_pw, s );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************
|
|
|
|
* Get the last passphrase used in passphrase_to_dek.
|
|
|
|
* Note: This removes the passphrase from this modules and
|
|
|
|
* the caller must free the result. May return NULL:
|
|
|
|
*/
|
|
|
|
char *
|
|
|
|
get_last_passphrase()
|
|
|
|
{
|
|
|
|
char *p = last_pw;
|
|
|
|
last_pw = NULL;
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
1997-11-18 15:06:00 +01:00
|
|
|
|
1998-09-28 21:25:31 +02:00
|
|
|
void
|
|
|
|
read_passphrase_from_fd( int fd )
|
|
|
|
{
|
|
|
|
int i, len;
|
|
|
|
char *pw;
|
|
|
|
|
2000-08-21 17:54:37 +02:00
|
|
|
if ( opt.use_agent )
|
|
|
|
return; /* not used here */
|
|
|
|
|
|
|
|
|
1998-09-28 21:25:31 +02:00
|
|
|
if( !opt.batch )
|
|
|
|
tty_printf("Reading passphrase from file descriptor %d ...", fd );
|
|
|
|
for( pw = NULL, i = len = 100; ; i++ ) {
|
|
|
|
if( i >= len-1 ) {
|
|
|
|
char *pw2 = pw;
|
|
|
|
len += 100;
|
2000-01-24 12:55:49 +01:00
|
|
|
pw = gcry_xmalloc_secure( len );
|
1998-09-28 21:25:31 +02:00
|
|
|
if( pw2 )
|
|
|
|
memcpy(pw, pw2, i );
|
|
|
|
else
|
|
|
|
i=0;
|
|
|
|
}
|
|
|
|
if( read( fd, pw+i, 1) != 1 || pw[i] == '\n' )
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
pw[i] = 0;
|
|
|
|
if( !opt.batch )
|
|
|
|
tty_printf("\b\b\b \n" );
|
|
|
|
|
2000-01-24 12:55:49 +01:00
|
|
|
gcry_free( fd_passwd );
|
1998-09-28 21:25:31 +02:00
|
|
|
fd_passwd = pw;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-08-21 17:54:37 +02:00
|
|
|
static int
|
|
|
|
writen ( int fd, const void *buf, size_t nbytes )
|
|
|
|
{
|
|
|
|
size_t nleft = nbytes;
|
|
|
|
ssize_t nwritten;
|
|
|
|
|
|
|
|
while( nleft > 0 ) {
|
|
|
|
nwritten = write( fd, buf, nleft );
|
|
|
|
if( nwritten < 0 ) {
|
|
|
|
if ( errno == EINTR )
|
|
|
|
nwritten = 0;
|
|
|
|
else {
|
|
|
|
log_error ( "writen() failed: %s\n", strerror (errno) );
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
nleft -= nwritten;
|
|
|
|
buf = (const char*)buf + nwritten;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
readn ( int fd, void *buf, size_t buflen, size_t *ret_nread )
|
|
|
|
{
|
|
|
|
size_t nleft = buflen;
|
|
|
|
int nread;
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
p = buf;
|
|
|
|
while( nleft > 0 ) {
|
|
|
|
nread = read ( fd, buf, nleft );
|
|
|
|
if( nread < 0 ) {
|
|
|
|
if (nread == EINTR)
|
|
|
|
nread = 0;
|
|
|
|
else {
|
|
|
|
log_error ( "read() error: %s\n", strerror (errno) );
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if( !nread )
|
|
|
|
break; /* EOF */
|
|
|
|
nleft -= nread;
|
|
|
|
buf = (char*)buf + nread;
|
|
|
|
}
|
|
|
|
if( ret_nread )
|
|
|
|
*ret_nread = buflen - nleft;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Open a connection to the agent and send the magic string
|
|
|
|
* Returns: -1 on error or an filedescriptor for urther processing
|
|
|
|
*/
|
|
|
|
|
|
|
|
static int
|
|
|
|
agent_open ()
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
char *infostr, *p;
|
|
|
|
struct sockaddr_un client_addr;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
infostr = getenv ( "GPG_AGENT_INFO" );
|
|
|
|
if ( !infostr ) {
|
|
|
|
log_error (_("gpg-agent is not available in this session\n"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
infostr = gcry_xstrdup ( infostr );
|
|
|
|
if ( !(p = strchr ( infostr, ':')) || p == infostr
|
|
|
|
|| (p-infostr)+1 >= sizeof client_addr.sun_path ) {
|
|
|
|
log_error (_("malformed GPG_AGENT_INFO environment variable\n"));
|
|
|
|
gcry_free (infostr );
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
*p = 0;
|
|
|
|
|
|
|
|
if( (fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1 ) {
|
|
|
|
log_error ("can't create socket: %s\n", strerror(errno) );
|
|
|
|
gcry_free (infostr );
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset( &client_addr, 0, sizeof client_addr );
|
|
|
|
client_addr.sun_family = AF_UNIX;
|
|
|
|
strcpy( client_addr.sun_path, infostr );
|
|
|
|
len = offsetof (struct sockaddr_un, sun_path)
|
|
|
|
+ strlen(client_addr.sun_path) + 1;
|
|
|
|
|
|
|
|
if( connect( fd, (struct sockaddr*)&client_addr, len ) == -1 ) {
|
|
|
|
log_error ( _("can't connect to `%s': %s\n"),
|
|
|
|
infostr, strerror (errno) );
|
|
|
|
gcry_free (infostr );
|
|
|
|
close (fd );
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
gcry_free (infostr);
|
|
|
|
|
|
|
|
if ( writen ( fd, "GPGA\0\0\0\x01", 8 ) ) {
|
|
|
|
close (fd);
|
|
|
|
fd = -1;
|
|
|
|
}
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
agent_close ( int fd )
|
|
|
|
{
|
|
|
|
close (fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Ask the GPG Agent for the passphrase.
|
|
|
|
* Mode 0: Allow cached passphrase
|
|
|
|
* 1: No cached passphrase FIXME: Not really implemented
|
|
|
|
* 2: Ditto, but change the text to "repeat entry"
|
|
|
|
*/
|
|
|
|
static char *
|
|
|
|
agent_get_passphrase ( u32 *keyid, int mode )
|
|
|
|
{
|
|
|
|
size_t n;
|
|
|
|
char *atext;
|
|
|
|
char buf[50];
|
|
|
|
int fd = -1;
|
|
|
|
int nread;
|
|
|
|
u32 reply;
|
|
|
|
char *pw = NULL;
|
|
|
|
PKT_public_key *pk = gcry_xcalloc( 1, sizeof *pk );
|
|
|
|
byte fpr[MAX_FINGERPRINT_LEN];
|
|
|
|
|
|
|
|
#if MAX_FINGERPRINT_LEN < 20
|
|
|
|
#error agent needs a 20 byte fingerprint
|
|
|
|
#endif
|
|
|
|
|
|
|
|
memset (fpr, 0, MAX_FINGERPRINT_LEN );
|
|
|
|
if( keyid && get_pubkey( pk, keyid ) )
|
|
|
|
pk = NULL; /* oops: no key for some reason */
|
|
|
|
|
|
|
|
if ( !mode && pk ) {
|
|
|
|
char *uid;
|
|
|
|
size_t uidlen;
|
|
|
|
const char *algo_name = gcry_pk_algo_name( pk->pubkey_algo );
|
|
|
|
const char *timestr;
|
|
|
|
char *maink;
|
|
|
|
const char *fmtstr;
|
|
|
|
|
|
|
|
if ( !algo_name )
|
|
|
|
algo_name = "?";
|
|
|
|
|
|
|
|
fmtstr = _(" (main key ID %08lX)");
|
|
|
|
maink = gcry_xmalloc ( strlen (fmtstr) + 20 );
|
|
|
|
if( keyid[2] && keyid[3] && keyid[0] != keyid[2]
|
|
|
|
&& keyid[1] != keyid[3] )
|
|
|
|
sprintf( maink, fmtstr, (ulong)keyid[3] );
|
|
|
|
else
|
|
|
|
*maink = 0;
|
|
|
|
|
|
|
|
uid = get_user_id( keyid, &uidlen );
|
|
|
|
timestr = strtimestamp (pk->timestamp);
|
|
|
|
fmtstr = _("You need a passphrase to unlock the"
|
|
|
|
" secret key for user:\n"
|
|
|
|
"\"%.*s\"\n"
|
|
|
|
"%u-bit %s key, ID %08lX, created %s%s\n" );
|
|
|
|
atext = gcry_xmalloc ( 100 + strlen (fmtstr)
|
|
|
|
+ uidlen + 15 + strlen(algo_name) + 8
|
|
|
|
+ strlen (timestr) + strlen (maink) );
|
|
|
|
sprintf (atext, fmtstr,
|
|
|
|
uidlen, uid,
|
|
|
|
nbits_from_pk (pk), algo_name, (ulong)keyid[1], timestr,
|
|
|
|
maink );
|
|
|
|
gcry_free (uid);
|
|
|
|
gcry_free (maink);
|
|
|
|
|
|
|
|
{
|
|
|
|
size_t dummy;
|
|
|
|
fingerprint_from_pk( pk, fpr, &dummy );
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
else if (mode == 1 )
|
|
|
|
atext = gcry_xstrdup ( _("Enter passphrase\n") );
|
|
|
|
else
|
|
|
|
atext = gcry_xstrdup ( _("Repeat passphrase\n") );
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ( (fd = agent_open ()) == -1 )
|
|
|
|
goto failure;
|
|
|
|
|
|
|
|
|
|
|
|
n = 4 + 20 + strlen (atext);
|
|
|
|
u32tobuf (buf, n );
|
|
|
|
u32tobuf (buf+4, GPGA_PROT_GET_PASSPHRASE );
|
|
|
|
memcpy (buf+8, fpr, 20 );
|
|
|
|
if ( writen ( fd, buf, 28 ) || writen ( fd, atext, strlen (atext) ) )
|
|
|
|
goto failure;
|
|
|
|
gcry_free (atext); atext = NULL;
|
|
|
|
|
|
|
|
/* get response */
|
|
|
|
if ( readn ( fd, buf, 12, &nread ) )
|
|
|
|
goto failure;
|
|
|
|
|
|
|
|
if ( nread < 8 ) {
|
|
|
|
log_error ( _("response from agent too short\n") );
|
|
|
|
goto failure;
|
|
|
|
}
|
|
|
|
n = buftou32 ( buf );
|
|
|
|
reply = buftou32 ( buf + 4 );
|
|
|
|
if ( reply == GPGA_PROT_GOT_PASSPHRASE ) {
|
|
|
|
size_t pwlen;
|
|
|
|
size_t nn;
|
|
|
|
|
|
|
|
if ( nread < 12 || n < 8 ) {
|
|
|
|
log_error ( _("response from agent too short\n") );
|
|
|
|
goto failure;
|
|
|
|
}
|
|
|
|
pwlen = buftou32 ( buf + 8 );
|
|
|
|
nread -= 12;
|
|
|
|
n -= 8;
|
|
|
|
if ( pwlen > n || n > 1000 ) {
|
|
|
|
log_error (_("passphrase too long\n"));
|
|
|
|
/* or protocol error */
|
|
|
|
goto failure;
|
|
|
|
}
|
|
|
|
/* we read the whole block in one chunk to give no hints
|
|
|
|
* on how long the passhrase actually is - this wastes some bytes
|
|
|
|
* but because we already have this padding we should not loosen
|
|
|
|
* the by issuing 2 read calls */
|
|
|
|
pw = gcry_xmalloc_secure ( n+1 );
|
|
|
|
if ( readn ( fd, pw, n, &nn ) )
|
|
|
|
goto failure;
|
|
|
|
if ( n != nn ) {
|
|
|
|
log_error (_("invalid response from agent\n"));
|
|
|
|
goto failure;
|
|
|
|
}
|
|
|
|
pw[pwlen] = 0; /* make a C String */
|
|
|
|
agent_close (fd);
|
|
|
|
free_public_key( pk );
|
|
|
|
return pw;
|
|
|
|
}
|
|
|
|
else if ( reply == GPGA_PROT_CANCELED ) {
|
|
|
|
log_info ( _("cancelled by user\n") );
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
log_error ( _("problem with the agent: agent returns 0x%lx\n"),
|
|
|
|
(ulong)reply );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
failure:
|
|
|
|
gcry_free (atext);
|
|
|
|
if ( fd != -1 )
|
|
|
|
agent_close (fd);
|
|
|
|
gcry_free (pw );
|
|
|
|
free_public_key( pk );
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Reste the cached passphrase
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
passphrase_clear_cache ( u32 *keyid, int algo )
|
|
|
|
{
|
|
|
|
size_t n;
|
|
|
|
char buf[50];
|
|
|
|
int fd = -1;
|
|
|
|
int nread;
|
|
|
|
u32 reply;
|
|
|
|
PKT_public_key *pk = gcry_xcalloc( 1, sizeof *pk );
|
|
|
|
byte fpr[MAX_FINGERPRINT_LEN];
|
|
|
|
|
|
|
|
#if MAX_FINGERPRINT_LEN < 20
|
|
|
|
#error agent needs a 20 byte fingerprint
|
|
|
|
#endif
|
|
|
|
|
|
|
|
memset (fpr, 0, MAX_FINGERPRINT_LEN );
|
|
|
|
if( !keyid || get_pubkey( pk, keyid ) ) {
|
|
|
|
log_debug ("oops, no key in passphrase_clear_cache\n");
|
|
|
|
goto failure; /* oops: no key for some reason */
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
size_t dummy;
|
|
|
|
fingerprint_from_pk( pk, fpr, &dummy );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( (fd = agent_open ()) == -1 )
|
|
|
|
goto failure;
|
|
|
|
|
|
|
|
n = 4 + 20;
|
|
|
|
u32tobuf (buf, n );
|
|
|
|
u32tobuf (buf+4, GPGA_PROT_CLEAR_PASSPHRASE );
|
|
|
|
memcpy (buf+8, fpr, 20 );
|
|
|
|
if ( writen ( fd, buf, 28 ) )
|
|
|
|
goto failure;
|
|
|
|
|
|
|
|
/* get response */
|
|
|
|
if ( readn ( fd, buf, 8, &nread ) )
|
|
|
|
goto failure;
|
|
|
|
|
|
|
|
if ( nread < 8 ) {
|
|
|
|
log_error ( _("response from agent too short\n") );
|
|
|
|
goto failure;
|
|
|
|
}
|
|
|
|
|
|
|
|
reply = buftou32 ( buf + 4 );
|
|
|
|
if ( reply != GPGA_PROT_OKAY && reply != GPGA_PROT_NO_PASSPHRASE ) {
|
|
|
|
log_error ( _("problem with the agent: agent returns 0x%lx\n"),
|
|
|
|
(ulong)reply );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
failure:
|
|
|
|
if ( fd != -1 )
|
|
|
|
agent_close (fd);
|
|
|
|
free_public_key( pk );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-11-18 15:06:00 +01:00
|
|
|
/****************
|
|
|
|
* Get a passphrase for the secret key with KEYID, display TEXT
|
|
|
|
* if the user needs to enter the passphrase.
|
1998-05-04 20:49:26 +02:00
|
|
|
* mode 0 = standard, 2 = create new passphrase
|
|
|
|
* Returns: a DEK with a session key; caller must free
|
|
|
|
* or NULL if the passphrase was not correctly repeated.
|
|
|
|
* (only for mode 2)
|
|
|
|
* a dek->keylen of 0 means: no passphrase entered.
|
|
|
|
* (only for mode 2)
|
1999-07-01 12:53:35 +02:00
|
|
|
* pubkey_algo is only informational.
|
1997-11-18 15:06:00 +01:00
|
|
|
*/
|
|
|
|
DEK *
|
1999-07-01 12:53:35 +02:00
|
|
|
passphrase_to_dek( u32 *keyid, int pubkey_algo,
|
|
|
|
int cipher_algo, STRING2KEY *s2k, int mode )
|
1997-11-18 15:06:00 +01:00
|
|
|
{
|
1998-05-26 15:38:00 +02:00
|
|
|
char *pw = NULL;
|
1997-11-18 15:06:00 +01:00
|
|
|
DEK *dek;
|
1998-05-04 20:49:26 +02:00
|
|
|
STRING2KEY help_s2k;
|
|
|
|
|
|
|
|
if( !s2k ) {
|
|
|
|
s2k = &help_s2k;
|
|
|
|
s2k->mode = 0;
|
|
|
|
/* this should be MD5 if cipher is IDEA, but because we do
|
1998-05-15 20:49:19 +02:00
|
|
|
* not have IDEA, we use the default one, the user
|
1998-05-04 20:49:26 +02:00
|
|
|
* can select it from the commandline
|
|
|
|
*/
|
1998-05-15 20:49:19 +02:00
|
|
|
s2k->hash_algo = opt.def_digest_algo?opt.def_digest_algo
|
|
|
|
:DEFAULT_DIGEST_ALGO;
|
1998-05-04 20:49:26 +02:00
|
|
|
}
|
1997-11-18 15:06:00 +01:00
|
|
|
|
1999-04-08 09:41:35 +02:00
|
|
|
if( !next_pw && is_status_enabled() ) {
|
1999-03-17 13:13:04 +01:00
|
|
|
char buf[50];
|
1999-04-08 09:41:35 +02:00
|
|
|
if( keyid ) {
|
|
|
|
sprintf( buf, "%08lX%08lX", (ulong)keyid[0], (ulong)keyid[1] );
|
|
|
|
if( keyid[2] && keyid[3] && keyid[0] != keyid[2]
|
|
|
|
&& keyid[1] != keyid[3] )
|
1999-07-01 12:53:35 +02:00
|
|
|
sprintf( buf+strlen(buf), " %08lX%08lX %d 0",
|
|
|
|
(ulong)keyid[2], (ulong)keyid[3], pubkey_algo );
|
1999-04-08 09:41:35 +02:00
|
|
|
write_status_text( STATUS_NEED_PASSPHRASE, buf );
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
sprintf( buf, "%d %d %d", cipher_algo, s2k->mode, s2k->hash_algo );
|
|
|
|
write_status_text( STATUS_NEED_PASSPHRASE_SYM, buf );
|
|
|
|
}
|
1999-03-17 13:13:04 +01:00
|
|
|
}
|
|
|
|
|
1998-05-26 15:38:00 +02:00
|
|
|
if( keyid && !opt.batch && !next_pw ) {
|
2000-01-24 12:55:49 +01:00
|
|
|
PKT_public_key *pk = gcry_xcalloc( 1, sizeof *pk );
|
1998-05-29 13:53:54 +02:00
|
|
|
size_t n;
|
|
|
|
char *p;
|
1997-12-20 18:23:29 +01:00
|
|
|
|
1998-05-29 13:53:54 +02:00
|
|
|
tty_printf(_("\nYou need a passphrase to unlock the secret key for\n"
|
|
|
|
"user: \"") );
|
|
|
|
p = get_user_id( keyid, &n );
|
1999-10-26 14:14:37 +02:00
|
|
|
tty_print_utf8_string( p, n );
|
2000-01-24 12:55:49 +01:00
|
|
|
gcry_free(p);
|
1998-05-29 13:53:54 +02:00
|
|
|
tty_printf("\"\n");
|
|
|
|
|
1998-06-29 14:30:57 +02:00
|
|
|
if( !get_pubkey( pk, keyid ) ) {
|
2000-07-14 19:34:53 +02:00
|
|
|
const char *s = gcry_pk_algo_name( pk->pubkey_algo );
|
1999-01-07 18:05:48 +01:00
|
|
|
tty_printf( _("%u-bit %s key, ID %08lX, created %s"),
|
2000-07-14 19:34:53 +02:00
|
|
|
nbits_from_pk( pk ), s?s:"?", (ulong)keyid[1],
|
1998-06-29 14:30:57 +02:00
|
|
|
strtimestamp(pk->timestamp) );
|
1999-01-07 18:05:48 +01:00
|
|
|
if( keyid[2] && keyid[3] && keyid[0] != keyid[2]
|
|
|
|
&& keyid[1] != keyid[3] )
|
|
|
|
tty_printf( _(" (main key ID %08lX)"), (ulong)keyid[3] );
|
|
|
|
tty_printf("\n");
|
1998-05-29 13:53:54 +02:00
|
|
|
}
|
1999-01-07 18:05:48 +01:00
|
|
|
|
1998-05-29 13:53:54 +02:00
|
|
|
tty_printf("\n");
|
1998-06-29 14:30:57 +02:00
|
|
|
free_public_key( pk );
|
1997-11-18 15:06:00 +01:00
|
|
|
}
|
1998-08-08 21:27:00 +02:00
|
|
|
|
1998-05-26 15:38:00 +02:00
|
|
|
if( next_pw ) {
|
|
|
|
pw = next_pw;
|
|
|
|
next_pw = NULL;
|
|
|
|
}
|
2000-08-21 17:54:37 +02:00
|
|
|
else if ( opt.use_agent ) {
|
|
|
|
pw = agent_get_passphrase ( keyid, mode == 2? 1: 0 );
|
|
|
|
if ( !pw )
|
|
|
|
pw = gcry_xstrdup ("");
|
|
|
|
if( *pw && mode == 2 ) {
|
|
|
|
char *pw2 = agent_get_passphrase ( keyid, 2 );
|
|
|
|
if ( !pw2 )
|
|
|
|
pw2 = gcry_xstrdup ("");
|
|
|
|
if( strcmp(pw, pw2) ) {
|
|
|
|
gcry_free(pw2);
|
|
|
|
gcry_free(pw);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
gcry_free(pw2);
|
|
|
|
}
|
|
|
|
}
|
1998-09-28 21:25:31 +02:00
|
|
|
else if( fd_passwd ) {
|
2000-01-24 12:55:49 +01:00
|
|
|
pw = gcry_xmalloc_secure( strlen(fd_passwd)+1 );
|
1998-09-28 21:25:31 +02:00
|
|
|
strcpy( pw, fd_passwd );
|
1997-11-18 15:06:00 +01:00
|
|
|
}
|
1999-06-15 14:31:07 +02:00
|
|
|
else if( opt.batch ) {
|
1999-07-12 14:57:54 +02:00
|
|
|
log_error(_("can't query password in batchmode\n"));
|
2000-01-24 12:55:49 +01:00
|
|
|
pw = gcry_xstrdup( "" ); /* return an empty passphrase */
|
1999-06-15 14:31:07 +02:00
|
|
|
}
|
1998-01-02 21:40:10 +01:00
|
|
|
else {
|
1998-11-27 12:42:49 +01:00
|
|
|
pw = cpr_get_hidden("passphrase.enter", _("Enter passphrase: ") );
|
1998-01-02 21:40:10 +01:00
|
|
|
tty_kill_prompt();
|
1998-08-08 21:27:00 +02:00
|
|
|
if( mode == 2 && !cpr_enabled() ) {
|
1998-11-27 12:42:49 +01:00
|
|
|
char *pw2 = cpr_get_hidden("passphrase.repeat",
|
1998-11-10 13:59:59 +01:00
|
|
|
_("Repeat passphrase: ") );
|
1998-05-04 20:49:26 +02:00
|
|
|
tty_kill_prompt();
|
|
|
|
if( strcmp(pw, pw2) ) {
|
2000-01-24 12:55:49 +01:00
|
|
|
gcry_free(pw2);
|
|
|
|
gcry_free(pw);
|
1998-05-04 20:49:26 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
2000-01-24 12:55:49 +01:00
|
|
|
gcry_free(pw2);
|
1997-11-18 15:06:00 +01:00
|
|
|
}
|
|
|
|
}
|
1999-04-09 12:34:44 +02:00
|
|
|
|
|
|
|
if( !pw || !*pw )
|
|
|
|
write_status( STATUS_MISSING_PASSPHRASE );
|
|
|
|
|
2000-01-24 12:55:49 +01:00
|
|
|
dek = gcry_xmalloc_secure( sizeof *dek );
|
1998-05-04 20:49:26 +02:00
|
|
|
dek->algo = cipher_algo;
|
|
|
|
if( !*pw && mode == 2 )
|
|
|
|
dek->keylen = 0;
|
1997-12-09 13:46:23 +01:00
|
|
|
else
|
1998-05-03 17:42:08 +02:00
|
|
|
hash_passphrase( dek, pw, s2k, mode==2 );
|
2000-01-24 12:55:49 +01:00
|
|
|
gcry_free(last_pw);
|
1998-05-26 15:38:00 +02:00
|
|
|
last_pw = pw;
|
1998-05-04 20:49:26 +02:00
|
|
|
return dek;
|
1997-11-18 15:06:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-05-03 17:42:08 +02:00
|
|
|
/****************
|
|
|
|
* Hash a passphrase using the supplied s2k. If create is true, create
|
1998-09-28 21:25:31 +02:00
|
|
|
* a new salt or what else must be filled into the s2k for a new key.
|
1998-05-03 17:42:08 +02:00
|
|
|
* always needs: dek->algo, s2k->mode, s2k->hash_algo.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
hash_passphrase( DEK *dek, char *pw, STRING2KEY *s2k, int create )
|
1997-11-18 15:06:00 +01:00
|
|
|
{
|
1999-10-26 14:14:37 +02:00
|
|
|
GCRY_MD_HD md;
|
1998-09-28 21:25:31 +02:00
|
|
|
int pass, i;
|
|
|
|
int used = 0;
|
|
|
|
int pwlen = strlen(pw);
|
1997-11-18 15:06:00 +01:00
|
|
|
|
1998-05-03 17:42:08 +02:00
|
|
|
assert( s2k->hash_algo );
|
1999-10-26 14:14:37 +02:00
|
|
|
dek->keylen = gcry_cipher_get_algo_keylen( dek->algo );
|
1998-09-28 21:25:31 +02:00
|
|
|
if( !(dek->keylen > 0 && dek->keylen <= DIM(dek->key)) )
|
|
|
|
BUG();
|
|
|
|
|
1999-10-26 14:14:37 +02:00
|
|
|
if( !(md = gcry_md_open( s2k->hash_algo, GCRY_MD_FLAG_SECURE )) )
|
|
|
|
BUG();
|
|
|
|
|
1998-09-28 21:25:31 +02:00
|
|
|
for(pass=0; used < dek->keylen ; pass++ ) {
|
|
|
|
if( pass ) {
|
1999-10-26 14:14:37 +02:00
|
|
|
gcry_md_reset(md);
|
1998-09-28 21:25:31 +02:00
|
|
|
for(i=0; i < pass; i++ ) /* preset the hash context */
|
1999-10-26 14:14:37 +02:00
|
|
|
gcry_md_putc(md, 0 );
|
1998-09-28 21:25:31 +02:00
|
|
|
}
|
1998-08-05 20:30:53 +02:00
|
|
|
|
1998-09-28 21:25:31 +02:00
|
|
|
if( s2k->mode == 1 || s2k->mode == 3 ) {
|
|
|
|
int len2 = pwlen + 8;
|
|
|
|
ulong count = len2;
|
1998-08-05 20:30:53 +02:00
|
|
|
|
1998-09-28 21:25:31 +02:00
|
|
|
if( create && !pass ) {
|
1999-11-13 17:43:23 +01:00
|
|
|
gcry_randomize(s2k->salt, 8, GCRY_STRONG_RANDOM );
|
1998-09-28 21:25:31 +02:00
|
|
|
if( s2k->mode == 3 )
|
1998-12-08 13:20:53 +01:00
|
|
|
s2k->count = 96; /* 65536 iterations */
|
1998-08-11 19:29:34 +02:00
|
|
|
}
|
1998-09-28 21:25:31 +02:00
|
|
|
|
|
|
|
if( s2k->mode == 3 ) {
|
|
|
|
count = (16ul + (s2k->count & 15)) << ((s2k->count >> 4) + 6);
|
|
|
|
if( count < len2 )
|
|
|
|
count = len2;
|
|
|
|
}
|
|
|
|
/* a little bit complicated because we need a ulong for count */
|
|
|
|
while( count > len2 ) { /* maybe iterated+salted */
|
1999-10-26 14:14:37 +02:00
|
|
|
gcry_md_write( md, s2k->salt, 8 );
|
|
|
|
gcry_md_write( md, pw, pwlen );
|
1998-09-28 21:25:31 +02:00
|
|
|
count -= len2;
|
|
|
|
}
|
|
|
|
if( count < 8 )
|
1999-10-26 14:14:37 +02:00
|
|
|
gcry_md_write( md, s2k->salt, count );
|
1998-08-05 20:30:53 +02:00
|
|
|
else {
|
1999-10-26 14:14:37 +02:00
|
|
|
gcry_md_write( md, s2k->salt, 8 );
|
1998-08-05 20:30:53 +02:00
|
|
|
count -= 8;
|
1999-10-26 14:14:37 +02:00
|
|
|
gcry_md_write( md, pw, count );
|
1998-08-05 20:30:53 +02:00
|
|
|
}
|
|
|
|
}
|
1998-09-28 21:25:31 +02:00
|
|
|
else
|
1999-10-26 14:14:37 +02:00
|
|
|
gcry_md_write( md, pw, pwlen );
|
|
|
|
gcry_md_final( md );
|
|
|
|
i = gcry_md_get_algo_dlen( s2k->hash_algo );
|
1998-09-28 21:25:31 +02:00
|
|
|
if( i > dek->keylen - used )
|
|
|
|
i = dek->keylen - used;
|
1999-10-26 14:14:37 +02:00
|
|
|
memcpy( dek->key+used, gcry_md_read(md, s2k->hash_algo), i );
|
1998-09-28 21:25:31 +02:00
|
|
|
used += i;
|
1998-04-04 22:16:55 +02:00
|
|
|
}
|
1999-10-26 14:14:37 +02:00
|
|
|
gcry_md_close(md);
|
1997-11-18 15:06:00 +01:00
|
|
|
}
|
|
|
|
|