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.
54 Always needs: dek->algo, s2k->mode, s2k->hash_algo. */
56 hash_passphrase ( DEK
*dek
, char *pw
, STRING2KEY
*s2k
)
61 int pwlen
= strlen(pw
);
63 assert ( s2k
->hash_algo
);
64 dek
->keylen
= gcry_cipher_get_algo_keylen (dek
->algo
);
65 if ( !(dek
->keylen
> 0 && dek
->keylen
<= DIM(dek
->key
)) )
68 if (gcry_md_open (&md
, s2k
->hash_algo
, 1))
70 for (pass
=0; used
< dek
->keylen
; pass
++ )
75 for (i
=0; i
< pass
; i
++ ) /* Preset the hash context. */
76 gcry_md_putc (md
, 0 );
79 if ( s2k
->mode
== 1 || s2k
->mode
== 3 )
86 count
= S2K_DECODE_COUNT(s2k
->count
);
91 /* A little bit complicated because we need a ulong for count. */
92 while ( count
> len2
) /* maybe iterated+salted */
94 gcry_md_write ( md
, s2k
->salt
, 8 );
95 gcry_md_write ( md
, pw
, pwlen
);
99 gcry_md_write ( md
, s2k
->salt
, count
);
102 gcry_md_write ( md
, s2k
->salt
, 8 );
104 gcry_md_write ( md
, pw
, count
);
108 gcry_md_write ( md
, pw
, pwlen
);
111 i
= gcry_md_get_algo_dlen ( s2k
->hash_algo
);
112 if ( i
> dek
->keylen
- used
)
113 i
= dek
->keylen
- used
;
115 memcpy (dek
->key
+used
, gcry_md_read (md
, s2k
->hash_algo
), i
);
124 have_static_passphrase()
126 return !!fd_passwd
&& opt
.batch
;
130 * Set the passphrase to be used for the next query and only for the next
134 set_next_passphrase( const char *s
)
140 next_pw
= xmalloc_secure( strlen(s
)+1 );
141 strcpy (next_pw
, s
);
146 * Get the last passphrase used in passphrase_to_dek.
147 * Note: This removes the passphrase from this modules and
148 * the caller must free the result. May return NULL:
151 get_last_passphrase()
158 /* As if we had used the passphrase - make it the last_pw. */
160 next_to_last_passphrase(void)
169 /* Here's an interesting question: since this passphrase was passed in
170 on the command line, is there really any point in using secure
171 memory for it? I'm going with 'yes', since it doesn't hurt, and
172 might help in some small way (swapping). */
175 set_passphrase_from_string(const char *pass
)
178 fd_passwd
= xmalloc_secure(strlen(pass
)+1);
179 strcpy (fd_passwd
, pass
);
184 read_passphrase_from_fd( int fd
)
190 { /* Not used but we have to do a dummy read, so that it won't end
191 up at the begin of the message if the quite usual trick to
192 prepend the passphtrase to the message is used. */
195 while (!(read (fd
, buf
, 1) != 1 || *buf
== '\n' ))
201 for (pw
= NULL
, i
= len
= 100; ; i
++ )
207 pw
= xmalloc_secure( len
);
216 if (read( fd
, pw
+i
, 1) != 1 || pw
[i
] == '\n' )
221 tty_printf("\b\b\b \n" );
229 * Ask the GPG Agent for the passphrase.
230 * Mode 0: Allow cached passphrase
231 * 1: No cached passphrase; that is we are asking for a new passphrase
232 * FIXME: Only partially implemented
234 * Note that TRYAGAIN_TEXT must not be translated. If CANCELED is not
235 * NULL, the function does set it to 1 if the user canceled the
236 * operation. If CACHEID is not NULL, it will be used as the cacheID
237 * for the gpg-agent; if is NULL and a key fingerprint can be
238 * computed, this will be used as the cacheid.
241 passphrase_get ( u32
*keyid
, int mode
, const char *cacheid
, int repeat
,
242 const char *tryagain_text
,
243 const char *custom_description
,
244 const char *custom_prompt
, int *canceled
)
249 PKT_public_key
*pk
= xmalloc_clear( sizeof *pk
);
250 byte fpr
[MAX_FINGERPRINT_LEN
];
254 char hexfprbuf
[20*2+1];
255 const char *my_cacheid
;
256 int check
= (mode
== 1);
261 #if MAX_FINGERPRINT_LEN < 20
262 #error agent needs a 20 byte fingerprint
265 memset (fpr
, 0, MAX_FINGERPRINT_LEN
);
266 if( keyid
&& get_pubkey( pk
, keyid
) )
269 free_public_key( pk
);
270 pk
= NULL
; /* oops: no key for some reason */
273 orig_codeset
= i18n_switchto_utf8 ();
275 if (custom_description
)
276 atext
= native_to_utf8 (custom_description
);
277 else if ( !mode
&& pk
&& keyid
)
281 const char *algo_name
= gcry_pk_algo_name ( pk
->pubkey_algo
);
288 #define KEYIDSTRING _(" (main key ID %s)")
290 maink
= xmalloc ( strlen (KEYIDSTRING
) + keystrlen() + 20 );
291 if( keyid
[2] && keyid
[3] && keyid
[0] != keyid
[2]
292 && keyid
[1] != keyid
[3] )
293 sprintf( maink
, KEYIDSTRING
, keystr(&keyid
[2]) );
297 uid
= get_user_id ( keyid
, &uidlen
);
298 timestr
= strtimestamp (pk
->timestamp
);
302 #define PROMPTSTRING _("Please enter the passphrase to unlock the" \
303 " secret key for the OpenPGP certificate:\n" \
305 "%u-bit %s key, ID %s,\n" \
308 atext
= xmalloc ( 100 + strlen (PROMPTSTRING
)
309 + uidlen
+ 15 + strlen(algo_name
) + keystrlen()
310 + strlen (timestr
) + strlen (maink
) );
311 sprintf (atext
, PROMPTSTRING
,
313 nbits_from_pk (pk
), algo_name
, keystr(&keyid
[0]), timestr
,
322 fingerprint_from_pk( pk
, fpr
, &dummy
);
328 atext
= xstrdup ( _("Enter passphrase\n") );
331 if (!mode
&& cacheid
)
332 my_cacheid
= cacheid
;
333 else if (!mode
&& have_fpr
)
334 my_cacheid
= bin2hex (fpr
, 20, hexfprbuf
);
339 tryagain_text
= _(tryagain_text
);
341 my_prompt
= custom_prompt
? native_to_utf8 (custom_prompt
): NULL
;
343 rc
= agent_get_passphrase (my_cacheid
, tryagain_text
, my_prompt
, atext
,
347 xfree (atext
); atext
= NULL
;
349 i18n_switchback (orig_codeset
);
354 else if ( gpg_err_code (rc
) == GPG_ERR_CANCELED
)
356 log_info (_("cancelled by user\n") );
362 log_error (_("problem with the agent: %s\n"), gpg_strerror (rc
));
363 /* Due to limitations in the API of the upper layers they
364 consider an error as no passphrase entered. This works in
365 most cases but not during key creation where this should
366 definitely not happen and let it continue without requiring a
367 passphrase. Given that now all the upper layers handle a
368 cancel correctly, we simply set the cancel flag now for all
369 errors from the agent. */
373 write_status_error ("get_passphrase", rc
);
377 free_public_key( pk
);
388 * Clear the cached passphrase. If CACHEID is not NULL, it will be
389 * used instead of a cache ID derived from KEYID.
392 passphrase_clear_cache ( u32
*keyid
, const char *cacheid
, int algo
)
401 # if MAX_FINGERPRINT_LEN < 20
402 # error agent needs a 20 byte fingerprint
404 byte fpr
[MAX_FINGERPRINT_LEN
];
405 char hexfprbuf
[2*20+1];
408 pk
= xcalloc (1, sizeof *pk
);
409 if ( !keyid
|| get_pubkey( pk
, keyid
) )
411 log_error ("key not found in passphrase_clear_cache\n");
412 free_public_key (pk
);
415 memset (fpr
, 0, MAX_FINGERPRINT_LEN
);
416 fingerprint_from_pk ( pk
, fpr
, &dummy
);
417 bin2hex (fpr
, 20, hexfprbuf
);
418 rc
= agent_clear_passphrase (hexfprbuf
);
419 free_public_key ( pk
);
422 rc
= agent_clear_passphrase (cacheid
);
425 log_error (_("problem with the agent: %s\n"), gpg_strerror (rc
));
429 /* Return a new DEK object Using the string-to-key sepcifier S2K. Use
430 KEYID and PUBKEY_ALGO to prompt the user. Returns NULL is the user
431 selected to cancel the passphrase entry and if CANCELED is not
432 NULL, sets it to true.
434 MODE 0: Allow cached passphrase
435 1: Ignore cached passphrase
436 2: Ditto, but create a new key
437 3: Allow cached passphrase; use the S2K salt as the cache ID
438 4: Ditto, but create a new key
441 passphrase_to_dek_ext (u32
*keyid
, int pubkey_algo
,
442 int cipher_algo
, STRING2KEY
*s2k
, int mode
,
443 const char *tryagain_text
,
444 const char *custdesc
, const char *custprompt
,
453 canceled
= &dummy_canceled
;
458 assert (mode
!= 3 && mode
!= 4);
459 /* This is used for the old rfc1991 mode
460 * Note: This must match the code in encode.c with opt.rfc1991 set */
463 s2k
->hash_algo
= S2K_DIGEST_ALGO
;
466 /* Create a new salt or what else to be filled into the s2k for a
468 if ((mode
== 2 || mode
== 4) && (s2k
->mode
== 1 || s2k
->mode
== 3))
470 gcry_randomize (s2k
->salt
, 8, GCRY_STRONG_RANDOM
);
471 if ( s2k
->mode
== 3 )
472 s2k
->count
= opt
.s2k_count
;
475 /* If we do not have a passphrase available in NEXT_PW and status
476 information are request, we print them now. */
477 if ( !next_pw
&& is_status_enabled() )
486 if ( keyid
[2] && keyid
[3] )
488 used_kid
[0] = keyid
[2];
489 used_kid
[1] = keyid
[3];
493 used_kid
[0] = keyid
[0];
494 used_kid
[1] = keyid
[1];
497 us
= get_long_user_id_string ( keyid
);
498 write_status_text ( STATUS_USERID_HINT
, us
);
501 snprintf (buf
, sizeof buf
-1, "%08lX%08lX %08lX%08lX %d 0",
502 (ulong
)keyid
[0], (ulong
)keyid
[1],
503 (ulong
)used_kid
[0], (ulong
)used_kid
[1],
506 write_status_text ( STATUS_NEED_PASSPHRASE
, buf
);
510 snprintf (buf
, sizeof buf
-1, "%d %d %d",
511 cipher_algo
, s2k
->mode
, s2k
->hash_algo
);
512 write_status_text ( STATUS_NEED_PASSPHRASE_SYM
, buf
);
516 /* If we do have a keyID, we do not have a passphrase available in
517 NEXT_PW, we are not running in batch mode and we do not want to
518 ignore the passphrase cache (mode!=1), print a prompt with
519 information on that key. */
520 if ( keyid
&& !opt
.batch
&& !next_pw
&& mode
!=1 )
522 PKT_public_key
*pk
= xmalloc_clear( sizeof *pk
);
525 p
= get_user_id_native(keyid
);
527 tty_printf (_("You need a passphrase to unlock the secret key for\n"
528 "user: \"%s\"\n"),p
);
531 if ( !get_pubkey( pk
, keyid
) )
533 const char *s
= gcry_pk_algo_name ( pk
->pubkey_algo
);
535 tty_printf (_("%u-bit %s key, ID %s, created %s"),
536 nbits_from_pk( pk
), s
?s
:"?", keystr(keyid
),
537 strtimestamp(pk
->timestamp
) );
538 if ( keyid
[2] && keyid
[3]
539 && keyid
[0] != keyid
[2] && keyid
[1] != keyid
[3] )
541 if ( keystrlen () > 10 )
544 tty_printf (_(" (subkey on main key ID %s)"),
548 tty_printf ( _(" (main key ID %s)"), keystr(&keyid
[2]) );
555 free_public_key( pk
);
560 /* Simply return the passphrase we already have in NEXT_PW. */
564 else if ( have_static_passphrase () )
566 /* Return the passphrase we have stored in FD_PASSWD. */
567 pw
= xmalloc_secure ( strlen(fd_passwd
)+1 );
568 strcpy ( pw
, fd_passwd
);
572 char *cacheid
= NULL
;
575 if ((mode
== 3 || mode
== 4) && (s2k
->mode
== 1 || s2k
->mode
== 3))
577 memset (buf
, 0, sizeof buf
);
579 bin2hex (s2k
->salt
, 8, buf
+ 1);
583 /* Divert to the gpg-agent. */
584 pw
= passphrase_get (keyid
, mode
== 2, cacheid
,
585 (mode
== 2 || mode
== 4)? opt
.passwd_repeat
: 0,
586 tryagain_text
, custdesc
, custprompt
, canceled
);
590 write_status( STATUS_MISSING_PASSPHRASE
);
596 write_status( STATUS_MISSING_PASSPHRASE
);
598 /* Hash the passphrase and store it in a newly allocated DEK object.
599 Keep a copy of the passphrase in LAST_PW for use by
600 get_last_passphrase(). */
601 dek
= xmalloc_secure_clear ( sizeof *dek
);
602 dek
->algo
= cipher_algo
;
603 if ( !*pw
&& (mode
== 2 || mode
== 4))
606 hash_passphrase (dek
, pw
, s2k
);
614 passphrase_to_dek (u32
*keyid
, int pubkey_algo
,
615 int cipher_algo
, STRING2KEY
*s2k
, int mode
,
616 const char *tryagain_text
, int *canceled
)
618 return passphrase_to_dek_ext (keyid
, pubkey_algo
, cipher_algo
,
619 s2k
, mode
, tryagain_text
, NULL
, NULL
,