1 /* passphrase.c - Get a passphrase
2 * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004,
3 * 2005, 2006, 2007 Free Software Foundation, Inc.
5 * This file is part of GnuPG.
7 * GnuPG is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
12 * GnuPG is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, see <http://www.gnu.org/licenses/>.
32 #ifdef HAVE_LANGINFO_CODESET
45 #include "call-agent.h"
48 static char *fd_passwd
= NULL
;
49 static char *next_pw
= NULL
;
50 static char *last_pw
= NULL
;
53 /* Hash a passphrase using the supplied s2k. If create is true, create
54 a new salt or what else must be filled into the s2k for a new key.
55 always needs: dek->algo, s2k->mode, s2k->hash_algo. */
57 hash_passphrase ( DEK
*dek
, char *pw
, STRING2KEY
*s2k
, int create
)
62 int pwlen
= strlen(pw
);
64 assert ( s2k
->hash_algo
);
65 dek
->keylen
= gcry_cipher_get_algo_keylen (dek
->algo
);
66 if ( !(dek
->keylen
> 0 && dek
->keylen
<= DIM(dek
->key
)) )
69 if (gcry_md_open (&md
, s2k
->hash_algo
, 1))
71 for (pass
=0; used
< dek
->keylen
; pass
++ )
76 for (i
=0; i
< pass
; i
++ ) /* Preset the hash context. */
77 gcry_md_putc (md
, 0 );
80 if ( s2k
->mode
== 1 || s2k
->mode
== 3 )
85 if ( create
&& !pass
)
87 gcry_randomize (s2k
->salt
, 8, GCRY_STRONG_RANDOM
);
89 s2k
->count
= opt
.s2k_count
;
94 count
= S2K_DECODE_COUNT(s2k
->count
);
99 /* A little bit complicated because we need a ulong for count. */
100 while ( count
> len2
) /* maybe iterated+salted */
102 gcry_md_write ( md
, s2k
->salt
, 8 );
103 gcry_md_write ( md
, pw
, pwlen
);
107 gcry_md_write ( md
, s2k
->salt
, count
);
110 gcry_md_write ( md
, s2k
->salt
, 8 );
112 gcry_md_write ( md
, pw
, count
);
116 gcry_md_write ( md
, pw
, pwlen
);
119 i
= gcry_md_get_algo_dlen ( s2k
->hash_algo
);
120 if ( i
> dek
->keylen
- used
)
121 i
= dek
->keylen
- used
;
123 memcpy (dek
->key
+used
, gcry_md_read (md
, s2k
->hash_algo
), i
);
132 have_static_passphrase()
134 return !!fd_passwd
&& opt
.batch
;
138 * Set the passphrase to be used for the next query and only for the next
142 set_next_passphrase( const char *s
)
148 next_pw
= xmalloc_secure( strlen(s
)+1 );
149 strcpy (next_pw
, s
);
154 * Get the last passphrase used in passphrase_to_dek.
155 * Note: This removes the passphrase from this modules and
156 * the caller must free the result. May return NULL:
159 get_last_passphrase()
166 /* As if we had used the passphrase - make it the last_pw. */
168 next_to_last_passphrase(void)
177 /* Here's an interesting question: since this passphrase was passed in
178 on the command line, is there really any point in using secure
179 memory for it? I'm going with 'yes', since it doesn't hurt, and
180 might help in some small way (swapping). */
183 set_passphrase_from_string(const char *pass
)
186 fd_passwd
= xmalloc_secure(strlen(pass
)+1);
187 strcpy (fd_passwd
, pass
);
192 read_passphrase_from_fd( int fd
)
198 { /* Not used but we have to do a dummy read, so that it won't end
199 up at the begin of the message if the quite usual trick to
200 prepend the passphtrase to the message is used. */
203 while (!(read (fd
, buf
, 1) != 1 || *buf
== '\n' ))
209 for (pw
= NULL
, i
= len
= 100; ; i
++ )
215 pw
= xmalloc_secure( len
);
224 if (read( fd
, pw
+i
, 1) != 1 || pw
[i
] == '\n' )
229 tty_printf("\b\b\b \n" );
237 * Ask the GPG Agent for the passphrase.
238 * Mode 0: Allow cached passphrase
239 * 1: No cached passphrase FIXME: Not really implemented
241 * Note that TRYAGAIN_TEXT must not be translated. If CANCELED is not
242 * NULL, the function does set it to 1 if the user canceled the
243 * operation. If CACHEID is not NULL, it will be used as the cacheID
244 * for the gpg-agent; if is NULL and a key fingerprint can be
245 * computed, this will be used as the cacheid.
248 passphrase_get ( u32
*keyid
, int mode
, const char *cacheid
, int repeat
,
249 const char *tryagain_text
,
250 const char *custom_description
,
251 const char *custom_prompt
, int *canceled
)
256 PKT_public_key
*pk
= xmalloc_clear( sizeof *pk
);
257 byte fpr
[MAX_FINGERPRINT_LEN
];
261 char hexfprbuf
[20*2+1];
262 const char *my_cacheid
;
267 #if MAX_FINGERPRINT_LEN < 20
268 #error agent needs a 20 byte fingerprint
271 memset (fpr
, 0, MAX_FINGERPRINT_LEN
);
272 if( keyid
&& get_pubkey( pk
, keyid
) )
275 free_public_key( pk
);
276 pk
= NULL
; /* oops: no key for some reason */
279 orig_codeset
= i18n_switchto_utf8 ();
281 if (custom_description
)
282 atext
= native_to_utf8 (custom_description
);
283 else if ( !mode
&& pk
&& keyid
)
287 const char *algo_name
= gcry_pk_algo_name ( pk
->pubkey_algo
);
294 #define KEYIDSTRING _(" (main key ID %s)")
296 maink
= xmalloc ( strlen (KEYIDSTRING
) + keystrlen() + 20 );
297 if( keyid
[2] && keyid
[3] && keyid
[0] != keyid
[2]
298 && keyid
[1] != keyid
[3] )
299 sprintf( maink
, KEYIDSTRING
, keystr(&keyid
[2]) );
303 uid
= get_user_id ( keyid
, &uidlen
);
304 timestr
= strtimestamp (pk
->timestamp
);
308 #define PROMPTSTRING _("Please enter the passphrase to unlock the" \
309 " secret key for the OpenPGP certificate:\n" \
311 "%u-bit %s key, ID %s,\n" \
314 atext
= xmalloc ( 100 + strlen (PROMPTSTRING
)
315 + uidlen
+ 15 + strlen(algo_name
) + keystrlen()
316 + strlen (timestr
) + strlen (maink
) );
317 sprintf (atext
, PROMPTSTRING
,
319 nbits_from_pk (pk
), algo_name
, keystr(&keyid
[0]), timestr
,
328 fingerprint_from_pk( pk
, fpr
, &dummy
);
334 atext
= xstrdup ( _("Enter passphrase\n") );
337 if (!mode
&& cacheid
)
338 my_cacheid
= cacheid
;
339 else if (!mode
&& have_fpr
)
340 my_cacheid
= bin2hex (fpr
, 20, hexfprbuf
);
345 tryagain_text
= _(tryagain_text
);
347 my_prompt
= custom_prompt
? native_to_utf8 (custom_prompt
): NULL
;
349 rc
= agent_get_passphrase (my_cacheid
, tryagain_text
, my_prompt
, atext
,
353 xfree (atext
); atext
= NULL
;
355 i18n_switchback (orig_codeset
);
360 else if ( gpg_err_code (rc
) == GPG_ERR_CANCELED
)
362 log_info (_("cancelled by user\n") );
368 log_error (_("problem with the agent: %s\n"), gpg_strerror (rc
));
369 /* Due to limitations in the API of the upper layers they
370 consider an error as no passphrase entered. This works in
371 most cases but not during key creation where this should
372 definitely not happen and let it continue without requiring a
373 passphrase. Given that now all the upper layers handle a
374 cancel correctly, we simply set the cancel flag now for all
375 errors from the agent. */
379 write_status_error ("get_passphrase", rc
);
383 free_public_key( pk
);
394 * Clear the cached passphrase. If CACHEID is not NULL, it will be
395 * used instead of a cache ID derived from KEYID.
398 passphrase_clear_cache ( u32
*keyid
, const char *cacheid
, int algo
)
407 # if MAX_FINGERPRINT_LEN < 20
408 # error agent needs a 20 byte fingerprint
410 byte fpr
[MAX_FINGERPRINT_LEN
];
411 char hexfprbuf
[2*20+1];
414 pk
= xcalloc (1, sizeof *pk
);
415 if ( !keyid
|| get_pubkey( pk
, keyid
) )
417 log_error ("key not found in passphrase_clear_cache\n");
418 free_public_key (pk
);
421 memset (fpr
, 0, MAX_FINGERPRINT_LEN
);
422 fingerprint_from_pk ( pk
, fpr
, &dummy
);
423 bin2hex (fpr
, 20, hexfprbuf
);
424 rc
= agent_clear_passphrase (hexfprbuf
);
425 free_public_key ( pk
);
428 rc
= agent_clear_passphrase (cacheid
);
431 log_error (_("problem with the agent: %s\n"), gpg_strerror (rc
));
436 * Ask for a passphrase and return that string.
439 ask_passphrase (const char *description
,
440 const char *tryagain_text
,
441 const char *promptid
,
443 const char *cacheid
, int *canceled
)
452 if (!opt
.batch
&& description
)
454 if (strchr (description
, '%'))
456 char *tmp
= percent_plus_unescape (description
, 0xff);
458 log_fatal(_("out of core\n"));
459 tty_printf ("\n%s\n", tmp
);
463 tty_printf ("\n%s\n",description
);
466 if (have_static_passphrase ())
468 pw
= xmalloc_secure (strlen(fd_passwd
)+1);
469 strcpy (pw
, fd_passwd
);
472 pw
= passphrase_get (NULL
, 0, cacheid
, 0,
473 tryagain_text
, description
, prompt
,
477 write_status( STATUS_MISSING_PASSPHRASE
);
483 /* Return a new DEK object Using the string-to-key sepcifier S2K. Use
484 KEYID and PUBKEY_ALGO to prompt the user. Returns NULL is the user
485 selected to cancel the passphrase entry and if CANCELED is not
486 NULL, sets it to true.
488 MODE 0: Allow cached passphrase
489 1: Ignore cached passphrase
490 2: Ditto, but change the text to "repeat entry"
493 passphrase_to_dek (u32
*keyid
, int pubkey_algo
,
494 int cipher_algo
, STRING2KEY
*s2k
, int mode
,
495 const char *tryagain_text
, int *canceled
)
503 canceled
= &dummy_canceled
;
508 /* This is used for the old rfc1991 mode
509 * Note: This must match the code in encode.c with opt.rfc1991 set */
512 s2k
->hash_algo
= S2K_DIGEST_ALGO
;
515 /* If we do not have a passphrase available in NEXT_PW and status
516 information are request, we print them now. */
517 if ( !next_pw
&& is_status_enabled() )
526 if ( keyid
[2] && keyid
[3] )
528 used_kid
[0] = keyid
[2];
529 used_kid
[1] = keyid
[3];
533 used_kid
[0] = keyid
[0];
534 used_kid
[1] = keyid
[1];
537 us
= get_long_user_id_string ( keyid
);
538 write_status_text ( STATUS_USERID_HINT
, us
);
541 snprintf (buf
, sizeof buf
-1, "%08lX%08lX %08lX%08lX %d 0",
542 (ulong
)keyid
[0], (ulong
)keyid
[1],
543 (ulong
)used_kid
[0], (ulong
)used_kid
[1],
546 write_status_text ( STATUS_NEED_PASSPHRASE
, buf
);
550 snprintf (buf
, sizeof buf
-1, "%d %d %d",
551 cipher_algo
, s2k
->mode
, s2k
->hash_algo
);
552 write_status_text ( STATUS_NEED_PASSPHRASE_SYM
, buf
);
556 /* If we do have a keyID, we do not have a passphrase available in
557 NEXT_PW, we are not running in batch mode and we do not want to
558 ignore the passphrase cache (mode!=1), print a prompt with
559 information on that key. */
560 if ( keyid
&& !opt
.batch
&& !next_pw
&& mode
!=1 )
562 PKT_public_key
*pk
= xmalloc_clear( sizeof *pk
);
565 p
= get_user_id_native(keyid
);
567 tty_printf (_("You need a passphrase to unlock the secret key for\n"
568 "user: \"%s\"\n"),p
);
571 if ( !get_pubkey( pk
, keyid
) )
573 const char *s
= gcry_pk_algo_name ( pk
->pubkey_algo
);
575 tty_printf (_("%u-bit %s key, ID %s, created %s"),
576 nbits_from_pk( pk
), s
?s
:"?", keystr(keyid
),
577 strtimestamp(pk
->timestamp
) );
578 if ( keyid
[2] && keyid
[3]
579 && keyid
[0] != keyid
[2] && keyid
[1] != keyid
[3] )
581 if ( keystrlen () > 10 )
584 tty_printf (_(" (subkey on main key ID %s)"),
588 tty_printf ( _(" (main key ID %s)"), keystr(&keyid
[2]) );
595 free_public_key( pk
);
600 /* Simply return the passphrase we already have in NEXT_PW. */
604 else if ( have_static_passphrase () )
606 /* Return the passphrase we have stored in FD_PASSWD. */
607 pw
= xmalloc_secure ( strlen(fd_passwd
)+1 );
608 strcpy ( pw
, fd_passwd
);
612 /* Divert to the gpg-agent. */
613 pw
= passphrase_get ( keyid
, mode
== 2, NULL
,
614 mode
== 2? opt
.passwd_repeat
: 0,
615 tryagain_text
, NULL
, NULL
, canceled
);
619 write_status( STATUS_MISSING_PASSPHRASE
);
625 write_status( STATUS_MISSING_PASSPHRASE
);
627 /* Hash the passphrase and store it in a newly allocated DEK object.
628 Keep a copy of the passphrase in LAST_PW for use by
629 get_last_passphrase(). */
630 dek
= xmalloc_secure_clear ( sizeof *dek
);
631 dek
->algo
= cipher_algo
;
632 if ( !*pw
&& mode
== 2 )
635 hash_passphrase( dek
, pw
, s2k
, mode
==2 );