1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
|
/* passphrase.c - Get a passphrase
* Copyright (C) 1998, 1999, 2000 Free Software Foundation, Inc.
*
* 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
* the Free Software Foundation; either version 2 of the License, or
* (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
* 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>
#include <unistd.h>
#include <assert.h>
#include <gcrypt.h>
#include "util.h"
#include "options.h"
#include "ttyio.h"
#include "keydb.h"
#include "main.h"
#include "i18n.h"
#include "status.h"
static char *fd_passwd = NULL;
static char *next_pw = NULL;
static char *last_pw = NULL;
static void hash_passphrase( DEK *dek, char *pw, STRING2KEY *s2k, int create );
int
have_static_passphrase()
{
return !!fd_passwd;
}
/****************
* Set the passphrase to be used for the next query and only for the next
* one.
*/
void
set_next_passphrase( const char *s )
{
gcry_free(next_pw);
next_pw = NULL;
if( s ) {
next_pw = gcry_xmalloc_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;
}
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 = gcry_xmalloc_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" );
gcry_free( fd_passwd );
fd_passwd = pw;
}
/****************
* Get a passphrase for the secret key with KEYID, display TEXT
* if the user needs to enter the passphrase.
* 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)
* pubkey_algo is only informational.
*/
DEK *
passphrase_to_dek( u32 *keyid, int pubkey_algo,
int cipher_algo, STRING2KEY *s2k, int mode )
{
char *pw = NULL;
DEK *dek;
STRING2KEY help_s2k;
if( !s2k ) {
s2k = &help_s2k;
s2k->mode = 0;
/* this should be MD5 if cipher is IDEA, but because we do
* not have IDEA, we use the default one, the user
* can select it from the commandline
*/
s2k->hash_algo = opt.def_digest_algo?opt.def_digest_algo
:DEFAULT_DIGEST_ALGO;
}
if( !next_pw && is_status_enabled() ) {
char buf[50];
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] )
sprintf( buf+strlen(buf), " %08lX%08lX %d 0",
(ulong)keyid[2], (ulong)keyid[3], pubkey_algo );
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 );
}
}
if( keyid && !opt.batch && !next_pw ) {
PKT_public_key *pk = gcry_xcalloc( 1, sizeof *pk );
size_t n;
char *p;
tty_printf(_("\nYou need a passphrase to unlock the secret key for\n"
"user: \"") );
p = get_user_id( keyid, &n );
tty_print_utf8_string( p, n );
gcry_free(p);
tty_printf("\"\n");
if( !get_pubkey( pk, keyid ) ) {
const char *s = gcry_pk_algo_name( pk->pubkey_algo );
tty_printf( _("%u-bit %s key, ID %08lX, created %s"),
nbits_from_pk( pk ), s?s:"?", (ulong)keyid[1],
strtimestamp(pk->timestamp) );
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");
}
tty_printf("\n");
free_public_key( pk );
}
if( next_pw ) {
pw = next_pw;
next_pw = NULL;
}
else if( fd_passwd ) {
pw = gcry_xmalloc_secure( strlen(fd_passwd)+1 );
strcpy( pw, fd_passwd );
}
else if( opt.batch ) {
log_error(_("can't query password in batchmode\n"));
pw = gcry_xstrdup( "" ); /* return an empty passphrase */
}
else {
pw = cpr_get_hidden("passphrase.enter", _("Enter passphrase: ") );
tty_kill_prompt();
if( mode == 2 && !cpr_enabled() ) {
char *pw2 = cpr_get_hidden("passphrase.repeat",
_("Repeat passphrase: ") );
tty_kill_prompt();
if( strcmp(pw, pw2) ) {
gcry_free(pw2);
gcry_free(pw);
return NULL;
}
gcry_free(pw2);
}
}
if( !pw || !*pw )
write_status( STATUS_MISSING_PASSPHRASE );
dek = gcry_xmalloc_secure( sizeof *dek );
dek->algo = cipher_algo;
if( !*pw && mode == 2 )
dek->keylen = 0;
else
hash_passphrase( dek, pw, s2k, mode==2 );
gcry_free(last_pw);
last_pw = pw;
return dek;
}
/****************
* Hash a passphrase using the supplied s2k. If create is true, create
* a new salt or what else must be filled into the s2k for a new key.
* always needs: dek->algo, s2k->mode, s2k->hash_algo.
*/
static void
hash_passphrase( DEK *dek, char *pw, STRING2KEY *s2k, int create )
{
GCRY_MD_HD md;
int pass, i;
int used = 0;
int pwlen = strlen(pw);
assert( s2k->hash_algo );
dek->keylen = gcry_cipher_get_algo_keylen( dek->algo );
if( !(dek->keylen > 0 && dek->keylen <= DIM(dek->key)) )
BUG();
if( !(md = gcry_md_open( s2k->hash_algo, GCRY_MD_FLAG_SECURE )) )
BUG();
for(pass=0; used < dek->keylen ; pass++ ) {
if( pass ) {
gcry_md_reset(md);
for(i=0; i < pass; i++ ) /* preset the hash context */
gcry_md_putc(md, 0 );
}
if( s2k->mode == 1 || s2k->mode == 3 ) {
int len2 = pwlen + 8;
ulong count = len2;
if( create && !pass ) {
gcry_randomize(s2k->salt, 8, GCRY_STRONG_RANDOM );
if( s2k->mode == 3 )
s2k->count = 96; /* 65536 iterations */
}
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 */
gcry_md_write( md, s2k->salt, 8 );
gcry_md_write( md, pw, pwlen );
count -= len2;
}
if( count < 8 )
gcry_md_write( md, s2k->salt, count );
else {
gcry_md_write( md, s2k->salt, 8 );
count -= 8;
assert( count >= 0 );
gcry_md_write( md, pw, count );
}
}
else
gcry_md_write( md, pw, pwlen );
gcry_md_final( md );
i = gcry_md_get_algo_dlen( s2k->hash_algo );
if( i > dek->keylen - used )
i = dek->keylen - used;
memcpy( dek->key+used, gcry_md_read(md, s2k->hash_algo), i );
used += i;
}
gcry_md_close(md);
}
|