1997-11-18 14:06:00 +00:00
|
|
|
/* passphrase.c - Get a passphrase
|
2000-05-12 11:59:49 +00:00
|
|
|
* Copyright (C) 1998, 1999, 2000 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>
|
|
|
|
#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>
|
|
|
|
#include "util.h"
|
|
|
|
#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"
|
|
|
|
#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"
|
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
|
|
|
|
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
|
|
|
{
|
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 )
|
|
|
|
{
|
|
|
|
m_free(next_pw);
|
|
|
|
next_pw = NULL;
|
|
|
|
if( s ) {
|
|
|
|
next_pw = m_alloc_secure( strlen(s)+1 );
|
|
|
|
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 )
|
|
|
|
{
|
|
|
|
int i, len;
|
|
|
|
char *pw;
|
|
|
|
|
|
|
|
if( !opt.batch )
|
|
|
|
tty_printf("Reading passphrase from file descriptor %d ...", fd );
|
|
|
|
for( pw = NULL, i = len = 100; ; i++ ) {
|
|
|
|
if( i >= len-1 ) {
|
|
|
|
char *pw2 = pw;
|
|
|
|
len += 100;
|
|
|
|
pw = m_alloc_secure( len );
|
|
|
|
if( pw2 )
|
|
|
|
memcpy(pw, pw2, i );
|
|
|
|
else
|
|
|
|
i=0;
|
|
|
|
}
|
|
|
|
if( read( fd, pw+i, 1) != 1 || pw[i] == '\n' )
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
pw[i] = 0;
|
|
|
|
if( !opt.batch )
|
|
|
|
tty_printf("\b\b\b \n" );
|
|
|
|
|
|
|
|
m_free( fd_passwd );
|
|
|
|
fd_passwd = pw;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-11-18 14:06:00 +00:00
|
|
|
/****************
|
|
|
|
* Get a passphrase for the secret key with KEYID, display TEXT
|
|
|
|
* if the user needs to enter the passphrase.
|
1998-05-04 18:49:26 +00:00
|
|
|
* mode 0 = standard, 2 = create new passphrase
|
|
|
|
* Returns: a DEK with a session key; caller must free
|
|
|
|
* or NULL if the passphrase was not correctly repeated.
|
|
|
|
* (only for mode 2)
|
|
|
|
* a dek->keylen of 0 means: no passphrase entered.
|
|
|
|
* (only for mode 2)
|
1999-07-01 10:53:35 +00:00
|
|
|
* pubkey_algo is only informational.
|
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,
|
|
|
|
int cipher_algo, STRING2KEY *s2k, int mode )
|
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;
|
|
|
|
|
|
|
|
if( !s2k ) {
|
|
|
|
s2k = &help_s2k;
|
|
|
|
s2k->mode = 0;
|
|
|
|
/* this should be MD5 if cipher is IDEA, but because we do
|
1998-05-15 18:49:19 +00:00
|
|
|
* not have IDEA, we use the default one, the user
|
1998-05-04 18:49:26 +00:00
|
|
|
* can select it from the commandline
|
|
|
|
*/
|
1998-05-15 18:49:19 +00:00
|
|
|
s2k->hash_algo = opt.def_digest_algo?opt.def_digest_algo
|
|
|
|
:DEFAULT_DIGEST_ALGO;
|
1998-05-04 18:49:26 +00:00
|
|
|
}
|
1997-11-18 14:06:00 +00:00
|
|
|
|
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];
|
1999-04-08 07:41:35 +00:00
|
|
|
if( keyid ) {
|
|
|
|
sprintf( buf, "%08lX%08lX", (ulong)keyid[0], (ulong)keyid[1] );
|
|
|
|
if( keyid[2] && keyid[3] && keyid[0] != keyid[2]
|
|
|
|
&& keyid[1] != keyid[3] )
|
1999-07-01 10:53:35 +00:00
|
|
|
sprintf( buf+strlen(buf), " %08lX%08lX %d 0",
|
|
|
|
(ulong)keyid[2], (ulong)keyid[3], 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
|
|
|
}
|
|
|
|
|
1998-05-26 13:38:00 +00:00
|
|
|
if( keyid && !opt.batch && !next_pw ) {
|
1998-06-29 12:30:57 +00:00
|
|
|
PKT_public_key *pk = m_alloc_clear( sizeof *pk );
|
1998-05-29 11:53:54 +00:00
|
|
|
size_t n;
|
|
|
|
char *p;
|
1997-12-20 17:23:29 +00:00
|
|
|
|
1998-05-29 11:53:54 +00:00
|
|
|
tty_printf(_("\nYou need a passphrase to unlock the secret key for\n"
|
|
|
|
"user: \"") );
|
|
|
|
p = get_user_id( keyid, &n );
|
1999-09-23 17:26:15 +00:00
|
|
|
tty_print_utf8_string( p, n );
|
1998-05-29 11:53:54 +00:00
|
|
|
m_free(p);
|
|
|
|
tty_printf("\"\n");
|
|
|
|
|
1998-06-29 12:30:57 +00:00
|
|
|
if( !get_pubkey( pk, keyid ) ) {
|
|
|
|
const char *s = pubkey_algo_to_string( pk->pubkey_algo );
|
1999-01-07 17:05:48 +00:00
|
|
|
tty_printf( _("%u-bit %s key, ID %08lX, created %s"),
|
1998-06-29 12:30:57 +00:00
|
|
|
nbits_from_pk( pk ), s?s:"?", (ulong)keyid[1],
|
|
|
|
strtimestamp(pk->timestamp) );
|
1999-01-07 17:05:48 +00:00
|
|
|
if( keyid[2] && keyid[3] && keyid[0] != keyid[2]
|
|
|
|
&& keyid[1] != keyid[3] )
|
|
|
|
tty_printf( _(" (main key ID %08lX)"), (ulong)keyid[3] );
|
|
|
|
tty_printf("\n");
|
1998-05-29 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
|
|
|
|
1998-05-26 13:38:00 +00:00
|
|
|
if( next_pw ) {
|
|
|
|
pw = next_pw;
|
|
|
|
next_pw = NULL;
|
|
|
|
}
|
1998-09-28 19:25:31 +00:00
|
|
|
else if( fd_passwd ) {
|
|
|
|
pw = m_alloc_secure( strlen(fd_passwd)+1 );
|
|
|
|
strcpy( pw, fd_passwd );
|
1997-11-18 14:06:00 +00:00
|
|
|
}
|
1999-06-15 12:31:07 +00:00
|
|
|
else if( opt.batch ) {
|
1999-07-12 12:57:54 +00:00
|
|
|
log_error(_("can't query password in batchmode\n"));
|
|
|
|
pw = m_strdup( "" ); /* return an empty passphrase */
|
1999-06-15 12:31:07 +00:00
|
|
|
}
|
1998-01-02 20:40:10 +00:00
|
|
|
else {
|
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) ) {
|
|
|
|
m_free(pw2);
|
|
|
|
m_free(pw);
|
|
|
|
return NULL;
|
|
|
|
}
|
1997-11-18 14:06:00 +00:00
|
|
|
m_free(pw2);
|
|
|
|
}
|
|
|
|
}
|
1999-04-09 10:34:44 +00:00
|
|
|
|
|
|
|
if( !pw || !*pw )
|
|
|
|
write_status( STATUS_MISSING_PASSPHRASE );
|
|
|
|
|
1998-05-04 18:49:26 +00:00
|
|
|
dek = m_alloc_secure( sizeof *dek );
|
|
|
|
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 );
|
1998-05-26 13:38:00 +00:00
|
|
|
m_free(last_pw);
|
|
|
|
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
|
|
|
{
|
1998-05-03 15:42:08 +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 );
|
1998-09-28 19:25:31 +00:00
|
|
|
dek->keylen = cipher_get_keylen( dek->algo ) / 8;
|
|
|
|
if( !(dek->keylen > 0 && dek->keylen <= DIM(dek->key)) )
|
|
|
|
BUG();
|
|
|
|
|
1998-05-03 15:42:08 +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 ) {
|
1999-10-04 19:27:02 +00:00
|
|
|
if( (opt.emulate_bugs & EMUBUG_3DESS2K)) {
|
|
|
|
int tmp = md->finalized;
|
|
|
|
md_reset( md );
|
|
|
|
md->finalized = tmp;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
md_reset(md);
|
1998-09-28 19:25:31 +00:00
|
|
|
for(i=0; i < pass; i++ ) /* preset the hash context */
|
|
|
|
md_putc(md, 0 );
|
|
|
|
}
|
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 ) {
|
|
|
|
randomize_buffer(s2k->salt, 8, 1);
|
|
|
|
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 */
|
|
|
|
md_write( md, s2k->salt, 8 );
|
|
|
|
md_write( md, pw, pwlen );
|
|
|
|
count -= len2;
|
|
|
|
}
|
|
|
|
if( count < 8 )
|
|
|
|
md_write( md, s2k->salt, count );
|
1998-08-05 18:30:53 +00:00
|
|
|
else {
|
|
|
|
md_write( md, s2k->salt, 8 );
|
|
|
|
count -= 8;
|
1998-09-28 19:25:31 +00:00
|
|
|
assert( count >= 0 );
|
1998-08-05 18:30:53 +00:00
|
|
|
md_write( md, pw, count );
|
|
|
|
}
|
|
|
|
}
|
1998-09-28 19:25:31 +00:00
|
|
|
else
|
|
|
|
md_write( md, pw, pwlen );
|
|
|
|
md_final( md );
|
|
|
|
i = md_digest_length( s2k->hash_algo );
|
|
|
|
if( i > dek->keylen - used )
|
|
|
|
i = dek->keylen - used;
|
|
|
|
memcpy( dek->key+used, md_read(md, s2k->hash_algo), i );
|
|
|
|
used += i;
|
1998-04-04 20:16:55 +00:00
|
|
|
}
|
1998-05-03 15:42:08 +00:00
|
|
|
md_close(md);
|
1997-11-18 14:06:00 +00:00
|
|
|
}
|
|
|
|
|