1
0
mirror of git://git.gnupg.org/gnupg.git synced 2024-11-04 20:38:50 +01:00

GPG agent support

This commit is contained in:
Werner Koch 2000-11-16 15:45:03 +00:00
parent d0af3b25d2
commit 85cec300b7
7 changed files with 407 additions and 0 deletions

2
NEWS
View File

@ -1,4 +1,6 @@
* Support for the gpg-agent from gpg 1.1
* Better LFS support. * Better LFS support.
* New option --ignore-crc-error * New option --ignore-crc-error

View File

@ -1,3 +1,12 @@
2000-11-16 Werner Koch <wk@gnupg.org>
* g10.c: New option --use-agent
* passphrase.c (agent_open,agent_close): New.
(agent_get_passphrase,agent_clear_passphrase): New.
(passphrase_clear_cache): New.
(passphrase_to_dek): Use the agent here.
* seckey-cert.c (do_check): Clear cached passphrases.
2000-11-15 Werner Koch <wk@gnupg.org> 2000-11-15 Werner Koch <wk@gnupg.org>
* status.c (write_status_text): Moved the big switch to ... * status.c (write_status_text): Moved the big switch to ...

View File

@ -196,6 +196,7 @@ enum cmd_and_opt_values { aNull = 0,
oOverrideSessionKey, oOverrideSessionKey,
oNoRandomSeedFile, oNoRandomSeedFile,
oNoAutoKeyRetrieve, oNoAutoKeyRetrieve,
oUseAgent,
oMergeOnly, oMergeOnly,
oTryAllSecrets, oTryAllSecrets,
oTrustedKey, oTrustedKey,
@ -282,6 +283,7 @@ static ARGPARSE_OPTS opts[] = {
{ oForceMDC, "force-mdc", 0, N_("always use a MDC for encryption") }, { oForceMDC, "force-mdc", 0, N_("always use a MDC for encryption") },
{ oDryRun, "dry-run", 0, N_("do not make any changes") }, { oDryRun, "dry-run", 0, N_("do not make any changes") },
/*{ oInteractive, "interactive", 0, N_("prompt before overwriting") }, */ /*{ oInteractive, "interactive", 0, N_("prompt before overwriting") }, */
{ oUseAgent, "use-agent",0, N_("use the gpg-agent")},
{ oBatch, "batch", 0, N_("batch mode: never ask")}, { oBatch, "batch", 0, N_("batch mode: never ask")},
{ oAnswerYes, "yes", 0, N_("assume yes on most questions")}, { oAnswerYes, "yes", 0, N_("assume yes on most questions")},
{ oAnswerNo, "no", 0, N_("assume no on most questions")}, { oAnswerNo, "no", 0, N_("assume no on most questions")},
@ -783,6 +785,7 @@ main( int argc, char **argv )
case oKOption: set_cmd( &cmd, aKMode ); break; case oKOption: set_cmd( &cmd, aKMode ); break;
case oBatch: opt.batch = 1; greeting = 0; break; case oBatch: opt.batch = 1; greeting = 0; break;
case oUseAgent: opt.use_agent = 1; break;
case oAnswerYes: opt.answer_yes = 1; break; case oAnswerYes: opt.answer_yes = 1; break;
case oAnswerNo: opt.answer_no = 1; break; case oAnswerNo: opt.answer_no = 1; break;
case oKeyring: append_to_strlist( &nrings, pargs.r.ret_str); break; case oKeyring: append_to_strlist( &nrings, pargs.r.ret_str); break;

View File

@ -125,6 +125,7 @@ int build_sk_list( STRLIST locusr, SK_LIST *ret_sk_list,
/*-- passphrase.h --*/ /*-- passphrase.h --*/
int have_static_passphrase(void); int have_static_passphrase(void);
void read_passphrase_from_fd( int fd ); void read_passphrase_from_fd( int fd );
void passphrase_clear_cache ( u32 *keyid, int algo );
DEK *passphrase_to_dek( u32 *keyid, int pubkey_algo, DEK *passphrase_to_dek( u32 *keyid, int pubkey_algo,
int cipher_algo, STRING2KEY *s2k, int mode); int cipher_algo, STRING2KEY *s2k, int mode);
void set_next_passphrase( const char *s ); void set_next_passphrase( const char *s );

View File

@ -95,6 +95,7 @@ struct {
int auto_key_retrieve; int auto_key_retrieve;
const char *override_session_key; const char *override_session_key;
int show_session_key; int show_session_key;
int use_agent;
int merge_only; int merge_only;
int try_all_secrets; int try_all_secrets;
} opt; } opt;

View File

@ -19,11 +19,16 @@
*/ */
#include <config.h> #include <config.h>
#include <stddef.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include <assert.h> #include <assert.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <errno.h>
#include "util.h" #include "util.h"
#include "memory.h" #include "memory.h"
#include "options.h" #include "options.h"
@ -34,6 +39,43 @@
#include "i18n.h" #include "i18n.h"
#include "status.h" #include "status.h"
enum gpga_protocol_codes {
/* Request codes */
GPGA_PROT_GET_VERSION = 1,
GPGA_PROT_GET_PASSPHRASE = 2,
GPGA_PROT_CLEAR_PASSPHRASE= 3,
GPGA_PROT_SHUTDOWN = 4,
GPGA_PROT_FLUSH = 5,
/* Reply codes */
GPGA_PROT_REPLY_BASE = 0x10000,
GPGA_PROT_OKAY = 0x10001,
GPGA_PROT_GOT_PASSPHRASE = 0x10002,
/* Error codes */
GPGA_PROT_ERROR_BASE = 0x20000,
GPGA_PROT_PROTOCOL_ERROR = 0x20001,
GPGA_PROT_INVALID_REQUEST= 0x20002,
GPGA_PROT_CANCELED = 0x20003,
GPGA_PROT_NO_PASSPHRASE = 0x20004,
GPGA_PROT_BAD_PASSPHRASE = 0x20005,
GPGA_PROT_INVALID_DATA = 0x20006,
GPGA_PROT_NOT_IMPLEMENTED= 0x20007,
GPGA_PROT_UI_PROBLEM = 0x20008,
};
#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)
static char *fd_passwd = NULL; static char *fd_passwd = NULL;
static char *next_pw = NULL; static char *next_pw = NULL;
static char *last_pw = NULL; static char *last_pw = NULL;
@ -43,6 +85,8 @@ static void hash_passphrase( DEK *dek, char *pw, STRING2KEY *s2k, int create );
int int
have_static_passphrase() have_static_passphrase()
{ {
if ( opt.use_agent )
return 0;
return !!fd_passwd; return !!fd_passwd;
} }
@ -81,6 +125,9 @@ read_passphrase_from_fd( int fd )
int i, len; int i, len;
char *pw; char *pw;
if ( opt.use_agent )
return; /* not used here */
if( !opt.batch ) if( !opt.batch )
tty_printf("Reading passphrase from file descriptor %d ...", fd ); tty_printf("Reading passphrase from file descriptor %d ...", fd );
for( pw = NULL, i = len = 100; ; i++ ) { for( pw = NULL, i = len = 100; ; i++ ) {
@ -104,6 +151,332 @@ read_passphrase_from_fd( int fd )
fd_passwd = pw; fd_passwd = pw;
} }
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 = m_strdup ( infostr );
if ( !(p = strchr ( infostr, ':')) || p == infostr
|| (p-infostr)+1 >= sizeof client_addr.sun_path ) {
log_error (_("malformed GPG_AGENT_INFO environment variable\n"));
m_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) );
m_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) );
m_free (infostr );
close (fd );
return -1;
}
m_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 = m_alloc_clear( 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 = pubkey_algo_to_string ( pk->pubkey_algo );
const char *timestr;
char *maink;
const char *fmtstr;
if ( !algo_name )
algo_name = "?";
fmtstr = _(" (main key ID %08lX)");
maink = m_alloc ( 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 = m_alloc ( 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 );
m_free (uid);
m_free (maink);
{
size_t dummy;
fingerprint_from_pk( pk, fpr, &dummy );
}
}
else if (mode == 1 )
atext = m_strdup ( _("Enter passphrase\n") );
else
atext = m_strdup ( _("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;
m_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
* this by issuing 2 read calls */
pw = m_alloc_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:
m_free (atext);
if ( fd != -1 )
agent_close (fd);
m_free (pw );
free_public_key( pk );
return NULL;
}
/*
* Clear 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 = m_alloc_clear ( 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 );
}
/**************** /****************
* Get a passphrase for the secret key with KEYID, display TEXT * Get a passphrase for the secret key with KEYID, display TEXT
@ -182,6 +555,22 @@ passphrase_to_dek( u32 *keyid, int pubkey_algo,
pw = next_pw; pw = next_pw;
next_pw = NULL; next_pw = NULL;
} }
else if ( opt.use_agent ) {
pw = agent_get_passphrase ( keyid, mode == 2? 1: 0 );
if ( !pw )
pw = m_strdup ("");
if( *pw && mode == 2 ) {
char *pw2 = agent_get_passphrase ( keyid, 2 );
if ( !pw2 )
pw2 = m_strdup ("");
if( strcmp(pw, pw2) ) {
m_free(pw2);
m_free(pw);
return NULL;
}
m_free(pw2);
}
}
else if( fd_passwd ) { else if( fd_passwd ) {
pw = m_alloc_secure( strlen(fd_passwd)+1 ); pw = m_alloc_secure( strlen(fd_passwd)+1 );
strcpy( pw, fd_passwd ); strcpy( pw, fd_passwd );

View File

@ -138,6 +138,7 @@ do_check( PKT_secret_key *sk )
/* now let's see whether we have used the right passphrase */ /* now let's see whether we have used the right passphrase */
if( csum != sk->csum ) { if( csum != sk->csum ) {
copy_secret_key( sk, save_sk ); copy_secret_key( sk, save_sk );
passphrase_clear_cache ( keyid, sk->pubkey_algo );
free_secret_key( save_sk ); free_secret_key( save_sk );
return G10ERR_BAD_PASS; return G10ERR_BAD_PASS;
} }
@ -145,6 +146,7 @@ do_check( PKT_secret_key *sk )
res = pubkey_check_secret_key( sk->pubkey_algo, sk->skey ); res = pubkey_check_secret_key( sk->pubkey_algo, sk->skey );
if( res ) { if( res ) {
copy_secret_key( sk, save_sk ); copy_secret_key( sk, save_sk );
passphrase_clear_cache ( keyid, sk->pubkey_algo );
free_secret_key( save_sk ); free_secret_key( save_sk );
return G10ERR_BAD_PASS; return G10ERR_BAD_PASS;
} }