2003-06-05 09:14:21 +02:00
|
|
|
/* passphrase.c - Get a passphrase
|
2006-04-19 13:26:11 +02:00
|
|
|
* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004,
|
2011-03-10 18:39:34 +01:00
|
|
|
* 2005, 2006, 2007, 2009, 2011 Free Software Foundation, Inc.
|
2003-06-05 09:14:21 +02:00
|
|
|
*
|
|
|
|
* This file is part of GnuPG.
|
|
|
|
*
|
|
|
|
* GnuPG is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
2007-07-04 21:49:40 +02:00
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
2003-06-05 09:14:21 +02:00
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* GnuPG is distributed in the hope that it will be useful,
|
|
|
|
* 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
|
2016-11-05 12:02:19 +01:00
|
|
|
* along with this program; if not, see <https://www.gnu.org/licenses/>.
|
2003-06-05 09:14:21 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#ifdef HAVE_LOCALE_H
|
|
|
|
#include <locale.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_LANGINFO_CODESET
|
|
|
|
#include <langinfo.h>
|
|
|
|
#endif
|
|
|
|
|
2003-06-18 21:56:13 +02:00
|
|
|
#include "gpg.h"
|
2017-03-07 12:21:23 +01:00
|
|
|
#include "../common/util.h"
|
2003-06-05 09:14:21 +02:00
|
|
|
#include "options.h"
|
2017-03-07 12:21:23 +01:00
|
|
|
#include "../common/ttyio.h"
|
2003-06-05 09:14:21 +02:00
|
|
|
#include "keydb.h"
|
|
|
|
#include "main.h"
|
2017-03-07 12:21:23 +01:00
|
|
|
#include "../common/i18n.h"
|
|
|
|
#include "../common/status.h"
|
2006-10-04 18:45:04 +02:00
|
|
|
#include "call-agent.h"
|
2013-02-07 20:37:58 +01:00
|
|
|
#include "../common/shareddefs.h"
|
2003-06-05 09:14:21 +02:00
|
|
|
|
|
|
|
static char *fd_passwd = NULL;
|
|
|
|
static char *next_pw = NULL;
|
|
|
|
static char *last_pw = NULL;
|
|
|
|
|
2006-10-04 18:45:04 +02:00
|
|
|
|
2003-06-05 09:14:21 +02:00
|
|
|
int
|
|
|
|
have_static_passphrase()
|
|
|
|
{
|
2013-02-07 20:37:58 +01:00
|
|
|
return (!!fd_passwd
|
|
|
|
&& (opt.batch || opt.pinentry_mode == PINENTRY_MODE_LOOPBACK));
|
|
|
|
}
|
|
|
|
|
2019-01-26 23:10:38 +01:00
|
|
|
|
2013-02-07 20:37:58 +01:00
|
|
|
/* Return a static passphrase. The returned value is only valid as
|
|
|
|
long as no other passphrase related function is called. NULL may
|
|
|
|
be returned if no passphrase has been set; better use
|
|
|
|
have_static_passphrase first. */
|
|
|
|
const char *
|
|
|
|
get_static_passphrase (void)
|
|
|
|
{
|
|
|
|
return fd_passwd;
|
2003-06-05 09:14:21 +02:00
|
|
|
}
|
|
|
|
|
2013-02-07 20:37:58 +01:00
|
|
|
|
2003-06-05 09:14:21 +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 )
|
|
|
|
{
|
2006-10-04 18:45:04 +02:00
|
|
|
xfree(next_pw);
|
|
|
|
next_pw = NULL;
|
|
|
|
if ( s )
|
|
|
|
{
|
|
|
|
next_pw = xmalloc_secure( strlen(s)+1 );
|
|
|
|
strcpy (next_pw, s );
|
2003-06-05 09:14:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************
|
|
|
|
* 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()
|
|
|
|
{
|
2006-10-04 18:45:04 +02:00
|
|
|
char *p = last_pw;
|
|
|
|
last_pw = NULL;
|
|
|
|
return p;
|
2003-06-05 09:14:21 +02:00
|
|
|
}
|
|
|
|
|
2006-04-19 13:26:11 +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)
|
|
|
|
{
|
2006-10-04 19:22:24 +02:00
|
|
|
xfree (fd_passwd);
|
2006-04-19 13:26:11 +02:00
|
|
|
fd_passwd = xmalloc_secure(strlen(pass)+1);
|
2006-10-04 19:22:24 +02:00
|
|
|
strcpy (fd_passwd, pass);
|
2006-04-19 13:26:11 +02:00
|
|
|
}
|
|
|
|
|
2003-06-05 09:14:21 +02:00
|
|
|
|
|
|
|
void
|
|
|
|
read_passphrase_from_fd( int fd )
|
|
|
|
{
|
2006-10-04 19:22:24 +02:00
|
|
|
int i, len;
|
|
|
|
char *pw;
|
|
|
|
|
2017-02-08 13:49:41 +01:00
|
|
|
if (! gnupg_fd_valid (fd))
|
|
|
|
log_fatal ("passphrase-fd is invalid: %s\n", strerror (errno));
|
|
|
|
|
2013-02-07 20:37:58 +01:00
|
|
|
if ( !opt.batch && opt.pinentry_mode != PINENTRY_MODE_LOOPBACK)
|
2006-10-04 19:22:24 +02:00
|
|
|
{ /* 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];
|
|
|
|
|
|
|
|
while (!(read (fd, buf, 1) != 1 || *buf == '\n' ))
|
|
|
|
;
|
|
|
|
*buf = 0;
|
2011-02-02 15:48:54 +01:00
|
|
|
return;
|
2006-10-04 19:22:24 +02:00
|
|
|
}
|
|
|
|
|
2011-02-02 15:48:54 +01:00
|
|
|
for (pw = NULL, i = len = 100; ; i++ )
|
2006-10-04 19:22:24 +02:00
|
|
|
{
|
2011-02-02 15:48:54 +01:00
|
|
|
if (i >= len-1 )
|
2006-10-04 19:22:24 +02:00
|
|
|
{
|
|
|
|
char *pw2 = pw;
|
|
|
|
len += 100;
|
|
|
|
pw = xmalloc_secure( len );
|
|
|
|
if( pw2 )
|
|
|
|
{
|
|
|
|
memcpy(pw, pw2, i );
|
|
|
|
xfree (pw2);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
i=0;
|
|
|
|
}
|
|
|
|
if (read( fd, pw+i, 1) != 1 || pw[i] == '\n' )
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
pw[i] = 0;
|
2013-02-07 20:37:58 +01:00
|
|
|
if (!opt.batch && opt.pinentry_mode != PINENTRY_MODE_LOOPBACK)
|
2006-10-04 19:22:24 +02:00
|
|
|
tty_printf("\b\b\b \n" );
|
|
|
|
|
|
|
|
xfree ( fd_passwd );
|
|
|
|
fd_passwd = pw;
|
2003-06-05 09:14:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Ask the GPG Agent for the passphrase.
|
2016-08-08 18:45:29 +02:00
|
|
|
* If NOCACHE is set the symmetric passpharse caching will not be used.
|
2003-06-05 09:14:21 +02:00
|
|
|
*
|
2006-10-04 18:45:04 +02:00
|
|
|
* Note that TRYAGAIN_TEXT must not be translated. If CANCELED is not
|
2003-06-05 09:14:21 +02:00
|
|
|
* NULL, the function does set it to 1 if the user canceled the
|
2006-04-19 13:26:11 +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.
|
2003-06-05 09:14:21 +02:00
|
|
|
*/
|
|
|
|
static char *
|
2016-08-08 18:45:29 +02:00
|
|
|
passphrase_get (int nocache, const char *cacheid, int repeat,
|
2016-08-08 17:42:37 +02:00
|
|
|
const char *tryagain_text, int *canceled)
|
2003-06-05 09:14:21 +02:00
|
|
|
{
|
2006-10-04 18:45:04 +02:00
|
|
|
int rc;
|
2003-06-05 09:14:21 +02:00
|
|
|
char *pw = NULL;
|
2007-10-19 17:58:38 +02:00
|
|
|
char *orig_codeset;
|
2006-10-04 18:45:04 +02:00
|
|
|
const char *my_cacheid;
|
2003-06-05 09:14:21 +02:00
|
|
|
|
|
|
|
if (canceled)
|
|
|
|
*canceled = 0;
|
|
|
|
|
2007-10-19 17:58:38 +02:00
|
|
|
orig_codeset = i18n_switchto_utf8 ();
|
2003-06-05 09:14:21 +02:00
|
|
|
|
2016-08-08 18:45:29 +02:00
|
|
|
if (!nocache && cacheid)
|
2006-10-04 18:45:04 +02:00
|
|
|
my_cacheid = cacheid;
|
|
|
|
else
|
|
|
|
my_cacheid = NULL;
|
2006-04-19 13:26:11 +02:00
|
|
|
|
2006-10-04 18:45:04 +02:00
|
|
|
if (tryagain_text)
|
|
|
|
tryagain_text = _(tryagain_text);
|
2006-04-19 13:26:11 +02:00
|
|
|
|
2016-08-08 18:45:29 +02:00
|
|
|
rc = agent_get_passphrase (my_cacheid, tryagain_text, NULL,
|
|
|
|
_("Enter passphrase\n"),
|
|
|
|
repeat, nocache, &pw);
|
2006-04-19 13:26:11 +02:00
|
|
|
|
2007-10-19 17:58:38 +02:00
|
|
|
i18n_switchback (orig_codeset);
|
|
|
|
|
|
|
|
|
2006-10-04 18:45:04 +02:00
|
|
|
if (!rc)
|
|
|
|
;
|
2011-02-02 15:48:54 +01:00
|
|
|
else if (gpg_err_code (rc) == GPG_ERR_CANCELED
|
2010-10-13 17:57:08 +02:00
|
|
|
|| gpg_err_code (rc) == GPG_ERR_FULLY_CANCELED)
|
2006-10-04 18:45:04 +02:00
|
|
|
{
|
|
|
|
log_info (_("cancelled by user\n") );
|
|
|
|
if (canceled)
|
|
|
|
*canceled = 1;
|
|
|
|
}
|
2011-02-02 15:48:54 +01:00
|
|
|
else
|
2007-01-31 15:24:41 +01:00
|
|
|
{
|
|
|
|
log_error (_("problem with the agent: %s\n"), gpg_strerror (rc));
|
|
|
|
/* Due to limitations in the API of the upper layers they
|
|
|
|
consider an error as no passphrase entered. This works in
|
|
|
|
most cases but not during key creation where this should
|
|
|
|
definitely not happen and let it continue without requiring a
|
|
|
|
passphrase. Given that now all the upper layers handle a
|
|
|
|
cancel correctly, we simply set the cancel flag now for all
|
2011-02-02 15:48:54 +01:00
|
|
|
errors from the agent. */
|
2007-01-31 15:24:41 +01:00
|
|
|
if (canceled)
|
|
|
|
*canceled = 1;
|
2008-12-12 13:01:20 +01:00
|
|
|
|
2010-01-08 20:18:49 +01:00
|
|
|
write_status_errcode ("get_passphrase", rc);
|
2007-01-31 15:24:41 +01:00
|
|
|
}
|
2007-10-19 17:58:38 +02:00
|
|
|
|
2006-10-04 18:45:04 +02:00
|
|
|
if (rc)
|
|
|
|
{
|
|
|
|
xfree (pw);
|
2016-08-08 18:45:29 +02:00
|
|
|
pw = NULL;
|
2006-10-04 18:45:04 +02:00
|
|
|
}
|
|
|
|
return pw;
|
2003-06-05 09:14:21 +02:00
|
|
|
}
|
|
|
|
|
2006-04-19 13:26:11 +02:00
|
|
|
|
2003-06-05 09:14:21 +02:00
|
|
|
/*
|
2016-08-08 18:45:29 +02:00
|
|
|
* Clear the cached passphrase with CACHEID.
|
2003-06-05 09:14:21 +02:00
|
|
|
*/
|
|
|
|
void
|
2016-08-08 18:45:29 +02:00
|
|
|
passphrase_clear_cache (const char *cacheid)
|
2003-06-05 09:14:21 +02:00
|
|
|
{
|
2006-10-04 18:45:04 +02:00
|
|
|
int rc;
|
2008-10-20 15:53:23 +02:00
|
|
|
|
2016-08-08 18:45:29 +02:00
|
|
|
rc = agent_clear_passphrase (cacheid);
|
2006-10-04 18:45:04 +02:00
|
|
|
if (rc)
|
|
|
|
log_error (_("problem with the agent: %s\n"), gpg_strerror (rc));
|
2003-06-05 09:14:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-08 18:45:29 +02:00
|
|
|
/* Return a new DEK object using the string-to-key specifier S2K.
|
|
|
|
* Returns NULL if the user canceled the passphrase entry and if
|
|
|
|
* CANCELED is not NULL, sets it to true.
|
|
|
|
*
|
|
|
|
* If CREATE is true a new passphrase sll be created. If NOCACHE is
|
|
|
|
* true the symmetric key caching will not be used. */
|
2003-06-05 09:14:21 +02:00
|
|
|
DEK *
|
2016-08-08 18:45:29 +02:00
|
|
|
passphrase_to_dek (int cipher_algo, STRING2KEY *s2k,
|
|
|
|
int create, int nocache,
|
|
|
|
const char *tryagain_text, int *canceled)
|
2003-06-05 09:14:21 +02:00
|
|
|
{
|
2006-10-04 18:45:04 +02:00
|
|
|
char *pw = NULL;
|
|
|
|
DEK *dek;
|
|
|
|
STRING2KEY help_s2k;
|
2006-12-06 11:16:50 +01:00
|
|
|
int dummy_canceled;
|
2016-08-08 17:42:37 +02:00
|
|
|
char s2k_cacheidbuf[1+16+1];
|
|
|
|
char *s2k_cacheid = NULL;
|
2006-12-06 11:16:50 +01:00
|
|
|
|
|
|
|
if (!canceled)
|
|
|
|
canceled = &dummy_canceled;
|
|
|
|
*canceled = 0;
|
2011-02-02 15:48:54 +01:00
|
|
|
|
2018-04-11 20:35:40 +02:00
|
|
|
if (opt.no_symkey_cache)
|
|
|
|
nocache = 1; /* Force no symmtric key caching. */
|
|
|
|
|
2006-10-04 18:45:04 +02:00
|
|
|
if ( !s2k )
|
|
|
|
{
|
2016-08-08 18:45:29 +02:00
|
|
|
log_assert (create && !nocache);
|
2011-02-02 15:48:54 +01:00
|
|
|
/* This is used for the old rfc1991 mode
|
2006-10-04 18:45:04 +02:00
|
|
|
* Note: This must match the code in encode.c with opt.rfc1991 set */
|
2016-12-08 19:02:56 +01:00
|
|
|
memset (&help_s2k, 0, sizeof (help_s2k));
|
2006-10-04 18:45:04 +02:00
|
|
|
s2k = &help_s2k;
|
|
|
|
s2k->hash_algo = S2K_DIGEST_ALGO;
|
2003-06-05 09:14:21 +02:00
|
|
|
}
|
|
|
|
|
2009-05-18 19:38:34 +02:00
|
|
|
/* Create a new salt or what else to be filled into the s2k for a
|
|
|
|
new key. */
|
2016-08-08 18:45:29 +02:00
|
|
|
if (create && (s2k->mode == 1 || s2k->mode == 3))
|
2009-05-18 19:38:34 +02:00
|
|
|
{
|
|
|
|
gcry_randomize (s2k->salt, 8, GCRY_STRONG_RANDOM);
|
|
|
|
if ( s2k->mode == 3 )
|
2010-01-08 20:18:49 +01:00
|
|
|
{
|
|
|
|
/* We delay the encoding until it is really needed. This is
|
|
|
|
if we are going to dynamically calibrate it, we need to
|
|
|
|
call out to gpg-agent and that should not be done during
|
|
|
|
option processing in main(). */
|
|
|
|
if (!opt.s2k_count)
|
2019-01-26 23:10:38 +01:00
|
|
|
opt.s2k_count = encode_s2k_iterations (agent_get_s2k_count ());
|
2010-01-08 20:18:49 +01:00
|
|
|
s2k->count = opt.s2k_count;
|
|
|
|
}
|
2009-05-18 19:38:34 +02:00
|
|
|
}
|
|
|
|
|
2006-10-04 18:45:04 +02:00
|
|
|
/* If we do not have a passphrase available in NEXT_PW and status
|
|
|
|
information are request, we print them now. */
|
2011-02-02 15:48:54 +01:00
|
|
|
if ( !next_pw && is_status_enabled() )
|
2006-10-04 18:45:04 +02:00
|
|
|
{
|
|
|
|
char buf[50];
|
2011-02-02 15:48:54 +01:00
|
|
|
|
Fix use cases of snprintf.
* agent/call-pinentry.c, agent/call-scd.c, agent/command.c,
build-aux/speedo/w32/g4wihelp.c, common/get-passphrase.c,
dirmngr/dirmngr.c, g10/call-agent.c, g10/cpr.c, g10/keygen.c,
g10/openfile.c, g10/passphrase.c, scd/app-openpgp.c, scd/scdaemon.c,
sm/call-agent.c, sm/call-dirmngr.c, sm/certreqgen.c: Fix assuming C99.
--
Signed-off-by: NIIBE Yutaka <gniibe@fsij.org>
2016-10-21 05:04:46 +02:00
|
|
|
snprintf (buf, sizeof buf, "%d %d %d",
|
2016-08-08 18:45:29 +02:00
|
|
|
cipher_algo, s2k->mode, s2k->hash_algo );
|
|
|
|
write_status_text ( STATUS_NEED_PASSPHRASE_SYM, buf );
|
2003-06-05 09:14:21 +02:00
|
|
|
}
|
|
|
|
|
2011-02-02 15:48:54 +01:00
|
|
|
if ( next_pw )
|
2006-10-04 18:45:04 +02:00
|
|
|
{
|
|
|
|
/* Simply return the passphrase we already have in NEXT_PW. */
|
|
|
|
pw = next_pw;
|
|
|
|
next_pw = NULL;
|
2003-06-05 09:14:21 +02:00
|
|
|
}
|
2011-02-02 15:48:54 +01:00
|
|
|
else if ( have_static_passphrase () )
|
2006-10-04 19:22:24 +02:00
|
|
|
{
|
2006-12-06 11:16:50 +01:00
|
|
|
/* Return the passphrase we have stored in FD_PASSWD. */
|
2006-10-04 19:22:24 +02:00
|
|
|
pw = xmalloc_secure ( strlen(fd_passwd)+1 );
|
|
|
|
strcpy ( pw, fd_passwd );
|
|
|
|
}
|
2011-02-02 15:48:54 +01:00
|
|
|
else
|
2006-10-04 18:45:04 +02:00
|
|
|
{
|
2016-08-08 18:45:29 +02:00
|
|
|
if (!nocache && (s2k->mode == 1 || s2k->mode == 3))
|
2009-05-18 19:38:34 +02:00
|
|
|
{
|
2009-08-20 10:41:15 +02:00
|
|
|
memset (s2k_cacheidbuf, 0, sizeof s2k_cacheidbuf);
|
|
|
|
*s2k_cacheidbuf = 'S';
|
|
|
|
bin2hex (s2k->salt, 8, s2k_cacheidbuf + 1);
|
|
|
|
s2k_cacheid = s2k_cacheidbuf;
|
2009-05-18 19:38:34 +02:00
|
|
|
}
|
|
|
|
|
2015-08-16 18:23:21 +02:00
|
|
|
if (opt.pinentry_mode == PINENTRY_MODE_LOOPBACK)
|
|
|
|
{
|
|
|
|
char buf[32];
|
|
|
|
|
|
|
|
snprintf (buf, sizeof (buf), "%u", 100);
|
|
|
|
write_status_text (STATUS_INQUIRE_MAXLEN, buf);
|
|
|
|
}
|
|
|
|
|
2006-04-19 13:26:11 +02:00
|
|
|
/* Divert to the gpg-agent. */
|
2016-08-08 18:45:29 +02:00
|
|
|
pw = passphrase_get (create && nocache, s2k_cacheid,
|
|
|
|
create? opt.passphrase_repeat : 0,
|
2016-08-08 17:42:37 +02:00
|
|
|
tryagain_text, canceled);
|
2006-12-06 11:16:50 +01:00
|
|
|
if (*canceled)
|
|
|
|
{
|
|
|
|
xfree (pw);
|
2007-04-28 18:04:44 +02:00
|
|
|
write_status( STATUS_MISSING_PASSPHRASE );
|
2006-12-06 11:16:50 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
2003-06-05 09:14:21 +02:00
|
|
|
}
|
2011-02-02 15:48:54 +01:00
|
|
|
|
2006-10-04 18:45:04 +02:00
|
|
|
if ( !pw || !*pw )
|
|
|
|
write_status( STATUS_MISSING_PASSPHRASE );
|
2003-06-05 09:14:21 +02:00
|
|
|
|
2006-10-04 18:45:04 +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(). */
|
|
|
|
dek = xmalloc_secure_clear ( sizeof *dek );
|
|
|
|
dek->algo = cipher_algo;
|
2016-08-08 18:45:29 +02:00
|
|
|
if ( (!pw || !*pw) && create)
|
2006-10-04 18:45:04 +02:00
|
|
|
dek->keylen = 0;
|
|
|
|
else
|
2011-03-10 18:39:34 +01:00
|
|
|
{
|
2013-02-21 20:35:10 +01:00
|
|
|
gpg_error_t err;
|
|
|
|
|
2011-03-10 18:39:34 +01:00
|
|
|
dek->keylen = openpgp_cipher_get_algo_keylen (dek->algo);
|
|
|
|
if (!(dek->keylen > 0 && dek->keylen <= DIM(dek->key)))
|
|
|
|
BUG ();
|
2013-02-21 20:35:10 +01:00
|
|
|
err = gcry_kdf_derive (pw, strlen (pw),
|
|
|
|
s2k->mode == 3? GCRY_KDF_ITERSALTED_S2K :
|
|
|
|
s2k->mode == 1? GCRY_KDF_SALTED_S2K :
|
|
|
|
/* */ GCRY_KDF_SIMPLE_S2K,
|
|
|
|
s2k->hash_algo, s2k->salt, 8,
|
|
|
|
S2K_DECODE_COUNT(s2k->count),
|
|
|
|
dek->keylen, dek->key);
|
|
|
|
if (err)
|
2011-03-10 18:39:34 +01:00
|
|
|
{
|
2013-02-21 20:35:10 +01:00
|
|
|
log_error ("gcry_kdf_derive failed: %s", gpg_strerror (err));
|
2011-03-10 18:39:34 +01:00
|
|
|
xfree (pw);
|
|
|
|
xfree (dek);
|
|
|
|
write_status( STATUS_MISSING_PASSPHRASE );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
2009-08-20 10:41:15 +02:00
|
|
|
if (s2k_cacheid)
|
|
|
|
memcpy (dek->s2k_cacheid, s2k_cacheid, sizeof dek->s2k_cacheid);
|
2006-10-04 18:45:04 +02:00
|
|
|
xfree(last_pw);
|
|
|
|
last_pw = pw;
|
|
|
|
return dek;
|
2003-06-05 09:14:21 +02:00
|
|
|
}
|
2009-05-15 21:26:46 +02:00
|
|
|
|
|
|
|
|
2013-02-07 20:37:58 +01:00
|
|
|
/* Emit the USERID_HINT and the NEED_PASSPHRASE status messages.
|
|
|
|
MAINKEYID may be NULL. */
|
|
|
|
void
|
2017-03-31 20:03:52 +02:00
|
|
|
emit_status_need_passphrase (ctrl_t ctrl,
|
|
|
|
u32 *keyid, u32 *mainkeyid, int pubkey_algo)
|
2013-02-07 20:37:58 +01:00
|
|
|
{
|
|
|
|
char buf[50];
|
|
|
|
char *us;
|
|
|
|
|
2017-03-31 20:03:52 +02:00
|
|
|
us = get_long_user_id_string (ctrl, keyid);
|
2013-02-07 20:37:58 +01:00
|
|
|
write_status_text (STATUS_USERID_HINT, us);
|
|
|
|
xfree (us);
|
|
|
|
|
Fix use cases of snprintf.
* agent/call-pinentry.c, agent/call-scd.c, agent/command.c,
build-aux/speedo/w32/g4wihelp.c, common/get-passphrase.c,
dirmngr/dirmngr.c, g10/call-agent.c, g10/cpr.c, g10/keygen.c,
g10/openfile.c, g10/passphrase.c, scd/app-openpgp.c, scd/scdaemon.c,
sm/call-agent.c, sm/call-dirmngr.c, sm/certreqgen.c: Fix assuming C99.
--
Signed-off-by: NIIBE Yutaka <gniibe@fsij.org>
2016-10-21 05:04:46 +02:00
|
|
|
snprintf (buf, sizeof buf, "%08lX%08lX %08lX%08lX %d 0",
|
2013-02-07 20:37:58 +01:00
|
|
|
(ulong)keyid[0],
|
|
|
|
(ulong)keyid[1],
|
|
|
|
(ulong)(mainkeyid? mainkeyid[0]:keyid[0]),
|
|
|
|
(ulong)(mainkeyid? mainkeyid[1]:keyid[1]),
|
|
|
|
pubkey_algo);
|
|
|
|
|
|
|
|
write_status_text (STATUS_NEED_PASSPHRASE, buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-10-26 11:10:29 +02:00
|
|
|
/* Return an allocated utf-8 string describing the key PK. If ESCAPED
|
2010-09-06 21:57:42 +02:00
|
|
|
is true spaces and control characters are percent or plus escaped.
|
2014-06-25 20:25:28 +02:00
|
|
|
MODE describes the use of the key description; use one of the
|
|
|
|
FORMAT_KEYDESC_ macros. */
|
2010-04-27 16:11:41 +02:00
|
|
|
char *
|
2017-03-31 20:03:52 +02:00
|
|
|
gpg_format_keydesc (ctrl_t ctrl, PKT_public_key *pk, int mode, int escaped)
|
2010-04-27 16:11:41 +02:00
|
|
|
{
|
|
|
|
char *uid;
|
|
|
|
size_t uidlen;
|
|
|
|
const char *algo_name;
|
|
|
|
const char *timestr;
|
|
|
|
char *orig_codeset;
|
|
|
|
char *maink;
|
|
|
|
char *desc;
|
2010-09-06 21:57:42 +02:00
|
|
|
const char *prompt;
|
2014-04-14 14:40:18 +02:00
|
|
|
const char *trailer = "";
|
|
|
|
int is_subkey;
|
2011-02-02 15:48:54 +01:00
|
|
|
|
2014-04-14 14:40:18 +02:00
|
|
|
is_subkey = (pk->main_keyid[0] && pk->main_keyid[1]
|
|
|
|
&& pk->keyid[0] != pk->main_keyid[0]
|
|
|
|
&& pk->keyid[1] != pk->main_keyid[1]);
|
2011-01-06 02:33:17 +01:00
|
|
|
algo_name = openpgp_pk_algo_name (pk->pubkey_algo);
|
2010-04-27 16:11:41 +02:00
|
|
|
timestr = strtimestamp (pk->timestamp);
|
2018-04-12 17:53:17 +02:00
|
|
|
uid = get_user_id (ctrl, is_subkey? pk->main_keyid:pk->keyid, &uidlen, NULL);
|
2010-04-27 16:11:41 +02:00
|
|
|
|
|
|
|
orig_codeset = i18n_switchto_utf8 ();
|
|
|
|
|
2014-04-14 14:40:18 +02:00
|
|
|
if (is_subkey)
|
2010-04-27 16:11:41 +02:00
|
|
|
maink = xtryasprintf (_(" (main key ID %s)"), keystr (pk->main_keyid));
|
|
|
|
else
|
|
|
|
maink = NULL;
|
|
|
|
|
2010-09-06 21:57:42 +02:00
|
|
|
switch (mode)
|
|
|
|
{
|
2014-04-14 14:40:18 +02:00
|
|
|
case FORMAT_KEYDESC_NORMAL:
|
2010-09-06 21:57:42 +02:00
|
|
|
prompt = _("Please enter the passphrase to unlock the"
|
2014-04-14 14:40:18 +02:00
|
|
|
" OpenPGP secret key:");
|
2010-09-06 21:57:42 +02:00
|
|
|
break;
|
2014-04-14 14:40:18 +02:00
|
|
|
case FORMAT_KEYDESC_IMPORT:
|
2010-09-06 21:57:42 +02:00
|
|
|
prompt = _("Please enter the passphrase to import the"
|
2014-04-14 14:40:18 +02:00
|
|
|
" OpenPGP secret key:");
|
|
|
|
break;
|
|
|
|
case FORMAT_KEYDESC_EXPORT:
|
|
|
|
if (is_subkey)
|
|
|
|
prompt = _("Please enter the passphrase to export the"
|
|
|
|
" OpenPGP secret subkey:");
|
|
|
|
else
|
|
|
|
prompt = _("Please enter the passphrase to export the"
|
|
|
|
" OpenPGP secret key:");
|
|
|
|
break;
|
|
|
|
case FORMAT_KEYDESC_DELKEY:
|
|
|
|
if (is_subkey)
|
|
|
|
prompt = _("Do you really want to permanently delete the"
|
|
|
|
" OpenPGP secret subkey key:");
|
|
|
|
else
|
|
|
|
prompt = _("Do you really want to permanently delete the"
|
|
|
|
" OpenPGP secret key:");
|
|
|
|
trailer = "?";
|
2010-09-06 21:57:42 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
prompt = "?";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
desc = xtryasprintf (_("%s\n"
|
2010-04-27 16:11:41 +02:00
|
|
|
"\"%.*s\"\n"
|
|
|
|
"%u-bit %s key, ID %s,\n"
|
2014-04-14 14:40:18 +02:00
|
|
|
"created %s%s.\n%s"),
|
2010-09-06 21:57:42 +02:00
|
|
|
prompt,
|
2010-04-27 16:11:41 +02:00
|
|
|
(int)uidlen, uid,
|
2011-02-02 15:48:54 +01:00
|
|
|
nbits_from_pk (pk), algo_name,
|
2010-04-27 16:11:41 +02:00
|
|
|
keystr (pk->keyid), timestr,
|
2014-04-15 16:40:48 +02:00
|
|
|
maink?maink:"", trailer);
|
2010-04-27 16:11:41 +02:00
|
|
|
xfree (maink);
|
|
|
|
xfree (uid);
|
|
|
|
|
|
|
|
i18n_switchback (orig_codeset);
|
|
|
|
|
|
|
|
if (escaped)
|
|
|
|
{
|
|
|
|
char *tmp = percent_plus_escape (desc);
|
2011-02-02 15:48:54 +01:00
|
|
|
xfree (desc);
|
2010-04-27 16:11:41 +02:00
|
|
|
desc = tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
return desc;
|
|
|
|
}
|