1997-11-18 14:06:00 +00:00
|
|
|
/* passphrase.c - Get a passphrase
|
2004-03-05 00:01:25 +00:00
|
|
|
* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003,
|
|
|
|
* 2004 Free Software Foundation, Inc.
|
1997-11-18 14:06:00 +00:00
|
|
|
*
|
1998-12-23 12:41:40 +00:00
|
|
|
* This file is part of GnuPG.
|
1997-11-18 14:06:00 +00:00
|
|
|
*
|
1998-12-23 12:41:40 +00:00
|
|
|
* GnuPG is free software; you can redistribute it and/or modify
|
1997-11-18 14:06:00 +00: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 12:41:40 +00:00
|
|
|
* GnuPG is distributed in the hope that it will be useful,
|
1997-11-18 14:06:00 +00: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>
|
2002-06-29 13:46:34 +00:00
|
|
|
#include <stddef.h>
|
1997-11-18 14:06:00 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
1998-01-16 21:15:24 +00:00
|
|
|
#include <unistd.h>
|
1997-11-18 14:06:00 +00:00
|
|
|
#include <assert.h>
|
2002-06-29 13:46:34 +00:00
|
|
|
#if !defined(HAVE_DOSISH_SYSTEM) && !defined(__riscos__)
|
2000-08-21 15:54:37 +00:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/un.h>
|
2002-06-29 13:46:34 +00:00
|
|
|
#endif
|
2004-12-20 05:19:09 +00:00
|
|
|
#if defined (_WIN32)
|
|
|
|
#include <windows.h>
|
2002-06-29 13:46:34 +00:00
|
|
|
#endif
|
2000-08-21 15:54:37 +00:00
|
|
|
#include <errno.h>
|
2002-06-29 13:46:34 +00:00
|
|
|
#ifdef HAVE_LOCALE_H
|
|
|
|
#include <locale.h>
|
|
|
|
#endif
|
2003-04-10 09:56:47 +00:00
|
|
|
#ifdef HAVE_LANGINFO_CODESET
|
|
|
|
#include <langinfo.h>
|
|
|
|
#endif
|
2000-07-14 17:34:53 +00:00
|
|
|
|
|
|
|
#include "util.h"
|
2002-06-29 13:46:34 +00:00
|
|
|
#include "memory.h"
|
1998-01-02 20:40:10 +00:00
|
|
|
#include "options.h"
|
1997-11-18 14:06:00 +00:00
|
|
|
#include "ttyio.h"
|
2002-06-29 13:46:34 +00:00
|
|
|
#include "cipher.h"
|
1997-12-20 17:23:29 +00:00
|
|
|
#include "keydb.h"
|
1998-05-15 18:49:19 +00:00
|
|
|
#include "main.h"
|
1998-05-29 11:53:54 +00:00
|
|
|
#include "i18n.h"
|
1998-08-08 19:27:00 +00:00
|
|
|
#include "status.h"
|
2002-06-29 13:46:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
};
|
|
|
|
|
2000-08-21 15:54:37 +00:00
|
|
|
|
|
|
|
#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)
|
|
|
|
|
2002-06-29 13:46:34 +00:00
|
|
|
#define digitp(p) (*(p) >= '0' && *(p) <= '9')
|
|
|
|
#define hexdigitp(a) (digitp (a) \
|
|
|
|
|| (*(a) >= 'A' && *(a) <= 'F') \
|
|
|
|
|| (*(a) >= 'a' && *(a) <= 'f'))
|
|
|
|
#define xtoi_1(p) (*(p) <= '9'? (*(p)- '0'): \
|
|
|
|
*(p) <= 'F'? (*(p)-'A'+10):(*(p)-'a'+10))
|
|
|
|
#define xtoi_2(p) ((xtoi_1(p) * 16) + xtoi_1((p)+1))
|
|
|
|
|
|
|
|
|
1997-11-18 14:06:00 +00:00
|
|
|
|
1998-09-28 19:25:31 +00:00
|
|
|
static char *fd_passwd = NULL;
|
1998-05-26 13:38:00 +00:00
|
|
|
static char *next_pw = NULL;
|
|
|
|
static char *last_pw = NULL;
|
1997-11-18 14:06:00 +00:00
|
|
|
|
2003-08-29 00:14:42 +00:00
|
|
|
#if defined (_WIN32)
|
2002-06-29 13:46:34 +00:00
|
|
|
static int read_fd = 0;
|
|
|
|
static int write_fd = 0;
|
|
|
|
#endif
|
|
|
|
|
1998-05-03 19:35:33 +00:00
|
|
|
static void hash_passphrase( DEK *dek, char *pw, STRING2KEY *s2k, int create );
|
1997-11-18 14:06:00 +00:00
|
|
|
|
1998-01-02 20:40:10 +00:00
|
|
|
int
|
1998-09-28 19:25:31 +00:00
|
|
|
have_static_passphrase()
|
1998-01-02 20:40:10 +00:00
|
|
|
{
|
2000-08-21 15:54:37 +00:00
|
|
|
if ( opt.use_agent )
|
|
|
|
return 0;
|
1998-09-28 19:25:31 +00:00
|
|
|
return !!fd_passwd;
|
1998-01-02 20:40:10 +00:00
|
|
|
}
|
|
|
|
|
1998-05-26 13:38:00 +00:00
|
|
|
/****************
|
|
|
|
* Set the passphrase to be used for the next query and only for the next
|
|
|
|
* one.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
set_next_passphrase( const char *s )
|
|
|
|
{
|
2002-06-29 13:46:34 +00:00
|
|
|
m_free(next_pw);
|
1998-05-26 13:38:00 +00:00
|
|
|
next_pw = NULL;
|
|
|
|
if( s ) {
|
2002-06-29 13:46:34 +00:00
|
|
|
next_pw = m_alloc_secure( strlen(s)+1 );
|
1998-05-26 13:38:00 +00: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 14:06:00 +00:00
|
|
|
|
1998-09-28 19:25:31 +00:00
|
|
|
void
|
|
|
|
read_passphrase_from_fd( int fd )
|
|
|
|
{
|
2003-04-10 09:56:47 +00:00
|
|
|
int i, len;
|
|
|
|
char *pw;
|
|
|
|
|
|
|
|
if ( opt.use_agent )
|
|
|
|
{ /* Not used but we have to do a dummy read, so that it won't end
|
|
|
|
up at the begin of the message if the quite usual trick to
|
|
|
|
prepend the passphtrase to the message is used. */
|
|
|
|
char buf[1];
|
1998-09-28 19:25:31 +00:00
|
|
|
|
2003-04-10 09:56:47 +00:00
|
|
|
while (!(read (fd, buf, 1) != 1 || *buf == '\n' ))
|
|
|
|
;
|
|
|
|
*buf = 0;
|
|
|
|
return;
|
|
|
|
}
|
2000-08-21 15:54:37 +00:00
|
|
|
|
2003-04-10 09:56:47 +00:00
|
|
|
if (!opt.batch )
|
1998-09-28 19:25:31 +00:00
|
|
|
tty_printf("Reading passphrase from file descriptor %d ...", fd );
|
2003-04-10 09:56:47 +00:00
|
|
|
for (pw = NULL, i = len = 100; ; i++ )
|
|
|
|
{
|
|
|
|
if (i >= len-1 )
|
|
|
|
{
|
|
|
|
char *pw2 = pw;
|
|
|
|
len += 100;
|
|
|
|
pw = m_alloc_secure( len );
|
|
|
|
if( pw2 )
|
2004-12-13 08:04:52 +00:00
|
|
|
{
|
|
|
|
memcpy(pw, pw2, i );
|
|
|
|
m_free (pw2);
|
|
|
|
}
|
2003-04-10 09:56:47 +00:00
|
|
|
else
|
|
|
|
i=0;
|
1998-09-28 19:25:31 +00:00
|
|
|
}
|
2003-04-10 09:56:47 +00:00
|
|
|
if (read( fd, pw+i, 1) != 1 || pw[i] == '\n' )
|
|
|
|
break;
|
1998-09-28 19:25:31 +00:00
|
|
|
}
|
2003-04-10 09:56:47 +00:00
|
|
|
pw[i] = 0;
|
|
|
|
if (!opt.batch)
|
|
|
|
tty_printf("\b\b\b \n" );
|
1998-09-28 19:25:31 +00:00
|
|
|
|
2003-04-10 09:56:47 +00:00
|
|
|
m_free( fd_passwd );
|
|
|
|
fd_passwd = pw;
|
1998-09-28 19:25:31 +00:00
|
|
|
}
|
|
|
|
|
2000-08-21 15:54:37 +00:00
|
|
|
static int
|
2004-11-03 20:03:46 +00:00
|
|
|
writen (int fd, const void *buf, size_t nbytes)
|
2000-08-21 15:54:37 +00:00
|
|
|
{
|
2003-08-29 00:14:42 +00:00
|
|
|
#if defined (_WIN32)
|
2004-11-03 20:03:46 +00:00
|
|
|
DWORD nwritten, nleft = nbytes;
|
2002-06-29 13:46:34 +00:00
|
|
|
|
2004-11-03 20:03:46 +00:00
|
|
|
while (nleft > 0)
|
|
|
|
{
|
|
|
|
if (!WriteFile ((HANDLE)write_fd, buf, nleft, &nwritten, NULL))
|
|
|
|
{
|
|
|
|
log_error ("write failed: %s\n", w32_strerror (0));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
/*log_info ("** WriteFile fd=%d nytes=%d nwritten=%d\n",
|
|
|
|
write_fd, nbytes, (int)nwritten);*/
|
|
|
|
Sleep (100);
|
2002-06-29 13:46:34 +00:00
|
|
|
|
2004-11-03 20:03:46 +00:00
|
|
|
nleft -= nwritten;
|
|
|
|
buf = (const BYTE *)buf + nwritten;
|
2002-06-29 13:46:34 +00:00
|
|
|
}
|
|
|
|
#elif defined(HAVE_DOSISH_SYSTEM) || defined(__riscos__)
|
2004-11-03 20:03:46 +00:00
|
|
|
/* not implemented */
|
2002-06-29 13:46:34 +00:00
|
|
|
#else
|
2004-11-03 20:03:46 +00:00
|
|
|
size_t nleft = nbytes;
|
|
|
|
int nwritten;
|
2000-08-21 15:54:37 +00:00
|
|
|
|
2004-11-03 20:03:46 +00:00
|
|
|
while (nleft > 0)
|
|
|
|
{
|
|
|
|
nwritten = write (fd, buf, nleft);
|
|
|
|
if (nwritten < 0)
|
|
|
|
{
|
|
|
|
if (errno == EINTR)
|
|
|
|
nwritten = 0;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
log_error ("write() failed: %s\n", strerror (errno));
|
|
|
|
return -1;
|
2000-08-21 15:54:37 +00:00
|
|
|
}
|
|
|
|
}
|
2004-11-03 20:03:46 +00:00
|
|
|
nleft -= nwritten;
|
|
|
|
buf = (const char*)buf + nwritten;
|
2000-08-21 15:54:37 +00:00
|
|
|
}
|
2002-06-29 13:46:34 +00:00
|
|
|
#endif
|
|
|
|
|
2004-11-03 20:03:46 +00:00
|
|
|
return 0;
|
2000-08-21 15:54:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
2004-11-03 20:03:46 +00:00
|
|
|
readn (int fd, void *buf, size_t buflen, size_t *ret_nread)
|
2000-08-21 15:54:37 +00:00
|
|
|
{
|
2003-08-29 00:14:42 +00:00
|
|
|
#if defined (_WIN32)
|
2004-11-03 20:03:46 +00:00
|
|
|
DWORD nread, nleft = buflen;
|
2002-06-29 13:46:34 +00:00
|
|
|
|
2004-11-03 20:03:46 +00:00
|
|
|
while (nleft > 0)
|
|
|
|
{
|
|
|
|
if (!ReadFile ((HANDLE)read_fd, buf, nleft, &nread, NULL))
|
|
|
|
{
|
|
|
|
log_error ("read() error: %s\n", w32_strerror (0));
|
|
|
|
return -1;
|
2002-06-29 13:46:34 +00:00
|
|
|
}
|
2004-11-03 20:03:46 +00:00
|
|
|
if (!nread || GetLastError() == ERROR_BROKEN_PIPE)
|
|
|
|
break;
|
|
|
|
/*log_info ("** ReadFile fd=%d buflen=%d nread=%d\n",
|
|
|
|
read_fd, buflen, (int)nread);*/
|
|
|
|
Sleep (100);
|
2002-06-29 13:46:34 +00:00
|
|
|
|
2004-11-03 20:03:46 +00:00
|
|
|
nleft -= nread;
|
|
|
|
buf = (BYTE *)buf + nread;
|
2002-06-29 13:46:34 +00:00
|
|
|
}
|
2004-11-03 20:03:46 +00:00
|
|
|
if (ret_nread)
|
|
|
|
*ret_nread = buflen - nleft;
|
2002-06-29 13:46:34 +00:00
|
|
|
|
|
|
|
#elif defined(HAVE_DOSISH_SYSTEM) || defined(__riscos__)
|
2004-11-03 20:03:46 +00:00
|
|
|
/* not implemented */
|
2002-06-29 13:46:34 +00:00
|
|
|
#else
|
2004-11-03 20:03:46 +00:00
|
|
|
size_t nleft = buflen;
|
|
|
|
int nread;
|
|
|
|
char *p;
|
2004-11-17 16:04:21 +00:00
|
|
|
|
2004-11-03 20:03:46 +00:00
|
|
|
p = buf;
|
2004-11-17 16:04:21 +00:00
|
|
|
while( nleft > 0 )
|
2004-11-03 20:03:46 +00:00
|
|
|
{
|
2004-11-17 16:04:21 +00:00
|
|
|
nread = read ( fd, buf, nleft );
|
|
|
|
if( nread < 0 )
|
2004-11-03 20:03:46 +00:00
|
|
|
{
|
2004-11-17 16:04:21 +00:00
|
|
|
if (errno == EINTR)
|
2004-11-03 20:03:46 +00:00
|
|
|
nread = 0;
|
2004-11-17 16:04:21 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
log_error ( "read() error: %s\n", strerror (errno) );
|
|
|
|
return -1;
|
|
|
|
}
|
2000-08-21 15:54:37 +00:00
|
|
|
}
|
2004-11-03 20:03:46 +00:00
|
|
|
else if (!nread)
|
|
|
|
break; /* EOF */
|
|
|
|
nleft -= nread;
|
|
|
|
buf = (char*)buf + nread;
|
2000-08-21 15:54:37 +00:00
|
|
|
}
|
2004-11-03 20:03:46 +00:00
|
|
|
if (ret_nread)
|
|
|
|
*ret_nread = buflen - nleft;
|
2002-06-29 13:46:34 +00:00
|
|
|
#endif
|
|
|
|
|
2004-11-03 20:03:46 +00:00
|
|
|
return 0;
|
2000-08-21 15:54:37 +00:00
|
|
|
}
|
|
|
|
|
2002-06-29 13:46:34 +00:00
|
|
|
/* read an entire line */
|
|
|
|
static int
|
|
|
|
readline (int fd, char *buf, size_t buflen)
|
|
|
|
{
|
|
|
|
size_t nleft = buflen;
|
|
|
|
char *p;
|
|
|
|
int nread = 0;
|
|
|
|
|
|
|
|
while (nleft > 0)
|
|
|
|
{
|
|
|
|
int n = read (fd, buf, nleft);
|
|
|
|
if (n < 0)
|
|
|
|
{
|
|
|
|
if (errno == EINTR)
|
|
|
|
continue;
|
|
|
|
return -1; /* read error */
|
|
|
|
}
|
|
|
|
else if (!n)
|
|
|
|
{
|
|
|
|
return -1; /* incomplete line */
|
|
|
|
}
|
|
|
|
p = buf;
|
|
|
|
nleft -= n;
|
|
|
|
buf += n;
|
|
|
|
nread += n;
|
|
|
|
|
|
|
|
for (; n && *p != '\n'; n--, p++)
|
|
|
|
;
|
|
|
|
if (n)
|
|
|
|
{
|
|
|
|
break; /* at least one full line available - that's enough.
|
|
|
|
This function is just a temporary hack until we use
|
|
|
|
the assuna lib in gpg. So it is okay to forget
|
|
|
|
about pending bytes */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nread;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#if !defined (__riscos__)
|
|
|
|
|
2003-08-29 00:14:42 +00:00
|
|
|
#if !defined (_WIN32)
|
2002-06-29 13:46:34 +00:00
|
|
|
/* For the new Assuan protocol we may have to send options */
|
|
|
|
static int
|
|
|
|
agent_send_option (int fd, const char *name, const char *value)
|
|
|
|
{
|
|
|
|
char buf[200];
|
|
|
|
int nread;
|
|
|
|
char *line;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
line = m_alloc (7 + strlen (name) + 1 + strlen (value) + 2);
|
|
|
|
strcpy (stpcpy (stpcpy (stpcpy (
|
|
|
|
stpcpy (line, "OPTION "), name), "="), value), "\n");
|
|
|
|
i = writen (fd, line, strlen (line));
|
|
|
|
m_free (line);
|
|
|
|
if (i)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* get response */
|
|
|
|
nread = readline (fd, buf, DIM(buf)-1);
|
|
|
|
if (nread < 3)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (buf[0] == 'O' && buf[1] == 'K' && (buf[2] == ' ' || buf[2] == '\n'))
|
|
|
|
return 0; /* okay */
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
agent_send_all_options (int fd)
|
|
|
|
{
|
|
|
|
char *dft_display = NULL;
|
2002-11-13 13:23:03 +00:00
|
|
|
const char *dft_ttyname = NULL;
|
2002-06-29 13:46:34 +00:00
|
|
|
char *dft_ttytype = NULL;
|
|
|
|
char *old_lc = NULL;
|
|
|
|
char *dft_lc = NULL;
|
|
|
|
int rc = 0;
|
|
|
|
|
|
|
|
dft_display = getenv ("DISPLAY");
|
|
|
|
if (opt.display || dft_display)
|
|
|
|
{
|
|
|
|
if (agent_send_option (fd, "display",
|
|
|
|
opt.display ? opt.display : dft_display))
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2002-11-09 17:48:41 +00:00
|
|
|
if (!opt.ttyname)
|
2003-08-29 00:14:42 +00:00
|
|
|
{
|
2004-10-26 07:50:47 +00:00
|
|
|
const char *tmp;
|
|
|
|
|
2003-08-29 00:14:42 +00:00
|
|
|
dft_ttyname = getenv ("GPG_TTY");
|
2004-10-26 07:50:47 +00:00
|
|
|
if ((!dft_ttyname || !*dft_ttyname) && (tmp=ttyname (0)))
|
|
|
|
dft_ttyname = tmp;
|
|
|
|
if ((!dft_ttyname || !*dft_ttyname) && (tmp=tty_get_ttyname ()))
|
|
|
|
dft_ttyname = tmp;
|
2003-08-29 00:14:42 +00:00
|
|
|
}
|
2002-06-29 13:46:34 +00:00
|
|
|
if (opt.ttyname || dft_ttyname)
|
|
|
|
{
|
|
|
|
if (agent_send_option (fd, "ttyname",
|
|
|
|
opt.ttyname ? opt.ttyname : dft_ttyname))
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
dft_ttytype = getenv ("TERM");
|
|
|
|
if (opt.ttytype || (dft_ttyname && dft_ttytype))
|
|
|
|
{
|
|
|
|
if (agent_send_option (fd, "ttytype",
|
|
|
|
opt.ttyname ? opt.ttytype : dft_ttytype))
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(HAVE_SETLOCALE) && defined(LC_CTYPE)
|
|
|
|
old_lc = setlocale (LC_CTYPE, NULL);
|
|
|
|
if (old_lc)
|
|
|
|
old_lc = m_strdup (old_lc);
|
|
|
|
dft_lc = setlocale (LC_CTYPE, "");
|
|
|
|
#endif
|
|
|
|
if (opt.lc_ctype || (dft_ttyname && dft_lc))
|
|
|
|
{
|
|
|
|
rc = agent_send_option (fd, "lc-ctype",
|
|
|
|
opt.lc_ctype ? opt.lc_ctype : dft_lc);
|
|
|
|
}
|
|
|
|
#if defined(HAVE_SETLOCALE) && defined(LC_CTYPE)
|
|
|
|
if (old_lc)
|
|
|
|
{
|
|
|
|
setlocale (LC_CTYPE, old_lc);
|
|
|
|
m_free (old_lc);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if (rc)
|
|
|
|
return rc;
|
|
|
|
|
|
|
|
#if defined(HAVE_SETLOCALE) && defined(LC_MESSAGES)
|
|
|
|
old_lc = setlocale (LC_MESSAGES, NULL);
|
|
|
|
if (old_lc)
|
|
|
|
old_lc = m_strdup (old_lc);
|
|
|
|
dft_lc = setlocale (LC_MESSAGES, "");
|
|
|
|
#endif
|
|
|
|
if (opt.lc_messages || (dft_ttyname && dft_lc))
|
|
|
|
{
|
|
|
|
rc = agent_send_option (fd, "lc-messages",
|
|
|
|
opt.lc_messages ? opt.lc_messages : dft_lc);
|
|
|
|
}
|
|
|
|
#if defined(HAVE_SETLOCALE) && defined(LC_MESSAGES)
|
|
|
|
if (old_lc)
|
|
|
|
{
|
|
|
|
setlocale (LC_MESSAGES, old_lc);
|
|
|
|
m_free (old_lc);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return rc;
|
|
|
|
}
|
2003-08-29 00:14:42 +00:00
|
|
|
#endif /*!_WIN32*/
|
2000-08-21 15:54:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Open a connection to the agent and send the magic string
|
|
|
|
* Returns: -1 on error or an filedescriptor for urther processing
|
|
|
|
*/
|
|
|
|
|
|
|
|
static int
|
2002-06-29 13:46:34 +00:00
|
|
|
agent_open (int *ret_prot)
|
2000-08-21 15:54:37 +00:00
|
|
|
{
|
2003-08-29 00:14:42 +00:00
|
|
|
#if defined (_WIN32)
|
2002-06-29 13:46:34 +00:00
|
|
|
int fd;
|
|
|
|
char *infostr, *p;
|
|
|
|
HANDLE h;
|
|
|
|
char pidstr[128];
|
|
|
|
|
|
|
|
*ret_prot = 0;
|
|
|
|
if ( !(infostr = read_w32_registry_string(NULL, "Software\\GNU\\GnuPG",
|
|
|
|
"agentPID"))
|
|
|
|
|| *infostr == '0') {
|
|
|
|
log_error( _("gpg-agent is not available in this session\n"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
free(infostr);
|
|
|
|
|
|
|
|
sprintf(pidstr, "%u", (unsigned int)GetCurrentProcessId());
|
|
|
|
if (write_w32_registry_string(NULL, "Software\\GNU\\GnuPG",
|
|
|
|
"agentCID", pidstr)) {
|
|
|
|
log_error( _("can't set client pid for the agent\n") );
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
h = OpenEvent(EVENT_ALL_ACCESS, FALSE, "gpg_agent");
|
|
|
|
SetEvent(h);
|
|
|
|
Sleep(50); /* some time for the server */
|
|
|
|
if ( !(p = read_w32_registry_string(NULL, "Software\\GNU\\GnuPG",
|
|
|
|
"agentReadFD")) ) {
|
|
|
|
log_error( _("can't get server read FD for the agent\n") );
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
read_fd = atol(p);
|
|
|
|
free(p);
|
|
|
|
if ( !(p = read_w32_registry_string(NULL, "Software\\GNU\\GnuPG",
|
|
|
|
"agentWriteFD")) ) {
|
|
|
|
log_error ( _("can't get server write FD for the agent\n") );
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
write_fd = atol(p);
|
|
|
|
free(p);
|
|
|
|
fd = 0;
|
|
|
|
|
|
|
|
if ( writen ( fd, "GPGA\0\0\0\x01", 8 ) ) {
|
|
|
|
fd = -1;
|
|
|
|
}
|
|
|
|
#else /* Posix */
|
|
|
|
|
2000-08-21 15:54:37 +00:00
|
|
|
int fd;
|
|
|
|
char *infostr, *p;
|
|
|
|
struct sockaddr_un client_addr;
|
|
|
|
size_t len;
|
2002-06-29 13:46:34 +00:00
|
|
|
int prot;
|
|
|
|
|
|
|
|
if (opt.gpg_agent_info)
|
|
|
|
infostr = m_strdup (opt.gpg_agent_info);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
infostr = getenv ( "GPG_AGENT_INFO" );
|
|
|
|
if ( !infostr ) {
|
|
|
|
log_error (_("gpg-agent is not available in this session\n"));
|
|
|
|
opt.use_agent = 0;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
infostr = m_strdup ( infostr );
|
|
|
|
}
|
2000-08-21 15:54:37 +00:00
|
|
|
|
|
|
|
if ( !(p = strchr ( infostr, ':')) || p == infostr
|
|
|
|
|| (p-infostr)+1 >= sizeof client_addr.sun_path ) {
|
2002-06-29 13:46:34 +00:00
|
|
|
log_error( _("malformed GPG_AGENT_INFO environment variable\n"));
|
|
|
|
m_free (infostr );
|
|
|
|
opt.use_agent = 0;
|
2000-08-21 15:54:37 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2002-06-29 13:46:34 +00:00
|
|
|
*p++ = 0;
|
|
|
|
/* See whether this is the new gpg-agent using the Assuna protocl.
|
|
|
|
This agent identifies itself by have an info string with a
|
|
|
|
version number in the 3rd field. */
|
|
|
|
while (*p && *p != ':')
|
|
|
|
p++;
|
|
|
|
prot = *p? atoi (p+1) : 0;
|
|
|
|
if ( prot < 0 || prot > 1) {
|
|
|
|
log_error (_("gpg-agent protocol version %d is not supported\n"),prot);
|
|
|
|
m_free (infostr );
|
|
|
|
opt.use_agent = 0;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
*ret_prot = prot;
|
|
|
|
|
2000-08-21 15:54:37 +00:00
|
|
|
if( (fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1 ) {
|
|
|
|
log_error ("can't create socket: %s\n", strerror(errno) );
|
2002-06-29 13:46:34 +00:00
|
|
|
m_free (infostr );
|
|
|
|
opt.use_agent = 0;
|
2000-08-21 15:54:37 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2002-06-29 13:46:34 +00:00
|
|
|
|
2000-08-21 15:54:37 +00:00
|
|
|
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) );
|
2002-06-29 13:46:34 +00:00
|
|
|
m_free (infostr );
|
2000-08-21 15:54:37 +00:00
|
|
|
close (fd );
|
2002-06-29 13:46:34 +00:00
|
|
|
opt.use_agent = 0;
|
2000-08-21 15:54:37 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2002-06-29 13:46:34 +00:00
|
|
|
m_free (infostr);
|
2000-08-21 15:54:37 +00:00
|
|
|
|
2002-06-29 13:46:34 +00:00
|
|
|
if (!prot) {
|
|
|
|
if ( writen ( fd, "GPGA\0\0\0\x01", 8 ) ) {
|
|
|
|
close (fd);
|
|
|
|
fd = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else { /* assuan based gpg-agent */
|
|
|
|
char line[200];
|
|
|
|
int nread;
|
|
|
|
|
|
|
|
nread = readline (fd, line, DIM(line));
|
|
|
|
if (nread < 3 || !(line[0] == 'O' && line[1] == 'K'
|
|
|
|
&& (line[2] == '\n' || line[2] == ' ')) ) {
|
|
|
|
log_error ( _("communication problem with gpg-agent\n"));
|
|
|
|
close (fd );
|
|
|
|
opt.use_agent = 0;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (agent_send_all_options (fd)) {
|
|
|
|
log_error (_("problem with the agent - disabling agent use\n"));
|
2000-08-21 15:54:37 +00:00
|
|
|
close (fd);
|
2002-06-29 13:46:34 +00:00
|
|
|
opt.use_agent = 0;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2000-08-21 15:54:37 +00:00
|
|
|
}
|
2002-06-29 13:46:34 +00:00
|
|
|
#endif
|
|
|
|
|
2000-08-21 15:54:37 +00:00
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
2002-06-29 13:46:34 +00:00
|
|
|
|
2000-08-21 15:54:37 +00:00
|
|
|
static void
|
|
|
|
agent_close ( int fd )
|
|
|
|
{
|
2003-08-29 00:14:42 +00:00
|
|
|
#if defined (_WIN32)
|
2002-06-29 13:46:34 +00:00
|
|
|
HANDLE h = OpenEvent(EVENT_ALL_ACCESS, FALSE, "gpg_agent");
|
|
|
|
ResetEvent(h);
|
|
|
|
#else
|
2000-08-21 15:54:37 +00:00
|
|
|
close (fd);
|
2002-06-29 13:46:34 +00:00
|
|
|
#endif
|
2000-08-21 15:54:37 +00:00
|
|
|
}
|
2002-06-29 13:46:34 +00:00
|
|
|
#endif /* !__riscos__ */
|
2000-08-21 15:54:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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"
|
2003-04-10 09:56:47 +00:00
|
|
|
*
|
|
|
|
* Note that TRYAGAIN_TEXT must not be translated. If canceled is not
|
|
|
|
* NULL, the function does set it to 1 if the user canceled the
|
|
|
|
* operation.
|
2000-08-21 15:54:37 +00:00
|
|
|
*/
|
|
|
|
static char *
|
2003-04-10 09:56:47 +00:00
|
|
|
agent_get_passphrase ( u32 *keyid, int mode, const char *tryagain_text,
|
2004-12-09 16:57:30 +00:00
|
|
|
const char *custom_description,
|
|
|
|
const char *custom_prompt, int *canceled)
|
2000-08-21 15:54:37 +00:00
|
|
|
{
|
2002-06-29 13:46:34 +00:00
|
|
|
#if defined(__riscos__)
|
|
|
|
return NULL;
|
|
|
|
#else
|
|
|
|
size_t n;
|
2003-04-10 09:56:47 +00:00
|
|
|
char *atext = NULL;
|
2002-06-29 13:46:34 +00:00
|
|
|
char buf[50];
|
|
|
|
int fd = -1;
|
|
|
|
u32 reply;
|
|
|
|
char *pw = NULL;
|
|
|
|
PKT_public_key *pk = m_alloc_clear( sizeof *pk );
|
|
|
|
byte fpr[MAX_FINGERPRINT_LEN];
|
2003-04-10 09:56:47 +00:00
|
|
|
int have_fpr = 0;
|
2002-06-29 13:46:34 +00:00
|
|
|
int prot;
|
2003-04-10 09:56:47 +00:00
|
|
|
char *orig_codeset = NULL;
|
|
|
|
|
|
|
|
if (canceled)
|
|
|
|
*canceled = 0;
|
2000-08-21 15:54:37 +00:00
|
|
|
|
|
|
|
#if MAX_FINGERPRINT_LEN < 20
|
2002-06-29 13:46:34 +00:00
|
|
|
#error agent needs a 20 byte fingerprint
|
2000-08-21 15:54:37 +00:00
|
|
|
#endif
|
|
|
|
|
2002-06-29 13:46:34 +00:00
|
|
|
memset (fpr, 0, MAX_FINGERPRINT_LEN );
|
|
|
|
if( keyid && get_pubkey( pk, keyid ) )
|
2003-03-11 22:12:20 +00:00
|
|
|
{
|
|
|
|
free_public_key( pk );
|
|
|
|
pk = NULL; /* oops: no key for some reason */
|
|
|
|
}
|
2002-06-29 13:46:34 +00:00
|
|
|
|
2003-04-10 09:56:47 +00:00
|
|
|
#ifdef ENABLE_NLS
|
2004-12-09 16:57:30 +00:00
|
|
|
/* The Assuan agent protol requires us to transmit utf-8 strings */
|
2003-04-10 09:56:47 +00:00
|
|
|
orig_codeset = bind_textdomain_codeset (PACKAGE, NULL);
|
|
|
|
#ifdef HAVE_LANGINFO_CODESET
|
|
|
|
if (!orig_codeset)
|
|
|
|
orig_codeset = nl_langinfo (CODESET);
|
|
|
|
#endif
|
|
|
|
if (orig_codeset)
|
|
|
|
{ /* We only switch when we are able to restore the codeset later. */
|
|
|
|
orig_codeset = m_strdup (orig_codeset);
|
|
|
|
if (!bind_textdomain_codeset (PACKAGE, "utf-8"))
|
|
|
|
orig_codeset = NULL;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if ( (fd = agent_open (&prot)) == -1 )
|
|
|
|
goto failure;
|
|
|
|
|
2004-12-09 16:57:30 +00:00
|
|
|
if (custom_description)
|
|
|
|
atext = native_to_utf8 (custom_description);
|
|
|
|
else if ( !mode && pk && keyid )
|
2002-06-29 13:46:34 +00:00
|
|
|
{
|
|
|
|
char *uid;
|
|
|
|
size_t uidlen;
|
|
|
|
const char *algo_name = pubkey_algo_to_string ( pk->pubkey_algo );
|
|
|
|
const char *timestr;
|
|
|
|
char *maink;
|
|
|
|
|
|
|
|
if ( !algo_name )
|
|
|
|
algo_name = "?";
|
2004-10-05 14:33:02 +00:00
|
|
|
|
|
|
|
#define KEYIDSTRING _(" (main key ID %s)")
|
|
|
|
|
|
|
|
maink = m_alloc ( strlen (KEYIDSTRING) + keystrlen() + 20 );
|
2002-06-29 13:46:34 +00:00
|
|
|
if( keyid[2] && keyid[3] && keyid[0] != keyid[2]
|
|
|
|
&& keyid[1] != keyid[3] )
|
2004-10-05 14:33:02 +00:00
|
|
|
sprintf( maink, KEYIDSTRING, keystr(&keyid[2]) );
|
2002-06-29 13:46:34 +00:00
|
|
|
else
|
|
|
|
*maink = 0;
|
|
|
|
|
2002-08-19 08:28:00 +00:00
|
|
|
uid = get_user_id ( keyid, &uidlen );
|
2002-06-29 13:46:34 +00:00
|
|
|
timestr = strtimestamp (pk->timestamp);
|
2004-10-05 14:33:02 +00:00
|
|
|
|
|
|
|
#undef KEYIDSTRING
|
|
|
|
|
|
|
|
#define PROMPTSTRING _("You need a passphrase to unlock the secret" \
|
|
|
|
" key for user:\n" \
|
|
|
|
"\"%.*s\"\n" \
|
|
|
|
"%u-bit %s key, ID %s, created %s%s\n" )
|
|
|
|
|
|
|
|
atext = m_alloc ( 100 + strlen (PROMPTSTRING)
|
|
|
|
+ uidlen + 15 + strlen(algo_name) + keystrlen()
|
2002-06-29 13:46:34 +00:00
|
|
|
+ strlen (timestr) + strlen (maink) );
|
2004-10-05 14:33:02 +00:00
|
|
|
sprintf (atext, PROMPTSTRING,
|
2004-10-17 13:28:37 +00:00
|
|
|
(int)uidlen, uid,
|
2004-10-05 14:33:02 +00:00
|
|
|
nbits_from_pk (pk), algo_name, keystr(&keyid[0]), timestr,
|
2002-06-29 13:46:34 +00:00
|
|
|
maink );
|
|
|
|
m_free (uid);
|
|
|
|
m_free (maink);
|
2004-10-05 14:33:02 +00:00
|
|
|
|
|
|
|
#undef PROMPTSTRING
|
|
|
|
|
2002-06-29 13:46:34 +00:00
|
|
|
{
|
|
|
|
size_t dummy;
|
|
|
|
fingerprint_from_pk( pk, fpr, &dummy );
|
2003-04-10 09:56:47 +00:00
|
|
|
have_fpr = 1;
|
2002-06-29 13:46:34 +00:00
|
|
|
}
|
|
|
|
|
2000-08-21 15:54:37 +00:00
|
|
|
}
|
2003-03-11 22:12:20 +00:00
|
|
|
else if (mode == 2 )
|
2002-06-29 13:46:34 +00:00
|
|
|
atext = m_strdup ( _("Repeat passphrase\n") );
|
2003-03-11 22:12:20 +00:00
|
|
|
else
|
|
|
|
atext = m_strdup ( _("Enter passphrase\n") );
|
2002-06-29 13:46:34 +00:00
|
|
|
|
|
|
|
if (!prot)
|
|
|
|
{ /* old style protocol */
|
2004-12-17 18:51:32 +00:00
|
|
|
size_t nread;
|
|
|
|
|
2002-06-29 13:46:34 +00:00
|
|
|
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) ) )
|
2000-08-21 15:54:37 +00:00
|
|
|
goto failure;
|
2002-06-29 13:46:34 +00:00
|
|
|
m_free (atext); atext = NULL;
|
|
|
|
|
|
|
|
/* get response */
|
|
|
|
if ( readn ( fd, buf, 12, &nread ) )
|
2000-08-21 15:54:37 +00:00
|
|
|
goto failure;
|
2002-06-29 13:46:34 +00:00
|
|
|
|
|
|
|
if ( nread < 8 )
|
|
|
|
{
|
|
|
|
log_error ( "response from agent too short\n" );
|
|
|
|
goto failure;
|
2000-08-21 15:54:37 +00:00
|
|
|
}
|
2002-06-29 13:46:34 +00:00
|
|
|
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 ) )
|
2000-08-21 15:54:37 +00:00
|
|
|
goto failure;
|
2002-06-29 13:46:34 +00:00
|
|
|
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 );
|
2003-04-10 09:56:47 +00:00
|
|
|
#ifdef ENABLE_NLS
|
|
|
|
if (orig_codeset)
|
|
|
|
bind_textdomain_codeset (PACKAGE, orig_codeset);
|
|
|
|
#endif
|
|
|
|
m_free (orig_codeset);
|
2002-06-29 13:46:34 +00:00
|
|
|
return pw;
|
2000-08-21 15:54:37 +00:00
|
|
|
}
|
2002-06-29 13:46:34 +00:00
|
|
|
else if ( reply == GPGA_PROT_CANCELED )
|
2003-04-10 09:56:47 +00:00
|
|
|
{
|
|
|
|
log_info ( _("cancelled by user\n") );
|
|
|
|
if (canceled)
|
|
|
|
*canceled = 1;
|
|
|
|
}
|
2002-06-29 13:46:34 +00:00
|
|
|
else
|
2000-08-21 15:54:37 +00:00
|
|
|
log_error ( _("problem with the agent: agent returns 0x%lx\n"),
|
2002-06-29 13:46:34 +00:00
|
|
|
(ulong)reply );
|
2000-08-21 15:54:37 +00:00
|
|
|
}
|
2002-06-29 13:46:34 +00:00
|
|
|
else
|
|
|
|
{ /* The new Assuan protocol */
|
2004-12-17 18:51:32 +00:00
|
|
|
int nread;
|
2002-06-29 13:46:34 +00:00
|
|
|
char *line, *p;
|
2002-10-04 05:43:54 +00:00
|
|
|
const unsigned char *s;
|
2002-06-29 13:46:34 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
if (!tryagain_text)
|
|
|
|
tryagain_text = "X";
|
2003-04-10 09:56:47 +00:00
|
|
|
else
|
|
|
|
tryagain_text = _(tryagain_text);
|
2002-06-29 13:46:34 +00:00
|
|
|
|
|
|
|
/* We allocate 2 time the needed space for atext so that there
|
2004-12-09 16:57:30 +00:00
|
|
|
is enough space for escaping */
|
2002-06-29 13:46:34 +00:00
|
|
|
line = m_alloc (15 + 46
|
2004-12-09 16:57:30 +00:00
|
|
|
+ 3*strlen (tryagain_text)
|
|
|
|
+ 3*strlen (atext)
|
|
|
|
+ 3*strlen (custom_prompt? custom_prompt:"")
|
|
|
|
+ 2);
|
2002-06-29 13:46:34 +00:00
|
|
|
strcpy (line, "GET_PASSPHRASE ");
|
|
|
|
p = line+15;
|
2003-04-10 09:56:47 +00:00
|
|
|
if (!mode && have_fpr)
|
2002-06-29 13:46:34 +00:00
|
|
|
{
|
|
|
|
for (i=0; i < 20; i++, p +=2 )
|
|
|
|
sprintf (p, "%02X", fpr[i]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
*p++ = 'X'; /* no caching */
|
|
|
|
*p++ = ' ';
|
2002-10-04 05:43:54 +00:00
|
|
|
for (i=0, s=tryagain_text; *s; s++)
|
2002-06-29 13:46:34 +00:00
|
|
|
{
|
2002-10-04 05:43:54 +00:00
|
|
|
if (*s < ' ' || *s == '+')
|
2002-06-29 13:46:34 +00:00
|
|
|
{
|
2002-10-04 05:43:54 +00:00
|
|
|
sprintf (p, "%%%02X", *s);
|
2002-06-29 13:46:34 +00:00
|
|
|
p += 3;
|
|
|
|
}
|
2002-10-04 05:43:54 +00:00
|
|
|
else if (*s == ' ')
|
2002-06-29 13:46:34 +00:00
|
|
|
*p++ = '+';
|
|
|
|
else
|
2002-10-04 05:43:54 +00:00
|
|
|
*p++ = *s;
|
2002-06-29 13:46:34 +00:00
|
|
|
}
|
|
|
|
*p++ = ' ';
|
2004-12-09 16:57:30 +00:00
|
|
|
|
|
|
|
/* The prompt. */
|
|
|
|
if (custom_prompt)
|
|
|
|
{
|
|
|
|
char *tmp = native_to_utf8 (custom_prompt);
|
|
|
|
for (i=0, s=tmp; *s; s++)
|
|
|
|
{
|
|
|
|
if (*s < ' ' || *s == '+')
|
|
|
|
{
|
|
|
|
sprintf (p, "%%%02X", *s);
|
|
|
|
p += 3;
|
|
|
|
}
|
|
|
|
else if (*s == ' ')
|
|
|
|
*p++ = '+';
|
|
|
|
else
|
|
|
|
*p++ = *s;
|
|
|
|
}
|
|
|
|
xfree (tmp);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
*p++ = 'X'; /* Use the standard prompt */
|
|
|
|
|
2002-06-29 13:46:34 +00:00
|
|
|
*p++ = ' ';
|
|
|
|
/* copy description */
|
2002-10-04 05:43:54 +00:00
|
|
|
for (i=0, s= atext; *s; s++)
|
2002-06-29 13:46:34 +00:00
|
|
|
{
|
2002-10-04 05:43:54 +00:00
|
|
|
if (*s < ' ' || *s == '+')
|
2002-06-29 13:46:34 +00:00
|
|
|
{
|
2002-10-04 05:43:54 +00:00
|
|
|
sprintf (p, "%%%02X", *s);
|
2002-06-29 13:46:34 +00:00
|
|
|
p += 3;
|
|
|
|
}
|
2002-10-04 05:43:54 +00:00
|
|
|
else if (*s == ' ')
|
2002-06-29 13:46:34 +00:00
|
|
|
*p++ = '+';
|
|
|
|
else
|
2002-10-04 05:43:54 +00:00
|
|
|
*p++ = *s;
|
2002-06-29 13:46:34 +00:00
|
|
|
}
|
|
|
|
*p++ = '\n';
|
|
|
|
i = writen (fd, line, p - line);
|
|
|
|
m_free (line);
|
|
|
|
if (i)
|
|
|
|
goto failure;
|
|
|
|
m_free (atext); atext = NULL;
|
|
|
|
|
|
|
|
/* get response */
|
|
|
|
pw = m_alloc_secure (500);
|
|
|
|
nread = readline (fd, pw, 499);
|
|
|
|
if (nread < 3)
|
|
|
|
goto failure;
|
|
|
|
|
|
|
|
if (pw[0] == 'O' && pw[1] == 'K' && pw[2] == ' ')
|
|
|
|
{ /* we got a passphrase - convert it back from hex */
|
|
|
|
size_t pwlen = 0;
|
|
|
|
|
|
|
|
for (i=3; i < nread && hexdigitp (pw+i); i+=2)
|
|
|
|
pw[pwlen++] = xtoi_2 (pw+i);
|
|
|
|
pw[pwlen] = 0; /* make a C String */
|
|
|
|
agent_close (fd);
|
|
|
|
free_public_key( pk );
|
2003-04-10 09:56:47 +00:00
|
|
|
#ifdef ENABLE_NLS
|
|
|
|
if (orig_codeset)
|
|
|
|
bind_textdomain_codeset (PACKAGE, orig_codeset);
|
|
|
|
#endif
|
|
|
|
m_free (orig_codeset);
|
2002-06-29 13:46:34 +00:00
|
|
|
return pw;
|
|
|
|
}
|
2004-08-18 00:06:08 +00:00
|
|
|
else if (nread > 4 && !memcmp (pw, "ERR ", 4)
|
|
|
|
&& (0xffff & strtoul (&pw[4], NULL, 0)) == 99)
|
|
|
|
{
|
|
|
|
/* 99 is GPG_ERR_CANCELED. FIXME: Check tail and overflow,
|
|
|
|
and use gpg-error. */
|
2003-04-10 09:56:47 +00:00
|
|
|
log_info (_("cancelled by user\n") );
|
|
|
|
if (canceled)
|
|
|
|
*canceled = 1;
|
|
|
|
}
|
2002-06-29 13:46:34 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
log_error (_("problem with the agent - disabling agent use\n"));
|
|
|
|
opt.use_agent = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-08-21 15:54:37 +00:00
|
|
|
|
2002-06-29 13:46:34 +00:00
|
|
|
failure:
|
2003-04-10 09:56:47 +00:00
|
|
|
#ifdef ENABLE_NLS
|
|
|
|
if (orig_codeset)
|
|
|
|
bind_textdomain_codeset (PACKAGE, orig_codeset);
|
|
|
|
#endif
|
2002-06-29 13:46:34 +00:00
|
|
|
m_free (atext);
|
|
|
|
if ( fd != -1 )
|
|
|
|
agent_close (fd);
|
|
|
|
m_free (pw );
|
|
|
|
free_public_key( pk );
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
#endif /* Posix or W32 */
|
2000-08-21 15:54:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2002-06-29 13:46:34 +00:00
|
|
|
* Clear the cached passphrase
|
2000-08-21 15:54:37 +00:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
passphrase_clear_cache ( u32 *keyid, int algo )
|
|
|
|
{
|
2002-06-29 13:46:34 +00:00
|
|
|
#if defined(__riscos__)
|
|
|
|
return ;
|
|
|
|
#else
|
|
|
|
size_t n;
|
|
|
|
char buf[200];
|
|
|
|
int fd = -1;
|
|
|
|
size_t nread;
|
|
|
|
u32 reply;
|
|
|
|
PKT_public_key *pk;
|
|
|
|
byte fpr[MAX_FINGERPRINT_LEN];
|
|
|
|
int prot;
|
|
|
|
|
2000-08-21 15:54:37 +00:00
|
|
|
#if MAX_FINGERPRINT_LEN < 20
|
|
|
|
#error agent needs a 20 byte fingerprint
|
|
|
|
#endif
|
|
|
|
|
2002-06-29 13:46:34 +00:00
|
|
|
if (!opt.use_agent)
|
|
|
|
return;
|
|
|
|
|
|
|
|
pk = m_alloc_clear ( sizeof *pk );
|
|
|
|
memset (fpr, 0, MAX_FINGERPRINT_LEN );
|
|
|
|
if( !keyid || get_pubkey( pk, keyid ) )
|
2000-08-21 15:54:37 +00:00
|
|
|
{
|
2002-06-29 13:46:34 +00:00
|
|
|
log_debug ("oops, no key in passphrase_clear_cache\n");
|
|
|
|
goto failure; /* oops: no key for some reason */
|
2000-08-21 15:54:37 +00:00
|
|
|
}
|
2002-06-29 13:46:34 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
size_t dummy;
|
|
|
|
fingerprint_from_pk( pk, fpr, &dummy );
|
|
|
|
}
|
2000-08-21 15:54:37 +00:00
|
|
|
|
2002-06-29 13:46:34 +00:00
|
|
|
if ( (fd = agent_open (&prot)) == -1 )
|
|
|
|
goto failure;
|
|
|
|
|
|
|
|
if (!prot)
|
|
|
|
{
|
|
|
|
n = 4 + 20;
|
|
|
|
u32tobuf (buf, n );
|
|
|
|
u32tobuf (buf+4, GPGA_PROT_CLEAR_PASSPHRASE );
|
|
|
|
memcpy (buf+8, fpr, 20 );
|
|
|
|
if ( writen ( fd, buf, 28 ) )
|
2000-08-21 15:54:37 +00:00
|
|
|
goto failure;
|
2002-06-29 13:46:34 +00:00
|
|
|
|
|
|
|
/* get response */
|
|
|
|
if ( readn ( fd, buf, 8, &nread ) )
|
2000-08-21 15:54:37 +00:00
|
|
|
goto failure;
|
2002-06-29 13:46:34 +00:00
|
|
|
|
|
|
|
if ( nread < 8 ) {
|
|
|
|
log_error ( "response from agent too short\n" );
|
2000-08-21 15:54:37 +00:00
|
|
|
goto failure;
|
2002-06-29 13:46:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 );
|
|
|
|
}
|
2000-08-21 15:54:37 +00:00
|
|
|
}
|
2002-06-29 13:46:34 +00:00
|
|
|
else
|
|
|
|
{ /* The assuan protocol */
|
|
|
|
char *line, *p;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
line = m_alloc (17 + 40 + 2);
|
|
|
|
strcpy (line, "CLEAR_PASSPHRASE ");
|
|
|
|
p = line+17;
|
|
|
|
for (i=0; i < 20; i++, p +=2 )
|
|
|
|
sprintf (p, "%02X", fpr[i]);
|
|
|
|
*p++ = '\n';
|
|
|
|
i = writen (fd, line, p - line);
|
|
|
|
m_free (line);
|
|
|
|
if (i)
|
|
|
|
goto failure;
|
|
|
|
|
|
|
|
/* get response */
|
|
|
|
nread = readline (fd, buf, DIM(buf)-1);
|
|
|
|
if (nread < 3)
|
|
|
|
goto failure;
|
|
|
|
|
|
|
|
if (buf[0] == 'O' && buf[1] == 'K' && (buf[2] == ' ' || buf[2] == '\n'))
|
|
|
|
;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
log_error (_("problem with the agent - disabling agent use\n"));
|
|
|
|
opt.use_agent = 0;
|
|
|
|
}
|
2000-08-21 15:54:37 +00:00
|
|
|
}
|
|
|
|
|
2002-06-29 13:46:34 +00:00
|
|
|
failure:
|
|
|
|
if (fd != -1)
|
|
|
|
agent_close (fd);
|
|
|
|
free_public_key( pk );
|
|
|
|
#endif /* Posix or W32 */
|
2000-08-21 15:54:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-06-29 13:46:34 +00:00
|
|
|
|
|
|
|
|
1997-11-18 14:06:00 +00:00
|
|
|
/****************
|
2003-09-30 08:00:08 +00:00
|
|
|
* Ask for a passphrase and return that string.
|
1997-11-18 14:06:00 +00:00
|
|
|
*/
|
2003-09-30 08:00:08 +00:00
|
|
|
char *
|
2003-10-21 18:22:21 +00:00
|
|
|
ask_passphrase (const char *description,
|
2004-11-17 16:04:21 +00:00
|
|
|
const char *tryagain_text,
|
2003-10-21 18:22:21 +00:00
|
|
|
const char *promptid,
|
|
|
|
const char *prompt, int *canceled)
|
2003-09-30 08:00:08 +00:00
|
|
|
{
|
|
|
|
char *pw = NULL;
|
|
|
|
|
|
|
|
if (canceled)
|
|
|
|
*canceled = 0;
|
|
|
|
|
|
|
|
if (!opt.batch && description)
|
|
|
|
tty_printf ("\n%s\n",description);
|
|
|
|
|
|
|
|
agent_died:
|
|
|
|
if ( opt.use_agent )
|
|
|
|
{
|
2004-11-17 16:04:21 +00:00
|
|
|
pw = agent_get_passphrase (NULL, 0,
|
2004-12-09 16:57:30 +00:00
|
|
|
tryagain_text, description, prompt,
|
2004-11-17 16:04:21 +00:00
|
|
|
canceled );
|
2003-09-30 08:00:08 +00:00
|
|
|
if (!pw)
|
|
|
|
{
|
|
|
|
if (!opt.use_agent)
|
|
|
|
goto agent_died;
|
|
|
|
pw = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (fd_passwd)
|
|
|
|
{
|
|
|
|
pw = m_alloc_secure (strlen(fd_passwd)+1);
|
|
|
|
strcpy (pw, fd_passwd);
|
|
|
|
}
|
|
|
|
else if (opt.batch)
|
|
|
|
{
|
2004-10-28 22:09:09 +00:00
|
|
|
log_error(_("can't query passphrase in batch mode\n"));
|
2003-09-30 08:00:08 +00:00
|
|
|
pw = NULL;
|
|
|
|
}
|
|
|
|
else {
|
2004-11-17 16:04:21 +00:00
|
|
|
if (tryagain_text)
|
|
|
|
tty_printf(_("%s.\n"), tryagain_text);
|
2003-10-21 18:22:21 +00:00
|
|
|
pw = cpr_get_hidden(promptid? promptid : "passphrase.ask",
|
2003-09-30 08:00:08 +00:00
|
|
|
prompt?prompt : _("Enter passphrase: ") );
|
|
|
|
tty_kill_prompt();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!pw || !*pw)
|
|
|
|
write_status( STATUS_MISSING_PASSPHRASE );
|
|
|
|
|
|
|
|
return pw;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-06-16 09:09:31 +00:00
|
|
|
/* Return a new DEK object Using the string-to-key sepcifier S2K. Use
|
|
|
|
* KEYID and PUBKEY_ALGO to prompt the user.
|
2003-09-30 08:00:08 +00:00
|
|
|
|
2004-06-16 09:09:31 +00:00
|
|
|
MODE 0: Allow cached passphrase
|
|
|
|
1: Ignore cached passphrase
|
|
|
|
2: Ditto, but change the text to "repeat entry"
|
|
|
|
*/
|
1997-11-18 14:06:00 +00:00
|
|
|
DEK *
|
1999-07-01 10:53:35 +00:00
|
|
|
passphrase_to_dek( u32 *keyid, int pubkey_algo,
|
2002-06-29 13:46:34 +00:00
|
|
|
int cipher_algo, STRING2KEY *s2k, int mode,
|
2003-04-10 09:56:47 +00:00
|
|
|
const char *tryagain_text, int *canceled)
|
1997-11-18 14:06:00 +00:00
|
|
|
{
|
1998-05-26 13:38:00 +00:00
|
|
|
char *pw = NULL;
|
1997-11-18 14:06:00 +00:00
|
|
|
DEK *dek;
|
1998-05-04 18:49:26 +00:00
|
|
|
STRING2KEY help_s2k;
|
|
|
|
|
2003-04-10 09:56:47 +00:00
|
|
|
if (canceled)
|
|
|
|
*canceled = 0;
|
|
|
|
|
1998-05-04 18:49:26 +00:00
|
|
|
if( !s2k ) {
|
2002-06-29 13:46:34 +00:00
|
|
|
/* This is used for the old rfc1991 mode
|
|
|
|
* Note: This must match the code in encode.c with opt.rfc1991 set */
|
1998-05-04 18:49:26 +00:00
|
|
|
s2k = &help_s2k;
|
|
|
|
s2k->mode = 0;
|
* main.h: Create S2K_DIGEST_ALGO macro so we do not need to always set
opt.s2k_digest_algo. This helps fix a problem with PGP 2.x encrypted
symmetric messages. Change all callers (encode.c, g10.c, keyedit.c,
keygen.c, passphrase.c, sign.c).
* armor.c, cardglue.c, getkey.c, import.c, keygen.c: Be consistent in some
more quoted strings. Always use 'user ID', not 'user id', "quotes" for
user IDs, etc.
2004-09-24 20:34:38 +00:00
|
|
|
s2k->hash_algo = S2K_DIGEST_ALGO;
|
1998-05-04 18:49:26 +00:00
|
|
|
}
|
1997-11-18 14:06:00 +00:00
|
|
|
|
2004-06-16 09:09:31 +00:00
|
|
|
/* If we do not have a passphrase available in NEXT_PW and status
|
|
|
|
information are request, we print them now. */
|
1999-04-08 07:41:35 +00:00
|
|
|
if( !next_pw && is_status_enabled() ) {
|
1999-03-17 12:13:04 +00:00
|
|
|
char buf[50];
|
2004-06-16 09:09:31 +00:00
|
|
|
|
1999-04-08 07:41:35 +00:00
|
|
|
if( keyid ) {
|
2002-06-29 13:46:34 +00:00
|
|
|
u32 used_kid[2];
|
|
|
|
char *us;
|
|
|
|
|
|
|
|
if( keyid[2] && keyid[3] ) {
|
|
|
|
used_kid[0] = keyid[2];
|
|
|
|
used_kid[1] = keyid[3];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
used_kid[0] = keyid[0];
|
|
|
|
used_kid[1] = keyid[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
us = get_long_user_id_string( keyid );
|
|
|
|
write_status_text( STATUS_USERID_HINT, us );
|
|
|
|
m_free(us);
|
|
|
|
|
|
|
|
sprintf( buf, "%08lX%08lX %08lX%08lX %d 0",
|
|
|
|
(ulong)keyid[0], (ulong)keyid[1],
|
|
|
|
(ulong)used_kid[0], (ulong)used_kid[1],
|
|
|
|
pubkey_algo );
|
|
|
|
|
1999-04-08 07:41:35 +00: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 12:13:04 +00:00
|
|
|
}
|
|
|
|
|
2004-06-16 09:09:31 +00:00
|
|
|
/* If we do have a keyID, we do not have a passphrase available in
|
|
|
|
NEXT_PW, we are not running in batch mode and we do not want to
|
|
|
|
ignore the passphrase cache (mode!=1), print a prompt with
|
|
|
|
information on that key. */
|
2002-11-06 16:58:28 +00:00
|
|
|
if( keyid && !opt.batch && !next_pw && mode!=1 ) {
|
2002-06-29 13:46:34 +00:00
|
|
|
PKT_public_key *pk = m_alloc_clear( sizeof *pk );
|
1998-05-29 11:53:54 +00:00
|
|
|
char *p;
|
1997-12-20 17:23:29 +00:00
|
|
|
|
2004-10-06 21:50:23 +00:00
|
|
|
p=get_user_id_native(keyid);
|
|
|
|
tty_printf("\n");
|
|
|
|
tty_printf(_("You need a passphrase to unlock the secret key for\n"
|
|
|
|
"user: \"%s\"\n"),p);
|
2002-06-29 13:46:34 +00:00
|
|
|
m_free(p);
|
1998-05-29 11:53:54 +00:00
|
|
|
|
1998-06-29 12:30:57 +00:00
|
|
|
if( !get_pubkey( pk, keyid ) ) {
|
2002-06-29 13:46:34 +00:00
|
|
|
const char *s = pubkey_algo_to_string( pk->pubkey_algo );
|
2004-03-05 00:01:25 +00:00
|
|
|
tty_printf( _("%u-bit %s key, ID %s, created %s"),
|
|
|
|
nbits_from_pk( pk ), s?s:"?", keystr(keyid),
|
1998-06-29 12:30:57 +00:00
|
|
|
strtimestamp(pk->timestamp) );
|
1999-01-07 17:05:48 +00:00
|
|
|
if( keyid[2] && keyid[3] && keyid[0] != keyid[2]
|
|
|
|
&& keyid[1] != keyid[3] )
|
2004-03-05 00:01:25 +00:00
|
|
|
{
|
|
|
|
if(keystrlen()>10)
|
|
|
|
{
|
|
|
|
tty_printf("\n");
|
2004-03-05 13:34:56 +00:00
|
|
|
tty_printf(_(" (subkey on main key ID %s)"),
|
2004-03-05 00:01:25 +00:00
|
|
|
keystr(&keyid[2]) );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
tty_printf( _(" (main key ID %s)"), keystr(&keyid[2]) );
|
|
|
|
}
|
1999-01-07 17:05:48 +00:00
|
|
|
tty_printf("\n");
|
1998-05-29 11:53:54 +00:00
|
|
|
}
|
1999-01-07 17:05:48 +00:00
|
|
|
|
1998-05-29 11:53:54 +00:00
|
|
|
tty_printf("\n");
|
1998-06-29 12:30:57 +00:00
|
|
|
free_public_key( pk );
|
1997-11-18 14:06:00 +00:00
|
|
|
}
|
1998-08-08 19:27:00 +00:00
|
|
|
|
2002-06-29 13:46:34 +00:00
|
|
|
agent_died:
|
1998-05-26 13:38:00 +00:00
|
|
|
if( next_pw ) {
|
2004-11-17 16:04:21 +00:00
|
|
|
/* Simply return the passphrase we already have in NEXT_PW. */
|
1998-05-26 13:38:00 +00:00
|
|
|
pw = next_pw;
|
|
|
|
next_pw = NULL;
|
|
|
|
}
|
2000-08-21 15:54:37 +00:00
|
|
|
else if ( opt.use_agent ) {
|
2004-12-09 16:57:30 +00:00
|
|
|
/* Divert to the gpg-agent. */
|
2003-04-10 09:56:47 +00:00
|
|
|
pw = agent_get_passphrase ( keyid, mode == 2? 1: 0,
|
2004-12-09 16:57:30 +00:00
|
|
|
tryagain_text, NULL, NULL, canceled );
|
2002-06-29 13:46:34 +00:00
|
|
|
if (!pw)
|
|
|
|
{
|
|
|
|
if (!opt.use_agent)
|
|
|
|
goto agent_died;
|
|
|
|
pw = m_strdup ("");
|
|
|
|
}
|
2000-08-21 15:54:37 +00:00
|
|
|
if( *pw && mode == 2 ) {
|
2004-12-09 16:57:30 +00:00
|
|
|
char *pw2 = agent_get_passphrase ( keyid, 2, NULL, NULL,
|
|
|
|
NULL, canceled );
|
2002-06-29 13:46:34 +00:00
|
|
|
if (!pw2)
|
|
|
|
{
|
|
|
|
if (!opt.use_agent)
|
|
|
|
{
|
|
|
|
m_free (pw);
|
|
|
|
pw = NULL;
|
|
|
|
goto agent_died;
|
|
|
|
}
|
|
|
|
pw2 = m_strdup ("");
|
|
|
|
}
|
2000-08-21 15:54:37 +00:00
|
|
|
if( strcmp(pw, pw2) ) {
|
2002-06-29 13:46:34 +00:00
|
|
|
m_free(pw2);
|
|
|
|
m_free(pw);
|
2000-08-21 15:54:37 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2002-06-29 13:46:34 +00:00
|
|
|
m_free(pw2);
|
2000-08-21 15:54:37 +00:00
|
|
|
}
|
|
|
|
}
|
1998-09-28 19:25:31 +00:00
|
|
|
else if( fd_passwd ) {
|
2004-06-16 09:09:31 +00:00
|
|
|
/* Return the passphrase we have store in FD_PASSWD. */
|
2002-06-29 13:46:34 +00:00
|
|
|
pw = m_alloc_secure( strlen(fd_passwd)+1 );
|
1998-09-28 19:25:31 +00:00
|
|
|
strcpy( pw, fd_passwd );
|
1997-11-18 14:06:00 +00:00
|
|
|
}
|
2004-10-28 22:09:09 +00:00
|
|
|
else if( opt.batch )
|
|
|
|
{
|
|
|
|
log_error(_("can't query passphrase in batch mode\n"));
|
2002-06-29 13:46:34 +00:00
|
|
|
pw = m_strdup( "" ); /* return an empty passphrase */
|
2004-10-28 22:09:09 +00:00
|
|
|
}
|
1998-01-02 20:40:10 +00:00
|
|
|
else {
|
2004-06-16 09:09:31 +00:00
|
|
|
/* Read the passphrase from the tty or the command-fd. */
|
1998-11-27 11:42:49 +00:00
|
|
|
pw = cpr_get_hidden("passphrase.enter", _("Enter passphrase: ") );
|
1998-01-02 20:40:10 +00:00
|
|
|
tty_kill_prompt();
|
1998-08-08 19:27:00 +00:00
|
|
|
if( mode == 2 && !cpr_enabled() ) {
|
1998-11-27 11:42:49 +00:00
|
|
|
char *pw2 = cpr_get_hidden("passphrase.repeat",
|
1998-11-10 12:59:59 +00:00
|
|
|
_("Repeat passphrase: ") );
|
1998-05-04 18:49:26 +00:00
|
|
|
tty_kill_prompt();
|
|
|
|
if( strcmp(pw, pw2) ) {
|
2002-06-29 13:46:34 +00:00
|
|
|
m_free(pw2);
|
|
|
|
m_free(pw);
|
1998-05-04 18:49:26 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2002-06-29 13:46:34 +00:00
|
|
|
m_free(pw2);
|
1997-11-18 14:06:00 +00:00
|
|
|
}
|
|
|
|
}
|
1999-04-09 10:34:44 +00:00
|
|
|
|
|
|
|
if( !pw || !*pw )
|
|
|
|
write_status( STATUS_MISSING_PASSPHRASE );
|
|
|
|
|
2004-06-16 09:09:31 +00:00
|
|
|
/* Hash the passphrase and store it in a newly allocated DEK
|
|
|
|
object. Keep a copy of the passphrase in LAST_PW for use by
|
|
|
|
get_last_passphrase(). */
|
2002-06-29 13:46:34 +00:00
|
|
|
dek = m_alloc_secure_clear ( sizeof *dek );
|
1998-05-04 18:49:26 +00:00
|
|
|
dek->algo = cipher_algo;
|
|
|
|
if( !*pw && mode == 2 )
|
|
|
|
dek->keylen = 0;
|
1997-12-09 12:46:23 +00:00
|
|
|
else
|
1998-05-03 15:42:08 +00:00
|
|
|
hash_passphrase( dek, pw, s2k, mode==2 );
|
2002-06-29 13:46:34 +00:00
|
|
|
m_free(last_pw);
|
1998-05-26 13:38:00 +00:00
|
|
|
last_pw = pw;
|
1998-05-04 18:49:26 +00:00
|
|
|
return dek;
|
1997-11-18 14:06:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-05-03 15:42:08 +00:00
|
|
|
/****************
|
|
|
|
* Hash a passphrase using the supplied s2k. If create is true, create
|
1998-09-28 19:25:31 +00:00
|
|
|
* a new salt or what else must be filled into the s2k for a new key.
|
1998-05-03 15:42:08 +00: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 14:06:00 +00:00
|
|
|
{
|
2002-06-29 13:46:34 +00:00
|
|
|
MD_HANDLE md;
|
1998-09-28 19:25:31 +00:00
|
|
|
int pass, i;
|
|
|
|
int used = 0;
|
|
|
|
int pwlen = strlen(pw);
|
1997-11-18 14:06:00 +00:00
|
|
|
|
1998-05-03 15:42:08 +00:00
|
|
|
assert( s2k->hash_algo );
|
2002-06-29 13:46:34 +00:00
|
|
|
dek->keylen = cipher_get_keylen( dek->algo ) / 8;
|
1998-09-28 19:25:31 +00:00
|
|
|
if( !(dek->keylen > 0 && dek->keylen <= DIM(dek->key)) )
|
|
|
|
BUG();
|
|
|
|
|
2002-06-29 13:46:34 +00:00
|
|
|
md = md_open( s2k->hash_algo, 1);
|
1998-09-28 19:25:31 +00:00
|
|
|
for(pass=0; used < dek->keylen ; pass++ ) {
|
|
|
|
if( pass ) {
|
2002-09-10 08:40:12 +00:00
|
|
|
md_reset(md);
|
1998-09-28 19:25:31 +00:00
|
|
|
for(i=0; i < pass; i++ ) /* preset the hash context */
|
2002-06-29 13:46:34 +00:00
|
|
|
md_putc(md, 0 );
|
1998-09-28 19:25:31 +00:00
|
|
|
}
|
1998-08-05 18:30:53 +00:00
|
|
|
|
1998-09-28 19:25:31 +00:00
|
|
|
if( s2k->mode == 1 || s2k->mode == 3 ) {
|
|
|
|
int len2 = pwlen + 8;
|
|
|
|
ulong count = len2;
|
1998-08-05 18:30:53 +00:00
|
|
|
|
1998-09-28 19:25:31 +00:00
|
|
|
if( create && !pass ) {
|
2002-06-29 13:46:34 +00:00
|
|
|
randomize_buffer(s2k->salt, 8, 1);
|
1998-09-28 19:25:31 +00:00
|
|
|
if( s2k->mode == 3 )
|
1998-12-08 12:20:53 +00:00
|
|
|
s2k->count = 96; /* 65536 iterations */
|
1998-08-11 17:29:34 +00:00
|
|
|
}
|
1998-09-28 19:25:31 +00: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 */
|
2002-06-29 13:46:34 +00:00
|
|
|
md_write( md, s2k->salt, 8 );
|
|
|
|
md_write( md, pw, pwlen );
|
1998-09-28 19:25:31 +00:00
|
|
|
count -= len2;
|
|
|
|
}
|
|
|
|
if( count < 8 )
|
2002-06-29 13:46:34 +00:00
|
|
|
md_write( md, s2k->salt, count );
|
1998-08-05 18:30:53 +00:00
|
|
|
else {
|
2002-06-29 13:46:34 +00:00
|
|
|
md_write( md, s2k->salt, 8 );
|
1998-08-05 18:30:53 +00:00
|
|
|
count -= 8;
|
2002-06-29 13:46:34 +00:00
|
|
|
md_write( md, pw, count );
|
1998-08-05 18:30:53 +00:00
|
|
|
}
|
|
|
|
}
|
1998-09-28 19:25:31 +00:00
|
|
|
else
|
2002-06-29 13:46:34 +00:00
|
|
|
md_write( md, pw, pwlen );
|
|
|
|
md_final( md );
|
|
|
|
i = md_digest_length( s2k->hash_algo );
|
1998-09-28 19:25:31 +00:00
|
|
|
if( i > dek->keylen - used )
|
|
|
|
i = dek->keylen - used;
|
2002-06-29 13:46:34 +00:00
|
|
|
memcpy( dek->key+used, md_read(md, s2k->hash_algo), i );
|
1998-09-28 19:25:31 +00:00
|
|
|
used += i;
|
1998-04-04 20:16:55 +00:00
|
|
|
}
|
2002-06-29 13:46:34 +00:00
|
|
|
md_close(md);
|
1997-11-18 14:06:00 +00:00
|
|
|
}
|
|
|
|
|