1997-11-18 15:06:00 +01:00
|
|
|
/* passphrase.c - Get a passphrase
|
2005-06-01 21:13:05 +02:00
|
|
|
* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004,
|
|
|
|
* 2005 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
|
2005-05-31 10:39:18 +02:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
|
|
|
|
* USA.
|
1997-11-18 15:06:00 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
2002-06-29 15:46:34 +02:00
|
|
|
#include <stddef.h>
|
1997-11-18 15:06:00 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#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>
|
2002-06-29 15:46:34 +02:00
|
|
|
#if !defined(HAVE_DOSISH_SYSTEM) && !defined(__riscos__)
|
2000-08-21 17:54:37 +02:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/un.h>
|
2002-06-29 15:46:34 +02:00
|
|
|
#endif
|
2004-12-20 06:19:09 +01:00
|
|
|
#if defined (_WIN32)
|
|
|
|
#include <windows.h>
|
2002-06-29 15:46:34 +02:00
|
|
|
#endif
|
2000-08-21 17:54:37 +02:00
|
|
|
#include <errno.h>
|
2002-06-29 15:46:34 +02:00
|
|
|
#ifdef HAVE_LOCALE_H
|
|
|
|
#include <locale.h>
|
|
|
|
#endif
|
2003-04-10 11:56:47 +02:00
|
|
|
#ifdef HAVE_LANGINFO_CODESET
|
|
|
|
#include <langinfo.h>
|
|
|
|
#endif
|
2000-07-14 19:34:53 +02:00
|
|
|
|
|
|
|
#include "util.h"
|
2002-06-29 15:46:34 +02:00
|
|
|
#include "memory.h"
|
1998-01-02 21:40:10 +01:00
|
|
|
#include "options.h"
|
1997-11-18 15:06:00 +01:00
|
|
|
#include "ttyio.h"
|
2002-06-29 15:46:34 +02:00
|
|
|
#include "cipher.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"
|
2005-04-05 19:09:13 +02:00
|
|
|
#ifdef ENABLE_AGENT_SUPPORT
|
|
|
|
#include "assuan.h"
|
|
|
|
#endif /*ENABLE_AGENT_SUPPORT*/
|
2002-06-29 15:46:34 +02:00
|
|
|
|
2000-08-21 17:54:37 +02: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 15:46:34 +02: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))
|
|
|
|
|
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 )
|
|
|
|
{
|
2005-07-27 20:10:56 +02:00
|
|
|
xfree(next_pw);
|
1998-05-26 15:38:00 +02:00
|
|
|
next_pw = NULL;
|
|
|
|
if( s ) {
|
2005-07-27 20:10:56 +02:00
|
|
|
next_pw = 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;
|
2005-08-05 21:54:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Here's an interesting question: since this passphrase was passed in
|
|
|
|
on the command line, is there really any point in using secure
|
|
|
|
memory for it? I'm going with 'yes', since it doesn't hurt, and
|
|
|
|
might help in some small way (swapping). */
|
|
|
|
|
|
|
|
void
|
|
|
|
set_passphrase_from_string(const char *pass)
|
|
|
|
{
|
|
|
|
xfree( fd_passwd );
|
|
|
|
fd_passwd = xmalloc_secure(strlen(pass)+1);
|
|
|
|
strcpy(fd_passwd,pass);
|
1998-05-26 15:38:00 +02:00
|
|
|
}
|
|
|
|
|
1997-11-18 15:06:00 +01:00
|
|
|
|
1998-09-28 21:25:31 +02:00
|
|
|
void
|
|
|
|
read_passphrase_from_fd( int fd )
|
|
|
|
{
|
2003-04-10 11:56:47 +02: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 21:25:31 +02:00
|
|
|
|
2003-04-10 11:56:47 +02:00
|
|
|
while (!(read (fd, buf, 1) != 1 || *buf == '\n' ))
|
|
|
|
;
|
|
|
|
*buf = 0;
|
|
|
|
return;
|
|
|
|
}
|
2000-08-21 17:54:37 +02:00
|
|
|
|
2003-04-10 11:56:47 +02:00
|
|
|
if (!opt.batch )
|
1998-09-28 21:25:31 +02:00
|
|
|
tty_printf("Reading passphrase from file descriptor %d ...", fd );
|
2003-04-10 11:56:47 +02:00
|
|
|
for (pw = NULL, i = len = 100; ; i++ )
|
|
|
|
{
|
|
|
|
if (i >= len-1 )
|
|
|
|
{
|
|
|
|
char *pw2 = pw;
|
|
|
|
len += 100;
|
2005-07-27 20:10:56 +02:00
|
|
|
pw = xmalloc_secure( len );
|
2003-04-10 11:56:47 +02:00
|
|
|
if( pw2 )
|
2004-12-13 09:04:52 +01:00
|
|
|
{
|
|
|
|
memcpy(pw, pw2, i );
|
2005-07-27 20:10:56 +02:00
|
|
|
xfree (pw2);
|
2004-12-13 09:04:52 +01:00
|
|
|
}
|
2003-04-10 11:56:47 +02:00
|
|
|
else
|
|
|
|
i=0;
|
1998-09-28 21:25:31 +02:00
|
|
|
}
|
2003-04-10 11:56:47 +02:00
|
|
|
if (read( fd, pw+i, 1) != 1 || pw[i] == '\n' )
|
|
|
|
break;
|
1998-09-28 21:25:31 +02:00
|
|
|
}
|
2003-04-10 11:56:47 +02:00
|
|
|
pw[i] = 0;
|
|
|
|
if (!opt.batch)
|
|
|
|
tty_printf("\b\b\b \n" );
|
1998-09-28 21:25:31 +02:00
|
|
|
|
2005-07-27 20:10:56 +02:00
|
|
|
xfree( fd_passwd );
|
2003-04-10 11:56:47 +02:00
|
|
|
fd_passwd = pw;
|
1998-09-28 21:25:31 +02:00
|
|
|
}
|
|
|
|
|
2002-06-29 15:46:34 +02:00
|
|
|
|
|
|
|
|
2005-04-05 19:09:13 +02:00
|
|
|
#ifdef ENABLE_AGENT_SUPPORT
|
|
|
|
/* Send one option to the gpg-agent. */
|
2002-06-29 15:46:34 +02:00
|
|
|
static int
|
2005-04-05 19:09:13 +02:00
|
|
|
agent_send_option (assuan_context_t ctx, const char *name, const char *value)
|
2002-06-29 15:46:34 +02:00
|
|
|
{
|
|
|
|
char *line;
|
2005-04-05 19:09:13 +02:00
|
|
|
int rc;
|
2002-06-29 15:46:34 +02:00
|
|
|
|
2005-04-05 19:09:13 +02:00
|
|
|
if (!value || !*value)
|
|
|
|
return 0; /* Avoid sending empty option values. */
|
|
|
|
|
|
|
|
line = xmalloc (7 + strlen (name) + 1 + strlen (value) + 1);
|
|
|
|
strcpy (stpcpy (stpcpy (stpcpy (line, "OPTION "), name), "="), value);
|
|
|
|
rc = assuan_transact (ctx, line, NULL, NULL, NULL, NULL, NULL, NULL);
|
|
|
|
xfree (line);
|
|
|
|
return rc? -1 : 0;
|
2002-06-29 15:46:34 +02:00
|
|
|
}
|
|
|
|
|
2005-04-05 19:09:13 +02:00
|
|
|
/* Send all required options to the gpg-agent. */
|
2002-06-29 15:46:34 +02:00
|
|
|
static int
|
2005-04-05 19:09:13 +02:00
|
|
|
agent_send_all_options (assuan_context_t ctx)
|
2002-06-29 15:46:34 +02:00
|
|
|
{
|
|
|
|
char *dft_display = NULL;
|
2002-11-13 14:23:03 +01:00
|
|
|
const char *dft_ttyname = NULL;
|
2002-06-29 15:46:34 +02: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)
|
|
|
|
{
|
2005-04-05 19:09:13 +02:00
|
|
|
if (agent_send_option (ctx, "display",
|
2002-06-29 15:46:34 +02:00
|
|
|
opt.display ? opt.display : dft_display))
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2002-11-09 18:48:41 +01:00
|
|
|
if (!opt.ttyname)
|
2003-08-29 02:14:42 +02:00
|
|
|
{
|
2004-10-26 09:50:47 +02:00
|
|
|
const char *tmp;
|
|
|
|
|
2003-08-29 02:14:42 +02:00
|
|
|
dft_ttyname = getenv ("GPG_TTY");
|
2004-10-26 09:50:47 +02: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 02:14:42 +02:00
|
|
|
}
|
2002-06-29 15:46:34 +02:00
|
|
|
if (opt.ttyname || dft_ttyname)
|
|
|
|
{
|
2005-04-05 19:09:13 +02:00
|
|
|
if (agent_send_option (ctx, "ttyname",
|
2002-06-29 15:46:34 +02:00
|
|
|
opt.ttyname ? opt.ttyname : dft_ttyname))
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
dft_ttytype = getenv ("TERM");
|
|
|
|
if (opt.ttytype || (dft_ttyname && dft_ttytype))
|
|
|
|
{
|
2005-04-05 19:09:13 +02:00
|
|
|
if (agent_send_option (ctx, "ttytype",
|
2002-06-29 15:46:34 +02:00
|
|
|
opt.ttyname ? opt.ttytype : dft_ttytype))
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(HAVE_SETLOCALE) && defined(LC_CTYPE)
|
|
|
|
old_lc = setlocale (LC_CTYPE, NULL);
|
|
|
|
if (old_lc)
|
2005-07-27 20:10:56 +02:00
|
|
|
old_lc = xstrdup (old_lc);
|
2002-06-29 15:46:34 +02:00
|
|
|
dft_lc = setlocale (LC_CTYPE, "");
|
|
|
|
#endif
|
|
|
|
if (opt.lc_ctype || (dft_ttyname && dft_lc))
|
|
|
|
{
|
2005-04-05 19:09:13 +02:00
|
|
|
rc = agent_send_option (ctx, "lc-ctype",
|
2002-06-29 15:46:34 +02:00
|
|
|
opt.lc_ctype ? opt.lc_ctype : dft_lc);
|
|
|
|
}
|
|
|
|
#if defined(HAVE_SETLOCALE) && defined(LC_CTYPE)
|
|
|
|
if (old_lc)
|
|
|
|
{
|
|
|
|
setlocale (LC_CTYPE, old_lc);
|
2005-07-27 20:10:56 +02:00
|
|
|
xfree (old_lc);
|
2002-06-29 15:46:34 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if (rc)
|
|
|
|
return rc;
|
|
|
|
|
|
|
|
#if defined(HAVE_SETLOCALE) && defined(LC_MESSAGES)
|
|
|
|
old_lc = setlocale (LC_MESSAGES, NULL);
|
|
|
|
if (old_lc)
|
2005-07-27 20:10:56 +02:00
|
|
|
old_lc = xstrdup (old_lc);
|
2002-06-29 15:46:34 +02:00
|
|
|
dft_lc = setlocale (LC_MESSAGES, "");
|
|
|
|
#endif
|
|
|
|
if (opt.lc_messages || (dft_ttyname && dft_lc))
|
|
|
|
{
|
2005-04-05 19:09:13 +02:00
|
|
|
rc = agent_send_option (ctx, "lc-messages",
|
2002-06-29 15:46:34 +02:00
|
|
|
opt.lc_messages ? opt.lc_messages : dft_lc);
|
|
|
|
}
|
|
|
|
#if defined(HAVE_SETLOCALE) && defined(LC_MESSAGES)
|
|
|
|
if (old_lc)
|
|
|
|
{
|
|
|
|
setlocale (LC_MESSAGES, old_lc);
|
2005-07-27 20:10:56 +02:00
|
|
|
xfree (old_lc);
|
2002-06-29 15:46:34 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return rc;
|
|
|
|
}
|
2005-04-05 19:09:13 +02:00
|
|
|
#endif /*ENABLE_AGENT_SUPPORT*/
|
2000-08-21 17:54:37 +02:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
2005-04-05 19:09:13 +02:00
|
|
|
* Open a connection to the agent and initializes the connection.
|
2005-05-20 22:37:08 +02:00
|
|
|
* Returns: -1 on error; on success an Assuan context for that
|
|
|
|
* connection is returned. With TRY set to true, no error messages
|
|
|
|
* are printed and the use of the agent won't get disabled on failure.
|
2005-07-26 21:08:11 +02:00
|
|
|
* If ORIG_CODESET is not NULL, the function will swithc the codeset
|
|
|
|
* back to that one before printing error messages.
|
2000-08-21 17:54:37 +02:00
|
|
|
*/
|
2005-04-05 19:09:13 +02:00
|
|
|
#ifdef ENABLE_AGENT_SUPPORT
|
2005-05-20 22:37:08 +02:00
|
|
|
assuan_context_t
|
2005-07-26 21:08:11 +02:00
|
|
|
agent_open (int try, const char *orig_codeset)
|
2000-08-21 17:54:37 +02:00
|
|
|
{
|
2005-04-05 19:09:13 +02:00
|
|
|
int rc;
|
|
|
|
assuan_context_t ctx;
|
|
|
|
char *infostr, *p;
|
|
|
|
int prot;
|
|
|
|
int pid;
|
2002-06-29 15:46:34 +02:00
|
|
|
|
2005-04-05 19:09:13 +02:00
|
|
|
if (opt.gpg_agent_info)
|
|
|
|
infostr = xstrdup (opt.gpg_agent_info);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
infostr = getenv ( "GPG_AGENT_INFO" );
|
|
|
|
if (!infostr || !*infostr)
|
|
|
|
{
|
2005-05-20 22:37:08 +02:00
|
|
|
if (!try)
|
|
|
|
{
|
2005-07-26 21:08:11 +02:00
|
|
|
#ifdef ENABLE_NLS
|
|
|
|
if (orig_codeset)
|
|
|
|
bind_textdomain_codeset (PACKAGE, orig_codeset);
|
|
|
|
#endif /*ENABLE_NLS*/
|
2005-09-07 17:53:03 +02:00
|
|
|
log_info (_("gpg-agent is not available in this session\n"));
|
2005-05-20 22:37:08 +02:00
|
|
|
opt.use_agent = 0;
|
|
|
|
}
|
2005-04-05 19:09:13 +02:00
|
|
|
return NULL;
|
2002-06-29 15:46:34 +02:00
|
|
|
}
|
2005-04-05 19:09:13 +02:00
|
|
|
infostr = xstrdup ( infostr );
|
2000-08-21 17:54:37 +02:00
|
|
|
}
|
2005-04-05 19:09:13 +02:00
|
|
|
|
|
|
|
if ( !(p = strchr (infostr, PATHSEP_C)) || p == infostr)
|
|
|
|
{
|
2005-05-20 22:37:08 +02:00
|
|
|
if (!try)
|
|
|
|
{
|
2005-07-26 21:08:11 +02:00
|
|
|
#ifdef ENABLE_NLS
|
|
|
|
if (orig_codeset)
|
|
|
|
bind_textdomain_codeset (PACKAGE, orig_codeset);
|
|
|
|
#endif /*ENABLE_NLS*/
|
2005-05-20 22:37:08 +02:00
|
|
|
log_error ( _("malformed GPG_AGENT_INFO environment variable\n"));
|
|
|
|
opt.use_agent = 0;
|
|
|
|
}
|
2005-04-05 19:09:13 +02:00
|
|
|
xfree (infostr);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
*p++ = 0;
|
|
|
|
pid = atoi (p);
|
|
|
|
while (*p && *p != PATHSEP_C)
|
|
|
|
p++;
|
|
|
|
prot = *p? atoi (p+1) : 0;
|
|
|
|
if (prot != 1)
|
|
|
|
{
|
2005-05-20 22:37:08 +02:00
|
|
|
if (!try)
|
|
|
|
{
|
2005-07-26 21:08:11 +02:00
|
|
|
#ifdef ENABLE_NLS
|
|
|
|
if (orig_codeset)
|
|
|
|
bind_textdomain_codeset (PACKAGE, orig_codeset);
|
|
|
|
#endif /*ENABLE_NLS*/
|
2005-05-20 22:37:08 +02:00
|
|
|
log_error (_("gpg-agent protocol version %d is not supported\n"),
|
|
|
|
prot);
|
|
|
|
opt.use_agent = 0;
|
|
|
|
}
|
2005-04-05 19:09:13 +02:00
|
|
|
xfree (infostr);
|
|
|
|
return NULL;
|
2000-08-21 17:54:37 +02:00
|
|
|
}
|
2005-04-05 19:09:13 +02:00
|
|
|
|
|
|
|
rc = assuan_socket_connect (&ctx, infostr, pid);
|
|
|
|
if (rc)
|
|
|
|
{
|
2005-05-20 22:37:08 +02:00
|
|
|
if (!try)
|
|
|
|
{
|
2005-07-26 21:08:11 +02:00
|
|
|
#ifdef ENABLE_NLS
|
|
|
|
if (orig_codeset)
|
|
|
|
bind_textdomain_codeset (PACKAGE, orig_codeset);
|
|
|
|
#endif /*ENABLE_NLS*/
|
2005-05-20 22:37:08 +02:00
|
|
|
log_error ( _("can't connect to `%s': %s\n"),
|
|
|
|
infostr, assuan_strerror (rc));
|
|
|
|
opt.use_agent = 0;
|
|
|
|
}
|
2005-04-05 19:09:13 +02:00
|
|
|
xfree (infostr );
|
|
|
|
return NULL;
|
2002-06-29 15:46:34 +02:00
|
|
|
}
|
2005-04-05 19:09:13 +02:00
|
|
|
xfree (infostr);
|
2002-06-29 15:46:34 +02:00
|
|
|
|
2005-04-05 19:09:13 +02:00
|
|
|
if (agent_send_all_options (ctx))
|
|
|
|
{
|
2005-05-20 22:37:08 +02:00
|
|
|
if (!try)
|
|
|
|
{
|
2005-07-26 21:08:11 +02:00
|
|
|
#ifdef ENABLE_NLS
|
|
|
|
if (orig_codeset)
|
|
|
|
bind_textdomain_codeset (PACKAGE, orig_codeset);
|
|
|
|
#endif /*ENABLE_NLS*/
|
2005-05-20 22:37:08 +02:00
|
|
|
log_error (_("problem with the agent - disabling agent use\n"));
|
|
|
|
opt.use_agent = 0;
|
|
|
|
}
|
2005-04-05 19:09:13 +02:00
|
|
|
assuan_disconnect (ctx);
|
|
|
|
return NULL;
|
2000-08-21 17:54:37 +02:00
|
|
|
}
|
2002-06-29 15:46:34 +02:00
|
|
|
|
2005-04-05 19:09:13 +02:00
|
|
|
return ctx;
|
2000-08-21 17:54:37 +02:00
|
|
|
}
|
2005-04-05 19:09:13 +02:00
|
|
|
#endif/*ENABLE_AGENT_SUPPORT*/
|
2000-08-21 17:54:37 +02:00
|
|
|
|
2002-06-29 15:46:34 +02:00
|
|
|
|
2005-04-05 19:09:13 +02:00
|
|
|
#ifdef ENABLE_AGENT_SUPPORT
|
2005-05-20 22:37:08 +02:00
|
|
|
void
|
2005-04-05 19:09:13 +02:00
|
|
|
agent_close (assuan_context_t ctx)
|
2000-08-21 17:54:37 +02:00
|
|
|
{
|
2005-04-05 19:09:13 +02:00
|
|
|
assuan_disconnect (ctx);
|
|
|
|
}
|
|
|
|
#endif /*ENABLE_AGENT_SUPPORT*/
|
|
|
|
|
|
|
|
|
|
|
|
/* Copy the text ATEXT into the buffer P and do plus '+' and percent
|
|
|
|
escaping. Note that the provided buffer needs to be 3 times the
|
|
|
|
size of ATEXT plus 1. Returns a pointer to the leading Nul in P. */
|
|
|
|
#ifdef ENABLE_AGENT_SUPPORT
|
|
|
|
static char *
|
|
|
|
percent_plus_escape (char *p, const char *atext)
|
|
|
|
{
|
|
|
|
const unsigned char *s;
|
|
|
|
|
|
|
|
for (s=atext; *s; s++)
|
|
|
|
{
|
|
|
|
if (*s < ' ' || *s == '+')
|
|
|
|
{
|
|
|
|
sprintf (p, "%%%02X", *s);
|
|
|
|
p += 3;
|
|
|
|
}
|
|
|
|
else if (*s == ' ')
|
|
|
|
*p++ = '+';
|
|
|
|
else
|
|
|
|
*p++ = *s;
|
|
|
|
}
|
|
|
|
*p = 0;
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
#endif /*ENABLE_AGENT_SUPPORT*/
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef ENABLE_AGENT_SUPPORT
|
|
|
|
|
|
|
|
/* Object for the agent_okay_cb function. */
|
|
|
|
struct agent_okay_cb_s {
|
|
|
|
char *pw;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* A callback used to get the passphrase from the okay line. See
|
|
|
|
agent-get_passphrase for details. LINE is the rest of the OK
|
|
|
|
status line without leading white spaces. */
|
|
|
|
static assuan_error_t
|
|
|
|
agent_okay_cb (void *opaque, const char *line)
|
|
|
|
{
|
|
|
|
struct agent_okay_cb_s *parm = opaque;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Note: If the malloc below fails we won't be able to wipe the
|
|
|
|
memory at LINE given the current implementation of the Assuan
|
|
|
|
code. There is no easy ay around this w/o adding a lot of more
|
|
|
|
memory function code to allow wiping arbitrary stuff on memory
|
|
|
|
failure. */
|
|
|
|
parm->pw = xmalloc_secure (strlen (line)/2+2);
|
|
|
|
|
|
|
|
for (i=0; hexdigitp (line) && hexdigitp (line+1); line += 2)
|
|
|
|
parm->pw[i++] = xtoi_2 (line);
|
|
|
|
parm->pw[i] = 0;
|
|
|
|
return 0;
|
2000-08-21 17:54:37 +02:00
|
|
|
}
|
2005-04-05 19:09:13 +02:00
|
|
|
#endif /*ENABLE_AGENT_SUPPORT*/
|
2000-08-21 17:54:37 +02: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 11:56:47 +02: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
|
2005-05-04 00:27:07 +02:00
|
|
|
* operation. If CACHEID is not NULL, it will be used as the cacheID
|
|
|
|
* for the gpg-agent; if is NULL and a key fingerprint can be
|
|
|
|
* computed, this will be used as the cacheid.
|
2000-08-21 17:54:37 +02:00
|
|
|
*/
|
|
|
|
static char *
|
2005-05-04 00:27:07 +02:00
|
|
|
agent_get_passphrase ( u32 *keyid, int mode, const char *cacheid,
|
|
|
|
const char *tryagain_text,
|
2004-12-09 17:57:30 +01:00
|
|
|
const char *custom_description,
|
|
|
|
const char *custom_prompt, int *canceled)
|
2000-08-21 17:54:37 +02:00
|
|
|
{
|
2005-04-05 19:09:13 +02:00
|
|
|
#ifdef ENABLE_AGENT_SUPPORT
|
2003-04-10 11:56:47 +02:00
|
|
|
char *atext = NULL;
|
2005-04-05 19:09:13 +02:00
|
|
|
assuan_context_t ctx = NULL;
|
2002-06-29 15:46:34 +02:00
|
|
|
char *pw = NULL;
|
2005-07-27 20:10:56 +02:00
|
|
|
PKT_public_key *pk = xmalloc_clear( sizeof *pk );
|
2002-06-29 15:46:34 +02:00
|
|
|
byte fpr[MAX_FINGERPRINT_LEN];
|
2003-04-10 11:56:47 +02:00
|
|
|
int have_fpr = 0;
|
|
|
|
char *orig_codeset = NULL;
|
|
|
|
|
|
|
|
if (canceled)
|
|
|
|
*canceled = 0;
|
2000-08-21 17:54:37 +02:00
|
|
|
|
|
|
|
#if MAX_FINGERPRINT_LEN < 20
|
2002-06-29 15:46:34 +02:00
|
|
|
#error agent needs a 20 byte fingerprint
|
2000-08-21 17:54:37 +02:00
|
|
|
#endif
|
|
|
|
|
2002-06-29 15:46:34 +02:00
|
|
|
memset (fpr, 0, MAX_FINGERPRINT_LEN );
|
|
|
|
if( keyid && get_pubkey( pk, keyid ) )
|
2003-03-11 23:12:20 +01:00
|
|
|
{
|
2005-02-15 12:02:32 +01:00
|
|
|
if (pk)
|
|
|
|
free_public_key( pk );
|
2003-03-11 23:12:20 +01:00
|
|
|
pk = NULL; /* oops: no key for some reason */
|
|
|
|
}
|
2002-06-29 15:46:34 +02:00
|
|
|
|
2003-04-10 11:56:47 +02:00
|
|
|
#ifdef ENABLE_NLS
|
2005-04-05 19:09:13 +02:00
|
|
|
/* The Assuan agent protocol requires us to transmit utf-8 strings */
|
2003-04-10 11:56:47 +02: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. */
|
2005-07-27 20:10:56 +02:00
|
|
|
orig_codeset = xstrdup (orig_codeset);
|
2003-04-10 11:56:47 +02:00
|
|
|
if (!bind_textdomain_codeset (PACKAGE, "utf-8"))
|
|
|
|
orig_codeset = NULL;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2005-07-26 21:08:11 +02:00
|
|
|
if ( !(ctx = agent_open (0, orig_codeset)) )
|
2003-04-10 11:56:47 +02:00
|
|
|
goto failure;
|
|
|
|
|
2004-12-09 17:57:30 +01:00
|
|
|
if (custom_description)
|
|
|
|
atext = native_to_utf8 (custom_description);
|
|
|
|
else if ( !mode && pk && keyid )
|
2002-06-29 15:46:34 +02: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 16:33:02 +02:00
|
|
|
|
|
|
|
#define KEYIDSTRING _(" (main key ID %s)")
|
|
|
|
|
2005-07-27 20:10:56 +02:00
|
|
|
maink = xmalloc ( strlen (KEYIDSTRING) + keystrlen() + 20 );
|
2002-06-29 15:46:34 +02:00
|
|
|
if( keyid[2] && keyid[3] && keyid[0] != keyid[2]
|
|
|
|
&& keyid[1] != keyid[3] )
|
2004-10-05 16:33:02 +02:00
|
|
|
sprintf( maink, KEYIDSTRING, keystr(&keyid[2]) );
|
2002-06-29 15:46:34 +02:00
|
|
|
else
|
|
|
|
*maink = 0;
|
|
|
|
|
2002-08-19 10:28:00 +02:00
|
|
|
uid = get_user_id ( keyid, &uidlen );
|
2002-06-29 15:46:34 +02:00
|
|
|
timestr = strtimestamp (pk->timestamp);
|
2004-10-05 16:33:02 +02: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" )
|
|
|
|
|
2005-07-27 20:10:56 +02:00
|
|
|
atext = xmalloc ( 100 + strlen (PROMPTSTRING)
|
2004-10-05 16:33:02 +02:00
|
|
|
+ uidlen + 15 + strlen(algo_name) + keystrlen()
|
2002-06-29 15:46:34 +02:00
|
|
|
+ strlen (timestr) + strlen (maink) );
|
2004-10-05 16:33:02 +02:00
|
|
|
sprintf (atext, PROMPTSTRING,
|
2004-10-17 15:28:37 +02:00
|
|
|
(int)uidlen, uid,
|
2004-10-05 16:33:02 +02:00
|
|
|
nbits_from_pk (pk), algo_name, keystr(&keyid[0]), timestr,
|
2002-06-29 15:46:34 +02:00
|
|
|
maink );
|
2005-07-27 20:10:56 +02:00
|
|
|
xfree (uid);
|
|
|
|
xfree (maink);
|
2004-10-05 16:33:02 +02:00
|
|
|
|
|
|
|
#undef PROMPTSTRING
|
|
|
|
|
2002-06-29 15:46:34 +02:00
|
|
|
{
|
|
|
|
size_t dummy;
|
|
|
|
fingerprint_from_pk( pk, fpr, &dummy );
|
2003-04-10 11:56:47 +02:00
|
|
|
have_fpr = 1;
|
2002-06-29 15:46:34 +02:00
|
|
|
}
|
|
|
|
|
2000-08-21 17:54:37 +02:00
|
|
|
}
|
2003-03-11 23:12:20 +01:00
|
|
|
else if (mode == 2 )
|
2005-07-27 20:10:56 +02:00
|
|
|
atext = xstrdup ( _("Repeat passphrase\n") );
|
2003-03-11 23:12:20 +01:00
|
|
|
else
|
2005-07-27 20:10:56 +02:00
|
|
|
atext = xstrdup ( _("Enter passphrase\n") );
|
2002-06-29 15:46:34 +02:00
|
|
|
|
2005-04-05 19:09:13 +02:00
|
|
|
{
|
2002-06-29 15:46:34 +02:00
|
|
|
char *line, *p;
|
2005-04-05 19:09:13 +02:00
|
|
|
int i, rc;
|
|
|
|
struct agent_okay_cb_s okay_cb_parm;
|
2002-06-29 15:46:34 +02:00
|
|
|
|
|
|
|
if (!tryagain_text)
|
|
|
|
tryagain_text = "X";
|
2003-04-10 11:56:47 +02:00
|
|
|
else
|
|
|
|
tryagain_text = _(tryagain_text);
|
2002-06-29 15:46:34 +02:00
|
|
|
|
2005-04-05 19:09:13 +02:00
|
|
|
/* We allocate 23 times the needed space for thye texts so that
|
|
|
|
there is enough space for escaping. */
|
|
|
|
line = xmalloc (15 + 46
|
2004-12-09 17:57:30 +01:00
|
|
|
+ 3*strlen (atext)
|
|
|
|
+ 3*strlen (custom_prompt? custom_prompt:"")
|
2005-05-04 00:27:07 +02:00
|
|
|
+ (cacheid? (3*strlen (cacheid)): 0)
|
2005-04-05 19:09:13 +02:00
|
|
|
+ 3*strlen (tryagain_text)
|
|
|
|
+ 1);
|
2002-06-29 15:46:34 +02:00
|
|
|
strcpy (line, "GET_PASSPHRASE ");
|
|
|
|
p = line+15;
|
2005-05-04 00:27:07 +02:00
|
|
|
if (!mode && cacheid)
|
|
|
|
{
|
|
|
|
p = percent_plus_escape (p, cacheid);
|
|
|
|
}
|
|
|
|
else if (!mode && have_fpr)
|
2002-06-29 15:46:34 +02:00
|
|
|
{
|
|
|
|
for (i=0; i < 20; i++, p +=2 )
|
|
|
|
sprintf (p, "%02X", fpr[i]);
|
|
|
|
}
|
|
|
|
else
|
2005-04-05 19:09:13 +02:00
|
|
|
*p++ = 'X'; /* No caching. */
|
2002-06-29 15:46:34 +02:00
|
|
|
*p++ = ' ';
|
2005-04-05 19:09:13 +02:00
|
|
|
|
|
|
|
p = percent_plus_escape (p, tryagain_text);
|
2002-06-29 15:46:34 +02:00
|
|
|
*p++ = ' ';
|
2004-12-09 17:57:30 +01:00
|
|
|
|
|
|
|
/* The prompt. */
|
|
|
|
if (custom_prompt)
|
|
|
|
{
|
|
|
|
char *tmp = native_to_utf8 (custom_prompt);
|
2005-04-05 19:09:13 +02:00
|
|
|
p = percent_plus_escape (p, tmp);
|
2004-12-09 17:57:30 +01:00
|
|
|
xfree (tmp);
|
|
|
|
}
|
|
|
|
else
|
2005-04-05 19:09:13 +02:00
|
|
|
*p++ = 'X'; /* Use the standard prompt. */
|
2002-06-29 15:46:34 +02:00
|
|
|
*p++ = ' ';
|
2005-04-05 19:09:13 +02:00
|
|
|
|
|
|
|
/* Copy description. */
|
|
|
|
percent_plus_escape (p, atext);
|
|
|
|
|
|
|
|
/* Call gpg-agent. */
|
|
|
|
memset (&okay_cb_parm, 0, sizeof okay_cb_parm);
|
|
|
|
rc = assuan_transact2 (ctx, line, NULL, NULL, NULL, NULL, NULL, NULL,
|
|
|
|
agent_okay_cb, &okay_cb_parm);
|
|
|
|
|
|
|
|
xfree (line);
|
|
|
|
xfree (atext); atext = NULL;
|
|
|
|
if (!rc)
|
2002-06-29 15:46:34 +02:00
|
|
|
{
|
2005-04-05 19:09:13 +02:00
|
|
|
assert (okay_cb_parm.pw);
|
|
|
|
pw = okay_cb_parm.pw;
|
|
|
|
agent_close (ctx);
|
2005-02-15 12:02:32 +01:00
|
|
|
if (pk)
|
|
|
|
free_public_key( pk );
|
2003-04-10 11:56:47 +02:00
|
|
|
#ifdef ENABLE_NLS
|
|
|
|
if (orig_codeset)
|
|
|
|
bind_textdomain_codeset (PACKAGE, orig_codeset);
|
|
|
|
#endif
|
2005-04-05 19:09:13 +02:00
|
|
|
xfree (orig_codeset);
|
2002-06-29 15:46:34 +02:00
|
|
|
return pw;
|
|
|
|
}
|
2005-04-05 19:09:13 +02:00
|
|
|
else if (rc && (rc & 0xffff) == 99)
|
2004-08-18 02:06:08 +02:00
|
|
|
{
|
2005-04-05 19:09:13 +02:00
|
|
|
/* 99 is GPG_ERR_CANCELED. */
|
2003-04-10 11:56:47 +02:00
|
|
|
log_info (_("cancelled by user\n") );
|
|
|
|
if (canceled)
|
|
|
|
*canceled = 1;
|
|
|
|
}
|
2002-06-29 15:46:34 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
log_error (_("problem with the agent - disabling agent use\n"));
|
|
|
|
opt.use_agent = 0;
|
|
|
|
}
|
2005-04-05 19:09:13 +02:00
|
|
|
}
|
2002-06-29 15:46:34 +02:00
|
|
|
|
2000-08-21 17:54:37 +02:00
|
|
|
|
2002-06-29 15:46:34 +02:00
|
|
|
failure:
|
2003-04-10 11:56:47 +02:00
|
|
|
#ifdef ENABLE_NLS
|
|
|
|
if (orig_codeset)
|
2005-07-26 21:08:11 +02:00
|
|
|
{
|
|
|
|
bind_textdomain_codeset (PACKAGE, orig_codeset);
|
|
|
|
xfree (orig_codeset);
|
|
|
|
}
|
2003-04-10 11:56:47 +02:00
|
|
|
#endif
|
2005-04-05 19:09:13 +02:00
|
|
|
xfree (atext);
|
|
|
|
agent_close (ctx);
|
|
|
|
xfree (pw );
|
2005-02-15 12:02:32 +01:00
|
|
|
if (pk)
|
|
|
|
free_public_key( pk );
|
2005-04-05 19:09:13 +02:00
|
|
|
|
|
|
|
#endif /*ENABLE_AGENT_SUPPORT*/
|
|
|
|
|
2002-06-29 15:46:34 +02:00
|
|
|
return NULL;
|
2000-08-21 17:54:37 +02:00
|
|
|
}
|
|
|
|
|
2005-04-05 19:09:13 +02:00
|
|
|
|
2000-08-21 17:54:37 +02:00
|
|
|
/*
|
2005-05-04 00:27:07 +02:00
|
|
|
* Clear the cached passphrase. If CACHEID is not NULL, it will be
|
|
|
|
* used instead of a cache ID derived from KEYID.
|
2000-08-21 17:54:37 +02:00
|
|
|
*/
|
|
|
|
void
|
2005-05-04 00:27:07 +02:00
|
|
|
passphrase_clear_cache ( u32 *keyid, const char *cacheid, int algo )
|
2000-08-21 17:54:37 +02:00
|
|
|
{
|
2005-04-05 19:09:13 +02:00
|
|
|
#ifdef ENABLE_AGENT_SUPPORT
|
|
|
|
assuan_context_t ctx = NULL;
|
2002-06-29 15:46:34 +02:00
|
|
|
PKT_public_key *pk;
|
|
|
|
byte fpr[MAX_FINGERPRINT_LEN];
|
|
|
|
|
2000-08-21 17:54:37 +02:00
|
|
|
#if MAX_FINGERPRINT_LEN < 20
|
|
|
|
#error agent needs a 20 byte fingerprint
|
|
|
|
#endif
|
|
|
|
|
2002-06-29 15:46:34 +02:00
|
|
|
if (!opt.use_agent)
|
|
|
|
return;
|
|
|
|
|
2005-05-04 00:27:07 +02:00
|
|
|
if (!cacheid)
|
2000-08-21 17:54:37 +02:00
|
|
|
{
|
2005-05-04 00:27:07 +02:00
|
|
|
pk = xcalloc (1, sizeof *pk);
|
|
|
|
memset (fpr, 0, MAX_FINGERPRINT_LEN );
|
|
|
|
if( !keyid || get_pubkey( pk, keyid ) )
|
|
|
|
{
|
|
|
|
goto failure; /* oops: no key for some reason */
|
|
|
|
}
|
2002-06-29 15:46:34 +02:00
|
|
|
|
2005-05-04 00:27:07 +02:00
|
|
|
{
|
|
|
|
size_t dummy;
|
|
|
|
fingerprint_from_pk( pk, fpr, &dummy );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
pk = NULL;
|
2000-08-21 17:54:37 +02:00
|
|
|
|
2005-07-26 21:08:11 +02:00
|
|
|
if ( !(ctx = agent_open (0, NULL)) )
|
2002-06-29 15:46:34 +02:00
|
|
|
goto failure;
|
|
|
|
|
2005-04-05 19:09:13 +02:00
|
|
|
{
|
2002-06-29 15:46:34 +02:00
|
|
|
char *line, *p;
|
2005-04-05 19:09:13 +02:00
|
|
|
int i, rc;
|
2002-06-29 15:46:34 +02:00
|
|
|
|
2005-05-04 00:27:07 +02:00
|
|
|
if (cacheid)
|
|
|
|
{
|
|
|
|
line = xmalloc (17 + 3*strlen (cacheid) + 2);
|
|
|
|
strcpy (line, "CLEAR_PASSPHRASE ");
|
|
|
|
p = line+17;
|
|
|
|
p = percent_plus_escape (p, cacheid);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
line = xmalloc (17 + 40 + 2);
|
|
|
|
strcpy (line, "CLEAR_PASSPHRASE ");
|
|
|
|
p = line+17;
|
|
|
|
for (i=0; i < 20; i++, p +=2 )
|
|
|
|
sprintf (p, "%02X", fpr[i]);
|
|
|
|
}
|
2005-04-05 19:09:13 +02:00
|
|
|
*p = 0;
|
|
|
|
|
|
|
|
rc = assuan_transact (ctx, line, NULL, NULL, NULL, NULL, NULL, NULL);
|
|
|
|
xfree (line);
|
|
|
|
if (rc)
|
2002-06-29 15:46:34 +02:00
|
|
|
{
|
|
|
|
log_error (_("problem with the agent - disabling agent use\n"));
|
|
|
|
opt.use_agent = 0;
|
|
|
|
}
|
2000-08-21 17:54:37 +02:00
|
|
|
}
|
|
|
|
|
2002-06-29 15:46:34 +02:00
|
|
|
failure:
|
2005-04-05 19:09:13 +02:00
|
|
|
agent_close (ctx);
|
2005-02-15 12:02:32 +01:00
|
|
|
if (pk)
|
|
|
|
free_public_key( pk );
|
2005-04-05 19:09:13 +02:00
|
|
|
#endif /*ENABLE_AGENT_SUPPORT*/
|
2000-08-21 17:54:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-11-18 15:06:00 +01:00
|
|
|
/****************
|
2003-09-30 10:00:08 +02:00
|
|
|
* Ask for a passphrase and return that string.
|
1997-11-18 15:06:00 +01:00
|
|
|
*/
|
2003-09-30 10:00:08 +02:00
|
|
|
char *
|
2003-10-21 20:22:21 +02:00
|
|
|
ask_passphrase (const char *description,
|
2004-11-17 17:04:21 +01:00
|
|
|
const char *tryagain_text,
|
2003-10-21 20:22:21 +02:00
|
|
|
const char *promptid,
|
2005-05-04 00:27:07 +02:00
|
|
|
const char *prompt,
|
|
|
|
const char *cacheid, int *canceled)
|
2003-09-30 10:00:08 +02:00
|
|
|
{
|
|
|
|
char *pw = NULL;
|
|
|
|
|
|
|
|
if (canceled)
|
|
|
|
*canceled = 0;
|
|
|
|
|
|
|
|
if (!opt.batch && description)
|
2005-05-24 14:39:42 +02:00
|
|
|
{
|
|
|
|
if (strchr (description, '%'))
|
|
|
|
{
|
|
|
|
char *tmp = unescape_percent_string (description);
|
|
|
|
tty_printf ("\n%s\n", tmp);
|
|
|
|
xfree (tmp);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
tty_printf ("\n%s\n",description);
|
|
|
|
}
|
2003-09-30 10:00:08 +02:00
|
|
|
|
|
|
|
agent_died:
|
|
|
|
if ( opt.use_agent )
|
|
|
|
{
|
2005-05-04 00:27:07 +02:00
|
|
|
pw = agent_get_passphrase (NULL, 0, cacheid,
|
2004-12-09 17:57:30 +01:00
|
|
|
tryagain_text, description, prompt,
|
2004-11-17 17:04:21 +01:00
|
|
|
canceled );
|
2003-09-30 10:00:08 +02:00
|
|
|
if (!pw)
|
|
|
|
{
|
|
|
|
if (!opt.use_agent)
|
|
|
|
goto agent_died;
|
|
|
|
pw = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (fd_passwd)
|
|
|
|
{
|
2005-07-27 20:10:56 +02:00
|
|
|
pw = xmalloc_secure (strlen(fd_passwd)+1);
|
2003-09-30 10:00:08 +02:00
|
|
|
strcpy (pw, fd_passwd);
|
|
|
|
}
|
|
|
|
else if (opt.batch)
|
|
|
|
{
|
2004-10-29 00:09:09 +02:00
|
|
|
log_error(_("can't query passphrase in batch mode\n"));
|
2003-09-30 10:00:08 +02:00
|
|
|
pw = NULL;
|
|
|
|
}
|
|
|
|
else {
|
2004-11-17 17:04:21 +01:00
|
|
|
if (tryagain_text)
|
|
|
|
tty_printf(_("%s.\n"), tryagain_text);
|
2003-10-21 20:22:21 +02:00
|
|
|
pw = cpr_get_hidden(promptid? promptid : "passphrase.ask",
|
2003-09-30 10:00:08 +02:00
|
|
|
prompt?prompt : _("Enter passphrase: ") );
|
|
|
|
tty_kill_prompt();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!pw || !*pw)
|
|
|
|
write_status( STATUS_MISSING_PASSPHRASE );
|
|
|
|
|
|
|
|
return pw;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-06-16 11:09:31 +02: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 10:00:08 +02:00
|
|
|
|
2004-06-16 11:09:31 +02:00
|
|
|
MODE 0: Allow cached passphrase
|
|
|
|
1: Ignore cached passphrase
|
|
|
|
2: Ditto, but change the text to "repeat entry"
|
|
|
|
*/
|
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,
|
2002-06-29 15:46:34 +02:00
|
|
|
int cipher_algo, STRING2KEY *s2k, int mode,
|
2003-04-10 11:56:47 +02:00
|
|
|
const char *tryagain_text, int *canceled)
|
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;
|
|
|
|
|
2003-04-10 11:56:47 +02:00
|
|
|
if (canceled)
|
|
|
|
*canceled = 0;
|
|
|
|
|
1998-05-04 20:49:26 +02:00
|
|
|
if( !s2k ) {
|
2002-06-29 15:46:34 +02: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 20:49:26 +02: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 22:34:38 +02:00
|
|
|
s2k->hash_algo = S2K_DIGEST_ALGO;
|
1998-05-04 20:49:26 +02:00
|
|
|
}
|
1997-11-18 15:06:00 +01:00
|
|
|
|
2004-06-16 11:09:31 +02:00
|
|
|
/* If we do not have a passphrase available in NEXT_PW and status
|
|
|
|
information are request, we print them now. */
|
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];
|
2004-06-16 11:09:31 +02:00
|
|
|
|
1999-04-08 09:41:35 +02:00
|
|
|
if( keyid ) {
|
2002-06-29 15:46:34 +02: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 );
|
2005-07-27 20:10:56 +02:00
|
|
|
xfree(us);
|
2002-06-29 15:46:34 +02:00
|
|
|
|
|
|
|
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 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
|
|
|
}
|
|
|
|
|
2004-06-16 11:09:31 +02: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 17:58:28 +01:00
|
|
|
if( keyid && !opt.batch && !next_pw && mode!=1 ) {
|
2005-07-27 20:10:56 +02:00
|
|
|
PKT_public_key *pk = xmalloc_clear( sizeof *pk );
|
1998-05-29 13:53:54 +02:00
|
|
|
char *p;
|
1997-12-20 18:23:29 +01:00
|
|
|
|
2004-10-06 23:50:23 +02: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);
|
2005-07-27 20:10:56 +02:00
|
|
|
xfree(p);
|
1998-05-29 13:53:54 +02:00
|
|
|
|
1998-06-29 14:30:57 +02:00
|
|
|
if( !get_pubkey( pk, keyid ) ) {
|
2002-06-29 15:46:34 +02:00
|
|
|
const char *s = pubkey_algo_to_string( pk->pubkey_algo );
|
2004-03-05 01:01:25 +01:00
|
|
|
tty_printf( _("%u-bit %s key, ID %s, created %s"),
|
|
|
|
nbits_from_pk( pk ), s?s:"?", keystr(keyid),
|
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] )
|
2004-03-05 01:01:25 +01:00
|
|
|
{
|
|
|
|
if(keystrlen()>10)
|
|
|
|
{
|
|
|
|
tty_printf("\n");
|
2004-03-05 14:34:56 +01:00
|
|
|
tty_printf(_(" (subkey on main key ID %s)"),
|
2004-03-05 01:01:25 +01:00
|
|
|
keystr(&keyid[2]) );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
tty_printf( _(" (main key ID %s)"), keystr(&keyid[2]) );
|
|
|
|
}
|
1999-01-07 18:05:48 +01:00
|
|
|
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");
|
2005-02-15 12:02:32 +01:00
|
|
|
if (pk)
|
|
|
|
free_public_key( pk );
|
1997-11-18 15:06:00 +01:00
|
|
|
}
|
1998-08-08 21:27:00 +02:00
|
|
|
|
2002-06-29 15:46:34 +02:00
|
|
|
agent_died:
|
1998-05-26 15:38:00 +02:00
|
|
|
if( next_pw ) {
|
2004-11-17 17:04:21 +01:00
|
|
|
/* Simply return the passphrase we already have in NEXT_PW. */
|
1998-05-26 15:38:00 +02:00
|
|
|
pw = next_pw;
|
|
|
|
next_pw = NULL;
|
|
|
|
}
|
2000-08-21 17:54:37 +02:00
|
|
|
else if ( opt.use_agent ) {
|
2004-12-09 17:57:30 +01:00
|
|
|
/* Divert to the gpg-agent. */
|
2005-05-04 00:27:07 +02:00
|
|
|
pw = agent_get_passphrase ( keyid, mode == 2? 1: 0, NULL,
|
2004-12-09 17:57:30 +01:00
|
|
|
tryagain_text, NULL, NULL, canceled );
|
2002-06-29 15:46:34 +02:00
|
|
|
if (!pw)
|
|
|
|
{
|
|
|
|
if (!opt.use_agent)
|
|
|
|
goto agent_died;
|
2005-07-27 20:10:56 +02:00
|
|
|
pw = xstrdup ("");
|
2002-06-29 15:46:34 +02:00
|
|
|
}
|
2000-08-21 17:54:37 +02:00
|
|
|
if( *pw && mode == 2 ) {
|
2005-05-04 00:27:07 +02:00
|
|
|
char *pw2 = agent_get_passphrase ( keyid, 2, NULL, NULL, NULL,
|
2004-12-09 17:57:30 +01:00
|
|
|
NULL, canceled );
|
2002-06-29 15:46:34 +02:00
|
|
|
if (!pw2)
|
|
|
|
{
|
|
|
|
if (!opt.use_agent)
|
|
|
|
{
|
2005-07-27 20:10:56 +02:00
|
|
|
xfree (pw);
|
2002-06-29 15:46:34 +02:00
|
|
|
pw = NULL;
|
|
|
|
goto agent_died;
|
|
|
|
}
|
2005-07-27 20:10:56 +02:00
|
|
|
pw2 = xstrdup ("");
|
2002-06-29 15:46:34 +02:00
|
|
|
}
|
2000-08-21 17:54:37 +02:00
|
|
|
if( strcmp(pw, pw2) ) {
|
2005-07-27 20:10:56 +02:00
|
|
|
xfree(pw2);
|
|
|
|
xfree(pw);
|
2000-08-21 17:54:37 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
2005-07-27 20:10:56 +02:00
|
|
|
xfree(pw2);
|
2000-08-21 17:54:37 +02:00
|
|
|
}
|
|
|
|
}
|
1998-09-28 21:25:31 +02:00
|
|
|
else if( fd_passwd ) {
|
2004-06-16 11:09:31 +02:00
|
|
|
/* Return the passphrase we have store in FD_PASSWD. */
|
2005-07-27 20:10:56 +02:00
|
|
|
pw = 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
|
|
|
}
|
2004-10-29 00:09:09 +02:00
|
|
|
else if( opt.batch )
|
|
|
|
{
|
|
|
|
log_error(_("can't query passphrase in batch mode\n"));
|
2005-07-27 20:10:56 +02:00
|
|
|
pw = xstrdup( "" ); /* return an empty passphrase */
|
2004-10-29 00:09:09 +02:00
|
|
|
}
|
1998-01-02 21:40:10 +01:00
|
|
|
else {
|
2004-06-16 11:09:31 +02:00
|
|
|
/* Read the passphrase from the tty or the command-fd. */
|
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) ) {
|
2005-07-27 20:10:56 +02:00
|
|
|
xfree(pw2);
|
|
|
|
xfree(pw);
|
1998-05-04 20:49:26 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
2005-07-27 20:10:56 +02:00
|
|
|
xfree(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 );
|
|
|
|
|
2004-06-16 11:09:31 +02: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(). */
|
2005-07-27 20:10:56 +02:00
|
|
|
dek = xmalloc_secure_clear ( 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 );
|
2005-07-27 20:10:56 +02:00
|
|
|
xfree(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
|
|
|
{
|
2002-06-29 15:46:34 +02:00
|
|
|
MD_HANDLE 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 );
|
2002-06-29 15:46:34 +02:00
|
|
|
dek->keylen = cipher_get_keylen( dek->algo ) / 8;
|
1998-09-28 21:25:31 +02:00
|
|
|
if( !(dek->keylen > 0 && dek->keylen <= DIM(dek->key)) )
|
|
|
|
BUG();
|
|
|
|
|
2002-06-29 15:46:34 +02:00
|
|
|
md = md_open( s2k->hash_algo, 1);
|
1998-09-28 21:25:31 +02:00
|
|
|
for(pass=0; used < dek->keylen ; pass++ ) {
|
|
|
|
if( pass ) {
|
2002-09-10 10:40:12 +02:00
|
|
|
md_reset(md);
|
1998-09-28 21:25:31 +02:00
|
|
|
for(i=0; i < pass; i++ ) /* preset the hash context */
|
2002-06-29 15:46:34 +02:00
|
|
|
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 ) {
|
2002-06-29 15:46:34 +02:00
|
|
|
randomize_buffer(s2k->salt, 8, 1);
|
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 */
|
2002-06-29 15:46:34 +02:00
|
|
|
md_write( md, s2k->salt, 8 );
|
|
|
|
md_write( md, pw, pwlen );
|
1998-09-28 21:25:31 +02:00
|
|
|
count -= len2;
|
|
|
|
}
|
|
|
|
if( count < 8 )
|
2002-06-29 15:46:34 +02:00
|
|
|
md_write( md, s2k->salt, count );
|
1998-08-05 20:30:53 +02:00
|
|
|
else {
|
2002-06-29 15:46:34 +02:00
|
|
|
md_write( md, s2k->salt, 8 );
|
1998-08-05 20:30:53 +02:00
|
|
|
count -= 8;
|
2002-06-29 15:46:34 +02:00
|
|
|
md_write( md, pw, count );
|
1998-08-05 20:30:53 +02:00
|
|
|
}
|
|
|
|
}
|
1998-09-28 21:25:31 +02:00
|
|
|
else
|
2002-06-29 15:46:34 +02:00
|
|
|
md_write( md, pw, pwlen );
|
|
|
|
md_final( md );
|
|
|
|
i = md_digest_length( s2k->hash_algo );
|
1998-09-28 21:25:31 +02:00
|
|
|
if( i > dek->keylen - used )
|
|
|
|
i = dek->keylen - used;
|
2002-06-29 15:46:34 +02:00
|
|
|
memcpy( dek->key+used, 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
|
|
|
}
|
2002-06-29 15:46:34 +02:00
|
|
|
md_close(md);
|
1997-11-18 15:06:00 +01:00
|
|
|
}
|
|
|
|
|